Skip to content

Commit 87dd41a

Browse files
authored
MslsCustom class tested (#307)
1 parent d7f1d9c commit 87dd41a

15 files changed

+139
-131
lines changed

includes/MslsBlogCollection.php

+16-3
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ public function __construct() {
9797
$this->objects[ $blog->userblog_id ] = new MslsBlog( $blog, $description );
9898
}
9999
}
100+
100101
uasort( $this->objects, array( MslsBlog::class, $this->objects_order ) );
101102
}
102103
}
@@ -109,7 +110,7 @@ public function __construct() {
109110
*
110111
* @return string|bool
111112
*/
112-
public static function get_configured_blog_description( $blog_id, $description = false ) {
113+
public static function get_configured_blog_description( int $blog_id, $description = false ) {
113114
if ( $description ) {
114115
return $description;
115116
}
@@ -138,7 +139,8 @@ public function get_blogs_of_reference_user( MslsOptions $options ) {
138139
1
139140
)
140141
);
141-
$blogs = get_blogs_of_user( $reference_user );
142+
143+
$blogs = get_blogs_of_user( $reference_user );
142144

143145
/**
144146
* @todo Check if this is still useful
@@ -229,10 +231,21 @@ public function get_current_blog() {
229231
*
230232
* @return MslsBlog[]
231233
*/
232-
public function get_objects() {
234+
public function get_objects(): array {
233235
return apply_filters( 'msls_blog_collection_get_objects', $this->objects );
234236
}
235237

238+
/**
239+
* Gets a specific blog-object
240+
*
241+
* @param int $blog_id
242+
*
243+
* @return ?MslsBlog
244+
*/
245+
public function get_object( int $blog_id ): ?MslsBlog {
246+
return $this->get_objects()[ $blog_id ] ?? null;
247+
}
248+
236249
/**
237250
* Is plugin active in the blog with that blog_id
238251
*

includes/MslsContentTypes.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,27 @@
11
<?php
2-
/**
3-
* MslsContentTypes
4-
* @author Dennis Ploetner <[email protected]>
5-
* @since 0.9.8
6-
*/
72

83
namespace lloc\Msls;
94

105
/**
116
* Supported content types
7+
*
128
* @package Msls
139
*/
1410
abstract class MslsContentTypes extends MslsRegistryInstance {
1511

1612
/**
1713
* Request
14+
*
1815
* @var string
1916
*/
2017
protected $request;
2118

2219
/**
2320
* Types
21+
*
2422
* @var array
2523
*/
26-
protected $types = [];
24+
protected $types = array();
2725

2826
/**
2927
* Factory method
@@ -33,13 +31,14 @@ abstract class MslsContentTypes extends MslsRegistryInstance {
3331
* @return MslsContentTypes
3432
*/
3533
public static function create() {
36-
$_request = MslsPlugin::get_superglobals( [ 'taxonomy' ] );
34+
$_request = MslsPlugin::get_superglobals( array( 'taxonomy' ) );
3735

3836
return '' != $_request['taxonomy'] ? MslsTaxonomy::instance() : MslsPostType::instance();
3937
}
4038

4139
/**
4240
* Check for post_type
41+
*
4342
* @return bool
4443
*/
4544
public function is_post_type() {
@@ -48,6 +47,7 @@ public function is_post_type() {
4847

4948
/**
5049
* Check for taxonomy
50+
*
5151
* @return bool
5252
*/
5353
public function is_taxonomy() {
@@ -68,6 +68,7 @@ public function acl_request() {
6868

6969
/**
7070
* Getter
71+
*
7172
* @return array
7273
*/
7374
abstract public static function get(): array;
@@ -78,5 +79,4 @@ abstract public static function get(): array;
7879
* @return string
7980
*/
8081
abstract public function get_request(): string;
81-
8282
}

includes/MslsCustomColumn.php

+8-13
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
<?php
2-
/**
3-
* MslsCustomColumn
4-
* @author Dennis Ploetner <[email protected]>
5-
* @since 0.9.8
6-
*/
72

83
namespace lloc\Msls;
94

105
/**
116
* Handling of existing/not existing translations in the backend listings of
127
* various post types
8+
*
139
* @package Msls
1410
*/
1511
class MslsCustomColumn extends MslsMain {
@@ -30,9 +26,9 @@ public static function init() {
3026
$post_type = MslsPostType::instance()->get_request();
3127

3228
if ( ! empty( $post_type ) ) {
33-
add_filter( "manage_{$post_type}_posts_columns", [ $obj, 'th' ] );
34-
add_action( "manage_{$post_type}_posts_custom_column", [ $obj, 'td' ], 10, 2 );
35-
add_action( 'trashed_post', [ $obj, 'delete' ] );
29+
add_filter( "manage_{$post_type}_posts_columns", array( $obj, 'th' ) );
30+
add_action( "manage_{$post_type}_posts_custom_column", array( $obj, 'td' ), 10, 2 );
31+
add_action( 'trashed_post', array( $obj, 'delete' ) );
3632
}
3733
}
3834

@@ -53,11 +49,11 @@ public function th( $columns ) {
5349
foreach ( $blogs as $blog ) {
5450
$language = $blog->get_language();
5551

56-
$icon_type= $this->options->admin_display === 'label' ? 'label' : 'flag';
52+
$icon_type = $this->options->admin_display === 'label' ? 'label' : 'flag';
5753

58-
$icon = new MslsAdminIcon( null );
54+
$icon = new MslsAdminIcon( null );
5955
$icon->set_language( $language );
60-
$icon->set_icon_type( $icon_type );
56+
$icon->set_icon_type( $icon_type );
6157

6258
if ( $post_id = get_the_ID() ) {
6359
$icon->set_id( $post_id );
@@ -78,7 +74,7 @@ public function th( $columns ) {
7874
* Table body
7975
*
8076
* @param string $column_name
81-
* @param int $item_id
77+
* @param int $item_id
8278
*
8379
* @codeCoverageIgnore
8480
*/
@@ -113,5 +109,4 @@ public function td( $column_name, $item_id ) {
113109
}
114110
}
115111
}
116-
117112
}

includes/MslsCustomColumnTaxonomy.php

+5-10
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
<?php
2-
/**
3-
* MslsCustomColumnTaxonomy
4-
* @author Dennis Ploetner <[email protected]>
5-
* @since 0.9.8
6-
*/
72

83
namespace lloc\Msls;
94

105
/**
116
* Handling of existing/not existing translations in the backend
127
* listings of various taxonomies
8+
*
139
* @package Msls
1410
*/
1511
class MslsCustomColumnTaxonomy extends MslsCustomColumn {
@@ -30,9 +26,9 @@ public static function init() {
3026
$taxonomy = MslsTaxonomy::instance()->get_request();
3127

3228
if ( ! empty( $taxonomy ) ) {
33-
add_filter( "manage_edit-{$taxonomy}_columns", [ $obj, 'th' ] );
34-
add_action( "manage_{$taxonomy}_custom_column", [ $obj, 'column_default' ], - 100, 3 );
35-
add_action( "delete_{$taxonomy}", [ $obj, 'delete' ] );
29+
add_filter( "manage_edit-{$taxonomy}_columns", array( $obj, 'th' ) );
30+
add_action( "manage_{$taxonomy}_custom_column", array( $obj, 'column_default' ), - 100, 3 );
31+
add_action( "delete_{$taxonomy}", array( $obj, 'delete' ) );
3632
}
3733
}
3834

@@ -44,7 +40,7 @@ public static function init() {
4440
*
4541
* @param string $deprecated
4642
* @param string $column_name
47-
* @param int $item_id
43+
* @param int $item_id
4844
*/
4945
public function column_default( $deprecated, $column_name, $item_id ) {
5046
$this->td( $column_name, $item_id );
@@ -60,5 +56,4 @@ public function column_default( $deprecated, $column_name, $item_id ) {
6056
public function delete( $object_id ) {
6157
$this->save( $object_id, MslsOptionsTax::class );
6258
}
63-
6459
}

includes/MslsCustomFilter.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function add_filter(): void {
4343
$id = (
4444
filter_has_var( INPUT_GET, 'msls_filter' ) ?
4545
filter_input( INPUT_GET, 'msls_filter', FILTER_SANITIZE_NUMBER_INT ) :
46-
''
46+
'0'
4747
);
4848

4949
$blogs = $this->collection->get();
@@ -54,7 +54,7 @@ public function add_filter(): void {
5454
printf(
5555
'<option value="%d" %s>%s</option>',
5656
$blog->userblog_id,
57-
selected( $id, $blog->userblog_id, false ),
57+
selected( intval( $id ), $blog->userblog_id, false ),
5858
sprintf(
5959
__( 'Not translated in the %s-blog', 'multisite-language-switcher' ),
6060
$blog->get_description()
@@ -73,18 +73,18 @@ public function add_filter(): void {
7373
* @return bool|\WP_Query
7474
*/
7575
public function execute_filter( \WP_Query $query ) {
76-
$blogs = $this->collection->get();
77-
7876
if ( ! filter_has_var( INPUT_GET, 'msls_filter' ) ) {
7977
return false;
8078
}
8179

82-
$id = filter_input( INPUT_GET, 'msls_filter', FILTER_SANITIZE_NUMBER_INT );
83-
if ( isset( $blogs[ $id ] ) ) {
80+
$id = filter_input( INPUT_GET, 'msls_filter', FILTER_SANITIZE_NUMBER_INT );
81+
$blog = $this->collection->get_object( intval( $id ) );
82+
83+
if ( $blog ) {
8484
$sql_cache = MslsSqlCacher::create( __CLASS__, __METHOD__ );
8585

8686
// load post we need to exclude (they already have a translation) from search query
87-
$translated_posts = ( new TranslatedPostsQuery( $sql_cache ) )( $blogs[ $id ]->get_language() );
87+
$translated_posts = ( new TranslatedPostsQuery( $sql_cache ) )( $blog->get_language() );
8888

8989
$exclude_ids = array();
9090
foreach ( $translated_posts as $post ) {

includes/MslsGetSet.php

-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
<?php
2-
/**
3-
* MslsGetSet
4-
*
5-
* @author Dennis Ploetner <[email protected]>
6-
* @since 0.9.8
7-
*/
82

93
namespace lloc\Msls;
104

includes/MslsJson.php

+6-10
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
11
<?php
2-
/**
3-
* MslsJson
4-
* @author Dennis Ploetner <[email protected]>
5-
* @since 0.9.9
6-
*/
72

83
namespace lloc\Msls;
94

105
/**
116
* Container for an array which will used in JavaScript as object in JSON
7+
*
128
* @example https://gist.githubusercontent.com/lloc/2c232cef3f910acf692f/raw/1c4f62e1de57ca48f19c37e3a63e7dc311b76b2f/MslsJson.php
139
* @package Msls
1410
*/
1511
class MslsJson {
1612

1713
/**
1814
* Container
15+
*
1916
* @var array
2017
*/
21-
protected array $arr = [];
18+
protected array $arr = array();
2219

2320
/** MslsLanguageArray
2421
* Adds a value label pair to the internal class container
@@ -29,10 +26,10 @@ class MslsJson {
2926
* @return MslsJson
3027
*/
3128
public function add( $value, $label ) {
32-
$this->arr[] = [
29+
$this->arr[] = array(
3330
'value' => intval( $value ),
3431
'label' => strval( $label ),
35-
];
32+
);
3633

3734
return $this;
3835
}
@@ -57,7 +54,7 @@ public static function compare( array $a, array $b ) {
5754
public function get(): array {
5855
$arr = $this->arr;
5956

60-
usort( $arr, [ __CLASS__, 'compare' ] );
57+
usort( $arr, array( __CLASS__, 'compare' ) );
6158

6259
return $arr;
6360
}
@@ -79,5 +76,4 @@ public function encode(): string {
7976
public function __toString() {
8077
return $this->encode();
8178
}
82-
8379
}

includes/MslsLanguageArray.php

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
<?php
2-
/**
3-
* MslsLanguageArray
4-
* @author Dennis Ploetner <[email protected]>
5-
* @since 0.9.8
6-
*/
72

83
namespace lloc\Msls;
94

105
/**
116
* Stores the language input from post
7+
*
128
* @example https://gist.githubusercontent.com/lloc/2c232cef3f910acf692f/raw/c78a78b42cb4c9e97a118523f7497f02b838a2ee/MslsLanguageArray.php
139
* @package Msls
1410
*/
1511
class MslsLanguageArray {
1612

1713
/**
1814
* Generic container
15+
*
1916
* @var array
2017
*/
2118
protected $arr;
@@ -25,7 +22,7 @@ class MslsLanguageArray {
2522
*
2623
* @param array $arr
2724
*/
28-
public function __construct( array $arr = [] ) {
25+
public function __construct( array $arr = array() ) {
2926
foreach ( $arr as $key => $value ) {
3027
$this->set( $key, $value );
3128
}
@@ -37,7 +34,7 @@ public function __construct( array $arr = [] ) {
3734
* - $value must be an integer > 0
3835
*
3936
* @param string $key
40-
* @param mixed $value
37+
* @param mixed $value
4138
*
4239
* @return MslsLanguageArray
4340
*/
@@ -77,5 +74,4 @@ public function get_arr( $key = '' ) {
7774

7875
return $arr;
7976
}
80-
8177
}

0 commit comments

Comments
 (0)