| @@ -4,9 +4,9 @@ | ||
| 4 | 4 | * Manages copy and synchronization of terms and post metas |
| 5 | 5 | * |
| 6 | 6 | * @since 1.2 |
| 7 | 7 | */ |
| 8 | -class PLL_Admin_Sync extends PLL_Sync { | |
| 8 | +class PLL_Admin_Sync { | |
| 9 | 9 | |
| 10 | 10 | /** |
| 11 | 11 | * Constructor |
| 12 | 12 | * |
| @@ -14,14 +14,22 @@ | ||
| 14 | 14 | * |
| 15 | 15 | * @param object $polylang |
| 16 | 16 | */ |
| 17 | 17 | public function __construct( &$polylang ) { |
| 18 | - parent::__construct( $polylang ); | |
| 18 | + $this->model = &$polylang->model; | |
| 19 | + $this->options = &$polylang->options; | |
| 19 | 20 | |
| 20 | 21 | add_filter( 'wp_insert_post_parent', array( $this, 'wp_insert_post_parent' ), 10, 3 ); |
| 21 | - add_filter( 'wp_insert_post_data', array( $this, 'wp_insert_post_data' ) ); | |
| 22 | - add_action( 'rest_api_init', array( $this, 'new_post_translation' ) ); // Block editor | |
| 23 | - add_action( 'add_meta_boxes', array( $this, 'new_post_translation' ), 5 ); // Classic editor, before Types which populates custom fields in same hook with priority 10 | |
| 22 | + add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ), 5, 2 ); // Before Types which populates custom fields in same hook with priority 10 | |
| 23 | + | |
| 24 | + add_action( 'pll_save_post', array( $this, 'pll_save_post' ), 10, 3 ); | |
| 25 | + add_action( 'pll_save_term', array( $this, 'pll_save_term' ), 10, 3 ); | |
| 26 | + | |
| 27 | + if ( $this->options['media_support'] ) { | |
| 28 | + add_action( 'pll_translate_media', array( $this, 'copy_taxonomies' ), 10, 3 ); | |
| 29 | + add_action( 'pll_translate_media', array( $this, 'copy_post_metas' ), 10, 3 ); | |
| 30 | + add_action( 'edit_attachment', array( $this, 'edit_attachment' ) ); | |
| 31 | + } | |
| 24 | 32 | } |
| 25 | 33 | |
| 26 | 34 | /** |
| 27 | 35 | * Translate post parent if exists when using "Add new" ( translation ) |
| @@ -33,125 +41,204 @@ | ||
| 33 | 41 | * @param array $postarr Array of parsed post data |
| 34 | 42 | * @return int |
| 35 | 43 | */ |
| 36 | 44 | public function wp_insert_post_parent( $post_parent, $post_id, $postarr ) { |
| 37 | - if ( isset( $_GET['from_post'], $_GET['new_lang'], $_GET['post_type'] ) ) { | |
| 38 | - check_admin_referer( 'new-post-translation' ); | |
| 39 | - // Make sure not to impact media translations created at the same time | |
| 40 | - if ( $_GET['post_type'] === $postarr['post_type'] && ( $id = wp_get_post_parent_id( (int) $_GET['from_post'] ) ) && $parent = $this->model->post->get_translation( $id, sanitize_key( $_GET['new_lang'] ) ) ) { | |
| 41 | - $post_parent = $parent; | |
| 42 | - } | |
| 43 | - } | |
| 44 | - return $post_parent; | |
| 45 | + // Make sure not to impact media translations created at the same time | |
| 46 | + return isset( $_GET['from_post'], $_GET['new_lang'], $_GET['post_type'] ) && $_GET['post_type'] === $postarr['post_type'] && ( $id = wp_get_post_parent_id( (int) $_GET['from_post'] ) ) && ( $parent = $this->model->post->get_translation( $id, $_GET['new_lang'] ) ) ? $parent : $post_parent; | |
| 45 | 47 | } |
| 46 | 48 | |
| 47 | 49 | /** |
| 48 | - * Copy menu order, comment, ping status and optionally the date when creating a new tanslation | |
| 50 | + * Copy post metas, menu order, comment and ping status when using "Add new" ( translation ) | |
| 51 | + * formerly used dbx_post_advanced deprecated in WP 3.7 | |
| 49 | 52 | * |
| 50 | - * @since 2.5 | |
| 53 | + * @since 1.2 | |
| 51 | 54 | * |
| 52 | - * @param array $data An array of slashed post data. | |
| 53 | - * @return array | |
| 55 | + * @param string $post_type unused | |
| 56 | + * @param object $post current post object | |
| 54 | 57 | */ |
| 55 | - public function wp_insert_post_data( $data ) { | |
| 56 | - if ( isset( $GLOBALS['pagenow'], $_GET['from_post'], $_GET['new_lang'] ) && 'post-new.php' === $GLOBALS['pagenow'] && $this->model->is_translated_post_type( $data['post_type'] ) ) { | |
| 57 | - check_admin_referer( 'new-post-translation' ); | |
| 58 | + public function add_meta_boxes( $post_type, $post ) { | |
| 59 | + if ( 'post-new.php' == $GLOBALS['pagenow'] && isset( $_GET['from_post'], $_GET['new_lang'] ) && $this->model->is_translated_post_type( $post->post_type ) ) { | |
| 60 | + // Capability check already done in post-new.php | |
| 61 | + $from_post_id = (int) $_GET['from_post']; | |
| 62 | + $from_post = get_post( $from_post_id ); | |
| 63 | + $lang = $this->model->get_language( $_GET['new_lang'] ); | |
| 58 | 64 | |
| 59 | - $from_post_id = (int) $_GET['from_post']; | |
| 60 | - $from_post = get_post( $from_post_id ); | |
| 65 | + if ( ! $from_post || ! $lang ) { | |
| 66 | + return; | |
| 67 | + } | |
| 61 | 68 | |
| 69 | + $this->copy_taxonomies( $from_post_id, $post->ID, $lang->slug ); | |
| 70 | + $this->copy_post_metas( $from_post_id, $post->ID, $lang->slug ); | |
| 71 | + | |
| 62 | 72 | foreach ( array( 'menu_order', 'comment_status', 'ping_status' ) as $property ) { |
| 63 | - $data[ $property ] = $from_post->$property; | |
| 73 | + $post->$property = $from_post->$property; | |
| 64 | 74 | } |
| 65 | 75 | |
| 66 | 76 | // Copy the date only if the synchronization is activated |
| 67 | 77 | if ( in_array( 'post_date', $this->options['sync'] ) ) { |
| 68 | - $data['post_date'] = $from_post->post_date; | |
| 69 | - $data['post_date_gmt'] = $from_post->post_date_gmt; | |
| 78 | + $post->post_date = $from_post->post_date; | |
| 79 | + $post->post_date_gmt = $from_post->post_date_gmt; | |
| 70 | 80 | } |
| 81 | + | |
| 82 | + if ( is_sticky( $from_post_id ) ) { | |
| 83 | + stick_post( $post->ID ); | |
| 84 | + } | |
| 71 | 85 | } |
| 86 | + } | |
| 72 | 87 | |
| 73 | - return $data; | |
| 88 | + /** | |
| 89 | + * Get the list of taxonomies to copy or to synchronize | |
| 90 | + * | |
| 91 | + * @since 1.7 | |
| 92 | + * @since 2.1 The `$from`, `$to`, `$lang` parameters were added. | |
| 93 | + * | |
| 94 | + * @param bool $sync true if it is synchronization, false if it is a copy | |
| 95 | + * @param int $from id of the post from which we copy informations, optional, defaults to null | |
| 96 | + * @param int $to id of the post to which we paste informations, optional, defaults to null | |
| 97 | + * @param string $lang language slug, optional, defaults to null | |
| 98 | + * @return array list of taxonomy names | |
| 99 | + */ | |
| 100 | + public function get_taxonomies_to_copy( $sync, $from = null, $to = null, $lang = null ) { | |
| 101 | + $taxonomies = ! $sync || in_array( 'taxonomies', $this->options['sync'] ) ? $this->model->get_translated_taxonomies() : array(); | |
| 102 | + if ( ! $sync || in_array( 'post_format', $this->options['sync'] ) ) { | |
| 103 | + $taxonomies[] = 'post_format'; | |
| 104 | + } | |
| 105 | + | |
| 106 | + /** | |
| 107 | + * Filter the taxonomies to copy or synchronize | |
| 108 | + * | |
| 109 | + * @since 1.7 | |
| 110 | + * @since 2.1 The `$from`, `$to`, `$lang` parameters were added. | |
| 111 | + * | |
| 112 | + * @param array $taxonomies list of taxonomy names | |
| 113 | + * @param bool $sync true if it is synchronization, false if it is a copy | |
| 114 | + * @param int $from id of the post from which we copy informations | |
| 115 | + * @param int $to id of the post to which we paste informations | |
| 116 | + * @param string $lang language slug | |
| 117 | + */ | |
| 118 | + return array_unique( apply_filters( 'pll_copy_taxonomies', $taxonomies, $sync, $from, $to, $lang ) ); | |
| 74 | 119 | } |
| 75 | 120 | |
| 76 | 121 | /** |
| 77 | - * Copy post metas, and taxonomies when using "Add new" ( translation ) | |
| 122 | + * Copy or synchronize terms | |
| 78 | 123 | * |
| 79 | - * @since 2.5 | |
| 124 | + * @since 1.8 | |
| 125 | + * | |
| 126 | + * @param int $from id of the post from which we copy informations | |
| 127 | + * @param int $to id of the post to which we paste informations | |
| 128 | + * @param string $lang language slug | |
| 129 | + * @param bool $sync true if it is synchronization, false if it is a copy, defaults to false | |
| 80 | 130 | */ |
| 81 | - public function new_post_translation() { | |
| 82 | - global $post; | |
| 83 | - static $done = array(); | |
| 131 | + public function copy_taxonomies( $from, $to, $lang, $sync = false ) { | |
| 132 | + // Get taxonomies to sync for this post type | |
| 133 | + $taxonomies = array_intersect( get_post_taxonomies( $from ), $this->get_taxonomies_to_copy( $sync, $from, $to, $lang ) ); | |
| 84 | 134 | |
| 85 | - if ( isset( $GLOBALS['pagenow'], $_GET['from_post'], $_GET['new_lang'] ) && 'post-new.php' === $GLOBALS['pagenow'] && $this->model->is_translated_post_type( $post->post_type ) ) { | |
| 86 | - check_admin_referer( 'new-post-translation' ); | |
| 135 | + // Update the term cache to reduce the number of queries in the loop | |
| 136 | + update_object_term_cache( $sync ? array( $from, $to ) : $from, get_post_type( $from ) ); | |
| 87 | 137 | |
| 88 | - // Capability check already done in post-new.php | |
| 89 | - $from_post_id = (int) $_GET['from_post']; | |
| 90 | - $lang = $this->model->get_language( sanitize_key( $_GET['new_lang'] ) ); | |
| 138 | + // Copy or synchronize terms | |
| 139 | + // FIXME quite a lot of query in foreach | |
| 140 | + foreach ( $taxonomies as $tax ) { | |
| 141 | + $terms = get_the_terms( $from, $tax ); | |
| 91 | 142 | |
| 92 | - if ( ! $from_post_id || ! $lang || ! empty( $done[ $from_post_id ] ) ) { | |
| 93 | - return; | |
| 94 | - } | |
| 143 | + // Translated taxonomy | |
| 144 | + if ( $this->model->is_translated_taxonomy( $tax ) ) { | |
| 145 | + $newterms = array(); | |
| 146 | + if ( is_array( $terms ) ) { | |
| 147 | + foreach ( $terms as $term ) { | |
| 148 | + if ( $term_id = $this->model->term->get_translation( $term->term_id, $lang ) ) { | |
| 149 | + $newterms[] = (int) $term_id; // Cast is important otherwise we get 'numeric' tags | |
| 150 | + } | |
| 151 | + } | |
| 152 | + } | |
| 95 | 153 | |
| 96 | - $done[ $from_post_id ] = true; // Avoid a second duplication in the block editor. Using an array only to allow multiple phpunit tests. | |
| 154 | + // For some reasons, the user may have untranslated terms in the translation. don't forget them. | |
| 155 | + if ( $sync ) { | |
| 156 | + $tr_terms = get_the_terms( $to, $tax ); | |
| 157 | + if ( is_array( $tr_terms ) ) { | |
| 158 | + foreach ( $tr_terms as $term ) { | |
| 159 | + if ( ! $this->model->term->get_translation( $term->term_id, $this->model->post->get_language( $from ) ) ) { | |
| 160 | + $newterms[] = (int) $term->term_id; | |
| 161 | + } | |
| 162 | + } | |
| 163 | + } | |
| 164 | + } | |
| 97 | 165 | |
| 98 | - $this->taxonomies->copy( $from_post_id, $post->ID, $lang->slug ); | |
| 99 | - $this->post_metas->copy( $from_post_id, $post->ID, $lang->slug ); | |
| 166 | + if ( ! empty( $newterms ) || $sync ) { | |
| 167 | + wp_set_object_terms( $to, $newterms, $tax ); // replace terms in translation | |
| 168 | + } | |
| 169 | + } | |
| 100 | 170 | |
| 101 | - if ( is_sticky( $from_post_id ) ) { | |
| 102 | - stick_post( $post->ID ); | |
| 171 | + // Untranslated taxonomy ( post format ) | |
| 172 | + // Don't use simple get_post_format / set_post_format to generalize the case to other taxonomies | |
| 173 | + else { | |
| 174 | + wp_set_object_terms( $to, is_array( $terms ) ? array_map( 'intval', wp_list_pluck( $terms, 'term_id' ) ) : null, $tax ); | |
| 103 | 175 | } |
| 104 | 176 | } |
| 105 | 177 | } |
| 106 | 178 | |
| 107 | 179 | /** |
| 108 | - * Get post fields to synchronize | |
| 180 | + * Copy or synchronize metas (custom fields) | |
| 109 | 181 | * |
| 110 | - * @since 2.4 | |
| 182 | + * @since 0.9 | |
| 111 | 183 | * |
| 112 | - * @param object $post Post object | |
| 113 | - * @return array | |
| 184 | + * @param int $from id of the post from which we copy informations | |
| 185 | + * @param int $to id of the post to which we paste informations | |
| 186 | + * @param string $lang language slug | |
| 187 | + * @param bool $sync true if it is synchronization, false if it is a copy, defaults to false | |
| 114 | 188 | */ |
| 115 | - protected function get_fields_to_sync( $post ) { | |
| 116 | - global $wpdb; | |
| 189 | + public function copy_post_metas( $from, $to, $lang, $sync = false ) { | |
| 190 | + // Copy or synchronize post metas and allow plugins to do the same | |
| 191 | + $metas = get_post_custom( $from ); | |
| 192 | + $keys = array(); | |
| 117 | 193 | |
| 118 | - $postarr = parent::get_fields_to_sync( $post ); | |
| 194 | + // Get public meta keys ( including from translated post in case we just deleted a custom field ) | |
| 195 | + if ( ! $sync || in_array( 'post_meta', $this->options['sync'] ) ) { | |
| 196 | + foreach ( $keys = array_unique( array_merge( array_keys( $metas ), array_keys( get_post_custom( $to ) ) ) ) as $k => $meta_key ) { | |
| 197 | + if ( is_protected_meta( $meta_key ) ) { | |
| 198 | + unset( $keys[ $k ] ); | |
| 199 | + } | |
| 200 | + } | |
| 201 | + } | |
| 119 | 202 | |
| 120 | - // For new drafts, save the date now otherwise it is overriden by WP. Thanks to JoryHogeveen. See #32. | |
| 121 | - if ( in_array( 'post_date', $this->options['sync'] ) && isset( $GLOBALS['pagenow'], $_GET['from_post'], $_GET['new_lang'] ) && 'post-new.php' === $GLOBALS['pagenow'] ) { | |
| 122 | - check_admin_referer( 'new-post-translation' ); | |
| 123 | - | |
| 124 | - unset( $postarr['post_date'] ); | |
| 125 | - unset( $postarr['post_date_gmt'] ); | |
| 126 | - | |
| 127 | - $original = get_post( (int) $_GET['from_post'] ); | |
| 128 | - $wpdb->update( | |
| 129 | - $wpdb->posts, | |
| 130 | - array( | |
| 131 | - 'post_date' => $original->post_date, | |
| 132 | - 'post_date_gmt' => $original->post_date_gmt, | |
| 133 | - ), | |
| 134 | - array( 'ID' => $post->ID ) | |
| 135 | - ); | |
| 203 | + // Add page template and featured image | |
| 204 | + foreach ( array( '_wp_page_template', '_thumbnail_id' ) as $meta ) { | |
| 205 | + if ( ! $sync || in_array( $meta, $this->options['sync'] ) ) { | |
| 206 | + $keys[] = $meta; | |
| 207 | + } | |
| 136 | 208 | } |
| 137 | 209 | |
| 138 | - if ( isset( $GLOBALS['post_type'] ) ) { | |
| 139 | - $post_type = $GLOBALS['post_type']; | |
| 140 | - } elseif ( isset( $_REQUEST['post_type'] ) ) { | |
| 141 | - $post_type = sanitize_key( $_REQUEST['post_type'] ); // 2nd case for quick edit | |
| 142 | - } | |
| 210 | + /** | |
| 211 | + * Filter the custom fields to copy or synchronize | |
| 212 | + * | |
| 213 | + * @since 0.6 | |
| 214 | + * @since 1.9.2 The `$from`, `$to`, `$lang` parameters were added. | |
| 215 | + * | |
| 216 | + * @param array $keys list of custom fields names | |
| 217 | + * @param bool $sync true if it is synchronization, false if it is a copy | |
| 218 | + * @param int $from id of the post from which we copy informations | |
| 219 | + * @param int $to id of the post to which we paste informations | |
| 220 | + * @param string $lang language slug | |
| 221 | + */ | |
| 222 | + $keys = array_unique( apply_filters( 'pll_copy_post_metas', $keys, $sync, $from, $to, $lang ) ); | |
| 143 | 223 | |
| 144 | - // Make sure not to impact media translations when creating them at the same time as post | |
| 145 | - if ( in_array( 'post_parent', $this->options['sync'] ) && ( ! isset( $post_type ) || $post_type !== $post->post_type ) ) { | |
| 146 | - unset( $postarr['post_parent'] ); | |
| 224 | + // And now copy / synchronize | |
| 225 | + foreach ( $keys as $key ) { | |
| 226 | + delete_post_meta( $to, $key ); // The synchronization process of multiple values custom fields is easier if we delete all metas first | |
| 227 | + if ( isset( $metas[ $key ] ) ) { | |
| 228 | + foreach ( $metas[ $key ] as $value ) { | |
| 229 | + // Important: always maybe_unserialize value coming from get_post_custom. See codex. | |
| 230 | + // Thanks to goncalveshugo http://wordpress.org/support/topic/plugin-polylang-pll_copy_post_meta | |
| 231 | + $value = maybe_unserialize( $value ); | |
| 232 | + // Special case for featured images which can be translated | |
| 233 | + add_post_meta( $to, $key, ( '_thumbnail_id' == $key && $tr_value = $this->model->post->get_translation( $value, $lang ) ) ? $tr_value : $value ); | |
| 234 | + } | |
| 235 | + } | |
| 147 | 236 | } |
| 148 | - | |
| 149 | - return $postarr; | |
| 150 | 237 | } |
| 151 | 238 | |
| 152 | 239 | /** |
| 153 | - * Synchronizes post fields in translations | |
| 240 | + * Synchronizes terms and metas in translations | |
| 154 | 241 | * |
| 155 | 242 | * @since 1.2 |
| 156 | 243 | * |
| 157 | 244 | * @param int $post_id post id |
| @@ -158,62 +245,151 @@ | ||
| 158 | 245 | * @param object $post post object |
| 159 | 246 | * @param array $translations post translations |
| 160 | 247 | */ |
| 161 | 248 | public function pll_save_post( $post_id, $post, $translations ) { |
| 162 | - parent::pll_save_post( $post_id, $post, $translations ); | |
| 249 | + global $wpdb; | |
| 163 | 250 | |
| 164 | - // Sticky posts | |
| 165 | - if ( in_array( 'sticky_posts', $this->options['sync'] ) ) { | |
| 166 | - $stickies = get_option( 'sticky_posts' ); | |
| 167 | - if ( isset( $_REQUEST['sticky'] ) && 'sticky' === $_REQUEST['sticky'] ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 168 | - $stickies = array_merge( $stickies, array_values( $translations ) ); | |
| 251 | + // Prepare properties to synchronize | |
| 252 | + foreach ( array( 'comment_status', 'ping_status', 'menu_order' ) as $property ) { | |
| 253 | + if ( in_array( $property, $this->options['sync'] ) ) { | |
| 254 | + $postarr[ $property ] = $post->$property; | |
| 255 | + } | |
| 256 | + } | |
| 257 | + | |
| 258 | + if ( in_array( 'post_date', $this->options['sync'] ) ) { | |
| 259 | + // For new drafts, save the date now otherwise it is overriden by WP. Thanks to JoryHogeveen. See #32. | |
| 260 | + if ( 'post-new.php' === $GLOBALS['pagenow'] && isset( $_GET['from_post'], $_GET['new_lang'] ) ) { | |
| 261 | + $original = get_post( (int) $_GET['from_post'] ); | |
| 262 | + $wpdb->update( | |
| 263 | + $wpdb->posts, array( | |
| 264 | + 'post_date' => $original->post_date, | |
| 265 | + 'post_date_gmt' => $original->post_date_gmt, | |
| 266 | + ), | |
| 267 | + array( 'ID' => $post_id ) | |
| 268 | + ); | |
| 169 | 269 | } else { |
| 170 | - $stickies = array_diff( $stickies, array_values( $translations ) ); | |
| 270 | + $postarr['post_date'] = $post->post_date; | |
| 271 | + $postarr['post_date_gmt'] = $post->post_date_gmt; | |
| 171 | 272 | } |
| 172 | - update_option( 'sticky_posts', array_unique( $stickies ) ); | |
| 173 | 273 | } |
| 274 | + | |
| 275 | + // Synchronize terms and metas in translations | |
| 276 | + foreach ( $translations as $lang => $tr_id ) { | |
| 277 | + if ( ! $tr_id || $tr_id === $post_id ) { | |
| 278 | + continue; | |
| 279 | + } | |
| 280 | + | |
| 281 | + // Synchronize terms and metas | |
| 282 | + $this->copy_taxonomies( $post_id, $tr_id, $lang, true ); | |
| 283 | + $this->copy_post_metas( $post_id, $tr_id, $lang, true ); | |
| 284 | + | |
| 285 | + // Sticky posts | |
| 286 | + if ( in_array( 'sticky_posts', $this->options['sync'] ) ) { | |
| 287 | + isset( $_REQUEST['sticky'] ) && 'sticky' === $_REQUEST['sticky'] ? stick_post( $tr_id ) : unstick_post( $tr_id ); | |
| 288 | + } | |
| 289 | + | |
| 290 | + // Add comment status, ping status, menu order... to synchronization | |
| 291 | + $tr_arr = empty( $postarr ) ? array() : $postarr; | |
| 292 | + | |
| 293 | + if ( isset( $GLOBALS['post_type'] ) ) { | |
| 294 | + $post_type = $GLOBALS['post_type']; | |
| 295 | + } elseif ( isset( $_REQUEST['post_type'] ) ){ | |
| 296 | + $post_type = $_REQUEST['post_type']; // 2nd case for quick edit | |
| 297 | + } | |
| 298 | + | |
| 299 | + // Add post parent to synchronization | |
| 300 | + // Make sure not to impact media translations when creating them at the same time as post | |
| 301 | + // Do not udpate the translation parent if the user set a parent with no translation | |
| 302 | + if ( in_array( 'post_parent', $this->options['sync'] ) && isset( $post_type ) && $post_type === $post->post_type ) { | |
| 303 | + $post_parent = ( $parent_id = wp_get_post_parent_id( $post_id ) ) ? $this->model->post->get_translation( $parent_id, $lang ) : 0; | |
| 304 | + if ( ! ( $parent_id && ! $post_parent ) ) { | |
| 305 | + $tr_arr['post_parent'] = $post_parent; | |
| 306 | + } | |
| 307 | + } | |
| 308 | + | |
| 309 | + // Update all the row at once | |
| 310 | + // Don't use wp_update_post to avoid infinite loop | |
| 311 | + if ( ! empty( $tr_arr ) ) { | |
| 312 | + $wpdb->update( $wpdb->posts, $tr_arr, array( 'ID' => $tr_id ) ); | |
| 313 | + clean_post_cache( $tr_id ); | |
| 314 | + } | |
| 315 | + } | |
| 174 | 316 | } |
| 175 | 317 | |
| 176 | 318 | /** |
| 177 | - * Some backward compatibility with Polylang < 2.3 | |
| 178 | - * allows to call PLL()->sync->copy_post_metas() and PLL()->sync->copy_taxonomies() | |
| 179 | - * used for example in Polylang for WooCommerce | |
| 180 | - * the compatibility is however only partial as the 4th argument $sync is lost | |
| 319 | + * Synchronize translations of a term in all posts | |
| 181 | 320 | * |
| 182 | - * @since 2.3 | |
| 321 | + * @since 1.2 | |
| 183 | 322 | * |
| 184 | - * @param string $func Function name | |
| 185 | - * @param array $args Function arguments | |
| 323 | + * @param int $term_id term id | |
| 324 | + * @param string $taxonomy taxonomy name of the term | |
| 325 | + * @param array $translations translations of the term | |
| 186 | 326 | */ |
| 187 | - public function __call( $func, $args ) { | |
| 188 | - $obj = substr( $func, 5 ); | |
| 327 | + public function pll_save_term( $term_id, $taxonomy, $translations ) { | |
| 328 | + // Check if the taxonomy is synchronized | |
| 329 | + if ( ! $this->model->is_translated_taxonomy( $taxonomy ) || ! in_array( $taxonomy, $this->get_taxonomies_to_copy( true ) ) ) { | |
| 330 | + return; | |
| 331 | + } | |
| 189 | 332 | |
| 190 | - if ( is_object( $this->$obj ) && method_exists( $this->$obj, 'copy' ) ) { | |
| 191 | - if ( WP_DEBUG ) { | |
| 192 | - $debug = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions | |
| 193 | - $i = 1 + empty( $debug[1]['line'] ); // The file and line are in $debug[2] if the function was called using call_user_func | |
| 333 | + // Get all posts associated to this term | |
| 334 | + $posts = get_posts( array( | |
| 335 | + 'numberposts' => -1, | |
| 336 | + 'nopaging' => true, | |
| 337 | + 'post_type' => 'any', | |
| 338 | + 'post_status' => 'any', | |
| 339 | + 'fields' => 'ids', | |
| 340 | + 'tax_query' => array( array( | |
| 341 | + 'taxonomy' => $taxonomy, | |
| 342 | + 'field' => 'id', | |
| 343 | + 'terms' => array_merge( array( $term_id ), array_values( $translations ) ), | |
| 344 | + 'include_children' => false, | |
| 345 | + ) ), | |
| 346 | + ) ); | |
| 194 | 347 | |
| 195 | - trigger_error( // phpcs:ignore WordPress.PHP.DevelopmentFunctions | |
| 196 | - sprintf( | |
| 197 | - '%1$s was called incorrectly in %3$s on line %4$s: the call to PLL()->sync->%1$s() has been deprecated in Polylang 2.3, use PLL()->sync->%2$s->copy() instead.' . "\nError handler", | |
| 198 | - esc_html( $func ), | |
| 199 | - esc_html( $obj ), | |
| 200 | - esc_html( $debug[ $i ]['file'] ), | |
| 201 | - absint( $debug[ $i ]['line'] ) | |
| 202 | - ) | |
| 203 | - ); | |
| 348 | + // Associate translated term to translated post | |
| 349 | + // FIXME quite a lot of query in foreach | |
| 350 | + foreach ( $this->model->get_languages_list() as $language ) { | |
| 351 | + if ( $translated_term = $this->model->term->get( $term_id, $language ) ) { | |
| 352 | + foreach ( $posts as $post_id ) { | |
| 353 | + if ( $translated_post = $this->model->post->get( $post_id, $language ) ) { | |
| 354 | + wp_set_object_terms( $translated_post, $translated_term, $taxonomy, true ); | |
| 355 | + } | |
| 356 | + } | |
| 204 | 357 | } |
| 205 | - return call_user_func_array( array( $this->$obj, 'copy' ), $args ); | |
| 206 | 358 | } |
| 207 | 359 | |
| 208 | - $debug = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions | |
| 209 | - trigger_error( // phpcs:ignore WordPress.PHP.DevelopmentFunctions | |
| 210 | - sprintf( | |
| 211 | - 'Call to undefined function PLL()->sync->%1$s() in %2$s on line %3$s' . "\nError handler", | |
| 212 | - esc_html( $func ), | |
| 213 | - esc_html( $debug[0]['file'] ), | |
| 214 | - absint( $debug[0]['line'] ) | |
| 215 | - ), | |
| 216 | - E_USER_ERROR | |
| 217 | - ); | |
| 360 | + // Synchronize parent in translations | |
| 361 | + // Calling clean_term_cache *after* this is mandatory otherwise the $taxonomy_children option is not correctly updated | |
| 362 | + // 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 | |
| 363 | + // This is the reason to use the edit_term filter and not edited_term | |
| 364 | + // Take care that $_POST contains the only valid values for the current term | |
| 365 | + // FIXME can I synchronize parent without using $_POST instead? | |
| 366 | + if ( isset( $_POST['term_tr_lang'] ) ) { | |
| 367 | + foreach ( $_POST['term_tr_lang'] as $lang => $tr_id ) { | |
| 368 | + if ( $tr_id ) { | |
| 369 | + if ( isset( $_POST['parent'] ) && -1 != $_POST['parent'] ) { // Since WP 3.1 | |
| 370 | + $term_parent = $this->model->term->get_translation( (int) $_POST['parent'], $lang ); | |
| 371 | + } | |
| 372 | + | |
| 373 | + global $wpdb; | |
| 374 | + $wpdb->update( $wpdb->term_taxonomy, | |
| 375 | + array( 'parent' => isset( $term_parent ) ? $term_parent : 0 ), | |
| 376 | + array( 'term_taxonomy_id' => get_term( (int) $tr_id, $taxonomy )->term_taxonomy_id ) | |
| 377 | + ); | |
| 378 | + | |
| 379 | + clean_term_cache( $tr_id, $taxonomy ); // OK since WP 3.9 | |
| 380 | + } | |
| 381 | + } | |
| 382 | + } | |
| 383 | + } | |
| 384 | + | |
| 385 | + /** | |
| 386 | + * Synchronizes terms and metas in translations for media | |
| 387 | + * | |
| 388 | + * @since 1.8 | |
| 389 | + * | |
| 390 | + * @param int $post_id post id | |
| 391 | + */ | |
| 392 | + public function edit_attachment( $post_id ) { | |
| 393 | + $this->pll_save_post( $post_id, get_post( $post_id ), $this->model->post->get_translations( $post_id ) ); | |
| 218 | 394 | } |
| 219 | 395 | } |