Completed
Push — add/eta-to-sync-status ( d17f42...551096 )
by
unknown
06:17
created

Terms::get_sync_speed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
	 *  An estimate of how many rows per second can be synced during a full sync.
19
	 *
20
	 * @access static
21
	 *
22
	 * @var int|null Null if speed is not important in a full sync.
23
	 */
24
	static $sync_speed = 769;
25
	/**
26
	 * Sync module name.
27
	 *
28
	 * @access public
29
	 *
30
	 * @return string
31
	 */
32
	public function name() {
33
		return 'terms';
34
	}
35
36
	/**
37
	 * The id field in the database.
38
	 *
39
	 * @access public
40
	 *
41
	 * @return string
42
	 */
43
	public function id_field() {
44
		return 'term_taxonomy_id';
45
	}
46
47
	/**
48
	 * The table in the database.
49
	 *
50
	 * @access public
51
	 *
52
	 * @return string
53
	 */
54
	public function table_name() {
55
		return 'term_taxonomy';
56
	}
57
58
	/**
59
	 * Allows WordPress.com servers to retrieve term-related objects via the sync API.
60
	 *
61
	 * @param string $object_type The type of object.
62
	 * @param int    $id          The id of the object.
63
	 *
64
	 * @return bool|object A WP_Term object, or a row from term_taxonomy table depending on object type.
65
	 */
66
	public function get_object_by_id( $object_type, $id ) {
67
		global $wpdb;
68
		$object = false;
69
		if ( 'term' === $object_type ) {
70
			$object = get_term( intval( $id ) );
71
72 View Code Duplication
			if ( is_wp_error( $object ) && $object->get_error_code() === 'invalid_taxonomy' ) {
73
				// Fetch raw term.
74
				$columns = implode( ', ', array_unique( array_merge( Defaults::$default_term_checksum_columns, array( 'term_group' ) ) ) );
75
				// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
76
				$object = $wpdb->get_row( $wpdb->prepare( "SELECT $columns FROM $wpdb->terms WHERE term_id = %d", $id ) );
77
			}
78
		}
79
80 View Code Duplication
		if ( 'term_taxonomy' === $object_type ) {
81
			$columns = implode( ', ', array_unique( array_merge( Defaults::$default_term_taxonomy_checksum_columns, array( 'description' ) ) ) );
82
			// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
83
			$object = $wpdb->get_row( $wpdb->prepare( "SELECT $columns FROM $wpdb->term_taxonomy WHERE term_taxonomy_id = %d", $id ) );
84
		}
85
86
		if ( 'term_relationships' === $object_type ) {
87
			$columns = implode( ', ', Defaults::$default_term_relationships_checksum_columns );
88
			// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
89
			$objects = $wpdb->get_results( $wpdb->prepare( "SELECT $columns FROM $wpdb->term_relationships WHERE object_id = %d", $id ) );
90
			$object  = (object) array(
91
				'object_id'     => $id,
92
				'relationships' => array_map( array( $this, 'expand_terms_for_relationship' ), $objects ),
93
			);
94
		}
95
96
		return $object ? $object : false;
97
	}
98
99
	/**
100
	 * Initialize terms action listeners.
101
	 *
102
	 * @access public
103
	 *
104
	 * @param callable $callable Action handler callable.
105
	 */
106
	public function init_listeners( $callable ) {
107
		add_action( 'created_term', array( $this, 'save_term_handler' ), 10, 3 );
108
		add_action( 'edited_term', array( $this, 'save_term_handler' ), 10, 3 );
109
		add_action( 'jetpack_sync_save_term', $callable );
110
		add_action( 'jetpack_sync_add_term', $callable );
111
		add_action( 'delete_term', $callable, 10, 4 );
112
		add_action( 'set_object_terms', $callable, 10, 6 );
113
		add_action( 'deleted_term_relationships', $callable, 10, 2 );
114
		add_filter( 'jetpack_sync_before_enqueue_jetpack_sync_save_term', array( $this, 'filter_blacklisted_taxonomies' ) );
115
		add_filter( 'jetpack_sync_before_enqueue_jetpack_sync_add_term', array( $this, 'filter_blacklisted_taxonomies' ) );
116
	}
117
118
	/**
119
	 * Initialize terms action listeners for full sync.
120
	 *
121
	 * @access public
122
	 *
123
	 * @param callable $callable Action handler callable.
124
	 */
125
	public function init_full_sync_listeners( $callable ) {
126
		add_action( 'jetpack_full_sync_terms', $callable, 10, 2 );
127
	}
128
129
	/**
130
	 * Initialize the module in the sender.
131
	 *
132
	 * @access public
133
	 */
134
	public function init_before_send() {
135
		// Full sync.
136
		add_filter( 'jetpack_sync_before_send_jetpack_full_sync_terms', array( $this, 'expand_term_taxonomy_id' ) );
137
	}
138
139
	/**
140
	 * Enqueue the terms actions for full sync.
141
	 *
142
	 * @access public
143
	 *
144
	 * @param array   $config               Full sync configuration for this sync module.
145
	 * @param int     $max_items_to_enqueue Maximum number of items to enqueue.
146
	 * @param boolean $state                True if full sync has finished enqueueing this module, false otherwise.
147
	 * @return array Number of actions enqueued, and next module state.
148
	 */
