Skip to content

Commit b82a886

Browse files
authored
Add new date fields (#70)
* Add new date and datetime fields. * PHP8+ code cleanup. * Code cleanup * Remove
1 parent 85f6a14 commit b82a886

3 files changed

Lines changed: 85 additions & 85 deletions

File tree

examples/Example.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class ExampleSettings implements WpHooksInterface
2323
* `WpSettingsApi::ACTION_PREFIX . 'init'`. This custom action passes three parameters (two prior to version 2.7)
2424
* so you have to register a priority and the parameter count.
2525
*/
26-
public function addHooks()
26+
public function addHooks(): void
2727
{
2828
\add_action(WpSettingsApi::ACTION_PREFIX . 'init', [$this, 'init'], 10, 3);
2929
}

src/Api/PluginSettings.php

Lines changed: 30 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?php declare(strict_types=1);
1+
<?php
2+
3+
declare(strict_types=1);
24

35
namespace Dwnload\WpSettingsApi\Api;
46

@@ -13,146 +15,101 @@ class PluginSettings extends BaseModel
1315
{
1416

1517
/** @var string $nonce_s */
16-
protected $nonce_s = WpSettingsApi::FILTER_PREFIX . '%s';
18+
protected string $nonce_s = WpSettingsApi::FILTER_PREFIX . '%s';
1719

1820
/** @var string $domain */
19-
private $domain;
21+
private string $domain;
2022

2123
/** @var string $file */
22-
private $file;
24+
private string $file;
2325

2426
/** @var string $menu_slug */
25-
private $menu_slug;
27+
private string $menu_slug;
2628

2729
/** @var string $menu_title */
28-
private $menu_title;
30+
private string $menu_title;
2931

3032
/** @var string $page_title */
31-
private $page_title;
33+
private string $page_title;
3234

3335
/** @var string $prefix */
34-
private $prefix;
36+
private string $prefix;
3537

3638
/** @var string $version */
37-
private $version;
39+
private string $version;
3840

39-
/**
40-
* @param string $domain
41-
*/
42-
public function setDomain(string $domain)
43-
{
44-
$this->domain = $domain;
45-
}
46-
47-
/**
48-
* @return string
49-
*/
5041
public function getDomain(): string
5142
{
5243
return $this->domain;
5344
}
5445

55-
/**
56-
* @param string $file
57-
*/
58-
public function setFile(string $file)
46+
protected function setDomain(string $domain)
5947
{
60-
$this->file = $file;
48+
$this->domain = $domain;
6149
}
6250

63-
/**
64-
* @return string
65-
*/
6651
public function getFile(): string
6752
{
6853
return $this->file;
6954
}
7055

71-
/**
72-
* @return string
73-
*/
74-
public function getNonce(): string
56+
protected function setFile(string $file)
7557
{
76-
return \sprintf($this->nonce_s, \plugin_basename($this->getFile()));
58+
$this->file = $file;
7759
}
7860

79-
/**
80-
* @param string $slug
81-
*/
82-
public function setMenuSlug(string $slug)
61+
public function getNonce(): string
8362
{
84-
$this->menu_slug = $slug;
63+
return \sprintf($this->nonce_s, \plugin_basename($this->getFile()));
8564
}
8665

87-
/**
88-
* @return string
89-
*/
9066
public function getMenuSlug(): string
9167
{
9268
return $this->menu_slug ?? $this->domain;
9369
}
9470

95-
/**
96-
* @param string $title
97-
*/
98-
public function setMenuTitle(string $title)
71+
protected function setMenuSlug(string $slug)
9972
{
100-
$this->menu_title = $title;
73+
$this->menu_slug = $slug;
10174
}
10275

103-
/**
104-
* @return string
105-
*/
10676
public function getMenuTitle(): string
10777
{
10878
return $this->menu_title;
10979
}
11080

111-
/**
112-
* @param string $title
113-
*/
114-
public function setPageTitle(string $title)
81+
protected function setMenuTitle(string $title)
11582
{
116-
$this->page_title = $title;
83+
$this->menu_title = $title;
11784
}
11885

119-
/**
120-
* @return string
121-
*/
12286
public function getPageTitle(): string
12387
{
12488
return $this->page_title;
12589
}
12690

127-
/**
128-
* @param string $prefix
129-
*/
130-
public function setPrefix(string $prefix)
91+
protected function setPageTitle(string $title)
13192
{
132-
$this->prefix = $prefix;
93+
$this->page_title = $title;
13394
}
13495

135-
/**
136-
* @return string
137-
*/
13896
public function getPrefix(): string
13997
{
14098
return $this->prefix;
14199
}
142100

143-
/**
144-
* @param string $version
145-
*/
146-
public function setVersion(string $version)
101+
protected function setPrefix(string $prefix)
147102
{
148-
$this->version = $version;
103+
$this->prefix = $prefix;
149104
}
150105

151-
/**
152-
* @return string
153-
*/
154106
public function getVersion(): string
155107
{
156108
return $this->version;
157109
}
110+
111+
protected function setVersion(string $version)
112+
{
113+
$this->version = $version;
114+
}
158115
}

src/Settings/FieldTypes.php

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?php declare(strict_types=1);
1+
<?php
2+
3+
declare(strict_types=1);
24

35
namespace Dwnload\WpSettingsApi\Settings;
46

@@ -24,6 +26,8 @@ class FieldTypes
2426

2527
public const FIELD_TYPE_TEXT = 'text';
2628
public const FIELD_TYPE_URL = 'url';
29+
public const FIELD_TYPE_DATE = 'date';
30+
public const FIELD_TYPE_DATETIME = 'datetime';
2731
public const FIELD_TYPE_EMAIL = 'email';
2832
public const FIELD_TYPE_COLOR = 'color';
2933
public const FIELD_TYPE_COLOR_ALPHA = 'coloralpha';
@@ -104,6 +108,40 @@ public function url(array $args): void
104108
$this->text($args);
105109
}
106110

