/includes/metabox.php

  1. <?php 
  2. /** 
  3. * Functions/hooks for metabox 
  4. * 
  5. * @package caldera_custom_fields 
  6. * @copyright 2014-2017 CalderaWP LLC and David Cramer 
  7. */ 
  8.  
  9. // add actions 
  10. add_action( 'add_meta_boxes', 'cf_custom_fields_form_as_metabox' ); 
  11. add_action( 'save_post', 'cf_custom_fields_save_post' ); 
  12.  
  13. // add filters 
  14. add_filter('caldera_forms_get_form_processors', 'cf_custom_fields_register_metabox_processor'); 
  15.  
  16.  
  17. /** 
  18. * Before regular submit kicks in, check if is metabox save, verify nonce then remove regular path to process. 
  19. * 
  20. * @since 2.1.3 
  21. * 
  22. * Save will happen at "save_post" action 
  23. */ 
  24. add_action( 'wp_loaded', function() { 
  25. if (isset($_POST['_cf_frm_id'], $_POST[ 'cf_metabox_forms'] ) ) { 
  26. if ( isset( $_POST[ '_cf_verify' ] ) && Caldera_Forms_Render_Nonce::verify_nonce( $_POST[ '_cf_verify' ], $_POST[ '_cf_frm_id' ] ) ) { 
  27. remove_action( 'wp_loaded', array( Caldera_Forms::get_instance(), 'cf_init_system' ), 25 ); 
  28.  
  29.  
  30. }, 19 ); 
  31.  
  32.  
  33.  
  34. // admin filters & actions 
  35. if( is_admin() ) { 
  36.  
  37. // disable redirect 
  38. add_filter('caldera_forms_redirect_url', 'cf_custom_fields_prevent_redirect', 1, 4); 
  39.  
  40. // disable mailer 
  41. add_filter('caldera_forms_get_form', 'cf_custom_fields_prevent_mailer', 1, 4); 
  42.  
  43. // save action to disable mailer 
  44. add_filter('caldera_forms_presave_form', 'cf_custom_fields_metabox_save_form'); 
  45.  
  46.  
  47.  
  48. /** 
  49. * Filter form database and mailer options if is set as a metabox 
  50. * 
  51. * @uses "caldera_forms_get_form" 
  52. * 
  53. * @param $form 
  54. * 
  55. * @return array 
  56. */ 
  57. function cf_custom_fields_prevent_mailer( $form ) { 
  58. $processors = Caldera_Forms::get_processor_by_type('cf_asmetabox', $form); 
  59. if( !empty( $processors ) ) { 
  60. if( isset( $form['mailer']['on_insert'] ) ) { 
  61. unset( $form['mailer']['on_insert'] ); 
  62. if( !empty( $form['mailer']['db_support'] ) ) { 
  63. $form['mailer']['db_support'] = false; 
  64. return $form; 
  65. /** 
  66. * Add the processor. 
  67. * 
  68. * @uses "caldera_forms_get_form_processors" 
  69. * 
  70. * @param $processors 
  71. * 
  72. * @return mixed 
  73. */ 
  74. function cf_custom_fields_register_metabox_processor($processors) { 
  75. $processors['cf_asmetabox'] = array( 
  76. "name" => __( 'Custom Fields Post Metabox', 'caldera-forms-metabox' ),  
  77. "author" => 'Caldera Labs',  
  78. "description" => __( 'Use a form as a custom metabox in the post editor.', 'caldera-forms-metabox' ),  
  79. "single" => true,  
  80. "processor" => 'cf_custom_fields_save_meta_data',  
  81. "template" => trailingslashit( CCF_PATH ) . "includes/metabox-config.php",  
  82. "icon" => CCF_URL . "/metabox-icon.png",  
  83. "conditionals" => false,  
  84. ); 
  85. return $processors; 
  86.  
  87.  
  88. /** 
  89. * Disable mailer/ form AJAX/ DB support for metabox forms. 
  90. * 
  91. * @since 1.?.? 
  92. * 
  93. * @uses "caldera_forms_presave_form" filter 
  94. * 
  95. * @param $form 
  96. */ 
  97. function cf_custom_fields_metabox_save_form($form) { 
  98. if ( isset( $form[ 'processors' ] ) ) { 
  99. foreach ( $form[ 'processors' ] as $processor ) { 
  100. if ( 'cf_asmetabox' == $processor[ 'type' ] ) { 
  101. $form[ 'is_metabox' ] = true; 
  102. // disable DB support 
  103. $form['db_support'] = 0; 
  104.  
  105. // no ajax forms 
  106. if( isset( $form['form_ajax'] ) ) { 
  107. unset( $form['form_ajax'] ); 
  108.  
  109. // disable mailer 
  110. $form['mailer']['enable_mailer'] = 0; 
  111. $form['db_support'] = 0; 
  112. $form['mailer']['enable_mailer'] = 0; 
  113.  
  114. return $form; 
  115.  
  116.  
  117.  
  118. return $form; 
  119.  
  120.  
  121.  
  122.  
  123.  
  124. /** 
  125. * Prevent redirect. 
  126. * 
  127. * @since 1.?.? 
  128. * 
  129. * @uses "caldera_forms_redirect_url" filter; 
  130. * 
  131. * @param string $url Redirect URL 
  132. * @param array $data Submission data. 
  133. * @param array $form Form config. 
  134. * 
  135. * @return bool 
  136. */ 
  137. function cf_custom_fields_prevent_redirect($url, $form, $process_id) { 
  138.  
  139. if( !empty($form['is_metabox'])) { 
  140. return false; 
  141.  
  142. return $url; 
  143.  
  144. /** 
  145. * Save meta data from form. 
  146. * 
  147. * @since 1.?.? 
  148. * 
  149. * @param array $config Processor config. 
  150. * @param array $form Form config. 
  151. */ 
  152. function cf_custom_fields_save_meta_data($config, $form) { 
  153. global $post; 
  154.  
  155. if(!is_admin()) { 
  156. return; 
  157.  
  158. $data = Caldera_Forms::get_submission_data($form); 
  159.  
  160. $field_toremove = array(); 
  161.  
  162. foreach($form['fields'] as $field_id => $field) { 
  163. if( 'button' == Caldera_Forms_Field_Util::get_type($field, $form ) && $field['config']['type'] === 'submit' ) { 
  164. unset( $form[ 'fields' ][ $field_id ] ); 
  165. }elseif ( Caldera_Forms_Field_Util::is_file_field( $field, $form ) ) { 
  166. unset( $form[ 'fields' ][ $field_id ] ); 
  167. }elseif( Caldera_Forms_Fields::not_support( Caldera_Forms_Field_Util::get_type( $field, $form ), 'entry_list')) { 
  168. unset( $form[ 'fields' ][ $field_id ] ); 
  169.  
  170.  
  171. delete_post_meta( $post->ID, $field['slug'] ); 
  172.  
  173. // add filter to remove submit buttons 
  174. add_filter('caldera_forms_render_setup_field', 'cf_custom_fields_submit_button_removal', 10, 2 ); 
  175.  
  176. // add filter to remove file fields 
  177. add_filter('caldera_forms_render_setup_field', 'cf_custom_fields_remove_file_fields', 10, 2 ); 
  178.  
  179. foreach($data as $key=>$value) { 
  180. if(empty($form['fields'][$key])) { 
  181. continue; 
  182.  
  183. $slug = $form['fields'][$key]['slug']; 
  184.  
  185. /** 
  186. * Filter value before saving using to metabox processor 
  187. * @since 2.0.3 
  188. * 
  189. * @param mixed $value The value to be saved 
  190. * @param string $slug Slug of field 
  191. * @param int $post_id ID of post 
  192. */ 
  193. $value = apply_filters( 'cf_custom_fields_pre_save_meta_key_metabox', $value, $slug, $post->ID ); 
  194. if( is_array( $value ) ) { 
  195. delete_post_meta( $post->ID, $slug ); 
  196. foreach( $value as $single_value ) { 
  197. add_post_meta( $post->ID, $slug, $single_value );  
  198. }else{ 
  199. update_post_meta( $post->ID, $slug, $value ); 
  200. if(isset($field_toremove[$form['fields'][$key]['slug']])) { 
  201. unset($field_toremove[$form['fields'][$key]['slug']]); 
  202.  
  203. return $data; 
  204.  
  205.  
  206. /** 
  207. * Setup form in the admin 
  208. * 
  209. * @uses "add_meta_boxes" action 
  210. * 
  211. * @since 1.?.? 
  212. */ 
  213. function cf_custom_fields_form_as_metabox() { 
  214. $forms = Caldera_Forms_Forms::get_forms( true ); 
  215. if(empty($forms)) { 
  216. return; 
  217. foreach($forms as $form) { 
  218. $form = Caldera_Forms_Forms::get_form( $form[ 'ID' ] ); 
  219. if( ! is_array( $form ) ) { 
  220. continue; 
  221. $processors = Caldera_Forms::get_processor_by_type('cf_asmetabox', $form); 
  222.  
  223. if(!empty( $processors )) { 
  224.  
  225. $processor = $processors[0]; 
  226.  
  227. // is metabox processor 
  228. if(!empty($form['processors'][ $processor['ID'] ]['config']['posttypes'])) { 
  229.  
  230. // add filter to get details of entry 
  231. add_filter('caldera_forms_get_entry_detail', 'cf_custom_fields_get_post_details', 10, 3 ); 
  232.  
  233. // add filter to remove submit buttons 
  234. add_filter('caldera_forms_render_setup_field', 'cf_custom_fields_submit_button_removal', 10, 2 ); 
  235.  
  236. // add filter to remove file fields 
  237. add_filter('caldera_forms_render_setup_field', 'cf_custom_fields_remove_file_fields', 10, 2 ); 
  238.  
  239. foreach( $form['processors'][ $processor['ID'] ]['config']['posttypes'] as $screen=>$enabled) { 
  240. add_meta_box( 
  241. $form['ID'],  
  242. $form['name'],  
  243. 'cf_custom_fields_render',  
  244. $screen,  
  245. $form['processors'][ $processor['ID'] ]['config']['context'],  
  246. $form['processors'][ $processor['ID'] ]['config']['priority'] 
  247. ); 
  248.  
  249. Caldera_Forms_Admin_Assets::admin_common(); 
  250. Caldera_Forms_Admin_Assets::form_editor(); 
  251. Caldera_Forms_Render_Assets::enqueue_all_fields(); 
  252.  
  253. // metabox & gridcss 
  254. wp_enqueue_style( 'cf-metabox-grid-styles', CCF_URL . 'css/metagrid.css'); 
  255. wp_enqueue_style( 'cf-metabox-styles', CCF_URL . 'css/metabox.css'); 
  256.  
  257.  
  258. /** 
  259. * Save form meta data. 
  260. * 
  261. * @since 1.?.? 
  262. * 
  263. * @uses "caldera_forms_render_get_entry" filter 
  264. * 
  265. * @param $data 
  266. * @param $form 
  267. * 
  268. * @return array 
  269. */ 
  270. function cf_custom_fields_get_meta_data($data, $form) { 
  271. global $post; 
  272. $entry = array(); 
  273. foreach($form['fields'] as $fieldslug => $field ) { 
  274. $data = get_post_meta( $post->ID, $field['slug'] ); 
  275. if( count( $data ) > 1 ) { 
  276. foreach( $data as $item ) { 
  277. $entry[$fieldslug][] = $item; 
  278. }else{ 
  279.  
  280. if( is_array( $data ) && isset( $data[0] ) && is_scalar( $data[0] ) ) { 
  281. $data = $data[0]; 
  282. }elseif( is_array( $data ) ) { 
  283. $data = ''; 
  284.  
  285. $entry[$fieldslug] = $data; 
  286.  
  287. return $entry; 
  288.  
  289.  
  290. /** 
  291. * Process data on post save. 
  292. * 
  293. * @since 1.?.? 
  294. * 
  295. * @uses "save_post" action 
  296. */ 
  297. function cf_custom_fields_save_post() { 
  298.  
  299. if(is_admin()) { 
  300. if(isset($_POST['cf_metabox_forms'])) { 
  301.  
  302. foreach( $_POST['cf_metabox_forms'] as $metaForm ) { 
  303. // add filter to get details of entry 
  304. $_POST['_cf_frm_id'] = $metaForm; 
  305. add_filter('caldera_forms_get_entry_detail', 'cf_custom_fields_get_post_details', 10, 3); 
  306. Caldera_Forms::process_submission(); 
  307.  
  308.  
  309.  
  310. /** 
  311. * Render fields in editor. 
  312. * 
  313. * @since 1.?.? 
  314. * 
  315. * @param object $post Post object. 
  316. * @param array $args Args 
  317. */ 
  318. function cf_custom_fields_render($post, $args) { 
  319. if(isset($_GET['cf_su'])) { 
  320. unset($_GET['cf_su']); 
  321. add_filter( 'caldera_forms_render_pre_get_entry', 'cf_custom_fields_get_meta_data', 10, 2); 
  322. add_filter( 'caldera_forms_render_form_element', function( $element ) { 
  323. return 'div'; 
  324. } ); 
  325. ob_start(); 
  326. $form = Caldera_Forms::render_form( $args['id'] ); 
  327.  
  328. // register this form for processing' 
  329. echo '<input type="hidden" name="cf_metabox_forms[]" value="' . $args['id'] . '">'; 
  330. echo $form; 
  331.  
  332.  
  333.  
  334. /** 
  335. * Get details from entry. 
  336. * 
  337. * @since 1.?.? 
  338. * 
  339. * @uses "caldera_forms_get_entry_detail" fitler 
  340. * 
  341. * @param $details 
  342. * @param $entry 
  343. * @param $form 
  344. * 
  345. * @return array 
  346. */ 
  347. function cf_custom_fields_get_post_details($details, $entry, $form) { 
  348. global $post; 
  349.  
  350. return array( 
  351. 'id' => $post->ID,  
  352. 'form_id' => $form['ID'],  
  353. 'user_id' => get_current_user_id(),  
  354. 'datestamp' => $post->post_date 
  355. ); 
  356.  
  357. /** 
  358. * Remove submit button. 
  359. * 
  360. * @since 1.?.? 
  361. * 
  362. * @uses "caldera_forms_render_setup_field" filter 
  363. * @param array $field 
  364. * @param array $form 
  365. * 
  366. * @return bool|array 
  367. */ 
  368. function cf_custom_fields_submit_button_removal($field, $form ) { 
  369. $type = Caldera_Forms_Field_Util::get_type($field, $form ); 
  370. if( 'button' == $type ) { 
  371. $field['config']['class'] .= ' button'; 
  372. if( $field['config']['type'] === 'submit' ) { 
  373. return false; 
  374. return $field; 
  375.  
  376.  
  377. /** 
  378. * Remove file fields 
  379. * 
  380. * @since 2.1.3 
  381. * 
  382. * @uses "caldera_forms_render_setup_field" filter 
  383. * @param array $field 
  384. * @param array $form 
  385. * 
  386. * @return bool|array 
  387. */ 
  388. function cf_custom_fields_remove_file_fields( $field, $form ) { 
  389. if( Caldera_Forms_Field_Util::is_file_field( $field, $form ) ) { 
  390. return false; 
  391.  
  392. return $field; 
  393.  
.