PageRenderTime 65ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/classes/PodsAdmin.php

https://github.com/ElmsPark/pods
PHP | 1633 lines | 1116 code | 366 blank | 151 comment | 363 complexity | cf249ccd56a4ffe257cd6089ac755e0b MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /**
  3. * @package Pods
  4. */
  5. class PodsAdmin {
  6. /**
  7. * @var PodsAPI
  8. */
  9. private $api;
  10. /**
  11. * @var PodsData
  12. */
  13. private $data;
  14. /**
  15. * Setup and Handle Admin functionality
  16. *
  17. * @license http://www.gnu.org/licenses/gpl-2.0.html
  18. * @since 2.0.0
  19. */
  20. public function __construct () {
  21. $this->api = pods_api();
  22. $this->data = pods_data();
  23. // Scripts / Stylesheets
  24. add_action( 'admin_enqueue_scripts', array( $this, 'admin_head' ) );
  25. // AJAX $_POST fix
  26. add_action( 'admin_init', array( $this, 'admin_init' ), 9 );
  27. // Menus
  28. add_action( 'admin_menu', array( $this, 'admin_menu' ), 99 );
  29. // AJAX for Admin
  30. add_action( 'wp_ajax_pods_admin', array( $this, 'admin_ajax' ) );
  31. add_action( 'wp_ajax_nopriv_pods_admin', array( $this, 'admin_ajax' ) );
  32. // AJAX for Uploads
  33. add_action( 'wp_ajax_pods_upload', array( $this, 'admin_ajax_upload' ) );
  34. add_action( 'wp_ajax_nopriv_pods_upload', array( $this, 'admin_ajax_upload' ) );
  35. // AJAX for Relationship lookups
  36. add_action( 'wp_ajax_pods_relationship', array( $this, 'admin_ajax_relationship' ) );
  37. add_action( 'wp_ajax_nopriv_pods_relationship', array( $this, 'admin_ajax_relationship' ) );
  38. // Add Media Bar button for Shortcode
  39. add_action( 'media_buttons', array( $this, 'media_button' ), 12 );
  40. // Add the Pods capabilities
  41. add_filter( 'members_get_capabilities', array( $this, 'admin_capabilities' ) );
  42. add_action( 'admin_head-media-upload-popup', array( $this, 'register_media_assets' ) );
  43. }
  44. /**
  45. * Init the admin area
  46. *
  47. * @since 2.0.0
  48. */
  49. public function admin_init () {
  50. // Fix for plugins that *don't do it right* so we don't cause issues for users
  51. if ( defined( 'DOING_AJAX' ) && !empty( $_POST ) && ( in_array( pods_var( 'action', 'get' ), array( 'pods_admin', 'pods_relationship', 'pods_upload', 'pods_admin_components' ) ) || in_array( pods_var( 'action', 'post' ), array( 'pods_admin', 'pods_relationship', 'pods_upload', 'pods_admin_components' ) ) ) ) {
  52. foreach ( $_POST as $key => $value ) {
  53. if ( 'action' == $key )
  54. continue;
  55. unset( $_POST[ $key ] );
  56. $_POST[ '_podsfix_' . $key ] = $value;
  57. }
  58. }
  59. }
  60. /**
  61. * Attach requirements to admin header
  62. *
  63. * @since 2.0.0
  64. */
  65. public function admin_head () {
  66. wp_register_style( 'pods-admin', PODS_URL . 'ui/css/pods-admin.css', array(), PODS_VERSION );
  67. wp_register_script( 'pods-floatmenu', PODS_URL . 'ui/js/floatmenu.js', array(), PODS_VERSION );
  68. wp_register_script( 'pods-admin-importer', PODS_URL . 'ui/js/admin-importer.js', array(), PODS_VERSION );
  69. wp_register_style( 'pods-manage', PODS_URL . 'ui/css/pods-manage.css', array(), PODS_VERSION );
  70. wp_register_style( 'pods-wizard', PODS_URL . 'ui/css/pods-wizard.css', array(), PODS_VERSION );
  71. wp_register_script( 'pods-upgrade', PODS_URL . 'ui/js/jquery.pods.upgrade.js', array(), PODS_VERSION );
  72. if ( isset( $_GET[ 'page' ] ) ) {
  73. $page = $_GET[ 'page' ];
  74. if ( 'pods' == $page || ( false !== strpos( $page, 'pods-' ) && 0 === strpos( $page, 'pods-' ) ) ) {
  75. ?>
  76. <script type="text/javascript">
  77. var PODS_URL = "<?php echo PODS_URL; ?>";
  78. </script>
  79. <?php
  80. wp_enqueue_script( 'jquery' );
  81. wp_enqueue_script( 'jquery-ui-core' );
  82. wp_enqueue_script( 'jquery-ui-sortable' );
  83. wp_enqueue_style( 'jquery-ui' );
  84. wp_enqueue_script( 'pods-floatmenu' );
  85. wp_enqueue_style( 'pods-qtip' );
  86. wp_enqueue_script( 'jquery-qtip' );
  87. wp_enqueue_script( 'pods-qtip-init' );
  88. wp_enqueue_script( 'pods' );
  89. if ( 0 === strpos( $page, 'pods-manage-' ) || 0 === strpos( $page, 'pods-add-new-' ) )
  90. wp_enqueue_script( 'post' );
  91. else
  92. wp_enqueue_style( 'pods-admin' );
  93. if ( 'pods-advanced' == $page ) {
  94. wp_register_style( 'pods-advanced', PODS_URL . 'ui/css/pods-advanced.css', array(), '1.0' );
  95. wp_enqueue_style( 'pods-advanced' );
  96. wp_enqueue_script( 'jquery-ui-effects-core', PODS_URL . 'ui/js/jquery-ui/jquery.effects.core.js', array( 'jquery' ), '1.8.8' );
  97. wp_enqueue_script( 'jquery-ui-effects-fade', PODS_URL . 'ui/js/jquery-ui/jquery.effects.fade.js', array( 'jquery' ), '1.8.8' );
  98. wp_enqueue_script( 'jquery-ui-dialog' );
  99. wp_register_script( 'pods-advanced', PODS_URL . 'ui/js/advanced.js', array(), PODS_VERSION );
  100. wp_enqueue_script( 'pods-advanced' );
  101. }
  102. elseif ( 'pods-packages' == $page )
  103. wp_enqueue_style( 'pods-wizard' );
  104. elseif ( 'pods-wizard' == $page || 'pods-upgrade' == $page || ( in_array( $page, array( 'pods', 'pods-add-new' ) ) && in_array( pods_var( 'action', 'get', 'manage' ), array( 'add', 'manage' ) ) ) ) {
  105. wp_enqueue_style( 'pods-wizard' );
  106. if ( 'pods-upgrade' == $page )
  107. wp_enqueue_script( 'pods-upgrade' );
  108. }
  109. }
  110. }
  111. }
  112. /**
  113. * Buld the admin menus
  114. *
  115. * @since 2.0.0
  116. */
  117. public function admin_menu () {
  118. $results = $this->api->load_pods( array(
  119. 'type' => 'pod'
  120. ) );
  121. $all_pods = $this->api->load_pods();
  122. if ( !PodsInit::$upgrade_needed ) {
  123. if ( false !== $results ) {
  124. $submenu = array();
  125. $pods_pages = 0;
  126. foreach ( (array) $results as $item ) {
  127. if ( !is_super_admin() && !current_user_can( 'delete_users' ) && !current_user_can( 'pods' ) && !current_user_can( 'pods_content' ) && !current_user_can( 'pods_add_' . $item[ 'name' ] ) && !current_user_can( 'pods_edit_' . $item[ 'name' ] ) && !current_user_can( 'pods_delete_' . $item[ 'name' ] ) )
  128. continue;
  129. if ( 1 == pods_var( 'show_in_menu', $item[ 'options' ], 0 ) ) {
  130. $menu_label = pods_var_raw( 'menu_name', $item[ 'options' ], pods_var_raw( 'label', $item, ucwords( str_replace( '_', ' ', $item[ 'name' ] ) ), null, true ), null, true );
  131. $menu_label = apply_filters( 'pods_admin_menu_label', $menu_label, $item );
  132. $singular_label = pods_var_raw( 'label_singular', $item[ 'options' ], pods_var_raw( 'label', $item, ucwords( str_replace( '_', ' ', $item[ 'name' ] ) ), null, true ), null, true );
  133. $plural_label = pods_var_raw( 'label', $item, ucwords( str_replace( '_', ' ', $item[ 'name' ] ) ), null, true );
  134. $menu_icon = pods_var_raw( 'menu_icon', $item[ 'options' ], '', null, true );
  135. $parent_page = null;
  136. if ( is_super_admin() || current_user_can( 'delete_users' ) || current_user_can( 'pods' ) || current_user_can( 'pods_content' ) || current_user_can( 'pods_edit_' . $item[ 'name' ] ) || current_user_can( 'pods_delete_' . $item[ 'name' ] ) ) {
  137. $pods_pages++;
  138. $parent_page = $page = 'pods-manage-' . $item[ 'name' ];
  139. add_menu_page( $menu_label, $menu_label, 'read', $parent_page, '', $menu_icon, '57.' . ( 10 < count( $pods_pages ) ? $pods_pages : '0' . $pods_pages ) );
  140. $all_title = $plural_label;
  141. $all_label = __( 'All', 'pods' ) . ' ' . $plural_label;
  142. if ( $page == pods_var( 'page', 'get' ) ) {
  143. if ( 'edit' == pods_var( 'action', 'get', 'manage' ) )
  144. $all_title = __( 'Edit', 'pods' ) . ' ' . $singular_label;
  145. elseif ( 'add' == pods_var( 'action', 'get', 'manage' ) )
  146. $all_title = __( 'Add New', 'pods' ) . ' ' . $singular_label;
  147. }
  148. add_submenu_page( $parent_page, $all_title, $all_label, 'read', $page, array( $this, 'admin_content' ) );
  149. }
  150. if ( is_super_admin() || current_user_can( 'delete_users' ) || current_user_can( 'pods' ) || current_user_can( 'pods_content' ) || current_user_can( 'pods_add_' . $item[ 'name' ] ) ) {
  151. $page = 'pods-add-new-' . $item[ 'name' ];
  152. if ( null === $parent_page ) {
  153. $pods_pages++;
  154. $parent_page = $page;
  155. add_menu_page( $menu_label, $menu_label, 'read', $parent_page, '', $menu_icon, '57.' . ( 10 < count( $pods_pages ) ? $pods_pages : '0' . $pods_pages ) );
  156. }
  157. $add_title = __( 'Add New', 'pods' ) . ' ' . $singular_label;
  158. $add_label = __( 'Add New', 'pods' );
  159. add_submenu_page( $parent_page, $add_title, $add_label, 'read', $page, array( $this, 'admin_content' ) );
  160. }
  161. }
  162. else
  163. $submenu[] = $item;
  164. }
  165. $submenu = apply_filters( 'pods_admin_menu_secondary_content', $submenu );
  166. if ( !empty( $submenu ) && ( !defined( 'PODS_DISABLE_CONTENT_MENU' ) || !PODS_DISABLE_CONTENT_MENU ) ) {
  167. $parent_page = null;
  168. foreach ( $submenu as $item ) {
  169. $singular_label = pods_var_raw( 'label_singular', $item[ 'options' ], pods_var_raw( 'label', $item, ucwords( str_replace( '_', ' ', $item[ 'name' ] ) ), null, true ), null, true );
  170. $plural_label = pods_var_raw( 'label', $item, ucwords( str_replace( '_', ' ', $item[ 'name' ] ) ), null, true );
  171. if ( is_super_admin() || current_user_can( 'delete_users' ) || current_user_can( 'pods' ) || current_user_can( 'pods_content' ) || current_user_can( 'pods_edit_' . $item[ 'name' ] ) || current_user_can( 'pods_delete_' . $item[ 'name' ] ) ) {
  172. $page = 'pods-manage-' . $item[ 'name' ];
  173. if ( null === $parent_page ) {
  174. $parent_page = $page;
  175. add_menu_page( 'Pods', 'Pods', 'read', $parent_page, null, PODS_URL . 'ui/images/icon16.png', '58.5' );
  176. }
  177. $all_title = $plural_label;
  178. $all_label = __( 'Manage', 'pods' ) . ' ' . $plural_label;
  179. if ( $page == pods_var( 'page', 'get' ) ) {
  180. if ( 'edit' == pods_var( 'action', 'get', 'manage' ) )
  181. $all_title = __( 'Edit', 'pods' ) . ' ' . $singular_label;
  182. elseif ( 'add' == pods_var( 'action', 'get', 'manage' ) )
  183. $all_title = __( 'Add New', 'pods' ) . ' ' . $singular_label;
  184. }
  185. add_submenu_page( $parent_page, $all_title, $all_label, 'read', $page, array( $this, 'admin_content' ) );
  186. }
  187. elseif ( current_user_can( 'pods_add_' . $item[ 'name' ] ) ) {
  188. $page = 'pods-add-new-' . $item[ 'name' ];
  189. if ( null === $parent_page ) {
  190. $parent_page = $page;
  191. add_menu_page( 'Pods', 'Pods', 'read', $parent_page, null, PODS_URL . 'ui/images/icon16.png', '58.5' );
  192. }
  193. $add_title = __( 'Add New', 'pods' ) . ' ' . $singular_label;
  194. $add_label = __( 'Manage', 'pods' ) . ' ' . $plural_label;
  195. add_submenu_page( $parent_page, $add_title, $add_label, 'read', $page, array( $this, 'admin_content' ) );
  196. }
  197. }
  198. }
  199. }
  200. $admin_menus = array(
  201. 'pods' => array(
  202. 'label' => __( 'Edit Pods', 'pods' ),
  203. 'function' => array( $this, 'admin_setup' ),
  204. 'access' => 'pods'
  205. ),
  206. 'pods-add-new' => array(
  207. 'label' => __( 'Add New', 'pods' ),
  208. 'function' => array( $this, 'admin_setup' ),
  209. 'access' => 'pods'
  210. ),
  211. 'pods-components' => array(
  212. 'label' => __( 'Components', 'pods' ),
  213. 'function' => array( $this, 'admin_components' ),
  214. 'access' => 'pods_components'
  215. ),
  216. 'pods-settings' => array(
  217. 'label' => __( 'Settings', 'pods' ),
  218. 'function' => array( $this, 'admin_settings' ),
  219. 'access' => 'pods_settings'
  220. ),
  221. 'pods-help' => array(
  222. 'label' => __( 'Help', 'pods' ),
  223. 'function' => array( $this, 'admin_help' )
  224. )
  225. );
  226. if ( empty( $all_pods ) )
  227. unset( $admin_menus[ 'pods' ] );
  228. }
  229. else {
  230. $admin_menus = array(
  231. 'pods-upgrade' => array(
  232. 'label' => __( 'Upgrade', 'pods' ),
  233. 'function' => array( $this, 'admin_upgrade' ),
  234. 'access' => 'manage_options'
  235. ),
  236. 'pods-settings' => array(
  237. 'label' => __( 'Settings', 'pods' ),
  238. 'function' => array( $this, 'admin_settings' ),
  239. 'access' => 'pods_settings'
  240. ),
  241. 'pods-help' => array(
  242. 'label' => __( 'Help', 'pods' ),
  243. 'function' => array( $this, 'admin_help' )
  244. )
  245. );
  246. add_action( 'admin_notices', array( $this, 'upgrade_notice' ) );
  247. }
  248. $admin_menus = apply_filters( 'pods_admin_menu', $admin_menus );
  249. $parent = false;
  250. if ( !empty( $admin_menus ) && ( !defined( 'PODS_DISABLE_ADMIN_MENU' ) || !PODS_DISABLE_ADMIN_MENU ) ) {
  251. foreach ( $admin_menus as $page => $menu_item ) {
  252. if ( !is_super_admin() && !current_user_can( 'delete_users' ) && isset( $menu_item[ 'access' ] ) ) {
  253. $access = (array) $menu_item[ 'access' ];
  254. $ok = false;
  255. foreach ( $access as $cap ) {
  256. if ( current_user_can( $cap ) ) {
  257. $ok = true;
  258. break;
  259. }
  260. }
  261. if ( !$ok )
  262. continue;
  263. }
  264. // Don't just show the help page
  265. if ( false === $parent && 'pods-help' == $page )
  266. continue;
  267. if ( !isset( $menu_item[ 'label' ] ) )
  268. $menu_item[ 'label' ] = $page;
  269. if ( false === $parent ) {
  270. $parent = $page;
  271. $menu = __( 'Pods Admin', 'pods' );
  272. if ( 'pods-upgrade' == $parent )
  273. $menu = __( 'Pods Upgrade', 'pods' );
  274. add_menu_page( $menu, $menu, 'read', $parent, null, PODS_URL . 'ui/images/icon16.png' );
  275. }
  276. add_submenu_page( $parent, $menu_item[ 'label' ], $menu_item[ 'label' ], 'read', $page, $menu_item[ 'function' ] );
  277. if ( 'pods-components' == $page )
  278. PodsInit::$components->menu( $parent );
  279. }
  280. }
  281. }
  282. public function upgrade_notice () {
  283. echo '<div class="error fade"><p>';
  284. echo sprintf(
  285. __( '<strong>NOTICE:</strong> Pods %s requires your action to complete the upgrade. Please run the <a href="%s">Upgrade Wizard</a>.', 'pods' ),
  286. PODS_VERSION,
  287. admin_url( 'admin.php?page=pods-upgrade' )
  288. );
  289. echo '</p></div>';
  290. }
  291. /**
  292. * Create PodsUI content for the administration pages
  293. */
  294. public function admin_content () {
  295. $pod_name = str_replace( array( 'pods-manage-', 'pods-add-new-' ), '', $_GET[ 'page' ] );
  296. $pod = pods( $pod_name, pods_var( 'id', 'get', null, null, true ) );
  297. $default = 'manage';
  298. if ( false !== strpos( $_GET[ 'page' ], 'pods-add-new-' ) )
  299. $default = 'add';
  300. $actions_disabled = array(
  301. 'duplicate' => 'duplicate',
  302. 'view' => 'view',
  303. 'export' => 'export'
  304. );
  305. if ( 1 == pods_var( 'ui_export', $pod->pod_data[ 'options' ], 0 ) )
  306. unset( $actions_disabled[ 'export' ] );
  307. $author_restrict = false;
  308. if ( isset( $pod->fields[ 'author' ] ) && 'pick' == $pod->fields[ 'author' ][ 'type' ] && 'user' == $pod->fields[ 'author' ][ 'pick_object' ] )
  309. $author_restrict = 'author.ID';
  310. if ( !is_super_admin() && !current_user_can( 'delete_users' ) && !current_user_can( 'pods' ) && !current_user_can( 'pods_content' ) ) {
  311. if ( !current_user_can( 'pods_add_' . $pod_name ) ) {
  312. $actions_disabled[ 'add' ] = 'add';
  313. $default = 'manage';
  314. }
  315. if ( !$author_restrict && !current_user_can( 'pods_edit_' . $pod_name ) && !current_user_can( 'pods_edit_others_' . $pod_name ) )
  316. $actions_disabled[ 'edit' ] = 'edit';
  317. if ( !$author_restrict && !current_user_can( 'pods_delete_' . $pod_name ) && !current_user_can( 'pods_delete_others_' . $pod_name ) )
  318. $actions_disabled[ 'delete' ] = 'delete';
  319. if ( !current_user_can( 'pods_export_' . $pod_name ) )
  320. $actions_disabled[ 'export' ] = 'export';
  321. }
  322. $_GET[ 'action' ] = pods_var( 'action', 'get', $default );
  323. $index = $pod->pod_data[ 'field_id' ];
  324. $label = __( 'ID', 'pods' );
  325. if ( isset( $pod->pod_data[ 'fields' ][ $pod->pod_data[ 'field_index' ] ] ) ) {
  326. $index = $pod->pod_data[ 'field_index' ];
  327. $label = $pod->pod_data[ 'fields' ][ $pod->pod_data[ 'field_index' ] ];
  328. }
  329. $manage = array(
  330. $index => $label
  331. );
  332. if ( isset( $pod->pod_data[ 'fields' ][ 'modified' ] ) )
  333. $manage[ 'modified' ] = $pod->pod_data[ 'fields' ][ 'modified' ][ 'label' ];
  334. $manage = apply_filters( 'pods_admin_ui_fields_' . $pod->pod, apply_filters( 'pods_admin_ui_fields', $manage, $pod->pod, $pod ), $pod->pod, $pod );
  335. $ui = array(
  336. 'pod' => $pod,
  337. 'fields' => array(
  338. 'manage' => $manage,
  339. 'add' => $pod->pod_data[ 'fields' ],
  340. 'edit' => $pod->pod_data[ 'fields' ],
  341. 'duplicate' => $pod->pod_data[ 'fields' ]
  342. ),
  343. 'actions_disabled' => $actions_disabled
  344. );
  345. if ( !empty( $author_restrict ) )
  346. $ui[ 'restrict' ] = array( 'author_restrict' => $author_restrict );
  347. if ( !in_array( 'delete', $ui[ 'actions_disabled' ] ) ) {
  348. $ui[ 'actions_bulk' ] = array(
  349. 'delete' => array(
  350. 'label' => __( 'Delete', 'pods' )
  351. // callback not needed, Pods has this built-in for delete
  352. )
  353. );
  354. }
  355. // @todo Customize the Add New / Manage links to point to their correct menu items
  356. $ui = apply_filters( 'pods_admin_ui_' . $pod->pod, apply_filters( 'pods_admin_ui', $ui, $pod->pod, $pod ), $pod->pod, $pod );
  357. pods_ui( $ui );
  358. }
  359. /**
  360. * Add media button for Pods shortcode
  361. *
  362. * @param $context
  363. *
  364. * @return string
  365. */
  366. public function media_button ( $context = null ) {
  367. $current_page = basename( $_SERVER[ 'PHP_SELF' ] );
  368. $current_page = explode( '?', $current_page );
  369. $current_page = explode( '#', $current_page[ 0 ] );
  370. $current_page = $current_page[ 0 ];
  371. // Only show the button on post type pages
  372. if ( !in_array( $current_page, array( 'post-new.php', 'post.php' ) ) )
  373. return $context;
  374. add_action( 'admin_footer', array( $this, 'mce_popup' ) );
  375. echo '<a href="#TB_inline?width=640&inlineId=pods_shortcode_form" class="thickbox" id="add_pod_button" title="Pods Shortcode"><img src="' . PODS_URL . 'ui/images/icon16.png" alt="Pods Shortcode" /></a>';
  376. }
  377. /**
  378. * Enqueue assets for Media Library Popup
  379. */
  380. public function register_media_assets () {
  381. if ( 'pods_media_attachment' == pods_var( 'inlineId', 'get' ) )
  382. wp_enqueue_style( 'pods-attach' );
  383. }
  384. /**
  385. * Output Pods shortcode popup window
  386. */
  387. public function mce_popup () {
  388. pods_view( PODS_DIR . 'ui/admin/shortcode.php', compact( array_keys( get_defined_vars() ) ) );
  389. }
  390. /**
  391. * Handle main Pods Setup area for managing Pods and Fields
  392. */
  393. public function admin_setup () {
  394. $pods = $this->api->load_pods();
  395. $view = pods_var( 'view', 'get', 'all', null, true );
  396. if ( empty( $pods ) && !isset( $_GET[ 'action' ] ) )
  397. $_GET[ 'action' ] = 'add';
  398. if ( 'pods-add-new' == $_GET[ 'page' ] ) {
  399. if ( isset( $_GET[ 'action' ] ) && 'add' != $_GET[ 'action' ] )
  400. pods_redirect( pods_var_update( array( 'page' => 'pods', 'action' => $_GET[ 'action' ] ) ) );
  401. else
  402. $_GET[ 'action' ] = 'add';
  403. }
  404. elseif ( isset( $_GET[ 'action' ] ) && 'add' == $_GET[ 'action' ] )
  405. pods_redirect( pods_var_update( array( 'page' => 'pods-add-new', 'action' => '' ) ) );
  406. $types = array(
  407. 'post_type' => __( 'Post Type (extended)', 'pods' ),
  408. 'taxonomy' => __( 'Taxonomy (extended)', 'pods' ),
  409. 'cpt' => __( 'Custom Post Type', 'pods' ),
  410. 'ct' => __( 'Custom Taxonomy', 'pods' ),
  411. 'user' => __( 'User (extended)', 'pods' ),
  412. 'media' => __( 'Media (extended)', 'pods' ),
  413. 'comment' => __( 'Comments (extended)', 'pods' ),
  414. 'pod' => __( 'Advanced Content Type', 'pods' )
  415. );
  416. $row = false;
  417. $pod_types_found = array();
  418. foreach ( $pods as $k => &$pod ) {
  419. if ( isset( $types[ $pod[ 'type' ] ] ) ) {
  420. if ( in_array( $pod[ 'type' ], array( 'post_type', 'taxonomy' ) ) ) {
  421. if ( empty( $pod[ 'object' ] ) ) {
  422. if ( 'post_type' == $pod[ 'type' ] )
  423. $pod[ 'type' ] = 'cpt';
  424. else
  425. $pod[ 'type' ] = 'ct';
  426. }
  427. }
  428. if ( !isset( $pod_types_found[ $pod[ 'type' ] ] ) )
  429. $pod_types_found[ $pod[ 'type' ] ] = 1;
  430. else
  431. $pod_types_found[ $pod[ 'type' ] ]++;
  432. if ( 'all' != $view && $view != $pod[ 'type' ] ) {
  433. unset( $pods[ $k ] );
  434. continue;
  435. }
  436. $pod[ 'type' ] = $types[ $pod[ 'type' ] ];
  437. }
  438. elseif ( 'all' != $view )
  439. continue;
  440. $pod[ 'storage' ] = ucwords( $pod[ 'storage' ] );
  441. if ( $pod[ 'id' ] == pods_var( 'id' ) && 'delete' != pods_var( 'action' ) )
  442. $row = $pod;
  443. }
  444. if ( false === $row && 0 < pods_var( 'id' ) && 'delete' != pods_var( 'action' ) ) {
  445. pods_message( 'Pod not found', 'error' );
  446. unset( $_GET[ 'id' ] );
  447. unset( $_GET[ 'action' ] );
  448. }
  449. $ui = array(
  450. 'data' => $pods,
  451. 'row' => $row,
  452. 'total' => count( $pods ),
  453. 'total_found' => count( $pods ),
  454. 'icon' => PODS_URL . 'ui/images/icon32.png',
  455. 'items' => 'Pods',
  456. 'item' => 'Pod',
  457. 'fields' => array(
  458. 'manage' => array(
  459. 'label' => array( 'label' => __( 'Label', 'pods' ) ),
  460. 'name' => array( 'label' => __( 'Name', 'pods' ) ),
  461. 'type' => array( 'label' => __( 'Type', 'pods' ) ),
  462. 'storage' => array( 'label' => __( 'Storage Type', 'pods' ) )
  463. )
  464. ),
  465. 'actions_disabled' => array( 'duplicate', 'view', 'export' ),
  466. 'actions_custom' => array(
  467. 'add' => array( $this, 'admin_setup_add' ),
  468. 'edit' => array( $this, 'admin_setup_edit' ),
  469. 'reset' => array(
  470. 'label' => __( 'Delete All Items', 'pods' ),
  471. 'confirm' => __( 'Are you sure you want to delete all items from this Pod?', 'pods' ),
  472. 'callback' => array( $this, 'admin_setup_reset' )
  473. ),
  474. 'delete' => array( $this, 'admin_setup_delete' )
  475. ),
  476. 'action_links' => array(
  477. 'add' => pods_var_update( array( 'page' => 'pods-add-new', 'action' => '' ) )
  478. ),
  479. 'search' => false,
  480. 'searchable' => false,
  481. 'sortable' => true,
  482. 'pagination' => false
  483. );
  484. if ( 1 < count( $pod_types_found ) ) {
  485. $ui[ 'views' ] = array( 'all' => __( 'All', 'pods' ) );
  486. $ui[ 'view' ] = $view;
  487. $ui[ 'heading' ] = array( 'views' => __( 'Type', 'pods' ) );
  488. $ui[ 'filters_enhanced' ] = true;
  489. foreach ( $pod_types_found as $pod_type => $number_found ) {
  490. $ui[ 'views' ][ $pod_type ] = $types[ $pod_type ];
  491. }
  492. }
  493. pods_ui( $ui );
  494. }
  495. /**
  496. * Get the add page of an object
  497. *
  498. * @param $obj
  499. */
  500. public function admin_setup_add ( $obj ) {
  501. pods_view( PODS_DIR . 'ui/admin/setup-add.php', compact( array_keys( get_defined_vars() ) ) );
  502. }
  503. /**
  504. * Get the edit page of an object
  505. *
  506. * @param $duplicate
  507. * @param $obj
  508. */
  509. public function admin_setup_edit ( $duplicate, $obj ) {
  510. pods_view( PODS_DIR . 'ui/admin/setup-edit.php', compact( array_keys( get_defined_vars() ) ) );
  511. }
  512. /**
  513. * Delete a pod
  514. *
  515. * @param $id
  516. * @param $obj
  517. *
  518. * @return mixed
  519. */
  520. public function admin_setup_delete ( $id, &$obj ) {
  521. $pod = $this->api->load_pod( array( 'id' => $id ), false );
  522. if ( empty( $pod ) )
  523. return $obj->error( __( 'Pod not found.', 'pods' ) );
  524. $this->api->delete_pod( array( 'id' => $id ) );
  525. unset( $obj->data[ $pod[ 'id' ] ] );
  526. $obj->total = count( $obj->data );
  527. $obj->total_found = count( $obj->data );
  528. $obj->message( __( 'Pod deleted successfully.', 'pods' ) );
  529. }
  530. /**
  531. * Reset a pod
  532. *
  533. * @param $id
  534. * @param $obj
  535. *
  536. * @return mixed
  537. */
  538. public function admin_setup_reset ( &$obj, $id ) {
  539. $pod = $this->api->load_pod( array( 'id' => $id ), false );
  540. if ( empty( $pod ) )
  541. return $obj->error( __( 'Pod not found.', 'pods' ) );
  542. $this->api->reset_pod( array( 'id' => $id ) );
  543. $obj->message( __( 'Pod reset successfully.', 'pods' ) );
  544. $obj->manage();
  545. }
  546. /**
  547. * Get advanced administration view.
  548. */
  549. public function admin_advanced () {
  550. pods_view( PODS_DIR . 'ui/admin/advanced.php', compact( array_keys( get_defined_vars() ) ) );
  551. }
  552. /**
  553. * Get settings administration view
  554. */
  555. public function admin_settings () {
  556. pods_view( PODS_DIR . 'ui/admin/settings.php', compact( array_keys( get_defined_vars() ) ) );
  557. }
  558. /**
  559. * Get components administration UI
  560. */
  561. public function admin_components () {
  562. $components = PodsInit::$components->components;
  563. $view = pods_var( 'view', 'get', 'all', null, true );
  564. foreach ( $components as $component => &$component_data ) {
  565. if ( 'all' != $view && ( !isset( $component_data[ 'Category' ] ) || $view != sanitize_title( $component_data[ 'Category' ] ) ) ) {
  566. unset( $components[ $component ] );
  567. continue;
  568. }
  569. $component_data[ 'Name' ] = strip_tags( $component_data[ 'Name' ] );
  570. $meta = array();
  571. if ( !empty( $component_data[ 'Version' ] ) )
  572. $meta[] = 'Version ' . $component_data[ 'Version' ];
  573. if ( empty( $component_data[ 'Author' ] ) ) {
  574. $component_data[ 'Author' ] = 'Pods Framework Team';
  575. $component_data[ 'AuthorURI' ] = 'http://pods.io/';
  576. }
  577. if ( !empty( $component_data[ 'AuthorURI' ] ) )
  578. $component_data[ 'Author' ] = '<a href="' . $component_data[ 'AuthorURI' ] . '">' . $component_data[ 'Author' ] . '</a>';
  579. $meta[] = sprintf( __( 'by %s', 'pods' ), $component_data[ 'Author' ] );
  580. if ( !empty( $component_data[ 'URI' ] ) )
  581. $meta[] = '<a href="' . $component_data[ 'URI' ] . '">' . __( 'Visit component site', 'pods' ) . '</a>';
  582. $component_data[ 'Description' ] = wpautop( make_clickable( strip_tags( $component_data[ 'Description' ], 'em,strong' ) ) );
  583. if ( !empty( $meta ) )
  584. $component_data[ 'Description' ] .= '<div class="pods-component-version-author-uri">' . implode( ' | ', $meta ) . '</div>';
  585. $component_data = array(
  586. 'id' => $component_data[ 'ID' ],
  587. 'name' => $component_data[ 'Name' ],
  588. 'category' => $component_data[ 'Category' ],
  589. 'description' => $component_data[ 'Description' ],
  590. 'developermode' => pods_var_raw( 'DeveloperMode', $component_data, false ),
  591. 'mustuse' => pods_var_raw( 'MustUse', $component_data, false ),
  592. 'toggle' => 0
  593. );
  594. if ( $component_data[ 'developermode' ] ) {
  595. if ( !pods_developer() ) {
  596. unset( $components[ $component ] );
  597. continue;
  598. }
  599. $component_data[ 'name' ] .= ' <em style="font-weight: normal;">(Developer Preview)</em>';
  600. }
  601. if ( isset( PodsInit::$components->settings[ 'components' ][ $component_data[ 'id' ] ] ) && 0 != PodsInit::$components->settings[ 'components' ][ $component_data[ 'id' ] ] )
  602. $component_data[ 'toggle' ] = 1;
  603. elseif ( $component_data[ 'mustuse' ] )
  604. $component_data[ 'toggle' ] = 1;
  605. }
  606. $ui = array(
  607. 'data' => $components,
  608. 'total' => count( $components ),
  609. 'total_found' => count( $components ),
  610. 'icon' => PODS_URL . 'ui/images/icon32.png',
  611. 'items' => 'Components',
  612. 'item' => 'Component',
  613. 'fields' => array(
  614. 'manage' => array(
  615. 'name' => array(
  616. 'label' => __( 'Name', 'pods' ),
  617. 'width' => '30%',
  618. 'type' => 'text'
  619. ),
  620. 'category' => array(
  621. 'label' => __( 'Category', 'pods' ),
  622. 'width' => '10%',
  623. 'type' => 'text'
  624. ),
  625. 'description' => array(
  626. 'label' => __( 'Description', 'pods' ),
  627. 'width' => '60%',
  628. 'type' => 'text'
  629. )
  630. )
  631. ),
  632. 'actions_disabled' => array( 'duplicate', 'view', 'export', 'add', 'edit', 'delete' ),
  633. 'actions_custom' => array(
  634. 'toggle' => array( 'callback' => array( $this, 'admin_components_toggle' ) )
  635. ),
  636. 'filters_enhanced' => true,
  637. 'views' => array(
  638. 'all' => __( 'All', 'pods' ),
  639. 'field-types' => __( 'Field Types', 'pods' ),
  640. 'tools' => __( 'Tools', 'pods' ),
  641. 'integration' => __( 'Integration', 'pods' ),
  642. 'migration' => __( 'Migration', 'pods' ),
  643. 'advanced' => __( 'Advanced', 'pods' )
  644. ),
  645. 'view' => $view,
  646. 'heading' => array(
  647. 'views' => __( 'Category', 'pods' )
  648. ),
  649. 'search' => false,
  650. 'searchable' => false,
  651. 'sortable' => false,
  652. 'pagination' => false
  653. );
  654. pods_ui( $ui );
  655. }
  656. /**
  657. * Toggle a component on or off
  658. *
  659. * @param PodsUI $ui
  660. *
  661. * @return bool
  662. */
  663. public function admin_components_toggle ( PodsUI $ui ) {
  664. $component = $_GET[ 'id' ];
  665. if ( !empty( PodsInit::$components->components[ $component ][ 'PluginDependency' ] ) ) {
  666. $dependency = explode( '|', PodsInit::$components->components[ $component ][ 'PluginDependency' ] );
  667. if ( !pods_is_plugin_active( $dependency[ 1 ] ) ) {
  668. $website = 'http://wordpress.org/extend/plugins/' . dirname( $dependency[ 1 ] ) . '/';
  669. if ( isset( $dependency[ 2 ] ) )
  670. $website = $dependency[ 2 ];
  671. if ( !empty( $website ) )
  672. $website = ' ' . sprintf( __( 'You can find it at %s', 'pods' ), '<a href="' . $website . '" target="_blank">' . $website . '</a>' );
  673. $message = sprintf( __( 'The %s component requires that you have the <strong>%s</strong> plugin installed and activated.', 'pods' ), PodsInit::$components->components[ $component ][ 'Name' ], $dependency[ 0 ] ) . $website;
  674. $ui->error( $message );
  675. $ui->manage();
  676. return;
  677. }
  678. }
  679. if ( !empty( PodsInit::$components->components[ $component ][ 'ThemeDependency' ] ) ) {
  680. $dependency = explode( '|', PodsInit::$components->components[ $component ][ 'ThemeDependency' ] );
  681. if ( strtolower( $dependency[ 1 ] ) != strtolower( get_template() ) && strtolower( $dependency[ 1 ] ) != strtolower( get_stylesheet() ) ) {
  682. $website = '';
  683. if ( isset( $dependency[ 2 ] ) )
  684. $website = ' ' . sprintf( __( 'You can find it at %s', 'pods' ), '<a href="' . $dependency[ 2 ] . '" target="_blank">' . $dependency[ 2 ] . '</a>' );
  685. $message = sprintf( __( 'The %s component requires that you have the <strong>%s</strong> theme installed and activated.', 'pods' ), PodsInit::$components->components[ $component ][ 'Name' ], $dependency[ 0 ] ) . $website;
  686. $ui->error( $message );
  687. $ui->manage();
  688. return;
  689. }
  690. }
  691. if ( !empty( PodsInit::$components->components[ $component ][ 'MustUse' ] ) ) {
  692. $message = sprintf( __( 'The %s component can not be disabled from here. You must deactivate the plugin or theme that added it.', 'pods' ), PodsInit::$components->components[ $component ][ 'Name' ] );
  693. $ui->error( $message );
  694. $ui->manage();
  695. return;
  696. }
  697. if ( 1 == pods_var( 'toggled' ) ) {
  698. $toggle = PodsInit::$components->toggle( $component );
  699. if ( true === $toggle )
  700. $ui->message( PodsInit::$components->components[ $component ][ 'Name' ] . ' ' . __( 'Component enabled', 'pods' ) );
  701. elseif ( false === $toggle )
  702. $ui->message( PodsInit::$components->components[ $component ][ 'Name' ] . ' ' . __( 'Component disabled', 'pods' ) );
  703. $components = PodsInit::$components->components;
  704. foreach ( $components as $component => &$component_data ) {
  705. $toggle = 0;
  706. if ( isset( PodsInit::$components->settings[ 'components' ][ $component_data[ 'ID' ] ] ) ) {
  707. if ( 0 != PodsInit::$components->settings[ 'components' ][ $component_data[ 'ID' ] ] )
  708. $toggle = 1;
  709. }
  710. if ( true === $component_data[ 'DeveloperMode' ] ) {
  711. if ( !pods_developer() ) {
  712. unset( $components[ $component ] );
  713. continue;
  714. }
  715. }
  716. $component_data = array(
  717. 'id' => $component_data[ 'ID' ],
  718. 'name' => $component_data[ 'Name' ],
  719. 'description' => make_clickable( $component_data[ 'Description' ] ),
  720. 'version' => $component_data[ 'Version' ],
  721. 'author' => $component_data[ 'Author' ],
  722. 'toggle' => $toggle
  723. );
  724. }
  725. $ui->data = $components;
  726. pods_transient_clear( 'pods_components' );
  727. $url = pods_var_update( array( 'toggled' => null ) );
  728. pods_redirect( $url );
  729. }
  730. elseif ( 1 == pods_var( 'toggle' ) )
  731. $ui->message( PodsInit::$components->components[ $component ][ 'Name' ] . ' ' . __( 'Component enabled', 'pods' ) );
  732. else
  733. $ui->message( PodsInit::$components->components[ $component ][ 'Name' ] . ' ' . __( 'Component disabled', 'pods' ) );
  734. $ui->manage();
  735. }
  736. /**
  737. * Get the admin upgrade page
  738. */
  739. public function admin_upgrade () {
  740. foreach ( PodsInit::$upgrades as $old_version => $new_version ) {
  741. if ( version_compare( $old_version, PodsInit::$version_last, '<=' ) && version_compare( PodsInit::$version_last, $new_version, '<' ) ) {
  742. $new_version = str_replace( '.', '_', $new_version );
  743. pods_view( PODS_DIR . 'ui/admin/upgrade/upgrade_' . $new_version . '.php', compact( array_keys( get_defined_vars() ) ) );
  744. break;
  745. }
  746. }
  747. }
  748. /**
  749. * Get the admin help page
  750. */
  751. public function admin_help () {
  752. pods_view( PODS_DIR . 'ui/admin/help.php', compact( array_keys( get_defined_vars() ) ) );
  753. }
  754. /**
  755. * Add pods specific capabilities.
  756. *
  757. * @param $capabilities List of extra capabilities to add
  758. *
  759. * @return array
  760. */
  761. public function admin_capabilities ( $capabilities ) {
  762. $pods = $this->api->load_pods();
  763. $capabilities[] = 'pods';
  764. $capabilities[] = 'pods_content';
  765. $capabilities[] = 'pods_settings';
  766. $capabilities[] = 'pods_components';
  767. foreach ( $pods as $pod ) {
  768. if ( !in_array( $pod[ 'type' ], array( 'pod', 'table' ) ) )
  769. continue;
  770. $capabilities[] = 'pods_add_' . $pod[ 'name' ];
  771. $capabilities[] = 'pods_edit_' . $pod[ 'name' ];
  772. if ( isset( $pod[ 'fields' ][ 'author' ] ) && 'pick' == $pod[ 'fields' ][ 'author' ][ 'type' ] && 'user' == $pod[ 'fields' ][ 'author' ][ 'pick_object' ] )
  773. $capabilities[] = 'pods_edit_others_' . $pod[ 'name' ];
  774. $capabilities[] = 'pods_delete_' . $pod[ 'name' ];
  775. if ( isset( $pod[ 'fields' ][ 'author' ] ) && 'pick' == $pod[ 'fields' ][ 'author' ][ 'type' ] && 'user' == $pod[ 'fields' ][ 'author' ][ 'pick_object' ] )
  776. $capabilities[] = 'pods_delete_others_' . $pod[ 'name' ];
  777. if ( 1 == pods_var( 'ui_export', $pod[ 'options' ], 0 ) )
  778. $capabilities[] = 'pods_export_' . $pod[ 'name' ];
  779. }
  780. return $capabilities;
  781. }
  782. /**
  783. * Handle ajax calls for the administration
  784. */
  785. public function admin_ajax () {
  786. if ( false === headers_sent() ) {
  787. if ( '' == session_id() )
  788. @session_start();
  789. header( 'Content-Type: text/html; charset=' . get_bloginfo( 'charset' ) );
  790. }
  791. // Sanitize input
  792. $params = stripslashes_deep( (array) $_POST );
  793. foreach ( $params as $key => $value ) {
  794. if ( 'action' == $key )
  795. continue;
  796. unset( $params[ $key ] );
  797. $params[ str_replace( '_podsfix_', '', $key ) ] = $value;
  798. }
  799. $params = (object) $params;
  800. $methods = array(
  801. 'add_pod' => array( 'priv' => true ),
  802. 'save_pod' => array( 'priv' => true ),
  803. 'load_sister_fields' => array( 'priv' => true ),
  804. 'process_form' => array( 'custom_nonce' => true ), // priv handled through nonce
  805. 'upgrade' => array( 'priv' => true )
  806. );
  807. $methods = apply_filters( 'pods_admin_ajax_methods', $methods, $this );
  808. if ( !isset( $params->method ) || !isset( $methods[ $params->method ] ) )
  809. pods_error( 'Invalid AJAX request', $this );
  810. $defaults = array(
  811. 'priv' => null,
  812. 'name' => $params->method,
  813. 'custom_nonce' => null
  814. );
  815. $method = (object) array_merge( $defaults, (array) $methods[ $params->method ] );
  816. if ( true !== $method->custom_nonce && ( !isset( $params->_wpnonce ) || false === wp_verify_nonce( $params->_wpnonce, 'pods-' . $params->method ) ) )
  817. pods_error( __( 'Unauthorized request', 'pods' ), $this );
  818. // Cleaning up $params
  819. unset( $params->action );
  820. unset( $params->method );
  821. if ( true !== $method->custom_nonce )
  822. unset( $params->_wpnonce );
  823. // Check permissions (convert to array to support multiple)
  824. if ( !empty( $method->priv ) && !is_super_admin() && !current_user_can( 'delete_users' ) && !current_user_can( 'pods' ) ) {
  825. if ( true !== $method->priv ) {
  826. foreach ( (array) $method->priv as $priv_val ) {
  827. if ( !current_user_can( $priv_val ) )
  828. pods_error( __( 'Access denied', 'pods' ), $this );
  829. }
  830. }
  831. }
  832. $params->method = $method->name;
  833. $params = apply_filters( 'pods_api_' . $method->name, $params, $method );
  834. if ( 'upgrade' == $method->name )
  835. $output = (string) pods_upgrade( $params->version )->ajax( $params );
  836. else {
  837. if ( !method_exists( $this->api, $method->name ) )
  838. pods_error( 'API method does not exist', $this );
  839. elseif ( 'save_pod' == $method->name ) {
  840. if ( isset( $params->field_data_json ) && is_array( $params->field_data_json ) ) {
  841. $params->fields = $params->field_data_json;
  842. unset( $params->field_data_json );
  843. foreach ( $params->fields as $k => $v ) {
  844. if ( empty( $v ) )
  845. unset( $params->fields[ $k ] );
  846. elseif ( !is_array( $v ) )
  847. $params->fields[ $k ] = (array) @json_decode( $v, true );
  848. }
  849. }
  850. }
  851. // Dynamically call the API method
  852. $params = (array) $params;
  853. $output = call_user_func( array( $this->api, $method->name ), $params );
  854. }
  855. // Output in json format
  856. if ( false !== $output ) {
  857. if ( is_array( $output ) || is_object( $output ) )
  858. echo json_encode( $output );
  859. else
  860. echo $output;
  861. }
  862. else
  863. pods_error( 'There was a problem with your request.' );
  864. die(); // KBAI!
  865. }
  866. /**
  867. * Handle ajax upload
  868. */
  869. public function admin_ajax_upload () {
  870. if ( false === headers_sent() ) {
  871. if ( '' == session_id() )
  872. @session_start();
  873. header( 'Content-Type: text/html; charset=' . get_bloginfo( 'charset' ) );
  874. }
  875. // Sanitize input
  876. $params = stripslashes_deep( (array) $_POST );
  877. foreach ( $params as $key => $value ) {
  878. if ( 'action' == $key )
  879. continue;
  880. unset( $params[ $key ] );
  881. $params[ str_replace( '_podsfix_', '', $key ) ] = $value;
  882. }
  883. $params = (object) $params;
  884. $methods = array(
  885. 'upload',
  886. );
  887. if ( !isset( $params->method ) || !in_array( $params->method, $methods ) || !isset( $params->pod ) || !isset( $params->field ) || !isset( $params->uri ) || empty( $params->uri ) )
  888. pods_error( 'Invalid AJAX request', $this );
  889. elseif ( !empty( $params->pod ) && empty( $params->field ) )
  890. pods_error( 'Invalid AJAX request', $this );
  891. elseif ( empty( $params->pod ) && !current_user_can( 'upload_files' ) )
  892. pods_error( 'Invalid AJAX request', $this );
  893. // Flash often fails to send cookies with the POST or upload, so we need to pass it in GET or POST instead
  894. if ( is_ssl() && empty( $_COOKIE[ SECURE_AUTH_COOKIE ] ) && !empty( $_REQUEST[ 'auth_cookie' ] ) )
  895. $_COOKIE[ SECURE_AUTH_COOKIE ] = $_REQUEST[ 'auth_cookie' ];
  896. elseif ( empty( $_COOKIE[ AUTH_COOKIE ] ) && !empty( $_REQUEST[ 'auth_cookie' ] ) )
  897. $_COOKIE[ AUTH_COOKIE ] = $_REQUEST[ 'auth_cookie' ];
  898. if ( empty( $_COOKIE[ LOGGED_IN_COOKIE ] ) && !empty( $_REQUEST[ 'logged_in_cookie' ] ) )
  899. $_COOKIE[ LOGGED_IN_COOKIE ] = $_REQUEST[ 'logged_in_cookie' ];
  900. global $current_user;
  901. unset( $current_user );
  902. /**
  903. * Access Checking
  904. */
  905. $upload_disabled = false;
  906. if ( defined( 'PODS_DISABLE_FILE_UPLOAD' ) && true === PODS_DISABLE_FILE_UPLOAD )
  907. $upload_disabled = true;
  908. elseif ( defined( 'PODS_UPLOAD_REQUIRE_LOGIN' ) && is_bool( PODS_UPLOAD_REQUIRE_LOGIN ) && true === PODS_UPLOAD_REQUIRE_LOGIN && !is_user_logged_in() )
  909. $upload_disabled = true;
  910. elseif ( defined( 'PODS_UPLOAD_REQUIRE_LOGIN' ) && !is_bool( PODS_UPLOAD_REQUIRE_LOGIN ) && ( !is_user_logged_in() || !current_user_can( PODS_UPLOAD_REQUIRE_LOGIN ) ) )
  911. $upload_disabled = true;
  912. $uid = @session_id();
  913. if ( is_user_logged_in() )
  914. $uid = 'user_' . get_current_user_id();
  915. $nonce_check = 'pods_upload_' . (int) $params->pod . '_' . $uid . '_' . $params->uri . '_' . (int) $params->field;
  916. if ( true === $upload_disabled || !isset( $params->_wpnonce ) || false === wp_verify_nonce( $params->_wpnonce, $nonce_check ) )
  917. pods_error( __( 'Unauthorized request', 'pods' ), $this );
  918. $pod = array();
  919. $field = array(
  920. 'type' => 'file',
  921. 'options' => array()
  922. );
  923. if ( !empty( $params->pod ) ) {
  924. $pod = $this->api->load_pod( array( 'id' => (int) $params->pod ) );
  925. $field = $this->api->load_field( array( 'id' => (int) $params->field ) );
  926. if ( empty( $pod ) || empty( $field ) || $pod[ 'id' ] != $field[ 'pod_id' ] || !isset( $pod[ 'fields' ][ $field[ 'name' ] ] ) )
  927. pods_error( __( 'Invalid field request', 'pods' ), $this );
  928. if ( !in_array( $field[ 'type' ], apply_filters( 'pods_file_field_types', array( 'file', 'avatar' ) ) ) )
  929. pods_error( __( 'Invalid field', 'pods' ), $this );
  930. }
  931. $method = $params->method;
  932. // Cleaning up $params
  933. unset( $params->action );
  934. unset( $params->method );
  935. unset( $params->_wpnonce );
  936. $params->post_id = pods_var( 'post_id', $params, 0, null, true );
  937. /**
  938. * Upload a new file (advanced - returns URL and ID)
  939. */
  940. if ( 'upload' == $method ) {
  941. $file = $_FILES[ 'Filedata' ];
  942. $limit_size = pods_var( $field[ 'type' ] . '_restrict_filesize', $field[ 'options' ] );
  943. if ( !empty( $limit_size ) ) {
  944. if ( false !== stripos( $limit_size, 'MB' ) ) {
  945. $limit_size = (float) trim( str_ireplace( 'MB', '', $limit_size ) );
  946. $limit_size = $limit_size * 1025 * 1025; // convert to KB to B
  947. }
  948. elseif ( false !== stripos( $limit_size, 'KB' ) ) {
  949. $limit_size = (float) trim( str_ireplace( 'KB', '', $limit_size ) );
  950. $limit_size = $limit_size * 1025 * 1025; // convert to B
  951. }
  952. elseif ( false !== stripos( $limit_size, 'GB' ) ) {
  953. $limit_size = (float) trim( str_ireplace( 'GB', '', $limit_size ) );
  954. $limit_size = $limit_size * 1025 * 1025 * 1025; // convert to MB to KB to B
  955. }
  956. elseif ( false !== stripos( $limit_size, 'B' ) )
  957. $limit_size = (float) trim( str_ireplace( 'B', '', $limit_size ) );
  958. else
  959. $limit_size = wp_max_upload_size();
  960. if ( 0 < $limit_size && $limit_size < $file[ 'size' ] ) {
  961. $error = __( 'File size too large, max size is %s', 'pods' );
  962. $error = sprintf( $error, pods_var( $field[ 'type' ] . '_restrict_filesize', $field[ 'options' ] ) );
  963. pods_error( '<div style="color:#FF0000">Error: ' . $error . '</div>' );
  964. }
  965. }
  966. $limit_file_type = pods_var( $field[ 'type' ] . '_type', $field[ 'options' ], 'images' );
  967. if ( 'images' == $limit_file_type )
  968. $limit_types = 'jpg,png,gif';
  969. elseif ( 'video' == $limit_file_type )
  970. $limit_types = 'mpg,mov,flv,mp4';
  971. elseif ( 'audio' == $limit_file_type )
  972. $limit_types = 'mp3,m4a,wav,wma';
  973. elseif ( 'text' == $limit_file_type )
  974. $limit_types = 'txt,rtx,csv,tsv';
  975. elseif ( 'any' == $limit_file_type )
  976. $limit_types = '';
  977. else
  978. $limit_types = pods_var( $field[ 'type' ] . '_allowed_extensions', $field[ 'options' ], '', null, true );
  979. $limit_types = trim( str_replace( array( ' ', '.', "\n", "\t", ';' ), array( '', ',', ',', ',' ), $limit_types ), ',' );
  980. if ( pods_wp_version( '3.5' ) ) {
  981. $mime_types = wp_get_mime_types();
  982. if ( in_array( $limit_file_type, array( 'images', 'audio', 'video' ) ) ) {
  983. $new_limit_types = array();
  984. foreach ( $mime_types as $type => $mime ) {
  985. if ( 0 === strpos( $mime, $limit_file_type ) ) {
  986. $type = explode( '|', $type );
  987. $new_limit_types = array_merge( $new_limit_types, $type );
  988. }
  989. }
  990. if ( !empty( $new_limit_types ) )
  991. $limit_types = implode( ',', $new_limit_types );
  992. }
  993. elseif ( 'any' != $limit_file_type ) {
  994. $new_limit_types = array();
  995. $limit_types = explode( ',', $limit_types );
  996. foreach ( $limit_types as $k => $limit_type ) {
  997. $found = false;
  998. foreach ( $mime_types as $type => $mime ) {
  999. if ( 0 === strpos( $mime, $limit_type ) ) {
  1000. $type = explode( '|', $type );
  1001. foreach ( $type as $t ) {
  1002. if ( !in_array( $t, $new_limit_types ) )
  1003. $new_limit_types[] = $t;
  1004. }
  1005. $found = true;
  1006. }
  1007. }
  1008. if ( !$found )
  1009. $new_limit_types[] = $limit_type;
  1010. }
  1011. if ( !empty( $new_limit_types ) )
  1012. $limit_types = implode( ', ', $new_limit_types );
  1013. }
  1014. }
  1015. $limit_types = explode( ',', $limit_types );
  1016. $limit_types = array_filter( array_unique( $limit_types ) );
  1017. if ( !empty( $limit_types ) ) {
  1018. $ok = false;
  1019. foreach ( $limit_types as $limit_type ) {
  1020. $limit_type = '.' . trim( $limit_type, ' .' );
  1021. if ( $limit_type)
  1022. $pos = ( strlen( $file[ 'name' ] ) - strlen( $limit_type ) );
  1023. if ( $pos === stripos( $file[ 'name' ], $limit_type ) ) {
  1024. $ok = true;
  1025. break;
  1026. }
  1027. }
  1028. if ( false === $ok ) {
  1029. $error = __( 'File type not allowed, please use one of the following: %s', 'pods' );
  1030. $error = sprintf( $error, '.' . implode( ', .', $limit_types ) );
  1031. pods_error( '<div style="color:#FF0000">Error: ' . $error . '</div>' );
  1032. }
  1033. }
  1034. $custom_handler = apply_filters( 'pods_upload_handle', null, 'Filedata', $params->post_id, $params );
  1035. if ( null === $custom_handler ) {
  1036. $attachment_id = media_handle_upload( 'Filedata', $params->post_id );
  1037. if ( is_object( $attachment_id ) ) {
  1038. $errors = array();
  1039. foreach ( $attachment_id->errors[ 'upload_error' ] as $error_code => $error_message ) {
  1040. $errors[] = '[' . $error_code . '] ' . $error_message;
  1041. }
  1042. pods_error( '<div style="color:#FF0000">Error: ' . implode( '</div><div>', $errors ) . '</div>' );
  1043. }
  1044. else {
  1045. $attachment = get_post( $attachment_id, ARRAY_A );
  1046. $attachment[ 'filename' ] = basename( $attachment[ 'guid' ] );
  1047. $thumb = wp_get_attachment_image_src( $attachment[ 'ID' ], 'thumbnail', true );
  1048. $attachment[ 'thumbnail' ] = $thumb[ 0 ];
  1049. $attachment = apply_filters( 'pods_upload_attachment', $attachment, $params->post_id );
  1050. echo json_encode( $attachment );
  1051. }
  1052. }
  1053. }
  1054. die(); // KBAI!
  1055. }
  1056. /**
  1057. * handle ajax relationship
  1058. */
  1059. public function admin_ajax_relationship () {
  1060. global $wpdb, $polylang;
  1061. if ( false === headers_sent() ) {
  1062. if ( '' == session_id() )
  1063. @session_start();
  1064. header( 'Content-Type: text/html; charset=' . get_bloginfo( 'charset' ) );
  1065. }
  1066. // Sanitize input
  1067. $params = stripslashes_deep( (array) $_POST );
  1068. foreach ( $params as $key => $value ) {
  1069. if ( 'action' == $key )
  1070. continue;
  1071. unset( $params[ $key ] );
  1072. $params[ str_replace( '_podsfix_', '', $key ) ] = $value;
  1073. }
  1074. $params = (object) $params;
  1075. $uid = @session_id();
  1076. if ( is_user_logged_in() )
  1077. $uid = 'user_' . get_current_user_id();
  1078. $nonce_check = 'pods_relationship_' . (int) $params->pod . '_' . $uid . '_' . $params->uri . '_' . (int) $params->field;
  1079. if ( !isset( $params->_wpnonce ) || false === wp_verify_nonce( $params->_wpnonce, $nonce_check ) )
  1080. pods_error( __( 'Unauthorized request', 'pods' ), $this );
  1081. $pod = $this->api->load_pod( array( 'id' => (int) $params->pod ) );
  1082. $field = $this->api->load_field( array( 'id' => (int) $params->field, 'table_info' => true ) );
  1083. if ( !isset( $params->query ) || strlen( trim( $params->query ) ) < 1 )
  1084. pods_error( __( 'Invalid field request', 'pods' ), $this );
  1085. elseif ( empty( $pod ) || empty( $field ) || $pod[ 'id' ] != $field[ 'pod_id' ] || !isset( $pod[ 'fields' ][ $field[ 'name' ] ] ) )
  1086. pods_error( __( 'Invalid field request', 'pods' ), $this );
  1087. elseif ( 'pick' != $field[ 'type' ] || empty( $field[ 'table_info' ] ) )
  1088. pods_error( __( 'Invalid field', 'pods' ), $this );
  1089. elseif ( 'single' == pods_var( 'pick_format_type', $field ) && 'autocomplete' == pods_var( 'pick_format_single', $field ) )
  1090. pods_error( __( 'Invalid field', 'pods' ), $this );
  1091. elseif ( 'multi' == pods_var( 'pick_format_type', $field ) && 'autocomplete' == pods_var( 'pick_format_multi', $field ) )
  1092. pods_error( __( 'Invalid field', 'pods' ), $this );
  1093. if ( 'pod' == pods_var( 'pick_object', $field ) && 0 < strlen( pods_var( 'pick_val', $field ) ) ) {
  1094. $data = pods_data( pods_var( 'pick_val', $field ) );
  1095. $where = pods_var_raw( 'pick_where', $field[ 'options' ] );
  1096. if ( !empty( $where ) )
  1097. $where = pods_evaluate_tags( $where, true );
  1098. }
  1099. else {
  1100. $field[ 'table_info' ] = $this->api->get_table_info( pods_var( 'pick_object', $field ), pods_var( 'pick_val', $field ) );
  1101. $data = pods_data();
  1102. $data->table = $field[ 'table_info' ][ 'table' ];
  1103. $data->join = $field[ 'table_info' ][ 'join' ];
  1104. $data->field_id = $field[ 'table_info' ][ 'field_id' ];
  1105. $data->field_index = $field[ 'table_info' ][ 'field_index' ];
  1106. $data->where = $field[ 'table_info' ][ 'where' ];
  1107. $data->orderby = $field[ 'table_info' ][ 'orderby' ];
  1108. $where = pods_var_raw( 'pick_where', $field[ 'options' ], $field[ 'table_info' ][ 'where_default' ], null, true );
  1109. if ( !empty( $where ) && $field[ 'table_info' ][ 'where_default' ] != $where )
  1110. $where = pods_evaluate_tags( $params[ 'where' ], true );
  1111. /* not needed yet
  1112. if ( !empty( $params[ 'orderby' ] ) )
  1113. $params[ 'orderby' ] = pods_evaluate_tags( $params[ 'orderby' ], true );
  1114. if ( !empty( $params[ 'groupby' ] ) )
  1115. $params[ 'groupby' ] = pods_evaluate_tags( $params[ 'groupby' ], true );*/
  1116. }
  1117. if ( empty( $where ) )
  1118. $where = array();
  1119. else
  1120. $where = (array) $where;
  1121. $data_params = array(
  1122. 'select' => "`t`.`{$data->field_id}`, `t`.`{$data->field_index}`",
  1123. 'table' => $data->table,
  1124. 'where' => $where,
  1125. 'groupby' => pods_var_raw( 'pick_groupby', $field[ 'options' ], null, null, true ),
  1126. 'limit' => 30
  1127. );
  1128. $display = trim( pods_var( 'pick_display', $field[ 'options' ] ), ' {@}' );
  1129. if ( 0 < strlen( $display ) ) {
  1130. if ( isset( $options[ 'table_info' ][ 'pod' ] ) && !empty( $options[ 'table_info' ][ 'pod' ] ) ) {
  1131. if ( isset( $options[ 'table_info' ][ 'pod' ][ 'object_fields' ] ) && isset( $options[ 'table_info' ][ 'pod' ][ 'object_fields' ][ $display ] ) ) {
  1132. $data->field_index = $display;
  1133. $data_params[ 'select' ] = "`t`.`{$data->field_id}`, `t`.`{$data->field_index}`";
  1134. }
  1135. elseif ( isset( $options[ 'table_info' ][ 'pod' ][ 'fields' ][ $display ] ) ) {
  1136. $data->field_index = $display;
  1137. if ( 'table' == $options[ 'table_info' ][ 'pod' ][ 'storage' ] && !in_array( $options[ 'table_info' ][ 'pod' ][ 'type' ], array( 'pod', 'table' ) ) )
  1138. $data_params[ 'select' ] = "`t`.`{$data->field_id}`, `d`.`{$data->field_index}`";
  1139. else
  1140. $data_params[ 'select' ] = "`t`.`{$data->field_id}`, `t`.`{$data->field_index}`";
  1141. }
  1142. }
  1143. elseif ( isset( $options[ 'table_info' ][ 'object_fields' ] ) && isset( $options[ 'table_info' ][ 'object_fields' ][ $display ] ) ) {
  1144. $data->field_index = $display;
  1145. $data_params[ 'select' ] = "`t`.`{$data->field_id}`, `t`.`{$data->field_index}`";
  1146. }
  1147. }
  1148. $lookup_where = array(
  1149. "`t`.`{$data->field_index}` LIKE '%" . like_escape( $params->query ) . "%'"
  1150. );
  1151. $extra = '';
  1152. // @todo Hook into WPML for each table
  1153. if ( $wpdb->users == $data->table ) {
  1154. $lookup_where[] = "`t`.`display_name` LIKE '%" . like_escape( $params->query ) . "%'";
  1155. $lookup_where[] = "`t`.`user_login` LIKE '%" . like_escape( $params->query ) . "%'";
  1156. $lookup_where[] = "`t`.`user_email` LIKE '%" . like_escape( $params->query ) . "%'";
  1157. }
  1158. elseif ( $wpdb->posts == $data->table ) {
  1159. $lookup_where[] = "`t`.`post_name` LIKE '%" . like_escape( $params->query ) . "%'";
  1160. $lookup_where[] = "`t`.`post_content` LIKE '%" . like_escape( $params->query ) . "%'";
  1161. $lookup_where[] = "`t`.`post_excerpt` LIKE '%" . like_escape( $params->query ) . "%'";
  1162. $extra = ', `t`.`post_type`';
  1163. }
  1164. elseif ( $wpdb->terms == $data->table ) {
  1165. $lookup_where[] = "`t`.`slug` LIKE '%" . like_escape( $params->query ) . "%'";
  1166. $extra = ', `tt`.`taxonomy`';
  1167. }
  1168. elseif ( $wpdb->comments == $data->table ) {
  1169. $lookup_where[] = "`t`.`comment_content` LIKE '%" . like_escape( $params->query ) . "%'";
  1170. $lookup_where[] = "`t`.`comment_author` LIKE '%" . like_escape( $params->query ) . "%'";
  1171. $lookup_where[] = "`t`.`comment_author_email` LIKE '%" . like_escape( $params->query ) . "%'";
  1172. }
  1173. $where[] = ' ( ' . implode( ' OR ', $lookup_where ) . ' ) ';
  1174. $orderby = array();
  1175. $orderby[] = "(`t`.`{$data->field_index}` LIKE '%" . like_escape( $params->query ) . "%' ) DESC";
  1176. $pick_orderby = pods_var_raw( 'pick_orderby', $field[ 'options' ], null, null, true );
  1177. if ( 0 < strlen( $pick_orderby ) )
  1178. $orderby[] = $pick_orderby;
  1179. $orderby[] = "`t`.`{$data->field_index}`";
  1180. $orderby[] = "`t`.`{$data->field_id}`";
  1181. $data_params[ 'select' ] .= $extra;
  1182. $data_params[ 'orderby' ] = $orderby;
  1183. $results = $data->select( $data_params );
  1184. $items = array();
  1185. $ids = array();
  1186. $pick_data = apply_filters( 'pods_field_pick_data_ajax', array(), $field[ 'name' ], null, $field, $pod, 0, $data );
  1187. if ( !empty( $pick_data ) ) {
  1188. foreach ( $pick_data as $id => $text ) {
  1189. $items[] = array(
  1190. 'id' => $id,
  1191. 'text' => $text
  1192. );
  1193. }
  1194. }
  1195. elseif ( !empty( $results ) ) {
  1196. foreach ( $results as $result ) {
  1197. $result = get_object_vars( $result );
  1198. // WPML integration for Post Types and Taxonomies
  1199. if ( in_array( $data->table, array( $wpdb->posts, $wpdb->terms ) ) && function_exists( 'icl_object_id' ) ) {
  1200. $object = '';
  1201. if ( $wpdb->posts == $data->table )
  1202. $object = $result[ 'post_type' ];
  1203. elseif ( $wpdb->terms == $data->table )
  1204. $object = $result[ 'taxonomy' ];
  1205. $id = icl_object_id( $result[ $data->field_id ], $object, false );
  1206. if ( 0 < $id && !in_array( $id, $ids ) ) {
  1207. $text = trim( $result[ $data->field_index ] );
  1208. if ( $result[ $data->field_id ] != $id ) {
  1209. if ( $wpdb->posts == $data->table )
  1210. $text = trim( get_the_title( $id ) );
  1211. elseif ( $wpdb->terms == $data->table )
  1212. $text = trim( get_term( $id, $object )->name );
  1213. }
  1214. if ( strlen( $text ) < 1 )
  1215. $text = '(No Title)';
  1216. $items[] = array(
  1217. 'id' => $id,
  1218. 'text' => $text
  1219. );
  1220. $ids[] = $id;
  1221. }
  1222. }
  1223. // Polylang integration for Post Types and Taxonomies
  1224. if ( in_array( $data->table, array( $wpdb->posts, $wpdb->terms ) ) && is_object( $polylang ) && method_exists( $polylang, 'get_translation' ) ) {
  1225. $object = '';
  1226. if ( $wpdb->posts == $data->table )
  1227. $object = $result[ 'post_type' ];
  1228. elseif ( $wpdb->terms == $data->table )
  1229. $object = $result[ 'taxonomy' ];
  1230. $id = $polylang->get_translation( $object, $result[ $data->field_id ] );
  1231. if ( 0 < $id && !in_array( $id, $ids ) ) {
  1232. $text = trim( $result[ $data->field_index ] );
  1233. if ( $result[ $data->field_id ] != $id ) {
  1234. if ( $wpdb->posts == $data->table )
  1235. $text = trim( get_the_title( $id ) );
  1236. elseif ( $wpdb->terms == $data->table )
  1237. $text = trim( get_term( $id, $object )->name );
  1238. }
  1239. if ( strlen( $text ) < 1 )
  1240. $text = '(No Title)';
  1241. $items[] = array(
  1242. 'id' => $id,
  1243. 'text' => $text
  1244. );
  1245. $ids[] = $id;
  1246. }
  1247. }
  1248. elseif( !in_array( $result[ $data->field_id ], $ids ) ) {
  1249. $result[ $data->field_index ] = trim( $result[ $data->field_index ] );
  1250. if ( strlen( $result[ $data->field_index ] ) < 1 )
  1251. $result[ $data->field_index ] = '(No Title)';
  1252. $items[] = array(
  1253. 'id' => $result[ $data->field_id ],
  1254. 'text' => $result[ $data->field_index ]
  1255. );
  1256. $ids[] = $result[ $data->field_id ];
  1257. }
  1258. }
  1259. }
  1260. $items = array(
  1261. 'results' => $items
  1262. );
  1263. echo json_encode( $items );
  1264. die(); // KBAI!
  1265. }
  1266. }