diff --git a/includes/MslsBlogCollection.php b/includes/MslsBlogCollection.php index 22d02c71..c845ac87 100644 --- a/includes/MslsBlogCollection.php +++ b/includes/MslsBlogCollection.php @@ -97,6 +97,7 @@ public function __construct() { $this->objects[ $blog->userblog_id ] = new MslsBlog( $blog, $description ); } } + uasort( $this->objects, array( MslsBlog::class, $this->objects_order ) ); } } @@ -109,7 +110,7 @@ public function __construct() { * * @return string|bool */ - public static function get_configured_blog_description( $blog_id, $description = false ) { + public static function get_configured_blog_description( int $blog_id, $description = false ) { if ( $description ) { return $description; } @@ -138,7 +139,8 @@ public function get_blogs_of_reference_user( MslsOptions $options ) { 1 ) ); - $blogs = get_blogs_of_user( $reference_user ); + + $blogs = get_blogs_of_user( $reference_user ); /** * @todo Check if this is still useful @@ -229,10 +231,21 @@ public function get_current_blog() { * * @return MslsBlog[] */ - public function get_objects() { + public function get_objects(): array { return apply_filters( 'msls_blog_collection_get_objects', $this->objects ); } + /** + * Gets a specific blog-object + * + * @param int $blog_id + * + * @return ?MslsBlog + */ + public function get_object( int $blog_id ): ?MslsBlog { + return $this->get_objects()[ $blog_id ] ?? null; + } + /** * Is plugin active in the blog with that blog_id * diff --git a/includes/MslsContentTypes.php b/includes/MslsContentTypes.php index d64f3727..ed322ff5 100644 --- a/includes/MslsContentTypes.php +++ b/includes/MslsContentTypes.php @@ -1,29 +1,27 @@ - * @since 0.9.8 - */ namespace lloc\Msls; /** * Supported content types + * * @package Msls */ abstract class MslsContentTypes extends MslsRegistryInstance { /** * Request + * * @var string */ protected $request; /** * Types + * * @var array */ - protected $types = []; + protected $types = array(); /** * Factory method @@ -33,13 +31,14 @@ abstract class MslsContentTypes extends MslsRegistryInstance { * @return MslsContentTypes */ public static function create() { - $_request = MslsPlugin::get_superglobals( [ 'taxonomy' ] ); + $_request = MslsPlugin::get_superglobals( array( 'taxonomy' ) ); return '' != $_request['taxonomy'] ? MslsTaxonomy::instance() : MslsPostType::instance(); } /** * Check for post_type + * * @return bool */ public function is_post_type() { @@ -48,6 +47,7 @@ public function is_post_type() { /** * Check for taxonomy + * * @return bool */ public function is_taxonomy() { @@ -68,6 +68,7 @@ public function acl_request() { /** * Getter + * * @return array */ abstract public static function get(): array; @@ -78,5 +79,4 @@ abstract public static function get(): array; * @return string */ abstract public function get_request(): string; - } diff --git a/includes/MslsCustomColumn.php b/includes/MslsCustomColumn.php index 8f3bd02e..b27b7cb0 100644 --- a/includes/MslsCustomColumn.php +++ b/includes/MslsCustomColumn.php @@ -1,15 +1,11 @@ - * @since 0.9.8 - */ namespace lloc\Msls; /** * Handling of existing/not existing translations in the backend listings of * various post types + * * @package Msls */ class MslsCustomColumn extends MslsMain { @@ -30,9 +26,9 @@ public static function init() { $post_type = MslsPostType::instance()->get_request(); if ( ! empty( $post_type ) ) { - add_filter( "manage_{$post_type}_posts_columns", [ $obj, 'th' ] ); - add_action( "manage_{$post_type}_posts_custom_column", [ $obj, 'td' ], 10, 2 ); - add_action( 'trashed_post', [ $obj, 'delete' ] ); + add_filter( "manage_{$post_type}_posts_columns", array( $obj, 'th' ) ); + add_action( "manage_{$post_type}_posts_custom_column", array( $obj, 'td' ), 10, 2 ); + add_action( 'trashed_post', array( $obj, 'delete' ) ); } } @@ -53,11 +49,11 @@ public function th( $columns ) { foreach ( $blogs as $blog ) { $language = $blog->get_language(); - $icon_type= $this->options->admin_display === 'label' ? 'label' : 'flag'; + $icon_type = $this->options->admin_display === 'label' ? 'label' : 'flag'; - $icon = new MslsAdminIcon( null ); + $icon = new MslsAdminIcon( null ); $icon->set_language( $language ); - $icon->set_icon_type( $icon_type ); + $icon->set_icon_type( $icon_type ); if ( $post_id = get_the_ID() ) { $icon->set_id( $post_id ); @@ -78,7 +74,7 @@ public function th( $columns ) { * Table body * * @param string $column_name - * @param int $item_id + * @param int $item_id * * @codeCoverageIgnore */ @@ -113,5 +109,4 @@ public function td( $column_name, $item_id ) { } } } - } diff --git a/includes/MslsCustomColumnTaxonomy.php b/includes/MslsCustomColumnTaxonomy.php index cd785fdd..3f4c859b 100644 --- a/includes/MslsCustomColumnTaxonomy.php +++ b/includes/MslsCustomColumnTaxonomy.php @@ -1,15 +1,11 @@ - * @since 0.9.8 - */ namespace lloc\Msls; /** * Handling of existing/not existing translations in the backend * listings of various taxonomies + * * @package Msls */ class MslsCustomColumnTaxonomy extends MslsCustomColumn { @@ -30,9 +26,9 @@ public static function init() { $taxonomy = MslsTaxonomy::instance()->get_request(); if ( ! empty( $taxonomy ) ) { - add_filter( "manage_edit-{$taxonomy}_columns", [ $obj, 'th' ] ); - add_action( "manage_{$taxonomy}_custom_column", [ $obj, 'column_default' ], - 100, 3 ); - add_action( "delete_{$taxonomy}", [ $obj, 'delete' ] ); + add_filter( "manage_edit-{$taxonomy}_columns", array( $obj, 'th' ) ); + add_action( "manage_{$taxonomy}_custom_column", array( $obj, 'column_default' ), - 100, 3 ); + add_action( "delete_{$taxonomy}", array( $obj, 'delete' ) ); } } @@ -44,7 +40,7 @@ public static function init() { * * @param string $deprecated * @param string $column_name - * @param int $item_id + * @param int $item_id */ public function column_default( $deprecated, $column_name, $item_id ) { $this->td( $column_name, $item_id ); @@ -60,5 +56,4 @@ public function column_default( $deprecated, $column_name, $item_id ) { public function delete( $object_id ) { $this->save( $object_id, MslsOptionsTax::class ); } - } diff --git a/includes/MslsCustomFilter.php b/includes/MslsCustomFilter.php index a0127df4..35c1da3a 100644 --- a/includes/MslsCustomFilter.php +++ b/includes/MslsCustomFilter.php @@ -43,7 +43,7 @@ public function add_filter(): void { $id = ( filter_has_var( INPUT_GET, 'msls_filter' ) ? filter_input( INPUT_GET, 'msls_filter', FILTER_SANITIZE_NUMBER_INT ) : - '' + '0' ); $blogs = $this->collection->get(); @@ -54,7 +54,7 @@ public function add_filter(): void { printf( '', $blog->userblog_id, - selected( $id, $blog->userblog_id, false ), + selected( intval( $id ), $blog->userblog_id, false ), sprintf( __( 'Not translated in the %s-blog', 'multisite-language-switcher' ), $blog->get_description() @@ -73,18 +73,18 @@ public function add_filter(): void { * @return bool|\WP_Query */ public function execute_filter( \WP_Query $query ) { - $blogs = $this->collection->get(); - if ( ! filter_has_var( INPUT_GET, 'msls_filter' ) ) { return false; } - $id = filter_input( INPUT_GET, 'msls_filter', FILTER_SANITIZE_NUMBER_INT ); - if ( isset( $blogs[ $id ] ) ) { + $id = filter_input( INPUT_GET, 'msls_filter', FILTER_SANITIZE_NUMBER_INT ); + $blog = $this->collection->get_object( intval( $id ) ); + + if ( $blog ) { $sql_cache = MslsSqlCacher::create( __CLASS__, __METHOD__ ); // load post we need to exclude (they already have a translation) from search query - $translated_posts = ( new TranslatedPostsQuery( $sql_cache ) )( $blogs[ $id ]->get_language() ); + $translated_posts = ( new TranslatedPostsQuery( $sql_cache ) )( $blog->get_language() ); $exclude_ids = array(); foreach ( $translated_posts as $post ) { diff --git a/includes/MslsGetSet.php b/includes/MslsGetSet.php index 791ea1cf..e764c768 100644 --- a/includes/MslsGetSet.php +++ b/includes/MslsGetSet.php @@ -1,10 +1,4 @@ - * @since 0.9.8 - */ namespace lloc\Msls; diff --git a/includes/MslsJson.php b/includes/MslsJson.php index 196ebcfd..66193aa7 100644 --- a/includes/MslsJson.php +++ b/includes/MslsJson.php @@ -1,14 +1,10 @@ - * @since 0.9.9 - */ namespace lloc\Msls; /** * Container for an array which will used in JavaScript as object in JSON + * * @example https://gist.githubusercontent.com/lloc/2c232cef3f910acf692f/raw/1c4f62e1de57ca48f19c37e3a63e7dc311b76b2f/MslsJson.php * @package Msls */ @@ -16,9 +12,10 @@ class MslsJson { /** * Container + * * @var array */ - protected array $arr = []; + protected array $arr = array(); /** MslsLanguageArray * Adds a value label pair to the internal class container @@ -29,10 +26,10 @@ class MslsJson { * @return MslsJson */ public function add( $value, $label ) { - $this->arr[] = [ + $this->arr[] = array( 'value' => intval( $value ), 'label' => strval( $label ), - ]; + ); return $this; } @@ -57,7 +54,7 @@ public static function compare( array $a, array $b ) { public function get(): array { $arr = $this->arr; - usort( $arr, [ __CLASS__, 'compare' ] ); + usort( $arr, array( __CLASS__, 'compare' ) ); return $arr; } @@ -79,5 +76,4 @@ public function encode(): string { public function __toString() { return $this->encode(); } - } diff --git a/includes/MslsLanguageArray.php b/includes/MslsLanguageArray.php index e205fac3..1d96a447 100644 --- a/includes/MslsLanguageArray.php +++ b/includes/MslsLanguageArray.php @@ -1,14 +1,10 @@ - * @since 0.9.8 - */ namespace lloc\Msls; /** * Stores the language input from post + * * @example https://gist.githubusercontent.com/lloc/2c232cef3f910acf692f/raw/c78a78b42cb4c9e97a118523f7497f02b838a2ee/MslsLanguageArray.php * @package Msls */ @@ -16,6 +12,7 @@ class MslsLanguageArray { /** * Generic container + * * @var array */ protected $arr; @@ -25,7 +22,7 @@ class MslsLanguageArray { * * @param array $arr */ - public function __construct( array $arr = [] ) { + public function __construct( array $arr = array() ) { foreach ( $arr as $key => $value ) { $this->set( $key, $value ); } @@ -37,7 +34,7 @@ public function __construct( array $arr = [] ) { * - $value must be an integer > 0 * * @param string $key - * @param mixed $value + * @param mixed $value * * @return MslsLanguageArray */ @@ -77,5 +74,4 @@ public function get_arr( $key = '' ) { return $arr; } - } diff --git a/includes/MslsLink.php b/includes/MslsLink.php index 8daf8682..9518cdea 100644 --- a/includes/MslsLink.php +++ b/includes/MslsLink.php @@ -1,15 +1,10 @@ - * @since 0.9.8 - */ namespace lloc\Msls; /** * Link type: Image and text + * * @package Msls * @property string $txt * @property string $src @@ -20,6 +15,7 @@ class MslsLink extends MslsGetSet { /** * Output format + * * @var string */ protected $format_string = '{alt} {txt}'; @@ -30,12 +26,12 @@ class MslsLink extends MslsGetSet { * @return string[] */ public static function get_types() { - return [ - MslsLink::class, + return array( + self::class, MslsLinkTextOnly::class, MslsLinkImageOnly::class, MslsLinkTextImage::class, - ]; + ); } /** @@ -53,10 +49,10 @@ public static function get_description() { * @return array */ public static function get_types_description() { - $types = []; + $types = array(); foreach ( self::get_types() as $key => $class ) { - $types[ $key ] = call_user_func( [ $class, 'get_description' ] ); + $types[ $key ] = call_user_func( array( $class, 'get_description' ) ); } return $types; @@ -80,7 +76,6 @@ public static function create( $display ) { * * @return MslsLink * @since 0.9.9 - * */ $obj = apply_filters( 'msls_link_create', $display ); if ( is_subclass_of( $obj, __CLASS__ ) ) { @@ -93,7 +88,7 @@ public static function create( $display ) { $display = 0; } - return new $types[ $display ]; + return new $types[ $display ](); } /** @@ -116,10 +111,9 @@ public function __toString() { $temp = $this->get_arr(); return str_replace( - array_map( [ $this, 'callback' ], array_keys( $temp ) ), + array_map( array( $this, 'callback' ), array_keys( $temp ) ), $temp, $this->format_string ); } - } diff --git a/includes/MslsLinkImageOnly.php b/includes/MslsLinkImageOnly.php index dbb8ab13..31178377 100644 --- a/includes/MslsLinkImageOnly.php +++ b/includes/MslsLinkImageOnly.php @@ -1,30 +1,27 @@ - * @since 0.9.8 - */ namespace lloc\Msls; /** * Link type: Image only + * * @package Msls */ class MslsLinkImageOnly extends MslsLink { /** * Output format + * * @var string */ protected $format_string = '{alt}'; /** * Get the description + * * @return string */ public static function get_description() { return __( 'Flag only', 'multisite-language-switcher' ); } - } diff --git a/includes/MslsLinkTextImage.php b/includes/MslsLinkTextImage.php index 1a4083ee..acfd60c6 100644 --- a/includes/MslsLinkTextImage.php +++ b/includes/MslsLinkTextImage.php @@ -1,30 +1,27 @@ - * @since 0.9.8 - */ namespace lloc\Msls; /** * Link type: Text and image + * * @package Msls */ class MslsLinkTextImage extends MslsLink { /** * Output format + * * @var string */ protected $format_string = '{txt} {alt}'; /** * Get the description + * * @return string */ public static function get_description() { return __( 'Description and flag', 'multisite-language-switcher' ); } - } diff --git a/includes/MslsLinkTextOnly.php b/includes/MslsLinkTextOnly.php index f4821816..81c4fb8a 100644 --- a/includes/MslsLinkTextOnly.php +++ b/includes/MslsLinkTextOnly.php @@ -1,30 +1,27 @@ - * @since 0.9.8 - */ namespace lloc\Msls; /** * Link type: Text only + * * @package Msls */ class MslsLinkTextOnly extends MslsLink { /** * Output format + * * @var string */ protected $format_string = '{txt}'; /** * Get the description + * * @return string */ public static function get_description() { return __( 'Description only', 'multisite-language-switcher' ); } - } diff --git a/includes/MslsMain.php b/includes/MslsMain.php index ef7ea176..79a9e48f 100644 --- a/includes/MslsMain.php +++ b/includes/MslsMain.php @@ -1,9 +1,4 @@ - * @since 0.9.8 - */ namespace lloc\Msls; @@ -31,7 +26,7 @@ class MslsMain { /** * Constructor * - * @param MslsOptions $options + * @param MslsOptions $options * @param MslsBlogCollection $collection */ public function __construct( MslsOptions $options, MslsBlogCollection $collection ) { @@ -73,7 +68,7 @@ public function debugger( $message ): void { * @return array */ public function get_input_array( $object_id ) { - $arr = []; + $arr = array(); $current_blog = $this->collection->get_current_blog(); if ( ! is_null( $current_blog ) ) { @@ -105,10 +100,10 @@ public function get_input_array( $object_id ) { */ protected function get_input_value( $key, $value ) { if ( false === strpos( $key, 'msls_input_' ) || empty( $value ) ) { - return [ '', 0 ]; + return array( '', 0 ); } - return [ substr( $key, 11 ), intval( $value ) ]; + return array( substr( $key, 11 ), intval( $value ) ); } /** @@ -148,7 +143,7 @@ public function delete( $object_id ) { /** * Save * - * @param int $object_id + * @param int $object_id * @param string $class * * @codeCoverageIgnore @@ -162,7 +157,6 @@ protected function save( $object_id, $class ) { * @param string Classname * * @since 0.9.9 - * */ do_action( 'msls_main_save', $object_id, $class ); @@ -203,5 +197,4 @@ protected function save( $object_id, $class ) { restore_current_blog(); } } - } diff --git a/patchwork.json b/patchwork.json index 82f0cc25..899174c0 100644 --- a/patchwork.json +++ b/patchwork.json @@ -1 +1 @@ -{"redefinable-internals": ["filter_input_array", "filter_has_var"]} \ No newline at end of file +{"redefinable-internals": ["filter_input_array", "filter_has_var", "filter_input"]} \ No newline at end of file diff --git a/tests/TestMslsCustomFilter.php b/tests/TestMslsCustomFilter.php index 2ea999be..aa97be8a 100644 --- a/tests/TestMslsCustomFilter.php +++ b/tests/TestMslsCustomFilter.php @@ -2,6 +2,7 @@ namespace lloc\MslsTests; +use lloc\Msls\MslsBlog; use lloc\Msls\MslsBlogCollection; use lloc\Msls\MslsCustomFilter; use lloc\Msls\MslsOptions; @@ -9,36 +10,76 @@ class TestMslsCustomFilter extends MslsUnitTestCase { - protected function setUp(): void - { - parent::setUp(); // TODO: Change the autogenerated stub + protected function setUp(): void { + parent::setUp(); - $options = \Mockery::mock( MslsOptions::class ); + $options = \Mockery::mock( MslsOptions::class ); - $collection = \Mockery::mock( MslsBlogCollection::class ); - $collection->shouldReceive( 'get' )->once()->andReturn( [] ); + $blog = \Mockery::mock( MslsBlog::class ); + $blog->userblog_id = 1; + $blog->shouldReceive( 'get_language' )->andReturn( 'de_DE' ); + $blog->shouldReceive( 'get_description' )->andReturn( 'Deutsch' ); - $this->test = new MslsCustomFilter( $options, $collection ); + $collection = \Mockery::mock( MslsBlogCollection::class ); + $collection->shouldReceive( 'get' )->andReturns( array( $blog ) ); + $collection->shouldReceive( 'get_object' )->with( 1 )->andReturns( $blog ); + $collection->shouldReceive( 'get_object' )->with( 2 )->andReturns( null ); - } + $this->test = new MslsCustomFilter( $options, $collection ); + } - public function test_execute_filter(): void { - $query = \Mockery::mock( 'WP_Query' ); + public function test_execute_filter(): void { + $query = \Mockery::mock( 'WP_Query' ); - $this->assertFalse( $this->test->execute_filter( $query ) ); + $this->assertFalse( $this->test->execute_filter( $query ) ); } - public function test_execute_filter_with_filter_input(): void { - Functions\expect('filter_has_var')->once()->with( INPUT_GET, 'msls_filter' )->andReturn( true ); + public function test_execute_filter_with_filter_input(): void { + global $wpdb; + + $result = array( + (object) array( + 'option_id' => 1, + 'option_name' => 'msls_123', + ), + ); + + Functions\expect( 'filter_has_var' )->once()->with( INPUT_GET, 'msls_filter' )->andReturn( true ); + Functions\expect( 'filter_input' )->once()->andReturn( '1' ); + Functions\expect( 'wp_cache_get' )->once()->andReturn( $result ); + + $wpdb = \Mockery::mock( '\wpdb' ); + $wpdb->shouldReceive( 'prepare' )->once(); - $query = \Mockery::mock( 'WP_Query' ); + $query = \Mockery::mock( '\WP_Query' ); - $this->assertFalse( $this->test->execute_filter( $query ) ); - } + $this->assertInstanceOf( '\WP_Query', $this->test->execute_filter( $query ) ); + } + + public function test_execute_filter_with_filter_but_no_blog(): void { + Functions\expect( 'filter_has_var' )->once()->with( INPUT_GET, 'msls_filter' )->andReturn( true ); + Functions\expect( 'filter_input' )->once()->andReturn( '2' ); - public function test_add_filter(): void { - $this->expectOutputString('' ); + $query = \Mockery::mock( '\WP_Query' ); + + $this->assertFalse( $this->test->execute_filter( $query ) ); + } - $this->test->add_filter(); - } + public function test_add_filter(): void { + Functions\expect( 'filter_has_var' )->once()->with( INPUT_GET, 'msls_filter' )->andReturn( true ); + Functions\expect( 'filter_input' )->once()->andReturn( '1' ); + Functions\expect( 'selected' )->once()->with( 1, 1, false )->andReturn( 'selected="selected"' ); + + $this->expectOutputString( '' ); + + $this->test->add_filter(); + } + + public function test_add_no_selected_blog(): void { + Functions\expect( 'selected' )->once()->with( 0, 1, false )->andReturn( '' ); + + $this->expectOutputString( '' ); + + $this->test->add_filter(); + } }