PageRenderTime 56ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/Admin/Menus/ImportExport.php

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