acf-meta-functions.php
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Returns an array of "ACF only" meta for the given post_id. |
| 5 | * |
| 6 | * @date 9/10/18 |
| 7 | * @since 5.8.0 |
| 8 | * |
| 9 | * @param mixed $post_id The post_id for this data. |
| 10 | * |
| 11 | * @return array |
| 12 | */ |
| 13 | function acf_get_meta( $post_id = 0 ) { |
| 14 | |
| 15 | // Allow filter to short-circuit load_value logic. |
| 16 | $null = apply_filters( 'acf/pre_load_meta', null, $post_id ); |
| 17 | if ( $null !== null ) { |
| 18 | return ( $null === '__return_null' ) ? null : $null; |
| 19 | } |
| 20 | |
| 21 | // Decode $post_id for $type and $id. |
| 22 | $decoded = acf_decode_post_id( $post_id ); |
| 23 | |
| 24 | /** |
| 25 | * Determine CRUD function. |
| 26 | * |
| 27 | * - Relies on decoded post_id result to identify option or meta types. |
| 28 | * - Uses xxx_metadata(type) instead of xxx_type_meta() to bypass additional logic that could alter the ID. |
| 29 | */ |
| 30 | if ( $decoded['type'] === 'option' ) { |
| 31 | $allmeta = acf_get_option_meta( $decoded['id'] ); |
| 32 | } else { |
| 33 | $allmeta = get_metadata( $decoded['type'], $decoded['id'], '' ); |
| 34 | } |
| 35 | |
| 36 | // Loop over meta and check that a reference exists for each value. |
| 37 | $meta = array(); |
| 38 | if ( $allmeta ) { |
| 39 | foreach ( $allmeta as $key => $value ) { |
| 40 | |
| 41 | // If a reference exists for this value, add it to the meta array. |
| 42 | if ( isset( $allmeta[ "_$key" ] ) ) { |
| 43 | $meta[ $key ] = $allmeta[ $key ][0]; |
| 44 | $meta[ "_$key" ] = $allmeta[ "_$key" ][0]; |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | // Unserialized results (get_metadata does not unserialize if $key is empty). |
| 50 | $meta = array_map( 'maybe_unserialize', $meta ); |
| 51 | |
| 52 | /** |
| 53 | * Filters the $meta array after it has been loaded. |
| 54 | * |
| 55 | * @date 25/1/19 |
| 56 | * @since 5.7.11 |
| 57 | * |
| 58 | * @param array $meta The array of loaded meta. |
| 59 | * @param string $post_id The $post_id for this meta. |
| 60 | */ |
| 61 | return apply_filters( 'acf/load_meta', $meta, $post_id ); |
| 62 | } |
| 63 | |
| 64 | |
| 65 | /** |
| 66 | * acf_get_option_meta |
| 67 | * |
| 68 | * Returns an array of meta for the given wp_option name prefix in the same format as get_post_meta(). |
| 69 | * |
| 70 | * @date 9/10/18 |
| 71 | * @since 5.8.0 |
| 72 | * |
| 73 | * @param string $prefix The wp_option name prefix. |
| 74 | * @return array |
| 75 | */ |
| 76 | function acf_get_option_meta( $prefix = '' ) { |
| 77 | |
| 78 | // Globals. |
| 79 | global $wpdb; |
| 80 | |
| 81 | // Vars. |
| 82 | $meta = array(); |
| 83 | $search = "{$prefix}_%"; |
| 84 | $_search = "_{$prefix}_%"; |
| 85 | |
| 86 | // Escape underscores for LIKE. |
| 87 | $search = str_replace( '_', '\_', $search ); |
| 88 | $_search = str_replace( '_', '\_', $_search ); |
| 89 | |
| 90 | // Query database for results. |
| 91 | $rows = $wpdb->get_results( |
| 92 | $wpdb->prepare( |
| 93 | "SELECT * |
| 94 | FROM $wpdb->options |
| 95 | WHERE option_name LIKE %s |
| 96 | OR option_name LIKE %s", |
| 97 | $search, |
| 98 | $_search |
| 99 | ), |
| 100 | ARRAY_A |
| 101 | ); |
| 102 | |
| 103 | // Loop over results and append meta (removing the $prefix from the option name). |
| 104 | $len = strlen( "{$prefix}_" ); |
| 105 | foreach ( $rows as $row ) { |
| 106 | $meta[ substr( $row['option_name'], $len ) ][] = $row['option_value']; |
| 107 | } |
| 108 | |
| 109 | // Return results. |
| 110 | return $meta; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Retrieves specific metadata from the database. |
| 115 | * |
| 116 | * @date 16/10/2015 |
| 117 | * @since 5.2.3 |
| 118 | * |
| 119 | * @param int|string $post_id The post id. |
| 120 | * @param string $name The meta name. |
| 121 | * @param bool $hidden If the meta is hidden (starts with an underscore). |
| 122 | * |
| 123 | * @return mixed |
| 124 | */ |
| 125 | function acf_get_metadata( $post_id = 0, $name = '', $hidden = false ) { |
| 126 | // Allow filter to short-circuit logic. |
| 127 | $null = apply_filters( 'acf/pre_load_metadata', null, $post_id, $name, $hidden ); |
| 128 | if ( $null !== null ) { |
| 129 | return ( $null === '__return_null' ) ? null : $null; |
| 130 | } |
| 131 | |
| 132 | // Decode $post_id for $type and $id. |
| 133 | $decoded = acf_decode_post_id( $post_id ); |
| 134 | $id = $decoded['id']; |
| 135 | $type = $decoded['type']; |
| 136 | |
| 137 | // Hidden meta uses an underscore prefix. |
| 138 | $prefix = $hidden ? '_' : ''; |
| 139 | |
| 140 | // Bail early if no $id (possible during new acf_form). |
| 141 | if ( ! $id ) { |
| 142 | return null; |
| 143 | } |
| 144 | |
| 145 | // Determine CRUD function. |
| 146 | // - Relies on decoded post_id result to identify option or meta types. |
| 147 | // - Uses xxx_metadata(type) instead of xxx_type_meta() to bypass additional logic that could alter the ID. |
| 148 | if ( $type === 'option' ) { |
| 149 | return get_option( "{$prefix}{$id}_{$name}", null ); |
| 150 | } else { |
| 151 | $meta = get_metadata( $type, $id, "{$prefix}{$name}", false ); |
| 152 | return isset( $meta[0] ) ? $meta[0] : null; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Updates metadata in the database. |
| 158 | * |
| 159 | * @date 16/10/2015 |
| 160 | * @since 5.2.3 |
| 161 | * |
| 162 | * @param int|string $post_id The post id. |
| 163 | * @param string $name The meta name. |
| 164 | * @param mixed $value The meta value. |
| 165 | * @param bool $hidden If the meta is hidden (starts with an underscore). |
| 166 | * |
| 167 | * @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure. |
| 168 | */ |
| 169 | function acf_update_metadata( $post_id = 0, $name = '', $value = '', $hidden = false ) { |
| 170 | // Allow filter to short-circuit logic. |
| 171 | $pre = apply_filters( 'acf/pre_update_metadata', null, $post_id, $name, $value, $hidden ); |
| 172 | if ( $pre !== null ) { |
| 173 | return $pre; |
| 174 | } |
| 175 | |
| 176 | // Decode $post_id for $type and $id. |
| 177 | $decoded = acf_decode_post_id( $post_id ); |
| 178 | $id = $decoded['id']; |
| 179 | $type = $decoded['type']; |
| 180 | |
| 181 | // Hidden meta uses an underscore prefix. |
| 182 | $prefix = $hidden ? '_' : ''; |
| 183 | |
| 184 | // Bail early if no $id (possible during new acf_form). |
| 185 | if ( ! $id ) { |
| 186 | return false; |
| 187 | } |
| 188 | |
| 189 | // Determine CRUD function. |
| 190 | // - Relies on decoded post_id result to identify option or meta types. |
| 191 | // - Uses xxx_metadata(type) instead of xxx_type_meta() to bypass additional logic that could alter the ID. |
| 192 | if ( $type === 'option' ) { |
| 193 | $value = wp_unslash( $value ); |
| 194 | $autoload = (bool) acf_get_setting( 'autoload' ); |
| 195 | return update_option( "{$prefix}{$id}_{$name}", $value, $autoload ); |
| 196 | } else { |
| 197 | return update_metadata( $type, $id, "{$prefix}{$name}", $value ); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Deletes metadata from the database. |
| 203 | * |
| 204 | * @date 16/10/2015 |
| 205 | * @since 5.2.3 |
| 206 | * |
| 207 | * @param int|string $post_id The post id. |
| 208 | * @param string $name The meta name. |
| 209 | * @param bool $hidden If the meta is hidden (starts with an underscore). |
| 210 | * |
| 211 | * @return bool |
| 212 | */ |
| 213 | function acf_delete_metadata( $post_id = 0, $name = '', $hidden = false ) { |
| 214 | // Allow filter to short-circuit logic. |
| 215 | $pre = apply_filters( 'acf/pre_delete_metadata', null, $post_id, $name, $hidden ); |
| 216 | if ( $pre !== null ) { |
| 217 | return $pre; |
| 218 | } |
| 219 | |
| 220 | // Decode $post_id for $type and $id. |
| 221 | $decoded = acf_decode_post_id( $post_id ); |
| 222 | $id = $decoded['id']; |
| 223 | $type = $decoded['type']; |
| 224 | |
| 225 | // Hidden meta uses an underscore prefix. |
| 226 | $prefix = $hidden ? '_' : ''; |
| 227 | |
| 228 | // Bail early if no $id (possible during new acf_form). |
| 229 | if ( ! $id ) { |
| 230 | return false; |
| 231 | } |
| 232 | |
| 233 | // Determine CRUD function. |
| 234 | // - Relies on decoded post_id result to identify option or meta types. |
| 235 | // - Uses xxx_metadata(type) instead of xxx_type_meta() to bypass additional logic that could alter the ID. |
| 236 | if ( $type === 'option' ) { |
| 237 | return delete_option( "{$prefix}{$id}_{$name}" ); |
| 238 | } else { |
| 239 | return delete_metadata( $type, $id, "{$prefix}{$name}" ); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * acf_copy_postmeta |
| 245 | * |
| 246 | * Copies meta from one post to another. Useful for saving and restoring revisions. |
| 247 | * |
| 248 | * @date 25/06/2016 |
| 249 | * @since 5.3.8 |
| 250 | * |
| 251 | * @param (int|string) $from_post_id The post id to copy from. |
| 252 | * @param (int|string) $to_post_id The post id to paste to. |
| 253 | * @return void |
| 254 | */ |
| 255 | function acf_copy_metadata( $from_post_id = 0, $to_post_id = 0 ) { |
| 256 | |
| 257 | // Get all postmeta. |
| 258 | $meta = acf_get_meta( $from_post_id ); |
| 259 | |
| 260 | // Check meta. |
| 261 | if ( $meta ) { |
| 262 | |
| 263 | // Slash data. WP expects all data to be slashed and will unslash it (fixes '\' character issues). |
| 264 | $meta = wp_slash( $meta ); |
| 265 | |
| 266 | // Loop over meta. |
| 267 | foreach ( $meta as $name => $value ) { |
| 268 | acf_update_metadata( $to_post_id, $name, $value ); |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * acf_copy_postmeta |
| 275 | * |
| 276 | * Copies meta from one post to another. Useful for saving and restoring revisions. |
| 277 | * |
| 278 | * @date 25/06/2016 |
| 279 | * @since 5.3.8 |
| 280 | * @deprecated 5.7.11 |
| 281 | * |
| 282 | * @param int $from_post_id The post id to copy from. |
| 283 | * @param int $to_post_id The post id to paste to. |
| 284 | * @return void |
| 285 | */ |
| 286 | function acf_copy_postmeta( $from_post_id = 0, $to_post_id = 0 ) { |
| 287 | return acf_copy_metadata( $from_post_id, $to_post_id ); |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * acf_get_meta_field |
| 292 | * |
| 293 | * Returns a field using the provided $id and $post_id parameters. |
| 294 | * Looks for a reference to help loading the correct field via name. |
| 295 | * |
| 296 | * @date 21/1/19 |
| 297 | * @since 5.7.10 |
| 298 | * |
| 299 | * @param string $key The meta name (field name). |
| 300 | * @param (int|string) $post_id The post_id where this field's value is saved. |
| 301 | * @return (array|false) The field array. |
| 302 | */ |
| 303 | function acf_get_meta_field( $key = 0, $post_id = 0 ) { |
| 304 | |
| 305 | // Try reference. |
| 306 | $field_key = acf_get_reference( $key, $post_id ); |
| 307 | |
| 308 | if ( $field_key ) { |
| 309 | $field = acf_get_field( $field_key ); |
| 310 | if ( $field ) { |
| 311 | $field['name'] = $key; |
| 312 | return $field; |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | // Return false. |
| 317 | return false; |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * acf_get_metaref |
| 322 | * |
| 323 | * Retrieves reference metadata from the database. |
| 324 | * |
| 325 | * @date 16/10/2015 |
| 326 | * @since 5.2.3 |
| 327 | * |
| 328 | * @param (int|string) $post_id The post id. |
| 329 | * @param string type The reference type (fields|groups). |
| 330 | * @param string $name An optional specific name |
| 331 | * @return mixed |
| 332 | */ |
| 333 | function acf_get_metaref( $post_id = 0, $type = 'fields', $name = '' ) { |
| 334 | |
| 335 | // Load existing meta. |
| 336 | $meta = acf_get_metadata( $post_id, "_acf_$type" ); |
| 337 | |
| 338 | // Handle no meta. |
| 339 | if ( ! $meta ) { |
| 340 | return $name ? '' : array(); |
| 341 | } |
| 342 | |
| 343 | // Return specific reference. |
| 344 | if ( $name ) { |
| 345 | return isset( $meta[ $name ] ) ? $meta[ $name ] : ''; |
| 346 | |
| 347 | // Or return all references. |
| 348 | } else { |
| 349 | return $meta; |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | /** |
| 354 | * acf_update_metaref |
| 355 | * |
| 356 | * Updates reference metadata in the database. |
| 357 | * |
| 358 | * @date 16/10/2015 |
| 359 | * @since 5.2.3 |
| 360 | * |
| 361 | * @param (int|string) $post_id The post id. |
| 362 | * @param string type The reference type (fields|groups). |
| 363 | * @param array $references An array of references. |
| 364 | * @return (int|bool) Meta ID if the key didn't exist, true on successful update, false on failure. |
| 365 | */ |
| 366 | function acf_update_metaref( $post_id = 0, $type = 'fields', $references = array() ) { |
| 367 | |
| 368 | // Get current references. |
| 369 | $current = acf_get_metaref( $post_id, $type ); |
| 370 | |
| 371 | // Merge in new references. |
| 372 | $references = array_merge( $current, $references ); |
| 373 | |
| 374 | // Simplify groups |
| 375 | if ( $type === 'groups' ) { |
| 376 | $references = array_values( $references ); |
| 377 | } |
| 378 | |
| 379 | // Remove duplicate references. |
| 380 | $references = array_unique( $references ); |
| 381 | |
| 382 | // Update metadata. |
| 383 | return acf_update_metadata( $post_id, "_acf_$type", $references ); |
| 384 | } |
| 385 |