1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Terms sync module. |
4
|
|
|
* |
5
|
|
|
* @package automattic/jetpack-sync |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Automattic\Jetpack\Sync\Modules; |
9
|
|
|
|
10
|
|
|
use Automattic\Jetpack\Sync\Defaults; |
11
|
|
|
use Automattic\Jetpack\Sync\Settings; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class to handle sync for terms. |
15
|
|
|
*/ |
16
|
|
|
class Terms extends Module { |
17
|
|
|
/** |
18
|
|
|
* Whitelist for taxonomies we want to sync. |
19
|
|
|
* |
20
|
|
|
* @access private |
21
|
|
|
* |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
private $taxonomy_whitelist; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Sync module name. |
28
|
|
|
* |
29
|
|
|
* @access public |
30
|
|
|
* |
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
|
|
public function name() { |
34
|
|
|
return 'terms'; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Allows WordPress.com servers to retrieve a term object via the sync API. |
39
|
|
|
* |
40
|
|
|
* @param string $object_type The type of object. |
41
|
|
|
* @param int $id The id of the object. |
42
|
|
|
* |
43
|
|
|
* @return bool|\WP_Term |
44
|
|
|
*/ |
45
|
|
|
public function get_object_by_id( $object_type, $id ) { |
46
|
|
|
if ( 'term' === $object_type ) { |
47
|
|
|
$term = get_term( intval( $id ) ); |
48
|
|
|
|
49
|
|
|
if ( is_wp_error( $term ) && $term->get_error_code() === 'invalid_taxonomy' ) { |
50
|
|
|
// Fet raw term. |
51
|
|
|
global $wpdb; |
52
|
|
|
$columns = implode( ', ', array_unique( array_merge( Defaults::$default_term_checksum_columns, array( 'term_group' ) ) ) ); |
|
|
|
|
53
|
|
|
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
54
|
|
|
$term = $wpdb->get_row( $wpdb->prepare( "SELECT $columns FROM $wpdb->terms WHERE term_id = %d", $id ) ); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $term ? $term : false; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return false; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Initialize terms action listeners. |
65
|
|
|
* |
66
|
|
|
* @access public |
67
|
|
|
* |
68
|
|
|
* @param callable $callable Action handler callable. |
69
|
|
|
*/ |
70
|
|
|
public function init_listeners( $callable ) { |
71
|
|
|
add_action( 'created_term', array( $this, 'save_term_handler' ), 10, 3 ); |
72
|
|
|
add_action( 'edited_term', array( $this, 'save_term_handler' ), 10, 3 ); |
73
|
|
|
add_action( 'jetpack_sync_save_term', $callable ); |
74
|
|
|
add_action( 'jetpack_sync_add_term', $callable ); |
75
|
|
|
add_action( 'delete_term', $callable, 10, 4 ); |
76
|
|
|
add_action( 'set_object_terms', $callable, 10, 6 ); |
77
|
|
|
add_action( 'deleted_term_relationships', $callable, 10, 2 ); |
78
|
|
|
add_filter( 'jetpack_sync_before_enqueue_jetpack_sync_save_term', array( $this, 'filter_blacklisted_taxonomies' ) ); |
79
|
|
|
add_filter( 'jetpack_sync_before_enqueue_jetpack_sync_add_term', array( $this, 'filter_blacklisted_taxonomies' ) ); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Initialize terms action listeners for full sync. |
84
|
|
|
* |
85
|
|
|
* @access public |
86
|
|
|
* |
87
|
|
|
* @param callable $callable Action handler callable. |
88
|
|
|
*/ |
89
|
|
|
public function init_full_sync_listeners( $callable ) { |
90
|
|
|
add_action( 'jetpack_full_sync_terms', $callable, 10, 2 ); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Initialize the module in the sender. |
95
|
|
|
* |
96
|
|
|
* @access public |
97
|
|
|
*/ |
98
|
|
|
public function init_before_send() { |
99
|
|
|
// Full sync. |
100
|
|
|
add_filter( 'jetpack_sync_before_send_jetpack_full_sync_terms', array( $this, 'expand_term_taxonomy_id' ) ); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Enqueue the terms actions for full sync. |
105
|
|
|
* |
106
|
|
|
* @access public |
107
|
|
|
* |
108
|
|
|
* @param array $config Full sync configuration for this sync module. |
109
|
|
|
* @param int $max_items_to_enqueue Maximum number of items to enqueue. |
110
|
|
|
* @param boolean $state True if full sync has finished enqueueing this module, false otherwise. |
111
|
|
|
* @return array Number of actions enqueued, and next module state. |
112
|
|
|
*/ |
113
|
|
|
public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { |
114
|
|
|
global $wpdb; |
115
|
|
|
return $this->enqueue_all_ids_as_action( 'jetpack_full_sync_terms', $wpdb->term_taxonomy, 'term_taxonomy_id', $this->get_where_sql( $config ), $max_items_to_enqueue, $state ); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Retrieve the WHERE SQL clause based on the module config. |
120
|
|
|
* |
121
|
|
|
* @access private |
122
|
|
|
* |
123
|
|
|
* @param array $config Full sync configuration for this sync module. |
124
|
|
|
* @return string WHERE SQL clause, or `null` if no comments are specified in the module config. |
125
|
|
|
*/ |
126
|
|
View Code Duplication |
private function get_where_sql( $config ) { |
127
|
|
|
$where_sql = Settings::get_blacklisted_taxonomies_sql(); |
128
|
|
|
|
129
|
|
|
if ( is_array( $config ) ) { |
130
|
|
|
$where_sql .= ' AND term_taxonomy_id IN (' . implode( ',', array_map( 'intval', $config ) ) . ')'; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return $where_sql; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Retrieve an estimated number of actions that will be enqueued. |
138
|
|
|
* |
139
|
|
|
* @access public |
140
|
|
|
* |
141
|
|
|
* @param array $config Full sync configuration for this sync module. |
142
|
|
|
* @return int Number of items yet to be enqueued. |
143
|
|
|
*/ |
144
|
|
View Code Duplication |
public function estimate_full_sync_actions( $config ) { |
145
|
|
|
global $wpdb; |
146
|
|
|
|
147
|
|
|
$query = "SELECT count(*) FROM $wpdb->term_taxonomy"; |
148
|
|
|
|
149
|
|
|
$where_sql = $this->get_where_sql( $config ); |
150
|
|
|
if ( $where_sql ) { |
151
|
|
|
$query .= ' WHERE ' . $where_sql; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared |
155
|
|
|
$count = $wpdb->get_var( $query ); |
156
|
|
|
|
157
|
|
|
return (int) ceil( $count / self::ARRAY_CHUNK_SIZE ); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Retrieve the actions that will be sent for this module during a full sync. |
162
|
|
|
* |
163
|
|
|
* @access public |
164
|
|
|
* |
165
|
|
|
* @return array Full sync actions of this module. |
166
|
|
|
*/ |
167
|
|
|
public function get_full_sync_actions() { |
168
|
|
|
return array( 'jetpack_full_sync_terms' ); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Handler for creating and updating terms. |
173
|
|
|
* |
174
|
|
|
* @access public |
175
|
|
|
* |
176
|
|
|
* @param int $term_id Term ID. |
177
|
|
|
* @param int $tt_id Term taxonomy ID. |
178
|
|
|
* @param string $taxonomy Taxonomy slug. |
179
|
|
|
*/ |
180
|
|
|
public function save_term_handler( $term_id, $tt_id, $taxonomy ) { |
181
|
|
|
if ( class_exists( '\\WP_Term' ) ) { |
182
|
|
|
$term_object = \WP_Term::get_instance( $term_id, $taxonomy ); |
183
|
|
|
} else { |
184
|
|
|
$term_object = get_term_by( 'id', $term_id, $taxonomy ); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
$current_filter = current_filter(); |
188
|
|
|
|
189
|
|
|
if ( 'created_term' === $current_filter ) { |
190
|
|
|
/** |
191
|
|
|
* Fires when the client needs to add a new term |
192
|
|
|
* |
193
|
|
|
* @since 5.0.0 |
194
|
|
|
* |
195
|
|
|
* @param object the Term object |
196
|
|
|
*/ |
197
|
|
|
do_action( 'jetpack_sync_add_term', $term_object ); |
198
|
|
|
return; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Fires when the client needs to update a term |
203
|
|
|
* |
204
|
|
|
* @since 4.2.0 |
205
|
|
|
* |
206
|
|
|
* @param object the Term object |
207
|
|
|
*/ |
208
|
|
|
do_action( 'jetpack_sync_save_term', $term_object ); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Filter blacklisted taxonomies. |
213
|
|
|
* |
214
|
|
|
* @access public |
215
|
|
|
* |
216
|
|
|
* @param array $args Hook args. |
217
|
|
|
* @return array|boolean False if not whitelisted, the original hook args otherwise. |
218
|
|
|
*/ |
219
|
|
|
public function filter_blacklisted_taxonomies( $args ) { |
220
|
|
|
$term = $args[0]; |
221
|
|
|
|
222
|
|
|
if ( in_array( $term->taxonomy, Settings::get_setting( 'taxonomies_blacklist' ), true ) ) { |
223
|
|
|
return false; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
return $args; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Set the taxonomy whitelist. |
231
|
|
|
* |
232
|
|
|
* @access public |
233
|
|
|
* |
234
|
|
|
* @param array $taxonomies The new taxonomyy whitelist. |
235
|
|
|
*/ |
236
|
|
|
public function set_taxonomy_whitelist( $taxonomies ) { |
237
|
|
|
$this->taxonomy_whitelist = $taxonomies; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Set module defaults. |
242
|
|
|
* Define the taxonomy whitelist to be the default one. |
243
|
|
|
* |
244
|
|
|
* @access public |
245
|
|
|
*/ |
246
|
|
|
public function set_defaults() { |
247
|
|
|
$this->taxonomy_whitelist = Defaults::$default_taxonomy_whitelist; |
|
|
|
|
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Expand the term taxonomy IDs to terms within a hook before they are serialized and sent to the server. |
252
|
|
|
* |
253
|
|
|
* @access public |
254
|
|
|
* |
255
|
|
|
* @param array $args The hook parameters. |
256
|
|
|
* @return array $args The expanded hook parameters. |
257
|
|
|
*/ |
258
|
|
|
public function expand_term_taxonomy_id( $args ) { |
259
|
|
|
list( $term_taxonomy_ids, $previous_end ) = $args; |
260
|
|
|
|
261
|
|
|
return array( |
262
|
|
|
'terms' => get_terms( |
263
|
|
|
array( |
264
|
|
|
'hide_empty' => false, |
265
|
|
|
'term_taxonomy_id' => $term_taxonomy_ids, |
266
|
|
|
'orderby' => 'term_taxonomy_id', |
267
|
|
|
'order' => 'DESC', |
268
|
|
|
) |
269
|
|
|
), |
270
|
|
|
'previous_end' => $previous_end, |
271
|
|
|
); |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
|
This check looks for access to properties that are not accessible from the current context.
If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.