クラス
SettingsPage
ソース ソース
ファイル: src/Admin/Pages/SettingsPage.php
class SettingsPage
{
const OPTION_KEY = 'wcexics_options';
const ENABLE_ITEMCSV_OPT = 'enable_itemcsv';
/**
* Creates settings page
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return void
*/
public static function init() {
add_action('admin_init', [get_class(), 'registerSetting']);
add_action('usces_action_shop_admin_menue', [get_class(), 'registerSettingsPage'], 20);
}
/**
* Returns options array
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return array
*/
public static function getOptions() {
return get_option(self::OPTION_KEY, [
self::ENABLE_ITEMCSV_OPT => 'no',
]);
}
/**
* Registers `wcexics_options` option names
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return void
*/
public static function registerSetting() {
register_setting(self::OPTION_KEY, self::ENABLE_ITEMCSV_OPT);
}
/**
* Registers settings page
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return void
*/
public static function registerSettingsPage() {
add_submenu_page(
USCES_PLUGIN_BASENAME,
__('WCEX Item Combo-Set', 'wcexics'),
__('COMBO-SET', 'wcexics'),
'manage_options',
'wcexics_settings_page',
[get_class(), 'addSettingsPage']
);
}
/**
* Outputs settings page HTML
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return void
*/
public static function addSettingsPage() {
if (!current_user_can('manage_options')) {
wp_die(__('You do not have sufficient permissions to access this page.'));
}
if (isset($_POST['wcexics_update_options']) && (int)wp_unslash(sanitize_text_field($_POST['wcexics_update_options'])) === 1) {
$opts = self::getOptions();
if (isset($_POST[self::ENABLE_ITEMCSV_OPT])) {
$optv = trim(wp_unslash(sanitize_text_field($_POST[self::ENABLE_ITEMCSV_OPT])));
if ($optv === 'yes' || $optv === 'no') {
$opts[self::ENABLE_ITEMCSV_OPT] = $optv;
}
}
update_option(self::OPTION_KEY, $opts);
}
?>
<div class="wrap">
<h1><?php esc_html_e('WCEX Item Combo-Set', 'wcexics'); ?></h1>
<form method="post" action="">
<?php settings_fields(self::OPTION_KEY); ?>
<table class="form-table" role="presentation">
<?php self::enableItemCSVSection(); ?>
</table>
<input type="hidden" name="wcexics_update_options" value="1" />
<?php submit_button(); ?>
</form>
</div>
<?php
}
/**
* Adds radio buttons for enabling/disabling item CSV
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return void
*/
public static function enableItemCSVSection() {
$opts = self::getOptions();
?>
<tr>
<th scope="row"><?php esc_html_e('Item CSV export/import', 'wcexics'); ?></th>
<td>
<fieldset>
<label>
<input
name="<?php echo self::ENABLE_ITEMCSV_OPT; ?>"
type="radio"
value="yes"
<?php echo $opts[self::ENABLE_ITEMCSV_OPT] === 'yes' ? 'checked' : ''; ?>
/>
<?php esc_html_e('Enabled') ?>
</label>
<br />
<label>
<input
name="<?php echo self::ENABLE_ITEMCSV_OPT; ?>"
type="radio"
value="no"
<?php echo $opts[self::ENABLE_ITEMCSV_OPT] === 'no' ? 'checked' : ''; ?>
/>
<?php esc_html_e('Disabled') ?>
</label>
</fieldset>
<p class="description">
<?php _e('If enabled, importing requires certain columns to be present in the CSV file.', 'wcexics'); ?>
</p>
</td>
</tr>
<?php
}
}
- addSettingsPage — Outputs settings page HTML
- enableItemCSVSection — Adds radio buttons for enabling/disabling item CSV
- getOptions — Returns options array
- init — Creates settings page
- registerSetting — Registers `wcexics_options` option names
- registerSettingsPage — Registers settings page