WordPress.org

Making WordPress.org

Changeset 9316


Ignore:
Timestamp:
12/10/2019 07:23:08 PM (2 months ago)
Author:
coffee2code
Message:

Developer: 404 any attempts to filter by invalid post type.

Props jonoaldersonwp, coffee2code.
Fixes #4647.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/inc/search.php

    r9185 r9316  
    2222     */
    2323    public static function do_init() {
     24        add_action( 'pre_get_posts', array( __CLASS__, 'invalid_post_type_filter_404' ), 9 );
    2425        add_action( 'pre_get_posts', array( __CLASS__, 'pre_get_posts' ), 20 );
    2526        add_filter( 'posts_orderby', array( __CLASS__, 'search_posts_orderby' ), 10, 2 );
     
    3435        if ( is_search() || isset( $_GET[ 'post_type' ] ) ) {
    3536            wp_no_robots();
     37        }
     38    }
     39
     40    /*
     41     * Makes request respond as a 404 if request is to filter by an invalid post_type.
     42     *
     43     * @access public
     44     *
     45     * @param  WP_Query $query WP_Query object
     46     */
     47    public static function invalid_post_type_filter_404( $query ) {
     48        // If the main query is being filtered by post_type.
     49        if ( $query->is_main_query() && isset( $_GET['post_type'] ) ) {
     50            // Get list of valid parsed post types specified in query.
     51            $valid_post_types = array_intersect( $_GET['post_type'], DevHub\get_parsed_post_types() );
     52
     53            // If no valid post types were specified, then request is a 404.
     54            if ( ! $valid_post_types ) {
     55                $query->set_404();
     56            }
    3657        }
    3758    }
Note: See TracChangeset for help on using the changeset viewer.