1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Carbon_Fields\Datastore; |
4
|
|
|
|
5
|
|
|
use Carbon_Fields\Field\Field; |
6
|
|
|
|
7
|
|
|
class Nav_Menu_Datastore extends Post_Meta_Datastore { |
8
|
|
|
|
9
|
|
|
public function get_garbage_prefix() { |
10
|
|
|
if ( !$this->get_id() ) { |
|
|
|
|
11
|
|
|
return ''; |
12
|
|
|
} |
13
|
|
|
return '_menu-item-' . $this->get_id() . '_'; |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
public function get_clean_field_name( $field ) { |
17
|
|
|
$name = ( is_object( $field ) && is_subclass_of( $field, 'Carbon_Fields\\Field\\Field' ) ) ? $field->get_name() : $field; |
18
|
|
|
return substr( $name, strlen( $this->get_garbage_prefix() ) ); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function get_dirty_field_name( $field ) { |
22
|
|
|
$name = ( is_object( $field ) && is_subclass_of( $field, 'Carbon_Fields\\Field\\Field' ) ) ? $field->get_name() : $field; |
23
|
|
|
return $this->get_garbage_prefix() . $name; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Save the field value(s) into the database. |
28
|
|
|
* |
29
|
|
|
* @param Field $field The field to save. |
30
|
|
|
*/ |
31
|
|
|
public function save( Field $field ) { |
32
|
|
View Code Duplication |
if ( ! update_metadata( $this->get_meta_type(), $this->get_id(), $this->get_clean_field_name( $field ), $field->get_value() ) ) { |
|
|
|
|
33
|
|
|
add_metadata( $this->get_meta_type(), $this->get_id(), $this->get_clean_field_name( $field ), $field->get_value(), true ); |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Load the field value(s) from the database. |
39
|
|
|
* |
40
|
|
|
* @param Field $field The field to retrieve value for. |
41
|
|
|
*/ |
42
|
|
|
public function load( Field $field ) { |
43
|
|
|
global $wpdb; |
44
|
|
|
|
45
|
|
|
if ( ! $this->get_id() ) { |
46
|
|
|
return; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$query = $wpdb->prepare( ' |
50
|
|
|
SELECT `meta_value` |
51
|
|
|
FROM ' . $this->get_table_name() . ' |
52
|
|
|
WHERE `' . $this->get_table_field_name() . '`= %d |
53
|
|
|
AND `meta_key`= %s |
54
|
|
|
LIMIT 1 |
55
|
|
|
', intval( $this->get_id() ), $this->get_clean_field_name( $field ) ); |
56
|
|
|
$value = $wpdb->get_col( $query ); |
|
|
|
|
57
|
|
|
|
58
|
|
View Code Duplication |
if ( ! is_array( $value ) || count( $value ) < 1 ) { |
|
|
|
|
59
|
|
|
$field->set_value( false ); |
60
|
|
|
return; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$field->set_value( $value[0] ); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Delete the field value(s) from the database. |
68
|
|
|
* |
69
|
|
|
* @param Field $field The field to delete. |
70
|
|
|
*/ |
71
|
|
|
public function delete( Field $field ) { |
72
|
|
|
delete_metadata( $this->get_meta_type(), $this->get_id(), $this->get_clean_field_name( $field ), $field->get_value() ); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Load complex field value(s) from the database. |
77
|
|
|
* |
78
|
|
|
* @param mixed $field The field to load values for. |
79
|
|
|
*/ |
80
|
|
|
public function load_values( $field ) { |
81
|
|
|
global $wpdb; |
82
|
|
|
|
83
|
|
|
if ( ! $this->get_id() ) { |
84
|
|
|
return; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$meta_key = $this->get_clean_field_name( $field ); |
88
|
|
|
|
89
|
|
|
$results = $wpdb->get_results( ' |
|
|
|
|
90
|
|
|
SELECT meta_key AS field_key, meta_value AS field_value FROM ' . $this->get_table_name() . ' |
91
|
|
|
WHERE `meta_key` LIKE "' . addslashes( $meta_key ) . '_%" AND `' . $this->get_table_field_name() . '`="' . intval( $this->get_id() ) . '" |
92
|
|
|
', ARRAY_A ); |
93
|
|
|
|
94
|
|
|
foreach ( $results as $index => $result ) { |
95
|
|
|
$result['field_key'] = $this->get_dirty_field_name( $result['field_key'] ); |
96
|
|
|
$results[$index] = $result; |
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $results; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Delete complex field value(s) from the database. |
104
|
|
|
* |
105
|
|
|
* @param mixed $field The field to delete values for. |
106
|
|
|
*/ |
107
|
|
View Code Duplication |
public function delete_values( $field ) { |
|
|
|
|
108
|
|
|
global $wpdb; |
109
|
|
|
|
110
|
|
|
$group_names = $field->get_group_names(); |
111
|
|
|
$field_name = $this->get_clean_field_name( $field ); |
112
|
|
|
|
113
|
|
|
$meta_key_constraint = '`meta_key` LIKE "' . $field_name . implode( '-%" OR `meta_key` LIKE "' . $field_name, $group_names ) . '-%"'; |
114
|
|
|
|
115
|
|
|
return $wpdb->query( ' |
|
|
|
|
116
|
|
|
DELETE FROM ' . $this->get_table_name() . ' |
117
|
|
|
WHERE (' . $meta_key_constraint . ') AND `' . $this->get_table_field_name() . '`="' . intval( $this->get_id() ) . '" |
118
|
|
|
' ); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|