Completed
Push — fix/refactor-full-sync-status-... ( baeb97...466398 )
by
unknown
63:36 queued 54:01
created

Jetpack_Sync_Module_Terms::name()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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