class.jetpack-sync-module.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * Basic methods implemented by Jetpack Sync extensions
  4. */
  5. abstract class Jetpack_Sync_Module {
  6. const ARRAY_CHUNK_SIZE = 10;
  7. abstract public function name();
  8. public function get_object_by_id( $object_type, $id ) {
  9. return false;
  10. }
  11. // override these to set up listeners and set/reset data/defaults
  12. public function init_listeners( $callable ) {
  13. }
  14. public function init_full_sync_listeners( $callable ) {
  15. }
  16. public function init_before_send() {
  17. }
  18. public function set_defaults() {
  19. }
  20. public function reset_data() {
  21. }
  22. public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) {
  23. // in subclasses, return the number of actions enqueued, and next module state (true == done)
  24. return array( 0, true );
  25. }
  26. public function estimate_full_sync_actions( $config ) {
  27. // in subclasses, return the number of items yet to be enqueued
  28. return 0;
  29. }
  30. public function get_full_sync_actions() {
  31. return array();
  32. }
  33. protected function count_actions( $action_names, $actions_to_count ) {
  34. return count( array_intersect( $action_names, $actions_to_count ) );
  35. }
  36. protected function get_check_sum( $values ) {
  37. return crc32( wp_json_encode( jetpack_json_wrap( $values ) ) );
  38. }
  39. protected function still_valid_checksum( $sums_to_check, $name, $new_sum ) {
  40. if ( isset( $sums_to_check[ $name ] ) && $sums_to_check[ $name ] === $new_sum ) {
  41. return true;
  42. }
  43. return false;
  44. }
  45. protected function enqueue_all_ids_as_action( $action_name, $table_name, $id_field, $where_sql, $max_items_to_enqueue, $state ) {
  46. global $wpdb;
  47. if ( ! $where_sql ) {
  48. $where_sql = '1 = 1';
  49. }
  50. $items_per_page = 1000;
  51. $page = 1;
  52. $chunk_count = 0;
  53. $previous_max_id = $state ? $state : '~0';
  54. $listener = Jetpack_Sync_Listener::get_instance();
  55. // count down from max_id to min_id so we get newest posts/comments/etc first
  56. while ( $ids = $wpdb->get_col( "SELECT {$id_field} FROM {$table_name} WHERE {$where_sql} AND {$id_field} < {$previous_max_id} ORDER BY {$id_field} DESC LIMIT {$items_per_page}" ) ) {
  57. // Request posts in groups of N for efficiency
  58. $chunked_ids = array_chunk( $ids, self::ARRAY_CHUNK_SIZE );
  59. // if we hit our row limit, process and return
  60. if ( $chunk_count + count( $chunked_ids ) >= $max_items_to_enqueue ) {
  61. $remaining_items_count = $max_items_to_enqueue - $chunk_count;
  62. $remaining_items = array_slice( $chunked_ids, 0, $remaining_items_count );
  63. $listener->bulk_enqueue_full_sync_actions( $action_name, $remaining_items );
  64. $last_chunk = end( $remaining_items );
  65. return array( $remaining_items_count + $chunk_count, end( $last_chunk ) );
  66. }
  67. $listener->bulk_enqueue_full_sync_actions( $action_name, $chunked_ids );
  68. $chunk_count += count( $chunked_ids );
  69. $page += 1;
  70. $previous_max_id = end( $ids );
  71. }
  72. return array( $chunk_count, true );
  73. }
  74. protected function get_metadata( $ids, $meta_type, $meta_key_whitelist ) {
  75. global $wpdb;
  76. $table = _get_meta_table( $meta_type );
  77. $id = $meta_type . '_id';
  78. if ( ! $table ) {
  79. return array();
  80. }
  81. $private_meta_whitelist_sql = "'" . implode( "','", array_map( 'esc_sql', $meta_key_whitelist ) ) . "'";
  82. return array_map(
  83. array( $this, 'unserialize_meta' ),
  84. $wpdb->get_results(
  85. "SELECT $id, meta_key, meta_value, meta_id FROM $table WHERE $id IN ( " . implode( ',', wp_parse_id_list( $ids ) ) . ' )'.
  86. " AND meta_key IN ( $private_meta_whitelist_sql ) "
  87. , OBJECT )
  88. );
  89. }
  90. public function init_listeners_for_meta_type( $meta_type, $callable ) {
  91. add_action( "added_{$meta_type}_meta", $callable, 10, 4 );
  92. add_action( "updated_{$meta_type}_meta", $callable, 10, 4 );
  93. add_action( "deleted_{$meta_type}_meta", $callable, 10, 4 );
  94. }
  95. public function init_meta_whitelist_handler( $meta_type, $whitelist_handler ) {
  96. add_filter( "jetpack_sync_before_enqueue_added_{$meta_type}_meta", $whitelist_handler );
  97. add_filter( "jetpack_sync_before_enqueue_updated_{$meta_type}_meta", $whitelist_handler );
  98. add_filter( "jetpack_sync_before_enqueue_deleted_{$meta_type}_meta", $whitelist_handler );
  99. }
  100. protected function get_term_relationships( $ids ) {
  101. global $wpdb;
  102. return $wpdb->get_results( "SELECT object_id, term_taxonomy_id FROM $wpdb->term_relationships WHERE object_id IN ( " . implode( ',', wp_parse_id_list( $ids ) ) . ' )', OBJECT );
  103. }
  104. public function unserialize_meta( $meta ) {
  105. $meta->meta_value = maybe_unserialize( $meta->meta_value );
  106. return $meta;
  107. }
  108. public function get_objects_by_id( $object_type, $ids ) {
  109. if ( empty( $ids ) || empty( $object_type ) ) {
  110. return array();
  111. }
  112. $objects = array();
  113. foreach( (array) $ids as $id ) {
  114. $object = $this->get_object_by_id( $object_type, $id );
  115. // Only add object if we have the object.
  116. if ( $object ) {
  117. $objects[ $id ] = $object;
  118. }
  119. }
  120. return $objects;
  121. }
  122. }