WordPress.org

Make WordPress Core

Changeset 42146


Ignore:
Timestamp:
11/10/17 22:29:45 (8 days ago)
Author:
westonruter
Message:

Settings: Replace count() call with empty() in get_settings_errors() to prevent PHP 7.2 warnings when $wp_settings_errors is null.

Props pross, dd32, westonruter.
See #40109.
Fixes #42498 for trunk.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/template.php

    r41906 r42146  
    14581458 
    14591459    // Check global in case errors have been added on this pageload. 
    1460     if ( ! count( $wp_settings_errors ) ) 
     1460    if ( empty( $wp_settings_errors ) ) { 
    14611461        return array(); 
     1462    } 
    14621463 
    14631464    // Filter the results to those of a specific setting if one was set. 
  • trunk/tests/phpunit/tests/option/sanitize-option.php

    r37747 r42146  
    158158        ); 
    159159    } 
     160 
     161    /** 
     162     * Test calling get_settings_errors() with variations on where it gets errors from. 
     163     * 
     164     * @ticket 42498 
     165     * @covers get_settings_errors() 
     166     * @global array $wp_settings_errors 
     167     */ 
     168    public function test_get_settings_errors_sources() { 
     169        global $wp_settings_errors; 
     170 
     171        $blogname_error = array( 
     172            'setting' => 'blogname', 
     173            'code'    => 'blogname', 
     174            'message' => 'Capital P dangit!', 
     175            'type'    => 'error', 
     176        ); 
     177        $blogdescription_error = array( 
     178            'setting' => 'blogdescription', 
     179            'code'    => 'blogdescription', 
     180            'message' => 'Too short', 
     181            'type'    => 'error', 
     182        ); 
     183 
     184        $wp_settings_errors = null; 
     185        $this->assertSame( array(), get_settings_errors( 'blogname' ) ); 
     186 
     187        // Test getting errors from transient. 
     188        $_GET['settings-updated'] = '1'; 
     189        set_transient( 'settings_errors', array( $blogname_error ) ); 
     190        $wp_settings_errors = null; 
     191        $this->assertSame( array( $blogname_error ), get_settings_errors( 'blogname' ) ); 
     192 
     193        // Test getting errors from transient and from global. 
     194        $_GET['settings-updated'] = '1'; 
     195        set_transient( 'settings_errors', array( $blogname_error ) ); 
     196        $wp_settings_errors = null; 
     197        add_settings_error( $blogdescription_error['setting'], $blogdescription_error['code'], $blogdescription_error['message'], $blogdescription_error['type'] ); 
     198        $this->assertEqualSets( array( $blogname_error, $blogdescription_error ), get_settings_errors() ); 
     199 
     200        $wp_settings_errors = null; 
     201    } 
    160202} 
Note: See TracChangeset for help on using the changeset viewer.