Make WordPress Core

Ticket #28436: 28436.5.diff

File 28436.5.diff, 6.0 KB (added by jeremyfelt, 9 years ago)
  • src/wp-includes/class-wp-theme.php

     
    11771177         * @static
    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.
    11861186                 *
    1187                  * @since MU
     1187                 * @since 4.5.0
    11881188                 *
    11891189                 * @param array $allowed_themes An array of theme stylesheet names.
     1190                 * @param int   $blog_id        ID of the site.
    11901191                 */
    1191                 $network = (array) apply_filters( 'allowed_themes', self::get_allowed_on_network() );
     1192                $network = (array) apply_filters( 'network_allowed_themes', self::get_allowed_on_network(), $blog_id );
    11921193                return $network + self::get_allowed_on_site( $blog_id );
    11931194        }
    11941195
     
    12061207         */
    12071208        public static function get_allowed_on_network() {
    12081209                static $allowed_themes;
    1209                 if ( ! isset( $allowed_themes ) )
     1210                if ( ! isset( $allowed_themes ) ) {
    12101211                        $allowed_themes = (array) get_site_option( 'allowedthemes' );
     1212                }
     1213
     1214                /**
     1215                 * Filter the array of themes allowed on the network.
     1216                 *
     1217                 * @since MU
     1218                 *
     1219                 * @param array $allowed_themes An array of theme stylesheet names.
     1220                 */
     1221                $allowed_themes = apply_filters( 'allowed_themes', $allowed_themes );
     1222
    12111223                return $allowed_themes;
    12121224        }
    12131225
     
    12211233         *
    12221234         * @staticvar array $allowed_themes
    12231235         *
    1224          * @param int $blog_id Optional. Defaults to current blog.
     1236         * @param int $blog_id Optional. ID of the site. Defaults to the current site.
    12251237         * @return array Array of stylesheet names.
    12261238         */
    12271239        public static function get_allowed_on_site( $blog_id = null ) {
     
    12301242                if ( ! $blog_id || ! is_multisite() )
    12311243                        $blog_id = get_current_blog_id();
    12321244
    1233                 if ( isset( $allowed_themes[ $blog_id ] ) )
    1234                         return $allowed_themes[ $blog_id ];
     1245                if ( isset( $allowed_themes[ $blog_id ] ) ) {
     1246                        /**
     1247                         * Filter the array of themes allowed on the site.
     1248                         *
     1249                         * @since 4.5.0
     1250                         *
     1251                         * @param array $allowed_themes An array of theme stylesheet names.
     1252                         * @param int   $blog_id        ID of the site. Defaults to current site.
     1253                         */
     1254                        return (array) apply_filters( 'site_allowed_themes', $allowed_themes[ $blog_id ], $blog_id );
     1255                }
    12351256
    12361257                $current = $blog_id == get_current_blog_id();
    12371258
     
    12791300                        }
    12801301                }
    12811302
    1282                 return (array) $allowed_themes[ $blog_id ];
     1303                /** This filter is documented in wp-includes/class-wp-theme.php */
     1304                return (array) apply_filters( 'site_allowed_themes', $allowed_themes[ $blog_id ], $blog_id );
    12831305        }
    12841306
    12851307        /**
  • tests/phpunit/tests/theme/getAllowedFilters.php

     
    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 {
    810        /**
     
    1012         */
    1113        protected $default_allowed;
    1214
     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        }
     27
    1328        /**
    1429         * Test the `allowed_themes` filter, which filters themes allowed on a network.
    1530         */
     
    2742                $this->assertEquals( $expected, $allowed );
    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;
    3281
    3382                return $allowed_themes;
    3483        }
    35 }
    36  No newline at end of file
     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        }
     98}
     99endif;
     100 No newline at end of file