Skip to content
This repository has been archived by the owner on Dec 16, 2022. It is now read-only.

Commit

Permalink
Simplify get_previewed_posts_for_query by replacing customize_posts_m…
Browse files Browse the repository at this point in the history
…ain_query_post_type filter with any post type lookup
  • Loading branch information
westonruter committed May 25, 2016
1 parent 06b180c commit 9cdcb7c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 32 deletions.
19 changes: 5 additions & 14 deletions php/class-wp-customize-posts-preview.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,22 +196,13 @@ public function get_previewed_posts_for_query( WP_Query $query ) {
continue;
}

/**
* Filter the post type used in the main query.
*
* @param string $post_type Main query post type.
* @param string $setting_post_type Current setting post type.
*/
$main_query_post_type = apply_filters( 'customize_posts_main_query_post_type', 'post', $matches['post_type'] );

$post_type_match = (
in_array( $matches['post_type'], $query_vars['post_type'], true ) ||
in_array( $matches['post_type'], $query_vars['post_type'], true )
||
(
$query->is_main_query() &&
(
$main_query_post_type === $matches['post_type'] ||
in_array( 'any', $query_vars['post_type'], true )
)
in_array( 'any', $query_vars['post_type'], true )
&&
in_array( $matches['post_type'], get_post_types( array( 'exclude_from_search' => false ) ), true )
)
);

Expand Down
20 changes: 2 additions & 18 deletions tests/php/test-class-wp-customize-posts-preview.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public function filter_the_posts_to_add_dynamic_post_settings_and_sections() {
/**
* Test get_previewed_drafts method.
*
* @see WP_Customize_Posts::get_previewed_posts_for_query()
* @see WP_Customize_Posts_Preview::get_previewed_posts_for_query()
*/
public function test_get_previewed_posts_for_query() {
global $wp_the_query;
Expand Down Expand Up @@ -266,25 +266,9 @@ public function test_get_previewed_posts_for_query() {
$query = new WP_Query( array( 'post_type' => 'any' ) );
$wp_the_query = $query;
$this->assertEquals( array( $post->ID, $page->ID ), $this->posts_component->preview->get_previewed_posts_for_query( $query ) );
add_filter( 'customize_posts_main_query_post_type', array( $this, 'filter_main_query_post_type' ), 10, 2 );
$query = new WP_Query( array( 'post_type' => 'post' ) );
$query = new WP_Query( array( 'post_type' => 'any' ) );
$wp_the_query = $query;
$this->assertEquals( array( $post->ID, $page->ID ), $this->posts_component->preview->get_previewed_posts_for_query( $query ) );
remove_filter( 'customize_posts_main_query_post_type', array( $this, 'filter_main_query_post_type' ), 10 );
}

/**
* Filter the main query post types
*
* @param string $post_type Post type.
* @param string $setting_post_type Setting post type.
* @return string
*/
public function filter_main_query_post_type( $post_type, $setting_post_type ) {
if ( 'page' === $setting_post_type ) {
return 'page';
}
return $post_type;
}

/**
Expand Down

0 comments on commit 9cdcb7c

Please sign in to comment.