Code Modernization: Rename parameters that use reserved keywords in wp-includes/post-template.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 `$echo` parameter to `$display` in `the_title()`.
* Renames the `$class` parameter to `$css_class` in:
 * `post_class()`
 * `get_post_class()`
 * `body_class()`
 * `get_body_class()`

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], [54927], [54929], [54930], [54931], [54932], [54933], [54938], [54943], [54944], [54945], [54946], [54947], [54948], [54950], [54951], [54952].

Props jrf, aristath, poena, justinahinon, SergeyBiryukov.
See #56788.
Built from https://develop.svn.wordpress.org/trunk@54956


git-svn-id: http://core.svn.wordpress.org/trunk@54508 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Sergey Biryukov 2022-12-11 13:42:15 +00:00
parent 4c2241830e
commit 92c86d749b
2 changed files with 43 additions and 39 deletions

View File

@ -34,12 +34,12 @@ function get_the_ID() { // phpcs:ignore WordPress.NamingConventions.ValidFunctio
* *
* @since 0.71 * @since 0.71
* *
* @param string $before Optional. Markup to prepend to the title. Default empty. * @param string $before Optional. Markup to prepend to the title. Default empty.
* @param string $after Optional. Markup to append to the title. Default empty. * @param string $after Optional. Markup to append to the title. Default empty.
* @param bool $echo Optional. Whether to echo or return the title. Default true for echo. * @param bool $display Optional. Whether to echo or return the title. Default true for echo.
* @return void|string Void if `$echo` argument is true, current post title if `$echo` is false. * @return void|string Void if `$display` argument is true, current post title if `$display` is false.
*/ */
function the_title( $before = '', $after = '', $echo = true ) { function the_title( $before = '', $after = '', $display = true ) {
$title = get_the_title(); $title = get_the_title();
if ( strlen( $title ) == 0 ) { if ( strlen( $title ) == 0 ) {
@ -48,7 +48,7 @@ function the_title( $before = '', $after = '', $echo = true ) {
$title = $before . $title . $after; $title = $before . $title . $after;
if ( $echo ) { if ( $display ) {
echo $title; echo $title;
} else { } else {
return $title; return $title;
@ -453,12 +453,13 @@ function has_excerpt( $post = 0 ) {
* *
* @since 2.7.0 * @since 2.7.0
* *
* @param string|string[] $class One or more classes to add to the class list. * @param string|string[] $css_class Optional. One or more classes to add to the class list.
* @param int|WP_Post $post Optional. Post ID or post object. Defaults to the global `$post`. * Default empty.
* @param int|WP_Post $post Optional. Post ID or post object. Defaults to the global `$post`.
*/ */
function post_class( $class = '', $post = null ) { function post_class( $css_class = '', $post = null ) {
// Separates classes with a single space, collates classes for post DIV. // Separates classes with a single space, collates classes for post DIV.
echo 'class="' . esc_attr( implode( ' ', get_post_class( $class, $post ) ) ) . '"'; echo 'class="' . esc_attr( implode( ' ', get_post_class( $css_class, $post ) ) ) . '"';
} }
/** /**
@ -473,28 +474,29 @@ function post_class( $class = '', $post = null ) {
* The 'post_tag' taxonomy is a special * The 'post_tag' taxonomy is a special
* case; the class has the 'tag-' prefix instead of 'post_tag-'. All class names are * case; the class has the 'tag-' prefix instead of 'post_tag-'. All class names are
* passed through the filter, {@see 'post_class'}, with the list of class names, followed by * passed through the filter, {@see 'post_class'}, with the list of class names, followed by
* $class parameter value, with the post ID as the last parameter. * $css_class parameter value, with the post ID as the last parameter.
* *
* @since 2.7.0 * @since 2.7.0
* @since 4.2.0 Custom taxonomy class names were added. * @since 4.2.0 Custom taxonomy class names were added.
* *
* @param string|string[] $class Space-separated string or array of class names to add to the class list. * @param string|string[] $css_class Optional. Space-separated string or array of class names
* @param int|WP_Post $post Optional. Post ID or post object. * to add to the class list. Default empty.
* @param int|WP_Post $post Optional. Post ID or post object.
* @return string[] Array of class names. * @return string[] Array of class names.
*/ */
function get_post_class( $class = '', $post = null ) { function get_post_class( $css_class = '', $post = null ) {
$post = get_post( $post ); $post = get_post( $post );
$classes = array(); $classes = array();
if ( $class ) { if ( $css_class ) {
if ( ! is_array( $class ) ) { if ( ! is_array( $css_class ) ) {
$class = preg_split( '#\s+#', $class ); $css_class = preg_split( '#\s+#', $css_class );
} }
$classes = array_map( 'esc_attr', $class ); $classes = array_map( 'esc_attr', $css_class );
} else { } else {
// Ensure that we always coerce class to being an array. // Ensure that we always coerce class to being an array.
$class = array(); $css_class = array();
} }
if ( ! $post ) { if ( ! $post ) {
@ -558,9 +560,9 @@ function get_post_class( $class = '', $post = null ) {
* @param string[] $taxonomies List of all taxonomy names to generate classes for. * @param string[] $taxonomies List of all taxonomy names to generate classes for.
* @param int $post_id The post ID. * @param int $post_id The post ID.
* @param string[] $classes An array of post class names. * @param string[] $classes An array of post class names.
* @param string[] $class An array of additional class names added to the post. * @param string[] $css_class An array of additional class names added to the post.
*/ */
$taxonomies = apply_filters( 'post_class_taxonomies', $taxonomies, $post->ID, $classes, $class ); $taxonomies = apply_filters( 'post_class_taxonomies', $taxonomies, $post->ID, $classes, $css_class );
foreach ( (array) $taxonomies as $taxonomy ) { foreach ( (array) $taxonomies as $taxonomy ) {
if ( is_object_in_taxonomy( $post->post_type, $taxonomy ) ) { if ( is_object_in_taxonomy( $post->post_type, $taxonomy ) ) {
@ -591,11 +593,11 @@ function get_post_class( $class = '', $post = null ) {
* *
* @since 2.7.0 * @since 2.7.0
* *
* @param string[] $classes An array of post class names. * @param string[] $classes An array of post class names.
* @param string[] $class An array of additional class names added to the post. * @param string[] $css_class An array of additional class names added to the post.
* @param int $post_id The post ID. * @param int $post_id The post ID.
*/ */
$classes = apply_filters( 'post_class', $classes, $class, $post->ID ); $classes = apply_filters( 'post_class', $classes, $css_class, $post->ID );
return array_unique( $classes ); return array_unique( $classes );
} }
@ -605,11 +607,12 @@ function get_post_class( $class = '', $post = null ) {
* *
* @since 2.8.0 * @since 2.8.0
* *
* @param string|string[] $class Space-separated string or array of class names to add to the class list. * @param string|string[] $css_class Optional. Space-separated string or array of class names
* to add to the class list. Default empty.
*/ */
function body_class( $class = '' ) { function body_class( $css_class = '' ) {
// Separates class names with a single space, collates class names for body element. // Separates class names with a single space, collates class names for body element.
echo 'class="' . esc_attr( implode( ' ', get_body_class( $class ) ) ) . '"'; echo 'class="' . esc_attr( implode( ' ', get_body_class( $css_class ) ) ) . '"';
} }
/** /**
@ -619,10 +622,11 @@ function body_class( $class = '' ) {
* *
* @global WP_Query $wp_query WordPress Query object. * @global WP_Query $wp_query WordPress Query object.
* *
* @param string|string[] $class Space-separated string or array of class names to add to the class list. * @param string|string[] $css_class Optional. Space-separated string or array of class names
* to add to the class list. Default empty.
* @return string[] Array of class names. * @return string[] Array of class names.
*/ */
function get_body_class( $class = '' ) { function get_body_class( $css_class = '' ) {
global $wp_query; global $wp_query;
$classes = array(); $classes = array();
@ -830,14 +834,14 @@ function get_body_class( $class = '' ) {
} }
} }
if ( ! empty( $class ) ) { if ( ! empty( $css_class ) ) {
if ( ! is_array( $class ) ) { if ( ! is_array( $css_class ) ) {
$class = preg_split( '#\s+#', $class ); $css_class = preg_split( '#\s+#', $css_class );
} }
$classes = array_merge( $classes, $class ); $classes = array_merge( $classes, $css_class );
} else { } else {
// Ensure that we always coerce class to being an array. // Ensure that we always coerce class to being an array.
$class = array(); $css_class = array();
} }
$classes = array_map( 'esc_attr', $classes ); $classes = array_map( 'esc_attr', $classes );
@ -847,10 +851,10 @@ function get_body_class( $class = '' ) {
* *
* @since 2.8.0 * @since 2.8.0
* *
* @param string[] $classes An array of body class names. * @param string[] $classes An array of body class names.
* @param string[] $class An array of additional class names added to the body. * @param string[] $css_class An array of additional class names added to the body.
*/ */
$classes = apply_filters( 'body_class', $classes, $class ); $classes = apply_filters( 'body_class', $classes, $css_class );
return array_unique( $classes ); return array_unique( $classes );
} }

View File

@ -16,7 +16,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '6.2-alpha-54955'; $wp_version = '6.2-alpha-54956';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.