Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.39 KB | None | 0 0
  1. <?php
  2. define( 'DIR_URI', get_template_directory_uri() . '/theme-modules/multilanguage-tracker' );
  3.  
  4. /*
  5. * Multilanguage URL tracker
  6.  */
  7. function mlt_url_tracker_menu() {
  8.   add_menu_page(
  9.     'URL Tracker',
  10.     'URL Tracker',
  11.     'manage_network_options',
  12.     'imm_url_tracker',
  13.     'mlt_url_tracker_callback'
  14.   );
  15. }
  16.  
  17. add_action( 'network_admin_menu', 'mlt_url_tracker_menu' );
  18.  
  19. /*
  20. * Callback function to call template
  21.  */
  22. function mlt_url_tracker_callback() {
  23.   //Add empty options
  24.   $items = array();
  25.   $language_options = get_site_option('imm_language_options');
  26.   if( !$language_options ) {
  27.     foreach ( get_sites() as $key => $site ) {
  28.       $items[] = array(
  29.         'site_id' => $site->blog_id,
  30.         'code'    => '',
  31.         'label'   => ''
  32.       );
  33.     }
  34.  
  35.     add_site_option('imm_language_options', $items);
  36.   }
  37.  
  38.   require_once 'template.php';
  39. }
  40.  
  41. /*
  42. * Add Metabox on edit pages or post
  43.  */
  44.  add_action('add_meta_boxes', 'mlt_add_edit_metabox');
  45.  
  46.  function mlt_add_edit_metabox() {
  47.    if( !is_multisite() ) {
  48.      return;
  49.    }
  50.  
  51.    $post_types = array('post','page');
  52.  
  53.    $args = array(
  54.     'public'   => true,
  55.     '_builtin' => false,
  56.    );
  57.  
  58.    $output = 'objects';
  59.    $operator = 'and';
  60.  
  61.    $custom_post_types = get_post_types( $args, $output, $operator );
  62.  
  63.    foreach ($custom_post_types as $key => $type) {
  64.      $post_types[] = $type->name;
  65.    }
  66.  
  67.    add_meta_box(
  68.      'mlt_edit_content',
  69.      'Multilanguage Tracker',
  70.      'mlt_metabox_callback',
  71.      $post_types,
  72.      'side',
  73.      'high'
  74.    );
  75.  }
  76.  
  77.  function mlt_metabox_callback($post, $metabox) {
  78.    $url_tracker = get_post_meta($post->ID, 'mlt_url_tracker')[0];
  79.    if( $url_tracker ) {
  80.       foreach ($url_tracker as $page) {
  81.         if( get_current_blog_id() != $page['site_id'] && $page['post_id'] != '' ) :
  82.           switch_to_blog($page['site_id']);
  83.           $post_id = $page['post_id'];
  84.           $title = site_url();
  85.         ?>
  86.           <div class="mlt-tracker-item">
  87.             <p class="site-name"><?php echo $title; ?></p>
  88.             <p class="post-title"><?php echo get_the_title($post_id); ?> <a href="<?php echo get_edit_post_link($post_id) ?>" target="_blank">Edit</a></p>
  89.           </div>
  90.         <?php
  91.         restore_current_blog();
  92.         endif;
  93.       }
  94.    }else{
  95.      echo '<p>Edit the multilanguage tracker to show linked pages</p>';
  96.    }
  97.  }
  98.  
  99. /*
  100. * Get post list with pagination
  101.  */
  102. function mlt_get_posts_list( $blog_id, $paged, $post_type ) {
  103.   switch_to_blog( $blog_id );
  104.   $post_list = array();
  105.  
  106.   $args = array(
  107.     'post_type'       => $post_type,
  108.     'post_status'     => 'publish',
  109.     'posts_per_page'  => 10,
  110.     'paged'           => $paged,
  111.   );
  112.  
  113.   $query = new WP_Query( $args );
  114.  
  115.   if( $query->have_posts() ) {
  116.     while( $query->have_posts() ) {
  117.       $query->the_post();
  118.       $post_list['posts'][] = array(
  119.         'id'        => get_the_ID(),
  120.         'title'     => get_the_title(),
  121.         'permalink' => get_permalink()
  122.       );
  123.     }
  124.  
  125.     $big = 999999999;
  126.     $post_list['pagination'] = paginate_links( array(
  127.       'base' => str_replace( array($big, '#038;'), array('%#%', ''), esc_url( get_pagenum_link( $big ) ) ),
  128.       'prev_text' => __( '&laquo;', 'imm-starter-theme' ),
  129.       'next_text' => __( '&raquo;', 'imm-starter-theme' ),
  130.       'format' => '?paged=%#%',
  131.       'current' => max( 1, $paged ),
  132.       'total' => $query->max_num_pages
  133.     ) );
  134.   }
  135.   wp_reset_postdata();
  136.  
  137.   return $post_list;
  138. }
  139.  
  140. /*
  141. * Enqueue scripts & styles on admin
  142.  */
  143. function mlt_enqueue_scripts_admin( $hook ) {
  144.   if( 'toplevel_page_imm_url_tracker' == $hook ) {
  145.     wp_enqueue_script('jquery-ui-sortable');
  146.  
  147.     wp_enqueue_script('mlt_select2', DIR_URI . '/select2/select2.min.js', array('jquery'), time(), true);
  148.     wp_enqueue_style('mlt_select2', DIR_URI . '/select2/select2.min.css');
  149.  
  150.     wp_enqueue_script('mlt_scripts', DIR_URI . '/assets/scripts.js', array('jquery'), time(), true);
  151.     wp_localize_script( 'mlt_scripts', 'mltAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce('ajax-nonce') ));
  152.  
  153.     wp_enqueue_style('mlt_style', DIR_URI . '/assets/style.css');
  154.   }
  155.  
  156.   wp_enqueue_style('mlt_admin_style', DIR_URI . '/assets/admin.css');
  157. }
  158.  
  159. add_action('admin_enqueue_scripts', 'mlt_enqueue_scripts_admin');
  160.  
  161. /*
  162. * Enqueue script & style on frontend
  163.  */
  164.  function mlt_enqueue_scripts_frontend( $hook ) {
  165.    $language_options = get_site_option('imm_language_options');
  166.    if( $language_options ) {
  167.      wp_enqueue_style('mlt_switcher', DIR_URI . '/assets/language-switcher.css');
  168.      wp_enqueue_script( 'mlt_switcher', DIR_URI . '/assets/language-switcher.js', array('jquery'), false, true );
  169.    }
  170.  }
  171.  
  172.  add_action('wp_enqueue_scripts', 'mlt_enqueue_scripts_frontend');
  173.  
  174. /*
  175. * Get post list from AJAX request
  176.  */
  177. function mlt_get_posts_list_ajax() {
  178.   $nonce = $_GET['nonce'];
  179.   if ( ! wp_verify_nonce( $nonce, 'ajax-nonce' ) )
  180.       die ( 'Your action is not verified');
  181.  
  182.   $site_id = $_GET['site_id'];
  183.   $post_type = $_GET['post_type'];
  184.   $paged = $_GET['page'];
  185.  
  186.   $result = array();
  187.   $result['pagination']['more'] = false;
  188.  
  189.   switch_to_blog( $site_id );
  190.   $args = array(
  191.     'post_type'       => $post_type,
  192.     'post_status'     => 'publish',
  193.     'posts_per_page'  => 10,
  194.     'paged'           => $paged
  195.   );
  196.  
  197.   if( isset($_GET['q']) ) {
  198.     $args['s'] = $_GET['q'];
  199.   }
  200.  
  201.   $query = new WP_Query( $args );
  202.  
  203.   if( $paged < $query->max_num_pages ) {
  204.     $result['pagination']['more'] = true;
  205.   }
  206.  
  207.   if( $query->have_posts() ) {
  208.     while( $query->have_posts() ) {
  209.       $query->the_post();
  210.       $result['results'][] = array(
  211.         'id'     => get_the_ID(),
  212.         'text'   => get_the_title()
  213.       );
  214.     }
  215.   }
  216.  
  217.   wp_reset_postdata();
  218.  
  219.   echo wp_json_encode( $result );
  220.  
  221.   die();
  222. }
  223.  
  224. add_action('wp_ajax_nopriv_mlt_get_posts_list_ajax', 'mlt_get_posts_list_ajax');
  225. add_action('wp_ajax_mlt_get_posts_list_ajax', 'mlt_get_posts_list_ajax');
  226.  
  227. /*
  228. * Update URL tracker on all sites
  229.  */
  230. function mlt_update_tracker() {
  231.   $nonce = $_POST['nonce'];
  232.   if ( ! wp_verify_nonce( $nonce, 'ajax-nonce' ) )
  233.       die ( 'Your action is not verified');
  234.  
  235.   $items = $_POST['items'];
  236.  
  237.   foreach ($items as $key => $item) {
  238.     switch_to_blog($item['site_id']);
  239.     update_post_meta( $item['post_id'], 'mlt_url_tracker', $items );
  240.   }
  241.  
  242.   die();
  243. }
  244.  
  245. add_action('wp_ajax_nopriv_mlt_update_tracker', 'mlt_update_tracker');
  246. add_action('wp_ajax_mlt_update_tracker', 'mlt_update_tracker');
  247.  
  248. /*
  249. * Update language options
  250.  */
  251. function mlt_update_language_options() {
  252.   $nonce = $_POST['nonce'];
  253.   if ( ! wp_verify_nonce( $nonce, 'ajax-nonce' ) )
  254.       die ( 'Your action is not verified');
  255.  
  256.   $items = $_POST['items'];
  257.  
  258.   delete_site_option('imm_language_options');
  259.   add_site_option('imm_language_options', $items);
  260.  
  261.   die();
  262. }
  263.  
  264. add_action('wp_ajax_nopriv_mlt_update_language_options', 'mlt_update_language_options');
  265. add_action('wp_ajax_mlt_update_language_options', 'mlt_update_language_options');
  266.  
  267. /*
  268. * Language switcher shortcode
  269.  */
  270. function mlt_language_switcher( $atts ) {
  271.   $a = shortcode_atts( array(
  272.         'type' => 'list',
  273.     ), $atts );
  274.  
  275.   ob_start();
  276.     require 'language-switcher-template.php';
  277.   return ob_get_clean();
  278. }
  279.  
  280. add_shortcode('mlt-language-switcher', 'mlt_language_switcher');
  281.  
  282. /*
  283. * Get linked pages based on post id
  284.  */
  285. function mlt_get_linked_pages() {
  286.   $result = array();
  287.   $language_options = get_site_option('imm_language_options');
  288.   $post_obj = get_queried_object();
  289.  
  290.   if( $language_options && isset($post_obj->ID) ) {
  291.     $post_id = $post_obj->ID;
  292.  
  293.     foreach ($language_options as $option_key => $option) {
  294.       $url_tracker = get_post_meta($post_id, 'mlt_url_tracker')[0];
  295.  
  296.       /*
  297.       * If linked pages not exists, use the current site url for each site
  298.        */
  299.       if( $url_tracker ) {
  300.         foreach ($url_tracker as $url_key => $url) {
  301.           if( $option['site_id'] == $url['site_id'] ) {
  302.             switch_to_blog($option['site_id']);
  303.  
  304.             $permalink = get_permalink($url['post_id']);
  305.  
  306.             $result[] = array(
  307.               'site_id'     => $option['site_id'],
  308.               'permalink'   => $permalink,
  309.               'code'        => $option['code'],
  310.               'label'       => $option['label']
  311.             );
  312.  
  313.             restore_current_blog();
  314.           }
  315.         }
  316.       }else{
  317.         switch_to_blog($option['site_id']);
  318.  
  319.         $permalink = site_url();
  320.  
  321.         $result[] = array(
  322.           'site_id'     => $option['site_id'],
  323.           'permalink'   => $permalink,
  324.           'code'        => $option['code'],
  325.           'label'       => $option['label']
  326.         );
  327.  
  328.         restore_current_blog();
  329.       }
  330.     }
  331.   }
  332.  
  333.   return $result;
  334. }
  335.  
  336. /*
  337. * Add Hreflang tag in header
  338.  */
  339.  
  340. function add_href_lang_tag() {
  341.   $template = '';
  342.  
  343.   $linked_pages = mlt_get_linked_pages();
  344.  
  345.   if( empty($linked_pages) ) {
  346.     return;
  347.   }
  348.  
  349.   foreach ($linked_pages as $key => $page) {
  350.     switch_to_blog($option['site_id']);
  351.  
  352.     //Don't render the tag if there is no linked pages on current page
  353.     if( !is_front_page() && ( site_url() != $option['permalink'] ) ) {
  354.       $template .= '<link rel="alternate" href="'. $page['permalink'] .'" hreflang="'. $page['code'] .'" />';
  355.     }
  356.  
  357.     restore_current_blog();
  358.   }
  359.  
  360.   echo $template;
  361. }
  362.  
  363. add_action('wp_head', 'add_href_lang_tag');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement