1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Carbon_Fields\Datastore; |
4
|
|
|
|
5
|
|
|
use Carbon_Fields\Field\Field; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Theme options datastore class. |
9
|
|
|
*/ |
10
|
|
|
class Theme_Options_Datastore extends Key_Value_Datastore { |
11
|
|
|
/** |
12
|
|
|
* Initialization tasks. |
13
|
|
|
**/ |
14
|
|
|
public function init() {} |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Get a raw database query results array for a field |
18
|
|
|
* |
19
|
|
|
* @param Field $field The field to retrieve value for. |
20
|
|
|
* @param array $storage_key_patterns |
21
|
|
|
* @return array<stdClass> Array of {key, value} objects |
22
|
|
|
*/ |
23
|
|
|
protected function get_storage_array( Field $field, $storage_key_patterns ) { |
24
|
|
|
global $wpdb; |
25
|
|
|
|
26
|
|
|
$storage_key_comparisons = $this->key_toolset->storage_key_patterns_to_sql( '`option_name`', $storage_key_patterns ); |
27
|
|
|
|
28
|
|
|
$storage_array = $wpdb->get_results( ' |
|
|
|
|
29
|
|
|
SELECT `option_name` AS `key`, `option_value` AS `value` |
30
|
|
|
FROM ' . $wpdb->options . ' |
31
|
|
|
WHERE ' . $storage_key_comparisons . ' |
32
|
|
|
ORDER BY `option_name` ASC |
33
|
|
|
' ); |
34
|
|
|
|
35
|
|
|
$storage_array = apply_filters( 'crb_datastore_storage_array', $storage_array, $this, $storage_key_patterns ); |
36
|
|
|
|
37
|
|
|
return $storage_array; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Save a single key-value pair to the database |
42
|
|
|
* |
43
|
|
|
* @param string $key |
44
|
|
|
* @param string $value |
45
|
|
|
*/ |
46
|
|
|
protected function save_key_value_pair( $key, $value ) { |
47
|
|
|
$this->save_key_value_pair_with_autoload( $key, $value, false ); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Save a single key-value pair to the database with autoload |
52
|
|
|
* |
53
|
|
|
* @param string $key |
54
|
|
|
* @param string $value |
55
|
|
|
* @param bool $autoload |
56
|
|
|
*/ |
57
|
|
|
protected function save_key_value_pair_with_autoload( $key, $value, $autoload = true ) { |
58
|
|
|
$autoload = $autoload ? 'yes': 'no'; |
59
|
|
|
$notoptions = wp_cache_get( 'notoptions', 'options' ); |
60
|
|
|
$notoptions[ $key ] = ''; |
61
|
|
|
wp_cache_set( 'notoptions', $notoptions, 'options' ); |
62
|
|
|
|
63
|
|
|
if ( ! add_option( $key, $value, null, $autoload ) ) { |
64
|
|
|
update_option( $key, $value, $autoload ); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Save the field value(s) |
70
|
|
|
* |
71
|
|
|
* @param Field $field The field to save. |
72
|
|
|
*/ |
73
|
|
View Code Duplication |
public function save( Field $field ) { |
|
|
|
|
74
|
|
|
$value_set = $field->get_full_value(); |
75
|
|
|
|
76
|
|
|
if ( empty( $value_set ) && $field->get_value_set()->keepalive() ) { |
77
|
|
|
$storage_key = $this->key_toolset->get_storage_key( |
78
|
|
|
$field->is_simple_root_field(), |
79
|
|
|
$this->get_full_hierarchy_for_field( $field ), |
80
|
|
|
$this->get_full_hierarchy_index_for_field( $field ), |
81
|
|
|
0, |
82
|
|
|
$this->key_toolset::KEEPALIVE_PROPERTY |
83
|
|
|
); |
84
|
|
|
$this->save_key_value_pair_with_autoload( $storage_key, '', $field->get_autoload() ); |
85
|
|
|
} |
86
|
|
|
foreach ( $value_set as $value_group_index => $values ) { |
|
|
|
|
87
|
|
|
foreach ( $values as $property => $value ) { |
88
|
|
|
$storage_key = $this->key_toolset->get_storage_key( |
89
|
|
|
$field->is_simple_root_field(), |
90
|
|
|
$this->get_full_hierarchy_for_field( $field ), |
91
|
|
|
$this->get_full_hierarchy_index_for_field( $field ), |
92
|
|
|
$value_group_index, |
93
|
|
|
$property |
94
|
|
|
); |
95
|
|
|
$this->save_key_value_pair_with_autoload( $storage_key, $value, $field->get_autoload() ); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Delete the field value(s) |
102
|
|
|
* |
103
|
|
|
* @param Field $field The field to delete. |
104
|
|
|
*/ |
105
|
|
|
public function delete( Field $field ) { |
106
|
|
|
global $wpdb; |
107
|
|
|
|
108
|
|
|
$storage_key_patterns = $this->key_toolset->get_storage_key_deleter_patterns( |
109
|
|
|
is_a( $field, 'Carbon_Fields\\Field\\Complex_Field' ), |
110
|
|
|
$field->is_simple_root_field(), |
111
|
|
|
$this->get_full_hierarchy_for_field( $field ), |
112
|
|
|
$this->get_full_hierarchy_index_for_field( $field ) |
113
|
|
|
); |
114
|
|
|
$storage_key_comparisons = $this->key_toolset->storage_key_patterns_to_sql( '`option_name`', $storage_key_patterns ); |
115
|
|
|
|
116
|
|
|
$wpdb->query( ' |
|
|
|
|
117
|
|
|
DELETE FROM ' . $wpdb->options . ' |
118
|
|
|
WHERE ' . $storage_key_comparisons . ' |
119
|
|
|
' ); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|