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 |
||
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 | 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 | 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; |
||
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 |