1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
|
<?php /** * CustomMetaDataStore class file. */
namespace Automattic\WooCommerce\Internal\DataStores;
/** * Implements functions similar to WP's add_metadata(), get_metadata(), and friends using a custom table. * * @see WC_Data_Store_WP For an implementation using WP's metadata functions and tables. */ abstract class CustomMetaDataStore {
/** * Returns the name of the table used for storage. * * @return string */ abstract protected function get_table_name();
/** * Returns the name of the field/column used for identifiying metadata entries. * * @return string */ protected function get_meta_id_field() { return 'id'; }
/** * Returns the name of the field/column used for associating meta with objects. * * @return string */ protected function get_object_id_field() { return 'object_id'; }
/** * Describes the structure of the metadata table. * * @return array Array elements: table, object_id_field, meta_id_field. */ protected function get_db_info() { return array( 'table' => $this->get_table_name(), 'meta_id_field' => $this->get_meta_id_field(), 'object_id_field' => $this->get_object_id_field(), ); }
/** * Returns an array of meta for an object. * * @param \WC_Data $object WC_Data object. * @return array */ public function read_meta( &$object ) { $object_id = $object->get_id(); $raw_meta_data = $this->get_meta_data_for_object_ids( array( $object_id ) );
return isset( $raw_meta_data[ $object_id ] ) ? (array) $raw_meta_data[ $object_id ] : array(); }
/** * Deletes meta based on meta ID. * * @param \WC_Data $object WC_Data object. * @param \stdClass $meta (containing at least ->id). * * @return bool */ public function delete_meta( &$object, $meta ) : bool { global $wpdb;
if ( ! isset( $meta->id ) ) { return false; }
$db_info = $this->get_db_info(); $meta_id = absint( $meta->id );
return (bool) $wpdb->delete( $db_info['table'], array( $db_info['meta_id_field'] => $meta_id, $db_info['object_id_field'] => $object->get_id(), ), '%d' ); }
/** * Add new piece of meta. * * @param WC_Data $object WC_Data object. * @param stdClass $meta (containing ->key and ->value). * * @return int|false meta ID */ public function add_meta( &$object, $meta ) { global $wpdb;
$db_info = $this->get_db_info();
$object_id = $object->get_id(); $meta_key = wp_unslash( wp_slash( $meta->key ) ); $meta_value = maybe_serialize( is_string( $meta->value ) ? wp_unslash( wp_slash( $meta->value ) ) : $meta->value );
// phpcs:disable WordPress.DB.SlowDBQuery.slow_db_query_meta_value,WordPress.DB.SlowDBQuery.slow_db_query_meta_key $result = $wpdb->insert( $db_info['table'], array( $db_info['object_id_field'] => $object_id, 'meta_key' => $meta_key, 'meta_value' => $meta_value, ) ); // phpcs:enable WordPress.DB.SlowDBQuery.slow_db_query_meta_value,WordPress.DB.SlowDBQuery.slow_db_query_meta_key
return $result ? (int) $wpdb->insert_id : false; }
/** * Update meta. * * @param \WC_Data $object WC_Data object. * @param \stdClass $meta (containing ->id, ->key and ->value). * * @return bool */ public function update_meta( &$object, $meta ) : bool { global $wpdb;
if ( ! isset( $meta->id ) || empty( $meta->key ) ) { return false; }
// phpcs:disable WordPress.DB.SlowDBQuery.slow_db_query_meta_value,WordPress.DB.SlowDBQuery.slow_db_query_meta_key $data = array( 'meta_key' => $meta->key, 'meta_value' => maybe_serialize( $meta->value ), ); // phpcs:enable WordPress.DB.SlowDBQuery.slow_db_query_meta_value,WordPress.DB.SlowDBQuery.slow_db_query_meta_key
$result = $wpdb->update( $this->get_table_name(), $data, array( $this->get_meta_id_field() => $meta->id, $this->get_object_id_field() => $object->get_id(), ), '%s', '%d' );
return 1 === $result; }
/** * Retrieves metadata by meta ID. * * @param int $meta_id Meta ID. * @return object|bool Metadata object or FALSE if not found. */ public function get_metadata_by_id( $meta_id ) { global $wpdb;
if ( ! is_numeric( $meta_id ) || floor( $meta_id ) != $meta_id ) { // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison return false; }
$db_info = $this->get_db_info();
$meta_id = absint( $meta_id ); // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared $meta = $wpdb->get_row( $wpdb->prepare( "SELECT {$db_info['meta_id_field']}, meta_key, meta_value, {$db_info['object_id_field']} FROM {$db_info['table']} WHERE {$db_info['meta_id_field']} = %d", $meta_id ) ); // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
if ( empty( $meta ) ) { return false; }
if ( isset( $meta->meta_value ) ) { $meta->meta_value = maybe_unserialize( $meta->meta_value ); }
return $meta; }
/** * Retrieves metadata by meta key. * * @param \WC_Abstract_Order $object Object ID. * @param string $meta_key Meta key. * * @return \stdClass|bool Metadata object or FALSE if not found. */ public function get_metadata_by_key( &$object, string $meta_key ) { global $wpdb;
$db_info = $this->get_db_info();
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared $meta = $wpdb->get_results( $wpdb->prepare( "SELECT {$db_info['meta_id_field']}, meta_key, meta_value, {$db_info['object_id_field']} FROM {$db_info['table']} WHERE meta_key = %s AND {$db_info['object_id_field']} = %d", $meta_key, $object->get_id(), ) ); // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
if ( empty( $meta ) ) { return false; }
foreach ( $meta as $row ) { if ( isset( $row->meta_value ) ) { $row->meta_value = maybe_unserialize( $row->meta_value ); } }
return $meta; }
/** * Returns distinct meta keys in use. * * @since 8.8.0 * * @param int $limit Maximum number of meta keys to return. Defaults to 100. * @param string $order Order to use for the results. Either 'ASC' or 'DESC'. Defaults to 'ASC'. * @param bool $include_private Whether to include private meta keys in the results. Defaults to FALSE. * @return string[] */ public function get_meta_keys( $limit = 100, $order = 'ASC', $include_private = false ) { global $wpdb;
$db_info = $this->get_db_info();
$query = "SELECT DISTINCT meta_key FROM {$db_info['table']} ";
if ( ! $include_private ) { $query .= $wpdb->prepare( "WHERE meta_key !='' AND meta_key NOT BETWEEN '_' AND '_z' AND meta_key NOT LIKE %s ", $wpdb->esc_like( '_' ) . '%' ); } else { $query .= "WHERE meta_key != '' "; }
$order = in_array( strtoupper( $order ), array( 'ASC', 'DESC' ), true ) ? $order : 'ASC'; $query .= 'ORDER BY meta_key ' . $order . ' ';
if ( $limit ) { $query .= $wpdb->prepare( 'LIMIT %d ', $limit ); }
return $wpdb->get_col( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- $query is prepared. }
/** * Return order meta data for multiple IDs. * * @param array $object_ids List of object IDs. * * @return \stdClass[][] An array, keyed by object_ids, containing array of raw meta data records for each object. Objects with no meta data will have an empty array. */ public function get_meta_data_for_object_ids( array $object_ids ): array { global $wpdb;
if ( empty( $object_ids ) ) { return array(); }
$id_placeholder = implode( ', ', array_fill( 0, count( $object_ids ), '%d' ) ); $meta_table = $this->get_table_name(); $object_id_column = $this->get_object_id_field();
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- $object_id_column and $meta_table is hardcoded. IDs are prepared above. $meta_rows = $wpdb->get_results( $wpdb->prepare( "SELECT id, $object_id_column as object_id, meta_key, meta_value FROM $meta_table WHERE $object_id_column in ( $id_placeholder )", $object_ids ) ); // phpcs:enable
$meta_data = array_fill_keys( $object_ids, array() ); foreach ( $meta_rows as $meta_row ) { if ( ! isset( $meta_data[ $meta_row->object_id ] ) ) { $meta_data[ $meta_row->object_id ] = array(); } $meta_data[ $meta_row->object_id ][] = (object) array( 'meta_id' => $meta_row->id, 'meta_key' => $meta_row->meta_key, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key 'meta_value' => $meta_row->meta_value, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value ); }
return $meta_data; }
}
|