Completed
Push — psr4-jetpack-sync-module ( 910f64...b1c668 )
by
unknown
284:58 queued 277:10
created

Terms   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 112
Duplicated Lines 17.86 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
wmc 16
lcom 2
cbo 2
dl 20
loc 112
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A name() 0 3 1
A init_listeners() 0 9 1
A init_full_sync_listeners() 0 3 1
A init_before_send() 0 4 1
A enqueue_full_sync_actions() 0 4 1
A get_where_sql() 7 7 2
A get_full_sync_actions() 0 3 1
A save_term_handler() 0 30 3
A set_taxonomy_whitelist() 0 3 1
A set_defaults() 0 3 1
A expand_term_taxonomy_id() 0 15 1
A estimate_full_sync_actions() 13 13 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Automattic\Jetpack\Sync\Modules;
4
5
use Automattic\Jetpack\Sync\Defaults;
6
7
class Terms extends Module {
8
	private $taxonomy_whitelist;
9
10
	function name() {
11
		return 'terms';
12
	}
13
14
	function init_listeners( $callable ) {
15
		add_action( 'created_term', array( $this, 'save_term_handler' ), 10, 3 );
16
		add_action( 'edited_term', array( $this, 'save_term_handler' ), 10, 3 );
17
		add_action( 'jetpack_sync_save_term', $callable );
18
		add_action( 'jetpack_sync_add_term', $callable );
19
		add_action( 'delete_term', $callable, 10, 4 );
20
		add_action( 'set_object_terms', $callable, 10, 6 );
21
		add_action( 'deleted_term_relationships', $callable, 10, 2 );
22
	}
23
24
	public function init_full_sync_listeners( $callable ) {
25
		add_action( 'jetpack_full_sync_terms', $callable, 10, 2 );
26
	}
27
28
	function init_before_send() {
29
		// full sync
30
		add_filter( 'jetpack_sync_before_send_jetpack_full_sync_terms', array( $this, 'expand_term_taxonomy_id' ) );
31
	}
32
33
	public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) {
34
		global $wpdb;
35
		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 );
36
	}
37
38 View Code Duplication
	private function get_where_sql( $config ) {
39
		if ( is_array( $config ) ) {
40
			return 'term_taxonomy_id IN (' . implode( ',', array_map( 'intval', $config ) ) . ')';
41
		}
42
43
		return '';
44
	}
45
46 View Code Duplication
	public function estimate_full_sync_actions( $config ) {
47
		global $wpdb;
48
49
		$query = "SELECT count(*) FROM $wpdb->term_taxonomy";
50
51
		if ( $where_sql = $this->get_where_sql( $config ) ) {
52
			$query .= ' WHERE ' . $where_sql;
53
		}
54
55
		$count = $wpdb->get_var( $query );
56
57
		return (int) ceil( $count / self::ARRAY_CHUNK_SIZE );
58
	}
59
60
	function get_full_sync_actions() {
61
		return array( 'jetpack_full_sync_terms' );
62
	}
63
64
	function save_term_handler( $term_id, $tt_id, $taxonomy ) {
65
		if ( class_exists( '\\WP_Term' ) ) {
66
			$term_object = \WP_Term::get_instance( $term_id, $taxonomy );
67
		} else {
68
			$term_object = get_term_by( 'id', $term_id, $taxonomy );
69
		}
70
71
		$current_filter = current_filter();
72
73
		if ( 'created_term' === $current_filter ) {
74
			/**
75
			 * Fires when the client needs to add a new term
76
			 *
77
			 * @since 5.0.0
78
			 *
79
			 * @param object the Term object
80
			 */
81
			do_action( 'jetpack_sync_add_term', $term_object );
82
			return;
83
		}
84
85
		/**
86
		 * Fires when the client needs to update a term
87
		 *
88
		 * @since 4.2.0
89
		 *
90
		 * @param object the Term object
91
		 */
92
		do_action( 'jetpack_sync_save_term', $term_object );
93
	}
94
95
	function set_taxonomy_whitelist( $taxonomies ) {
96
		$this->taxonomy_whitelist = $taxonomies;
97
	}
98
99
	function set_defaults() {
100
		$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...
101
	}
102
103
	public function expand_term_taxonomy_id( $args ) {
104
		list( $term_taxonomy_ids,  $previous_end ) = $args;
105
106
		return array(
107
			'terms'        => get_terms(
108
				array(
109
					'hide_empty'       => false,
110
					'term_taxonomy_id' => $term_taxonomy_ids,
111
					'orderby'          => 'term_taxonomy_id',
112
					'order'            => 'DESC',
113
				)
114
			),
115
			'previous_end' => $previous_end,
116
		);
117
	}
118
}
119