1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Meta sync module. |
4
|
|
|
* |
5
|
|
|
* @package automattic/jetpack-sync |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Automattic\Jetpack\Sync\Modules; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class to handle sync for meta. |
12
|
|
|
*/ |
13
|
|
|
class Meta extends Module { |
14
|
|
|
/** |
15
|
|
|
* Sync module name. |
16
|
|
|
* |
17
|
|
|
* @access public |
18
|
|
|
* |
19
|
|
|
* @return string |
20
|
|
|
*/ |
21
|
|
|
public function name() { |
22
|
|
|
return 'meta'; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* This implementation of get_objects_by_id() is a bit hacky since we're not passing in an array of meta IDs, |
27
|
|
|
* but instead an array of post or comment IDs for which to retrieve meta for. On top of that, |
28
|
|
|
* we also pass in an associative array where we expect there to be 'meta_key' and 'ids' keys present. |
29
|
|
|
* |
30
|
|
|
* This seemed to be required since if we have missing meta on WP.com and need to fetch it, we don't know what |
31
|
|
|
* the meta key is, but we do know that we have missing meta for a given post or comment. |
32
|
|
|
* |
33
|
|
|
* @todo Refactor the $wpdb->prepare call to use placeholders. |
34
|
|
|
* |
35
|
|
|
* @param string $object_type The type of object for which we retrieve meta. Either 'post' or 'comment'. |
36
|
|
|
* @param array $config Must include 'meta_key' and 'ids' keys. |
37
|
|
|
* |
38
|
|
|
* @return array |
39
|
|
|
*/ |
40
|
|
|
public function get_objects_by_id( $object_type, $config ) { |
41
|
|
|
$table = _get_meta_table( $object_type ); |
42
|
|
|
|
43
|
|
|
if ( ! $table ) { |
44
|
|
|
return array(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if ( ! is_array( $config ) ) { |
48
|
|
|
return array(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$meta_objects = array(); |
52
|
|
|
foreach ( $config as $item ) { |
53
|
|
|
$meta = null; |
54
|
|
|
if ( isset( $item['id'] ) && isset( $item['meta_key'] ) ) { |
55
|
|
|
$meta = $this->get_object_by_id( $object_type, (int) $item['id'], (string) $item['meta_key'] ); |
56
|
|
|
} |
57
|
|
|
$meta_objects[ $item['id'] . '-' . $item['meta_key'] ] = $meta; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $meta_objects; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get a single Meta Result. |
65
|
|
|
* |
66
|
|
|
* @param string $object_type post, comment, term, user. |
67
|
|
|
* @param null $id Object ID. |
68
|
|
|
* @param null $meta_key Meta Key. |
69
|
|
|
* |
70
|
|
|
* @return mixed|null |
71
|
|
|
*/ |
72
|
|
|
public function get_object_by_id( $object_type, $id = null, $meta_key = null ) { |
73
|
|
|
global $wpdb; |
74
|
|
|
|
75
|
|
|
if ( ! is_int( $id ) || ! is_string( $meta_key ) ) { |
76
|
|
|
return null; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$table = _get_meta_table( $object_type ); |
80
|
|
|
$object_id_column = $object_type . '_id'; |
81
|
|
|
|
82
|
|
|
// Sanitize so that the array only has integer values. |
83
|
|
|
$meta = $wpdb->get_row( |
84
|
|
|
$wpdb->prepare( |
85
|
|
|
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
86
|
|
|
"SELECT * FROM {$table} WHERE {$object_id_column} = %d AND meta_key = %s", |
87
|
|
|
$id, |
88
|
|
|
$meta_key |
89
|
|
|
) |
90
|
|
|
); |
91
|
|
|
|
92
|
|
|
$meta_object = null; |
93
|
|
|
if ( ! is_null( $meta ) ) { |
94
|
|
|
$meta_object = (array) $meta; |
95
|
|
|
if ( 'post' === $object_type && strlen( $meta_object['meta_value'] ) >= Posts::MAX_POST_META_LENGTH ) { |
96
|
|
|
$meta_object['meta_value'] = ''; |
97
|
|
|
} |
98
|
|
|
$meta_object = array( |
99
|
|
|
'meta_type' => $object_type, |
100
|
|
|
'meta_id' => $meta_object['meta_id'], |
101
|
|
|
'meta_key' => $meta_key, |
102
|
|
|
'meta_value' => $meta_object['meta_value'], |
103
|
|
|
'object_id' => $meta_object[ $object_id_column ], |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $meta_object; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|