PluginProbe
Code Snippets / 2.14.0
Code Snippets v2.14.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 2.14.0, at php/admin-menus/class-import-menu.php

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