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