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 ) ) { |
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
|
|
|
$whitelist_handler = array( $this, 'filter_meta' ); |
74
|
|
|
|
75
|
|
|
foreach ( $this->meta_types as $meta_type ) { |
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
|
|
|
add_filter( "jetpack_sync_before_enqueue_added_{$meta_type}_meta", $whitelist_handler ); |
81
|
|
|
add_filter( "jetpack_sync_before_enqueue_updated_{$meta_type}_meta", $whitelist_handler ); |
82
|
|
|
add_filter( "jetpack_sync_before_enqueue_deleted_{$meta_type}_meta", $whitelist_handler ); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Should we allow the meta key to be synced? |
88
|
|
|
* |
89
|
|
|
* @param string $meta_key The meta key. |
90
|
|
|
* |
91
|
|
|
* @return bool |
92
|
|
|
*/ |
93
|
|
|
function is_meta_key_allowed( $meta_key ) { |
94
|
|
|
if ( '_' === $meta_key[0] && |
95
|
|
|
! in_array( $meta_key, Jetpack_Sync_Defaults::$default_whitelist_meta_keys ) && |
|
|
|
|
96
|
|
|
! wp_startswith( $meta_key, '_wpas_skip_' ) |
97
|
|
|
) { |
98
|
|
|
return false; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if ( in_array( $meta_key, Jetpack_Sync_Settings::get_setting( 'meta_blacklist' ) ) ) { |
102
|
|
|
return false; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return true; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
function filter_meta( $args ) { |
109
|
|
|
if ( ! $this->is_meta_key_allowed( $args[2] ) ) { |
110
|
|
|
return false; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $args; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
This check looks for access to properties that are not accessible from the current context.
If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.