Settings.php
225 lines
| 1 | <?php |
| 2 | namespace EmbedPress\Ends\Back; |
| 3 | |
| 4 | use \EmbedPress\Plugin; |
| 5 | |
| 6 | (defined('ABSPATH') && defined('EMBEDPRESS_IS_LOADED')) or die("No direct script access allowed."); |
| 7 | |
| 8 | /** |
| 9 | * Entity that handles the plugin's settings page. |
| 10 | * |
| 11 | * @package EmbedPress |
| 12 | * @subpackage EmbedPress/Ends/Back |
| 13 | * @author PressShack <help@pressshack.com> |
| 14 | * @copyright Copyright (C) 2016 Open Source Training, LLC. All rights reserved. |
| 15 | * @license GPLv2 or later |
| 16 | * @since 1.0 |
| 17 | */ |
| 18 | class Settings |
| 19 | { |
| 20 | /** |
| 21 | * This class namespace. |
| 22 | * |
| 23 | * @since 1.0 |
| 24 | * @access private |
| 25 | * @static |
| 26 | * |
| 27 | * @var string $namespace |
| 28 | */ |
| 29 | private static $namespace = '\EmbedPress\Ends\Back\Settings'; |
| 30 | |
| 31 | /** |
| 32 | * The plugin's unique identifier. |
| 33 | * |
| 34 | * @since 1.0 |
| 35 | * @access private |
| 36 | * @static |
| 37 | * |
| 38 | * @var string $identifier |
| 39 | */ |
| 40 | private static $identifier = "plg_embedpress"; |
| 41 | |
| 42 | /** |
| 43 | * Unique identifier to the plugin's admin settings section. |
| 44 | * |
| 45 | * @since 1.0 |
| 46 | * @access private |
| 47 | * @static |
| 48 | * |
| 49 | * @var string $sectionAdminIdentifier |
| 50 | */ |
| 51 | private static $sectionAdminIdentifier = "embedpress_options_admin"; |
| 52 | |
| 53 | /** |
| 54 | * Unique identifier to the plugin's general settings section. |
| 55 | * |
| 56 | * @since 1.0 |
| 57 | * @access private |
| 58 | * @static |
| 59 | * |
| 60 | * @var string $sectionGroupIdentifier The name of the plugin. |
| 61 | */ |
| 62 | private static $sectionGroupIdentifier = "embedpress_options"; |
| 63 | |
| 64 | /** |
| 65 | * Map to all settings. |
| 66 | * |
| 67 | * @since 1.0 |
| 68 | * @access private |
| 69 | * @static |
| 70 | * |
| 71 | * @var string $fieldMap |
| 72 | */ |
| 73 | private static $fieldMap = array( |
| 74 | 'enablePluginInAdmin' => array( |
| 75 | 'label' => "Enable EmbedPress in the admin area", |
| 76 | 'section' => "admin" |
| 77 | ), |
| 78 | 'displayPreviewBox' => array( |
| 79 | 'label' => "Display Preview Box inside editor", |
| 80 | 'section' => "admin" |
| 81 | ) |
| 82 | ); |
| 83 | |
| 84 | /** |
| 85 | * Class constructor. This prevents the class being directly instantiated. |
| 86 | * |
| 87 | * @since 1.0 |
| 88 | */ |
| 89 | public function __construct() |
| 90 | {} |
| 91 | |
| 92 | /** |
| 93 | * This prevents the class being cloned. |
| 94 | * |
| 95 | * @since 1.0 |
| 96 | */ |
| 97 | public function __clone() |
| 98 | {} |
| 99 | |
| 100 | /** |
| 101 | * Method that adds an sub-item for EmbedPress to the WordPress Settings menu. |
| 102 | * |
| 103 | * @since 1.0 |
| 104 | * @static |
| 105 | */ |
| 106 | public static function registerMenuItem() |
| 107 | { |
| 108 | add_options_page('EmbedPress Settings', 'EmbedPress', 'manage_options', 'embedpress', array(self::$namespace, 'renderForm')); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Method that configures the EmbedPress settings page. |
| 113 | * |
| 114 | * @since 1.0 |
| 115 | * @static |
| 116 | */ |
| 117 | public static function registerActions() |
| 118 | { |
| 119 | register_setting(self::$sectionGroupIdentifier, self::$sectionGroupIdentifier, array(self::$namespace, "validateForm")); |
| 120 | |
| 121 | add_settings_section(self::$sectionAdminIdentifier, 'Admin Section Settings', array(self::$namespace, 'renderHelpText'), self::$identifier); |
| 122 | |
| 123 | foreach (self::$fieldMap as $fieldName => $field) { |
| 124 | add_settings_field($fieldName, $field['label'], array(self::$namespace, "renderField_{$fieldName}"), self::$identifier, self::${"section". ucfirst($field['section']) ."Identifier"}); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Method that render the settings's form. |
| 130 | * |
| 131 | * @since 1.0 |
| 132 | * @static |
| 133 | */ |
| 134 | public static function renderForm() |
| 135 | { |
| 136 | ?> |
| 137 | <div> |
| 138 | <h2>EmbedPress Settings Page</h2> |
| 139 | <form action="options.php" method="POST"> |
| 140 | <?php settings_fields(self::$sectionGroupIdentifier); ?> |
| 141 | <?php do_settings_sections(self::$identifier); ?> |
| 142 | |
| 143 | <input name="Submit" type="submit" value="Save changes" /> |
| 144 | </form> |
| 145 | </div> |
| 146 | <?php |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Method that validates the form data. |
| 151 | * |
| 152 | * @since 1.0 |
| 153 | * @static |
| 154 | * |
| 155 | * @param mixed $freshData Data received from the form. |
| 156 | * |
| 157 | * @return array |
| 158 | */ |
| 159 | public static function validateForm($freshData) |
| 160 | { |
| 161 | $data = array( |
| 162 | 'displayPreviewBox' => (bool)$freshData['displayPreviewBox'], |
| 163 | 'enablePluginInAdmin' => (bool)$freshData['enablePluginInAdmin'] |
| 164 | ); |
| 165 | |
| 166 | return $data; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Method that prints help info for the form. |
| 171 | * |
| 172 | * @since 1.0 |
| 173 | * @static |
| 174 | * |
| 175 | * @return string |
| 176 | */ |
| 177 | public static function renderHelpText() |
| 178 | { |
| 179 | return ""; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Method that renders the displayPreviewBox input. |
| 184 | * |
| 185 | * @since 1.0 |
| 186 | * @static |
| 187 | */ |
| 188 | public static function renderField_displayPreviewBox() |
| 189 | { |
| 190 | $fieldName = "displayPreviewBox"; |
| 191 | |
| 192 | $options = get_option(self::$sectionGroupIdentifier); |
| 193 | |
| 194 | $activeOptions = Plugin::getSettings(); |
| 195 | if (isset($activeOptions->enablePluginInAdmin) && (bool)$activeOptions->enablePluginInAdmin === false) { |
| 196 | $options[$fieldName] = false; |
| 197 | } else { |
| 198 | $options[$fieldName] = !isset($options[$fieldName]) ? true : (bool)$options[$fieldName]; |
| 199 | } |
| 200 | unset($activeOptions); |
| 201 | |
| 202 | echo '<label><input type="radio" id="'. $fieldName .'_0" name="'. self::$sectionGroupIdentifier .'['. $fieldName .']" value="0" '. (!$options[$fieldName] ? "checked" : "") .' /> No</label>'; |
| 203 | echo " "; |
| 204 | echo '<label><input type="radio" id="'. $fieldName .'_1" name="'. self::$sectionGroupIdentifier .'['. $fieldName .']" value="1" '. ($options[$fieldName] ? "checked" : "") .' /> Yes</label>'; |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Method that renders the enablePluginInAdmin input. |
| 209 | * |
| 210 | * @since 1.0 |
| 211 | * @static |
| 212 | */ |
| 213 | public static function renderField_enablePluginInAdmin() |
| 214 | { |
| 215 | $fieldName = "enablePluginInAdmin"; |
| 216 | |
| 217 | $options = get_option(self::$sectionGroupIdentifier); |
| 218 | |
| 219 | $options[$fieldName] = !isset($options[$fieldName]) ? true : (bool)$options[$fieldName]; |
| 220 | |
| 221 | echo '<label><input type="radio" id="'. $fieldName .'_0" name="'. self::$sectionGroupIdentifier .'['. $fieldName .']" value="0" '. (!$options[$fieldName] ? "checked" : "") .' /> No</label>'; |
| 222 | echo " "; |
| 223 | echo '<label><input type="radio" id="'. $fieldName .'_1" name="'. self::$sectionGroupIdentifier .'['. $fieldName .']" value="1" '. ($options[$fieldName] ? "checked" : "") .' /> Yes</label>'; |
| 224 | } |
| 225 | } |