-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
1741 lines (1443 loc) · 54 KB
/
functions.php
File metadata and controls
1741 lines (1443 loc) · 54 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
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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php defined( 'ABSPATH' ) || exit;
/**
* Register a custom table
*
* @param string $name
* @param array $args
*
* @return TDB_Table
*/
function tdb_register_table( $name, $args ) {
global $tdb_registered_tables;
if ( ! is_array( $tdb_registered_tables ) ) {
$tdb_registered_tables = array();
}
$name = sanitize_key( $name );
if( isset( $tdb_registered_tables[$name] ) ) {
return $tdb_registered_tables[$name];
}
$tdb_table = new TDB_Table( $name, $args );
$tdb_registered_tables[$name] = $tdb_table;
return $tdb_table;
}
/**
* Setup the global table
*
* @since 1.0.0
*
* @param TDB_Table|string $object TDB_Table object or TDB_Table name
*
* @return TDB_Table $tdb_table
*/
function tdb_setup_table( $object ) {
global $tdb_registered_tables, $tdb_previous_table, $tdb_table;
if( is_object( $tdb_table ) ) {
$tdb_previous_table = $tdb_table;
}
if( is_object( $object ) ) {
$tdb_table = $object;
} else if( gettype( $object ) === 'string' && isset( $tdb_registered_tables[$object] ) ) {
$tdb_table = $tdb_registered_tables[$object];
}
return $tdb_table;
}
/**
* Setup the global table
*
* @since 1.0.0
*
* @return TDB_Table $tdb_table
*/
function tdb_reset_setup_table() {
global $tdb_registered_tables, $tdb_previous_table, $tdb_table;
if( is_object( $tdb_previous_table ) ) {
$tdb_table = $tdb_previous_table;
}
return $tdb_table;
}
/**
* get the TDB_Table object of given table name
*
* @param TDB_Table|string $name TDB_Table object or TDB_Table name
*
* @since 1.0.0
*
* @return TDB_Table
*/
function tdb_get_table_object( $name ) {
global $tdb_registered_tables;
if( is_object( $name ) ) {
$tdb_table = $name;
} else if( gettype( $name ) === 'string' && isset( $tdb_registered_tables[$name] ) ) {
$tdb_table = $tdb_registered_tables[$name];
}
return $tdb_table;
}
/**
* Get the table labels
*
* @since 1.0.0
*
* @param TDB_Table $tdb_table
*
* @return array
*/
function tdb_get_table_labels( $tdb_table ) {
$default_labels = array(
'name' => __('%1$s', 'tdb'),
'singular_name' => __('%1$s', 'tdb'),
'plural_name' => __('%2$s', 'tdb'),
'add_new' => __('Add New', 'tdb'),
'add_new_item' => __('Add New %1$s', 'tdb'),
'edit_item' => __('Edit %1$s', 'tdb'),
'new_item' => __('New %1$s', 'tdb'),
'view_item' => __('View %1$s', 'tdb'),
'view_items' => __('View %2$s', 'tdb'),
'search_items' => __( 'Search %2$s', 'tdb' ),
'not_found' => __( 'No %2$s found.', 'tdb' ),
'not_found_in_trash' => __( 'No %2$s found in Trash.', 'tdb' ),
'parent_item_colon' => __( 'Parent %1$s:', 'tdb' ),
'all_items' => __( 'All %2$s', 'tdb' ),
'archives' => __( '%1$s Archives', 'tdb' ),
'attributes' => __( '%1$s Attributes', 'tdb' ),
'insert_into_item' => __( 'Insert into %1$s', 'tdb' ),
'uploaded_to_this_item' => __( 'Uploaded to this post', 'tdb' ),
'featured_image' => __( 'Featured Image', 'tdb' ),
'set_featured_image' => __( 'Set featured image', 'tdb' ),
'remove_featured_image' => __( 'Remove featured image', 'tdb' ),
'use_featured_image' => __( 'Use as featured image', 'tdb' ),
'filter_items_list' => __( 'Filter posts list', 'tdb' ),
'items_list_navigation' => __( '%2$s list navigation', 'tdb' ),
'items_list' => __( '%2$s list', 'tdb' ),
);
foreach( $default_labels as $label => $pattern ) {
$default_labels[$label] = sprintf( $pattern, $tdb_table->singular, $tdb_table->plural );
}
return (object) $default_labels;
}
/**
* Get the table fields
*
* @since 1.0.0
*
* @param TDB_Table $tdb_table
*
* @return array
*/
function tdb_get_table_fields( $tdb_table ) {
return $tdb_table->db->schema->fields;
}
/**
* Execute role creation.
*
* @since 1.0.0
*/
function tdb_populate_roles() {
// Add caps for Administrator role
$role = get_role( 'administrator' );
// Bail if administrator role is not setup
if( ! $role ) {
return;
}
$args = (object) array(
'capabilities' => array(),
'capability_type' => 'item',
'map_meta_cap' => null
);
$capabilities = tdb_get_table_capabilities( $args );
foreach( $capabilities as $cap ) {
$role->add_cap( $cap );
}
}
/**
* Build an object with all tables capabilities out of a table object
*
* Post type capabilities use the 'capability_type' argument as a base, if the
* capability is not set in the 'capabilities' argument array or if the
* 'capabilities' argument is not supplied.
*
* The capability_type argument can optionally be registered as an array, with
* the first value being singular and the second plural, e.g. array('story, 'stories')
* Otherwise, an 's' will be added to the value for the plural form. After
* registration, capability_type will always be a string of the singular value.
*
* By default, seven keys are accepted as part of the capabilities array:
*
* - edit_item, read_item, and delete_item are meta capabilities, which are then
* generally mapped to corresponding primitive capabilities depending on the
* context, which would be the item being edited/read/deleted and the user or
* role being checked. Thus these capabilities would generally not be granted
* directly to users or roles.
*
* - edit_items - Controls whether objects of this item type can be edited.
* - edit_others_items - Controls whether objects of this type owned by other users
* can be edited. If the item type does not support an author, then this will
* behave like edit_items.
* - publish_items - Controls publishing objects of this item type.
* - read_private_items - Controls whether private objects can be read.
*
* These four primitive capabilities are checked in core in various locations.
* There are also seven other primitive capabilities which are not referenced
* directly in core, except in map_meta_cap(), which takes the three aforementioned
* meta capabilities and translates them into one or more primitive capabilities
* that must then be checked against the user or role, depending on the context.
*
* - read - Controls whether objects of this item type can be read.
* - delete_items - Controls whether objects of this item type can be deleted.
* - delete_private_items - Controls whether private objects can be deleted.
* - delete_published_items - Controls whether published objects can be deleted.
* - delete_others_items - Controls whether objects owned by other users can be
* can be deleted. If the item type does not support an author, then this will
* behave like delete_items.
* - edit_private_items - Controls whether private objects can be edited.
* - edit_published_items - Controls whether published objects can be edited.
*
* These additional capabilities are only used in map_meta_cap(). Thus, they are
* only assigned by default if the item type is registered with the 'map_meta_cap'
* argument set to true (default is false).
*
* @since 1.0.0
*
* @see register_post_type()
* @see map_meta_cap()
*
* @param object $args Post type registration arguments.
* @return object object with all the capabilities as member variables.
*/
function tdb_get_table_capabilities( $args ) {
if ( ! is_array( $args->capability_type ) ) {
$args->capability_type = array( $args->capability_type, $args->capability_type . 's' );
}
// Singular base for meta capabilities, plural base for primitive capabilities.
list( $singular_base, $plural_base ) = $args->capability_type;
$default_capabilities = array(
// Meta capabilities
'edit_item' => 'edit_' . $singular_base,
'read_item' => 'read_' . $singular_base,
'delete_item' => 'delete_' . $singular_base,
'delete_items' => 'delete_' . $plural_base,
// Primitive capabilities used outside of map_meta_cap():
'edit_items' => 'edit_' . $plural_base,
'edit_others_items' => 'edit_others_' . $plural_base,
'publish_items' => 'publish_' . $plural_base,
'read_private_items' => 'read_private_' . $plural_base,
);
// Primitive capabilities used within map_meta_cap():
if ( $args->map_meta_cap ) {
$default_capabilities_for_mapping = array(
'read' => 'read',
'delete_items' => 'delete_' . $plural_base,
'delete_private_items' => 'delete_private_' . $plural_base,
'delete_published_items' => 'delete_published_' . $plural_base,
'delete_others_items' => 'delete_others_' . $plural_base,
'edit_private_items' => 'edit_private_' . $plural_base,
'edit_published_items' => 'edit_published_' . $plural_base,
'add_post_meta' => 'add_' . $singular_base . '_meta',
'edit_post_meta' => 'edit_' . $singular_base . '_meta',
'delete_post_meta' => 'delete_' . $singular_base . '_meta',
);
$default_capabilities = array_merge( $default_capabilities, $default_capabilities_for_mapping );
}
$capabilities = array_merge( $default_capabilities, $args->capabilities );
// Post creation capability simply maps to edit_items by default:
if ( ! isset( $capabilities['create_items'] ) ) {
$capabilities['create_items'] = $capabilities['edit_items'];
}
// Remember meta capabilities for future reference.
if ( $args->map_meta_cap ) {
_tdb_meta_capabilities( $capabilities );
}
// Shortcut to override all capabilities by a specific capability
if( property_exists( $args, 'capability' ) && ! empty( $args->capability ) ) {
foreach ( $capabilities as $key => $value ) {
$capabilities[$key] = $args->capability;
}
}
return (object) $capabilities;
}
/**
* Store or return a list of table meta caps for map_meta_cap().
*
* @since 1.0.0
* @access private
*
* @global array $post_type_meta_caps Used to store meta capabilities.
*
* @param array $capabilities Post type meta capabilities.
*/
function _tdb_meta_capabilities( $capabilities = null ) {
global $tdb_meta_caps;
foreach ( $capabilities as $core => $custom ) {
if ( in_array( $core, array( 'read_item', 'delete_item', 'edit_item' ) ) ) {
$tdb_meta_caps[ $custom ] = $core;
}
}
}
/**
* Retrieves object data given a object primary key or object object.
*
* @since 1.0.0
*
* @param int|stdClass|null $object Optional. Object primary key or object object.
* @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to
* a WP_Post object, an associative array, or a numeric array, respectively. Default OBJECT.
* @param string $filter Optional. Type of filter to apply. Accepts 'raw', 'edit', 'db',
* or 'display'. Default 'raw'.
* @return stdClass|array|null Type corresponding to $output on success or null on failure.
* When $output is OBJECT, a `stdClass` instance is returned.
*/
function tdb_get_object( $object = null, $output = OBJECT, $filter = 'raw' ) {
global $wpdb, $tdb_table;
if ( is_object( $object ) ) {
$primary_key = $tdb_table->db->primary_key;
$object = tdb_get_object_instance( $object->$primary_key );
} else {
$object = tdb_get_object_instance( $object );
}
if ( ! $object )
return null;
if ( $output == ARRAY_A )
return (array) $object;
elseif ( $output == ARRAY_N )
return array_values( (array) $object );
return $object;
}
/**
* Retrieve the object instance.
*
* @since 1.0.0
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param int $object_id Object primary key.
* @return stdClass|false Object std object, false otherwise.
*/
function tdb_get_object_instance( $object_id = null ) {
global $wpdb, $tdb_table;
$object_id = (int) $object_id;
if ( ! $object_id ) {
return false;
}
$object = wp_cache_get( $object_id, $tdb_table->name );
if ( ! $object ) {
$object = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$tdb_table->db->table_name} WHERE {$tdb_table->db->primary_key} = %d LIMIT 1", $object_id ) );
if ( ! $object )
return false;
wp_cache_add( $object_id, $object, $tdb_table->name );
}
return $object;
}
/**
* Will clean the object in the cache.
*
* Cleaning means delete from the cache of the object.
*
* This function not run if $_wp_suspend_cache_invalidation is not empty. See wp_suspend_cache_invalidation().
*
* @since 1.0.0
*
* @global bool $_wp_suspend_cache_invalidation
*
* @param int|Object $object Post ID or post object to remove from the cache.
*/
function tdb_clean_object_cache( $object ) {
global $_wp_suspend_cache_invalidation, $tdb_table;
if ( ! empty( $_wp_suspend_cache_invalidation ) )
return;
$object = tdb_get_object( $object );
if ( empty( $object ) )
return;
$primary_key = $tdb_table->db->primary_key;
wp_cache_delete( $object->$primary_key, $tdb_table->name );
//wp_cache_delete( 'wp_get_archives', 'general' );
/**
* Fires immediately after the given object's cache is cleaned.
*
* @since 1.0.0
*
* @param int $post_id Post ID.
* @param WP_Post $post Post object.
*/
do_action( 'tdb_clean_object_cache', $object->$primary_key, $object );
wp_cache_set( 'last_changed', microtime(), $tdb_table->name );
}
/**
* Update an object with new data.
*
* The date does not have to be set for drafts. You can set the date and it will
* not be overridden.
*
* @since 1.0.0
*
* @param array|object $object_data Optional. Object data. Arrays are expected to be escaped, objects are not. Default array.
* @param bool $wp_error Optional. Allow return of WP_Error on failure. Default false.
* @return int|WP_Error The value 0 or WP_Error on failure. The post ID on success.
*/
function tdb_update_object( $object_data = array(), $wp_error = false ) {
global $tdb_table;
if ( is_object( $object_data ) ) {
// Non-escaped post was passed.
$object_data = get_object_vars( $object_data );
$object_data = wp_slash( $object_data );
}
$primary_key = $tdb_table->db->primary_key;
// First, get all of the original fields.
$object = tdb_get_object( $object_data[$primary_key], ARRAY_A );
if ( is_null( $object ) ) {
if ( $wp_error )
return new WP_Error( 'invalid_object', __( 'Invalid object ID.' ) );
return 0;
}
// Escape data pulled from DB.
$object = wp_slash( $object );
// Merge old and new fields with new fields overwriting old ones.
$object_data = array_merge( $object, $object_data );
return tdb_insert_object( $object_data, $wp_error );
}
/**
* Insert or update an object.
*
* @since 1.0.0
*
* @global wpdb $wpdb WordPress database abstraction object.
* @global TDB_Table $tdb_table Custom Tables TDB_Table object type.
*
* @param array $object_data An array of elements that make up a post to update or insert.
* @param bool $wp_error Optional. Whether to return a WP_Error on failure. Default false.
*
* @return int|WP_Error The post ID on success. The value 0 or WP_Error on failure.
*/
function tdb_insert_object( $object_data, $wp_error = false ) {
global $wpdb, $tdb_table;
if( ! is_a( $tdb_table, 'TDB_Table' ) ) {
return new WP_Error( 'invalid_tdb_table', __( 'Invalid TDB Table object.' ) );
}
/**
* Setup the default object for the add new view.
*
* The dynamic portion of the hook, `$this->name`, refers to the object type of the object.
*
* @since 1.0.0
*
* @param array $default_data Default data to be filtered.
*/
$defaults = apply_filters( "tdb_{$tdb_table->name}_default_data", array() );
$object_data = wp_parse_args( $object_data, $defaults );
// Are we updating or creating?
$object_id = 0;
$original_object_data = array();
$update = false;
$primary_key = $tdb_table->db->primary_key;
if ( ! empty( $object_data[$primary_key] ) ) {
$update = true;
// Get the object ID.
$object_id = $object_data[$primary_key];
$object_before = tdb_get_object( $object_id );
$original_object_data = tdb_get_object( $object_id, ARRAY_A );
if ( is_null( $original_object_data ) ) {
if ( $wp_error ) {
return new WP_Error( 'invalid_object', __( 'Invalid object ID.' ) );
}
return 0;
}
}
/**
* Filters slashed post data just before it is inserted into the database.
*
* @since 1.0.0
*
* @param array $object_data An array with new object data.
* @param array $original_object_data An array with the original object data.
*/
$object_data = apply_filters( 'tdb_insert_object_data', $object_data, $original_object_data );
$object_data = wp_unslash( $object_data );
$where = array( $primary_key => $object_id );
if ( $update ) {
/**
* Fires immediately before an existing object is updated in the database.
*
* @since 1.0.0
*
* @param int $object_id Object ID.
* @param array $data Array of unslashed object data.
*/
do_action( 'pre_object_update', $object_id, $object_data );
if ( false === $tdb_table->db->update( $object_data, $where ) ) {
if ( $wp_error ) {
return new WP_Error('db_update_error', __('Could not update object in the database'), $wpdb->last_error);
} else {
return 0;
}
}
} else {
$import_id = isset( $object_data['import_id'] ) ? $object_data['import_id'] : 0;
// If there is a suggested ID, use it if not already present.
if ( ! empty( $import_id ) ) {
$import_id = (int) $import_id;
if ( ! $wpdb->get_var( $wpdb->prepare("SELECT {$primary_key} FROM {$tdb_table->db->table_name} WHERE {$primary_key} = %d", $import_id) ) ) {
$object_data[$primary_key] = $import_id;
}
}
if (empty($object_data)) {
/**
* IMPORTANT: Data cannot be an empty array; otherwise the call to
* $wpdb->insert() throws a notice: "wpdb::prepare was called incorrectly.
* The query argument of wpdb::prepare() must have a placeholder."
*
* That's because it creates an SQL query with empty columns:
* INSERT INTO `table_name` (``) VALUES ()
*
* This is a workaround to insert an empty object.
*/
$wpdb->insert_id = 0;
$wpdb->query("INSERT INTO {$tdb_table->db->table_name} () VALUES ();");
} elseif ( false === $wpdb->insert( $tdb_table->db->table_name, $object_data ) ) {
if ( $wp_error ) {
return new WP_Error('db_insert_error', __('Could not insert object into the database'), $wpdb->last_error);
} else {
return 0;
}
}
$object_id = (int) $wpdb->insert_id;
}
// If isset meta_input and object supports meta, then add meta data
if ( ! empty( $object_data['meta_input'] ) && $tdb_table->meta ) {
foreach ( $object_data['meta_input'] as $field => $value ) {
tdb_update_object_meta( $object_id, $field, $value );
}
}
tdb_clean_object_cache( $object_id );
$object = tdb_get_object( $object_id );
if ( $update ) {
/**
* Fires once an existing post has been updated.
*
* @since 1.0.0
*
* @param int $post_ID Post ID.
* @param WP_Post $post Post object.
*/
do_action( 'tdb_edit_object', $object_id, $object );
$object_after = tdb_get_object( $object_id );
/**
* Fires once an existing post has been updated.
*
* @since 1.0.0
*
* @param int $object_id Object ID.
* @param WP_Post $object_after Object following the update.
* @param WP_Post $object_before Object before the update.
*/
do_action( 'tdb_object_updated', $object_id, $object_after, $object_before);
}
/**
* Fires once a object has been saved.
*
* The dynamic portion of the hook name, `{$tdb_table->name}`, refers to the object type.
*
* @since 1.0.0
*
* @param int $object_id Object ID.
* @param stdClass $object Object.
* @param bool $update Whether this is an existing object being updated or not.
*/
do_action( "tdb_save_object_{$tdb_table->name}", $object_id, $object, $update );
/**
* Fires once a post has been saved.
*
* @since 1.0.0
*
* @param int $object_id Object ID.
* @param stdClass $object Object.
* @param bool $update Whether this is an existing object being updated or not.
*/
do_action( 'tdb_save_object', $object_id, $object, $update );
/**
* Fires once a post has been saved.
*
* @since 1.0.0
*
* @param int $object_id Object ID.
* @param stdClass $object Object.
* @param bool $update Whether this is an existing object being updated or not.
*/
do_action( 'tdb_insert_object', $object_id, $object, $update );
return $object_id;
}
/**
* Trash or delete an object.
*
* When the post and page is permanently deleted, everything that is tied to
* it is deleted also. This includes comments, post meta fields, and terms
* associated with the post.
*
* The post or page is moved to trash instead of permanently deleted unless
* trash is disabled, item is already in the trash, or $force_delete is true.
*
* @since 1.0.0
*
* @global wpdb $wpdb WordPress database abstraction object.
* @see wp_delete_attachment()
* @see wp_trash_post()
*
* @param int $object_id Optional. Object ID. Default 0.
* @param bool $force_delete Optional. Whether to bypass trash and force deletion.
* Default false.
* @return WP_Post|false|null Post data on success, false or null on failure.
*/
function tdb_delete_object( $object_id = 0, $force_delete = false ) {
global $wpdb, $tdb_table;
if( ! is_a( $tdb_table, 'TDB_Table' ) ) {
return new WP_Error( 'invalid_tdb_table', __( 'Invalid TDB Table object.' ) );
}
$tdb_object = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$tdb_table->db->table_name} WHERE {$tdb_table->db->primary_key} = %d", $object_id ) );
if ( ! $tdb_object ) {
return $tdb_object;
}
$tdb_object = tdb_get_object( $tdb_object );
// TODO: Add support for trash functionality
//if ( ! $force_delete && ( 'post' === $post->post_type || 'page' === $post->post_type ) && 'trash' !== get_post_status( $postid ) && EMPTY_TRASH_DAYS ) {
//return tdb_trash_object( $object_id );
//}
/**
* Filters whether a post deletion should take place.
*
* @since 1.0.0
*
* @param bool $delete Whether to go forward with deletion.
* @param WP_Post $post Post object.
* @param bool $force_delete Whether to bypass the trash.
*/
$check = apply_filters( 'pre_delete_object', null, $tdb_object, $force_delete );
if ( null !== $check ) {
return $check;
}
/**
* Fires before a post is deleted, at the start of tdb_delete_object().
*
* @since 1.0.0
*
* @see tdb_delete_object()
*
* @param int $object_id Object ID.
*/
do_action( 'before_delete_object', $object_id );
if( $tdb_table->meta ) {
tdb_delete_object_meta( $object_id, '_wp_trash_meta_status' );
tdb_delete_object_meta( $object_id, '_wp_trash_meta_time' );
$object_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT {$tdb_table->meta->db->primary_key} FROM {$tdb_table->meta->db->table_name} WHERE {$tdb_table->db->primary_key} = %d ", $object_id ));
foreach ( $object_meta_ids as $mid ) {
tdb_delete_metadata_by_mid( 'post', $mid );
}
}
/**
* Fires immediately before a post is deleted from the database.
*
* @since 1.0.0
*
* @param int $object_id Object ID.
*/
do_action( 'delete_object', $object_id );
$result = $tdb_table->db->delete( $object_id );
if ( ! $result ) {
return false;
}
/**
* Fires immediately after a post is deleted from the database.
*
* @since 1.0.0
*
* @param int $object_id Object ID.
*/
do_action( 'deleted_object', $object_id );
tdb_clean_object_cache( $tdb_object );
/**
* Fires after a post is deleted, at the conclusion of tdb_delete_object().
*
* @since 1.0.0
*
* @see tdb_delete_object()
*
* @param int $object_id Object ID.
*/
do_action( 'after_delete_object', $object_id );
return $tdb_object;
}
//
// Object meta functions
//
/**
* Add meta data field to an object.
*
* Object meta data is called "Custom Fields" on the Administration Screen.
*
* @since 1.0.0
*
* @param int $object_id Object ID.
* @param string $meta_key Metadata name.
* @param mixed $meta_value Metadata value. Must be serializable if non-scalar.
* @param bool $unique Optional. Whether the same key should not be added. Default false.
* @return int|false Meta ID on success, false on failure.
*/
function tdb_add_object_meta( $object_id, $meta_key, $meta_value, $unique = false ) {
global $wpdb, $tdb_table;
// Bail if TDB_Table not supports meta data
if( ! $tdb_table->meta ) {
return false;
}
$object_id = absint( $object_id );
if ( ! $object_id ) {
return false;
}
$primary_key = $tdb_table->db->primary_key;
$meta_primary_key = $tdb_table->meta->db->primary_key;
$meta_table_name = $tdb_table->meta->db->table_name;
// expected_slashed ($meta_key)
$meta_key = wp_unslash($meta_key);
$meta_value = wp_unslash($meta_value);
$meta_value = sanitize_meta( $meta_key, $meta_value, $tdb_table->name );
/**
* Filters whether to add metadata of a specific type.
*
* The dynamic portion of the hook, `$meta_type`, refers to the meta
* object type (comment, post, or user). Returning a non-null value
* will effectively short-circuit the function.
*
* @since 1.0.0
*
* @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.
*/
$check = apply_filters( "add_{$tdb_table->name}_metadata", null, $object_id, $meta_key, $meta_value, $unique );
if ( null !== $check )
return $check;
if ( $unique && $wpdb->get_var( $wpdb->prepare(
"SELECT COUNT(*) FROM $meta_table_name WHERE meta_key = %s AND $primary_key = %d",
$meta_key, $object_id ) ) )
return false;
$_meta_value = $meta_value;
$meta_value = maybe_serialize( $meta_value );
/**
* Fires immediately before meta of a specific type is added.
*
* The dynamic portion of the hook, `$meta_type`, refers to the meta
* object type (comment, post, or user).
*
* @since 1.0.0
*
* @param int $object_id Object ID.
* @param string $meta_key Meta key.
* @param mixed $meta_value Meta value.
*/
do_action( "add_{$tdb_table->name}_meta", $object_id, $meta_key, $_meta_value );
$result = $wpdb->insert( $meta_table_name, array(
$primary_key => $object_id,
'meta_key' => $meta_key,
'meta_value' => $meta_value
) );
if ( ! $result )
return false;
$mid = (int) $wpdb->insert_id;
wp_cache_delete( $object_id, $tdb_table->meta->name );
/**
* Fires immediately after meta of a specific type is added.
*
* The dynamic portion of the hook, `$meta_type`, refers to the meta
* object type (comment, post, or user).
*
* @since 1.0.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_{$tdb_table->name}_meta", $mid, $object_id, $meta_key, $_meta_value );
return $mid;
}
/**
* Remove metadata matching criteria from a post.
*
* You can match based on the key, or key and value. Removing based on key and
* value, will keep from removing duplicate metadata with the same key. It also
* allows removing all metadata matching key, if needed.
*
* @since 1.0.0
*
* @param int $object_id Post ID.
* @param string $meta_key Metadata name.
* @param mixed $meta_value Optional. Metadata value. Must be serializable if
* non-scalar. Default empty.
* @return bool True on success, false on failure.
*/
function tdb_delete_object_meta( $object_id, $meta_key, $meta_value = '' ) {
global $wpdb, $tdb_table;
// Bail if TDB_Table not supports meta data
if( ! $tdb_table->meta ) {
return false;
}
$object_id = absint( $object_id );
if ( ! $object_id ) {
return false;
}
$primary_key = $tdb_table->db->primary_key;
$meta_primary_key = $tdb_table->meta->db->primary_key;
$meta_table_name = $tdb_table->meta->db->table_name;
// expected_slashed ($meta_key)
$meta_key = wp_unslash($meta_key);
$meta_value = wp_unslash($meta_value);
$delete_all = false;
/**
* Filters whether to delete metadata of a specific type.
*
* The dynamic portion of the hook, `$meta_type`, refers to the meta
* object type (comment, post, or user). Returning a non-null value
* will effectively short-circuit the function.
*
* @since 1.0.0
*
* @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.
*/
$check = apply_filters( "delete_{$tdb_table->name}_metadata", null, $object_id, $meta_key, $meta_value, $delete_all );
if ( null !== $check )
return (bool) $check;
$_meta_value = $meta_value;
$meta_value = maybe_serialize( $meta_value );
$query = $wpdb->prepare( "SELECT $meta_primary_key FROM $meta_table_name WHERE meta_key = %s", $meta_key );
if ( !$delete_all )
$query .= $wpdb->prepare(" AND $primary_key = %d", $object_id );
if ( '' !== $meta_value && null !== $meta_value && false !== $meta_value )
$query .= $wpdb->prepare(" AND meta_value = %s", $meta_value );
$meta_ids = $wpdb->get_col( $query );
if ( !count( $meta_ids ) )
return false;
if ( $delete_all ) {
$value_clause = '';
if ( '' !== $meta_value && null !== $meta_value && false !== $meta_value ) {
$value_clause = $wpdb->prepare( " AND meta_value = %s", $meta_value );
}
$object_ids = $wpdb->get_col( $wpdb->prepare( "SELECT $meta_primary_key FROM $meta_table_name WHERE meta_key = %s $value_clause", $meta_key ) );
}
/**
* Fires immediately before deleting metadata of a specific type.