Completed
Push — add/sync_term_actions ( 6f0123...b958f7 )
by
unknown
08:22
created

Jetpack_Sync_Module_Terms::set_defaults()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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