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