-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathghost-meta.php
More file actions
523 lines (412 loc) · 14.1 KB
/
Copy pathghost-meta.php
File metadata and controls
523 lines (412 loc) · 14.1 KB
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
<?php
/*
Plugin Name: Ghost Meta
Plugin URI: https://www.scottkclark.com/
Description: Ghost meta allows you to store multiple meta values in a single meta record, with an function API that mirrors the Metadata API.
Version: 1.0
Author: Scott Kingsley Clark
Author URI: https://www.scottkclark.com/
Text Domain: ghost-meta
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
if ( ! defined( 'ABSPATH' ) ) {
return;
}
// Load Ghost Meta
add_action( 'plugins_loaded', array( 'Ghost_Meta', 'get_instance' ) );
class Ghost_Meta {
/**
* Ghost Meta class instance
*
* @var Ghost_Meta
*/
protected static $instance;
/**
* Meta type for Ghost meta.
*
* @param string
*/
public $meta_type = 'ghost';
/**
* Meta key for Ghost meta storage.
*
* @param string
*/
public $meta_key = '_ghost_meta';
/**
* Setup instance if not initiated, then return class object.
*
* @return Ghost_Meta
*/
public static function get_instance() {
$class_name = get_called_class();
if ( ! static::$instance || ! is_a( static::$instance, $class_name ) ) {
static::$instance = new static;
}
return static::$instance;
}
/**
* Add hooks for Ghost meta.
*/
private function __construct() {
if ( ! has_filter( 'get_' . $this->meta_type . '_metadata', array( $this, 'get_metadata' ) ) ) {
add_filter( 'get_' . $this->meta_type . '_metadata', array( $this, 'get_metadata' ), 10, 4 );
}
if ( ! has_filter( 'add_' . $this->meta_type . '_metadata', array( $this, 'add_metadata' ) ) ) {
add_filter( 'add_' . $this->meta_type . '_metadata', array( $this, 'add_metadata' ), 10, 5 );
}
if ( ! has_filter( 'update_' . $this->meta_type . '_metadata', array( $this, 'update_metadata' ) ) ) {
add_filter( 'update_' . $this->meta_type . '_metadata', array( $this, 'update_metadata' ), 10, 5 );
}
if ( ! has_filter( 'delete_' . $this->meta_type . '_metadata', array( $this, 'delete_metadata' ) ) ) {
add_filter( 'delete_' . $this->meta_type . '_metadata', array( $this, 'delete_metadata' ), 10, 5 );
}
if ( ! has_filter( 'ep_post_sync_args', array( $this, 'ep_post_sync_args' ) ) ) {
add_filter( 'ep_post_sync_args', array( $this, 'ep_post_sync_args' ), 10, 2 );
}
/**
* @var $wpdb wpdb
*/
global $wpdb;
// Add temporary table property to wpdb because
// _get_meta_table isn't hookable and it's called too early
// in add_metadata / update_metadata / delete_metadata
$table_name = $this->meta_type . 'meta';
$wpdb->{$table_name} = $wpdb->prefix . $table_name;
include_once 'functions.php';
}
/**
* Filter get_metadata for Ghost Meta.
*
* @param null|array|string $value The value get_metadata() should return - a single metadata value,
* or an array of values.
* @param int $object_id Object ID.
* @param string $meta_key Meta key.
* @param bool $single Whether to return only the first value of the specified $meta_key.
*
* @return array|string
*/
public function get_metadata( $value, $object_id, $meta_key, $single ) {
$meta_info = $this->get_meta_info_from_key( $meta_key );
$meta_type = $meta_info['type'];
$meta_key = $meta_info['key'];
$meta = $this->get_ghost_meta( $meta_type, $object_id );
if ( ! $meta_key ) {
return $meta;
}
if ( isset( $meta[ $meta_key ] ) ) {
if ( $single ) {
$value = maybe_unserialize( $meta[ $meta_key ][0] );
} else {
$value = array_map( 'maybe_unserialize', $meta[ $meta_key ] );
}
} elseif ( $single ) {
$value = '';
} else {
$value = array();
}
return $value;
}
/**
* Filter add_metadata for Ghost Meta.
*
* @param null|bool $check Whether to allow adding metadata for the given type.
* @param int $object_id Object ID.
* @param string $meta_key Meta key.
* @param mixed $meta_value Meta value. Must be serializable if non-scalar.
* @param bool $unique Whether the specified meta key should be unique
* for the object. Optional. Default false.
*
* @return int|false The meta ID on success, false on failure.
*/
public function add_metadata( $check, $object_id, $meta_key, $meta_value, $unique = false ) {
$_meta_key = $meta_key;
$meta_info = $this->get_meta_info_from_key( $meta_key );
$meta_type = $meta_info['type'];
$meta_key = $meta_info['key'];
$meta = $this->get_ghost_meta( $meta_type, $object_id );
$_meta_value = $meta_value;
$meta_value = maybe_serialize( $meta_value );
if ( ! isset( $meta[ $meta_key ] ) ) {
$meta[ $meta_key ] = array();
}
$meta[ $meta_key ][] = $meta_value;
$check = $this->update_ghost_meta( $meta_type, $object_id, $meta );
if ( false !== $check ) {
$check = 1;
}
$mid = 0;
/**
* Fires immediately after meta of a specific type is added.
*
* @since 1.0
*
* @param int $mid The meta ID after successful update.
* @param int $object_id Object ID.
* @param string $meta_key Meta key.
* @param mixed $meta_value Meta value.
*/
do_action( 'added_' . $this->meta_type . '_meta', $mid, $object_id, $_meta_key, $_meta_value );
return $check;
}
/**
* Filter update_metadata for Ghost Meta.
*
* @param null|bool $check Whether to allow updating metadata for the given type.
* @param int $object_id Object ID.
* @param string $meta_key Meta key.
* @param mixed $meta_value Meta value. Must be serializable if non-scalar.
* @param mixed $prev_value Optional. If specified, only update existing
* metadata entries with the specified value.
* Otherwise, update all entries.
*
* @return bool True on successful update, false on failure.
*/
public function update_metadata( $check, $object_id, $meta_key, $meta_value, $prev_value ) {
$_meta_key = $meta_key;
$meta_info = $this->get_meta_info_from_key( $meta_key );
$meta_type = $meta_info['type'];
$meta_key = $meta_info['key'];
$meta = $this->get_ghost_meta( $meta_type, $object_id );
$_meta_value = $meta_value;
$meta_value = maybe_serialize( $meta_value );
if ( ! isset( $meta[ $meta_key ] ) ) {
$meta[ $meta_key ] = array();
}
$changed = false;
if ( $prev_value ) {
$prev_value = maybe_serialize( $prev_value );
// Update meta values if they match $prev_value
foreach ( $meta[ $meta_key ] as $prev_meta_k => $prev_meta_v ) {
if ( $prev_value == $prev_meta_v ) {
if ( is_bool( $prev_meta_v ) && (boolean) $prev_value === $prev_meta_v ) {
// Strict matching for booleans
$meta[ $meta_key ][ $prev_meta_k ] = $meta_value;
$changed = true;
} elseif ( is_int( $prev_meta_v ) && (int) $prev_value === $prev_meta_v ) {
// Strict matching for integers
$meta[ $meta_key ][ $prev_meta_k ] = $meta_value;
$changed = true;
} else {
// Loose matching like MySQL would do by default
$meta[ $meta_key ][ $prev_meta_k ] = $meta_value;
$changed = true;
}
}
}
} elseif ( 1 !== count( $meta[ $meta_key ] ) || $meta_value !== $meta[ $meta_key ][0] ) {
// Handle changes only if the value does not match
$changed = true;
// Update default is to remove all meta values and set as one single value
$meta[ $meta_key ] = array(
$meta_value
);
}
$check = false;
if ( $changed ) {
$check = $this->update_ghost_meta( $meta_type, $object_id, $meta );
if ( false !== $check ) {
$check = 1;
}
}
$meta_id = 0;
/**
* Fires immediately after updating metadata of a specific type.
*
* @since 1.0
*
* @param int $meta_id ID of updated metadata entry. (not currently used)
* @param int $object_id Object ID.
* @param string $meta_key Meta key.
* @param mixed $meta_value Meta value.
*/
do_action( 'updated_' . $this->meta_type . '_meta', $meta_id, $object_id, $_meta_key, $_meta_value );
return $check;
}
/**
* Filter delete_metadata for Ghost Meta.
*
* @param null|bool $delete Whether to allow metadata deletion of the given type.
* @param int $object_id Object ID.
* @param string $meta_key Meta key.
* @param mixed $meta_value Meta value. Must be serializable if non-scalar.
* @param bool $delete_all Whether to delete the matching metadata entries
* for all objects, ignoring the specified $object_id.
* Default false.
*
* @return bool True on successful delete, false on failure.
*/
public function delete_metadata( $delete, $object_id, $meta_key, $meta_value, $delete_all ) {
$_meta_key = $meta_key;
$meta_info = $this->get_meta_info_from_key( $meta_key );
$meta_type = $meta_info['type'];
$meta_key = $meta_info['key'];
$meta = $this->get_ghost_meta( $meta_type, $object_id );
$delete = false;
$changed = false;
$_meta_value = $meta_value;
$meta_ids = array();
if ( $delete_all ) {
// Handle deleting all values in ghost meta for this key
/**
* @var $wpdb wpdb
*/
global $wpdb;
$table = _get_meta_table( $meta_type );
$type_column = sanitize_key( $meta_type . '_id' );
if ( ! $table ) {
return false;
}
// Build like value so we only get ghost meta for objects that have something that matches
$like_value = sprintf(
'%%s:%d:"%s"%%', // Example: %s:7:"example"%
mb_strlen( $meta_key ),
$wpdb->esc_like( $meta_key )
);
// Get all objects in this meta
$object_ids_sql = $wpdb->prepare(
"
SELECT {$type_column}
FROM {$table}
WHERE
meta_key = %s
AND meta_value LIKE %s
",
$this->meta_key,
$like_value
);
$object_ids = $wpdb->get_col( $object_ids_sql );
foreach ( $object_ids as $o_id ) {
$check_deleted = $this->delete_metadata( null, $o_id, $_meta_key, $meta_value, false );
if ( $check_deleted ) {
$delete = 1;
}
}
} elseif ( isset( $meta[ $meta_key ] ) ) {
if ( '' !== $meta_value && null !== $meta_value && false !== $meta_value ) {
// Handle deleting only values that match
$meta_value = maybe_serialize( $meta_value );
// Update meta values if they match $prev_value
foreach ( $meta[ $meta_key ] as $prev_meta_k => $prev_meta_v ) {
if ( $meta_value == $prev_meta_v ) {
if ( is_bool( $prev_meta_v ) && (boolean) $meta_value === $prev_meta_v ) {
// Strict matching for booleans
unset( $meta[ $meta_key ][ $prev_meta_k ] );
$changed = true;
} elseif ( is_int( $prev_meta_v ) && (int) $meta_value === $prev_meta_v ) {
// Strict matching for integers
unset( $meta[ $meta_key ][ $prev_meta_k ] );
$changed = true;
} else {
// Loose matching like MySQL would do by default
unset( $meta[ $meta_key ][ $prev_meta_k ] );
$changed = true;
}
}
}
// Rekey the values
if ( $changed ) {
$meta[ $meta_key ] = array_values( $meta[ $meta_key ] );
}
} else {
// Remove the whole array of data
unset( $meta[ $meta_key ] );
$changed = true;
}
}
if ( $changed ) {
$delete = $this->update_ghost_meta( $meta_type, $object_id, $meta );
if ( false !== $delete ) {
$delete = 1;
}
}
/**
* Fires immediately after deleting Ghost metadata.
*
* @since 1.0
*
* @param array $meta_ids An array of deleted metadata entry IDs. (not currently used)
* @param int $object_id Object ID.
* @param string $meta_key Meta key.
* @param mixed $meta_value Meta value.
*/
do_action( 'deleted_' . $this->meta_type . '_meta', $meta_ids, $object_id, $_meta_key, $_meta_value );
return $delete;
}
/**
* Hook into ElasticPress post arguments to add our ghost meta, just as if it were normal meta.
*
* @param array $post_args Post arguments to send to Elasticsearch.
* @param int $post_id Post ID.
*
* @return array Post arguments to send to Elasticsearch.
*/
public function ep_post_sync_args( $post_args, $post_id ) {
$meta = $this->get_ghost_meta( 'post', $post_id );
if ( ! empty( $meta ) ) {
// Merge ghost meta values
$post_args['post_meta'] = array_merge( $post_args['post_meta'], $meta );
}
return $post_args;
}
/**
* Get Ghost Meta from the object.
*
* @param string $meta_type Meta type.
* @param int $object_id Object ID.
*
* @return array Ghost meta
*/
protected function get_ghost_meta( $meta_type, $object_id ) {
$meta = get_metadata( $meta_type, $object_id, $this->meta_key, true );
if ( empty( $meta ) || ! is_array( $meta ) ) {
$meta = array();
}
return $meta;
}
/**
* Update Ghost Meta for the object.
*
* @param string $meta_type Meta type.
* @param int $object_id Object ID.
* @param array $meta Ghost meta.
*
* @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure.
*/
protected function update_ghost_meta( $meta_type, $object_id, $meta ) {
return update_metadata( $meta_type, $object_id, $this->meta_key, $meta );
}
/**
* Get meta info from a meta key.
*
* Examples: some_meta_value, post.some_meta_value, user.some_meta_value
*
* @param string $meta_key Meta key
*
* @return array Meta info
*/
protected function get_meta_info_from_key( $meta_key ) {
$meta_info = array(
'type' => 'post',
'key' => $meta_key,
);
if ( false !== strpos( $meta_key, '.' ) ) {
$meta_key_data = explode( '.', $meta_key );
if ( 2 === count( $meta_key_data ) ) {
$meta_info['type'] = $meta_key_data[0];
$meta_info['key'] = $meta_key_data[1];
}
}
return $meta_info;
}
}