Make WordPress Core

Changeset 36366


Ignore:
Timestamp:
01/20/2016 06:28:33 PM (11 years ago)
Author:
jeremyfelt
Message:

Themes: Enhance filtering options for allowed themes on a network.

  • Move the legacy allowed_themes filter to WP_Theme::get_allowed_on_network(), where it will continue to filter themes allowed on the network.
  • Add network_allowed_themes filter to WP_Theme::get_allowed() and pass $blog_id to provide context.
  • Add site_allowed_themes filter to WP_Theme::get_allowed_on_site() and pass $blog_id to provide context.

Props pauldewouters, lamosty, michalzuber, dmsnell, johnnypea, rob.
Fixes #28436.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-theme.php

    r36250 r36366  
    11781178         * @access public
    11791179         *
    1180          * @param int $blog_id Optional. Defaults to current blog.
     1180         * @param int $blog_id Optional. ID of the site. Defaults to the current site.
    11811181         * @return array Array of stylesheet names.
    11821182         */
    11831183        public static function get_allowed( $blog_id = null ) {
    11841184                /**
    1185                  * Filter the array of themes allowed on the site or network.
     1185                 * Filter the array of themes allowed on the network.
     1186                 *
     1187                 * Site is provided as context so that a list of network allowed themes can
     1188                 * be filtered further.
     1189                 *
     1190                 * @since 4.5.0
     1191                 *
     1192                 * @param array $allowed_themes An array of theme stylesheet names.
     1193                 * @param int   $blog_id        ID of the site.
     1194                 */
     1195                $network = (array) apply_filters( 'network_allowed_themes', self::get_allowed_on_network(), $blog_id );
     1196                return $network + self::get_allowed_on_site( $blog_id );
     1197        }
     1198
     1199        /**
     1200         * Returns array of stylesheet names of themes allowed on the network.
     1201         *
     1202         * @since 3.4.0
     1203         *
     1204         * @static
     1205         * @access public
     1206         *
     1207         * @staticvar array $allowed_themes
     1208         *
     1209         * @return array Array of stylesheet names.
     1210         */
     1211        public static function get_allowed_on_network() {
     1212                static $allowed_themes;
     1213                if ( ! isset( $allowed_themes ) ) {
     1214                        $allowed_themes = (array) get_site_option( 'allowedthemes' );
     1215                }
     1216
     1217                /**
     1218                 * Filter the array of themes allowed on the network.
    11861219                 *
    11871220                 * @since MU
    … …  
    11891222                 * @param array $allowed_themes An array of theme stylesheet names.
    11901223                 */
    1191                 $network = (array) apply_filters( 'allowed_themes', self::get_allowed_on_network() );
    1192                 return $network + self::get_allowed_on_site( $blog_id );
    1193         }
    1194 
    1195         /**
    1196          * Returns array of stylesheet names of themes allowed on the network.
     1224                $allowed_themes = apply_filters( 'allowed_themes', $allowed_themes );
     1225
     1226                return $allowed_themes;
     1227        }
     1228
     1229        /**
     1230         * Returns array of stylesheet names of themes allowed on the site.
    11971231         *
    11981232         * @since 3.4.0
    … …  
    12031237         * @staticvar array $allowed_themes
    12041238         *
    1205          * @return array Array of stylesheet names.
    1206          */
    1207         public static function get_allowed_on_network() {
    1208                 static $allowed_themes;
    1209                 if ( ! isset( $allowed_themes ) )
    1210                         $allowed_themes = (array) get_site_option( 'allowedthemes' );
    1211                 return $allowed_themes;
    1212         }
    1213 
    1214         /**
    1215          * Returns array of stylesheet names of themes allowed on the site.
    1216          *
    1217          * @since 3.4.0
    1218          *
    1219          * @static
    1220          * @access public
    1221          *
    1222          * @staticvar array $allowed_themes
    1223          *
    1224          * @param int $blog_id Optional. Defaults to current blog.
     1239         * @param int $blog_id Optional. ID of the site. Defaults to the current site.
    12251240         * @return array Array of stylesheet names.
    12261241         */
    … …  
    12311246                        $blog_id = get_current_blog_id();
    12321247
    1233                 if ( isset( $allowed_themes[ $blog_id ] ) )
    1234                         return $allowed_themes[ $blog_id ];
     1248                if ( isset( $allowed_themes[ $blog_id ] ) ) {
     1249                        /**
     1250                         * Filter the array of themes allowed on the site.
     1251                         *
     1252                         * @since 4.5.0
     1253                         *
     1254                         * @param array $allowed_themes An array of theme stylesheet names.
     1255                         * @param int   $blog_id        ID of the site. Defaults to current site.
     1256                         */
     1257                        return (array) apply_filters( 'site_allowed_themes', $allowed_themes[ $blog_id ], $blog_id );
     1258                }
    12351259
    12361260                $current = $blog_id == get_current_blog_id();
    … …  
    12801304                }
    12811305
    1282                 return (array) $allowed_themes[ $blog_id ];
     1306                /** This filter is documented in wp-includes/class-wp-theme.php */
     1307                return (array) apply_filters( 'site_allowed_themes', $allowed_themes[ $blog_id ], $blog_id );
    12831308        }
    12841309
  • trunk/tests/phpunit/tests/theme/getAllowedFilters.php

    r36350 r36366  
    11<?php
     2if ( is_multisite() ) :
    23/**
    34 * Tests specific to the filtering of `WP_Theme::get_allowed()` and related functions.
    45 *
    56 * @group themes
     7 * @group multisite
    68 */
    79class Tests_WP_Theme_Get_Allowed_Filters extends WP_UnitTestCase {
    … …  
    1012         */
    1113        protected $default_allowed;
     14
     15        protected $filter_network_allowed_themes_args;
     16
     17        public function test_network_allowed_themes_filter_sends_blog_id() {
     18                $blog_id = 1;
     19
     20                add_filter( 'network_allowed_themes', array( $this, 'filter_network_allowed_themes' ), 10, 2 );
     21                WP_Theme::get_allowed( $blog_id );
     22                remove_filter( 'network_allowed_themes', array( $this, 'filter_network_allowed_themes' ) );
     23
     24                $this->assertEquals( 2, count( $this->filter_network_allowed_themes_args ) );
     25                $this->assertEquals( $blog_id, $this->filter_network_allowed_themes_args[1] );
     26        }
    1227
    1328        /**
    … …  
    2843        }
    2944
     45        /**
     46         * Test the `network_allowed_themes` filter, which filters allowed themes on the network and provides `$blog_id`.
     47         */
     48        public function test_wp_theme_get_allowed_with_network_allowed_themes_filter() {
     49                $blog_id = 1;
     50
     51                $this->default_allowed = WP_Theme::get_allowed( $blog_id );
     52
     53                add_filter( 'network_allowed_themes', array( $this, 'filter_network_allowed_themes' ), 10, 2 );
     54                $allowed = WP_Theme::get_allowed( $blog_id );
     55                remove_filter( 'network_allowed_themes', array( $this, 'filter_network_allowed_themes' ), 10 );
     56
     57                $expected = $this->default_allowed + array( 'network-allowed-theme' => true );
     58
     59                $this->assertEquals( $expected, $allowed );
     60        }
     61
     62        /**
     63         * Test the `site_allowed_themes` filter, which filters allowed themes for a site and provides `$blog_id`.
     64         */
     65        public function test_wp_theme_get_allowed_with_site_allowed_themes_filter() {
     66                $blog_id = 1;
     67
     68                $this->default_allowed = WP_Theme::get_allowed( $blog_id );
     69
     70                add_filter( 'site_allowed_themes', array( $this, 'filter_site_allowed_themes' ), 10, 2 );
     71                $allowed = WP_Theme::get_allowed( $blog_id );
     72                remove_filter( 'site_allowed_themes', array( $this, 'filter_site_allowed_themes' ), 10 );
     73
     74                $expected = $this->default_allowed + array( 'site-allowed-theme' => true );
     75
     76                $this->assertEquals( $expected, $allowed );
     77        }
     78
    3079        public function filter_allowed_themes( $allowed_themes ) {
    3180                $allowed_themes['allow-on-network'] = true;
    … …  
    3382                return $allowed_themes;
    3483        }
     84
     85        public function filter_network_allowed_themes( $allowed_themes, $blog_id ) {
     86                $this->filter_network_allowed_themes_args = func_get_args();
     87
     88                $allowed_themes['network-allowed-theme'] = true;
     89
     90                return $allowed_themes;
     91        }
     92
     93        public function filter_site_allowed_themes( $allowed_themes, $blog_id ) {
     94                $allowed_themes['site-allowed-theme'] = true;
     95
     96                return $allowed_themes;
     97        }
    3598}
     99endif;
Note: See TracChangeset for help on using the changeset viewer.