111+
/**
112+
* Renders an input date field.
113+
* @param array $args Array of Field object parameters
114+
*/
115+
public function date(array $args): void
116+
{
117+
$field = $this->getSettingFieldObject($args);
118+
$args[SettingField::TYPE] = FieldTypes::FIELD_TYPE_DATE;
119+
$field->setAttributes(
120+
array_merge(
121+
$field->getAttributes(),
122+
['pattern' => '\d{4}-\d{2}-\d{2}']
123+
)
124+
);
125+
$this->text($args);
126+
}
127+
128+
/**
129+
* Renders an input datetime-local field.
130+
* @param array $args Array of Field object parameters
131+
*/
132+
public function datetime(array $args): void
133+
{
134+
$field = $this->getSettingFieldObject($args);
135+
$args[SettingField::TYPE] = sprintf('%s-local', FieldTypes::FIELD_TYPE_DATETIME);
136+
$field->setAttributes(
137+
array_merge(
138+
$field->getAttributes(),
139+
['pattern' => '[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}']
140+
)
141+
);
142+
$this->text($args);
143+
}
144+
107145
/**
108146
* Renders an input email field.
109147
* @param array $args Array of Field object parameters
@@ -115,16 +153,18 @@ public function email(array $args): void
115153
}
116154

117155
/**
118-
* Renders a ninput color field.
156+
* Renders an input color field.
119157
* @param array $args Array of Field object parameters
120158
*/
121159
public function color(array $args): void
122160
{
123161
$field = $this->getSettingFieldObject($args);
124-
$field->setAttributes(array_merge(
125-
$field->getAttributes(),
126-
['class' => ['color-picker']]
127-
));
162+
$field->setAttributes(
163+
array_merge(
164+
$field->getAttributes(),
165+
['class' => ['color-picker']]
166+
)
167+
);
128168
$field->setType(FieldTypes::FIELD_TYPE_TEXT);
129169
$this->text($args);
130170
}
@@ -136,10 +176,12 @@ public function color(array $args): void
136176
public function coloralpha(array $args): void
137177
{
138178
$field = $this->getSettingFieldObject($args);
139-
$field->setAttributes(array_merge(
140-
$field->getAttributes(),
141-
['class' => ['color-picker'], 'data-alpha-enabled' => 'true']
142-
));
179+
$field->setAttributes(
180+
array_merge(
181+
$field->getAttributes(),
182+
['class' => ['color-picker'], 'data-alpha-enabled' => 'true']
183+
)
184+
);
143185
$field->setType(FieldTypes::FIELD_TYPE_TEXT);
144186
$this->text($args);
145187
}
@@ -467,7 +509,8 @@ protected function getInputField(array $args): string
467509
$field->getId(),
468510
esc_attr($value),
469511
$this->getExtraFieldParams($args),
470-
implode(' ',
512+
implode(
513+
' ',
471514
array_map('\sanitize_html_class', $field->getAttributes()['class'] ?? [])
472515
)
473516
);

0 commit comments

Comments
 (0)