PluginProbe ʕ •ᴥ•ʔ
Secure Custom Fields / 6.4.0-beta2
Secure Custom Fields v6.4.0-beta2
6.9.5 6.9.4 6.9.3 6.9.2 6.9.1 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 / local-meta.php
secure-custom-fields / includes Last commit date
Blocks 1 year ago admin 1 year ago ajax 1 year ago api 1 year ago fields 1 year ago forms 1 year ago legacy 1 year ago locations 1 year ago post-types 1 year ago rest-api 1 year ago walkers 1 year ago acf-bidirectional-functions.php 1 year ago acf-field-functions.php 1 year ago acf-field-group-functions.php 1 year ago acf-form-functions.php 1 year ago acf-helper-functions.php 1 year ago acf-hook-functions.php 1 year ago acf-input-functions.php 1 year ago acf-internal-post-type-functions.php 1 year ago acf-meta-functions.php 1 year ago acf-post-functions.php 1 year ago acf-post-type-functions.php 1 year ago acf-taxonomy-functions.php 1 year ago acf-user-functions.php 1 year ago acf-utility-functions.php 1 year ago acf-value-functions.php 1 year ago acf-wp-functions.php 1 year ago assets.php 1 year ago class-acf-data.php 1 year ago class-acf-internal-post-type.php 1 year ago class-acf-site-health.php 1 year ago compatibility.php 1 year ago deprecated.php 1 year ago fields.php 1 year ago index.php 1 year ago l10n.php 1 year ago local-fields.php 1 year ago local-json.php 1 year ago local-meta.php 1 year ago locations.php 1 year ago loop.php 1 year ago media.php 1 year ago rest-api.php 1 year ago revisions.php 1 year ago third-party.php 1 year ago upgrades.php 1 year ago validation.php 1 year ago wpml.php 1 year ago
local-meta.php
257 lines
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit; // Exit if accessed directly
5 }
6
7 if ( ! class_exists( 'ACF_Local_Meta' ) ) :
8
9 class ACF_Local_Meta {
10
11 /** @var array Storage for meta data. */
12 var $meta = array();
13
14 /** @var mixed Storage for the current post_id. */
15 var $post_id = 0;
16
17 /**
18 * __construct
19 *
20 * Sets up the class functionality.
21 *
22 * @date 8/10/18
23 * @since 5.8.0
24 *
25 * @param void
26 * @return void
27 */
28 function __construct() {
29
30 // add filters
31 add_filter( 'acf/pre_load_post_id', array( $this, 'pre_load_post_id' ), 1, 2 );
32 add_filter( 'acf/pre_load_meta', array( $this, 'pre_load_meta' ), 1, 2 );
33 add_filter( 'acf/pre_load_metadata', array( $this, 'pre_load_metadata' ), 1, 4 );
34 }
35
36 /**
37 * add
38 *
39 * Adds postmeta to storage.
40 * Accepts data in either raw or request format.
41 *
42 * @date 8/10/18
43 * @since 5.8.0
44 *
45 * @param array $meta An array of metdata to store.
46 * @param mixed $post_id The post_id for this data.
47 * @param boolean $is_main Makes this postmeta visible to get_field() without a $post_id value.
48 * @return array
49 */
50 function add( $meta = array(), $post_id = 0, $is_main = false ) {
51
52 // Capture meta if supplied meta is from a REQUEST.
53 if ( $this->is_request( $meta ) ) {
54 $meta = $this->capture( $meta, $post_id );
55 }
56
57 // Add to storage.
58 $this->meta[ $post_id ] = $meta;
59
60 // Set $post_id reference when is the "main" postmeta.
61 if ( $is_main ) {
62 $this->post_id = $post_id;
63 }
64
65 // Return meta.
66 return $meta;
67 }
68
69 /**
70 * is_request
71 *
72 * Returns true if the supplied $meta is from a REQUEST (serialized <form> data).
73 *
74 * @date 11/3/19
75 * @since 5.7.14
76 *
77 * @param array $meta An array of metdata to check.
78 * @return boolean
79 */
80 function is_request( $meta = array() ) {
81 return acf_is_field_key( key( $meta ) );
82 }
83
84 /**
85 * capture
86 *
87 * Returns a flattened array of meta for the given postdata.
88 * This is achieved by simulating a save whilst capturing all meta changes.
89 *
90 * @date 26/2/19
91 * @since 5.7.13
92 *
93 * @param array $values An array of raw values.
94 * @param mixed $post_id The post_id for this data.
95 * @return array
96 */
97 function capture( $values = array(), $post_id = 0 ) {
98
99 // Reset meta.
100 $this->meta[ $post_id ] = array();
101
102 // Listen for any added meta.
103 add_filter( 'acf/pre_update_metadata', array( $this, 'capture_update_metadata' ), 1, 5 );
104
105 // Simulate update.
106 if ( $values ) {
107 acf_update_values( $values, $post_id );
108 }
109
110 // Remove listener filter.
111 remove_filter( 'acf/pre_update_metadata', array( $this, 'capture_update_metadata' ), 1, 5 );
112
113 // Return meta.
114 return $this->meta[ $post_id ];
115 }
116
117 /**
118 * capture_update_metadata
119 *
120 * Records all meta activity and returns a non null value to bypass DB updates.
121 *
122 * @date 26/2/19
123 * @since 5.7.13
124 *
125 * @param null $null .
126 * @param (int|string) $post_id The post id.
127 * @param string $name The meta name.
128 * @param mixed $value The meta value.
129 * @param boolean $hidden If the meta is hidden (starts with an underscore).
130 * @return false.
131 */
132 function capture_update_metadata( $null, $post_id, $name, $value, $hidden ) {
133 $name = ( $hidden ? '_' : '' ) . $name;
134 $this->meta[ $post_id ][ $name ] = $value;
135
136 // Return non null value to escape update process.
137 return true;
138 }
139
140 /**
141 * remove
142 *
143 * Removes postmeta from storage.
144 *
145 * @date 8/10/18
146 * @since 5.8.0
147 *
148 * @param mixed $post_id The post_id for this data.
149 * @return void
150 */
151 function remove( $post_id = 0 ) {
152
153 // unset meta
154 unset( $this->meta[ $post_id ] );
155
156 // reset post_id
157 if ( $post_id === $this->post_id ) {
158 $this->post_id = 0;
159 }
160 }
161
162 /**
163 * pre_load_meta
164 *
165 * Injects the local meta.
166 *
167 * @date 8/10/18
168 * @since 5.8.0
169 *
170 * @param null $null An empty parameter. Return a non null value to short-circuit the function.
171 * @param mixed $post_id The post_id for this data.
172 * @return mixed
173 */
174 function pre_load_meta( $null, $post_id ) {
175 if ( isset( $this->meta[ $post_id ] ) ) {
176 return $this->meta[ $post_id ];
177 }
178 return $null;
179 }
180
181 /**
182 * pre_load_metadata
183 *
184 * Injects the local meta.
185 *
186 * @date 8/10/18
187 * @since 5.8.0
188 *
189 * @param null $null An empty parameter. Return a non null value to short-circuit the function.
190 * @param (int|string) $post_id The post id.
191 * @param string $name The meta name.
192 * @param boolean $hidden If the meta is hidden (starts with an underscore).
193 * @return mixed
194 */
195 function pre_load_metadata( $null, $post_id, $name, $hidden ) {
196 $name = ( $hidden ? '_' : '' ) . $name;
197 if ( isset( $this->meta[ $post_id ] ) ) {
198 if ( isset( $this->meta[ $post_id ][ $name ] ) ) {
199 return $this->meta[ $post_id ][ $name ];
200 }
201 return '__return_null';
202 }
203 return $null;
204 }
205
206 /**
207 * pre_load_post_id
208 *
209 * Injects the local post_id.
210 *
211 * @date 8/10/18
212 * @since 5.8.0
213 *
214 * @param null $null An empty parameter. Return a non null value to short-circuit the function.
215 * @param mixed $post_id The post_id for this data.
216 * @return mixed
217 */
218 function pre_load_post_id( $null, $post_id ) {
219 if ( ! $post_id && $this->post_id ) {
220 return $this->post_id;
221 }
222 return $null;
223 }
224 }
225
226 endif; // class_exists check
227
228 /**
229 * acf_setup_meta
230 *
231 * Adds postmeta to storage.
232 *
233 * @date 8/10/18
234 * @since 5.8.0
235 * @see ACF_Local_Meta::add() for list of parameters.
236 *
237 * @return array
238 */
239 function acf_setup_meta( $meta = array(), $post_id = 0, $is_main = false ) {
240 return acf_get_instance( 'ACF_Local_Meta' )->add( $meta, $post_id, $is_main );
241 }
242
243 /**
244 * acf_reset_meta
245 *
246 * Removes postmeta to storage.
247 *
248 * @date 8/10/18
249 * @since 5.8.0
250 * @see ACF_Local_Meta::remove() for list of parameters.
251 *
252 * @return void
253 */
254 function acf_reset_meta( $post_id = 0 ) {
255 return acf_get_instance( 'ACF_Local_Meta' )->remove( $post_id );
256 }
257