Make WordPress Core

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

File 43755-fix-coding-standards.patch, 13.9 KB (added by jipmoors, 7 years ago)
  • 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        }
     
    9393                $redirect['query'] = remove_query_arg( 'preview', $redirect['query'] );
    9494        }
    9595
    96         if ( is_feed() && ( $id = get_query_var( 'p' ) ) ) {
    97                 if ( $redirect_url = get_post_comments_feed_link( $id, get_query_var( 'feed' ) ) ) {
    98                         $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type', 'feed' ), $redirect_url );
    99                         $redirect['path']  = parse_url( $redirect_url, PHP_URL_PATH );
     96        if ( is_feed() ) {
     97                $id = get_query_var( 'p' );
     98                if ( ! empty( $id ) ) {
     99                        $redirect_url = get_post_comments_feed_link( $id, get_query_var( 'feed' ) );
     100                        if ( ! empty( $redirect_url ) && is_string( $redirect_url ) ) {
     101                                $redirect['query'] = _remove_qs_args_if_not_in_url(
     102                                        $redirect['query'],
     103                                        array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type', 'feed' ),
     104                                        $redirect_url
     105                                );
     106                                $redirect['path']  = wp_parse_url( $redirect_url, PHP_URL_PATH );
     107                        }
    100108                }
    101109        }
    102110
    103         if ( is_singular() && 1 > $wp_query->post_count && ( $id = get_query_var( 'p' ) ) ) {
     111        if ( is_singular() && 1 > $wp_query->post_count ) {
     112                $id = get_query_var( 'p' );
     113                if ( ! empty( $id ) ) {
    104114
    105                 $vars = $wpdb->get_results( $wpdb->prepare( "SELECT post_type, post_parent FROM $wpdb->posts WHERE ID = %d", $id ) );
     115                        $vars = $wpdb->get_results(
     116                                $wpdb->prepare( "SELECT post_type, post_parent FROM $wpdb->posts WHERE ID = %d", $id )
     117                        );
    106118
    107                 if ( isset( $vars[0] ) && $vars = $vars[0] ) {
    108                         if ( 'revision' == $vars->post_type && $vars->post_parent > 0 ) {
    109                                 $id = $vars->post_parent;
    110                         }
     119                        if ( ! empty( $vars[0] ) ) {
     120                                $vars = $vars[0];
     121                                if ( 'revision' === $vars->post_type && $vars->post_parent > 0 ) {
     122                                        $id = $vars->post_parent;
     123                                }
    111124
    112                         if ( $redirect_url = get_permalink( $id ) ) {
    113                                 $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ), $redirect_url );
     125                                $redirect_url = get_permalink( $id );
     126                                if ( ! empty( $redirect_url ) && is_string( $redirect_url ) ) {
     127                                        $redirect['query'] = _remove_qs_args_if_not_in_url(
     128                                                $redirect['query'],
     129                                                array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ),
     130                                                $redirect_url
     131                                        );
     132                                }
    114133                        }
    115134                }
    116135        }
     
    120139
    121140                // Redirect ?page_id, ?p=, ?attachment_id= to their respective url's
    122141                $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 );
     142                if ( $id ) {
     143                        $redirect_post = get_post( $id );
     144                        if ( ! empty( $redirect_post ) ) {
     145                                $post_type_obj = get_post_type_object( $redirect_post->post_type );
     146                                if ( $post_type_obj->public && 'auto-draft' !== $redirect_post->post_status ) {
     147                                        $redirect_url      = get_permalink( $redirect_post );
     148                                        $redirect['query'] = _remove_qs_args_if_not_in_url(
     149                                                $redirect['query'],
     150                                                array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ),
     151                                                $redirect_url
     152                                        );
     153                                }
    128154                        }
    129155                }
    130156
     
    143169                }
    144170
    145171                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 );
     172                        $redirect_url = redirect_guess_404_permalink();
     173                        if ( ! empty( $redirect_url ) && is_string( $redirect_url ) ) {
     174                                $redirect['query'] = _remove_qs_args_if_not_in_url(
     175                                        $redirect['query'],
     176                                        array( 'page', 'feed', 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ),
     177                                        $redirect_url
     178                                );
    148179                        }
    149180                }
    150181
     
    168199                                $redirect_url = get_attachment_link();
    169200                        }
    170201                } elseif ( is_single() && ! empty( $_GET['p'] ) && ! $redirect_url ) {
    171                         if ( $redirect_url = get_permalink( get_query_var( 'p' ) ) ) {
     202                        $redirect_url = get_permalink( get_query_var( 'p' ) );
     203                        if ( ! empty( $redirect_url ) && is_string( $redirect_url ) ) {
    172204                                $redirect['query'] = remove_query_arg( array( 'p', 'post_type' ), $redirect['query'] );
    173205                        }
    174206                } elseif ( is_single() && ! empty( $_GET['name'] ) && ! $redirect_url ) {
    175                         if ( $redirect_url = get_permalink( $wp_query->get_queried_object_id() ) ) {
     207                        $redirect_url = get_permalink( $wp_query->get_queried_object_id() );
     208                        if ( ! empty( $redirect_url ) && is_string( $redirect_url ) ) {
    176209                                $redirect['query'] = remove_query_arg( 'name', $redirect['query'] );
    177210                        }
    178211                } elseif ( is_page() && ! empty( $_GET['page_id'] ) && ! $redirect_url ) {
    179                         if ( $redirect_url = get_permalink( get_query_var( 'page_id' ) ) ) {
     212                        $redirect_url = get_permalink( get_query_var( 'page_id' ) );
     213                        if ( ! empty( $redirect_url ) && is_string( $redirect_url ) ) {
    180214                                $redirect['query'] = remove_query_arg( 'page_id', $redirect['query'] );
    181215                        }
    182216                } elseif ( is_page() && ! is_feed() && 'page' == get_option( 'show_on_front' ) && get_queried_object_id() == get_option( 'page_on_front' ) && ! $redirect_url ) {
    183217                        $redirect_url = home_url( '/' );
    184218                } 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' ) ) ) {
     219                        $redirect_url = get_permalink( get_option( 'page_for_posts' ) );
     220                        if ( ! empty( $redirect_url ) && is_string( $redirect_url ) ) {
    186221                                $redirect['query'] = remove_query_arg( 'page_id', $redirect['query'] );
    187222                        }
    188223                } elseif ( ! empty( $_GET['m'] ) && ( is_year() || is_month() || is_day() ) ) {
     
    203238                        }
    204239                        // now moving on to non ?m=X year/month/day links
    205240                } 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' ) ) ) {
     241                        $redirect_url = get_day_link(
     242                                get_query_var( 'year' ),
     243                                get_query_var( 'monthnum' ),
     244                                get_query_var( 'day' )
     245                        );
     246                        if ( ! empty( $redirect_url ) && is_string( $redirect_url ) ) {
    207247                                $redirect['query'] = remove_query_arg( array( 'year', 'monthnum', 'day' ), $redirect['query'] );
    208248                        }
    209249                } 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' ) ) ) {
     250                        $redirect_url = get_month_link( get_query_var( 'year' ), get_query_var( 'monthnum' ) );
     251                        if ( ! empty( $redirect_url ) && is_string( $redirect_url ) ) {
    211252                                $redirect['query'] = remove_query_arg( array( 'year', 'monthnum' ), $redirect['query'] );
    212253                        }
    213254                } elseif ( is_year() && ! empty( $_GET['year'] ) ) {
    214                         if ( $redirect_url = get_year_link( get_query_var( 'year' ) ) ) {
     255                        $redirect_url = get_year_link( get_query_var( 'year' ) );
     256                        if ( ! empty( $redirect_url ) && is_string( $redirect_url ) ) {
    215257                                $redirect['query'] = remove_query_arg( 'year', $redirect['query'] );
    216258                        }
    217259                } elseif ( is_author() && ! empty( $_GET['author'] ) && preg_match( '|^[0-9]+$|', $_GET['author'] ) ) {
    218260                        $author = get_userdata( get_query_var( 'author' ) );
    219261                        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 ) ) {
     262                                $redirect_url = get_author_posts_url( $author->ID, $author->user_nicename );
     263                                if ( ! empty( $redirect_url ) && is_string( $redirect_url ) ) {
    221264                                        $redirect['query'] = remove_query_arg( 'author', $redirect['query'] );
    222265                                }
    223266                        }
     
    229272                        }
    230273
    231274                        $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'] ) ) {
     275                        if ( $term_count <= 1 && ! empty( $obj->term_id ) ) {
     276                                $tax_url = get_term_link( (int) $obj->term_id, $obj->taxonomy );
     277                                if ( ! empty( $tax_url ) && ! is_wp_error( $tax_url ) && ! empty( $redirect['query'] ) ) {
    234278                                        // Strip taxonomy query vars off the url.
    235279                                        $qv_remove = array( 'term', 'taxonomy' );
    236280                                        if ( is_category() ) {
     
    252296                                                $redirect['query'] = remove_query_arg( $qv_remove, $redirect['query'] ); //Remove all of the per-tax qv's
    253297
    254298                                                // Create the destination url for this taxonomy
    255                                                 $tax_url = parse_url( $tax_url );
     299                                                $tax_url = wp_parse_url( $tax_url );
    256300                                                if ( ! empty( $tax_url['query'] ) ) { // Taxonomy accessible via ?taxonomy=..&term=.. or any custom qv..
    257301                                                        parse_str( $tax_url['query'], $query_vars );
    258302                                                        $redirect['query'] = add_query_arg( $query_vars, $redirect['query'] );
     
    268312                                        }
    269313                                }
    270314                        }
    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() );
     315                } elseif ( is_single() && strpos( $wp_rewrite->permalink_structure, '%category%' ) !== false ) {
     316                        $cat = get_query_var( 'category_name' );
     317                        if ( ! empty( $cat ) ) {
     318                                $category = get_category_by_path( $cat );
     319                                if ( ( ! $category || is_wp_error( $category ) )
     320                                        || ! has_term( $category->term_id, 'category', $wp_query->get_queried_object_id() )
     321                                ) {
     322                                        $redirect_url = get_permalink( $wp_query->get_queried_object_id() );
     323                                }
    275324                        }
    276325                }
    277326
     
    378427        $redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] );
    379428        if ( $redirect_url && ! empty( $redirect['query'] ) ) {
    380429                parse_str( $redirect['query'], $_parsed_query );
    381                 $redirect = @parse_url( $redirect_url );
     430                $redirect = wp_parse_url( $redirect_url );
    382431
    383432                if ( ! empty( $_parsed_query['name'] ) && ! empty( $redirect['query'] ) ) {
    384433                        parse_str( $redirect['query'], $_parsed_redirect_query );
     
    393442        }
    394443
    395444        if ( $redirect_url ) {
    396                 $redirect = @parse_url( $redirect_url );
     445                $redirect = wp_parse_url( $redirect_url );
    397446        }
    398447
    399448        // www.example.com vs example.com
    400         $user_home = @parse_url( home_url() );
     449        $user_home = wp_parse_url( home_url() );
    401450        if ( ! empty( $user_home['host'] ) ) {
    402451                $redirect['host'] = $user_home['host'];
    403452        }
     
    603652 * @return string The altered query string
    604653 */
    605654function _remove_qs_args_if_not_in_url( $query_string, array $args_to_check, $url ) {
    606         $parsed_url = @parse_url( $url );
     655        $parsed_url = wp_parse_url( $url );
    607656        if ( ! empty( $parsed_url['query'] ) ) {
    608657                parse_str( $parsed_url['query'], $parsed_query );
    609658                foreach ( $args_to_check as $qv ) {
     
    626675 * @return string The altered URL.
    627676 */
    628677function strip_fragment_from_url( $url ) {
    629         $parsed_url = @parse_url( $url );
     678        $parsed_url = wp_parse_url( $url );
    630679        if ( ! empty( $parsed_url['host'] ) ) {
    631680                // This mirrors code in redirect_canonical(). It does not handle every case.
    632681                $url = $parsed_url['scheme'] . '://' . $parsed_url['host'];
     
    665714                if ( get_query_var( 'post_type' ) ) {
    666715                        $where .= $wpdb->prepare( ' AND post_type = %s', get_query_var( 'post_type' ) );
    667716                } else {
    668                         $where .= " AND post_type IN ('" . implode( "', '", get_post_types( array( 'public' => true ) ) ) . "')";
     717                        // Fetch the variables needed to place in the post_types query.
     718                        $post_types = get_post_types( array( 'public' => true ) );
     719                        if ( is_array( $post_types ) && ! array() === $post_types ) {
     720                                // Create placeholders for all the variables.
     721                                $placeholders = implode( ', ', array_fill( 0, count( $post_types ), '%s' ) );
     722
     723                                /*
     724                                 * Add the statement to the where condition.
     725                                 *
     726                                 * The ignore is needed because the placeholders are inside a variable, which is concatinated to the
     727                                 * where statement, but the sniff is not picking it up correctly yet.
     728                                 *
     729                                 * This is a WPCS problem and has been fixed in the following PR
     730                                 * @link https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/pull/515
     731                                 */
     732                                $post_type_where = sprintf( ' AND post_type IN ( %s )', $placeholders );
     733                                $where          .= $wpdb->prepare(
     734                                        $post_type_where, // phpcs:ignore WordPress.WP.PreparedSQL.NotPrepared
     735                                        $post_types
     736                                );
     737                        }
    669738                }
    670739
    671740                if ( get_query_var( 'year' ) ) {
     
    678747                        $where .= $wpdb->prepare( ' AND DAYOFMONTH(post_date) = %d', get_query_var( 'day' ) );
    679748                }
    680749
    681                 $post_id = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE $where AND post_status = 'publish'" );
     750                /*
     751                 * Build the compelte SQL statement and execute it.
     752                 *
     753                 * The ignore has been added because all of the variables used are being prepared in the above lines.
     754                 */
     755                $post_id = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE $where AND post_status = 'publish'" ); // phpcs:ignore WordPress.WP.PreparedSQL.NotPrepared
    682756                if ( ! $post_id ) {
    683757                        return false;
    684758                }