PageRenderTime 26ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/polylang/modules/sync/admin-sync.php

https://gitlab.com/hop23typhu/bryepoxy
PHP | 358 lines | 180 code | 41 blank | 137 comment | 48 complexity | a610c9ed95393b34fbeebc8ac0eb83dd MD5 | raw file
  1. <?php
  2. /**
  3. * manages copy and synchronization of terms and post metas
  4. *
  5. * @since 1.2
  6. */
  7. class PLL_Admin_Sync {
  8. /**
  9. * constructor
  10. *
  11. * @since 1.2
  12. *
  13. * @param object $polylang
  14. */
  15. public function __construct( &$polylang ) {
  16. $this->model = &$polylang->model;
  17. $this->options = &$polylang->options;
  18. add_filter( 'wp_insert_post_parent', array( $this, 'wp_insert_post_parent' ) );
  19. add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ), 5, 2 ); // before Types which populates custom fields in same hook with priority 10
  20. add_action( 'pll_save_post', array( $this, 'pll_save_post' ), 10, 3 );
  21. add_action( 'pll_save_term', array( $this, 'pll_save_term' ), 10, 3 );
  22. if ( $this->options['media_support'] ) {
  23. add_action( 'pll_translate_media', array( $this, 'copy_taxonomies' ), 10, 3 );
  24. add_action( 'pll_translate_media', array( $this, 'copy_post_metas' ), 10, 3 );
  25. add_action( 'edit_attachment', array( $this, 'edit_attachment' ) );
  26. }
  27. }
  28. /**
  29. * translate post parent if exists when using "Add new" ( translation )
  30. *
  31. * @since 0.6
  32. *
  33. * @param int $post_parent
  34. * @return int
  35. */
  36. public function wp_insert_post_parent( $post_parent ) {
  37. return isset( $_GET['from_post'], $_GET['new_lang'] ) && ( $id = wp_get_post_parent_id( (int) $_GET['from_post'] ) ) && ( $parent = $this->model->post->get_translation( $id, $_GET['new_lang'] ) ) ? $parent : $post_parent;
  38. }
  39. /**
  40. * copy post metas, menu order, comment and ping status when using "Add new" ( translation )
  41. * formerly used dbx_post_advanced deprecated in WP 3.7
  42. *
  43. * @since 1.2
  44. *
  45. * @param string $post_type unused
  46. * @param object $post current post object
  47. */
  48. public function add_meta_boxes( $post_type, $post ) {
  49. if ( 'post-new.php' == $GLOBALS['pagenow'] && isset( $_GET['from_post'], $_GET['new_lang'] ) && $this->model->is_translated_post_type( $post->post_type ) ) {
  50. // capability check already done in post-new.php
  51. $from_post_id = (int) $_GET['from_post'];
  52. $from_post = get_post( $from_post_id );
  53. $lang = $this->model->get_language( $_GET['new_lang'] );
  54. if ( ! $from_post || ! $lang ) {
  55. return;
  56. }
  57. $this->copy_taxonomies( $from_post_id, $post->ID, $lang->slug );
  58. $this->copy_post_metas( $from_post_id, $post->ID, $lang->slug );
  59. foreach ( array( 'menu_order', 'comment_status', 'ping_status' ) as $property ) {
  60. $post->$property = $from_post->$property;
  61. }
  62. if ( is_sticky( $from_post_id ) ) {
  63. stick_post( $post->ID );
  64. }
  65. }
  66. }
  67. /**
  68. * get the list of taxonomies to copy or to synchronize
  69. *
  70. * @since 1.7
  71. *
  72. * @param bool $sync true if it is synchronization, false if it is a copy
  73. * @return array list of taxonomy names
  74. */
  75. public function get_taxonomies_to_copy( $sync ) {
  76. $taxonomies = ! $sync || in_array( 'taxonomies', $this->options['sync'] ) ? $this->model->get_translated_taxonomies() : array();
  77. if ( ! $sync || in_array( 'post_format', $this->options['sync'] ) ) {
  78. $taxonomies[] = 'post_format';
  79. }
  80. /**
  81. * Filter the taxonomies to copy or synchronize
  82. *
  83. * @since 1.7
  84. *
  85. * @param array $taxonomies list of taxonomy names
  86. * @param bool $sync true if it is synchronization, false if it is a copy
  87. */
  88. return array_unique( apply_filters( 'pll_copy_taxonomies', $taxonomies, $sync ) );
  89. }
  90. /**
  91. * copy or synchronize terms
  92. *
  93. * @since 1.8
  94. *
  95. * @param int $from id of the post from which we copy informations
  96. * @param int $to id of the post to which we paste informations
  97. * @param string $lang language slug
  98. * @param bool $sync true if it is synchronization, false if it is a copy, defaults to false
  99. */
  100. public function copy_taxonomies( $from, $to, $lang, $sync = false ) {
  101. // get taxonomies to sync for this post type
  102. $taxonomies = array_intersect( get_post_taxonomies( $from ), $this->get_taxonomies_to_copy( $sync ) );
  103. // update the term cache to reduce the number of queries in the loop
  104. update_object_term_cache( $sync ? array( $from, $to ) : $from, get_post_type( $from ) );
  105. // copy or synchronize terms
  106. // FIXME quite a lot of query in foreach
  107. foreach ( $taxonomies as $tax ) {
  108. $terms = get_the_terms( $from, $tax );
  109. // translated taxonomy
  110. if ( $this->model->is_translated_taxonomy( $tax ) ) {
  111. $newterms = array();
  112. if ( is_array( $terms ) ) {
  113. foreach ( $terms as $term ) {
  114. if ( $term_id = $this->model->term->get_translation( $term->term_id, $lang ) ) {
  115. $newterms[] = (int) $term_id; // cast is important otherwise we get 'numeric' tags
  116. }
  117. }
  118. }
  119. // for some reasons, the user may have untranslated terms in the translation. don't forget them.
  120. if ( $sync ) {
  121. $tr_terms = get_the_terms( $to, $tax );
  122. if ( is_array( $tr_terms ) ) {
  123. foreach ( $tr_terms as $term ) {
  124. if ( ! $this->model->term->get_translation( $term->term_id, $this->model->post->get_language( $from ) ) ) {
  125. $newterms[] = (int) $term->term_id;
  126. }
  127. }
  128. }
  129. }
  130. if ( ! empty( $newterms ) || $sync ) {
  131. wp_set_object_terms( $to, $newterms, $tax ); // replace terms in translation
  132. }
  133. }
  134. // untranslated taxonomy ( post format )
  135. // don't use simple get_post_format / set_post_format to generalize the case to other taxonomies
  136. else {
  137. wp_set_object_terms( $to, is_array( $terms ) ? array_map( 'intval', wp_list_pluck( $terms, 'term_id' ) ) : null, $tax );
  138. }
  139. }
  140. }
  141. /**
  142. * copy or synchronize metas (custom fields)
  143. *
  144. * @since 0.9
  145. *
  146. * @param int $from id of the post from which we copy informations
  147. * @param int $to id of the post to which we paste informations
  148. * @param string $lang language slug
  149. * @param bool $sync true if it is synchronization, false if it is a copy, defaults to false
  150. */
  151. public function copy_post_metas( $from, $to, $lang, $sync = false ) {
  152. // copy or synchronize post metas and allow plugins to do the same
  153. $metas = get_post_custom( $from );
  154. $keys = array();
  155. // get public meta keys ( including from translated post in case we just deleted a custom field )
  156. if ( ! $sync || in_array( 'post_meta', $this->options['sync'] ) ) {
  157. foreach ( $keys = array_unique( array_merge( array_keys( $metas ), array_keys( get_post_custom( $to ) ) ) ) as $k => $meta_key ) {
  158. if ( is_protected_meta( $meta_key ) ) {
  159. unset( $keys[ $k ] );
  160. }
  161. }
  162. }
  163. // add page template and featured image
  164. foreach ( array( '_wp_page_template', '_thumbnail_id' ) as $meta ) {
  165. if ( ! $sync || in_array( $meta, $this->options['sync'] ) ) {
  166. $keys[] = $meta;
  167. }
  168. }
  169. /**
  170. * Filter the custom fields to copy or synchronize
  171. *
  172. * @since 0.6
  173. * @since 1.9.2 The `$from`, `$to`, `$lang` parameters were added.
  174. *
  175. * @param array $keys list of custom fields names
  176. * @param bool $sync true if it is synchronization, false if it is a copy
  177. * @param int $from id of the post from which we copy informations
  178. * @param int $to id of the post to which we paste informations
  179. * @param string $lang language slug
  180. */
  181. $keys = array_unique( apply_filters( 'pll_copy_post_metas', $keys, $sync, $from, $to, $lang ) );
  182. // and now copy / synchronize
  183. foreach ( $keys as $key ) {
  184. delete_post_meta( $to, $key ); // the synchronization process of multiple values custom fields is easier if we delete all metas first
  185. if ( isset( $metas[ $key ] ) ) {
  186. foreach ( $metas[ $key ] as $value ) {
  187. // important: always maybe_unserialize value coming from get_post_custom. See codex.
  188. // thanks to goncalveshugo http://wordpress.org/support/topic/plugin-polylang-pll_copy_post_meta
  189. $value = maybe_unserialize( $value );
  190. // special case for featured images which can be translated
  191. add_post_meta( $to, $key, ( '_thumbnail_id' == $key && $tr_value = $this->model->post->get_translation( $value, $lang ) ) ? $tr_value : $value );
  192. }
  193. }
  194. }
  195. }
  196. /**
  197. * synchronizes terms and metas in translations
  198. *
  199. * @since 1.2
  200. *
  201. * @param int $post_id post id
  202. * @param object $post post object
  203. * @param array $translations post translations
  204. */
  205. public function pll_save_post( $post_id, $post, $translations ) {
  206. global $wpdb;
  207. // prepare properties to synchronize
  208. foreach ( array( 'comment_status', 'ping_status', 'menu_order', 'post_date' ) as $property ) {
  209. if ( in_array( $property, $this->options['sync'] ) ) {
  210. $postarr[ $property ] = $post->$property;
  211. }
  212. }
  213. if ( in_array( 'post_date', $this->options['sync'] ) ) {
  214. $postarr['post_date_gmt'] = $post->post_date_gmt;
  215. }
  216. // synchronize terms and metas in translations
  217. foreach ( $translations as $lang => $tr_id ) {
  218. if ( ! $tr_id || $tr_id === $post_id ) {
  219. continue;
  220. }
  221. // synchronize terms and metas
  222. $this->copy_taxonomies( $post_id, $tr_id, $lang, true );
  223. $this->copy_post_metas( $post_id, $tr_id, $lang, true );
  224. // sticky posts
  225. if ( in_array( 'sticky_posts', $this->options['sync'] ) ) {
  226. isset( $_REQUEST['sticky'] ) ? stick_post( $tr_id ) : unstick_post( $tr_id );
  227. }
  228. // add comment status, ping status, menu order... to synchronization
  229. $tr_arr = empty( $postarr ) ? array() : $postarr;
  230. // add post parent to synchronization
  231. // do not udpate the translation parent if the user set a parent with no translation
  232. if ( in_array( 'post_parent', $this->options['sync'] ) ) {
  233. $post_parent = ( $parent_id = wp_get_post_parent_id( $post_id ) ) ? $this->model->post->get_translation( $parent_id, $lang ) : 0;
  234. if ( ! ( $parent_id && ! $post_parent ) ) {
  235. $tr_arr['post_parent'] = $post_parent;
  236. }
  237. }
  238. // update all the row at once
  239. // don't use wp_update_post to avoid infinite loop
  240. if ( ! empty( $tr_arr ) ) {
  241. $wpdb->update( $wpdb->posts, $tr_arr, array( 'ID' => $tr_id ) );
  242. clean_post_cache( $tr_id );
  243. }
  244. }
  245. }
  246. /**
  247. * synchronize translations of a term in all posts
  248. *
  249. * @since 1.2
  250. *
  251. * @param int $term_id term id
  252. * @param string $taxonomy taxonomy name of the term
  253. * @param array $translations translations of the term
  254. */
  255. public function pll_save_term( $term_id, $taxonomy, $translations ) {
  256. // check if the taxonomy is synchronized
  257. if ( ! $this->model->is_translated_taxonomy( $taxonomy ) || ! in_array( $taxonomy, $this->get_taxonomies_to_copy( true ) ) ) {
  258. return;
  259. }
  260. // get all posts associated to this term
  261. $posts = get_posts( array(
  262. 'numberposts' => -1,
  263. 'nopaging' => true,
  264. 'post_type' => 'any',
  265. 'post_status' => 'any',
  266. 'fields' => 'ids',
  267. 'tax_query' => array( array(
  268. 'taxonomy' => $taxonomy,
  269. 'field' => 'id',
  270. 'terms' => array_merge( array( $term_id ), array_values( $translations ) ),
  271. 'include_children' => false,
  272. ) ),
  273. ) );
  274. // associate translated term to translated post
  275. // FIXME quite a lot of query in foreach
  276. foreach ( $this->model->get_languages_list() as $language ) {
  277. if ( $translated_term = $this->model->term->get( $term_id, $language ) ) {
  278. foreach ( $posts as $post_id ) {
  279. if ( $translated_post = $this->model->post->get( $post_id, $language ) ) {
  280. wp_set_object_terms( $translated_post, $translated_term, $taxonomy, true );
  281. }
  282. }
  283. }
  284. }
  285. // synchronize parent in translations
  286. // calling clean_term_cache *after* this is mandatory otherwise the $taxonomy_children option is not correctly updated
  287. // before WP 3.9 clean_term_cache could be called ( efficiently ) only one time due to static array which prevented to update the option more than once
  288. // this is the reason to use the edit_term filter and not edited_term
  289. // take care that $_POST contains the only valid values for the current term
  290. // FIXME can I synchronize parent without using $_POST instead?
  291. if ( isset( $_POST['term_tr_lang'] ) ) {
  292. foreach ( $_POST['term_tr_lang'] as $lang => $tr_id ) {
  293. if ( $tr_id ) {
  294. if ( isset( $_POST['parent'] ) && -1 != $_POST['parent'] ) { // since WP 3.1
  295. $term_parent = $this->model->term->get_translation( (int) $_POST['parent'], $lang );
  296. }
  297. global $wpdb;
  298. $wpdb->update( $wpdb->term_taxonomy,
  299. array( 'parent' => isset( $term_parent ) ? $term_parent : 0 ),
  300. array( 'term_taxonomy_id' => get_term( (int) $tr_id, $taxonomy )->term_taxonomy_id )
  301. );
  302. clean_term_cache( $tr_id, $taxonomy ); // OK since WP 3.9
  303. }
  304. }
  305. }
  306. }
  307. /**
  308. * synchronizes terms and metas in translations for media
  309. *
  310. * @since 1.8
  311. *
  312. * @param int $post_id post id
  313. */
  314. public function edit_attachment( $post_id ) {
  315. $this->pll_save_post( $post_id, get_post( $post_id ), $this->model->post->get_translations( $post_id ) );
  316. }
  317. }