-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathMslsCustomFilter.php
100 lines (85 loc) · 2.37 KB
/
MslsCustomFilter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
namespace lloc\Msls;
use lloc\Msls\Query\TranslatedPostsQuery;
/**
* Adding custom filter to posts/pages table.
*
* @package Msls
*/
class MslsCustomFilter extends MslsMain {
/**
* Init
*
* @codeCoverageIgnore
*
* @return MslsCustomFilter
*/
public static function init() {
$options = msls_options();
$collection = msls_blog_collection();
$obj = new static( $options, $collection );
if ( ! $options->is_excluded() ) {
$post_type = MslsPostType::instance()->get_request();
if ( ! empty( $post_type ) ) {
add_action( 'restrict_manage_posts', array( $obj, 'add_filter' ) );
add_filter( 'parse_query', array( $obj, 'execute_filter' ) );
}
}
return $obj;
}
/**
* Echo's select tag with list of blogs
*
* @uses selected
*/
public function add_filter(): void {
$id = (
filter_has_var( INPUT_GET, 'msls_filter' ) ?
filter_input( INPUT_GET, 'msls_filter', FILTER_SANITIZE_NUMBER_INT ) :
''
);
$blogs = $this->collection->get();
if ( $blogs ) {
echo '<select name="msls_filter" id="msls_filter">';
echo '<option value="">' . esc_html( __( 'Show all blogs', 'multisite-language-switcher' ) ) . '</option>';
foreach ( $blogs as $blog ) {
printf(
'<option value="%d" %s>%s</option>',
$blog->userblog_id,
selected( $id, $blog->userblog_id, false ),
sprintf(
__( 'Not translated in the %s-blog', 'multisite-language-switcher' ),
$blog->get_description()
)
);
}
echo '</select>';
}
}
/**
* Executes filter, excludes translated posts from WP_Query
*
* @param \WP_Query $query
*
* @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 ] ) ) {
$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() );
$exclude_ids = array();
foreach ( $translated_posts as $post ) {
$exclude_ids[] = substr( $post->option_name, 5 );
}
$query->query_vars['post__not_in'] = $exclude_ids;
return $query;
}
return false;
}
}