Make WordPress Core

Changeset 62956


Ignore:
Timestamp:
07/31/2026 01:08:01 PM (7 weeks ago)
Author:
SergeyBiryukov
Message:

Coding Standards: Use array_last() to get the last array element.

This commit replaces $array[ count( $array ) - 1 ] constructs with the array_last() function (added in PHP 8.5 and polyfilled as of WordPress 6.9) for improved readability.

Follow-up to [60672].

Props Soean, westonruter.
See #64897.

Location:
trunk/src
Files:
6 edited

Legend:

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

    r62705 r62956  
    129129        if ( str_contains( $search, ',' ) ) {
    130130                $search = explode( ',', $search );
    131                 $search = $search[ count( $search ) - 1 ];
     131                $search = array_last( $search );
    132132        }
    133133
  • trunk/src/wp-includes/class-wp-block-parser.php

    r61995 r62956  
    343343         */
    344344        public function add_inner_block( WP_Block_Parser_Block $block, $token_start, $token_length, $last_offset = null ) {
    345                 $parent                       = $this->stack[ count( $this->stack ) - 1 ];
     345                $parent                       = $this->stack[ array_key_last( $this->stack ) ];
    346346                $parent->block->innerBlocks[] = (array) $block;
    347347                $html                         = substr( $this->document, $parent->prev_offset, $token_start - $parent->prev_offset );
  • trunk/src/wp-includes/class-wp-query.php

    r62804 r62956  
    24312431                if ( '' !== $query_vars['author_name'] ) {
    24322432                        if ( str_contains( $query_vars['author_name'], '/' ) ) {
    2433                                 $query_vars['author_name'] = explode( '/', $query_vars['author_name'] );
    2434                                 if ( $query_vars['author_name'][ count( $query_vars['author_name'] ) - 1 ] ) {
    2435                                         $query_vars['author_name'] = $query_vars['author_name'][ count( $query_vars['author_name'] ) - 1 ]; // No trailing slash.
     2433                                $author_name_parts = explode( '/', $query_vars['author_name'] );
     2434                                $last_part         = array_last( $author_name_parts );
     2435
     2436                                if ( $last_part ) {
     2437                                        $query_vars['author_name'] = $last_part; // No trailing slash.
    24362438                                } else {
    2437                                         $query_vars['author_name'] = $query_vars['author_name'][ count( $query_vars['author_name'] ) - 2 ]; // There was a trailing slash.
     2439                                        $query_vars['author_name'] = $author_name_parts[ count( $author_name_parts ) - 2 ]; // There was a trailing slash.
    24382440                                }
    24392441                        }
  • trunk/src/wp-includes/class-wp-theme-json.php

    r62853 r62956  
    39953995                $is_processing_element = in_array( 'elements', $block_metadata['path'], true );
    39963996
    3997                 $current_element = $is_processing_element ? $block_metadata['path'][ count( $block_metadata['path'] ) - 1 ] : null;
     3997                $current_element = $is_processing_element ? array_last( $block_metadata['path'] ) : null;
    39983998
    39993999                $element_pseudo_allowed = array();
     
    46894689                         * $metadata['path'] = array( 'styles', 'elements', 'link' );
    46904690                         */
    4691                         $current_element = $metadata['path'][ count( $metadata['path'] ) - 1 ];
     4691                        $current_element = array_last( $metadata['path'] );
    46924692
    46934693                        /*
  • trunk/src/wp-includes/pomo/plural-forms.php

    r57883 r62956  
    129129                                                $found = false;
    130130                                                while ( ! empty( $stack ) ) {
    131                                                         $o2 = $stack[ count( $stack ) - 1 ];
     131                                                        $o2 = array_last( $stack );
    132132                                                        if ( '(' !== $o2 ) {
    133133                                                                $output[] = array( 'op', array_pop( $stack ) );
     
    164164
    165165                                                while ( ! empty( $stack ) ) {
    166                                                         $o2 = $stack[ count( $stack ) - 1 ];
     166                                                        $o2 = array_last( $stack );
    167167
    168168                                                        // Ternary is right-associative in C.
  • trunk/src/wp-trackback.php

    r62704 r62956  
    5151if ( ! isset( $_GET['tb_id'] ) || ! $_GET['tb_id'] ) {
    5252        $post_id = explode( '/', $_SERVER['REQUEST_URI'] );
    53         $post_id = (int) $post_id[ count( $post_id ) - 1 ];
     53        $post_id = (int) array_last( $post_id );
    5454}
    5555
Note: See TracChangeset for help on using the changeset viewer.