Skip to content

Commit e047a6c

Browse files
committed
get_superglobals refactored as get_request
1 parent 16a927d commit e047a6c

7 files changed

+83
-35
lines changed

includes/MslsContentTypes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ abstract class MslsContentTypes extends MslsRegistryInstance {
3131
* @return MslsContentTypes
3232
*/
3333
public static function create() {
34-
$_request = MslsPlugin::get_superglobals( array( 'taxonomy' ) );
34+
$_request = MslsRequest::get_request( array( 'taxonomy' ) );
3535

3636
return '' != $_request['taxonomy'] ? MslsTaxonomy::instance() : MslsPostType::instance();
3737
}

includes/MslsFields.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class MslsFields {
66

77
const FIELD_BLOG_ID = 'blog_id';
88
const FIELD_POST_TYPE = 'post_type';
9+
const FIELD_TAXONOMY = 'taxonomy';
910
const FIELD_S = 's';
1011
const FIELD_ACTION = 'action';
1112
const FIELD_MSLS_FILTER = 'msls_filter';
@@ -22,6 +23,10 @@ class MslsFields {
2223
INPUT_POST,
2324
FILTER_SANITIZE_FULL_SPECIAL_CHARS,
2425
),
26+
self::FIELD_TAXONOMY => array(
27+
INPUT_POST,
28+
FILTER_SANITIZE_FULL_SPECIAL_CHARS,
29+
),
2530
self::FIELD_S => array(
2631
INPUT_POST,
2732
FILTER_SANITIZE_FULL_SPECIAL_CHARS,

includes/MslsPlugin.php

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -442,28 +442,4 @@ public static function cleanup() {
442442

443443
return false;
444444
}
445-
446-
/**
447-
* Get specific vars from $_POST and $_GET in a safe way
448-
*
449-
* @param array $list
450-
* @param string $default
451-
*
452-
* @return array
453-
*/
454-
public static function get_superglobals( array $list, $default = '' ): array {
455-
$arr = array();
456-
457-
foreach ( $list as $var ) {
458-
$arr[ $var ] = $default;
459-
460-
if ( filter_has_var( INPUT_POST, $var ) ) {
461-
$arr[ $var ] = filter_input( INPUT_POST, $var );
462-
} elseif ( filter_has_var( INPUT_GET, $var ) ) {
463-
$arr[ $var ] = filter_input( INPUT_GET, $var );
464-
}
465-
}
466-
467-
return $arr;
468-
}
469445
}

includes/MslsPostType.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
/**
33
* MslsPostType
4+
*
45
* @author Dennis Ploetner <re@lloc.de>
56
* @since 0.9.8
67
*/
@@ -9,6 +10,7 @@
910

1011
/**
1112
* Content types: Post types (Pages, Posts, ...)
13+
*
1214
* @package Msls
1315
*/
1416
class MslsPostType extends MslsContentTypes {
@@ -24,12 +26,16 @@ public function __construct() {
2426
/**
2527
* @return string[]
2628
* @uses get_post_types
27-
*
2829
*/
2930
public static function get(): array {
3031
$types = array_merge(
31-
[ 'post', 'page' ], // we don't need attachment, revision or nav_menu_item here
32-
get_post_types( [ 'public' => true, '_builtin' => false ] )
32+
array( 'post', 'page' ), // we don't need attachment, revision or nav_menu_item here
33+
get_post_types(
34+
array(
35+
'public' => true,
36+
'_builtin' => false,
37+
)
38+
)
3339
);
3440

3541
return (array) apply_filters( 'msls_supported_post_types', $types );
@@ -39,18 +45,18 @@ public static function get(): array {
3945
* @return string
4046
*/
4147
public function get_request(): string {
42-
$request = MslsPlugin::get_superglobals( [ 'post_type' ] );
48+
$request = MslsRequest::get_request( array( 'post_type' ) );
4349
$post_type = ! empty( $request['post_type'] ) ? esc_attr( $request['post_type'] ) : 'post';
4450

4551
return in_array( $post_type, $this->get() ) ? $post_type : '';
4652
}
4753

4854
/**
4955
* Check for post_type
56+
*
5057
* @return bool
5158
*/
5259
public function is_post_type() {
5360
return true;
5461
}
55-
5662
}

includes/MslsRequest.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
class MslsRequest {
66

7-
87
public static function get_config( $name ): array {
98
$config = MslsFields::CONFIG[ $name ] ?? null;
109

@@ -59,4 +58,31 @@ public static function isset( array $keys ): bool {
5958

6059
return true;
6160
}
61+
62+
/**
63+
* Gets the request values for a list of keys.
64+
*
65+
* It will treat each key as a string and will return an array with every key as index and the value as a sanitized string.
66+
*
67+
* @param string[] $keys
68+
* @param mixed $default
69+
*
70+
* @return array<string, mixed>
71+
*/
72+
public static function get_request( array $keys, $default = '' ): array {
73+
$values = array();
74+
75+
foreach ( $keys as $key ) {
76+
list( , $filter ) = self::get_config( $key );
77+
$values[ $key ] = $default;
78+
79+
if ( filter_has_var( INPUT_POST, $key ) ) {
80+
$values[ $key ] = filter_input( INPUT_POST, $key, $filter );
81+
} elseif ( filter_has_var( INPUT_GET, $key ) ) {
82+
$values[ $key ] = filter_input( INPUT_GET, $key, $filter );
83+
}
84+
}
85+
86+
return $values;
87+
}
6288
}

includes/MslsTaxonomy.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
/**
33
* MslsTaxonomy
4+
*
45
* @author Dennis Ploetner <re@lloc.de>
56
* @since 0.9.8
67
*/
@@ -16,6 +17,7 @@ class MslsTaxonomy extends MslsContentTypes {
1617

1718
/**
1819
* Post type
20+
*
1921
* @var string
2022
*/
2123
protected $post_type = '';
@@ -34,8 +36,13 @@ public function __construct() {
3436
*/
3537
public static function get(): array {
3638
$types = array_merge(
37-
[ 'category', 'post_tag' ], // no 'post_link' here
38-
get_taxonomies( [ 'public' => true, '_builtin' => false ] )
39+
array( 'category', 'post_tag' ), // no 'post_link' here
40+
get_taxonomies(
41+
array(
42+
'public' => true,
43+
'_builtin' => false,
44+
)
45+
)
3946
);
4047

4148
return (array) apply_filters( 'msls_supported_taxonomies', $types );
@@ -45,7 +52,7 @@ public static function get(): array {
4552
* @return string
4653
*/
4754
public function get_request(): string {
48-
$request = MslsPlugin::get_superglobals( [ 'taxonomy', 'post_type' ] );
55+
$request = MslsRequest::get_request( array( 'taxonomy', 'post_type' ) );
4956

5057
if ( ! empty( $request['taxonomy'] ) ) {
5158
$this->post_type = esc_attr( $request['post_type'] ?? '' );
@@ -58,6 +65,7 @@ public function get_request(): string {
5865

5966
/**
6067
* Check for taxonomy
68+
*
6169
* @return bool
6270
*/
6371
public function is_taxonomy() {
@@ -93,5 +101,4 @@ public function acl_request() {
93101
public function get_post_type() {
94102
return $this->post_type;
95103
}
96-
97104
}

tests/TestMslsRequest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,32 @@ public function test_has_var_ko(): void {
4040
public function test_get_var_ko(): void {
4141
$this->assertNull( MslsRequest::get_var( 'non_existent_key' ) );
4242
}
43+
44+
public function test_request_ok(): void {
45+
Functions\expect( 'filter_has_var' )->once()->with( INPUT_POST, MslsFields::FIELD_POST_TYPE )->andReturn( false );
46+
Functions\expect( 'filter_has_var' )->once()->with( INPUT_GET, MslsFields::FIELD_POST_TYPE )->andReturn( true );
47+
48+
Functions\expect( 'filter_input' )->once()->with( INPUT_GET, MslsFields::FIELD_POST_TYPE, FILTER_SANITIZE_FULL_SPECIAL_CHARS )->andReturn( 'book' );
49+
50+
Functions\expect( 'filter_has_var' )->once()->with( INPUT_POST, MslsFields::FIELD_TAXONOMY )->andReturn( true );
51+
Functions\expect( 'filter_input' )->once()->with( INPUT_POST, MslsFields::FIELD_TAXONOMY, FILTER_SANITIZE_FULL_SPECIAL_CHARS )->andReturn( 'fantasy' );
52+
53+
$expected = array(
54+
'post_type' => 'book',
55+
'taxonomy' => 'fantasy',
56+
);
57+
58+
$this->assertEquals( $expected, MslsRequest::get_request( array( MslsFields::FIELD_POST_TYPE, MslsFields::FIELD_TAXONOMY ) ) );
59+
}
60+
61+
public function test_request_ko(): void {
62+
Functions\expect( 'filter_has_var' )->times( 4 )->andReturn( false );
63+
64+
$expected = array(
65+
'post_type' => '',
66+
'taxonomy' => '',
67+
);
68+
69+
$this->assertEquals( $expected, MslsRequest::get_request( array( MslsFields::FIELD_POST_TYPE, MslsFields::FIELD_TAXONOMY ) ) );
70+
}
4371
}

0 commit comments

Comments
 (0)