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