Make WordPress Core

Changeset 37202


Ignore:
Timestamp:
04/14/2016 03:39:45 AM (8 years ago)
Author:
jeremyfelt
Message:

Multisite: Introduce WP_Theme methods to network enable/disable themes.

  • WP_Theme::network_enable_theme() can be used to enable a theme or array of themes on a network.
  • WP_Theme::network_disable_theme() can be used to disable a theme or array of themes on a network.
  • Use these new methods in the network admin vs direct update_site_option() calls.
  • Add tests.

Props igmoweb.
Fixes #30594.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/network/themes.php

    r36770 r37202  
    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' ) );
     
    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;
     
    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;
     
    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;
  • trunk/src/wp-includes/class-wp-theme.php

    r36739 r37202  
    13171317
    13181318    /**
     1319     * Enable a theme for all sites on the current network.
     1320     *
     1321     * @since 4.6.0
     1322     *
     1323     * @static
     1324     * @access public
     1325     *
     1326     * @param string|array $stylesheets Stylesheet name or array of stylesheet names.
     1327     */
     1328    public static function network_enable_theme( $stylesheets ) {
     1329        if ( ! is_multisite() ) {
     1330            return;
     1331        }
     1332
     1333        if ( ! is_array( $stylesheets ) ) {
     1334            $stylesheets = array( $stylesheets );
     1335        }
     1336
     1337        $allowed_themes = get_site_option( 'allowedthemes' );
     1338        foreach ( $stylesheets as $stylesheet ) {
     1339            $allowed_themes[ $stylesheet ] = true;
     1340        }
     1341
     1342        update_site_option( 'allowedthemes', $allowed_themes );
     1343    }
     1344
     1345    /**
     1346     * Disable a theme for all sites on the current network.
     1347     *
     1348     * @since 4.6.0
     1349     *
     1350     * @static
     1351     * @access public
     1352     *
     1353     * @param string|array $stylesheets Stylesheet name or array of stylesheet names.
     1354     */
     1355    public static function network_disable_theme( $stylesheets ) {
     1356        if ( ! is_multisite() ) {
     1357            return;
     1358        }
     1359
     1360        if ( ! is_array( $stylesheets ) ) {
     1361            $stylesheets = array( $stylesheets );
     1362        }
     1363
     1364        $allowed_themes = get_site_option( 'allowedthemes' );
     1365        foreach ( $stylesheets as $stylesheet ) {
     1366            if ( isset( $allowed_themes[ $stylesheet ] ) ) {
     1367                unset( $allowed_themes[ $stylesheet ] );
     1368            }
     1369        }
     1370
     1371        update_site_option( 'allowedthemes', $allowed_themes );
     1372    }
     1373
     1374    /**
    13191375     * Sorts themes by name.
    13201376     *
  • trunk/tests/phpunit/tests/theme/WPTheme.php

    r27915 r37202  
    140140        $this->assertFalse( $theme->display( 'Tags' ) );
    141141    }
     142
     143
     144    /**
     145     * Enable a single theme on a network.
     146     *
     147     * @ticket 30594
     148     */
     149    function test_wp_theme_network_enable_single_theme() {
     150        if ( ! is_multisite() ) {
     151            $this->markTestSkipped( 'This test requires multisite' );
     152        }
     153
     154        $theme = 'testtheme-1';
     155        $current_allowed_themes = get_site_option( 'allowedthemes' );
     156        WP_Theme::network_enable_theme( $theme );
     157        $new_allowed_themes = get_site_option( 'allowedthemes' );
     158        update_site_option( 'allowedthemes', $current_allowed_themes ); // reset previous value.
     159        $current_allowed_themes['testtheme-1'] = true; // Add the new theme to the previous set.
     160
     161        $this->assertEqualSetsWithIndex( $current_allowed_themes, $new_allowed_themes );
     162    }
     163
     164    /**
     165     * Enable multiple themes on a network.
     166     *
     167     * @ticket 30594
     168     */
     169    function test_wp_theme_network_enable_multiple_themes() {
     170        if ( ! is_multisite() ) {
     171            $this->markTestSkipped( 'This test requires multisite' );
     172        }
     173
     174        $themes = array( 'testtheme-2', 'testtheme-3' );
     175        $current_allowed_themes = get_site_option( 'allowedthemes' );
     176        WP_Theme::network_enable_theme( $themes );
     177        $new_allowed_themes = get_site_option( 'allowedthemes' );
     178        update_site_option( 'allowedthemes', $current_allowed_themes ); // reset previous value.
     179        $current_allowed_themes = array_merge( $current_allowed_themes, array( 'testtheme-2' => true, 'testtheme-3' => true ) );
     180
     181        $this->assertEqualSetsWithIndex( $current_allowed_themes, $new_allowed_themes );
     182    }
     183
     184    /**
     185     * Disable a single theme on a network.
     186     *
     187     * @ticket 30594
     188     */
     189    function test_network_disable_single_theme() {
     190        if ( ! is_multisite() ) {
     191            $this->markTestSkipped( 'This test requires multisite' );
     192        }
     193
     194        $current_allowed_themes = get_site_option( 'allowedthemes' );
     195
     196        $allowed_themes = array( 'existing-1' => true, 'existing-2' => true, 'existing-3' => true );
     197        update_site_option( 'allowedthemes', $allowed_themes );
     198
     199        $disable_theme = 'existing-2';
     200        WP_Theme::network_disable_theme( $disable_theme );
     201        $new_allowed_themes = get_site_option( 'allowedthemes' );
     202        update_site_option( 'allowedthemes', $current_allowed_themes ); // reset previous value.
     203        unset( $allowed_themes[ $disable_theme ] ); // Remove deleted theme from initial set.
     204
     205        $this->assertEqualSetsWithIndex( $allowed_themes, $new_allowed_themes );
     206    }
     207
     208    /**
     209     * Disable multiple themes on a network.
     210     *
     211     * @ticket 30594
     212     */
     213    function test_network_disable_multiple_themes() {
     214        if ( ! is_multisite() ) {
     215            $this->markTestSkipped( 'This test requires multisite' );
     216        }
     217
     218        $current_allowed_themes = get_site_option( 'allowedthemes' );
     219
     220        $allowed_themes = array( 'existing-4' => true, 'existing-5' => true, 'existing-6' => true );
     221        update_site_option( 'allowedthemes', $allowed_themes );
     222
     223        $disable_themes = array( 'existing-4', 'existing-5' );
     224        WP_Theme::network_disable_theme( $disable_themes );
     225        $new_allowed_themes = get_site_option( 'allowedthemes' );
     226        update_site_option( 'allowedthemes', $current_allowed_themes ); // reset previous value.
     227        unset( $allowed_themes['existing-4'] );
     228        unset( $allowed_themes['existing-5'] );
     229
     230        $this->assertEqualSetsWithIndex( $allowed_themes, $new_allowed_themes );
     231    }
    142232}
Note: See TracChangeset for help on using the changeset viewer.