PluginProbe
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More / trunk
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More vtrunk
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.10 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 2.0.0 2.0.1 2.0.10 2.0.11 2.0.12 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 47 releases
content-control / classes / Upgrades / UserMeta_2.php

UserMeta_2.php in Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More trunk, at classes/Upgrades/UserMeta_2.php

102 lines 2.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Content Control Migrations
4 *
5 * @package ContentControl\Plugin
6 */
7
8 namespace ContentControl\Upgrades;
9
10 defined( 'ABSPATH' ) || exit;
11
12 use function ContentControl\plugin;
13
14 /**
15 * User meta v2 migration.
16 */
17 class UserMeta_2 extends \ContentControl\Base\Upgrade {
18
19 const TYPE = 'user_meta';
20 const VERSION = 2;
21
22 /**
23 * Get the label for the upgrade.
24 *
25 * @return string
26 */
27 public function label() {
28 return __( 'Migrate user meta', 'content-control' );
29 }
30
31 /**
32 * Get the dependencies for this upgrade.
33 *
34 * @return string[]
35 */
36 public function get_dependencies() {
37 return [
38 'backup-2',
39 ];
40 }
41
42 /**
43 * Get the remaps for this upgrade.
44 *
45 * @var array<string,string>
46 */
47 private $remaps = [
48 '_jp_cc_reviews_already_did' => 'content_control_reviews_already_did',
49 '_jp_cc_reviews_dismissed_triggers' => 'content_control_reviews_dismissed_triggers',
50 '_jp_cc_reviews_last_dismissed' => 'content_control_reviews_last_dismissed',
51 ];
52
53 /**
54 * Check if the upgrade is required.
55 *
56 * @return bool
57 */
58 public function is_required() {
59 $remaps = $this->remaps;
60
61 foreach ( $remaps as $key => $new_key ) {
62 $value = \get_user_meta( get_current_user_id(), $key, true );
63
64 if ( [] !== $value && '' !== $value && ! is_null( $value ) ) {
65 return true;
66 }
67 }
68
69 return false;
70 }
71
72 /**
73 * Run the migration.
74 *
75 * @return void
76 */
77 public function run() {
78 global $wpdb;
79
80 // Gets a stream or mock stream for sending events.
81 $stream = $this->stream();
82
83 $stream->start_task( __( 'Migrating user meta', 'content-control' ) );
84
85 $remapped_keys = $this->remaps;
86
87 // Update all keys via $wpdb.
88 foreach ( $remapped_keys as $old_key => $new_key ) {
89 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.DirectQuery
90 $wpdb->query(
91 $wpdb->prepare(
92 "UPDATE {$wpdb->usermeta} SET meta_key = %s WHERE meta_key = %s",
93 $new_key,
94 $old_key
95 )
96 );
97 }
98
99 $stream->complete_task( __( 'User meta migrated', 'content-control' ) );
100 }
101 }
102