Make WordPress Core

Ticket #18088: 18088.6.diff

File 18088.6.diff, 7.3 KB (added by flixos90, 8 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_all_error_codes() {
     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                $result = get_settings_errors();
     143
     144                $wp_settings_errors = $_orig_settings_errors;
     145
     146                $this->assertEquals( array_merge( $foo_codes, $bar_codes ), wp_list_pluck( $result, 'code' ) );
     147        }
     148
     149        /**
     150         * @ticket 18088
     151         */
     152        public function test_get_settings_errors_single_error_code() {
     153                global $wp_settings_errors;
     154
     155                $_orig_settings_errors = $wp_settings_errors;
     156
     157                $wp_settings_errors = array();
     158
     159                $foo_codes = range( 0, 4 );
     160                $bar_codes = range( 13, 16 );
     161
     162                foreach ( $foo_codes as $foo_code ) {
     163                        $wp_settings_errors[] = array(
     164                                'setting' => 'foo',
     165                                'code'    => $foo_code,
     166                                'message' => 'Foo.',
     167                                'type'    => 'error',
     168                        );
     169                }
     170
     171                foreach ( $bar_codes as $bar_code ) {
     172                        $wp_settings_errors[] = array(
     173                                'setting' => 'bar',
     174                                'code'    => $bar_code,
     175                                'message' => 'Bar.',
     176                                'type'    => 'error',
     177                        );
     178                }
     179
     180                $result = get_settings_errors( 'foo' );
     181
     182                $wp_settings_errors = $_orig_settings_errors;
     183
     184                $this->assertEquals( $foo_codes, wp_list_pluck( $result, 'code' ) );
     185        }
     186
     187        /**
     188         * @ticket 18088
     189         */
     190        public function test_get_settings_errors_invalid_error_code() {
     191                global $wp_settings_errors;
     192
     193                $_orig_settings_errors = $wp_settings_errors;
     194
     195                $wp_settings_errors = array();
     196
     197                $codes = range( 0, 4 );
     198
     199                foreach ( $codes as $code ) {
     200                        $wp_settings_errors[] = array(
     201                                'setting' => 'foo',
     202                                'code'    => $code,
     203                                'message' => 'Foo.',
     204                                'type'    => 'error',
     205                        );
     206                }
     207
     208                $result = get_settings_errors( 'foobar' );
     209
     210                $wp_settings_errors = $_orig_settings_errors;
     211
     212                $this->assertEmpty( $result );
     213        }
     214
     215        /**
     216         * @ticket 18088
     217         */
     218        public function test_get_settings_errors_from_transient() {
     219                global $wp_settings_errors;
     220
     221                $_orig_settings_errors = $wp_settings_errors;
     222
     223                $wp_settings_errors = array();
     224
     225                $codes = range( 0, 4 );
     226
     227                $errors = array();
     228                foreach ( $codes as $code ) {
     229                        $errors[] = array(
     230                                'setting' => 'foo',
     231                                'code'    => $code,
     232                                'message' => 'Foo.',
     233                                'type'    => 'error',
     234                        );
     235                }
     236
     237                set_transient( 'settings_errors', $errors, 30 );
     238
     239                $_GET['settings-updated'] = 'true';
     240
     241                $result = get_settings_errors();
     242
     243                unset( $_GET['settings-updated'] );
     244                $wp_settings_errors = $_orig_settings_errors;
     245
     246                $this->assertEquals( $codes, wp_list_pluck( $result, 'code' ) );
     247        }
     248
     249        /**
     250         * @ticket 18088
     251         */
     252        public function test_get_settings_errors_from_network_transient() {
     253                if ( ! is_multisite() ) {
     254                        $this->markTestSkipped( 'Test only runs in multisite' );
     255                }
     256
     257                global $wp_settings_errors, $current_screen;
     258
     259                $_orig_settings_errors = $wp_settings_errors;
     260                $_orig_screen          = $current_screen;
     261
     262                $wp_settings_errors = array();
     263                $current_screen = WP_Screen::get( 'dashboard-network' );
     264
     265                $codes = range( 3, 8 );
     266
     267                $errors = array();
     268                foreach ( $codes as $code ) {
     269                        $errors[] = array(
     270                                'setting' => 'foo',
     271                                'code'    => $code,
     272                                'message' => 'Foo.',
     273                                'type'    => 'error',
     274                        );
     275                }
     276
     277                set_site_transient( 'settings_errors', $errors, 30 );
     278
     279                $_GET['updated'] = 'true';
     280
     281                $result = get_settings_errors();
     282
     283                unset( $_GET['updated'] );
     284                $wp_settings_errors = $_orig_settings_errors;
     285                $current_screen     = $_orig_screen;
     286
     287                $this->assertEquals( $codes, wp_list_pluck( $result, 'code' ) );
     288        }
     289
     290}