Updater.php
124 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 | // TODO: https://github.com/codepress/admin-columns-issues/issues/982 |
| 40 | //$this->apply_updates = 'true' === filter_input( INPUT_GET, 'ac_do_update' ); |
| 41 | $this->apply_updates = true; |
| 42 | } |
| 43 | |
| 44 | public function add_update( AC_Plugin_Update $update ) { |
| 45 | $this->updates[ $update->get_version() ] = $update; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Checks conditions like user permissions |
| 50 | * |
| 51 | */ |
| 52 | public function check_update_conditions() { |
| 53 | if ( ! AC()->user_can_manage_admin_columns() ) { |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | // Network wide updating is not supported yet |
| 58 | if ( is_network_admin() ) { |
| 59 | return false; |
| 60 | } |
| 61 | |
| 62 | return true; |
| 63 | } |
| 64 | |
| 65 | public function parse_updates() { |
| 66 | if ( ! $this->check_update_conditions() ) { |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | if ( $this->plugin->is_new_install() ) { |
| 71 | $this->plugin->update_stored_version(); |
| 72 | |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | krsort( $this->updates, SORT_NUMERIC ); |
| 77 | |
| 78 | /* @var AC_Plugin_Update $update */ |
| 79 | foreach ( $this->updates as $update ) { |
| 80 | if ( $update->needs_update() ) { |
| 81 | if ( ! $this->apply_updates ) { |
| 82 | $this->show_update_notice(); |
| 83 | |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | $update->apply_update(); |
| 88 | $this->plugin->update_stored_version( $update->get_version() ); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | if ( ! $this->apply_updates ) { |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | $this->plugin->update_stored_version(); |
| 97 | // TODO: https://github.com/codepress/admin-columns-issues/issues/982 |
| 98 | //$this->show_completed_notice(); |
| 99 | } |
| 100 | |
| 101 | protected function show_completed_notice() { |
| 102 | $message = sprintf( '<strong>%s</strong> – %s', |
| 103 | esc_html__( 'Admin Columns', 'codepress-admin-columns' ), |
| 104 | esc_html__( 'Your database is up to date. You are awesome.', 'codepress-admin-columns' ) |
| 105 | ); |
| 106 | |
| 107 | AC()->notice( $message ); |
| 108 | } |
| 109 | |
| 110 | protected function show_update_notice() { |
| 111 | $url = add_query_arg( array( 'ac_do_update' => 'true' ), AC()->admin()->get_settings_url() ); |
| 112 | |
| 113 | $message = sprintf( '<strong>%s</strong> – %s <a href="%s" class="button ac-update-now">%s</a>', |
| 114 | esc_html__( 'Admin Columns', 'codepress-admin-columns' ), |
| 115 | esc_html__( 'We need to update your database to the latest version.', 'codepress-admin-columns' ), |
| 116 | esc_url( $url ), |
| 117 | esc_html__( 'Run the updater', 'codepress-admin-columns' ) |
| 118 | ); |
| 119 | |
| 120 | AC()->notice( $message, 'notice-info' ); |
| 121 | } |
| 122 | |
| 123 | } |
| 124 |