admin-upgrade.php 6.5 KB

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