Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
8.55% covered (danger)
8.55%
13 / 152
15.79% covered (danger)
15.79%
3 / 19
CRAP
0.00% covered (danger)
0.00%
0 / 1
Terms
8.00% covered (danger)
8.00%
12 / 150
15.79% covered (danger)
15.79%
3 / 19
2410.53
0.00% covered (danger)
0.00%
0 / 1
 name
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 id_field
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 table_name
n/a
0 / 0
n/a
0 / 0
1
 table
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_object_by_id
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 get_objects_by_id
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 1
90
 init_listeners
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
1
 init_full_sync_listeners
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 init_before_send
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 enqueue_full_sync_actions
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_where_sql
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
 estimate_full_sync_actions
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 get_full_sync_actions
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 save_term_handler
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
 filter_blacklisted_taxonomies
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 filter_set_object_terms_no_update
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
72
 expand_term_taxonomy_id
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
2
 expand_terms_for_relationship
n/a
0 / 0
n/a
0 / 0
1
 expand_raw_terms
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
30
 expand_raw_term_taxonomies
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
30
 expand_raw_term_relationships
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2/**
3 * Terms sync module.
4 *
5 * @package automattic/jetpack-sync
6 */
7
8namespace Automattic\Jetpack\Sync\Modules;
9
10use Automattic\Jetpack\Sync\Settings;
11
12if ( ! defined( 'ABSPATH' ) ) {
13    exit( 0 );
14}
15
16/**
17 * Class to handle sync for terms.
18 */
19class Terms extends Module {
20
21    /**
22     * Sync module name.
23     *
24     * @access public
25     *
26     * @return string
27     */
28    public function name() {
29        return 'terms';
30    }
31
32    /**
33     * The id field in the database.
34     *
35     * @access public
36     *
37     * @return string
38     */
39    public function id_field() {
40        return 'term_taxonomy_id';
41    }
42
43    /**
44     * The table name.
45     *
46     * @access public
47     *
48     * @return string
49     * @deprecated since 3.11.0 Use table() instead.
50     */
51    public function table_name() {
52        _deprecated_function( __METHOD__, '3.11.0', 'Automattic\\Jetpack\\Sync\\Terms->table' );
53        return 'term_taxonomy';
54    }
55
56    /**
57     * The table in the database with the prefix.
58     *
59     * @access public
60     *
61     * @return string|bool
62     */
63    public function table() {
64        global $wpdb;
65        return $wpdb->term_taxonomy;
66    }
67
68    /**
69     * Allows WordPress.com servers to retrieve term-related objects via the sync API.
70     *
71     * @param string $object_type The type of object. Accepts: 'term', 'term_taxonomy', 'term_relationships'.
72     * @param int    $id          The id of the object.
73     *
74     * @return false|object A term or term_taxonomy object, depending on object type.
75     */
76    public function get_object_by_id( $object_type, $id ) {
77        $id = (int) $id;
78
79        if ( empty( $id ) ) {
80            return false;
81        }
82
83        $objects = $this->get_objects_by_id( $object_type, array( $id ) );
84
85        return $objects[ $id ] ?? false;
86    }
87
88    /**
89     * Retrieve a set of objects by their IDs.
90     *
91     * @access public
92     *
93     * @param string $object_type Object type. Accepts: 'term', 'term_taxonomy', 'term_relationships'.
94     * @param array  $ids         Object IDs.
95     *
96     * @return array Array of objects.
97     */
98    public function get_objects_by_id( $object_type, $ids ) {
99        global $wpdb;
100
101        $objects = array();
102
103        if ( ! is_array( $ids ) || empty( $ids ) || empty( $object_type ) ) {
104            return $objects;
105        }
106
107        // Sanitize.
108        $ids     = array_map( 'intval', $ids );
109        $ids_str = implode( ',', $ids );
110
111        $where_sql = Settings::get_whitelisted_taxonomies_sql();
112
113        switch ( $object_type ) {
114            case 'term':
115                $query    = "SELECT * FROM $wpdb->terms t INNER JOIN $wpdb->term_taxonomy tt ON t.term_id=tt.term_id WHERE t.term_id IN ( $ids_str ) AND ";
116                $callback = 'expand_raw_terms';
117                break;
118            case 'term_taxonomy':
119                $query    = "SELECT * FROM $wpdb->term_taxonomy WHERE term_taxonomy_id IN ( $ids_str ) AND ";
120                $callback = 'expand_raw_term_taxonomies';
121                break;
122            case 'term_relationships':
123                $query    = "SELECT * FROM $wpdb->term_relationships tr INNER JOIN $wpdb->term_taxonomy tt ON tr.term_taxonomy_id=tt.term_taxonomy_id WHERE object_id IN ( $ids_str ) AND ";
124                $callback = 'expand_raw_term_relationships';
125                break;
126            default:
127                return array();
128        }
129
130        $query .= $where_sql;
131
132        // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Already sanitized above.
133        $results = $wpdb->get_results( $query );
134
135        if ( ! is_array( $results ) ) {
136            return array();
137        }
138
139        $objects = $this->$callback( $results );
140
141        return $objects ?? array();
142    }
143
144    /**
145     * Initialize terms action listeners.
146     *
147     * @access public
148     *
149     * @param callable $callable Action handler callable.
150     */
151    public function init_listeners( $callable ) {
152        add_action( 'created_term', array( $this, 'save_term_handler' ), 10, 3 );
153        add_action( 'edited_term', array( $this, 'save_term_handler' ), 10, 3 );
154        add_action( 'jetpack_sync_save_term', $callable );
155        add_action( 'jetpack_sync_add_term', $callable );
156        add_action( 'delete_term', $callable, 10, 4 );
157        add_action( 'set_object_terms', $callable, 10, 6 );
158        add_action( 'deleted_term_relationships', $callable, 10, 2 );
159        add_filter( 'jetpack_sync_before_enqueue_set_object_terms', array( $this, 'filter_set_object_terms_no_update' ) );
160        add_filter( 'jetpack_sync_before_enqueue_jetpack_sync_save_term', array( $this, 'filter_blacklisted_taxonomies' ) );
161        add_filter( 'jetpack_sync_before_enqueue_jetpack_sync_add_term', array( $this, 'filter_blacklisted_taxonomies' ) );
162    }
163
164    /**
165     * Initialize terms action listeners for full sync.
166     *
167     * @access public
168     *
169     * @param callable $callable Action handler callable.
170     */
171    public function init_full_sync_listeners( $callable ) {
172        add_action( 'jetpack_full_sync_terms', $callable, 10, 2 );
173    }
174
175    /**
176     * Initialize the module in the sender.
177     *
178     * @access public
179     */
180    public function init_before_send() {
181        // Full sync.
182        add_filter( 'jetpack_sync_before_send_jetpack_full_sync_terms', array( $this, 'expand_term_taxonomy_id' ) );
183    }
184
185    /**
186     * Enqueue the terms actions for full sync.
187     *
188     * @access public
189     *
190     * @param array   $config               Full sync configuration for this sync module.
191     * @param int     $max_items_to_enqueue Maximum number of items to enqueue.
192     * @param boolean $state                True if full sync has finished enqueueing this module, false otherwise.
193     * @return array Number of actions enqueued, and next module state.
194     */
195    public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) {
196        global $wpdb;
197        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 );
198    }
199
200    /**
201     * Retrieve the WHERE SQL clause based on the module config.
202     *
203     * @access public
204     *
205     * @param array $config Full sync configuration for this sync module.
206     * @return string WHERE SQL clause, or `null` if no comments are specified in the module config.
207     */
208    public function get_where_sql( $config ) {
209        $where_sql = Settings::get_blacklisted_taxonomies_sql();
210
211        if ( is_array( $config ) && ! empty( $config ) ) {
212            $where_sql .= ' AND term_taxonomy_id IN (' . implode( ',', array_map( 'intval', $config ) ) . ')';
213        }
214
215        return $where_sql;
216    }
217
218    /**
219     * Retrieve an estimated number of actions that will be enqueued.
220     *
221     * @access public
222     *
223     * @param array $config Full sync configuration for this sync module.
224     * @return int Number of items yet to be enqueued.
225     */
226    public function estimate_full_sync_actions( $config ) {
227        global $wpdb;
228
229        $query = "SELECT count(*) FROM $wpdb->term_taxonomy";
230
231        $where_sql = $this->get_where_sql( $config );
232        if ( $where_sql ) {
233            $query .= ' WHERE ' . $where_sql;
234        }
235
236        // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
237        $count = (int) $wpdb->get_var( $query );
238
239        return (int) ceil( $count / self::ARRAY_CHUNK_SIZE );
240    }
241
242    /**
243     * Retrieve the actions that will be sent for this module during a full sync.
244     *
245     * @access public
246     *
247     * @return array Full sync actions of this module.
248     */
249    public function get_full_sync_actions() {
250        return array( 'jetpack_full_sync_terms' );
251    }
252
253    /**
254     * Handler for creating and updating terms.
255     *
256     * @access public
257     *
258     * @param int    $term_id  Term ID.
259     * @param int    $tt_id    Term taxonomy ID.
260     * @param string $taxonomy Taxonomy slug.
261     */
262    public function save_term_handler( $term_id, $tt_id, $taxonomy ) {
263        if ( class_exists( '\\WP_Term' ) ) {
264            $term_object = \WP_Term::get_instance( $term_id, $taxonomy );
265        } else {
266            $term_object = get_term_by( 'id', $term_id, $taxonomy );
267        }
268
269        $current_filter = current_filter();
270
271        if ( 'created_term' === $current_filter ) {
272            /**
273             * Fires when the client needs to add a new term
274             *
275             * @since 1.6.3
276             * @since-jetpack 5.0.0
277             *
278             * @param object the Term object
279             */
280            do_action( 'jetpack_sync_add_term', $term_object );
281            return;
282        }
283
284        /**
285         * Fires when the client needs to update a term
286         *
287         * @since 1.6.3
288         * @since-jetpack 4.2.0
289         *
290         * @param object the Term object
291         */
292        do_action( 'jetpack_sync_save_term', $term_object );
293    }
294
295    /**
296     * Filter blacklisted taxonomies.
297     *
298     * @access public
299     *
300     * @param array $args Hook args.
301     * @return array|boolean False if not whitelisted, the original hook args otherwise.
302     */
303    public function filter_blacklisted_taxonomies( $args ) {
304        $term = $args[0];
305
306        if ( in_array( $term->taxonomy, Settings::get_setting( 'taxonomies_blacklist' ), true ) ) {
307            return false;
308        }
309
310        return $args;
311    }
312
313    /**
314     * Filter out set_object_terms actions with blacklisted taxonomies or where the terms have not changed.
315     *
316     * @param array $args Hook args.
317     * @return array|boolean False if blacklisted taxonomy or no change in terms, the original hook args otherwise.
318     */
319    public function filter_set_object_terms_no_update( $args ) {
320        // Check if the taxonomy is blacklisted. $args[3] is the taxonomy.
321        if ( isset( $args[3] ) && in_array( $args[3], Settings::get_setting( 'taxonomies_blacklist' ), true ) ) {
322            return false;
323        }
324        // There is potential for other plugins to modify args, therefore lets validate # of and types.
325        // $args[2] is $tt_ids, $args[5] is $old_tt_ids see wp-includes/taxonomy.php L2740.
326        if ( 6 === count( $args ) && is_array( $args[2] ) && is_array( $args[5] ) ) {
327            if ( empty( array_diff( $args[2], $args[5] ) ) && empty( array_diff( $args[5], $args[2] ) ) ) {
328                return false;
329            }
330        }
331        return $args;
332    }
333
334    /**
335     * Expand the term taxonomy IDs to terms within a hook before they are serialized and sent to the server.
336     *
337     * @access public
338     *
339     * @param array $args The hook parameters.
340     * @return array $args The expanded hook parameters.
341     */
342    public function expand_term_taxonomy_id( $args ) {
343        list( $term_taxonomy_ids, $previous_end ) = $args;
344
345        return array(
346            // @phan-suppress-next-line PhanAccessMethodInternal @phan-suppress-current-line UnusedSuppression -- Fixed in WP 6.9, but then we need a suppression for the WP 6.8 compat run. @todo Remove this suppression when we drop WP <6.9.
347            'terms'        => get_terms(
348                array(
349                    'hide_empty'       => false,
350                    'orderby'          => 'term_taxonomy_id',
351                    'order'            => 'DESC',
352                    'taxonomy'         => array(),
353                    'term_taxonomy_id' => $term_taxonomy_ids,
354                )
355            ),
356            'previous_end' => $previous_end,
357        );
358    }
359
360    /**
361     * Gets a term object based on a given row from the term_relationships database table.
362     *
363     * @access public
364     *
365     * @deprecated since 4.8.1
366     *
367     * @param object $relationship A row object from the term_relationships table.
368     * @return object|bool A term object, or false if term taxonomy doesn't exist.
369     */
370    public function expand_terms_for_relationship( $relationship ) {
371        _deprecated_function( __METHOD__, '4.8.1' );
372
373        return get_term_by( 'term_taxonomy_id', $relationship->term_taxonomy_id );
374    }
375
376    /**
377     * Prepare raw terms and return them in a standard format.
378     *
379     * @param  array $terms An array of raw term objects.
380     * @return array        An array of term objects.
381     */
382    private function expand_raw_terms( array $terms ) {
383        $objects = array();
384        $columns = array(
385            'term_id'          => 'int',
386            'name'             => 'string',
387            'slug'             => 'string',
388            'taxonomy'         => 'string',
389            'description'      => 'string',
390            'term_group'       => 'int',
391            'term_taxonomy_id' => 'int',
392            'parent'           => 'int',
393            'count'            => 'int',
394        );
395
396        foreach ( $terms as $term ) {
397            if ( ! array_key_exists( $term->term_id, $objects ) ) {
398                $t_array = array();
399
400                foreach ( $columns as $field => $type ) {
401                    $value             = $term->$field ?? '';
402                    $t_array[ $field ] = 'int' === $type ? (int) $value : $value;
403                }
404                // This will allow us to know on WPCOM that the term name is the raw one, coming from the DB. Useful with backwards compatibility in mind.
405                $t_array['raw_name'] = $t_array['name'];
406
407                $objects[ $term->term_id ] = (object) $t_array;
408            }
409        }
410
411        return $objects;
412    }
413
414    /**
415     * Prepare raw term taxonomies and return them in a standard format.
416     *
417     * @param  array $term_taxonomies An array of raw term_taxonomy objects.
418     * @return array                  An array of term_taxonomy objects.
419     */
420    private function expand_raw_term_taxonomies( array $term_taxonomies ) {
421        $objects = array();
422        $columns = array(
423            'term_id'          => 'int',
424            'taxonomy'         => 'string',
425            'description'      => 'string',
426            'term_taxonomy_id' => 'int',
427            'parent'           => 'int',
428            'count'            => 'int',
429        );
430
431        foreach ( $term_taxonomies as $tt ) {
432            if ( ! array_key_exists( $tt->term_taxonomy_id, $objects ) ) {
433                $t_array = array();
434
435                foreach ( $columns as $field => $type ) {
436                    $value             = $tt->$field ?? '';
437                    $t_array[ $field ] = 'int' === $type ? (int) $value : $value;
438                }
439
440                $objects[ $tt->term_taxonomy_id ] = (object) $t_array;
441            }
442        }
443
444        return $objects;
445    }
446
447    /**
448     * Prepare raw term taxonomies and return them in a standard format.
449     *
450     * @param  array $term_relationships An array of raw term_taxonomy objects.
451     * @return array                     An array of term_taxonomy objects or false.
452     */
453    private function expand_raw_term_relationships( array $term_relationships ) {
454        $objects = array();
455        $columns = array(
456            'object_id'        => 'int',
457            'term_id'          => 'int',
458            'taxonomy'         => 'string',
459            'term_taxonomy_id' => 'int',
460            'term_order'       => 'int',
461            'parent'           => 'int',
462            'count'            => 'int',
463        );
464
465        foreach ( $term_relationships as $tt ) {
466            $object_id = (int) $tt->object_id;
467
468            $relationship = array();
469            foreach ( $columns as $field => $type ) {
470                $value                  = $tt->$field ?? '';
471                $relationship[ $field ] = 'int' === $type ? (int) $value : $value;
472            }
473
474            $relationship = (object) $relationship;
475
476            if ( ! array_key_exists( $object_id, $objects ) ) {
477                $objects[ $object_id ] = (object) array(
478                    'object_id'     => $object_id,
479                    'relationships' => array( $relationship ),
480                );
481            } else {
482                $objects[ $object_id ]->relationships[] = $relationship;
483            }
484        }
485
486        return $objects;
487    }
488}