Admin
2 weeks ago
Ajax
2 weeks ago
Asset
2 weeks ago
Context
2 weeks ago
Customizer
2 weeks ago
Debug_Bar
2 weeks ago
Dialog
2 weeks ago
Documentation
2 weeks ago
Duplicate
2 weeks ago
Editor
2 weeks ago
Image
2 weeks ago
JSON_LD
2 weeks ago
Languages
2 weeks ago
Log
2 weeks ago
Meta
2 weeks ago
Models
2 weeks ago
PUE
2 weeks ago
Process
2 weeks ago
Promoter
2 weeks ago
REST
2 weeks ago
Repository
2 weeks ago
Service_Providers
2 weeks ago
Shortcode
2 weeks ago
Support
2 weeks ago
Tabbed_View
2 weeks ago
Tooltip
2 weeks ago
Traits
2 weeks ago
Utils
2 weeks ago
Validator
2 weeks ago
Widget
2 weeks ago
Abstract_Deactivation.php
2 weeks ago
Abstract_Plugin_Register.php
2 weeks ago
App_Shop.php
2 weeks ago
Assets.php
2 weeks ago
Assets_Pipeline.php
2 weeks ago
Autoloader.php
2 weeks ago
Cache.php
2 weeks ago
Cache_Listener.php
2 weeks ago
Changelog_Reader.php
2 weeks ago
Container.php
2 weeks ago
Context.php
2 weeks ago
Cost_Utils.php
2 weeks ago
Credits.php
2 weeks ago
Customizer.php
2 weeks ago
DB_Lock.php
2 weeks ago
Data.php
2 weeks ago
Date_Utils.php
2 weeks ago
Db.php
2 weeks ago
Debug.php
2 weeks ago
Dependency.php
2 weeks ago
Deprecation.php
2 weeks ago
Editor.php
2 weeks ago
Error.php
2 weeks ago
Exception.php
2 weeks ago
Extension.php
2 weeks ago
Extension_Loader.php
2 weeks ago
Feature_Detection.php
2 weeks ago
Field.php
2 weeks ago
Field_Conditional.php
2 weeks ago
Freemius.php
2 weeks ago
Log.php
2 weeks ago
Main.php
2 weeks ago
Notices.php
2 weeks ago
Plugin_Meta_Links.php
2 weeks ago
Plugins.php
2 weeks ago
Plugins_API.php
2 weeks ago
Post_History.php
2 weeks ago
Post_Transient.php
2 weeks ago
Promise.php
2 weeks ago
Repository.php
2 weeks ago
Rewrite.php
2 weeks ago
Settings.php
2 weeks ago
Settings_Manager.php
2 weeks ago
Settings_Tab.php
2 weeks ago
Simple_Table.php
2 weeks ago
Support.php
2 weeks ago
Tabbed_View.php
2 weeks ago
Template.php
2 weeks ago
Template_Factory.php
2 weeks ago
Template_Part_Cache.php
2 weeks ago
Templates.php
2 weeks ago
Terms.php
2 weeks ago
Timezones.php
2 weeks ago
Tracker.php
2 weeks ago
Updater.php
2 weeks ago
Validate.php
2 weeks ago
View_Helpers.php
2 weeks ago
Updater.php
181 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | /** |
| 5 | * Run schema updates on plugin activation or updates |
| 6 | * |
| 7 | * @since 4.9.4 |
| 8 | * |
| 9 | */ |
| 10 | class Tribe__Updater { |
| 11 | |
| 12 | protected $version_option = 'schema-version'; |
| 13 | protected $reset_version = '3.9'; // when a reset() is called, go to this version |
| 14 | protected $current_version = 0; |
| 15 | |
| 16 | public $capabilities; |
| 17 | |
| 18 | /** |
| 19 | * Tribe__Updater constructor. |
| 20 | * |
| 21 | * @since 4.9.4 |
| 22 | * |
| 23 | * @param int $current_version the current version number of a plugin |
| 24 | */ |
| 25 | public function __construct( $current_version ) { |
| 26 | $this->current_version = $current_version; |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * We've had problems with the notoptions and |
| 31 | * alloptions caches getting out of sync with the DB, |
| 32 | * forcing an eternal update cycle |
| 33 | * |
| 34 | * @since 4.9.4 |
| 35 | * |
| 36 | */ |
| 37 | protected function clear_option_caches() { |
| 38 | wp_cache_delete( 'notoptions', 'options' ); |
| 39 | wp_cache_delete( 'alloptions', 'options' ); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Run Updates for a Plugin |
| 44 | * |
| 45 | * @since 4.9.4 |
| 46 | * |
| 47 | */ |
| 48 | public function do_updates() { |
| 49 | $this->clear_option_caches(); |
| 50 | $updates = $this->get_update_callbacks(); |
| 51 | uksort( $updates, 'version_compare' ); |
| 52 | |
| 53 | try { |
| 54 | foreach ( $updates as $version => $callback ) { |
| 55 | |
| 56 | if ( ! $this->is_new_install() && version_compare( $version, $this->current_version, '<=' ) && $this->is_version_in_db_less_than( $version ) ) { |
| 57 | call_user_func( $callback ); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | foreach ( $this->get_constant_update_callbacks() as $callback ) { |
| 62 | call_user_func( $callback ); |
| 63 | } |
| 64 | |
| 65 | $this->update_version_option( $this->current_version ); |
| 66 | } catch ( Exception $e ) { |
| 67 | // fail silently, but it should try again next time |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Update Version Number for a Plugin |
| 73 | * |
| 74 | * @since 4.9.4 |
| 75 | * |
| 76 | * @param int $new_version the current version number of a plugin |
| 77 | */ |
| 78 | public function update_version_option( $new_version ) { |
| 79 | Tribe__Settings_Manager::set_option( $this->version_option, $new_version ); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Returns an array of callbacks with version strings as keys. |
| 84 | * Any key higher than the version recorded in the DB |
| 85 | * and lower than $this->current_version will have its |
| 86 | * callback called. |
| 87 | * |
| 88 | * @since 4.9.4 |
| 89 | * |
| 90 | * @return array |
| 91 | */ |
| 92 | public function get_update_callbacks() { |
| 93 | return []; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Returns an array of callbacks that should be called |
| 98 | * every time the version is updated |
| 99 | * |
| 100 | * @since 4.9.4 |
| 101 | * |
| 102 | * @return array |
| 103 | */ |
| 104 | public function get_constant_update_callbacks() { |
| 105 | return [ |
| 106 | [ $this, 'flush_rewrites' ], |
| 107 | ]; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Get version from Tribe Settings for the Plugin |
| 112 | * |
| 113 | * @since 4.9.4 |
| 114 | * |
| 115 | * @return mixed the version number of the plugin saved in the options |
| 116 | */ |
| 117 | public function get_version_from_db() { |
| 118 | return Tribe__Settings_Manager::get_option( $this->version_option ); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Returns true if the version in the DB is less than the provided version |
| 123 | * |
| 124 | * @since 4.9.4 |
| 125 | * |
| 126 | * @return boolean |
| 127 | */ |
| 128 | public function is_version_in_db_less_than( $version ) { |
| 129 | $version_in_db = $this->get_version_from_db(); |
| 130 | |
| 131 | return ( version_compare( $version, $version_in_db ) > 0 ); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Returns true if this is a new install |
| 136 | * |
| 137 | * @since 4.9.4 |
| 138 | * |
| 139 | * @return boolean |
| 140 | */ |
| 141 | public function is_new_install() { |
| 142 | $version_in_db = $this->get_version_from_db(); |
| 143 | |
| 144 | return empty( $version_in_db ); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Returns true if an update is required |
| 149 | * |
| 150 | * @since 4.9.4 |
| 151 | * |
| 152 | * @return boolean |
| 153 | */ |
| 154 | public function update_required() { |
| 155 | return $this->is_version_in_db_less_than( $this->current_version ); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Flush Rewrite rules |
| 160 | * |
| 161 | * @since 4.9.4 |
| 162 | * |
| 163 | */ |
| 164 | public function flush_rewrites() { |
| 165 | // run after 'init' to ensure that all CPTs are registered |
| 166 | add_action( 'wp_loaded', 'flush_rewrite_rules' ); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Reset update flags. All updates past $this->reset_version will |
| 171 | * run again on the next page load |
| 172 | * |
| 173 | * @since 4.9.4 |
| 174 | * |
| 175 | */ |
| 176 | public function reset() { |
| 177 | $this->update_version_option( $this->reset_version ); |
| 178 | } |
| 179 | |
| 180 | } |
| 181 |