n ) { $ICL_Pro_Translation->fix_links_to_translated_content( $id, $lang ); } if ( ! is_wp_error( $id ) ) { $ret = $this->run_wpml_actions( $master_post, $trid, $lang, $id, $post_array ); } else { throw new Exception( $id->get_error_message() ); } return $ret; } /** * @param int $element_id * * @return null|string */ private function is_external( $element_id ) { $query = "SELECT element_type FROM {$this->wpdb->prefix}icl_translations WHERE element_id=%d AND element_type LIKE %s LIMIT 1"; return ! $this->wpdb->get_var( $this->wpdb->prepare( $query, $element_id, 'post_%' ) ); } private function run_wpml_actions( $master_post, $trid, $lang, $id, $post_array ) { $master_post_id = $master_post->ID; $this->sitepress->set_element_language_details( $id, 'post_' . $master_post->post_type, $trid, $lang ); $this->sync_duplicate_password( $master_post_id, $id ); $this->sync_page_template( $master_post_id, $id ); $this->duplicate_fix_children( $master_post_id, $lang ); // make sure post name is copied $this->wpdb->update( $this->wpdb->posts, array( 'post_name' => $master_post->post_name ), array( 'ID' => $id ) ); if ( $this->sitepress->get_setting( 'sync_post_taxonomies', false ) ) { $this->duplicate_taxonomies( $master_post_id, $lang ); } $this->duplicate_custom_fields( $master_post_id, $lang ); update_post_meta( $id, '_icl_lang_duplicate_of', $master_post->ID ); // Duplicate post format after the taxonomies because post format is stored // as a taxonomy by WP. if ( $this->sitepress->get_setting( 'sync_post_format' ) ) { $_wp_post_format = get_post_format( $master_post_id ); $_wp_post_format && set_post_format( $id, $_wp_post_format ); } if ( $this->sitepress->get_setting( 'sync_comments_on_duplicates' ) ) { $this->duplicate_comments( $master_post_id, $id ); } $status_helper = wpml_get_post_status_helper(); $status_helper->set_status( $id, ICL_TM_DUPLICATE ); $status_helper->set_update_status( $id, false ); do_action( 'icl_make_duplicate', $master_post_id, $lang, $post_array, $id ); clean_post_cache( $id ); return $id; } private function sync_page_template( $master_post_id, $duplicate_post_id ) { $_wp_page_template = get_post_meta( $master_post_id, '_wp_page_template', true ); if ( ! empty( $_wp_page_template ) ) { update_post_meta( $duplicate_post_id, '_wp_page_template', $_wp_page_template ); } } private function duplicate_comments( $master_post_id, $translated_id ) { global $sitepress; remove_filter( 'comments_clauses', array( $sitepress, 'comments_clauses' ), 10 ); $comments_on_master = get_comments( array( 'post_id' => $master_post_id ) ); $comments_on_translation = get_comments( array( 'post_id' => $translated_id, 'status' => 'any' ) ); add_filter( 'comments_clauses', array( $sitepress, 'comments_clauses' ), 10, 2 ); foreach ( $comments_on_translation as $comment ) { wp_delete_comment( $comment->comment_ID, true ); clean_comment_cache( $comment->comment_ID ); } $iclTranslationManagement = wpml_load_core_tm(); foreach ( $comments_on_master as $comment ) { $iclTranslationManagement->duplication_insert_comment( $comment->comment_ID ); clean_comment_cache( $comment->comment_ID ); } wp_update_comment_count_now( $master_post_id ); wp_update_comment_count_now( $translated_id ); } /** * @param array $post_array * @param string $lang * * @return int|WP_Error */ private function save_duplicate( array $post_array, $lang ) { return wpml_get_create_post_helper()->insert_post( $post_array, $lang, true ); } private function duplicate_fix_children( $master_post_id, $lang ) { $post_type = $this->wpdb->get_var( $this->wpdb->prepare( "SELECT post_type FROM {$this->wpdb->posts} WHERE ID=%d", $master_post_id ) ); $master_children = $this->wpdb->get_col( $this->wpdb->prepare( "SELECT ID FROM {$this->wpdb->posts} WHERE post_parent=%d AND post_type != 'revision'", $master_post_id ) ); $dup_parent = icl_object_id( $master_post_id, $post_type, false, $lang ); if ( $master_children ) { foreach ( $master_children as $master_child ) { $dup_child = icl_object_id( $master_child, $post_type, false, $lang ); if ( $dup_child ) { $this->wpdb->update( $this->wpdb->posts, array( 'post_parent' => $dup_parent ), array( 'ID' => $dup_child ) ); } $this->duplicate_fix_children( $master_child, $lang ); } } } private function duplicate_taxonomies( $master_post_id, $lang ) { $post_type = get_post_field( 'post_type', $master_post_id ); $taxonomies = get_object_taxonomies( $post_type ); $trid = $this->sitepress->get_element_trid( $master_post_id, 'post_' . $post_type ); if ( $trid ) { $translations = $this->sitepress->get_element_translations( $trid, 'post_' . $post_type, false, false, true ); if ( isset( $translations[ $lang ] ) ) { $duplicate_post_id = $translations[ $lang ]->element_id; /* If we have an existing post, we first of all remove all terms currently attached to it. * The main reason behind is the removal of the potentially present default category on the post. */ wp_delete_object_term_relationships( $duplicate_post_id, $taxonomies ); } else { return false; // translation not found! } } $term_helper = wpml_get_term_translation_util(); $term_helper->duplicate_terms( $master_post_id, $lang ); return true; } private function sync_duplicate_password( $master_post_id, $duplicate_post_id ) { if ( post_password_required( $master_post_id ) ) { $sql = $this->wpdb->prepare( "UPDATE {$this->wpdb->posts} AS dupl, (SELECT org.post_password FROM {$this->wpdb->posts} AS org WHERE ID = %d ) AS pwd SET dupl.post_password = pwd.post_password WHERE dupl.ID = %d", array( $master_post_id, $duplicate_post_id ) ); $this->wpdb->query( $sql ); } } private function duplicate_custom_fields( $master_post_id, $lang ) { $duplicate_post_id = false; $post_type = get_post_field( 'post_type', $master_post_id ); $trid = $this->sitepress->get_element_trid( $master_post_id, 'post_' . $post_type ); if ( $trid ) { $translations = $this->sitepress->get_element_translations( $trid, 'post_' . $post_type ); if ( isset( $translations[ $lang ] ) ) { $duplicate_post_id = $translations[ $lang ]->element_id; } else { return false; // translation not found! } } $default_exceptions = WPML_Config::get_custom_fields_translation_settings(); $exceptions = apply_filters( 'wpml_duplicate_custom_fields_exceptions', array() ); $exceptions = array_merge( $exceptions, $default_exceptions ); $exceptions = array_unique( $exceptions ); $exceptions_in = ! empty( $exceptions ) ? 'AND meta_key NOT IN ( ' . wpml_prepare_in( $exceptions ) . ') ' : ''; $from_where_string = "FROM {$this->wpdb->postmeta} WHERE post_id = %d " . $exceptions_in; $post_meta_master = $this->wpdb->get_results( "SELECT meta_key, meta_value " . $this->wpdb->prepare( $from_where_string, $master_post_id ) ); $this->wpdb->query( "DELETE " . $this->wpdb->prepare( $from_where_string, $duplicate_post_id ) ); $values = []; foreach ( $post_meta_master as $post_meta ) { $is_serialized = is_serialized( $post_meta->meta_value ); $meta_data = array( 'context' => 'custom_field', 'attribute' => 'value', 'key' => $post_meta->meta_key, 'is_serialized' => $is_serialized, 'post_id' => $duplicate_post_id, 'master_post_id' => $master_post_id, ); /** * @deprecated use 'wpml_duplicate_generic_string' instead, with the same arguments */ $icl_duplicate_generic_string = apply_filters( 'icl_duplicate_generic_string', $post_meta->meta_value, $lang, $meta_data ); $post_meta->meta_value = $icl_duplicate_generic_string; $wpml_duplicate_generic_string = apply_filters( 'wpml_duplicate_generic_string', $post_meta->meta_value, $lang, $meta_data ); $post_meta->meta_value = $wpml_duplicate_generic_string; if ( ! is_serialized( $post_meta->meta_value ) ) { $post_meta->meta_value = maybe_serialize( $post_meta->meta_value ); } $values[] = $this->wpdb->prepare( '( %d, %s, %s )', $duplicate_post_id, $post_meta->meta_key, $post_meta->meta_value ); } if ( ! empty( $values ) ) { $values = implode( ', ', $values ); $this->wpdb->query( // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared "INSERT INTO `{$this->wpdb->postmeta}` (`post_id`, `meta_key`, `meta_value`) VALUES {$values}" ); } return true; } /** * @param string $lang * @param \WP_Post $master_post * * @return array */ private function duplicate_post_content( $lang, $master_post ) { $duplicated_post_content_meta = array( 'context' => 'post', 'attribute' => 'content', 'key' => $master_post->ID ); $duplicated_post_content = $master_post->post_content; $duplicated_post_content = apply_filters( 'icl_duplicate_generic_string', $duplicated_post_content, $lang, $duplicated_post_content_meta ); $duplicated_post_content = apply_filters( 'wpml_duplicate_generic_string', $duplicated_post_content, $lang, $duplicated_post_content_meta ); return $duplicated_post_content; } /** * @param string $lang * @param \WP_Post $master_post * * @return mixed */ private function duplicate_post_title( $lang, $master_post ) { $duplicated_post_title_meta = array( 'context' => 'post', 'attribute' => 'title', 'key' => $master_post->ID ); $duplicated_post_title = $master_post->post_title; $duplicated_post_title = apply_filters( 'icl_duplicate_generic_string', $duplicated_post_title, $lang, $duplicated_post_title_meta ); $duplicated_post_title = apply_filters( 'wpml_duplicate_generic_string', $duplicated_post_title, $lang, $duplicated_post_title_meta ); return $duplicated_post_title; } /** * @param string $lang * @param WP_Post $master_post * * @return mixed */ private function duplicate_post_excerpt( $lang, $master_post ) { $duplicated_post_excerpt_meta = array( 'context' => 'post', 'attribute' => 'excerpt', 'key' => $master_post->ID ); $duplicated_post_excerpt = $master_post->post_excerpt; $duplicated_post_excerpt = apply_filters( 'icl_duplicate_generic_string', $duplicated_post_excerpt, $lang, $duplicated_post_excerpt_meta ); $duplicated_post_excerpt = apply_filters( 'wpml_duplicate_generic_string', $duplicated_post_excerpt, $lang, $duplicated_post_excerpt_meta ); return $duplicated_post_excerpt; } }