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