Make WordPress Core

Changeset 52348


Ignore:
Timestamp:
12/09/2021 03:11:17 PM (2 years ago)
Author:
SergeyBiryukov
Message:

Coding Standards: Simplify or wrap some long conditions in wp-includes/theme.php for better readability.

Includes adjusting some DocBlocks per the documentation standards.

Follow-up to [35595], [41937], [52256].

See #53359, #53399.

File:
1 edited

Legend:

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

    r52330 r52348  
    11941194    $width  = absint( $header->width );
    11951195    $height = absint( $header->height );
     1196    $alt    = '';
    11961197
    11971198    // Use alternative text assigned to the image, if available. Otherwise, leave it empty.
    1198     if ( ! empty( $header->attachment_id ) && is_string( get_post_meta( $header->attachment_id, '_wp_attachment_image_alt', true ) ) ) {
    1199         $alt = get_post_meta( $header->attachment_id, '_wp_attachment_image_alt', true );
    1200     } else {
    1201         $alt = '';
     1199    if ( ! empty( $header->attachment_id ) ) {
     1200        $image_alt = get_post_meta( $header->attachment_id, '_wp_attachment_image_alt', true );
     1201
     1202        if ( is_string( $image_alt ) ) {
     1203            $alt = $image_alt;
     1204        }
    12021205    }
    12031206
     
    12191222        if ( is_array( $image_meta ) ) {
    12201223            $srcset = wp_calculate_image_srcset( $size_array, $header->url, $image_meta, $header->attachment_id );
    1221             $sizes  = ! empty( $attr['sizes'] ) ? $attr['sizes'] : wp_calculate_image_sizes( $size_array, $header->url, $image_meta, $header->attachment_id );
     1224
     1225            if ( ! empty( $attr['sizes'] ) ) {
     1226                $sizes = $attr['sizes'];
     1227            } else {
     1228                $sizes = wp_calculate_image_sizes( $size_array, $header->url, $image_meta, $header->attachment_id );
     1229            }
    12221230
    12231231            if ( $srcset && $sizes ) {
     
    12821290 */
    12831291function _get_random_header_data() {
     1292    global $_wp_default_headers;
    12841293    static $_wp_random_header = null;
    12851294
    12861295    if ( empty( $_wp_random_header ) ) {
    1287         global $_wp_default_headers;
    12881296        $header_image_mod = get_theme_mod( 'header_image', '' );
    12891297        $headers          = array();
     
    13071315        $_wp_random_header = (object) $headers[ array_rand( $headers ) ];
    13081316
    1309         $_wp_random_header->url           = sprintf( $_wp_random_header->url, get_template_directory_uri(), get_stylesheet_directory_uri() );
    1310         $_wp_random_header->thumbnail_url = sprintf( $_wp_random_header->thumbnail_url, get_template_directory_uri(), get_stylesheet_directory_uri() );
     1317        $_wp_random_header->url = sprintf(
     1318            $_wp_random_header->url,
     1319            get_template_directory_uri(),
     1320            get_stylesheet_directory_uri()
     1321        );
     1322
     1323        $_wp_random_header->thumbnail_url = sprintf(
     1324            $_wp_random_header->thumbnail_url,
     1325            get_template_directory_uri(),
     1326            get_stylesheet_directory_uri()
     1327        );
    13111328    }
    13121329
     
    14081425        $header_index = $header->ID;
    14091426
    1410         $header_images[ $header_index ]                      = array();
    1411         $header_images[ $header_index ]['attachment_id']     = $header->ID;
    1412         $header_images[ $header_index ]['url']               = $url;
    1413         $header_images[ $header_index ]['thumbnail_url']     = $url;
    1414         $header_images[ $header_index ]['alt_text']          = get_post_meta( $header->ID, '_wp_attachment_image_alt', true );
    1415         $header_images[ $header_index ]['attachment_parent'] = isset( $header_data['attachment_parent'] ) ? $header_data['attachment_parent'] : '';
     1427        $header_images[ $header_index ]                  = array();
     1428        $header_images[ $header_index ]['attachment_id'] = $header->ID;
     1429        $header_images[ $header_index ]['url']           = $url;
     1430        $header_images[ $header_index ]['thumbnail_url'] = $url;
     1431        $header_images[ $header_index ]['alt_text']      = get_post_meta( $header->ID, '_wp_attachment_image_alt', true );
     1432
     1433        if ( isset( $header_data['attachment_parent'] ) ) {
     1434            $header_images[ $header_index ]['attachment_parent'] = $header_data['attachment_parent'];
     1435        } else {
     1436            $header_images[ $header_index ]['attachment_parent'] = '';
     1437        }
    14161438
    14171439        if ( isset( $header_data['width'] ) ) {
     
    15041526function unregister_default_headers( $header ) {
    15051527    global $_wp_default_headers;
     1528
    15061529    if ( is_array( $header ) ) {
    15071530        array_map( 'unregister_default_headers', $header );
     
    18391862        ?>
    18401863        <style<?php echo $type_attr; ?> id="wp-custom-css">
    1841             <?php echo strip_tags( $styles ); // Note that esc_html() cannot be used because `div &gt; span` is not interpreted properly. ?>
     1864            <?php
     1865            // Note that esc_html() cannot be used because `div &gt; span` is not interpreted properly.
     1866            echo strip_tags( $styles );
     1867            ?>
    18421868        </style>
    18431869        <?php
     
    19401966 *     Args.
    19411967 *
    1942  *     @type string $preprocessed Pre-processed CSS, stored in `post_content_filtered`. Normally empty string. Optional.
    1943  *     @type string $stylesheet   Stylesheet (child theme) to update. Optional, defaults to current theme/stylesheet.
     1968 *     @type string $preprocessed Optional. Pre-processed CSS, stored in `post_content_filtered`.
     1969 *                                Normally empty string.
     1970 *     @type string $stylesheet   Optional. Stylesheet (child theme) to update.
     1971 *                                Defaults to current theme/stylesheet.
    19441972 * }
    19451973 * @return WP_Post|WP_Error Post on success, error on failure.
     
    19601988
    19611989    /**
    1962      * Filters the `css` (`post_content`) and `preprocessed` (`post_content_filtered`) args for a `custom_css` post being updated.
     1990     * Filters the `css` (`post_content`) and `preprocessed` (`post_content_filtered`) args
     1991     * for a `custom_css` post being updated.
    19631992     *
    19641993     * This filter can be used by plugin that offer CSS pre-processors, to store the original
     
    19822011     *
    19832012     *     @type string $css          CSS stored in `post_content`.
    1984      *     @type string $preprocessed Pre-processed CSS stored in `post_content_filtered`. Normally empty string.
     2013     *     @type string $preprocessed Pre-processed CSS stored in `post_content_filtered`.
     2014     *                                Normally empty string.
    19852015     * }
    19862016     * @param array $args {
     
    21682198                        array(
    21692199                            '<strong>' . _x( 'Address', 'Theme starter content' ) . "</strong>\n",
    2170                             _x( '123 Main Street', 'Theme starter content' ) . "\n" . _x( 'New York, NY 10001', 'Theme starter content' ) . "\n\n",
     2200                            _x( '123 Main Street', 'Theme starter content' ) . "\n",
     2201                            _x( 'New York, NY 10001', 'Theme starter content' ) . "\n\n",
    21712202                            '<strong>' . _x( 'Hours', 'Theme starter content' ) . "</strong>\n",
    2172                             _x( 'Monday&ndash;Friday: 9:00AM&ndash;5:00PM', 'Theme starter content' ) . "\n" . _x( 'Saturday &amp; Sunday: 11:00AM&ndash;3:00PM', 'Theme starter content' ),
     2203                            _x( 'Monday&ndash;Friday: 9:00AM&ndash;5:00PM', 'Theme starter content' ) . "\n",
     2204                            _x( 'Saturday &amp; Sunday: 11:00AM&ndash;3:00PM', 'Theme starter content' ),
    21732205                        )
    21742206                    ),
     
    23722404
    23732405                            $content[ $type ][ $sidebar_id ][] = $widget;
    2374                         } elseif ( is_string( $widget ) && ! empty( $core_content[ $type ] ) && ! empty( $core_content[ $type ][ $widget ] ) ) {
     2406                        } elseif ( is_string( $widget )
     2407                            && ! empty( $core_content[ $type ] )
     2408                            && ! empty( $core_content[ $type ][ $widget ] )
     2409                        ) {
    23752410                            $content[ $type ][ $sidebar_id ][] = $core_content[ $type ][ $widget ];
    23762411                        }
     
    23992434
    24002435                            $content[ $type ][ $nav_menu_location ]['items'][] = $nav_menu_item;
    2401                         } elseif ( is_string( $nav_menu_item ) && ! empty( $core_content[ $type ] ) && ! empty( $core_content[ $type ][ $nav_menu_item ] ) ) {
     2436                        } elseif ( is_string( $nav_menu_item )
     2437                            && ! empty( $core_content[ $type ] )
     2438                            && ! empty( $core_content[ $type ][ $nav_menu_item ] )
     2439                        ) {
    24022440                            $content[ $type ][ $nav_menu_location ]['items'][] = $core_content[ $type ][ $nav_menu_item ];
    24032441                        }
     
    25592597                $args[0] = array_intersect( $args[0], array_keys( $post_formats ) );
    25602598            } else {
    2561                 _doing_it_wrong( "add_theme_support( 'post-formats' )", __( 'You need to pass an array of post formats.' ), '5.6.0' );
     2599                _doing_it_wrong(
     2600                    "add_theme_support( 'post-formats' )",
     2601                    __( 'You need to pass an array of post formats.' ),
     2602                    '5.6.0'
     2603                );
    25622604                return false;
    25632605            }
     
    25702612                $args = array( 0 => array( 'comment-list', 'comment-form', 'search-form' ) );
    25712613            } elseif ( ! isset( $args[0] ) || ! is_array( $args[0] ) ) {
    2572                 _doing_it_wrong( "add_theme_support( 'html5' )", __( 'You need to pass an array of types.' ), '3.6.1' );
     2614                _doing_it_wrong(
     2615                    "add_theme_support( 'html5' )",
     2616                    __( 'You need to pass an array of types.' ),
     2617                    '3.6.1'
     2618                );
    25732619                return false;
    25742620            }
     
    28112857 */
    28122858function _custom_logo_header_styles() {
    2813     if ( ! current_theme_supports( 'custom-header', 'header-text' ) && get_theme_support( 'custom-logo', 'header-text' ) && ! get_theme_mod( 'header_text', true ) ) {
     2859    if ( ! current_theme_supports( 'custom-header', 'header-text' )
     2860        && get_theme_support( 'custom-logo', 'header-text' )
     2861        && ! get_theme_mod( 'header_text', true )
     2862    ) {
    28142863        $classes = (array) get_theme_support( 'custom-logo', 'header-text' );
    28152864        $classes = array_map( 'sanitize_html_class', $classes );
     
    28502899function get_theme_support( $feature, ...$args ) {
    28512900    global $_wp_theme_features;
     2901
    28522902    if ( ! isset( $_wp_theme_features[ $feature ] ) ) {
    28532903        return false;
     
    31673217
    31683218    if ( is_array( $args['show_in_rest'] ) ) {
    3169         if ( isset( $args['show_in_rest']['prepare_callback'] ) && ! is_callable( $args['show_in_rest']['prepare_callback'] ) ) {
     3219        if ( isset( $args['show_in_rest']['prepare_callback'] )
     3220            && ! is_callable( $args['show_in_rest']['prepare_callback'] )
     3221        ) {
    31703222            return new WP_Error(
    31713223                'invalid_rest_prepare_callback',
     
    32863338function check_theme_switched() {
    32873339    $stylesheet = get_option( 'theme_switched' );
     3340
    32883341    if ( $stylesheet ) {
    32893342        $old_theme = wp_get_theme( $stylesheet );
     
    33153368            do_action( 'after_switch_theme', $stylesheet, $old_theme );
    33163369        }
     3370
    33173371        flush_rewrite_rules();
    33183372
     
    33543408     * the values should contain any characters needing slashes anyway.
    33553409     */
    3356     $keys       = array( 'changeset_uuid', 'customize_changeset_uuid', 'customize_theme', 'theme', 'customize_messenger_channel', 'customize_autosaved' );
     3410    $keys       = array(
     3411        'changeset_uuid',
     3412        'customize_changeset_uuid',
     3413        'customize_theme',
     3414        'theme',
     3415        'customize_messenger_channel',
     3416        'customize_autosaved',
     3417    );
    33573418    $input_vars = array_merge(
    33583419        wp_array_slice_assoc( $_GET, $keys ),
     
    34103471
    34113472    require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
    3412     $GLOBALS['wp_customize'] = new WP_Customize_Manager( compact( 'changeset_uuid', 'theme', 'messenger_channel', 'settings_previewed', 'autosaved', 'branching' ) );
     3473    $GLOBALS['wp_customize'] = new WP_Customize_Manager(
     3474        compact(
     3475            'changeset_uuid',
     3476            'theme',
     3477            'messenger_channel',
     3478            'settings_previewed',
     3479            'autosaved',
     3480            'branching',
     3481        )
     3482    );
    34133483}
    34143484
     
    36223692
    36233693/**
    3624  * Makes sure that auto-draft posts get their post_date bumped or status changed to draft to prevent premature garbage-collection.
     3694 * Makes sure that auto-draft posts get their post_date bumped or status changed
     3695 * to draft to prevent premature garbage-collection.
    36253696 *
    36263697 * When a changeset is updated but remains an auto-draft, ensure the post_date
Note: See TracChangeset for help on using the changeset viewer.