11<?php
2- /**
3- * MslsCustomFilter
4- * @author Maciej Czerpiński <[email protected] > 5- * @contributor Dennis Ploetner <[email protected] > 6- * @since 0.9.9
7- */
82
93namespace lloc \Msls ;
104
5+ use lloc \Msls \Query \TranslatedPostsQuery ;
6+
117/**
128 * Adding custom filter to posts/pages table.
9+ *
1310 * @package Msls
1411 */
1512class MslsCustomFilter extends MslsMain {
@@ -22,15 +19,15 @@ class MslsCustomFilter extends MslsMain {
2219 * @return MslsCustomFilter
2320 */
2421 public static function init () {
25- $ options = msls_options ();
22+ $ options = msls_options ();
2623 $ collection = msls_blog_collection ();
27- $ obj = new static ( $ options , $ collection );
24+ $ obj = new static ( $ options , $ collection );
2825
2926 if ( ! $ options ->is_excluded () ) {
3027 $ post_type = MslsPostType::instance ()->get_request ();
3128 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 ' ) );
3431 }
3532 }
3633
@@ -39,11 +36,12 @@ public static function init() {
3936
4037 /**
4138 * Echo's select tag with list of blogs
39+ *
4240 * @uses selected
4341 */
4442 public function add_filter (): void {
4543 $ id = (
46- filter_has_var ( INPUT_GET , 'msls_filter ' ) ?
44+ filter_has_var ( INPUT_GET , 'msls_filter ' ) ?
4745 filter_input ( INPUT_GET , 'msls_filter ' , FILTER_SANITIZE_NUMBER_INT ) :
4846 ''
4947 );
@@ -57,8 +55,10 @@ public function add_filter(): void {
5755 '<option value="%d" %s>%s</option> ' ,
5856 $ blog ->userblog_id ,
5957 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+ )
6262 );
6363 }
6464 echo '</select> ' ;
@@ -80,21 +80,14 @@ public function execute_filter( \WP_Query $query ) {
8080 }
8181
8282 $ id = filter_input ( INPUT_GET , 'msls_filter ' , FILTER_SANITIZE_NUMBER_INT );
83-
8483 if ( isset ( $ blogs [ $ id ] ) ) {
85- $ cache = MslsSqlCacher::init ( __CLASS__ )-> set_params ( __METHOD__ );
84+ $ sql_cache = MslsSqlCacher::create ( __CLASS__ , __METHOD__ );
8685
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 () );
9588
96- $ exclude_ids = [] ;
97- foreach ( $ posts as $ post ) {
89+ $ exclude_ids = array () ;
90+ foreach ( $ translated_posts as $ post ) {
9891 $ exclude_ids [] = substr ( $ post ->option_name , 5 );
9992 }
10093 $ query ->query_vars ['post__not_in ' ] = $ exclude_ids ;
@@ -104,5 +97,4 @@ public function execute_filter( \WP_Query $query ) {
10497
10598 return false ;
10699 }
107-
108100}
0 commit comments