Completed
Push — master ( c4d923...29c181 )
by Marin
58:57 queued 51:31
created

Terms::get_object_by_id()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 5
nop 2
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Automattic\Jetpack\Sync\Modules;
4
5
use Automattic\Jetpack\Sync\Defaults;
6
use Automattic\Jetpack\Sync\Settings;
7
8
class Terms extends Module {
9
	private $taxonomy_whitelist;
10
11
	function name() {
12
		return 'terms';
13
	}
14
15
	/**
16
	 * Allows WordPress.com servers to retrieve a term object via the sync API.
17
	 *
18
	 * @param string $object_type The type of object.
19
	 * @param int $id The id of the object.
20
	 *
21
	 * @return bool|\WP_Term
22
	 */
23
	public function get_object_by_id( $object_type, $id ) {
24
		if ( $object_type === 'term' ) {
25
			$term = get_term( intval( $id ) );
26
			return ( $term && ! is_wp_error( $term ) ) ? $term : false;
27
		}
28
29
		return false;
30
	}
31
32
	function init_listeners( $callable ) {
33
		add_action( 'created_term', array( $this, 'save_term_handler' ), 10, 3 );
34
		add_action( 'edited_term', array( $this, 'save_term_handler' ), 10, 3 );
35
		add_action( 'jetpack_sync_save_term', $callable );
36
		add_action( 'jetpack_sync_add_term', $callable );
37
		add_action( 'delete_term', $callable, 10, 4 );
38
		add_action( 'set_object_terms', $callable, 10, 6 );
39
		add_action( 'deleted_term_relationships', $callable, 10, 2 );
40
		add_filter( 'jetpack_sync_before_enqueue_jetpack_sync_save_term', array( $this, 'filter_blacklisted_taxonomies' ) );
41
		add_filter( 'jetpack_sync_before_enqueue_jetpack_sync_add_term', array( $this, 'filter_blacklisted_taxonomies' ) );
42
	}
43
44
	public function init_full_sync_listeners( $callable ) {
45
		add_action( 'jetpack_full_sync_terms', $callable, 10, 2 );
46
	}
47
48
	function init_before_send() {
49
		// full sync
50
		add_filter( 'jetpack_sync_before_send_jetpack_full_sync_terms', array( $this, 'expand_term_taxonomy_id' ) );
51
	}
52
53
	public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) {
54
		global $wpdb;
55
		return $this->enqueue_all_ids_as_action( 'jetpack_full_sync_terms', $wpdb->term_taxonomy, 'term_taxonomy_id', $this->get_where_sql( $config ), $max_items_to_enqueue, $state );
56
	}
57
58 View Code Duplication
	private function get_where_sql( $config ) {
59
		$where_sql = Settings::get_blacklisted_taxonomies_sql();
60
61
		if ( is_array( $config ) ) {
62
			$where_sql .= ' AND term_taxonomy_id IN (' . implode( ',', array_map( 'intval', $config ) ) . ')';
63
		}
64
65
		return $where_sql;
66
	}
67
68 View Code Duplication
	public function estimate_full_sync_actions( $config ) {
69
		global $wpdb;
70
71
		$query = "SELECT count(*) FROM $wpdb->term_taxonomy";
72
73
		if ( $where_sql = $this->get_where_sql( $config ) ) {
74
			$query .= ' WHERE ' . $where_sql;
75
		}
76
77
		$count = $wpdb->get_var( $query );
78
79
		return (int) ceil( $count / self::ARRAY_CHUNK_SIZE );
80
	}
81
82
	function get_full_sync_actions() {
83
		return array( 'jetpack_full_sync_terms' );
84
	}
85
86
	function save_term_handler( $term_id, $tt_id, $taxonomy ) {
87
		if ( class_exists( '\\WP_Term' ) ) {
88
			$term_object = \WP_Term::get_instance( $term_id, $taxonomy );
89
		} else {
90
			$term_object = get_term_by( 'id', $term_id, $taxonomy );
91
		}
92
93
		$current_filter = current_filter();
94
95
		if ( 'created_term' === $current_filter ) {
96
			/**
97
			 * Fires when the client needs to add a new term
98
			 *
99
			 * @since 5.0.0
100
			 *
101
			 * @param object the Term object
102
			 */
103
			do_action( 'jetpack_sync_add_term', $term_object );
104
			return;
105
		}
106
107
		/**
108
		 * Fires when the client needs to update a term
109
		 *
110
		 * @since 4.2.0
111
		 *
112
		 * @param object the Term object
113
		 */
114
		do_action( 'jetpack_sync_save_term', $term_object );
115
	}
116
117
	function filter_blacklisted_taxonomies( $args ) {
118
		$term = $args[0];
119
120
		if ( in_array( $term->taxonomy, Settings::get_setting( 'taxonomies_blacklist' ), true ) ) {
121
			return false;
122
		}
123
124
		return $args;
125
	}
126
127
	function set_taxonomy_whitelist( $taxonomies ) {
128
		$this->taxonomy_whitelist = $taxonomies;
129
	}
130
131
	function set_defaults() {
132
		$this->taxonomy_whitelist = 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 Automattic\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...
133
	}
134
135
	public function expand_term_taxonomy_id( $args ) {
136
		list( $term_taxonomy_ids,  $previous_end ) = $args;
137
138
		return array(
139
			'terms'        => get_terms(
140
				array(
141
					'hide_empty'       => false,
142
					'term_taxonomy_id' => $term_taxonomy_ids,
143
					'orderby'          => 'term_taxonomy_id',
144
					'order'            => 'DESC',
145
				)
146
			),
147
			'previous_end' => $previous_end,
148
		);
149
	}
150
}
151