| 1 |
<?php |
| 2 |
|
| 3 |
namespace Code_Snippets; |
| 4 |
|
| 5 |
/** |
| 6 |
* This class handles the import admin menu. |
| 7 |
* |
| 8 |
* @since 2.4.0 |
| 9 |
* @package Code_Snippets |
| 10 |
*/ |
| 11 |
class Import_Menu extends Admin_Menu { |
| 12 |
|
| 13 |
/** |
| 14 |
* Class constructor |
| 15 |
*/ |
| 16 |
public function __construct() { |
| 17 |
parent::__construct( 'import', |
| 18 |
_x( 'Import', 'menu label', 'code-snippets' ), |
| 19 |
__( 'Import Snippets', 'code-snippets' ) |
| 20 |
); |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Register action and filter hooks |
| 25 |
*/ |
| 26 |
public function run() { |
| 27 |
parent::run(); |
| 28 |
add_action( 'admin_init', array( $this, 'register_importer' ) ); |
| 29 |
add_action( 'load-importer-code-snippets', array( $this, 'load' ) ); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Executed when the menu is loaded |
| 34 |
*/ |
| 35 |
public function load() { |
| 36 |
parent::load(); |
| 37 |
|
| 38 |
$contextual_help = new Contextual_Help( 'import' ); |
| 39 |
$contextual_help->load(); |
| 40 |
|
| 41 |
$this->process_import_files(); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Process the uploaded import files |
| 46 |
* |
| 47 |
* @uses import_snippets() to process the import file |
| 48 |
* @uses wp_redirect() to pass the import results to the page |
| 49 |
* @uses add_query_arg() to append the results to the current URI |
| 50 |
*/ |
| 51 |
private function process_import_files() { |
| 52 |
|
| 53 |
/* Ensure the import file exists */ |
| 54 |
if ( ! isset( |
| 55 |
$_FILES['code_snippets_import_files']['name'], |
| 56 |
$_FILES['code_snippets_import_files']['type'], |
| 57 |
$_FILES['code_snippets_import_files']['tmp_name'] |
| 58 |
) ) { |
| 59 |
return; |
| 60 |
} |
| 61 |
|
| 62 |
check_admin_referer( 'import_code_snippets_file' ); |
| 63 |
|
| 64 |
$count = 0; |
| 65 |
$network = is_network_admin(); |
| 66 |
$error = false; |
| 67 |
|
| 68 |
$upload_files = array_map( 'sanitize_text_field', wp_unslash( $_FILES['code_snippets_import_files']['tmp_name'] ) ); |
| 69 |
$upload_filenames = array_map( 'sanitize_text_field', wp_unslash( $_FILES['code_snippets_import_files']['name'] ) ); |
| 70 |
$upload_mime_types = array_map( 'sanitize_mime_type', wp_unslash( $_FILES['code_snippets_import_files']['type'] ) ); |
| 71 |
|
| 72 |
$dup_action = isset( $_POST['duplicate_action'] ) ? sanitize_key( $_POST['duplicate_action'] ) : 'ignore'; |
| 73 |
|
| 74 |
/* Loop through the uploaded files and import the snippets */ |
| 75 |
|
| 76 |
foreach ( $upload_files as $i => $import_file ) { |
| 77 |
$ext = pathinfo( $upload_filenames[ $i ] ); |
| 78 |
$ext = $ext['extension']; |
| 79 |
$mime_type = $upload_mime_types[ $i ]; |
| 80 |
|
| 81 |
$import = new Import( $import_file, $network, $dup_action ); |
| 82 |
|
| 83 |
if ( 'json' === $ext || 'application/json' === $mime_type ) { |
| 84 |
$result = $import->import_json(); |
| 85 |
} elseif ( 'xml' === $ext || 'text/xml' === $mime_type ) { |
| 86 |
$result = $import->import_xml(); |
| 87 |
} else { |
| 88 |
$result = false; |
| 89 |
} |
| 90 |
|
| 91 |
if ( false === $result ) { |
| 92 |
$error = true; |
| 93 |
} else { |
| 94 |
$count += count( $result ); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
/* Send the amount of imported snippets to the page */ |
| 99 |
$url = add_query_arg( $error ? array( 'error' => true ) : array( 'imported' => $count ) ); |
| 100 |
wp_redirect( esc_url_raw( $url ) ); |
| 101 |
exit; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Add the importer to the Tools > Import menu |
| 106 |
*/ |
| 107 |
public function register_importer() { |
| 108 |
|
| 109 |
/* Only register the importer if the current user can manage snippets */ |
| 110 |
if ( ! defined( 'WP_LOAD_IMPORTERS' ) || ! code_snippets()->current_user_can() ) { |
| 111 |
return; |
| 112 |
} |
| 113 |
|
| 114 |
/* Register the Code Snippets importer with WordPress */ |
| 115 |
register_importer( |
| 116 |
'code-snippets', |
| 117 |
__( 'Code Snippets', 'code-snippets' ), |
| 118 |
__( 'Import snippets from a code snippets export file', 'code-snippets' ), |
| 119 |
array( $this, 'render' ) |
| 120 |
); |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Print the status and error messages |
| 125 |
*/ |
| 126 |
protected function print_messages() { |
| 127 |
|
| 128 |
if ( ! empty( $_REQUEST['error'] ) ) { |
| 129 |
echo '<div id="message" class="error fade"><p>'; |
| 130 |
esc_html_e( 'An error occurred when processing the import files.', 'code-snippets' ); |
| 131 |
echo '</p></div>'; |
| 132 |
} |
| 133 |
|
| 134 |
if ( ! empty( $_REQUEST['imported'] ) && intval( $_REQUEST['imported'] ) >= 0 ) { |
| 135 |
echo '<div id="message" class="updated fade"><p>'; |
| 136 |
|
| 137 |
$imported = intval( $_REQUEST['imported'] ); |
| 138 |
|
| 139 |
if ( 0 === $imported ) { |
| 140 |
esc_html_e( 'No snippets were imported.', 'code-snippets' ); |
| 141 |
|
| 142 |
} else { |
| 143 |
/* translators: 1: amount of snippets imported, 2: link to Snippets menu */ |
| 144 |
$text = _n( |
| 145 |
'Successfully imported <strong>%1$d</strong> snippet. <a href="%2$s">Have fun!</a>', |
| 146 |
'Successfully imported <strong>%1$d</strong> snippets. <a href="%2$s">Have fun!</a>', |
| 147 |
$imported, |
| 148 |
'code-snippets' |
| 149 |
); |
| 150 |
|
| 151 |
printf( wp_kses_post( $text ), esc_html( $imported ), esc_url( code_snippets()->get_menu_url( 'manage' ) ) ); |
| 152 |
} |
| 153 |
|
| 154 |
echo '</p></div>'; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Empty implementation for enqueue_assets. |
| 160 |
* |
| 161 |
* @return void |
| 162 |
*/ |
| 163 |
public function enqueue_assets() { |
| 164 |
// none required. |
| 165 |
} |
| 166 |
} |
| 167 |
|