-
Notifications
You must be signed in to change notification settings - Fork 0
/
WPMetaOptimizer.php
572 lines (495 loc) · 23.4 KB
/
WPMetaOptimizer.php
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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
<?php
/*!
* Plugin Name: Meta Optimizer
* Version: 1.4
* Plugin URI: https://parsakafi.github.io/wp-meta-optimizer
* Description: You can use Meta Optimizer to make your WordPress website load faster if you use metadata. For example, Post/Comment/User/Term metas.
* Author: Parsa Kafi
* Author URI: https://parsa.ws
* Text Domain: meta-optimizer
*/
namespace WPMetaOptimizer;
// Check run from WP
defined( 'ABSPATH' ) || die();
require_once __DIR__ . '/inc/Base.php';
define( 'WPMETAOPTIMIZER_PLUGIN_KEY', 'wp-meta-optimizer' );
define( 'WPMETAOPTIMIZER_OPTION_KEY', 'wp_meta_optimizer' );
define( 'WPMETAOPTIMIZER_PLUGIN_NAME', 'Meta Optimizer' );
define( 'WPMETAOPTIMIZER_PLUGIN_FILE_PATH', __FILE__ );
define( 'WPMETAOPTIMIZER_CACHE_EXPIRE', DAY_IN_SECONDS );
define( 'WPMETAOPTIMIZER_DEFAULT_IMPORT_NUMBER', 5 );
/**
* Main class run Meta Optimizer plugin
*/
class WPMetaOptimizer extends Base {
public static $instance = null;
protected $Helpers, $Options;
function __construct() {
parent::__construct();
$this->include();
$this->Helpers = Helpers::getInstance();
$this->Options = Options::getInstance();
Actions::getInstance();
Queries::getInstance();
Integration::getInstance();
Tools::getInstance();
$actionPriority = 99999999;
$types = array_keys( $this->Options->getOption( 'meta_save_types', [] ) );
foreach ( $types as $type ) {
if ( $type == 'hidden' )
continue;
add_filter( 'get_' . $type . '_metadata', [ $this, 'getMeta' ], $actionPriority, 5 );
add_filter( 'add_' . $type . '_metadata', [
$this,
'add' . ucwords( $type ) . 'Meta'
], $actionPriority, 5 );
add_filter( 'update_' . $type . '_metadata', [
$this,
'update' . ucwords( $type ) . 'Meta'
], $actionPriority, 5 );
add_filter( 'delete_' . $type . '_metadata', [
$this,
'delete' . ucwords( $type ) . 'Meta'
], $actionPriority, 5 );
}
}
/**
* Adds a meta field to the given post.
*
* @param null|bool $check Whether to allow adding metadata for the given type.
* @param int $objectID ID of the object metadata is for.
* @param string $metaKey Metadata key.
* @param mixed $metaValue Metadata value. Must be serializable if non-scalar.
* @param bool $unique Whether the specified meta key should be unique for the object.
*
* @return int|false The meta ID on success, false on failure.
*/
function addPostMeta( $check, $objectID, $metaKey, $metaValue, $unique ) {
return $this->addMeta( 'post', $check, $objectID, $metaKey, $metaValue, $unique );
}
/**
* Updates a post-meta field based on the given post ID.
*
* @param null|bool $check Whether to allow updating metadata for the given type.
* @param int $objectID ID of the object metadata is for.
* @param string $metaKey Metadata key.
* @param mixed $metaValue Metadata value. Must be serializable if non-scalar.
* @param mixed $prevValue Optional. Previous value to check before updating.
* If specified, only update existing metadata entries with
* this value. Otherwise, update all entries.
*
* @return int|bool The new meta field ID if a field with the given key didn't exist
* and was therefore added, true on successful update,
* false on failure or if the value passed to the function
* is the same as the one that is already in the database.
*/
function updatePostMeta( $check, $objectID, $metaKey, $metaValue, $prevValue ) {
return $this->updateMeta( 'post', $check, $objectID, $metaKey, $metaValue, $prevValue );
}
/**
* Removes metadata matching criteria from a post.
* Fires after WordPress meta removed
*
* @param null|bool $delete Whether to allow metadata deletion of the given type.
* @param int $objectID ID of the object metadata is for.
* @param string $metaKey Metadata key.
* @param mixed $metaValue Metadata value.
* @param bool $deleteAll Whether to delete the matching metadata entries
* for all objects, ignoring the specified $object_id.
* Default false.
*
* @return null|bool return $delete value
*/
function deletePostMeta( $delete, $objectID, $metaKey, $metaValue, $deleteAll ) {
$this->deleteMeta( 'post', $objectID, $metaKey, $metaValue, $deleteAll );
return $delete;
}
/**
* Adds a meta field to the given comment.
*
* @param null|bool $check Whether to allow adding metadata for the given type.
* @param int $objectID ID of the object metadata is for.
* @param string $metaKey Metadata key.
* @param mixed $metaValue Metadata value. Must be serializable if non-scalar.
* @param bool $unique Whether the specified meta key should be unique for the object.
*
* @return int|false The meta ID on success, false on failure.
*/
function addCommentMeta( $check, $objectID, $metaKey, $metaValue, $unique ) {
return $this->addMeta( 'comment', $check, $objectID, $metaKey, $metaValue, $unique );
}
/**
* Updates a comment meta field based on the given post ID.
*
* @param null|bool $check Whether to allow updating metadata for the given type.
* @param int $objectID ID of the object metadata is for.
* @param string $metaKey Metadata key.
* @param mixed $metaValue Metadata value. Must be serializable if non-scalar.
* @param mixed $prevValue Optional. Previous value to check before updating.
* If specified, only update existing metadata entries with
* this value. Otherwise, update all entries.
*
* @return int|bool The new meta field ID if a field with the given key didn't exist
* and was therefore added, true on successful update,
* false on failure or if the value passed to the function
* is the same as the one that is already in the database.
*/
function updateCommentMeta( $check, $objectID, $metaKey, $metaValue, $prevValue ) {
return $this->updateMeta( 'comment', $check, $objectID, $metaKey, $metaValue, $prevValue );
}
/**
* Removes metadata matching criteria from a comment.
* Fires after WordPress meta removed
*
* @param null|bool $delete Whether to allow metadata deletion of the given type.
* @param int $objectID ID of the object metadata is for.
* @param string $metaKey Metadata key.
* @param mixed $metaValue Metadata value.
* @param bool $deleteAll Whether to delete the matching metadata entries
* for all objects, ignoring the specified $object_id.
* Default false.
*
* @return null|bool return $delete value
*/
function deleteCommentMeta( $delete, $objectID, $metaKey, $metaValue, $deleteAll ) {
$this->deleteMeta( 'comment', $objectID, $metaKey, $metaValue, $deleteAll );
return $delete;
}
/**
* Adds a meta field to the given term.
*
* @param null|bool $check Whether to allow adding metadata for the given type.
* @param int $objectID ID of the object metadata is for.
* @param string $metaKey Metadata key.
* @param mixed $metaValue Metadata value. Must be serializable if non-scalar.
* @param bool $unique Whether the specified meta key should be unique for the object.
*
* @return int|false The meta ID on success, false on failure.
*/
function addTermMeta( $check, $objectID, $metaKey, $metaValue, $unique ) {
return $this->addMeta( 'term', $check, $objectID, $metaKey, $metaValue, $unique );
}
/**
* Updates a term meta field based on the given term ID.
*
* @param null|bool $check Whether to allow updating metadata for the given type.
* @param int $objectID ID of the object metadata is for.
* @param string $metaKey Metadata key.
* @param mixed $metaValue Metadata value. Must be serializable if non-scalar.
* @param mixed $prevValue Optional. Previous value to check before updating.
* If specified, only update existing metadata entries with
* this value. Otherwise, update all entries.
*
* @return int|bool The new meta field ID if a field with the given key didn't exist
* and was therefore added, true on successful update,
* false on failure or if the value passed to the function
* is the same as the one that is already in the database.
*/
function updateTermMeta( $check, $objectID, $metaKey, $metaValue, $prevValue ) {
return $this->updateMeta( 'term', $check, $objectID, $metaKey, $metaValue, $prevValue );
}
/**
* Removes metadata matching criteria from a term.
* Fires after WordPress meta removed
*
* @param null|bool $delete Whether to allow metadata deletion of the given type.
* @param int $objectID ID of the object metadata is for.
* @param string $metaKey Metadata key.
* @param mixed $metaValue Metadata value.
* @param bool $deleteAll Whether to delete the matching metadata entries
* for all objects, ignore the specified $object_id.
* Default false.
*
* @return null|bool return $delete value
*/
function deleteTermMeta( $delete, $objectID, $metaKey, $metaValue, $deleteAll ) {
$this->deleteMeta( 'term', $objectID, $metaKey, $metaValue, $deleteAll );
return $delete;
}
/**
* Adds a meta field to the given user.
*
* @param null|bool $check Whether to allow adding metadata for the given type.
* @param int $objectID ID of the object metadata is for.
* @param string $metaKey Metadata key.
* @param mixed $metaValue Metadata value. Must be serializable if non-scalar.
* @param bool $unique Whether the specified meta key should be unique for the object.
*
* @return int|false The meta ID on success, false on failure.
*/
function addUserMeta( $check, $objectID, $metaKey, $metaValue, $unique ) {
return $this->addMeta( 'user', $check, $objectID, $metaKey, $metaValue, $unique );
}
/**
* Updates a user meta field based on the given user ID.
*
* @param null|bool $check Whether to allow updating metadata for the given type.
* @param int $objectID ID of the object metadata is for.
* @param string $metaKey Metadata key.
* @param mixed $metaValue Metadata value. Must be serializable if non-scalar.
* @param mixed $prevValue Optional. Previous value to check before updating.
* If specified, only update existing metadata entries with
* this value. Otherwise, update all entries.
*
* @return int|bool The new meta field ID if a field with the given key didn't exist
* and was therefore added, true on successful update,
* false on failure or if the value passed to the function
* is the same as the one that is already in the database.
*/
function updateUserMeta( $check, $objectID, $metaKey, $metaValue, $prevValue ) {
return $this->updateMeta( 'user', $check, $objectID, $metaKey, $metaValue, $prevValue );
}
/**
* Removes metadata matching criteria from a user.
* Fires after WordPress meta removed
*
* @param null|bool $delete Whether to allow metadata deletion of the given type.
* @param int $objectID ID of the object metadata is for.
* @param string $metaKey Metadata key.
* @param mixed $metaValue Metadata value.
* @param bool $deleteAll Whether to delete the matching metadata entries
* for all objects, ignore the specified $object_id.
* Default false.
*
* @return null|bool return $delete value
*/
function deleteUserMeta( $delete, $objectID, $metaKey, $metaValue, $deleteAll ) {
$this->deleteMeta( 'user', $objectID, $metaKey, $metaValue, $deleteAll );
return $delete;
}
/**
* Retrieves raw metadata value for the specified object.
*
* @param mixed $value The value to return, either a single metadata value or an array
* of values depending on the value of `$single`. Default null.
* @param int $objectID ID of the object metadata is for.
* @param string $metaKey Metadata key.
* @param bool $single Whether to return only the first value of the specified `$metaKey`.
* @param string $metaType Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
* or any other object type with an associated meta table.
*
* @return mixed An array of values if `$single` is false.
* The value of the meta field if `$single` is true.
* False for an invalid `$objectID` (non-numeric, zero, or negative value),
* or if `$metaType` is not specified.
* Null if the value does not exist.
*/
function getMetaRaw( $value, $objectID, $metaKey, $metaType ) {
global $wpdb;
//if ($metaKey === '')
// return $value;
if ( defined( 'IMPORT_PROCESS_WPMO' ) )
return $value;
$tableName = $this->Helpers->getMetaTableName( $metaType );
if ( ! $tableName )
return $value;
if ( ! $this->Helpers->checkMetaType( $metaType ) )
return $value;
if ( $metaType === 'post' && ! $this->Helpers->checkPostType( $objectID ) )
return $value;
if ( $this->Helpers->checkInBlackWhiteList( $metaType, $metaKey, 'black_list' ) === true || $this->Helpers->checkInBlackWhiteList( $metaType, $metaKey, 'white_list' ) === false )
return $value;
if ( substr( $metaKey, - ( strlen( $this->reservedKeysSuffix ) ) ) !== $this->reservedKeysSuffix )
$metaKey = $this->Helpers->translateColumnName( $metaType, $metaKey );
if ( ! $this->Helpers->checkColumnExists( $tableName, $metaType, $metaKey ) )
return $value;
$debugBacktrace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS & DEBUG_BACKTRACE_PROVIDE_OBJECT, 5 );
if ( isset( $debugBacktrace[3] ) && isset( $debugBacktrace[4] ) && $debugBacktrace[3]['function'] == 'get_metadata_raw' && $debugBacktrace[4]['function'] == 'update_metadata' )
return $value;
$metaRow = wp_cache_get( $tableName . '_' . $metaType . '_' . $objectID . '_row', WPMETAOPTIMIZER_PLUGIN_KEY );
if ( $metaRow === false ) {
$tableColumns = $this->Helpers->getTableColumns( $tableName, $metaType, true );
$tableColumns = '`' . implode( '`,`', $tableColumns ) . '`';
$sql = "SELECT {$tableColumns} FROM `{$tableName}` WHERE {$metaType}_id = {$objectID}";
$metaRow = $wpdb->get_row( $sql, ARRAY_A );
wp_cache_set( $tableName . '_' . $metaType . '_' . $objectID . '_row', $metaRow, WPMETAOPTIMIZER_PLUGIN_KEY, WPMETAOPTIMIZER_CACHE_EXPIRE );
}
if ( is_array( $metaRow ) && isset( $metaRow[ $metaKey ] ) ) {
return maybe_unserialize( $metaRow[ $metaKey ] );
}
return $value;
}
/**
* Retrieves raw metadata value for the specified object.
*
* @param mixed $value The value to return, either a single metadata value or an array
* of values depending on the value of `$single`. Default null.
* @param int $objectID ID of the object metadata is for.
* @param string $metaKey Metadata key.
* @param bool $single Whether to return only the first value of the specified `$metaKey`.
* @param string $metaType Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
* or any other object type with an associated meta table.
*
* @return mixed An array of values if `$single` is false.
* The value of the meta field if `$single` is true.
* False for an invalid `$objectID` (non-numeric, zero, or negative value),
* or if `$metaType` is not specified.
* Null if the value does not exist.
*/
function getMeta( $value, $objectID, $metaKey, $single, $metaType ) {
$metaValue = $this->getMetaRaw( $value, $objectID, $metaKey, $metaType );
if ( ! is_null( $metaValue ) ) {
$metaValue = maybe_unserialize( $metaValue );
if ( is_array( $metaValue ) && isset( $metaValue['wpmoai0'] ) )
$metaValue = array_values( $metaValue );
if ( $single && is_array( $metaValue ) && isset( $metaValue[0] ) )
return $metaValue;
elseif ( $single && is_array( $metaValue ) )
return array( $metaValue );
else
return $metaValue;
}
return $value;
}
/**
* Adds metadata for the specified object.
*
* @param string $metaType Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
* or any other object type with an associated meta table.
* @param null|bool $check Whether to allow adding metadata for the given type.
* @param int $objectID ID of the object metadata is for.
* @param string $metaKey Metadata key.
* @param mixed $metaValue Metadata value. Must be serializable if non-scalar.
* @param bool $unique Whether the specified meta key should be unique for the object.
*
* @return int|false The meta ID on success, false on failure.
*/
function addMeta( $metaType, $check, $objectID, $metaKey, $metaValue, $unique ) {
if ( ! $this->Helpers->checkMetaType( $metaType ) )
return $check;
if ( $metaType === 'post' && ! $this->Helpers->checkPostType( $objectID ) )
return $check;
if ( $this->Helpers->checkInBlackWhiteList( $metaType, $metaKey, 'black_list' ) === true || $this->Helpers->checkInBlackWhiteList( $metaType, $metaKey, 'white_list' ) === false )
return $check;
$metaKey = $this->Helpers->translateColumnName( $metaType, $metaKey );
$metaValue = maybe_unserialize( $metaValue );
$result = $this->Helpers->insertMeta(
[
'metaType' => $metaType,
'objectID' => $objectID,
'metaKey' => $metaKey,
'metaValue' => $metaValue,
'unique' => $unique,
'addMeta' => true
]
);
$tableName = $this->Helpers->getMetaTableName( $metaType );
wp_cache_delete( $tableName . '_' . $metaType . '_' . $objectID . '_row', WPMETAOPTIMIZER_PLUGIN_KEY );
return $this->Helpers->checkDontSaveInDefaultTable( $metaType ) ? $result : $check;
}
/**
* Updates metadata for the specified object. If no value already exists for the specified object
* ID and metadata key, the metadata will be added.
*
* @param string $metaType Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
* or any other object type with an associated meta table.
* @param null|bool $check Whether to allow updating metadata for the given type.
* @param int $objectID ID of the object metadata is for.
* @param string $metaKey Metadata key.
* @param mixed $metaValue Metadata value. Must be serializable if non-scalar.
* @param mixed $prevValue Optional. Previous value to check before updating.
* If specified, only update existing metadata entries with
* this value. Otherwise, update all entries. Default empty.
*
* @return int|bool The new meta field ID if a field with the given key didn't exist
* and was therefore added, true on successful update,
* false on failure or if the value passed to the function
* is the same as the one that is already in the database.
* @global \wpdb $wpdb WordPress database abstraction object.
*
*/
function updateMeta( $metaType, $check, $objectID, $metaKey, $metaValue, $prevValue ) {
if ( ! $this->Helpers->checkMetaType( $metaType ) )
return $check;
if ( $metaType === 'post' && ! $this->Helpers->checkPostType( $objectID ) )
return $check;
if ( $this->Helpers->checkInBlackWhiteList( $metaType, $metaKey, 'black_list' ) === true || $this->Helpers->checkInBlackWhiteList( $metaType, $metaKey, 'white_list' ) === false )
return $check;
$metaKey = $this->Helpers->translateColumnName( $metaType, $metaKey );
$metaValue = maybe_unserialize( $metaValue );
$result = $this->Helpers->insertMeta(
[
'metaType' => $metaType,
'objectID' => $objectID,
'metaKey' => $metaKey,
'metaValue' => $metaValue,
'unique' => false,
'prevValue' => $prevValue
]
);
$tableName = $this->Helpers->getMetaTableName( $metaType );
wp_cache_delete( $tableName . '_' . $metaType . '_' . $objectID . '_row', WPMETAOPTIMIZER_PLUGIN_KEY );
return $this->Helpers->checkDontSaveInDefaultTable( $metaType ) ? $result : $check;
}
/**
* Deletes metadata for the specified object.
*
* @param string $metaType Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
* or any other object type with an associated meta table.
* @param int $objectID ID of the object metadata is for.
* @param string $metaKey Metadata key.
* @param mixed $metaValue Metadata value.
* @param bool $deleteAll Whether to delete the matching metadata entries
* for all objects, ignore the specified $objectID.
*
* @return boolean|int
*/
private function deleteMeta( $metaType, $objectID, $metaKey, $metaValue, $deleteAll ) {
global $wpdb;
$tableName = $this->Helpers->getMetaTableName( $metaType );
if ( ! $tableName )
return false;
$column = sanitize_key( $metaType . '_id' );
$metaKey = $this->Helpers->translateColumnName( $metaType, $metaKey );
$newValue = null;
if ( ! $deleteAll && '' !== $metaValue && null !== $metaValue && false !== $metaValue ) {
$metaValue = maybe_unserialize( $metaValue );
$metaRawValue = $this->getMetaRaw( $newValue, $objectID, $metaKey, $metaType );
$newValue = $currentValue = $this->getMeta( $newValue, $objectID, $metaKey, false, $metaType );
if ( is_array( $currentValue ) && ( $indexValue = array_search( $metaValue, $currentValue, false ) ) !== false ) {
unset( $currentValue[ $indexValue ] );
$newValue = $currentValue;
if ( is_array( $metaRawValue ) && isset( $metaRawValue['wpmoai0'] ) )
$newValue = array_values( $newValue );
} elseif ( is_array( $currentValue ) )
return false;
$newValue = $this->Helpers->reIndexMetaValue( $newValue );
$newValue = maybe_serialize( $newValue );
}
$result = $wpdb->update(
$tableName,
[ $metaKey => $newValue, 'updated_at' => $this->now ],
[ $column => $objectID ]
);
wp_cache_delete( $tableName . '_' . $metaType . '_' . $objectID . '_row', WPMETAOPTIMIZER_PLUGIN_KEY );
return $result;
}
private function include() {
require_once __DIR__ . '/inc/Install.php';
require_once __DIR__ . '/inc/DBIndexes.php';
require_once __DIR__ . '/inc/Optimize.php';
require_once __DIR__ . '/inc/Tools.php';
require_once __DIR__ . '/inc/Helpers.php';
require_once __DIR__ . '/inc/Options.php';
require_once __DIR__ . '/inc/Actions.php';
require_once __DIR__ . '/inc/Queries.php';
require_once __DIR__ . '/inc/MetaQuery.php';
require_once __DIR__ . '/inc/PostQueries.php';
require_once __DIR__ . '/inc/CommentQueries.php';
require_once __DIR__ . '/inc/UserQueries.php';
require_once __DIR__ . '/inc/TermQueries.php';
require_once __DIR__ . '/inc/Integration.php';
}
/**
* Returns an instance of class
*
* @return WPMetaOptimizer
*/
static function getInstance() {
if ( self::$instance == null )
self::$instance = new WPMetaOptimizer();
return self::$instance;
}
}
WPMetaOptimizer::getInstance();
register_activation_hook( __FILE__, array( 'WPMetaOptimizer\Install', 'install' ) );