PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/ninja-forms/includes/Admin/Menus/ImportExport.php

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