Completed
Push — sync/user-meta ( e1df47...21525d )
by
unknown
14:15 queued 05:39
created

Jetpack_Sync_Module_Meta::name()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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
		$object_meta_id_column = ( 'user' === $object_type ? 'umeta_id' : 'meta_id' );
39
40
		// Sanitize so that the array only has integer values
41
		$ids_string = implode( ', ', array_map( 'intval', $ids ) );
42
		$metas = $wpdb->get_results(
43
			$wpdb->prepare(
44
				"SELECT * FROM {$table} WHERE {$object_id_column} IN ( {$ids_string} ) AND meta_key = %s",
45
				$meta_key
46
			)
47
		);
48
49
		$meta_objects = array();
50
		foreach( (array) $metas as $meta_object ) {
51
			$meta_object = (array) $meta_object;
52
			$meta_objects[ $meta_object[ $object_id_column ] ] = array(
53
				'meta_type' => $object_type,
54
				'meta_id' => $meta_object[ $object_meta_id_column ],
55
				'meta_key' => $meta_key,
56
				'meta_value' => $meta_object['meta_value'],
57
				'object_id' => $meta_object[ $object_id_column ],
58
			);
59
		}
60
61
		return $meta_objects;
62
	}
63
}
64