Skip to content

Commit 7e1d343

Browse files
authored
Merge pull request #315 from lloc/refactoring-version-2-8
filter_input and filter_has refactored
2 parents 0b8c168 + 128dadf commit 7e1d343

14 files changed

+414
-160
lines changed

includes/ContentImport/MetaBox.php

Lines changed: 157 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
use lloc\Msls\ContentImport\Importers\ImportersFactory;
66
use lloc\Msls\ContentImport\Importers\Map;
77
use lloc\Msls\MslsBlogCollection;
8+
use lloc\Msls\MslsFields;
89
use lloc\Msls\MslsOptionsPost;
910
use lloc\Msls\MslsPlugin;
1011
use lloc\Msls\MslsRegistryInstance;
12+
use lloc\Msls\MslsRequest;
1113

1214
class MetaBox extends MslsRegistryInstance {
13-
protected $data = [];
15+
protected $data = array();
1416

1517
/**
1618
* Renders the content import metabox.
@@ -21,23 +23,30 @@ public function render() {
2123
$languages = MslsOptionsPost::instance()->get_available_languages();
2224
$current = MslsBlogCollection::get_blog_language( get_current_blog_id() );
2325
$languages = array_diff_key( $languages, array( $current => $current ) );
24-
$input_lang = isset( $_GET['msls_lang'] ) ? $_GET['msls_lang'] : null;
25-
$input_id = isset( $_GET['msls_id'] ) ? $_GET['msls_id'] : null;
26+
$input_lang = MslsRequest::get( MslsFields::FIELD_MSLS_LANG, null );
27+
$input_id = MslsRequest::get( MslsFields::FIELD_MSLS_ID, null );
2628
$has_input = null !== $input_lang && null !== $input_id;
2729
$blogs = msls_blog_collection();
28-
$available = array_filter( array_map( function ( $lang ) use ( $mydata ) {
29-
return $mydata->{$lang};
30-
}, array_keys( $languages ) ) );
30+
$available = array_filter(
31+
array_map(
32+
function ( $lang ) use ( $mydata ) {
33+
return $mydata->{$lang};
34+
},
35+
array_keys( $languages )
36+
)
37+
);
3138
$has_translation = count( $available ) >= 1;
3239

3340
if ( $has_input || $has_translation ) {
3441
add_thickbox();
3542
$label_template = __( 'Import content from %s', 'multisite-language-switcher' );
3643
$output = '<fieldset>';
37-
$output .= '<legend>'
38-
. esc_html__( 'Warning! This will override and replace all the post content with the content from the source post!',
39-
'multisite-language-switcher' )
40-
. '</legend>';
44+
$output .= '<legend>'
45+
. esc_html__(
46+
'Warning! This will override and replace all the post content with the content from the source post!',
47+
'multisite-language-switcher'
48+
)
49+
. '</legend>';
4150
foreach ( $languages as $language => $label ) {
4251
$id = $mydata->{$language};
4352
$blog = $blogs->get_blog_id( $language );
@@ -47,10 +56,11 @@ public function render() {
4756
$blog = $blogs->get_blog_id( $language );
4857
}
4958
if ( null !== $id ) {
50-
$this->data = [
59+
$this->data = array(
5160
'msls_import' => "{$blog}|{$id}",
52-
];
53-
$output .= sprintf( '<a class="button button-primary thickbox" href="%s" title="%s">%s</a>',
61+
);
62+
$output .= sprintf(
63+
'<a class="button button-primary thickbox" href="%s" title="%s">%s</a>',
5464
$this->inline_thickbox_url( $this->data ),
5565
$label,
5666
$label
@@ -60,21 +70,26 @@ public function render() {
6070
$output .= '</fieldset>';
6171
} else {
6272
$output = '<p>' .
63-
esc_html__( 'No translated versions linked to this post: import content functionality is disabled.',
64-
'multisite-language-switcher' )
65-
. '</p>';
73+
esc_html__(
74+
'No translated versions linked to this post: import content functionality is disabled.',
75+
'multisite-language-switcher'
76+
)
77+
. '</p>';
6678
}
6779

6880
echo $output;
6981
}
7082

71-
protected function inline_thickbox_url( array $data = [] ) {
72-
$args = array_merge( [
73-
'modal' => true,
74-
'width' => 770, // meh, just a guess on *most* devices
75-
'height' => 770,
76-
'inlineId' => 'msls-import-dialog-' . str_replace( '|', '-', $data['msls_import'] ),
77-
], $data );
83+
protected function inline_thickbox_url( array $data = array() ) {
84+
$args = array_merge(
85+
array(
86+
'modal' => true,
87+
'width' => 770, // meh, just a guess on *most* devices
88+
'height' => 770,
89+
'inlineId' => 'msls-import-dialog-' . str_replace( '|', '-', $data['msls_import'] ),
90+
),
91+
$data
92+
);
7893

7994
return esc_url(
8095
'#TB_inline' . add_query_arg( $args, '' )
@@ -85,7 +100,7 @@ public function print_modal_html() {
85100
echo $this->inline_thickbox_html( true, $this->data );
86101
}
87102

88-
protected function inline_thickbox_html( $echo = true, array $data = [] ) {
103+
protected function inline_thickbox_html( $echo = true, array $data = array() ) {
89104
if ( ! isset( $data['msls_import'] ) ) {
90105
return '';
91106
}
@@ -94,84 +109,139 @@ protected function inline_thickbox_html( $echo = true, array $data = [] ) {
94109

95110
ob_start();
96111
?>
97-
<div style="display: none;" id="msls-import-dialog-<?php
98-
echo esc_attr( $slug ) ?>">
99-
<h3><?php
100-
esc_html_e( 'Select what should be imported and how', 'multisite-language-switcher' ) ?></h3>
101-
102-
<form action="<?php
103-
echo add_query_arg( [] ) ?>" method="post">
112+
<div style="display: none;" id="msls-import-dialog-
113+
<?php
114+
echo esc_attr( $slug )
115+
?>
116+
">
117+
<h3>
118+
<?php
119+
esc_html_e( 'Select what should be imported and how', 'multisite-language-switcher' )
120+
?>
121+
</h3>
122+
123+
<form action="
124+
<?php
125+
echo add_query_arg( array() )
126+
?>
127+
" method="post">
104128

105129
<?php
106-
wp_nonce_field( MslsPlugin::path(), 'msls_noncename' ); ?>
130+
wp_nonce_field( MslsPlugin::path(), 'msls_noncename' );
131+
?>
107132

108133
<?php
109-
foreach ( $data as $key => $value ) : ?>
110-
<input type="hidden" name="<?php
111-
echo esc_attr( $key ) ?>" value="<?php
112-
echo esc_attr( $value ) ?>">
113-
<?php
114-
endforeach; ?>
134+
foreach ( $data as $key => $value ) :
135+
?>
136+
<input type="hidden" name="
137+
<?php
138+
echo esc_attr( $key )
139+
?>
140+
" value="
141+
<?php
142+
echo esc_attr( $value )
143+
?>
144+
">
145+
<?php
146+
endforeach;
147+
?>
115148

116149
<?php
117150
/** @var ImportersFactory $factory */
118-
foreach ( Map::instance()->factories() as $slug => $factory ) : ?>
151+
foreach ( Map::instance()->factories() as $slug => $factory ) :
152+
?>
119153
<?php
120-
$details = $factory->details() ?>
121-
<h4><?php
122-
echo esc_html( $details->name ) ?></h4>
154+
$details = $factory->details()
155+
?>
156+
<h4>
123157
<?php
124-
if ( empty( $details->importers ) ) : ?>
125-
<p><?php
126-
esc_html_e( 'No importers available for this type of content.',
127-
'multisite-language-switcher' ) ?></p>
158+
echo esc_html( $details->name )
159+
?>
160+
</h4>
128161
<?php
129-
else: ?>
130-
<ul>
131-
<li>
132-
<label>
133-
<input type="radio" name="msls_importers[<?php
134-
echo esc_attr( $details->slug ) ?>]">
162+
if ( empty( $details->importers ) ) :
163+
?>
164+
<p>
165+
<?php
166+
esc_html_e(
167+
'No importers available for this type of content.',
168+
'multisite-language-switcher'
169+
)
170+
?>
171+
</p>
172+
<?php
173+
else :
174+
?>
175+
<ul>
176+
<li>
177+
<label>
178+
<input type="radio" name="msls_importers[
135179
<?php
136-
esc_html_e( 'Off - Do not import this type of content in the destination post.',
137-
'multisite-language-switcher' ) ?>
138-
</label>
139-
</li>
180+
echo esc_attr( $details->slug )
181+
?>
182+
]">
183+
<?php
184+
esc_html_e(
185+
'Off - Do not import this type of content in the destination post.',
186+
'multisite-language-switcher'
187+
)
188+
?>
189+
</label>
190+
</li>
140191
<?php
141-
foreach ( $details->importers as $importer_slug => $importer_info ) : ?>
142-
<li>
143-
<label>
144-
<input type="radio" name="msls_importers[<?php
145-
echo esc_attr( $details->slug ) ?>]"
146-
value="<?php
147-
echo esc_attr( $importer_slug ) ?>"
192+
foreach ( $details->importers as $importer_slug => $importer_info ) :
193+
?>
194+
<li>
195+
<label>
196+
<input type="radio" name="msls_importers[
197+
<?php
198+
echo esc_attr( $details->slug )
199+
?>
200+
]"
201+
value="
202+
<?php
203+
echo esc_attr( $importer_slug )
204+
?>
205+
"
148206
<?php
149-
checked( $details->selected, $importer_slug ) ?>
150-
>
207+
checked( $details->selected, $importer_slug )
208+
?>
209+
>
151210
<?php
152-
echo( esc_html( sprintf( '%s - %s',
153-
$importer_info->name,
154-
$importer_info->description ) ) ) ?>
155-
</label>
156-
</li>
157-
<?php
158-
endforeach; ?>
159-
</ul>
211+
echo( esc_html(
212+
sprintf(
213+
'%s - %s',
214+
$importer_info->name,
215+
$importer_info->description
216+
)
217+
) )
218+
?>
219+
</label>
220+
</li>
221+
<?php
222+
endforeach;
223+
?>
224+
</ul>
225+
<?php
226+
endif;
227+
?>
160228
<?php
161-
endif; ?>
162-
<?php
163-
endforeach; ?>
164-
165-
<div>
166-
<input
167-
type="submit"
168-
class="button button-primary"
169-
value="<?php
170-
esc_html_e( 'Import Content', 'multisite-language-switcher' ) ?>"
171-
>
172-
</div>
173-
</form>
174-
</div>
229+
endforeach;
230+
?>
231+
232+
<div>
233+
<input
234+
type="submit"
235+
class="button button-primary"
236+
value="
237+
<?php
238+
esc_html_e( 'Import Content', 'multisite-language-switcher' )
239+
?>
240+
"
241+
>
242+
</div>
243+
</form>
244+
</div>
175245

