Make WordPress Core

Ticket #28315: 28315.3.diff

File 28315.3.diff, 6.6 KB (added by SergeyBiryukov, 10 years ago)
  • src/wp-includes/meta.php

     
    2727 * @return int|bool The meta ID on success, false on failure.
    2828 */
    2929function add_metadata($meta_type, $object_id, $meta_key, $meta_value, $unique = false) {
    30         if ( !$meta_type || !$meta_key )
     30        global $wpdb;
     31
     32        if ( ! $meta_type || ! $meta_key || ! is_numeric( $object_id ) ) {
    3133                return false;
     34        }
    3235
    33         if ( !$object_id = absint($object_id) )
     36        $object_id = absint( $object_id );
     37        if ( ! $object_id ) {
    3438                return false;
     39        }
    3540
    36         if ( ! $table = _get_meta_table($meta_type) )
     41        $table = _get_meta_table( $meta_type );
     42        if ( ! $table ) {
    3743                return false;
     44        }
    3845
    39         global $wpdb;
    40 
    4146        $column = sanitize_key($meta_type . '_id');
    4247
    4348        // expected_slashed ($meta_key)
     
    134139 * @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure.
    135140 */
    136141function update_metadata($meta_type, $object_id, $meta_key, $meta_value, $prev_value = '') {
    137         if ( !$meta_type || !$meta_key )
     142        global $wpdb;
     143
     144        if ( ! $meta_type || ! $meta_key || ! is_numeric( $object_id ) ) {
    138145                return false;
     146        }
    139147
    140         if ( ! is_numeric( $object_id ) || ! $object_id = absint( $object_id ) ) {
     148        $object_id = absint( $object_id );
     149        if ( ! $object_id ) {
    141150                return false;
    142151        }
    143152
    144         if ( ! $table = _get_meta_table($meta_type) )
     153        $table = _get_meta_table( $meta_type );
     154        if ( ! $table ) {
    145155                return false;
     156        }
    146157
    147         global $wpdb;
    148 
    149158        $column = sanitize_key($meta_type . '_id');
    150159        $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';
    151160
     
    282291 * @return bool True on successful delete, false on failure.
    283292 */
    284293function delete_metadata($meta_type, $object_id, $meta_key, $meta_value = '', $delete_all = false) {
    285         if ( !$meta_type || !$meta_key )
     294        global $wpdb;
     295
     296        if ( ! $meta_type || ! $meta_key || ! is_numeric( $object_id ) && ! $delete_all ) {
    286297                return false;
     298        }
    287299
    288         if ( (!$object_id = absint($object_id)) && !$delete_all )
     300        $object_id = absint( $object_id );
     301        if ( ! $object_id && ! $delete_all ) {
    289302                return false;
     303        }
    290304
    291         if ( ! $table = _get_meta_table($meta_type) )
     305        $table = _get_meta_table( $meta_type );
     306        if ( ! $table ) {
    292307                return false;
     308        }
    293309
    294         global $wpdb;
    295 
    296310        $type_column = sanitize_key($meta_type . '_id');
    297311        $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';
    298312        // expected_slashed ($meta_key)
     
    424438 * @return string|array Single metadata value, or array of values
    425439 */
    426440function get_metadata($meta_type, $object_id, $meta_key = '', $single = false) {
    427         if ( !$meta_type )
     441        if ( ! $meta_type || ! is_numeric( $object_id ) ) {
    428442                return false;
     443        }
    429444
    430         if ( !$object_id = absint($object_id) )
     445        $object_id = absint( $object_id );
     446        if ( ! $object_id ) {
    431447                return false;
     448        }
    432449
    433450        /**
    434451         * Filter whether to retrieve metadata of a specific type.
     
    488505 * @return boolean true of the key is set, false if not.
    489506 */
    490507function metadata_exists( $meta_type, $object_id, $meta_key ) {
    491         if ( ! $meta_type )
     508        if ( ! $meta_type || ! is_numeric( $object_id ) ) {
    492509                return false;
     510        }
    493511
    494         if ( ! $object_id = absint( $object_id ) )
     512        $object_id = absint( $object_id );
     513        if ( ! $object_id ) {
    495514                return false;
     515        }
    496516
    497517        /** This filter is documented in wp-includes/meta.php */
    498518        $check = apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, true );
     
    524544function get_metadata_by_mid( $meta_type, $meta_id ) {
    525545        global $wpdb;
    526546
    527         if ( ! $meta_type )
     547        if ( ! $meta_type || ! is_numeric( $meta_id ) ) {
    528548                return false;
     549        }
    529550
    530         if ( !$meta_id = absint( $meta_id ) )
     551        $meta_id = absint( $meta_id );
     552        if ( ! $meta_id ) {
    531553                return false;
     554        }
    532555
    533         if ( ! $table = _get_meta_table($meta_type) )
     556        $table = _get_meta_table( $meta_type );
     557        if ( ! $table ) {
    534558                return false;
     559        }
    535560
    536561        $id_column = ( 'user' == $meta_type ) ? 'umeta_id' : 'meta_id';
    537562
     
    564589        global $wpdb;
    565590
    566591        // Make sure everything is valid.
    567         if ( ! $meta_type )
     592        if ( ! $meta_type || ! is_numeric( $meta_id ) ) {
    568593                return false;
     594        }
    569595
    570         if ( ! $meta_id = absint( $meta_id ) )
     596        $meta_id = absint( $meta_id );
     597        if ( ! $meta_id ) {
    571598                return false;
     599        }
    572600
    573         if ( ! $table = _get_meta_table( $meta_type ) )
     601        $table = _get_meta_table( $meta_type );
     602        if ( ! $table ) {
    574603                return false;
     604        }
    575605
    576606        $column = sanitize_key($meta_type . '_id');
    577607        $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';
     
    651681        global $wpdb;
    652682
    653683        // Make sure everything is valid.
    654         if ( ! $meta_type )
     684        if ( ! $meta_type || ! is_numeric( $meta_id ) ) {
    655685                return false;
     686        }
    656687
    657         if ( ! $meta_id = absint( $meta_id ) )
     688        $meta_id = absint( $meta_id );
     689        if ( ! $meta_id ) {
    658690                return false;
     691        }
    659692
    660         if ( ! $table = _get_meta_table( $meta_type ) )
     693        $table = _get_meta_table( $meta_type );
     694        if ( ! $table ) {
    661695                return false;
     696        }
    662697
    663698        // object and id columns
    664699        $column = sanitize_key($meta_type . '_id');
     
    729764 * @return mixed Metadata cache for the specified objects, or false on failure.
    730765 */
    731766function update_meta_cache($meta_type, $object_ids) {
    732         if ( empty( $meta_type ) || empty( $object_ids ) )
     767        global $wpdb;
     768
     769        if ( ! $meta_type || ! $object_ids ) {
    733770                return false;
     771        }
    734772
    735         if ( ! $table = _get_meta_table($meta_type) )
     773        $table = _get_meta_table( $meta_type );
     774        if ( ! $table ) {
    736775                return false;
     776        }
    737777
    738778        $column = sanitize_key($meta_type . '_id');
    739779
    740         global $wpdb;
    741 
    742780        if ( !is_array($object_ids) ) {
    743781                $object_ids = preg_replace('|[^0-9,]|', '', $object_ids);
    744782                $object_ids = explode(',', $object_ids);
  • tests/phpunit/tests/meta.php

     
    236236
    237237                $this->assertEquals( wp_list_pluck( $posts, 'post_title' ), wp_list_pluck( $posts2, 'post_title' ) );
    238238        }
     239
     240        /**
     241         * @ticket 28315
     242         */
     243        function test_non_numeric_object_id() {
     244                $this->assertFalse( add_metadata( 'user', array( 1 ), 'meta_key', 'meta_value' ) );
     245                $this->assertFalse( update_metadata( 'user', array( 1 ), 'meta_key', 'meta_new_value' ) );
     246                $this->assertFalse( delete_metadata( 'user', array( 1 ), 'meta_key' ) );
     247                $this->assertFalse( get_metadata( 'user', array( 1 ) ) );
     248                $this->assertFalse( metadata_exists( 'user', array( 1 ), 'meta_key' ) );
     249        }
     250
     251        /**
     252         * @ticket 28315
     253         */
     254        function test_non_numeric_meta_id() {
     255                $this->assertFalse( get_metadata_by_mid( 'user', array( 1 ) ) );
     256                $this->assertFalse( update_metadata_by_mid( 'user', array( 1 ), 'meta_new_value' ) );
     257                $this->assertFalse( delete_metadata_by_mid( 'user', array( 1 ) ) );
     258        }
    239259}