Make WordPress Core


Ignore:
Timestamp:
01/20/2016 06:28:33 PM (9 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.