Skip to content

Commit

Permalink
Merge pull request #1456 from sapayth/fix/form_import_is_not_working
Browse files Browse the repository at this point in the history
fix: form import is not working
  • Loading branch information
sapayth authored May 6, 2024
2 parents ecc329e + 421dd21 commit fcb8626
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 21 deletions.
1 change: 1 addition & 0 deletions includes/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function __construct() {
$this->plugin_upgrade_notice = new Admin\Plugin_Upgrade_Notice();
$this->posting = new Admin\Posting();
$this->shortcodes_button = new Admin\Shortcodes_Button();
$this->tools = new Admin\Admin_Tools();

// dynamic hook. format: "admin_action_{$action}". more details: wp-admin/admin.php
add_action( 'admin_action_post_form_template', [ $this, 'create_post_form_from_template' ] );
Expand Down
57 changes: 36 additions & 21 deletions includes/Admin/Admin_Tools.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace WeDevs\Wpuf\Admin;

use WP_Error;
use WP_Http;
use WP_Query;

/**
Expand All @@ -12,9 +14,9 @@
class Admin_Tools {

public function __construct() {
add_action( 'admin_init', [ $this, 'handle_tools_action' ] );
add_filter( 'wp_handle_upload_prefilter', [ $this, 'enable_json_upload' ], 1 );
add_action( 'wpuf_load_tools', [ $this, 'handle_tools_action' ] );
add_filter( 'upload_mimes', [ $this, 'add_json_mime_type' ] );
add_filter( 'wp_handle_upload_prefilter', [ $this, 'enable_json_upload' ] );
}

/**
Expand Down Expand Up @@ -73,7 +75,7 @@ public function list_forms() {

<?php
} else {
sprintf( '<p>%s</p>', __( 'Sorry you have no form to export', 'wp-user-frontend' ) );
printf( '<p>%s</p>', __( 'Sorry you have no post form to export', 'wp-user-frontend' ) );
}
}

Expand Down Expand Up @@ -138,7 +140,7 @@ public function list_regis_forms() {
</div>
<?php
} else {
sprintf( '<p>%s</p>', __( 'Sorry you have no form to export', 'wp-user-frontend' ) );
printf( '<p>%s</p>', __( 'Sorry you have no registration form to export', 'wp-user-frontend' ) );
}
}

Expand Down Expand Up @@ -215,12 +217,12 @@ public static function import_json_file( $file ) {
* Export normal form data
*
* @param string $export_type
* @param int $form_ids
* @param array $form_ids
*/
public function export_forms( $form_type, $export_type, $form_ids ) {
if ( $export_type === 'all' ) {
static::export_to_json( $form_type );
} else if ( $export_type === 'selected' ) {
} else if ( 'selected' === $export_type ) {
if ( empty( $form_ids ) ) {
printf(
'<div class="error"><p>%s</p></div>',
Expand Down Expand Up @@ -455,32 +457,45 @@ public function enable_json_upload( $file ) {
public function import_forms() {
check_ajax_referer( 'wpuf_admin_tools' );
if ( ! isset( $_POST['file_id'] ) ) {
wp_send_json_error( new WP_Error( 'wpuf_ajax_import_forms_error',
__( 'Missing file_id param', 'wp-user-frontend' ) ),
WP_Http::BAD_REQUEST );
wp_send_json_error(
new WP_Error(
'wpuf_ajax_import_forms_error',
__( 'Missing file_id param', 'wp-user-frontend' )
),
WP_Http::BAD_REQUEST
);
}
$file_id = absint( wp_unslash( $_POST['file_id'] ) );
$file = get_attached_file( $file_id );
if ( empty( $file ) ) {
wp_send_json_error( new WP_Error( 'wpuf_ajax_import_forms_error',
__( 'JSON file not found', 'wp-user-frontend' ) ), WP_Http::NOT_FOUND );
wp_send_json_error(
new WP_Error(
'wpuf_ajax_import_forms_error',
__( 'JSON file not found', 'wp-user-frontend' )
),
WP_Http::NOT_FOUND
);
}
$filetype = wp_check_filetype( $file, [ 'json' => 'application/json' ] );
if ( ! isset( $filetype['type'] ) || 'application/json' !== $filetype['type'] ) {
wp_send_json_error( new WP_Error( 'wpuf_ajax_import_forms_error',
__( 'Provided file is not a JSON file.', 'wp-user-frontend' ) ),
WP_Http::UNSUPPORTED_MEDIA_TYPE );
wp_send_json_error(
new WP_Error(
'wpuf_ajax_import_forms_error',
__( 'Provided file is not a JSON file.', 'wp-user-frontend' )
),
WP_Http::UNSUPPORTED_MEDIA_TYPE
);
}
if ( ! class_exists( 'WPUF_Admin_Tools' ) ) {
require_once WPUF_ROOT . '/admin/class-tools.php';
}
$imported = Admin_Tools::import_json_file( $file );

$imported = self::import_json_file( $file );
if ( is_wp_error( $imported ) ) {
wp_send_json_error( $imported, WP_Http::UNPROCESSABLE_ENTITY );
}
wp_send_json_success( [
'message' => __( 'Forms imported successfully.', 'wp-user-frontend' ),
] );
wp_send_json_success(
[
'message' => __( 'Forms imported successfully.', 'wp-user-frontend' ),
]
);
}

/**
Expand Down
6 changes: 6 additions & 0 deletions includes/Admin/Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,12 @@ public function fix_submenu_file( $submenu_file ) {
* @return void
*/
public function enqueue_tools_script() {
/**
* Backdoor for calling the menu hook.
* This hook won't get translated even the site language is changed
*/
do_action( 'wpuf_load_tools' );

wp_enqueue_media(); // for uploading JSON

wp_enqueue_script( 'wpuf-vue' );
Expand Down

0 comments on commit fcb8626

Please sign in to comment.