| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! class_exists( 'WP_Importer' ) ) { |
| 4 |
$class_wp_importer = ABSPATH . 'wp-admin/includes/class-wp-importer.php'; |
| 5 |
if ( file_exists( $class_wp_importer ) ) { |
| 6 |
require $class_wp_importer; |
| 7 |
} |
| 8 |
} |
| 9 |
|
| 10 |
|
| 11 |
class HappyForms_Import_Controller extends WP_Importer { |
| 12 |
|
| 13 |
var $file_id; |
| 14 |
|
| 15 |
private static $instance; |
| 16 |
|
| 17 |
public static function instance() { |
| 18 |
if ( is_null( self::$instance ) ) { |
| 19 |
self::$instance = new self(); |
| 20 |
} |
| 21 |
|
| 22 |
self::$instance->hook(); |
| 23 |
|
| 24 |
return self::$instance; |
| 25 |
} |
| 26 |
|
| 27 |
public function hook() { |
| 28 |
add_filter( 'happyforms_import_page_method', array( $this, 'set_admin_page_method' ) ); |
| 29 |
add_filter( 'happyforms_import_page_url', array( $this, 'set_admin_page_url' ) ); |
| 30 |
} |
| 31 |
|
| 32 |
public function set_admin_page_method() { |
| 33 |
return array( $this, 'dispatch' ); |
| 34 |
} |
| 35 |
|
| 36 |
public function set_admin_page_url() { |
| 37 |
return 'happyforms-import'; |
| 38 |
} |
| 39 |
|
| 40 |
public function dispatch() { |
| 41 |
$this->header(); |
| 42 |
|
| 43 |
if ( ! isset( $_FILES['import']['name'] ) ) { |
| 44 |
$this->upload_form(); |
| 45 |
} else { |
| 46 |
if ( $this->handle_upload() ) { |
| 47 |
check_admin_referer( 'import-upload' ); |
| 48 |
require_once( happyforms_get_include_folder() . '/classes/class-importer-xml.php' ); |
| 49 |
|
| 50 |
$file = get_attached_file( $this->id ); |
| 51 |
$importer = new HappyForms_Importer_XML(); |
| 52 |
$result = $importer->import( $file ); |
| 53 |
|
| 54 |
$messages = []; |
| 55 |
|
| 56 |
if ( is_wp_error( $result ) ) { |
| 57 |
$messages = $result->get_error_messages(); |
| 58 |
} else { |
| 59 |
$messages = $importer->get_success_messages(); |
| 60 |
} |
| 61 |
|
| 62 |
if ( ! empty( $messages ) ) { |
| 63 |
$this->print_messages( $messages ); |
| 64 |
} |
| 65 |
|
| 66 |
wp_import_cleanup( $this->id ); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
$this->footer(); |
| 71 |
} |
| 72 |
|
| 73 |
function header() { |
| 74 |
echo '<div class="wrap">'; |
| 75 |
echo '<h2>' . __( 'Import Forms', 'happyforms' ) . '</h2>'; |
| 76 |
} |
| 77 |
|
| 78 |
function footer() { |
| 79 |
echo '</div>'; |
| 80 |
} |
| 81 |
|
| 82 |
function upload_form() { |
| 83 |
echo '<div class="narrow">'; |
| 84 |
echo '<p>' . __( 'Choose a forms export (.xml) file to upload, then click Upload file and import.', 'happyforms' ) . '</p>'; |
| 85 |
wp_import_upload_form( 'admin.php?page=happyforms-import' ); |
| 86 |
echo '</div>'; |
| 87 |
} |
| 88 |
|
| 89 |
public function handle_upload() { |
| 90 |
$file = wp_import_handle_upload(); |
| 91 |
|
| 92 |
if ( isset( $file['error'] ) ) { |
| 93 |
echo '<p><strong>' . __( 'Sorry, there has been an error.', 'happyforms' ) . '</strong><br />'; |
| 94 |
echo esc_html( $file['error'] ) . '</p>'; |
| 95 |
|
| 96 |
return false; |
| 97 |
} else if ( ! file_exists( $file['file'] ) ) { |
| 98 |
echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />'; |
| 99 |
printf( __( 'The export file could not be found at <code>%s</code>. It is likely that this was caused by a permissions problem.', 'happyforms' ), esc_html( $file['file'] ) ); |
| 100 |
echo '</p>'; |
| 101 |
|
| 102 |
return false; |
| 103 |
} |
| 104 |
|
| 105 |
$this->id = (int) $file['id']; |
| 106 |
|
| 107 |
return true; |
| 108 |
} |
| 109 |
|
| 110 |
private function print_messages( $messages ) { |
| 111 |
$output = array_map( function( $message ) { |
| 112 |
return "<p>$message</p>"; |
| 113 |
}, $messages ); |
| 114 |
|
| 115 |
$output = implode( '', $output ); |
| 116 |
|
| 117 |
echo $output; |
| 118 |
} |
| 119 |
|
| 120 |
} |
| 121 |
|
| 122 |
if ( ! function_exists( 'happyforms_get_import_controller' ) ): |
| 123 |
|
| 124 |
function happyforms_get_import_controller() { |
| 125 |
return HappyForms_Import_Controller::instance(); |
| 126 |
} |
| 127 |
|
| 128 |
endif; |
| 129 |
|
| 130 |
happyforms_get_import_controller(); |
| 131 |
|