-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent-control-migration-tester.php
More file actions
621 lines (539 loc) · 14.7 KB
/
Copy pathcontent-control-migration-tester.php
File metadata and controls
621 lines (539 loc) · 14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
<?php
/**
* Plugin headers
* Plugin Name: Content Control Migration Helper Plugin
* Plugin URI:
* Description: Testing & troubleshooting for the migration from Content Control v1 to v2
* Version: 1.1.0
* Author: Code Atlantic
* Author URI: https://code-atlantic.com/
* Text Domain: content-control-migration-tester
* Domain Path: /languages
* License: GPLv2 or later
*
* @package ContentControlMigrationTester
*/
namespace ContentControlMigrationTester;
defined( 'ABSPATH' ) || exit;
define( 'CONTENT_CONTROL_LOGGING', true );
// Add admin bar menu item and sub items to reset v1 data, clear v2 data etc.
\add_action( 'admin_bar_menu', __NAMESPACE__ . '\add_admin_bar_menu_item', 999 );
/**
* Add admin bar menu item and sub items to reset v1 data, clear v2 data etc.
*
* @param \WP_Admin_Bar $wp_admin_bar Admin bar instance.
*/
function add_admin_bar_menu_item( $wp_admin_bar ) {
$nonce = \wp_create_nonce( 'content-control-migration-tester' );
$wp_admin_bar->add_menu(
[
'id' => 'content-control-migration-tester',
'title' => 'Content Control Tester',
]
);
$wp_admin_bar->add_menu(
[
'id' => 'content-control-migration-tester-plugins-v1',
'title' => 'Activate v1',
'parent' => 'content-control-migration-tester',
'href' => \add_query_arg(
[
'cc_action' => 'activate_v1',
'cc_nonce' => $nonce,
]
),
]
);
$wp_admin_bar->add_menu(
[
'id' => 'content-control-migration-tester-plugins-v2',
'title' => 'Activate v2',
'parent' => 'content-control-migration-tester',
'href' => \add_query_arg(
[
'cc_action' => 'activate_v2',
'cc_nonce' => $nonce,
]
),
]
);
$wp_admin_bar->add_menu(
[
'id' => 'content-control-migration-tester-import_v1_data',
'title' => 'Import v1 Data',
'parent' => 'content-control-migration-tester',
'href' => \add_query_arg(
[
'page' => 'content_control_migrator_json_uploader',
]
),
]
);
$wp_admin_bar->add_menu(
[
'id' => 'content-control-migration-tester-export_v1_data',
'title' => 'Export v1 Data',
'parent' => 'content-control-migration-tester',
'href' => \add_query_arg(
[
'cc_action' => 'export_v1_data',
'cc_nonce' => $nonce,
]
),
]
);
$wp_admin_bar->add_menu(
[
'id' => 'content-control-migration-tester-data',
'title' => 'Load/Save/Reset Data',
'parent' => 'content-control-migration-tester',
]
);
$wp_admin_bar->add_menu(
[
'id' => 'content-control-migration-tester-clean_install',
'title' => 'Clean Install',
'parent' => 'content-control-migration-tester-data',
'href' => \add_query_arg(
[
'cc_action' => 'clean_install',
'cc_nonce' => $nonce,
]
),
]
);
$wp_admin_bar->add_menu(
[
'id' => 'content-control-migration-tester-clear_completed_upgrades',
'title' => 'Clear Completed Upgrades',
'parent' => 'content-control-migration-tester-data',
'href' => \add_query_arg(
[
'cc_action' => 'clear_completed_upgrades',
'cc_nonce' => $nonce,
]
),
]
);
$wp_admin_bar->add_menu(
[
'id' => 'content-control-migration-tester-v1-separator',
'title' => '---- V1 Data ----',
'parent' => 'content-control-migration-tester-data',
'href' => '#',
]
);
$wp_admin_bar->add_menu(
[
'id' => 'content-control-migration-tester-load_v1_data',
'title' => 'Load v1 Data',
'parent' => 'content-control-migration-tester-data',
'href' => \add_query_arg(
[
'cc_action' => 'load_v1_data',
'cc_nonce' => $nonce,
]
),
]
);
$wp_admin_bar->add_menu(
[
'id' => 'content-control-migration-tester-save_v1_data',
'title' => 'Save v1 Data',
'parent' => 'content-control-migration-tester-data',
'href' => \add_query_arg(
[
'cc_action' => 'save_v1_data',
'cc_nonce' => $nonce,
]
),
]
);
$wp_admin_bar->add_menu(
[
'id' => 'content-control-migration-tester-delete_v1_data',
'title' => 'Delete v1 Data',
'parent' => 'content-control-migration-tester-data',
'href' => \add_query_arg(
[
'cc_action' => 'delete_v1_data',
'cc_nonce' => $nonce,
]
),
]
);
// Add Separator.
$wp_admin_bar->add_menu(
[
'id' => 'content-control-migration-tester-v2',
'title' => '---- V2 Data ----',
'parent' => 'content-control-migration-tester-data',
'href' => '#',
]
);
$wp_admin_bar->add_menu(
[
'id' => 'content-control-migration-tester-delete_v2_data',
'title' => 'Delete v2 Data',
'parent' => 'content-control-migration-tester-data',
'href' => \add_query_arg(
[
'cc_action' => 'delete_v2_data',
'cc_nonce' => $nonce,
]
),
]
);
}
// Listen for the admin bar menu item clicks.
\add_action( 'plugins_loaded', __NAMESPACE__ . '\listen_for_admin_bar_menu_item_clicks', -100 );
/**
* Listen for admin bar menu clicks.
*
* @return void
*/
function listen_for_admin_bar_menu_item_clicks() {
if ( ! isset( $_GET['cc_action'], $_GET['cc_nonce'] ) ) {
return;
}
if ( ! \wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['cc_nonce'] ) ), 'content-control-migration-tester' ) ) {
return;
}
if ( ! function_exists( 'activate_plugin' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
switch ( $_GET['cc_action'] ) {
case 'activate_v1':
deactivate_plugins( 'content-control/content-control.php', true );
activate_plugin( 'content-control-old/content-control-old.php', admin_url( 'options-general.php?page=jp-cc-settings' ), false, true );
return;
case 'activate_v2':
deactivate_plugins( 'content-control-old/content-control-old.php', true );
activate_plugin( 'content-control/content-control.php', admin_url( 'options-general.php?page=content-control-settings' ), false, true );
return;
case 'export_v1_data':
export_v1_data();
break;
case 'save_v1_data':
save_v1_data();
break;
case 'delete_v1_data':
delete_v1_data();
set_data_versioning( 2 );
break;
case 'load_v1_data':
clean_install();
load_v1_data();
break;
case 'delete_v2_data':
delete_v2_data();
break;
case 'clean_install':
clean_install();
break;
case 'clear_completed_upgrades':
clear_completed_upgrades();
break;
}
\wp_safe_redirect( \remove_query_arg( [ 'cc_action', 'cc_nonce' ] ) );
}
/**
* Delete all plugin data.
*
* @return void
*/
function clean_install() {
delete_v1_data();
delete_v2_data();
}
/**
* Delete all v1 data.
*
* @return void
*/
function delete_v1_data() {
$option_keys = [
'jp_cc_settings',
'jp_cc_reviews_installed_on',
];
foreach ( $option_keys as $option_key ) {
\delete_option( $option_key );
}
$user_meta_keys = [
'_jp_cc_reviews_dismissed_triggers',
'_jp_cc_reviews_last_dismissed',
'_jp_cc_reviews_already_did',
];
foreach ( $user_meta_keys as $user_meta_key ) {
\delete_user_meta( 1, $user_meta_key );
}
$transient_keys = [
'content_control_installed',
];
foreach ( $transient_keys as $transient ) {
\delete_transient( $transient );
}
}
/**
* Get v1 data.
*
* @return array<string,array<string,string|bool>> Array of data.
*/
function get_v1_data() {
$data = [];
$data['settings'] = \get_option( 'jp_cc_settings', [] );
$data['user_meta'] = [
'_jp_cc_reviews_dismissed_triggers' => \get_user_meta( 1, '_jp_cc_reviews_dismissed_triggers', true ),
'_jp_cc_reviews_last_dismissed' => \get_user_meta( 1, '_jp_cc_reviews_last_dismissed', true ),
'_jp_cc_reviews_already_did' => \get_user_meta( 1, '_jp_cc_reviews_already_did', true ),
];
$data['plugin_meta'] = [
'jp_cc_reviews_installed_on' => \get_option( 'jp_cc_reviews_installed_on' ),
];
return $data;
}
/**
* Save v1 data to a json file.
*
* @return void
*/
function save_v1_data() {
$data = get_v1_data();
// Save to file in the same folder as v1_data.json.
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents
\file_put_contents( __DIR__ . '/v1_data.json', \wp_json_encode( $data ) );
}
/**
* Export v1 data to a json file.
*
* @return void
*/
function export_v1_data() {
$data = get_v1_data();
header( 'Content-Type: application/json' );
header( 'Content-Disposition: attachment; filename="v1_data.json"' );
header( 'Content-Length: ' . strlen( \wp_json_encode( $data ) ) );
echo \wp_json_encode( $data );
exit;
}
/**
* Load v1 data from a json file.
*
* @param array|null $data Data to load. If null, load from file.
*
* @return void
*/
function load_v1_data( $data = null ) {
if ( null === $data ) {
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
$data = \json_decode( \file_get_contents( __DIR__ . '/v1_data.json' ), true );
}
\update_option( 'jp_cc_settings', $data['settings'] );
foreach ( $data['user_meta'] as $meta_key => $meta_value ) {
if ( ! empty( $meta_value ) ) {
\update_user_meta( 1, $meta_key, $meta_value );
}
}
foreach ( $data['plugin_meta'] as $meta_key => $meta_value ) {
\update_option( $meta_key, $meta_value );
}
}
/**
* Delete all v2 data.
*
* @return void
*/
function delete_v2_data() {
// Delete all cc_restriction post type items.
$restriction_post_ids = \get_posts(
[
'post_type' => 'cc_restriction',
'posts_per_page' => -1,
'fields' => 'ids',
'post_status' => 'any',
]
);
foreach ( $restriction_post_ids as $post_id ) {
\wp_delete_post( $post_id, true );
}
$transient_keys = [
'content_control_installed',
];
foreach ( $transient_keys as $transient ) {
\delete_transient( $transient );
}
// Delete the log file if it exists.
if ( function_exists( '\ContentControl\plugin' ) ) {
\ContentControl\plugin( 'logging' )->delete_logs();
}
$option_keys = [
'content_control_settings',
'content_control_version',
'content_control_activated',
'content_control_data_versioning',
'content_control_debug_log_token', // delete log first.
'content_control_installed_on',
'content_control_known_blockTypes',
'content_control_pro_version',
'content_control_completed_upgrades',
'content_control_license',
'content_control_pro_activation_date',
'content_control_connect_token',
];
foreach ( $option_keys as $option ) {
\delete_option( $option );
}
$site_option = [
'content_control_activated',
];
foreach ( $site_option as $option ) {
\delete_site_option( $option );
}
}
/**
* Set data versioning to v1 or v2.
*
* @param int $v 1 or 2.
*/
function set_data_versioning( $v = 1 ) {
\update_option(
'content_control_data_versioning', [
'backup' => $v,
'settings' => $v,
'restrictions' => $v,
'user_meta' => $v,
'plugin_meta' => $v,
]
);
}
/**
* Delete v2+ completed upgrades data.
*/
function clear_completed_upgrades() {
set_data_versioning( 1 );
\delete_option( 'content_control_completed_upgrades' );
}
add_action( 'admin_footer', function () {
?>
<script type="text/javascript">
jQuery(function () {
$ = jQuery;
const idPrefix = 'wp-admin-bar-content-control-migration-tester-';
const confirmLinks = [
'clean_install',
'load_v1_data',
'save_v1_data',
'delete_v1_data',
'delete_v2_data',
'clear_completed_upgrades',
];
const selector = `#${idPrefix}` + confirmLinks.join(` a, #${idPrefix}`) + ' a';
const $links = $( selector );
console.log( confirmLinks,selector, $links );
$links.on('click', function (event) {
const $this = $(this);
const id = $this.parent().attr('id')
.replace(idPrefix, '')
// Replace all _ with spaces
.replace(/_/g, " ")
// Uppercase first letter of each word
.replace(/\w\S*/g, function (txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
});
event.preventDefault();
if (confirm( 'Are you sure you want to ' + id )) {
window.location.href = $this.attr('href');
}
})
})
</script>
<?php
} );
/**
* Add a hidden page to upload a JSON file.
*/
function json_uploader_admin_menu() {
// This adds a hidden page, as there's no parent slug.
add_submenu_page(
null, // No parent slug = hidden page.
'JSON Uploader', // Page Title.
'JSON Uploader', // Menu Title - won't be used as it's hidden.
'manage_options', // Capability.
'content_control_migrator_json_uploader', // Menu Slug.
'\ContentControlMigrationTester\json_uploader_admin_page' // Callback function.
);
}
add_action( 'admin_menu', '\ContentControlMigrationTester\json_uploader_admin_menu' );
/**
* Process the uploaded file.
*/
function json_uploader_admin_page() {
if ( isset( $_SERVER['REQUEST_METHOD'] ) && 'POST' === $_SERVER['REQUEST_METHOD'] ) {
process_uploaded_file();
} else {
display_upload_form();
}
}
/**
* Display the upload form.
*/
function display_upload_form() {
$nonce = wp_create_nonce( 'json_uploader_nonce' );
?>
<form action="" method="post" enctype="multipart/form-data">
Select JSON File to Upload:
<input type="file" name="jsonFile" id="jsonFile">
<input type="hidden" name="json_uploader_nonce" value="<?php echo esc_attr( $nonce ); ?>">
<input type="submit" value="Upload JSON" name="submit">
</form>
<?php
}
/**
* Process the uploaded file.
*/
function process_uploaded_file() {
// Check for nonce.
if ( ! isset( $_POST['json_uploader_nonce'] ) || ! wp_verify_nonce( wp_unslash( sanitize_key( $_POST['json_uploader_nonce'] ) ), 'json_uploader_nonce' ) ) {
die( 'Security check failed!' );
}
if ( isset( $_FILES['jsonFile'] ) ) {
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$file = $_FILES['jsonFile'];
// Check for upload errors.
if ( 0 !== $file['error'] ) {
echo 'Error uploading file!';
return;
}
// Ensure it's a JSON file.
$file_type = pathinfo( $file['name'], PATHINFO_EXTENSION );
if ( 'json' !== $file_type ) {
echo 'Only JSON files are allowed!';
return;
}
// Read the content.
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
$json_data = file_get_contents( $file['tmp_name'] );
$json = json_decode( $json_data, true );
if ( json_last_error() !== JSON_ERROR_NONE ) {
echo 'Invalid JSON format!';
return;
}
// Import the data.
load_v1_data( $json );
// Print success and code to reload in 5 seconds.
// Conditionaly change url based on if v1 or v2 is active.
$url = function_exists( '\ContentControl\plugin' ) ?
admin_url( 'options-general.php?page=content-control-settings' ) :
admin_url( 'options-general.php?page=jp-cc-settings' );
?>
<div id="message" class="updated notice is-dismissible">
<p>JSON file uploaded successfully. Reloading in 5 seconds...</p>
</div>
<script>
setTimeout( function () {
window.location.href = "<?php echo esc_url_raw( $url ); ?>";
}, 3000 );
</script>
<?php
}
}