PageRenderTime 58ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/classes/PodsMeta.php

https://github.com/ElmsPark/pods
PHP | 1946 lines | 1216 code | 465 blank | 265 comment | 327 complexity | 19272ecb9c715423b5e56fe23c9136e5 MD5 | raw file
Possible License(s): AGPL-1.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * @package Pods
  4. */
  5. class PodsMeta {
  6. /**
  7. * @var PodsAPI
  8. */
  9. private $api;
  10. /**
  11. * @var int
  12. */
  13. public static $object_identifier = -1;
  14. /**
  15. * @var array
  16. */
  17. public static $advanced_content_types = array();
  18. /**
  19. * @var array
  20. */
  21. public static $post_types = array();
  22. /**
  23. * @var array
  24. */
  25. public static $taxonomies = array();
  26. /**
  27. * @var array
  28. */
  29. public static $media = array();
  30. /**
  31. * @var array
  32. */
  33. public static $user = array();
  34. /**
  35. * @var array
  36. */
  37. public static $comment = array();
  38. /**
  39. * @var array
  40. */
  41. public static $queue = array();
  42. /**
  43. * @var array
  44. */
  45. public static $groups = array();
  46. /**
  47. *
  48. */
  49. function __construct () {
  50. $this->api = pods_api();
  51. }
  52. /**
  53. * @return PodsMeta
  54. */
  55. public function init () {
  56. self::$advanced_content_types = $this->api->load_pods( array( 'type' => 'pod' ) );
  57. self::$post_types = $this->api->load_pods( array( 'type' => 'post_type' ) );
  58. self::$taxonomies = $this->api->load_pods( array( 'type' => 'taxonomy' ) );
  59. self::$media = $this->api->load_pods( array( 'type' => 'media' ) );
  60. self::$user = $this->api->load_pods( array( 'type' => 'user' ) );
  61. self::$comment = $this->api->load_pods( array( 'type' => 'comment' ) );
  62. // Handle Post Type Editor (needed for Pods core)
  63. // Loop through and add meta boxes for individual types (can't use this, Tabify doesn't pick it up)
  64. /*
  65. foreach ( self::$post_types as $post_type ) {
  66. $post_type_name = $post_type[ 'name' ];
  67. if ( !empty( $post_type[ 'object' ] ) )
  68. $post_type_name = $post_type[ 'object' ];
  69. add_action( 'add_meta_boxes_' . $post_type_name, array( $this, 'meta_post_add' ) );
  70. }
  71. */
  72. add_action( 'add_meta_boxes', array( $this, 'meta_post_add' ) );
  73. add_action( 'save_post', array( $this, 'save_post' ), 10, 2 );
  74. // Handle *_post_meta
  75. add_filter( 'get_post_metadata', array( $this, 'get_post_meta' ), 10, 4 );
  76. add_filter( 'add_post_metadata', array( $this, 'add_post_meta' ), 10, 5 );
  77. add_filter( 'update_post_metadata', array( $this, 'update_post_meta' ), 10, 5 );
  78. add_filter( 'delete_post_metadata', array( $this, 'delete_post_meta' ), 10, 5 );
  79. add_action( 'delete_post', array( $this, 'delete_post' ), 10, 1 );
  80. if ( !empty( self::$taxonomies ) ) {
  81. // Handle Taxonomy Editor
  82. foreach ( self::$taxonomies as $taxonomy ) {
  83. $taxonomy_name = $taxonomy[ 'name' ];
  84. if ( !empty( $taxonomy[ 'object' ] ) )
  85. $taxonomy_name = $taxonomy[ 'object' ];
  86. add_action( $taxonomy_name . '_edit_form_fields', array( $this, 'meta_taxonomy' ), 10, 2 );
  87. add_action( $taxonomy_name . '_add_form_fields', array( $this, 'meta_taxonomy' ), 10, 1 );
  88. }
  89. // Handle Term Editor
  90. add_action( 'edit_term', array( $this, 'save_taxonomy' ), 10, 3 );
  91. add_action( 'create_term', array( $this, 'save_taxonomy' ), 10, 3 );
  92. // Handle *_term_meta, LOL just kidding
  93. /*
  94. add_filter( 'get_term_metadata', array( $this, 'get_term_meta' ), 10, 4 );
  95. add_filter( 'add_term_metadata', array( $this, 'add_term_meta' ), 10, 5 );
  96. add_filter( 'update_term_metadata', array( $this, 'update_term_meta' ), 10, 5 );
  97. add_filter( 'delete_term_metadata', array( $this, 'delete_term_meta' ), 10, 5 );
  98. */
  99. // Handle Delete
  100. add_action( 'delete_term_taxonomy', array( $this, 'delete_taxonomy' ), 10, 1 );
  101. }
  102. if ( !empty( self::$media ) ) {
  103. if ( pods_wp_version( '3.5' ) ) {
  104. add_action( 'add_meta_boxes', array( $this, 'meta_post_add' ) );
  105. add_action( 'wp_ajax_save-attachment-compat', array( $this, 'save_media_ajax' ), 0 );
  106. }
  107. add_filter( 'attachment_fields_to_edit', array( $this, 'meta_media' ), 10, 2 );
  108. add_filter( 'attachment_fields_to_save', array( $this, 'save_media' ), 10, 2 );
  109. add_filter( 'wp_update_attachment_metadata', array( $this, 'save_media' ), 10, 2 );
  110. // Handle Delete
  111. add_action( 'delete_attachment', array( $this, 'delete_media' ), 10, 1 );
  112. }
  113. if ( !empty( self::$user ) ) {
  114. // Handle User Editor
  115. add_action( 'show_user_profile', array( $this, 'meta_user' ) );
  116. add_action( 'edit_user_profile', array( $this, 'meta_user' ) );
  117. add_action( 'personal_options_update', array( $this, 'save_user' ) );
  118. add_action( 'edit_user_profile_update', array( $this, 'save_user' ) );
  119. // Handle *_user_meta
  120. add_filter( 'get_user_metadata', array( $this, 'get_user_meta' ), 10, 4 );
  121. add_filter( 'add_user_metadata', array( $this, 'add_user_meta' ), 10, 5 );
  122. add_filter( 'update_user_metadata', array( $this, 'update_user_meta' ), 10, 5 );
  123. add_filter( 'delete_user_metadata', array( $this, 'delete_user_meta' ), 10, 5 );
  124. // Handle Delete
  125. add_action( 'delete_user', array( $this, 'delete_user' ), 10, 1 );
  126. }
  127. if ( !empty( self::$comment ) ) {
  128. // Handle Comment Form / Editor
  129. add_action( 'comment_form_logged_in_after', array( $this, 'meta_comment_new_logged_in' ), 10, 2 );
  130. add_filter( 'comment_form_default_fields', array( $this, 'meta_comment_new' ) );
  131. add_action( 'add_meta_boxes_comment', array( $this, 'meta_comment_add' ) );
  132. add_filter( 'pre_comment_approved', array( $this, 'validate_comment' ), 10, 2 );
  133. add_action( 'comment_post', array( $this, 'save_comment' ) );
  134. add_action( 'edit_comment', array( $this, 'save_comment' ) );
  135. // Handle *_comment_meta
  136. add_filter( 'get_comment_metadata', array( $this, 'get_comment_meta' ), 10, 4 );
  137. add_filter( 'add_comment_metadata', array( $this, 'add_comment_meta' ), 10, 5 );
  138. add_filter( 'update_comment_metadata', array( $this, 'update_comment_meta' ), 10, 5 );
  139. add_filter( 'delete_comment_metadata', array( $this, 'delete_comment_meta' ), 10, 5 );
  140. // Handle Delete
  141. add_action( 'delete_comment', array( $this, 'delete_comment' ), 10, 1 );
  142. }
  143. if ( is_admin() )
  144. $this->integrations();
  145. add_action( 'init', array( $this, 'enqueue' ), 9 );
  146. do_action( 'pods_meta_init' );
  147. return $this;
  148. }
  149. public static function enqueue () {
  150. foreach ( self::$queue as $type => $objects ) {
  151. foreach ( $objects as $pod_name => $pod ) {
  152. pods_transient_set( 'pods_pod_' . $pod_name, $pod );
  153. }
  154. self::$$type = array_merge( self::$$type, $objects );
  155. }
  156. }
  157. public function register ( $type, $pod ) {
  158. $pod_type = $type;
  159. if ( 'post_type' == $type )
  160. $type = 'post_types';
  161. elseif ( 'taxonomy' == $type )
  162. $type = 'taxonomies';
  163. elseif ( 'pod' == $type )
  164. $type = 'advanced_content_types';
  165. if ( !isset( self::$queue[ $type ] ) )
  166. self::$queue[ $type ] = array();
  167. $pod[ 'type' ] = $pod_type;
  168. $pod = $this->api->save_pod( $pod, false, self::$object_identifier );
  169. if ( !empty( $pod ) ) {
  170. self::$object_identifier--;
  171. self::$queue[ $type ][ $pod[ 'name' ] ] = $pod;
  172. return $pod;
  173. }
  174. return false;
  175. }
  176. public function register_field ( $pod, $field ) {
  177. $pod = $this->api->load_pod( array( 'name' => $pod ), false );
  178. if ( !empty( $pod ) ) {
  179. $type = $pod[ 'type' ];
  180. if ( 'post_type' == $pod[ 'type' ] )
  181. $type = 'post_types';
  182. elseif ( 'taxonomy' == $pod[ 'type' ] )
  183. $type = 'taxonomies';
  184. elseif ( 'pod' == $pod[ 'type' ] )
  185. $type = 'advanced_content_types';
  186. if ( !isset( self::$queue[ $pod[ 'type' ] ] ) )
  187. self::$queue[ $type ] = array();
  188. $field = $this->api->save_field( $field, false, false, $pod[ 'id' ] );
  189. if ( !empty( $field ) ) {
  190. $pod[ 'fields' ][ $field[ 'name' ] ] = $field;
  191. self::$queue[ $type ][ $pod[ 'name' ] ] = $pod;
  192. return $field;
  193. }
  194. }
  195. return false;
  196. }
  197. public function integrations () {
  198. add_filter( 'cpac-get-meta-by-type', array( $this, 'cpac_meta_keys' ), 10, 2 );
  199. add_filter( 'cpac-get-post-types', array( $this, 'cpac_post_types' ), 10, 1 );
  200. add_filter( 'cpac_get_column_value_custom_field', array( $this, 'cpac_meta_values' ), 10, 5 );
  201. }
  202. public function cpac_meta_keys ( $meta_fields, $type ) {
  203. $object_type = 'post_type';
  204. $object = $type;
  205. if ( 'wp-media' == $type )
  206. $object_type = $object = 'media';
  207. elseif ( 'wp-users' == $type )
  208. $object_type = $object = 'user';
  209. elseif ( 'wp-comments' == $type )
  210. $object_type = $object = 'comment';
  211. $pod = $this->api->load_pod( array( 'name' => $object ), false );
  212. // Add Pods fields
  213. if ( !empty( $pod ) && $object_type == $pod[ 'type' ] ) {
  214. foreach ( $pod[ 'fields' ] as $field => $field_data ) {
  215. if ( !is_array( $meta_fields ) )
  216. $meta_fields = array();
  217. if ( !in_array( $field, $meta_fields ) )
  218. $meta_fields[] = $field;
  219. }
  220. }
  221. // Remove internal Pods fields
  222. if ( is_array( $meta_fields ) ) {
  223. foreach ( $meta_fields as $meta_field ) {
  224. if ( 0 === strpos( $meta_field, '_pods_' ) )
  225. unset( $meta_fields[ $meta_field ] );
  226. }
  227. }
  228. return $meta_fields;
  229. }
  230. public function cpac_post_types ( $post_types ) {
  231. // Remove internal Pods post types
  232. foreach ( $post_types as $post_type => $post_type_name ) {
  233. if ( 0 === strpos( $post_type, '_pods_' ) || 0 === strpos( $post_type_name, '_pods_' ) )
  234. unset( $post_types[ $post_type ] );
  235. }
  236. return $post_types;
  237. }
  238. public function cpac_meta_values ( $meta, $fieldtype, $field, $type, $object_id ) {
  239. $tableless_field_types = apply_filters( 'pods_tableless_field_types', array( 'pick', 'file', 'avatar' ) );
  240. $object = $type;
  241. if ( 'wp-media' == $type )
  242. $object = 'media';
  243. elseif ( 'wp-users' == $type )
  244. $object = 'user';
  245. elseif ( 'wp-comments' == $type )
  246. $object = 'comment';
  247. $pod = $this->api->load_pod( array( 'name' => $object ), false );
  248. // Add Pods fields
  249. if ( !empty( $pod ) && isset( $pod[ 'fields' ][ $field ] ) ) {
  250. if ( in_array( $pod[ 'type' ], array( 'post_type', 'user', 'comment', 'media' ) ) && ( !empty( $fieldtype ) || in_array( $pod[ 'fields' ][ $field ][ 'type' ], $tableless_field_types ) ) )
  251. $meta = get_metadata( ( 'post_type' == $pod[ 'type' ] ? 'post' : $pod[ 'type' ] ), $object_id, $field, true );
  252. $meta = PodsForm::field_method( $pod[ 'fields' ][ $field ][ 'type' ], 'ui', $object_id, $meta, $field, array_merge( $pod[ 'fields' ][ $field ][ 'options' ], $pod[ 'fields' ][ $field ] ), $pod[ 'fields' ], $pod[ 'name' ] );
  253. }
  254. return $meta;
  255. }
  256. /**
  257. * Add a meta group of fields to add/edit forms
  258. *
  259. * @param string|array $pod The pod or type of element to attach the group to.
  260. * @param string $label Title of the edit screen section, visible to user.
  261. * @param string|array $fields Either a comma separated list of text fields or an associative array containing field infomration.
  262. * @param string $context (optional) The part of the page where the edit screen section should be shown ('normal', 'advanced', or 'side').
  263. * @param string $priority (optional) The priority within the context where the boxes should show ('high', 'core', 'default' or 'low').
  264. *
  265. * @since 2.0.0
  266. *
  267. * @return mixed|void
  268. */
  269. public function group_add ( $pod, $label, $fields, $context = 'normal', $priority = 'default' ) {
  270. if ( !is_array( $pod ) ) {
  271. $_pod = $this->api->load_pod( array( 'name' => $pod ), false );
  272. if ( !empty( $_pod ) )
  273. $pod = $_pod;
  274. else {
  275. $type = 'post_type';
  276. if ( in_array( $pod, array( 'media', 'user', 'comment' ) ) )
  277. $type = $pod;
  278. $pod = array(
  279. 'name' => $pod,
  280. 'type' => $type
  281. );
  282. }
  283. }
  284. if ( is_array( $pod ) && !isset( $pod[ 'id' ] ) ) {
  285. $defaults = array(
  286. 'name' => '',
  287. 'type' => 'post_type'
  288. );
  289. $pod = array_merge( $defaults, $pod );
  290. }
  291. if ( 'post' == $pod[ 'type' ] )
  292. $pod[ 'type' ] = 'post_type';
  293. if ( empty( $pod[ 'name' ] ) && isset( $pod[ 'object' ] ) && !empty( $pod[ 'object' ] ) )
  294. $pod[ 'name' ] = $pod[ 'object' ];
  295. elseif ( !isset( $pod[ 'object' ] ) || empty( $pod[ 'object' ] ) )
  296. $pod[ 'object' ] = $pod[ 'name' ];
  297. if ( empty( $pod[ 'object' ] ) )
  298. return pods_error( __( 'Object required to add a Pods meta group', 'pods' ) );
  299. $object_name = $pod[ 'object' ];
  300. if ( 'pod' == $pod[ 'type' ] )
  301. $object_name = $pod[ 'name' ];
  302. if ( !isset( self::$groups[ $pod[ 'type' ] ] ) )
  303. self::$groups[ $pod[ 'type' ] ] = array();
  304. if ( !isset( self::$groups[ $pod[ 'type' ] ][ $object_name ] ) )
  305. self::$groups[ $pod[ 'type' ] ][ $object_name ] = array();
  306. $_fields = array();
  307. if ( !is_array( $fields ) )
  308. $fields = explode( ',', $fields );
  309. foreach ( $fields as $k => $field ) {
  310. $name = $k;
  311. $defaults = array(
  312. 'name' => $name,
  313. 'label' => $name,
  314. 'type' => 'text'
  315. );
  316. if ( !is_array( $field ) ) {
  317. $name = trim( $field );
  318. $field = array(
  319. 'name' => $name,
  320. 'label' => $name
  321. );
  322. }
  323. $field = array_merge( $defaults, $field );
  324. $field[ 'name' ] = trim( $field[ 'name' ] );
  325. if ( isset( $pod[ 'fields' ] ) && isset( $pod[ 'fields' ][ $field[ 'name' ] ] ) )
  326. $field = array_merge( $field, $pod[ 'fields' ][ $field[ 'name' ] ] );
  327. $_fields[ $k ] = $field;
  328. }
  329. // Setup field options
  330. $fields = PodsForm::fields_setup( $_fields );
  331. $group = array(
  332. 'pod' => $pod,
  333. 'label' => $label,
  334. 'fields' => $fields,
  335. 'context' => $context,
  336. 'priority' => $priority
  337. );
  338. // Filter group data, pass vars separately for reference down the line (in case array changed by other filter)
  339. $group = apply_filters( 'pods_meta_group_add_' . $pod[ 'type' ] . '_' . $object_name, $group, $pod, $label, $fields );
  340. $group = apply_filters( 'pods_meta_group_add_' . $pod[ 'type' ], $group, $pod, $label, $fields );
  341. $group = apply_filters( 'pods_meta_group_add', $group, $pod, $label, $fields );
  342. self::$groups[ $pod[ 'type' ] ][ $object_name ][] = $group;
  343. // Hook it up!
  344. if ( 'post_type' == $pod[ 'type' ] ) {
  345. if ( !has_action( 'add_meta_boxes', array( $this, 'meta_post_add' ) ) )
  346. add_action( 'add_meta_boxes', array( $this, 'meta_post_add' ) );
  347. /*if ( !has_action( 'save_post', array( $this, 'save_post' ), 10, 2 ) )
  348. add_action( 'save_post', array( $this, 'save_post' ), 10, 2 );*/
  349. }
  350. elseif ( 'taxonomy' == $pod[ 'type' ] ) {
  351. if ( !has_action( $pod[ 'object' ] . '_edit_form_fields', array( $this, 'meta_taxonomy' ), 10, 2 ) ) {
  352. add_action( $pod[ 'object' ] . '_edit_form_fields', array( $this, 'meta_taxonomy' ), 10, 2 );
  353. add_action( $pod[ 'object' ] . '_add_form_fields', array( $this, 'meta_taxonomy' ), 10, 1 );
  354. }
  355. if ( !has_action( 'edit_term', array( $this, 'save_taxonomy' ), 10, 3 ) ) {
  356. add_action( 'edit_term', array( $this, 'save_taxonomy' ), 10, 3 );
  357. add_action( 'create_term', array( $this, 'save_taxonomy' ), 10, 3 );
  358. }
  359. }
  360. elseif ( 'media' == $pod[ 'type' ] ) {
  361. if ( !has_filter( 'wp_update_attachment_metadata', array( $this, 'save_media' ), 10, 2 ) ) {
  362. if ( pods_wp_version( '3.5' ) ) {
  363. add_action( 'add_meta_boxes', array( $this, 'meta_post_add' ) );
  364. add_action( 'wp_ajax_save-attachment-compat', array( $this, 'save_media_ajax' ), 0 );
  365. }
  366. add_filter( 'attachment_fields_to_edit', array( $this, 'meta_media' ), 10, 2 );
  367. add_filter( 'attachment_fields_to_save', array( $this, 'save_media' ), 10, 2 );
  368. add_filter( 'wp_update_attachment_metadata', array( $this, 'save_media' ), 10, 2 );
  369. }
  370. }
  371. elseif ( 'user' == $pod[ 'type' ] ) {
  372. if ( !has_action( 'show_user_profile', array( $this, 'meta_user' ) ) ) {
  373. add_action( 'show_user_profile', array( $this, 'meta_user' ) );
  374. add_action( 'edit_user_profile', array( $this, 'meta_user' ) );
  375. add_action( 'personal_options_update', array( $this, 'save_user' ) );
  376. add_action( 'edit_user_profile_update', array( $this, 'save_user' ) );
  377. }
  378. }
  379. elseif ( 'comment' == $pod[ 'type' ] ) {
  380. if ( !has_action( 'comment_form_logged_in_after', array( $this, 'meta_comment_new_logged_in' ), 10, 2 ) ) {
  381. add_action( 'comment_form_logged_in_after', array( $this, 'meta_comment_new_logged_in' ), 10, 2 );
  382. add_filter( 'comment_form_default_fields', array( $this, 'meta_comment_new' ) );
  383. add_action( 'add_meta_boxes_comment', array( $this, 'meta_comment_add' ) );
  384. add_action( 'wp_insert_comment', array( $this, 'save_comment' ) );
  385. add_action( 'edit_comment', array( $this, 'save_comment' ) );
  386. }
  387. }
  388. }
  389. public function object_get ( $type, $name ) {
  390. $object = self::$post_types;
  391. if ( 'taxonomy' == $type )
  392. $object = self::$taxonomies;
  393. elseif ( 'media' == $type )
  394. $object = self::$media;
  395. elseif ( 'user' == $type )
  396. $object = self::$user;
  397. elseif ( 'comment' == $type )
  398. $object = self::$comment;
  399. if ( 'pod' != $type && !empty( $object ) && is_array( $object ) && isset( $object[ $name ] ) )
  400. $pod = $object[ $name ];
  401. else
  402. $pod = $this->api->load_pod( array( 'name' => $name ), false );
  403. if ( empty( $pod ) )
  404. return array();
  405. $defaults = array(
  406. 'name' => 'post',
  407. 'object' => 'post',
  408. 'type' => 'post_type'
  409. );
  410. $pod = array_merge( $defaults, (array) $pod );
  411. if ( empty( $pod[ 'name' ] ) )
  412. $pod[ 'name' ] = $pod[ 'object' ];
  413. elseif ( empty( $pod[ 'object' ] ) )
  414. $pod[ 'object' ] = $pod[ 'name' ];
  415. if ( $pod[ 'type' ] != $type )
  416. return array();
  417. return $pod;
  418. }
  419. /**
  420. * @param $type
  421. * @param $name
  422. *
  423. * @return array
  424. */
  425. public function groups_get ( $type, $name ) {
  426. if ( 'post_type' == $type && 'attachment' == $name ) {
  427. $type = 'media';
  428. $name = 'media';
  429. }
  430. do_action( 'pods_meta_groups', $type, $name );
  431. $pod = array();
  432. $fields = array();
  433. $object = self::$post_types;
  434. if ( 'taxonomy' == $type )
  435. $object = self::$taxonomies;
  436. elseif ( 'media' == $type )
  437. $object = self::$media;
  438. elseif ( 'user' == $type )
  439. $object = self::$user;
  440. elseif ( 'comment' == $type )
  441. $object = self::$comment;
  442. if ( 'pod' != $type && !empty( $object ) && is_array( $object ) && isset( $object[ $name ] ) )
  443. $fields = $object[ $name ][ 'fields' ];
  444. else {
  445. $pod = $this->api->load_pod( array( 'name' => $name ), false );
  446. if ( !empty( $pod ) )
  447. $fields = $pod[ 'fields' ];
  448. }
  449. $defaults = array(
  450. 'name' => 'post',
  451. 'object' => 'post',
  452. 'type' => 'post_type'
  453. );
  454. $pod = array_merge( $defaults, (array) $pod );
  455. if ( empty( $pod[ 'name' ] ) )
  456. $pod[ 'name' ] = $pod[ 'object' ];
  457. elseif ( empty( $pod[ 'object' ] ) )
  458. $pod[ 'object' ] = $pod[ 'name' ];
  459. if ( $pod[ 'type' ] != $type )
  460. return array();
  461. $groups = array(
  462. array(
  463. 'pod' => $pod,
  464. 'label' => apply_filters( 'pods_meta_default_box_title', __( 'More Fields', 'pods' ), $pod, $fields, $type, $name ),
  465. 'fields' => $fields,
  466. 'context' => 'normal',
  467. 'priority' => 'default'
  468. )
  469. );
  470. if ( isset( self::$groups[ $type ] ) && isset( self::$groups[ $type ][ $name ] ) )
  471. $groups = self::$groups[ $type ][ $name ];
  472. return $groups;
  473. }
  474. /**
  475. * @param $post_type
  476. * @param null $post
  477. */
  478. public function meta_post_add ( $post_type, $post = null ) {
  479. if ( 'comment' == $post_type )
  480. return;
  481. if ( is_object( $post ) )
  482. $post_type = $post->post_type;
  483. $groups = $this->groups_get( 'post_type', $post_type );
  484. foreach ( $groups as $group ) {
  485. if ( empty( $group[ 'fields' ] ) )
  486. continue;
  487. if ( empty( $group[ 'label' ] ) )
  488. $group[ 'label' ] = get_post_type_object( $post_type )->labels->label;
  489. add_meta_box(
  490. $post_type . '-pods-meta-' . sanitize_title( $group[ 'label' ] ),
  491. $group[ 'label' ],
  492. array( $this, 'meta_post' ),
  493. $post_type,
  494. $group[ 'context' ],
  495. $group[ 'priority' ],
  496. array( 'group' => $group )
  497. );
  498. }
  499. }
  500. /**
  501. * @param $post
  502. * @param $metabox
  503. */
  504. public function meta_post ( $post, $metabox ) {
  505. wp_enqueue_style( 'pods-form' );
  506. wp_enqueue_script( 'pods' );
  507. ?>
  508. <table class="form-table pods-metabox pods-admin pods-dependency">
  509. <?php
  510. $id = null;
  511. if ( is_object( $post ) && false === strpos( $_SERVER[ 'REQUEST_URI' ], '/post-new.php?' ) )
  512. $id = $post->ID;
  513. $pod = pods( $metabox[ 'args' ][ 'group' ][ 'pod' ][ 'name' ], $id, true );
  514. foreach ( $metabox[ 'args' ][ 'group' ][ 'fields' ] as $field ) {
  515. if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $metabox[ 'args' ][ 'group' ][ 'fields' ], $pod, $id ) )
  516. continue;
  517. $value = '';
  518. if ( !empty( $pod ) ) {
  519. pods_no_conflict_on( 'post' );
  520. $value = $pod->field( array( 'name' => $field[ 'name' ], 'in_form' => true ) );
  521. pods_no_conflict_off( 'post' );
  522. }
  523. elseif ( !empty( $id ) )
  524. $value = get_post_meta( $id, $field[ 'name' ], true );
  525. $depends = PodsForm::dependencies( $field, 'pods-meta-' );
  526. ?>
  527. <tr class="form-field pods-field <?php echo $depends; ?>">
  528. <th scope="row" valign="top"><?php echo PodsForm::label( 'pods_meta_' . $field[ 'name' ], $field[ 'label' ], $field[ 'help' ], pods_var_raw( 'label_options', $field ) ); ?></th>
  529. <td>
  530. <?php echo PodsForm::field( 'pods_meta_' . $field[ 'name' ], $value, $field[ 'type' ], $field, $pod, $id ); ?>
  531. <?php echo PodsForm::comment( 'pods_meta_' . $field[ 'name' ], $field[ 'description' ], $field ); ?>
  532. </td>
  533. </tr>
  534. <?php
  535. }
  536. ?>
  537. </table>
  538. <input type="hidden" name="pods_metasave" value="1" />
  539. <script type="text/javascript">
  540. jQuery( function ( $ ) {
  541. $( document ).Pods( 'dependency', true );
  542. } );
  543. </script>
  544. <?php
  545. }
  546. /**
  547. * @param $post_id
  548. * @param $post
  549. *
  550. * @return mixed
  551. */
  552. public function save_post ( $post_id, $post ) {
  553. $blacklisted_types = array(
  554. 'revision',
  555. '_pods_pod',
  556. '_pods_field'
  557. );
  558. $blacklisted_types = apply_filters( 'pods_meta_save_post_blacklist_types', $blacklisted_types, $post_id, $post );
  559. if ( empty( $_POST ) || 1 != pods_var( 'pods_metasave', 'post' ) )
  560. return $post_id;
  561. // @todo Figure out how to hook into autosave for saving meta
  562. // Block Autosave and Revisions
  563. if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || in_array( $post->post_type, $blacklisted_types ) )
  564. return $post_id;
  565. // Block Quick Edits / Bulk Edits
  566. if ( 'edit.php' == pods_var( 'pagenow', 'global' ) && ( 'inline-save' == pods_var( 'action', 'post' ) || null != pods_var( 'bulk_edit', 'get' ) || is_array( pods_var( 'post', 'get' ) ) ) )
  567. return $post_id;
  568. // Block Trash
  569. if ( in_array( pods_var( 'action', 'get' ), array( 'untrash', 'trash' ) ) )
  570. return $post_id;
  571. // Block Auto-drafting and Trash (not via Admin action)
  572. $blacklisted_status = array(
  573. 'auto-draft',
  574. 'trash'
  575. );
  576. $blacklisted_status = apply_filters( 'pods_meta_save_post_blacklist_status', $blacklisted_status, $post_id, $post );
  577. if ( in_array( $post->post_status, $blacklisted_status ) )
  578. return $post_id;
  579. $groups = $this->groups_get( 'post_type', $post->post_type );
  580. if ( empty( $groups ) )
  581. return $post_id;
  582. $data = array();
  583. $id = $post_id;
  584. $pod = null;
  585. foreach ( $groups as $group ) {
  586. if ( empty( $group[ 'fields' ] ) )
  587. continue;
  588. if ( null === $pod )
  589. $pod = pods( $group[ 'pod' ][ 'name' ], $id, true );
  590. foreach ( $group[ 'fields' ] as $field ) {
  591. if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) )
  592. continue;
  593. $data[ $field[ 'name' ] ] = '';
  594. if ( isset( $_POST[ 'pods_meta_' . $field[ 'name' ] ] ) )
  595. $data[ $field[ 'name' ] ] = $_POST[ 'pods_meta_' . $field[ 'name' ] ];
  596. }
  597. }
  598. do_action( 'pods_meta_save_pre_post', $data, $pod, $id, $groups, $post, $post->post_type );
  599. do_action( "pods_meta_save_pre_post_{$post->post_type}", $data, $pod, $id, $groups, $post );
  600. pods_no_conflict_on( 'post' );
  601. if ( !empty( $pod ) ) {
  602. // Fix for Pods doing it's own sanitization
  603. $data = stripslashes_deep( $data );
  604. $pod->save( $data );
  605. }
  606. elseif ( !empty( $id ) ) {
  607. foreach ( $data as $field => $value ) {
  608. update_post_meta( $id, $field, $value );
  609. }
  610. }
  611. pods_no_conflict_off( 'post' );
  612. do_action( 'pods_meta_save_post', $data, $pod, $id, $groups, $post, $post->post_type );
  613. do_action( "pods_meta_save_post_{$post->post_type}", $data, $pod, $id, $groups, $post );
  614. return $post_id;
  615. }
  616. /**
  617. * @param $form_fields
  618. * @param $post
  619. *
  620. * @return array
  621. */
  622. public function meta_media ( $form_fields, $post ) {
  623. $groups = $this->groups_get( 'media', 'media' );
  624. if ( empty( $groups ) || 'attachment' == pods_var( 'typenow', 'global' ) )
  625. return $form_fields;
  626. wp_enqueue_style( 'pods-form' );
  627. $id = null;
  628. if ( is_object( $post ) )
  629. $id = $post->ID;
  630. $pod = null;
  631. foreach ( $groups as $group ) {
  632. if ( empty( $group[ 'fields' ] ) )
  633. continue;
  634. if ( null === $pod )
  635. $pod = pods( $group[ 'pod' ][ 'name' ], $id, true );
  636. foreach ( $group[ 'fields' ] as $field ) {
  637. if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) )
  638. continue;
  639. $value = '';
  640. if ( !empty( $pod ) )
  641. $value = $pod->field( array( 'name' => $field[ 'name' ], 'in_form' => true ) );
  642. elseif ( !empty( $id ) ) {
  643. pods_no_conflict_on( 'post' );
  644. $value = get_post_meta( $id, $field[ 'name' ], true );
  645. pods_no_conflict_off( 'post' );
  646. }
  647. $form_fields[ 'pods_meta_' . $field[ 'name' ] ] = array(
  648. 'label' => $field[ 'label' ],
  649. 'input' => 'html',
  650. 'html' => PodsForm::field( 'pods_meta_' . $field[ 'name' ], $value, $field[ 'type' ], $field, $pod, $id ) . PodsForm::field( 'pods_metasave', 1, 'hidden' ),
  651. 'helps' => PodsForm::comment( 'pods_meta_' . $field[ 'name' ], $field[ 'description' ], $field )
  652. );
  653. }
  654. }
  655. return $form_fields;
  656. }
  657. /**
  658. * @param $post
  659. * @param $attachment
  660. *
  661. * @return mixed
  662. */
  663. public function save_media ( $post, $attachment ) {
  664. $groups = $this->groups_get( 'media', 'media' );
  665. if ( empty( $groups ) )
  666. return $post;
  667. $post_id = $attachment;
  668. if ( empty( $_POST ) || 1 != pods_var( 'pods_metasave', 'post' ) )
  669. return $post;
  670. if ( is_array( $post ) && !empty( $post ) && isset( $post[ 'ID' ] ) && 'attachment' == $post[ 'post_type' ] )
  671. $post_id = $post[ 'ID' ];
  672. if ( is_array( $post_id ) || empty( $post_id ) )
  673. return $post;
  674. $data = array();
  675. $id = $post_id;
  676. $pod = null;
  677. foreach ( $groups as $group ) {
  678. if ( empty( $group[ 'fields' ] ) )
  679. continue;
  680. if ( null === $pod )
  681. $pod = pods( $group[ 'pod' ][ 'name' ], $id, true );
  682. foreach ( $group[ 'fields' ] as $field ) {
  683. if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) )
  684. continue;
  685. $data[ $field[ 'name' ] ] = '';
  686. if ( isset( $_POST[ 'pods_meta_' . $field[ 'name' ] ] ) )
  687. $data[ $field[ 'name' ] ] = $_POST[ 'pods_meta_' . $field[ 'name' ] ];
  688. }
  689. }
  690. do_action( 'pods_meta_save_pre_media', $data, $pod, $id, $groups, $post, $attachment );
  691. if ( !empty( $pod ) ) {
  692. // Fix for Pods doing it's own sanitization
  693. $data = stripslashes_deep( $data );
  694. $pod->save( $data );
  695. }
  696. elseif ( !empty( $id ) ) {
  697. pods_no_conflict_on( 'post' );
  698. foreach ( $data as $field => $value ) {
  699. update_post_meta( $id, $field, $value );
  700. }
  701. pods_no_conflict_off( 'post' );
  702. }
  703. do_action( 'pods_meta_save_media', $data, $pod, $id, $groups, $post, $attachment );
  704. return $post;
  705. }
  706. public function save_media_ajax () {
  707. if ( !isset( $_POST[ 'id' ] ) || empty( $_POST[ 'id' ] ) || absint( $_POST[ 'id' ] ) < 1 )
  708. return;
  709. $id = absint( $_POST[ 'id' ] );
  710. if ( !isset( $_POST[ 'nonce' ] ) || empty( $_POST[ 'nonce' ] ) )
  711. return;
  712. check_ajax_referer( 'update-post_' . $id, 'nonce' );
  713. if ( !current_user_can( 'edit_post', $id ) )
  714. return;
  715. $post = get_post( $id, ARRAY_A );
  716. if ( 'attachment' != $post[ 'post_type' ] )
  717. return;
  718. // fix ALL THE THINGS
  719. if ( !isset( $_REQUEST[ 'attachments' ] ) )
  720. $_REQUEST[ 'attachments' ] = array();
  721. if ( !isset( $_REQUEST[ 'attachments' ][ $id ] ) )
  722. $_REQUEST[ 'attachments' ][ $id ] = array();
  723. if ( empty( $_REQUEST[ 'attachments' ][ $id ] ) )
  724. $_REQUEST[ 'attachments' ][ $id ][ '_fix_wp' ] = 1;
  725. }
  726. /**
  727. * @param $tag
  728. * @param null $taxonomy
  729. */
  730. public function meta_taxonomy ( $tag, $taxonomy = null ) {
  731. wp_enqueue_style( 'pods-form' );
  732. $taxonomy_name = $taxonomy;
  733. if ( !is_object( $tag ) )
  734. $taxonomy_name = $tag;
  735. $groups = $this->groups_get( 'taxonomy', $taxonomy_name );
  736. $id = null;
  737. if ( is_object( $tag ) )
  738. $id = $tag->term_id;
  739. $pod = null;
  740. foreach ( $groups as $group ) {
  741. if ( empty( $group[ 'fields' ] ) )
  742. continue;
  743. if ( null === $pod )
  744. $pod = pods( $group[ 'pod' ][ 'name' ], $id, true );
  745. foreach ( $group[ 'fields' ] as $field ) {
  746. if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) )
  747. continue;
  748. $value = '';
  749. if ( !empty( $pod ) )
  750. $value = $pod->field( array( 'name' => $field[ 'name' ], 'in_form' => true ) );
  751. if ( !is_object( $tag ) ) {
  752. ?>
  753. <div class="form-field pods-field">
  754. <?php
  755. echo PodsForm::label( 'pods_meta_' . $field[ 'name' ], $field[ 'label' ], $field[ 'help' ] );
  756. echo PodsForm::field( 'pods_meta_' . $field[ 'name' ], $value, $field[ 'type' ], $field, $pod, $id );
  757. echo PodsForm::comment( 'pods_meta_' . $field[ 'name' ], $field[ 'description' ], $field );
  758. ?>
  759. </div>
  760. <?php
  761. }
  762. else {
  763. ?>
  764. <tr class="form-field pods-field">
  765. <th scope="row" valign="top"><?php echo PodsForm::label( 'pods_meta_' . $field[ 'name' ], $field[ 'label' ], $field[ 'help' ] ); ?></th>
  766. <td>
  767. <?php
  768. echo PodsForm::field( 'pods_meta_' . $field[ 'name' ], $value, $field[ 'type' ], $field, $pod, $id );
  769. echo PodsForm::comment( 'pods_meta_' . $field[ 'name' ], $field[ 'description' ], $field );
  770. ?>
  771. </td>
  772. </tr>
  773. <?php
  774. }
  775. }
  776. }
  777. }
  778. /**
  779. * @param $term_id
  780. * @param $term_taxonomy_id
  781. * @param $taxonomy
  782. */
  783. public function save_taxonomy ( $term_id, $term_taxonomy_id, $taxonomy ) {
  784. $groups = $this->groups_get( 'taxonomy', $taxonomy );
  785. if ( empty( $groups ) )
  786. return;
  787. $term = get_term( $term_id, $taxonomy );
  788. $data = array(
  789. 'name' => $term->name
  790. );
  791. $id = $term_id;
  792. $pod = null;
  793. foreach ( $groups as $group ) {
  794. if ( empty( $group[ 'fields' ] ) )
  795. continue;
  796. if ( null === $pod )
  797. $pod = pods( $group[ 'pod' ][ 'name' ], $id, true );
  798. foreach ( $group[ 'fields' ] as $field ) {
  799. if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) )
  800. continue;
  801. $data[ $field[ 'name' ] ] = '';
  802. if ( isset( $_POST[ 'pods_meta_' . $field[ 'name' ] ] ) )
  803. $data[ $field[ 'name' ] ] = $_POST[ 'pods_meta_' . $field[ 'name' ] ];
  804. }
  805. }
  806. do_action( 'pods_meta_save_pre_taxonomy', $data, $pod, $id, $groups, $term_id, $term_taxonomy_id, $taxonomy );
  807. do_action( "pods_meta_save_pre_taxonomy_{$taxonomy}", $data, $pod, $id, $groups, $term_id, $term_taxonomy_id, $taxonomy );
  808. if ( !empty( $pod ) ) {
  809. // Fix for Pods doing it's own sanitization
  810. $data = stripslashes_deep( $data );
  811. $pod->save( $data );
  812. }
  813. do_action( 'pods_meta_save_taxonomy', $data, $pod, $id, $groups, $term_id, $term_taxonomy_id, $taxonomy );
  814. do_action( "pods_meta_save_taxonomy_{$taxonomy}", $data, $pod, $id, $groups, $term_id, $term_taxonomy_id, $taxonomy );
  815. }
  816. /**
  817. * @param $user_id
  818. */
  819. public function meta_user ( $user_id ) {
  820. wp_enqueue_style( 'pods-form' );
  821. if ( is_object( $user_id ) )
  822. $user_id = $user_id->ID;
  823. $groups = $this->groups_get( 'user', 'user' );
  824. if ( is_object( $user_id ) )
  825. $user_id = $user_id->ID;
  826. $id = $user_id;
  827. $pod = null;
  828. foreach ( $groups as $group ) {
  829. if ( empty( $group[ 'fields' ] ) )
  830. continue;
  831. if ( null === $pod )
  832. $pod = pods( $group[ 'pod' ][ 'name' ], $id, true );
  833. ?>
  834. <h3><?php echo $group[ 'label' ]; ?></h3>
  835. <table class="form-table pods-meta">
  836. <tbody>
  837. <?php
  838. foreach ( $group[ 'fields' ] as $field ) {
  839. if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) )
  840. continue;
  841. $value = '';
  842. if ( !empty( $pod ) )
  843. $value = $pod->field( array( 'name' => $field[ 'name' ], 'in_form' => true ) );
  844. elseif ( !empty( $id ) ) {
  845. pods_no_conflict_on( 'user' );
  846. $value = get_user_meta( $id, $field[ 'name' ], true );
  847. pods_no_conflict_off( 'user' );
  848. }
  849. ?>
  850. <tr class="form-field pods-field">
  851. <th scope="row" valign="top"><?php echo PodsForm::label( 'pods_meta_' . $field[ 'name' ], $field[ 'label' ], $field[ 'help' ] ); ?></th>
  852. <td>
  853. <?php echo PodsForm::field( 'pods_meta_' . $field[ 'name' ], $value, $field[ 'type' ], $field, $pod, $id ); ?>
  854. <?php echo PodsForm::comment( 'pods_meta_' . $field[ 'name' ], $field[ 'description' ], $field ); ?>
  855. </td>
  856. </tr>
  857. <?php
  858. }
  859. ?>
  860. </tbody>
  861. </table>
  862. <?php
  863. }
  864. }
  865. /**
  866. * @param $user_id
  867. */
  868. public function save_user ( $user_id ) {
  869. $groups = $this->groups_get( 'user', 'user' );
  870. if ( empty( $groups ) )
  871. return;
  872. if ( is_object( $user_id ) )
  873. $user_id = $user_id->ID;
  874. $data = array();
  875. $id = $user_id;
  876. $pod = null;
  877. foreach ( $groups as $group ) {
  878. if ( empty( $group[ 'fields' ] ) )
  879. continue;
  880. if ( null === $pod )
  881. $pod = pods( $group[ 'pod' ][ 'name' ], $id, true );
  882. foreach ( $group[ 'fields' ] as $field ) {
  883. if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) )
  884. continue;
  885. $data[ $field[ 'name' ] ] = '';
  886. if ( isset( $_POST[ 'pods_meta_' . $field[ 'name' ] ] ) )
  887. $data[ $field[ 'name' ] ] = $_POST[ 'pods_meta_' . $field[ 'name' ] ];
  888. }
  889. }
  890. if ( !empty( $pod ) ) {
  891. // Fix for Pods doing it's own sanitization
  892. $data = stripslashes_deep( $data );
  893. $pod->save( $data );
  894. }
  895. elseif ( !empty( $id ) ) {
  896. pods_no_conflict_on( 'user' );
  897. foreach ( $data as $field => $value ) {
  898. update_user_meta( $id, $field, $value );
  899. }
  900. pods_no_conflict_off( 'user' );
  901. }
  902. do_action( 'pods_meta_save_user', $data, $pod, $id, $groups );
  903. }
  904. /**
  905. * @param $commenter
  906. * @param $user_identity
  907. */
  908. public function meta_comment_new_logged_in ( $commenter, $user_identity ) {
  909. wp_enqueue_style( 'pods-form' );
  910. $groups = $this->groups_get( 'comment', 'comment' );
  911. $id = null;
  912. $pod = null;
  913. foreach ( $groups as $group ) {
  914. if ( empty( $group[ 'fields' ] ) )
  915. continue;
  916. if ( null === $pod )
  917. $pod = pods( $group[ 'pod' ][ 'name' ], $id, true );
  918. foreach ( $group[ 'fields' ] as $field ) {
  919. if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) )
  920. continue;
  921. $value = '';
  922. if ( !empty( $pod ) )
  923. $value = $pod->field( array( 'name' => $field[ 'name' ], 'in_form' => true ) );
  924. elseif ( !empty( $id ) ) {
  925. pods_no_conflict_on( 'comment' );
  926. $value = get_comment_meta( $id, $field[ 'name' ], true );
  927. pods_no_conflict_off( 'comment' );
  928. }
  929. ?>
  930. <p class="comment-form-author comment-form-pods-meta-<?php echo $field[ 'name' ]; ?> pods-field">
  931. <?php
  932. echo PodsForm::label( 'pods_meta_' . $field[ 'name' ], $field[ 'label' ], $field[ 'help' ] );
  933. echo PodsForm::field( 'pods_meta_' . $field[ 'name' ], $value, $field[ 'type' ], $field, $pod, $id );
  934. echo PodsForm::comment( 'pods_meta_' . $field[ 'name' ], $field[ 'description' ], $field );
  935. ?>
  936. </p>
  937. <?php
  938. }
  939. }
  940. }
  941. /**
  942. * @param $form_fields
  943. *
  944. * @return array
  945. */
  946. public function meta_comment_new ( $form_fields ) {
  947. wp_enqueue_style( 'pods-form' );
  948. $groups = $this->groups_get( 'comment', 'comment' );
  949. $id = null;
  950. $pod = null;
  951. foreach ( $groups as $group ) {
  952. if ( empty( $group[ 'fields' ] ) )
  953. continue;
  954. if ( null === $pod )
  955. $pod = pods( $group[ 'pod' ][ 'name' ], $id, true );
  956. foreach ( $group[ 'fields' ] as $field ) {
  957. if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) )
  958. continue;
  959. $value = '';
  960. if ( !empty( $pod ) )
  961. $value = $pod->field( array( 'name' => $field[ 'name' ], 'in_form' => true ) );
  962. elseif ( !empty( $id ) ) {
  963. pods_no_conflict_on( 'comment' );
  964. $value = get_comment_meta( $id, $field[ 'name' ], true );
  965. pods_no_conflict_off( 'comment' );
  966. }
  967. ob_start();
  968. ?>
  969. <p class="comment-form-author comment-form-pods-meta-<?php echo $field[ 'name' ]; ?> pods-field">
  970. <?php
  971. echo PodsForm::label( 'pods_meta_' . $field[ 'name' ], $field[ 'label' ], $field[ 'help' ] );
  972. echo PodsForm::field( 'pods_meta_' . $field[ 'name' ], $value, $field[ 'type' ], $field, $pod, $id );
  973. echo PodsForm::comment( 'pods_meta_' . $field[ 'name' ], $field[ 'description' ], $field );
  974. ?>
  975. </p>
  976. <?php
  977. $form_fields[ 'pods_meta_' . $field[ 'name' ] ] = ob_get_clean();
  978. }
  979. }
  980. return $form_fields;
  981. }
  982. /**
  983. * @param $comment_type
  984. * @param null $comment
  985. */
  986. public function meta_comment_add ( $comment_type, $comment = null ) {
  987. if ( is_object( $comment ) && isset( $comment_type->comment_type ) )
  988. $comment_type = $comment->comment_type;
  989. if ( is_object( $comment_type ) && isset( $comment_type->comment_type ) ) {
  990. $comment = $comment_type;
  991. $comment_type = $comment_type->comment_type;
  992. }
  993. if ( is_object( $comment_type ) )
  994. return;
  995. elseif ( empty( $comment_type ) )
  996. $comment_type = 'comment';
  997. $groups = $this->groups_get( 'comment', $comment_type );
  998. foreach ( $groups as $group ) {
  999. if ( empty( $group[ 'fields' ] ) )
  1000. continue;
  1001. add_meta_box(
  1002. $comment_type . '-pods-meta-' . sanitize_title( $group[ 'label' ] ),
  1003. $group[ 'label' ],
  1004. array( $this, 'meta_comment' ),
  1005. $comment_type,
  1006. $group[ 'context' ],
  1007. $group[ 'priority' ],
  1008. array( 'group' => $group )
  1009. );
  1010. }
  1011. }
  1012. /**
  1013. * @param $comment
  1014. * @param $metabox
  1015. */
  1016. public function meta_comment ( $comment, $metabox ) {
  1017. wp_enqueue_style( 'pods-form' );
  1018. ?>
  1019. <table class="form-table editcomment pods-metabox">
  1020. <?php
  1021. $id = null;
  1022. if ( is_object( $comment ) )
  1023. $id = $comment->comment_ID;
  1024. $pod = pods( $metabox[ 'args' ][ 'group' ][ 'pod' ][ 'name' ], $id, true );
  1025. foreach ( $metabox[ 'args' ][ 'group' ][ 'fields' ] as $field ) {
  1026. if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $metabox[ 'args' ][ 'group' ][ 'fields' ], $pod, $id ) )
  1027. continue;
  1028. $value = '';
  1029. if ( !empty( $pod ) )
  1030. $value = $pod->field( array( 'name' => $field[ 'name' ], 'in_form' => true ) );
  1031. ?>
  1032. <tr class="form-field pods-field">
  1033. <th scope="row" valign="top"><?php echo PodsForm::label( 'pods_meta_' . $field[ 'name' ], $field[ 'label' ], $field[ 'help' ] ); ?></th>
  1034. <td>
  1035. <?php echo PodsForm::field( 'pods_meta_' . $field[ 'name' ], $value, $field[ 'type' ], $field, $pod, $id ); ?>
  1036. <?php echo PodsForm::comment( 'pods_meta_' . $field[ 'name' ], $field[ 'description' ], $field ); ?>
  1037. </td>
  1038. </tr>
  1039. <?php
  1040. }
  1041. ?>
  1042. </table>
  1043. <?php
  1044. }
  1045. /**
  1046. * @param $approved
  1047. * @param $commentdata
  1048. */
  1049. public function validate_comment ( $approved, $commentdata ) {
  1050. $groups = $this->groups_get( 'comment', 'comment' );
  1051. if ( empty( $groups ) )
  1052. return $approved;
  1053. $data = array();
  1054. $pod = null;
  1055. $id = null;
  1056. foreach ( $groups as $group ) {
  1057. if ( empty( $group[ 'fields' ] ) )
  1058. continue;
  1059. if ( null === $pod )
  1060. $pod = pods( $group[ 'pod' ][ 'name' ], $id, true );
  1061. foreach ( $group[ 'fields' ] as $field ) {
  1062. if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) )
  1063. continue;
  1064. $data[ $field[ 'name' ] ] = '';
  1065. if ( isset( $_POST[ 'pods_meta_' . $field[ 'name' ] ] ) )
  1066. $data[ $field[ 'name' ] ] = $_POST[ 'pods_meta_' . $field[ 'name' ] ];
  1067. $validate = $this->api->handle_field_validation( $data[ $field[ 'name' ] ], $field[ 'name' ], $this->api->get_wp_object_fields( 'comment' ), $pod->fields(), $pod, array() );
  1068. if ( false === $validate )
  1069. $validate = sprintf( __( 'There was an issue validating the field %s', 'pods' ), $field[ 'label' ] );
  1070. if ( !is_bool( $validate ) && !empty( $validate ) )
  1071. return pods_error( $validate, $this );
  1072. }
  1073. }
  1074. return $approved;
  1075. }
  1076. /**
  1077. * @param $comment_id
  1078. */
  1079. public function save_comment ( $comment_id ) {
  1080. $groups = $this->groups_get( 'comment', 'comment' );
  1081. if ( empty( $groups ) )
  1082. return $comment_id;
  1083. $data = array();
  1084. $id = $comment_id;
  1085. $pod = null;
  1086. foreach ( $groups as $group ) {
  1087. if ( empty( $group[ 'fields' ] ) )
  1088. continue;
  1089. if ( null === $pod )
  1090. $pod = pods( $group[ 'pod' ][ 'name' ], $id, true );
  1091. foreach ( $group[ 'fields' ] as $field ) {
  1092. if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) )
  1093. continue;
  1094. $data[ $field[ 'name' ] ] = '';
  1095. if ( isset( $_POST[ 'pods_meta_' . $field[ 'name' ] ] ) )
  1096. $data[ $field[ 'name' ] ] = $_POST[ 'pods_meta_' . $field[ 'name' ] ];
  1097. }
  1098. }
  1099. do_action( 'pods_meta_save_pre_comment', $data, $pod, $id, $groups );
  1100. if ( !empty( $pod ) ) {
  1101. // Fix for Pods doing it's own sanitization
  1102. $data = stripslashes_deep( $data );
  1103. $pod->save( $data );
  1104. }
  1105. elseif ( !empty( $id ) ) {
  1106. pods_no_conflict_on( 'comment' );
  1107. foreach ( $data

Large files files are truncated, but you can click here to view the full file