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_taxonomy_id' ) ); |
| 27 | } |
| 28 | |
| 29 | public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { |
| 30 | global $wpdb; |
| 31 | return $this->enqueue_all_ids_as_action( 'jetpack_full_sync_terms', $wpdb->term_taxonomy, 'term_taxonomy_id', $this->get_where_sql( $config ), $max_items_to_enqueue, $state ); |
| 32 | } |
| 33 | |
| 34 | private function get_where_sql( $config ) { |
| 35 | if ( is_array( $config ) ) { |
| 36 | return 'term_taxonomy_id IN (' . implode( ',', array_map( 'intval', $config ) ) . ')'; |
| 37 | } |
| 38 | |
| 39 | return ''; |
| 40 | } |
| 41 | |
| 42 | public function estimate_full_sync_actions( $config ) { |
| 43 | global $wpdb; |
| 44 | |
| 45 | $query = "SELECT count(*) FROM $wpdb->term_taxonomy"; |
| 46 | |
| 47 | if ( $where_sql = $this->get_where_sql( $config ) ) { |
| 48 | $query .= ' WHERE ' . $where_sql; |
| 49 | } |
| 50 | |
| 51 | $count = $wpdb->get_var( $query ); |
| 52 | |
| 53 | return (int) ceil( $count / self::ARRAY_CHUNK_SIZE ); |
| 54 | } |
| 55 | |
| 56 | function get_full_sync_actions() { |
| 57 | return array( 'jetpack_full_sync_terms' ); |
| 58 | } |
| 59 | |
| 60 | function save_term_handler( $term_id, $tt_id, $taxonomy ) { |
| 61 | if ( class_exists( 'WP_Term' ) ) { |
| 62 | $term_object = WP_Term::get_instance( $term_id, $taxonomy ); |
| 63 | } else { |
| 64 | $term_object = get_term_by( 'id', $term_id, $taxonomy ); |
| 65 | } |
| 66 | |
| 67 | $current_filter = current_filter(); |
| 68 | |
| 69 | if ( 'created_term' === $current_filter ) { |
| 70 | /** |
| 71 | * Fires when the client needs to add a new term |
| 72 | * |
| 73 | * @since 5.0.0 |
| 74 | * |
| 75 | * @param object the Term object |
| 76 | */ |
| 77 | do_action( 'jetpack_sync_add_term', $term_object ); |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Fires when the client needs to update a term |
| 83 | * |
| 84 | * @since 4.2.0 |
| 85 | * |
| 86 | * @param object the Term object |
| 87 | */ |
| 88 | do_action( 'jetpack_sync_save_term', $term_object ); |
| 89 | } |
| 90 | |
| 91 | function set_taxonomy_whitelist( $taxonomies ) { |
| 92 | $this->taxonomy_whitelist = $taxonomies; |
| 93 | } |
| 94 | |
| 95 | function set_defaults() { |
| 96 | $this->taxonomy_whitelist = Jetpack_Sync_Defaults::$default_taxonomy_whitelist; |
| 97 | } |
| 98 | |
| 99 | public function expand_term_taxonomy_id( $args ) { |
| 100 | list( $term_taxonomy_ids, $previous_end ) = $args; |
| 101 | |
| 102 | return |
| 103 | array( 'terms' => get_terms( |
| 104 | array( |
| 105 | 'hide_empty' => false, |
| 106 | 'term_taxonomy_id' => $term_taxonomy_ids, |
| 107 | 'orderby' => 'term_taxonomy_id', |
| 108 | 'order' => 'DESC' |
| 109 | ) |
| 110 | ), |
| 111 | 'previous_end' => $previous_end |
| 112 | ); |
| 113 | } |
| 114 | } |
| 115 |