V3201.php
110 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AC\Plugin\Update; |
| 4 | |
| 5 | use AC\Plugin\Update; |
| 6 | use AC\Preferences; |
| 7 | use DirectoryIterator; |
| 8 | use FilesystemIterator; |
| 9 | use RecursiveDirectoryIterator; |
| 10 | use RecursiveIteratorIterator; |
| 11 | |
| 12 | class V3201 extends Update { |
| 13 | |
| 14 | public function apply_update() { |
| 15 | $this->uppercase_class_files( AC()->get_dir() . '/classes' ); |
| 16 | $this->update_notice_preference_review(); |
| 17 | $this->update_notice_preference_addons(); |
| 18 | } |
| 19 | |
| 20 | protected function set_version() { |
| 21 | $this->version = '3.2.1'; |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Set all files to the proper case |
| 26 | * |
| 27 | * @param string Directory |
| 28 | */ |
| 29 | protected function uppercase_class_files( $directory ) { |
| 30 | $iterator = new RecursiveIteratorIterator( |
| 31 | new RecursiveDirectoryIterator( $directory, FilesystemIterator::SKIP_DOTS ) |
| 32 | ); |
| 33 | |
| 34 | /** @var DirectoryIterator $leaf */ |
| 35 | foreach ( $iterator as $leaf ) { |
| 36 | $file = $leaf->getFilename(); |
| 37 | |
| 38 | if ( $leaf->isFile() && 'php' === $leaf->getExtension() && $file == strtolower( $file ) ) { |
| 39 | @rename( $leaf->getPathname(), trailingslashit( $leaf->getPath() ) . ucfirst( $file ) ); |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Update user preferences for review |
| 46 | */ |
| 47 | private function update_notice_preference_review() { |
| 48 | $mapping = [ |
| 49 | 'ac_hide_notice_review' => 'dismiss-review', |
| 50 | 'ac-first-login-timestamp' => 'first-login-review', |
| 51 | ]; |
| 52 | |
| 53 | foreach ( $mapping as $old => $new ) { |
| 54 | foreach ( $this->get_users_by_meta_key( $old ) as $user_id ) { |
| 55 | |
| 56 | $value = get_user_meta( $user_id, $old, true ); |
| 57 | |
| 58 | $option = new Preferences\User( 'check-review', $user_id ); |
| 59 | $option->set( $new, $value, true ); |
| 60 | |
| 61 | delete_user_meta( $user_id, $old ); |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Update user preferences for addons |
| 68 | */ |
| 69 | private function update_notice_preference_addons() { |
| 70 | $mapping = [ |
| 71 | 'ac_hide_notice_addons' => 'dismiss-notice', |
| 72 | ]; |
| 73 | |
| 74 | foreach ( $mapping as $old => $new ) { |
| 75 | foreach ( $this->get_users_by_meta_key( $old ) as $user_id ) { |
| 76 | |
| 77 | $value = get_user_meta( $user_id, $old, true ); |
| 78 | |
| 79 | $option = new Preferences\User( 'check-addon-available', $user_id ); |
| 80 | $option->set( $new, $value, true ); |
| 81 | |
| 82 | delete_user_meta( $user_id, $old ); |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * @param string $key |
| 89 | * |
| 90 | * @return array ID's |
| 91 | */ |
| 92 | protected function get_users_by_meta_key( $key ) { |
| 93 | $user_ids = get_users( [ |
| 94 | 'fields' => 'ids', |
| 95 | 'meta_query' => [ |
| 96 | [ |
| 97 | 'key' => $key, |
| 98 | 'compare' => 'EXISTS', |
| 99 | ], |
| 100 | ], |
| 101 | ] ); |
| 102 | |
| 103 | if ( ! $user_ids ) { |
| 104 | return []; |
| 105 | } |
| 106 | |
| 107 | return $user_ids; |
| 108 | } |
| 109 | |
| 110 | } |