Skip to content

Commit

Permalink
Settings (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
SebKay committed Jun 25, 2024
1 parent 7db5be2 commit 5076629
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 12 deletions.
8 changes: 8 additions & 0 deletions inc/helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace WPT\Helpers;

function setting(string $key, $default = false)
{
return \get_option("wpt_{$key}", $default);
}
4 changes: 3 additions & 1 deletion inc/notices.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

use WPT\Enums\NoticeType;

use function WPT\Helpers\setting;

function adminNotices()
{
return [
NoticeType::Test->value => [
'enabled' => \get_option('wpt_notice_for_'.NoticeType::Test->value, false),
'enabled' => setting('notice_for_'.NoticeType::Test->value, false),
'type' => 'info',
'text' => __('This is a test notice.', 'wp-plugin-template'),
],
Expand Down
53 changes: 44 additions & 9 deletions inc/options.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
<?php defined('ABSPATH') or exit; ?>
<?php

use function WPT\Helpers\setting;

defined('ABSPATH') or exit;

?>

<div class="wrap">
<h1>
Expand All @@ -20,14 +26,14 @@
</th>
<td>
<label>
<input type="radio" name="wpt_cron_enabled" value="0" <?php checked(0, esc_attr(get_option('wpt_cron_enabled', 0))); ?>>
<input type="radio" name="wpt_cron_enabled" value="0" <?php checked(0, esc_attr(setting('cron_enabled'))); ?>>
<span>
No
</span>
</label>
<br><br>
<label>
<input type="radio" name="wpt_cron_enabled" value="1" <?php checked(1, esc_attr(get_option('wpt_cron_enabled', 0))); ?>>
<input type="radio" name="wpt_cron_enabled" value="1" <?php checked(1, esc_attr(setting('cron_enabled'))); ?>>
<span>
Yes
</span>
Expand All @@ -40,7 +46,7 @@
<?php _e('Text', 'wp-plugin-template'); ?>
</th>
<td>
<input type="text" name="wpt_text_option" value="<?php echo esc_attr(get_option('wpt_text_option')); ?>">
<input type="text" name="wpt_text_option" value="<?php echo esc_attr(setting('text_option')); ?>">
</td>
</tr>

Expand All @@ -50,14 +56,14 @@
</th>
<td>
<label>
<input type="radio" name="wpt_radio_option" value="0" <?php checked(0, esc_attr(get_option('wpt_radio_option', 0))); ?>>
<input type="radio" name="wpt_radio_option" value="0" <?php checked(0, esc_attr(setting('radio_option', 0))); ?>>
<span>
No
</span>
</label>
<br><br>
<label>
<input type="radio" name="wpt_radio_option" value="1" <?php checked(1, esc_attr(get_option('wpt_radio_option', 0))); ?>>
<input type="radio" name="wpt_radio_option" value="1" <?php checked(1, esc_attr(setting('radio_option', 0))); ?>>
<span>
Yes
</span>
Expand All @@ -71,16 +77,45 @@
</th>
<td>
<select name="wpt_select_option">
<option value="1" <?php selected(get_option('wpt_select_option'), 1); ?>>1</option>
<option value="10" <?php selected(get_option('wpt_select_option'), 10); ?>>10</option>
<option value="25" <?php selected(get_option('wpt_select_option'), 25); ?>>25</option>
<option value="1" <?php selected(setting('select_option'), 1); ?>>1</option>
<option value="10" <?php selected(setting('select_option'), 10); ?>>10</option>
<option value="25" <?php selected(setting('select_option'), 25); ?>>25</option>
</select>
</td>
</tr>
</table>

<hr>

<h2>
<?php _e('Cron Jobs', 'wp-plugin-template'); ?>
</h2>

<table class="form-table">
<tr valign="top">
<th scope="row">
<?php _e('Test', 'wp-plugin-template'); ?>
</th>
<td>
<label>
<input type="radio" name="wpt_test_cron_enabled" value="0" <?php checked(0, esc_attr(setting('test_cron_enabled'))); ?>>
<span>
No
</span>
</label>
<br><br>
<label>
<input type="radio" name="wpt_test_cron_enabled" value="1" <?php checked(1, esc_attr(setting('test_cron_enabled'))); ?>>
<span>
Yes
</span>
</label>
</td>
</tr>
</table>

<hr>

<?php submit_button(); ?>
</form>
</div>
7 changes: 5 additions & 2 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use WPT\Concerns\Instanceable;
use WPT\Crons\CronJob;

use function WPT\Helpers\setting;

class Plugin
{
use Instanceable;
Expand Down Expand Up @@ -39,12 +41,13 @@ public function options(): void
\register_setting('wpt-options', 'wpt_text_option');
\register_setting('wpt-options', 'wpt_radio_option');
\register_setting('wpt-options', 'wpt_select_option');
\register_setting('wpt-options', 'wpt_test_cron_enabled');
}, 10);
}

public function cronSchedules(): void
{
add_filter('cron_schedules', function (array $schedules): array {
\add_filter('cron_schedules', function (array $schedules): array {
\collect(Helpers::wpCronIntervals())->each(function (array $interval) use (&$schedules) {
$schedules[$interval['slug']] = [
'interval' => $interval['value'] ?? '',
Expand All @@ -67,7 +70,7 @@ public function run(): void
$this->options();
$this->cronSchedules();

if (! \get_option('wpt_cron_enabled', 0)) {
if (! setting('cron_enabled') || ! setting('test_cron_enabled')) {
$this->cronJob()->unscheduleCron();
} else {
$this->cronJob()->scheduleCron();
Expand Down
1 change: 1 addition & 0 deletions wordpress-plugin-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

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

require_once WPT_DIR_PATH.'/inc/helpers.php';
require_once WPT_DIR_PATH.'/inc/notices.php';

function wptPlugin()
Expand Down

0 comments on commit 5076629

Please sign in to comment.