169 * @param int $object_id ID of the object metadata is for. |
171 * @param int $object_id ID of the object metadata is for. |
170 * @param string $meta_key Metadata key. |
172 * @param string $meta_key Metadata key. |
171 * @param mixed $meta_value Metadata value. Must be serializable if non-scalar. |
173 * @param mixed $meta_value Metadata value. Must be serializable if non-scalar. |
172 * @param mixed $prev_value Optional. Previous value to check before updating. |
174 * @param mixed $prev_value Optional. Previous value to check before updating. |
173 * If specified, only update existing metadata entries with |
175 * If specified, only update existing metadata entries with |
174 * this value. Otherwise, update all entries. Default empty. |
176 * this value. Otherwise, update all entries. Default empty string. |
175 * @return int|bool The new meta field ID if a field with the given key didn't exist |
177 * @return int|bool The new meta field ID if a field with the given key didn't exist |
176 * and was therefore added, true on successful update, |
178 * and was therefore added, true on successful update, |
177 * false on failure or if the value passed to the function |
179 * false on failure or if the value passed to the function |
178 * is the same as the one that is already in the database. |
180 * is the same as the one that is already in the database. |
179 */ |
181 */ |
367 * If specified, only delete metadata entries with this value. |
369 * If specified, only delete metadata entries with this value. |
368 * Otherwise, delete all entries with the specified meta_key. |
370 * Otherwise, delete all entries with the specified meta_key. |
369 * Pass `null`, `false`, or an empty string to skip this check. |
371 * Pass `null`, `false`, or an empty string to skip this check. |
370 * (For backward compatibility, it is not possible to pass an empty string |
372 * (For backward compatibility, it is not possible to pass an empty string |
371 * to delete those entries with an empty string for a value.) |
373 * to delete those entries with an empty string for a value.) |
|
374 * Default empty string. |
372 * @param bool $delete_all Optional. If true, delete matching metadata entries for all objects, |
375 * @param bool $delete_all Optional. If true, delete matching metadata entries for all objects, |
373 * ignoring the specified object_id. Otherwise, only delete |
376 * ignoring the specified object_id. Otherwise, only delete |
374 * matching metadata entries for the specified object_id. Default false. |
377 * matching metadata entries for the specified object_id. Default false. |
375 * @return bool True on successful delete, false on failure. |
378 * @return bool True on successful delete, false on failure. |
376 */ |
379 */ |
556 * |
559 * |
557 * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', |
560 * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', |
558 * or any other object type with an associated meta table. |
561 * or any other object type with an associated meta table. |
559 * @param int $object_id ID of the object metadata is for. |
562 * @param int $object_id ID of the object metadata is for. |
560 * @param string $meta_key Optional. Metadata key. If not specified, retrieve all metadata for |
563 * @param string $meta_key Optional. Metadata key. If not specified, retrieve all metadata for |
561 * the specified object. Default empty. |
564 * the specified object. Default empty string. |
562 * @param bool $single Optional. If true, return only the first value of the specified `$meta_key`. |
565 * @param bool $single Optional. If true, return only the first value of the specified `$meta_key`. |
563 * This parameter has no effect if `$meta_key` is not specified. Default false. |
566 * This parameter has no effect if `$meta_key` is not specified. Default false. |
564 * @return mixed An array of values if `$single` is false. |
567 * @return mixed An array of values if `$single` is false. |
565 * The value of the meta field if `$single` is true. |
568 * The value of the meta field if `$single` is true. |
566 * False for an invalid `$object_id` (non-numeric, zero, or negative value), |
569 * False for an invalid `$object_id` (non-numeric, zero, or negative value), |
583 * |
586 * |
584 * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', |
587 * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', |
585 * or any other object type with an associated meta table. |
588 * or any other object type with an associated meta table. |
586 * @param int $object_id ID of the object metadata is for. |
589 * @param int $object_id ID of the object metadata is for. |
587 * @param string $meta_key Optional. Metadata key. If not specified, retrieve all metadata for |
590 * @param string $meta_key Optional. Metadata key. If not specified, retrieve all metadata for |
588 * the specified object. Default empty. |
591 * the specified object. Default empty string. |
589 * @param bool $single Optional. If true, return only the first value of the specified `$meta_key`. |
592 * @param bool $single Optional. If true, return only the first value of the specified `$meta_key`. |
590 * This parameter has no effect if `$meta_key` is not specified. Default false. |
593 * This parameter has no effect if `$meta_key` is not specified. Default false. |
591 * @return mixed An array of values if `$single` is false. |
594 * @return mixed An array of values if `$single` is false. |
592 * The value of the meta field if `$single` is true. |
595 * The value of the meta field if `$single` is true. |
593 * False for an invalid `$object_id` (non-numeric, zero, or negative value), |
596 * False for an invalid `$object_id` (non-numeric, zero, or negative value), |
905 $meta = get_metadata_by_mid( $meta_type, $meta_id ); |
908 $meta = get_metadata_by_mid( $meta_type, $meta_id ); |
906 if ( $meta ) { |
909 if ( $meta ) { |
907 $original_key = $meta->meta_key; |
910 $original_key = $meta->meta_key; |
908 $object_id = $meta->{$column}; |
911 $object_id = $meta->{$column}; |
909 |
912 |
910 // If a new meta_key (last parameter) was specified, change the meta key, |
913 /* |
911 // otherwise use the original key in the update statement. |
914 * If a new meta_key (last parameter) was specified, change the meta key, |
|
915 * otherwise use the original key in the update statement. |
|
916 */ |
912 if ( false === $meta_key ) { |
917 if ( false === $meta_key ) { |
913 $meta_key = $original_key; |
918 $meta_key = $original_key; |
914 } elseif ( ! is_string( $meta_key ) ) { |
919 } elseif ( ! is_string( $meta_key ) ) { |
915 return false; |
920 return false; |
916 } |
921 } |
1228 * |
1233 * |
1229 * @param array $meta_query A meta query. |
1234 * @param array $meta_query A meta query. |
1230 * @param string $type Type of meta. |
1235 * @param string $type Type of meta. |
1231 * @param string $primary_table Primary database table name. |
1236 * @param string $primary_table Primary database table name. |
1232 * @param string $primary_id_column Primary ID column name. |
1237 * @param string $primary_id_column Primary ID column name. |
1233 * @param object $context Optional. The main query object |
1238 * @param object $context Optional. The main query object. Default null. |
1234 * @return array Associative array of `JOIN` and `WHERE` SQL. |
1239 * @return string[]|false { |
|
1240 * Array containing JOIN and WHERE SQL clauses to append to the main query, |
|
1241 * or false if no table exists for the requested meta type. |
|
1242 * |
|
1243 * @type string $join SQL fragment to append to the main JOIN clause. |
|
1244 * @type string $where SQL fragment to append to the main WHERE clause. |
|
1245 * } |
1235 */ |
1246 */ |
1236 function get_meta_sql( $meta_query, $type, $primary_table, $primary_id_column, $context = null ) { |
1247 function get_meta_sql( $meta_query, $type, $primary_table, $primary_id_column, $context = null ) { |
1237 $meta_query_obj = new WP_Meta_Query( $meta_query ); |
1248 $meta_query_obj = new WP_Meta_Query( $meta_query ); |
1238 return $meta_query_obj->get_sql( $type, $primary_table, $primary_id_column, $context ); |
1249 return $meta_query_obj->get_sql( $type, $primary_table, $primary_id_column, $context ); |
1239 } |
1250 } |
1266 * |
1277 * |
1267 * @since 3.1.3 |
1278 * @since 3.1.3 |
1268 * |
1279 * |
1269 * @param string $meta_key Metadata key. |
1280 * @param string $meta_key Metadata key. |
1270 * @param string $meta_type Optional. Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', |
1281 * @param string $meta_type Optional. Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', |
1271 * or any other object type with an associated meta table. Default empty. |
1282 * or any other object type with an associated meta table. Default empty string. |
1272 * @return bool Whether the meta key is considered protected. |
1283 * @return bool Whether the meta key is considered protected. |
1273 */ |
1284 */ |
1274 function is_protected_meta( $meta_key, $meta_type = '' ) { |
1285 function is_protected_meta( $meta_key, $meta_type = '' ) { |
1275 $sanitized_key = preg_replace( "/[^\x20-\x7E\p{L}]/", '', $meta_key ); |
1286 $sanitized_key = preg_replace( "/[^\x20-\x7E\p{L}]/", '', $meta_key ); |
1276 $protected = strlen( $sanitized_key ) > 0 && ( '_' === $sanitized_key[0] ); |
1287 $protected = strlen( $sanitized_key ) > 0 && ( '_' === $sanitized_key[0] ); |
1296 * |
1307 * |
1297 * @param string $meta_key Metadata key. |
1308 * @param string $meta_key Metadata key. |
1298 * @param mixed $meta_value Metadata value to sanitize. |
1309 * @param mixed $meta_value Metadata value to sanitize. |
1299 * @param string $object_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', |
1310 * @param string $object_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', |
1300 * or any other object type with an associated meta table. |
1311 * or any other object type with an associated meta table. |
1301 * @param string $object_subtype Optional. The subtype of the object type. |
1312 * @param string $object_subtype Optional. The subtype of the object type. Default empty string. |
1302 * @return mixed Sanitized $meta_value. |
1313 * @return mixed Sanitized $meta_value. |
1303 */ |
1314 */ |
1304 function sanitize_meta( $meta_key, $meta_value, $object_type, $object_subtype = '' ) { |
1315 function sanitize_meta( $meta_key, $meta_value, $object_type, $object_subtype = '' ) { |
1305 if ( ! empty( $object_subtype ) && has_filter( "sanitize_{$object_type}_meta_{$meta_key}_for_{$object_subtype}" ) ) { |
1316 if ( ! empty( $object_subtype ) && has_filter( "sanitize_{$object_type}_meta_{$meta_key}_for_{$object_subtype}" ) ) { |
1306 |
1317 |
1354 * to support an array of data to attach to registered meta keys}. Previous arguments for |
1365 * to support an array of data to attach to registered meta keys}. Previous arguments for |
1355 * `$sanitize_callback` and `$auth_callback` have been folded into this array. |
1366 * `$sanitize_callback` and `$auth_callback` have been folded into this array. |
1356 * @since 4.9.8 The `$object_subtype` argument was added to the arguments array. |
1367 * @since 4.9.8 The `$object_subtype` argument was added to the arguments array. |
1357 * @since 5.3.0 Valid meta types expanded to include "array" and "object". |
1368 * @since 5.3.0 Valid meta types expanded to include "array" and "object". |
1358 * @since 5.5.0 The `$default` argument was added to the arguments array. |
1369 * @since 5.5.0 The `$default` argument was added to the arguments array. |
|
1370 * @since 6.4.0 The `$revisions_enabled` argument was added to the arguments array. |
1359 * |
1371 * |
1360 * @param string $object_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', |
1372 * @param string $object_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', |
1361 * or any other object type with an associated meta table. |
1373 * or any other object type with an associated meta table. |
1362 * @param string $meta_key Meta key to register. |
1374 * @param string $meta_key Meta key to register. |
1363 * @param array $args { |
1375 * @param array $args { |
1379 * @type bool|array $show_in_rest Whether data associated with this meta key can be considered public and |
1391 * @type bool|array $show_in_rest Whether data associated with this meta key can be considered public and |
1380 * should be accessible via the REST API. A custom post type must also declare |
1392 * should be accessible via the REST API. A custom post type must also declare |
1381 * support for custom fields for registered meta to be accessible via REST. |
1393 * support for custom fields for registered meta to be accessible via REST. |
1382 * When registering complex meta values this argument may optionally be an |
1394 * When registering complex meta values this argument may optionally be an |
1383 * array with 'schema' or 'prepare_callback' keys instead of a boolean. |
1395 * array with 'schema' or 'prepare_callback' keys instead of a boolean. |
|
1396 * @type bool $revisions_enabled Whether to enable revisions support for this meta_key. Can only be used when the |
|
1397 * object type is 'post'. |
1384 * } |
1398 * } |
1385 * @param string|array $deprecated Deprecated. Use `$args` instead. |
1399 * @param string|array $deprecated Deprecated. Use `$args` instead. |
1386 * @return bool True if the meta key was successfully registered in the global array, false if not. |
1400 * @return bool True if the meta key was successfully registered in the global array, false if not. |
1387 * Registering a meta key with distinct sanitize and auth callbacks will fire those callbacks, |
1401 * Registering a meta key with distinct sanitize and auth callbacks will fire those callbacks, |
1388 * but will not add to the global registry. |
1402 * but will not add to the global registry. |
1401 'default' => '', |
1415 'default' => '', |
1402 'single' => false, |
1416 'single' => false, |
1403 'sanitize_callback' => null, |
1417 'sanitize_callback' => null, |
1404 'auth_callback' => null, |
1418 'auth_callback' => null, |
1405 'show_in_rest' => false, |
1419 'show_in_rest' => false, |
|
1420 'revisions_enabled' => false, |
1406 ); |
1421 ); |
1407 |
1422 |
1408 // There used to be individual args for sanitize and auth callbacks. |
1423 // There used to be individual args for sanitize and auth callbacks. |
1409 $has_old_sanitize_cb = false; |
1424 $has_old_sanitize_cb = false; |
1410 $has_old_auth_cb = false; |
1425 $has_old_auth_cb = false; |
1447 return false; |
1462 return false; |
1448 } |
1463 } |
1449 } |
1464 } |
1450 |
1465 |
1451 $object_subtype = ! empty( $args['object_subtype'] ) ? $args['object_subtype'] : ''; |
1466 $object_subtype = ! empty( $args['object_subtype'] ) ? $args['object_subtype'] : ''; |
|
1467 if ( $args['revisions_enabled'] ) { |
|
1468 if ( 'post' !== $object_type ) { |
|
1469 _doing_it_wrong( __FUNCTION__, __( 'Meta keys cannot enable revisions support unless the object type supports revisions.' ), '6.4.0' ); |
|
1470 |
|
1471 return false; |
|
1472 } elseif ( ! empty( $object_subtype ) && ! post_type_supports( $object_subtype, 'revisions' ) ) { |
|
1473 _doing_it_wrong( __FUNCTION__, __( 'Meta keys cannot enable revisions support unless the object subtype supports revisions.' ), '6.4.0' ); |
|
1474 |
|
1475 return false; |
|
1476 } |
|
1477 } |
1452 |
1478 |
1453 // If `auth_callback` is not provided, fall back to `is_protected_meta()`. |
1479 // If `auth_callback` is not provided, fall back to `is_protected_meta()`. |
1454 if ( empty( $args['auth_callback'] ) ) { |
1480 if ( empty( $args['auth_callback'] ) ) { |
1455 if ( is_protected_meta( $meta_key, $object_type ) ) { |
1481 if ( is_protected_meta( $meta_key, $object_type ) ) { |
1456 $args['auth_callback'] = '__return_false'; |
1482 $args['auth_callback'] = '__return_false'; |
1572 * @since 4.9.8 The `$object_subtype` parameter was added. |
1598 * @since 4.9.8 The `$object_subtype` parameter was added. |
1573 * |
1599 * |
1574 * @param string $object_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', |
1600 * @param string $object_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', |
1575 * or any other object type with an associated meta table. |
1601 * or any other object type with an associated meta table. |
1576 * @param string $meta_key Metadata key. |
1602 * @param string $meta_key Metadata key. |
1577 * @param string $object_subtype Optional. The subtype of the object type. |
1603 * @param string $object_subtype Optional. The subtype of the object type. Default empty string. |
1578 * @return bool True if the meta key is registered to the object type and, if provided, |
1604 * @return bool True if the meta key is registered to the object type and, if provided, |
1579 * the object subtype. False if not. |
1605 * the object subtype. False if not. |
1580 */ |
1606 */ |
1581 function registered_meta_key_exists( $object_type, $meta_key, $object_subtype = '' ) { |
1607 function registered_meta_key_exists( $object_type, $meta_key, $object_subtype = '' ) { |
1582 $meta_keys = get_registered_meta_keys( $object_type, $object_subtype ); |
1608 $meta_keys = get_registered_meta_keys( $object_type, $object_subtype ); |
1591 * @since 4.9.8 The `$object_subtype` parameter was added. |
1617 * @since 4.9.8 The `$object_subtype` parameter was added. |
1592 * |
1618 * |
1593 * @param string $object_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', |
1619 * @param string $object_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', |
1594 * or any other object type with an associated meta table. |
1620 * or any other object type with an associated meta table. |
1595 * @param string $meta_key Metadata key. |
1621 * @param string $meta_key Metadata key. |
1596 * @param string $object_subtype Optional. The subtype of the object type. |
1622 * @param string $object_subtype Optional. The subtype of the object type. Default empty string. |
1597 * @return bool True if successful. False if the meta key was not registered. |
1623 * @return bool True if successful. False if the meta key was not registered. |
1598 */ |
1624 */ |
1599 function unregister_meta_key( $object_type, $meta_key, $object_subtype = '' ) { |
1625 function unregister_meta_key( $object_type, $meta_key, $object_subtype = '' ) { |
1600 global $wp_meta_keys; |
1626 global $wp_meta_keys; |
1601 |
1627 |
1633 |
1659 |
1634 return true; |
1660 return true; |
1635 } |
1661 } |
1636 |
1662 |
1637 /** |
1663 /** |
1638 * Retrieves a list of registered meta keys for an object type. |
1664 * Retrieves a list of registered metadata args for an object type, keyed by their meta keys. |
1639 * |
1665 * |
1640 * @since 4.6.0 |
1666 * @since 4.6.0 |
1641 * @since 4.9.8 The `$object_subtype` parameter was added. |
1667 * @since 4.9.8 The `$object_subtype` parameter was added. |
1642 * |
1668 * |
1643 * @param string $object_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', |
1669 * @param string $object_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', |
1644 * or any other object type with an associated meta table. |
1670 * or any other object type with an associated meta table. |
1645 * @param string $object_subtype Optional. The subtype of the object type. |
1671 * @param string $object_subtype Optional. The subtype of the object type. Default empty string. |
1646 * @return string[] List of registered meta keys. |
1672 * @return array[] List of registered metadata args, keyed by their meta keys. |
1647 */ |
1673 */ |
1648 function get_registered_meta_keys( $object_type, $object_subtype = '' ) { |
1674 function get_registered_meta_keys( $object_type, $object_subtype = '' ) { |
1649 global $wp_meta_keys; |
1675 global $wp_meta_keys; |
1650 |
1676 |
1651 if ( ! is_array( $wp_meta_keys ) || ! isset( $wp_meta_keys[ $object_type ] ) || ! isset( $wp_meta_keys[ $object_type ][ $object_subtype ] ) ) { |
1677 if ( ! is_array( $wp_meta_keys ) || ! isset( $wp_meta_keys[ $object_type ] ) || ! isset( $wp_meta_keys[ $object_type ][ $object_subtype ] ) ) { |