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