PluginProbe ʕ •ᴥ•ʔ
Pods – Custom Content Types and Fields / 3.3.7
Pods – Custom Content Types and Fields v3.3.7
trunk 1.14.8 2.7.31.3 2.8.23.3 2.9.19.3 3.0.10.3 3.1.4.1 3.2.0 3.2.1 3.2.1.1 3.2.2 3.2.4 3.2.5 3.2.6 3.2.7 3.2.7.1 3.2.8 3.2.8.1 3.2.8.2 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9
pods / classes / PodsComponents.php
pods / classes Last commit date
cli 4 months ago fields 4 months ago widgets 4 months ago Pods.php 4 months ago PodsAPI.php 4 months ago PodsAdmin.php 4 months ago PodsArray.php 4 months ago PodsComponent.php 4 months ago PodsComponents.php 4 months ago PodsData.php 4 months ago PodsField.php 4 months ago PodsForm.php 4 months ago PodsI18n.php 4 months ago PodsInit.php 4 months ago PodsMeta.php 4 months ago PodsMigrate.php 4 months ago PodsRESTFields.php 4 months ago PodsRESTHandlers.php 4 months ago PodsTermSplitting.php 4 months ago PodsUI.php 4 months ago PodsView.php 4 months ago
PodsComponents.php
876 lines
1 <?php
2
3 // Don't load directly.
4 if ( ! defined( 'ABSPATH' ) ) {
5 die( '-1' );
6 }
7
8 /**
9 * Component managing class
10 *
11 * @package Pods
12 */
13 class PodsComponents {
14
15 /**
16 * @var PodsComponents
17 */
18 public static $instance = null;
19
20 /**
21 * Root of Components directory
22 *
23 * @var string
24 *
25 * @private
26 * @since 2.0.0
27 */
28 private $components_dir = null;
29
30 /**
31 * Available components
32 *
33 * @var array
34 *
35 * @since 2.0.0
36 */
37 public $components = [];
38
39 /**
40 * Registered component menu items.
41 *
42 * @var array
43 *
44 * @since 2.9.8
45 */
46 public $components_menu_items = [];
47
48 /**
49 * Components settings
50 *
51 * @var array
52 *
53 * @since 2.0.0
54 */
55 public $settings = [];
56
57 /**
58 * Singleton handling for a basic pods_components() request
59 *
60 * @return \PodsComponents
61 *
62 * @since 2.3.5
63 */
64 public static function init() {
65
66 if ( ! is_object( self::$instance ) ) {
67 self::$instance = new self();
68 }
69
70 return self::$instance;
71 }
72
73 /**
74 * Setup actions and get options
75 *
76 * @since 2.0.0
77 */
78 public function __construct() {
79
80 $this->components_dir = realpath( apply_filters( 'pods_components_dir', PODS_DIR . 'components' ) ) . '/';
81
82 $settings = get_option( 'pods_component_settings', '' );
83
84 if ( ! empty( $settings ) ) {
85 $this->settings = (array) json_decode( $settings, true );
86 }
87
88 if ( ! isset( $this->settings['components'] ) ) {
89 $this->settings['components'] = array();
90 }
91
92 // Get components (give it access to theme)
93 add_action( 'setup_theme', array( $this, 'get_components' ), 11 );
94
95 // Load in components
96 add_action( 'setup_theme', array( $this, 'load' ), 12 );
97
98 // AJAX handling
99 if ( is_admin() ) {
100 add_action( 'wp_ajax_pods_admin_components', array( $this, 'admin_ajax' ) );
101 add_action( 'wp_ajax_nopriv_pods_admin_components', array( $this, 'admin_ajax' ) );
102
103 // Add the Pods Components capabilities
104 add_filter( 'members_get_capabilities', array( $this, 'admin_capabilities' ) );
105 }
106 }
107
108 /**
109 * Add menu item
110 *
111 * @param string $parent The parent slug.
112 *
113 * @since 2.0.0
114 *
115 * @uses add_submenu_page
116 */
117 public function menu( $parent ) {
118
119 global $submenu;
120
121 $custom_component_menus = array();
122
123 $pods_component_menu_items = array();
124
125 foreach ( $this->components as $component => $component_data ) {
126 $component_id = $component_data['ID'];
127
128 $component_data['MustUse'] = apply_filters( "pods_component_require_{$component_id}", $component_data['MustUse'], $component_data );
129
130 if ( empty( $component_data['MustUse'] ) && ( ! isset( $this->settings['components'][ $component ] ) || 0 === $this->settings['components'][ $component ] ) ) {
131 continue;
132 }
133
134 if ( ! empty( $component_data['Hide'] ) ) {
135 continue;
136 }
137
138 if ( ! empty( $component_data['DeveloperMode'] ) && ! pods_developer() ) {
139 continue;
140 }
141
142 if ( empty( $component_data['TablelessMode'] ) && pods_tableless() ) {
143 continue;
144 }
145
146 if ( empty( $component_data['MenuPage'] ) ) {
147 if ( ! isset( $component_data['object'] ) ) {
148 continue;
149 } elseif ( ! method_exists( $component_data['object'], 'admin' ) && ! method_exists( $component_data['object'], 'options' ) ) {
150 continue;
151 }
152 }
153
154 if ( false === $component_data['External'] ) {
155 $component_data['File'] = realpath( $this->components_dir . $component_data['File'] );
156 }
157
158 if ( ! file_exists( $component_data['File'] ) ) {
159 pods_message( 'Pods Component not found: ' . $component_data['File'] );
160
161 pods_transient_clear( 'pods_components' );
162
163 continue;
164 }
165
166 $capability = 'pods_component_' . str_replace( '-', '_', sanitize_title( $component ) );
167
168 if ( 0 < strlen( (string) $component_data['Capability'] ) ) {
169 $capability = $component_data['Capability'];
170 }
171
172 if ( ! pods_is_admin( array( 'pods', 'pods_components', $capability ) ) ) {
173 continue;
174 }
175
176 $menu_page = 'pods-component-' . $component;
177
178 if ( ! empty( $component_data['MenuPage'] ) ) {
179 $custom_component_menus[ $menu_page ] = $component_data;
180 }
181
182 $pods_component_menu_items[ $component_data['MenuName'] ] = array(
183 'menu_page' => $menu_page,
184 'page_title' => $component_data['Name'],
185 'capability' => 'read',
186 'callback' => array( $this, 'admin_handler' ),
187 );
188
189 if ( isset( $component_data['object'] ) && method_exists( $component_data['object'], 'admin_assets' ) ) {
190 $pods_component_menu_items[ $component_data['MenuName'] ]['assets'] = array(
191 $component_data['object'],
192 'admin_assets',
193 );
194 }
195 }//end foreach
196
197 /**
198 * Add or change the items in the Pods Components Submenu.
199 *
200 * Can also be used to change which menu components is a submenu of or change title of menu.
201 *
202 * @param array $pods_component_menu_items {
203 * An array of arguments for add_submenu_page
204 *
205 * @type string $parent_slug The slug name for the parent menu (or the file name of a standard WordPress admin page)
206 * @type string $page_title The text to be displayed in the title tags of the page when the menu is selected
207 * @type string $menu_title The text to be used for the menu
208 * @type string $capability The capability required for this menu to be displayed to the user.
209 * @type string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
210 * @type string $function The function to be called to output the content for this page.
211 * }
212 *
213 * @since 2.4.1
214 */
215 $pods_component_menu_items = apply_filters( 'pods_admin_components_menu', $pods_component_menu_items );
216
217 ksort( $pods_component_menu_items );
218
219 $this->components_menu_items = $pods_component_menu_items;
220
221 foreach ( $pods_component_menu_items as $menu_title => $menu_data ) {
222 if (
223 (
224 '' !== $menu_data['callback']
225 || false === strpos( (string) $menu_data['menu_page'], '.php' )
226 )
227 && ! is_callable( $menu_data['callback'] )
228 ) {
229 continue;
230 }
231
232 $page = add_submenu_page( $parent, wp_strip_all_tags( $menu_data['page_title'] ), '- ' . wp_strip_all_tags( $menu_title ), pods_v( 'capability', $menu_data, 'read', true ), $menu_data['menu_page'], $menu_data['callback'] );
233
234 if ( isset( $menu_data['assets'] ) && is_callable( $menu_data['assets'] ) ) {
235 add_action( 'admin_print_styles-' . $page, $menu_data['assets'] );
236 }
237 }
238
239 if ( ! empty( $custom_component_menus ) ) {
240 foreach ( $custom_component_menus as $menu_page => $component_data ) {
241 if ( isset( $submenu[ $parent ] ) ) {
242 foreach ( $submenu[ $parent ] as $sub => &$menu ) {
243 if ( $menu[2] === $menu_page ) {
244 $menu_page = $component_data['MenuPage'];
245
246 $menu[2] = $menu_page;
247
248 $page = current( explode( '?', $menu[2] ) );
249
250 if ( isset( $component_data['object'] ) && method_exists( $component_data['object'], 'admin_assets' ) ) {
251 add_action(
252 'admin_print_styles-' . $page, array(
253 $component_data['object'],
254 'admin_assets',
255 )
256 );
257 }
258
259 break;
260 }//end if
261 }//end foreach
262 }//end if
263 }//end foreach
264 }//end if
265 }
266
267 /**
268 * Load activated components and init component
269 *
270 * @since 2.0.0
271 */
272 public function load() {
273
274 do_action( 'pods_components_load' );
275
276 foreach ( (array) $this->components as $component => $component_data ) {
277 $component_id = $component_data['ID'];
278
279 $component_data['MustUse'] = apply_filters( "pods_component_require_{$component_id}", $component_data['MustUse'], $component_data );
280
281 if ( false === $component_data['MustUse'] && ( ! isset( $this->settings['components'][ $component ] ) || 0 === $this->settings['components'][ $component ] ) ) {
282 continue;
283 }
284
285 if ( ! empty( $component_data['PluginDependency'] ) ) {
286 $dependency = explode( '|', $component_data['PluginDependency'] );
287
288 if ( ! pods_is_plugin_active( $dependency[1] ) ) {
289 continue;
290 }
291 }
292
293 if ( ! empty( $component_data['ThemeDependency'] ) ) {
294 $dependency = explode( '|', $component_data['ThemeDependency'] );
295
296 $check = strtolower( $dependency[1] );
297
298 if ( strtolower( get_template() ) !== $check && strtolower( get_stylesheet() ) !== $check ) {
299 continue;
300 }
301 }
302
303 if ( false === $component_data['External'] ) {
304 $component_data['File'] = realpath( $this->components_dir . $component_data['File'] );
305 }
306
307 if ( empty( $component_data['File'] ) ) {
308 pods_transient_clear( 'pods_components' );
309
310 continue;
311 }
312
313 if ( ! file_exists( $component_data['File'] ) ) {
314 pods_message( 'Pods Component not found: ' . $component_data['File'] );
315
316 pods_transient_clear( 'pods_components' );
317
318 continue;
319 }
320
321 include_once $component_data['File'];
322
323 if ( ( ! empty( $component_data['Class'] ) && class_exists( $component_data['Class'] ) ) || isset( $component_data['object'] ) ) {
324 if ( ! isset( $this->components[ $component ]['object'] ) ) {
325 $this->components[ $component ]['object'] = new $component_data['Class']();
326 }
327
328 if ( method_exists( $this->components[ $component ]['object'], 'options' ) ) {
329 if ( isset( $this->settings['components'][ $component ] ) ) {
330 $this->components[ $component ]['options'] = $this->components[ $component ]['object']->options( $this->settings['components'][ $component ] );
331 } else {
332 $this->components[ $component ]['options'] = $this->components[ $component ]['object']->options( array() );
333 }
334
335 $this->options( $component, $this->components[ $component ]['options'] );
336 } else {
337 $this->options( $component, array() );
338 }
339
340 if ( method_exists( $this->components[ $component ]['object'], 'handler' ) ) {
341 $this->components[ $component ]['object']->handler( $this->settings['components'][ $component ] );
342 }
343 }//end if
344 }//end foreach
345 }
346
347 /**
348 * Get list of components available
349 *
350 * @since 2.0.0
351 */
352 public function get_components() {
353
354 $components = pods_transient_get( 'pods_components' );
355
356 if ( 1 === (int) pods_v( 'pods_debug_components', 'get', 0 ) && pods_is_admin( array( 'pods' ) ) ) {
357 $components = array();
358 }
359
360 if ( PODS_VERSION !== PodsInit::$version || ! is_array( $components ) || empty( $components ) || ( is_admin() && 'pods-components' === pods_v( 'page' ) && 1 !== (int) pods_transient_get( 'pods_components_refresh' ) ) ) {
361 do_action( 'pods_components_get' );
362
363 // @codingStandardsIgnoreLine
364 $component_dir = @opendir( untrailingslashit( $this->components_dir ) );
365 $component_files = array();
366
367 if ( false !== $component_dir ) {
368 $file = readdir( $component_dir );
369
370 while ( false !== $file ) {
371 if ( '.' === substr( $file, 0, 1 ) ) {
372 // Get next file
373 $file = readdir( $component_dir );
374
375 continue;
376 } elseif ( is_dir( $this->components_dir . $file ) ) {
377 // @codingStandardsIgnoreLine
378 $component_subdir = @opendir( $this->components_dir . $file );
379
380 if ( $component_subdir ) {
381 $subfile = readdir( $component_subdir );
382
383 while ( false !== $subfile ) {
384 if ( '.' === substr( $subfile, 0, 1 ) ) {
385 // Get next file
386 $subfile = readdir( $component_subdir );
387
388 continue;
389 } elseif ( '.php' === substr( $subfile, - 4 ) ) {
390 $component_files[] = str_replace( '\\', '/', $file . '/' . $subfile );
391 }
392
393 // Get next file
394 $subfile = readdir( $component_subdir );
395 }
396
397 closedir( $component_subdir );
398 }
399 } elseif ( '.php' === substr( $file, - 4 ) ) {
400 $component_files[] = $file;
401 }//end if
402
403 // Get next file
404 $file = readdir( $component_dir );
405 }//end while
406
407 closedir( $component_dir );
408 }//end if
409
410 $default_headers = [
411 'ID' => 'ID',
412 'Name' => 'Name',
413 'ShortName' => 'Short Name',
414 'PluginName' => 'Plugin Name',
415 'ComponentName' => 'Component Name',
416 'URI' => 'URI',
417 'MenuName' => 'Menu Name',
418 'MenuPage' => 'Menu Page',
419 'MenuAddPage' => 'Menu Add Page',
420 'MustUse' => 'Must Use',
421 'Description' => 'Description',
422 'Deprecated' => 'Deprecated',
423 'DeprecatedInVersion' => 'Deprecated In Version',
424 'DeprecatedRemovalVersion' => 'Deprecated Removal Version',
425 'Version' => 'Version',
426 'Category' => 'Category',
427 'Author' => 'Author',
428 'AuthorURI' => 'Author URI',
429 'Class' => 'Class',
430 'Hide' => 'Hide',
431 'PluginDependency' => 'Plugin Dependency',
432 'ThemeDependency' => 'Theme Dependency',
433 'DeveloperMode' => 'Developer Mode',
434 'TablelessMode' => 'Tableless Mode',
435 'Capability' => 'Capability',
436 'Plugin' => 'Plugin',
437 ];
438
439 $component_files = apply_filters( 'pods_components_register', $component_files );
440
441 $components = array();
442
443 foreach ( $component_files as $component_file ) {
444 $external = false;
445
446 if ( is_array( $component_file ) && isset( $component_file['File'] ) ) {
447 $component_file = $component_file['File'];
448 $component = $component_file;
449
450 $external = true;
451 } else {
452 $component = $this->components_dir . $component_file;
453 }
454
455 if ( ! is_readable( $component ) ) {
456 continue;
457 }
458
459 $component_data = get_file_data( $component, $default_headers, 'pods_component' );
460
461 if ( ( empty( $component_data['Name'] ) && empty( $component_data['ComponentName'] ) && empty( $component_data['PluginName'] ) ) || 'yes' === $component_data['Hide'] ) {
462 continue;
463 }
464
465 if ( isset( $component_data['Plugin'] ) && pods_is_plugin_active( $component_data['Plugin'] ) ) {
466 continue;
467 }
468
469 if ( empty( $component_data['Name'] ) ) {
470 if ( ! empty( $component_data['ComponentName'] ) ) {
471 $component_data['Name'] = $component_data['ComponentName'];
472 } elseif ( ! empty( $component_data['PluginName'] ) ) {
473 $component_data['Name'] = $component_data['PluginName'];
474 }
475 }
476
477 if ( empty( $component_data['ShortName'] ) ) {
478 $component_data['ShortName'] = $component_data['Name'];
479 }
480
481 if ( empty( $component_data['MenuName'] ) ) {
482 $component_data['MenuName'] = $component_data['Name'];
483 }
484
485 if ( empty( $component_data['Class'] ) ) {
486 $component_data['Class'] = 'Pods_' . pods_js_name( basename( $component, '.php' ), false );
487 }
488
489 if ( empty( $component_data['ID'] ) ) {
490 $component_data['ID'] = $component_data['Name'];
491 }
492
493 $component_data['ID'] = sanitize_title( $component_data['ID'] );
494
495 if ( 'on' === strtolower( $component_data['DeveloperMode'] ) && '1' === $component_data['DeveloperMode'] ) {
496 $component_data['DeveloperMode'] = true;
497 } else {
498 $component_data['DeveloperMode'] = false;
499 }
500
501 if ( 'on' === strtolower( $component_data['TablelessMode'] ) && '1' === $component_data['TablelessMode'] ) {
502 $component_data['TablelessMode'] = true;
503 } else {
504 $component_data['TablelessMode'] = false;
505 }
506
507 $component_data['External'] = $external;
508
509 if ( 'on' === strtolower( $component_data['MustUse'] ) || '1' === $component_data['MustUse'] ) {
510 $component_data['MustUse'] = true;
511 } elseif ( 'off' === strtolower( $component_data['MustUse'] ) || '0' === $component_data['MustUse'] ) {
512 $component_data['MustUse'] = false;
513 } else {
514 $component_data['MustUse'] = $component_data['External'];
515 }
516
517 $component_data['File'] = $component_file;
518
519 $components[ $component_data['ID'] ] = $component_data;
520 }//end foreach
521
522 ksort( $components );
523
524 pods_transient_set( 'pods_components_refresh', 1, HOUR_IN_SECONDS * 12 );
525
526 pods_transient_set( 'pods_components', $components, WEEK_IN_SECONDS );
527 }//end if
528
529 if ( is_admin() ) {
530 if ( 1 === (int) pods_v( 'pods_debug_components', 'get', 0 ) && pods_is_admin( [ 'pods' ] ) ) {
531 pods_debug( __METHOD__ . ':' . __LINE__ );
532 pods_debug( $components );
533 }
534
535 pods_debug_log_data( $components, 'components', __METHOD__, __LINE__ );
536 }
537
538 $this->components = $components;
539
540 return $this->components;
541 }
542
543 /**
544 * Set component options
545 *
546 * @param string $component Component name.
547 * @param array $options Component options.
548 *
549 * @since 2.0.0
550 */
551 public function options( $component, $options ) {
552
553 if ( ! isset( $this->settings['components'][ $component ] ) || ! is_array( $this->settings['components'][ $component ] ) ) {
554 $this->settings['components'][ $component ] = array();
555 }
556
557 foreach ( $options as $option => $data ) {
558 if ( ! isset( $this->settings['components'][ $component ][ $option ] ) && isset( $data['default'] ) ) {
559 $this->settings['components'][ $component ][ $option ] = $data['default'];
560 }
561 }
562 }
563
564 /**
565 * Call component specific admin functions
566 *
567 * @since 2.0.0
568 */
569 public function admin_handler() {
570 $component = str_replace( 'pods-component-', '', pods_v_sanitized( 'page' ) );
571
572 if ( isset( $this->components[ $component ] ) && isset( $this->components[ $component ]['object'] ) && is_object( $this->components[ $component ]['object'] ) ) {
573 // Component init
574 if ( method_exists( $this->components[ $component ]['object'], 'init' ) ) {
575 $this->components[ $component ]['object']->init( $this->settings['components'][ $component ], $component );
576 }
577
578 if ( method_exists( $this->components[ $component ]['object'], 'admin' ) ) {
579 // Component Admin handler
580 $this->components[ $component ]['object']->admin( $this->settings['components'][ $component ], $component );
581 } elseif ( method_exists( $this->components[ $component ]['object'], 'options' ) ) {
582 // Built-in Admin Handler
583 $this->admin( $this->components[ $component ]['object']->options( $this->settings['components'][ $component ] ), $this->settings['components'][ $component ], $component );
584 }
585 }
586 }
587
588 /**
589 * Render components admin.
590 *
591 * @param array $options Component options.
592 * @param array $settings Component setting values.
593 * @param string $component Component name.
594 */
595 public function admin( $options, $settings, $component ) {
596
597 if ( ! isset( $this->components[ $component ] ) ) {
598 wp_die( 'Invalid Component', '', array( 'back_link' => true ) );
599 }
600
601 $component_label = $this->components[ $component ]['Name'];
602
603 include PODS_DIR . 'ui/admin/components-admin.php';
604 }
605
606 /**
607 * Check if a component is active or not
608 *
609 * @param string $component The component name to check if active.
610 *
611 * @return bool
612 *
613 * @since 2.7.0
614 */
615 public function is_component_active( $component ) {
616
617 $active = false;
618
619 if ( isset( $this->components[ $component ] ) && isset( $this->settings['components'][ $component ] ) && 0 !== $this->settings['components'][ $component ] ) {
620 $active = true;
621 }
622
623 return $active;
624
625 }
626
627 /**
628 * Activate a component
629 *
630 * @param string $component The component name to activate.
631 *
632 * @return boolean Whether the component was activated.
633 *
634 * @since 2.7.0
635 */
636 public function activate_component( $component ) {
637
638 $activated = false;
639
640 if ( ! $this->is_component_active( $component ) ) {
641 if ( empty( $this->components ) ) {
642 // Setup components
643 PodsInit::$components->get_components();
644 }
645
646 if ( isset( $this->components[ $component ] ) ) {
647 $this->settings['components'][ $component ] = array();
648
649 $this->update_settings( $this->settings );
650
651 $activated = true;
652 }
653 } else {
654 $activated = true;
655 }//end if
656
657 return $activated;
658
659 }
660
661 /**
662 * Deactivate a component
663 *
664 * @param string $component The component name to deactivate.
665 *
666 * @since 2.7.0
667 */
668 public function deactivate_component( $component ) {
669
670 if ( $this->is_component_active( $component ) ) {
671 if ( isset( $this->components[ $component ] ) ) {
672 $this->settings['components'][ $component ] = 0;
673
674 $this->update_settings( $this->settings );
675 }
676 }
677
678 }
679
680 /**
681 * Toggle a component on or off
682 *
683 * @param string $component The component name to toggle.
684 * @param boolean $toggle_mode Force toggle on or off.
685 *
686 * @return bool
687 *
688 * @since 2.0.0
689 */
690 public function toggle( $component, $toggle_mode = false ) {
691
692 $toggled = null;
693
694 $toggle_mode = (boolean) pods_v( 'toggle', 'get', $toggle_mode );
695
696 if ( $toggle_mode ) {
697 $toggled = $this->activate_component( $component );
698 } else {
699 $this->deactivate_component( $component );
700
701 $toggled = false;
702 }
703
704 return $toggled;
705
706 }
707
708 /**
709 * Add pods specific capabilities.
710 *
711 * @param array $capabilities List of extra capabilities to add.
712 *
713 * @return array
714 */
715 public function admin_capabilities( $capabilities ) {
716
717 foreach ( $this->components as $component => $component_data ) {
718 if ( ! empty( $component_data['Hide'] ) ) {
719 continue;
720 }
721
722 if ( ! pods_developer() ) {
723 if ( true === (boolean) pods_v( 'DeveloperMode', $component_data, false ) ) {
724 continue;
725 }
726
727 if ( true === (boolean) pods_v( 'TablelessMode', $component_data, false ) ) {
728 continue;
729 }
730 }
731
732 if ( empty( $component_data['MenuPage'] ) && ( ! isset( $component_data['object'] ) || ! method_exists( $component_data['object'], 'admin' ) ) ) {
733 continue;
734 }
735
736 $capability = 'pods_component_' . str_replace( '-', '_', sanitize_title( str_replace( ' and ', ' ', wp_strip_all_tags( $component_data['Name'] ) ) ) );
737
738 if ( 0 < strlen( (string) $component_data['Capability'] ) ) {
739 $capability = $component_data['Capability'];
740 }
741
742 if ( ! in_array( $capability, $capabilities, true ) ) {
743 $capabilities[] = $capability;
744 }
745 }//end foreach
746
747 return $capabilities;
748 }
749
750 /**
751 * Handle admin ajax
752 *
753 * @since 2.0.0
754 */
755 public function admin_ajax() {
756
757 if ( false === headers_sent() ) {
758 pods_session_start();
759
760 header( 'Content-Type: text/html; charset=' . get_bloginfo( 'charset' ) );
761 }
762
763 // Sanitize input @codingStandardsIgnoreLine
764 $params = pods_unslash( (array) $_POST );
765
766 foreach ( $params as $key => $value ) {
767 if ( 'action' === $key ) {
768 continue;
769 }
770
771 unset( $params[ $key ] );
772
773 $params[ str_replace( '_podsfix_', '', $key ) ] = $value;
774 }
775
776 $params = (object) $params;
777
778 $component = $params->component;
779 $method = $params->method;
780
781 if ( ! isset( $component ) || ! isset( $this->components[ $component ] ) || ! isset( $this->settings['components'][ $component ] ) ) {
782 pods_error( __( 'Invalid AJAX request', 'pods' ), $this );
783 }
784
785 if ( ! isset( $params->_wpnonce ) || false === wp_verify_nonce( $params->_wpnonce, 'pods-component-' . $component . '-' . $method ) ) {
786 pods_error( __( 'Unauthorized request', 'pods' ), $this );
787 }
788
789 // Cleaning up $params
790 unset( $params->action, $params->component, $params->method, $params->_wpnonce );
791
792 $params = (object) apply_filters( "pods_component_ajax_{$component}_{$method}", $params, $component, $method );
793
794 $output = false;
795
796 // Component init
797 if ( isset( $this->components[ $component ]['object'] ) && method_exists( $this->components[ $component ]['object'], 'init' ) ) {
798 $this->components[ $component ]['object']->init( $this->settings['components'][ $component ], $component );
799 }
800
801 if ( isset( $this->components[ $component ]['object'] ) && ! method_exists( $this->components[ $component ]['object'], 'ajax_' . $method ) && method_exists( $this, 'admin_ajax_' . $method ) ) {
802 // Handle internal methods
803 $output = call_user_func( array( $this, 'admin_ajax_' . $method ), $component, $params );
804 } elseif ( ! isset( $this->components[ $component ]['object'] ) || ! method_exists( $this->components[ $component ]['object'], 'ajax_' . $method ) ) {
805 // Make sure method exists
806 pods_error( __( 'API method does not exist', 'pods' ), $this );
807 } else {
808 // Dynamically call the component method
809 $output = call_user_func( array( $this->components[ $component ]['object'], 'ajax_' . $method ), $params );
810 }
811
812 if ( ! is_bool( $output ) ) {
813 // @codingStandardsIgnoreLine
814 echo $output;
815 }
816
817 die();
818 // KBAI!
819 }
820
821 /**
822 * Handle admin AJAX settings saving.
823 *
824 * @param string $component Component name.
825 * @param array $params AJAX parameters.
826 *
827 * @return string
828 */
829 public function admin_ajax_settings( $component, $params ) {
830
831 if ( ! isset( $this->components[ $component ] ) ) {
832 wp_die( 'Invalid Component', '', array( 'back_link' => true ) );
833 } elseif ( ! method_exists( $this->components[ $component ]['object'], 'options' ) ) {
834 pods_error( __( 'Component options method does not exist', 'pods' ), $this );
835 }
836
837 $options = $this->components[ $component ]['object']->options( $this->settings['components'][ $component ] );
838
839 if ( empty( $this->settings['components'][ $component ] ) ) {
840 $this->settings['components'][ $component ] = array();
841 }
842
843 foreach ( $options as $field_name => $field_option ) {
844 $field_option = PodsForm::field_setup( $field_option, null, $field_option['type'] );
845
846 if ( ! is_array( $field_option['group'] ) ) {
847 $field_value = pods_v( 'pods_setting_' . $field_name, $params );
848
849 $this->settings['components'][ $component ][ $field_name ] = $field_value;
850 } else {
851 foreach ( $field_option['group'] as $field_group_name => $field_group_option ) {
852 $field_value = pods_v( 'pods_setting_' . $field_group_name, $params );
853
854 $this->settings['components'][ $component ][ $field_group_name ] = $field_value;
855 }
856 }
857 }
858
859 $this->update_settings( $this->settings );
860
861 return '1';
862 }
863
864 /**
865 * Update settings.
866 *
867 * @param array $settings Component settings.
868 */
869 public function update_settings( $settings ) {
870 $settings = wp_json_encode( $settings, JSON_UNESCAPED_UNICODE );
871
872 update_option( 'pods_component_settings', $settings );
873
874 }
875 }
876