Bez popisu

admin-upgrade.php 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. * __construct
  9. *
  10. * Sets up the class functionality.
  11. *
  12. * @date 31/7/18
  13. * @since 5.7.2
  14. *
  15. * @param void
  16. * @return void
  17. */
  18. function __construct() {
  19. // actions
  20. add_action( 'admin_menu', array( $this, 'admin_menu' ), 20 );
  21. if ( is_multisite() ) {
  22. add_action( 'network_admin_menu', array( $this, 'network_admin_menu' ), 20 );
  23. }
  24. }
  25. /**
  26. * admin_menu
  27. *
  28. * Setus up logic if DB Upgrade is needed on a single site.
  29. *
  30. * @date 24/8/18
  31. * @since 5.7.4
  32. *
  33. * @param void
  34. * @return void
  35. */
  36. function admin_menu() {
  37. // check if upgrade is avaialble
  38. if ( acf_has_upgrade() ) {
  39. // add notice
  40. add_action( 'admin_notices', array( $this, 'admin_notices' ) );
  41. // add page
  42. $page = add_submenu_page( 'index.php', __( 'Upgrade Database', 'acf' ), __( 'Upgrade Database', 'acf' ), acf_get_setting( 'capability' ), 'acf-upgrade', array( $this, 'admin_html' ) );
  43. // actions
  44. add_action( 'load-' . $page, array( $this, 'admin_load' ) );
  45. }
  46. }
  47. /**
  48. * network_admin_menu
  49. *
  50. * Sets up admin logic if DB Upgrade is required on a multi site.
  51. *
  52. * @date 24/8/18
  53. * @since 5.7.4
  54. *
  55. * @param void
  56. * @return void
  57. */
  58. function network_admin_menu() {
  59. // Vars.
  60. $upgrade = false;
  61. // Loop over sites and check for upgrades.
  62. $sites = get_sites( array( 'number' => 0 ) );
  63. if ( $sites ) {
  64. // Unhook action to avoid memory issue (as seen in wp-includes/ms-site.php).
  65. remove_action( 'switch_blog', 'wp_switch_roles_and_user', 1 );
  66. foreach ( $sites as $site ) {
  67. // Switch site.
  68. switch_to_blog( $site->blog_id );
  69. // Check for upgrade.
  70. $site_upgrade = acf_has_upgrade();
  71. // Restore site.
  72. // Ideally, we would switch back to the original site at after looping, however,
  73. // the restore_current_blog() is needed to modify global vars.
  74. restore_current_blog();
  75. // Check if upgrade was found.
  76. if ( $site_upgrade ) {
  77. $upgrade = true;
  78. break;
  79. }
  80. }
  81. add_action( 'switch_blog', 'wp_switch_roles_and_user', 1, 2 );
  82. }
  83. // Bail early if no upgrade is needed.
  84. if ( ! $upgrade ) {
  85. return;
  86. }
  87. // Add notice.
  88. add_action( 'network_admin_notices', array( $this, 'network_admin_notices' ) );
  89. // Add page.
  90. $page = add_submenu_page(
  91. 'index.php',
  92. __( 'Upgrade Database', 'acf' ),
  93. __( 'Upgrade Database', 'acf' ),
  94. acf_get_setting( 'capability' ),
  95. 'acf-upgrade-network',
  96. array( $this, 'network_admin_html' )
  97. );
  98. add_action( "load-$page", array( $this, 'network_admin_load' ) );
  99. }
  100. /**
  101. * admin_load
  102. *
  103. * Runs during the loading of the admin page.
  104. *
  105. * @date 24/8/18
  106. * @since 5.7.4
  107. *
  108. * @param type $var Description. Default.
  109. * @return type Description.
  110. */
  111. function admin_load() {
  112. // remove prompt
  113. remove_action( 'admin_notices', array( $this, 'admin_notices' ) );
  114. // Enqueue core script.
  115. acf_enqueue_script( 'acf' );
  116. }
  117. /**
  118. * network_admin_load
  119. *
  120. * Runs during the loading of the network admin page.
  121. *
  122. * @date 24/8/18
  123. * @since 5.7.4
  124. *
  125. * @param type $var Description. Default.
  126. * @return type Description.
  127. */
  128. function network_admin_load() {
  129. // remove prompt
  130. remove_action( 'network_admin_notices', array( $this, 'network_admin_notices' ) );
  131. // Enqueue core script.
  132. acf_enqueue_script( 'acf' );
  133. }
  134. /**
  135. * admin_notices
  136. *
  137. * Displays the DB Upgrade prompt.
  138. *
  139. * @date 23/8/18
  140. * @since 5.7.3
  141. *
  142. * @param void
  143. * @return void
  144. */
  145. function admin_notices() {
  146. // vars
  147. $view = array(
  148. 'button_text' => __( 'Upgrade Database', 'acf' ),
  149. 'button_url' => admin_url( 'index.php?page=acf-upgrade' ),
  150. 'confirm' => true,
  151. );
  152. // view
  153. acf_get_view( 'html-notice-upgrade', $view );
  154. }
  155. /**
  156. * network_admin_notices
  157. *
  158. * Displays the DB Upgrade prompt on a multi site.
  159. *
  160. * @date 23/8/18
  161. * @since 5.7.3
  162. *
  163. * @param void
  164. * @return void
  165. */
  166. function network_admin_notices() {
  167. // vars
  168. $view = array(
  169. 'button_text' => __( 'Review sites & upgrade', 'acf' ),
  170. 'button_url' => network_admin_url( 'index.php?page=acf-upgrade-network' ),
  171. 'confirm' => false,
  172. );
  173. // view
  174. acf_get_view( 'html-notice-upgrade', $view );
  175. }
  176. /**
  177. * admin_html
  178. *
  179. * Displays the HTML for the admin page.
  180. *
  181. * @date 24/8/18
  182. * @since 5.7.4
  183. *
  184. * @param void
  185. * @return void
  186. */
  187. function admin_html() {
  188. acf_get_view( 'html-admin-page-upgrade' );
  189. }
  190. /**
  191. * network_admin_html
  192. *
  193. * Displays the HTML for the network upgrade admin page.
  194. *
  195. * @date 24/8/18
  196. * @since 5.7.4
  197. *
  198. * @param void
  199. * @return void
  200. */
  201. function network_admin_html() {
  202. acf_get_view( 'html-admin-page-upgrade-network' );
  203. }
  204. }
  205. // instantiate
  206. acf_new_instance( 'ACF_Admin_Upgrade' );
  207. endif; // class_exists check