PluginProbe
Slider Ultimate / 2.2.10
Slider Ultimate v2.2.10
2.2.10 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 All 54 releases
ultimate-slider / lib / simple-admin-pages / classes / Library.class.php
Library.class.php
537 lines 16.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( !class_exists( 'sapLibrary_2_6_19' ) ) {
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_19 {
11
12 // Version of the library
13 private $version = '2.6.19';
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.19' );
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 'time' :
225 require_once('AdminPageSetting.Time.class.php');
226 return $this->get_versioned_classname( 'sapAdminPageSettingTime' );
227
228 case 'mcapikey' :
229 require_once('AdminPageSetting.McApiKey.class.php');
230 return $this->get_versioned_classname( 'mcfrtbAdminPageSettingMcApiKey' );
231
232 case 'mclistmerge' :
233 require_once('AdminPageSetting.McListMerge.class.php');
234 return $this->get_versioned_classname( 'mcfrtbAdminPageSettingMcListMerge' );
235
236 case 'password' :
237 require_once('AdminPageSetting.Password.class.php');
238 return $this->get_versioned_classname( 'sapAdminPageSettingPassword' );
239
240 default :
241
242 // Exit early if a custom type is declared without providing the
243 // details to find the type class
244 if ( ( !is_array( $type ) || !isset( $type['id'] ) ) ||
245 ( !isset( $type['class'] ) || !isset( $type['filename'] ) ) ) {
246 return false;
247 }
248
249 // Load the custom type file. Look for the file in the library's
250 // folder or check the custom library extension path.
251 if ( file_exists( $type['filename'] ) ) {
252 require_once( $type['filename'] );
253 } elseif ( isset( $this->lib_extension_path ) && file_exists( $this->lib_extension_path . $type['filename'] ) ) {
254 require_once( $this->lib_extension_path . '/' . $type['filename'] );
255 if ( !class_exists( $type['class'] ) ) {
256 return false;
257 } else {
258 return $type['class'];
259 }
260 } else {
261 return false;
262 }
263
264
265 // Check that we've loaded the appropriate class
266 if ( !$this->versioned_class_exists( $type['class'] ) ) {
267 return false;
268 }
269
270 return $this->get_versioned_classname( $type['class'] );
271
272 }
273
274 }
275
276 /**
277 * Initialize a page
278 * @since 1.0
279 *
280 * @todo perform some checks on args to ensure a valid page can be constructed
281 */
282 public function add_page( $menu_location, $args = array() ) {
283
284 // default should be 'options'
285 $class = $this->get_versioned_classname( 'sapAdminPage' );
286
287 if ( $menu_location == 'themes' ) {
288 $this->load_class( 'sapAdminPageThemes', 'AdminPage.Themes.class.php' );
289 $class = $this->get_versioned_classname( 'sapAdminPageThemes' );
290 } elseif ( $menu_location == 'menu' ) {
291 $this->load_class( 'sapAdminPageMenu', 'AdminPage.Menu.class.php' );
292 $class = $this->get_versioned_classname( 'sapAdminPageMenu' );
293 } elseif ( $menu_location == 'submenu' ) {
294 $this->load_class( 'sapAdminPageSubmenu', 'AdminPage.Submenu.class.php' );
295 $class = $this->get_versioned_classname( 'sapAdminPageSubmenu' );
296 }
297
298 if ( class_exists( $class ) ) {
299 $this->pages[ $args['id'] ] = new $class( $args );
300 }
301
302 }
303
304 /**
305 * Initialize a section
306 * @since 1.0
307 *
308 * @todo perform some checks on args to ensure a valid section can be constructed
309 */
310 public function add_section( $page, $args = array() ) {
311
312 if ( !isset( $this->pages[ $page ] ) ) {
313 return false;
314 } else {
315 $args['page'] = $page;
316 }
317
318 $class = $this->get_versioned_classname( 'sapAdminPageSection' );
319 if ( class_exists( $class ) ) {
320 $this->pages[ $page ]->add_section( new $class( $args ) );
321 }
322
323 }
324
325 /**
326 * Initialize a setting
327 *
328 * The type variable can be a string pointing to a pre-defined setting type,
329 * or an array consisting of an id, classname and filename which references
330 * a custom setting type. @sa get_setting_classname()
331 *
332 * @since 1.0
333 */
334 public function add_setting( $page, $section, $type, $args = array() ) {
335
336 if ( !isset( $this->pages[ $page ] ) || !isset( $this->pages[ $page ]->sections[ $section ] ) ) {
337 return false;
338 } else {
339 $args['page'] = $page;
340 $args['tab'] = $this->pages[$page]->sections[ $section ]->get_page_slug();
341 }
342
343 $class = $this->get_setting_classname( $type );
344 if ( $class && class_exists( $class ) ) {
345 $this->pages[ $page ]->sections[ $section ]->add_setting( new $class( $args ) );
346 }
347
348 }
349
350 /**
351 * Removes a setting from a section after it's been initialized
352 *
353 * @since 2.6.15
354 */
355 public function remove_setting( $page, $section, $setting_id ) {
356
357 if ( !isset( $this->pages[ $page ] ) || !isset( $this->pages[ $page ]->sections[ $section ] ) || !isset( $this->pages[ $page ]->sections[ $section ]->settings[ $setting_id ] ) ) {
358 return false;
359 }
360
361 unset( $this->pages[ $page ]->sections[ $section ]->settings[ $setting_id ] );
362 }
363
364 /**
365 * Register all page, section and settings content with WordPress
366 * @since 1.0
367 */
368 public function add_admin_menus() {
369
370 // If the library is run in debug mode, check for any errors in content,
371 // print any errors found, and don't add the menu if there are errors
372 if ( $this->debug_mode ) {
373 $errors = array();
374 foreach ( $this->pages as $page ) {
375 foreach ( $page->sections as $section ) {
376 if ( count( $section->errors ) ) {
377 array_merge( $errors, $section->errors );
378 }
379 foreach ( $section->settings as $setting ) {
380 if ( count( $setting->errors ) ) {
381 $errors = array_merge( $errors, $setting->errors );
382 }
383 }
384 }
385 }
386 if ( count( $errors ) ) {
387 print_r( $errors );
388 return;
389 }
390 }
391
392 // Add the action hooks
393 foreach ( $this->pages as $id => $page ) {
394 add_action( 'admin_menu', array( $page, 'add_admin_menu' ) );
395 add_action( 'admin_init', array( $page, 'register_admin_menu' ) );
396 }
397 }
398
399 /**
400 * Port data from a previous version to the current version
401 *
402 * Version 2.0 of the library changes the structure of how it stores data.
403 * In order to upgrade the version of the library your plugin/theme is
404 * using, this method must be called after all of your pages and settings
405 * have been declared but before you run add_admin_menus().
406 *
407 * This method will loop over all of the settings data and port any existing
408 * data to the new data structure. It will check if the data has been ported
409 * first before it updates the data. The old data will be removed to keep
410 * the database clean.
411 *
412 * @var int target_version Which data version the library should update to.
413 * @since 2.0
414 */
415 public function port_data( $target_version, $delete_old_data = true ) {
416
417 // Port data to the storage structure in version 2
418 if ( $target_version == 2 ) {
419
420 foreach ( $this->pages as $page_id => $page ) {
421
422 // Skip if this page has already been ported
423 if ( get_option( $page_id ) !== false ) {
424 continue;
425 }
426
427 $page_values = array();
428
429 foreach ( $page->sections as $section ) {
430 foreach ( $section->settings as $setting ) {
431 $value = get_option( $setting->id );
432 if ( $value !== false ) {
433 $page_values[ $setting->id ] = $value;
434 }
435 }
436 }
437
438 if ( count( $page_values ) ) {
439 $result = add_option( $page_id, $page_values );
440
441 // Delete old data if the flag is set and the new data was
442 // saved successfully.
443 if ( $delete_old_data === true && $result !== false ) {
444 foreach( $page_values as $setting_id => $setting_value ) {
445 delete_option( $setting_id );
446 }
447 }
448
449 // Reset settings values
450 if ( $result === true ) {
451
452 foreach ( $page->sections as $section ) {
453 foreach ( $section->settings as $setting ) {
454 $setting->set_value();
455 }
456 }
457
458 }
459 }
460 }
461 }
462
463 }
464
465 /**
466 * Enqueue the stylesheets and scripts
467 * @since 1.0
468 */
469 public function enqueue_scripts() {
470
471 $screen = get_current_screen();
472
473 foreach ( $this->pages as $page_id => $page ) {
474
475 // Only enqueue assets for the current page
476 if ( strpos( $screen->base, $page_id ) !== false ) {
477
478 // Theme specifc files
479 switch ($this->current_theme) {
480 case 'blue':
481 wp_enqueue_style(
482 'sap-admin-settings-css-blue-' . $this->version,
483 $this->lib_url . 'css/admin-settings-blue.css',
484 array(),
485 $this->version
486 );
487 break;
488
489 case 'purple':
490 wp_enqueue_style(
491 'sap-admin-settings-css-purple-' . $this->version,
492 $this->lib_url . 'css/admin-settings-purple.css',
493 array(),
494 $this->version
495 );
496 break;
497 }
498
499 wp_enqueue_style( 'sap-admin-style-' . $this->version, $this->lib_url . 'css/admin.css', array(), $this->version );
500 wp_enqueue_style( 'sap-spectrum-css-' . $this->version, $this->lib_url . 'css/spectrum.css', array(), $this->version );
501 wp_enqueue_style( 'sap-admin-settings-css-' . $this->version, $this->lib_url . 'css/admin-settings.css', array(), $this->version );
502 wp_enqueue_script( 'sap-spectrum-js-' . $this->version, $this->lib_url . 'js/spectrum.js', array( 'jquery' ), $this->version );
503 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 );
504 wp_enqueue_media();
505
506 foreach ( $page->sections as $section ) {
507 foreach ( $section->settings as $setting ) {
508 foreach( $setting->scripts as $handle => $script ) {
509 wp_enqueue_script( $handle, $this->lib_url . $script['path'], $script['dependencies'], $script['version'], $script['footer'] );
510 }
511 foreach( $setting->styles as $handle => $style ) {
512 wp_enqueue_style( $handle, $this->lib_url . $style['path'], $style['dependencies'], $style['version'], $style['media'] );
513 }
514 }
515 }
516 }
517 }
518 }
519
520 /**
521 * Set an error
522 * @since 1.0
523 */
524 public function set_error( $error ) {
525 $this->errors[] = array_merge(
526 $error,
527 array(
528 'class' => get_class( $this ),
529 'id' => $this->id,
530 'backtrace' => debug_backtrace()
531 )
532 );
533 }
534
535 }
536 } // endif;
537