admin-upgrade.php
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; // Exit if accessed directly |
| 5 | } |
| 6 | |
| 7 | if ( ! class_exists( 'ACF_Admin_Upgrade' ) ) : |
| 8 | |
| 9 | class ACF_Admin_Upgrade { |
| 10 | |
| 11 | /** |
| 12 | * The name of the transient to store the network update check status. |
| 13 | * |
| 14 | * @var string |
| 15 | */ |
| 16 | public $network_upgrade_needed_transient; |
| 17 | |
| 18 | /** |
| 19 | * __construct |
| 20 | * |
| 21 | * Sets up the class functionality. |
| 22 | * |
| 23 | * @date 31/7/18 |
| 24 | * @since 5.7.2 |
| 25 | * |
| 26 | * @param void |
| 27 | * @return void |
| 28 | */ |
| 29 | function __construct() { |
| 30 | |
| 31 | $this->network_upgrade_needed_transient = 'acf_network_upgrade_needed_' . ACF_UPGRADE_VERSION; |
| 32 | |
| 33 | add_action( 'admin_menu', array( $this, 'admin_menu' ), 20 ); |
| 34 | if ( is_multisite() ) { |
| 35 | add_action( 'network_admin_menu', array( $this, 'network_admin_menu' ), 20 ); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * admin_menu |
| 41 | * |
| 42 | * Setus up logic if DB Upgrade is needed on a single site. |
| 43 | * |
| 44 | * @date 24/8/18 |
| 45 | * @since 5.7.4 |
| 46 | * |
| 47 | * @param void |
| 48 | * @return void |
| 49 | */ |
| 50 | function admin_menu() { |
| 51 | |
| 52 | // check if upgrade is avaialble |
| 53 | if ( acf_has_upgrade() ) { |
| 54 | |
| 55 | // add notice |
| 56 | add_action( 'admin_notices', array( $this, 'admin_notices' ) ); |
| 57 | |
| 58 | // add page |
| 59 | $page = add_submenu_page( 'index.php', __( 'Upgrade Database', 'acf' ), __( 'Upgrade Database', 'acf' ), acf_get_setting( 'capability' ), 'acf-upgrade', array( $this, 'admin_html' ) ); |
| 60 | |
| 61 | // actions |
| 62 | add_action( 'load-' . $page, array( $this, 'admin_load' ) ); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Displays a “Database Upgrade Required” network admin notice and adds |
| 68 | * the “Upgrade Database” submenu under the “Dashboard” network admin |
| 69 | * menu item if an ACF upgrade needs to run on any network site. |
| 70 | * |
| 71 | * @date 24/8/18 |
| 72 | * @since 5.7.4 |
| 73 | * @since 6.0.0 Reduce memory usage, cache network upgrade checks. |
| 74 | * |
| 75 | * @return void |
| 76 | */ |
| 77 | function network_admin_menu() { |
| 78 | $network_upgrade_needed = get_site_transient( $this->network_upgrade_needed_transient ); |
| 79 | |
| 80 | // No transient value exists, so run the upgrade check. |
| 81 | if ( $network_upgrade_needed === false ) { |
| 82 | $network_upgrade_needed = $this->check_for_network_upgrades(); |
| 83 | } |
| 84 | |
| 85 | if ( $network_upgrade_needed === 'no' ) { |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | add_action( 'network_admin_notices', array( $this, 'network_admin_notices' ) ); |
| 90 | |
| 91 | $page = add_submenu_page( |
| 92 | 'index.php', |
| 93 | __( 'Upgrade Database', 'acf' ), |
| 94 | __( 'Upgrade Database', 'acf' ), |
| 95 | acf_get_setting( 'capability' ), |
| 96 | 'acf-upgrade-network', |
| 97 | array( $this, 'network_admin_html' ) |
| 98 | ); |
| 99 | |
| 100 | add_action( "load-$page", array( $this, 'network_admin_load' ) ); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Checks if an ACF database upgrade is required on any site in the |
| 105 | * multisite network. |
| 106 | * |
| 107 | * Stores the result in `$this->network_upgrade_needed_transient`, |
| 108 | * which is version-linked to ACF_UPGRADE_VERSION: the highest ACF |
| 109 | * version that requires an upgrade function to run. Bumping |
| 110 | * ACF_UPGRADE_VERSION will trigger new upgrade checks but incrementing |
| 111 | * ACF_VERSION alone will not. |
| 112 | * |
| 113 | * @since 6.0.0 |
| 114 | * @return string 'yes' if any site in the network requires an upgrade, |
| 115 | * otherwise 'no'. String instead of boolean so that |
| 116 | * `false` returned from a get_site_transient check can |
| 117 | * denote that an upgrade check is needed. |
| 118 | */ |
| 119 | public function check_for_network_upgrades() { |
| 120 | $network_upgrade_needed = 'no'; |
| 121 | |
| 122 | $sites = get_sites( |
| 123 | array( |
| 124 | 'number' => 0, |
| 125 | 'fields' => 'ids', // Reduces PHP memory usage. |
| 126 | ) |
| 127 | ); |
| 128 | |
| 129 | if ( $sites ) { |
| 130 | // Reduces memory usage (same pattern used in wp-includes/ms-site.php). |
| 131 | remove_action( 'switch_blog', 'wp_switch_roles_and_user', 1 ); |
| 132 | |
| 133 | foreach ( $sites as $site_id ) { |
| 134 | switch_to_blog( $site_id ); |
| 135 | |
| 136 | $site_needs_upgrade = acf_has_upgrade(); |
| 137 | |
| 138 | restore_current_blog(); // Restores global vars. |
| 139 | |
| 140 | if ( $site_needs_upgrade ) { |
| 141 | $network_upgrade_needed = 'yes'; |
| 142 | break; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | add_action( 'switch_blog', 'wp_switch_roles_and_user', 1, 2 ); |
| 147 | } |
| 148 | |
| 149 | set_site_transient( |
| 150 | $this->network_upgrade_needed_transient, |
| 151 | $network_upgrade_needed, |
| 152 | 3 * MONTH_IN_SECONDS |
| 153 | ); |
| 154 | |
| 155 | return $network_upgrade_needed; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * admin_load |
| 160 | * |
| 161 | * Runs during the loading of the admin page. |
| 162 | * |
| 163 | * @date 24/8/18 |
| 164 | * @since 5.7.4 |
| 165 | * |
| 166 | * @param type $var Description. Default. |
| 167 | * @return type Description. |
| 168 | */ |
| 169 | function admin_load() { |
| 170 | |
| 171 | // remove prompt |
| 172 | remove_action( 'admin_notices', array( $this, 'admin_notices' ) ); |
| 173 | |
| 174 | // Enqueue core script. |
| 175 | acf_enqueue_script( 'acf' ); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * network_admin_load |
| 180 | * |
| 181 | * Runs during the loading of the network admin page. |
| 182 | * |
| 183 | * @date 24/8/18 |
| 184 | * @since 5.7.4 |
| 185 | * |
| 186 | * @param type $var Description. Default. |
| 187 | * @return type Description. |
| 188 | */ |
| 189 | function network_admin_load() { |
| 190 | |
| 191 | // remove prompt |
| 192 | remove_action( 'network_admin_notices', array( $this, 'network_admin_notices' ) ); |
| 193 | |
| 194 | // Enqueue core script. |
| 195 | acf_enqueue_script( 'acf' ); |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * admin_notices |
| 200 | * |
| 201 | * Displays the DB Upgrade prompt. |
| 202 | * |
| 203 | * @date 23/8/18 |
| 204 | * @since 5.7.3 |
| 205 | * |
| 206 | * @param void |
| 207 | * @return void |
| 208 | */ |
| 209 | function admin_notices() { |
| 210 | |
| 211 | // vars |
| 212 | $view = array( |
| 213 | 'button_text' => __( 'Upgrade Database', 'acf' ), |
| 214 | 'button_url' => admin_url( 'index.php?page=acf-upgrade' ), |
| 215 | 'confirm' => true, |
| 216 | ); |
| 217 | |
| 218 | // view |
| 219 | acf_get_view( 'html-notice-upgrade', $view ); |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * network_admin_notices |
| 224 | * |
| 225 | * Displays the DB Upgrade prompt on a multi site. |
| 226 | * |
| 227 | * @date 23/8/18 |
| 228 | * @since 5.7.3 |
| 229 | * |
| 230 | * @param void |
| 231 | * @return void |
| 232 | */ |
| 233 | function network_admin_notices() { |
| 234 | |
| 235 | // vars |
| 236 | $view = array( |
| 237 | 'button_text' => __( 'Review sites & upgrade', 'acf' ), |
| 238 | 'button_url' => network_admin_url( 'index.php?page=acf-upgrade-network' ), |
| 239 | 'confirm' => false, |
| 240 | ); |
| 241 | |
| 242 | // view |
| 243 | acf_get_view( 'html-notice-upgrade', $view ); |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * admin_html |
| 248 | * |
| 249 | * Displays the HTML for the admin page. |
| 250 | * |
| 251 | * @date 24/8/18 |
| 252 | * @since 5.7.4 |
| 253 | * |
| 254 | * @param void |
| 255 | * @return void |
| 256 | */ |
| 257 | function admin_html() { |
| 258 | acf_get_view( 'html-admin-page-upgrade' ); |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * network_admin_html |
| 263 | * |
| 264 | * Displays the HTML for the network upgrade admin page. |
| 265 | * |
| 266 | * @date 24/8/18 |
| 267 | * @since 5.7.4 |
| 268 | * |
| 269 | * @param void |
| 270 | * @return void |
| 271 | */ |
| 272 | function network_admin_html() { |
| 273 | acf_get_view( 'html-admin-page-upgrade-network' ); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | // instantiate |
| 278 | acf_new_instance( 'ACF_Admin_Upgrade' ); |
| 279 | |
| 280 | endif; // class_exists check |
| 281 |