NF_Admin_Menus_ImportExport

The Ninja Forms NF Admin Menus ImportExport class.

Defined (1)

The class is defined in the following location(s).

/includes/Admin/Menus/ImportExport.php  
  1. final class NF_Admin_Menus_ImportExport extends NF_Abstracts_Submenu 
  2. public $parent_slug = 'ninja-forms'; 
  3.  
  4. public $menu_slug = 'nf-import-export'; 
  5.  
  6. public function __construct() 
  7. add_action( 'plugins_loaded', array( $this, 'import_form_listener' ) ); 
  8. add_action( 'plugins_loaded', array( $this, 'export_form_listener' ) ); 
  9.  
  10. add_action( 'plugins_loaded', array( $this, 'import_fields_listener' ) ); 
  11. add_action( 'plugins_loaded', array( $this, 'export_fields_listener' ) ); 
  12.  
  13. add_filter( 'ninja_forms_before_import_fields', array( $this, 'import_fields_backwards_compatibility' ) ); 
  14.  
  15. parent::__construct(); 
  16.  
  17. public function get_page_title() 
  18. return __( 'Import / Export', 'ninja-forms' ); 
  19.  
  20. public function import_form_listener() 
  21. if( ! current_user_can( apply_filters( 'ninja_forms_admin_import_form_capabilities', 'manage_options' ) ) ) return; 
  22.  
  23. if( ! isset( $_FILES[ 'nf_import_form' ] ) || ! $_FILES[ 'nf_import_form' ] ) return; 
  24.  
  25. $this->upload_error_check( $_FILES[ 'nf_import_form' ] ); 
  26.  
  27. $data = file_get_contents( $_FILES[ 'nf_import_form' ][ 'tmp_name' ] ); 
  28.  
  29. $import = Ninja_Forms()->form()->import_form( $data ); 
  30.  
  31. if( ! $import ) { 
  32.  
  33. wp_die( 
  34. __( 'There uploaded file is not a valid format.', 'ninja-forms' ) . ' ' . ( function_exists( 'json_last_error' ) ) ? json_last_error_msg() : '',  
  35. __( 'Invalid Form Upload.', 'ninja-forms' ) 
  36. ); 
  37.  
  38. public function export_form_listener() 
  39. if( ! current_user_can( apply_filters( 'ninja_forms_admin_export_form_capabilities', 'manage_options' ) ) ) return; 
  40.  
  41. if( isset( $_REQUEST[ 'nf_export_form' ] ) && $_REQUEST[ 'nf_export_form' ] ) { 
  42. $form_id = $_REQUEST[ 'nf_export_form' ]; 
  43. Ninja_Forms()->form( $form_id )->export_form(); 
  44.  
  45. public function import_fields_listener() 
  46. if( ! current_user_can( apply_filters( 'ninja_forms_admin_import_fields_capabilities', 'manage_options' ) ) ) return; 
  47.  
  48. if( ! isset( $_FILES[ 'nf_import_fields' ] ) || ! $_FILES[ 'nf_import_fields' ] ) return; 
  49.  
  50. $this->upload_error_check( $_FILES[ 'nf_import_fields' ] ); 
  51.  
  52. $import = file_get_contents( $_FILES[ 'nf_import_fields' ][ 'tmp_name' ] ); 
  53.  
  54. $fields = unserialize( $import ); 
  55.  
  56. foreach( $fields as $settings ) { 
  57. Ninja_Forms()->form()->import_field( $settings ); 
  58.  
  59. public function export_fields_listener() 
  60. if( ! current_user_can( apply_filters( 'ninja_forms_admin_export_fields_capabilities', 'manage_options' ) ) ) return; 
  61.  
  62. if( isset( $_REQUEST[ 'nf_export_fields' ] ) && $_REQUEST[ 'nf_export_fields' ] ) { 
  63. $field_ids = $_REQUEST[ 'nf_export_fields' ]; 
  64.  
  65. $fields = array(); 
  66. foreach( $field_ids as $field_id ) { 
  67. $field = Ninja_Forms()->form()->field( $field_id )->get(); 
  68.  
  69. $fields[] = $field->get_settings(); 
  70.  
  71. header("Content-type: application/csv"); 
  72. header("Content-Disposition: attachment; filename=favorites-" . time() . ".nff"); 
  73. header("Pragma: no-cache"); 
  74. header("Expires: 0"); 
  75.  
  76. echo serialize( $fields ); 
  77.  
  78. die(); 
  79.  
  80.  
  81. public function display() 
  82. $tabs = apply_filters( 'ninja_forms_import_export_tabs', array( 
  83. 'forms' => __( 'Form', 'ninja-forms' ),  
  84. 'favorite_fields' => __( 'Favorite Fields', 'ninja-forms' ) 
  85. ); 
  86.  
  87. $tab_keys = array_keys( $tabs ); 
  88. $active_tab = ( isset( $_GET[ 'tab' ] ) ) ? $_GET[ 'tab' ] : reset( $tab_keys ); 
  89.  
  90. $this->add_meta_boxes(); 
  91.  
  92. wp_enqueue_script('postbox'); 
  93. wp_enqueue_script('jquery-ui-draggable'); 
  94.  
  95. wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false ); 
  96. wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false ); 
  97.  
  98. Ninja_Forms::template( 'admin-menu-import-export.html.php', compact( 'tabs', 'active_tab' ) ); 
  99.  
  100. public function add_meta_boxes() 
  101. /** 
  102. * Forms 
  103. */ 
  104. add_meta_box( 
  105. 'nf_import_export_forms_import',  
  106. __( 'Import Forms', 'ninja-forms' ),  
  107. array( $this, 'template_import_forms' ),  
  108. 'nf_import_export_forms' 
  109. ); 
  110.  
  111. add_meta_box( 
  112. 'nf_import_export_forms_export',  
  113. __( 'Export Forms', 'ninja-forms' ),  
  114. array( $this, 'template_export_forms' ),  
  115. 'nf_import_export_forms' 
  116. ); 
  117.  
  118. /** 
  119. * FAVORITE FIELDS 
  120. */ 
  121. add_meta_box( 
  122. 'nf_import_export_favorite_fields_import',  
  123. __( 'Import Favorite Fields', 'ninja-forms' ),  
  124. array( $this, 'template_import_favorite_fields' ),  
  125. 'nf_import_export_favorite_fields' 
  126. ); 
  127.  
  128. add_meta_box( 
  129. 'nf_import_export_favorite_fields_export',  
  130. __( 'Export Favorite Fields', 'ninja-forms' ),  
  131. array( $this, 'template_export_favorite_fields' ),  
  132. 'nf_import_export_favorite_fields' 
  133. ); 
  134.  
  135. public function template_import_forms() 
  136. Ninja_Forms::template( 'admin-metabox-import-export-forms-import.html.php' ); 
  137.  
  138. public function template_export_forms() 
  139. $forms = Ninja_Forms()->form()->get_forms(); 
  140. Ninja_Forms::template( 'admin-metabox-import-export-forms-export.html.php', compact( 'forms' ) ); 
  141.  
  142. public function template_import_favorite_fields() 
  143. Ninja_Forms::template( 'admin-metabox-import-export-favorite-fields-import.html.php' ); 
  144.  
  145. public function template_export_favorite_fields() 
  146. $fields = Ninja_Forms()->form()->get_fields( array( 'saved' => 1) ); 
  147. Ninja_Forms::template( 'admin-metabox-import-export-favorite-fields-export.html.php', compact( 'fields' ) ); 
  148.  
  149. /** 
  150. |-------------------------------------------------------------------------- 
  151. | Backwards Compatibility 
  152. |-------------------------------------------------------------------------- 
  153. */ 
  154.  
  155. public function import_fields_backwards_compatibility( $field ) 
  156. //TODO: This was copied over. Instead need to abstract backwards compatibility for re-use. 
  157. // Flatten field settings array 
  158. if( isset( $field[ 'data' ] ) ) { 
  159. $field = array_merge( $field, $field[ 'data' ] ); 
  160. unset( $field[ 'data' ] ); 
  161.  
  162. // Drop form_id in favor of parent_id, which is set by the form. 
  163. if( isset( $field[ 'form_id' ] ) ) { 
  164. unset( $field[ 'form_id' ] ); 
  165.  
  166. // Remove `_` prefix from type setting 
  167. $field[ 'type' ] = ltrim( $field[ 'type' ], '_' ); 
  168.  
  169. // Type: `text` -> `textbox` 
  170. if( 'text' == $field[ 'type' ] ) { 
  171. $field[ 'type' ] = 'textbox'; 
  172.  
  173. if( 'submit' == $field[ 'type' ] ) { 
  174. $field[ 'processing_label' ] = 'Processing'; 
  175.  
  176. if( 'calc' == $field[ 'type' ] ) { 
  177. $field[ 'type' ] = 'note'; 
  178.  
  179. if( isset( $field[ 'calc_method' ] ) ) { 
  180.  
  181. switch( $field[ 'calc_method' ] ) { 
  182. case 'eq': 
  183. $method = __( 'Equation (Advanced)', 'ninja-forms' ); 
  184. break; 
  185. case 'fields': 
  186. $method = __( 'Operations and Fields (Advanced)', 'ninja-forms' ); 
  187. break; 
  188. case 'auto': 
  189. $method = __( 'Auto-Total Fields', 'ninja-forms' ); 
  190. break; 
  191. default: 
  192. $method = ''; 
  193. $field['default'] = $method . "\r\n"; 
  194.  
  195. if ('eq' == $field['calc_method'] && isset( $field['calc_eq'] ) ) { 
  196. $field['default'] .= $field['calc_eq']; 
  197.  
  198. if ('fields' == $field['calc_method'] && isset( $field['calc'] ) ) { 
  199. // TODO: Support 'operations and fields (advanced)' calculations. 
  200.  
  201. if ('auto' == $field['calc_method'] && isset( $field['calc'] ) ) { 
  202. // TODO: Support 'auto-totaling' calculations. 
  203.  
  204. unset( $field[ 'calc' ] ); 
  205. unset( $field[ 'calc_eq' ] ); 
  206. unset( $field[ 'calc_method' ] ); 
  207.  
  208. if( isset( $field[ 'email' ] ) ) { 
  209.  
  210. if( 'textbox' == $field[ 'type' ] && $field[ 'email' ] ) { 
  211. $field['type'] = 'email'; 
  212. unset( $field[ 'email' ] ); 
  213.  
  214. if( isset( $field[ 'class' ] ) ) { 
  215. $field[ 'element_class' ] = $field[ 'class' ]; 
  216. unset( $field[ 'class' ] ); 
  217.  
  218. if( isset( $field[ 'req' ] ) ) { 
  219. $field[ 'required' ] = $field[ 'req' ]; 
  220. unset( $field[ 'req' ] ); 
  221.  
  222. if( isset( $field[ 'default_value_type' ] ) ) { 
  223.  
  224. /** User Data */ 
  225. if( '_user_id' == $field[ 'default_value_type' ] ) $field[ 'default' ] = '{user:id}'; 
  226. if( '_user_email' == $field[ 'default_value_type' ] ) $field[ 'default' ] = '{user:email}'; 
  227. if( '_user_lastname' == $field[ 'default_value_type' ] ) $field[ 'default' ] = '{user:last_name}'; 
  228. if( '_user_firstname' == $field[ 'default_value_type' ] ) $field[ 'default' ] = '{user:first_name}'; 
  229. if( '_user_display_name' == $field[ 'default_value_type' ] ) $field[ 'default' ] = '{user:display_name}'; 
  230.  
  231. /** Post Data */ 
  232. if( 'post_id' == $field[ 'default_value_type' ] ) $field[ 'default' ] = '{post:id}'; 
  233. if( 'post_url' == $field[ 'default_value_type' ] ) $field[ 'default' ] = '{post:url}'; 
  234. if( 'post_title' == $field[ 'default_value_type' ] ) $field[ 'default' ] = '{post:title}'; 
  235.  
  236. /** System Data */ 
  237. if( 'today' == $field[ 'default_value_type' ] ) $field[ 'default' ] = '{system:date}'; 
  238.  
  239. /** Miscellaneous */ 
  240. if( '_custom' == $field[ 'default_value_type' ] && isset( $field[ 'default_value' ] ) ) { 
  241. $field[ 'default' ] = $field[ 'default_value' ]; 
  242. if( 'querystring' == $field[ 'default_value_type' ] && isset( $field[ 'default_value' ] ) ) { 
  243. $field[ 'default' ] = '{' . $field[ 'default_value' ] . '}'; 
  244.  
  245. unset( $field[ 'default_value' ] ); 
  246. unset( $field[ 'default_value_type' ] ); 
  247.  
  248. if( 'list' == $field[ 'type' ] ) { 
  249.  
  250. if ( isset( $field[ 'list_type' ] ) ) { 
  251.  
  252. if ('dropdown' == $field['list_type']) { 
  253. $field['type'] = 'listselect'; 
  254. if ('radio' == $field['list_type']) { 
  255. $field['type'] = 'listradio'; 
  256. if ('checkbox' == $field['list_type']) { 
  257. $field['type'] = 'listcheckbox'; 
  258. if ('multi' == $field['list_type']) { 
  259. $field['type'] = 'listmultiselect'; 
  260.  
  261. if( isset( $field[ 'list' ][ 'options' ] ) ) { 
  262. $field[ 'options' ] = $field[ 'list' ][ 'options' ]; 
  263. unset( $field[ 'list' ][ 'options' ] ); 
  264.  
  265. // Convert `textbox` to other field types 
  266. foreach( array( 'fist_name', 'last_name', 'user_zip', 'user_city', 'user_phone', 'user_email', 'user_address_1', 'user_address_2', 'datepicker' ) as $item ) { 
  267. if ( isset( $field[ $item ] ) && $field[ $item ] ) { 
  268. $field[ 'type' ] = str_replace( array( '_', 'user', '1', '2', 'picker' ), '', $item ); 
  269.  
  270. unset( $field[ $item ] ); 
  271.  
  272. if( 'timed_submit' == $field[ 'type' ] ) { 
  273. $field[ 'type' ] = 'submit'; 
  274.  
  275. return $field; 
  276.  
  277. private function upload_error_check( $file ) 
  278. if( ! $file[ 'error' ] ) return; 
  279.  
  280. switch ( $file[ 'error' ] ) { 
  281. case UPLOAD_ERR_INI_SIZE: 
  282. $error_message = __( 'The uploaded file exceeds the upload_max_filesize directive in php.ini.', 'ninja-forms' ); 
  283. break; 
  284. case UPLOAD_ERR_FORM_SIZE: 
  285. $error_message = __( 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.', 'ninja-forms' ); 
  286. break; 
  287. case UPLOAD_ERR_PARTIAL: 
  288. $error_message = __( 'The uploaded file was only partially uploaded.', 'ninja-forms' ); 
  289. break; 
  290. case UPLOAD_ERR_NO_FILE: 
  291. $error_message = __( 'No file was uploaded.', 'ninja-forms' ); 
  292. break; 
  293. case UPLOAD_ERR_NO_TMP_DIR: 
  294. $error_message = __( 'Missing a temporary folder.', 'ninja-forms' ); 
  295. break; 
  296. case UPLOAD_ERR_CANT_WRITE: 
  297. $error_message = __( 'Failed to write file to disk.', 'ninja-forms' ); 
  298. break; 
  299. case UPLOAD_ERR_EXTENSION: 
  300. $error_message = __( 'File upload stopped by extension.', 'ninja-forms' ); 
  301. break; 
  302. default: 
  303. $error_message = __( 'Unknown upload error.', 'ninja-forms' ); 
  304. break; 
  305.  
  306. $args = array( 
  307. 'title' => __( 'File Upload Error', 'ninja-forms' ),  
  308. 'message' => $error_message,  
  309. 'debug' => $file,  
  310. ); 
  311. $message = Ninja_Forms()->template( 'admin-wp-die.html.php', $args ); 
  312. wp_die( $message, $args[ 'title' ], array( 'back_link' => TRUE ) ); 
  313.  
  314. public function get_capability() 
  315. return apply_filters( 'ninja_forms_admin_import_export_capabilities', $this->capability );