Jetpack_Sync_Module

Basic methods implemented by Jetpack Sync extensions.

Defined (1)

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

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