Completed
Push — psr4-jetpack-sync-module ( 862b11...910f64 )
by
unknown
277:16 queued 269:36
created

Meta   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 59
c 0
b 0
f 0
rs 10
wmc 7
lcom 0
cbo 0
1
<?php
2
3
namespace Automattic\Jetpack\Sync\Modules;
4
^
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

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