| @@ -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 | * |
| @@ -25,14 +22,10 @@ | ||
| 25 | 22 | $this->taxonomies = new PLL_Sync_Tax( $polylang ); |
| 26 | 23 | $this->post_metas = new PLL_Sync_Post_Metas( $polylang ); |
| 27 | 24 | $this->term_metas = new PLL_Sync_Term_Metas( $polylang ); |
| 28 | 25 | |
| 29 | - add_filter( 'wp_insert_post_parent', array( $this, 'can_sync_post_parent' ), 10, 3 ); | |
| 30 | - add_filter( 'wp_insert_post_data', array( $this, 'can_sync_post_data' ), 10, 2 ); | |
| 31 | - | |
| 32 | 26 | add_action( 'pll_save_post', array( $this, 'pll_save_post' ), 10, 3 ); |
| 33 | - add_action( 'created_term', array( $this, 'sync_term_parent' ), 10, 3 ); | |
| 34 | - add_action( 'edited_term', array( $this, 'sync_term_parent' ), 10, 3 ); | |
| 27 | + add_action( 'pll_save_term', array( $this, 'sync_term_parent' ), 10, 3 ); | |
| 35 | 28 | |
| 36 | 29 | add_action( 'pll_duplicate_term', array( $this->term_metas, 'copy' ), 10, 3 ); |
| 37 | 30 | |
| 38 | 31 | if ( $this->options['media_support'] ) { |
| @@ -73,53 +66,8 @@ | ||
| 73 | 66 | return $postarr; |
| 74 | 67 | } |
| 75 | 68 | |
| 76 | 69 | /** |
| 77 | - * Prevents synchronized post parent modification if the current user hasn't enough rights | |
| 78 | - * | |
| 79 | - * @since 2.6 | |
| 80 | - * | |
| 81 | - * @param int $post_parent Post parent ID | |
| 82 | - * @param int $post_id Post ID, unused | |
| 83 | - * @param array $postarr Array of parsed post data | |
| 84 | - * @return int | |
| 85 | - */ | |
| 86 | - public function can_sync_post_parent( $post_parent, $post_id, $postarr ) { | |
| 87 | - if ( ! empty( $postarr['ID'] ) && ! $this->model->post->current_user_can_synchronize( $postarr['ID'] ) ) { | |
| 88 | - $tr_ids = $this->model->post->get_translations( $postarr['ID'] ); | |
| 89 | - foreach ( $tr_ids as $tr_id ) { | |
| 90 | - if ( $tr_id !== $postarr['ID'] && $post = get_post( $tr_id ) ) { | |
| 91 | - $post_parent = $post->post_parent; | |
| 92 | - break; | |
| 93 | - } | |
| 94 | - } | |
| 95 | - } | |
| 96 | - return $post_parent; | |
| 97 | - } | |
| 98 | - | |
| 99 | - /** | |
| 100 | - * Prevents synchronized post data modification if the current user hasn't enough rights | |
| 101 | - * | |
| 102 | - * @since 2.6 | |
| 103 | - * | |
| 104 | - * @param array $data An array of slashed post data. | |
| 105 | - * @param array $postarr An array of sanitized, but otherwise unmodified post data. | |
| 106 | - * @return array | |
| 107 | - */ | |
| 108 | - public function can_sync_post_data( $data, $postarr ) { | |
| 109 | - if ( ! empty( $postarr['ID'] ) && ! $this->model->post->current_user_can_synchronize( $postarr['ID'] ) ) { | |
| 110 | - foreach ( $this->model->post->get_translations( $postarr['ID'] ) as $tr_id ) { | |
| 111 | - if ( $tr_id !== $postarr['ID'] && $post = get_post( $tr_id ) ) { | |
| 112 | - $to_sync = $this->get_fields_to_sync( $post ); | |
| 113 | - $data = array_merge( $data, $to_sync ); | |
| 114 | - break; | |
| 115 | - } | |
| 116 | - } | |
| 117 | - } | |
| 118 | - return $data; | |
| 119 | - } | |
| 120 | - | |
| 121 | - /** | |
| 122 | 70 | * Synchronizes post fields in translations |
| 123 | 71 | * |
| 124 | 72 | * @since 2.4 |
| 125 | 73 | * |
| @@ -129,35 +77,31 @@ | ||
| 129 | 77 | */ |
| 130 | 78 | public function pll_save_post( $post_id, $post, $translations ) { |
| 131 | 79 | global $wpdb; |
| 132 | 80 | |
| 133 | - if ( $this->model->post->current_user_can_synchronize( $post_id ) ) { | |
| 134 | - $postarr = $this->get_fields_to_sync( $post ); | |
| 81 | + $postarr = $this->get_fields_to_sync( $post ); | |
| 135 | 82 | |
| 136 | - if ( ! empty( $postarr ) ) { | |
| 137 | - foreach ( $translations as $lang => $tr_id ) { | |
| 138 | - if ( ! $tr_id || $tr_id === $post_id ) { | |
| 139 | - continue; | |
| 140 | - } | |
| 83 | + if ( ! empty( $postarr ) ) { | |
| 84 | + foreach ( $translations as $lang => $tr_id ) { | |
| 85 | + if ( ! $tr_id || $tr_id === $post_id ) { | |
| 86 | + continue; | |
| 87 | + } | |
| 141 | 88 | |
| 142 | - $tr_arr = $postarr; | |
| 143 | - unset( $tr_arr['post_parent'] ); | |
| 89 | + $tr_arr = $postarr; | |
| 90 | + unset( $tr_arr['post_parent'] ); | |
| 144 | 91 | |
| 145 | - // Do not udpate the translation parent if the user set a parent with no translation. | |
| 146 | - if ( isset( $postarr['post_parent'] ) ) { | |
| 147 | - $post_parent = $postarr['post_parent'] ? $this->model->post->get_translation( $postarr['post_parent'], $lang ) : 0; | |
| 148 | - if ( ! ( $postarr['post_parent'] && ! $post_parent ) ) { | |
| 149 | - $tr_arr['post_parent'] = $post_parent; | |
| 150 | - } | |
| 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; | |
| 151 | 97 | } |
| 98 | + } | |
| 152 | 99 | |
| 153 | - // Update all the rows at once. | |
| 154 | - if ( ! empty( $tr_arr ) ) { | |
| 155 | - // Don't use wp_update_post to avoid infinite loop. | |
| 156 | - $wpdb->update( $wpdb->posts, $tr_arr, array( 'ID' => $tr_id ) ); | |
| 157 | - clean_post_cache( $tr_id ); | |
| 158 | - } | |
| 159 | - } | |
| 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 ); | |
| 160 | 104 | } |
| 161 | 105 | } |
| 162 | 106 | } |
| 163 | 107 | |
| @@ -163,25 +107,25 @@ | ||
| 163 | 107 | |
| 164 | 108 | /** |
| 165 | 109 | * Synchronize term parent in translations |
| 166 | 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 | |
| 167 | 113 | * |
| 168 | 114 | * @since 2.3 |
| 169 | 115 | * |
| 170 | - * @param int $term_id Term id. | |
| 171 | - * @param int $tt_id Term taxonomy id, not used. | |
| 172 | - * @param string $taxonomy Taxonomy name. | |
| 116 | + * @param int $term_id Term id | |
| 117 | + * @param string $taxonomy Taxonomy name | |
| 118 | + * @param array $translations The list of translations term ids | |
| 173 | 119 | */ |
| 174 | - public function sync_term_parent( $term_id, $tt_id, $taxonomy ) { | |
| 120 | + public function sync_term_parent( $term_id, $taxonomy, $translations ) { | |
| 175 | 121 | global $wpdb; |
| 176 | 122 | |
| 177 | 123 | if ( is_taxonomy_hierarchical( $taxonomy ) && $this->model->is_translated_taxonomy( $taxonomy ) ) { |
| 178 | 124 | $term = get_term( $term_id ); |
| 179 | - $translations = $this->model->term->get_translations( $term_id ); | |
| 180 | 125 | |
| 181 | 126 | foreach ( $translations as $lang => $tr_id ) { |
| 182 | - if ( ! empty( $tr_id ) && $tr_id !== $term_id ) { | |
| 183 | - $tr_parent = $this->model->term->get_translation( $term->parent, $lang ); | |
| 127 | + if ( ! empty( $tr_id ) && $tr_id !== $term_id && $tr_parent = $this->model->term->get_translation( $term->parent, $lang ) ) { | |
| 184 | 128 | $wpdb->update( |
| 185 | 129 | $wpdb->term_taxonomy, |
| 186 | 130 | array( 'parent' => isset( $tr_parent ) ? $tr_parent : 0 ), |
| 187 | 131 | array( 'term_taxonomy_id' => get_term( (int) $tr_id, $taxonomy )->term_taxonomy_id ) |