PodsComponents

Component managing class.

Defined (1)

The class is defined in the following location(s).

/classes/PodsComponents.php  
  1. class PodsComponents { 
  2.  
  3. /** 
  4. * @var PodsComponents 
  5. */ 
  6. static $instance = null; 
  7.  
  8. /** 
  9. * Root of Components directory 
  10. * @var string 
  11. * @private 
  12. * @since 2.0 
  13. */ 
  14. private $components_dir = null; 
  15.  
  16. /** 
  17. * Available components 
  18. * @var string 
  19. * @since 2.0 
  20. */ 
  21. public $components = array(); 
  22.  
  23. /** 
  24. * Components settings 
  25. * @var string 
  26. * @since 2.0 
  27. */ 
  28. public $settings = array(); 
  29.  
  30. /** 
  31. * Singleton handling for a basic pods_components() request 
  32. * @return \PodsComponents 
  33. * @since 2.3.5 
  34. */ 
  35. public static function init () { 
  36. if ( !is_object( self::$instance ) ) 
  37. self::$instance = new PodsComponents(); 
  38.  
  39. return self::$instance; 
  40.  
  41. /** 
  42. * Setup actions and get options 
  43. * @return \PodsComponents 
  44. * @since 2.0 
  45. */ 
  46. public function __construct () { 
  47. $this->components_dir = realpath( apply_filters( 'pods_components_dir', PODS_DIR . 'components' ) ) . '/'; 
  48.  
  49. $settings = get_option( 'pods_component_settings', '' ); 
  50.  
  51. if ( !empty( $settings ) ) 
  52. $this->settings = (array) json_decode( $settings, true ); 
  53.  
  54. if ( !isset( $this->settings[ 'components' ] ) ) 
  55. $this->settings[ 'components' ] = array(); 
  56.  
  57. // Get components (give it access to theme) 
  58. add_action( 'setup_theme', array( $this, 'get_components' ), 11 ); 
  59.  
  60. // Load in components 
  61. add_action( 'setup_theme', array( $this, 'load' ), 12 ); 
  62.  
  63. // AJAX handling 
  64. if ( is_admin() ) { 
  65. add_action( 'wp_ajax_pods_admin_components', array( $this, 'admin_ajax' ) ); 
  66. add_action( 'wp_ajax_nopriv_pods_admin_components', array( $this, 'admin_ajax' ) ); 
  67.  
  68. // Add the Pods Components capabilities 
  69. add_filter( 'members_get_capabilities', array( $this, 'admin_capabilities' ) ); 
  70.  
  71. /** 
  72. * Add menu item 
  73. * @param string $parent The parent slug. 
  74. * @since 2.0 
  75. * @uses add_submenu_page 
  76. */ 
  77. public function menu ( $parent ) { 
  78. global $submenu; 
  79.  
  80. $custom_component_menus = array(); 
  81.  
  82. $pods_component_menu_items = array(); 
  83.  
  84. foreach ( $this->components as $component => $component_data ) { 
  85. $component_data[ 'MustUse' ] = apply_filters( 'pods_component_require_' . $component_data[ 'ID' ], $component_data[ 'MustUse' ], $component_data ); 
  86.  
  87. if ( empty( $component_data[ 'MustUse' ] ) && ( !isset( $this->settings[ 'components' ][ $component ] ) || 0 == $this->settings[ 'components' ][ $component ] ) ) 
  88. continue; 
  89.  
  90. if ( !empty( $component_data[ 'Hide' ] ) ) 
  91. continue; 
  92.  
  93. if ( !empty( $component_data[ 'DeveloperMode' ] ) && !pods_developer() ) 
  94. continue; 
  95.  
  96. if ( empty( $component_data[ 'TablelessMode' ] ) && pods_tableless() ) 
  97. continue; 
  98.  
  99. if ( empty( $component_data[ 'MenuPage' ] ) ) { 
  100. if ( !isset( $component_data[ 'object' ] ) ) 
  101. continue; 
  102. elseif ( !method_exists( $component_data[ 'object' ], 'admin' ) && !method_exists( $component_data[ 'object' ], 'options' ) ) 
  103. continue; 
  104.  
  105. if ( false === $component_data[ 'External' ] ) 
  106. $component_data[ 'File' ] = realpath( $this->components_dir . $component_data[ 'File' ] ); 
  107.  
  108. if ( !file_exists( $component_data[ 'File' ] ) ) { 
  109. pods_message( 'Pods Component not found: ' . $component_data[ 'File' ] ); 
  110.  
  111. pods_transient_clear( 'pods_components' ); 
  112.  
  113. continue; 
  114.  
  115. $capability = 'pods_component_' . str_replace( '-', '_', sanitize_title( $component ) ); 
  116.  
  117. if ( 0 < strlen( $component_data[ 'Capability' ] ) ) 
  118. $capability = $component_data[ 'Capability' ]; 
  119.  
  120. if ( !pods_is_admin( array( 'pods', 'pods_components', $capability ) ) ) 
  121. continue; 
  122.  
  123. $menu_page = 'pods-component-' . $component; 
  124.  
  125. if ( !empty( $component_data[ 'MenuPage' ] ) ) 
  126. $custom_component_menus[ $menu_page ] = $component_data; 
  127.  
  128. $pods_component_menu_items[ $component_data[ 'MenuName' ] ] = array( 
  129. 'menu_page' => $menu_page,  
  130. 'page_title' => $component_data[ 'Name' ],  
  131. 'capability' => 'read',  
  132. 'callback' => array( $this, 'admin_handler' ) 
  133. ); 
  134.  
  135. if ( isset( $component_data[ 'object' ] ) && method_exists( $component_data[ 'object' ], 'admin_assets' ) ) { 
  136. $pods_component_menu_items[ $component_data[ 'MenuName' ] ][ 'assets' ] = array( $component_data[ 'object' ], 'admin_assets' ); 
  137.  
  138. /** 
  139. * Add or change the items in the Pods Components Submenu. 
  140. * Can also be used to change which menu components is a submenu of or change title of menu. 
  141. * @params array $pods_component_menu_items { 
  142. * An array of arguments for add_submenu_page 
  143. * @param string $parent_slug The slug name for the parent menu (or the file name of a standard WordPress admin page) 
  144. * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected 
  145. * @param $menu_title The text to be used for the menu 
  146. * @param $capability The capability required for this menu to be displayed to the user. 
  147. * @param $menu_slug The slug name to refer to this menu by (should be unique for this menu) 
  148. * @param $function The function to be called to output the content for this page. 
  149. * } 
  150. * @returns array Array of submenu pages to be passed to add_submenu_page() 
  151. * @since 2.4.1 
  152. */ 
  153. $pods_component_menu_items = apply_filters( 'pods_admin_components_menu', $pods_component_menu_items ); 
  154.  
  155. ksort( $pods_component_menu_items ); 
  156.  
  157. foreach ( $pods_component_menu_items as $menu_title => $menu_data ) { 
  158. if ( !is_callable( $menu_data[ 'callback' ] ) ) { 
  159. continue; 
  160.  
  161. $page = add_submenu_page( 
  162. $parent,  
  163. strip_tags( $menu_data[ 'page_title' ] ),  
  164. '- ' . strip_tags( $menu_title ),  
  165. pods_v( 'capability', $menu_data, 'read', true ),  
  166. $menu_data[ 'menu_page' ],  
  167. $menu_data[ 'callback' ] 
  168. ); 
  169.  
  170. if ( isset( $menu_data[ 'assets' ] ) && is_callable( $menu_data[ 'assets' ] ) ) { 
  171. add_action( 'admin_print_styles-' . $page, $menu_data[ 'assets' ] ); 
  172.  
  173. if ( !empty( $custom_component_menus ) ) { 
  174. foreach ( $custom_component_menus as $menu_page => $component_data ) { 
  175. if ( isset( $submenu[ $parent ] ) ) { 
  176. foreach ( $submenu[ $parent ] as $sub => &$menu ) { 
  177. if ( $menu[ 2 ] == $menu_page ) { 
  178. $menu_page = $component_data[ 'MenuPage' ]; 
  179.  
  180. /**if ( !empty( $component_data[ 'MenuAddPage' ] ) ) { 
  181. if ( false !== strpos( $_SERVER[ 'REQUEST_URI' ], $component_data[ 'MenuAddPage' ] ) ) 
  182. $menu_page = $component_data[ 'MenuAddPage' ]; 
  183. }*/ 
  184.  
  185. $menu[ 2 ] = $menu_page; 
  186.  
  187. $page = current( explode( '?', $menu[ 2 ] ) ); 
  188.  
  189. if ( isset( $component_data[ 'object' ] ) && method_exists( $component_data[ 'object' ], 'admin_assets' ) ) 
  190. add_action( 'admin_print_styles-' . $page, array( $component_data[ 'object' ], 'admin_assets' ) ); 
  191.  
  192. break; 
  193.  
  194. /** 
  195. * Load activated components and init component 
  196. * @since 2.0 
  197. */ 
  198. public function load () { 
  199. do_action( 'pods_components_load' ); 
  200.  
  201. foreach ( (array) $this->components as $component => $component_data ) { 
  202. $component_data[ 'MustUse' ] = apply_filters( 'pods_component_require_' . $component_data[ 'ID' ], $component_data[ 'MustUse' ], $component_data ); 
  203.  
  204. if ( false === $component_data[ 'MustUse' ] && ( !isset( $this->settings[ 'components' ][ $component ] ) || 0 == $this->settings[ 'components' ][ $component ] ) ) 
  205. continue; 
  206.  
  207. if ( !empty( $component_data[ 'PluginDependency' ] ) ) { 
  208. $dependency = explode( '|', $component_data[ 'PluginDependency' ] ); 
  209.  
  210. if ( !pods_is_plugin_active( $dependency[ 1 ] ) ) 
  211. continue; 
  212.  
  213. if ( !empty( $component_data[ 'ThemeDependency' ] ) ) { 
  214. $dependency = explode( '|', $component_data[ 'ThemeDependency' ] ); 
  215.  
  216. if ( strtolower( $dependency[ 1 ] ) != strtolower( get_template() ) && strtolower( $dependency[ 1 ] ) != strtolower( get_stylesheet() ) ) 
  217. continue; 
  218.  
  219. if ( false === $component_data[ 'External' ] ) 
  220. $component_data[ 'File' ] = realpath( $this->components_dir . $component_data[ 'File' ] ); 
  221.  
  222. if ( empty( $component_data[ 'File' ] ) ) { 
  223. pods_transient_clear( 'pods_components' ); 
  224.  
  225. continue; 
  226.  
  227. if ( !file_exists( $component_data[ 'File' ] ) ) { 
  228. pods_message( 'Pods Component not found: ' . $component_data[ 'File' ] ); 
  229.  
  230. pods_transient_clear( 'pods_components' ); 
  231.  
  232. continue; 
  233.  
  234. include_once $component_data[ 'File' ]; 
  235.  
  236. if ( ( !empty( $component_data[ 'Class' ] ) && class_exists( $component_data[ 'Class' ] ) ) || isset( $component_data[ 'object' ] ) ) { 
  237. if ( !isset( $this->components[ $component ][ 'object' ] ) ) 
  238. $this->components[ $component ][ 'object' ] = new $component_data[ 'Class' ]; 
  239.  
  240. if ( method_exists( $this->components[ $component ][ 'object' ], 'options' ) ) { 
  241. if ( isset( $this->settings[ 'components' ][ $component ] ) ) 
  242. $this->components[ $component ][ 'options' ] = $this->components[ $component ][ 'object' ]->options( $this->settings[ 'components' ][ $component ] ); 
  243. else 
  244. $this->components[ $component ][ 'options' ] = $this->components[ $component ][ 'object' ]->options( array() ); 
  245.  
  246. $this->options( $component, $this->components[ $component ][ 'options' ] ); 
  247. else 
  248. $this->options( $component, array() ); 
  249.  
  250. if ( method_exists( $this->components[ $component ][ 'object' ], 'handler' ) ) 
  251. $this->components[ $component ][ 'object' ]->handler( $this->settings[ 'components' ][ $component ] ); 
  252.  
  253. /** 
  254. * Get list of components available 
  255. * @since 2.0 
  256. */ 
  257. public function get_components () { 
  258. $components = pods_transient_get( 'pods_components' ); 
  259.  
  260. if ( 1 == pods_var( 'pods_debug_components', 'get', 0 ) && pods_is_admin( array( 'pods' ) ) ) 
  261. $components = array(); 
  262.  
  263. if ( PodsInit::$version != PODS_VERSION || !is_array( $components ) || empty( $components ) || ( is_admin() && isset( $_GET[ 'page' ] ) && 'pods-components' == $_GET[ 'page' ] && 1 !== pods_transient_get( 'pods_components_refresh' ) ) ) { 
  264. do_action( 'pods_components_get' ); 
  265.  
  266. $component_dir = @opendir( untrailingslashit( $this->components_dir ) ); 
  267. $component_files = array(); 
  268.  
  269. if ( false !== $component_dir ) { 
  270. while ( false !== ( $file = readdir( $component_dir ) ) ) { 
  271. if ( '.' == substr( $file, 0, 1 ) ) 
  272. continue; 
  273. elseif ( is_dir( $this->components_dir . $file ) ) { 
  274. $component_subdir = @opendir( $this->components_dir . $file ); 
  275.  
  276. if ( $component_subdir ) { 
  277. while ( false !== ( $subfile = readdir( $component_subdir ) ) ) { 
  278. if ( '.' == substr( $subfile, 0, 1 ) ) 
  279. continue; 
  280. elseif ( '.php' == substr( $subfile, -4 ) ) 
  281. $component_files[] = str_replace( '\\', '/', $file . '/' . $subfile ); 
  282.  
  283. closedir( $component_subdir ); 
  284. elseif ( '.php' == substr( $file, -4 ) ) 
  285. $component_files[] = $file; 
  286.  
  287. closedir( $component_dir ); 
  288.  
  289. $default_headers = array( 
  290. 'ID' => 'ID',  
  291. 'Name' => 'Name',  
  292. 'ShortName' => 'Short Name',  
  293. 'PluginName' => 'Plugin Name',  
  294. 'ComponentName' => 'Component Name',  
  295. 'URI' => 'URI',  
  296. 'MenuName' => 'Menu Name',  
  297. 'MenuPage' => 'Menu Page',  
  298. 'MenuAddPage' => 'Menu Add Page',  
  299. 'MustUse' => 'Must Use',  
  300. 'Description' => 'Description',  
  301. 'Version' => 'Version',  
  302. 'Category' => 'Category',  
  303. 'Author' => 'Author',  
  304. 'AuthorURI' => 'Author URI',  
  305. 'Class' => 'Class',  
  306. 'Hide' => 'Hide',  
  307. 'PluginDependency' => 'Plugin Dependency',  
  308. 'ThemeDependency' => 'Theme Dependency',  
  309. 'DeveloperMode' => 'Developer Mode',  
  310. 'TablelessMode' => 'Tableless Mode',  
  311. 'Capability' => 'Capability',  
  312. 'Plugin' => 'Plugin' 
  313. ); 
  314.  
  315. $component_files = apply_filters( 'pods_components_register', $component_files ); 
  316.  
  317. $components = array(); 
  318.  
  319. foreach ( $component_files as $component_file ) { 
  320. $external = false; 
  321.  
  322. if ( is_array( $component_file ) && isset( $component_file[ 'File' ] ) ) { 
  323. $component = $component_file = $component_file[ 'File' ]; 
  324.  
  325. $external = true; 
  326. else 
  327. $component = $this->components_dir . $component_file; 
  328.  
  329. if ( !is_readable( $component ) ) 
  330. continue; 
  331.  
  332. $component_data = get_file_data( $component, $default_headers, 'pods_component' ); 
  333.  
  334. if ( ( empty( $component_data[ 'Name' ] ) && empty( $component_data[ 'ComponentName' ] ) && empty( $component_data[ 'PluginName' ] ) ) || 'yes' == $component_data[ 'Hide' ] ) 
  335. continue; 
  336.  
  337. if ( isset( $component_data[ 'Plugin' ] ) && pods_is_plugin_active( $component_data[ 'Plugin' ] ) ) 
  338. continue; 
  339.  
  340. if ( empty( $component_data[ 'Name' ] ) ) { 
  341. if ( !empty( $component_data[ 'ComponentName' ] ) ) 
  342. $component_data[ 'Name' ] = $component_data[ 'ComponentName' ]; 
  343. elseif ( !empty( $component_data[ 'PluginName' ] ) ) 
  344. $component_data[ 'Name' ] = $component_data[ 'PluginName' ]; 
  345.  
  346. if ( empty( $component_data[ 'ShortName' ] ) ) 
  347. $component_data[ 'ShortName' ] = $component_data[ 'Name' ]; 
  348.  
  349. if ( empty( $component_data[ 'MenuName' ] ) ) 
  350. $component_data[ 'MenuName' ] = $component_data[ 'Name' ]; 
  351.  
  352. if ( empty( $component_data[ 'Class' ] ) ) 
  353. $component_data[ 'Class' ] = 'Pods_' . pods_js_name( basename( $component, '.php' ), false ); 
  354.  
  355. if ( empty( $component_data[ 'ID' ] ) ) 
  356. $component_data[ 'ID' ] = $component_data[ 'Name' ]; 
  357.  
  358. $component_data[ 'ID' ] = sanitize_title( $component_data[ 'ID' ] ); 
  359.  
  360. if ( 'on' == strtolower( $component_data[ 'DeveloperMode' ] ) || 1 == $component_data[ 'DeveloperMode' ] ) 
  361. $component_data[ 'DeveloperMode' ] = true; 
  362. else 
  363. $component_data[ 'DeveloperMode' ] = false; 
  364.  
  365. if ( 'on' == strtolower( $component_data[ 'TablelessMode' ] ) || 1 == $component_data[ 'TablelessMode' ] ) 
  366. $component_data[ 'TablelessMode' ] = true; 
  367. else 
  368. $component_data[ 'TablelessMode' ] = false; 
  369.  
  370. $component_data[ 'External' ] = (boolean) $external; 
  371.  
  372. if ( 'on' == strtolower($component_data[ 'MustUse' ] ) || '1' == $component_data[ 'MustUse' ] ) 
  373. $component_data[ 'MustUse' ] = true; 
  374. elseif ( 'off' == strtolower($component_data[ 'MustUse' ] ) || '0' == $component_data[ 'MustUse' ] ) 
  375. $component_data[ 'MustUse' ] = false; 
  376. else 
  377. $component_data[ 'MustUse' ] = $component_data[ 'External' ]; 
  378.  
  379. $component_data[ 'File' ] = $component_file; 
  380.  
  381. $components[ $component_data[ 'ID' ] ] = $component_data; 
  382.  
  383. ksort( $components ); 
  384.  
  385. pods_transient_set( 'pods_components_refresh', 1, ( 60 * 60 * 12 ) ); 
  386.  
  387. pods_transient_set( 'pods_components', $components ); 
  388.  
  389. if ( 1 == pods_var( 'pods_debug_components', 'get', 0 ) && pods_is_admin( array( 'pods' ) ) ) 
  390. pods_debug( $components ); 
  391.  
  392. $this->components = $components; 
  393.  
  394. return $this->components; 
  395.  
  396. /** 
  397. * Set component options 
  398. * @param $component 
  399. * @param $options 
  400. * @since 2.0 
  401. */ 
  402. public function options ( $component, $options ) { 
  403. if ( !isset( $this->settings[ 'components' ][ $component ] ) || !is_array( $this->settings[ 'components' ][ $component ] ) ) 
  404. $this->settings[ 'components' ][ $component ] = array(); 
  405.  
  406. foreach ( $options as $option => $data ) { 
  407. if ( !isset( $this->settings[ 'components' ][ $component ][ $option ] ) && isset( $data[ 'default' ] ) ) 
  408. $this->settings[ 'components' ][ $component ][ $option ] = $data[ 'default' ]; 
  409.  
  410. /** 
  411. * Call component specific admin functions 
  412. * @since 2.0 
  413. */ 
  414. public function admin_handler () { 
  415. $component = str_replace( 'pods-component-', '', $_GET[ 'page' ] ); 
  416.  
  417. if ( isset( $this->components[ $component ] ) && isset( $this->components[ $component ][ 'object' ] ) && is_object( $this->components[ $component ][ 'object' ] ) ) { 
  418. // Component init 
  419. if ( method_exists( $this->components[ $component ][ 'object' ], 'init' ) ) 
  420. $this->components[ $component ][ 'object' ]->init( $this->settings[ 'components' ][ $component ], $component ); 
  421.  
  422. // Component Admin handler 
  423. if ( method_exists( $this->components[ $component ][ 'object' ], 'admin' ) ) 
  424. $this->components[ $component ][ 'object' ]->admin( $this->settings[ 'components' ][ $component ], $component ); 
  425. // Built-in Admin Handler 
  426. elseif ( method_exists( $this->components[ $component ][ 'object' ], 'options' ) ) 
  427. $this->admin( $this->components[ $component ][ 'object' ]->options( $this->settings[ 'components' ][ $component ] ), $this->settings[ 'components' ][ $component ], $component ); 
  428.  
  429. public function admin ( $options, $settings, $component ) { 
  430. if ( !isset( $this->components[ $component ] ) ) 
  431. wp_die( 'Invalid Component' ); 
  432.  
  433. $component_label = $this->components[ $component ][ 'Name' ]; 
  434.  
  435. include PODS_DIR . 'ui/admin/components-admin.php'; 
  436.  
  437. /** 
  438. * Toggle a component on or off 
  439. * @param string $component The component name to toggle 
  440. * @return bool 
  441. * @since 2.0 
  442. */ 
  443. public function toggle ( $component ) { 
  444. $toggle = null; 
  445.  
  446. if ( isset( $this->components[ $component ] ) ) { 
  447. if ( 1 == pods_var( 'toggle', 'get' ) && ( !isset( $this->settings[ 'components' ][ $component ] ) || 0 == $this->settings[ 'components' ][ $component ] ) ) { 
  448. $this->settings[ 'components' ][ $component ] = array(); 
  449. $toggle = true; 
  450. elseif ( 0 == pods_var( 'toggle', 'get' ) ) { 
  451. $this->settings[ 'components' ][ $component ] = 0; 
  452. $toggle = false; 
  453.  
  454. $settings = version_compare( PHP_VERSION, '5.4.0', '>=' ) ? json_encode( $this->settings, JSON_UNESCAPED_UNICODE ) : json_encode( $this->settings ); 
  455.  
  456. update_option( 'pods_component_settings', $settings ); 
  457.  
  458. return $toggle; 
  459.  
  460. /** 
  461. * Add pods specific capabilities. 
  462. * @param $capabilities List of extra capabilities to add 
  463. * @return array 
  464. */ 
  465. public function admin_capabilities ( $capabilities ) { 
  466. foreach ( $this->components as $component => $component_data ) { 
  467. if ( !empty( $component_data[ 'Hide' ] ) ) 
  468. continue; 
  469.  
  470. if ( true === (boolean) pods_var( 'DeveloperMode', $component_data, false ) && !pods_developer() ) 
  471. continue; 
  472.  
  473. if ( true === (boolean) pods_var( 'TablelessMode', $component_data, false ) && !pods_developer() ) 
  474. continue; 
  475.  
  476. if ( empty( $component_data[ 'MenuPage' ] ) && ( !isset( $component_data[ 'object' ] ) || !method_exists( $component_data[ 'object' ], 'admin' ) ) ) 
  477. continue; 
  478.  
  479. $capability = 'pods_component_' . str_replace( '-', '_', sanitize_title( str_replace( ' and ', ' ', strip_tags( $component_data[ 'Name' ] ) ) ) ); 
  480.  
  481. if ( 0 < strlen ( $component_data[ 'Capability' ] ) ) 
  482. $capability = $component_data[ 'Capability' ]; 
  483.  
  484. if ( !in_array( $capability, $capabilities ) ) 
  485. $capabilities[] = $capability; 
  486.  
  487. return $capabilities; 
  488.  
  489. /** 
  490. * Handle admin ajax 
  491. * @since 2.0 
  492. */ 
  493. public function admin_ajax () { 
  494. if ( false === headers_sent() ) { 
  495. pods_session_start(); 
  496.  
  497. header( 'Content-Type: text/html; charset=' . get_bloginfo( 'charset' ) ); 
  498.  
  499. // Sanitize input 
  500. $params = pods_unslash( (array) $_POST ); 
  501.  
  502. foreach ( $params as $key => $value ) { 
  503. if ( 'action' == $key ) 
  504. continue; 
  505.  
  506. unset( $params[ $key ] ); 
  507.  
  508. $params[ str_replace( '_podsfix_', '', $key ) ] = $value; 
  509.  
  510. $params = (object) $params; 
  511.  
  512. $component = $params->component; 
  513. $method = $params->method; 
  514.  
  515. if ( !isset( $component ) || !isset( $this->components[ $component ] ) || !isset( $this->settings[ 'components' ][ $component ] ) ) 
  516. pods_error( 'Invalid AJAX request', $this ); 
  517.  
  518. if ( !isset( $params->_wpnonce ) || false === wp_verify_nonce( $params->_wpnonce, 'pods-component-' . $component . '-' . $method ) ) 
  519. pods_error( 'Unauthorized request', $this ); 
  520.  
  521. // Cleaning up $params 
  522. unset( $params->action ); 
  523. unset( $params->component ); 
  524. unset( $params->method ); 
  525. unset( $params->_wpnonce ); 
  526.  
  527. $params = (object) apply_filters( 'pods_component_ajax_' . $component . '_' . $method, $params, $component, $method ); 
  528.  
  529. $output = false; 
  530.  
  531. // Component init 
  532. if ( isset( $this->components[ $component ][ 'object' ] ) && method_exists( $this->components[ $component ][ 'object' ], 'init' ) ) 
  533. $this->components[ $component ][ 'object' ]->init( $this->settings[ 'components' ][ $component ], $component ); 
  534.  
  535. // Handle internal methods 
  536. if ( isset( $this->components[ $component ][ 'object' ] ) && !method_exists( $this->components[ $component ][ 'object' ], 'ajax_' . $method ) && method_exists( $this, 'admin_ajax_' . $method ) ) 
  537. $output = call_user_func( array( $this, 'admin_ajax_' . $method ), $component, $params ); 
  538. // Make sure method exists 
  539. elseif ( !isset( $this->components[ $component ][ 'object' ] ) || !method_exists( $this->components[ $component ][ 'object' ], 'ajax_' . $method ) ) 
  540. pods_error( 'API method does not exist', $this ); 
  541. // Dynamically call the component method 
  542. else 
  543. $output = call_user_func( array( $this->components[ $component ][ 'object' ], 'ajax_' . $method ), $params ); 
  544.  
  545. if ( !is_bool( $output ) ) 
  546. echo $output; 
  547.  
  548. die(); // KBAI! 
  549.  
  550. public function admin_ajax_settings ( $component, $params ) { 
  551. if ( !isset( $this->components[ $component ] ) ) 
  552. wp_die( 'Invalid Component' ); 
  553. elseif ( !method_exists( $this->components[ $component ][ 'object' ], 'options' ) ) 
  554. pods_error( 'Component options method does not exist', $this ); 
  555.  
  556. $options = $this->components[ $component ][ 'object' ]->options( $this->settings[ 'components' ][ $component ] ); 
  557.  
  558. if ( empty( $this->settings[ 'components' ][ $component ] ) ) 
  559. $this->settings[ 'components' ][ $component ] = array(); 
  560.  
  561. foreach ( $options as $field_name => $field_option ) { 
  562. $field_option = PodsForm::field_setup( $field_option, null, $field_option[ 'type' ] ); 
  563.  
  564. if ( !is_array( $field_option[ 'group' ] ) ) { 
  565. $field_value = pods_var_raw( 'pods_setting_' . $field_name, $params ); 
  566.  
  567. $this->settings[ 'components' ][ $component ][ $field_name ] = $field_value; 
  568. else { 
  569. foreach ( $field_option[ 'group' ] as $field_group_name => $field_group_option ) { 
  570. $field_value = pods_var_raw( 'pods_setting_' . $field_group_name, $params ); 
  571.  
  572. $this->settings[ 'components' ][ $component ][ $field_group_name ] = $field_value; 
  573.  
  574. $settings = version_compare( PHP_VERSION, '5.4.0', '>=' ) ? json_encode( $this->settings, JSON_UNESCAPED_UNICODE ) : json_encode( $this->settings ); 
  575.  
  576. update_option( 'pods_component_settings', $settings ); 
  577.  
  578. return '1';