Make WordPress Core

Ticket #43728: 43728.3.diff

File 43728.3.diff, 2.6 KB (added by SirLouen, 4 weeks ago)

Refactor

  • src/wp-includes/class-wp-theme.php

    diff --git src/wp-includes/class-wp-theme.php src/wp-includes/class-wp-theme.php
    index 49020d5e96..e5b45b4d33 100644
    final class WP_Theme implements ArrayAccess { 
    20572057                delete_site_transient( 'wp_theme_files_patterns-' . $this->cache_hash );
    20582058        }
    20592059
     2060        /**
     2061         * Enables a theme for a site.
     2062         *
     2063         * @since x.x.0
     2064         *
     2065         * @param string|string[] $stylesheets Stylesheet name or array of stylesheet names.
     2066         * @param int             $site_id     Site ID.
     2067         */
     2068        public static function site_enable_theme( $stylesheets, $site_id = null ) {
     2069                self::_handle_site_theme_operation(
     2070                        $stylesheets,
     2071                        $site_id,
     2072                        function ( &$allowed_themes, $stylesheets ) {
     2073                                foreach ( $stylesheets as $stylesheet ) {
     2074                                        $allowed_themes[ $stylesheet ] = true;
     2075                                }
     2076                        }
     2077                );
     2078        }
     2079
     2080        /**
     2081         * Disables a theme for a site.
     2082         *
     2083         * @since x.x.0
     2084         *
     2085         * @param string|string[] $stylesheets Stylesheet name or array of stylesheet names.
     2086         * @param int             $site_id     Site ID.
     2087         */
     2088        public static function site_disable_theme( $stylesheets, $site_id = null ) {
     2089                self::_handle_site_theme_operation(
     2090                        $stylesheets,
     2091                        $site_id,
     2092                        function ( &$allowed_themes, $stylesheets ) {
     2093                                foreach ( $stylesheets as $stylesheet ) {
     2094                                        if ( isset( $allowed_themes[ $stylesheet ] ) ) {
     2095                                                unset( $allowed_themes[ $stylesheet ] );
     2096                                        }
     2097                                }
     2098                        }
     2099                );
     2100        }
     2101
    20602102        /**
    20612103         * Enables a theme for all sites on the current network.
    20622104         *
    final class WP_Theme implements ArrayAccess { 
    21672209                }
    21682210                return true;
    21692211        }
     2212
     2213        /**
     2214         * Handles common logic for site theme operations.
     2215         *
     2216         * @since x.x.0
     2217         * @access private
     2218         *
     2219         * @param string|string[] $stylesheets Stylesheet name or array of stylesheet names.
     2220         * @param int             $site_id     Site ID.
     2221         * @param callable        $operation   Callback function to perform on the allowed themes.
     2222         * @return void
     2223         */
     2224        private static function _handle_site_theme_operation( $stylesheets, $site_id, $operation ) {
     2225                $site_id = (int) $site_id;
     2226
     2227                if ( ! is_multisite() ) {
     2228                        return;
     2229                }
     2230
     2231                if ( ! is_array( $stylesheets ) ) {
     2232                        $stylesheets = array( $stylesheets );
     2233                }
     2234
     2235                $current_site_id = get_current_blog_id();
     2236                if ( ! $site_id ) {
     2237                        $site_id = $current_site_id;
     2238                }
     2239
     2240                if ( $site_id !== $current_site_id ) {
     2241                        switch_to_blog( $site_id );
     2242                }
     2243
     2244                $allowed_themes = get_option( 'allowedthemes' );
     2245                $operation( $allowed_themes, $stylesheets );
     2246                update_option( 'allowedthemes', $allowed_themes );
     2247
     2248                if ( $site_id !== $current_site_id ) {
     2249                        restore_current_blog();
     2250                }
     2251        }
    21702252}