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