Completed
Push — core/custom-css-4.7 ( 7f4836...adaf63 )
by
unknown
09:28
created

Jetpack_Sync_Module_Meta::get_objects_by_id()   C

Complexity

Conditions 8
Paths 7

Size

Total Lines 48
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 31
nc 7
nop 2
dl 0
loc 48
rs 5.9322
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
		if ( ! in_array( $object_type, $this->meta_types ) ) {
26
			return array();
27
		}
28
29
		if ( ! isset( $config['meta_key'] ) || ! isset( $config['ids'] ) || ! is_array( $config['ids'] ) ) {
30
			return array();
31
		}
32
33
		$meta_key = $config['meta_key'];
34
		$ids = $config['ids'];
35
36
		if ( ! $this->is_meta_key_allowed( $meta_key ) ) {
0 ignored issues
show
Bug introduced by
The method is_meta_key_allowed() does not seem to exist on object<Jetpack_Sync_Module_Meta>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
37
			return array();
38
		}
39
40
		if ( 'post' == $object_type ) {
41
			$table = $wpdb->postmeta;
42
			$object_id_column = 'post_id';
43
		} else {
44
			$table = $wpdb->commentmeta;
45
			$object_id_column = 'comment_id';
46
		}
47
48
		// Sanitize so that the array only has integer values
49
		$ids_string = implode( ', ', array_map( 'intval', $ids ) );
50
		$metas = $wpdb->get_results(
51
			$wpdb->prepare(
52
				"SELECT * FROM {$table} WHERE {$object_id_column} IN ( {$ids_string} ) AND meta_key = %s",
53
				$meta_key
54
			)
55
		);
56
57
		$meta_objects = array();
58
		foreach( (array) $metas as $meta_object ) {
59
			$meta_object = (array) $meta_object;
60
			$meta_objects[ $meta_object[ $object_id_column ] ] = array(
61
				'meta_type' => $object_type,
62
				'meta_id' => $meta_object['meta_id'],
63
				'meta_key' => $meta_key,
64
				'meta_value' => $meta_object['meta_value'],
65
				'object_id' => $meta_object[ $object_id_column ],
66
			);
67
		}
68
69
		return $meta_objects;
70
	}
71
72
	public function init_listeners( $callable ) {
73
		foreach ( $this->meta_types as $meta_type ) {
74
			add_action( "added_{$meta_type}_meta", $callable, 10, 4 );
75
			add_action( "updated_{$meta_type}_meta", $callable, 10, 4 );
76
			add_action( "deleted_{$meta_type}_meta", $callable, 10, 4 );
77
78
			$whitelist_handler = array( $this, 'filter_meta_' . $meta_type );
79
			add_filter( "jetpack_sync_before_enqueue_added_{$meta_type}_meta", $whitelist_handler );
80
			add_filter( "jetpack_sync_before_enqueue_updated_{$meta_type}_meta", $whitelist_handler );
81
			add_filter( "jetpack_sync_before_enqueue_deleted_{$meta_type}_meta", $whitelist_handler );
82
		}
83
	}
84
	// POST Meta
85
	function get_post_meta_whitelist() {
86
		return Jetpack_Sync_Settings::get_setting( 'post_meta_whitelist' );
87
	}
88
89
	function is_whitelisted_post_meta( $meta_key ) {
90
		// _wpas_skip_ is used by publicize
91
		return in_array( $meta_key, $this->get_post_meta_whitelist() ) || wp_startswith( $meta_key, '_wpas_skip_' );
92
	}
93
94
	// Comment Meta
95
	function get_comment_meta_whitelist() {
96
		return Jetpack_Sync_Settings::get_setting( 'comment_meta_whitelist' );
97
	}
98
99
	function is_whitelisted_comment_meta( $meta_key ) {
100
		return in_array( $meta_key, $this->get_comment_meta_whitelist() );
101
	}
102
103
	function is_post_type_allowed( $post_id ) {
104
		$post = get_post( $post_id );
105
		return ! in_array( $post->post_type, Jetpack_Sync_Settings::get_setting( 'post_types_blacklist' ) );
106
	}
107
108
	function filter_meta_post( $args ) {
109
		if ( ! $this->is_whitelisted_post_meta( $args[2] ) ) {
110
			return false;
111
		}
112
		return ( $this->is_post_type_allowed( $args[1] ) ? $args : false );
113
	}
114
115
	function filter_meta_comment( $args ) {
116
		return ( $this->is_whitelisted_comment_meta( $args[2] ) ? $args : false );
117
	}
118
	
119
}
120