Automattic /
jetpack
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 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, 10, 4 ); |
||
| 14 | add_action( 'delete_term', $callable, 10, 4 ); |
||
| 15 | add_action( 'set_object_terms', $callable, 10, 6 ); |
||
| 16 | add_action( 'deleted_term_relationships', $callable, 10, 2 ); |
||
| 17 | } |
||
| 18 | |||
| 19 | public function init_full_sync_listeners( $callable ) { |
||
| 20 | add_action( 'jetpack_full_sync_terms', $callable, 10, 2 ); |
||
| 21 | } |
||
| 22 | |||
| 23 | function init_before_send() { |
||
| 24 | // full sync |
||
| 25 | add_filter( 'jetpack_sync_before_send_jetpack_full_sync_terms', array( $this, 'expand_term_ids' ) ); |
||
| 26 | } |
||
| 27 | |||
| 28 | function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { |
||
| 29 | global $wpdb; |
||
| 30 | |||
| 31 | // TODO: process state |
||
|
0 ignored issues
–
show
Coding Style
Best Practice
introduced
by
Loading history...
|
|||
| 32 | |||
| 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 | /** |
||
| 77 | * Fires when the client needs to sync a new term |
||
| 78 | * |
||
| 79 | * @since 4.2.0 |
||
| 80 | * |
||
| 81 | * @param object the Term object |
||
| 82 | */ |
||
| 83 | do_action( 'jetpack_sync_save_term', $term_object ); |
||
| 84 | } |
||
| 85 | |||
| 86 | function set_taxonomy_whitelist( $taxonomies ) { |
||
| 87 | $this->taxonomy_whitelist = $taxonomies; |
||
| 88 | } |
||
| 89 | |||
| 90 | function set_defaults() { |
||
| 91 | $this->taxonomy_whitelist = Jetpack_Sync_Defaults::$default_taxonomy_whitelist; |
||
| 92 | } |
||
| 93 | |||
| 94 | public function expand_term_ids( $args ) { |
||
| 95 | global $wp_version; |
||
| 96 | $term_ids = $args[0]; |
||
| 97 | $taxonomy = $args[1]; |
||
| 98 | // version 4.5 or higher |
||
| 99 | if ( version_compare( $wp_version, 4.5, '>=' ) ) { |
||
| 100 | $terms = get_terms( array( |
||
| 101 | 'taxonomy' => $taxonomy, |
||
| 102 | 'hide_empty' => false, |
||
| 103 | 'include' => $term_ids, |
||
| 104 | ) ); |
||
| 105 | } else { |
||
| 106 | $terms = get_terms( $taxonomy, array( |
||
| 107 | 'hide_empty' => false, |
||
| 108 | 'include' => $term_ids, |
||
| 109 | ) ); |
||
| 110 | } |
||
| 111 | |||
| 112 | return $terms; |
||
| 113 | } |
||
| 114 | } |
||
| 115 |