149
	public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) {
150
		global $wpdb;
151
		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 );
152
	}
153
154
	/**
155
	 * Retrieve the WHERE SQL clause based on the module config.
156
	 *
157
	 * @access public
158
	 *
159
	 * @param array $config Full sync configuration for this sync module.
160
	 * @return string WHERE SQL clause, or `null` if no comments are specified in the module config.
161
	 */
162 View Code Duplication
	public function get_where_sql( $config ) {
163
		$where_sql = Settings::get_blacklisted_taxonomies_sql();
164
165
		if ( is_array( $config ) ) {
166
			$where_sql .= ' AND term_taxonomy_id IN (' . implode( ',', array_map( 'intval', $config ) ) . ')';
167
		}
168
169
		return $where_sql;
170
	}
171
172
	/**
173
	 * Retrieve an estimated number of actions that will be enqueued.
174
	 *
175
	 * @access public
176
	 *
177
	 * @param array $config Full sync configuration for this sync module.
178
	 * @return int Number of items yet to be enqueued.
179
	 */
180 View Code Duplication
	public function estimate_full_sync_actions( $config ) {
181
		global $wpdb;
182
183
		$query = "SELECT count(*) FROM $wpdb->term_taxonomy";
184
185
		$where_sql = $this->get_where_sql( $config );
186
		if ( $where_sql ) {
187
			$query .= ' WHERE ' . $where_sql;
188
		}
189
190
		// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared
191
		$count = $wpdb->get_var( $query );
192
193
		return (int) ceil( $count / self::ARRAY_CHUNK_SIZE );
194
	}
195
196
	/**
197
	 * Retrieve the actions that will be sent for this module during a full sync.
198
	 *
199
	 * @access public
200
	 *
201
	 * @return array Full sync actions of this module.
202
	 */
203
	public function get_full_sync_actions() {
204
		return array( 'jetpack_full_sync_terms' );
205
	}
206
207
	/**
208
	 * Handler for creating and updating terms.
209
	 *
210
	 * @access public
211
	 *
212
	 * @param int    $term_id  Term ID.
213
	 * @param int    $tt_id    Term taxonomy ID.
214
	 * @param string $taxonomy Taxonomy slug.
215
	 */
216
	public function save_term_handler( $term_id, $tt_id, $taxonomy ) {
217
		if ( class_exists( '\\WP_Term' ) ) {
218
			$term_object = \WP_Term::get_instance( $term_id, $taxonomy );
219
		} else {
220
			$term_object = get_term_by( 'id', $term_id, $taxonomy );
221
		}
222
223
		$current_filter = current_filter();
224
225
		if ( 'created_term' === $current_filter ) {
226
			/**
227
			 * Fires when the client needs to add a new term
228
			 *
229
			 * @since 5.0.0
230
			 *
231
			 * @param object the Term object
232
			 */
233
			do_action( 'jetpack_sync_add_term', $term_object );
234
			return;
235
		}
236
237
		/**
238
		 * Fires when the client needs to update a term
239
		 *
240
		 * @since 4.2.0
241
		 *
242
		 * @param object the Term object
243
		 */
244
		do_action( 'jetpack_sync_save_term', $term_object );
245
	}
246
247
	/**
248
	 * Filter blacklisted taxonomies.
249
	 *
250
	 * @access public
251
	 *
252
	 * @param array $args Hook args.
253
	 * @return array|boolean False if not whitelisted, the original hook args otherwise.
254
	 */
255
	public function filter_blacklisted_taxonomies( $args ) {
256
		$term = $args[0];
257
258
		if ( in_array( $term->taxonomy, Settings::get_setting( 'taxonomies_blacklist' ), true ) ) {
259
			return false;
260
		}
261
262
		return $args;
263
	}
264
265
	/**
266
	 * Expand the term taxonomy IDs to terms within a hook before they are serialized and sent to the server.
267
	 *
268
	 * @access public
269
	 *
270
	 * @param array $args The hook parameters.
271
	 * @return array $args The expanded hook parameters.
272
	 */
273
	public function expand_term_taxonomy_id( $args ) {
274
		list( $term_taxonomy_ids,  $previous_end ) = $args;
275
276
		return array(
277
			'terms'        => get_terms(
278
				array(
279
					'hide_empty'       => false,
280
					'term_taxonomy_id' => $term_taxonomy_ids,
281
					'orderby'          => 'term_taxonomy_id',
282
					'order'            => 'DESC',
283
				)
284
			),
285
			'previous_end' => $previous_end,
286
		);
287
	}
288
289
	/**
290
	 * Gets a term object based on a given row from the term_relationships database table.
291
	 *
292
	 * @access public
293
	 *
294
	 * @param object $relationship A row object from the term_relationships table.
295
	 * @return object|bool A term object, or false if term taxonomy doesn't exist.
296
	 */
297
	public function expand_terms_for_relationship( $relationship ) {
298
		return get_term_by( 'term_taxonomy_id', $relationship->term_taxonomy_id );
299
	}
300
301
	/**
302
	 * Gets the sync speed of a module.
303
	 *
304
	 * @access public
305
	 *
306
	 * @return int
307
	 */
308
	public function get_sync_speed() {
309
		return self::$sync_speed;
310
	}
311
}
312