class.jetpack-sync-module-terms.php
| 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 | $taxonomies = get_taxonomies(); |
| 34 | $total_chunks_counter = 0; |
| 35 | foreach ( $taxonomies as $taxonomy ) { |
| 36 | // I hope this is never bigger than RAM... |
| 37 | $term_ids = $wpdb->get_col( $wpdb->prepare( "SELECT term_id FROM $wpdb->term_taxonomy WHERE taxonomy = %s", $taxonomy ) ); // Should we set a limit here? |
| 38 | // Request posts in groups of N for efficiency |
| 39 | $chunked_term_ids = array_chunk( $term_ids, self::ARRAY_CHUNK_SIZE ); |
| 40 | |
| 41 | // Send each chunk as an array of objects |
| 42 | foreach ( $chunked_term_ids as $chunk ) { |
| 43 | do_action( 'jetpack_full_sync_terms', $chunk, $taxonomy ); |
| 44 | $total_chunks_counter ++; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | return array( $total_chunks_counter, true ); |
| 49 | } |
| 50 | |
| 51 | function estimate_full_sync_actions( $config ) { |
| 52 | // TODO - make this (and method above) more efficient for large numbers of terms or taxonomies |
| 53 | global $wpdb; |
| 54 | |
| 55 | $taxonomies = get_taxonomies(); |
| 56 | $total_chunks_counter = 0; |
| 57 | foreach ( $taxonomies as $taxonomy ) { |
| 58 | $total_ids = $wpdb->get_var( $wpdb->prepare( "SELECT count(term_id) FROM $wpdb->term_taxonomy WHERE taxonomy = %s", $taxonomy ) ); |
| 59 | $total_chunks_counter += (int) ceil( $total_ids / self::ARRAY_CHUNK_SIZE ); |
| 60 | } |
| 61 | |
| 62 | return $total_chunks_counter; |
| 63 | } |
| 64 | |
| 65 | function get_full_sync_actions() { |
| 66 | return array( 'jetpack_full_sync_terms' ); |
| 67 | } |
| 68 | |
| 69 | function save_term_handler( $term_id, $tt_id, $taxonomy ) { |
| 70 | if ( class_exists( 'WP_Term' ) ) { |
| 71 | $term_object = WP_Term::get_instance( $term_id, $taxonomy ); |
| 72 | } else { |
| 73 | $term_object = get_term_by( 'id', $term_id, $taxonomy ); |
| 74 | } |
| 75 | |
| 76 | $current_filter = current_filter(); |
| 77 | |
| 78 | if ( 'created_term' === $current_filter ) { |
| 79 | /** |
| 80 | * Fires when the client needs to add a new term |
| 81 | * |
| 82 | * @since 5.0.0 |
| 83 | * |
| 84 | * @param object the Term object |
| 85 | */ |
| 86 | do_action( 'jetpack_sync_add_term', $term_object ); |
| 87 | return; |
| 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 | $term_ids = $args[0]; |
| 110 | $taxonomy = $args[1]; |
| 111 | |
| 112 | return get_terms( |
| 113 | array( |
| 114 | 'taxonomy' => $taxonomy, |
| 115 | 'hide_empty' => false, |
| 116 | 'include' => $term_ids, |
| 117 | ) |
| 118 | ); |
| 119 | } |
| 120 | } |
| 121 |