PluginProbe
Code Snippets / 3.5.0
Code Snippets v3.5.0
3.10.2 3.10.1 3.10.0 3.10.0-beta.2 3.10.0-beta.1 4.0.0-beta.1 3.9.6 trunk 2.10.0 2.10.1 2.12.0 2.12.1 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.14.1 2.14.2 2.14.3 2.14.4 2.14.5 2.14.6 3.0.0 3.0.1 All 64 releases
code-snippets / php / admin-menus / class-import-menu.php

class-import-menu.php in Code Snippets 3.5.0, at php/admin-menus/class-import-menu.php

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