Make WordPress Core

Ticket #34848: 34848.diff

File 34848.diff, 11.1 KB (added by patrickgarman, 6 years ago)
  • src/wp-includes/meta.php

    diff --git src/wp-includes/meta.php src/wp-includes/meta.php
    index 010822fdc8..02999d0dce 100644
    function add_metadata( $meta_type, $object_id, $meta_key, $meta_value, $unique = 
    132132        return $mid;
    133133}
    134134
     135/**
     136 * Add multiple metadatas for the specified object. Similar to calling add_metadata for each metadata individually,
     137 * and is only applicable for unique meta data. If a meta key already exists for an object it will not be stored.
     138 *
     139 * @since x.x.x
     140 *
     141 * @global wpdb $wpdb WordPress database abstraction object.
     142 *
     143 * @param string $meta_type  Type of object metadata is for (e.g., comment, post, or user)
     144 * @param int    $object_id  ID of the object metadata is for
     145 * @param string $meta_key   Metadata key
     146 * @param mixed  $meta_value Metadata value. Must be serializable if non-scalar.
     147 * @param bool   $unique     Optional, default is false.
     148 *                           Whether the specified metadata key should be unique for the object.
     149 *                           If true, and the object already has a value for the specified metadata key,
     150 *                           no change will be made.
     151 * @return int|false The meta ID on success, false on failure.
     152 */
     153function add_metadatas( $meta_type, $object_id, $meta_data ) {
     154        global $wpdb;
     155
     156        if ( ! $meta_type || ! is_numeric( $object_id ) || ! is_array( $meta_data ) ) {
     157                return false;
     158        }
     159
     160        $object_id = absint( $object_id );
     161        if ( ! $object_id ) {
     162                return false;
     163        }
     164
     165        $table = _get_meta_table( $meta_type );
     166        if ( ! $table ) {
     167                return false;
     168        }
     169
     170        $column = sanitize_key( $meta_type . '_id' );
     171
     172        /**
     173         * Filters whether to add metadata of a specific type.
     174         *
     175         * The dynamic portion of the hook, `$meta_type`, refers to the meta
     176         * object type (comment, post, or user). Returning a non-null value
     177         * will effectively short-circuit the function.
     178         *
     179         * @since 3.1.0
     180         *
     181         * @param null|bool $check      Whether to allow adding metadata for the given type.
     182         * @param int       $object_id  Object ID.
     183         * @param string $meta_data     Meta data, an array of meta keys and meta datas, meta values must be serializable
     184         *                              if non-scalar.
     185         */
     186        $check = apply_filters( "add_{$meta_type}_metadatas", null, $object_id, $meta_data );
     187        if ( null !== $check ) {
     188                return $check;
     189        }
     190
     191        $_meta_data = array();
     192        foreach( $meta_data as $key => $value ) {
     193                if ( 0 == absint( $wpdb->get_var(
     194                                $wpdb->prepare( "SELECT COUNT(*) FROM $table WHERE meta_key = %s AND $column = %d", $key, $object_id )
     195                        ) ) ) {
     196                        $key = wp_unslash( $key );
     197                        $value = wp_unslash( sanitize_meta( $key, $value, $meta_type ) );
     198
     199                        $_meta_data[ $key ] = maybe_serialize( $value );
     200
     201                        /**
     202                         * Fires immediately before meta of a specific type is added.
     203                         *
     204                         * The dynamic portion of the hook, `$meta_type`, refers to the meta
     205                         * object type (comment, post, or user).
     206                         *
     207                         * @since 3.1.0
     208                         *
     209                         * @param int    $object_id  Object ID.
     210                         * @param string $meta_key   Meta key.
     211                         * @param mixed  $meta_value Meta value.
     212                         */
     213                        do_action( "add_{$meta_type}_meta", $object_id, $key, $value );
     214
     215                }
     216        }
     217
     218        $rows = array();
     219        if( ! empty( $_meta_data ) ) {
     220                $sql = "INSERT INTO {$table} ({$column}, meta_key, meta_value) VALUES ";
     221
     222                $comma = false;
     223                foreach( $_meta_data as $key => $value ) {
     224                        if( true == $comma ) {
     225                                $sql .= ',';
     226                        }
     227
     228                        $sql = "({$object_id}, '{$key}', '{$value}')";
     229                        $comma = true;
     230                }
     231        }
     232
     233        $result = $wpdb->query( $sql );
     234
     235        if ( ! $result ) {
     236                return false;
     237        }
     238
     239        wp_cache_delete($object_id, $meta_type . '_meta');
     240
     241        return true;
     242}
     243
    135244/**
    136245 * Update metadata for the specified object. If no value already exists for the specified object
    137246 * ID and metadata key, the metadata will be added.
    function delete_metadata( $meta_type, $object_id, $meta_key, $meta_value = '', $ 
    462571        return true;
    463572}
    464573
     574/**
     575 * Delete metadatas for the specified object.
     576 *
     577 * @since x.x.x
     578 *
     579 * @global wpdb $wpdb WordPress database abstraction object.
     580 *
     581 * @param string $meta_type  Type of object metadata is for (e.g., comment, post, or user)
     582 * @param int    $object_id  ID of the object metadata is for
     583 * @param string $meta_keys   Metadata key
     584 * @param mixed  $meta_value Optional. Metadata value. Must be serializable if non-scalar. If specified, only delete
     585 *                           metadata entries with this value. Otherwise, delete all entries with the specified meta_key.
     586 *                           Pass `null, `false`, or an empty string to skip this check. (For backward compatibility,
     587 *                           it is not possible to pass an empty string to delete those entries with an empty string
     588 *                           for a value.)
     589 * @param bool   $delete_all Optional, default is false. If true, delete matching metadata entries for all objects,
     590 *                           ignoring the specified object_id. Otherwise, only delete matching metadata entries for
     591 *                           the specified object_id.
     592 * @return bool True on successful delete, false on failure.
     593 */
     594function delete_metadatas( $meta_type, $object_id, $meta_keys ) {
     595        global $wpdb;
     596
     597        if ( ! $meta_type || ! is_numeric( $object_id ) && ! is_array( $meta_keys ) ) {
     598                return false;
     599        }
     600
     601        $object_id = absint( $object_id );
     602        if ( ! $object_id ) {
     603                return false;
     604        }
     605
     606        $table = _get_meta_table( $meta_type );
     607        if ( ! $table ) {
     608                return false;
     609        }
     610
     611        $type_column = sanitize_key( $meta_type . '_id' );
     612        $id_column   = 'user' == $meta_type ? 'umeta_id' : 'meta_id';
     613        // expected_slashed ($meta_key)
     614        $meta_key = array_map( 'wp_unslash', $meta_keys );
     615
     616        /**
     617         * Filters whether to delete metadata of a specific type.
     618         *
     619         * The dynamic portion of the hook, `$meta_type`, refers to the meta
     620         * object type (comment, post, or user). Returning a non-null value
     621         * will effectively short-circuit the function.
     622         *
     623         * @since x.x.x
     624         *
     625         * @param null|bool $delete     Whether to allow metadata deletion of the given type.
     626         * @param int       $object_id  Object ID.
     627         * @param string    $meta_keys  Meta keys.
     628         */
     629        $check = apply_filters( "delete_{$meta_type}_metadatas", null, $object_id, $meta_keys );
     630        if ( null !== $check ) {
     631                return (bool) $check;
     632        }
     633
     634        /**
     635         * Fires immediately before deleting metadata of a specific type.
     636         *
     637         * The dynamic portion of the hook, `$meta_type`, refers to the meta
     638         * object type (comment, post, or user).
     639         *
     640         * @since x.x.x
     641         *
     642         * @param int    $object_id  Object ID.
     643         * @param string $meta_keys  Meta keys.
     644         */
     645        do_action( "delete_{$meta_type}_metas", $object_id, $meta_keys );
     646        $sql = "DELETE FROM {$table} WHERE {$type_column} = {$object_id} and meta_key IN( ";
     647        $comma = false;
     648        foreach( $meta_keys as $meta_key ) {
     649                if( true == $comma ) {
     650                        $sql .= ', ';
     651                }
     652                $sql .= " `{$meta_key}`";
     653                $comma = true;
     654        }
     655        $sql .= " )";
     656        $count = $wpdb->query( $sql );
     657
     658        if ( ! $count ) {
     659                return false;
     660        }
     661        wp_cache_delete($object_id, $meta_type . '_meta');
     662
     663        /**
     664         * Fires immediately after deleting metadata of a specific type.
     665         *
     666         * The dynamic portion of the hook name, `$meta_type`, refers to the meta
     667         * object type (comment, post, or user).
     668         *
     669         * @since x.x.x
     670         *
     671         * @param int    $object_id  Object ID.
     672         * @param string $meta_keys  Meta key.
     673         */
     674        do_action( "deleted_{$meta_type}_metas", $object_id, $meta_keys );
     675
     676        return true;
     677}
     678
    465679/**
    466680 * Retrieve metadata for the specified object.
    467681 *
  • src/wp-includes/post.php

    diff --git src/wp-includes/post.php src/wp-includes/post.php
    index 45fb3573b8..cab1e97b49 100644
    function add_post_meta( $post_id, $meta_key, $meta_value, $unique = false ) { 
    18231823        return $added;
    18241824}
    18251825
     1826/**
     1827 * Add meta datas to a post.
     1828 *
     1829 * Post meta data is called "Custom Fields" on the Administration Screen.
     1830 *
     1831 * @since x.x.x
     1832 *
     1833 * @param int    $post_id    Post ID.
     1834 * @param string $meta_data  Metadata as a key/value pair array. Values must be serializable if non-scalar.
     1835 * @return bool  Was the data inserted
     1836 */
     1837function add_post_metas( $post_id, $meta_data ) {
     1838        // Make sure meta is added to the post, not a revision.
     1839        if ( $the_post = wp_is_post_revision( $post_id ) ) {
     1840                $post_id = $the_post;
     1841        }
     1842
     1843        $added =  add_metadatas( 'post', $post_id, $meta_data );
     1844        if ( $added ) {
     1845                wp_cache_set( 'last_changed', microtime(), 'posts' );
     1846        }
     1847        return $added;
     1848}
     1849
    18261850/**
    18271851 * Remove metadata matching criteria from a post.
    18281852 *
    function delete_post_meta( $post_id, $meta_key, $meta_value = '' ) { 
    18511875        return $deleted;
    18521876}
    18531877
     1878/**
     1879 * Remove metadatas matching criteria from a post.
     1880 *
     1881 * @since x.x.x
     1882 *
     1883 * @param int    $post_id    Post ID.
     1884 * @param string $meta_key   Metadata name.
     1885 * @param mixed  $meta_value Optional. Metadata value. Must be serializable if
     1886 *                           non-scalar. Default empty.
     1887 * @return bool True on success, false on failure.
     1888 */
     1889function delete_post_metas( $post_id, $meta_keys ) {
     1890        // Make sure meta is added to the post, not a revision.
     1891        if ( $the_post = wp_is_post_revision( $post_id ) ) {
     1892                $post_id = $the_post;
     1893        }
     1894
     1895        $deleted = delete_metadatas( 'post', $post_id, $meta_keys );
     1896        if ( $deleted ) {
     1897                wp_cache_set( 'last_changed', microtime(), 'posts' );
     1898        }
     1899        return $deleted;
     1900}
     1901
    18541902/**
    18551903 * Retrieve post meta field for a post.
    18561904 *
    function update_post_meta( $post_id, $meta_key, $meta_value, $prev_value = '' ) 
    19001948        return $updated;
    19011949}
    19021950
     1951/**
     1952 * Update metadatas on a post. This is done by deleting all the post meta and then re-inserting it.
     1953 *
     1954 * To update post meta another way it would require two calls of update_post_meta
     1955 * Each update_post_meta call creates 1 select and 1 update mysql query
     1956 * 2 update_post_meta calls creates 2 select and 2 update queries
     1957 * update_post_metas creates 1 delete query, 1 insert query, and 1 select for every post meta being added
     1958 *
     1959 * Updating X metas:
     1960 * update_post_meta  = X selects, X updates
     1961 * update_post_metas = X selects, 1 delete, 1 insert
     1962 *
     1963 * The benefits of update_post_metas grows the more post metas being updated.
     1964 *
     1965 * Example: Updating 22 address metas on a post
     1966 * update_post_meta  = 22 selects, 22 updates
     1967 * update_post_metas = 22 selects, 1 delete, 1 insert
     1968 *
     1969 * @since x.x.x
     1970 *
     1971 * @param int    $post_id    Post ID.
     1972 * @param string $meta_key   Metadata key.
     1973 * @param mixed  $meta_value Metadata value. Must be serializable if non-scalar.
     1974 * @param mixed  $prev_value Optional. Previous value to check before removing.
     1975 *                           Default empty.
     1976 * @return int|bool Meta ID if the key didn't exist, true on successful update,
     1977 *                  false on failure.
     1978 */
     1979function update_post_metas( $post_id, $meta_data ) {
     1980        // Make sure meta is added to the post, not a revision.
     1981        if ( $the_post = wp_is_post_revision( $post_id ) ) {
     1982                $post_id = $the_post;
     1983        }
     1984
     1985        delete_post_metas( $post_id, array_keys( $meta_data ) );
     1986
     1987        $updated = add_post_metas( $post_id, $meta_data );
     1988        if ( $updated ) {
     1989                wp_cache_set( 'last_changed', microtime(), 'posts' );
     1990        }
     1991        return $updated;
     1992}
     1993
    19031994/**
    19041995 * Delete everything from post meta matching meta key.
    19051996 *