Created
September 2, 2016 14:40
-
-
Save AntoscencoVladimir/eed43fd3f7700a8e86aef6ecf3e117fe to your computer and use it in GitHub Desktop.
Custom settings page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if (!defined('FW')) die('Forbidden'); | |
// file: {theme}/inc/includes/demo-settings-page.php // https://github.com/ThemeFuse/Theme-Includes | |
final class _Andromeda_Unyson_Settings_Page | |
{ | |
/** | |
* @var FW_Form | |
*/ | |
private static $settings_form; | |
public static function init() | |
{ | |
self::$settings_form = new FW_Form('andromeda_settings', array( | |
'render' => array(__CLASS__, '_settings_form_render'), | |
'validate' => array(__CLASS__, '_settings_form_validate'), | |
'save' => array(__CLASS__, '_settings_form_save'), | |
)); | |
add_action('admin_menu', array(__CLASS__, '_action_admin_menu')); | |
} | |
private static function get_options() | |
{ | |
/** | |
* Load options from | |
* {theme}/framework-customizations/theme/options/settings.php | |
*/ | |
$options = fw()->theme->get_options('andromeda-settings'); | |
if (empty($options)) { // the file doesn't exist, use demo options | |
$options = array( | |
't1' => array( | |
'type' => 'tab', | |
'title' => __('Demo Tab #1', 'fw'), | |
'options' => array( | |
'b1' => array( | |
'type' => 'box', | |
'title' => __('Demo Box #1', 'fw'), | |
'options' => array( | |
'o1' => array( | |
'type' => 'text', | |
'label' => __('Demo Option #1', 'fw'), | |
'value' => 'Default Value #1', | |
), | |
'o2' => array( | |
'type' => 'textarea', | |
'label' => __('Demo Option #2', 'fw'), | |
'value' => 'Default Value #2', | |
), | |
), | |
), | |
'b2' => array( | |
'type' => 'box', | |
'title' => __('Demo Box #2', 'fw'), | |
'options' => array( | |
'o3' => array( | |
'type' => 'checkbox', | |
'label' => __('Demo Option #3', 'fw'), | |
), | |
'o4' => array( | |
'type' => 'text', | |
'label' => __('Demo Option #4', 'fw'), | |
), | |
), | |
), | |
), | |
), | |
't2' => array( | |
'type' => 'tab', | |
'title' => __('Demo Tab #1', 'fw'), | |
'options' => array( | |
'o5' => array( | |
'type' => 'checkbox', | |
'label' => __('Demo Option #5', 'fw'), | |
), | |
'o6' => array( | |
'type' => 'text', | |
'label' => __('Demo Option #6', 'fw'), | |
), | |
), | |
), | |
); | |
} | |
return $options; | |
} | |
private static function get_db_options() | |
{ | |
return FW_WP_Option::get('andromeda_settings_options'); | |
} | |
private static function set_db_options($value) | |
{ | |
if (empty($value)) { // happens on settings reset | |
delete_option('andromeda_settings_options'); | |
} else { | |
FW_WP_Option::set( | |
'andromeda_settings_options', | |
null, | |
$value | |
); | |
} | |
} | |
public static function _action_admin_menu() | |
{ | |
add_menu_page( | |
__( 'Andromeda Settings', 'fw' ), | |
__( 'Andromeda Settings', 'fw' ), | |
'manage_options', | |
'andromeda-settings', | |
array(__CLASS__, '_print_settings_page') | |
); | |
} | |
public static function _print_settings_page() | |
{ | |
echo '<div class="wrap">'; | |
echo '<h2>' . __('Andromeda Settings', 'fw') . '</h2><br/>'; | |
self::$settings_form->render(); | |
echo '</div>'; | |
} | |
public static function _settings_form_render($data) | |
{ | |
$options = self::get_options(); | |
if ($values = FW_Request::POST(FW_Option_Type::get_default_name_prefix())) { | |
// This is form submit, extract correct values from $_POST values | |
$values = fw_get_options_values_from_input($options, $values); | |
} else { | |
// This is GET request, extract previously saved correct values | |
$values = self::get_db_options(); | |
} | |
$data['attr']['class'] = 'andromeda-settings-form'; | |
$data['submit']['html'] = | |
fw_html_tag('input', array( | |
'type' => 'submit', | |
'name' => 'andromeda_settings_save', | |
'value' => __('Save', 'fw'), | |
'class' => 'button-primary button-large', | |
)) | |
. ' ' . | |
fw_html_tag('input', array( | |
'type' => 'submit', | |
'name' => 'andromeda_settings_reset', | |
'value' => __('Reset', 'fw'), | |
'class' => 'button-secondary button-large', | |
)); | |
echo fw()->backend->render_options($options, $values); | |
return $data; | |
} | |
public static function _settings_form_validate($errors) | |
{ | |
if (!current_user_can('manage_options')) { | |
$errors['_no_permission'] = __('You have no permissions to change settings options', 'fw');; | |
} | |
return $errors; | |
} | |
public static function _settings_form_save($data) | |
{ | |
if ( ! empty( $_POST['andromeda_settings_reset'] ) ) { // The "Reset" button was pressed | |
self::set_db_options(null); | |
FW_Flash_Messages::add( | |
'andromeda_settings_form_reset', | |
__('The options were successfully reset', 'fw'), | |
'success' | |
); | |
} else { | |
self::set_db_options( | |
fw_get_options_values_from_input(self::get_options()) | |
); | |
FW_Flash_Messages::add( | |
'andromeda_settings_form_save', | |
__('The options were successfully saved', 'fw'), | |
'success' | |
); | |
} | |
// redirect to the same page to prevent form re-submit on page refresh | |
$data['redirect'] = fw_current_url(); | |
return $data; | |
} | |
} | |
add_action('fw_init', array('_Andromeda_Unyson_Settings_Page', 'init')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment