diff --git a/includes/MslsOptionsTax.php b/includes/MslsOptionsTax.php index 8d5af2ff..56911a98 100644 --- a/includes/MslsOptionsTax.php +++ b/includes/MslsOptionsTax.php @@ -19,34 +19,21 @@ class MslsOptionsTax extends MslsOptions implements OptionsTaxInterface { * @return OptionsTaxInterface */ public static function create( $id = 0 ): OptionsTaxInterface { - $id = ! empty( $id ) ? (int) $id : get_queried_object_id(); - - $req = ''; - if ( is_admin() ) { - $req = msls_content_types()->acl_request(); - } elseif ( is_category() ) { - $req = 'category'; - } elseif ( is_tag( $id ) ) { - $req = 'post_tag'; - } + $id = ! empty( $id ) ? (int) $id : get_queried_object_id(); + $req = self::get_content_type( $id ); switch ( $req ) { case 'category': $options = new MslsOptionsTaxTermCategory( $id ); - add_filter( 'msls_get_postlink', array( $options, 'check_base' ), 9, 2 ); break; case 'post_tag': $options = new MslsOptionsTaxTerm( $id ); - add_filter( 'msls_get_postlink', array( $options, 'check_base' ), 9, 2 ); break; default: - global $wp_rewrite; - - $options = new MslsOptionsTax( $id ); - $options->with_front = ! empty( $wp_rewrite->extra_permastructs[ $options->get_tax_query() ]['with_front'] ); + $options = new MslsOptionsTax( $id ); } - return $options; + return $options->handle_rewrite(); } /** @@ -54,12 +41,20 @@ public static function create( $id = 0 ): OptionsTaxInterface { * * @return string */ - public function get_content_type( int $id ): string { + public static function get_content_type( int $id ): string { if ( is_admin() ) { return msls_content_types()->acl_request(); } - return ( is_category() ? 'category' : is_tag( $id ) ) ? 'post_tag' : ''; + return ( is_category( $id ) ? 'category' : ( is_tag( $id ) ? 'post_tag' : '' ) ); + } + + public function handle_rewrite(): OptionsTaxInterface { + global $wp_rewrite; + + $this->with_front = ! empty( $wp_rewrite->extra_permastructs[ $this->get_tax_query() ]['with_front'] ); + + return $this; } /** diff --git a/includes/MslsOptionsTaxTerm.php b/includes/MslsOptionsTaxTerm.php index c8b977f9..da3a39d1 100644 --- a/includes/MslsOptionsTaxTerm.php +++ b/includes/MslsOptionsTaxTerm.php @@ -20,6 +20,12 @@ class MslsOptionsTaxTerm extends MslsOptionsTax implements OptionsTaxInterface { */ public ?bool $with_front = true; + public function handle_rewrite(): OptionsTaxInterface { + add_filter( 'msls_get_postlink', array( $this, 'check_base' ), 9, 2 ); + + return $this; + } + /** * Check and correct URL * diff --git a/includes/OptionsTaxInterface.php b/includes/OptionsTaxInterface.php index 0d39cc39..10b1476a 100644 --- a/includes/OptionsTaxInterface.php +++ b/includes/OptionsTaxInterface.php @@ -5,4 +5,6 @@ interface OptionsTaxInterface extends OptionsInterface { public static function get_base_option(): string; + + public function handle_rewrite(): OptionsTaxInterface; } diff --git a/tests/phpunit/TestMslsOptionsTax.php b/tests/phpunit/TestMslsOptionsTax.php index ed8323f2..973792d8 100644 --- a/tests/phpunit/TestMslsOptionsTax.php +++ b/tests/phpunit/TestMslsOptionsTax.php @@ -12,13 +12,13 @@ final class TestMslsOptionsTax extends MslsUnitTestCase { private function MslsOptionsTaxFactory(): MslsOptionsTax { Functions\expect( 'get_option' )->atLeast()->once()->andReturn( array( 'de_DE' => 42 ) ); - return new MslsOptionsTax( 0 ); + return new MslsOptionsTax(); } public function test_create_category(): void { Functions\expect( 'get_queried_object_id' )->once()->andReturn( 42 ); Functions\expect( 'is_admin' )->once()->andReturnFalse(); - Functions\expect( 'is_category' )->once()->andReturnTrue(); + Functions\expect( 'is_category' )->once()->with( 42 )->andReturnTrue(); Functions\expect( 'get_option' )->atLeast()->once()->andReturn( array( 'de_DE' => 42 ) ); $this->assertInstanceOf( MslsOptionsTaxTermCategory::class, MslsOptionsTax::create() ); diff --git a/tests/phpunit/TestMslsPostTag.php b/tests/phpunit/TestMslsPostTag.php index 45fa4589..7257f70c 100644 --- a/tests/phpunit/TestMslsPostTag.php +++ b/tests/phpunit/TestMslsPostTag.php @@ -87,7 +87,7 @@ public function test_edit_input(): void { $taxonomy = \Mockery::mock( MslsTaxonomy::class ); $taxonomy->shouldReceive( 'is_taxonomy' )->atLeast()->once()->andReturn( true ); $taxonomy->shouldReceive( 'get_request' )->atLeast()->once()->andReturn( 'post' ); - $taxonomy->shouldReceive( 'acl_request' )->atLeast()->once()->andReturn( array( 'taxonomy', 'post_tag' ) ); + $taxonomy->shouldReceive( 'acl_request' )->atLeast()->once()->andReturn( 'taxonomy' ); $term = \Mockery::mock( \WP_Term::class ); $term->name = 'test-term-name'; @@ -140,7 +140,7 @@ public function test_add_input(): void { $taxonomy = \Mockery::mock( MslsTaxonomy::class ); $taxonomy->shouldReceive( 'is_taxonomy' )->atLeast()->once()->andReturnTrue(); $taxonomy->shouldReceive( 'get_request' )->atLeast()->once()->andReturn( 'post' ); - $taxonomy->shouldReceive( 'acl_request' )->atLeast()->once()->andReturn( array( 'taxonomy', 'post_tag' ) ); + $taxonomy->shouldReceive( 'acl_request' )->atLeast()->once()->andReturn( 'taxonomy' ); Functions\expect( 'msls_content_types' )->atLeast()->once()->andReturn( $taxonomy ); Functions\expect( 'get_queried_object_id' )->atLeast()->once()->andReturn( 42 ); diff --git a/tests/phpunit/TestMslsPostTagClassic.php b/tests/phpunit/TestMslsPostTagClassic.php index 94117095..4ded68c3 100644 --- a/tests/phpunit/TestMslsPostTagClassic.php +++ b/tests/phpunit/TestMslsPostTagClassic.php @@ -70,7 +70,7 @@ public function test_edit_input(): void { Functions\expect( 'get_edit_term_link' )->atLeast()->once()->andReturn( 'edit_term_link' ); $taxonomy = \Mockery::mock( \WP_Taxonomy::class ); - $taxonomy->shouldReceive( 'acl_request' )->atLeast()->once()->andReturn( array( 'taxonomy', 'post_type' ) ); + $taxonomy->shouldReceive( 'acl_request' )->atLeast()->once()->andReturn( 'taxonomy' ); $taxonomy->shouldReceive( 'is_taxonomy' )->atLeast()->once()->andReturnTrue(); $taxonomy->shouldReceive( 'get_request' )->atLeast()->once()->andReturn( 'post_type' ); @@ -133,7 +133,7 @@ public function test_add_input(): void { Functions\expect( 'get_edit_term_link' )->atLeast()->once()->andReturn( 'edit_term_link' ); $taxonomy = \Mockery::mock( \WP_Taxonomy::class ); - $taxonomy->shouldReceive( 'acl_request' )->atLeast()->once()->andReturn( array( 'taxonomy', 'post_type' ) ); + $taxonomy->shouldReceive( 'acl_request' )->atLeast()->once()->andReturn( 'taxonomy' ); $taxonomy->shouldReceive( 'is_taxonomy' )->atLeast()->once()->andReturnTrue(); $taxonomy->shouldReceive( 'get_request' )->atLeast()->once()->andReturn( 'post_type' );