class.jetpack-sync-module-terms.php 3.6 KB

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