Skip to content

Commit e234503

Browse files
committed
Refactoring
1 parent d2097f3 commit e234503

26 files changed

+261
-160
lines changed

MultisiteLanguageSwitcher.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,33 @@ function msls_options(): \lloc\Msls\MslsOptions {
152152
return \lloc\Msls\MslsOptions::instance();
153153
}
154154

155+
/**
156+
* Gets the MslsContentTypes instance
157+
*
158+
* @return \lloc\Msls\MslsContentTypes
159+
*/
160+
function msls_content_types(): \lloc\Msls\MslsContentTypes {
161+
return \lloc\Msls\MslsContentTypes::create();
162+
}
163+
164+
/**
165+
* Gets the MslsPostType instance
166+
*
167+
* @return \lloc\Msls\MslsPostType
168+
*/
169+
function msls_post_type(): \lloc\Msls\MslsPostType {
170+
return \lloc\Msls\MslsPostType::instance();
171+
}
172+
173+
/**
174+
* Gets the MslsTaxonomy instance
175+
*
176+
* @return \lloc\Msls\MslsTaxonomy
177+
*/
178+
function msls_taxonomy(): \lloc\Msls\MslsTaxonomy {
179+
return \lloc\Msls\MslsTaxonomy::instance();
180+
}
181+
155182
/**
156183
* Gets the MslsOutput instance
157184
*

includes/Component/Input/Option.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
/**
88
* Class Option
9+
*
910
* @package lloc\Msls\Component\Input
1011
*/
1112
class Option implements InputInterface {
@@ -26,21 +27,25 @@ class Option implements InputInterface {
2627
protected $selected;
2728

2829
/**
29-
* @param string $key
30-
* @param string|null $value
31-
* @param string|null $selected
30+
* @param string $key
31+
* @param string $value
32+
* @param ?string $selected
3233
*/
33-
public function __construct( string $key, $value, $selected = null ) {
34-
$this->key = esc_attr( $key );
35-
$this->value = esc_attr( $value );
36-
$this->selected = selected( $key, esc_attr( $selected ), false );
34+
public function __construct( string $key, string $value, ?string $selected = null ) {
35+
$this->key = $key;
36+
$this->value = $value;
37+
$this->selected = selected( $key, $selected, false );
3738
}
3839

3940
/**
4041
* @return string
4142
*/
4243
public function render(): string {
43-
return sprintf( '<option value="%1$s" %2$s>%3$s</option>', $this->key, $this->selected, $this->value );
44+
return sprintf(
45+
'<option value="%1$s" %2$s>%3$s</option>',
46+
esc_attr( $this->key ),
47+
esc_attr( $this->selected ),
48+
esc_html( $this->value )
49+
);
4450
}
45-
46-
}
51+
}

includes/Component/Input/Select.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@ class Select implements InputInterface {
1919
protected $options;
2020

2121
/**
22-
* @param string $key Name and ID of the form-element
23-
* @param string[] $arr Options as associative array
24-
* @param ?string $selected Values which should be selected
22+
* @param string $key Name and ID of the form-element
23+
* @param mixed[] $arr Options as associative array
24+
* @param ?string $selected Values which should be selected
2525
*/
2626
public function __construct( string $key, array $arr, ?string $selected = null ) {
27-
$this->key = esc_attr( $key );
27+
$this->key = $key;
2828

2929
$this->options = new Group( '' );
3030
foreach ( $arr as $key => $value ) {
31-
$this->options->add( new Option( $key, $value, $selected ) );
31+
$this->options->add( new Option( $key, strval( $value ), $selected ) );
3232
}
3333
}
3434

@@ -38,6 +38,11 @@ public function __construct( string $key, array $arr, ?string $selected = null )
3838
public function render(): string {
3939
$name = apply_filters( self::RENDER_FILTER, 'msls[' . $this->key . ']' );
4040

41-
return sprintf( '<select id="%1$s" name="%2$s">%3$s</select>', $this->key, esc_attr( $name ), $this->options->render() );
41+
return sprintf(
42+
'<select id="%1$s" name="%2$s">%3$s</select>',
43+
esc_attr( $this->key ),
44+
esc_attr( $name ),
45+
$this->options->render()
46+
);
4247
}
4348
}

includes/Component/Input/Text.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33

44
namespace lloc\Msls\Component\Input;
55

6-
76
use lloc\Msls\Component\InputInterface;
87

98
class Text implements InputInterface {
109

10+
const DEFAULT_SIZE = 30;
11+
1112
/**
1213
* @var string
1314
*/
@@ -19,7 +20,7 @@ class Text implements InputInterface {
1920
protected $value;
2021

2122
/**
22-
* @var string
23+
* @var int
2324
*/
2425
protected $size;
2526

@@ -29,15 +30,15 @@ class Text implements InputInterface {
2930
protected $readonly;
3031

3132
/**
32-
* @param string $key
33+
* @param string $key
3334
* @param string|null $value
34-
* @param string $size
35-
* @param bool $readonly
35+
* @param int $size
36+
* @param bool $readonly
3637
*/
37-
public function __construct( string $key, $value, string $size = '30', bool $readonly = false ) {
38-
$this->key = esc_attr( $key );
39-
$this->value = esc_attr( $value );
40-
$this->size = esc_attr( $size );
38+
public function __construct( string $key, ?string $value, int $size = self::DEFAULT_SIZE, bool $readonly = false ) {
39+
$this->key = $key;
40+
$this->value = $value;
41+
$this->size = $size;
4142
$this->readonly = $readonly ? ' readonly="readonly"' : '';
4243
}
4344

@@ -46,12 +47,11 @@ public function __construct( string $key, $value, string $size = '30', bool $rea
4647
*/
4748
public function render(): string {
4849
return sprintf(
49-
'<input type="text" class="regular-text" id="%1$s" name="msls[%1$s]" value="%2$s" size="%3$s"%4$s/>',
50-
$this->key,
51-
$this->value,
50+
'<input type="text" class="regular-text" id="%1$s" name="msls[%1$s]" value="%2$s" size="%3$d"%4$s/>',
51+
esc_attr( $this->key ),
52+
esc_attr( $this->value ),
5253
$this->size,
53-
$this->readonly
54+
esc_attr( $this->readonly )
5455
);
5556
}
56-
57-
}
57+
}

includes/Map/HrefLang.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
class HrefLang {
1313

1414
/**
15-
* @var array<string, <array<string>>>
15+
* @var array<string, array<int, string>>
1616
*/
1717
protected $map = array();
1818

includes/MslsAdmin.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ public function subsubsub(): string {
204204
*
205205
* @codeCoverageIgnore
206206
*/
207-
public function register() {
207+
public function register(): void {
208208
register_setting( 'msls', 'msls', array( $this, 'validate' ) );
209209

210210
$sections = array(
@@ -310,8 +310,8 @@ public function rewrites_section(): int {
310310
}
311311

312312
/**
313-
* @param array $map
314-
* @param string $section
313+
* @param array<string, string> $map
314+
* @param string $section
315315
*
316316
* @return int
317317
*/
@@ -336,7 +336,7 @@ protected function add_settings_fields( array $map, string $section ): int {
336336
/**
337337
* Shows the select-form-field 'blog_language'
338338
*/
339-
public function blog_language() {
339+
public function blog_language(): void {
340340
$languages = $this->options->get_available_languages();
341341
$selected = get_locale();
342342

@@ -346,14 +346,14 @@ public function blog_language() {
346346
/**
347347
* Shows the select-form-field 'display'
348348
*/
349-
public function display() {
350-
echo ( new Select( 'display', MslsLink::get_types_description(), $this->options->display ) )->render();
349+
public function display(): void {
350+
echo ( new Select( 'display', MslsLink::get_types_description(), strval( $this->options->display ) ) )->render();
351351
}
352352

353353
/**
354354
* Shows the select-form-field 'admin_display'
355355
*/
356-
public function admin_display() {
356+
public function admin_display(): void {
357357
echo ( new Select(
358358
'admin_display',
359359
array(
@@ -367,7 +367,7 @@ public function admin_display() {
367367
/**
368368
* Shows the select-form-field 'reference_user'
369369
*/
370-
public function reference_user() {
370+
public function reference_user(): void {
371371
$users = array();
372372

373373
foreach ( (array) apply_filters( 'msls_reference_users', $this->collection->get_users() ) as $user ) {
@@ -386,16 +386,16 @@ public function reference_user() {
386386
trigger_error( sprintf( $format, self::MAX_REFERENCE_USERS ) );
387387
}
388388

389-
echo ( new Select( 'reference_user', $users, $this->options->reference_user ) )->render();
389+
echo ( new Select( 'reference_user', $users, strval( $this->options->reference_user ) ) )->render();
390390
}
391391

392392
/**
393393
* The description for the current blog
394394
*
395395
* The language will be used ff there is no description.
396396
*/
397-
public function description() {
398-
echo ( new Text( 'description', $this->options->description, '40' ) )->render();
397+
public function description(): void {
398+
echo ( new Text( 'description', $this->options->description, 40 ) )->render();
399399
}
400400

401401
/**
@@ -405,20 +405,20 @@ public function description() {
405405
* trouble. So you can decide a higher (from 1) or a lower (to 100) priority
406406
* for the output
407407
*/
408-
public function content_priority() {
408+
public function content_priority(): void {
409409
$temp = array_merge( range( 1, 10 ), array( 20, 50, 100 ) );
410410
$arr = array_combine( $temp, $temp );
411411
$selected = empty( $this->options->content_priority ) ? 10 : $this->options->content_priority;
412412

413-
echo ( new Select( 'content_priority', $arr, $selected ) )->render();
413+
echo ( new Select( 'content_priority', $arr, strval( $selected ) ) )->render();
414414
}
415415

416416
/**
417417
* Rewrites slugs for registered post types
418418
*
419-
* @param string $key
419+
* @param mixed $key
420420
*/
421-
public function render_rewrite( $key ) {
421+
public function render_rewrite( $key ): void {
422422
$rewrite = get_post_type_object( $key )->rewrite;
423423

424424
$value = '';
@@ -434,9 +434,9 @@ public function render_rewrite( $key ) {
434434
/**
435435
* Validates input before saving it
436436
*
437-
* @param array $arr Values of the submitted form
437+
* @param array<string, mixed> $arr Values of the submitted form
438438
*
439-
* @return array Validated input
439+
* @return array<string, mixed>
440440
*/
441441
public function validate( array $arr ) {
442442
/**
@@ -459,9 +459,9 @@ public function validate( array $arr ) {
459459
/**
460460
* Filter which sets the global blog language
461461
*
462-
* @param array $arr
462+
* @param string[] $arr
463463
*
464-
* @return array
464+
* @return string[]
465465
*/
466466
public function set_blog_language( array $arr ) {
467467
if ( isset( $arr['blog_language'] ) ) {

includes/MslsAdminIcon.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function __toString() {
8888
* @return MslsAdminIcon|MslsAdminIconTaxonomy
8989
*/
9090
public static function create( ?string $type = null ) {
91-
$obj = MslsContentTypes::create();
91+
$obj = msls_content_types();
9292

9393
if ( ! $type ) {
9494
$type = $obj->get_request();

includes/MslsCustomColumn.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ protected function add_hooks(): void {
2525
return;
2626
}
2727

28-
$post_type = MslsPostType::instance()->get_request();
28+
$post_type = msls_post_type()->get_request();
2929

3030
if ( ! empty( $post_type ) ) {
3131
add_filter( "manage_{$post_type}_posts_columns", array( $this, 'th' ) );

includes/MslsCustomFilter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public static function init(): void {
2121
$obj = new self( $options, $collection );
2222

2323
if ( ! $options->is_excluded() ) {
24-
$post_type = MslsPostType::instance()->get_request();
24+
$post_type = msls_post_type()->get_request();
2525
if ( ! empty( $post_type ) ) {
2626
add_action( 'restrict_manage_posts', array( $obj, 'add_filter' ) );
2727
add_filter( 'parse_query', array( $obj, 'execute_filter' ) );

includes/MslsMetaBox.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public static function init(): void {
107107
* Adds the meta box to the post types
108108
*/
109109
public function add(): void {
110-
foreach ( MslsPostType::instance()->get() as $post_type ) {
110+
foreach ( msls_post_type()->get() as $post_type ) {
111111

112112
add_meta_box(
113113
'msls',

0 commit comments

Comments
 (0)