/wp-content/plugins/pods/classes/PodsComponents.php

https://gitlab.com/najomie/ljm · PHP · 667 lines · 386 code · 147 blank · 134 comment · 155 complexity · bbd8e15bc68f7196c177e40d02da538f MD5 · raw file

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