Skip to content

Commit d7f1d9c

Browse files
authored
Merge pull request #306 from lloc/refactoring-version-2-8
Refactoring version 2 8
2 parents 2a1d0cd + a2d95e4 commit d7f1d9c

24 files changed

+483
-253
lines changed

includes/MslsCustomFilter.php

+18-26
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
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

93
namespace lloc\Msls;
104

5+
use lloc\Msls\Query\TranslatedPostsQuery;
6+
117
/**
128
* Adding custom filter to posts/pages table.
9+
*
1310
* @package Msls
1411
*/
1512
class 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
}

includes/MslsGetSet.php

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
/**
33
* MslsGetSet
4+
*
45
* @author Dennis Ploetner <[email protected]>
56
* @since 0.9.8
67
*/
@@ -18,15 +19,16 @@ class MslsGetSet extends MslsRegistryInstance {
1819

1920
/**
2021
* Generic container for all properties of an instance
22+
*
2123
* @var array $arr
2224
*/
23-
protected $arr = [];
25+
protected $arr = array();
2426

2527
/**
2628
* Overloads the set method.
2729
*
2830
* @param string $key
29-
* @param mixed $value
31+
* @param mixed $value
3032
*/
3133
public function __set( $key, $value ) {
3234
$this->arr[ $key ] = $value;
@@ -75,7 +77,7 @@ public function __unset( $key ) {
7577
* @return MslsGetSet
7678
*/
7779
public function reset() {
78-
$this->arr = [];
80+
$this->arr = array();
7981

8082
return $this;
8183
}
@@ -98,7 +100,7 @@ public function reset() {
98100
*
99101
* @return bool
100102
*/
101-
public function has_value( $key ) {
103+
public function has_value( string $key ): bool {
102104
return ! empty( $this->arr[ $key ] );
103105
}
104106

@@ -119,5 +121,4 @@ public function is_empty() {
119121
public function get_arr() {
120122
return $this->arr;
121123
}
122-
123124
}

includes/MslsOptionsQuery.php

+30-24
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
<?php
2-
/**
3-
* MslsOptionsQuery
4-
*
5-
* @author Dennis Ploetner <[email protected]>
6-
* @since 0.9.8
7-
*/
82

93
namespace lloc\Msls;
104

115
/**
12-
* OptionsQuery
6+
* MslsOptionsQuery
137
*
148
* @package Msls
159
*/
@@ -22,36 +16,48 @@ class MslsOptionsQuery extends MslsOptions {
2216
*/
2317
public $with_front = true;
2418

19+
protected MslsSqlCacher $sql_cache;
20+
21+
public function __construct( MslsSqlCacher $sql_cache ) {
22+
parent::__construct();
23+
24+
$this->sql_cache = $sql_cache;
25+
}
26+
27+
/**
28+
* @return array<string, mixed>
29+
*/
30+
public static function get_params(): array {
31+
return array();
32+
}
33+
2534
/**
2635
* Factory method
2736
*
2837
* @param int $id This parameter is unused here
2938
*
30-
* @return MslsOptionsQuery|null
39+
* @return ?MslsOptionsQuery
3140
*/
32-
public static function create( $id = 0 ) {
33-
$query = null;
34-
41+
public static function create( $id = 0 ): ?MslsOptionsQuery {
3542
if ( is_day() ) {
36-
$query = new MslsOptionsQueryDay(
37-
get_query_var( 'year' ),
38-
get_query_var( 'monthnum' ),
39-
get_query_var( 'day' )
40-
);
43+
$query_class = MslsOptionsQueryDay::class;
4144
} elseif ( is_month() ) {
42-
$query = new MslsOptionsQueryMonth(
43-
get_query_var( 'year' ),
44-
get_query_var( 'monthnum' )
45-
);
45+
$query_class = MslsOptionsQueryMonth::class;
4646
} elseif ( is_year() ) {
47-
$query = new MslsOptionsQueryYear( get_query_var( 'year' ) );
47+
$query_class = MslsOptionsQueryYear::class;
4848
} elseif ( is_author() ) {
49-
$query = new MslsOptionsQueryAuthor( get_queried_object_id() );
49+
$query_class = MslsOptionsQueryAuthor::class;
5050
} elseif ( is_post_type_archive() ) {
51-
$query = new MslsOptionsQueryPostType( get_query_var( 'post_type' ) );
51+
$query_class = MslsOptionsQueryPostType::class;
52+
}
53+
54+
if ( ! isset( $query_class ) ) {
55+
return null;
5256
}
5357

54-
return $query;
58+
$sql_cache = MslsSqlCacher::create( $query_class, $query_class::get_params() );
59+
60+
return new $query_class( $sql_cache );
5561
}
5662

5763
/**

includes/MslsOptionsQueryAuthor.php

+23-17
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,43 @@
11
<?php
2-
/**
3-
* MslsOptionsQueryAuthor
4-
*
5-
* @author Dennis Ploetner <[email protected]>
6-
* @since 0.9.8
7-
*/
82

93
namespace lloc\Msls;
104

5+
use lloc\Msls\Query\AuthorPostsCounterQuery;
6+
117
/**
12-
* OptionsQueryAuthor
8+
* MslsOptionsQueryAuthor
139
*
1410
* @package Msls
1511
*/
1612
class MslsOptionsQueryAuthor extends MslsOptionsQuery {
1713

14+
protected int $author_id;
15+
16+
public function __construct( MslsSqlCacher $sql_cache ) {
17+
parent::__construct( $sql_cache );
18+
19+
$this->author_id = self::get_params()['author_id'];
20+
}
21+
22+
/**
23+
* @return array<string, mixed>
24+
*/
25+
public static function get_params(): array {
26+
return array(
27+
'author_id' => get_queried_object_id(),
28+
);
29+
}
30+
1831
/**
1932
* Check if the array has a non-empty item which has $language as a key
2033
*
2134
* @param string $language
2235
*
2336
* @return bool
2437
*/
25-
public function has_value( $language ) {
38+
public function has_value( string $language ): bool {
2639
if ( ! isset( $this->arr[ $language ] ) ) {
27-
$cache = MslsSqlCacher::init( __CLASS__ )->set_params( $this->args );
28-
29-
$this->arr[ $language ] = $cache->get_var(
30-
$cache->prepare(
31-
"SELECT count(ID) FROM {$cache->posts} WHERE post_author = %d AND post_status = 'publish'",
32-
$this->get_arg( 0, 0 )
33-
)
34-
);
40+
$this->arr[ $language ] = ( new AuthorPostsCounterQuery( $this->sql_cache ) )( $this->author_id );
3541
}
3642

3743
return (bool) $this->arr[ $language ];
@@ -43,6 +49,6 @@ public function has_value( $language ) {
4349
* @return string
4450
*/
4551
public function get_current_link() {
46-
return get_author_posts_url( $this->get_arg( 0, 0 ) );
52+
return get_author_posts_url( $this->author_id );
4753
}
4854
}

includes/MslsOptionsQueryDay.php

+32-21
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,51 @@
11
<?php
2-
/**
3-
* MslsOptionsQueryDay
4-
* @author Dennis Ploetner <[email protected]>
5-
* @since 0.9.8
6-
*/
72

83
namespace lloc\Msls;
94

5+
use lloc\Msls\Query\DatePostsCounterQuery;
6+
107
/**
11-
* OptionsQueryDay
8+
* MslsOptionsQueryDay
129
*
1310
* @package Msls
1411
*/
1512
class MslsOptionsQueryDay extends MslsOptionsQuery {
1613

14+
protected int $year;
15+
16+
protected int $monthnum;
17+
protected int $day;
18+
19+
public function __construct( MslsSqlCacher $sql_cache ) {
20+
parent::__construct( $sql_cache );
21+
22+
$params = self::get_params();
23+
24+
$this->year = $params['year'];
25+
$this->monthnum = $params['monthnum'];
26+
$this->day = $params['day'];
27+
}
28+
29+
public static function get_params(): array {
30+
return array(
31+
'year' => get_query_var( 'year' ),
32+
'monthnum' => get_query_var( 'monthnum' ),
33+
'day' => get_query_var( 'day' ),
34+
);
35+
}
36+
1737
/**
18-
* Check if the array has an non empty item which has $language as a key
38+
* Check if the array has a non-empty item which has $language as a key
1939
*
2040
* @param string $language
2141
*
2242
* @return bool
2343
*/
24-
public function has_value( $language ) {
44+
public function has_value( string $language ): bool {
2545
if ( ! isset( $this->arr[ $language ] ) ) {
26-
$date = new \DateTime();
27-
$cache = MslsSqlCacher::init( __CLASS__ )->set_params( $this->args );
28-
29-
$this->arr[ $language ] = $cache->get_var(
30-
$cache->prepare(
31-
"SELECT count(ID) FROM {$cache->posts} WHERE DATE(post_date) = %s AND post_status = 'publish'",
32-
$date->setDate( $this->get_arg( 0, 0 ),
33-
$this->get_arg( 1, 0 ),
34-
$this->get_arg( 2, 0 ) )->format( 'Y-m-d' )
35-
)
36-
);
46+
$query_callable = new DatePostsCounterQuery( $this->sql_cache );
47+
48+
$this->arr[ $language ] = $query_callable( $this->year, $this->monthnum, $this->day );
3749
}
3850

3951
return (bool) $this->arr[ $language ];
@@ -45,7 +57,6 @@ public function has_value( $language ) {
4557
* @return string
4658
*/
4759
public function get_current_link() {
48-
return get_day_link( $this->get_arg( 0, 0 ), $this->get_arg( 1, 0 ), $this->get_arg( 2, 0 ) );
60+
return get_day_link( $this->year, $this->monthnum, $this->day );
4961
}
50-
5162
}

0 commit comments

Comments
 (0)