Completed
Push — add/estimate-totals-for-full-s... ( 58808f )
by
unknown
415:30 queued 405:27
created

Jetpack_Sync_Module_Terms::expand_term_ids()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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