| 1 |
<?php |
| 2 |
class HappyForms_Export_Controller { |
| 3 |
|
| 4 |
private static $instance; |
| 5 |
|
| 6 |
public $export_import_action = 'happyforms_export_import'; |
| 7 |
|
| 8 |
public static function instance() { |
| 9 |
if ( is_null( self::$instance ) ) { |
| 10 |
self::$instance = new self(); |
| 11 |
} |
| 12 |
|
| 13 |
self::$instance->hook(); |
| 14 |
|
| 15 |
return self::$instance; |
| 16 |
} |
| 17 |
|
| 18 |
public function hook() { |
| 19 |
add_filter( 'wp_check_filetype_and_ext', array( $this, 'add_xml_mime_type' ), 10, 3 ); |
| 20 |
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); |
| 21 |
add_filter( 'happyforms_export_page_method', array( $this, 'set_admin_page_method' ) ); |
| 22 |
add_filter( 'happyforms_export_page_url', array( $this, 'set_admin_page_url' ) ); |
| 23 |
add_action( 'admin_action_happyforms_export_import', array( $this, 'handle_request' ) ); |
| 24 |
} |
| 25 |
|
| 26 |
public function add_xml_mime_type( $info, $file, $filename ) { |
| 27 |
if ( isset( $_REQUEST['is_happyforms_export'] ) ) { |
| 28 |
$extension = pathinfo( $filename, PATHINFO_EXTENSION ); |
| 29 |
|
| 30 |
if ( 'xml' !== $extension ) { |
| 31 |
return $info; |
| 32 |
} |
| 33 |
|
| 34 |
if ( extension_loaded( 'fileinfo' ) ) { |
| 35 |
$finfo = finfo_open( FILEINFO_MIME_TYPE ); |
| 36 |
$mime = finfo_file( $finfo, $file ); |
| 37 |
finfo_close( $finfo ); |
| 38 |
|
| 39 |
$xml_mimes = array( |
| 40 |
'text/xml', 'application/xml' |
| 41 |
); |
| 42 |
|
| 43 |
if ( ! in_array( $mime, $xml_mimes ) ) { |
| 44 |
return $info; |
| 45 |
} |
| 46 |
|
| 47 |
$info = array( |
| 48 |
'ext' => $extension, |
| 49 |
'type' => $mime, |
| 50 |
); |
| 51 |
} else { |
| 52 |
$info = array( |
| 53 |
'ext' => $extension, |
| 54 |
'type' => 'text/xml', |
| 55 |
); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
return $info; |
| 60 |
} |
| 61 |
|
| 62 |
public function set_admin_page_method( $method ) { |
| 63 |
$method = array( $this, 'exports_page' ); |
| 64 |
|
| 65 |
return $method; |
| 66 |
} |
| 67 |
|
| 68 |
public function set_admin_page_url( $url ) { |
| 69 |
$url = 'happyforms-export'; |
| 70 |
|
| 71 |
return $url; |
| 72 |
} |
| 73 |
|
| 74 |
public function exports_page() { |
| 75 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 76 |
wp_die( __( 'Sorry, you are not allowed to access this page.', 'happyforms' ) ); |
| 77 |
} |
| 78 |
|
| 79 |
add_filter( 'admin_footer_text', 'happyforms_admin_footer' ); |
| 80 |
|
| 81 |
require_once( happyforms_get_include_folder() . '/templates/admin-export.php' ); |
| 82 |
} |
| 83 |
|
| 84 |
public function admin_enqueue_scripts() { |
| 85 |
if ( happyforms_is_admin_screen( 'happyforms-export' ) ) { |
| 86 |
wp_register_script( |
| 87 |
'happyforms-export', |
| 88 |
happyforms_get_plugin_url() . 'inc/assets/js/admin/export.js', |
| 89 |
array(), happyforms_get_version(), true |
| 90 |
); |
| 91 |
|
| 92 |
wp_enqueue_script( 'happyforms-export' ); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
public function handle_request() { |
| 97 |
if ( ! isset( $_REQUEST['happyforms_export_nonce'] ) ) { |
| 98 |
return; |
| 99 |
} |
| 100 |
|
| 101 |
if ( ! wp_verify_nonce( $_REQUEST['happyforms_export_nonce'], $this->export_import_action ) ) { |
| 102 |
return; |
| 103 |
} |
| 104 |
|
| 105 |
$action_type = sanitize_text_field( $_REQUEST['action_type'] ); |
| 106 |
|
| 107 |
$response = ''; |
| 108 |
|
| 109 |
switch ( $action_type ) { |
| 110 |
case 'export_form': |
| 111 |
$form_id = ( isset( $_REQUEST['form_id'] ) ) ? intval( $_REQUEST['form_id'] ) : 0; |
| 112 |
|
| 113 |
$response = $this->export_form( $form_id ); |
| 114 |
break; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
public function get_file_name( $form_id, $extension ) { |
| 119 |
$form_title = get_the_title( $form_id ); |
| 120 |
$form_title = sanitize_title( $form_title ); |
| 121 |
$form_title = "{$form_title}.{$extension}"; |
| 122 |
|
| 123 |
return $form_title; |
| 124 |
} |
| 125 |
|
| 126 |
public function export_form( $form_id ) { |
| 127 |
require_once( happyforms_get_include_folder() . '/classes/class-exporter-xml.php' ); |
| 128 |
|
| 129 |
$filename = $this->get_file_name( $form_id, 'xml' ); |
| 130 |
$exporter = new HappyForms_Exporter_XML( $form_id, $filename ); |
| 131 |
$exporter->export(); |
| 132 |
} |
| 133 |
|
| 134 |
} |
| 135 |
|
| 136 |
if ( ! function_exists( 'happyforms_get_export_controller' ) ): |
| 137 |
|
| 138 |
function happyforms_get_export_controller() { |
| 139 |
return HappyForms_Export_Controller::instance(); |
| 140 |
} |
| 141 |
|
| 142 |
endif; |
| 143 |
|