post-types
1 year ago
tools
1 year ago
views
1 year ago
admin-internal-post-type-list.php
1 year ago
admin-internal-post-type.php
1 year ago
admin-notices.php
1 year ago
admin-tools.php
1 year ago
admin-upgrade.php
1 year ago
admin.php
1 year ago
index.php
1 year ago
admin-upgrade.php
294 lines
| 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 | * @since 5.7.4 |
| 72 | * @since 6.0.0 Reduce memory usage, cache network upgrade checks. |
| 73 | */ |
| 74 | function network_admin_menu() { |
| 75 | $network_upgrade_needed = get_site_transient( $this->network_upgrade_needed_transient ); |
| 76 | |
| 77 | // No transient value exists, so run the upgrade check. |
| 78 | if ( $network_upgrade_needed === false ) { |
| 79 | $network_upgrade_needed = $this->check_for_network_upgrades(); |
| 80 | } |
| 81 | |
| 82 | if ( $network_upgrade_needed === 'no' ) { |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | add_action( 'network_admin_notices', array( $this, 'network_admin_notices' ) ); |
| 87 | |
| 88 | $page = add_submenu_page( |
| 89 | 'index.php', |
| 90 | __( 'Upgrade Database', 'acf' ), |
| 91 | __( 'Upgrade Database', 'acf' ), |
| 92 | acf_get_setting( 'capability' ), |
| 93 | 'acf-upgrade-network', |
| 94 | array( $this, 'network_admin_html' ) |
| 95 | ); |
| 96 | |
| 97 | add_action( "load-$page", array( $this, 'network_admin_load' ) ); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Checks if an ACF database upgrade is required on any site in the |
| 102 | * multisite network. |
| 103 | * |
| 104 | * Stores the result in `$this->network_upgrade_needed_transient`, |
| 105 | * which is version-linked to ACF_UPGRADE_VERSION: the highest ACF |
| 106 | * version that requires an upgrade function to run. Bumping |
| 107 | * ACF_UPGRADE_VERSION will trigger new upgrade checks but incrementing |
| 108 | * ACF_VERSION alone will not. |
| 109 | * |
| 110 | * @since 6.0.0 |
| 111 | * @return string 'yes' if any site in the network requires an upgrade, |
| 112 | * otherwise 'no'. String instead of boolean so that |
| 113 | * `false` returned from a get_site_transient check can |
| 114 | * denote that an upgrade check is needed. |
| 115 | */ |
| 116 | public function check_for_network_upgrades() { |
| 117 | $network_upgrade_needed = 'no'; |
| 118 | |
| 119 | $sites = get_sites( |
| 120 | array( |
| 121 | 'number' => 0, |
| 122 | 'fields' => 'ids', // Reduces PHP memory usage. |
| 123 | ) |
| 124 | ); |
| 125 | |
| 126 | if ( $sites ) { |
| 127 | // Reduces memory usage (same pattern used in wp-includes/ms-site.php). |
| 128 | remove_action( 'switch_blog', 'wp_switch_roles_and_user', 1 ); |
| 129 | |
| 130 | foreach ( $sites as $site_id ) { |
| 131 | switch_to_blog( $site_id ); |
| 132 | |
| 133 | $site_needs_upgrade = acf_has_upgrade(); |
| 134 | |
| 135 | restore_current_blog(); // Restores global vars. |
| 136 | |
| 137 | if ( $site_needs_upgrade ) { |
| 138 | $network_upgrade_needed = 'yes'; |
| 139 | break; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | add_action( 'switch_blog', 'wp_switch_roles_and_user', 1, 2 ); |
| 144 | } |
| 145 | |
| 146 | set_site_transient( |
| 147 | $this->network_upgrade_needed_transient, |
| 148 | $network_upgrade_needed, |
| 149 | 3 * MONTH_IN_SECONDS |
| 150 | ); |
| 151 | |
| 152 | return $network_upgrade_needed; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * admin_load |
| 157 | * |
| 158 | * Runs during the loading of the admin page. |
| 159 | * |
| 160 | * @date 24/8/18 |
| 161 | * @since 5.7.4 |
| 162 | * |
| 163 | * @param type $var Description. Default. |
| 164 | * @return type Description. |
| 165 | */ |
| 166 | function admin_load() { |
| 167 | |
| 168 | add_action( 'admin_body_class', array( $this, 'admin_body_class' ) ); |
| 169 | |
| 170 | // remove prompt |
| 171 | remove_action( 'admin_notices', array( $this, 'admin_notices' ) ); |
| 172 | |
| 173 | // Enqueue core script. |
| 174 | acf_enqueue_script( 'acf' ); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * network_admin_load |
| 179 | * |
| 180 | * Runs during the loading of the network admin page. |
| 181 | * |
| 182 | * @date 24/8/18 |
| 183 | * @since 5.7.4 |
| 184 | * |
| 185 | * @param type $var Description. Default. |
| 186 | * @return type Description. |
| 187 | */ |
| 188 | function network_admin_load() { |
| 189 | |
| 190 | add_action( 'admin_body_class', array( $this, 'admin_body_class' ) ); |
| 191 | |
| 192 | // remove prompt |
| 193 | remove_action( 'network_admin_notices', array( $this, 'network_admin_notices' ) ); |
| 194 | |
| 195 | // Enqueue core script. |
| 196 | acf_enqueue_script( 'acf' ); |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Modifies the admin body class. |
| 201 | * |
| 202 | * @since 6.0.0 |
| 203 | * |
| 204 | * @param string $classes Space-separated list of CSS classes. |
| 205 | * @return string |
| 206 | */ |
| 207 | public function admin_body_class( $classes ) { |
| 208 | $classes .= ' acf-admin-page'; |
| 209 | return $classes; |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * admin_notices |
| 214 | * |
| 215 | * Displays the DB Upgrade prompt. |
| 216 | * |
| 217 | * @date 23/8/18 |
| 218 | * @since 5.7.3 |
| 219 | * |
| 220 | * @param void |
| 221 | * @return void |
| 222 | */ |
| 223 | function admin_notices() { |
| 224 | |
| 225 | // vars |
| 226 | $view = array( |
| 227 | 'button_text' => __( 'Upgrade Database', 'acf' ), |
| 228 | 'button_url' => admin_url( 'index.php?page=acf-upgrade' ), |
| 229 | 'confirm' => true, |
| 230 | ); |
| 231 | |
| 232 | // view |
| 233 | acf_get_view( 'upgrade/notice', $view ); |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * network_admin_notices |
| 238 | * |
| 239 | * Displays the DB Upgrade prompt on a multi site. |
| 240 | * |
| 241 | * @date 23/8/18 |
| 242 | * @since 5.7.3 |
| 243 | * |
| 244 | * @param void |
| 245 | * @return void |
| 246 | */ |
| 247 | function network_admin_notices() { |
| 248 | |
| 249 | // vars |
| 250 | $view = array( |
| 251 | 'button_text' => __( 'Review sites & upgrade', 'acf' ), |
| 252 | 'button_url' => network_admin_url( 'index.php?page=acf-upgrade-network' ), |
| 253 | 'confirm' => false, |
| 254 | ); |
| 255 | |
| 256 | // view |
| 257 | acf_get_view( 'upgrade/notice', $view ); |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * admin_html |
| 262 | * |
| 263 | * Displays the HTML for the admin page. |
| 264 | * |
| 265 | * @date 24/8/18 |
| 266 | * @since 5.7.4 |
| 267 | * |
| 268 | * @param void |
| 269 | * @return void |
| 270 | */ |
| 271 | function admin_html() { |
| 272 | acf_get_view( 'upgrade/upgrade' ); |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * network_admin_html |
| 277 | * |
| 278 | * Displays the HTML for the network upgrade admin page. |
| 279 | * |
| 280 | * @date 24/8/18 |
| 281 | * @since 5.7.4 |
| 282 | * |
| 283 | * @param void |
| 284 | * @return void |
| 285 | */ |
| 286 | function network_admin_html() { |
| 287 | acf_get_view( 'upgrade/network' ); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | // instantiate |
| 292 | acf_new_instance( 'ACF_Admin_Upgrade' ); |
| 293 | endif; // class_exists check |
| 294 |