Skip to content

Commit 5076629

Browse files
authored
Settings (#7)
1 parent 7db5be2 commit 5076629

File tree

5 files changed

+61
-12
lines changed

5 files changed

+61
-12
lines changed

inc/helpers.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace WPT\Helpers;
4+
5+
function setting(string $key, $default = false)
6+
{
7+
return \get_option("wpt_{$key}", $default);
8+
}

inc/notices.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44

55
use WPT\Enums\NoticeType;
66

7+
use function WPT\Helpers\setting;
8+
79
function adminNotices()
810
{
911
return [
1012
NoticeType::Test->value => [
11-
'enabled' => \get_option('wpt_notice_for_'.NoticeType::Test->value, false),
13+
'enabled' => setting('notice_for_'.NoticeType::Test->value, false),
1214
'type' => 'info',
1315
'text' => __('This is a test notice.', 'wp-plugin-template'),
1416
],

inc/options.php

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
<?php defined('ABSPATH') or exit; ?>
1+
<?php
2+
3+
use function WPT\Helpers\setting;
4+
5+
defined('ABSPATH') or exit;
6+
7+
?>
28

39
<div class="wrap">
410
<h1>
@@ -20,14 +26,14 @@
2026
</th>
2127
<td>
2228
<label>
23-
<input type="radio" name="wpt_cron_enabled" value="0" <?php checked(0, esc_attr(get_option('wpt_cron_enabled', 0))); ?>>
29+
<input type="radio" name="wpt_cron_enabled" value="0" <?php checked(0, esc_attr(setting('cron_enabled'))); ?>>
2430
<span>
2531
No
2632
</span>
2733
</label>
2834
<br><br>
2935
<label>
30-
<input type="radio" name="wpt_cron_enabled" value="1" <?php checked(1, esc_attr(get_option('wpt_cron_enabled', 0))); ?>>
36+
<input type="radio" name="wpt_cron_enabled" value="1" <?php checked(1, esc_attr(setting('cron_enabled'))); ?>>
3137
<span>
3238
Yes
3339
</span>
@@ -40,7 +46,7 @@
4046
<?php _e('Text', 'wp-plugin-template'); ?>
4147
</th>
4248
<td>
43-
<input type="text" name="wpt_text_option" value="<?php echo esc_attr(get_option('wpt_text_option')); ?>">
49+
<input type="text" name="wpt_text_option" value="<?php echo esc_attr(setting('text_option')); ?>">
4450
</td>
4551
</tr>
4652

@@ -50,14 +56,14 @@
5056
</th>
5157
<td>
5258
<label>
53-
<input type="radio" name="wpt_radio_option" value="0" <?php checked(0, esc_attr(get_option('wpt_radio_option', 0))); ?>>
59+
<input type="radio" name="wpt_radio_option" value="0" <?php checked(0, esc_attr(setting('radio_option', 0))); ?>>
5460
<span>
5561
No
5662
</span>
5763
</label>
5864
<br><br>
5965
<label>
60-
<input type="radio" name="wpt_radio_option" value="1" <?php checked(1, esc_attr(get_option('wpt_radio_option', 0))); ?>>
66+
<input type="radio" name="wpt_radio_option" value="1" <?php checked(1, esc_attr(setting('radio_option', 0))); ?>>
6167
<span>
6268
Yes
6369
</span>
@@ -71,16 +77,45 @@
7177
</th>
7278
<td>
7379
<select name="wpt_select_option">
74-
<option value="1" <?php selected(get_option('wpt_select_option'), 1); ?>>1</option>
75-
<option value="10" <?php selected(get_option('wpt_select_option'), 10); ?>>10</option>
76-
<option value="25" <?php selected(get_option('wpt_select_option'), 25); ?>>25</option>
80+
<option value="1" <?php selected(setting('select_option'), 1); ?>>1</option>
81+
<option value="10" <?php selected(setting('select_option'), 10); ?>>10</option>
82+
<option value="25" <?php selected(setting('select_option'), 25); ?>>25</option>
7783
</select>
7884
</td>
7985
</tr>
8086
</table>
8187

8288
<hr>
8389

90+
<h2>
91+
<?php _e('Cron Jobs', 'wp-plugin-template'); ?>
92+
</h2>
93+
94+
<table class="form-table">
95+
<tr valign="top">
96+
<th scope="row">
97+
<?php _e('Test', 'wp-plugin-template'); ?>
98+
</th>
99+
<td>
100+
<label>
101+
<input type="radio" name="wpt_test_cron_enabled" value="0" <?php checked(0, esc_attr(setting('test_cron_enabled'))); ?>>
102+
<span>
103+
No
104+
</span>
105+
</label>
106+
<br><br>
107+
<label>
108+
<input type="radio" name="wpt_test_cron_enabled" value="1" <?php checked(1, esc_attr(setting('test_cron_enabled'))); ?>>
109+
<span>
110+
Yes
111+
</span>
112+
</label>
113+
</td>
114+
</tr>
115+
</table>
116+
117+
<hr>
118+
84119
<?php submit_button(); ?>
85120
</form>
86121
</div>

src/Plugin.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use WPT\Concerns\Instanceable;
77
use WPT\Crons\CronJob;
88

9+
use function WPT\Helpers\setting;
10+
911
class Plugin
1012
{
1113
use Instanceable;
@@ -39,12 +41,13 @@ public function options(): void
3941
\register_setting('wpt-options', 'wpt_text_option');
4042
\register_setting('wpt-options', 'wpt_radio_option');
4143
\register_setting('wpt-options', 'wpt_select_option');
44+
\register_setting('wpt-options', 'wpt_test_cron_enabled');
4245
}, 10);
4346
}
4447

4548
public function cronSchedules(): void
4649
{
47-
add_filter('cron_schedules', function (array $schedules): array {
50+
\add_filter('cron_schedules', function (array $schedules): array {
4851
\collect(Helpers::wpCronIntervals())->each(function (array $interval) use (&$schedules) {
4952
$schedules[$interval['slug']] = [
5053
'interval' => $interval['value'] ?? '',
@@ -67,7 +70,7 @@ public function run(): void
6770
$this->options();
6871
$this->cronSchedules();
6972

70-
if (! \get_option('wpt_cron_enabled', 0)) {
73+
if (! setting('cron_enabled') || ! setting('test_cron_enabled')) {
7174
$this->cronJob()->unscheduleCron();
7275
} else {
7376
$this->cronJob()->scheduleCron();

wordpress-plugin-template.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
require_once WPT_DIR_PATH.'/vendor/autoload.php';
2323

24+
require_once WPT_DIR_PATH.'/inc/helpers.php';
2425
require_once WPT_DIR_PATH.'/inc/notices.php';
2526

2627
function wptPlugin()

0 commit comments

Comments
 (0)