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