| @@ -1,8 +1,5 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * @package Polylang | |
| 4 | - */ | |
| 5 | 2 | |
| 6 | 3 | /** |
| 7 | 4 | * Manages copy and synchronization of terms and post metas on front |
| 8 | 5 | * |
| @@ -8,36 +5,11 @@ | ||
| 8 | 5 | * |
| 9 | 6 | * @since 2.4 |
| 10 | 7 | */ |
| 11 | 8 | class PLL_Sync { |
| 12 | - /** | |
| 13 | - * @var PLL_Sync_Tax | |
| 14 | - */ | |
| 15 | - public $taxonomies; | |
| 9 | + public $taxonomies, $post_metas, $term_meta; | |
| 16 | 10 | |
| 17 | 11 | /** |
| 18 | - * @var PLL_Sync_Post_Metas | |
| 19 | - */ | |
| 20 | - public $post_metas; | |
| 21 | - | |
| 22 | - /** | |
| 23 | - * @var PLL_Sync_Term_Metas | |
| 24 | - */ | |
| 25 | - public $term_metas; | |
| 26 | - | |
| 27 | - /** | |
| 28 | - * Stores the plugin options. | |
| 29 | - * | |
| 30 | - * @var array | |
| 31 | - */ | |
| 32 | - protected $options; | |
| 33 | - | |
| 34 | - /** | |
| 35 | - * @var PLL_Model | |
| 36 | - */ | |
| 37 | - protected $model; | |
| 38 | - | |
| 39 | - /** | |
| 40 | 12 | * Constructor |
| 41 | 13 | * |
| 42 | 14 | * @since 1.2 |
| 43 | 15 | * |
| @@ -50,14 +22,10 @@ | ||
| 50 | 22 | $this->taxonomies = new PLL_Sync_Tax( $polylang ); |
| 51 | 23 | $this->post_metas = new PLL_Sync_Post_Metas( $polylang ); |
| 52 | 24 | $this->term_metas = new PLL_Sync_Term_Metas( $polylang ); |
| 53 | 25 | |
| 54 | - add_filter( 'wp_insert_post_parent', array( $this, 'can_sync_post_parent' ), 10, 3 ); | |
| 55 | - add_filter( 'wp_insert_post_data', array( $this, 'can_sync_post_data' ), 10, 2 ); | |
| 56 | - | |
| 57 | 26 | add_action( 'pll_save_post', array( $this, 'pll_save_post' ), 10, 3 ); |
| 58 | - add_action( 'created_term', array( $this, 'sync_term_parent' ), 10, 3 ); | |
| 59 | - add_action( 'edited_term', array( $this, 'sync_term_parent' ), 10, 3 ); | |
| 27 | + add_action( 'pll_save_term', array( $this, 'sync_term_parent' ), 10, 3 ); | |
| 60 | 28 | |
| 61 | 29 | add_action( 'pll_duplicate_term', array( $this->term_metas, 'copy' ), 10, 3 ); |
| 62 | 30 | |
| 63 | 31 | if ( $this->options['media_support'] ) { |
| @@ -69,13 +37,13 @@ | ||
| 69 | 37 | add_filter( 'pre_update_option_sticky_posts', array( $this, 'sync_sticky_posts' ), 10, 2 ); |
| 70 | 38 | } |
| 71 | 39 | |
| 72 | 40 | /** |
| 73 | - * Get post fields to synchronize. | |
| 41 | + * Get post fields to synchornize | |
| 74 | 42 | * |
| 75 | 43 | * @since 2.4 |
| 76 | 44 | * |
| 77 | - * @param WP_Post $post Post object. | |
| 45 | + * @param object $post Post object | |
| 78 | 46 | * @return array |
| 79 | 47 | */ |
| 80 | 48 | protected function get_fields_to_sync( $post ) { |
| 81 | 49 | $postarr = array(); |
| @@ -98,92 +66,42 @@ | ||
| 98 | 66 | return $postarr; |
| 99 | 67 | } |
| 100 | 68 | |
| 101 | 69 | /** |
| 102 | - * Prevents synchronized post parent modification if the current user hasn't enough rights | |
| 70 | + * Synchronizes post fields in translations | |
| 103 | 71 | * |
| 104 | - * @since 2.6 | |
| 105 | - * | |
| 106 | - * @param int $post_parent Post parent ID | |
| 107 | - * @param int $post_id Post ID, unused | |
| 108 | - * @param array $postarr Array of parsed post data | |
| 109 | - * @return int | |
| 110 | - */ | |
| 111 | - public function can_sync_post_parent( $post_parent, $post_id, $postarr ) { | |
| 112 | - if ( ! empty( $postarr['ID'] ) && ! $this->model->post->current_user_can_synchronize( $postarr['ID'] ) ) { | |
| 113 | - $tr_ids = $this->model->post->get_translations( $postarr['ID'] ); | |
| 114 | - foreach ( $tr_ids as $tr_id ) { | |
| 115 | - if ( $tr_id !== $postarr['ID'] && $post = get_post( $tr_id ) ) { | |
| 116 | - $post_parent = $post->post_parent; | |
| 117 | - break; | |
| 118 | - } | |
| 119 | - } | |
| 120 | - } | |
| 121 | - return $post_parent; | |
| 122 | - } | |
| 123 | - | |
| 124 | - /** | |
| 125 | - * Prevents synchronized post data modification if the current user hasn't enough rights | |
| 126 | - * | |
| 127 | - * @since 2.6 | |
| 128 | - * | |
| 129 | - * @param array $data An array of slashed post data. | |
| 130 | - * @param array $postarr An array of sanitized, but otherwise unmodified post data. | |
| 131 | - * @return array | |
| 132 | - */ | |
| 133 | - public function can_sync_post_data( $data, $postarr ) { | |
| 134 | - if ( ! empty( $postarr['ID'] ) && ! $this->model->post->current_user_can_synchronize( $postarr['ID'] ) ) { | |
| 135 | - foreach ( $this->model->post->get_translations( $postarr['ID'] ) as $tr_id ) { | |
| 136 | - if ( $tr_id !== $postarr['ID'] && $post = get_post( $tr_id ) ) { | |
| 137 | - $to_sync = $this->get_fields_to_sync( $post ); | |
| 138 | - $data = array_merge( $data, $to_sync ); | |
| 139 | - break; | |
| 140 | - } | |
| 141 | - } | |
| 142 | - } | |
| 143 | - return $data; | |
| 144 | - } | |
| 145 | - | |
| 146 | - /** | |
| 147 | - * Synchronizes post fields in translations. | |
| 148 | - * | |
| 149 | 72 | * @since 2.4 |
| 150 | 73 | * |
| 151 | - * @param int $post_id Post id. | |
| 152 | - * @param WP_Post $post Post object. | |
| 153 | - * @param int[] $translations Post translations. | |
| 154 | - * @return void | |
| 74 | + * @param int $post_id post id | |
| 75 | + * @param object $post post object | |
| 76 | + * @param array $translations post translations | |
| 155 | 77 | */ |
| 156 | 78 | public function pll_save_post( $post_id, $post, $translations ) { |
| 157 | 79 | global $wpdb; |
| 158 | 80 | |
| 159 | - if ( $this->model->post->current_user_can_synchronize( $post_id ) ) { | |
| 160 | - $postarr = $this->get_fields_to_sync( $post ); | |
| 81 | + $postarr = $this->get_fields_to_sync( $post ); | |
| 161 | 82 | |
| 162 | - if ( ! empty( $postarr ) ) { | |
| 163 | - foreach ( $translations as $lang => $tr_id ) { | |
| 164 | - if ( ! $tr_id || $tr_id === $post_id ) { | |
| 165 | - continue; | |
| 166 | - } | |
| 83 | + if ( ! empty( $postarr ) ) { | |
| 84 | + foreach ( $translations as $lang => $tr_id ) { | |
| 85 | + if ( ! $tr_id || $tr_id === $post_id ) { | |
| 86 | + continue; | |
| 87 | + } | |
| 167 | 88 | |
| 168 | - $tr_arr = $postarr; | |
| 169 | - unset( $tr_arr['post_parent'] ); | |
| 89 | + $tr_arr = $postarr; | |
| 90 | + unset( $tr_arr['post_parent'] ); | |
| 170 | 91 | |
| 171 | - // Do not udpate the translation parent if the user set a parent with no translation. | |
| 172 | - if ( isset( $postarr['post_parent'] ) ) { | |
| 173 | - $post_parent = $postarr['post_parent'] ? $this->model->post->get_translation( $postarr['post_parent'], $lang ) : 0; | |
| 174 | - if ( ! ( $postarr['post_parent'] && ! $post_parent ) ) { | |
| 175 | - $tr_arr['post_parent'] = $post_parent; | |
| 176 | - } | |
| 92 | + // Do not udpate the translation parent if the user set a parent with no translation | |
| 93 | + if ( isset( $postarr['post_parent'] ) ) { | |
| 94 | + $post_parent = $postarr['post_parent'] ? $this->model->post->get_translation( $postarr['post_parent'], $lang ) : 0; | |
| 95 | + if ( ! ( $postarr['post_parent'] && ! $post_parent ) ) { | |
| 96 | + $tr_arr['post_parent'] = $post_parent; | |
| 177 | 97 | } |
| 98 | + } | |
| 178 | 99 | |
| 179 | - // Update all the rows at once. | |
| 180 | - if ( ! empty( $tr_arr ) ) { | |
| 181 | - // Don't use wp_update_post to avoid infinite loop. | |
| 182 | - $wpdb->update( $wpdb->posts, $tr_arr, array( 'ID' => $tr_id ) ); | |
| 183 | - clean_post_cache( $tr_id ); | |
| 184 | - } | |
| 185 | - } | |
| 100 | + // Update all the row at once | |
| 101 | + // Don't use wp_update_post to avoid infinite loop | |
| 102 | + $wpdb->update( $wpdb->posts, $tr_arr, array( 'ID' => $tr_id ) ); | |
| 103 | + clean_post_cache( $tr_id ); | |
| 186 | 104 | } |
| 187 | 105 | } |
| 188 | 106 | } |
| 189 | 107 | |
| @@ -189,40 +107,32 @@ | ||
| 189 | 107 | |
| 190 | 108 | /** |
| 191 | 109 | * Synchronize term parent in translations |
| 192 | 110 | * Calling clean_term_cache *after* this is mandatory otherwise the $taxonomy_children option is not correctly updated |
| 111 | + * 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 | |
| 112 | + * This is the reason to use the edit_term filter and not edited_term | |
| 193 | 113 | * |
| 194 | 114 | * @since 2.3 |
| 195 | 115 | * |
| 196 | - * @param int $term_id Term id. | |
| 197 | - * @param int $tt_id Term taxonomy id, not used. | |
| 198 | - * @param string $taxonomy Taxonomy name. | |
| 199 | - * @return void | |
| 116 | + * @param int $term_id Term id | |
| 117 | + * @param string $taxonomy Taxonomy name | |
| 118 | + * @param array $translations The list of translations term ids | |
| 200 | 119 | */ |
| 201 | - public function sync_term_parent( $term_id, $tt_id, $taxonomy ) { | |
| 120 | + public function sync_term_parent( $term_id, $taxonomy, $translations ) { | |
| 202 | 121 | global $wpdb; |
| 203 | 122 | |
| 204 | 123 | if ( is_taxonomy_hierarchical( $taxonomy ) && $this->model->is_translated_taxonomy( $taxonomy ) ) { |
| 205 | 124 | $term = get_term( $term_id ); |
| 206 | 125 | |
| 207 | - if ( $term instanceof WP_Term ) { | |
| 208 | - $translations = $this->model->term->get_translations( $term_id ); | |
| 126 | + foreach ( $translations as $lang => $tr_id ) { | |
| 127 | + if ( ! empty( $tr_id ) && $tr_id !== $term_id && $tr_parent = $this->model->term->get_translation( $term->parent, $lang ) ) { | |
| 128 | + $wpdb->update( | |
| 129 | + $wpdb->term_taxonomy, | |
| 130 | + array( 'parent' => isset( $tr_parent ) ? $tr_parent : 0 ), | |
| 131 | + array( 'term_taxonomy_id' => get_term( (int) $tr_id, $taxonomy )->term_taxonomy_id ) | |
| 132 | + ); | |
| 209 | 133 | |
| 210 | - foreach ( $translations as $lang => $tr_id ) { | |
| 211 | - if ( ! empty( $tr_id ) && $tr_id !== $term_id ) { | |
| 212 | - $tr_parent = $this->model->term->get_translation( $term->parent, $lang ); | |
| 213 | - $tr_term = get_term( (int) $tr_id, $taxonomy ); | |
| 214 | - | |
| 215 | - if ( $tr_term instanceof WP_Term ) { | |
| 216 | - $wpdb->update( | |
| 217 | - $wpdb->term_taxonomy, | |
| 218 | - array( 'parent' => $tr_parent ? $tr_parent : 0 ), | |
| 219 | - array( 'term_taxonomy_id' => $tr_term->term_taxonomy_id ) | |
| 220 | - ); | |
| 221 | - | |
| 222 | - clean_term_cache( $tr_id, $taxonomy ); // OK since WP 3.9. | |
| 223 | - } | |
| 224 | - } | |
| 134 | + clean_term_cache( $tr_id, $taxonomy ); // OK since WP 3.9 | |
| 225 | 135 | } |
| 226 | 136 | } |
| 227 | 137 | } |
| 228 | 138 | } |
| @@ -232,9 +142,8 @@ | ||
| 232 | 142 | * |
| 233 | 143 | * @since 1.8 |
| 234 | 144 | * |
| 235 | 145 | * @param int $post_id post id |
| 236 | - * @return void | |
| 237 | 146 | */ |
| 238 | 147 | public function edit_attachment( $post_id ) { |
| 239 | 148 | $this->pll_save_post( $post_id, get_post( $post_id ), $this->model->post->get_translations( $post_id ) ); |
| 240 | 149 | } |
| @@ -239,15 +148,15 @@ | ||
| 239 | 148 | $this->pll_save_post( $post_id, get_post( $post_id ), $this->model->post->get_translations( $post_id ) ); |
| 240 | 149 | } |
| 241 | 150 | |
| 242 | 151 | /** |
| 243 | - * Synchronize sticky posts. | |
| 152 | + * Synchronize sticky posts | |
| 244 | 153 | * |
| 245 | 154 | * @since 2.3 |
| 246 | 155 | * |
| 247 | - * @param int[] $value New option value. | |
| 248 | - * @param int[] $old_value Old option value. | |
| 249 | - * @return int[] | |
| 156 | + * @param array $value New option value | |
| 157 | + * @param array $old_value Old option value | |
| 158 | + * @return array | |
| 250 | 159 | */ |
| 251 | 160 | public function sync_sticky_posts( $value, $old_value ) { |
| 252 | 161 | if ( in_array( 'sticky_posts', $this->options['sync'] ) ) { |
| 253 | 162 | // Stick post |