Açıklama Yok

class-wc-notes-run-db-update.php 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <?php
  2. /**
  3. * WooCommerce: Db update note.
  4. *
  5. * Adds a note to complete the WooCommerce db update after the upgrade in the WC Admin context.
  6. *
  7. * @package WooCommerce
  8. */
  9. defined( 'ABSPATH' ) || exit;
  10. use \Automattic\Jetpack\Constants;
  11. use Automattic\WooCommerce\Admin\Notes\Note;
  12. /**
  13. * WC_Notes_Run_Db_Update.
  14. */
  15. class WC_Notes_Run_Db_Update {
  16. const NOTE_NAME = 'wc-update-db-reminder';
  17. /**
  18. * Attach hooks.
  19. */
  20. public function __construct() {
  21. // If the old notice gets dismissed, also hide this new one.
  22. add_action( 'woocommerce_hide_update_notice', array( __CLASS__, 'set_notice_actioned' ) );
  23. // Not using Jetpack\Constants here as it can run before 'plugin_loaded' is done.
  24. if ( defined( 'DOING_AJAX' ) && DOING_AJAX
  25. || defined( 'DOING_CRON' ) && DOING_CRON
  26. || ! is_admin() ) {
  27. return;
  28. }
  29. add_action( 'current_screen', array( __CLASS__, 'show_reminder' ) );
  30. }
  31. /**
  32. * Get current notice id from the database.
  33. *
  34. * Retrieves the first notice of this type.
  35. *
  36. * @return int|void Note id or null in case no note was found.
  37. */
  38. private static function get_current_notice() {
  39. try {
  40. $data_store = \WC_Data_Store::load( 'admin-note' );
  41. } catch ( Exception $e ) {
  42. return;
  43. }
  44. $note_ids = $data_store->get_notes_with_name( self::NOTE_NAME );
  45. if ( empty( $note_ids ) ) {
  46. return;
  47. }
  48. if ( count( $note_ids ) > 1 ) {
  49. // Remove weird duplicates. Leave the first one.
  50. $current_notice = array_shift( $note_ids );
  51. foreach ( $note_ids as $note_id ) {
  52. $note = new Note( $note_id );
  53. $data_store->delete( $note );
  54. }
  55. return $current_notice;
  56. }
  57. return current( $note_ids );
  58. }
  59. /**
  60. * Set this notice to an actioned one, so that it's no longer displayed.
  61. */
  62. public static function set_notice_actioned() {
  63. $note_id = self::get_current_notice();
  64. if ( ! $note_id ) {
  65. return;
  66. }
  67. $note = new Note( $note_id );
  68. $note->set_status( Note::E_WC_ADMIN_NOTE_ACTIONED );
  69. $note->save();
  70. }
  71. /**
  72. * Check whether the note is up to date for a fresh display.
  73. *
  74. * The check tests if
  75. * - actions are set up for the first 'Update database' notice, and
  76. * - URL for note's action is equal to the given URL (to check for potential nonce update).
  77. *
  78. * @param Note $note Note to check.
  79. * @param string $update_url URL to check the note against.
  80. * @param array<int, string> $current_actions List of actions to check for.
  81. * @return bool
  82. */
  83. private static function note_up_to_date( $note, $update_url, $current_actions ) {
  84. $actions = $note->get_actions();
  85. return count( $current_actions ) === count( array_intersect( wp_list_pluck( $actions, 'name' ), $current_actions ) )
  86. && in_array( $update_url, wp_list_pluck( $actions, 'query' ), true );
  87. }
  88. /**
  89. * Create and set up the first (out of 3) 'Database update needed' notice and store it in the database.
  90. *
  91. * If a $note_id is given, the method updates the note instead of creating a new one.
  92. *
  93. * @param integer $note_id Note db record to update.
  94. * @return int Created/Updated note id
  95. */
  96. private static function update_needed_notice( $note_id = null ) {
  97. $update_url =
  98. add_query_arg(
  99. array(
  100. 'do_update_woocommerce' => 'true',
  101. ),
  102. wc_get_current_admin_url() ? wc_get_current_admin_url() : admin_url( 'admin.php?page=wc-settings' )
  103. );
  104. $note_actions = array(
  105. array(
  106. 'name' => 'update-db_run',
  107. 'label' => __( 'Update WooCommerce Database', 'woocommerce' ),
  108. 'url' => $update_url,
  109. 'status' => 'unactioned',
  110. 'primary' => true,
  111. 'nonce_action' => 'wc_db_update',
  112. 'nonce_name' => 'wc_db_update_nonce',
  113. ),
  114. array(
  115. 'name' => 'update-db_learn-more',
  116. 'label' => __( 'Learn more about updates', 'woocommerce' ),
  117. 'url' => 'https://docs.woocommerce.com/document/how-to-update-woocommerce/',
  118. 'status' => 'unactioned',
  119. 'primary' => false,
  120. ),
  121. );
  122. if ( $note_id ) {
  123. $note = new Note( $note_id );
  124. } else {
  125. $note = new Note();
  126. }
  127. // Check if the note needs to be updated (e.g. expired nonce or different note type stored in the previous run).
  128. if ( self::note_up_to_date( $note, $update_url, wp_list_pluck( $note_actions, 'name' ) ) ) {
  129. return $note_id;
  130. }
  131. $note->set_title( __( 'WooCommerce database update required', 'woocommerce' ) );
  132. $note->set_content(
  133. __( 'WooCommerce has been updated! To keep things running smoothly, we have to update your database to the newest version.', 'woocommerce' )
  134. /* translators: %1$s: opening <a> tag %2$s: closing </a> tag*/
  135. . sprintf( ' ' . esc_html__( 'The database update process runs in the background and may take a little while, so please be patient. Advanced users can alternatively update via %1$sWP CLI%2$s.', 'woocommerce' ), '<a href="https://github.com/woocommerce/woocommerce/wiki/Upgrading-the-database-using-WP-CLI">', '</a>' )
  136. );
  137. $note->set_type( Note::E_WC_ADMIN_NOTE_UPDATE );
  138. $note->set_name( self::NOTE_NAME );
  139. $note->set_content_data( (object) array() );
  140. $note->set_source( 'woocommerce-core' );
  141. // In case db version is out of sync with WC version or during the next update, the notice needs to show up again,
  142. // so set it to unactioned.
  143. $note->set_status( Note::E_WC_ADMIN_NOTE_UNACTIONED );
  144. // Set new actions.
  145. $note->clear_actions();
  146. foreach ( $note_actions as $note_action ) {
  147. $note->add_action( ...array_values( $note_action ) );
  148. if ( isset( $note_action['nonce_action'] ) ) {
  149. $note->add_nonce_to_action( $note_action['name'], $note_action['nonce_action'], $note_action['nonce_name'] );
  150. }
  151. }
  152. return $note->save();
  153. }
  154. /**
  155. * Update the existing note with $note_id with information about the db upgrade being in progress.
  156. *
  157. * This is the second out of 3 notices displayed to the user.
  158. *
  159. * @param int $note_id Note id to update.
  160. */
  161. private static function update_in_progress_notice( $note_id ) {
  162. // Same actions as in includes/admin/views/html-notice-updating.php. This just redirects, performs no action, so without nonce.
  163. $pending_actions_url = admin_url( 'admin.php?page=wc-status&tab=action-scheduler&s=woocommerce_run_update&status=pending' );
  164. $cron_disabled = Constants::is_true( 'DISABLE_WP_CRON' );
  165. $cron_cta = $cron_disabled ? __( 'You can manually run queued updates here.', 'woocommerce' ) : __( 'View progress →', 'woocommerce' );
  166. $note = new Note( $note_id );
  167. $note->set_title( __( 'WooCommerce database update in progress', 'woocommerce' ) );
  168. $note->set_content( __( 'WooCommerce is updating the database in the background. The database update process may take a little while, so please be patient.', 'woocommerce' ) );
  169. $note->clear_actions();
  170. $note->add_action(
  171. 'update-db_see-progress',
  172. $cron_cta,
  173. $pending_actions_url,
  174. 'unactioned',
  175. false
  176. );
  177. $note->save();
  178. }
  179. /**
  180. * Update the existing note with $note_id with information that db upgrade is done.
  181. *
  182. * This is the last notice (3 out of 3 notices) displayed to the user.
  183. *
  184. * @param int $note_id Note id to update.
  185. */
  186. private static function update_done_notice( $note_id ) {
  187. $hide_notices_url = html_entity_decode( // to convert &amp;s to normal &, otherwise produces invalid link.
  188. add_query_arg(
  189. array(
  190. 'wc-hide-notice' => 'update',
  191. ),
  192. wc_get_current_admin_url() ? remove_query_arg( 'do_update_woocommerce', wc_get_current_admin_url() ) : admin_url( 'admin.php?page=wc-settings' )
  193. )
  194. );
  195. $note_actions = array(
  196. array(
  197. 'name' => 'update-db_done',
  198. 'label' => __( 'Thanks!', 'woocommerce' ),
  199. 'url' => $hide_notices_url,
  200. 'status' => 'actioned',
  201. 'primary' => true,
  202. 'nonce_action' => 'woocommerce_hide_notices_nonce',
  203. 'nonce_name' => '_wc_notice_nonce',
  204. ),
  205. );
  206. $note = new Note( $note_id );
  207. // Check if the note needs to be updated (e.g. expired nonce or different note type stored in the previous run).
  208. if ( self::note_up_to_date( $note, $hide_notices_url, wp_list_pluck( $note_actions, 'name' ) ) ) {
  209. return $note_id;
  210. }
  211. $note->set_title( __( 'WooCommerce database update done', 'woocommerce' ) );
  212. $note->set_content( __( 'WooCommerce database update complete. Thank you for updating to the latest version!', 'woocommerce' ) );
  213. $note->clear_actions();
  214. foreach ( $note_actions as $note_action ) {
  215. $note->add_action( ...array_values( $note_action ) );
  216. if ( isset( $note_action['nonce_action'] ) ) {
  217. $note->add_nonce_to_action( $note_action['name'], $note_action['nonce_action'], $note_action['nonce_name'] );
  218. }
  219. }
  220. $note->save();
  221. }
  222. /**
  223. * Prepare the correct content of the db update note to be displayed by WC Admin.
  224. *
  225. * This one gets called on each page load, so try to bail quickly.
  226. *
  227. * If the db needs an update, the notice should be always shown.
  228. * If the db does not need an update, but the notice has *not* been actioned (i.e. after the db update, when
  229. * store owner hasn't acknowledged the successful db update), still show the Thanks notice.
  230. * If the db does not need an update, and the notice has been actioned, then notice should *not* be shown.
  231. * The notice should also be hidden if the db does not need an update and the notice does not exist.
  232. */
  233. public static function show_reminder() {
  234. $needs_db_update = \WC_Install::needs_db_update();
  235. $note_id = self::get_current_notice();
  236. if ( ! $needs_db_update ) {
  237. // Db update not needed && note does not exist -> don't show it.
  238. if ( ! $note_id ) {
  239. return;
  240. }
  241. $note = new Note( $note_id );
  242. if ( $note::E_WC_ADMIN_NOTE_ACTIONED === $note->get_status() ) {
  243. // Db update not needed && note actioned -> don't show it.
  244. return;
  245. } else {
  246. // Db update not needed && notice is unactioned -> Thank you note.
  247. self::update_done_notice( $note_id );
  248. return;
  249. }
  250. } else {
  251. // Db needs update &&.
  252. if ( ! $note_id ) {
  253. // Db needs update && no notice exists -> create one that shows Nudge to update.
  254. $note_id = self::update_needed_notice();
  255. }
  256. $next_scheduled_date = WC()->queue()->get_next( 'woocommerce_run_update_callback', null, 'woocommerce-db-updates' );
  257. if ( $next_scheduled_date || ! empty( $_GET['do_update_woocommerce'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
  258. // Db needs update && db update is scheduled -> update note to In progress.
  259. self::update_in_progress_notice( $note_id );
  260. } else {
  261. // Db needs update && db update is not scheduled -> Nudge to run the db update.
  262. self::update_needed_notice( $note_id );
  263. }
  264. }
  265. }
  266. }