Skip to content

Commit c2a63ce

Browse files
authored
Merge pull request #313 from lloc/refactoring-version-2-8
Tests added
2 parents 3a00556 + 4574b26 commit c2a63ce

11 files changed

+249
-106
lines changed

includes/MslsMetaBox.php

Lines changed: 51 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
/**
44
* MslsMetaBox
5+
*
56
* @author Dennis Ploetner <re@lloc.de>
67
* @since 0.9.8
78
*/
@@ -12,6 +13,7 @@
1213

1314
/**
1415
* Meta box for the edit mode of the (custom) post types
16+
*
1517
* @package Msls
1618
*/
1719
class MslsMetaBox extends MslsMain {
@@ -23,17 +25,17 @@ class MslsMetaBox extends MslsMain {
2325
* the requested search-term and then die silently
2426
*/
2527
public static function suggest() {
26-
$json = new MslsJson;
28+
$json = new MslsJson();
2729

2830
if ( filter_has_var( INPUT_POST, 'blog_id' ) ) {
2931
switch_to_blog(
3032
filter_input( INPUT_POST, 'blog_id', FILTER_SANITIZE_NUMBER_INT )
3133
);
3234

33-
$args = [
34-
'post_status' => get_post_stati( [ 'internal' => '' ] ),
35+
$args = array(
36+
'post_status' => get_post_stati( array( 'internal' => '' ) ),
3537
'posts_per_page' => 10,
36-
];
38+
);
3739

3840
if ( filter_has_var( INPUT_POST, 'post_type' ) ) {
3941
$args['post_type'] = sanitize_text_field(
@@ -55,7 +57,7 @@ public static function suggest() {
5557

5658
/**
5759
* @param MslsJson $json
58-
* @param array $args
60+
* @param array $args
5961
*
6062
* @return mixed
6163
*/
@@ -66,25 +68,21 @@ public static function get_suggested_fields( $json, $args ) {
6668
* @param array $args
6769
*
6870
* @since 0.9.9
69-
*
7071
*/
7172
$args = (array) apply_filters( 'msls_meta_box_suggest_args', $args );
7273

73-
$my_query = new \WP_Query( $args );
74-
while ( $my_query->have_posts() ) {
75-
$my_query->the_post();
76-
74+
$posts = get_posts( $args );
75+
foreach ( $posts as $post ) {
7776
/**
7877
* Manipulates the WP_Post object before using it
7978
*
8079
* @param \WP_Post $post
8180
*
8281
* @since 0.9.9
83-
*
8482
*/
85-
$my_query->post = apply_filters( 'msls_meta_box_suggest_post', $my_query->post );
86-
if ( is_object( $my_query->post ) ) {
87-
$json->add( get_the_ID(), get_the_title() );
83+
$post = apply_filters( 'msls_meta_box_suggest_post', $post );
84+
if ( is_object( $post ) ) {
85+
$json->add( $post->ID, get_the_title( $post ) );
8886
}
8987
}
9088

@@ -106,9 +104,9 @@ public static function init() {
106104
$obj = new static( $options, msls_blog_collection() );
107105

108106
if ( ! $options->is_excluded() ) {
109-
add_action( 'add_meta_boxes', [ $obj, 'add' ] );
110-
add_action( 'save_post', [ $obj, 'set' ] );
111-
add_action( 'trashed_post', [ $obj, 'delete' ] );
107+
add_action( 'add_meta_boxes', array( $obj, 'add' ) );
108+
add_action( 'save_post', array( $obj, 'set' ) );
109+
add_action( 'trashed_post', array( $obj, 'delete' ) );
112110
}
113111

114112
return $obj;
@@ -125,14 +123,14 @@ public function add() {
125123
'msls_metabox_post_select_title',
126124
__( 'Multisite Language Switcher', 'multisite-language-switcher' )
127125
),
128-
[
126+
array(
129127
$this,
130128
(
131129
msls_options()->activate_autocomplete ?
132130
'render_input' :
133131
'render_select'
134132
),
135-
],
133+
),
136134
$post_type,
137135
'side',
138136
'high'
@@ -145,21 +143,22 @@ public function add() {
145143
'msls_metabox_post_import_title',
146144
__( 'Multisite Language Switcher - Import content', 'multisite-language-switcher' )
147145
),
148-
[
146+
array(
149147
ContentImportMetaBox::instance(),
150148
'render',
151-
],
149+
),
152150
$post_type,
153151
'side',
154152
'high'
155153
);
156-
add_action( 'admin_footer', [ ContentImportMetaBox::instance(), 'print_modal_html' ] );
154+
add_action( 'admin_footer', array( ContentImportMetaBox::instance(), 'print_modal_html' ) );
157155
}
158156
}
159157
}
160158

161159
/**
162160
* Render the classic select-box
161+
*
163162
* @uses selected
164163
*/
165164
public function render_select() {
@@ -183,7 +182,7 @@ public function render_select() {
183182

184183
$language = $blog->get_language();
185184
$iconType = MslsAdminIcon::TYPE_FLAG === $this->options->admin_display ? MslsAdminIcon::TYPE_FLAG : MslsAdminIcon::TYPE_LABEL;
186-
$icon = MslsAdminIcon::create($type)->set_language( $language )->set_icon_type( $iconType );
185+
$icon = MslsAdminIcon::create( $type )->set_language( $language )->set_icon_type( $iconType );
187186

188187
if ( $mydata->has_value( $language ) ) {
189188
$icon->set_href( $mydata->$language );
@@ -193,23 +192,22 @@ public function render_select() {
193192
$p_object = get_post_type_object( $type );
194193

195194
if ( $p_object->hierarchical ) {
196-
$args = [
195+
$args = array(
197196
'post_type' => $type,
198197
'selected' => $mydata->$language,
199198
'name' => 'msls_input_' . $language,
200199
'show_option_none' => ' ',
201200
'option_none_value' => 0,
202201
'sort_column' => 'menu_order, post_title',
203202
'echo' => 0,
204-
];
203+
);
205204

206205
/**
207206
* Overrides the args for wp_dropdown_pages when using the HTML select in the MetaBox
208207
*
209208
* @param array $args
210209
*
211210
* @since 1.0.5
212-
*
213211
*/
214212
$args = (array) apply_filters( 'msls_meta_box_render_select_hierarchical', $args );
215213

@@ -239,8 +237,10 @@ public function render_select() {
239237
} else {
240238
printf(
241239
'<p>%s</p>',
242-
__( 'You should define at least another blog in a different language in order to have some benefit from this plugin!',
243-
'multisite-language-switcher' )
240+
__(
241+
'You should define at least another blog in a different language in order to have some benefit from this plugin!',
242+
'multisite-language-switcher'
243+
)
244244
);
245245
}
246246
}
@@ -252,21 +252,20 @@ public function render_select() {
252252
* @return string
253253
*/
254254
public function render_options( $type, $msls_id ): string {
255-
$options = [];
256-
257-
$my_query = new \WP_Query( [
258-
'post_type' => $type,
259-
'post_status' => get_post_stati( [ 'internal' => '' ] ),
260-
'orderby' => 'title',
261-
'order' => 'ASC',
262-
'posts_per_page' => - 1,
263-
'fields' => 'ids',
264-
] );
265-
266-
if ( $my_query->have_posts() ) {
267-
foreach ( $my_query->posts as $post_id ) {
268-
$options[] = $this->render_option( $post_id, $msls_id );
269-
}
255+
$options = array();
256+
257+
$posts = get_posts(
258+
array(
259+
'post_type' => $type,
260+
'post_status' => get_post_stati( array( 'internal' => '' ) ),
261+
'orderby' => 'title',
262+
'order' => 'ASC',
263+
'posts_per_page' => - 1,
264+
)
265+
);
266+
267+
foreach ( $posts as $post ) {
268+
$options[] = $this->render_option( $post->ID, $msls_id );
270269
}
271270

272271
return implode( PHP_EOL, $options );
@@ -279,10 +278,12 @@ public function render_options( $type, $msls_id ): string {
279278
* @return string
280279
*/
281280
public function render_option( $post_id, $msls_id ) {
282-
return sprintf( '<option value="%s" %s>%s</option>',
281+
return sprintf(
282+
'<option value="%s" %s>%s</option>',
283283
$post_id,
284284
selected( $post_id, $msls_id, false ),
285-
get_the_title( $post_id ) );
285+
get_the_title( $post_id )
286+
);
286287
}
287288

288289
/**
@@ -311,7 +312,7 @@ public function render_input( $echo = true ) {
311312

312313
$language = $blog->get_language();
313314
$icon = MslsAdminIcon::create()
314-
->set_language( $language );
315+
->set_language( $language );
315316

316317
if ( $this->options->admin_display === 'label' ) {
317318
$icon->set_icon_type( 'label' );
@@ -356,8 +357,10 @@ public function render_input( $echo = true ) {
356357
} else {
357358
printf(
358359
'<p>%s</p>',
359-
__( 'You should define at least another blog in a different language in order to have some benefit from this plugin!',
360-
'multisite-language-switcher' )
360+
__(
361+
'You should define at least another blog in a different language in order to have some benefit from this plugin!',
362+
'multisite-language-switcher'
363+
)
361364
);
362365
}
363366
}

includes/MslsOptions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ public function is_excluded() {
296296
*
297297
* @return bool
298298
*/
299-
public function is_content_filter() {
299+
public function is_content_filter(): bool {
300300
return isset( $this->content_filter );
301301
}
302302

includes/MslsPlugin.php

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace lloc\Msls;
44

5+
use lloc\Msls\Query\BlogsInNetworkQuery;
6+
57
/**
68
* Provides functionalities for general hooks and activation/deactivation
79
*
@@ -164,13 +166,9 @@ public static function print_alternate_links() {
164166
*
165167
* @return string
166168
*/
167-
public function content_filter( $content ) {
168-
if ( ! is_front_page() && is_singular() ) {
169-
$options = $this->options;
170-
171-
if ( $options->is_content_filter() ) {
172-
$content .= $this->filter_string();
173-
}
169+
public function content_filter( string $content ) {
170+
if ( ! is_front_page() && is_singular() && $this->options->is_content_filter() ) {
171+
$content .= $this->filter_string();
174172
}
175173

176174
return $content;
@@ -413,15 +411,8 @@ public static function uninstall() {
413411
* restore_current_blog
414412
*/
415413
if ( function_exists( 'is_multisite' ) && is_multisite() ) {
416-
$cache = MslsSqlCacher::create( __CLASS__, __METHOD__ );
417-
418-
$blogs = $cache->get_results(
419-
$cache->prepare(
420-
"SELECT blog_id FROM {$cache->blogs} WHERE blog_id != %d AND site_id = %d",
421-
$cache->blogid,
422-
$cache->siteid
423-
)
424-
);
414+
$sql_cache = MslsSqlCacher::create( __CLASS__, __METHOD__ );
415+
$blogs = ( new BlogsInNetworkQuery( $sql_cache ) )();
425416

426417
foreach ( $blogs as $blog ) {
427418
switch_to_blog( $blog->blog_id );

includes/MslsPostTag.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public function edit_input( \WP_Term $tag, string $taxonomy ): void {
145145
/**
146146
* Print the input fields
147147
*
148-
* Returns true if the blogcollection is not empty
148+
* Returns true if the blog collection is not empty
149149
*
150150
* @param ?\WP_Term $tag
151151
* @param string $title_format

includes/MslsPostTagClassic.php

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
<?php
2-
/**
3-
* MslsPostTagClassic
4-
* @author Dennis Ploetner <re@lloc.de>
5-
* @since 0.9.9
6-
*/
72

83
namespace lloc\Msls;
94

@@ -37,7 +32,7 @@ public function add_input( string $taxonomy ): void {
3732
* Add the input fields to the edit-screen of the taxonomies
3833
*
3934
* @param \WP_Term $tag
40-
* @param string $taxonomy
35+
* @param string $taxonomy
4136
*/
4237
public function edit_input( \WP_Term $tag, string $taxonomy ): void {
4338
$title_format = '<tr>
@@ -64,8 +59,8 @@ public function edit_input( \WP_Term $tag, string $taxonomy ): void {
6459
* Returns true if the blogcollection is not empty
6560
*
6661
* @param ?\WP_Term $tag
67-
* @param string $title_format
68-
* @param string $item_format
62+
* @param string $title_format
63+
* @param string $item_format
6964
*
7065
* @return boolean
7166
*/
@@ -76,7 +71,7 @@ public function the_input( ?\WP_Term $tag, string $title_format, string $item_fo
7671
return false;
7772
}
7873

79-
$count ++;
74+
++$count;
8075

8176
$blogs = $this->collection->get();
8277
if ( ! empty( $blogs ) ) {
@@ -101,10 +96,10 @@ public function the_input( ?\WP_Term $tag, string $title_format, string $item_fo
10196
/**
10297
* Prints options inputs
10398
*
104-
* @param MslsBlog $blog
105-
* @param string $type
99+
* @param MslsBlog $blog
100+
* @param string $type
106101
* @param MslsOptionsTax $mydata
107-
* @param string $item_format
102+
* @param string $item_format
108103
*/
109104
public function print_option( MslsBlog $blog, string $type, MslsOptionsTax $mydata, string $item_format ): void {
110105
switch_to_blog( $blog->userblog_id );
@@ -118,15 +113,19 @@ public function print_option( MslsBlog $blog, string $type, MslsOptionsTax $myda
118113
}
119114

120115
$options = '';
121-
$terms = get_terms( [ 'taxonomy' => $type, 'hide_empty' => false ] );
116+
$terms = get_terms(
117+
array(
118+
'taxonomy' => $type,
119+
'hide_empty' => false,
120+
)
121+
);
122122
if ( ! empty( $terms ) ) {
123123
foreach ( $terms as $term ) {
124124
$options .= sprintf(
125125
'<option value="%s" %s>%s</option>',
126126
$term->term_id,
127127
selected( $term->term_id, $mydata->$language, false ),
128128
$term->name
129-
130129
);
131130
}
132131
}
@@ -135,5 +134,4 @@ public function print_option( MslsBlog $blog, string $type, MslsOptionsTax $myda
135134

136135
restore_current_blog();
137136
}
138-
139137
}

0 commit comments

Comments
 (0)