暫無描述

wc-admin-update-functions.php 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. /**
  3. * WooCommerce Admin Updates
  4. *
  5. * Functions for updating data, used by the background updater.
  6. *
  7. * @package WooCommerce\Admin
  8. */
  9. use \Automattic\WooCommerce\Admin\Install as Installer;
  10. use \Automattic\WooCommerce\Admin\Notes\Notes;
  11. use \Automattic\WooCommerce\Admin\Notes\UnsecuredReportFiles;
  12. use \Automattic\WooCommerce\Admin\Notes\DeactivatePlugin;
  13. use \Automattic\WooCommerce\Admin\ReportExporter;
  14. /**
  15. * Update order stats `status` index length.
  16. * See: https://github.com/woocommerce/woocommerce-admin/issues/2969.
  17. */
  18. function wc_admin_update_0201_order_status_index() {
  19. global $wpdb;
  20. // Max DB index length. See wp_get_db_schema().
  21. $max_index_length = 191;
  22. $index = $wpdb->get_row( "SHOW INDEX FROM {$wpdb->prefix}wc_order_stats WHERE key_name = 'status'" );
  23. if ( property_exists( $index, 'Sub_part' ) ) {
  24. // The index was created with the right length. Time to bail.
  25. if ( $max_index_length === $index->Sub_part ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName
  26. return;
  27. }
  28. // We need to drop the index so it can be recreated.
  29. $wpdb->query( "DROP INDEX `status` ON {$wpdb->prefix}wc_order_stats" );
  30. }
  31. // Recreate the status index with a max length.
  32. $wpdb->query( $wpdb->prepare( "ALTER TABLE {$wpdb->prefix}wc_order_stats ADD INDEX status (status(%d))", $max_index_length ) );
  33. }
  34. /**
  35. * Update DB Version.
  36. */
  37. function wc_admin_update_0201_db_version() {
  38. Installer::update_db_version( '0.20.1' );
  39. }
  40. /**
  41. * Rename "gross_total" to "total_sales".
  42. * See: https://github.com/woocommerce/woocommerce-admin/issues/3175
  43. */
  44. function wc_admin_update_0230_rename_gross_total() {
  45. global $wpdb;
  46. // We first need to drop the new `total_sales` column, since dbDelta() will have created it.
  47. $wpdb->query( "ALTER TABLE {$wpdb->prefix}wc_order_stats DROP COLUMN `total_sales`" );
  48. // Then we can rename the existing `gross_total` column.
  49. $wpdb->query( "ALTER TABLE {$wpdb->prefix}wc_order_stats CHANGE COLUMN `gross_total` `total_sales` double DEFAULT 0 NOT NULL" );
  50. }
  51. /**
  52. * Update DB Version.
  53. */
  54. function wc_admin_update_0230_db_version() {
  55. Installer::update_db_version( '0.23.0' );
  56. }
  57. /**
  58. * Remove the note unsnoozing scheduled action.
  59. */
  60. function wc_admin_update_0251_remove_unsnooze_action() {
  61. as_unschedule_action( Notes::UNSNOOZE_HOOK, null, 'wc-admin-data' );
  62. as_unschedule_action( Notes::UNSNOOZE_HOOK, null, 'wc-admin-notes' );
  63. }
  64. /**
  65. * Update DB Version.
  66. */
  67. function wc_admin_update_0251_db_version() {
  68. Installer::update_db_version( '0.25.1' );
  69. }
  70. /**
  71. * Remove Facebook Extension note.
  72. */
  73. function wc_admin_update_110_remove_facebook_note() {
  74. Notes::delete_notes_with_name( 'wc-admin-facebook-extension' );
  75. }
  76. /**
  77. * Update DB Version.
  78. */
  79. function wc_admin_update_110_db_version() {
  80. Installer::update_db_version( '1.1.0' );
  81. }
  82. /**
  83. * Remove Dismiss action from tracking opt-in admin note.
  84. */
  85. function wc_admin_update_130_remove_dismiss_action_from_tracking_opt_in_note() {
  86. global $wpdb;
  87. $wpdb->query( "DELETE actions FROM {$wpdb->prefix}wc_admin_note_actions actions INNER JOIN {$wpdb->prefix}wc_admin_notes notes USING (note_id) WHERE actions.name = 'tracking-dismiss' AND notes.name = 'wc-admin-usage-tracking-opt-in'" );
  88. }
  89. /**
  90. * Update DB Version.
  91. */
  92. function wc_admin_update_130_db_version() {
  93. Installer::update_db_version( '1.3.0' );
  94. }
  95. /**
  96. * Change the deactivate plugin note type to 'info'.
  97. */
  98. function wc_admin_update_140_change_deactivate_plugin_note_type() {
  99. global $wpdb;
  100. $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->prefix}wc_admin_notes SET type = 'info' WHERE name = %s", DeactivatePlugin::NOTE_NAME ) );
  101. }
  102. /**
  103. * Update DB Version.
  104. */
  105. function wc_admin_update_140_db_version() {
  106. Installer::update_db_version( '1.4.0' );
  107. }
  108. /**
  109. * Remove Facebook Experts note.
  110. */
  111. function wc_admin_update_160_remove_facebook_note() {
  112. Notes::delete_notes_with_name( 'wc-admin-facebook-marketing-expert' );
  113. }
  114. /**
  115. * Update DB Version.
  116. */
  117. function wc_admin_update_160_db_version() {
  118. Installer::update_db_version( '1.6.0' );
  119. }
  120. /**
  121. * Set "two column" homescreen layout as default for existing stores.
  122. */
  123. function wc_admin_update_170_homescreen_layout() {
  124. add_option( 'woocommerce_default_homepage_layout', 'two_columns', '', 'no' );
  125. }
  126. /**
  127. * Update DB Version.
  128. */
  129. function wc_admin_update_170_db_version() {
  130. Installer::update_db_version( '1.7.0' );
  131. }
  132. /**
  133. * Update the old task list options.
  134. */
  135. function wc_admin_update_270_update_task_list_options() {
  136. $hidden_lists = get_option( 'woocommerce_task_list_hidden_lists', array() );
  137. $setup_list_hidden = get_option( 'woocommerce_task_list_hidden', 'no' );
  138. $extended_list_hidden = get_option( 'woocommerce_extended_task_list_hidden', 'no' );
  139. if ( 'yes' === $setup_list_hidden ) {
  140. $hidden_lists[] = 'setup';
  141. }
  142. if ( 'yes' === $extended_list_hidden ) {
  143. $hidden_lists[] = 'extended';
  144. }
  145. update_option( 'woocommerce_task_list_hidden_lists', array_unique( $hidden_lists ) );
  146. delete_option( 'woocommerce_task_list_hidden' );
  147. delete_option( 'woocommerce_extended_task_list_hidden' );
  148. }
  149. /**
  150. * Delete the preexisting export files.
  151. */
  152. function wc_admin_update_270_delete_report_downloads() {
  153. $upload_dir = wp_upload_dir();
  154. $base_dir = trailingslashit( $upload_dir['basedir'] );
  155. $failed_files = array();
  156. $exports_status = get_option( ReportExporter::EXPORT_STATUS_OPTION, array() );
  157. $has_failure = false;
  158. if ( ! is_array( $exports_status ) ) {
  159. // This is essentially the same path as files failing deletion. Handle as such.
  160. return;
  161. }
  162. // Delete all export files based on the status option values.
  163. foreach ( $exports_status as $key => $progress ) {
  164. list( $report_type, $export_id ) = explode( ':', $key );
  165. if ( ! $export_id ) {
  166. continue;
  167. }
  168. $file = "{$base_dir}wc-{$report_type}-report-export-{$export_id}.csv";
  169. $header = $file . '.headers';
  170. // phpcs:ignore
  171. if ( @file_exists( $file ) && false === @unlink( $file ) ) {
  172. array_push( $failed_files, $file );
  173. }
  174. // phpcs:ignore
  175. if ( @file_exists( $header ) && false === @unlink( $header ) ) {
  176. array_push( $failed_files, $header );
  177. }
  178. }
  179. // If the status option was missing or corrupt, there will be files left over.
  180. $potential_exports = glob( $base_dir . 'wc-*-report-export-*.csv' );
  181. $reports_pattern = '(revenue|products|variations|orders|categories|coupons|taxes|stock|customers|downloads)';
  182. /**
  183. * Look for files we can be reasonably sure were created by the report export.
  184. *
  185. * Export files we created will match the 'wc-*-report-export-*.csv' glob, with
  186. * the first wildcard being one of the exportable report slugs, and the second
  187. * being an integer with 11-14 digits (from microtime()'s output) that represents
  188. * a time in the past.
  189. */
  190. foreach ( $potential_exports as $potential_export ) {
  191. $matches = array();
  192. // See if the filename matches an unfiltered export pattern.
  193. if ( ! preg_match( "/wc-{$reports_pattern}-report-export-(?P<export_id>\d{11,14})\.csv\$/", $potential_export, $matches ) ) {
  194. $has_failure = true;
  195. continue;
  196. }
  197. // Validate the timestamp (anything in the past).
  198. $timestamp = (int) substr( $matches['export_id'], 0, 10 );
  199. if ( ! $timestamp || $timestamp > time() ) {
  200. $has_failure = true;
  201. continue;
  202. }
  203. // phpcs:ignore
  204. if ( false === @unlink( $potential_export ) ) {
  205. array_push( $failed_files, $potential_export );
  206. }
  207. }
  208. // Try deleting failed files once more.
  209. foreach ( $failed_files as $failed_file ) {
  210. // phpcs:ignore
  211. if ( false === @unlink( $failed_file ) ) {
  212. $has_failure = true;
  213. }
  214. }
  215. if ( $has_failure ) {
  216. UnsecuredReportFiles::possibly_add_note();
  217. }
  218. }
  219. /**
  220. * Update DB Version.
  221. */
  222. function wc_admin_update_270_db_version() {
  223. Installer::update_db_version( '2.7.0' );
  224. }