Make WordPress Core

Changeset 53888


Ignore:
Timestamp:
08/12/2022 12:09:10 PM (23 months ago)
Author:
audrasjb
Message:

General: Add required fields helper functions for better reusability.

This changeset introduces new wp_required_field_indicator() and wp_required_field_message() helper functions to generate reusable and consistent required field indicator and message. It also implements these functions in various admin screens.

Props sabernhardt, ryokuhi, joedolson, audrasjb, SergeyBiryukov.
Fixes #54394.

Location:
trunk/src
Files:
4 edited

Legend:

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

    r53777 r53888  
    17041704        <tr><td style='display:none' colspan='2' class='image-editor' id='image-editor-$post->ID'></td></tr>\n
    17051705        <tr><td colspan='2'><p class='media-types media-types-required-info'>" .
    1706             /* translators: %s: Asterisk symbol (*). */
    1707             sprintf( __( 'Required fields are marked %s' ), '<span class="required">*</span>' ) .
     1706            wp_required_field_message() .
    17081707        "</p></td></tr>\n";
    17091708
     
    17851784        }
    17861785
    1787         $required      = $field['required'] ? '<span class="required">*</span>' : '';
     1786        $required      = $field['required'] ? ' ' . wp_required_field_indicator() : '';
    17881787        $required_attr = $field['required'] ? ' required' : '';
    17891788        $class         = $id;
     
    19761975
    19771976        $readonly      = ! $user_can_edit && ! empty( $field['taxonomy'] ) ? " readonly='readonly' " : '';
    1978         $required      = $field['required'] ? '<span class="required">*</span>' : '';
     1977        $required      = $field['required'] ? ' ' . wp_required_field_indicator() : '';
    19791978        $required_attr = $field['required'] ? ' required' : '';
    19801979        $class         = 'compat-field-' . $id;
     
    20322031    if ( $item ) {
    20332032        $item = '<p class="media-types media-types-required-info">' .
    2034             /* translators: %s: Asterisk symbol (*). */
    2035             sprintf( __( 'Required fields are marked %s' ), '<span class="required">*</span>' ) .
     2033            wp_required_field_message() .
    20362034            '</p>' .
    20372035            '<table class="compat-attachment-fields">' . $item . '</table>';
     
    29302928    <p class="media-types"><label><input type="radio" name="media_type" value="image" id="image-only"' . checked( 'image-only', $view, false ) . ' /> ' . __( 'Image' ) . '</label> &nbsp; &nbsp; <label><input type="radio" name="media_type" value="generic" id="not-image"' . checked( 'not-image', $view, false ) . ' /> ' . __( 'Audio, Video, or Other File' ) . '</label></p>
    29312929    <p class="media-types media-types-required-info">' .
    2932         /* translators: %s: Asterisk symbol (*). */
    2933         sprintf( __( 'Required fields are marked %s' ), '<span class="required">*</span>' ) .
     2930        wp_required_field_message() .
    29342931    '</p>
    29352932    <table class="describe ' . $table_class . '"><tbody>
    29362933        <tr>
    29372934            <th scope="row" class="label" style="width:130px;">
    2938                 <label for="src"><span class="alignleft">' . __( 'URL' ) . '</span> <span class="required">*</span></label>
     2935                <label for="src"><span class="alignleft">' . __( 'URL' ) . '</span> ' . wp_required_field_indicator() . '</label>
    29392936                <span class="alignright" id="status_img"></span>
    29402937            </th>
     
    29442941        <tr>
    29452942            <th scope="row" class="label">
    2946                 <label for="title"><span class="alignleft">' . __( 'Title' ) . '</span> <span class="required">*</span></label>
     2943                <label for="title"><span class="alignleft">' . __( 'Title' ) . '</span> ' . wp_required_field_indicator() . '</label>
    29472944            </th>
    29482945            <td class="field"><input id="title" name="title" value="" type="text" required /></td>
     
    29532950        <tr class="image-only">
    29542951            <th scope="row" class="label">
    2955                 <label for="alt"><span class="alignleft">' . __( 'Alternative Text' ) . '</span></label>
     2952                <label for="alt"><span class="alignleft">' . __( 'Alternative Text' ) . '</span> ' . wp_required_field_indicator() . '</label>
    29562953            </th>
    29572954            <td class="field"><input id="alt" name="alt" value="" type="text" required />
  • trunk/src/wp-admin/network/site-new.php

    r52978 r53888  
    194194}
    195195?>
    196 <p>
    197 <?php
    198 printf(
    199     /* translators: %s: Asterisk symbol (*). */
    200     __( 'Required fields are marked %s' ),
    201     '<span class="required">*</span>'
    202 );
    203 ?>
    204 </p>
     196<p><?php echo wp_required_field_message(); ?></p>
    205197<form method="post" action="<?php echo esc_url( network_admin_url( 'site-new.php?action=add-site' ) ); ?>" novalidate="novalidate">
    206198<?php wp_nonce_field( 'add-blog', '_wpnonce_add-blog' ); ?>
    207199    <table class="form-table" role="presentation">
    208200        <tr class="form-field form-required">
    209             <th scope="row"><label for="site-address"><?php _e( 'Site Address (URL)' ); ?> <span class="required">*</span></label></th>
     201            <th scope="row">
     202                <label for="site-address">
     203                    <?php
     204                    _e( 'Site Address (URL)' );
     205                    echo ' ' . wp_required_field_indicator();
     206                    ?>
     207                </label>
     208            </th>
    210209            <td>
    211210            <?php if ( is_subdomain_install() ) { ?>
     
    223222        </tr>
    224223        <tr class="form-field form-required">
    225             <th scope="row"><label for="site-title"><?php _e( 'Site Title' ); ?> <span class="required">*</span></label></th>
     224            <th scope="row">
     225                <label for="site-title">
     226                    <?php
     227                    _e( 'Site Title' );
     228                    echo ' ' . wp_required_field_indicator();
     229                    ?>
     230                </label>
     231            </th>
    226232            <td><input name="blog[title]" type="text" class="regular-text" id="site-title" required /></td>
    227233        </tr>
     
    258264        <?php endif; // Languages. ?>
    259265        <tr class="form-field form-required">
    260             <th scope="row"><label for="admin-email"><?php _e( 'Admin Email' ); ?> <span class="required">*</span></label></th>
     266            <th scope="row">
     267                <label for="admin-email">
     268                    <?php
     269                    _e( 'Admin Email' );
     270                    echo ' ' . wp_required_field_indicator();
     271                    ?>
     272                </label>
     273            </th>
    261274            <td><input name="blog[email]" type="email" class="regular-text wp-suggest-user" id="admin-email" data-autocomplete-type="search" data-autocomplete-field="user_email" aria-describedby="site-admin-email" required /></td>
    262275        </tr>
  • trunk/src/wp-includes/comment-template.php

    r53796 r53888  
    23552355    $checked_attribute  = ( $html5 ? ' checked' : ' checked="checked"' );
    23562356
    2357     // Identify required fields visually.
    2358     $required_indicator = ' <span class="required" aria-hidden="true">*</span>';
     2357    // Identify required fields visually and create a message about the indicator.
     2358    $required_indicator = ' ' . wp_required_field_indicator();
     2359    $required_text      = ' ' . wp_required_field_message();
    23592360
    23602361    $fields = array(
     
    24202421        }
    24212422    }
    2422 
    2423     $required_text = sprintf(
    2424         /* translators: %s: Asterisk symbol (*). */
    2425         ' <span class="required-field-message" aria-hidden="true">' . __( 'Required fields are marked %s' ) . '</span>',
    2426         trim( $required_indicator )
    2427     );
    24282423
    24292424    /**
  • trunk/src/wp-includes/general-template.php

    r53846 r53888  
    50235023
    50245024/**
     5025 * Assigns a visual indicator for required form fields.
     5026 *
     5027 * @since 6.1.0
     5028 *
     5029 * @return string Indicator glyph wrapped in a `span` tag.
     5030 */
     5031function wp_required_field_indicator() {
     5032    /* translators: Character to identify required form fields. */
     5033    $glyph     = __( '*' );
     5034    $indicator = '<span class="required" aria-hidden="true">' . esc_html( $glyph ) . '</span>';
     5035
     5036    return $indicator;
     5037}
     5038
     5039/**
     5040 * Creates a message to explain required form fields.
     5041 *
     5042 * @since 6.1.0
     5043 *
     5044 * @return string Message text and glyph wrapped in a `span` tag.
     5045 */
     5046function wp_required_field_message() {
     5047    $message = sprintf(
     5048        '<span class="required-field-message" aria-hidden="true">%s</span>',
     5049        /* translators: %s: Asterisk symbol (*). */
     5050        sprintf( __( 'Required fields are marked %s' ), wp_required_field_indicator() )
     5051    );
     5052
     5053    return $message;
     5054}
     5055
     5056/**
    50255057 * Default settings for heartbeat.
    50265058 *
Note: See TracChangeset for help on using the changeset viewer.