From 2da7f9f5247bce4527660146efdb186757b1e863 Mon Sep 17 00:00:00 2001 From: Gary Pendergast Date: Mon, 15 Jul 2019 06:24:08 +0000 Subject: [PATCH] Code Modernisation: Fix known instances of array access on data types that can't be accessed as arrays. PHP 7.4 addes a warning when trying access a null/bool/int/float/resource (everything but array, string and object) as if it were an array. This change fixes all of these warnings visible in unit tests. Props jrf. See #47704. git-svn-id: https://develop.svn.wordpress.org/trunk@45639 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-wp-customize-manager.php | 8 ++++---- .../class-wp-customize-nav-menu-item-setting.php | 9 ++++++--- src/wp-includes/meta.php | 6 +++++- src/wp-includes/rest-api/class-wp-rest-request.php | 6 ++++-- src/wp-includes/theme.php | 12 ++++++------ src/wp-includes/user.php | 5 ++++- src/wp-includes/wp-db.php | 2 +- tests/phpunit/tests/query/generatePostdata.php | 2 +- tests/phpunit/tests/term/termExists.php | 2 +- 9 files changed, 32 insertions(+), 20 deletions(-) diff --git a/src/wp-includes/class-wp-customize-manager.php b/src/wp-includes/class-wp-customize-manager.php index 4d7bf3ba44..be369a1cb3 100644 --- a/src/wp-includes/class-wp-customize-manager.php +++ b/src/wp-includes/class-wp-customize-manager.php @@ -5098,10 +5098,10 @@ final class WP_Customize_Manager { 'label' => __( 'Logo' ), 'section' => 'title_tagline', 'priority' => 8, - 'height' => $custom_logo_args[0]['height'], - 'width' => $custom_logo_args[0]['width'], - 'flex_height' => $custom_logo_args[0]['flex-height'], - 'flex_width' => $custom_logo_args[0]['flex-width'], + 'height' => isset( $custom_logo_args[0]['height'] ) ? $custom_logo_args[0]['height'] : null, + 'width' => isset( $custom_logo_args[0]['width'] ) ? $custom_logo_args[0]['width'] : null, + 'flex_height' => isset( $custom_logo_args[0]['flex-height'] ) ? $custom_logo_args[0]['flex-height'] : null, + 'flex_width' => isset( $custom_logo_args[0]['flex-width'] ) ? $custom_logo_args[0]['flex-width'] : null, 'button_labels' => array( 'select' => __( 'Select logo' ), 'change' => __( 'Change logo' ), diff --git a/src/wp-includes/customize/class-wp-customize-nav-menu-item-setting.php b/src/wp-includes/customize/class-wp-customize-nav-menu-item-setting.php index bccf1d9388..5f66023b43 100644 --- a/src/wp-includes/customize/class-wp-customize-nav-menu-item-setting.php +++ b/src/wp-includes/customize/class-wp-customize-nav-menu-item-setting.php @@ -477,8 +477,11 @@ class WP_Customize_Nav_Menu_Item_Setting extends WP_Customize_Setting { */ public function filter_wp_get_nav_menu_items( $items, $menu, $args ) { $this_item = $this->value(); - $current_nav_menu_term_id = $this_item['nav_menu_term_id']; - unset( $this_item['nav_menu_term_id'] ); + $current_nav_menu_term_id = null; + if ( isset( $this_item['nav_menu_term_id'] ) ) { + $current_nav_menu_term_id = $this_item['nav_menu_term_id']; + unset( $this_item['nav_menu_term_id'] ); + } $should_filter = ( $menu->term_id === $this->original_nav_menu_term_id @@ -493,7 +496,7 @@ class WP_Customize_Nav_Menu_Item_Setting extends WP_Customize_Setting { $should_remove = ( false === $this_item || - true === $this_item['_invalid'] + ( isset( $this_item['_invalid'] ) && true === $this_item['_invalid'] ) || ( $this->original_nav_menu_term_id === $menu->term_id diff --git a/src/wp-includes/meta.php b/src/wp-includes/meta.php index 5446c16d3e..9bd989540c 100644 --- a/src/wp-includes/meta.php +++ b/src/wp-includes/meta.php @@ -523,7 +523,11 @@ function get_metadata( $meta_type, $object_id, $meta_key = '', $single = false ) if ( ! $meta_cache ) { $meta_cache = update_meta_cache( $meta_type, array( $object_id ) ); - $meta_cache = $meta_cache[ $object_id ]; + if ( isset( $meta_cache[ $object_id ] ) ) { + $meta_cache = $meta_cache[ $object_id ]; + } else { + $meta_cache = null; + } } if ( ! $meta_key ) { diff --git a/src/wp-includes/rest-api/class-wp-rest-request.php b/src/wp-includes/rest-api/class-wp-rest-request.php index ee106b5c5a..3089547ce7 100644 --- a/src/wp-includes/rest-api/class-wp-rest-request.php +++ b/src/wp-includes/rest-api/class-wp-rest-request.php @@ -294,7 +294,9 @@ class WP_REST_Request implements ArrayAccess { * * @since 4.4.0 * - * @return array Map containing 'value' and 'parameters' keys. + * @return array|null Map containing 'value' and 'parameters' keys + * or null when no valid content-type header was + * available. */ public function get_content_type() { $value = $this->get_header( 'content-type' ); @@ -334,7 +336,7 @@ class WP_REST_Request implements ArrayAccess { $order = array(); $content_type = $this->get_content_type(); - if ( $content_type['value'] === 'application/json' ) { + if ( isset( $content_type['value'] ) && 'application/json' === $content_type['value'] ) { $order[] = 'JSON'; } diff --git a/src/wp-includes/theme.php b/src/wp-includes/theme.php index f7f061d351..eddd76684a 100644 --- a/src/wp-includes/theme.php +++ b/src/wp-includes/theme.php @@ -2371,14 +2371,14 @@ function add_theme_support( $feature, ...$args ) { * Merge post types with any that already declared their support * for post thumbnails. */ - if ( is_array( $args[0] ) && isset( $_wp_theme_features['post-thumbnails'] ) ) { + if ( isset( $args[0] ) && is_array( $args[0] ) && isset( $_wp_theme_features['post-thumbnails'] ) ) { $args[0] = array_unique( array_merge( $_wp_theme_features['post-thumbnails'][0], $args[0] ) ); } break; case 'post-formats': - if ( is_array( $args[0] ) ) { + if ( isset( $args[0] ) && is_array( $args[0] ) ) { $post_formats = get_post_format_slugs(); unset( $post_formats['standard'] ); @@ -2391,7 +2391,7 @@ function add_theme_support( $feature, ...$args ) { if ( empty( $args[0] ) ) { // Build an array of types for back-compat. $args = array( 0 => array( 'comment-list', 'comment-form', 'search-form' ) ); - } elseif ( ! is_array( $args[0] ) ) { + } elseif ( ! isset( $args[0] ) || ! is_array( $args[0] ) ) { _doing_it_wrong( "add_theme_support( 'html5' )", __( 'You need to pass an array of types.' ), '3.6.1' ); return false; } @@ -2403,7 +2403,7 @@ function add_theme_support( $feature, ...$args ) { break; case 'custom-logo': - if ( ! is_array( $args ) ) { + if ( true === $args ) { $args = array( 0 => array() ); } $defaults = array( @@ -2426,7 +2426,7 @@ function add_theme_support( $feature, ...$args ) { return add_theme_support( 'custom-header', array( 'uploads' => true ) ); case 'custom-header': - if ( ! is_array( $args ) ) { + if ( true === $args ) { $args = array( 0 => array() ); } @@ -2516,7 +2516,7 @@ function add_theme_support( $feature, ...$args ) { break; case 'custom-background': - if ( ! is_array( $args ) ) { + if ( true === $args ) { $args = array( 0 => array() ); } diff --git a/src/wp-includes/user.php b/src/wp-includes/user.php index 3d2bf939d4..fab14519df 100644 --- a/src/wp-includes/user.php +++ b/src/wp-includes/user.php @@ -2088,7 +2088,10 @@ All at ###SITENAME### $logged_in_cookie = wp_parse_auth_cookie( '', 'logged_in' ); /** This filter is documented in wp-includes/pluggable.php */ $default_cookie_life = apply_filters( 'auth_cookie_expiration', ( 2 * DAY_IN_SECONDS ), $ID, false ); - $remember = ( ( $logged_in_cookie['expiration'] - time() ) > $default_cookie_life ); + $remember = false; + if ( false !== $logged_in_cookie && ( $logged_in_cookie['expiration'] - time() ) > $default_cookie_life ) { + $remember = true; + } wp_set_auth_cookie( $ID, $remember ); } diff --git a/src/wp-includes/wp-db.php b/src/wp-includes/wp-db.php index 232b3e8ac9..4f8223fc4b 100644 --- a/src/wp-includes/wp-db.php +++ b/src/wp-includes/wp-db.php @@ -3113,7 +3113,7 @@ class wpdb { foreach ( $data as $col => $value ) { if ( ! empty( $value['db'] ) ) { // We're going to need to truncate by characters or bytes, depending on the length value we have. - if ( 'byte' === $value['length']['type'] ) { + if ( isset( $value['length']['type'] ) && 'byte' === $value['length']['type'] ) { // Using binary causes LEFT() to truncate by bytes. $charset = 'binary'; } else { diff --git a/tests/phpunit/tests/query/generatePostdata.php b/tests/phpunit/tests/query/generatePostdata.php index 20525ee9d3..fa7d0afb46 100644 --- a/tests/phpunit/tests/query/generatePostdata.php +++ b/tests/phpunit/tests/query/generatePostdata.php @@ -24,7 +24,7 @@ class Tests_Query_GeneratePostdata extends WP_UnitTestCase { $data = generate_postdata( $fake->ID ); // Fails because there's no post with this ID. - $this->assertNotSame( $fake->ID, $data['id'] ); + $this->assertFalse( $data ); } /** diff --git a/tests/phpunit/tests/term/termExists.php b/tests/phpunit/tests/term/termExists.php index 658cac087a..a99aefbb76 100644 --- a/tests/phpunit/tests/term/termExists.php +++ b/tests/phpunit/tests/term/termExists.php @@ -150,7 +150,7 @@ class Tests_TermExists extends WP_UnitTestCase { _unregister_taxonomy( 'foo' ); - $this->assertSame( null, $found['term_id'] ); + $this->assertSame( null, $found ); } public function test_term_exists_taxonomy_nonempty_parent_nonempty_match_name() {