/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. /** 
  17. * Register this as a processor. 
  18. * 
  19. * @uses "caldera_forms_get_form_processors" filter 
  20. * 
  21. * @param $processors 
  22. * 
  23. * @return mixed 
  24. */ 
  25. function cf_custom_fields_posttype_process($processors) { 
  26.  
  27. $processors['post_type'] = array( 
  28. "name" => __( 'Save as Post Type', 'caldera-forms-metabox' ),  
  29. "author" => 'David Cramer',  
  30. "description" => __( 'Store form entries as a post with custom fields.', 'caldera-forms-metabox' ),  
  31. "post_processor" => 'cf_custom_fields_capture_entry',  
  32. "template" => trailingslashit( CCF_PATH ) . "includes/to-post-type-config.php",  
  33. "icon" => CCF_URL . "/post-type.png",  
  34. "default" => array( 
  35. 'post_status' => "draft" 
  36. ),  
  37. "meta_template" => trailingslashit( CCF_PATH ) . "includes/meta_template.php",  
  38. "magic_tags" => array( 
  39. "ID",  
  40. "permalink" 
  41.  
  42. ); 
  43. return $processors; 
  44.  
  45.  
  46. /** 
  47. * Prepared from data for edit post. 
  48. * 
  49. * @since 1.1.0 
  50. * 
  51. * @uses "caldera_forms_render_pre_get_entry" filter 
  52. * 
  53. * @param array $data Form data. 
  54. * @param array $form Form config. 
  55. * @param $entry_id 
  56. * 
  57. * @return array 
  58. */ 
  59. function cf_custom_fields_populate_form_edit( $data, $form, $entry_id ) { 
  60.  
  61. $processors = Caldera_Forms::get_processor_by_type( 'post_type', $form ); 
  62. if( !empty( $processors ) ) { 
  63. foreach( $processors as $processor ) { 
  64. if( !empty( $processor['config']['ID'] ) ) { 
  65. // ooo ID!!! 
  66. $ID = Caldera_Forms::do_magic_tags( $processor['config']['ID'], $entry_id ); 
  67. if( !empty( $ID ) ) { 
  68. $post = get_post( $ID ); 
  69. if( empty( $post ) ) { 
  70. return $data; 
  71.  
  72. $data[ $processor['config']['post_title'] ] = $post->post_title; 
  73. $data[ $processor['config']['post_content'] ] = $post->post_content; 
  74. foreach( $form['fields'] as $field_id => $field ) { 
  75. if( $post->{$field['slug']} ) { 
  76. $data[ $field_id ] = $post->{$field['slug']}; 
  77. return $data; 
  78.  
  79. /** 
  80. * Register the add-on 
  81. * 
  82. * @since 1.1.0 
  83. * 
  84. * @uses "caldera_forms_get_addons" filter 
  85. * 
  86. * @param array $addons 
  87. * 
  88. * @return array 
  89. */ 
  90. function cf_custom_fields_savetoposttype_addon($addons) { 
  91. $addons['savetoposttype'] = __FILE__; 
  92. return $addons; 
  93.  
  94.  
  95. /** 
  96. * Set template for meta fields 
  97. * 
  98. * @since 1.1.0 
  99. * 
  100. * @uses "caldera_forms_processor_templates" action 
  101. */ 
  102. function cf_custom_fields_meta_template() { 
  103. ?> 
  104. <script type="text/html" id="post-meta-field-tmpl"> 
  105. <div class="caldera-config-group"> 
  106. <label>Field Name<input type="text" class="block-input field-config" name="{{_name}}[metakey][]" value="{{metakey}}"></label> 
  107. <div class="caldera-config-field"> 
  108. <div>Value Field</div> 
  109. <div style="width: 280px; display:inline-block;">{{{_field slug="meta_field" array="true"}}}</div> 
  110. <button class="button remove-meta-field{{_id}}" type="button"><?php echo __('Remove', 'caldera-forms'); ?></button> 
  111. </div> 
  112. </div> 
  113. </script> 
  114. <?php 
  115.  
  116. /** 
  117. * Prepare DB storage 
  118. * 
  119. * @uses "caldera_forms_get_entry_meta_db_storage" filter 
  120. * 
  121. * @param $meta 
  122. * 
  123. * @return mixed 
  124. */ 
  125. function cf_custom_fields_meta_view($meta) { 
  126. $postid = $meta['meta_value']; 
  127. $meta['meta_key'] = _('Post Name', 'caldera-forms-metabox' ); 
  128. $meta['meta_value'] = get_the_title($meta['meta_value']); 
  129. $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>'; 
  130. $meta['post'] = get_post( $postid ); 
  131. return $meta; 
  132.  
  133. /** 
  134. * Check if a form has the post type processor 
  135. * 
  136. * @since 1.1.0 
  137. * 
  138. * @param array $form Form config. 
  139. * @param string $entry_id Entry ID. 
  140. * 
  141. * @return array|bool False if not in processor, else post object/ config. 
  142. */ 
  143. function cf_custom_fields_has_pr_processor($form, $entry_id) { 
  144. if(!empty($form['processors'])) { 
  145. foreach($form['processors'] as $processor) { 
  146. if($processor['type'] === 'db_storage') { 
  147. $post = get_post($entry_id); 
  148. if(empty($post)) { 
  149. return false; 
  150.  
  151. if($post->post_type === $processor['config']['post_type']) { 
  152. return array( 
  153. 'post' => $post,  
  154. 'config'=> $processor['config'] 
  155. ); 
  156.  
  157. return false; 
  158.  
  159. /** 
  160. * Get entry details 
  161. * 
  162. * @since 1.1.0 
  163. * 
  164. * @param array $entry Entry details 
  165. * @param string $entry_id Entry ID 
  166. * @param array $form 
  167. * 
  168. * @return array 
  169. */ 
  170. function cf_custom_fields_entry_details($entry, $entry_id, $form) { 
  171.  
  172.  
  173. if($processor = cf_custom_fields_has_pr_processor($form, $entry_id)) { 
  174.  
  175. $entry = array( 
  176. 'id' => $entry_id,  
  177. 'form_id' => $form['ID'],  
  178. 'user_id' => $processor['post']->post_author,  
  179. 'datestamp' => $processor['post']->post_date 
  180. ); 
  181.  
  182.  
  183. return $entry; 
  184.  
  185.  
  186. /** 
  187. * Render saved entry when editing posts. 
  188. * 
  189. * @since 1.1.0 
  190. * 
  191. * @uses "caldera_forms_render_get_entry" filter 
  192. * 
  193. * @param array $data Rendered data. 
  194. * @param array $form Form config. 
  195. * @param string $entry_id Entry ID. 
  196. * 
  197. * @return array 
  198. */ 
  199. function cf_custom_fields_get_post_type_entry($data, $form, $entry_id) { 
  200.  
  201. if($processor = cf_custom_fields_has_pr_processor($form, $entry_id)) { 
  202. $fields = $form['fields']; 
  203.  
  204. $data = array(); 
  205. $data[$fields[$processor['config']['post_title']]['slug']] = $processor['post']->post_title; 
  206. unset($fields[$processor['config']['post_title']]); 
  207.  
  208. if(!empty($processor['config']['post_content'])) { 
  209. $data[$fields[$processor['config']['post_content']]['slug']] = $processor['post']->post_content; 
  210. unset($fields[$processor['config']['post_content']]); 
  211.  
  212.  
  213. foreach($fields as $field) { 
  214. $data[$field['slug']] = get_post_meta( $processor['post']->ID, $field['slug'], true ); 
  215. return $data; 
  216.  
  217.  
  218.  
  219. /** 
  220. * Process entry and save as post/ post meta. 
  221. * 
  222. * @since 1.1.0 
  223. * 
  224. * @param array $config Processor config. 
  225. * @param array $form From config. 
  226. * 
  227. * @return array 
  228. */ 
  229. function cf_custom_fields_capture_entry($config, $form) { 
  230.  
  231. $user_id = get_current_user_id(); 
  232. if( !empty( $config['post_author'] ) ) { 
  233. $user_id = Caldera_Forms::do_magic_tags( $config['post_author'] ); 
  234.  
  235. $entry = array( 
  236. 'post_title' => Caldera_Forms::get_field_data( $config['post_title'], $form ),  
  237. 'post_status' => Caldera_Forms::do_magic_tags( $config['post_status'] ),  
  238. 'post_type' => $config['post_type'],  
  239. 'post_content' => Caldera_Forms::get_field_data( $config['post_content'], $form ),  
  240. 'post_parent' => Caldera_Forms::do_magic_tags( $config['post_parent'] ),  
  241. 'to_ping' => Caldera_Forms::do_magic_tags( $config['to_ping'] ),  
  242. 'post_password' => Caldera_Forms::do_magic_tags( $config['post_password'] ),  
  243. 'post_excerpt' => Caldera_Forms::do_magic_tags( $config['post_excerpt'] ),  
  244. 'comment_status'=> $config['comment_status'],  
  245. ); 
  246.  
  247. if( empty( $entry[ 'post_content' ] ) ) { 
  248. $entry[ 'post_content' ] = ''; 
  249.  
  250. // set the ID 
  251. if( !empty( $config['ID'] ) ) { 
  252. $is_post_id = Caldera_Forms::do_magic_tags( $config['ID'] ); 
  253. $post = get_post( $is_post_id ); 
  254. if( !empty( $post ) && $post->post_type == $entry['post_type'] ) { 
  255. $entry['ID'] = $is_post_id; 
  256.  
  257.  
  258. // set author 
  259. if( !empty( $user_id ) ) { 
  260. $entry['post_author'] = $user_id; 
  261.  
  262. //is edit? 
  263. if(!empty($_POST['_cf_frm_edt'])) { 
  264. // need to work on this still. SIGH! 
  265. }else{ 
  266. // Insert the post into the database 
  267. $entry_id = wp_insert_post( $entry ); 
  268. if(empty($entry_id)) { 
  269. return; 
  270.  
  271.  
  272.  
  273. // do upload + attach 
  274. if( !empty( $config['featured_image'] ) ) { 
  275. $featured_image = Caldera_Forms::get_field_data( $config['featured_image'], $form ); 
  276. foreach( (array) $featured_image as $filename ) { 
  277. $featured_image = cf_custom_fields_attach_file( $filename, $entry_id ); 
  278. update_post_meta($entry_id, '_thumbnail_id', $featured_image ); 
  279.  
  280.  
  281. //handle taxonomies 
  282. $terms_saved = false; 
  283. $tax_fields = cf_custom_fields_get_taxonomy_fields( $config ); 
  284. if ( ! empty( $tax_fields ) ) { 
  285. $terms_saved = cf_custom_fields_save_terms( $tax_fields, $entry_id ); 
  286. if ( $terms_saved ) { 
  287. $term_values = wp_list_pluck( $tax_fields, 'terms' ); 
  288.  
  289. //get post fields into an array of fields not to save as meta. 
  290. $post_fields = array_keys( $entry ); 
  291. // get all submission data 
  292. $data = Caldera_Forms::get_submission_data( $form ); 
  293. update_post_meta( $entry_id, '_cf_form_id', $form['ID'] ); 
  294. foreach($data as $field=>$value) { 
  295. if ( '_entry_token' != $field && '_entry_id' != $field ) { 
  296. if ( in_array( $field, $post_fields ) || in_array( $form['fields'][ $field ]['ID'], $post_fields ) ) { 
  297. continue; 
  298.  
  299.  
  300. if ( $terms_saved ) { 
  301. if ( is_array( $value ) ) { 
  302. $_value = implode( ', ', $value ); 
  303. } else { 
  304. $_value = $value; 
  305.  
  306. if( in_array( $_value, $term_values ) ) { 
  307. continue; 
  308.  
  309.  
  310.  
  311.  
  312. if(empty($form['fields'][$field])) { 
  313. continue; 
  314. if( in_array( $form['fields'][$field]['type'], array( 'button', 'html' ) ) ) { 
  315. continue; 
  316. if( $form['fields'][$field]['type'] == 'file' ) { 
  317. if( $field == $config['featured_image'] ) { 
  318. continue; // dont attache twice. 
  319. foreach( (array) $value as $file ) { 
  320. cf_custom_fields_attach_file( $file , $entry_id ); 
  321.  
  322. $slug = $form['fields'][$field]['slug']; 
  323.  
  324. /** 
  325. * Filter value before saving using to post type processor 
  326. * 
  327. * @since 2.0.3 
  328. * 
  329. * @param mixed $value The value to be saved 
  330. * @param string $slug Slug of field 
  331. * @param int $entry ID of post 
  332. */ 
  333. $value = apply_filters( 'cf_custom_fields_pre_save_meta_key_to_post_type', $value, $slug, $entry_id ); 
  334. update_post_meta( $entry_id, $slug, $value ); 
  335.  
  336. return array('Post ID' => $entry_id, 'ID' => $entry_id, 'permalink' => get_permalink( $entry_id ) ); 
  337.  
  338. /** 
  339. * Handle file fields. 
  340. * 
  341. * @since 1.1.0 
  342. * 
  343. * @param string $file File path. 
  344. * @param string $entry_id The entry ID 
  345. * 
  346. * @return int Attachment ID. 
  347. */ 
  348. function cf_custom_fields_attach_file( $file, $entry_id ) { 
  349.  
  350. // Make sure that this file is included, as wp_generate_attachment_metadata() depends on it. 
  351. require_once( ABSPATH . 'wp-admin/includes/image.php' ); 
  352.  
  353. // Check the type of file. We'll use this as the 'post_mime_type'. 
  354. $filetype = wp_check_filetype( basename( $file ), null ); 
  355.  
  356. // Get the path to the upload directory. 
  357. $wp_upload_dir = wp_upload_dir(); 
  358.  
  359. $filename = $wp_upload_dir['path'] . '/' . basename( $file ); 
  360. $attachment = array( 
  361. 'guid' => $wp_upload_dir['url'] . '/' . basename( $file ),  
  362. 'post_mime_type' => $filetype['type'],  
  363. 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $file ) ),  
  364. 'post_content' => '',  
  365. 'post_status' => 'inherit' 
  366. ); 
  367.  
  368. // Insert the attachment. 
  369. $attach_id = wp_insert_attachment( $attachment, $filename, $entry_id ); 
  370.  
  371. // Generate the metadata for the attachment, and update the database record. 
  372. $attach_data = wp_generate_attachment_metadata( $attach_id, $filename ); 
  373. wp_update_attachment_metadata( $attach_id, $attach_data ); 
  374.  
  375. return $attach_id; 
  376.  
  377.  
  378.  
  379. function cf_custom_fields_taxonomy_ui() { 
  380. $taxonomies = get_taxonomies( array(), 'objects' ); 
  381. $fields = array(); 
  382. $args = array( 
  383. 'magic' => true,  
  384. 'block' => true,  
  385. 'type' => 'text',  
  386. ); 
  387. foreach( $taxonomies as $taxonomy => $obj ) { 
  388. $args[ 'id' ] = 'cf-custom-fields-tax-' . $taxonomy; 
  389. $args[ 'label' ] = $obj->labels->singular_name; 
  390. $args[ 'extra_classes' ] = $taxonomy; 
  391. $fields[] = Caldera_Forms_Processor_UI::config_field( $args ); 
  392.  
  393. return implode( "\n\n", $fields ); 
  394.  
  395. /** 
  396. * Find taxonomy fields and values 
  397. * 
  398. * @since 2.1.0 
  399. *  
  400. * @param $all_fields 
  401. * 
  402. * @return array 
  403. */ 
  404. function cf_custom_fields_get_taxonomy_fields( $all_fields ) { 
  405. $tax_fields = array(); 
  406. foreach( $all_fields as $field => $value ) { 
  407. if( false !== strpos( $field, 'cf-custom-fields-tax-') ) { 
  408. if ( ! empty( $value ) ) { 
  409. $tax_fields[ $field ] = array( 
  410. 'taxonomy' => str_replace( 'cf-custom-fields-tax-', '', $field ),  
  411. 'terms' => Caldera_Forms::do_magic_tags( $value ) 
  412. ); 
  413.  
  414. return $tax_fields; 
  415.  
  416. /** 
  417. * Save taxonomy terms 
  418. *  
  419. * @since 2.1.0 
  420. *  
  421. * @param array $tax_fields Taxonomy fields to save 
  422. * @param int $post_id Post ID 
  423. * 
  424. * @return bool 
  425. */ 
  426. function cf_custom_fields_save_terms( $tax_fields, $post_id ) { 
  427. if ( is_array( $tax_fields ) ) { 
  428. foreach ( $tax_fields as $taxonomy => $data ) { 
  429. if( empty( $data[ 'terms' ] ) ) { 
  430. continue; 
  431. $terms = $data[ 'terms' ]; 
  432. if( is_numeric( $terms ) && false === strpos( $terms, ', ' ) ) { 
  433. $terms = (int) $terms; 
  434.  
  435. }elseif( is_string( $terms ) && false != strpos( $terms, ', ' ) ) { 
  436. $terms = explode( ', ', $terms ); 
  437. foreach( $terms as $i => $term ) { 
  438. $terms[ $i ] = intval( $terms[ $i ] ); 
  439. }elseif( is_array( $terms ) ) { 
  440. //yolo(?) 
  441. }else { 
  442. continue; 
  443.  
  444. $updated = wp_set_object_terms( $post_id, $terms, $data[ 'taxonomy'] ); 
  445.  
  446.  
  447.  
  448.  
  449. return true; 
.