Completed
Push — fix/refactor-sync-meta-module ( 46351d )
by
unknown
11:13 queued 02:59
created

Jetpack_Sync_Module_Meta::get_objects_by_id()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 40
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 25
nc 4
nop 2
dl 0
loc 40
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
class Jetpack_Sync_Module_Meta extends Jetpack_Sync_Module {
4
	private $meta_types = array( 'post', 'comment' );
5
6
	public function name() {
7
		return 'meta';
8
	}
9
10
	/**
11
	 * This implementation of get_objects_by_id() is a bit hacky since we're not passing in an array of meta IDs,
12
	 * but instead an array of post or comment IDs for which to retrieve meta for. On top of that,
13
	 * we also pass in an associative array where we expect there to be 'meta_key' and 'ids' keys present.
14
	 *
15
	 * This seemed to be required since if we have missing meta on WP.com and need to fetch it, we don't know what
16
	 * the meta key is, but we do know that we have missing meta for a given post or comment.
17
	 *
18
	 * @param string $object_type The type of object for which we retrieve meta. Either 'post' or 'comment'
19
	 * @param array $config Must include 'meta_key' and 'ids' keys
20
	 *
21
	 * @return array
22
	 */
23
	public function get_objects_by_id( $object_type, $config ) {
24
		global $wpdb;
25
26
		$table = _get_meta_table( $object_type );
27
28
		if ( ! $table ) {
29
			return array();
30
		}
31
32
		if ( ! isset( $config['meta_key'] ) || ! isset( $config['ids'] ) || ! is_array( $config['ids'] ) ) {
33
			return array();
34
		}
35
36
		$meta_key = $config['meta_key'];
37
		$ids = $config['ids'];
38
		$object_id_column = $object_type.'_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['meta_id'],
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
	public function init_listeners( $callable ) {
65
		foreach ( $this->meta_types as $meta_type ) {
66
			$this->init_listeners_for_meta_type( $meta_type, $callable );
67
68
			$whitelist_handler = array( $this, 'filter_meta_' . $meta_type );
69
			add_filter( "jetpack_sync_before_enqueue_added_{$meta_type}_meta", $whitelist_handler );
70
			add_filter( "jetpack_sync_before_enqueue_updated_{$meta_type}_meta", $whitelist_handler );
71
			add_filter( "jetpack_sync_before_enqueue_deleted_{$meta_type}_meta", $whitelist_handler );
72
		}
73
	}
74
75
	public function init_listeners_for_meta_type( $meta_type, $callable ) {
76
		add_action( "added_{$meta_type}_meta", $callable, 10, 4 );
77
		add_action( "updated_{$meta_type}_meta", $callable, 10, 4 );
78
		add_action( "deleted_{$meta_type}_meta", $callable, 10, 4 );
79
	}
80
81
	// POST Meta
82
	function is_whitelisted_post_meta( $meta_key ) {
83
		// _wpas_skip_ is used by publicize
84
		return in_array( $meta_key, Jetpack_Sync_Settings::get_setting( 'post_meta_whitelist' ) ) || wp_startswith( $meta_key, '_wpas_skip_' );
85
	}
86
87
	// Comment Meta
88
	function is_whitelisted_comment_meta( $meta_key ) {
89
		return in_array( $meta_key, Jetpack_Sync_Settings::get_setting( 'comment_meta_whitelist' ) );
90
	}
91
92
	function is_post_type_allowed( $post_id ) {
93
		$post = get_post( $post_id );
94
		return ! in_array( $post->post_type, Jetpack_Sync_Settings::get_setting( 'post_types_blacklist' ) );
95
	}
96
97
	function filter_meta_post( $args ) {
98
		if ( ! $this->is_whitelisted_post_meta( $args[2] ) ) {
99
			return false;
100
		}
101
		return ( $this->is_post_type_allowed( $args[1] ) ? $args : false );
102
	}
103
104
	function filter_meta_comment( $args ) {
105
		return ( $this->is_whitelisted_comment_meta( $args[2] ) ? $args : false );
106
	}
107
	
108
}
109