Make WordPress Core

Changeset 56362


Ignore:
Timestamp:
08/07/2023 09:59:20 AM (3 years ago)
Author:
SergeyBiryukov
Message:

Coding Standards: Use strict comparison in wp-includes/class-wp.php.

Includes minor code layout fixes for better readability.

Follow-up to [1043], [2534], [2584], [2627], [2958], [3252], [3564], [21818], [37356].

Props aristath, poena, afercia, SergeyBiryukov.
See #58831.

File:
1 edited

Legend:

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

    r56180 r56362  
    206206                        } else {
    207207                                // If the request uri is the index, blank it out so that we don't try to match it against a rule.
    208                                 if ( $req_uri == $wp_rewrite->index ) {
     208                                if ( $req_uri === $wp_rewrite->index ) {
    209209                                        $req_uri = '';
    210210                                }
     211
    211212                                $requested_path = $req_uri;
    212213                        }
     214
    213215                        $requested_file = $req_uri;
    214216
     
    227229                                foreach ( (array) $rewrite as $match => $query ) {
    228230                                        // If the requested file is the anchor of the match, prepend it to the path info.
    229                                         if ( ! empty( $requested_file ) && str_starts_with( $match, $requested_file ) && $requested_file != $requested_path ) {
     231                                        if ( ! empty( $requested_file )
     232                                                && str_starts_with( $match, $requested_file )
     233                                                && $requested_file !== $requested_path
     234                                        ) {
    230235                                                $request_match = $requested_file . '/' . $requested_path;
    231236                                        }
    232237
    233                                         if ( preg_match( "#^$match#", $request_match, $matches ) ||
    234                                                 preg_match( "#^$match#", urldecode( $request_match ), $matches ) ) {
    235 
    236                                                 if ( $wp_rewrite->use_verbose_page_rules && preg_match( '/pagename=\$matches\[([0-9]+)\]/', $query, $varmatch ) ) {
     238                                        if ( preg_match( "#^$match#", $request_match, $matches )
     239                                                || preg_match( "#^$match#", urldecode( $request_match ), $matches )
     240                                        ) {
     241
     242                                                if ( $wp_rewrite->use_verbose_page_rules
     243                                                        && preg_match( '/pagename=\$matches\[([0-9]+)\]/', $query, $varmatch )
     244                                                ) {
    237245                                                        // This is a verbose page match, let's check to be sure about it.
    238246                                                        $page = get_page_by_path( $matches[ $varmatch[1] ] );
     247
    239248                                                        if ( ! $page ) {
    240249                                                                continue;
     
    242251
    243252                                                        $post_status_obj = get_post_status_object( $page->post_status );
     253
    244254                                                        if ( ! $post_status_obj->public && ! $post_status_obj->protected
    245                                                                 && ! $post_status_obj->private && $post_status_obj->exclude_from_search ) {
     255                                                                && ! $post_status_obj->private && $post_status_obj->exclude_from_search
     256                                                        ) {
    246257                                                                continue;
    247258                                                        }
     
    268279
    269280                                // If we're processing a 404 request, clear the error var since we found something.
    270                                 if ( '404' == $error ) {
     281                                if ( '404' === $error ) {
    271282                                        unset( $error, $_GET['error'] );
    272283                                }
     
    274285
    275286                        // If req_uri is empty or if it is a request for ourself, unset error.
    276                         if ( empty( $requested_path ) || $requested_file == $self || str_contains( $_SERVER['PHP_SELF'], 'wp-admin/' ) ) {
     287                        if ( empty( $requested_path ) || $requested_file === $self
     288                                || str_contains( $_SERVER['PHP_SELF'], 'wp-admin/' )
     289                        ) {
    277290                                unset( $error, $_GET['error'] );
    278291
     
    307320                        if ( isset( $this->extra_query_vars[ $wpvar ] ) ) {
    308321                                $this->query_vars[ $wpvar ] = $this->extra_query_vars[ $wpvar ];
    309                         } elseif ( isset( $_GET[ $wpvar ] ) && isset( $_POST[ $wpvar ] ) && $_GET[ $wpvar ] !== $_POST[ $wpvar ] ) {
    310                                 wp_die( __( 'A variable mismatch has been detected.' ), __( 'Sorry, you are not allowed to view this item.' ), 400 );
     322                        } elseif ( isset( $_GET[ $wpvar ] ) && isset( $_POST[ $wpvar ] )
     323                                && $_GET[ $wpvar ] !== $_POST[ $wpvar ]
     324                        ) {
     325                                wp_die(
     326                                        __( 'A variable mismatch has been detected.' ),
     327                                        __( 'Sorry, you are not allowed to view this item.' ),
     328                                        400
     329                                );
    311330                        } elseif ( isset( $_POST[ $wpvar ] ) ) {
    312331                                $this->query_vars[ $wpvar ] = $_POST[ $wpvar ];
     
    358377                if ( isset( $this->query_vars['post_type'] ) ) {
    359378                        $queryable_post_types = get_post_types( array( 'publicly_queryable' => true ) );
     379
    360380                        if ( ! is_array( $this->query_vars['post_type'] ) ) {
    361381                                if ( ! in_array( $this->query_vars['post_type'], $queryable_post_types, true ) ) {
     
    435455                if ( ! empty( $this->query_vars['error'] ) ) {
    436456                        $status = (int) $this->query_vars['error'];
     457
    437458                        if ( 404 === $status ) {
    438459                                if ( ! is_user_logged_in() ) {
    439460                                        $headers = array_merge( $headers, wp_get_nocache_headers() );
    440461                                }
     462
    441463                                $headers['Content-Type'] = get_option( 'html_type' ) . '; charset=' . get_option( 'blog_charset' );
    442464                        } elseif ( in_array( $status, array( 403, 500, 502, 503 ), true ) ) {
     
    451473                                $type = get_default_feed();
    452474                        }
     475
    453476                        $headers['Content-Type'] = feed_content_type( $type ) . '; charset=' . get_option( 'blog_charset' );
    454477
     
    468491                                $wp_last_modified_post    = mysql2date( $date_format, get_lastpostmodified( 'GMT' ), false );
    469492                                $wp_last_modified_comment = mysql2date( $date_format, get_lastcommentmodified( 'GMT' ), false );
     493
    470494                                if ( strtotime( $wp_last_modified_post ) > strtotime( $wp_last_modified_comment ) ) {
    471495                                        $wp_last_modified = $wp_last_modified_post;
     
    502526
    503527                        if ( ( $client_last_modified && $client_etag ) ?
    504                                         ( ( $client_modified_timestamp >= $wp_modified_timestamp ) && ( $client_etag == $wp_etag ) ) :
    505                                         ( ( $client_modified_timestamp >= $wp_modified_timestamp ) || ( $client_etag == $wp_etag ) ) ) {
     528                                        ( ( $client_modified_timestamp >= $wp_modified_timestamp ) && ( $client_etag === $wp_etag ) ) :
     529                                        ( ( $client_modified_timestamp >= $wp_modified_timestamp ) || ( $client_etag === $wp_etag ) )
     530                        ) {
    506531                                $status        = 304;
    507532                                $exit_required = true;
     
    571596        public function build_query_string() {
    572597                $this->query_string = '';
     598
    573599                foreach ( (array) array_keys( $this->query_vars ) as $wpvar ) {
    574                         if ( '' != $this->query_vars[ $wpvar ] ) {
     600                        if ( '' !== $this->query_vars[ $wpvar ] ) {
    575601                                $this->query_string .= ( strlen( $this->query_string ) < 1 ) ? '' : '&';
     602
    576603                                if ( ! is_scalar( $this->query_vars[ $wpvar ] ) ) { // Discard non-scalars.
    577604                                        continue;
    578605                                }
     606
    579607                                $this->query_string .= $wpvar . '=' . rawurlencode( $this->query_vars[ $wpvar ] );
    580608                        }
     
    596624                                'query_vars, request'
    597625                        );
     626
    598627                        parse_str( $this->query_string, $this->query_vars );
    599628                }
Note: See TracChangeset for help on using the changeset viewer.