Make WordPress Core

Changeset 60995

Timestamp:
10/20/2025 10:29:35 PM (5 weeks ago)
Author:
jorbin
Message:

Plugins and Themes: Add filters to validation functions.

It is now possible to add additional validation requirements for themes and plugins besides the built in core checks.

Developed in https://github.com/WordPress/wordpress-develop/pull/1812 and https://github.com/WordPress/wordpress-develop/pull/10361.

Props kraftbj, mukesh27, jorbin, joedolson.
Fixes #54381.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/plugin.php

    r60666 r60995  
    12481248    }
    12491249
    1250     return true;
     1250    /**
     1251     * Filters the plugin requirement validation response.
     1252     *
     1253     * If a plugin fails due to a Core-provided validation (incompatible WP, PHP versions), this
     1254     * filter will not fire. A WP_Error response will already be returned.
     1255     *
     1256     * This filter is intended to add additional validation steps by site administrators.
     1257     *
     1258     * @since 6.9.0
     1259     *
     1260     * @param bool|WP_Error $met_requirements True if the plugin meets requirements, WP_Error if not.
     1261     * @param string $plugin Path to the plugin file relative to the plugins directory.
     1262     */
     1263    return apply_filters( 'validate_plugin_requirements', true, $plugin );
    12511264}
    12521265
  • trunk/src/wp-includes/theme.php

    r60979 r60995  
    10031003    }
    10041004
    1005     return true;
     1005    /**
     1006     * Filters the theme requirement validation response.
     1007     *
     1008     * If a theme fails due to a Core-provided validation (incompatible WP, PHP versions), this
     1009     * filter will not fire. A WP_Error response will already be returned.
     1010     *
     1011     * This filter is intended to add additional validation steps by site administrators.
     1012     *
     1013     * @since 6.9.0
     1014     *
     1015     * @param bool|WP_Error $met_requirements True if the theme meets requirements, WP_Error if not.
     1016     * @param string $stylesheet Directory name for the theme.
     1017     */
     1018    return apply_filters( 'validate_theme_requirements', true, $stylesheet );
    10061019}
    10071020
  • trunk/tests/phpunit/tests/theme.php

    r60934 r60995  
    15301530        $this->assertSame( $template_path, $new_template_path, 'Switching blogs switches the template path' );
    15311531    }
     1532
     1533    /**
     1534     * Verify the validate_theme_requirements theme responds as expected for twentyten.
     1535     *
     1536     * @ticket 54381
     1537     */
     1538    public function test_validate_theme_requirements_filter_default() {
     1539        // Default expectation since twentyten has the least strict requirements.
     1540        $this->assertTrue( validate_theme_requirements( 'twentyten' ) );
     1541    }
     1542
     1543    /**
     1544     * Verify that a filtered failure of validate_theme_requirements returns WP_Error
     1545     *
     1546     * @ticket 54381
     1547     */
     1548    public function test_validate_theme_requirements_filter_error() {
     1549        // Adds an extra requirement that always fails.
     1550        add_filter(
     1551            'validate_theme_requirements',
     1552            function () {
     1553                return new WP_Error( 'theme_test_failed_requirement' );
     1554            }
     1555        );
     1556
     1557        $this->assertInstanceOf( 'WP_Error', validate_theme_requirements( 'twentyten' ) );
     1558    }
     1559
     1560    /**
     1561     * Verify that the theme is passed through to the validate_theme_requirements filter by selectively erroring.
     1562     *
     1563     * @ticket 54381
     1564     */
     1565    public function test_validate_theme_requirements_filter_selective_failure() {
     1566        // Adds an extra requirement only for a particular theme.
     1567        add_filter(
     1568            'validate_theme_requirements',
     1569            function ( $met_requirements, $stylesheet ) {
     1570                if ( 'twentytwenty' === $stylesheet ) {
     1571                    return new WP_Error( 'theme_test_failed_requirement' );
     1572                }
     1573                return $met_requirements;
     1574            },
     1575            10,
     1576            2
     1577        );
     1578
     1579        $this->assertTrue( validate_theme_requirements( 'twentyten' ) );
     1580        $this->assertInstanceOf( 'WP_Error', validate_theme_requirements( 'twentytwenty' ) );
     1581    }
    15321582}
Note: See TracChangeset for help on using the changeset viewer.