Make WordPress Core

Ticket #30594: 30594.2.diff

File 30594.2.diff, 7.2 KB (added by igmoweb, 11 years ago)

Patch + Unit tests splited into 4 cases

  • src/wp-admin/network/themes.php

     
    2929$referer = remove_query_arg( $temp_args, wp_get_referer() );
    3030
    3131if ( $action ) {
    32         $allowed_themes = get_site_option( 'allowedthemes' );
    3332        switch ( $action ) {
    3433                case 'enable':
    3534                        check_admin_referer('enable-theme_' . $_GET['theme']);
    36                         $allowed_themes[ $_GET['theme'] ] = true;
    37                         update_site_option( 'allowedthemes', $allowed_themes );
     35                        WP_Theme::network_enable_theme( $_GET['theme'] );
    3836                        if ( false === strpos( $referer, '/network/themes.php' ) )
    3937                                wp_redirect( network_admin_url( 'themes.php?enabled=1' ) );
    4038                        else
    … …  
    4240                        exit;
    4341                case 'disable':
    4442                        check_admin_referer('disable-theme_' . $_GET['theme']);
    45                         unset( $allowed_themes[ $_GET['theme'] ] );
    46                         update_site_option( 'allowedthemes', $allowed_themes );
     43                        WP_Theme::network_disable_theme( $_GET['theme'] );
    4744                        wp_safe_redirect( add_query_arg( 'disabled', '1', $referer ) );
    4845                        exit;
    4946                case 'enable-selected':
    … …  
    5350                                wp_safe_redirect( add_query_arg( 'error', 'none', $referer ) );
    5451                                exit;
    5552                        }
    56                         foreach ( (array) $themes as $theme )
    57                                 $allowed_themes[ $theme ] = true;
    58                         update_site_option( 'allowedthemes', $allowed_themes );
     53                        WP_Theme::network_enable_theme( (array) $themes );
    5954                        wp_safe_redirect( add_query_arg( 'enabled', count( $themes ), $referer ) );
    6055                        exit;
    6156                case 'disable-selected':
    … …  
    6560                                wp_safe_redirect( add_query_arg( 'error', 'none', $referer ) );
    6661                                exit;
    6762                        }
    68                         foreach ( (array) $themes as $theme )
    69                                 unset( $allowed_themes[ $theme ] );
    70                         update_site_option( 'allowedthemes', $allowed_themes );
     63                        WP_Theme::network_disable_theme( (array) $themes );
    7164                        wp_safe_redirect( add_query_arg( 'disabled', count( $themes ), $referer ) );
    7265                        exit;
    7366                case 'update-selected' :
  • src/wp-includes/class-wp-theme.php

     
    13081308        }
    13091309
    13101310        /**
     1311         * Enable a theme for a whole network
     1312         *
     1313         * @since 4.5
     1314         *
     1315         * @static
     1316         * @access public
     1317         *
     1318         * @param string|array $stylesheets Stylesheet name or array of stylesheets
     1319         */
     1320        public static function network_enable_theme( $stylesheets ) {
     1321            if ( ! is_array( $stylesheets ) ) {
     1322                    $stylesheets = array( $stylesheets );
     1323            }
     1324
     1325            $allowed_themes = get_site_option( 'allowedthemes' );
     1326            foreach ( $stylesheets as $stylesheet ) {
     1327                    $allowed_themes[ $stylesheet ] = true;
     1328            }
     1329
     1330            update_site_option( 'allowedthemes', $allowed_themes );
     1331    }
     1332
     1333        /**
     1334         * Disable a theme for a whole network
     1335         *
     1336         * @since 4.5
     1337         *
     1338         * @static
     1339         * @access public
     1340         *
     1341         * @param string|array $stylesheets Stylesheet name or array of stylesheets
     1342         */
     1343        public static function network_disable_theme( $stylesheets ) {
     1344        if ( ! is_array( $stylesheets ) ) {
     1345                $stylesheets = array( $stylesheets );
     1346        }
     1347
     1348        $allowed_themes = get_site_option( 'allowedthemes' );
     1349        foreach ( $stylesheets as $stylesheet ) {
     1350                if ( isset( $allowed_themes[ $stylesheet ] ) ) {
     1351                        unset( $allowed_themes[ $stylesheet ] );
     1352                }
     1353        }
     1354
     1355        update_site_option( 'allowedthemes', $allowed_themes );
     1356    }
     1357
     1358        /**
    13111359         * Sort themes by name.
    13121360         *
    13131361         * @since 3.4.0
  • tests/phpunit/tests/multisite/network.php

     
    249249                $dashboard_blog = get_dashboard_blog();
    250250                $this->assertEquals( $blog_id, $dashboard_blog->blog_id );
    251251        }
     252
     253        /**
     254         * Network enable one theme
     255         *
     256         * @ticket 30594
     257         */
     258        function test_network_enable_theme() {
     259                $theme = 'testtheme-1';
     260                WP_Theme::network_enable_theme( $theme );
     261                $enabled = get_site_option( 'allowedthemes' );
     262                $this->assertArrayHasKey( 'testtheme-1', $enabled );
     263
     264                // Enable again
     265                WP_Theme::network_enable_theme( $theme );
     266                $enabled = get_site_option( 'allowedthemes' );
     267                $this->assertArrayHasKey( 'testtheme-1', $enabled );
     268        }
     269
     270        /**
     271         * Network enable a few themes
     272         *
     273         * @ticket 30594
     274         */
     275        function test_network_enable_themes() {
     276                // Enable one
     277                WP_Theme::network_enable_theme( array( 'testtheme-1' ) );
     278
     279                // Now enable the rest
     280                $themes = array( 'testtheme-2', 'testtheme-3' );
     281                WP_Theme::network_enable_theme( $themes );
     282                $enabled = get_site_option( 'allowedthemes' );
     283                $this->assertArrayHasKey( 'testtheme-1', $enabled );
     284                $this->assertArrayHasKey( 'testtheme-2', $enabled );
     285                $this->assertArrayHasKey( 'testtheme-3', $enabled );
     286
     287                // Enable again
     288                WP_Theme::network_enable_theme( $themes );
     289                $enabled = get_site_option( 'allowedthemes' );
     290                $this->assertArrayHasKey( 'testtheme-1', $enabled );
     291                $this->assertArrayHasKey( 'testtheme-2', $enabled );
     292                $this->assertArrayHasKey( 'testtheme-3', $enabled );
     293
     294        }
     295
     296        /**
     297         * Network disable one theme
     298         *
     299         * @ticket 30594
     300         */
     301        function test_network_disable_theme() {
     302                // First, enable a few themes
     303                $themes = array( 'testtheme-1', 'testtheme-2', 'testtheme-3', 'testtheme-4' );
     304                WP_Theme::network_enable_theme( $themes );
     305
     306                // Disable one only theme
     307                WP_Theme::network_disable_theme( 'testtheme-2' );
     308                $enabled = get_site_option( 'allowedthemes' );
     309
     310                $this->assertArrayHasKey( 'testtheme-1', $enabled );
     311                $this->assertArrayHasKey( 'testtheme-3', $enabled );
     312                $this->assertArrayHasKey( 'testtheme-4', $enabled );
     313                $this->assertFalse( isset( $enabled['testtheme-2'] ) );
     314
     315                // And disable again
     316                WP_Theme::network_disable_theme( 'testtheme-2' );
     317                $enabled = get_site_option( 'allowedthemes' );
     318                $this->assertArrayHasKey( 'testtheme-1', $enabled );
     319                $this->assertArrayHasKey( 'testtheme-3', $enabled );
     320                $this->assertArrayHasKey( 'testtheme-4', $enabled );
     321                $this->assertFalse( isset( $enabled['testtheme-2'] ) );
     322        }
     323
     324        /**
     325         * Network disable a few themes
     326         *
     327         * @ticket 30594
     328         */
     329        function test_network_disable_themes() {
     330                // First, enable a few themes
     331                $themes = array( 'testtheme-1', 'testtheme-2', 'testtheme-3', 'testtheme-4' );
     332                WP_Theme::network_enable_theme( $themes );
     333
     334                // Disable one
     335                WP_Theme::network_disable_theme( array( 'testtheme-2' ) );
     336
     337                // And disable the rest of themes
     338                $remove = array( 'testtheme-1', 'testtheme-3' );
     339                WP_Theme::network_disable_theme( $remove );
     340                $enabled = get_site_option( 'allowedthemes' );
     341                $this->assertArrayHasKey( 'testtheme-4', $enabled );
     342                $this->assertFalse( isset( $enabled['testtheme-1'] ) );
     343                $this->assertFalse( isset( $enabled['testtheme-2'] ) );
     344                $this->assertFalse( isset( $enabled['testtheme-3'] ) );
     345
     346                // And disable again
     347                WP_Theme::network_disable_theme( $remove );
     348                $enabled = get_site_option( 'allowedthemes' );
     349                $this->assertArrayHasKey( 'testtheme-4', $enabled );
     350                $this->assertFalse( isset( $enabled['testtheme-1'] ) );
     351                $this->assertFalse( isset( $enabled['testtheme-2'] ) );
     352                $this->assertFalse( isset( $enabled['testtheme-3'] ) );
     353        }
    252354}
    253355
    254356endif;