PluginProbe
QuadMenu – Mega Menu / 1.8.9
QuadMenu – Mega Menu v1.8.9
3.3.8 3.3.7 3.3.6 trunk 1.8.4 1.8.5 1.8.7 1.8.8 1.8.9 1.9.0 1.9.2 1.9.3 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 88 releases
quadmenu / lib / ReduxCore / inc / class.redux_api.php

class.redux_api.php in QuadMenu – Mega Menu 1.8.9, at lib/ReduxCore/inc/class.redux_api.php

654 lines 28.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Redux Framework API Class
5 * Makes instantiating a Redux object an absolute piece of cake.
6 *
7 * @package Redux_Framework
8 * @author Dovy Paukstys
9 * @subpackage Core
10 */
11
12 // Exit if accessed directly
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17 // Don't duplicate me!
18 if ( ! class_exists( 'Redux' ) ) {
19
20 /**
21 * Redux API Class
22 * Simple API for Redux Framework
23 *
24 * @since 1.0.0
25 */
26 class Redux {
27
28 public static $fields = array();
29 public static $sections = array();
30 public static $help = array();
31 public static $args = array();
32 public static $priority = array();
33 public static $errors = array();
34 public static $init = array();
35 public static $extensions = array();
36 public static $uses_extensions = array();
37
38 public function __call( $closure, $args ) {
39 return call_user_func_array( $this->{$closure}->bindTo( $this ), $args );
40 }
41
42 public function __toString() {
43 return call_user_func( $this->{"__toString"}->bindTo( $this ) );
44 }
45
46 public static function load() {
47 add_action( 'after_setup_theme', array( 'Redux', 'createRedux' ));
48 add_action( 'init', array( 'Redux', 'createRedux' ));
49 add_action( 'switch_theme', array( 'Redux', 'createRedux' ));
50 }
51
52 public static function init( $opt_name = "" ) {
53 if ( ! empty( $opt_name ) ) {
54 self::loadRedux( $opt_name );
55 remove_action( 'setup_theme', array( 'Redux', 'createRedux' ) );
56 }
57 }
58
59 public static function loadExtensions( $ReduxFramework ) {
60 if ( $instanceExtensions = self::getExtensions( $ReduxFramework->args['opt_name'], "" ) ) {
61 foreach ( $instanceExtensions as $name => $extension ) {
62 if ( ! class_exists( $extension['class'] ) ) {
63 // In case you wanted override your override, hah.
64 $extension['path'] = apply_filters( 'redux/extension/' . $ReduxFramework->args['opt_name'] . '/' . $name, $extension['path'] );
65 if ( file_exists( $extension['path'] ) ) {
66 require_once $extension['path'];
67 }
68 }
69 if ( ! isset( $ReduxFramework->extensions[ $name ] ) ) {
70 if ( class_exists( $extension['class'] ) ) {
71 $ReduxFramework->extensions[ $name ] = new $extension['class']( $ReduxFramework );
72 //if (isset($ReduxFramework->extensions[ $name ]->min_redux_version)) {
73 //var_dump($ReduxFramework->extensions[ $name ]->min_redux_version);
74 //}
75
76 } else {
77 echo '<div id="message" class="error"><p>No class named <strong>' . $extension['class'] . '</strong> exists. Please verify your extension path.</p></div>';
78 }
79
80 }
81 }
82 }
83 }
84
85 public static function extensionPath( $extension, $folder = true ) {
86 if ( ! isset( Redux::$extensions[ $extension ] ) ) {
87 return;
88 }
89 $path = end( Redux::$extensions[ $extension ] );
90 if ( ! $folder ) {
91 return $path;
92 }
93
94 return str_replace( 'extension_' . $extension . '.php', '', $path );
95 }
96
97
98 public static function loadRedux( $opt_name = "" ) {
99
100 if ( empty( $opt_name ) ) {
101 return;
102 }
103
104 $check = ReduxFrameworkInstances::get_instance( $opt_name );
105
106 if ( isset( $check->apiHasRun ) ) {
107 return;
108 }
109
110 $args = self::constructArgs( $opt_name );
111 $sections = self::constructSections( $opt_name );
112 if ( ! class_exists( 'ReduxFramework' ) ) {
113 echo '<div id="message" class="error"><p>Redux Framework is <strong>not installed</strong>. Please install it.</p></div>';
114
115 return;
116 }
117
118 if ( isset( self::$uses_extensions[ $opt_name ] ) && ! empty( self::$uses_extensions[ $opt_name ] ) ) {
119 add_action( "redux/extensions/{$opt_name}/before", array( 'Redux', 'loadExtensions' ), 0 );
120 }
121
122 $redux = new ReduxFramework( $sections, $args );
123 $redux->apiHasRun = 1;
124 self::$init[ $opt_name ] = 1;
125 if ( isset( $redux->args['opt_name'] ) && $redux->args['opt_name'] != $opt_name ) {
126 self::$init[ $redux->args['opt_name'] ] = 1;
127 }
128
129 }
130
131 public static function createRedux() {
132 foreach ( self::$sections as $opt_name => $theSections ) {
133 if ( ! self::$init[ $opt_name ] ) {
134 self::loadRedux( $opt_name );
135 }
136 }
137 }
138
139 public static function constructArgs( $opt_name ) {
140 $args = isset( self::$args[ $opt_name ] ) ? self::$args[ $opt_name ] : array();
141
142 $args['opt_name'] = $opt_name;
143 if ( ! isset( $args['menu_title'] ) ) {
144 $args['menu_title'] = ucfirst( $opt_name ) . ' Options';
145 }
146 if ( ! isset( $args['page_title'] ) ) {
147 $args['page_title'] = ucfirst( $opt_name ) . ' Options';
148 }
149 if ( ! isset( $args['page_slug'] ) ) {
150 $args['page_slug'] = $opt_name . '_options';
151 }
152
153 return $args;
154 }
155
156 public static function constructSections( $opt_name ) {
157 $sections = array();
158 if ( ! isset( self::$sections[ $opt_name ] ) ) {
159 return $sections;
160
161 }
162 foreach ( self::$sections[ $opt_name ] as $section_id => $section ) {
163 $section['fields'] = self::constructFields( $opt_name, $section_id );
164 $p = $section['priority'];
165 while ( isset( $sections[ $p ] ) ) {
166 $p++;
167 }
168 $sections[ $p ] = $section;
169 }
170 ksort( $sections );
171
172 return $sections;
173 }
174
175 public static function constructFields( $opt_name = "", $section_id = "" ) {
176 $fields = array();
177 if ( ! empty( self::$fields[ $opt_name ] ) ) {
178 foreach ( self::$fields[ $opt_name ] as $key => $field ) {
179 if ( $field['section_id'] == $section_id ) {
180 $p = $field['priority'];
181 while ( isset( $fields[ $p ] ) ) {
182 echo $p ++;
183 }
184 $fields[ $p ] = $field;
185 }
186 }
187 }
188 ksort( $fields );
189
190 return $fields;
191 }
192
193 public static function getSection( $opt_name = '', $id = '' ) {
194 self::check_opt_name( $opt_name );
195 if ( ! empty( $opt_name ) && ! empty( $id ) ) {
196 if ( ! isset( self::$sections[ $opt_name ][ $id ] ) ) {
197 $id = strtolower( sanitize_html_class( $id ) );
198 }
199
200 return isset( self::$sections[ $opt_name ][ $id ] ) ? self::$sections[ $opt_name ][ $id ] : false;
201 }
202
203 return false;
204 }
205
206 public static function setSections( $opt_name = '', $sections = array() ) {
207 self::check_opt_name( $opt_name );
208 if ( ! empty( $sections ) ) {
209 foreach ( $sections as $section ) {
210 Redux::setSection( $opt_name, $section );
211 }
212 }
213 }
214
215 public static function getSections( $opt_name = '' ) {
216 self::check_opt_name( $opt_name );
217 if ( ! empty( self::$sections[ $opt_name ] ) ) {
218 return self::$sections[ $opt_name ];
219 }
220
221 return array();
222 }
223
224 public static function removeSection( $opt_name = '', $id = "", $fields = false ) {
225 if ( ! empty( $opt_name ) && ! empty( $id ) ) {
226 if ( isset( self::$sections[ $opt_name ][ $id ] ) ) {
227 $priority = '';
228
229 foreach ( self::$sections[ $opt_name ] as $key => $section ) {
230 if ( $key == $id ) {
231 $priority = $section['priority'];
232 self::$priority[ $opt_name ]['sections'] --;
233 unset( self::$sections[ $opt_name ][ $id ] );
234 continue;
235 }
236 if ( $priority != "" ) {
237 $newPriority = $section['priority'];
238 $section['priority'] = $priority;
239 self::$sections[ $opt_name ][ $key ] = $section;
240 $priority = $newPriority;
241 }
242 }
243
244 if ( isset( self::$fields[ $opt_name ] ) && ! empty( self::$fields[ $opt_name ] ) && $fields == true ) {
245 foreach ( self::$fields[ $opt_name ] as $key => $field ) {
246 if ( $field['section_id'] == $id ) {
247 unset( self::$fields[ $opt_name ][ $key ] );
248 }
249 }
250 }
251 }
252 }
253 }
254
255 public static function setSection( $opt_name = '', $section = array() ) {
256
257 self::check_opt_name( $opt_name );
258 if ( empty( $section ) ) {
259 return;
260 }
261 if ( ! isset( $section['id'] ) ) {
262 if ( isset( $section['type'] ) && $section['type'] == "divide" ) {
263 $section['id'] = time();
264 } else {
265 if ( isset( $section['title'] ) ) {
266 $section['id'] = strtolower( sanitize_title( $section['title'] ) );
267 } else {
268 $section['id'] = time();
269 }
270 }
271 if ( ! isset( $section['id'] ) ) {
272 print_r( $section );
273 echo "DOVY";
274 }
275
276 if ( isset( self::$sections[ $opt_name ][ $section['id'] ] ) ) {
277 $orig = $section['id'];
278 $i = 0;
279 while ( isset( self::$sections[ $opt_name ][ $section['id'] ] ) ) {
280 $section['id'] = $orig . '_' . $i;
281
282 $i ++;
283 }
284 }
285 }
286
287 if ( ! empty( $opt_name ) && is_array( $section ) && ! empty( $section ) ) {
288 if ( ! isset( $section['id'] ) && ! isset( $section['title'] ) ) {
289 self::$errors[ $opt_name ]['section']['missing_title'] = "Unable to create a section due to missing id and title.";
290
291 return;
292 }
293 if ( ! isset( $section['priority'] ) ) {
294 $section['priority'] = self::getPriority( $opt_name, 'sections' );
295 }
296 if ( isset( $section['fields'] ) ) {
297 if ( ! empty( $section['fields'] ) && is_array( $section['fields'] ) ) {
298 self::processFieldsArray( $opt_name, $section['id'], $section['fields'] );
299 }
300 unset( $section['fields'] );
301 }
302 self::$sections[ $opt_name ][ $section['id'] ] = $section;
303 } else {
304 self::$errors[ $opt_name ]['section']['empty'] = "Unable to create a section due an empty section array or the section variable passed was not an array.";
305
306 return;
307 }
308 }
309
310 public static function hideSection( $opt_name = '', $id = '', $hide = true ) {
311 self::check_opt_name( $opt_name );
312
313 if ( ! empty( $opt_name ) && ! empty( $id ) ) {
314 if ( isset ( self::$sections[ $opt_name ][ $id ] ) ) {
315 self::$sections[ $opt_name ][ $id ]['hidden'] = $hide;
316 }
317 }
318 }
319
320 public static function processFieldsArray( $opt_name = "", $section_id = "", $fields = array() ) {
321 if ( ! empty( $opt_name ) && ! empty( $section_id ) && is_array( $fields ) && ! empty( $fields ) ) {
322 foreach ( $fields as $field ) {
323 if ( ! is_array( $field ) ) {
324 continue;
325 }
326 $field['section_id'] = $section_id;
327 self::setField( $opt_name, $field );
328 }
329 }
330 }
331
332 public static function getField( $opt_name = '', $id = '' ) {
333 self::check_opt_name( $opt_name );
334 if ( ! empty( $opt_name ) && ! empty( $id ) ) {
335 return isset( self::$fields[ $opt_name ][ $id ] ) ? self::$fields[ $opt_name ][ $id ] : false;
336 }
337
338 return false;
339 }
340
341 public static function hideField( $opt_name = '', $id = '', $hide = true ) {
342 self::check_opt_name( $opt_name );
343
344 if ( ! empty( $opt_name ) && ! empty( $id ) ) {
345 if ( isset ( self::$fields[ $opt_name ][ $id ] ) ) {
346 if ( ! $hide ) {
347 self::$fields[ $opt_name ][ $id ]['class'] = str_replace( 'hidden', '', self::$fields[ $opt_name ][ $id ]['class'] );
348 } else {
349 self::$fields[ $opt_name ][ $id ]['class'] .= 'hidden';
350 }
351 }
352 }
353 }
354
355 public static function setField( $opt_name = '', $field = array() ) {
356 self::check_opt_name( $opt_name );
357
358 if ( ! empty( $opt_name ) && is_array( $field ) && ! empty( $field ) ) {
359
360 if ( ! isset( $field['priority'] ) ) {
361 $field['priority'] = self::getPriority( $opt_name, 'fields' );
362 }
363 if ( isset( $field['id'] ) ) {
364 self::$fields[ $opt_name ][ $field['id'] ] = $field;
365 }
366 }
367 }
368
369 public static function removeField( $opt_name = '', $id = '' ) {
370 self::check_opt_name( $opt_name );
371
372 if ( ! empty( $opt_name ) && ! empty( $id ) ) {
373 if ( isset( self::$fields[ $opt_name ][ $id ] ) ) {
374 foreach ( self::$fields[ $opt_name ] as $key => $field ) {
375 if ( $key == $id ) {
376 $priority = $field['priority'];
377 self::$priority[ $opt_name ]['fields'] --;
378 unset( self::$fields[ $opt_name ][ $id ] );
379 continue;
380 }
381 if ( isset( $priority ) && $priority != "" ) {
382 $newPriority = $field['priority'];
383 $field['priority'] = $priority;
384 self::$fields[ $opt_name ][ $key ] = $field;
385 $priority = $newPriority;
386 }
387 }
388 }
389 }
390
391 return false;
392 }
393
394 public static function setHelpTab( $opt_name = "", $tab = array() ) {
395 self::check_opt_name( $opt_name );
396 if ( ! empty( $opt_name ) && ! empty( $tab ) ) {
397 if ( ! isset( self::$args[ $opt_name ]['help_tabs'] ) ) {
398 self::$args[ $opt_name ]['help_tabs'] = array();
399 }
400 if ( isset( $tab['id'] ) ) {
401 self::$args[ $opt_name ]['help_tabs'][] = $tab;
402 } else if ( is_array( end( $tab ) ) ) {
403 foreach ( $tab as $tab_item ) {
404 self::$args[ $opt_name ]['help_tabs'][] = $tab_item;
405 }
406 }
407 }
408 }
409
410 public static function setHelpSidebar( $opt_name = "", $content = "" ) {
411 self::check_opt_name( $opt_name );
412 if ( ! empty( $opt_name ) && ! empty( $content ) ) {
413 self::$args[ $opt_name ]['help_sidebar'] = $content;
414 }
415 }
416
417 public static function setArgs( $opt_name = "", $args = array() ) {
418 self::check_opt_name( $opt_name );
419 if ( ! empty( $opt_name ) && ! empty( $args ) && is_array( $args ) ) {
420 if ( isset( self::$args[ $opt_name ] ) && isset( self::$args[ $opt_name ]['clearArgs'] ) ) {
421 self::$args[ $opt_name ] = array();
422 }
423 self::$args[ $opt_name ] = wp_parse_args( $args, self::$args[ $opt_name ] );
424 }
425 }
426
427 public static function getArgs( $opt_name = "" ) {
428 self::check_opt_name( $opt_name );
429 if ( ! empty( $opt_name ) && ! empty( self::$args[ $opt_name ] ) ) {
430 return self::$args[ $opt_name ];
431 }
432 }
433
434 public static function getArg( $opt_name = "", $key = "" ) {
435 self::check_opt_name( $opt_name );
436 if ( ! empty( $opt_name ) && ! empty( $key ) && ! empty( self::$args[ $opt_name ] ) ) {
437 return self::$args[ $opt_name ][ $key ];
438 } else {
439 return;
440 }
441 }
442
443 public static function getOption( $opt_name = "", $key = "" ) {
444 self::check_opt_name( $opt_name );
445
446 if ( ! empty( $opt_name ) && ! empty( $key ) ) {
447 $redux = get_option( $opt_name );
448
449 if ( isset( $redux[ $key ] ) ) {
450 return $redux[ $key ];
451 } else {
452 return;
453 }
454 } else {
455 return;
456 }
457 }
458
459 public static function setOption( $opt_name = "", $key = "", $option = "" ) {
460 self::check_opt_name( $opt_name );
461
462 if ( ! empty( $opt_name ) && ! empty( $key ) ) {
463 $redux = get_option( $opt_name );
464 $redux[ $key ] = $option;
465
466 return update_option( $opt_name, $redux );
467 } else {
468 return false;
469 }
470 }
471
472 public static function getPriority( $opt_name, $type ) {
473 $priority = self::$priority[ $opt_name ][ $type ];
474 self::$priority[ $opt_name ][ $type ] += 1;
475
476 return $priority;
477 }
478
479 public static function check_opt_name( $opt_name = "" ) {
480 if ( empty( $opt_name ) || is_array( $opt_name ) ) {
481 return;
482 }
483 if ( ! isset( self::$sections[ $opt_name ] ) ) {
484 self::$sections[ $opt_name ] = array();
485 self::$priority[ $opt_name ]['sections'] = 1;
486 }
487 if ( ! isset( self::$args[ $opt_name ] ) ) {
488 self::$args[ $opt_name ] = array();
489 self::$priority[ $opt_name ]['args'] = 1;
490 }
491 if ( ! isset( self::$fields[ $opt_name ] ) ) {
492 self::$fields[ $opt_name ] = array();
493 self::$priority[ $opt_name ]['fields'] = 1;
494 }
495 if ( ! isset( self::$help[ $opt_name ] ) ) {
496 self::$help[ $opt_name ] = array();
497 self::$priority[ $opt_name ]['help'] = 1;
498 }
499 if ( ! isset( self::$errors[ $opt_name ] ) ) {
500 self::$errors[ $opt_name ] = array();
501 }
502 if ( ! isset( self::$init[ $opt_name ] ) ) {
503 self::$init[ $opt_name ] = false;
504 }
505 }
506
507 /**
508 * Retrieve metadata from a file. Based on WP Core's get_file_data function
509 *
510 * @since 2.1.1
511 *
512 * @param string $file Path to the file
513 *
514 * @return string
515 */
516 public static function getFileVersion( $file ) {
517 $data = get_file_data( $file, array( 'version' ), 'plugin' );
518
519 return $data[0];
520 }
521
522 public static function checkExtensionClassFile( $opt_name, $name = "", $class_file = "", $instance = "" ) {
523 if ( file_exists( $class_file ) ) {
524
525 self::$uses_extensions[ $opt_name ] = isset( self::$uses_extensions[ $opt_name ] ) ? self::$uses_extensions[ $opt_name ] : array();
526 if ( ! in_array( $name, self::$uses_extensions[ $opt_name ] ) ) {
527 self::$uses_extensions[ $opt_name ][] = $name;
528 }
529
530 self::$extensions[ $name ] = isset( self::$extensions[ $name ] ) ? self::$extensions[ $name ] : array();
531 $version = Redux_Helpers::get_template_version( $class_file );
532 if ( empty( $version ) && ! empty( $instance ) ) {
533 if ( isset( $instance->version ) ) {
534 $version = $instance->version;
535 }
536 }
537 self::$extensions[ $name ][ $version ] = isset( self::$extensions[ $name ][ $version ] ) ? self::$extensions[ $name ][ $version ] : $class_file;
538
539 $api_check = str_replace( 'extension_' . $name, $name . '_api', $class_file );
540 if ( file_exists( $api_check ) && ! class_exists( 'Redux_' . ucfirst( $name ) ) ) {
541 include_once( $api_check );
542 }
543 }
544 }
545
546 public static function setExtensions( $opt_name, $path ) {
547 if ( is_dir( $path ) ) {
548 $path = trailingslashit( $path );
549 $folder = str_replace( '.php', '', basename( $path ) );
550 if ( file_exists( $path . 'extension_' . $folder . '.php' ) ) {
551 self::checkExtensionClassFile( $opt_name, $folder, $path . 'extension_' . $folder . '.php' );
552 } else {
553 $folders = scandir( $path, 1 );
554 foreach ( $folders as $folder ) {
555 if ( $folder === '.' or $folder === '..' or $folder[0] == "." ) {
556 continue;
557 }
558 if ( file_exists( $path . $folder . '/extension_' . $folder . '.php' ) ) {
559 self::checkExtensionClassFile( $opt_name, $folder, $path . $folder . '/extension_' . $folder . '.php' );
560 } else if ( is_dir( $path . $folder ) ) {
561 self::setExtensions( $opt_name, $path . $folder );
562 continue;
563 }
564 }
565 }
566 } else if ( file_exists( $path ) ) {
567 $name = explode( 'extension_', basename( $path ) );
568 if ( isset( $name[1] ) && ! empty( $name[1] ) ) {
569 $name = str_replace( '.php', '', $name[1] );
570 self::checkExtensionClassFile( $opt_name, $name, $path );
571 }
572 }
573 }
574
575 public static function getAllExtensions() {
576 $redux = ReduxFrameworkInstances::get_all_instances();
577 foreach ( $redux as $instance ) {
578 if ( ! empty( self::$uses_extensions[ $instance['args']['opt_name'] ] ) ) {
579 continue;
580 }
581 if ( ! empty( $instance['extensions'] ) ) {
582
583 Redux::getInstanceExtensions( $instance['args']['opt_name'], $instance );
584 }
585 }
586 }
587
588 public static function getInstanceExtensions( $opt_name, $instance = array() ) {
589 if ( ! empty( self::$uses_extensions[ $opt_name ] ) ) {
590 return;
591 }
592 if ( empty( $instance ) ) {
593 $instance = ReduxFrameworkInstances::get_instance( $opt_name );
594 }
595 if ( empty( $instance ) || empty( $instance->extensions ) ) {
596 return;
597 }
598 foreach ( $instance->extensions as $name => $extension ) {
599 if ( $name == "widget_areas" ) {
600 $new = new Redux_Widget_Areas( $instance );
601 }
602 if ( isset( self::$uses_extensions[ $opt_name ][ $name ] ) ) {
603 continue;
604 }
605 if ( isset( $extension->extension_dir ) ) {
606 Redux::setExtensions( $opt_name, str_replace( $name, '', $extension->extension_dir ) );
607
608 } else if ( isset( $extension->_extension_dir ) ) {
609 Redux::setExtensions( $opt_name, str_replace( $name, '', $extension->_extension_dir ) );
610 }
611 }
612 }
613
614 public static function getExtensions( $opt_name = "", $key = "" ) {
615
616 if ( empty( $opt_name ) ) {
617 Redux::getAllExtensions();
618 if ( empty( $key ) ) {
619 return self::$extension_paths;
620 } else {
621 if ( isset( self::$extension_paths[ $key ] ) ) {
622 return self::$extension_paths[ $key ];
623 }
624 }
625 } else {
626 if ( empty( self::$uses_extensions[ $opt_name ] ) ) {
627 Redux::getInstanceExtensions( $opt_name );
628 }
629
630 if ( empty( self::$uses_extensions[ $opt_name ] ) ) {
631 return false;
632 }
633 $instanceExtensions = array();
634 foreach ( self::$uses_extensions[ $opt_name ] as $extension ) {
635 $class_file = end( self::$extensions[ $extension ] );
636 $name = str_replace( '.php', '', basename( $extension ) );
637 $extension_class = 'ReduxFramework_Extension_' . $name;
638 $instanceExtensions[ $extension ] = array(
639 'path' => $class_file,
640 'class' => $extension_class,
641 'version' => Redux_Helpers::get_template_version( $class_file )
642 );
643 }
644
645 return $instanceExtensions;
646 }
647
648 return false;
649 }
650 }
651
652 Redux::load();
653 }
654