class.jetpack-sync-module-meta.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. class Jetpack_Sync_Module_Meta extends Jetpack_Sync_Module {
  3. public function name() {
  4. return 'meta';
  5. }
  6. /**
  7. * This implementation of get_objects_by_id() is a bit hacky since we're not passing in an array of meta IDs,
  8. * but instead an array of post or comment IDs for which to retrieve meta for. On top of that,
  9. * we also pass in an associative array where we expect there to be 'meta_key' and 'ids' keys present.
  10. *
  11. * This seemed to be required since if we have missing meta on WP.com and need to fetch it, we don't know what
  12. * the meta key is, but we do know that we have missing meta for a given post or comment.
  13. *
  14. * @param string $object_type The type of object for which we retrieve meta. Either 'post' or 'comment'
  15. * @param array $config Must include 'meta_key' and 'ids' keys
  16. *
  17. * @return array
  18. */
  19. public function get_objects_by_id( $object_type, $config ) {
  20. global $wpdb;
  21. $table = _get_meta_table( $object_type );
  22. if ( ! $table ) {
  23. return array();
  24. }
  25. if ( ! isset( $config['meta_key'] ) || ! isset( $config['ids'] ) || ! is_array( $config['ids'] ) ) {
  26. return array();
  27. }
  28. $meta_key = $config['meta_key'];
  29. $ids = $config['ids'];
  30. $object_id_column = $object_type.'_id';
  31. // Sanitize so that the array only has integer values
  32. $ids_string = implode( ', ', array_map( 'intval', $ids ) );
  33. $metas = $wpdb->get_results(
  34. $wpdb->prepare(
  35. "SELECT * FROM {$table} WHERE {$object_id_column} IN ( {$ids_string} ) AND meta_key = %s",
  36. $meta_key
  37. )
  38. );
  39. $meta_objects = array();
  40. foreach( (array) $metas as $meta_object ) {
  41. $meta_object = (array) $meta_object;
  42. $meta_objects[ $meta_object[ $object_id_column ] ] = array(
  43. 'meta_type' => $object_type,
  44. 'meta_id' => $meta_object['meta_id'],
  45. 'meta_key' => $meta_key,
  46. 'meta_value' => $meta_object['meta_value'],
  47. 'object_id' => $meta_object[ $object_id_column ],
  48. );
  49. }
  50. return $meta_objects;
  51. }
  52. }