Skip to content

Commit

Permalink
Options/Meta APIs: Document type juggling of meta data.
Browse files Browse the repository at this point in the history
Document that unserialised data types are stored as strings in the database and returned as such by the meta data functions. For example, setting meta data to the integer value `1` will be returned as `"1"` when subsequently queried via `get_metadata()` and the related functions.

Props sukhendu2002, azaozz, jrf, rodrigosprimo.
Fixes ticket:61950.




git-svn-id: https://develop.svn.wordpress.org/trunk@59657 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
peterwilsoncc committed Jan 17, 2025
1 parent ab4b4eb commit bc3fdb4
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 6 deletions.
13 changes: 12 additions & 1 deletion src/wp-includes/comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,13 @@ function get_comment_count( $post_id = 0 ) {
*
* @param int $comment_id Comment ID.
* @param string $meta_key Metadata name.
* @param mixed $meta_value Metadata value. Must be serializable if non-scalar.
* @param mixed $meta_value Metadata value. Arrays and objects are stored as serialized data and
* will be returned as the same type when retrieved. Other data types will
* be stored as strings in the database:
* - false is stored and retrieved as an empty string ('')
* - true is stored and retrieved as '1'
* - numbers (both integer and float) are stored and retrieved as strings
* Must be serializable if non-scalar.
* @param bool $unique Optional. Whether the same key should not be added.
* Default false.
* @return int|false Meta ID on success, false on failure.
Expand Down Expand Up @@ -481,6 +487,11 @@ function delete_comment_meta( $comment_id, $meta_key, $meta_value = '' ) {
* False for an invalid `$comment_id` (non-numeric, zero, or negative value).
* An empty array if a valid but non-existing comment ID is passed and `$single` is false.
* An empty string if a valid but non-existing comment ID is passed and `$single` is true.
* Note: Non-serialized values are returned as strings:
* - false values are returned as empty strings ('')
* - true values are returned as '1'
* - numbers are returned as strings
* Arrays and objects retain their original type.
*/
function get_comment_meta( $comment_id, $key = '', $single = false ) {
return get_metadata( 'comment', $comment_id, $key, $single );
Expand Down
13 changes: 12 additions & 1 deletion src/wp-includes/meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@
* or any other object type with an associated meta table.
* @param int $object_id ID of the object metadata is for.
* @param string $meta_key Metadata key.
* @param mixed $meta_value Metadata value. Must be serializable if non-scalar.
* @param mixed $meta_value Metadata value. Arrays and objects are stored as serialized data and
* will be returned as the same type when retrieved. Other data types will
* be stored as strings in the database:
* - false is stored and retrieved as an empty string ('')
* - true is stored and retrieved as '1'
* - numbers (both integer and float) are stored and retrieved as strings
* Must be serializable if non-scalar.
* @param bool $unique Optional. Whether the specified metadata key should be unique for the object.
* If true, and the object already has a value for the specified metadata key,
* no change will be made. Default false.
Expand Down Expand Up @@ -570,6 +576,11 @@ function delete_metadata( $meta_type, $object_id, $meta_key, $meta_value = '', $
* or if `$meta_type` is not specified.
* An empty array if a valid but non-existing object ID is passed and `$single` is false.
* An empty string if a valid but non-existing object ID is passed and `$single` is true.
* Note: Non-serialized values are returned as strings:
* - false values are returned as empty strings ('')
* - true values are returned as '1'
* - numbers (both integer and float) are returned as strings
* Arrays and objects retain their original type.
*/
function get_metadata( $meta_type, $object_id, $meta_key = '', $single = false ) {
$value = get_metadata_raw( $meta_type, $object_id, $meta_key, $single );
Expand Down
13 changes: 12 additions & 1 deletion src/wp-includes/ms-site.php
Original file line number Diff line number Diff line change
Expand Up @@ -1026,7 +1026,13 @@ function clean_blog_cache( $blog ) {
*
* @param int $site_id Site ID.
* @param string $meta_key Metadata name.
* @param mixed $meta_value Metadata value. Must be serializable if non-scalar.
* @param mixed $meta_value Metadata value. Arrays and objects are stored as serialized data and
* will be returned as the same type when retrieved. Other data types will
* be stored as strings in the database:
* - false is stored and retrieved as an empty string ('')
* - true is stored and retrieved as '1'
* - numbers (both integer and float) are stored and retrieved as strings
* Must be serializable if non-scalar.
* @param bool $unique Optional. Whether the same key should not be added.
* Default false.
* @return int|false Meta ID on success, false on failure.
Expand Down Expand Up @@ -1071,6 +1077,11 @@ function delete_site_meta( $site_id, $meta_key, $meta_value = '' ) {
* False for an invalid `$site_id` (non-numeric, zero, or negative value).
* An empty array if a valid but non-existing site ID is passed and `$single` is false.
* An empty string if a valid but non-existing site ID is passed and `$single` is true.
* Note: Non-serialized values are returned as strings:
* - false values are returned as empty strings ('')
* - true values are returned as '1'
* - numbers (both integer and float) are returned as strings
* Arrays and objects retain their original type.
*/
function get_site_meta( $site_id, $key = '', $single = false ) {
return get_metadata( 'blog', $site_id, $key, $single );
Expand Down
13 changes: 12 additions & 1 deletion src/wp-includes/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -2558,7 +2558,13 @@ function get_posts( $args = null ) {
*
* @param int $post_id Post ID.
* @param string $meta_key Metadata name.
* @param mixed $meta_value Metadata value. Must be serializable if non-scalar.
* @param mixed $meta_value Metadata value. Arrays and objects are stored as serialized data and
* will be returned as the same type when retrieved. Other data types will
* be stored as strings in the database:
* - false is stored and retrieved as an empty string ('')
* - true is stored and retrieved as '1'
* - numbers (both integer and float) are stored and retrieved as strings
* Must be serializable if non-scalar.
* @param bool $unique Optional. Whether the same key should not be added.
* Default false.
* @return int|false Meta ID on success, false on failure.
Expand Down Expand Up @@ -2615,6 +2621,11 @@ function delete_post_meta( $post_id, $meta_key, $meta_value = '' ) {
* False for an invalid `$post_id` (non-numeric, zero, or negative value).
* An empty array if a valid but non-existing post ID is passed and `$single` is false.
* An empty string if a valid but non-existing post ID is passed and `$single` is true.
* Note: Non-serialized values are returned as strings:
* - false values are returned as empty strings ('')
* - true values are returned as '1'
* - numbers (both integer and float) are returned as strings
* Arrays and objects retain their original type.
*/
function get_post_meta( $post_id, $key = '', $single = false ) {
return get_metadata( 'post', $post_id, $key, $single );
Expand Down
13 changes: 12 additions & 1 deletion src/wp-includes/taxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -1386,7 +1386,13 @@ function get_terms( $args = array(), $deprecated = '' ) {
*
* @param int $term_id Term ID.
* @param string $meta_key Metadata name.
* @param mixed $meta_value Metadata value. Must be serializable if non-scalar.
* @param mixed $meta_value Metadata value. Arrays and objects are stored as serialized data and
* will be returned as the same type when retrieved. Other data types will
* be stored as strings in the database:
* - false is stored and retrieved as an empty string ('')
* - true is stored and retrieved as '1'
* - numbers (both integer and float) are stored and retrieved as strings
* Must be serializable if non-scalar.
* @param bool $unique Optional. Whether the same key should not be added.
* Default false.
* @return int|false|WP_Error Meta ID on success, false on failure.
Expand Down Expand Up @@ -1432,6 +1438,11 @@ function delete_term_meta( $term_id, $meta_key, $meta_value = '' ) {
* False for an invalid `$term_id` (non-numeric, zero, or negative value).
* An empty array if a valid but non-existing term ID is passed and `$single` is false.
* An empty string if a valid but non-existing term ID is passed and `$single` is true.
* Note: Non-serialized values are returned as strings:
* - false values are returned as empty strings ('')
* - true values are returned as '1'
* - numbers are returned as strings
* Arrays and objects retain their original type.
*/
function get_term_meta( $term_id, $key = '', $single = false ) {
return get_metadata( 'term', $term_id, $key, $single );
Expand Down
13 changes: 12 additions & 1 deletion src/wp-includes/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -1151,7 +1151,13 @@ function is_user_member_of_blog( $user_id = 0, $blog_id = 0 ) {
*
* @param int $user_id User ID.
* @param string $meta_key Metadata name.
* @param mixed $meta_value Metadata value. Must be serializable if non-scalar.
* @param mixed $meta_value Metadata value. Arrays and objects are stored as serialized data and
* will be returned as the same type when retrieved. Other data types will
* be stored as strings in the database:
* - false is stored and retrieved as an empty string ('')
* - true is stored and retrieved as '1'
* - numbers (both integer and float) are stored and retrieved as strings
* Must be serializable if non-scalar.
* @param bool $unique Optional. Whether the same key should not be added.
* Default false.
* @return int|false Meta ID on success, false on failure.
Expand Down Expand Up @@ -1200,6 +1206,11 @@ function delete_user_meta( $user_id, $meta_key, $meta_value = '' ) {
* False for an invalid `$user_id` (non-numeric, zero, or negative value).
* An empty array if a valid but non-existing user ID is passed and `$single` is false.
* An empty string if a valid but non-existing user ID is passed and `$single` is true.
* Note: Non-serialized values are returned as strings:
* - false values are returned as empty strings ('')
* - true values are returned as '1'
* - numbers (both integer and float) are returned as strings
* Arrays and objects retain their original type.
*/
function get_user_meta( $user_id, $key = '', $single = false ) {
return get_metadata( 'user', $user_id, $key, $single );
Expand Down

0 comments on commit bc3fdb4

Please sign in to comment.