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 Datastore { |
11
|
|
|
/** |
12
|
|
|
* Initialization tasks. |
13
|
|
|
**/ |
14
|
|
|
public function init() {} |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Save the field value(s) into the database. |
18
|
|
|
* |
19
|
|
|
* @param Field $field The field to save. |
20
|
|
|
*/ |
21
|
|
|
public function save( Field $field ) { |
22
|
|
View Code Duplication |
if ( ! update_metadata( $this->get_meta_type(), $this->get_id(), $field->get_name(), $field->get_value() ) ) { |
|
|
|
|
23
|
|
|
add_metadata( $this->get_meta_type(), $this->get_id(), $field->get_name(), $field->get_value(), true ); |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Load the field value(s) from the database. |
29
|
|
|
* |
30
|
|
|
* @param Field $field The field to retrieve value for. |
31
|
|
|
*/ |
32
|
|
|
public function load( Field $field ) { |
33
|
|
|
global $wpdb; |
34
|
|
|
|
35
|
|
|
$value = $wpdb->get_col( ' |
|
|
|
|
36
|
|
|
SELECT `meta_value` |
37
|
|
|
FROM ' . $this->get_table_name() . ' |
38
|
|
|
WHERE `' . $this->get_table_field_name() . '`=' . intval( $this->get_id() ) . ' |
39
|
|
|
AND `meta_key`="' . $field->get_name() . '" |
40
|
|
|
LIMIT 1 |
41
|
|
|
' ); |
42
|
|
|
|
43
|
|
View Code Duplication |
if ( ! is_array( $value ) || count( $value ) < 1 ) { |
|
|
|
|
44
|
|
|
$field->set_value( false ); |
45
|
|
|
return; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$field->set_value( $value[0] ); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Delete the field value(s) from the database. |
53
|
|
|
* |
54
|
|
|
* @param Field $field The field to delete. |
55
|
|
|
*/ |
56
|
|
|
public function delete( Field $field ) { |
57
|
|
|
delete_metadata( $this->get_meta_type(), $this->get_id(), $field->get_name(), $field->get_value() ); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Load complex field value(s) from the database. |
62
|
|
|
* |
63
|
|
|
* @param mixed $field The field to load values for. |
64
|
|
|
*/ |
65
|
|
|
public function load_values( $field ) { |
66
|
|
|
global $wpdb; |
67
|
|
|
|
68
|
|
|
if ( is_object( $field ) && is_subclass_of( $field, 'Carbon_Fields\\Field\\Field' ) ) { |
69
|
|
|
$meta_key = $field->get_name(); |
70
|
|
|
} else { |
71
|
|
|
$meta_key = $field; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $wpdb->get_results( ' |
|
|
|
|
75
|
|
|
SELECT meta_key AS field_key, meta_value AS field_value FROM ' . $this->get_table_name() . ' |
76
|
|
|
WHERE `meta_key` LIKE "' . addslashes( $meta_key ) . '_%" AND `' . $this->get_table_field_name() . '`="' . intval( $this->get_id() ) . '" |
77
|
|
|
', ARRAY_A ); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Delete complex field value(s) from the database. |
82
|
|
|
* |
83
|
|
|
* @param mixed $field The field to delete values for. |
84
|
|
|
*/ |
85
|
|
View Code Duplication |
public function delete_values( $field ) { |
|
|
|
|
86
|
|
|
global $wpdb; |
87
|
|
|
|
88
|
|
|
$group_names = $field->get_group_names(); |
89
|
|
|
$field_name = $field->get_name(); |
90
|
|
|
|
91
|
|
|
$meta_key_constraint = '`meta_key` LIKE "' . $field_name . implode( '-%" OR `meta_key` LIKE "' . $field_name, $group_names ) . '-%"'; |
92
|
|
|
|
93
|
|
|
return $wpdb->query( ' |
|
|
|
|
94
|
|
|
DELETE FROM ' . $this->get_table_name() . ' |
95
|
|
|
WHERE (' . $meta_key_constraint . ') AND `' . $this->get_table_field_name() . '`="' . intval( $this->get_id() ) . '" |
96
|
|
|
' ); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Retrieve the type of meta data. |
101
|
|
|
*/ |
102
|
|
|
abstract public function get_meta_type(); |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Retrieve the meta table name to query. |
106
|
|
|
*/ |
107
|
|
|
abstract public function get_table_name(); |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Retrieve the meta table field name to query by. |
111
|
|
|
*/ |
112
|
|
|
abstract public function get_table_field_name(); |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Set the ID of the datastore. |
116
|
|
|
*/ |
117
|
|
|
abstract public function set_id( $id ); |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Retrieve the ID of the datastore. |
121
|
|
|
*/ |
122
|
|
|
abstract public function get_id(); |
123
|
|
|
} |
124
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.