PerformanceUpgrade.php
5 years ago
TransientsUpgrade.php
5 years ago
Upgrades.php
5 years ago
VisitsUpgrade.php
5 years ago
TransientsUpgrade.php
76 lines
| 1 | <?php |
| 2 | |
| 3 | namespace PrestoPlayer\Database\Upgrades; |
| 4 | |
| 5 | use PrestoPlayer\Plugin; |
| 6 | |
| 7 | /** |
| 8 | * Clears pro transients every upgrade to ensure update notifications are immediate |
| 9 | */ |
| 10 | class TransientsUpgrade |
| 11 | { |
| 12 | protected $name = 'presto_player_pro_update_transient'; |
| 13 | |
| 14 | public function run() |
| 15 | { |
| 16 | $this->runUpdate(); |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * Checks to see if update is run |
| 21 | * |
| 22 | * @return void |
| 23 | */ |
| 24 | public function runUpdate() |
| 25 | { |
| 26 | $current_version = get_option($this->name, 0); |
| 27 | |
| 28 | // we've done this for the update already |
| 29 | if (Plugin::version() === $current_version) { |
| 30 | return; |
| 31 | } |
| 32 | |
| 33 | // delete update transient to check for pro upgrade |
| 34 | $this->deleteTransients(); |
| 35 | |
| 36 | // update version |
| 37 | update_option($this->name, Plugin::version()); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Delete upgrade transients by key |
| 42 | * |
| 43 | * @return void |
| 44 | */ |
| 45 | public function deleteTransients() |
| 46 | { |
| 47 | foreach ($this->getTransientsWithPrefix('presto_player_check_for_plugin_update') as $key) { |
| 48 | delete_site_transient($key); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Gets transients with a specific prefix. |
| 54 | * |
| 55 | * @param string $prefix |
| 56 | * @return array |
| 57 | */ |
| 58 | public function getTransientsWithPrefix($prefix) |
| 59 | { |
| 60 | global $wpdb; |
| 61 | |
| 62 | $prefix = $wpdb->esc_like('_site_transient_' . $prefix); |
| 63 | $sql = "SELECT `option_name` FROM $wpdb->options WHERE `option_name` LIKE '%s'"; |
| 64 | $keys = $wpdb->get_results($wpdb->prepare($sql, $prefix . '%'), ARRAY_A); |
| 65 | |
| 66 | if (is_wp_error($keys)) { |
| 67 | return []; |
| 68 | } |
| 69 | |
| 70 | // Remove '_transient_' from the option name. |
| 71 | return array_map(function ($key) { |
| 72 | return ltrim($key['option_name'], '_transient_'); |
| 73 | }, $keys); |
| 74 | } |
| 75 | } |
| 76 |