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