1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class Jetpack_Sync_Module_Terms extends Jetpack_Sync_Module { |
4
|
|
|
private $taxonomy_whitelist; |
5
|
|
|
|
6
|
|
|
private $callable; |
7
|
|
|
|
8
|
|
|
function name() { |
9
|
|
|
return 'terms'; |
10
|
|
|
} |
11
|
|
|
|
12
|
|
|
function init_listeners( $callable ) { |
13
|
|
|
$this->callable = $callable; |
14
|
|
|
add_action( 'created_term', array( $this, 'save_term_handler' ), 10, 3 ); |
15
|
|
|
add_action( 'edited_term', array( $this, 'save_term_handler' ), 10, 3 ); |
16
|
|
|
add_action( 'jetpack_sync_save_term', $callable ); |
17
|
|
|
add_action( 'jetpack_sync_add_term', $callable ); |
18
|
|
|
add_action( 'delete_term', $callable, 10, 4 ); |
19
|
|
|
add_action( 'set_object_terms', array( $this, 'set_object_terms' ), 10, 6 ); |
20
|
|
|
add_action( 'deleted_term_relationships', $callable, 10, 2 ); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function set_object_terms() { |
24
|
|
|
if ( $this->is_save_post() ) { |
25
|
|
|
return; |
26
|
|
|
} |
27
|
|
|
call_user_func_array( $this->callable, func_get_args() ); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
private function is_save_post() { |
31
|
|
|
return Jetpack::is_function_in_backtrace( 'wp_insert_post' ); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function init_full_sync_listeners( $callable ) { |
35
|
|
|
add_action( 'jetpack_full_sync_terms', $callable, 10, 2 ); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
function init_before_send() { |
39
|
|
|
// full sync |
40
|
|
|
add_filter( 'jetpack_sync_before_send_jetpack_full_sync_terms', array( $this, 'expand_term_ids' ) ); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { |
44
|
|
|
global $wpdb; |
45
|
|
|
|
46
|
|
|
// TODO: process state |
|
|
|
|
47
|
|
|
|
48
|
|
|
$taxonomies = get_taxonomies(); |
49
|
|
|
$total_chunks_counter = 0; |
50
|
|
|
foreach ( $taxonomies as $taxonomy ) { |
51
|
|
|
// I hope this is never bigger than RAM... |
52
|
|
|
$term_ids = $wpdb->get_col( $wpdb->prepare( "SELECT term_id FROM $wpdb->term_taxonomy WHERE taxonomy = %s", $taxonomy ) ); // Should we set a limit here? |
53
|
|
|
// Request posts in groups of N for efficiency |
54
|
|
|
$chunked_term_ids = array_chunk( $term_ids, self::ARRAY_CHUNK_SIZE ); |
55
|
|
|
|
56
|
|
|
// Send each chunk as an array of objects |
57
|
|
|
foreach ( $chunked_term_ids as $chunk ) { |
58
|
|
|
do_action( 'jetpack_full_sync_terms', $chunk, $taxonomy ); |
59
|
|
|
$total_chunks_counter ++; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return array( $total_chunks_counter, true ); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
function estimate_full_sync_actions( $config ) { |
67
|
|
|
// TODO - make this (and method above) more efficient for large numbers of terms or taxonomies |
|
|
|
|
68
|
|
|
global $wpdb; |
69
|
|
|
|
70
|
|
|
$taxonomies = get_taxonomies(); |
71
|
|
|
$total_chunks_counter = 0; |
72
|
|
|
foreach ( $taxonomies as $taxonomy ) { |
73
|
|
|
$total_ids = $wpdb->get_var( $wpdb->prepare( "SELECT count(term_id) FROM $wpdb->term_taxonomy WHERE taxonomy = %s", $taxonomy ) ); |
74
|
|
|
$total_chunks_counter += (int) ceil( $total_ids / self::ARRAY_CHUNK_SIZE ); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $total_chunks_counter; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
function get_full_sync_actions() { |
81
|
|
|
return array( 'jetpack_full_sync_terms' ); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
function save_term_handler( $term_id, $tt_id, $taxonomy ) { |
85
|
|
|
if ( class_exists( 'WP_Term' ) ) { |
86
|
|
|
$term_object = WP_Term::get_instance( $term_id, $taxonomy ); |
87
|
|
|
} else { |
88
|
|
|
$term_object = get_term_by( 'id', $term_id, $taxonomy ); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$current_filter = current_filter(); |
92
|
|
|
|
93
|
|
|
if ( 'created_term' === $current_filter ) { |
94
|
|
|
/** |
95
|
|
|
* Fires when the client needs to add a new term |
96
|
|
|
* |
97
|
|
|
* @since 5.0.0 |
98
|
|
|
* |
99
|
|
|
* @param object the Term object |
100
|
|
|
*/ |
101
|
|
|
do_action( 'jetpack_sync_add_term', $term_object ); |
102
|
|
|
return; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Fires when the client needs to update a term |
107
|
|
|
* |
108
|
|
|
* @since 4.2.0 |
109
|
|
|
* |
110
|
|
|
* @param object the Term object |
111
|
|
|
*/ |
112
|
|
|
do_action( 'jetpack_sync_save_term', $term_object ); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
function set_taxonomy_whitelist( $taxonomies ) { |
116
|
|
|
$this->taxonomy_whitelist = $taxonomies; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
function set_defaults() { |
120
|
|
|
$this->taxonomy_whitelist = Jetpack_Sync_Defaults::$default_taxonomy_whitelist; |
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function expand_term_ids( $args ) { |
124
|
|
|
global $wp_version; |
125
|
|
|
$term_ids = $args[0]; |
126
|
|
|
$taxonomy = $args[1]; |
127
|
|
|
// version 4.5 or higher |
128
|
|
|
if ( version_compare( $wp_version, 4.5, '>=' ) ) { |
129
|
|
|
$terms = get_terms( array( |
130
|
|
|
'taxonomy' => $taxonomy, |
131
|
|
|
'hide_empty' => false, |
132
|
|
|
'include' => $term_ids, |
133
|
|
|
) ); |
134
|
|
|
} else { |
135
|
|
|
$terms = get_terms( $taxonomy, array( |
136
|
|
|
'hide_empty' => false, |
137
|
|
|
'include' => $term_ids, |
138
|
|
|
) ); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return $terms; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|