| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Manages copy and synchronization of terms and post metas on front |
| 5 |
* |
| 6 |
* @since 2.4 |
| 7 |
*/ |
| 8 |
class PLL_Sync { |
| 9 |
public $taxonomies, $post_metas, $term_meta; |
| 10 |
|
| 11 |
/** |
| 12 |
* Constructor |
| 13 |
* |
| 14 |
* @since 1.2 |
| 15 |
* |
| 16 |
* @param object $polylang |
| 17 |
*/ |
| 18 |
public function __construct( &$polylang ) { |
| 19 |
$this->model = &$polylang->model; |
| 20 |
$this->options = &$polylang->options; |
| 21 |
|
| 22 |
$this->taxonomies = new PLL_Sync_Tax( $polylang ); |
| 23 |
$this->post_metas = new PLL_Sync_Post_Metas( $polylang ); |
| 24 |
$this->term_metas = new PLL_Sync_Term_Metas( $polylang ); |
| 25 |
|
| 26 |
add_action( 'pll_save_post', array( $this, 'pll_save_post' ), 10, 3 ); |
| 27 |
add_action( 'pll_save_term', array( $this, 'sync_term_parent' ), 10, 3 ); |
| 28 |
|
| 29 |
add_action( 'pll_duplicate_term', array( $this->term_metas, 'copy' ), 10, 3 ); |
| 30 |
|
| 31 |
if ( $this->options['media_support'] ) { |
| 32 |
add_action( 'pll_translate_media', array( $this->taxonomies, 'copy' ), 10, 3 ); |
| 33 |
add_action( 'pll_translate_media', array( $this->post_metas, 'copy' ), 10, 3 ); |
| 34 |
add_action( 'edit_attachment', array( $this, 'edit_attachment' ) ); |
| 35 |
} |
| 36 |
|
| 37 |
add_filter( 'pre_update_option_sticky_posts', array( $this, 'sync_sticky_posts' ), 10, 2 ); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Get post fields to synchornize |
| 42 |
* |
| 43 |
* @since 2.4 |
| 44 |
* |
| 45 |
* @param object $post Post object |
| 46 |
* @return array |
| 47 |
*/ |
| 48 |
protected function get_fields_to_sync( $post ) { |
| 49 |
$postarr = array(); |
| 50 |
|
| 51 |
foreach ( array( 'comment_status', 'ping_status', 'menu_order' ) as $property ) { |
| 52 |
if ( in_array( $property, $this->options['sync'] ) ) { |
| 53 |
$postarr[ $property ] = $post->$property; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
if ( in_array( 'post_date', $this->options['sync'] ) ) { |
| 58 |
$postarr['post_date'] = $post->post_date; |
| 59 |
$postarr['post_date_gmt'] = $post->post_date_gmt; |
| 60 |
} |
| 61 |
|
| 62 |
if ( in_array( 'post_parent', $this->options['sync'] ) ) { |
| 63 |
$postarr['post_parent'] = wp_get_post_parent_id( $post->ID ); |
| 64 |
} |
| 65 |
|
| 66 |
return $postarr; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Synchronizes post fields in translations |
| 71 |
* |
| 72 |
* @since 2.4 |
| 73 |
* |
| 74 |
* @param int $post_id post id |
| 75 |
* @param object $post post object |
| 76 |
* @param array $translations post translations |
| 77 |
*/ |
| 78 |
public function pll_save_post( $post_id, $post, $translations ) { |
| 79 |
global $wpdb; |
| 80 |
|
| 81 |
$postarr = $this->get_fields_to_sync( $post ); |
| 82 |
|
| 83 |
if ( ! empty( $postarr ) ) { |
| 84 |
foreach ( $translations as $lang => $tr_id ) { |
| 85 |
if ( ! $tr_id || $tr_id === $post_id ) { |
| 86 |
continue; |
| 87 |
} |
| 88 |
|
| 89 |
$tr_arr = $postarr; |
| 90 |
unset( $tr_arr['post_parent'] ); |
| 91 |
|
| 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; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 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 ); |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Synchronize term parent in translations |
| 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 |
| 113 |
* |
| 114 |
* @since 2.3 |
| 115 |
* |
| 116 |
* @param int $term_id Term id |
| 117 |
* @param string $taxonomy Taxonomy name |
| 118 |
* @param array $translations The list of translations term ids |
| 119 |
*/ |
| 120 |
public function sync_term_parent( $term_id, $taxonomy, $translations ) { |
| 121 |
global $wpdb; |
| 122 |
|
| 123 |
if ( is_taxonomy_hierarchical( $taxonomy ) && $this->model->is_translated_taxonomy( $taxonomy ) ) { |
| 124 |
$term = get_term( $term_id ); |
| 125 |
|
| 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 |
); |
| 133 |
|
| 134 |
clean_term_cache( $tr_id, $taxonomy ); // OK since WP 3.9 |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Synchronizes terms and metas in translations for media |
| 142 |
* |
| 143 |
* @since 1.8 |
| 144 |
* |
| 145 |
* @param int $post_id post id |
| 146 |
*/ |
| 147 |
public function edit_attachment( $post_id ) { |
| 148 |
$this->pll_save_post( $post_id, get_post( $post_id ), $this->model->post->get_translations( $post_id ) ); |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Synchronize sticky posts |
| 153 |
* |
| 154 |
* @since 2.3 |
| 155 |
* |
| 156 |
* @param array $value New option value |
| 157 |
* @param array $old_value Old option value |
| 158 |
* @return array |
| 159 |
*/ |
| 160 |
public function sync_sticky_posts( $value, $old_value ) { |
| 161 |
if ( in_array( 'sticky_posts', $this->options['sync'] ) ) { |
| 162 |
// Stick post |
| 163 |
if ( $sticked = array_diff( $value, $old_value ) ) { |
| 164 |
$translations = $this->model->post->get_translations( reset( $sticked ) ); |
| 165 |
$value = array_unique( array_merge( $value, array_values( $translations ) ) ); |
| 166 |
} |
| 167 |
|
| 168 |
// Unstick post |
| 169 |
if ( $unsticked = array_diff( $old_value, $value ) ) { |
| 170 |
$translations = $this->model->post->get_translations( reset( $unsticked ) ); |
| 171 |
$value = array_unique( array_diff( $value, array_values( $translations ) ) ); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
return $value; |
| 176 |
} |
| 177 |
} |
| 178 |
|