Advertisement
nshelper

APTO - Update the current language list with the one set on the English, when published a post and l

Sep 28th, 2024
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.17 KB | None | 0 0
  1. <?php
  2.    
  3.     /**
  4.     * Update the current language list with the one set on the English, when published a post and linked to an english origin object
  5.     *
  6.     */
  7.     define ( 'APTO_WPML_BASE_LANG', 'en' );
  8.     add_action( 'save_post', '__custom_save_post', 10, 3 );
  9.     function __custom_save_post( $post_ID, $post, $update )
  10.         {
  11.             $current_language   =   apply_filters( 'wpml_current_language', null );
  12.             if ( $current_language  === APTO_WPML_BASE_LANG )
  13.                 return;
  14.                
  15.             //Get the base languag post ID
  16.             $base_post_id = apply_filters( 'wpml_object_id', $post_ID, $post->post_type , false, APTO_WPML_BASE_LANG );
  17.             if ( empty ( $base_post_id ) )
  18.                 return;
  19.                
  20.             //attempt to identify the sort list
  21.             $args   =   array(
  22.                                 '_view_type'            =>  array('multiple'),
  23.                                 '_wpml_synchronize'     =>  array('yes')
  24.                                 );
  25.                          
  26.             $matched_sorts    =   APTO_functions::get_sorts_by_filters( $args );
  27.            
  28.             if ( ! is_array ( $matched_sorts )    ||  count ( $matched_sorts )  <   1 )
  29.                 return;
  30.                
  31.             reset ( $matched_sorts );
  32.             $sort_item  =   current ( $matched_sorts );
  33.             $sort_ID    =   $sort_item->ID;
  34.            
  35.             //identify the base sort view
  36.             $args = array(
  37.                             '_view_selection'   =>  'archive',
  38.                             '_view_language'    =>  APTO_WPML_BASE_LANG
  39.                             );
  40.             $base_sort_view_id   =   APTO_functions::get_sort_view_id_by_attributes( $sort_ID, $args );
  41.            
  42.             if ( empty ( $base_sort_view_id ) )
  43.                 return;
  44.            
  45.             //Identify the current language sort_view
  46.             $args = array(
  47.                             '_view_selection'   =>  'archive',
  48.                             '_view_language'    =>  $current_language
  49.                             );
  50.             $language_sort_view_id   =   APTO_functions::get_sort_view_id_by_attributes( $sort_ID, $args );
  51.            
  52.             if ( empty ( $language_sort_view_id ) )
  53.                 {
  54.                     //create the sort view
  55.                     $sort_view_meta     =   array(
  56.                                                     '_order_type'               =>  'manual',
  57.                                                     '_view_selection'           =>  'archive',
  58.                                                     '_view_language'            =>  $current_language
  59.                                                     );
  60.                        
  61.                     $language_sort_view_id       =   $this->create_view( $sort_ID, $sort_view_meta);
  62.                 }
  63.                
  64.             $base_order_list    =   APTO_functions::get_order_list( $base_sort_view_id );
  65.            
  66.             $translated_objects =   [];
  67.             foreach ( $base_order_list  as  $post_id )
  68.                 {
  69.                     $translated_post_id =   icl_object_id ( $post_id, $post->post_type, FALSE, $current_language );
  70.                     if ( $translated_post_id    >   0   )
  71.                         $translated_objects[]   =   $translated_post_id;
  72.                 }
  73.            
  74.             APTO_functions::delete_sort_list_from_table( $language_sort_view_id );
  75.            
  76.             global $wpdb;
  77.            
  78.             $query = "INSERT INTO `". $wpdb->prefix ."apto_sort_list`
  79.                        (`sort_view_id`, `object_id`)
  80.                        VALUES ";
  81.            
  82.             $first  =   TRUE;            
  83.             foreach ( $translated_objects as $post_id )
  84.                 {
  85.                     if ( ! $first )
  86.                         $query  .=  ",\n";
  87.                        
  88.                     $query  .=  "('". $language_sort_view_id ."', '". $post_id ."')";
  89.                    
  90.                     $first  =   FALSE;
  91.                 }
  92.             $results = $wpdb->get_results($query);
  93.            
  94.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement