PluginProbe ʕ •ᴥ•ʔ
Secure Custom Fields / 6.5.2
Secure Custom Fields v6.5.2
6.9.0 6.8.9 6.8.7 6.8.8 6.8.6 6.8.4 6.8.5 trunk 6.4.0-beta1 6.4.0-beta2 6.4.1 6.4.1-beta3 6.4.1-beta4 6.4.1-beta5 6.4.1-beta6 6.4.1-beta7 6.4.2 6.5.0 6.5.1 6.5.2 6.5.3 6.5.4 6.5.5 6.5.6 6.5.7 6.6.0 6.7.0 6.7.1 6.8.0 6.8.1 6.8.2 6.8.3
secure-custom-fields / includes / Meta / MetaLocation.php
secure-custom-fields / includes / Meta Last commit date
Comment.php 1 year ago MetaLocation.php 1 year ago Option.php 1 year ago Post.php 1 year ago Term.php 1 year ago User.php 1 year ago WooOrder.php 1 year ago
MetaLocation.php
189 lines
1 <?php
2 /**
3 * A class that can be extended to add support
4 * for different meta types/locations in SCF.
5 *
6 * @package SCF
7 * @subpackage Meta
8 * @since SCF 6.5
9 */
10
11 namespace SCF\Meta;
12
13 /**
14 * The MetaType base class.
15 */
16 class MetaLocation {
17
18 /**
19 * The unique slug/name of the meta location.
20 *
21 * @var string
22 */
23 public string $location_type = '';
24
25 /**
26 * The prefix to use for SCF reference keys/hidden meta.
27 *
28 * @var string
29 */
30 public string $reference_prefix = '_';
31
32 /**
33 * Constructs the location.
34 *
35 * @since 6.5
36 */
37 public function __construct() {
38 $this->register();
39 }
40
41 /**
42 * Registers the meta location with SCF, so it can be used by
43 * various CRUD helper functions.
44 *
45 * @since 6.5
46 *
47 * @return void
48 */
49 public function register() {
50 if ( empty( $this->location_type ) ) {
51 return;
52 }
53
54 $store = acf_get_store( 'acf-meta-locations' );
55
56 if ( ! $store ) {
57 $store = acf_register_store( 'acf-meta-locations' );
58 }
59
60 $store->set( $this->location_type, get_class( $this ) );
61 }
62
63 /**
64 * Retrieves all SCF meta for the provided object ID.
65 *
66 * @since 6.5
67 *
68 * @param integer|string $object_id The ID of the object to get meta from.
69 * @return array
70 */
71 public function get_meta( $object_id = 0 ): array {
72 $meta = array();
73 $all_meta = get_metadata( $this->location_type, $object_id );
74
75 if ( $all_meta ) {
76 foreach ( $all_meta as $key => $value ) {
77 // If a reference exists for this value, add it to the meta array.
78 if ( isset( $all_meta[ $this->reference_prefix . $key ] ) ) {
79 $meta[ $key ] = $value[0];
80 $meta[ $this->reference_prefix . $key ] = $all_meta[ $this->reference_prefix . $key ][0];
81 }
82 }
83 }
84
85 // Unserialize results and return.
86 return array_map( 'acf_maybe_unserialize', $meta );
87 }
88
89 /**
90 * Retrieves a field value from the database.
91 *
92 * @since 6.5
93 *
94 * @param integer|string $object_id The ID of the object the metadata is for.
95 * @param array $field The field array.
96 * @return mixed
97 */
98 public function get_value( $object_id = 0, array $field = array() ) {
99 $meta = get_metadata( $this->location_type, $object_id, $field['name'] );
100 return $meta[0] ?? null;
101 }
102
103 /**
104 * Gets a reference key for the provided field name.
105 *
106 * @since 6.5
107 *
108 * @param integer|string $object_id The ID of the object to get the reference key from.
109 * @param string $field_name The name of the field to get the reference for.
110 * @return string|null
111 */
112 public function get_reference( $object_id = 0, $field_name = '' ) {
113 $reference = get_metadata( $this->location_type, $object_id, $this->reference_prefix . $field_name );
114 return $reference[0] ?? null;
115 }
116
117 /**
118 * Updates an object ID with the provided meta array.
119 *
120 * @since 6.5
121 *
122 * @param integer|string $object_id The ID of the object the metadata is for.
123 * @param array $meta The metadata to save to the object.
124 * @return void
125 */
126 public function update_meta( $object_id = 0, array $meta = array() ) {
127 // Slash data. WP expects all data to be slashed and will unslash it (fixes '\' character issues).
128 $meta = wp_slash( $meta );
129
130 foreach ( $meta as $name => $value ) {
131 update_metadata( $this->location_type, $object_id, $name, $value );
132 }
133 }
134
135 /**
136 * Updates a field value in the database.
137 *
138 * @since 6.5
139 *
140 * @param integer|string $object_id The ID of the object the metadata is for.
141 * @param array $field The field array.
142 * @param mixed $value The metadata value.
143 * @return integer|boolean
144 */
145 public function update_value( $object_id = 0, array $field = array(), $value = '' ) {
146 return update_metadata( $this->location_type, $object_id, $field['name'], $value );
147 }
148
149 /**
150 * Updates a reference key in the database.
151 *
152 * @since 6.5
153 *
154 * @param integer|string $object_id The ID of the object the metadata is for.
155 * @param string $field_name The name of the field to update the reference for.
156 * @param string $value The value of the reference key.
157 * @return integer|boolean
158 */
159 public function update_reference( $object_id = 0, string $field_name = '', string $value = '' ) {
160 return update_metadata( $this->location_type, $object_id, $this->reference_prefix . $field_name, $value );
161 }
162
163 /**
164 * Deletes a field value from the database.
165 *
166 * @since 6.5
167 *
168 * @param integer|string $object_id The ID of the object the metadata is for.
169 * @param array $field The field array.
170 * @return boolean
171 */
172 public function delete_value( $object_id = 0, array $field = array() ): bool {
173 return delete_metadata( $this->location_type, $object_id, $field['name'] );
174 }
175
176 /**
177 * Deletes a reference key from the database.
178 *
179 * @since 6.5
180 *
181 * @param integer|string $object_id The ID of the object the metadata is for.
182 * @param string $field_name The name of the field to delete the reference from.
183 * @return boolean
184 */
185 public function delete_reference( $object_id = 0, string $field_name = '' ): bool {
186 return delete_metadata( $this->location_type, $object_id, $this->reference_prefix . $field_name );
187 }
188 }
189