settings = get_option('alp_settings'); add_action('init', array($this,'init')); } function __destruct(){ return; } function init(){ $this->plugin_localization(); // Check if WPML is active. If not display warning message and not load Sticky links if(!defined('ICL_SITEPRESS_VERSION') || ICL_PLUGIN_INACTIVE){ if ( !function_exists('is_multisite') || !is_multisite() ) { add_action('admin_notices', array($this, '_no_wpml_warning')); } return false; }elseif(version_compare(ICL_SITEPRESS_VERSION, '2.0.5', '<')){ add_action('admin_notices', array($this, '_old_wpml_warning')); return false; } require_once ICL_PLUGIN_PATH . '/inc/absolute-links/absolute-links.class.php'; $this->absolute_links_object = new AbsoluteLinks; global $sitepress_settings; if(isset($_POST['save_alp']) && $_POST['save_alp']){ // $this->settings = $_POST; // $this->save_settings(); // TBD! } $this->ajax_responses(); add_action('save_post', array($this,'save_default_urls'), 10, 2); add_action('admin_head',array($this,'js_scripts')); add_filter('the_content', array($this,'show_permalinks')); if($this->settings['sticky_links_widgets'] || $this->settings['sticky_links_strings']){ add_filter('widget_text', array($this,'show_permalinks'), 99); // low priority - allow translation to be set } if($this->settings['sticky_links_widgets']){ add_filter('pre_update_option_widget_text', array($this,'pre_update_option_widget_text'), 5, 2); } if(empty($this->settings) && !empty($sitepress_settings['modules']['absolute-links'])){ $this->settings = $sitepress_settings['modules']['absolute-links']; $this->save_settings(); } add_action('admin_menu', array($this, 'menu')); add_filter('plugin_action_links', array($this, 'plugin_action_links'), 10, 2); if(is_admin() && !defined('DOING_AJAX')){ wp_enqueue_script('wpml-sticky-links-js', WPML_STICKY_LINKS_URL . '/res/js/scripts.js', array(), WPML_STICKY_LINKS_VERSION); } add_action('wp_ajax_wpml_sticky_links_save_options', array($this, '_save_options')); // add message to WPML dashboard widget add_action('icl_dashboard_widget_content', array($this, 'icl_dashboard_widget_content')); } function _no_wpml_warning(){ ?>
settings['sticky_links_widgets'] = intval($_POST['icl_sticky_links_widgets']); $this->settings['sticky_links_strings'] = intval($_POST['icl_sticky_links_strings']); $this->save_settings(); } } function save_settings(){ update_option('alp_settings', $this->settings); } function ajax_responses(){ if(!isset($_POST['alp_ajx_action'])){ return; } global $wpdb; $post_types = array(); foreach($GLOBALS['wp_post_types'] as $key=>$val){ if($val->public && !in_array($key, array('attachment'))){ $post_types[] = $key; } } $limit = 5; switch($_POST['alp_ajx_action']){ case 'rescan': $posts_pages = $wpdb->get_col(" SELECT SQL_CALC_FOUND_ROWS p1.ID FROM {$wpdb->posts} p1 WHERE p1.post_type IN ('".join("','", $post_types)."') AND p1.post_status NOT IN ('auto-draft') AND p1.ID NOT IN ( SELECT m.post_id FROM {$wpdb->postmeta} m JOIN {$wpdb->posts} p2 ON p2.ID = m.post_id WHERE m.meta_key = '_alp_processed' AND p2.post_type IN ('" . join("','", $post_types) . "') AND p2.post_status NOT IN ('auto-draft') ) ORDER BY p1.ID ASC LIMIT $limit "); if($posts_pages){ $found = $wpdb->get_var("SELECT FOUND_ROWS()"); foreach($posts_pages as $ppid){ $this->absolute_links_object->process_post($ppid); } echo $found >= $limit ? $found - $limit : 0; }else{ echo -1; } break; case 'rescan_reset': $wpdb->query("DELETE FROM {$wpdb->postmeta} WHERE meta_key='_alp_processed'"); echo mysql_affected_rows(); break; case 'use_suggestion': $broken_links = get_post_meta($_POST['post_id'],'_alp_broken_links', true); foreach($broken_links as $k=>$bl){ if($k==$_POST['orig_url']){ $broken = $k; $repl = $bl['suggestions'][$_POST['sug_id']]['absolute']; unset($broken_links[$k]); $c = count($broken_links); if($c){ update_post_meta($_POST['post_id'],'_alp_broken_links', $broken_links); }else{ delete_post_meta($_POST['post_id'],'_alp_broken_links'); } echo $c.'|'.$bl['suggestions'][$_POST['sug_id']]['perma']; break; } } if(!empty($broken)){ $post_content = $wpdb->get_var("SELECT post_content FROM {$wpdb->posts} WHERE ID={$_POST['post_id']}"); $post_content = preg_replace('@href="('.$broken.')"@i', 'href="'.$repl.'"', $post_content); $wpdb->update($wpdb->posts, array('post_content'=>$post_content), array('ID'=>$_POST['post_id'])); } break; case 'alp_revert_urls': $posts_pages = $wpdb->get_results(" SELECT SQL_CALC_FOUND_ROWS p.ID, p.post_content FROM {$wpdb->posts} p JOIN {$wpdb->postmeta} m ON p.ID = m.post_id WHERE m.meta_key = '_alp_processed' AND p.post_type IN ('" . join("','", $post_types) . "') AND p.post_status NOT IN ('auto-draft') ORDER BY p.ID ASC LIMIT $limit "); if($posts_pages){ $found = $wpdb->get_var("SELECT FOUND_ROWS()"); foreach($posts_pages as $p){ $cont = $this->show_permalinks($p->post_content); $wpdb->update($wpdb->posts, array('post_content'=>$cont), array('ID'=>$p->ID)); delete_post_meta($p->ID,'_alp_processed'); delete_post_meta($p->ID,'_alp_broken_links'); } echo $found >= $limit ? $found - $limit : 0; }else{ echo -1; } break; } exit; } function js_scripts(){ ?> $val){ if($val->public && !in_array($key, array('attachment'))){ $types[] = $key; } } $this->get_broken_links(); $total_posts_pages = $wpdb->get_var(" SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_type IN ('" . join("','", $types) . "') AND post_status NOT IN ('auto-draft') AND ID NOT IN ( SELECT m.post_id FROM {$wpdb->postmeta} m JOIN {$wpdb->posts} p ON m.post_id = p.ID WHERE m.meta_key = '_alp_processed' AND p.post_type IN ('" . join("','", $types) . "') AND p.post_status NOT IN ('auto-draft') ) "); $total_posts_pages_processed = (int) $wpdb->get_var(" SELECT COUNT(m.meta_id) FROM {$wpdb->postmeta} m JOIN {$wpdb->posts} p ON p.ID = m.post_id WHERE m.meta_key = '_alp_processed' AND p.post_type IN ('" . join("','", $types) . "') AND p.post_status NOT IN ('auto-draft') "); include WPML_STICKY_LINKS_PATH . '/menu/management.php'; } function pre_update_option_widget_text($new_value, $old_value){ global $wpdb; if(is_array($new_value)){ foreach($new_value as $k=>$w){ if(isset($w['text'])){ $new_value[$k]['text'] = $this->absolute_links_object->_process_generic_text($w['text']); } } if($new_value !== $old_value){ $wpdb->update($wpdb->options, array('option_value'=>$new_value), array('option_name'=>'widget_text')); } } return $new_value; } function save_default_urls($post_id, $post){ if($post->post_status == 'auto-draft' || isset($_POST['autosave'])) return; if(!in_array( $post->post_type, get_post_types( array('show_ui' => true ) ) )) return; if(!post_type_supports($post->post_type, 'editor')) return; if(in_array($post->post_type, array('revision', 'attachment', 'nav_menu_item'))) return; $this->absolute_links_object->process_post($post_id); } function show_permalinks($cont){ if(!isset($GLOBALS['__disable_absolute_links_permalink_filter']) || !$GLOBALS['__disable_absolute_links_permalink_filter']){ $home = rtrim(get_option('home'),'/'); $parts = parse_url($home); $abshome = $parts['scheme'] .'://' . $parts['host']; $path = @ltrim($parts['path'],'/'); $tx_qvs = join('|',$this->absolute_links_object->taxonomies_query_vars); $reg_ex = '@]+)?href="(('.$abshome.')?/'.$path.'/?\?(p|page_id|cat_ID|'.$tx_qvs.')=([0-9a-z-]+))(#?[^"]*)"([^>]+)?>@i'; $cont = preg_replace_callback( $reg_ex, array($this,'show_permalinks_cb'),$cont); } return $cont; } function show_permalinks_cb($matches){ if($matches[4]=='cat_ID'){ $url = get_category_link($matches[5]); }elseif($tax = array_search($matches[4],$this->absolute_links_object->taxonomies_query_vars)){ $url = get_term_link($matches[5], $tax); }else{ $url = get_permalink($matches[5]); } $fragment = $matches[6]; if ($fragment != '') { $fragment = str_replace('&', '&', $fragment); $fragment = str_replace('&', '&', $fragment); if ($fragment[0] == '&') { if (strpos($url, '?') === FALSE) { $fragment[0] = '?'; } } } $trail = ''; if (isset($matches[7])) { $trail = $matches[7]; } return ''; } function get_broken_links(){ global $wpdb; $this->broken_links = $wpdb->get_results("SELECT p2.ID, p2.post_title, p1.meta_value AS links FROM {$wpdb->postmeta} p1 JOIN {$wpdb->posts} p2 ON p1.post_id=p2.ID WHERE p1.meta_key='_alp_broken_links'"); } function icl_dashboard_widget_content(){ ?> ' . __('Configure', 'wpml-sticky-links') . ''; } return $links; } // Localization function plugin_localization(){ load_plugin_textdomain( 'wpml-sticky-links', false, WPML_STICKY_LINKS_FOLDER . '/locale'); } }