settings = get_option( 'alp_settings' ); $this->init_hooks(); } public function init_hooks() { add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ), 0 ); //init WPML_Sticky_Links after init AbsoluteLinks add_action( 'init', array( $this, 'init' ), 1001 ); } function plugins_loaded() { $this->plugin_localization(); // Check if WPML is active. If not display warning message and not load Sticky links if ( defined( 'ICL_PLUGIN_PATH' ) ) { $this->absolute_links_object = new AbsoluteLinks; } } function init() { if ( !defined( 'ICL_SITEPRESS_VERSION' ) || ICL_PLUGIN_INACTIVE ) { add_action( 'admin_notices', array( $this, '_no_wpml_warning' ) ); } elseif ( version_compare( ICL_SITEPRESS_VERSION, '2.0.5', '<' ) ) { add_action( 'admin_notices', array( $this, '_old_wpml_warning' ) ); } if ( !defined( 'ICL_PLUGIN_PATH' ) ) { return; } global $sitepress_settings; $this->ajax_responses(); add_action( 'save_post', array( $this, 'save_default_urls' ), 120, 2 ); add_action( 'admin_head', array( $this, 'js_scripts' ) ); add_filter( 'the_content', array( $this, 'show_permalinks' ), 0 ); if ( $this->settings[ 'sticky_links_widgets' ] ) { 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( 'wpml_admin_menu_configure', array( $this, 'menu' ) ); add_filter( 'plugin_action_links', array( $this, 'plugin_action_links' ), 10, 2 ); if ( is_admin() && !defined( 'DOING_AJAX' ) ) { add_action( 'admin_print_scripts', array( $this, 'admin_print_scripts' ) ); add_action( 'admin_print_styles', array( $this, 'admin_print_styles' ) ); } 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 admin_print_styles() { wp_enqueue_style('wpml-sticky-links-css', WPML_STICKY_LINKS_URL . '/res/css/management.css', array(), WPML_STICKY_LINKS_VERSION); } function admin_print_scripts() { wp_enqueue_script('wpml-sticky-links-js', WPML_STICKY_LINKS_URL . '/res/js/scripts.js', array('jquery'), WPML_STICKY_LINKS_VERSION); $wpml_sticky_links_ajax_loader_img_src = WPML_STICKY_LINKS_URL . 'res/img/ajax-loader.gif'; $wpml_sticky_links_ajax_loader_img = 'loading'; wp_localize_script('wpml-sticky-links-js', 'data', array( 'wpml_sticky_links_ajax_loader_img' => $wpml_sticky_links_ajax_loader_img )); } function _no_wpml_warning(){ ?>

WPML in order to work.', 'wpml-sticky-links'), 'https://wpml.org/'); ?>

WPML versions prior 2.0.5.', 'wpml-sticky-links'), 'https://wpml.org/'); ?>

settings['sticky_links_widgets'] = (int) $_POST['icl_sticky_links_widgets']; } else { $this->settings['sticky_links_widgets'] = 0; } $this->save_settings(); } } function save_settings(){ update_option('alp_settings', $this->settings); } function ajax_responses(){ $nonce = filter_input(INPUT_POST, '_icl_sl_nonce'); if(!isset($_POST['alp_ajx_action']) || ! wp_verify_nonce($nonce, 'wpml_sticky_links_nonce')){ return; } global $wpdb; $post_types = array(); foreach($GLOBALS['wp_post_types'] as $key=>$val){ if ( $val->public && $key !== '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 (" . wpml_prepare_in( $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 (" . wpml_prepare_in( $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': $affected_rows = $wpdb->query("DELETE FROM {$wpdb->postmeta} WHERE meta_key='_alp_processed'"); echo $affected_rows; break; case 'use_suggestion': $post_id = filter_input(INPUT_POST, 'post_id', FILTER_SANITIZE_NUMBER_INT); $orig_url = filter_input(INPUT_POST, 'orig_url'); $broken_links = get_post_meta($post_id,'_alp_broken_links', true); foreach($broken_links as $k=>$bl){ if($k === $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_id,'_alp_broken_links', $broken_links); }else{ delete_post_meta($post_id,'_alp_broken_links'); } echo $c.'|'.$bl['suggestions'][$_POST['sug_id']]['perma']; break; } } if(!empty($broken) && !empty($repl)){ $q = "SELECT post_content FROM {$wpdb->posts} WHERE ID=%d"; $q_prepared = $wpdb->prepare($q, $post_id); $post_content = $wpdb->get_var($q_prepared); $post_content = preg_replace('@href="('.$broken.')"@i', 'href="'.$repl.'"', $post_content); $wpdb->update($wpdb->posts, array('post_content' => $post_content), array('ID'=> $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 (" . wpml_prepare_in( $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); if ( $cont != $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(){ ?> get_current_language(); $default_language = $sitepress->get_default_language(); $sitepress->switch_lang( $default_language ); $alp_broken_links = array(); 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' ], $alp_broken_links ); } } if ( $new_value !== $old_value ) { $wpdb->update( $wpdb->options, array( 'option_value' => $new_value ), array( 'option_name' => 'widget_text' ) ); } } $sitepress->switch_lang( $current_language ); } 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 ) { global $sitepress; if ( !isset( $GLOBALS[ '__disable_absolute_links_permalink_filter' ] ) || !$GLOBALS[ '__disable_absolute_links_permalink_filter' ] ) { $absolute_to_permalinks = new WPML_Absolute_To_Permalinks( $sitepress ); $cont = $absolute_to_permalinks->convert_text( $cont ); } return $cont; } function convert_to_sticky_links_filter( $text ) { $alp_broken_links = array(); return $this->absolute_links_object->_process_generic_text( $text, $alp_broken_links ); } function get_broken_links(){ global $wpdb; $broken_links_prepared = $wpdb->prepare( " 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=%s AND p1.meta_value<>'array' ", array( '_alp_broken_links' ) ); $this->broken_links = $wpdb->get_results( $broken_links_prepared ); } 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'); } }