/includes/to-post-type.php

  1. <?php 
  2. /** 
  3. * Functions/hooks for regular form to post type 
  4. * 
  5. * @package caldera_custom_fields 
  6. * @copyright 2014-2015 CalderaWP LLC and David Cramer 
  7. */ 
  8. add_filter( 'caldera_forms_get_form_processors', 'cf_custom_fields_posttype_process' ); 
  9. add_filter( 'caldera_forms_get_entry_detail', 'cf_custom_fields_entry_details', 10, 3); 
  10. add_filter( 'caldera_forms_render_get_entry', 'cf_custom_fields_get_post_type_entry', 10, 3); 
  11. add_filter( 'caldera_forms_get_entry_meta_db_storage', 'cf_custom_fields_meta_view'); 
  12. add_action( 'caldera_forms_processor_templates', 'cf_custom_fields_meta_template'); 
  13. add_filter( 'caldera_forms_render_pre_get_entry', 'cf_custom_fields_populate_form_edit', 11, 3 ); 
  14. add_filter( 'caldera_forms_get_addons', 'cf_custom_fields_savetoposttype_addon' ); 
  15.  
  16. // Better upload attach if CF Core is 1.5.0.9 
  17. //see https://github.com/CalderaWP/caldera-custom-fields/issues/17 
  18. if ( method_exists( 'Caldera_Forms_Admin', 'is_page' ) ) { 
  19. add_filter( 'caldera_forms_render_setup_field', 'cf_custom_fields_filter_featured_image', 25, 2 ); 
  20. add_filter( 'caldera_forms_file_upload_handler', 'cf_custom_fields_filter_upload_handler', 10, 3 ); 
  21.  
  22. /** 
  23. * Register this as a processor. 
  24. * 
  25. * @uses "caldera_forms_get_form_processors" filter 
  26. * 
  27. * @param $processors 
  28. * 
  29. * @return mixed 
  30. */ 
  31. function cf_custom_fields_posttype_process($processors) { 
  32.  
  33. $processors['post_type'] = array( 
  34. "name" => __( 'Save as Post Type', 'caldera-forms-metabox' ),  
  35. "author" => 'Caldera Labs',  
  36. "description" => __( 'Store form entries as a post with custom fields.', 'caldera-forms-metabox' ),  
  37. "post_processor" => 'cf_custom_fields_capture_entry',  
  38. "template" => trailingslashit( CCF_PATH ) . "includes/to-post-type-config.php",  
  39. "icon" => CCF_URL . "/post-type.png",  
  40. "default" => array( 
  41. 'post_status' => "draft" 
  42. ),  
  43. "meta_template" => trailingslashit( CCF_PATH ) . "includes/meta_template.php",  
  44. "magic_tags" => array( 
  45. "ID",  
  46. "permalink" 
  47.  
  48. ); 
  49. return $processors; 
  50.  
  51.  
  52. /** 
  53. * Prepared from data for edit post. 
  54. * 
  55. * @since 1.1.0 
  56. * 
  57. * @uses "caldera_forms_render_pre_get_entry" filter 
  58. * 
  59. * @param array $data Form data. 
  60. * @param array $form Form config. 
  61. * @param $entry_id 
  62. * 
  63. * @return array 
  64. */ 
  65. function cf_custom_fields_populate_form_edit( $data, $form, $entry_id ) { 
  66.  
  67. $processors = Caldera_Forms::get_processor_by_type( 'post_type', $form ); 
  68. if( !empty( $processors ) ) { 
  69. foreach( $processors as $processor ) { 
  70. if( !empty( $processor['config']['ID'] ) ) { 
  71. // ooo ID!!! 
  72. $ID = Caldera_Forms::do_magic_tags( $processor['config']['ID'], $entry_id ); 
  73. if( !empty( $ID ) ) { 
  74. $post = get_post( $ID ); 
  75. if( empty( $post ) ) { 
  76. return $data; 
  77.  
  78. $data[ $processor['config']['post_title'] ] = $post->post_title; 
  79. $data[ $processor['config']['post_content'] ] = $post->post_content; 
  80. foreach( $form['fields'] as $field_id => $field ) { 
  81. if( $post->{$field['slug']} ) { 
  82. $data[ $field_id ] = $post->{$field['slug']}; 
  83. return $data; 
  84.  
  85. /** 
  86. * Register the add-on 
  87. * 
  88. * @since 1.1.0 
  89. * 
  90. * @uses "caldera_forms_get_addons" filter 
  91. * 
  92. * @param array $addons 
  93. * 
  94. * @return array 
  95. */ 
  96. function cf_custom_fields_savetoposttype_addon($addons) { 
  97. $addons['savetoposttype'] = __FILE__; 
  98. return $addons; 
  99.  
  100.  
  101. /** 
  102. * Set template for meta fields 
  103. * 
  104. * @since 1.1.0 
  105. * 
  106. * @uses "caldera_forms_processor_templates" action 
  107. */ 
  108. function cf_custom_fields_meta_template() { 
  109. ?> 
  110. <script type="text/html" id="post-meta-field-tmpl"> 
  111. <div class="caldera-config-group"> 
  112. <label>Field Name<input type="text" class="block-input field-config" name="{{_name}}[metakey][]" value="{{metakey}}"></label> 
  113. <div class="caldera-config-field"> 
  114. <div>Value Field</div> 
  115. <div style="width: 280px; display:inline-block;">{{{_field slug="meta_field" array="true"}}}</div> 
  116. <button class="button remove-meta-field{{_id}}" type="button"><?php echo __('Remove', 'caldera-forms'); ?></button> 
  117. </div> 
  118. </div> 
  119. </script> 
  120. <?php 
  121.  
  122. /** 
  123. * Prepare DB storage 
  124. * 
  125. * @uses "caldera_forms_get_entry_meta_db_storage" filter 
  126. * 
  127. * @param $meta 
  128. * 
  129. * @return mixed 
  130. */ 
  131. function cf_custom_fields_meta_view($meta) { 
  132. $postid = $meta['meta_value']; 
  133. $meta['meta_key'] = _('Post Name', 'caldera-forms-metabox' ); 
  134. $meta['meta_value'] = get_the_title($meta['meta_value']); 
  135. $meta['meta_value'] .= '<div><a href="post.php?post='.$postid.'&action=edit" target="_blank">'.__('Edit').'</a> | <a href="' . get_permalink( $postid ) . '" target="_blank">'.__('View').'</a></div>'; 
  136. $meta['post'] = get_post( $postid ); 
  137. return $meta; 
  138.  
  139. /** 
  140. * Check if a form has the post type processor 
  141. * 
  142. * @since 1.1.0 
  143. * 
  144. * @param array $form Form config. 
  145. * @param string $entry_id Entry ID. 
  146. * 
  147. * @return array|bool False if not in processor, else post object/ config. 
  148. */ 
  149. function cf_custom_fields_has_pr_processor($form, $entry_id) { 
  150. if(!empty($form['processors'])) { 
  151. foreach($form['processors'] as $processor) { 
  152. if($processor['type'] === 'db_storage') { 
  153. $post = get_post($entry_id); 
  154. if(empty($post)) { 
  155. return false; 
  156.  
  157. if($post->post_type === $processor['config']['post_type']) { 
  158. return array( 
  159. 'post' => $post,  
  160. 'config'=> $processor['config'] 
  161. ); 
  162.  
  163. return false; 
  164.  
  165. /** 
  166. * Get entry details 
  167. * 
  168. * @since 1.1.0 
  169. * 
  170. * @param array $entry Entry details 
  171. * @param string $entry_id Entry ID 
  172. * @param array $form 
  173. * 
  174. * @return array 
  175. */ 
  176. function cf_custom_fields_entry_details($entry, $entry_id, $form) { 
  177.  
  178.  
  179. if($processor = cf_custom_fields_has_pr_processor($form, $entry_id)) { 
  180.  
  181. $entry = array( 
  182. 'id' => $entry_id,  
  183. 'form_id' => $form['ID'],  
  184. 'user_id' => $processor['post']->post_author,  
  185. 'datestamp' => $processor['post']->post_date 
  186. ); 
  187.  
  188.  
  189. return $entry; 
  190.  
  191.  
  192. /** 
  193. * Render saved entry when editing posts. 
  194. * 
  195. * @since 1.1.0 
  196. * 
  197. * @uses "caldera_forms_render_get_entry" filter 
  198. * 
  199. * @param array $data Rendered data. 
  200. * @param array $form Form config. 
  201. * @param string $entry_id Entry ID. 
  202. * 
  203. * @return array 
  204. */ 
  205. function cf_custom_fields_get_post_type_entry($data, $form, $entry_id) { 
  206.  
  207. if($processor = cf_custom_fields_has_pr_processor($form, $entry_id)) { 
  208. $fields = $form['fields']; 
  209.  
  210. $data = array(); 
  211. $data[$fields[$processor['config']['post_title']]['slug']] = $processor['post']->post_title; 
  212. unset($fields[$processor['config']['post_title']]); 
  213.  
  214. if(!empty($processor['config']['post_content'])) { 
  215. $data[$fields[$processor['config']['post_content']]['slug']] = $processor['post']->post_content; 
  216. unset($fields[$processor['config']['post_content']]); 
  217.  
  218.  
  219. foreach($fields as $field) { 
  220. $data[$field['slug']] = get_post_meta( $processor['post']->ID, $field['slug'], true ); 
  221. return $data; 
  222.  
  223.  
  224.  
  225. /** 
  226. * Process entry and save as post/ post meta. 
  227. * 
  228. * @since 1.1.0 
  229. * 
  230. * @param array $config Processor config. 
  231. * @param array $form From config. 
  232. * 
  233. * @return array 
  234. */ 
  235. function cf_custom_fields_capture_entry($config, $form) { 
  236.  
  237. $user_id = get_current_user_id(); 
  238. if( !empty( $config['post_author'] ) ) { 
  239. $user_id = Caldera_Forms::do_magic_tags( $config['post_author'] ); 
  240.  
  241. $entry = array( 
  242. 'post_title' => Caldera_Forms::get_field_data( $config['post_title'], $form ),  
  243. 'post_status' => Caldera_Forms::do_magic_tags( $config['post_status'] ),  
  244. 'post_type' => $config['post_type'],  
  245. 'post_content' => Caldera_Forms::get_field_data( $config['post_content'], $form ),  
  246. 'post_parent' => Caldera_Forms::do_magic_tags( $config['post_parent'] ),  
  247. 'to_ping' => Caldera_Forms::do_magic_tags( $config['to_ping'] ),  
  248. 'post_password' => Caldera_Forms::do_magic_tags( $config['post_password'] ),  
  249. 'post_excerpt' => Caldera_Forms::do_magic_tags( $config['post_excerpt'] ),  
  250. 'comment_status'=> $config['comment_status'],  
  251. ); 
  252.  
  253. if( empty( $entry[ 'post_content' ] ) ) { 
  254. $entry[ 'post_content' ] = ''; 
  255.  
  256. // set the ID 
  257. if( !empty( $config['ID'] ) ) { 
  258. $is_post_id = Caldera_Forms::do_magic_tags( $config['ID'] ); 
  259. $post = get_post( $is_post_id ); 
  260. if( !empty( $post ) && $post->post_type == $entry['post_type'] ) { 
  261. $entry['ID'] = $is_post_id; 
  262.  
  263.  
  264. // set author 
  265. if( !empty( $user_id ) ) { 
  266. $entry['post_author'] = $user_id; 
  267.  
  268. //is edit? 
  269. if(!empty($_POST['_cf_frm_edt'])) { 
  270. // need to work on this still. SIGH! 
  271. }else{ 
  272. // Insert the post into the database 
  273. $entry_id = wp_insert_post( $entry ); 
  274. if(empty($entry_id)) { 
  275. return; 
  276.  
  277. $post = get_post( $entry_id ); 
  278.  
  279.  
  280.  
  281. // do upload + attach before 1.5.0.9 
  282. //see https://github.com/CalderaWP/caldera-custom-fields/issues/17 
  283. if ( ! method_exists( 'Caldera_Forms_Admin', 'is_page' ) ) { 
  284. if ( ! empty( $config[ 'featured_image' ] ) ) { 
  285. $featured_image = Caldera_Forms::get_field_data( $config[ 'featured_image' ], $form ); 
  286. foreach ( (array) $featured_image as $filename ) { 
  287. $featured_image = cf_custom_fields_attach_file( $filename, $entry_id ); 
  288. update_post_meta( $entry_id, '_thumbnail_id', $featured_image ); 
  289. }else{ 
  290. if( !empty( $config['featured_image'] ) ) { 
  291. $featured_image = Caldera_Forms_Transient::get_transient( 'cf_cf_featured_' . $form[ 'ID' ] ); 
  292. if ( is_numeric( $featured_image ) ) { 
  293. set_post_thumbnail( $post, $featured_image ); 
  294.  
  295.  
  296.  
  297.  
  298. //handle taxonomies 
  299. $terms_saved = false; 
  300. $tax_fields = cf_custom_fields_get_taxonomy_fields( $config ); 
  301. if ( ! empty( $tax_fields ) ) { 
  302. $terms_saved = cf_custom_fields_save_terms( $tax_fields, $entry_id ); 
  303. if ( $terms_saved ) { 
  304. $term_values = wp_list_pluck( $tax_fields, 'terms' ); 
  305.  
  306. //get post fields into an array of fields not to save as meta. 
  307. $post_fields = array_keys( $entry ); 
  308. $mapped_post_fields = array(); 
  309. foreach ( $config as $field => $bound ) { 
  310. if( ! empty( $bound ) && in_array( $field, $post_fields ) ) { 
  311. $mapped_post_fields[ $field ] = $bound; 
  312.  
  313.  
  314. // get all submission data 
  315. $data = Caldera_Forms::get_submission_data( $form ); 
  316. update_post_meta( $entry_id, '_cf_form_id', $form['ID'] ); 
  317. foreach ( $data as $field_id => $value ) { 
  318.  
  319. $field = Caldera_Forms_Field_Util::get_field( $field_id, $form ); 
  320. if( empty( $field ) ) { 
  321. continue; 
  322. $slug = $field[ 'slug' ]; 
  323. if( in_array( $slug, $mapped_post_fields ) || in_array( $field[ 'ID' ], $mapped_post_fields ) ) { 
  324. continue; 
  325.  
  326. if( in_array( '%' . $slug . '%', $mapped_post_fields ) ) { 
  327. continue; 
  328.  
  329. $type = Caldera_Forms_Field_Util::get_type( $field, $form ); 
  330. if( Caldera_Forms_Fields::not_support( $type, 'entry_list')) { 
  331. continue; 
  332.  
  333. if ( '_entry_token' != $field && '_entry_id' != $field ) { 
  334. if ( in_array( $field, $post_fields ) || in_array( $form['fields'][ $field_id ]['ID'], $post_fields ) ) { 
  335. continue; 
  336.  
  337.  
  338. if ( $terms_saved && is_array( $term_values ) ) { 
  339. if ( is_array( $value ) ) { 
  340. $_value = implode( ', ', $value ); 
  341. } else { 
  342. $_value = $value; 
  343.  
  344. if( in_array( $_value, $term_values ) ) { 
  345. continue; 
  346.  
  347.  
  348.  
  349. if( Caldera_Forms_Field_Util::is_file_field( $field, $form ) ) { 
  350. if( $field == $config['featured_image'] ) { 
  351. continue; // dont attach twice. 
  352. foreach( (array) $value as $file ) { 
  353. cf_custom_fields_attach_file( $file , $entry_id ); 
  354.  
  355.  
  356.  
  357. /** 
  358. * Filter value before saving using to post type processor 
  359. * 
  360. * @since 2.0.3 
  361. * 
  362. * @param mixed $value The value to be saved 
  363. * @param string $slug Slug of field 
  364. * @param int $entry ID of post 
  365. */ 
  366. $value = apply_filters( 'cf_custom_fields_pre_save_meta_key_to_post_type', $value, $slug, $entry_id ); 
  367. update_post_meta( $entry_id, $slug, $value ); 
  368.  
  369. return array('Post ID' => $entry_id, 'ID' => $entry_id, 'permalink' => get_permalink( $entry_id ) ); 
  370.  
  371. /** 
  372. * Handle file fields. 
  373. * 
  374. * @since 1.1.0 
  375. * 
  376. * @param string $file File path. 
  377. * @param string $entry_id The entry ID 
  378. * 
  379. * @return int Attachment ID. 
  380. */ 
  381. function cf_custom_fields_attach_file( $file, $entry_id ) { 
  382.  
  383. // Make sure that this file is included, as wp_generate_attachment_metadata() depends on it. 
  384. require_once( ABSPATH . 'wp-admin/includes/image.php' ); 
  385.  
  386. // Check the type of file. We'll use this as the 'post_mime_type'. 
  387. $filetype = wp_check_filetype( basename( $file ), null ); 
  388.  
  389. // Get the path to the upload directory. 
  390. $wp_upload_dir = wp_upload_dir(); 
  391.  
  392. $filename = $wp_upload_dir['path'] . '/' . basename( $file ); 
  393. $attachment = array( 
  394. 'guid' => $wp_upload_dir['url'] . '/' . basename( $file ),  
  395. 'post_mime_type' => $filetype['type'],  
  396. 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $file ) ),  
  397. 'post_content' => '',  
  398. 'post_status' => 'inherit' 
  399. ); 
  400.  
  401. // Insert the attachment. 
  402. $attach_id = wp_insert_attachment( $attachment, $filename, $entry_id ); 
  403.  
  404. // Generate the metadata for the attachment, and update the database record. 
  405. $attach_data = wp_generate_attachment_metadata( $attach_id, $filename ); 
  406. wp_update_attachment_metadata( $attach_id, $attach_data ); 
  407.  
  408. return $attach_id; 
  409.  
  410.  
  411.  
  412. function cf_custom_fields_taxonomy_ui() { 
  413. $taxonomies = get_taxonomies( array(), 'objects' ); 
  414. $fields = array(); 
  415. $args = array( 
  416. 'magic' => true,  
  417. 'block' => true,  
  418. 'type' => 'text',  
  419. ); 
  420. foreach( $taxonomies as $taxonomy => $obj ) { 
  421. $args[ 'id' ] = 'cf-custom-fields-tax-' . $taxonomy; 
  422. $args[ 'label' ] = $obj->labels->singular_name; 
  423. $args[ 'extra_classes' ] = $taxonomy; 
  424. $fields[] = Caldera_Forms_Processor_UI::config_field( $args ); 
  425.  
  426. return implode( "\n\n", $fields ); 
  427.  
  428. /** 
  429. * Find taxonomy fields and values 
  430. * 
  431. * @since 2.1.0 
  432. *  
  433. * @param $all_fields 
  434. * 
  435. * @return array 
  436. */ 
  437. function cf_custom_fields_get_taxonomy_fields( $all_fields ) { 
  438. $tax_fields = array(); 
  439. foreach( $all_fields as $field => $value ) { 
  440. if( false !== strpos( $field, 'cf-custom-fields-tax-') ) { 
  441. if ( ! empty( $value ) ) { 
  442. $tax_fields[ $field ] = array( 
  443. 'taxonomy' => str_replace( 'cf-custom-fields-tax-', '', $field ),  
  444. 'terms' => Caldera_Forms::do_magic_tags( $value ) 
  445. ); 
  446.  
  447. return $tax_fields; 
  448.  
  449. /** 
  450. * Save taxonomy terms 
  451. *  
  452. * @since 2.1.0 
  453. *  
  454. * @param array $tax_fields Taxonomy fields to save 
  455. * @param int $post_id Post ID 
  456. * 
  457. * @return bool 
  458. */ 
  459. function cf_custom_fields_save_terms( $tax_fields, $post_id ) { 
  460. if ( is_array( $tax_fields ) ) { 
  461. foreach ( $tax_fields as $taxonomy => $data ) { 
  462. if( empty( $data[ 'terms' ] ) ) { 
  463. continue; 
  464. $terms = $data[ 'terms' ]; 
  465. if( is_numeric( $terms ) && false === strpos( $terms, ', ' ) ) { 
  466. $terms = (int) $terms; 
  467.  
  468. }elseif( is_string( $terms ) && false != strpos( $terms, ', ' ) ) { 
  469. $terms = explode( ', ', $terms ); 
  470. foreach( $terms as $i => $term ) { 
  471. $terms[ $i ] = intval( $terms[ $i ] ); 
  472. }elseif ( is_string( $terms ) ) { 
  473. $terms = get_term_by( 'slug', $data[ 'terms' ], $data[ 'taxonomy'] ); 
  474. if( is_a( $terms, 'WP_Term') ) { 
  475. $terms = $terms->term_id; 
  476. }else{ 
  477. $terms = get_term_by( 'name', $data[ 'terms' ], $data[ 'taxonomy'] ); 
  478. if( is_a( $terms, 'WP_Term') ) { 
  479. $terms = $terms->term_id; 
  480. }else{ 
  481. continue; 
  482. } elseif( is_array( $terms ) ) { 
  483. //yolo(?) 
  484. }else { 
  485. continue; 
  486.  
  487. $updated = wp_set_object_terms( $post_id, $terms, $data[ 'taxonomy'] ); 
  488.  
  489.  
  490.  
  491.  
  492. return true; 
  493.  
  494. /** 
  495. * Use custom handler for featured image 
  496. * 
  497. * @uses "caldera_forms_file_upload_handler" filter 
  498. * 
  499. * @since 2.1.4 
  500. * 
  501. * @param array|string|callable Callable 
  502. * @param array $form Form config 
  503. * @param array $field Field config 
  504. * 
  505. * @return string 
  506. */ 
  507. function cf_custom_fields_filter_upload_handler( $handler, $form, $field ) { 
  508. if( cf_custom_fields_is_featured_image_field( $field, $form ) ) { 
  509. return 'cf_custom_fields_upload_handler'; 
  510.  
  511. return $handler; 
  512.  
  513.  
  514. /** 
  515. * Handle featured image uploads 
  516. * 
  517. * @since 2.1.4 
  518. * 
  519. * @param array $upload 
  520. * @param array $file 
  521. * @param array $upload_args 
  522. */ 
  523. function cf_custom_fields_upload_handler( $upload, $file, $upload_args ) { 
  524. $upload = wp_handle_upload($upload, array( 'test_form' => false ) ); 
  525. $attachment_id = Caldera_Forms_Files::add_to_media_library( $upload ); 
  526. if ( is_numeric( $attachment_id ) ) { 
  527. Caldera_Forms_Transient::set_transient( 'cf_cf_featured_' . $file ['form_id'], $attachment_id ); 
  528.  
  529. /** 
  530. * Set featured image fields NOT to add to media library since they already are 
  531. * 
  532. * @since 2.1.4 
  533. * 
  534. * @uses "caldera_forms_render_setup_field" filter 
  535. * 
  536. * @param array $field Field config 
  537. * @param array $form Form config 
  538. * 
  539. * @return mixed 
  540. */ 
  541. function cf_custom_fields_filter_featured_image( $field, $form ) { 
  542. if( cf_custom_fields_is_featured_image_field( $field, $form ) ) { 
  543. $field['config']['media_lib'] = false; 
  544.  
  545. return $field; 
  546.  
  547.  
  548. /** 
  549. * Check if field is the featured image field 
  550. * 
  551. * @since 2.1.4 
  552. * 
  553. * @param array $field Field config 
  554. * @param array $form Form config 
  555. * 
  556. * @return bool 
  557. */ 
  558. function cf_custom_fields_is_featured_image_field( $field, $form ) { 
  559. $has = Caldera_Forms::get_processor_by_type( 'post_type', $form ); 
  560. if( $has && $field[ 'ID' ] == $has[0]['config'][ 'featured_image'] ) { 
  561. return true; 
  562.  
  563. return false; 
.