| 1 |
<?php |
| 2 |
|
| 3 |
class AF_Admin_Forms_Import { |
| 4 |
function __construct() { |
| 5 |
add_action( 'admin_menu', array( $this, 'register_admin_page' ), 5, 0 ); |
| 6 |
add_action( 'admin_init', array( $this, 'import_json_file' ), 10, 0 ); |
| 7 |
add_filter( 'admin_title', array( $this, 'fix_admin_title' ), 10, 2 ); |
| 8 |
} |
| 9 |
|
| 10 |
/** |
| 11 |
* Register hidden admin page for form import. |
| 12 |
* |
| 13 |
* @since 1.7.0 |
| 14 |
* |
| 15 |
*/ |
| 16 |
function register_admin_page() { |
| 17 |
// By using add_submenu_page without a parent the page won't be shown in the admin menu. |
| 18 |
add_submenu_page( 'edit.php?post_type=af_form', __( 'Import form', 'advanced-forms '), __( 'Import', 'advanced-forms' ), 'edit_pages', 'af_import_form', array( $this, 'import_form' ) ); |
| 19 |
} |
| 20 |
|
| 21 |
|
| 22 |
/** |
| 23 |
* Display form import page. |
| 24 |
* |
| 25 |
* @since 1.7.0 |
| 26 |
* |
| 27 |
*/ |
| 28 |
function import_form() { |
| 29 |
?> |
| 30 |
<div class="af-form-import wrap"> |
| 31 |
<h1><?php _e( 'Import form', 'advanced-forms' ); ?></h1> |
| 32 |
<hr class="wp-header-end" /> |
| 33 |
|
| 34 |
<div id="poststuff" class="af-postbox-wrapper"> |
| 35 |
<div class="postbox af-postbox-left"> |
| 36 |
<h2 class="hndle"><span>Import JSON file</span></h2> |
| 37 |
<div class="inside"> |
| 38 |
<form method="post" enctype="multipart/form-data"> |
| 39 |
<?php wp_nonce_field( 'af_import_form', 'af_import_nonce' ); ?> |
| 40 |
<p> |
| 41 |
<?php _e( 'Select the form JSON file you would like to import. If a form with the same key already exists it will be overwritten.', 'advanced-forms' ); ?> |
| 42 |
</p> |
| 43 |
|
| 44 |
<div class="acf-fields"> |
| 45 |
<?php |
| 46 |
acf_render_field_wrap(array( |
| 47 |
'label' => __( 'Select File', 'advanced-forms' ), |
| 48 |
'type' => 'file', |
| 49 |
'name' => 'af_import_json_file', |
| 50 |
'value' => false, |
| 51 |
'uploader' => 'basic', |
| 52 |
)); |
| 53 |
?> |
| 54 |
</div> |
| 55 |
|
| 56 |
<input type="submit" class="button button-primary" name="af_import_json" value="<?php _e( 'Import File', 'advanced-forms' ); ?>" /> |
| 57 |
</form> |
| 58 |
</div> |
| 59 |
</div> |
| 60 |
</div> |
| 61 |
</div> |
| 62 |
<?php |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
function import_json_file() { |
| 67 |
if ( ! isset( $_POST['af_import_json'] ) ) { |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
// Only users who can manage forms may import. This also guards against |
| 72 |
// unauthenticated requests, since admin_init fires on admin-post.php. |
| 73 |
if ( ! current_user_can( 'edit_pages' ) ) { |
| 74 |
wp_die( |
| 75 |
__( 'You do not have sufficient permissions to import forms.', 'advanced-forms' ), |
| 76 |
__( 'Error: Insufficient permissions', 'advanced-forms' ), |
| 77 |
array( 'back_link' => true ) |
| 78 |
); |
| 79 |
} |
| 80 |
|
| 81 |
// Verify the nonce rendered on the import page. |
| 82 |
$nonce = isset( $_POST['af_import_nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['af_import_nonce'] ) ) : ''; |
| 83 |
if ( ! wp_verify_nonce( $nonce, 'af_import_form' ) ) { |
| 84 |
wp_die( |
| 85 |
__( 'Form could not be imported due to a missing or invalid nonce.', 'advanced-forms' ), |
| 86 |
__( 'Error: Invalid nonce', 'advanced-forms' ), |
| 87 |
array( 'back_link' => true ) |
| 88 |
); |
| 89 |
} |
| 90 |
|
| 91 |
if ( empty( $_FILES['af_import_json_file']['size'] ) ) { |
| 92 |
return acf_add_admin_notice( __( 'No files selected', 'advanced-forms' ) ); |
| 93 |
} |
| 94 |
|
| 95 |
$file = $_FILES['af_import_json_file']; |
| 96 |
$json = file_get_contents( $file['tmp_name'] ); |
| 97 |
$json = json_decode( $json, true ); |
| 98 |
|
| 99 |
// Bail if the uploaded file isn't a JSON object describing a form. |
| 100 |
if ( ! is_array( $json ) || empty( $json['key'] ) ) { |
| 101 |
return acf_add_admin_notice( __( 'The selected file is not a valid form export.', 'advanced-forms' ) ); |
| 102 |
} |
| 103 |
|
| 104 |
$post = af_import_form( $json ); |
| 105 |
if ( $post ) { |
| 106 |
/* translators: The ACF field key */ |
| 107 |
$message = sprintf( __( 'Form with key %s imported.', 'advanced-forms' ), $json['key'] ); |
| 108 |
$link = sprintf( '<a href="%s">%s</a>', get_edit_post_link( $post ), __( 'Edit form', 'advanced-forms' ) ); |
| 109 |
acf_add_admin_notice( $message . ' ' . $link, 'success' ); |
| 110 |
} else { |
| 111 |
acf_add_admin_notice( __( 'Failed to import form', 'advanced-forms' ) ); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Wordpress won't display a page title, probably because of the submenu hack in register_admin_page. |
| 117 |
* This function is hooked to the admin_title filter and fixes the issue. |
| 118 |
* |
| 119 |
* @since 1.6.5 |
| 120 |
* |
| 121 |
*/ |
| 122 |
function fix_admin_title( $admin_title, $title ) { |
| 123 |
if ( get_current_screen()->id != 'admin_page_af_import_form' ) { |
| 124 |
return $admin_title; |
| 125 |
} |
| 126 |
|
| 127 |
return __( 'Import form', 'advanced-forms' ) . $admin_title; |
| 128 |
} |
| 129 |
|
| 130 |
} |
| 131 |
|
| 132 |
return new AF_Admin_Forms_Import(); |