Updater.php
96 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | class AC_Plugin_Updater { |
| 8 | |
| 9 | /** |
| 10 | * @var self |
| 11 | */ |
| 12 | protected static $instance; |
| 13 | |
| 14 | /** |
| 15 | * @var bool |
| 16 | */ |
| 17 | protected $fresh_install; |
| 18 | |
| 19 | /** |
| 20 | * @var bool |
| 21 | */ |
| 22 | protected $apply_updates; |
| 23 | |
| 24 | /** |
| 25 | * @var AC_Plugin_Update[] |
| 26 | */ |
| 27 | protected $updates; |
| 28 | |
| 29 | /** |
| 30 | * @var AC_Plugin |
| 31 | */ |
| 32 | protected $plugin; |
| 33 | |
| 34 | /** |
| 35 | * @param AC_Plugin $plugin |
| 36 | */ |
| 37 | public function __construct( AC_Plugin $plugin ) { |
| 38 | $this->plugin = $plugin; |
| 39 | $this->apply_updates = 'true' === filter_input( INPUT_GET, 'ac_do_update' ); |
| 40 | } |
| 41 | |
| 42 | public function add_update( AC_Plugin_Update $update ) { |
| 43 | $this->updates[ $update->get_version() ] = $update; |
| 44 | } |
| 45 | |
| 46 | public function parse_updates() { |
| 47 | |
| 48 | // Network wide updating is not allowed |
| 49 | if ( is_network_admin() ) { |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | $plugin = $this->plugin; |
| 54 | |
| 55 | if ( $plugin->is_fresh_install() ) { |
| 56 | $plugin->update_stored_version( $plugin->get_version() ); |
| 57 | |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | krsort( $this->updates, SORT_NUMERIC ); |
| 62 | |
| 63 | /* @var AC_Plugin_Update $update */ |
| 64 | foreach ( $this->updates as $update ) { |
| 65 | if ( $update->needs_update() ) { |
| 66 | if ( ! $this->apply_updates ) { |
| 67 | $this->show_update_notice(); |
| 68 | |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | $update->apply_update(); |
| 73 | $plugin->update_stored_version( $update->get_version() ); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | if ( $this->apply_updates ) { |
| 78 | $plugin->update_stored_version( $plugin->get_version() ); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | public function show_update_notice() { |
| 83 | $url = add_query_arg( array( 'ac_do_update' => 'true' ), AC()->admin()->get_settings_url() ); |
| 84 | |
| 85 | $message = sprintf( '<strong>%s</strong> – %s <a href="%s" class="button ac-update-now">%s</a>', |
| 86 | esc_html__( 'Admin Columns', 'codepress-admin-columns' ), |
| 87 | esc_html__( 'We need to update your database to the latest version.', 'codepress-admin-columns' ), |
| 88 | esc_url( $url ), |
| 89 | esc_html__( 'Run the updater', 'codepress-admin-columns' ) |
| 90 | ); |
| 91 | |
| 92 | AC()->notice( $message, 'notice-info' ); |
| 93 | } |
| 94 | |
| 95 | } |
| 96 |