Completed
Push — update/sync-posts-less-events ( df315a )
by
unknown
09:51
created

Jetpack_Sync_Module_Terms::save_term_handler()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 30
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 4
nop 3
dl 0
loc 30
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
class Jetpack_Sync_Module_Terms extends Jetpack_Sync_Module {
4
	private $taxonomy_whitelist;
5
	private $action_handler;
6
7
	function name() {
8
		return 'terms';
9
	}
10
11
	function init_listeners( $callable ) {
12
		$this->action_handler = $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', array( $this, 'set_object_terms' ), 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_ids' ) );
29
	}
30
31
	function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) {
32
		global $wpdb;
33
34
		// 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...
35
36
		$taxonomies           = get_taxonomies();
37
		$total_chunks_counter = 0;
38
		foreach ( $taxonomies as $taxonomy ) {
39
			// I hope this is never bigger than RAM...
40
			$term_ids = $wpdb->get_col( $wpdb->prepare( "SELECT term_id FROM $wpdb->term_taxonomy WHERE taxonomy = %s", $taxonomy ) ); // Should we set a limit here?
41
			// Request posts in groups of N for efficiency
42
			$chunked_term_ids = array_chunk( $term_ids, self::ARRAY_CHUNK_SIZE );
43
44
			// Send each chunk as an array of objects
45
			foreach ( $chunked_term_ids as $chunk ) {
46
				do_action( 'jetpack_full_sync_terms', $chunk, $taxonomy );
47
				$total_chunks_counter ++;
48
			}
49
		}
50
51
		return array( $total_chunks_counter, true );
52
	}
53
54
	function estimate_full_sync_actions( $config ) {
55
		// 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...
56
		global $wpdb;
57
58
		$taxonomies           = get_taxonomies();
59
		$total_chunks_counter = 0;
60
		foreach ( $taxonomies as $taxonomy ) {
61
			$total_ids = $wpdb->get_var( $wpdb->prepare( "SELECT count(term_id) FROM $wpdb->term_taxonomy WHERE taxonomy = %s", $taxonomy ) );
62
			$total_chunks_counter += (int) ceil( $total_ids / self::ARRAY_CHUNK_SIZE );
63
		}
64
65
		return $total_chunks_counter;
66
	}
67
68
	function get_full_sync_actions() {
69
		return array( 'jetpack_full_sync_terms' );
70
	}
71
72
	public function set_object_terms( $object_id, $term_ids, $tt_ids, $taxonomy, $append ) {
73
		$posts_sync_module = Jetpack_Sync_Modules::get_module( 'posts' );
74
		if ( $posts_sync_module && $posts_sync_module->is_saving_post( $object_id ) ) {
75
			return;
76
		}
77
		$args = func_get_args();
78
		call_user_func_array( $this->action_handler, $args );
79
	}
80
81
	function save_term_handler( $term_id, $tt_id, $taxonomy ) {
82
		if ( class_exists( 'WP_Term' ) ) {
83
			$term_object = WP_Term::get_instance( $term_id, $taxonomy );
84
		} else {
85
			$term_object = get_term_by( 'id', $term_id, $taxonomy );
86
		}
87
88
		$current_filter = current_filter();
89
90
		if ( 'created_term' === $current_filter ) {
91
			/**
92
			 * Fires when the client needs to add a new term
93
			 *
94
			 * @since 5.0.0
95
			 *
96
			 * @param object the Term object
97
			 */
98
			do_action( 'jetpack_sync_add_term', $term_object );
99
			return;
100
		}
101
102
		/**
103
		 * Fires when the client needs to update a term
104
		 *
105
		 * @since 4.2.0
106
		 *
107
		 * @param object the Term object
108
		 */
109
		do_action( 'jetpack_sync_save_term', $term_object );
110
	}
111
112
	function set_taxonomy_whitelist( $taxonomies ) {
113
		$this->taxonomy_whitelist = $taxonomies;
114
	}
115
116
	function set_defaults() {
117
		$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...
118
	}
119
120
	public function expand_term_ids( $args ) {
121
		global $wp_version;
122
		$term_ids = $args[0];
123
		$taxonomy = $args[1];
124
		// version 4.5 or higher
125
		if ( version_compare( $wp_version, 4.5, '>=' ) ) {
126
			$terms = get_terms( array(
127
				'taxonomy'   => $taxonomy,
128
				'hide_empty' => false,
129
				'include'    => $term_ids,
130
			) );
131
		} else {
132
			$terms = get_terms( $taxonomy, array(
133
				'hide_empty' => false,
134
				'include'    => $term_ids,
135
			) );
136
		}
137
138
		return $terms;
139
	}
140
}
141