Make WordPress Core

Ticket #40012: 40012.diff

File 40012.diff, 2.2 KB (added by johnjamesjacoby, 8 years ago)

Proof of concept. Needs testing with serialized data.

  • src/wp-includes/meta.php

     
    2121 * @param int    $object_id  ID of the object metadata is for
    2222 * @param string $meta_key   Metadata key
    2323 * @param mixed  $meta_value Metadata value. Must be serializable if non-scalar.
    24  * @param bool   $unique     Optional, default is false.
     24 * @param mixed  $unique     Optional, default is false.
    2525 *                           Whether the specified metadata key should be unique for the object.
    2626 *                           If true, and the object already has a value for the specified metadata key,
    2727 *                           no change will be made.
     28 *                           If 'value', and the object already has the same metadata value being added
     29 *                           for the specified metadata key, no change will be made.
    2830 * @return int|false The meta ID on success, false on failure.
    2931 */
    3032function add_metadata($meta_type, $object_id, $meta_key, $meta_value, $unique = false) {
     
    6466         * @param int       $object_id  Object ID.
    6567         * @param string    $meta_key   Meta key.
    6668         * @param mixed     $meta_value Meta value. Must be serializable if non-scalar.
    67          * @param bool      $unique     Whether the specified meta key should be unique
     69         * @param mixed     $unique     Whether the specified meta key should be unique
    6870         *                              for the object. Optional. Default false.
    6971         */
    7072        $check = apply_filters( "add_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $unique );
    7173        if ( null !== $check )
    7274                return $check;
    7375
    74         if ( $unique && $wpdb->get_var( $wpdb->prepare(
    75                 "SELECT COUNT(*) FROM $table WHERE meta_key = %s AND $column = %d",
    76                 $meta_key, $object_id ) ) )
    77                 return false;
     76        if ( false !== $unique ) {
     77                if ( 'value' === $unique ) {
     78                        $sql = $wpdb->prepare( "SELECT COUNT(*) FROM {$table} WHERE meta_key = %s AND meta_value = %s AND {$column} = %d", $meta_key, $meta_value, $object_id );
     79                } else {
     80                        $sql = $wpdb->prepare( "SELECT COUNT(*) FROM {$table} WHERE meta_key = %s AND {$column} = %d", $meta_key, $meta_value, $object_id );
     81                }
    7882
     83                if ( $wpdb->get_var( $sql ) ) {
     84                        return false;
     85                }
     86        }
     87
    7988        $_meta_value = $meta_value;
    8089        $meta_value = maybe_serialize( $meta_value );
    8190