/deprecated/includes/admin/pages/ninja-forms-impexp/tabs/impexp-forms/impexp-forms.php

  1. <?php if ( ! defined( 'ABSPATH' ) ) exit; 
  2. add_action('init', 'ninja_forms_register_tab_impexp_forms'); 
  3.  
  4. function ninja_forms_register_tab_impexp_forms() { 
  5. $args = array( 
  6. 'name' => __( 'Forms', 'ninja-forms' ),  
  7. 'page' => 'ninja-forms-impexp',  
  8. 'display_function' => '',  
  9. 'save_function' => 'ninja_forms_save_impexp_forms',  
  10. 'show_save' => false,  
  11. ); 
  12. ninja_forms_register_tab('impexp_forms', $args); 
  13.  
  14.  
  15. add_action('init', 'ninja_forms_register_imp_forms_metabox'); 
  16. function ninja_forms_register_imp_forms_metabox() { 
  17. $args = array( 
  18. 'page' => 'ninja-forms-impexp',  
  19. 'tab' => 'impexp_forms',  
  20. 'slug' => 'imp_form',  
  21. 'title' => __( 'Import a form', 'ninja-forms' ),  
  22. 'settings' => array( 
  23. array( 
  24. 'name' => 'userfile',  
  25. 'type' => 'file',  
  26. 'label' => __( 'Select a file', 'ninja-forms' ),  
  27. 'desc' => '',  
  28. 'max_file_size' => 5000000,  
  29. 'help_text' => '',  
  30. ),  
  31. array( 
  32. 'name' => 'submit',  
  33. 'type' => 'submit',  
  34. 'label' => __( 'Import Form', 'ninja-forms' ),  
  35. 'class' => 'button-secondary',  
  36. ),  
  37. ),  
  38. ); 
  39. ninja_forms_register_tab_metabox($args); 
  40.  
  41. function ninja_forms_register_exp_forms_metabox() { 
  42. if ( ! isset ( $_REQUEST['page'] ) || 'ninja-forms-impexp' != $_REQUEST['page'] ) 
  43. return false; 
  44.  
  45. $form_results = ninja_forms_get_all_forms(); 
  46. $form_select = array(); 
  47. if(is_array($form_results) AND !empty($form_results)) { 
  48. foreach($form_results as $form) { 
  49. if( isset( $form['data'] ) ) { 
  50. $data = $form['data']; 
  51. $form_title = $data['form_title']; 
  52. array_push($form_select, array('name' => $form_title, 'value' => $form['id'])); 
  53. $args = array( 
  54. 'page' => 'ninja-forms-impexp',  
  55. 'tab' => 'impexp_forms',  
  56. 'slug' => 'exp_form',  
  57. 'title' => __('Export a form', 'ninja-forms'),  
  58. 'settings' => array( 
  59. array( 
  60. 'name' => 'form_id',  
  61. 'type' => 'select',  
  62. 'label' => __('Select a form', 'ninja-forms'),  
  63. 'desc' => '',  
  64. 'options' => $form_select,  
  65. 'help_text' => '',  
  66. ),  
  67. array( 
  68. 'name' => 'submit',  
  69. 'type' => 'submit',  
  70. 'label' => __('Export Form', 'ninja-forms'),  
  71. 'class' => 'button-secondary',  
  72. ),  
  73. ),  
  74. ); 
  75. ninja_forms_register_tab_metabox($args); 
  76.  
  77. add_action('admin_init', 'ninja_forms_register_exp_forms_metabox'); 
  78.  
  79. /** 
  80. * 
  81. * Function that returns a serialized string containing the form for export. 
  82. * 
  83. * @since 2.2.42 
  84. * @returns $form_row string 
  85. */ 
  86.  
  87. function ninja_forms_serialize_form( $form_id ) { 
  88. if ( $form_id == '' ) 
  89. return; 
  90.  
  91. $plugin_settings = nf_get_settings(); 
  92. $form_row = array(); 
  93. $form_row['data'] = Ninja_Forms()->form( $form_id )->get_all_settings(); 
  94. $field_results = ninja_forms_get_fields_by_form_id( $form_id ); 
  95. $form_row['id'] = NULL; 
  96. if ( is_array ( $form_row ) AND ! empty ( $form_row ) ) { 
  97. if ( is_array( $field_results ) AND ! empty( $field_results ) ) { 
  98. $x = 0; 
  99. foreach( $field_results as $field ) { 
  100. $form_row['field'][$x] = $field; 
  101. $x++; 
  102.  
  103. // Get all of our notifications for this form 
  104. $notifications = nf_get_notifications_by_form_id( $form_id ); 
  105. $form_row['notifications'] = $notifications; 
  106.  
  107. $form_row = apply_filters( 'nf_export_form_row', $form_row ); 
  108.  
  109. $form_row = serialize($form_row); 
  110.  
  111. return $form_row; 
  112.  
  113.  
  114. function ninja_forms_export_form( $form_id ) { 
  115. if($form_id == '') 
  116. return; 
  117. $plugin_settings = nf_get_settings(); 
  118. $form_title = Ninja_Forms()->form( $form_id )->get_setting( 'form_title' ); 
  119. $form_row = ninja_forms_serialize_form( $form_id ); 
  120. $form_title = preg_replace('/[^a-zA-Z0-9-]/', '', $form_title); 
  121. $form_title = str_replace (" ", "-", $form_title); 
  122.  
  123. if(isset($plugin_settings['date_format'])) { 
  124. $date_format = $plugin_settings['date_format']; 
  125. }else{ 
  126. $date_format = 'm/d/Y'; 
  127.  
  128. //$today = date($date_format); 
  129. $current_time = current_time('timestamp'); 
  130. $today = date($date_format, $current_time); 
  131.  
  132. header("Content-type: application/csv"); 
  133. header('Content-Disposition: attachment; filename="'.$form_title.'"-"'.$today.'".nff"'); 
  134. header("Pragma: no-cache"); 
  135. header("Expires: 0"); 
  136. echo $form_row; 
  137. die(); 
  138.  
  139. function ninja_forms_save_impexp_forms($data) { 
  140. global $wpdb, $ninja_forms_admin_update_message; 
  141. $plugin_settings = nf_get_settings(); 
  142. $form_id = isset( $_REQUEST['form_id'] ) ? absint( $_REQUEST['form_id'] ) : ''; 
  143. $update_msg = ''; 
  144. if( $_REQUEST['submit'] == __('Export Form', 'ninja-forms') OR ( isset( $_REQUEST['export_form'] ) AND absint( $_REQUEST['export_form'] ) == 1 ) ) { 
  145. if($form_id != '') { 
  146. ninja_forms_export_form( $form_id ); 
  147. }else{ 
  148. $ninja_forms_admin_update_message = __( 'Please select a form.', 'ninja-forms' ); 
  149. }elseif($_REQUEST['submit'] == __('Import Form', 'ninja-forms')) { 
  150. if ($_FILES['userfile']['error'] == UPLOAD_ERR_OK AND is_uploaded_file($_FILES['userfile']['tmp_name'])) { 
  151. $file = file_get_contents($_FILES['userfile']['tmp_name']); 
  152. ninja_forms_import_form( $file ); 
  153. $update_msg = __( 'Form Imported Successfully.', 'ninja-forms' ); 
  154. }else{ 
  155. //echo $_FILES['userfile']['error']; 
  156. $update_msg = __( 'Please select a valid exported form file.', 'ninja-forms' ); 
  157. return $update_msg; 
  158.  
  159.  
  160. /** 
  161. * 
  162. * Function that fixes calculation fields and their references to newly created fields. 
  163. * 
  164. * @since 2.2.40 
  165. * @returns void 
  166. */ 
  167.  
  168. add_action( 'ninja_forms_after_import_form', 'ninja_forms_calc_after_import_form' ); 
  169.  
  170. function ninja_forms_calc_after_import_form( $form ) { 
  171. global $wpdb; 
  172.  
  173. if( is_array( $form['field'] ) AND !empty( $form['field'] ) ) { 
  174. $field_rows = ninja_forms_get_fields_by_form_id( $form['id'] ); 
  175. if( is_array( $field_rows ) AND !empty( $field_rows ) ) { 
  176. for ($y=0; $y < count( $field_rows ); $y++) { 
  177. if ( isset ( $field_rows[$y]['data']['calc'] ) AND is_array( $field_rows[$y]['data']['calc'] ) ) { 
  178. for ( $i=0; $i < count( $field_rows[$y]['data']['calc']); $i++ ) { 
  179. foreach( $form['field'] as $inserted_field ) { 
  180. if ( isset ( $field_rows[$y]['data']['calc'][$i]['field'] ) AND $inserted_field['old_id'] == $field_rows[$y]['data']['calc'][$i]['field'] ) { 
  181. $field_rows[$y]['data']['calc'][$i]['field'] = $inserted_field['id']; 
  182.  
  183. if ( isset ( $field_rows[$y]['data']['calc_eq'] ) AND $field_rows[$y]['data']['calc_eq'] != '' ) { 
  184. $calc_eq = $field_rows[$y]['data']['calc_eq']; 
  185. foreach( $form['field'] as $inserted_field ) { 
  186. $calc_eq = str_replace( 'field_'.$inserted_field['old_id'], 'field_'.$inserted_field['id'], $calc_eq ); 
  187. $field_rows[$y]['data']['calc_eq'] = $calc_eq; 
  188. }  
  189.  
  190. $field_rows[$y]['data'] = serialize( $field_rows[$y]['data'] ); 
  191. $args = array( 
  192. 'update_array' => array( 
  193. 'data' => $field_rows[$y]['data'],  
  194. ),  
  195. 'where' => array( 
  196. 'id' => $field_rows[$y]['id'],  
  197. ),  
  198. ); 
  199. ninja_forms_update_field($args); 
  200.  
  201. if ( isset ( $form['data']['success_msg'] ) AND $form['data']['success_msg'] != '' ) { 
  202. $success_msg = $form['data']['success_msg']; 
  203. foreach( $form['field'] as $inserted_field ) { 
  204. $success_msg = str_replace( '[ninja_forms_field id='.$inserted_field['old_id'].']', '[ninja_forms_field id='.$inserted_field['id'].']', $success_msg ); 
  205. $form['data']['success_msg'] = $success_msg; 
  206.  
  207. if ( isset ( $form['data']['user_subject'] ) AND $form['data']['user_subject'] != '' ) { 
  208. $user_subject = $form['data']['user_subject']; 
  209. foreach( $form['field'] as $inserted_field ) { 
  210. $user_subject = str_replace( '[ninja_forms_field id='.$inserted_field['old_id'].']', '[ninja_forms_field id='.$inserted_field['id'].']', $user_subject ); 
  211. $form['data']['user_subject'] = $user_subject; 
  212. }  
  213.  
  214. if ( isset ( $form['data']['user_email_msg'] ) AND $form['data']['user_email_msg'] != '' ) { 
  215. $user_email_msg = $form['data']['user_email_msg']; 
  216. foreach( $form['field'] as $inserted_field ) { 
  217. $user_email_msg = str_replace( '[ninja_forms_field id='.$inserted_field['old_id'].']', '[ninja_forms_field id='.$inserted_field['id'].']', $user_email_msg ); 
  218. $form['data']['user_email_msg'] = $user_email_msg; 
  219.  
  220. if ( isset ( $form['data']['admin_subject'] ) AND $form['data']['admin_subject'] != '' ) { 
  221. $admin_subject = $form['data']['admin_subject']; 
  222. foreach( $form['field'] as $inserted_field ) { 
  223. $admin_subject = str_replace( '[ninja_forms_field id='.$inserted_field['old_id'].']', '[ninja_forms_field id='.$inserted_field['id'].']', $admin_subject ); 
  224. $form['data']['admin_subject'] = $admin_subject; 
  225. }  
  226.  
  227. if ( isset ( $form['data']['admin_email_msg'] ) AND $form['data']['admin_email_msg'] != '' ) { 
  228. $admin_email_msg = $form['data']['admin_email_msg']; 
  229. foreach( $form['field'] as $inserted_field ) { 
  230. $admin_email_msg = str_replace( '[ninja_forms_field id='.$inserted_field['old_id'].']', '[ninja_forms_field id='.$inserted_field['id'].']', $admin_email_msg ); 
  231. $form['data']['admin_email_msg'] = $admin_email_msg; 
  232.  
  233. if ( isset ( $form['data']['mp_confirm_msg'] ) AND $form['data']['mp_confirm_msg'] != '' ) { 
  234. $mp_confirm_msg = $form['data']['mp_confirm_msg']; 
  235. foreach( $form['field'] as $inserted_field ) { 
  236. $mp_confirm_msg = str_replace( '[ninja_forms_field id='.$inserted_field['old_id'].']', '[ninja_forms_field id='.$inserted_field['id'].']', $mp_confirm_msg ); 
  237. $form['data']['mp_confirm_msg'] = $mp_confirm_msg; 
  238.  
  239. if ( isset ( $form['data']['save_msg'] ) AND $form['data']['save_msg'] != '' ) { 
  240. $save_msg = $form['data']['save_msg']; 
  241. foreach( $form['field'] as $inserted_field ) { 
  242. $save_msg = str_replace( '[ninja_forms_field id='.$inserted_field['old_id'].']', '[ninja_forms_field id='.$inserted_field['id'].']', $save_msg ); 
  243. $form['data']['save_msg'] = $save_msg; 
  244.  
  245. if ( isset ( $form['data']['save_email_msg'] ) AND $form['data']['save_email_msg'] != '' ) { 
  246. $save_email_msg = $form['data']['save_email_msg']; 
  247. foreach( $form['field'] as $inserted_field ) { 
  248. $save_email_msg = str_replace( '[ninja_forms_field id='.$inserted_field['old_id'].']', '[ninja_forms_field id='.$inserted_field['id'].']', $save_email_msg ); 
  249. $form['data']['save_email_msg'] = $save_email_msg; 
  250.  
  251. $args = array( 
  252. 'update_array' => array( 
  253. 'data' => serialize( $form['data'] ),  
  254. ),  
  255. 'where' => array( 
  256. 'id' => $form['id'],  
  257. ),  
  258. ); 
  259. ninja_forms_update_form($args); 
.