Jetpack_Sync_Module_Terms

The WordPress Core Jetpack Sync Module Terms class.

Defined (1)

The class is defined in the following location(s).

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