V3005.php
125 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AC\Plugin\Update; |
| 4 | |
| 5 | use AC\Plugin\Update; |
| 6 | |
| 7 | class V3005 extends Update { |
| 8 | |
| 9 | public function apply_update() { |
| 10 | $this->migrate_user_specific_settings(); |
| 11 | $this->delete_deprecated_settings(); |
| 12 | $this->delete_deprecated_options(); |
| 13 | } |
| 14 | |
| 15 | protected function set_version() { |
| 16 | $this->version = '3.0.5'; |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * @param string $key |
| 21 | * |
| 22 | * @return bool |
| 23 | */ |
| 24 | private function validate_key( $key ) { |
| 25 | if ( empty( $key ) ) { |
| 26 | return false; |
| 27 | } |
| 28 | |
| 29 | if ( ! is_string( $key ) ) { |
| 30 | return false; |
| 31 | } |
| 32 | |
| 33 | if ( strlen( $key ) < 10 ) { |
| 34 | return false; |
| 35 | } |
| 36 | |
| 37 | return true; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * @param string $key |
| 42 | * |
| 43 | * @return array |
| 44 | */ |
| 45 | private function get_meta( $key ) { |
| 46 | global $wpdb; |
| 47 | |
| 48 | if ( ! $this->validate_key( $key ) ) { |
| 49 | return []; |
| 50 | } |
| 51 | |
| 52 | $sql = $wpdb->prepare( " |
| 53 | SELECT * |
| 54 | FROM {$wpdb->usermeta} |
| 55 | WHERE meta_key LIKE %s |
| 56 | ORDER BY user_id ASC |
| 57 | ", $key ); |
| 58 | |
| 59 | $results = $wpdb->get_results( $sql ); |
| 60 | |
| 61 | if ( ! $results ) { |
| 62 | return []; |
| 63 | } |
| 64 | |
| 65 | return $results; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Migrate USER specific preferences |
| 70 | */ |
| 71 | private function migrate_user_specific_settings() { |
| 72 | global $wpdb; |
| 73 | |
| 74 | $mapping = [ |
| 75 | 'cpac-hide-install-addons-notice' => 'ac_hide_notice_addons', |
| 76 | 'cpac-hide-review-notice' => 'ac_hide_notice_review', |
| 77 | ]; |
| 78 | |
| 79 | foreach ( $mapping as $current => $new ) { |
| 80 | $sql_meta_key = $wpdb->esc_like( $current ) . '%'; |
| 81 | |
| 82 | foreach ( $this->get_meta( $sql_meta_key ) as $row ) { |
| 83 | update_user_meta( $row->user_id, $new, $row->meta_value ); |
| 84 | } |
| 85 | |
| 86 | $this->delete( $current ); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Preference to be REMOVED |
| 92 | */ |
| 93 | private function delete_deprecated_settings() { |
| 94 | $this->delete( 'cpac_current_model' ); |
| 95 | $this->delete( 'cpac-install-timestamp' ); |
| 96 | } |
| 97 | |
| 98 | private function delete_deprecated_options() { |
| 99 | delete_option( 'cpac_version' ); |
| 100 | delete_option( 'cpac_version_prev' ); |
| 101 | delete_option( 'cpac-install-timestamp' ); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Remove meta data |
| 106 | * |
| 107 | * @param string $key |
| 108 | */ |
| 109 | private function delete( $key ) { |
| 110 | global $wpdb; |
| 111 | |
| 112 | if ( ! $this->validate_key( $key ) ) { |
| 113 | return; |
| 114 | } |
| 115 | |
| 116 | $sql = $wpdb->prepare( " |
| 117 | DELETE |
| 118 | FROM {$wpdb->usermeta} |
| 119 | WHERE meta_key LIKE %s |
| 120 | ", $wpdb->esc_like( $key ) . '%' ); |
| 121 | |
| 122 | $wpdb->query( $sql ); |
| 123 | } |
| 124 | |
| 125 | } |