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