Make WordPress Core

Ticket #32605: 32605.patch

File 32605.patch, 4.1 KB (added by dlh, 10 years ago)
  • src/wp-includes/meta.php

     
    576576                return false;
    577577        }
    578578
     579        /**
     580         * Filter whether to retrieve metadata of a specific type by its meta ID.
     581         *
     582         * The dynamic portion of the hook, `$meta_type`, refers to the meta
     583         * object type (comment, post, or user). Returning a non-null value
     584         * will effectively short-circuit the retrieval.
     585         *
     586         * @since x.y.z
     587         *
     588         * @param null|object $value Whether to retrieve the meta row object.
     589         * @param int mixed $meta_id Meta row ID.
     590         */
     591        $check = apply_filters( "get_{$meta_type}_meta_by_mid", null, $meta_id );
     592        if ( null !== $check ) {
     593                if ( empty( $check ) ) {
     594                        return false;
     595                }
     596
     597                if ( isset( $check->meta_value ) ) {
     598                        $check->meta_value = maybe_unserialize( $check->meta_value );
     599                }
     600
     601                return $check;
     602        }
     603
    579604        $id_column = ( 'user' == $meta_type ) ? 'umeta_id' : 'meta_id';
    580605
    581606        $meta = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table WHERE $id_column = %d", $meta_id ) );
     
    621646        $column = sanitize_key($meta_type . '_id');
    622647        $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';
    623648
     649        /**
     650         * Filter whether to update metadata of a specific type by its meta ID.
     651         *
     652         * The dynamic portion of the hook, `$meta_type`, refers to the meta
     653         * object type (comment, post, or user). Returning a non-null value
     654         * will effectively short-circuit the update.
     655         *
     656         * @since x.y.z
     657         *
     658         * @param null|bool $check Whether to allow updating metadata for the given type.
     659         * @param int $meta_id Meta row ID.
     660         * @param mixed $meta_value Meta value. Must be serializable if non-scalar.
     661         * @param string|bool $meta_key Meta key to update.
     662         */
     663        $check = apply_filters( "update_{$meta_type}_meta_by_mid", null, $meta_id, $meta_value, $meta_key );
     664        if ( null !== $check ) {
     665                return false;
     666        }
     667
    624668        // Fetch the meta and go on if it's found.
    625669        if ( $meta = get_metadata_by_mid( $meta_type, $meta_id ) ) {
    626670                $original_key = $meta->meta_key;
     
    711755        $column = sanitize_key($meta_type . '_id');
    712756        $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';
    713757
     758        /**
     759         * Filter whether to delete metadata of a specific type by its meta ID.
     760         *
     761         * The dynamic portion of the hook, `$meta_type`, refers to the meta
     762         * object type (comment, post, or user). Returning a non-null value
     763         * will effectively short-circuit the deletion.
     764         *
     765         * @since x.y.z
     766         *
     767         * @param null|bool $check Whether to allow deleting metadata for the given type.
     768         * @param int $meta_id Meta row ID.
     769         */
     770        $check = apply_filters( "delete_{$meta_type}_meta_by_mid", null, $meta_id );
     771        if ( null !== $check ) {
     772                return false;
     773        }
     774
    714775        // Fetch the meta and go on if it's found.
    715776        if ( $meta = get_metadata_by_mid( $meta_type, $meta_id ) ) {
    716777                $object_id = $meta->{$column};
  • tests/phpunit/tests/meta.php

     
    3131                $this->assertFalse( delete_metadata_by_mid( 'user', 0 ) );
    3232                $this->assertFalse( delete_metadata_by_mid( 'non_existing_meta', $this->delete_meta_id ) );
    3333
     34                // Test short-circuiting deletion.
     35                add_filter( 'delete_user_meta_by_mid', '__return_true' );
     36                $this->assertFalse( delete_metadata_by_mid( 'user', $this->delete_meta_id ) );
     37                remove_filter( 'delete_user_meta_by_mid', '__return_true' );
     38
    3439                // Now let's delete the real meta data
    3540                $this->assertTrue( delete_metadata_by_mid( 'user', $this->delete_meta_id ) );
    3641
     
    7681                $this->assertTrue( update_metadata_by_mid( 'user', $this->meta_id, 'other_meta_value' ) );
    7782                $second = get_user_meta( $meta->user_id, $meta->meta_key );
    7883                $this->assertFalse( $first === $second );
     84
     85                // Test short-circuiting updates.
     86                add_filter( 'update_user_meta_by_mid', '__return_true' );
     87                $this->assertFalse( update_metadata_by_mid( 'user', $this->meta_id, 'meta_new_value' ) );
     88                remove_filter( 'update_user_meta_by_mid', '__return_true' );
    7989        }
    8090
    8191        /**