~canonical-sysadmins/wordpress/5.1

« back to all changes in this revision

Viewing changes to wp-includes/query.php

  • Committer: Barry Price
  • Date: 2018-03-12 09:10:47 UTC
  • mfrom: (1.2.3 upstream)
  • Revision ID: barry.price@canonical.com-20180312091047-rhie6qpndx5nwlo1
Merge WP4.9.4 from upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
843
843
 * Attempts to find the current slug from the past slugs.
844
844
 *
845
845
 * @since 2.1.0
846
 
 *
847
 
 * @global wpdb $wpdb WordPress database abstraction object.
848
846
 */
849
847
function wp_old_slug_redirect() {
850
848
        if ( is_404() && '' !== get_query_var( 'name' ) ) {
851
 
                global $wpdb;
852
 
 
853
849
                // Guess the current post_type based on the query vars.
854
850
                if ( get_query_var( 'post_type' ) ) {
855
851
                        $post_type = get_query_var( 'post_type' );
873
869
                        return;
874
870
                }
875
871
 
876
 
                $query = $wpdb->prepare("SELECT post_id FROM $wpdb->postmeta, $wpdb->posts WHERE ID = post_id AND post_type = %s AND meta_key = '_wp_old_slug' AND meta_value = %s", $post_type, get_query_var( 'name' ) );
877
 
 
878
 
                // if year, monthnum, or day have been specified, make our query more precise
879
 
                // just in case there are multiple identical _wp_old_slug values
880
 
                if ( get_query_var( 'year' ) ) {
881
 
                        $query .= $wpdb->prepare(" AND YEAR(post_date) = %d", get_query_var( 'year' ) );
882
 
                }
883
 
                if ( get_query_var( 'monthnum' ) ) {
884
 
                        $query .= $wpdb->prepare(" AND MONTH(post_date) = %d", get_query_var( 'monthnum' ) );
885
 
                }
886
 
                if ( get_query_var( 'day' ) ) {
887
 
                        $query .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", get_query_var( 'day' ) );
888
 
                }
889
 
 
890
 
                $id = (int) $wpdb->get_var( $query );
 
872
                $id = _find_post_by_old_slug( $post_type );
 
873
 
 
874
                if ( ! $id ) {
 
875
                        $id = _find_post_by_old_date( $post_type );
 
876
                }
 
877
 
 
878
                /**
 
879
                 * Filters the old slug redirect post ID.
 
880
                 *
 
881
                 * @since 4.9.3
 
882
                 *
 
883
                 * @param string $id The redirect post ID.
 
884
                 */
 
885
                $id = apply_filters( 'old_slug_redirect_post_id', $id );
891
886
 
892
887
                if ( ! $id ) {
893
888
                        return;
920
915
}
921
916
 
922
917
/**
 
918
 * Find the post ID for redirecting an old slug.
 
919
 *
 
920
 * @see wp_old_slug_redirect()
 
921
 *
 
922
 * @since 4.9.3
 
923
 * @access private
 
924
 *
 
925
 * @global wpdb $wpdb WordPress database abstraction object.
 
926
 *
 
927
 * @param string $post_type The current post type based on the query vars.
 
928
 * @return int $id The Post ID.
 
929
 */
 
930
function _find_post_by_old_slug( $post_type ) {
 
931
        global $wpdb;
 
932
 
 
933
        $query = $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta, $wpdb->posts WHERE ID = post_id AND post_type = %s AND meta_key = '_wp_old_slug' AND meta_value = %s", $post_type, get_query_var( 'name' ) );
 
934
 
 
935
        // if year, monthnum, or day have been specified, make our query more precise
 
936
        // just in case there are multiple identical _wp_old_slug values
 
937
        if ( get_query_var( 'year' ) ) {
 
938
                $query .= $wpdb->prepare( " AND YEAR(post_date) = %d", get_query_var( 'year' ) );
 
939
        }
 
940
        if ( get_query_var( 'monthnum' ) ) {
 
941
                $query .= $wpdb->prepare( " AND MONTH(post_date) = %d", get_query_var( 'monthnum' ) );
 
942
        }
 
943
        if ( get_query_var( 'day' ) ) {
 
944
                $query .= $wpdb->prepare( " AND DAYOFMONTH(post_date) = %d", get_query_var( 'day' ) );
 
945
        }
 
946
 
 
947
        $id = (int) $wpdb->get_var( $query );
 
948
 
 
949
        return $id;
 
950
}
 
951
 
 
952
/**
 
953
 * Find the post ID for redirecting an old date.
 
954
 *
 
955
 * @see wp_old_slug_redirect()
 
956
 *
 
957
 * @since 4.9.3
 
958
 * @access private
 
959
 *
 
960
 * @global wpdb $wpdb WordPress database abstraction object.
 
961
 *
 
962
 * @param string $post_type The current post type based on the query vars.
 
963
 * @return int $id The Post ID.
 
964
 */
 
965
 
 
966
function _find_post_by_old_date( $post_type ) {
 
967
        global $wpdb;
 
968
 
 
969
        $date_query = '';
 
970
        if ( get_query_var( 'year' ) ) {
 
971
                $date_query .= $wpdb->prepare( " AND YEAR(pm_date.meta_value) = %d", get_query_var( 'year' ) );
 
972
        }
 
973
        if ( get_query_var( 'monthnum' ) ) {
 
974
                $date_query .= $wpdb->prepare( " AND MONTH(pm_date.meta_value) = %d", get_query_var( 'monthnum' ) );
 
975
        }
 
976
        if ( get_query_var( 'day' ) ) {
 
977
                $date_query .= $wpdb->prepare( " AND DAYOFMONTH(pm_date.meta_value) = %d", get_query_var( 'day' ) );
 
978
        }
 
979
 
 
980
        $id = 0;
 
981
        if ( $date_query ) {
 
982
                $id = (int) $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta AS pm_date, $wpdb->posts WHERE ID = post_id AND post_type = %s AND meta_key = '_wp_old_date' AND post_name = %s" . $date_query, $post_type, get_query_var( 'name' ) ) );
 
983
 
 
984
                if ( ! $id ) {
 
985
                        // Check to see if an old slug matches the old date
 
986
                        $id = (int) $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts, $wpdb->postmeta AS pm_slug, $wpdb->postmeta AS pm_date WHERE ID = pm_slug.post_id AND ID = pm_date.post_id AND post_type = %s AND pm_slug.meta_key = '_wp_old_slug' AND pm_slug.meta_value = %s AND pm_date.meta_key = '_wp_old_date'" . $date_query, $post_type, get_query_var( 'name' ) ) );
 
987
                }
 
988
        }
 
989
 
 
990
        return $id;
 
991
}
 
992
 
 
993
/**
923
994
 * Set up global post data.
924
995
 *
925
996
 * @since 1.5.0