Completed
Push — improving-sync-with-enej ( 925a49...86a629 )
by
unknown
25:00 queued 15:46
created

Jetpack_Sync_Module_Terms   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 141
rs 10
c 0
b 0
f 0
wmc 20
lcom 2
cbo 3

13 Methods

Rating   Name   Duplication   Size   Complexity  
A name() 0 3 1
A init_listeners() 0 10 1
A set_object_terms() 0 6 2
A is_save_post() 0 3 1
A init_full_sync_listeners() 0 3 1
A init_before_send() 0 4 1
A enqueue_full_sync_actions() 0 22 3
A estimate_full_sync_actions() 0 13 2
A get_full_sync_actions() 0 3 1
B save_term_handler() 0 30 3
A set_taxonomy_whitelist() 0 3 1
A set_defaults() 0 3 1
A expand_term_ids() 0 20 2
1
<?php
2
3
class Jetpack_Sync_Module_Terms extends Jetpack_Sync_Module {
4
	private $taxonomy_whitelist;
5
6
	private $callable;
7
8
	function name() {
9
		return 'terms';
10
	}
11
12
	function init_listeners( $callable ) {
13
		$this->callable = $callable;
14
		add_action( 'created_term', array( $this, 'save_term_handler' ), 10, 3 );
15
		add_action( 'edited_term', array( $this, 'save_term_handler' ), 10, 3 );
16
		add_action( 'jetpack_sync_save_term', $callable );
17
		add_action( 'jetpack_sync_add_term', $callable );
18
		add_action( 'delete_term', $callable, 10, 4 );
19
		add_action( 'set_object_terms', array( $this, 'set_object_terms' ), 10, 6 );
20
		add_action( 'deleted_term_relationships', $callable, 10, 2 );
21
	}
22
23
	public function set_object_terms() {
24
		if ( $this->is_save_post() ) {
25
			return;
26
		}
27
		call_user_func_array( $this->callable, func_get_args() );
28
	}
29
30
	private function is_save_post() {
31
		return Jetpack::is_function_in_backtrace( 'wp_insert_post' );
32
	}
33
34
	public function init_full_sync_listeners( $callable ) {
35
		add_action( 'jetpack_full_sync_terms', $callable, 10, 2 );
36
	}
37
38
	function init_before_send() {
39
		// full sync
40
		add_filter( 'jetpack_sync_before_send_jetpack_full_sync_terms', array( $this, 'expand_term_ids' ) );
41
	}
42
43
	function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) {
44
		global $wpdb;
45
46
		// TODO: process state
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
47
48
		$taxonomies           = get_taxonomies();
49
		$total_chunks_counter = 0;
50
		foreach ( $taxonomies as $taxonomy ) {
51
			// I hope this is never bigger than RAM...
52
			$term_ids = $wpdb->get_col( $wpdb->prepare( "SELECT term_id FROM $wpdb->term_taxonomy WHERE taxonomy = %s", $taxonomy ) ); // Should we set a limit here?
53
			// Request posts in groups of N for efficiency
54
			$chunked_term_ids = array_chunk( $term_ids, self::ARRAY_CHUNK_SIZE );
55
56
			// Send each chunk as an array of objects
57
			foreach ( $chunked_term_ids as $chunk ) {
58
				do_action( 'jetpack_full_sync_terms', $chunk, $taxonomy );
59
				$total_chunks_counter ++;
60
			}
61
		}
62
63
		return array( $total_chunks_counter, true );
64
	}
65
66
	function estimate_full_sync_actions( $config ) {
67
		// TODO - make this (and method above) more efficient for large numbers of terms or taxonomies
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
68
		global $wpdb;
69
70
		$taxonomies           = get_taxonomies();
71
		$total_chunks_counter = 0;
72
		foreach ( $taxonomies as $taxonomy ) {
73
			$total_ids = $wpdb->get_var( $wpdb->prepare( "SELECT count(term_id) FROM $wpdb->term_taxonomy WHERE taxonomy = %s", $taxonomy ) );
74
			$total_chunks_counter += (int) ceil( $total_ids / self::ARRAY_CHUNK_SIZE );
75
		}
76
77
		return $total_chunks_counter;
78
	}
79
80
	function get_full_sync_actions() {
81
		return array( 'jetpack_full_sync_terms' );
82
	}
83
84
	function save_term_handler( $term_id, $tt_id, $taxonomy ) {
85
		if ( class_exists( 'WP_Term' ) ) {
86
			$term_object = WP_Term::get_instance( $term_id, $taxonomy );
87
		} else {
88
			$term_object = get_term_by( 'id', $term_id, $taxonomy );
89
		}
90
91
		$current_filter = current_filter();
92
93
		if ( 'created_term' === $current_filter ) {
94
			/**
95
			 * Fires when the client needs to add a new term
96
			 *
97
			 * @since 5.0.0
98
			 *
99
			 * @param object the Term object
100
			 */
101
			do_action( 'jetpack_sync_add_term', $term_object );
102
			return;
103
		}
104
105
		/**
106
		 * Fires when the client needs to update a term
107
		 *
108
		 * @since 4.2.0
109
		 *
110
		 * @param object the Term object
111
		 */
112
		do_action( 'jetpack_sync_save_term', $term_object );
113
	}
114
115
	function set_taxonomy_whitelist( $taxonomies ) {
116
		$this->taxonomy_whitelist = $taxonomies;
117
	}
118
119
	function set_defaults() {
120
		$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...
121
	}
122
123
	public function expand_term_ids( $args ) {
124
		global $wp_version;
125
		$term_ids = $args[0];
126
		$taxonomy = $args[1];
127
		// version 4.5 or higher
128
		if ( version_compare( $wp_version, 4.5, '>=' ) ) {
129
			$terms = get_terms( array(
130
				'taxonomy'   => $taxonomy,
131
				'hide_empty' => false,
132
				'include'    => $term_ids,
133
			) );
134
		} else {
135
			$terms = get_terms( $taxonomy, array(
136
				'hide_empty' => false,
137
				'include'    => $term_ids,
138
			) );
139
		}
140
141
		return $terms;
142
	}
143
}
144