|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Carbon_Fields\Datastore; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon_Fields\Field\Field; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Abstract meta datastore class. |
|
9
|
|
|
*/ |
|
10
|
|
|
abstract class Meta_Datastore extends Key_Value_Datastore { |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Initialization tasks. |
|
14
|
|
|
*/ |
|
15
|
|
|
public function init() {} |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Get a raw database query results array for a field |
|
19
|
|
|
* |
|
20
|
|
|
* @param Field $field The field to retrieve value for. |
|
21
|
|
|
* @param array $storage_key_patterns |
|
22
|
|
|
* @return array<stdClass> Array of {key, value} objects |
|
23
|
|
|
*/ |
|
24
|
|
|
protected function get_storage_array( Field $field, $storage_key_patterns ) { |
|
25
|
|
|
global $wpdb; |
|
26
|
|
|
|
|
27
|
|
|
$storage_key_comparisons = $this->key_toolset->storage_key_patterns_to_sql( '`meta_key`', $storage_key_patterns ); |
|
28
|
|
|
|
|
29
|
|
|
// @codingStandardsIgnoreStart sanitized in `storage_key_patterns_to_sql` |
|
30
|
|
|
$storage_array = $wpdb->get_results( ' |
|
31
|
|
|
SELECT `meta_key` AS `key`, `meta_value` AS `value` |
|
32
|
|
|
FROM ' . $this->get_table_name() . ' |
|
33
|
|
|
WHERE `' . $this->get_table_field_name() . '` = ' . intval( $this->get_object_id() ) . ' |
|
34
|
|
|
AND ' . $storage_key_comparisons . ' |
|
35
|
|
|
ORDER BY `meta_key` ASC |
|
36
|
|
|
' ); |
|
37
|
|
|
// @codingStandardsIgnoreEnd |
|
38
|
|
|
|
|
39
|
|
|
$storage_array = apply_filters( 'carbon_fields_datastore_storage_array', $storage_array, $this, $storage_key_patterns ); |
|
40
|
|
|
|
|
41
|
|
|
return $storage_array; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Save a single key-value pair to the database |
|
46
|
|
|
* |
|
47
|
|
|
* @param string $key |
|
48
|
|
|
* @param string $value |
|
49
|
|
|
*/ |
|
50
|
|
|
protected function save_key_value_pair( $key, $value ) { |
|
51
|
|
|
$value = wp_slash( $value ); |
|
52
|
|
|
|
|
53
|
|
|
if ( ! update_metadata( $this->get_meta_type(), $this->get_object_id(), $key, $value ) ) { |
|
54
|
|
|
add_metadata( $this->get_meta_type(), $this->get_object_id(), $key, $value, true ); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Delete the field value(s) |
|
60
|
|
|
* |
|
61
|
|
|
* @param Field $field The field to delete. |
|
62
|
|
|
*/ |
|
63
|
|
|
public function delete( Field $field ) { |
|
64
|
|
|
global $wpdb; |
|
65
|
|
|
|
|
66
|
|
|
$storage_key_patterns = $this->key_toolset->get_storage_key_deleter_patterns( |
|
67
|
|
|
is_a( $field, 'Carbon_Fields\\Field\\Complex_Field' ), |
|
68
|
|
|
$field->is_simple_root_field(), |
|
69
|
|
|
$this->get_full_hierarchy_for_field( $field ), |
|
70
|
|
|
$this->get_full_hierarchy_index_for_field( $field ) |
|
71
|
|
|
); |
|
72
|
|
|
$storage_key_comparisons = $this->key_toolset->storage_key_patterns_to_sql( '`meta_key`', $storage_key_patterns ); |
|
73
|
|
|
|
|
74
|
|
|
// @codingStandardsIgnoreStart sanitized in `storage_key_patterns_to_sql` |
|
75
|
|
|
$meta_keys = $wpdb->get_col( ' |
|
76
|
|
|
SELECT `meta_key` |
|
77
|
|
|
FROM `' . $this->get_table_name() . '` |
|
78
|
|
|
WHERE `' . $this->get_table_field_name() . '` = ' . intval( $this->get_object_id() ) . ' |
|
79
|
|
|
AND ' . $storage_key_comparisons . ' |
|
80
|
|
|
' ); |
|
81
|
|
|
// @codingStandardsIgnoreEnd |
|
82
|
|
|
|
|
83
|
|
|
foreach ( $meta_keys as $meta_key ) { |
|
84
|
|
|
delete_metadata( $this->get_meta_type(), $this->get_object_id(), $meta_key ); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Get the type of meta data. |
|
90
|
|
|
*/ |
|
91
|
|
|
abstract public function get_meta_type(); |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Get the meta table name to query. |
|
95
|
|
|
*/ |
|
96
|
|
|
abstract public function get_table_name(); |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Get the meta table field name to query by. |
|
100
|
|
|
*/ |
|
101
|
|
|
abstract public function get_table_field_name(); |
|
102
|
|
|
} |
|
103
|
|
|
|