From e0f7dcfbad4d3d5244e66935a63ec7a855779b3d Mon Sep 17 00:00:00 2001 From: Fredrik Forsmo Date: Mon, 30 Nov 2015 15:21:36 +0100 Subject: [PATCH] Fix issue with papi_get_page_type_id and only page type filter --- src/lib/page.php | 10 ++++++++++ tests/cases/lib/page-test.php | 15 +++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/lib/page.php b/src/lib/page.php index ca0181193..8cb0622a7 100644 --- a/src/lib/page.php +++ b/src/lib/page.php @@ -302,6 +302,16 @@ function papi_get_page_type_id( $post_id = 0 ) { $page_type = empty( $meta_value ) ? '' : $meta_value; } + // When using `only_page_type` filter we need to fetch the value since it + // maybe not always saved in the database. + if ( empty ( $page_type ) ) { + $post_type = get_post_type( $post_id ); + + if ( is_string( $post_type ) && $page_type = papi_filter_settings_only_page_type( $post_type ) ) { + return $page_type; + } + } + // Load page type id from the container if it exists or // load it from `papi_get_all_page_types`. if ( empty( $page_type ) ) { diff --git a/tests/cases/lib/page-test.php b/tests/cases/lib/page-test.php index 1b7152162..523868153 100644 --- a/tests/cases/lib/page-test.php +++ b/tests/cases/lib/page-test.php @@ -260,6 +260,21 @@ public function test_papi_get_page_type_id() { unset( $_GET['post_type'] ); } + public function test_papi_get_page_type_id_only_page_type_filter() { + $this->assertEmpty( papi_get_page_type_id() ); + + $_GET['post'] = $this->factory->post->create( ['post_type' => 'module'] ); + $_GET['post_type'] = 'module'; + + add_filter( 'papi/settings/only_page_type_module', function () { + return 'modules/feature-module-type'; + } ); + + $this->assertSame( 'modules/feature-module-type', papi_get_page_type_id() ); + unset( $_GET['post'] ); + unset( $_GET['post_type'] ); + } + public function test_papi_get_post_types() { $actual = papi_get_post_types();