PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 6.9.2
Jetpack – WP Security, Backup, Speed, & Growth v6.9.2
12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 14.4.2 All 500 releases
jetpack / sync / class.jetpack-sync-module-terms.php
class.jetpack-sync-module-terms.php
130 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 return;
89 }
90
91 /**
92 * Fires when the client needs to update a term
93 *
94 * @since 4.2.0
95 *
96 * @param object the Term object
97 */
98 do_action( 'jetpack_sync_save_term', $term_object );
99 }
100
101 function set_taxonomy_whitelist( $taxonomies ) {
102 $this->taxonomy_whitelist = $taxonomies;
103 }
104
105 function set_defaults() {
106 $this->taxonomy_whitelist = Jetpack_Sync_Defaults::$default_taxonomy_whitelist;
107 }
108
109 public function expand_term_ids( $args ) {
110 global $wp_version;
111 $term_ids = $args[0];
112 $taxonomy = $args[1];
113 // version 4.5 or higher
114 if ( version_compare( $wp_version, 4.5, '>=' ) ) {
115 $terms = get_terms( array(
116 'taxonomy' => $taxonomy,
117 'hide_empty' => false,
118 'include' => $term_ids,
119 ) );
120 } else {
121 $terms = get_terms( $taxonomy, array(
122 'hide_empty' => false,
123 'include' => $term_ids,
124 ) );
125 }
126
127 return $terms;
128 }
129 }
130