diff --git a/includes/MslsAdminIcon.php b/includes/MslsAdminIcon.php index e2615daa..b73f0265 100644 --- a/includes/MslsAdminIcon.php +++ b/includes/MslsAdminIcon.php @@ -1,9 +1,4 @@ - * @since 0.9.8 - */ namespace lloc\Msls; @@ -12,6 +7,7 @@ /** * Handles the icon links in the backend + * * @package Msls */ class MslsAdminIcon { @@ -121,7 +117,7 @@ public function set_icon_type( $icon_type ): MslsAdminIcon { */ public function set_path(): MslsAdminIcon { if ( 'post' != $this->type ) { - $query_vars = [ 'post_type' => $this->type ]; + $query_vars = array( 'post_type' => $this->type ); $this->path = add_query_arg( $query_vars, $this->path ); } @@ -209,12 +205,16 @@ public function get_img(): string { */ public function get_a(): string { if ( empty( $this->href ) ) { - $title = sprintf( __( 'Create a new translation in the %s-blog', 'multisite-language-switcher' ), - $this->language ); + $title = sprintf( + __( 'Create a new translation in the %s-blog', 'multisite-language-switcher' ), + $this->language + ); $href = $this->get_edit_new(); } else { - $title = sprintf( __( 'Edit the translation in the %s-blog', 'multisite-language-switcher' ), - $this->language ); + $title = sprintf( + __( 'Edit the translation in the %s-blog', 'multisite-language-switcher' ), + $this->language + ); $href = $this->href; } @@ -232,21 +232,25 @@ public function get_icon(): string { } switch ( $this->icon_type ) { - case MslsAdminIcon::TYPE_FLAG: - $icon = sprintf( '%s', + case self::TYPE_FLAG: + $icon = sprintf( + '%s', ( new IconSvg() )->get( $this->language ), $this->language ); break; - case MslsAdminIcon::TYPE_LABEL: - $icon = sprintf( '%s', + case self::TYPE_LABEL: + $icon = sprintf( + '%s', $this->language, ( new IconLabel() )->get( $this->language ) ); break; default: - $icon = sprintf( '', - empty( $this->href ) ? 'dashicons-plus' : 'dashicons-edit' ); + $icon = sprintf( + '', + empty( $this->href ) ? 'dashicons-plus' : 'dashicons-edit' + ); } return $icon; @@ -261,7 +265,13 @@ public function get_edit_new(): string { $path = $this->path; if ( null !== $this->id && null !== $this->origin_language ) { - $path = add_query_arg( [ 'msls_id' => $this->id, 'msls_lang' => $this->origin_language ], $this->path ); + $path = add_query_arg( + array( + 'msls_id' => $this->id, + 'msls_lang' => $this->origin_language, + ), + $this->path + ); } /** @@ -275,5 +285,4 @@ public function get_edit_new(): string { return get_admin_url( get_current_blog_id(), $path ); } - } diff --git a/includes/MslsAdminIconTaxonomy.php b/includes/MslsAdminIconTaxonomy.php index 5db43b8e..46e7734d 100644 --- a/includes/MslsAdminIconTaxonomy.php +++ b/includes/MslsAdminIconTaxonomy.php @@ -1,20 +1,17 @@ - * @since 0.9.8 - */ namespace lloc\Msls; /** * Handles backend icons for taxonomies + * * @package Msls */ class MslsAdminIconTaxonomy extends MslsAdminIcon { /** * Path + * * @var string */ protected $path = 'edit-tags.php'; @@ -28,7 +25,7 @@ class MslsAdminIconTaxonomy extends MslsAdminIcon { * @uses get_edit_term_link() */ public function set_href( int $id ): MslsAdminIcon { - $object_type = MslsTaxonomy::instance()->get_post_type(); + $object_type = MslsTaxonomy::instance()->get_post_type(); $this->href = get_edit_term_link( $id, $this->type, $object_type ); @@ -39,17 +36,15 @@ public function set_href( int $id ): MslsAdminIcon { * Set the path by type * * @return MslsAdminIconTaxonomy - * */ public function set_path(): MslsAdminIcon { - $args = [ 'taxonomy' => $this->type ]; + $args = array( 'taxonomy' => $this->type ); $post_type = MslsTaxonomy::instance()->get_post_type(); - $post_type !== '' && $args['post_type'] = $post_type; + $post_type !== '' && $args['post_type'] = $post_type; $this->path = add_query_arg( $args, $this->path ); return $this; } - } diff --git a/includes/MslsOptions.php b/includes/MslsOptions.php index 9a9c7cc7..250acc4c 100644 --- a/includes/MslsOptions.php +++ b/includes/MslsOptions.php @@ -108,7 +108,7 @@ public static function create( $id = 0 ) { $options = new MslsOptionsPost( get_queried_object_id() ); } - add_filter( 'check_url', array( $options, 'check_for_blog_slug' ), 10, 2 ); + add_filter( 'msls_get_postlink', array( $options, 'check_for_blog_slug' ), 10, 2 ); return $options; } @@ -323,16 +323,16 @@ public function get_url( $dir ) { /** * Returns slug for a post type * + * @todo This method is not used anywhere in the codebase. Should it be removed? + * * @param string $post_type * * @return string */ - public function get_slug( $post_type ) { + public function get_slug( string $post_type ): string { $key = "rewrite_{$post_type}"; - error_log( $key ); - - return isset( $this->$key ) ? $this->$key : ''; + return $this->$key ?? ''; } /** @@ -352,11 +352,7 @@ public function get_icon( $language ) { * @return string */ public function get_flag_url( $language ) { - if ( ! is_admin() && isset( $this->image_url ) ) { - $url = $this->__get( 'image_url' ); - } else { - $url = $this->get_url( 'flags' ); - } + $url = ! is_admin() && isset( $this->image_url ) ? $this->__get( 'image_url' ) : $this->get_url( 'flags' ); /** * Override the path to the flag-icons diff --git a/includes/MslsOptionsPost.php b/includes/MslsOptionsPost.php index c60093c9..d9128b55 100644 --- a/includes/MslsOptionsPost.php +++ b/includes/MslsOptionsPost.php @@ -45,7 +45,11 @@ public function get_postlink( $language ) { $this->with_front = ! empty( $post_object->rewrite['with_front'] ); } - return apply_filters( 'check_url', get_permalink( $post ), $this ); + $post_link = get_permalink( $post ); + + $post_link = apply_filters_deprecated( 'check_url', array( $post_link, $this ), '2.7.1', 'msls_get_postlink' ); + + return apply_filters( 'msls_get_postlink', $post_link, $this ); } /** diff --git a/includes/MslsOptionsQuery.php b/includes/MslsOptionsQuery.php index 8e3c6771..f568c9e4 100644 --- a/includes/MslsOptionsQuery.php +++ b/includes/MslsOptionsQuery.php @@ -1,6 +1,7 @@ * @since 0.9.8 */ @@ -16,6 +17,7 @@ class MslsOptionsQuery extends MslsOptions { /** * Rewrite with front + * * @var bool */ public $with_front = true; @@ -61,13 +63,14 @@ public static function create( $id = 0 ) { */ public function get_postlink( $language ) { if ( $this->has_value( $language ) ) { - $link = $this->get_current_link(); - if ( ! empty( $link ) ) { - return apply_filters( 'check_url', $link, $this ); + $post_link = $this->get_current_link(); + if ( ! empty( $post_link ) ) { + $post_link = apply_filters_deprecated( 'check_url', array( $post_link, $this ), '2.7.1', 'msls_get_postlink' ); + + return apply_filters( 'msls_get_postlink', $post_link, $this ); } } return ''; } - } diff --git a/includes/MslsOptionsQueryAuthor.php b/includes/MslsOptionsQueryAuthor.php index dfa0320b..d985f544 100644 --- a/includes/MslsOptionsQueryAuthor.php +++ b/includes/MslsOptionsQueryAuthor.php @@ -1,6 +1,7 @@ * @since 0.9.8 */ @@ -15,7 +16,7 @@ class MslsOptionsQueryAuthor extends MslsOptionsQuery { /** - * Check if the array has an non empty item which has $language as a key + * Check if the array has a non-empty item which has $language as a key * * @param string $language * @@ -44,5 +45,4 @@ public function has_value( $language ) { public function get_current_link() { return get_author_posts_url( $this->get_arg( 0, 0 ) ); } - } diff --git a/includes/MslsOptionsQueryPostType.php b/includes/MslsOptionsQueryPostType.php index e26fd859..95950d5c 100644 --- a/includes/MslsOptionsQueryPostType.php +++ b/includes/MslsOptionsQueryPostType.php @@ -1,6 +1,7 @@ * @since 0.9.8 */ @@ -15,7 +16,7 @@ class MslsOptionsQueryPostType extends MslsOptionsQuery { /** - * Check if the array has an non empty item which has $language as a key + * Check if the array has a non-empty item which has $language as a key * * @param string $language * @@ -37,5 +38,4 @@ public function has_value( $language ) { public function get_current_link() { return (string) get_post_type_archive_link( $this->get_arg( 0, '' ) ); } - } diff --git a/includes/MslsOptionsQueryYear.php b/includes/MslsOptionsQueryYear.php index baf3bc6f..ad2c062b 100644 --- a/includes/MslsOptionsQueryYear.php +++ b/includes/MslsOptionsQueryYear.php @@ -1,6 +1,7 @@ * @since 0.9.8 */ @@ -15,7 +16,7 @@ class MslsOptionsQueryYear extends MslsOptionsQuery { /** - * Check if the array has an non empty item which has $language as a key + * Check if the array has a non-empty item which has $language as a key * * @param string $language * @@ -44,5 +45,4 @@ public function has_value( $language ) { public function get_current_link() { return get_year_link( $this->get_arg( 0, 0 ) ); } - } diff --git a/includes/MslsOptionsTax.php b/includes/MslsOptionsTax.php index a2117a77..5921e3aa 100644 --- a/includes/MslsOptionsTax.php +++ b/includes/MslsOptionsTax.php @@ -1,6 +1,7 @@ * @since 0.9.8 */ @@ -9,18 +10,21 @@ /** * Taxonomy options + * * @package Msls */ class MslsOptionsTax extends MslsOptions { /** * Separator + * * @var string */ protected $sep = '_term_'; /** * Autoload + * * @var string */ protected $autoload = 'no'; @@ -58,7 +62,7 @@ public static function create( $id = 0 ) { } if ( $req ) { - add_filter( 'check_url', [ $options, 'check_base' ], 9, 2 ); + add_filter( 'msls_get_postlink', array( $options, 'check_base' ), 9, 2 ); } else { global $wp_rewrite; @@ -70,6 +74,7 @@ public static function create( $id = 0 ) { /** * Get the queried taxonomy + * * @return string */ public function get_tax_query() { @@ -92,17 +97,20 @@ public function get_tax_query() { * @return string */ public function get_postlink( $language ) { - $url = ''; + $post_link = ''; if ( $this->has_value( $language ) ) { - $url = $this->get_term_link( (int) $this->__get( $language ) ); + $post_link = $this->get_term_link( (int) $this->__get( $language ) ); } - return apply_filters( 'check_url', $url, $this ); + $post_link = apply_filters_deprecated( 'check_url', array( $post_link, $this ), '2.7.1', 'msls_get_postlink' ); + + return apply_filters( 'msls_get_postlink', $post_link, $this ); } /** * Get current link + * * @return string */ public function get_current_link() { @@ -129,5 +137,4 @@ public function get_term_link( $term_id ) { return ''; } - } diff --git a/tests/TestMslsOptions.php b/tests/TestMslsOptions.php index 26cf5f9d..259ab335 100644 --- a/tests/TestMslsOptions.php +++ b/tests/TestMslsOptions.php @@ -4,13 +4,14 @@ use Brain\Monkey\Functions; +use lloc\Msls\MslsAdminIcon; use lloc\Msls\MslsOptions; class TestMslsOptions extends MslsUnitTestCase { public function get_test(): MslsOptions { Functions\when( 'home_url' )->justReturn( 'https://lloc.de' ); - Functions\when( 'get_option' )->justReturn( [] ); + Functions\when( 'get_option' )->justReturn( array() ); Functions\when( 'update_option' )->justReturn( true ); return new MslsOptions(); @@ -36,9 +37,9 @@ public function test_is_query_page_method(): void { public function test_create_method(): void { Functions\when( 'is_admin' )->justReturn( true ); - Functions\when( 'get_post_types' )->justReturn( [] ); + Functions\when( 'get_post_types' )->justReturn( array() ); Functions\when( 'get_post_type' )->justReturn( 'post' ); - Functions\when( 'get_option' )->justReturn( [] ); + Functions\when( 'get_option' )->justReturn( array() ); $this->assertInstanceOf( MslsOptions::class, MslsOptions::create() ); } @@ -49,14 +50,23 @@ public function test_get_arg_method(): void { $this->assertNull( $obj->get_arg( 0 ) ); $this->assertIsSTring( $obj->get_arg( 0, '' ) ); $this->assertIsFloat( $obj->get_arg( 0, 1.1 ) ); - $this->assertIsArray( $obj->get_arg( 0, [] ) ); + $this->assertIsArray( $obj->get_arg( 0, array() ) ); } function test_set_method(): void { $obj = $this->get_test(); - $this->assertTrue( $obj->set( [] ) ); - $this->assertTrue( $obj->set( array( 'temp' => 'abc' ) ) ); + $this->assertTrue( $obj->set( array() ) ); + $this->assertTrue( + $obj->set( + array( + 'temp' => 'abc', + 'en' => 1, + 'us' => 2, + ) + ) + ); + $this->assertFalse( $obj->set( 'Test' ) ); $this->assertFalse( $obj->set( 1 ) ); $this->assertFalse( $obj->set( 1.1 ) ); @@ -120,11 +130,37 @@ function test_get_flag_url_method(): void { } function test_get_available_languages_method(): void { - Functions\when( 'get_available_languages' )->justReturn( [] ); + Functions\expect( 'get_available_languages' )->once()->andReturn( array( 'de_DE', 'it_IT' ) ); + Functions\expect( 'format_code_lang' )->atLeast()->once()->andReturnUsing( + function ( $code ) { + $map = array( + 'de_DE' => 'German', + 'it_IT' => 'Italian', + ); + return $map[ $code ] ?? 'American English'; + } + ); + + $obj = $this->get_test(); + + $expected = array( + 'en_US' => 'American English', + 'de_DE' => 'German', + 'it_IT' => 'Italian', + ); + $this->assertEquals( $expected, $obj->get_available_languages() ); + } + public function test_get_icon_type_standard(): void { $obj = $this->get_test(); - $this->assertIsArray( $obj->get_available_languages() ); + $this->assertEquals( MslsAdminIcon::TYPE_FLAG, $obj->get_icon_type() ); } + public function test_get_icon_type_admin_display(): void { + $obj = $this->get_test(); + $obj->set( array( 'admin_display' => MslsAdminIcon::TYPE_LABEL ) ); + + $this->assertEquals( MslsAdminIcon::TYPE_LABEL, $obj->get_icon_type() ); + } }