Skip to content

Commit ca53a1b

Browse files
authored
Merge pull request #309 from lloc/refactoring-version-2-8
MslsCustomFilter refactoring
2 parents 1e17331 + a8ee200 commit ca53a1b

14 files changed

+110
-91
lines changed

includes/Component/Input/Select.php

+6-5
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ class Select implements InputInterface {
1717
protected $options;
1818

1919
/**
20-
* @param string $key Name and ID of the form-element
20+
* @param string $key Name and ID of the form-element
2121
* @param string[] $arr Options as associative array
22-
* @param ?string $selected Values which should be selected
22+
* @param ?string $selected Values which should be selected
2323
*/
2424
public function __construct( string $key, array $arr, ?string $selected = null ) {
2525
$this->key = esc_attr( $key );
@@ -34,7 +34,8 @@ public function __construct( string $key, array $arr, ?string $selected = null )
3434
* @return string
3535
*/
3636
public function render(): string {
37-
return sprintf( '<select id="%1$s" name="msls[%1$s]">%2$s</select>', $this->key, $this->options->render() );
38-
}
37+
$name = apply_filters( 'msls_input_select_name', 'msls[' . $this->key . ']' );
3938

40-
}
39+
return sprintf( '<select id="%1$s" name="%2$s">%3$s</select>', $this->key, $name, $this->options->render() );
40+
}
41+
}

includes/MslsCustomFilter.php

+21-23
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
namespace lloc\Msls;
44

5-
use lloc\Msls\Query\TranslatedPostsQuery;
5+
use lloc\Msls\Component\Input\Select;
6+
use lloc\Msls\Query\TranslatedPostIdQuery;
67

