Make WordPress Core


Ignore:
Timestamp:
12/02/2022 06:51:56 PM (17 months ago)
Author:
SergeyBiryukov
Message:

Code Modernization: Rename parameters that use reserved keywords in wp-includes/formatting.php.

While using reserved PHP keywords as parameter name labels is allowed, in the context of function calls using named parameters in PHP 8.0+, this will easily lead to confusion. To avoid that, it is recommended not to use reserved keywords as function parameter names.

This commit:

  • Renames the $string parameter to $text in:
    • _wp_specialchars()
    • wp_specialchars_decode()
    • wp_check_invalid_utf8()
    • remove_accents()
    • _split_str_by_whitespace()
    • wp_strip_all_tags()
  • Renames the $string parameter to $value in:
    • backslashit()
    • trailingslashit()
    • untrailingslashit()
  • Renames the $string parameter to $subject in wp_iso_descrambler().
  • Renames the $match parameter to $matches in _wp_iso_convert().
  • Renames the $string parameter to $date_string in:
    • get_gmt_from_date()
    • get_date_from_gmt()
  • Renames the $string parameter to$input in wp_parse_str().
  • Renames the $string parameter to $content in wp_pre_kses_block_attributes().
  • Amends the $text parameter in wp_pre_kses_less_than() for consistency.

Follow-up to [52946], [52996], [52997], [52998], [53003], [53014], [53029], [53039], [53116], [53117], [53137], [53174], [53184], [53185], [53192], [53193], [53198], [53203], [53207], [53215], [53216], [53220], [53230], [53232], [53236], [53239], [53240], [53242], [53243], [53245], [53246], [53257], [53269], [53270], [53271], [53272], [53273], [53274], [53275], [53276], [53277], [53281], [53283], [53284], [53285], [53287], [53364], [53365].

Props jrf, aristath, poena, justinahinon, SergeyBiryukov.
See #56788.

File:
1 edited

