WordPress.org

Make WordPress Core

Ticket #26516: 26516.2.diff

File 26516.2.diff, 7.1 KB (added by obenland, 6 years ago)

Adds some documentation and makes sure current_theme_supports() always returns a boolean.

  • wp-includes/theme.php

     
    13661366 * The init hook may be too late for some features.
    13671367 *
    13681368 * @since 2.9.0
    1369  * @param string $feature the feature being added
     1369 * @global array $_wp_theme_features All supported features.
     1370 * @param string $feature The feature being added.
     1371 * @return bool|void
    13701372 */
    13711373function add_theme_support( $feature ) {
    13721374        global $_wp_theme_features;
     
    15601562 * Gets the theme support arguments passed when registering that support
    15611563 *
    15621564 * @since 3.1
    1563  * @param string $feature the feature to check
    1564  * @return array The array of extra arguments
     1565 * @global array $_wp_theme_features All supported features.
     1566 * @param string $feature The feature to check.
     1567 * @return array|bool The array of extra arguments, or true if there are none.
    15651568 */
    15661569function get_theme_support( $feature ) {
    15671570        global $_wp_theme_features;
    15681571        if ( ! isset( $_wp_theme_features[ $feature ] ) )
    15691572                return false;
    15701573
    1571         if ( func_num_args() <= 1 )
    1572                 return $_wp_theme_features[ $feature ];
    1573 
    15741574        $args = array_slice( func_get_args(), 1 );
    1575         switch ( $feature ) {
    1576                 case 'custom-header' :
    1577                 case 'custom-background' :
    1578                         if ( isset( $_wp_theme_features[ $feature ][0][ $args[0] ] ) )
    1579                                 return $_wp_theme_features[ $feature ][0][ $args[0] ];
    1580                         return false;
    1581                         break;
    1582                 default :
    1583                         return $_wp_theme_features[ $feature ];
    1584                         break;
     1575        if ( ! empty( $args ) ) {
     1576                switch ( $feature ) {
     1577                        case 'custom-header' :
     1578                        case 'custom-background' :
     1579                                if ( isset( $_wp_theme_features[ $feature ][0][ $args[0] ] ) )
     1580                                        return $_wp_theme_features[ $feature ][0][ $args[0] ];
     1581                                return false;
     1582                                break;
     1583                }
    15851584        }
     1585
     1586        $feature_args = $_wp_theme_features[ $feature ];
     1587        if ( is_array( $feature_args ) ) {
     1588                $feature_args = array_merge( $feature_args, $_wp_theme_features[ $feature ][0] );
     1589        }
     1590
     1591        /**
     1592         * Filter whether the theme support for a specific feature.
     1593         *
     1594         * The dynamic portion of the hook name, $feature, refers to
     1595         * the specific theme feature. Possible values include 'post-formats',
     1596         * 'post-thumbnails', 'menus', 'automatic-feed-links', and 'html5'.
     1597         *
     1598         * @since 3.9.0
     1599         *
     1600         * @param array|bool $feature_args The array of extra arguments, or true if there are none.
     1601         * @param array      $args         Array of arguments for the feature.
     1602         */
     1603        return apply_filters( "get_theme_support-{$feature}", $feature_args, $args );
    15861604}
    15871605
    15881606/**
     
    15931611 *
    15941612 * @since 3.0.0
    15951613 * @see add_theme_support()
    1596  * @param string $feature the feature being added
     1614 * @param string $feature The feature being removed.
    15971615 * @return bool Whether feature was removed.
    15981616 */
    15991617function remove_theme_support( $feature ) {
     
    16091627 *
    16101628 * @access private
    16111629 * @since 3.1.0
     1630 * @param string $feature The feature being removed.
     1631 * @return bool Whether feature was removed.
    16121632 */
    16131633function _remove_theme_support( $feature ) {
    1614         global $_wp_theme_features;
    1615 
    16161634        switch ( $feature ) {
    16171635                case 'custom-header-uploads' :
    1618                         if ( ! isset( $_wp_theme_features['custom-header'] ) )
     1636                        if ( ! current_theme_supports( 'custom-header' ) )
    16191637                                return false;
    16201638                        add_theme_support( 'custom-header', array( 'uploads' => false ) );
    16211639                        return; // Do not continue - custom-header-uploads no longer exists.
    16221640        }
    16231641
    1624         if ( ! isset( $_wp_theme_features[ $feature ] ) )
     1642        if ( ! current_theme_supports( $feature ) )
    16251643                return false;
    16261644
    16271645        switch ( $feature ) {
    16281646                case 'custom-header' :
    16291647                        if ( ! did_action( 'wp_loaded' ) )
    16301648                                break;
    1631                         $support = get_theme_support( 'custom-header' );
    1632                         if ( $support[0]['wp-head-callback'] )
    1633                                 remove_action( 'wp_head', $support[0]['wp-head-callback'] );
     1649                        remove_action( 'wp_head', get_theme_support( 'custom-header', 'wp-head-callback' ) );
    16341650                        remove_action( 'admin_menu', array( $GLOBALS['custom_image_header'], 'init' ) );
    16351651                        unset( $GLOBALS['custom_image_header'] );
    16361652                        break;
     
    16381654                case 'custom-background' :
    16391655                        if ( ! did_action( 'wp_loaded' ) )
    16401656                                break;
    1641                         $support = get_theme_support( 'custom-background' );
    1642                         remove_action( 'wp_head', $support[0]['wp-head-callback'] );
     1657                        remove_action( 'wp_head', get_theme_support( 'custom-background', 'wp-head-callback' ) );
    16431658                        remove_action( 'admin_menu', array( $GLOBALS['custom_background'], 'init' ) );
    16441659                        unset( $GLOBALS['custom_background'] );
    16451660                        break;
    16461661        }
    16471662
    1648         unset( $_wp_theme_features[ $feature ] );
     1663        unset( $GLOBALS['_wp_theme_features'][ $feature ] );
    16491664        return true;
    16501665}
    16511666
     
    16531668 * Checks a theme's support for a given feature
    16541669 *
    16551670 * @since 2.9.0
    1656  * @param string $feature the feature being checked
    1657  * @return boolean
     1671 * @param string $feature The feature being checked.
     1672 * @return boolean Whether the current theme supports the given feature.
    16581673 */
    16591674function current_theme_supports( $feature ) {
    1660         global $_wp_theme_features;
    1661 
    16621675        if ( 'custom-header-uploads' == $feature )
    16631676                return current_theme_supports( 'custom-header', 'uploads' );
    16641677
    1665         if ( !isset( $_wp_theme_features[$feature] ) )
     1678        if ( ! get_theme_support( $feature ) )
    16661679                return false;
    16671680
    16681681        // If no args passed then no extra checks need be performed
     
    16761689                        // post-thumbnails can be registered for only certain content/post types by passing
    16771690                        // an array of types to add_theme_support(). If no array was passed, then
    16781691                        // any type is accepted
    1679                         if ( true === $_wp_theme_features[$feature] )  // Registered for all types
     1692                        if ( true === get_theme_support( $feature ) )  // Registered for all types
    16801693                                return true;
    16811694                        $content_type = $args[0];
    1682                         return in_array( $content_type, $_wp_theme_features[$feature][0] );
     1695                        return in_array( $content_type, get_theme_support( $feature ) );
    16831696                        break;
    16841697
    16851698                case 'html5':
     
    16901703                        // Specific areas of HTML5 support *must* be passed via an array to add_theme_support()
    16911704
    16921705                        $type = $args[0];
    1693                         return in_array( $type, $_wp_theme_features[$feature][0] );
     1706                        return in_array( $type, get_theme_support( $feature ) );
    16941707                        break;
    16951708
    16961709                case 'custom-header':
    16971710                case 'custom-background' :
    16981711                        // specific custom header and background capabilities can be registered by passing
    16991712                        // an array to add_theme_support()
    1700                         $header_support = $args[0];
    1701                         return ( isset( $_wp_theme_features[$feature][0][$header_support] ) && $_wp_theme_features[$feature][0][$header_support] );
     1713                        $capability = $args[0];
     1714                        return (bool) get_theme_support( $feature, $capability );
    17021715                        break;
    17031716        }
    17041717
     
    17121725         *
    17131726         * @since 3.4.0
    17141727         *
    1715          * @param bool   true     Whether the current theme supports the given feature. Default true.
    1716          * @param array  $args    Array of arguments for the feature.
    1717          * @param string $feature The theme feature.
     1728         * @param bool  true     Whether the current theme supports the given feature. Default true.
     1729         * @param array $args    Array of arguments for the feature.
     1730         * @param array $support Specific theme support for the feature.
    17181731         */
    1719         return apply_filters( "current_theme_supports-{$feature}", true, $args, $_wp_theme_features[$feature] );
     1732        return apply_filters( "current_theme_supports-{$feature}", true, $args, get_theme_support( $feature ) );
    17201733}
    17211734
    17221735/**