176246
<?php
177247
$html = ob_get_clean();

includes/MslsCustomFilter.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
*/
1313
class MslsCustomFilter extends MslsMain {
1414

15-
const FILTER_NAME = 'msls_filter';
16-
1715
/**
1816
* Init
1917
*
@@ -34,7 +32,7 @@ public static function init() {
3432
add_filter(
3533
'msls_input_select_name',
3634
function () {
37-
return self::FILTER_NAME;
35+
return MslsFields::FIELD_MSLS_FILTER;
3836
}
3937
);
4038
}
@@ -59,13 +57,9 @@ public function add_filter(): void {
5957
);
6058
}
6159

62-
$id = (
63-
filter_has_var( INPUT_GET, self::FILTER_NAME ) ?
64-
filter_input( INPUT_GET, self::FILTER_NAME, FILTER_SANITIZE_NUMBER_INT ) :
65-
'0'
66-
);
60+
$id = MslsRequest::get( MslsFields::FIELD_MSLS_FILTER, 0 );
6761

68-
echo ( new Select( self::FILTER_NAME, $options, $id ) )->render();
62+
echo ( new Select( MslsFields::FIELD_MSLS_FILTER, $options, $id ) )->render();
6963
}
7064
}
7165

@@ -77,11 +71,11 @@ public function add_filter(): void {
7771
* @return bool|\WP_Query
7872
*/
7973
public function execute_filter( \WP_Query $query ) {
80-
if ( ! filter_has_var( INPUT_GET, self::FILTER_NAME ) ) {
74+
if ( ! MslsRequest::has_var( MslsFields::FIELD_MSLS_FILTER ) ) {
8175
return false;
8276
}
8377

84-
$id = filter_input( INPUT_GET, self::FILTER_NAME, FILTER_SANITIZE_NUMBER_INT );
78+
$id = MslsRequest::get_var( MslsFields::FIELD_MSLS_FILTER );
8579
$blog = $this->collection->get_object( intval( $id ) );
8680

8781
if ( ! $blog ) {

0 commit comments

Comments
 (0)