Legend:

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

    r54813 r54927  
    919919 * @access private
    920920 *
    921  * @param string       $string        The text which is to be encoded.
     921 * @param string       $text          The text which is to be encoded.
    922922 * @param int|string   $quote_style   Optional. Converts double quotes if set to ENT_COMPAT,
    923923 *                                    both single and double if set to ENT_QUOTES or none if set to ENT_NOQUOTES.
     
    932932 * @return string The encoded text with HTML entities.
    933933 */
    934 function _wp_specialchars( $string, $quote_style = ENT_NOQUOTES, $charset = false, $double_encode = false ) {
    935     $string = (string) $string;
    936 
    937     if ( 0 === strlen( $string ) ) {
     934function _wp_specialchars( $text, $quote_style = ENT_NOQUOTES, $charset = false, $double_encode = false ) {
     935    $text = (string) $text;
     936
     937    if ( 0 === strlen( $text ) ) {
    938938        return '';
    939939    }
    940940
    941941    // Don't bother if there are no specialchars - saves some processing.
    942     if ( ! preg_match( '/[&<>"\']/', $string ) ) {
    943         return $string;
     942    if ( ! preg_match( '/[&<>"\']/', $text ) ) {
     943        return $text;
    944944    }
    945945
     
    979979        // Guarantee every &entity; is valid, convert &garbage; into &amp;garbage;
    980980        // This is required for PHP < 5.4.0 because ENT_HTML401 flag is unavailable.
    981         $string = wp_kses_normalize_entities( $string, ( $quote_style & ENT_XML1 ) ? 'xml' : 'html' );
    982     }
    983 
    984     $string = htmlspecialchars( $string, $quote_style, $charset, $double_encode );
     981        $text = wp_kses_normalize_entities( $text, ( $quote_style & ENT_XML1 ) ? 'xml' : 'html' );
     982    }
     983
     984    $text = htmlspecialchars( $text, $quote_style, $charset, $double_encode );
    985985
    986986    // Back-compat.
    987987    if ( 'single' === $_quote_style ) {
    988         $string = str_replace( "'", '&#039;', $string );
    989     }
    990 
    991     return $string;
     988        $text = str_replace( "'", '&#039;', $text );
     989    }
     990
     991    return $text;
    992992}
    993993
     
    10021002 * @since 2.8.0
    10031003 *
    1004  * @param string     $string The text which is to be decoded.
     1004 * @param string     $text        The text which is to be decoded.
    10051005 * @param string|int $quote_style Optional. Converts double quotes if set to ENT_COMPAT,
    10061006 *                                both single and double if set to ENT_QUOTES or
     
    10121012 * @return string The decoded text without HTML entities.
    10131013 */
    1014 function wp_specialchars_decode( $string, $quote_style = ENT_NOQUOTES ) {
    1015     $string = (string) $string;
    1016 
    1017     if ( 0 === strlen( $string ) ) {
     1014function wp_specialchars_decode( $text, $quote_style = ENT_NOQUOTES ) {
     1015    $text = (string) $text;
     1016
     1017    if ( 0 === strlen( $text ) ) {
    10181018        return '';
    10191019    }
    10201020
    10211021    // Don't bother if there are no entities - saves a lot of processing.
    1022     if ( strpos( $string, '&' ) === false ) {
    1023         return $string;
     1022    if ( strpos( $text, '&' ) === false ) {
     1023        return $text;
    10241024    }
    10251025
     
    10801080
    10811081    // Remove zero padding on numeric entities.
    1082     $string = preg_replace( array_keys( $translation_preg ), array_values( $translation_preg ), $string );
     1082    $text = preg_replace( array_keys( $translation_preg ), array_values( $translation_preg ), $text );
    10831083
    10841084    // Replace characters according to translation table.
    1085     return strtr( $string, $translation );
     1085    return strtr( $text, $translation );
    10861086}
    10871087
     
    10911091 * @since 2.8.0
    10921092 *
    1093  * @param string $string The text which is to be checked.
     1093 * @param string $text  The text which is to be checked.
    10941094 * @param bool   $strip  Optional. Whether to attempt to strip out invalid UTF8. Default false.
    10951095 * @return string The checked text.
    10961096 */
    1097 function wp_check_invalid_utf8( $string, $strip = false ) {
    1098     $string = (string) $string;
    1099 
    1100     if ( 0 === strlen( $string ) ) {
     1097function wp_check_invalid_utf8( $text, $strip = false ) {
     1098    $text = (string) $text;
     1099
     1100    if ( 0 === strlen( $text ) ) {
    11011101        return '';
    11021102    }
     
    11081108    }
    11091109    if ( ! $is_utf8 ) {
    1110         return $string;
     1110        return $text;
    11111111    }
    11121112
     
    11191119    // We can't demand utf8 in the PCRE installation, so just return the string in those cases.
    11201120    if ( ! $utf8_pcre ) {
    1121         return $string;
    1122     }
    1123 
    1124     // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- preg_match fails when it encounters invalid UTF8 in $string.
    1125     if ( 1 === @preg_match( '/^./us', $string ) ) {
    1126         return $string;
     1121        return $text;
     1122    }
     1123
     1124    // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- preg_match fails when it encounters invalid UTF8 in $text.
     1125    if ( 1 === @preg_match( '/^./us', $text ) ) {
     1126        return $text;
    11271127    }
    11281128
    11291129    // Attempt to strip the bad chars if requested (not recommended).
    11301130    if ( $strip && function_exists( 'iconv' ) ) {
    1131         return iconv( 'utf-8', 'utf-8', $string );
     1131        return iconv( 'utf-8', 'utf-8', $text );
    11321132    }
    11331133
     
    15871587 * @since 6.1.0 Added Unicode NFC encoding normalization support.
    15881588 *
    1589  * @param string $string Text that might have accent characters.
     1589 * @param string $text  Text that might have accent characters.
    15901590 * @param string $locale Optional. The locale to use for accent removal. Some character
    15911591 *                       replacements depend on the locale being used (e.g. 'de_DE').
     
    15931593 * @return string Filtered string with replaced "nice" characters.
    15941594 */
    1595 function remove_accents( $string, $locale = '' ) {
    1596     if ( ! preg_match( '/[\x80-\xff]/', $string ) ) {
    1597         return $string;
    1598     }
    1599 
    1600     if ( seems_utf8( $string ) ) {
     1595function remove_accents( $text, $locale = '' ) {
     1596    if ( ! preg_match( '/[\x80-\xff]/', $text ) ) {
     1597        return $text;
     1598    }
     1599
     1600    if ( seems_utf8( $text ) ) {
    16011601
    16021602        // Unicode sequence normalization from NFD (Normalization Form Decomposed)
     
    16051605            && function_exists( 'normalizer_normalize' )
    16061606        ) {
    1607             if ( ! normalizer_is_normalized( $string ) ) {
    1608                 $string = normalizer_normalize( $string );
     1607            if ( ! normalizer_is_normalized( $text ) ) {
     1608                $text = normalizer_normalize( $text );
    16091609            }
    16101610        }
     
    19711971        }
    19721972
    1973         $string = strtr( $string, $chars );
     1973        $text = strtr( $text, $chars );
    19741974    } else {
    19751975        $chars = array();
     
    19881988        $chars['out'] = 'EfSZszYcYuAAAAAACEEEEIIIINOOOOOOUUUUYaaaaaaceeeeiiiinoooooouuuuyy';
    19891989
    1990         $string              = strtr( $string, $chars['in'], $chars['out'] );
     1990        $text                = strtr( $text, $chars['in'], $chars['out'] );
    19911991        $double_chars        = array();
    19921992        $double_chars['in']  = array( "\x8c", "\x9c", "\xc6", "\xd0", "\xde", "\xdf", "\xe6", "\xf0", "\xfe" );
    19931993        $double_chars['out'] = array( 'OE', 'oe', 'AE', 'DH', 'TH', 'ss', 'ae', 'dh', 'th' );
    1994         $string              = str_replace( $double_chars['in'], $double_chars['out'], $string );
    1995     }
    1996 
    1997     return $string;
     1994        $text                = str_replace( $double_chars['in'], $double_chars['out'], $text );
     1995    }
     1996
     1997    return $text;
    19981998}
    19991999
     
    27432743 * @since 0.71
    27442744 *
    2745  * @param string $string Value to which backslashes will be added.
     2745 * @param string $value Value to which backslashes will be added.
    27462746 * @return string String with backslashes inserted.
    27472747 */
    2748 function backslashit( $string ) {
    2749     if ( isset( $string[0] ) && $string[0] >= '0' && $string[0] <= '9' ) {
    2750         $string = '\\\\' . $string;
    2751     }
    2752     return addcslashes( $string, 'A..Za..z' );
     2748function backslashit( $value ) {
     2749    if ( isset( $value[0] ) && $value[0] >= '0' && $value[0] <= '9' ) {
     2750        $value = '\\\\' . $value;
     2751    }
     2752    return addcslashes( $value, 'A..Za..z' );
    27532753}
    27542754
     
    27642764 * @since 1.2.0
    27652765 *
    2766  * @param string $string What to add the trailing slash to.
     2766 * @param string $value Value to which trailing slash will be added.
    27672767 * @return string String with trailing slash added.
    27682768 */
    2769 function trailingslashit( $string ) {
    2770     return untrailingslashit( $string ) . '/';
     2769function trailingslashit( $value ) {
     2770    return untrailingslashit( $value ) . '/';
    27712771}
    27722772
     
    27792779 * @since 2.2.0
    27802780 *
    2781  * @param string $string What to remove the trailing slashes from.
     2781 * @param string $text Value from which trailing slashes will be removed.
    27822782 * @return string String without the trailing slashes.
    27832783 */
    2784 function untrailingslashit( $string ) {
    2785     return rtrim( $string, '/\\' );
     2784function untrailingslashit( $value ) {
     2785    return rtrim( $value, '/\\' );
    27862786}
    27872787
     
    30853085 *         5 => '1234567890a ', // 12 characters: Too long, but no inner whitespace on which to split.
    30863086 *         6 => '   45678   ',  // 11 characters: Perfect split.
    3087  *         7 => '1 3 5 7 90 ',  // 11 characters: End of $string.
     3087 *         7 => '1 3 5 7 90 ',  // 11 characters: End of $text.
    30883088 *     );
    30893089 *
     
    30913091 * @access private
    30923092 *
    3093  * @param string $string The string to split.
     3093 * @param string $text  The string to split.
    30943094 * @param int    $goal   The desired chunk length.
    30953095 * @return array Numeric array of chunks.
    30963096 */
    3097 function _split_str_by_whitespace( $string, $goal ) {
     3097function _split_str_by_whitespace( $text, $goal ) {
    30983098    $chunks = array();
    30993099
    3100     $string_nullspace = strtr( $string, "\r\n\t\v\f ", "\000\000\000\000\000\000" );
     3100    $string_nullspace = strtr( $text, "\r\n\t\v\f ", "\000\000\000\000\000\000" );
    31013101
    31023102    while ( $goal < strlen( $string_nullspace ) ) {
     
    31103110        }
    31113111
    3112         $chunks[]         = substr( $string, 0, $pos + 1 );
    3113         $string           = substr( $string, $pos + 1 );
     3112        $chunks[]         = substr( $text, 0, $pos + 1 );
     3113        $text             = substr( $text, $pos + 1 );
    31143114        $string_nullspace = substr( $string_nullspace, $pos + 1 );
    31153115    }
    31163116
    3117     if ( $string ) {
    3118         $chunks[] = $string;
     3117    if ( $text ) {
     3118        $chunks[] = $text;
    31193119    }
    31203120
     
    35533553 * @since 1.2.0
    35543554 *
    3555  * @param string $string Subject line.
     3555 * @param string $subject Subject line.
    35563556 * @return string Converted string to ASCII.
    35573557 */
    3558 function wp_iso_descrambler( $string ) {
     3558function wp_iso_descrambler( $subject ) {
    35593559    /* this may only work with iso-8859-1, I'm afraid */
    3560     if ( ! preg_match( '#\=\?(.+)\?Q\?(.+)\?\=#i', $string, $matches ) ) {
    3561         return $string;
    3562     } else {
    3563         $subject = str_replace( '_', ' ', $matches[2] );
    3564         return preg_replace_callback( '#\=([0-9a-f]{2})#i', '_wp_iso_convert', $subject );
    3565     }
     3560    if ( ! preg_match( '#\=\?(.+)\?Q\?(.+)\?\=#i', $subject, $matches ) ) {
     3561        return $subject;
     3562    }
     3563
     3564    $subject = str_replace( '_', ' ', $matches[2] );
     3565    return preg_replace_callback( '#\=([0-9a-f]{2})#i', '_wp_iso_convert', $subject );
    35663566}
    35673567
     
    35723572 * @access private
    35733573 *
    3574  * @param array $match The preg_replace_callback matches array.
     3574 * @param array $matches The preg_replace_callback matches array.
    35753575 * @return string Converted chars.
    35763576 */
    3577 function _wp_iso_convert( $match ) {
    3578     return chr( hexdec( strtolower( $match[1] ) ) );
     3577function _wp_iso_convert( $matches ) {
     3578    return chr( hexdec( strtolower( $matches[1] ) ) );
    35793579}
    35803580
     
    35873587 * @since 1.2.0
    35883588 *
    3589  * @param string $string The date to be converted, in the timezone of the site.
    3590  * @param string $format The format string for the returned date. Default 'Y-m-d H:i:s'.
     3589 * @param string $date_string The date to be converted, in the timezone of the site.
     3590 * @param string $format      The format string for the returned date. Default 'Y-m-d H:i:s'.
    35913591 * @return string Formatted version of the date, in UTC.
    35923592 */
    3593 function get_gmt_from_date( $string, $format = 'Y-m-d H:i:s' ) {
    3594     $datetime = date_create( $string, wp_timezone() );
     3593function get_gmt_from_date( $date_string, $format = 'Y-m-d H:i:s' ) {
     3594    $datetime = date_create( $date_string, wp_timezone() );
    35953595
    35963596    if ( false === $datetime ) {
     
    36093609 * @since 1.2.0
    36103610 *
    3611  * @param string $string The date to be converted, in UTC or GMT timezone.
    3612  * @param string $format The format string for the returned date. Default 'Y-m-d H:i:s'.
     3611 * @param string $date_string The date to be converted, in UTC or GMT timezone.
     3612 * @param string $format      The format string for the returned date. Default 'Y-m-d H:i:s'.
    36133613 * @return string Formatted version of the date, in the site's timezone.
    36143614 */
    3615 function get_date_from_gmt( $string, $format = 'Y-m-d H:i:s' ) {
    3616     $datetime = date_create( $string, new DateTimeZone( 'UTC' ) );
     3615function get_date_from_gmt( $date_string, $format = 'Y-m-d H:i:s' ) {
     3616    $datetime = date_create( $date_string, new DateTimeZone( 'UTC' ) );
    36173617
    36183618    if ( false === $datetime ) {
     
    50345034 * @since 2.2.1
    50355035 *
    5036  * @param string $string The string to be parsed.
    5037  * @param array  $array Variables will be stored in this array.
    5038  */
    5039 function wp_parse_str( $string, &$array ) {
    5040     parse_str( (string) $string, $array );
     5036 * @param string $input The string to be parsed.
     5037 * @param array  $result Variables will be stored in this array.
     5038 */
     5039function wp_parse_str( $input, &$result ) {
     5040    parse_str( (string) $input, $result );
    50415041
    50425042    /**
     
    50455045     * @since 2.2.1
    50465046     *
    5047      * @param array $array The array populated with variables.
     5047     * @param array $result The array populated with variables.
    50485048     */
    5049     $array = apply_filters( 'wp_parse_str', $array );
     5049    $result = apply_filters( 'wp_parse_str', $result );
    50505050}
    50515051
     
    50575057 * @since 2.3.0
    50585058 *
    5059  * @param string $text Text to be converted.
     5059 * @param string $content Text to be converted.
    50605060 * @return string Converted text.
    50615061 */
    5062 function wp_pre_kses_less_than( $text ) {
    5063     return preg_replace_callback( '%<[^>]*?((?=<)|>|$)%', 'wp_pre_kses_less_than_callback', $text );
     5062function wp_pre_kses_less_than( $content ) {
     5063    return preg_replace_callback( '%<[^>]*?((?=<)|>|$)%', 'wp_pre_kses_less_than_callback', $content );
    50645064}
    50655065
     
    50855085 * @since 5.3.1
    50865086 *
    5087  * @param string         $string            Content to be run through KSES.
     5087 * @param string         $content           Content to be run through KSES.
    50885088 * @param array[]|string $allowed_html      An array of allowed HTML elements
    50895089 *                                          and attributes, or a context name
     
    50925092 * @return string Filtered text to run through KSES.
    50935093 */
    5094 function wp_pre_kses_block_attributes( $string, $allowed_html, $allowed_protocols ) {
     5094function wp_pre_kses_block_attributes( $content, $allowed_html, $allowed_protocols ) {
    50955095    /*
    50965096     * `filter_block_content` is expected to call `wp_kses`. Temporarily remove
     
    50985098     */
    50995099    remove_filter( 'pre_kses', 'wp_pre_kses_block_attributes', 10 );
    5100     $string = filter_block_content( $string, $allowed_html, $allowed_protocols );
     5100    $content = filter_block_content( $content, $allowed_html, $allowed_protocols );
    51015101    add_filter( 'pre_kses', 'wp_pre_kses_block_attributes', 10, 3 );
    51025102
    5103     return $string;
     5103    return $content;
    51045104}
    51055105
     
    53905390 * @since 2.9.0
    53915391 *
    5392  * @param string $string        String containing HTML tags
     5392 * @param string $text          String containing HTML tags
    53935393 * @param bool   $remove_breaks Optional. Whether to remove left over line breaks and white space chars
    53945394 * @return string The processed string.
    53955395 */
    5396 function wp_strip_all_tags( $string, $remove_breaks = false ) {
    5397     $string = preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $string );
    5398     $string = strip_tags( $string );
     5396function wp_strip_all_tags( $text, $remove_breaks = false ) {
     5397    $text = preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $text );
     5398    $text = strip_tags( $text );
    53995399
    54005400    if ( $remove_breaks ) {
    5401         $string = preg_replace( '/[\r\n\t ]+/', ' ', $string );
    5402     }
    5403 
    5404     return trim( $string );
     5401        $text = preg_replace( '/[\r\n\t ]+/', ' ', $text );
     5402    }
     5403
     5404    return trim( $text );
    54055405}
    54065406
Note: See TracChangeset for help on using the changeset viewer.