Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Update the current language list with the one set on the English, when published a post and linked to an english origin object
- *
- */
- define ( 'APTO_WPML_BASE_LANG', 'en' );
- add_action( 'save_post', '__custom_save_post', 10, 3 );
- function __custom_save_post( $post_ID, $post, $update )
- {
- $current_language = apply_filters( 'wpml_current_language', null );
- if ( $current_language === APTO_WPML_BASE_LANG )
- return;
- //Get the base languag post ID
- $base_post_id = apply_filters( 'wpml_object_id', $post_ID, $post->post_type , false, APTO_WPML_BASE_LANG );
- if ( empty ( $base_post_id ) )
- return;
- //attempt to identify the sort list
- $args = array(
- '_view_type' => array('multiple'),
- '_wpml_synchronize' => array('yes')
- );
- $matched_sorts = APTO_functions::get_sorts_by_filters( $args );
- if ( ! is_array ( $matched_sorts ) || count ( $matched_sorts ) < 1 )
- return;
- reset ( $matched_sorts );
- $sort_item = current ( $matched_sorts );
- $sort_ID = $sort_item->ID;
- //identify the base sort view
- $args = array(
- '_view_selection' => 'archive',
- '_view_language' => APTO_WPML_BASE_LANG
- );
- $base_sort_view_id = APTO_functions::get_sort_view_id_by_attributes( $sort_ID, $args );
- if ( empty ( $base_sort_view_id ) )
- return;
- //Identify the current language sort_view
- $args = array(
- '_view_selection' => 'archive',
- '_view_language' => $current_language
- );
- $language_sort_view_id = APTO_functions::get_sort_view_id_by_attributes( $sort_ID, $args );
- if ( empty ( $language_sort_view_id ) )
- {
- include_once(APTO_PATH . '/include/apto_interface_helper-class.php');;
- include_once(APTO_PATH . '/include/apto_admin_functions-class.php');
- $APTO_interface_helper = new APTO_interface_helper();
- //create the sort view
- $sort_view_meta = array(
- '_order_type' => 'manual',
- '_view_selection' => 'archive',
- '_view_language' => $current_language
- );
- $language_sort_view_id = $APTO_interface_helper->create_view( $sort_ID, $sort_view_meta);
- }
- $base_order_list = APTO_functions::get_order_list( $base_sort_view_id );
- $translated_objects = [];
- foreach ( $base_order_list as $post_id )
- {
- $translated_post_id = icl_object_id ( $post_id, $post->post_type, FALSE, $current_language );
- if ( $translated_post_id > 0 )
- $translated_objects[] = $translated_post_id;
- }
- APTO_functions::delete_sort_list_from_table( $language_sort_view_id );
- global $wpdb;
- $query = "INSERT INTO `". $wpdb->prefix ."apto_sort_list`
- (`sort_view_id`, `object_id`)
- VALUES ";
- $first = TRUE;
- foreach ( $translated_objects as $post_id )
- {
- if ( ! $first )
- $query .= ",\n";
- $query .= "('". $language_sort_view_id ."', '". $post_id ."')";
- $first = FALSE;
- }
- $results = $wpdb->get_results($query);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement