Completed
Push — try/statically-access-asset-to... ( f0c1d6...542253 )
by
unknown
396:52 queued 388:09
created

Terms::expand_term_taxonomy_id()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
1
<?php
2
3
namespace Automattic\Jetpack\Sync\Modules;
4
5
class Terms extends \Jetpack_Sync_Module {
6
	private $taxonomy_whitelist;
7
8
	function name() {
9
		return 'terms';
10
	}
11
12
	function init_listeners( $callable ) {
13
		add_action( 'created_term', array( $this, 'save_term_handler' ), 10, 3 );
14
		add_action( 'edited_term', array( $this, 'save_term_handler' ), 10, 3 );
15
		add_action( 'jetpack_sync_save_term', $callable );
16
		add_action( 'jetpack_sync_add_term', $callable );
17
		add_action( 'delete_term', $callable, 10, 4 );
18
		add_action( 'set_object_terms', $callable, 10, 6 );
19
		add_action( 'deleted_term_relationships', $callable, 10, 2 );
20
	}
21
22
	public function init_full_sync_listeners( $callable ) {
23
		add_action( 'jetpack_full_sync_terms', $callable, 10, 2 );
24
	}
25
26
	function init_before_send() {
27
		// full sync
28
		add_filter( 'jetpack_sync_before_send_jetpack_full_sync_terms', array( $this, 'expand_term_taxonomy_id' ) );
29
	}
30
31
	public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) {
32
		global $wpdb;
33
		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 );
34
	}
35
36 View Code Duplication
	private function get_where_sql( $config ) {
37
		if ( is_array( $config ) ) {
38
			return 'term_taxonomy_id IN (' . implode( ',', array_map( 'intval', $config ) ) . ')';
39
		}
40
41
		return '';
42
	}
43
44
	public function estimate_full_sync_actions( $config ) {
45
		global $wpdb;
46
47
		$query = "SELECT count(*) FROM $wpdb->term_taxonomy";
48
49
		if ( $where_sql = $this->get_where_sql( $config ) ) {
50
			$query .= ' WHERE ' . $where_sql;
51
		}
52
53
		$count = $wpdb->get_var( $query );
54
55
		return (int) ceil( $count / self::ARRAY_CHUNK_SIZE );
56
	}
57
58
	function get_full_sync_actions() {
59
		return array( 'jetpack_full_sync_terms' );
60
	}
61
62
	function save_term_handler( $term_id, $tt_id, $taxonomy ) {
63
		if ( class_exists( '\\WP_Term' ) ) {
64
			$term_object = \WP_Term::get_instance( $term_id, $taxonomy );
65
		} else {
66
			$term_object = get_term_by( 'id', $term_id, $taxonomy );
67
		}
68
69
		$current_filter = current_filter();
70
71
		if ( 'created_term' === $current_filter ) {
72
			/**
73
			 * Fires when the client needs to add a new term
74
			 *
75
			 * @since 5.0.0
76
			 *
77
			 * @param object the Term object
78
			 */
79
			do_action( 'jetpack_sync_add_term', $term_object );
80
			return;
81
		}
82
83
		/**
84
		 * Fires when the client needs to update a term
85
		 *
86
		 * @since 4.2.0
87
		 *
88
		 * @param object the Term object
89
		 */
90
		do_action( 'jetpack_sync_save_term', $term_object );
91
	}
92
93
	function set_taxonomy_whitelist( $taxonomies ) {
94
		$this->taxonomy_whitelist = $taxonomies;
95
	}
96
97
	function set_defaults() {
98
		$this->taxonomy_whitelist = \Jetpack_Sync_Defaults::$default_taxonomy_whitelist;
0 ignored issues
show
Bug introduced by
The property default_taxonomy_whitelist cannot be accessed from this context as it is declared private in class Jetpack_Sync_Defaults.

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.

Loading history...
99
	}
100
101
	public function expand_term_taxonomy_id( $args ) {
102
		list( $term_taxonomy_ids,  $previous_end ) = $args;
103
104
		return
105
			array( 'terms' => get_terms(
106
				array(
107
					'hide_empty'       => false,
108
					'term_taxonomy_id' => $term_taxonomy_ids,
109
					'orderby'          => 'term_taxonomy_id',
110
					'order'            => 'DESC'
111
				)
112
			),
113
			'previous_end' => $previous_end
114
		);
115
	}
116
}
117