Settings.php
432 lines
| 1 | <?php |
| 2 | namespace EmbedPress\Ends\Back; |
| 3 | |
| 4 | use \EmbedPress\Core; |
| 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.0 |
| 17 | */ |
| 18 | class Settings |
| 19 | { |
| 20 | /** |
| 21 | * This class namespace. |
| 22 | * |
| 23 | * @since 1.0.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.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.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.0 |
| 57 | * @access private |
| 58 | * @static |
| 59 | * |
| 60 | * @var string $sectionGroupIdentifier The name of the plugin. |
| 61 | */ |
| 62 | private static $sectionGroupIdentifier = "embedpress"; |
| 63 | |
| 64 | /** |
| 65 | * Map to all settings. |
| 66 | * |
| 67 | * @since 1.0.0 |
| 68 | * @access private |
| 69 | * @static |
| 70 | * |
| 71 | * @var string $fieldMap |
| 72 | */ |
| 73 | private static $fieldMap = array( |
| 74 | 'enablePluginInAdmin' => array( |
| 75 | 'label' => "Allow EmbedPress in Admin", |
| 76 | 'section' => "admin" |
| 77 | ), |
| 78 | 'displayPreviewBox' => array( |
| 79 | 'label' => "Load embeds inside Editors", |
| 80 | 'section' => "admin" |
| 81 | ), |
| 82 | 'forceFacebookLanguage' => array( |
| 83 | 'label' => "Facebook embeds language", |
| 84 | 'section' => "admin" |
| 85 | ) |
| 86 | ); |
| 87 | |
| 88 | /** |
| 89 | * Class constructor. This prevents the class being directly instantiated. |
| 90 | * |
| 91 | * @since 1.0.0 |
| 92 | */ |
| 93 | public function __construct() |
| 94 | {} |
| 95 | |
| 96 | /** |
| 97 | * This prevents the class being cloned. |
| 98 | * |
| 99 | * @since 1.0.0 |
| 100 | */ |
| 101 | public function __clone() |
| 102 | {} |
| 103 | |
| 104 | /** |
| 105 | * Method that adds an sub-item for EmbedPress to the WordPress Settings menu. |
| 106 | * |
| 107 | * @since 1.0.0 |
| 108 | * @static |
| 109 | */ |
| 110 | public static function registerMenuItem() |
| 111 | { |
| 112 | add_object_page('EmbedPress Settings', 'EmbedPress', 'manage_options', 'embedpress', array(self::$namespace, 'renderForm')); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Method that configures the EmbedPress settings page. |
| 117 | * |
| 118 | * @since 1.0.0 |
| 119 | * @static |
| 120 | */ |
| 121 | public static function registerActions() |
| 122 | { |
| 123 | $activeTab = isset($_GET['tab']) ? strtolower($_GET['tab']) : ""; |
| 124 | if ($activeTab !== "embedpress") { |
| 125 | $action = "embedpress:{$activeTab}:settings:register"; |
| 126 | } else { |
| 127 | $activeTab = ""; |
| 128 | } |
| 129 | |
| 130 | if (!empty($activeTab) && has_action($action)) { |
| 131 | do_action($action, array( |
| 132 | 'id' => self::$sectionAdminIdentifier, |
| 133 | 'slug' => self::$identifier |
| 134 | )); |
| 135 | } else { |
| 136 | register_setting(self::$sectionGroupIdentifier, self::$sectionGroupIdentifier, array(self::$namespace, "validateForm")); |
| 137 | |
| 138 | add_settings_section(self::$sectionAdminIdentifier, 'General Settings', null, self::$identifier); |
| 139 | |
| 140 | foreach (self::$fieldMap as $fieldName => $field) { |
| 141 | add_settings_field($fieldName, $field['label'], array(self::$namespace, "renderField_{$fieldName}"), self::$identifier, self::${"section". ucfirst($field['section']) ."Identifier"}); |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Method that render the settings's form. |
| 148 | * |
| 149 | * @since 1.0.0 |
| 150 | * @static |
| 151 | */ |
| 152 | public static function renderForm() |
| 153 | { |
| 154 | $activeTab = isset($_GET['tab']) ? strtolower($_GET['tab']) : ""; |
| 155 | $settingsFieldsIdentifier = !empty($activeTab) ? "embedpress:{$activeTab}" : self::$sectionGroupIdentifier; |
| 156 | $settingsSectionsIdentifier = !empty($activeTab) ? "embedpress:{$activeTab}" : self::$identifier; |
| 157 | ?> |
| 158 | <div> |
| 159 | <h1>EmbedPress</h1> |
| 160 | |
| 161 | <?php settings_errors(); ?> |
| 162 | |
| 163 | <h2 class="nav-tab-wrapper"> |
| 164 | <a href="?page=embedpress" class="nav-tab<?php echo $activeTab === 'embedpress' || empty($activeTab) ? ' nav-tab-active' : ''; ?> ">General settings</a> |
| 165 | |
| 166 | <?php do_action('embedpress:settings:render:tab', $activeTab); ?> |
| 167 | </h2> |
| 168 | |
| 169 | <form action="options.php" method="POST"> |
| 170 | <?php settings_fields($settingsFieldsIdentifier); ?> |
| 171 | <?php do_settings_sections($settingsSectionsIdentifier); ?> |
| 172 | |
| 173 | <input name="Submit" type="submit" class="button button-primary" value="Save changes" /> |
| 174 | </form> |
| 175 | </div> |
| 176 | <?php |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Method that validates the form data. |
| 181 | * |
| 182 | * @since 1.0.0 |
| 183 | * @static |
| 184 | * |
| 185 | * @param mixed $freshData Data received from the form. |
| 186 | * |
| 187 | * @return array |
| 188 | */ |
| 189 | public static function validateForm($freshData) |
| 190 | { |
| 191 | $data = array( |
| 192 | 'displayPreviewBox' => (bool)$freshData['displayPreviewBox'], |
| 193 | 'enablePluginInAdmin' => (bool)$freshData['enablePluginInAdmin'], |
| 194 | 'fbLanguage' => $freshData['fbLanguage'] |
| 195 | ); |
| 196 | |
| 197 | return $data; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Method that renders the displayPreviewBox input. |
| 202 | * |
| 203 | * @since 1.0.0 |
| 204 | * @static |
| 205 | */ |
| 206 | public static function renderField_displayPreviewBox() |
| 207 | { |
| 208 | $fieldName = "displayPreviewBox"; |
| 209 | |
| 210 | $options = get_option(self::$sectionGroupIdentifier); |
| 211 | |
| 212 | $activeOptions = Core::getSettings(); |
| 213 | if (isset($activeOptions->enablePluginInAdmin) && (bool)$activeOptions->enablePluginInAdmin === false) { |
| 214 | $options[$fieldName] = false; |
| 215 | } else { |
| 216 | $options[$fieldName] = !isset($options[$fieldName]) ? true : (bool)$options[$fieldName]; |
| 217 | } |
| 218 | unset($activeOptions); |
| 219 | |
| 220 | echo '<label><input type="radio" id="'. $fieldName .'_0" name="'. self::$sectionGroupIdentifier .'['. $fieldName .']" value="0" '. (!$options[$fieldName] ? "checked" : "") .' /> No</label>'; |
| 221 | echo " "; |
| 222 | echo '<label><input type="radio" id="'. $fieldName .'_1" name="'. self::$sectionGroupIdentifier .'['. $fieldName .']" value="1" '. ($options[$fieldName] ? "checked" : "") .' /> Yes</label>'; |
| 223 | echo '<p class="description">Load embeds automatically detected inside your editor\'s content (i.e. TinyMCE).</p>'; |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Method that renders the enablePluginInAdmin input. |
| 228 | * |
| 229 | * @since 1.0.0 |
| 230 | * @static |
| 231 | */ |
| 232 | public static function renderField_enablePluginInAdmin() |
| 233 | { |
| 234 | $fieldName = "enablePluginInAdmin"; |
| 235 | |
| 236 | $options = get_option(self::$sectionGroupIdentifier); |
| 237 | |
| 238 | $options[$fieldName] = !isset($options[$fieldName]) ? true : (bool)$options[$fieldName]; |
| 239 | |
| 240 | echo '<label><input type="radio" id="'. $fieldName .'_0" name="'. self::$sectionGroupIdentifier .'['. $fieldName .']" value="0" '. (!$options[$fieldName] ? "checked" : "") .' /> No</label>'; |
| 241 | echo " "; |
| 242 | echo '<label><input type="radio" id="'. $fieldName .'_1" name="'. self::$sectionGroupIdentifier .'['. $fieldName .']" value="1" '. ($options[$fieldName] ? "checked" : "") .' /> Yes</label>'; |
| 243 | echo '<p class="description">Allow EmbedPress to run here in the Admin area. Disabling this <strong>will not</strong> affect your frontend embeds.</p>'; |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Method that renders the forceFacebookLanguage input. |
| 248 | * |
| 249 | * @since 1.3.0 |
| 250 | * @static |
| 251 | */ |
| 252 | public static function renderField_forceFacebookLanguage() |
| 253 | { |
| 254 | $fieldName = "fbLanguage"; |
| 255 | |
| 256 | $options = get_option(self::$sectionGroupIdentifier); |
| 257 | |
| 258 | $options[$fieldName] = !isset($options[$fieldName]) ? "" : $options[$fieldName]; |
| 259 | |
| 260 | $facebookLocales = self::getFacebookAvailableLocales(); |
| 261 | |
| 262 | echo '<select name="'. self::$sectionGroupIdentifier .'['. $fieldName .']">'; |
| 263 | echo '<option value="0">Automatic (by Facebook)</option>'; |
| 264 | echo '<optgroup label="Available">'; |
| 265 | foreach ($facebookLocales as $locale => $localeName) { |
| 266 | echo '<option value="'. $locale .'"'. ($options[$fieldName] === $locale ? ' selected' : '') .'>'. $localeName .'</option>'; |
| 267 | } |
| 268 | echo '</optgroup>'; |
| 269 | echo '</select>'; |
| 270 | |
| 271 | echo '<p class="description">Choose a different language for your Facebook embeds.</p>'; |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Returns a list of locales that can be used on Facebook embeds. |
| 276 | * |
| 277 | * @since 1.3.0 |
| 278 | * @static |
| 279 | * |
| 280 | * @return array |
| 281 | */ |
| 282 | public static function getFacebookAvailableLocales() |
| 283 | { |
| 284 | $locales = array( |
| 285 | 'af_ZA' => "Afrikaans", |
| 286 | 'ak_GH' => "Akan", |
| 287 | 'am_ET' => "Amharic", |
| 288 | 'ar_AR' => "Arabic", |
| 289 | 'as_IN' => "Assamese", |
| 290 | 'ay_BO' => "Aymara", |
| 291 | 'az_AZ' => "Azerbaijani", |
| 292 | 'be_BY' => "Belarusian", |
| 293 | 'bg_BG' => "Bulgarian", |
| 294 | 'bn_IN' => "Bengali", |
| 295 | 'br_FR' => "Breton", |
| 296 | 'bs_BA' => "Bosnian", |
| 297 | 'ca_ES' => "Catalan", |
| 298 | 'cb_IQ' => "Sorani Kurdish", |
| 299 | 'ck_US' => "Cherokee", |
| 300 | 'co_FR' => "Corsican", |
| 301 | 'cs_CZ' => "Czech", |
| 302 | 'cx_PH' => "Cebuano", |
| 303 | 'cy_GB' => "Welsh", |
| 304 | 'da_DK' => "Danish", |
| 305 | 'de_DE' => "German", |
| 306 | 'el_GR' => "Greek", |
| 307 | 'en_GB' => "English (UK)", |
| 308 | 'en_IN' => "English (India)", |
| 309 | 'en_PI' => "English (Pirate)", |
| 310 | 'en_UD' => "English (Upside Down)", |
| 311 | 'en_US' => "English (US)", |
| 312 | 'eo_EO' => "Esperanto", |
| 313 | 'es_CL' => "Spanish (Chile)", |
| 314 | 'es_CO' => "Spanish (Colombia)", |
| 315 | 'es_ES' => "Spanish (Spain)", |
| 316 | 'es_LA' => "Spanish", |
| 317 | 'es_MX' => "Spanish (Mexico)", |
| 318 | 'es_VE' => "Spanish (Venezuela)", |
| 319 | 'et_EE' => "Estonian", |
| 320 | 'eu_ES' => "Basque", |
| 321 | 'fa_IR' => "Persian", |
| 322 | 'fb_LT' => "Leet Speak", |
| 323 | 'ff_NG' => "Fulah", |
| 324 | 'fi_FI' => "Finnish", |
| 325 | 'fo_FO' => "Faroese", |
| 326 | 'fr_CA' => "French (Canada)", |
| 327 | 'fr_FR' => "French (France)", |
| 328 | 'fy_NL' => "Frisian", |
| 329 | 'ga_IE' => "Irish", |
| 330 | 'gl_ES' => "Galician", |
| 331 | 'gn_PY' => "Guarani", |
| 332 | 'gu_IN' => "Gujarati", |
| 333 | 'gx_GR' => "Classical Greek", |
| 334 | 'ha_NG' => "Hausa", |
| 335 | 'he_IL' => "Hebrew", |
| 336 | 'hi_IN' => "Hindi", |
| 337 | 'hr_HR' => "Croatian", |
| 338 | 'ht_HT' => "Haitian Creole", |
| 339 | 'hu_HU' => "Hungarian", |
| 340 | 'hy_AM' => "Armenian", |
| 341 | 'id_ID' => "Indonesian", |
| 342 | 'ig_NG' => "Igbo", |
| 343 | 'is_IS' => "Icelandic", |
| 344 | 'it_IT' => "Italian", |
| 345 | 'ja_JP' => "Japanese", |
| 346 | 'ja_KS' => "Japanese (Kansai)", |
| 347 | 'jv_ID' => "Javanese", |
| 348 | 'ka_GE' => "Georgian", |
| 349 | 'kk_KZ' => "Kazakh", |
| 350 | 'km_KH' => "Khmer", |
| 351 | 'kn_IN' => "Kannada", |
| 352 | 'ko_KR' => "Korean", |
| 353 | 'ku_TR' => "Kurdish (Kurmanji)", |
| 354 | 'ky_KG' => "Kyrgyz", |
| 355 | 'la_VA' => "Latin", |
| 356 | 'lg_UG' => "Ganda", |
| 357 | 'li_NL' => "Limburgish", |
| 358 | 'ln_CD' => "Lingala", |
| 359 | 'lo_LA' => "Lao", |
| 360 | 'lt_LT' => "Lithuanian", |
| 361 | 'lv_LV' => "Latvian", |
| 362 | 'mg_MG' => "Malagasy", |
| 363 | 'mi_NZ' => "Māori", |
| 364 | 'mk_MK' => "Macedonian", |
| 365 | 'ml_IN' => "Malayalam", |
| 366 | 'mn_MN' => "Mongolian", |
| 367 | 'mr_IN' => "Marathi", |
| 368 | 'ms_MY' => "Malay", |
| 369 | 'mt_MT' => "Maltese", |
| 370 | 'my_MM' => "Burmese", |
| 371 | 'nb_NO' => "Norwegian (bokmal)", |
| 372 | 'nd_ZW' => "Ndebele", |
| 373 | 'ne_NP' => "Nepali", |
| 374 | 'nl_BE' => "Dutch (België)", |
| 375 | 'nl_NL' => "Dutch", |
| 376 | 'nn_NO' => "Norwegian (nynorsk)", |
| 377 | 'ny_MW' => "Chewa", |
| 378 | 'or_IN' => "Oriya", |
| 379 | 'pa_IN' => "Punjabi", |
| 380 | 'pl_PL' => "Polish", |
| 381 | 'ps_AF' => "Pashto", |
| 382 | 'pt_BR' => "Portuguese (Brazil)", |
| 383 | 'pt_PT' => "Portuguese (Portugal)", |
| 384 | 'qc_GT' => "Quiché", |
| 385 | 'qu_PE' => "Quechua", |
| 386 | 'rm_CH' => "Romansh", |
| 387 | 'ro_RO' => "Romanian", |
| 388 | 'ru_RU' => "Russian", |
| 389 | 'rw_RW' => "Kinyarwanda", |
| 390 | 'sa_IN' => "Sanskrit", |
| 391 | 'sc_IT' => "Sardinian", |
| 392 | 'se_NO' => "Northern Sámi", |
| 393 | 'si_LK' => "Sinhala", |
| 394 | 'sk_SK' => "Slovak", |
| 395 | 'sl_SI' => "Slovenian", |
| 396 | 'sn_ZW' => "Shona", |
| 397 | 'so_SO' => "Somali", |
| 398 | 'sq_AL' => "Albanian", |
| 399 | 'sr_RS' => "Serbian", |
| 400 | 'sv_SE' => "Swedish", |
| 401 | 'sw_KE' => "Swahili", |
| 402 | 'sy_SY' => "Syriac", |
| 403 | 'sz_PL' => "Silesian", |
| 404 | 'ta_IN' => "Tamil", |
| 405 | 'te_IN' => "Telugu", |
| 406 | 'tg_TJ' => "Tajik", |
| 407 | 'th_TH' => "Thai", |
| 408 | 'tk_TM' => "Turkmen", |
| 409 | 'tl_PH' => "Filipino", |
| 410 | 'tl_ST' => "Klingon", |
| 411 | 'tr_TR' => "Turkish", |
| 412 | 'tt_RU' => "Tatar", |
| 413 | 'tz_MA' => "Tamazight", |
| 414 | 'uk_UA' => "Ukrainian", |
| 415 | 'ur_PK' => "Urdu", |
| 416 | 'uz_UZ' => "Uzbek", |
| 417 | 'vi_VN' => "Vietnamese", |
| 418 | 'wo_SN' => "Wolof", |
| 419 | 'xh_ZA' => "Xhosa", |
| 420 | 'yi_DE' => "Yiddish", |
| 421 | 'yo_NG' => "Yoruba", |
| 422 | 'zh_CN' => "Simplified Chinese (China)", |
| 423 | 'zh_HK' => "Traditional Chinese (Hong Kong)", |
| 424 | 'zh_TW' => "Traditional Chinese (Taiwan)", |
| 425 | 'zu_ZA' => "Zulu", |
| 426 | 'zz_TR' => "Zazaki" |
| 427 | ); |
| 428 | |
| 429 | return $locales; |
| 430 | } |
| 431 | } |
| 432 |