1
1
<?php
2
- /**
3
- * MslsCustomFilter
4
- * @author Maciej Czerpiński <[email protected] >
5
- * @contributor Dennis Ploetner <[email protected] >
6
- * @since 0.9.9
7
- */
8
2
9
3
namespace lloc \Msls ;
10
4
5
+ use lloc \Msls \Query \TranslatedPostsQuery ;
6
+
11
7
/**
12
8
* Adding custom filter to posts/pages table.
9
+ *
13
10
* @package Msls
14
11
*/
15
12
class MslsCustomFilter extends MslsMain {
@@ -22,15 +19,15 @@ class MslsCustomFilter extends MslsMain {
22
19
* @return MslsCustomFilter
23
20
*/
24
21
public static function init () {
25
- $ options = msls_options ();
22
+ $ options = msls_options ();
26
23
$ collection = msls_blog_collection ();
27
- $ obj = new static ( $ options , $ collection );
24
+ $ obj = new static ( $ options , $ collection );
28
25
29
26
if ( ! $ options ->is_excluded () ) {
30
27
$ post_type = MslsPostType::instance ()->get_request ();
31
28
if ( ! empty ( $ post_type ) ) {
32
- add_action ( 'restrict_manage_posts ' , [ $ obj , 'add_filter ' ] );
33
- add_filter ( 'parse_query ' , [ $ obj , 'execute_filter ' ] );
29
+ add_action ( 'restrict_manage_posts ' , array ( $ obj , 'add_filter ' ) );
30
+ add_filter ( 'parse_query ' , array ( $ obj , 'execute_filter ' ) );
34
31
}
35
32
}
36
33
@@ -39,11 +36,12 @@ public static function init() {
39
36
40
37
/**
41
38
* Echo's select tag with list of blogs
39
+ *
42
40
* @uses selected
43
41
*/
44
42
public function add_filter (): void {
45
43
$ id = (
46
- filter_has_var ( INPUT_GET , 'msls_filter ' ) ?
44
+ filter_has_var ( INPUT_GET , 'msls_filter ' ) ?
47
45
filter_input ( INPUT_GET , 'msls_filter ' , FILTER_SANITIZE_NUMBER_INT ) :
48
46
''
49
47
);
@@ -57,8 +55,10 @@ public function add_filter(): void {
57
55
'<option value="%d" %s>%s</option> ' ,
58
56
$ blog ->userblog_id ,
59
57
selected ( $ id , $ blog ->userblog_id , false ),
60
- sprintf ( __ ( 'Not translated in the %s-blog ' , 'multisite-language-switcher ' ),
61
- $ blog ->get_description () )
58
+ sprintf (
59
+ __ ( 'Not translated in the %s-blog ' , 'multisite-language-switcher ' ),
60
+ $ blog ->get_description ()
61
+ )
62
62
);
63
63
}
64
64
echo '</select> ' ;
@@ -80,21 +80,14 @@ public function execute_filter( \WP_Query $query ) {
80
80
}
81
81
82
82
$ id = filter_input ( INPUT_GET , 'msls_filter ' , FILTER_SANITIZE_NUMBER_INT );
83
-
84
83
if ( isset ( $ blogs [ $ id ] ) ) {
85
- $ cache = MslsSqlCacher::init ( __CLASS__ )-> set_params ( __METHOD__ );
84
+ $ sql_cache = MslsSqlCacher::create ( __CLASS__ , __METHOD__ );
86
85
87
- // load post we need to exclude (already have translation) from search query
88
- $ posts = $ cache ->get_results (
89
- $ cache ->prepare (
90
- "SELECT option_id, option_name FROM {$ cache ->options } WHERE option_name LIKE %s AND option_value LIKE %s " ,
91
- 'msls_% ' ,
92
- '%" ' . $ blogs [ $ id ]->get_language () . '"% '
93
- )
94
- );
86
+ // 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 () );
95
88
96
- $ exclude_ids = [] ;
97
- foreach ( $ posts as $ post ) {
89
+ $ exclude_ids = array () ;
90
+ foreach ( $ translated_posts as $ post ) {
98
91
$ exclude_ids [] = substr ( $ post ->option_name , 5 );
99
92
}
100
93
$ query ->query_vars ['post__not_in ' ] = $ exclude_ids ;
@@ -104,5 +97,4 @@ public function execute_filter( \WP_Query $query ) {
104
97
105
98
return false ;
106
99
}
107
-
108
100
}
0 commit comments