-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathMslsCustomFilter.php
More file actions
90 lines (75 loc) · 2.32 KB
/
MslsCustomFilter.php
File metadata and controls
90 lines (75 loc) · 2.32 KB
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
<?php declare( strict_types=1 );
namespace lloc\Msls;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
use lloc\Msls\Component\Input\Select;
use lloc\Msls\Query\TranslatedPostIdQuery;
/**
* Adding custom filter to posts/pages table.
*
* @package Msls
*/
final class MslsCustomFilter extends MslsMain {
/**
* @codeCoverageIgnore
*/
public static function init(): void {
$options = msls_options();
$collection = msls_blog_collection();
$obj = new self( $options, $collection );
if ( ! $options->is_excluded() ) {
$post_type = msls_post_type()->get_request();
if ( ! empty( $post_type ) ) {
add_action( 'restrict_manage_posts', array( $obj, 'add_filter' ) );
add_filter( 'parse_query', array( $obj, 'execute_filter' ) );
add_filter(
Select::RENDER_FILTER,
function () {
return MslsFields::FIELD_MSLS_FILTER;
}
);
}
}
}
/**
* Echo's select tag with list of blogs
*
* @uses selected
*/
public function add_filter(): void {
$blogs = $this->collection->get();
if ( $blogs ) {
$options = array( '' => esc_html( __( 'Show all posts', 'multisite-language-switcher' ) ) );
foreach ( $blogs as $blog ) {
/* translators: %s: blog name */
$format = __( 'Not translated in the %s-blog', 'multisite-language-switcher' );
$options[ strval( $blog->userblog_id ) ] = sprintf( $format, $blog->get_description() );
}
$id = MslsRequest::get( MslsFields::FIELD_MSLS_FILTER, 0 );
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo ( new Select( MslsFields::FIELD_MSLS_FILTER, $options, strval( $id ) ) )->render();
}
}
/**
* Executes filter, excludes translated posts from WP_Query
*
* @param \WP_Query $query
*
* @return bool|\WP_Query
*/
public function execute_filter( \WP_Query $query ) {
if ( ! MslsRequest::has_var( MslsFields::FIELD_MSLS_FILTER ) ) {
return false;
}
$id = MslsRequest::get_var( MslsFields::FIELD_MSLS_FILTER );
$blog = $this->collection->get_object( intval( $id ) );
if ( ! $blog ) {
return false;
}
$sql_cache = MslsSqlCacher::create( __CLASS__, __METHOD__ );
// Load post we need to exclude (they already have a translation) from search query.
$query->query_vars['post__not_in'] = ( new TranslatedPostIdQuery( $sql_cache ) )( $blog->get_language() );
return $query;
}
}