PageRenderTime 45ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/sitepress-multilingual-cms/inc/post-translation/wpml-post-duplication.class.php

https://gitlab.com/woxiprogrammers/infinia-wordpress
PHP | 361 lines | 288 code | 38 blank | 35 comment | 35 complexity | f2be498fb3e183c449edabd873b49bea MD5 | raw file
  1. <?php
  2. require_once 'wpml-wordpress-actions.class.php';
  3. /**
  4. * Class WPML_Post_Duplication
  5. *
  6. * @package wpml-core
  7. * @subpackage post-translation
  8. */
  9. class WPML_Post_Duplication extends WPML_WPDB_And_SP_User {
  10. function get_duplicates( $master_post_id ) {
  11. global $wpml_post_translations;
  12. $duplicates = array();
  13. $post_ids_query = " SELECT post_id
  14. FROM {$this->wpdb->postmeta}
  15. WHERE meta_key='_icl_lang_duplicate_of'
  16. AND meta_value = %d
  17. AND post_id <> %d";
  18. $post_ids_prepare = $this->wpdb->prepare( $post_ids_query, array( $master_post_id, $master_post_id ) );
  19. $post_ids = $this->wpdb->get_col( $post_ids_prepare );
  20. foreach ( $post_ids as $post_id ) {
  21. $language_code = $wpml_post_translations->get_element_lang_code( $post_id );
  22. $duplicates[ $language_code ] = $post_id;
  23. }
  24. return $duplicates;
  25. }
  26. function make_duplicate( $master_post_id, $lang ) {
  27. global $wpml_post_translations, $wpml_language_resolution;
  28. static $duplicated_post_ids;
  29. if ( ! isset( $duplicated_post_ids ) ) {
  30. $duplicated_post_ids = array();
  31. }
  32. //It is already done? (avoid infinite recursions)
  33. if ( ! $wpml_language_resolution->is_language_active( $lang )
  34. || in_array( $master_post_id . '|' . $lang, $duplicated_post_ids )
  35. ) {
  36. return true;
  37. }
  38. $duplicated_post_ids[ ] = $master_post_id . '|' . $lang;
  39. do_action( 'icl_before_make_duplicate', $master_post_id, $lang );
  40. $master_post = get_post( $master_post_id );
  41. $is_duplicated = false;
  42. $translations = $wpml_post_translations->get_element_translations( $master_post_id, false, false );
  43. if ( isset( $translations[ $lang ] ) ) {
  44. $post_array[ 'ID' ] = $translations[ $lang ];
  45. if ( WPML_WordPress_Actions::is_bulk_trash( $post_array[ 'ID' ] ) || WPML_WordPress_Actions::is_bulk_untrash( $post_array[ 'ID' ] ) ) {
  46. return true;
  47. }
  48. $is_duplicated = get_post_meta( $translations[ $lang ], '_icl_lang_duplicate_of', true );
  49. }
  50. $post_array['post_author'] = $master_post->post_author;
  51. $post_array['post_date'] = $master_post->post_date;
  52. $post_array['post_date_gmt'] = $master_post->post_date_gmt;
  53. $duplicated_post_content = $this->duplicate_post_content( $lang, $master_post );
  54. $post_array['post_content'] = addslashes_gpc( $duplicated_post_content );
  55. $duplicated_post_title = $this->duplicate_post_title( $lang, $master_post );
  56. $post_array['post_title'] = addslashes_gpc( $duplicated_post_title );
  57. $duplicated_post_excerpt = $this->duplicate_post_excerpt( $lang, $master_post );
  58. $post_array['post_excerpt'] = addslashes_gpc( $duplicated_post_excerpt );
  59. if ( $this->sitepress->get_setting('sync_post_status' ) ) {
  60. $sync_post_status = true;
  61. } else {
  62. $sync_post_status = ( ! isset( $post_array[ 'ID' ] )
  63. || ( $this->sitepress->get_setting( 'sync_delete' ) && $master_post->post_status === 'trash' ) || $is_duplicated );
  64. }
  65. if ( $sync_post_status || ( isset( $post_array[ 'ID' ] ) && get_post_status( $post_array[ 'ID' ] ) === 'auto-draft' ) ) {
  66. $post_array[ 'post_status' ] = $master_post->post_status;
  67. }
  68. $post_array[ 'comment_status' ] = $master_post->comment_status;
  69. $post_array[ 'ping_status' ] = $master_post->ping_status;
  70. $post_array[ 'post_name' ] = $master_post->post_name;
  71. if ( $master_post->post_parent ) {
  72. $parent = $this->sitepress->get_object_id( $master_post->post_parent, $master_post->post_type, false, $lang );
  73. $post_array[ 'post_parent' ] = $parent;
  74. }
  75. $post_array['menu_order'] = $master_post->menu_order;
  76. $post_array['post_type'] = $master_post->post_type;
  77. $post_array['post_mime_type'] = $master_post->post_mime_type;
  78. $trid = $this->sitepress->get_element_trid( $master_post->ID,
  79. 'post_' . $master_post->post_type );
  80. $id = $this->save_duplicate( $post_array, $lang );
  81. require_once ICL_PLUGIN_PATH . '/inc/cache.php';
  82. icl_cache_clear();
  83. global $ICL_Pro_Translation;
  84. /** @var WPML_Pro_Translation $ICL_Pro_Translation */
  85. if ( $ICL_Pro_Translation ) {
  86. $ICL_Pro_Translation->_content_fix_links_to_translated_content( $id, $lang );
  87. }
  88. if ( ! is_wp_error( $id ) ) {
  89. $ret = $this->run_wpml_actions( $master_post, $trid, $lang, $id, $post_array );
  90. } else {
  91. $ret = false;
  92. }
  93. return $ret;
  94. }
  95. private function run_wpml_actions( $master_post, $trid, $lang, $id, $post_array ) {
  96. $master_post_id = $master_post->ID;
  97. $this->sitepress->set_element_language_details( $id, 'post_' . $master_post->post_type, $trid, $lang );
  98. $this->sync_duplicate_password( $master_post_id, $id );
  99. $this->sync_page_template( $master_post_id, $id );
  100. $this->duplicate_fix_children( $master_post_id, $lang );
  101. // make sure post name is copied
  102. $this->wpdb->update( $this->wpdb->posts, array( 'post_name' => $master_post->post_name ), array( 'ID' => $id ) );
  103. update_post_meta( $id, '_icl_lang_duplicate_of', $master_post->ID );
  104. if ( $this->sitepress->get_option( 'sync_post_taxonomies' ) ) {
  105. $this->duplicate_taxonomies( $master_post_id, $lang );
  106. }
  107. $this->duplicate_custom_fields( $master_post_id, $lang );
  108. // Duplicate post format after the taxonomies because post format is stored
  109. // as a taxonomy by WP.
  110. if ( $this->sitepress->get_setting( 'sync_post_format' ) ) {
  111. $_wp_post_format = get_post_format( $master_post_id );
  112. set_post_format( $id, $_wp_post_format );
  113. }
  114. if ( $this->sitepress->get_setting( 'sync_comments_on_duplicates' ) ) {
  115. $this->duplicate_comments( $master_post_id, $id );
  116. }
  117. $status_helper = wpml_get_post_status_helper();
  118. $status_helper->set_status( $id, ICL_TM_DUPLICATE );
  119. $status_helper->set_update_status( $id, false );
  120. do_action( 'icl_make_duplicate', $master_post_id, $lang, $post_array, $id );
  121. clean_post_cache( $id );
  122. return $id;
  123. }
  124. private function sync_page_template( $master_post_id, $duplicate_post_id ) {
  125. $_wp_page_template = get_post_meta( $master_post_id, '_wp_page_template', true );
  126. if ( ! empty( $_wp_page_template ) ) {
  127. update_post_meta( $duplicate_post_id, '_wp_page_template', $_wp_page_template );
  128. }
  129. }
  130. private function duplicate_comments( $master_post_id, $translated_id ) {
  131. global $sitepress;
  132. remove_filter( 'comments_clauses', array( $sitepress, 'comments_clauses' ), 10 );
  133. $comments_on_master = get_comments( array( 'post_id' => $master_post_id ) );
  134. $comments_on_translation = get_comments( array( 'post_id' => $translated_id, 'status' => 'any' ) );
  135. add_filter( 'comments_clauses', array( $sitepress, 'comments_clauses' ), 10, 2 );
  136. foreach ( $comments_on_translation as $comment ) {
  137. wp_delete_comment( $comment->comment_ID, true );
  138. clean_comment_cache( $comment->comment_ID );
  139. }
  140. $iclTranslationManagement = wpml_load_core_tm();
  141. foreach ( $comments_on_master as $comment ) {
  142. $iclTranslationManagement->duplication_insert_comment( $comment->comment_ID );
  143. clean_comment_cache( $comment->comment_ID );
  144. }
  145. wp_update_comment_count_now( $master_post_id );
  146. wp_update_comment_count_now( $translated_id );
  147. }
  148. private function save_duplicate( $post_array, $lang ) {
  149. if ( isset( $post_array[ 'ID' ] ) ) {
  150. $id = wp_update_post( $post_array );
  151. } else {
  152. $create_post_helper = wpml_get_create_post_helper();
  153. $id = $create_post_helper->icl_insert_post( $post_array, $lang );
  154. }
  155. return $id;
  156. }
  157. private function duplicate_fix_children( $master_post_id, $lang ) {
  158. $post_type = $this->wpdb->get_var(
  159. $this->wpdb->prepare( "SELECT post_type FROM {$this->wpdb->posts} WHERE ID=%d", $master_post_id )
  160. );
  161. $master_children = $this->wpdb->get_col(
  162. $this->wpdb->prepare(
  163. "SELECT ID FROM {$this->wpdb->posts} WHERE post_parent=%d AND post_type != 'revision'",
  164. $master_post_id
  165. )
  166. );
  167. $dup_parent = icl_object_id( $master_post_id, $post_type, false, $lang );
  168. if ( $master_children ) {
  169. foreach ( $master_children as $master_child ) {
  170. $dup_child = icl_object_id( $master_child, $post_type, false, $lang );
  171. if ( $dup_child ) {
  172. $this->wpdb->update( $this->wpdb->posts, array( 'post_parent' => $dup_parent ), array( 'ID' => $dup_child ) );
  173. }
  174. $this->duplicate_fix_children( $master_child, $lang );
  175. }
  176. }
  177. }
  178. private function duplicate_taxonomies( $master_post_id, $lang ) {
  179. $post_type = get_post_field( 'post_type', $master_post_id );
  180. $taxonomies = get_object_taxonomies( $post_type );
  181. $trid = $this->sitepress->get_element_trid( $master_post_id, 'post_' . $post_type );
  182. if ( $trid ) {
  183. $translations = $this->sitepress->get_element_translations( $trid, 'post_' . $post_type, false, false, true );
  184. if ( isset( $translations[ $lang ] ) ) {
  185. $duplicate_post_id = $translations[ $lang ]->element_id;
  186. /* If we have an existing post, we first of all remove all terms currently attached to it.
  187. * The main reason behind is the removal of the potentially present default category on the post.
  188. */
  189. wp_delete_object_term_relationships( $duplicate_post_id, $taxonomies );
  190. } else {
  191. return false; // translation not found!
  192. }
  193. }
  194. $term_helper = wpml_get_term_translation_util();
  195. $term_helper->duplicate_terms( $master_post_id, $lang );
  196. return true;
  197. }
  198. private function sync_duplicate_password( $master_post_id, $duplicate_post_id ) {
  199. if ( post_password_required( $master_post_id ) ) {
  200. $sql = $this->wpdb->prepare( "UPDATE {$this->wpdb->posts} AS dupl,
  201. (SELECT org.post_password FROM {$this->wpdb->posts} AS org WHERE ID = %d ) AS pwd
  202. SET dupl.post_password = pwd.post_password
  203. WHERE dupl.ID = %d",
  204. array( $master_post_id, $duplicate_post_id ) );
  205. $this->wpdb->query( $sql );
  206. }
  207. }
  208. private function duplicate_custom_fields( $master_post_id, $lang ) {
  209. $duplicate_post_id = false;
  210. $post_type = get_post_field( 'post_type', $master_post_id );
  211. $trid = $this->sitepress->get_element_trid( $master_post_id, 'post_' . $post_type );
  212. if ( $trid ) {
  213. $translations = $this->sitepress->get_element_translations( $trid, 'post_' . $post_type );
  214. if ( isset( $translations[ $lang ] ) ) {
  215. $duplicate_post_id = $translations[ $lang ]->element_id;
  216. } else {
  217. return false; // translation not found!
  218. }
  219. }
  220. $default_exceptions = WPML_Config::get_custom_fields_translation_settings();
  221. $exceptions = apply_filters( 'wpml_duplicate_custom_fields_exceptions', array() );
  222. $exceptions = array_merge( $exceptions, $default_exceptions );
  223. $exceptions = array_unique( $exceptions );
  224. $exceptions_in = ! empty( $exceptions )
  225. ? 'AND meta_key NOT IN ( ' . wpml_prepare_in( $exceptions ) . ') ' : '';
  226. $from_where_string = "FROM {$this->wpdb->postmeta} WHERE post_id = %d " . $exceptions_in;
  227. $post_meta_master = $this->wpdb->get_results( "SELECT meta_key, meta_value " . $this->wpdb->prepare( $from_where_string,
  228. $master_post_id ) );
  229. $this->wpdb->query( "DELETE " . $this->wpdb->prepare( $from_where_string, $duplicate_post_id ) );
  230. foreach ( $post_meta_master as $post_meta ) {
  231. $is_serialized = is_serialized( $post_meta->meta_value );
  232. $meta_data = array(
  233. 'context' => 'custom_field',
  234. 'attribute' => 'value',
  235. 'key' => $post_meta->meta_key,
  236. 'is_serialized' => $is_serialized,
  237. 'post_id' => $duplicate_post_id,
  238. 'master_post_id' => $master_post_id,
  239. );
  240. /**
  241. * @deprecated use 'wpml_duplicate_generic_string' instead, with the same arguments
  242. */
  243. $icl_duplicate_generic_string = apply_filters( 'icl_duplicate_generic_string',
  244. $post_meta->meta_value,
  245. $lang,
  246. $meta_data );
  247. $post_meta->meta_value = $icl_duplicate_generic_string;
  248. $wpml_duplicate_generic_string = apply_filters( 'wpml_duplicate_generic_string',
  249. $post_meta->meta_value,
  250. $lang,
  251. $meta_data );
  252. $post_meta->meta_value = $wpml_duplicate_generic_string;
  253. if ( ! is_serialized( $post_meta->meta_value ) ) {
  254. $post_meta->meta_value = maybe_serialize( $post_meta->meta_value );
  255. }
  256. $this->wpdb->insert( $this->wpdb->postmeta,
  257. array(
  258. 'post_id' => $duplicate_post_id,
  259. 'meta_key' => $post_meta->meta_key,
  260. 'meta_value' => $post_meta->meta_value
  261. ),
  262. array( '%d', '%s', '%s' ) );
  263. }
  264. return true;
  265. }
  266. /**
  267. * @param $lang
  268. * @param $master_post
  269. *
  270. * @return mixed|void
  271. */
  272. private function duplicate_post_content( $lang, $master_post ) {
  273. $duplicated_post_content_meta = array(
  274. 'context' => 'post',
  275. 'attribute' => 'content',
  276. 'key' => $master_post->ID
  277. );
  278. $duplicated_post_content = $master_post->post_content;
  279. $duplicated_post_content = apply_filters( 'icl_duplicate_generic_string', $duplicated_post_content, $lang, $duplicated_post_content_meta );
  280. $duplicated_post_content = apply_filters( 'wpml_duplicate_generic_string', $duplicated_post_content, $lang, $duplicated_post_content_meta );
  281. return $duplicated_post_content;
  282. }
  283. /**
  284. * @param $lang
  285. * @param $master_post
  286. *
  287. * @return mixed|void
  288. */
  289. private function duplicate_post_title( $lang, $master_post ) {
  290. $duplicated_post_title_meta = array(
  291. 'context' => 'post',
  292. 'attribute' => 'title',
  293. 'key' => $master_post->ID
  294. );
  295. $duplicated_post_title = $master_post->post_title;
  296. $duplicated_post_title = apply_filters( 'icl_duplicate_generic_string', $duplicated_post_title, $lang, $duplicated_post_title_meta );
  297. $duplicated_post_title = apply_filters( 'wpml_duplicate_generic_string', $duplicated_post_title, $lang, $duplicated_post_title_meta );
  298. return $duplicated_post_title;
  299. }
  300. /**
  301. * @param string $lang
  302. * @param WP_Post $master_post
  303. *
  304. * @return mixed|void
  305. */
  306. private function duplicate_post_excerpt( $lang, $master_post ) {
  307. $duplicated_post_excerpt_meta = array(
  308. 'context' => 'post',
  309. 'attribute' => 'excerpt',
  310. 'key' => $master_post->ID
  311. );
  312. $duplicated_post_excerpt = $master_post->post_excerpt;
  313. $duplicated_post_excerpt = apply_filters( 'icl_duplicate_generic_string',
  314. $duplicated_post_excerpt,
  315. $lang,
  316. $duplicated_post_excerpt_meta );
  317. $duplicated_post_excerpt = apply_filters( 'wpml_duplicate_generic_string',
  318. $duplicated_post_excerpt,
  319. $lang,
  320. $duplicated_post_excerpt_meta );
  321. return $duplicated_post_excerpt;
  322. }
  323. }