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