V3005.php
2.2 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
<?php
namespace AC\Plugin\Update;
use AC\Plugin\Update;
use AC\Plugin\Version;
class V3005 extends Update {
public function __construct() {
parent::__construct( new Version( '3.0.5' ) );
}
public function apply_update(): void
{
$this->migrate_user_specific_settings();
$this->delete_deprecated_settings();
$this->delete_deprecated_options();
}
/**
* @param string $key
*
* @return bool
*/
private function validate_key( $key ) {
if ( empty( $key ) ) {
return false;
}
if ( ! is_string( $key ) ) {
return false;
}
if ( strlen( $key ) < 10 ) {
return false;
}
return true;
}
/**
* @param string $key
*
* @return array
*/
private function get_meta( $key ) {
global $wpdb;
if ( ! $this->validate_key( $key ) ) {
return [];
}
$sql = $wpdb->prepare( "
SELECT *
FROM {$wpdb->usermeta}
WHERE meta_key LIKE %s
ORDER BY user_id ASC
", $key );
$results = $wpdb->get_results( $sql );
if ( ! $results ) {
return [];
}
return $results;
}
/**
* Migrate USER specific preferences
*/
private function migrate_user_specific_settings() {
global $wpdb;
$mapping = [
'cpac-hide-install-addons-notice' => 'ac_hide_notice_addons',
'cpac-hide-review-notice' => 'ac_hide_notice_review',
];
foreach ( $mapping as $current => $new ) {
$sql_meta_key = $wpdb->esc_like( $current ) . '%';
foreach ( $this->get_meta( $sql_meta_key ) as $row ) {
update_user_meta( $row->user_id, $new, $row->meta_value );
}
$this->delete( $current );
}
}
/**
* Preference to be REMOVED
*/
private function delete_deprecated_settings() {
$this->delete( 'cpac_current_model' );
$this->delete( 'cpac-install-timestamp' );
}
private function delete_deprecated_options() {
delete_option( 'cpac_version' );
delete_option( 'cpac_version_prev' );
delete_option( 'cpac-install-timestamp' );
}
/**
* Remove meta data
*
* @param string $key
*/
private function delete( $key ) {
global $wpdb;
if ( ! $this->validate_key( $key ) ) {
return;
}
$sql = $wpdb->prepare( "
DELETE
FROM {$wpdb->usermeta}
WHERE meta_key LIKE %s
", $wpdb->esc_like( $key ) . '%' );
$wpdb->query( $sql );
}
}