Make WordPress Core

Ticket #18088: 18088.5.diff

File 18088.5.diff, 6.2 KB (added by flixos90, 9 years ago)
  • src/wp-admin/includes/template.php

     
    13941394 * action hook).
    13951395 *
    13961396 * @since 3.0.0
     1397 * @since 4.7.0 The function now supports getting network settings errors from a transient.
    13971398 *
    13981399 * @global array $wp_settings_errors Storage array of errors registered during this pageload
    13991400 *
     
    14121413        if ( $sanitize )
    14131414                sanitize_option( $setting, get_option( $setting ) );
    14141415
    1415         // If settings were passed back from options.php then use them.
     1416        // If settings were passed back from options.php or network/settings.php then use them.
    14161417        if ( isset( $_GET['settings-updated'] ) && $_GET['settings-updated'] && get_transient( 'settings_errors' ) ) {
    14171418                $wp_settings_errors = array_merge( (array) $wp_settings_errors, get_transient( 'settings_errors' ) );
    14181419                delete_transient( 'settings_errors' );
     1420        } elseif ( is_network_admin() && isset( $_GET['updated'] ) && $_GET['updated'] && get_site_transient( 'settings_errors' ) ) {
     1421                $wp_settings_errors = array_merge( (array) $wp_settings_errors, get_site_transient( 'settings_errors' ) );
     1422                delete_site_transient( 'settings_errors' );
    14191423        }
    14201424
    14211425        // Check global in case errors have been added on this pageload.
  • src/wp-admin/network/settings.php

     
    8484         */
    8585        do_action( 'update_wpmu_options' );
    8686
     87        // If no settings errors were registered, add a general 'updated' message.
     88        if ( ! count( get_settings_errors() ) ) {
     89                add_settings_error( 'general', 'settings_updated', __( 'Settings saved.' ), 'updated' );
     90        }
     91
     92        set_site_transient( 'settings_errors', get_settings_errors(), 30 );
     93
    8794        wp_redirect( add_query_arg( 'updated', 'true', network_admin_url( 'settings.php' ) ) );
    8895        exit();
    8996}
     
    9198include( ABSPATH . 'wp-admin/admin-header.php' );
    9299
    93100if ( isset( $_GET['updated'] ) ) {
    94         ?><div id="message" class="updated notice is-dismissible"><p><?php _e( 'Settings saved.' ) ?></p></div><?php
     101        settings_errors();
    95102}
    96103?>
    97104
  • tests/phpunit/tests/admin/includesTemplate.php

     
    5050                global $wp_meta_boxes;
    5151
    5252                add_meta_box( 'testbox1', 'Test Metabox', '__return_false', 'post' );
    53                
     53
    5454                $this->assertArrayHasKey( 'testbox1', $wp_meta_boxes['post']['advanced']['default'] );
    5555        }
    5656
     
    7979                // Add a meta box to three different post types
    8080                add_meta_box( 'testbox1', 'Test Metabox', '__return_false', array( 'post', 'comment', 'attachment' ) );
    8181
    82                 $this->assertArrayHasKey( 'testbox1', $wp_meta_boxes['post']['advanced']['default'] ); 
     82                $this->assertArrayHasKey( 'testbox1', $wp_meta_boxes['post']['advanced']['default'] );
    8383                $this->assertArrayHasKey( 'testbox1', $wp_meta_boxes['comment']['advanced']['default'] );
    8484                $this->assertArrayHasKey( 'testbox1', $wp_meta_boxes['attachment']['advanced']['default'] );
    8585        }
     
    108108                $this->assertFalse( $wp_meta_boxes['attachment']['advanced']['default']['testbox1'] );
    109109        }
    110110
    111 }
    112  No newline at end of file
     111        /**
     112         * @ticket 18088
     113         */
     114        public function test_get_settings_errors() {
     115                global $wp_settings_errors;
     116
     117                $_orig_settings_errors = $wp_settings_errors;
     118
     119                $wp_settings_errors = array();
     120
     121                $foo_codes = range( 0, 4 );
     122                $bar_codes = range( 13, 16 );
     123
     124                foreach ( $foo_codes as $foo_code ) {
     125                        $wp_settings_errors[] = array(
     126                                'setting' => 'foo',
     127                                'code'    => $foo_code,
     128                                'message' => 'Foo.',
     129                                'type'    => 'error',
     130                        );
     131                }
     132
     133                foreach ( $bar_codes as $bar_code ) {
     134                        $wp_settings_errors[] = array(
     135                                'setting' => 'bar',
     136                                'code'    => $bar_code,
     137                                'message' => 'Bar.',
     138                                'type'    => 'error',
     139                        );
     140                }
     141
     142                // Get all settings errors.
     143                $result = get_settings_errors();
     144                $this->assertEquals( array_merge( $foo_codes, $bar_codes ), wp_list_pluck( $result, 'code' ) );
     145
     146                // Get all foo errors.
     147                $result = get_settings_errors( 'foo' );
     148                $this->assertEquals( $foo_codes, wp_list_pluck( $result, 'code' ) );
     149
     150                // Get all foobar errors.
     151                $result = get_settings_errors( 'foobar' );
     152                $this->assertEmpty( $result );
     153
     154                $wp_settings_errors = $_orig_settings_errors;
     155        }
     156
     157        /**
     158         * @ticket 18088
     159         */
     160        public function test_get_settings_errors_transient() {
     161                global $wp_settings_errors;
     162
     163                $_orig_settings_errors = $wp_settings_errors;
     164
     165                $wp_settings_errors = array();
     166
     167                $codes = range( 0, 4 );
     168
     169                $errors = array();
     170                foreach ( $codes as $code ) {
     171                        $errors[] = array(
     172                                'setting' => 'foo',
     173                                'code'    => $code,
     174                                'message' => 'Foo.',
     175                                'type'    => 'error',
     176                        );
     177                }
     178
     179                set_transient( 'settings_errors', $errors, 30 );
     180
     181                $_GET['settings-updated'] = 'true';
     182
     183                $result = get_settings_errors();
     184                $this->assertEquals( $codes, wp_list_pluck( $result, 'code' ) );
     185
     186                unset( $_GET['settings-updated'] );
     187
     188                $wp_settings_errors = $_orig_settings_errors;
     189        }
     190
     191        /**
     192         * @ticket 18088
     193         */
     194        public function test_get_settings_errors_network_transient() {
     195                if ( ! is_multisite() ) {
     196                        $this->markTestSkipped( 'Test only runs in multisite' );
     197                }
     198
     199                global $wp_settings_errors, $current_screen;
     200
     201                $_orig_settings_errors = $wp_settings_errors;
     202                $_orig_screen          = $current_screen;
     203
     204                $wp_settings_errors = array();
     205                $current_screen = WP_Screen::get( 'dashboard-network' );
     206
     207                $codes = range( 3, 8 );
     208
     209                $errors = array();
     210                foreach ( $codes as $code ) {
     211                        $errors[] = array(
     212                                'setting' => 'foo',
     213                                'code'    => $code,
     214                                'message' => 'Foo.',
     215                                'type'    => 'error',
     216                        );
     217                }
     218
     219                set_site_transient( 'settings_errors', $errors, 30 );
     220
     221                $_GET['updated'] = 'true';
     222
     223                $result = get_settings_errors();
     224                $this->assertEquals( $codes, wp_list_pluck( $result, 'code' ) );
     225
     226                unset( $_GET['updated'] );
     227
     228                $wp_settings_errors = $_orig_settings_errors;
     229                $current_screen     = $_orig_screen;
     230        }
     231
     232}