Make WordPress Core

Ticket #43755: 43755-fix-coding-standards-2.patch

File 43755-fix-coding-standards-2.patch, 11.2 KB (added by jipmoors, 6 years ago)

Removed added type-checks and placed declarations before if-statements where possible

  • src/wp-includes/canonical.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    6767                $requested_url .= $_SERVER['REQUEST_URI'];
    6868        }
    6969
    70         $original = @parse_url( $requested_url );
     70        $original = wp_parse_url( $requested_url );
    7171        if ( false === $original ) {
    7272                return;
    7373        }
     
    120120
    121121                // Redirect ?page_id, ?p=, ?attachment_id= to their respective url's
    122122                $id = max( get_query_var( 'p' ), get_query_var( 'page_id' ), get_query_var( 'attachment_id' ) );
    123                 if ( $id && $redirect_post = get_post( $id ) ) {
    124                         $post_type_obj = get_post_type_object( $redirect_post->post_type );
    125                         if ( $post_type_obj->public && 'auto-draft' != $redirect_post->post_status ) {
    126                                 $redirect_url      = get_permalink( $redirect_post );
    127                                 $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ), $redirect_url );
     123                if ( $id ) {
     124                        $redirect_post = get_post( $id );
     125                        if ( $redirect_post ) {
     126                                $post_type_obj = get_post_type_object( $redirect_post->post_type );
     127                                if ( $post_type_obj->public && 'auto-draft' !== $redirect_post->post_status ) {
     128                                        $redirect_url      = get_permalink( $redirect_post );
     129                                        $redirect['query'] = _remove_qs_args_if_not_in_url(
     130                                                $redirect['query'],
     131                                                array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ),
     132                                                $redirect_url
     133                                        );
     134                                }
    128135                        }
    129136                }
    130137
     
    143150                }
    144151
    145152                if ( ! $redirect_url ) {
    146                         if ( $redirect_url = redirect_guess_404_permalink() ) {
    147                                 $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'page', 'feed', 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ), $redirect_url );
     153                        $redirect_url = redirect_guess_404_permalink();
     154                        if ( $redirect_url ) {
     155                                $redirect['query'] = _remove_qs_args_if_not_in_url(
     156                                        $redirect['query'],
     157                                        array( 'page', 'feed', 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ),
     158                                        $redirect_url
     159                                );
    148160                        }
    149161                }
    150162
     
    168180                                $redirect_url = get_attachment_link();
    169181                        }
    170182                } elseif ( is_single() && ! empty( $_GET['p'] ) && ! $redirect_url ) {
    171                         if ( $redirect_url = get_permalink( get_query_var( 'p' ) ) ) {
     183                        $redirect_url = get_permalink( get_query_var( 'p' ) );
     184                        if ( $redirect_url ) {
    172185                                $redirect['query'] = remove_query_arg( array( 'p', 'post_type' ), $redirect['query'] );
    173186                        }
    174187                } elseif ( is_single() && ! empty( $_GET['name'] ) && ! $redirect_url ) {
    175                         if ( $redirect_url = get_permalink( $wp_query->get_queried_object_id() ) ) {
     188                        $redirect_url = get_permalink( $wp_query->get_queried_object_id() );
     189                        if ( $redirect_url ) {
    176190                                $redirect['query'] = remove_query_arg( 'name', $redirect['query'] );
    177191                        }
    178192                } elseif ( is_page() && ! empty( $_GET['page_id'] ) && ! $redirect_url ) {
    179                         if ( $redirect_url = get_permalink( get_query_var( 'page_id' ) ) ) {
     193                        $redirect_url = get_permalink( get_query_var( 'page_id' ) );
     194                        if ( $redirect_url ) {
    180195                                $redirect['query'] = remove_query_arg( 'page_id', $redirect['query'] );
    181196                        }
    182197                } elseif ( is_page() && ! is_feed() && 'page' == get_option( 'show_on_front' ) && get_queried_object_id() == get_option( 'page_on_front' ) && ! $redirect_url ) {
    183198                        $redirect_url = home_url( '/' );
    184199                } elseif ( is_home() && ! empty( $_GET['page_id'] ) && 'page' == get_option( 'show_on_front' ) && get_query_var( 'page_id' ) == get_option( 'page_for_posts' ) && ! $redirect_url ) {
    185                         if ( $redirect_url = get_permalink( get_option( 'page_for_posts' ) ) ) {
     200                        $redirect_url = get_permalink( get_option( 'page_for_posts' ) );
     201                        if ( $redirect_url ) {
    186202                                $redirect['query'] = remove_query_arg( 'page_id', $redirect['query'] );
    187203                        }
    188204                } elseif ( ! empty( $_GET['m'] ) && ( is_year() || is_month() || is_day() ) ) {
     
    203219                        }
    204220                        // now moving on to non ?m=X year/month/day links
    205221                } elseif ( is_day() && get_query_var( 'year' ) && get_query_var( 'monthnum' ) && ! empty( $_GET['day'] ) ) {
    206                         if ( $redirect_url = get_day_link( get_query_var( 'year' ), get_query_var( 'monthnum' ), get_query_var( 'day' ) ) ) {
     222                        $redirect_url = get_day_link(
     223                                get_query_var( 'year' ),
     224                                get_query_var( 'monthnum' ),
     225                                get_query_var( 'day' )
     226                        );
     227                        if ( $redirect_url ) {
    207228                                $redirect['query'] = remove_query_arg( array( 'year', 'monthnum', 'day' ), $redirect['query'] );
    208229                        }
    209230                } elseif ( is_month() && get_query_var( 'year' ) && ! empty( $_GET['monthnum'] ) ) {
    210                         if ( $redirect_url = get_month_link( get_query_var( 'year' ), get_query_var( 'monthnum' ) ) ) {
     231                        $redirect_url = get_month_link( get_query_var( 'year' ), get_query_var( 'monthnum' ) );
     232                        if ( $redirect_url ) {
    211233                                $redirect['query'] = remove_query_arg( array( 'year', 'monthnum' ), $redirect['query'] );
    212234                        }
    213235                } elseif ( is_year() && ! empty( $_GET['year'] ) ) {
    214                         if ( $redirect_url = get_year_link( get_query_var( 'year' ) ) ) {
     236                        $redirect_url = get_year_link( get_query_var( 'year' ) );
     237                        if ( $redirect_url ) {
    215238                                $redirect['query'] = remove_query_arg( 'year', $redirect['query'] );
    216239                        }
    217240                } elseif ( is_author() && ! empty( $_GET['author'] ) && preg_match( '|^[0-9]+$|', $_GET['author'] ) ) {
    218241                        $author = get_userdata( get_query_var( 'author' ) );
    219242                        if ( ( false !== $author ) && $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE $wpdb->posts.post_author = %d AND $wpdb->posts.post_status = 'publish' LIMIT 1", $author->ID ) ) ) {
    220                                 if ( $redirect_url = get_author_posts_url( $author->ID, $author->user_nicename ) ) {
     243                                $redirect_url = get_author_posts_url( $author->ID, $author->user_nicename );
     244                                if ( $redirect_url ) {
    221245                                        $redirect['query'] = remove_query_arg( 'author', $redirect['query'] );
    222246                                }
    223247                        }
     
    229253                        }
    230254
    231255                        $obj = $wp_query->get_queried_object();
    232                         if ( $term_count <= 1 && ! empty( $obj->term_id ) && ( $tax_url = get_term_link( (int) $obj->term_id, $obj->taxonomy ) ) && ! is_wp_error( $tax_url ) ) {
    233                                 if ( ! empty( $redirect['query'] ) ) {
     256                        if ( $term_count <= 1 && ! empty( $obj->term_id ) ) {
     257                                $tax_url = get_term_link( (int) $obj->term_id, $obj->taxonomy );
     258                                if ( ! empty( $tax_url ) && ! is_wp_error( $tax_url ) && ! empty( $redirect['query'] ) ) {
    234259                                        // Strip taxonomy query vars off the url.
    235260                                        $qv_remove = array( 'term', 'taxonomy' );
    236261                                        if ( is_category() ) {
     
    252277                                                $redirect['query'] = remove_query_arg( $qv_remove, $redirect['query'] ); //Remove all of the per-tax qv's
    253278
    254279                                                // Create the destination url for this taxonomy
    255                                                 $tax_url = parse_url( $tax_url );
     280                                                $tax_url = wp_parse_url( $tax_url );
    256281                                                if ( ! empty( $tax_url['query'] ) ) { // Taxonomy accessible via ?taxonomy=..&term=.. or any custom qv..
    257282                                                        parse_str( $tax_url['query'], $query_vars );
    258283                                                        $redirect['query'] = add_query_arg( $query_vars, $redirect['query'] );
     
    268293                                        }
    269294                                }
    270295                        }
    271                 } elseif ( is_single() && strpos( $wp_rewrite->permalink_structure, '%category%' ) !== false && $cat = get_query_var( 'category_name' ) ) {
    272                         $category = get_category_by_path( $cat );
    273                         if ( ( ! $category || is_wp_error( $category ) ) || ! has_term( $category->term_id, 'category', $wp_query->get_queried_object_id() ) ) {
    274                                 $redirect_url = get_permalink( $wp_query->get_queried_object_id() );
     296                } elseif ( is_single() && strpos( $wp_rewrite->permalink_structure, '%category%' ) !== false ) {
     297                        $cat = get_query_var( 'category_name' );
     298                        if ( $cat ) {
     299                                $category = get_category_by_path( $cat );
     300                                if ( ( ! $category || is_wp_error( $category ) )
     301                                        || ! has_term( $category->term_id, 'category', $wp_query->get_queried_object_id() )
     302                                ) {
     303                                        $redirect_url = get_permalink( $wp_query->get_queried_object_id() );
     304                                }
    275305                        }
    276306                }
    277307
     
    378408        $redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] );
    379409        if ( $redirect_url && ! empty( $redirect['query'] ) ) {
    380410                parse_str( $redirect['query'], $_parsed_query );
    381                 $redirect = @parse_url( $redirect_url );
     411                $redirect = wp_parse_url( $redirect_url );
    382412
    383413                if ( ! empty( $_parsed_query['name'] ) && ! empty( $redirect['query'] ) ) {
    384414                        parse_str( $redirect['query'], $_parsed_redirect_query );
     
    396426        }
    397427
    398428        if ( $redirect_url ) {
    399                 $redirect = @parse_url( $redirect_url );
     429                $redirect = wp_parse_url( $redirect_url );
    400430        }
    401431
    402432        // www.example.com vs example.com
    403         $user_home = @parse_url( home_url() );
     433        $user_home = wp_parse_url( home_url() );
    404434        if ( ! empty( $user_home['host'] ) ) {
    405435                $redirect['host'] = $user_home['host'];
    406436        }
     
    608638 * @return string The altered query string
    609639 */
    610640function _remove_qs_args_if_not_in_url( $query_string, array $args_to_check, $url ) {
    611         $parsed_url = @parse_url( $url );
     641        $parsed_url = wp_parse_url( $url );
    612642        if ( ! empty( $parsed_url['query'] ) ) {
    613643                parse_str( $parsed_url['query'], $parsed_query );
    614644                foreach ( $args_to_check as $qv ) {
     
    631661 * @return string The altered URL.
    632662 */
    633663function strip_fragment_from_url( $url ) {
    634         $parsed_url = @parse_url( $url );
     664        $parsed_url = wp_parse_url( $url );
    635665        if ( ! empty( $parsed_url['host'] ) ) {
    636666                // This mirrors code in redirect_canonical(). It does not handle every case.
    637667                $url = $parsed_url['scheme'] . '://' . $parsed_url['host'];
     
    670700                if ( get_query_var( 'post_type' ) ) {
    671701                        $where .= $wpdb->prepare( ' AND post_type = %s', get_query_var( 'post_type' ) );
    672702                } else {
    673                         $where .= " AND post_type IN ('" . implode( "', '", get_post_types( array( 'public' => true ) ) ) . "')";
     703                        // Fetch the variables needed to place in the post_types query.
     704                        $post_types = get_post_types( array( 'public' => true ) );
     705                        if ( is_array( $post_types ) && ! array() === $post_types ) {
     706                                // Create placeholders for all the variables.
     707                                $placeholders = implode( ', ', array_fill( 0, count( $post_types ), '%s' ) );
     708
     709                                /*
     710                                 * Add the statement to the where condition.
     711                                 *
     712                                 * The ignore is needed because the placeholders are inside a variable, which is concatinated to the
     713                                 * where statement, but the sniff is not picking it up correctly yet.
     714                                 *
     715                                 * This is a WPCS problem and has been fixed in the following PR
     716                                 * @link https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/pull/515
     717                                 */
     718                                $post_type_where = sprintf( ' AND post_type IN ( %s )', $placeholders );
     719                                $where          .= $wpdb->prepare(
     720                                        $post_type_where, // phpcs:ignore WordPress.WP.PreparedSQL.NotPrepared
     721                                        $post_types
     722                                );
     723                        }
    674724                }
    675725
    676726                if ( get_query_var( 'year' ) ) {
     
    683733                        $where .= $wpdb->prepare( ' AND DAYOFMONTH(post_date) = %d', get_query_var( 'day' ) );
    684734                }
    685735
    686                 $post_id = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE $where AND post_status = 'publish'" );
     736                /*
     737                 * Build the compelte SQL statement and execute it.
     738                 *
     739                 * The ignore has been added because all of the variables used are being prepared in the above lines.
     740                 */
     741                $post_id = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE $where AND post_status = 'publish'" ); // phpcs:ignore WordPress.WP.PreparedSQL.NotPrepared
    687742                if ( ! $post_id ) {
    688743                        return false;
    689744                }