class-hustle-410-migration.php
5 months ago
class-hustle-430-migration.php
3 years ago
class-hustle-441-migration.php
3 years ago
class-hustle-441-migration.php
155 lines
| 1 | <?php |
| 2 | /** |
| 3 | * File for Hustle_441_Migration class. |
| 4 | * |
| 5 | * @package Hustle |
| 6 | * @since 4.4.1 |
| 7 | */ |
| 8 | |
| 9 | /** |
| 10 | * Class Hustle_441_Migration. |
| 11 | * |
| 12 | * This class handles the migration when going to 4.4.1. |
| 13 | * The only update here is making the "Triggers" property' |
| 14 | * from "behavior" an array instead of a string. |
| 15 | * |
| 16 | * @since 4.4.1 |
| 17 | */ |
| 18 | class Hustle_441_Migration { |
| 19 | |
| 20 | /** |
| 21 | * Flag name to mark the migration as "done". |
| 22 | * |
| 23 | * @since 4.4.1 |
| 24 | */ |
| 25 | const MIGRATION_FLAG = '441'; |
| 26 | |
| 27 | /** |
| 28 | * Run the migration if required. |
| 29 | * |
| 30 | * @since 4.4.1 |
| 31 | * |
| 32 | * @todo To be abstracted in the base migration class. |
| 33 | */ |
| 34 | public function maybe_migrate() { |
| 35 | if ( $this->is_do_migration() ) { |
| 36 | $this->do_migration(); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Checks whether this migration should be run. |
| 42 | * |
| 43 | * @since 4.4.1 |
| 44 | * |
| 45 | * @todo To be abstracted in the base migration class. |
| 46 | * |
| 47 | * @return boolean |
| 48 | */ |
| 49 | private function is_do_migration() { |
| 50 | // Bail out if the migration is being forced but it's not 4.4.1. |
| 51 | if ( |
| 52 | filter_input( INPUT_GET, 'run-migration', FILTER_VALIDATE_INT ) && |
| 53 | self::MIGRATION_FLAG !== filter_input( INPUT_GET, 'run-migration', FILTER_SANITIZE_SPECIAL_CHARS ) |
| 54 | ) { |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | return true; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Does the migration to 4.4.1. |
| 63 | * |
| 64 | * @since 4.4.1 |
| 65 | */ |
| 66 | public function do_migration() { |
| 67 | global $wpdb; |
| 68 | |
| 69 | $limit = apply_filters( 'hustle_441_migration_limit', 100 ); |
| 70 | |
| 71 | do { |
| 72 | $offset = get_option( 'hustle_441_migration_offset', 0 ); |
| 73 | $settings = $this->get_all_hustle_module_behavior_setting( $limit, $offset ); |
| 74 | |
| 75 | foreach ( $settings as $meta ) { |
| 76 | |
| 77 | $meta_id = $meta->meta_id; |
| 78 | $value = json_decode( $meta->meta_value, true ); |
| 79 | |
| 80 | if ( ! empty( $value['triggers'] ) ) { |
| 81 | |
| 82 | // Make the triggers an array to support having multiple. |
| 83 | if ( ! is_array( $value['triggers']['trigger'] ) ) { |
| 84 | |
| 85 | if ( 'adblock' === $value['triggers']['trigger'] && '0' === $value['triggers']['on_adblock'] ) { |
| 86 | // If "adblock" was selected and "on_adblock" was disabled, disable "adblock". |
| 87 | // The old "on_adblock" setting will be unused. |
| 88 | $value['triggers']['trigger'] = array(); |
| 89 | |
| 90 | } else { |
| 91 | // "Trigger" was a string before. We just need to make it an array now. |
| 92 | $value['triggers']['trigger'] = array( $value['triggers']['trigger'] ); |
| 93 | } |
| 94 | |
| 95 | // The on/off setting for delaying the trigger on exit intent will be unused. |
| 96 | if ( isset( $value['triggers']['on_exit_intent_delayed'] ) && '0' === $value['triggers']['on_exit_intent_delayed'] ) { |
| 97 | $value['triggers']['on_exit_intent_delayed_time'] = '0'; |
| 98 | } |
| 99 | |
| 100 | // The on/off setting for delaying the trigger on adblock will be unused. |
| 101 | if ( isset( $value['triggers']['enable_on_adblock_delay'] ) && '0' === $value['triggers']['enable_on_adblock_delay'] ) { |
| 102 | $value['triggers']['on_adblock_delay'] = '0'; |
| 103 | } |
| 104 | } |
| 105 | } else { |
| 106 | // The default value for 'triggers' was an empty string in old versions. |
| 107 | // Remove that troubling value and let the module grab the new defaults. |
| 108 | unset( $value['triggers'] ); |
| 109 | } |
| 110 | |
| 111 | // Save the transformed behavior. |
| 112 | $wpdb->update( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery |
| 113 | Hustle_Db::modules_meta_table(), |
| 114 | array( 'meta_value' => wp_json_encode( $value ) ), // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value |
| 115 | array( 'meta_id' => $meta_id ) |
| 116 | ); |
| 117 | |
| 118 | wp_cache_delete( $meta->module_id, 'hustle_module_meta' ); |
| 119 | } |
| 120 | |
| 121 | $count_settings = count( $settings ); |
| 122 | $offset += $limit; |
| 123 | |
| 124 | update_option( 'hustle_441_migration_offset', $offset ); |
| 125 | |
| 126 | } while ( $count_settings === $limit ); |
| 127 | |
| 128 | Hustle_Migration::migration_passed( self::MIGRATION_FLAG ); |
| 129 | delete_option( 'hustle_441_migration_offset' ); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Gets all the stored visibility metas. |
| 134 | * |
| 135 | * @since 4.1.0 |
| 136 | * |
| 137 | * @param int $limit Query limit. |
| 138 | * @param int $offset Query offset. |
| 139 | * @return array |
| 140 | */ |
| 141 | private function get_all_hustle_module_behavior_setting( $limit, $offset ) { |
| 142 | global $wpdb; |
| 143 | |
| 144 | $modules_table = Hustle_Db::modules_meta_table(); |
| 145 | |
| 146 | // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 147 | $results = $wpdb->get_results( |
| 148 | // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 149 | $wpdb->prepare( "SELECT meta_id, module_id, meta_value FROM {$modules_table} WHERE meta_key = 'settings' LIMIT %d OFFSET %d", intval( $limit ), intval( $offset ) ) |
| 150 | ); |
| 151 | |
| 152 | return $results; |
| 153 | } |
| 154 | } |
| 155 |