78
/**
89
* Adding custom filter to posts/pages table.
@@ -11,6 +12,8 @@
1112
*/
1213
class MslsCustomFilter extends MslsMain {
1314

15+
const FILTER_NAME = 'msls_filter';
16+
1417
/**
1518
* Init
1619
*
@@ -28,6 +31,12 @@ public static function init() {
2831
if ( ! empty( $post_type ) ) {
2932
add_action( 'restrict_manage_posts', array( $obj, 'add_filter' ) );
3033
add_filter( 'parse_query', array( $obj, 'execute_filter' ) );
34+
add_filter(
35+
'msls_input_select_name',
36+
function () {
37+
return self::FILTER_NAME;
38+
}
39+
);
3140
}
3241
}
3342

@@ -41,27 +50,22 @@ public static function init() {
4150
*/
4251
public function add_filter(): void {
4352
$id = (
44-
filter_has_var( INPUT_GET, 'msls_filter' ) ?
45-
filter_input( INPUT_GET, 'msls_filter', FILTER_SANITIZE_NUMBER_INT ) :
53+
filter_has_var( INPUT_GET, self::FILTER_NAME ) ?
54+
filter_input( INPUT_GET, self::FILTER_NAME, FILTER_SANITIZE_NUMBER_INT ) :
4655
'0'
4756
);
4857

4958
$blogs = $this->collection->get();
5059
if ( $blogs ) {
51-
echo '<select name="msls_filter" id="msls_filter">';
52-
echo '<option value="">' . esc_html( __( 'Show all blogs', 'multisite-language-switcher' ) ) . '</option>';
60+
$options = array( '' => esc_html( __( 'Show all posts', 'multisite-language-switcher' ) ) );
5361
foreach ( $blogs as $blog ) {
54-
printf(
55-
'<option value="%d" %s>%s</option>',
56-
$blog->userblog_id,
57-
selected( intval( $id ), $blog->userblog_id, false ),
58-
sprintf(
59-
__( 'Not translated in the %s-blog', 'multisite-language-switcher' ),
60-
$blog->get_description()
61-
)
62+
$options[ strval( $blog->userblog_id ) ] = sprintf(
63+
__( 'Not translated in the %s-blog', 'multisite-language-switcher' ),
64+
$blog->get_description()
6265
);
6366
}
64-
echo '</select>';
67+
68+
echo ( new Select( self::FILTER_NAME, $options, $id ) )->render();
6569
}
6670
}
6771

@@ -73,24 +77,18 @@ public function add_filter(): void {
7377
* @return bool|\WP_Query
7478
*/
7579
public function execute_filter( \WP_Query $query ) {
76-
if ( ! filter_has_var( INPUT_GET, 'msls_filter' ) ) {
80+
if ( ! filter_has_var( INPUT_GET, self::FILTER_NAME ) ) {
7781
return false;
7882
}
7983

80-
$id = filter_input( INPUT_GET, 'msls_filter', FILTER_SANITIZE_NUMBER_INT );
84+
$id = filter_input( INPUT_GET, self::FILTER_NAME, FILTER_SANITIZE_NUMBER_INT );
8185
$blog = $this->collection->get_object( intval( $id ) );
8286

8387
if ( $blog ) {
8488
$sql_cache = MslsSqlCacher::create( __CLASS__, __METHOD__ );
8589

8690
// load post we need to exclude (they already have a translation) from search query
87-
$translated_posts = ( new TranslatedPostsQuery( $sql_cache ) )( $blog->get_language() );
88-
89-
$exclude_ids = array();
90-
foreach ( $translated_posts as $post ) {
91-
$exclude_ids[] = substr( $post->option_name, 5 );
92-
}
93-
$query->query_vars['post__not_in'] = $exclude_ids;
91+
$query->query_vars['post__not_in'] = ( new TranslatedPostIdQuery( $sql_cache ) )( $blog->get_language() );
9492

9593
return $query;
9694
}

includes/MslsOptionsTax.php

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

93
namespace lloc\Msls;
104

115
/**
12-
* Taxonomy options
6+
* MslsOptionsTax
137
*
148
* @package Msls
159
*/

includes/MslsOptionsTaxTerm.php

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

83
namespace lloc\Msls;
94

105
/**
11-
* Tag options
6+
* MslsOptionsTaxTerm
7+
*
128
* @package Msls
139
*/
1410
class MslsOptionsTaxTerm extends MslsOptionsTax {
1511

1612
/**
1713
* Base option
14+
*
1815
* @var string
1916
*/
2017
protected $base_option = 'tag_base';
2118

2219
/**
2320
* Base definition
21+
*
2422
* @var string
2523
*/
2624
protected $base_defined = 'tag';
2725

2826
/**
2927
* Rewrite with front
28+
*
3029
* @var bool
3130
*/
3231
public $with_front = true;
3332

3433
/**
3534
* Check and correct URL
3635
*
37-
* @param string $url
36+
* @param string $url
3837
* @param MslsOptions $options
3938
*
4039
* @return string
@@ -72,5 +71,4 @@ public function check_base( $url, $options ) {
7271

7372
return $url;
7473
}
75-
7674
}
+4-7
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
11
<?php
2-
/**
3-
* MslsOptionsTaxTermCategory
4-
* @author Dennis Ploetner <[email protected]>
5-
* @since 0.9.8
6-
*/
72

83
namespace lloc\Msls;
94

105
/**
11-
* Category options
6+
* MslsOptionsTaxTermCategory
7+
*
128
* @package Msls
139
*/
1410
class MslsOptionsTaxTermCategory extends MslsOptionsTaxTerm {
1511

1612
/**
1713
* Base option
14+
*
1815
* @var string
1916
*/
2017
protected $base_option = 'category_base';
2118

2219
/**
2320
* Base standard definition
21+
*
2422
* @var string
2523
*/
2624
protected $base_defined = 'category';
27-
2825
}

includes/MslsOutput.php

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

83
namespace lloc\Msls;
94

105
use lloc\Msls\Map\HrefLang;
116

127
/**
138
* Output in the frontend
9+
*
1410
* @package Msls
1511
*/
1612
class MslsOutput extends MslsMain {
1713

1814
/**
1915
* Holds the format for the output
16+
*
2017
* @var array $tags
2118
*/
2219
protected $tags;
2320

2421
/**
2522
* Creates and gets the output as an array
2623
*
27-
* @param int $display
24+
* @param int $display
2825
* @param bool $filter
2926
* @param bool $exists
3027
*
@@ -33,7 +30,7 @@ class MslsOutput extends MslsMain {
3330
* @uses MslsOptions
3431
*/
3532
public function get( $display, $filter = false, $exists = false ) {
36-
$arr = [];
33+
$arr = array();
3734

3835
$blogs = $this->collection->get_filtered( $filter );
3936
if ( $blogs ) {
@@ -73,9 +70,8 @@ public function get( $display, $filter = false, $exists = false ) {
7370
* @param bool $is_current_blog
7471
*
7572
* @since 0.9.8
76-
*
7773
*/
78-
$arr[] = ( string ) apply_filters( 'msls_output_get', $url, $link, $is_current_blog );
74+
$arr[] = (string) apply_filters( 'msls_output_get', $url, $link, $is_current_blog );
7975
} else {
8076
$arr[] = sprintf(
8177
'<a href="%s" title="%s"%s>%s</a>',
@@ -101,7 +97,7 @@ public function get_alternate_links() {
10197
$hreflang = new HrefLang( $blogs );
10298
$options = MslsOptions::create();
10399

104-
$arr = [];
100+
$arr = array();
105101
$default = '';
106102

107103
foreach ( $blogs->get_objects() as $blog ) {
@@ -132,6 +128,7 @@ public function get_alternate_links() {
132128

133129
/**
134130
* Returns a string when the object will be treated like a string
131+
*
135132
* @return string
136133
*/
137134
public function __toString() {
@@ -147,32 +144,32 @@ public function __toString() {
147144
$tags = $this->get_tags();
148145

149146
return $tags['before_output'] . $tags['before_item'] .
150-
implode( $tags['after_item'] . $tags['before_item'], $arr ) .
151-
$tags['after_item'] . $tags['after_output'];
147+
implode( $tags['after_item'] . $tags['before_item'], $arr ) .
148+
$tags['after_item'] . $tags['after_output'];
152149
}
153150

154151
/**
155152
* Gets tags for the output
153+
*
156154
* @return array
157155
*/
158156
public function get_tags() {
159157
if ( empty( $this->tags ) ) {
160-
$this->tags = [
158+
$this->tags = array(
161159
'before_item' => $this->options->before_item,
162160
'after_item' => $this->options->after_item,
163161
'before_output' => $this->options->before_output,
164162
'after_output' => $this->options->after_output,
165-
];
163+
);
166164

167165
/**
168166
* Returns tags array for the output
169167
*
170168
* @param array $tags
171169
*
172170
* @since 1.0
173-
*
174171
*/
175-
$this->tags = ( array ) apply_filters( 'msls_output_get_tags', $this->tags );
172+
$this->tags = (array) apply_filters( 'msls_output_get_tags', $this->tags );
176173
}
177174

178175
return $this->tags;
@@ -185,7 +182,7 @@ public function get_tags() {
185182
*
186183
* @return MslsOutput
187184
*/
188-
public function set_tags( array $arr = [] ) {
185+
public function set_tags( array $arr = array() ) {
189186
$this->tags = wp_parse_args( $this->get_tags(), $arr );
190187

191188
return $this;
@@ -195,8 +192,8 @@ public function set_tags( array $arr = [] ) {
195192
* Returns true if the requirements not fulfilled
196193
*
197194
* @param MslsOptions|null $thing
198-
* @param boolean $exists
199-
* @param string $language
195+
* @param boolean $exists
196+
* @param string $language
200197
*
201198
* @return boolean
202199
*/
@@ -207,5 +204,4 @@ public function is_requirements_not_fulfilled( $thing, $exists, $language ) {
207204

208205
return MslsOptions::class != get_class( $thing ) && ! $thing->has_value( $language ) && $exists;
209206
}
210-
211207
}

includes/MslsPlugin.php

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

93
namespace lloc\Msls;
104

includes/Query/AbstractQuery.php

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
use lloc\Msls\MslsSqlCacher;
66

7+
/**
8+
* AbstractQuery
9+
*
10+
* @package Msls
11+
*/
712
abstract class AbstractQuery {
813

914
protected MslsSqlCacher $sql_cache;

includes/Query/AuthorPostsCounterQuery.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
namespace lloc\Msls\Query;
44

5+
/**
6+
* Gets the number of published posts by an author
7+
*
8+
* @package Msls
9+
*/
510
class AuthorPostsCounterQuery extends AbstractQuery {
611

7-
812
public function __invoke( int $author_id ) {
913
if ( $author_id <= 0 ) {
1014
return 0;

0 commit comments

Comments
 (0)