|
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
|
|
|
|
|
9
|
|
|
namespace lloc\Msls; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Adding custom filter to posts/pages table. |
|
13
|
|
|
* @package Msls |
|
14
|
|
|
*/ |
|
15
|
|
|
class MslsCustomFilter extends MslsMain { |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Init |
|
19
|
|
|
* |
|
20
|
|
|
* @codeCoverageIgnore |
|
21
|
|
|
* |
|
22
|
|
|
* @return MslsCustomFilter |
|
23
|
|
|
*/ |
|
24
|
|
|
public static function init() { |
|
25
|
|
|
$options = MslsOptions::instance(); |
|
26
|
|
|
$collection = MslsBlogCollection::instance(); |
|
27
|
|
|
$obj = new static( $options, $collection ); |
|
28
|
|
|
|
|
29
|
|
|
if ( ! $options->is_excluded() ) { |
|
30
|
|
|
$post_type = MslsPostType::instance()->get_request(); |
|
31
|
|
|
if ( ! empty( $post_type ) ) { |
|
32
|
|
|
add_action( 'restrict_manage_posts', [ $obj, 'add_filter' ] ); |
|
33
|
|
|
add_filter( 'parse_query', [ $obj, 'execute_filter' ] ); |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
return $obj; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Echo's select tag with list of blogs |
|
42
|
|
|
* @uses selected |
|
43
|
|
|
*/ |
|
44
|
|
|
public function add_filter() { |
|
45
|
|
|
$id = ( |
|
46
|
|
|
filter_has_var( INPUT_GET, 'msls_filter' ) ? |
|
47
|
|
|
filter_input( INPUT_GET, 'msls_filter', FILTER_SANITIZE_NUMBER_INT ) : |
|
48
|
|
|
'' |
|
49
|
|
|
); |
|
50
|
|
|
|
|
51
|
|
|
$blogs = $this->collection->get(); |
|
52
|
|
|
if ( $blogs ) { |
|
|
|
|
|
|
53
|
|
|
echo '<select name="msls_filter" id="msls_filter">'; |
|
54
|
|
|
echo '<option value="">' . esc_html( __( 'Show all blogs', 'multisite-language-switcher' ) ) . '</option>'; |
|
55
|
|
|
foreach ( $blogs as $blog ) { |
|
56
|
|
|
printf( |
|
57
|
|
|
'<option value="%d" %s>%s</option>', |
|
58
|
|
|
$blog->userblog_id, |
|
59
|
|
|
selected( $id, $blog->userblog_id, false ), |
|
60
|
|
|
sprintf( __( 'Not translated in the %s-blog', 'multisite-language-switcher' ), $blog->get_description() ) |
|
61
|
|
|
); |
|
62
|
|
|
} |
|
63
|
|
|
echo '</select>'; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Executes filter, excludes translated posts from WP_Query |
|
69
|
|
|
* |
|
70
|
|
|
* @param \WP_Query $query |
|
71
|
|
|
* |
|
72
|
|
|
* @return bool|\WP_Query |
|
73
|
|
|
*/ |
|
74
|
|
|
public function execute_filter( \WP_Query $query ) { |
|
75
|
|
|
$blogs = $this->collection->get(); |
|
76
|
|
|
|
|
77
|
|
|
if ( ! filter_has_var( INPUT_GET, 'msls_filter' ) ) { |
|
78
|
|
|
return false; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$id = filter_input( INPUT_GET, 'msls_filter', FILTER_SANITIZE_NUMBER_INT ); |
|
82
|
|
|
|
|
83
|
|
|
if ( isset( $blogs[ $id ] ) ) { |
|
84
|
|
|
$cache = MslsSqlCacher::init( __CLASS__ )->set_params( __METHOD__ ); |
|
85
|
|
|
|
|
86
|
|
|
// load post we need to exclude (already have translation) from search query |
|
87
|
|
|
$posts = $cache->get_results( |
|
88
|
|
|
$cache->prepare( |
|
89
|
|
|
"SELECT option_id, option_name FROM {$cache->options} WHERE option_name LIKE %s AND option_value LIKE %s", |
|
90
|
|
|
'msls_%', |
|
91
|
|
|
'%"' . $blogs[ $id ]->get_language() . '"%' |
|
92
|
|
|
) |
|
93
|
|
|
); |
|
94
|
|
|
|
|
95
|
|
|
$exclude_ids = []; |
|
96
|
|
|
foreach ( $posts as $post ) { |
|
97
|
|
|
$exclude_ids[] = substr( $post->option_name, 5 ); |
|
98
|
|
|
} |
|
99
|
|
|
$query->query_vars['post__not_in'] = $exclude_ids; |
|
100
|
|
|
|
|
101
|
|
|
return $query; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return false; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.