PluginProbe ʕ •ᴥ•ʔ
Advanced Access Manager – Access Governance for WordPress / 5.11
Advanced Access Manager – Access Governance for WordPress v5.11
6.8.4 6.8.5 6.9.0 6.9.1 6.9.10 6.9.11 6.9.12 6.9.13 6.9.14 6.9.15 6.9.16 6.9.17 6.9.18 6.9.19 6.9.2 6.9.20 6.9.21 6.9.22 6.9.23 6.9.24 6.9.25 6.9.26 6.9.27 6.9.28 6.9.29 6.9.3 6.9.30 6.9.31 6.9.32 6.9.33 6.9.34 6.9.35 6.9.36 6.9.37 6.9.38 6.9.39 6.9.4 6.9.41 6.9.42 6.9.43 6.9.44 6.9.45 6.9.46 6.9.47 6.9.48 6.9.49 6.9.5 6.9.51 6.9.6 6.9.7 6.9.8 6.9.9 7.0.0 7.0.0-alpha.6 7.0.0-alpha.7 7.0.0-beta.1 7.0.0-rc1 7.0.0-rc2 7.0.0-rc3 7.0.1 7.0.10 7.0.11 7.0.2 7.0.3 7.0.4 7.0.5 7.0.6 7.0.7 7.0.8 7.0.9 7.1.0 7.1.1 trunk 3.0 4.0 4.0.1 4.1 4.2 4.3 4.4 4.4.1 4.5 4.6 4.6.1 4.6.2 4.7 4.7.1 4.7.2 4.7.5 4.7.6 4.8 4.8.1 4.9 4.9.1 4.9.2 4.9.3 4.9.4 4.9.5 4.9.5.1 4.9.5.2 5.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1 5.1.1 5.10 5.11 5.2 5.2.1 5.2.5 5.2.6 5.2.7 5.3 5.3.1 5.3.2 5.3.3 5.3.4 5.3.5 5.4 5.4.1 5.4.2 5.4.3 5.4.3.1 5.4.3.2 5.5 5.5.1 5.5.2 5.6 5.6.1 5.6.1.1 5.7 5.7.1 5.7.2 5.7.3 5.8 5.8.1 5.8.2 5.8.3 5.9 5.9.1 5.9.1.1 5.9.2 5.9.2.1 5.9.3 5.9.4 5.9.5 5.9.6 5.9.6.1 5.9.6.2 5.9.6.3 5.9.7 5.9.7.1 5.9.7.2 5.9.7.3 5.9.8 5.9.8.1 5.9.9 5.9.9.1 6.0.0 6.0.1 6.0.2 6.0.3 6.0.4 6.0.5 6.1.0 6.1.1 6.2.0 6.2.1 6.2.2 6.3.0 6.3.1 6.3.2 6.3.3 6.4.0 6.4.1 6.4.2 6.4.3 6.5.0 6.5.1 6.5.2 6.5.3 6.5.4 6.6.0 6.6.1 6.6.2 6.6.3 6.6.4 6.7.0 6.7.1 6.7.2 6.7.3 6.7.4 6.7.5 6.7.6 6.7.7 6.7.8 6.7.9 6.8.0 6.8.1 6.8.2 6.8.3
advanced-access-manager / application / Core / Importer.php
advanced-access-manager / application / Core Last commit date
Api 6 years ago ConfigPress 6 years ago Jwt 6 years ago Object 6 years ago Policy 6 years ago Subject 6 years ago API.php 6 years ago Cache.php 6 years ago Compatibility.php 6 years ago Config.php 6 years ago ConfigPress.php 6 years ago Console.php 6 years ago Exporter.php 6 years ago Gateway.php 6 years ago Importer.php 6 years ago Login.php 6 years ago Media.php 6 years ago Object.php 6 years ago Request.php 6 years ago Server.php 6 years ago Subject.php 6 years ago
Importer.php
302 lines
1 <?php
2
3 /**
4 * ======================================================================
5 * LICENSE: This file is subject to the terms and conditions defined in *
6 * file 'license.txt', which is part of this source code package. *
7 * ======================================================================
8 */
9
10 /**
11 * AAM Importer
12 *
13 * @package AAM
14 * @author Vasyl Martyniuk <vasyl@vasyltech.com>
15 */
16 class AAM_Core_Importer {
17
18 /**
19 *
20 * @var type
21 */
22 protected $input = null;
23
24 /**
25 * Undocumented variable
26 *
27 * @var [type]
28 */
29 protected $blog = null;
30
31 /**
32 *
33 * @param type $input
34 */
35 public function __construct($input = null, $blog = null) {
36 if (!is_null($input)) {
37 $this->input = json_decode($input);
38 }
39
40 $this->setBlog(is_null($blog) ? get_current_blog_id() : $blog);
41 }
42
43 /**
44 *
45 * @param type $blog_id
46 * @return type
47 */
48 public function setBlog($blog_id) {
49 if ( is_numeric( $blog_id ) ) {
50 $blog_id = (int) $blog_id;
51 } else {
52 $blog = 'http://' . preg_replace( '#^https?://#', '', $blog_id );
53 if ( ( !$parsed = parse_url( $blog ) ) || empty( $parsed['host'] ) ) {
54 fwrite( STDERR, "Error: can not determine blog_id from $blog_id\n" );
55 exit();
56 }
57 if ( empty( $parsed['path'] ) ) {
58 $parsed['path'] = '/';
59 }
60 $blogs = get_sites( array( 'domain' => $parsed['host'], 'number' => 1, 'path' => $parsed['path'] ) );
61 if ( ! $blogs ) {
62 fwrite( STDERR, "Error: Could not find blog\n" );
63 exit();
64 }
65 $blog = array_shift( $blogs );
66 $blog_id = (int) $blog->blog_id;
67 }
68
69 if ( function_exists( 'is_multisite' ) ) {
70 if ( is_multisite() )
71 switch_to_blog( $blog_id );
72 }
73
74 return $blog_id;
75 }
76
77 /**
78 *
79 */
80 public function dispatch() {
81 $this->header();
82
83 switch(AAM_Core_Request::get('step', 0)) {
84 case 0:
85 $this->greet();
86 break;
87
88 case 1:
89 check_admin_referer('import-upload');
90
91 if ($this->handleUpload()) {
92 $this->renderConfirmationStep();
93 }
94 break;
95
96 case 2:
97 check_admin_referer( 'import-wordpress' );
98
99 $this->id = intval(AAM_Core_Request::post('import_id'));
100 $filepath = get_attached_file($this->id);
101 $this->import_start( $filepath );
102 $this->run();
103 $this->import_end();
104 break;
105 }
106
107 $this->footer();
108 }
109
110 // Display import page title
111 protected function header() {
112 echo '<div class="wrap">';
113 echo '<h2>' . __('Import AAM Settings', AAM_KEY) . '</h2>';
114 }
115
116 // Close div.wrap
117 protected function footer() {
118 echo '</div>';
119 }
120
121 /**
122 * Display introductory text and file upload form
123 */
124 protected function greet() {
125 echo '<div class="narrow">';
126 echo '<p>' . __('Howdy! Upload your AAM JSON file and we&#8217;ll import the access settings into this site.', AAM_KEY) . '</p>';
127 echo '<p>' . __('Choose a JSON (.json) file to upload, then click Upload file and import.', AAM_KEY) . '</p>';
128 wp_import_upload_form('admin.php?import=aam&amp;step=1');
129 echo '</div>';
130 }
131
132 /**
133 *
134 * @return boolean
135 */
136 protected function handleUpload() {
137 $result = true;
138 $file = wp_import_handle_upload();
139
140 if ( isset( $file['error'] ) ) {
141 echo '<p><strong>' . __( 'Sorry, there has been an error.', AAM_KEY ) . '</strong><br />';
142 echo esc_html( $file['error'] ) . '</p>';
143 $result = false;
144 } else if ( ! file_exists( $file['file'] ) ) {
145 echo '<p><strong>' . __( 'Sorry, there has been an error.', AAM_KEY ) . '</strong><br />';
146 printf( __( 'The export file could not be found at <code>%s</code>. It is likely that this was caused by a permissions problem.', AAM_KEY ), esc_html( $file['file'] ) );
147 echo '</p>';
148 $result = false;
149 } else {
150 $this->file = $file;
151 }
152
153 return $result;
154 }
155
156 /**
157 *
158 */
159 protected function renderConfirmationStep() {
160 ?>
161 <form action="<?php echo admin_url('admin.php?import=aam&amp;step=2' ); ?>" method="post">
162 <?php wp_nonce_field('import-wordpress' ); ?>
163 <input type="hidden" name="import_id" value="<?php echo $this->file['id']; ?>" />
164
165 <p>Please confirm the AAM access settings import. Note! All imported access settings will override existing.</p>
166
167 <p class="submit"><input type="submit" class="button" value="<?php esc_attr_e( 'Submit', AAM_KEY ); ?>" /></p>
168 </form>
169 <?php
170 }
171
172 /**
173 * Parses the WXR file and prepares us for the task of processing parsed data
174 *
175 * @param string $file Path to the WXR file for importing
176 */
177 protected function import_start( $file ) {
178 if ( ! is_file($file) ) {
179 echo '<p><strong>' . __( 'Sorry, there has been an error.', AAM_KEY ) . '</strong><br />';
180 echo __( 'The file does not exist, please try again.', AAM_KEY ) . '</p>';
181 $this->footer();
182 die();
183 }
184
185 $this->input = json_decode(file_get_contents($file));
186
187 if ( empty( $this->input ) ) {
188 echo '<p><strong>' . __( 'Sorry, there has been an error. File content is invalid', AAM_KEY ) . '</strong></p>';
189 $this->footer();
190 die();
191 }
192 }
193
194 /**
195 * Performs post-import cleanup of files and the cache
196 */
197 function import_end() {
198 wp_import_cleanup( $this->id );
199
200 wp_cache_flush();
201
202 echo '<p>' . __( 'All done.', 'wordpress-importer' ) . ' <a href="' . admin_url() . '">' . __( 'Have fun!', 'wordpress-importer' ) . '</a>' . '</p>';
203 }
204
205 /**
206 *
207 * @return type
208 */
209 public function run() {
210 $response = array('status' => 'success');
211
212 if (version_compare($this->input->version, AAM_Core_API::version()) === 0) {
213 foreach ($this->input->dataset as $table => $data) {
214 if ($table === '_options') {
215 $this->insertOptions($data);
216 } elseif ($table === '_postmeta') {
217 $this->insertPostmeta($data);
218 } elseif ($table === '_usermeta') {
219 $this->insertUsermeta($data);
220 } else {
221 do_action('aam-import-action', $table, $data);
222 }
223 }
224 } else {
225 $response = array(
226 'status' => 'failure',
227 'reason' => __('Version of exported settings do not match current AAM version', AAM_KEY)
228 );
229 }
230
231 return $response;
232 }
233
234 /**
235 * Undocumented function
236 *
237 * @param [type] $data
238 * @return void
239 */
240 protected function insertOptions($data) {
241 global $wpdb;
242
243 foreach ($data as $key => $value) {
244 AAM_Core_API::updateOption(
245 preg_replace('/^_/', $wpdb->get_blog_prefix(), $key),
246 $this->prepareValue($value)
247 );
248 }
249 }
250
251 /**
252 * Undocumented function
253 *
254 * @param [type] $data
255 * @return void
256 */
257 protected function insertUsermeta($data) {
258 global $wpdb;
259
260 foreach ($data as $id => $set) {
261 foreach ($set as $key => $value) {
262 update_user_meta(
263 $id,
264 preg_replace('/^_/', $wpdb->get_blog_prefix(), $key),
265 $this->prepareValue($value)
266 );
267 }
268 }
269 }
270
271 /**
272 * Undocumented function
273 *
274 * @param [type] $data
275 * @return void
276 */
277 protected function insertPostmeta($data) {
278 global $wpdb;
279
280 foreach ($data as $id => $set) {
281 foreach ($set as $key => $value) {
282 update_post_meta(
283 $id,
284 preg_replace('/^_/', $wpdb->prefix, $key),
285 $this->prepareValue($value)
286 );
287 }
288 }
289 }
290
291 /**
292 * Undocumented function
293 *
294 * @param [type] $value
295 * @return void
296 */
297 protected function prepareValue($value) {
298 return json_decode(base64_decode($value), true);
299 }
300
301 }
302