admin
3 months ago
field-groups
3 months ago
fields
1 week ago
fields-settings
3 months ago
forms
3 months ago
locations
3 months ago
modules
1 week ago
screens
3 months ago
acfe-deprecated-functions.php
2 years ago
acfe-field-functions.php
3 months ago
acfe-field-group-functions.php
3 months ago
acfe-file-functions.php
1 year ago
acfe-form-functions.php
3 months ago
acfe-helper-array-functions.php
3 months ago
acfe-helper-functions.php
3 months ago
acfe-helper-multi-functions.php
3 months ago
acfe-helper-string-functions.php
3 months ago
acfe-meta-functions.php
3 months ago
acfe-post-functions.php
3 months ago
acfe-screen-functions.php
3 months ago
acfe-template-functions.php
3 months ago
acfe-term-functions.php
3 months ago
acfe-user-functions.php
3 months ago
acfe-wp-functions.php
3 years ago
assets.php
3 months ago
compatibility-acf-5.8.php
4 months ago
compatibility-acf-5.9.php
3 months ago
compatibility-acf-6.5.php
3 months ago
compatibility-acf-6.8.6.php
1 week ago
compatibility.php
3 months ago
field-extend.php
4 months ago
field.php
4 months ago
hooks.php
3 months ago
init.php
3 months ago
local-meta.php
3 years ago
media.php
3 months ago
module-acf.php
3 months ago
module-db.php
3 months ago
module-l10n.php
3 months ago
module-legacy.php
3 years ago
module-manager.php
4 months ago
module-post.php
3 months ago
module-posts.php
4 months ago
module-upgrades.php
3 years ago
module.php
3 months ago
multilang.php
3 months ago
revisions.php
4 months ago
screen.php
3 months ago
settings.php
3 months ago
template-tags.php
3 months ago
third-party.php
3 years ago
upgrades.php
3 months ago
local-meta.php
317 lines
| 1 | <?php |
| 2 | |
| 3 | if(!defined('ABSPATH')){ |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | if(!class_exists('ACFE_Local_Meta')): |
| 8 | |
| 9 | class ACFE_Local_Meta{ |
| 10 | |
| 11 | // vars |
| 12 | var $meta = array(); |
| 13 | var $curr_id = array(); |
| 14 | var $main_id = array(); |
| 15 | |
| 16 | /** |
| 17 | * construct |
| 18 | */ |
| 19 | function __construct(){ |
| 20 | |
| 21 | // Filters |
| 22 | add_filter('acf/pre_load_post_id', array($this, 'pre_load_post_id'), 1, 2); |
| 23 | add_filter('acf/pre_load_meta', array($this, 'pre_load_meta'), 1, 2); |
| 24 | add_filter('acf/pre_load_metadata', array($this, 'pre_load_metadata'), 1, 4); |
| 25 | |
| 26 | } |
| 27 | |
| 28 | |
| 29 | /** |
| 30 | * add |
| 31 | * |
| 32 | * @param $meta |
| 33 | * @param $post_id |
| 34 | * @param $is_main |
| 35 | * |
| 36 | * @return array|mixed |
| 37 | */ |
| 38 | function add($meta = array(), $post_id = 0, $is_main = false){ |
| 39 | |
| 40 | // Capture meta |
| 41 | if($this->is_request($meta)){ |
| 42 | $meta = $this->capture($meta, $post_id); |
| 43 | } |
| 44 | |
| 45 | // Add to storage |
| 46 | $this->meta[$post_id] = $meta; |
| 47 | |
| 48 | // Add to current ID |
| 49 | $this->curr_id[] = $post_id; |
| 50 | |
| 51 | // Add to main ID |
| 52 | if($is_main){ |
| 53 | $this->main_id[] = $post_id; |
| 54 | } |
| 55 | |
| 56 | // Return meta. |
| 57 | return $meta; |
| 58 | |
| 59 | } |
| 60 | |
| 61 | |
| 62 | /** |
| 63 | * remove |
| 64 | */ |
| 65 | function remove(){ |
| 66 | |
| 67 | // unset meta |
| 68 | unset($this->meta[end($this->curr_id)]); |
| 69 | |
| 70 | // reset main id |
| 71 | if(end($this->curr_id) === end($this->main_id)){ |
| 72 | |
| 73 | // remove last value of main id |
| 74 | array_pop($this->main_id); |
| 75 | |
| 76 | } |
| 77 | |
| 78 | // remove last value of current id |
| 79 | array_pop($this->curr_id); |
| 80 | |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * pre_load_post_id |
| 85 | * |
| 86 | * acf/pre_load_post_id:1 |
| 87 | * |
| 88 | * @param $null |
| 89 | * @param $post_id |
| 90 | * |
| 91 | * @return false|mixed |
| 92 | */ |
| 93 | function pre_load_post_id($null, $post_id){ |
| 94 | |
| 95 | if(!$post_id && $this->main_id && end($this->curr_id) === end($this->main_id)){ |
| 96 | return end($this->main_id); |
| 97 | } |
| 98 | |
| 99 | return $null; |
| 100 | |
| 101 | } |
| 102 | |
| 103 | |
| 104 | /** |
| 105 | * is_request |
| 106 | * |
| 107 | * @param $meta |
| 108 | * |
| 109 | * @return bool |
| 110 | */ |
| 111 | function is_request($meta = array()){ |
| 112 | return acf_is_field_key(key($meta)); |
| 113 | } |
| 114 | |
| 115 | |
| 116 | /** |
| 117 | * capture |
| 118 | * |
| 119 | * @param $values |
| 120 | * @param $post_id |
| 121 | * |
| 122 | * @return array |
| 123 | */ |
| 124 | function capture($values = array(), $post_id = 0){ |
| 125 | |
| 126 | // Reset meta. |
| 127 | $this->meta[ $post_id ] = array(); |
| 128 | |
| 129 | // Listen for any added meta. |
| 130 | add_filter('acf/pre_update_metadata', array($this, 'capture_update_metadata'), 1, 5); |
| 131 | |
| 132 | // Simulate update. |
| 133 | if($values){ |
| 134 | |
| 135 | // Get hook variations |
| 136 | $hook = acf_get_store('hook-variations')->get('acf/update_value'); |
| 137 | |
| 138 | // Clone Hook |
| 139 | $_hook = $hook; |
| 140 | unset($_hook['variations'][1]); // unset name |
| 141 | unset($_hook['variations'][2]); // unset key |
| 142 | |
| 143 | // Update hook variations |
| 144 | acf_get_store('hook-variations')->set('acf/update_value', $_hook); |
| 145 | |
| 146 | // update values |
| 147 | acf_update_values($values, $post_id); |
| 148 | |
| 149 | // Reset hook variations back to default |
| 150 | acf_get_store('hook-variations')->set('acf/update_value', $hook); |
| 151 | |
| 152 | } |
| 153 | |
| 154 | // Remove listener filter. |
| 155 | remove_filter('acf/pre_update_metadata', array($this, 'capture_update_metadata'), 1); |
| 156 | |
| 157 | // Return meta. |
| 158 | return $this->meta[ $post_id ]; |
| 159 | |
| 160 | } |
| 161 | |
| 162 | |
| 163 | /** |
| 164 | * capture_update_metadata |
| 165 | * |
| 166 | * acf/pre_update_metadata:1 |
| 167 | * |
| 168 | * @param $null |
| 169 | * @param $post_id |
| 170 | * @param $name |
| 171 | * @param $value |
| 172 | * @param $hidden |
| 173 | * |
| 174 | * @return bool |
| 175 | */ |
| 176 | function capture_update_metadata($null, $post_id, $name, $value, $hidden){ |
| 177 | |
| 178 | $name = ($hidden ? '_' : '') . $name; |
| 179 | $this->meta[ $post_id ][ $name ] = $value; |
| 180 | |
| 181 | // Return non null value to escape update process. |
| 182 | return true; |
| 183 | |
| 184 | } |
| 185 | |
| 186 | |
| 187 | /** |
| 188 | * pre_load_meta |
| 189 | * |
| 190 | * acf/pre_load_meta:1 |
| 191 | * |
| 192 | * @param $null |
| 193 | * @param $post_id |
| 194 | * |
| 195 | * @return mixed |
| 196 | */ |
| 197 | function pre_load_meta($null, $post_id){ |
| 198 | |
| 199 | if(isset($this->meta[ $post_id ])){ |
| 200 | return $this->meta[ $post_id ]; |
| 201 | } |
| 202 | |
| 203 | return $null; |
| 204 | |
| 205 | } |
| 206 | |
| 207 | |
| 208 | /** |
| 209 | * pre_load_metadata |
| 210 | * |
| 211 | * acf/pre_load_metadata:1 |
| 212 | * |
| 213 | * @param $null |
| 214 | * @param $post_id |
| 215 | * @param $name |
| 216 | * @param $hidden |
| 217 | * |
| 218 | * @return mixed|string |
| 219 | */ |
| 220 | function pre_load_metadata($null, $post_id, $name, $hidden){ |
| 221 | |
| 222 | $name = ($hidden ? '_' : '') . $name; |
| 223 | |
| 224 | if(isset($this->meta[ $post_id ])){ |
| 225 | |
| 226 | if(isset($this->meta[ $post_id ][ $name ])){ |
| 227 | return $this->meta[ $post_id ][ $name ]; |
| 228 | } |
| 229 | return '__return_null'; |
| 230 | |
| 231 | } |
| 232 | |
| 233 | return $null; |
| 234 | |
| 235 | } |
| 236 | |
| 237 | } |
| 238 | |
| 239 | endif; |
| 240 | |
| 241 | |
| 242 | /** |
| 243 | * acfe_setup_meta |
| 244 | * |
| 245 | * @param $meta |
| 246 | * @param $post_id |
| 247 | * @param $is_main |
| 248 | * |
| 249 | * @return mixed |
| 250 | */ |
| 251 | function acfe_setup_meta($meta = array(), $post_id = 0, $is_main = false){ |
| 252 | return acf_get_instance('ACFE_Local_Meta')->add($meta, $post_id, $is_main); |
| 253 | } |
| 254 | |
| 255 | |
| 256 | /** |
| 257 | * acfe_reset_meta |
| 258 | * |
| 259 | * @return mixed |
| 260 | */ |
| 261 | function acfe_reset_meta(){ |
| 262 | return acf_get_instance('ACFE_Local_Meta')->remove(); |
| 263 | } |
| 264 | |
| 265 | |
| 266 | /** |
| 267 | * acfe_get_local_post_ids |
| 268 | * @return array |
| 269 | */ |
| 270 | function acfe_get_local_post_ids(){ |
| 271 | |
| 272 | $post_ids = array(); |
| 273 | |
| 274 | // ACF local meta |
| 275 | $acf_meta = acf_get_instance('ACF_Local_Meta')->meta; |
| 276 | $post_ids = array_merge($post_ids, array_keys($acf_meta)); |
| 277 | |
| 278 | // ACFE local meta |
| 279 | $acfe_meta = acf_get_instance('ACFE_Local_Meta')->meta; |
| 280 | $post_ids = array_merge($post_ids, array_keys($acfe_meta)); |
| 281 | |
| 282 | return array_unique($post_ids); |
| 283 | |
| 284 | } |
| 285 | |
| 286 | |
| 287 | /** |
| 288 | * acfe_get_local_post_id |
| 289 | * @return false|mixed |
| 290 | */ |
| 291 | function acfe_get_local_post_id(){ |
| 292 | |
| 293 | $post_ids = acfe_get_local_post_ids(); |
| 294 | return end($post_ids); |
| 295 | |
| 296 | } |
| 297 | |
| 298 | |
| 299 | /** |
| 300 | * acfe_is_local_post_id |
| 301 | * |
| 302 | * @param $post_id |
| 303 | * |
| 304 | * @return bool |
| 305 | */ |
| 306 | function acfe_is_local_post_id($post_id){ |
| 307 | return in_array($post_id, acfe_get_local_post_ids()); |
| 308 | } |
| 309 | |
| 310 | |
| 311 | /** |
| 312 | * acfe_is_local_meta |
| 313 | * @return bool |
| 314 | */ |
| 315 | function acfe_is_local_meta(){ |
| 316 | return !empty(acfe_get_local_post_ids()); |
| 317 | } |