AdminPage.Menu.class.php
4 years ago
AdminPage.Submenu.class.php
4 years ago
AdminPage.Themes.class.php
4 years ago
AdminPage.class.php
4 years ago
AdminPageSection.class.php
4 years ago
AdminPageSetting.Address.class.php
4 years ago
AdminPageSetting.Checkbox.class.php
4 years ago
AdminPageSetting.ColorPicker.class.php
4 years ago
AdminPageSetting.Count.class.php
4 years ago
AdminPageSetting.Editor.class.php
4 years ago
AdminPageSetting.FileUpload.class.php
4 years ago
AdminPageSetting.HTML.class.php
4 years ago
AdminPageSetting.Image.class.php
4 years ago
AdminPageSetting.InfiniteTable.class.php
4 years ago
AdminPageSetting.McApiKey.class.php
4 years ago
AdminPageSetting.McListMerge.class.php
4 years ago
AdminPageSetting.Number.class.php
4 years ago
AdminPageSetting.OpeningHours.class.php
4 years ago
AdminPageSetting.Ordering.class.php
4 years ago
AdminPageSetting.Radio.class.php
4 years ago
AdminPageSetting.Scheduler.class.php
4 years ago
AdminPageSetting.Select.class.php
4 years ago
AdminPageSetting.SelectMenu.class.php
4 years ago
AdminPageSetting.SelectPost.class.php
4 years ago
AdminPageSetting.SelectTaxonomy.class.php
4 years ago
AdminPageSetting.Text.class.php
4 years ago
AdminPageSetting.Textarea.class.php
4 years ago
AdminPageSetting.Time.class.php
4 years ago
AdminPageSetting.Toggle.class.php
4 years ago
AdminPageSetting.WarningTip.class.php
4 years ago
AdminPageSetting.class.php
4 years ago
Library.class.php
4 years ago
Library.class.php
515 lines
| 1 | <?php |
| 2 | if ( !class_exists( 'sapLibrary_2_6_1' ) ) { |
| 3 | /** |
| 4 | * This library class loads and provides access to the correct version of the |
| 5 | * Simple Admin Pages library. |
| 6 | * |
| 7 | * @since 1.0 |
| 8 | * @package Simple Admin Pages |
| 9 | */ |
| 10 | class sapLibrary_2_6_1 { |
| 11 | |
| 12 | // Version of the library |
| 13 | private $version = '2.6.1'; |
| 14 | |
| 15 | // A full URL to the library which is used to correctly link scripts and |
| 16 | // stylesheets. |
| 17 | public $lib_url; |
| 18 | |
| 19 | // A relative path to any custom library extension classes. When |
| 20 | // instantiating a custom setting class, the library will search in its own |
| 21 | // directory of classes adn also the $lib_extension_path. This way, |
| 22 | // developers can add on their own classes without mixing them with the |
| 23 | // default classes. |
| 24 | public $lib_extension_path; |
| 25 | |
| 26 | // An array of pages to add to the admin menus |
| 27 | public $pages = array(); |
| 28 | |
| 29 | // Collects errors for debugging |
| 30 | public $errors = array(); |
| 31 | |
| 32 | // Set debug mode to true to stop and print errors found while processing. |
| 33 | // @note This is not related to your PHP error reporting setting, but is an |
| 34 | // internal error tracking mechanism to catch missing or malformed data |
| 35 | // during development. |
| 36 | public $debug_mode = false; |
| 37 | |
| 38 | public $available_themes = [ |
| 39 | 'purple', |
| 40 | 'blue' |
| 41 | ]; |
| 42 | |
| 43 | public $current_theme = 'blue'; |
| 44 | |
| 45 | /** |
| 46 | * Initialize the library with the appropriate version |
| 47 | * @since 1.0 |
| 48 | */ |
| 49 | public function __construct( $args ) { |
| 50 | |
| 51 | if ( ! defined( 'SAP_VERSION' ) ) { |
| 52 | define( 'SAP_VERSION', '2.6.1' ); |
| 53 | } |
| 54 | |
| 55 | // If no URL path to the library is passed, we won't be able to add the |
| 56 | // CSS and Javascript to the admin panel |
| 57 | if ( !isset( $args['lib_url'] ) ) { |
| 58 | $this->set_error( |
| 59 | array( |
| 60 | 'id' => 'no-lib-url', |
| 61 | 'desc' => 'No URL path to the library provided when the libary was created.', |
| 62 | 'var' => $args, |
| 63 | 'line' => __LINE__, |
| 64 | 'function' => __FUNCTION__ |
| 65 | ) |
| 66 | ); |
| 67 | } else { |
| 68 | $this->lib_url = $args['lib_url']; |
| 69 | } |
| 70 | |
| 71 | // Set a library extension path if passed |
| 72 | if ( isset( $args['lib_extension_path'] ) ) { |
| 73 | $this->lib_extension_path = $args['lib_extension_path']; |
| 74 | } |
| 75 | |
| 76 | // Set the debug mode |
| 77 | if ( isset( $args['debug_mode'] ) && $args['debug_mode'] === true ) { |
| 78 | $this->debug_mode = true; |
| 79 | } |
| 80 | |
| 81 | // Set the current theme |
| 82 | if ( isset( $args['theme'] ) && in_array( $args['theme'], $this->available_themes ) ) { |
| 83 | $this->current_theme = $args['theme']; |
| 84 | } |
| 85 | |
| 86 | // Ensure we have access to WordPress' plugin functions |
| 87 | require_once(ABSPATH . '/wp-admin/includes/plugin.php'); |
| 88 | |
| 89 | // Load the required classes |
| 90 | $this->load_class( 'sapAdminPage', 'AdminPage.class.php' ); |
| 91 | $this->load_class( 'sapAdminPageSection', 'AdminPageSection.class.php' ); |
| 92 | $this->load_class( 'sapAdminPageSetting', 'AdminPageSetting.class.php' ); |
| 93 | |
| 94 | // Add the scripts to the admin pages |
| 95 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
| 96 | |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Load the class if it isn't already loaded |
| 101 | * @since 1.0 |
| 102 | */ |
| 103 | private function load_class( $class, $file ) { |
| 104 | |
| 105 | if ( !class_exists( $this->get_versioned_classname( $class ) ) ) { |
| 106 | require_once( $file ); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Return the version suffix for a class |
| 112 | * @since 1.0 |
| 113 | */ |
| 114 | private function get_versioned_classname( $class ) { |
| 115 | return $class . '_' . str_replace( '.', '_', $this->version ); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Check if the correct version of a class exists |
| 120 | * @since 1.0 |
| 121 | */ |
| 122 | private function versioned_class_exists( $class ) { |
| 123 | if ( class_exists( $this->get_versioned_classname( $class ) ) ) { |
| 124 | return true; |
| 125 | } else { |
| 126 | return false; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Load the files for a specific setting type and return the class |
| 132 | * to use when instantiating the setting object. |
| 133 | * |
| 134 | * @since 1.0 |
| 135 | */ |
| 136 | private function get_setting_classname( $type ) { |
| 137 | |
| 138 | switch( $type ) { |
| 139 | |
| 140 | case 'text' : |
| 141 | require_once('AdminPageSetting.Text.class.php'); |
| 142 | return $this->get_versioned_classname( 'sapAdminPageSettingText' ); |
| 143 | |
| 144 | case 'number' : |
| 145 | require_once('AdminPageSetting.Number.class.php'); |
| 146 | return $this->get_versioned_classname( 'sapAdminPageSettingNumber' ); |
| 147 | |
| 148 | case 'colorpicker' : |
| 149 | require_once('AdminPageSetting.ColorPicker.class.php'); |
| 150 | return $this->get_versioned_classname( 'sapAdminPageSettingColorPicker' ); |
| 151 | |
| 152 | case 'textarea' : |
| 153 | require_once('AdminPageSetting.Textarea.class.php'); |
| 154 | return $this->get_versioned_classname( 'sapAdminPageSettingTextarea' ); |
| 155 | |
| 156 | case 'select' : |
| 157 | require_once('AdminPageSetting.Select.class.php'); |
| 158 | return $this->get_versioned_classname( 'sapAdminPageSettingSelect' ); |
| 159 | |
| 160 | case 'toggle' : |
| 161 | require_once('AdminPageSetting.Toggle.class.php'); |
| 162 | return $this->get_versioned_classname( 'sapAdminPageSettingToggle' ); |
| 163 | |
| 164 | case 'image' : |
| 165 | require_once('AdminPageSetting.Image.class.php'); |
| 166 | return $this->get_versioned_classname( 'sapAdminPageSettingImage' ); |
| 167 | |
| 168 | case 'radio' : |
| 169 | require_once('AdminPageSetting.Radio.class.php'); |
| 170 | return $this->get_versioned_classname( 'sapAdminPageSettingRadio' ); |
| 171 | |
| 172 | case 'checkbox' : |
| 173 | require_once('AdminPageSetting.Checkbox.class.php'); |
| 174 | return $this->get_versioned_classname( 'sapAdminPageSettingCheckbox' ); |
| 175 | |
| 176 | case 'infinite_table' : |
| 177 | require_once('AdminPageSetting.InfiniteTable.class.php'); |
| 178 | return $this->get_versioned_classname( 'sapAdminPageSettingInfiniteTable' ); |
| 179 | |
| 180 | case 'count' : |
| 181 | require_once('AdminPageSetting.Count.class.php'); |
| 182 | return $this->get_versioned_classname( 'sapAdminPageSettingCount' ); |
| 183 | |
| 184 | case 'post' : |
| 185 | require_once('AdminPageSetting.SelectPost.class.php'); |
| 186 | return $this->get_versioned_classname( 'sapAdminPageSettingSelectPost' ); |
| 187 | |
| 188 | case 'menu' : |
| 189 | require_once('AdminPageSetting.SelectMenu.class.php'); |
| 190 | return $this->get_versioned_classname( 'sapAdminPageSettingSelectMenu' ); |
| 191 | |
| 192 | case 'taxonomy' : |
| 193 | require_once('AdminPageSetting.SelectTaxonomy.class.php'); |
| 194 | return $this->get_versioned_classname( 'sapAdminPageSettingSelectTaxonomy' ); |
| 195 | |
| 196 | case 'editor' : |
| 197 | require_once('AdminPageSetting.Editor.class.php'); |
| 198 | return $this->get_versioned_classname( 'sapAdminPageSettingEditor' ); |
| 199 | |
| 200 | case 'html' : |
| 201 | require_once('AdminPageSetting.HTML.class.php'); |
| 202 | return $this->get_versioned_classname( 'sapAdminPageSettingHTML' ); |
| 203 | |
| 204 | case 'scheduler' : |
| 205 | require_once('AdminPageSetting.Scheduler.class.php'); |
| 206 | return $this->get_versioned_classname( 'sapAdminPageSettingScheduler' ); |
| 207 | |
| 208 | case 'opening-hours' : |
| 209 | require_once('AdminPageSetting.OpeningHours.class.php'); |
| 210 | return $this->get_versioned_classname( 'sapAdminPageSettingOpeningHours' ); |
| 211 | |
| 212 | case 'address' : |
| 213 | require_once('AdminPageSetting.Address.class.php'); |
| 214 | return $this->get_versioned_classname( 'sapAdminPageSettingAddress' ); |
| 215 | |
| 216 | case 'file-upload' : |
| 217 | require_once('AdminPageSetting.FileUpload.class.php'); |
| 218 | return $this->get_versioned_classname( 'sapAdminPageSettingFileUpload' ); |
| 219 | |
| 220 | case 'ordering-table' : |
| 221 | require_once('AdminPageSetting.Ordering.class.php'); |
| 222 | return $this->get_versioned_classname( 'sapAdminPageSettingOrdering' ); |
| 223 | |
| 224 | case 'mcapikey' : |
| 225 | require_once('AdminPageSetting.McApiKey.class.php'); |
| 226 | return $this->get_versioned_classname( 'mcfrtbAdminPageSettingMcApiKey' ); |
| 227 | |
| 228 | case 'mclistmerge' : |
| 229 | require_once('AdminPageSetting.McListMerge.class.php'); |
| 230 | return $this->get_versioned_classname( 'mcfrtbAdminPageSettingMcListMerge' ); |
| 231 | |
| 232 | default : |
| 233 | |
| 234 | // Exit early if a custom type is declared without providing the |
| 235 | // details to find the type class |
| 236 | if ( ( !is_array( $type ) || !isset( $type['id'] ) ) || |
| 237 | ( !isset( $type['class'] ) || !isset( $type['filename'] ) ) ) { |
| 238 | return false; |
| 239 | } |
| 240 | |
| 241 | // Load the custom type file. Look for the file in the library's |
| 242 | // folder or check the custom library extension path. |
| 243 | if ( file_exists( $type['filename'] ) ) { |
| 244 | require_once( $type['filename'] ); |
| 245 | } elseif ( isset( $this->lib_extension_path ) && file_exists( $this->lib_extension_path . $type['filename'] ) ) { |
| 246 | require_once( $this->lib_extension_path . '/' . $type['filename'] ); |
| 247 | if ( !class_exists( $type['class'] ) ) { |
| 248 | return false; |
| 249 | } else { |
| 250 | return $type['class']; |
| 251 | } |
| 252 | } else { |
| 253 | return false; |
| 254 | } |
| 255 | |
| 256 | |
| 257 | // Check that we've loaded the appropriate class |
| 258 | if ( !$this->versioned_class_exists( $type['class'] ) ) { |
| 259 | return false; |
| 260 | } |
| 261 | |
| 262 | return $this->get_versioned_classname( $type['class'] ); |
| 263 | |
| 264 | } |
| 265 | |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Initialize a page |
| 270 | * @since 1.0 |
| 271 | * |
| 272 | * @todo perform some checks on args to ensure a valid page can be constructed |
| 273 | */ |
| 274 | public function add_page( $menu_location, $args = array() ) { |
| 275 | |
| 276 | // default should be 'options' |
| 277 | $class = $this->get_versioned_classname( 'sapAdminPage' ); |
| 278 | |
| 279 | if ( $menu_location == 'themes' ) { |
| 280 | $this->load_class( 'sapAdminPageThemes', 'AdminPage.Themes.class.php' ); |
| 281 | $class = $this->get_versioned_classname( 'sapAdminPageThemes' ); |
| 282 | } elseif ( $menu_location == 'menu' ) { |
| 283 | $this->load_class( 'sapAdminPageMenu', 'AdminPage.Menu.class.php' ); |
| 284 | $class = $this->get_versioned_classname( 'sapAdminPageMenu' ); |
| 285 | } elseif ( $menu_location == 'submenu' ) { |
| 286 | $this->load_class( 'sapAdminPageSubmenu', 'AdminPage.Submenu.class.php' ); |
| 287 | $class = $this->get_versioned_classname( 'sapAdminPageSubmenu' ); |
| 288 | } |
| 289 | |
| 290 | if ( class_exists( $class ) ) { |
| 291 | $this->pages[ $args['id'] ] = new $class( $args ); |
| 292 | } |
| 293 | |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Initialize a section |
| 298 | * @since 1.0 |
| 299 | * |
| 300 | * @todo perform some checks on args to ensure a valid section can be constructed |
| 301 | */ |
| 302 | public function add_section( $page, $args = array() ) { |
| 303 | |
| 304 | if ( !isset( $this->pages[ $page ] ) ) { |
| 305 | return false; |
| 306 | } else { |
| 307 | $args['page'] = $page; |
| 308 | } |
| 309 | |
| 310 | $class = $this->get_versioned_classname( 'sapAdminPageSection' ); |
| 311 | if ( class_exists( $class ) ) { |
| 312 | $this->pages[ $page ]->add_section( new $class( $args ) ); |
| 313 | } |
| 314 | |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * Initialize a setting |
| 319 | * |
| 320 | * The type variable can be a string pointing to a pre-defined setting type, |
| 321 | * or an array consisting of an id, classname and filename which references |
| 322 | * a custom setting type. @sa get_setting_classname() |
| 323 | * |
| 324 | * @since 1.0 |
| 325 | */ |
| 326 | public function add_setting( $page, $section, $type, $args = array() ) { |
| 327 | |
| 328 | if ( !isset( $this->pages[ $page ] ) || !isset( $this->pages[ $page ]->sections[ $section ] ) ) { |
| 329 | return false; |
| 330 | } else { |
| 331 | $args['page'] = $page; |
| 332 | $args['tab'] = $this->pages[$page]->sections[ $section ]->get_page_slug(); |
| 333 | } |
| 334 | |
| 335 | $class = $this->get_setting_classname( $type ); |
| 336 | if ( $class && class_exists( $class ) ) { |
| 337 | $this->pages[ $page ]->sections[ $section ]->add_setting( new $class( $args ) ); |
| 338 | } |
| 339 | |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * Register all page, section and settings content with WordPress |
| 344 | * @since 1.0 |
| 345 | */ |
| 346 | public function add_admin_menus() { |
| 347 | |
| 348 | // If the library is run in debug mode, check for any errors in content, |
| 349 | // print any errors found, and don't add the menu if there are errors |
| 350 | if ( $this->debug_mode ) { |
| 351 | $errors = array(); |
| 352 | foreach ( $this->pages as $page ) { |
| 353 | foreach ( $page->sections as $section ) { |
| 354 | if ( count( $section->errors ) ) { |
| 355 | array_merge( $errors, $section->errors ); |
| 356 | } |
| 357 | foreach ( $section->settings as $setting ) { |
| 358 | if ( count( $setting->errors ) ) { |
| 359 | $errors = array_merge( $errors, $setting->errors ); |
| 360 | } |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | if ( count( $errors ) ) { |
| 365 | print_r( $errors ); |
| 366 | return; |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | // Add the action hooks |
| 371 | foreach ( $this->pages as $id => $page ) { |
| 372 | add_action( 'admin_menu', array( $page, 'add_admin_menu' ) ); |
| 373 | add_action( 'admin_init', array( $page, 'register_admin_menu' ) ); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | /** |
| 378 | * Port data from a previous version to the current version |
| 379 | * |
| 380 | * Version 2.0 of the library changes the structure of how it stores data. |
| 381 | * In order to upgrade the version of the library your plugin/theme is |
| 382 | * using, this method must be called after all of your pages and settings |
| 383 | * have been declared but before you run add_admin_menus(). |
| 384 | * |
| 385 | * This method will loop over all of the settings data and port any existing |
| 386 | * data to the new data structure. It will check if the data has been ported |
| 387 | * first before it updates the data. The old data will be removed to keep |
| 388 | * the database clean. |
| 389 | * |
| 390 | * @var int target_version Which data version the library should update to. |
| 391 | * @since 2.0 |
| 392 | */ |
| 393 | public function port_data( $target_version, $delete_old_data = true ) { |
| 394 | |
| 395 | // Port data to the storage structure in version 2 |
| 396 | if ( $target_version == 2 ) { |
| 397 | |
| 398 | foreach ( $this->pages as $page_id => $page ) { |
| 399 | |
| 400 | // Skip if this page has already been ported |
| 401 | if ( get_option( $page_id ) !== false ) { |
| 402 | continue; |
| 403 | } |
| 404 | |
| 405 | $page_values = array(); |
| 406 | |
| 407 | foreach ( $page->sections as $section ) { |
| 408 | foreach ( $section->settings as $setting ) { |
| 409 | $value = get_option( $setting->id ); |
| 410 | if ( $value !== false ) { |
| 411 | $page_values[ $setting->id ] = $value; |
| 412 | } |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | if ( count( $page_values ) ) { |
| 417 | $result = add_option( $page_id, $page_values ); |
| 418 | |
| 419 | // Delete old data if the flag is set and the new data was |
| 420 | // saved successfully. |
| 421 | if ( $delete_old_data === true && $result !== false ) { |
| 422 | foreach( $page_values as $setting_id => $setting_value ) { |
| 423 | delete_option( $setting_id ); |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | // Reset settings values |
| 428 | if ( $result === true ) { |
| 429 | |
| 430 | foreach ( $page->sections as $section ) { |
| 431 | foreach ( $section->settings as $setting ) { |
| 432 | $setting->set_value(); |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | } |
| 437 | } |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | } |
| 442 | |
| 443 | /** |
| 444 | * Enqueue the stylesheets and scripts |
| 445 | * @since 1.0 |
| 446 | */ |
| 447 | public function enqueue_scripts() { |
| 448 | |
| 449 | $screen = get_current_screen(); |
| 450 | |
| 451 | foreach ( $this->pages as $page_id => $page ) { |
| 452 | |
| 453 | // Only enqueue assets for the current page |
| 454 | if ( strpos( $screen->base, $page_id ) !== false ) { |
| 455 | |
| 456 | // Theme specifc files |
| 457 | switch ($this->current_theme) { |
| 458 | case 'blue': |
| 459 | wp_enqueue_style( |
| 460 | 'sap-admin-settings-css-blue-' . $this->version, |
| 461 | $this->lib_url . 'css/admin-settings-blue.css', |
| 462 | array(), |
| 463 | $this->version |
| 464 | ); |
| 465 | break; |
| 466 | |
| 467 | case 'purple': |
| 468 | wp_enqueue_style( |
| 469 | 'sap-admin-settings-css-purple-' . $this->version, |
| 470 | $this->lib_url . 'css/admin-settings-purple.css', |
| 471 | array(), |
| 472 | $this->version |
| 473 | ); |
| 474 | break; |
| 475 | } |
| 476 | |
| 477 | wp_enqueue_style( 'sap-admin-style-' . $this->version, $this->lib_url . 'css/admin.css', array(), $this->version ); |
| 478 | wp_enqueue_style( 'sap-spectrum-css-' . $this->version, $this->lib_url . 'css/spectrum.css', array(), $this->version ); |
| 479 | wp_enqueue_style( 'sap-admin-settings-css-' . $this->version, $this->lib_url . 'css/admin-settings.css', array(), $this->version ); |
| 480 | wp_enqueue_script( 'sap-spectrum-js-' . $this->version, $this->lib_url . 'js/spectrum.js', array( 'jquery' ), $this->version ); |
| 481 | wp_enqueue_script( 'sap-admin-settings-js-' . $this->version, $this->lib_url . 'js/admin-settings.js', array( 'jquery', 'sap-spectrum-js-' . $this->version ), $this->version ); |
| 482 | wp_enqueue_media(); |
| 483 | |
| 484 | foreach ( $page->sections as $section ) { |
| 485 | foreach ( $section->settings as $setting ) { |
| 486 | foreach( $setting->scripts as $handle => $script ) { |
| 487 | wp_enqueue_script( $handle, $this->lib_url . $script['path'], $script['dependencies'], $script['version'], $script['footer'] ); |
| 488 | } |
| 489 | foreach( $setting->styles as $handle => $style ) { |
| 490 | wp_enqueue_style( $handle, $this->lib_url . $style['path'], $style['dependencies'], $style['version'], $style['media'] ); |
| 491 | } |
| 492 | } |
| 493 | } |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | /** |
| 499 | * Set an error |
| 500 | * @since 1.0 |
| 501 | */ |
| 502 | public function set_error( $error ) { |
| 503 | $this->errors[] = array_merge( |
| 504 | $error, |
| 505 | array( |
| 506 | 'class' => get_class( $this ), |
| 507 | 'id' => $this->id, |
| 508 | 'backtrace' => debug_backtrace() |
| 509 | ) |
| 510 | ); |
| 511 | } |
| 512 | |
| 513 | } |
| 514 | } // endif; |
| 515 |