Sin descripción

wc-update-functions.php 67KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300
  1. <?php
  2. /**
  3. * WooCommerce Updates
  4. *
  5. * Functions for updating data, used by the background updater.
  6. *
  7. * @package WooCommerce\Functions
  8. * @version 3.3.0
  9. */
  10. defined( 'ABSPATH' ) || exit;
  11. use Automattic\WooCommerce\Internal\AssignDefaultCategory;
  12. /**
  13. * Update file paths for 2.0
  14. *
  15. * @return void
  16. */
  17. function wc_update_200_file_paths() {
  18. global $wpdb;
  19. // Upgrade old style files paths to support multiple file paths.
  20. $existing_file_paths = $wpdb->get_results( "SELECT meta_value, meta_id, post_id FROM {$wpdb->postmeta} WHERE meta_key = '_file_path' AND meta_value != '';" );
  21. if ( $existing_file_paths ) {
  22. foreach ( $existing_file_paths as $existing_file_path ) {
  23. $old_file_path = trim( $existing_file_path->meta_value );
  24. if ( ! empty( $old_file_path ) ) {
  25. // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize
  26. $file_paths = serialize( array( md5( $old_file_path ) => $old_file_path ) );
  27. $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->postmeta} SET meta_key = '_file_paths', meta_value = %s WHERE meta_id = %d", $file_paths, $existing_file_path->meta_id ) );
  28. $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->prefix}woocommerce_downloadable_product_permissions SET download_id = %s WHERE product_id = %d", md5( $old_file_path ), $existing_file_path->post_id ) );
  29. }
  30. }
  31. }
  32. }
  33. /**
  34. * Update permalinks for 2.0
  35. *
  36. * @return void
  37. */
  38. function wc_update_200_permalinks() {
  39. // Setup default permalinks if shop page is defined.
  40. $permalinks = get_option( 'woocommerce_permalinks' );
  41. $shop_page_id = wc_get_page_id( 'shop' );
  42. if ( empty( $permalinks ) && $shop_page_id > 0 ) {
  43. $base_slug = $shop_page_id > 0 && get_post( $shop_page_id ) ? get_page_uri( $shop_page_id ) : 'shop';
  44. $category_base = 'yes' === get_option( 'woocommerce_prepend_shop_page_to_urls' ) ? trailingslashit( $base_slug ) : '';
  45. $category_slug = get_option( 'woocommerce_product_category_slug' ) ? get_option( 'woocommerce_product_category_slug' ) : _x( 'product-category', 'slug', 'woocommerce' );
  46. $tag_slug = get_option( 'woocommerce_product_tag_slug' ) ? get_option( 'woocommerce_product_tag_slug' ) : _x( 'product-tag', 'slug', 'woocommerce' );
  47. if ( 'yes' === get_option( 'woocommerce_prepend_shop_page_to_products' ) ) {
  48. $product_base = trailingslashit( $base_slug );
  49. } else {
  50. $product_slug = get_option( 'woocommerce_product_slug' );
  51. if ( false !== $product_slug && ! empty( $product_slug ) ) {
  52. $product_base = trailingslashit( $product_slug );
  53. } else {
  54. $product_base = trailingslashit( _x( 'product', 'slug', 'woocommerce' ) );
  55. }
  56. }
  57. if ( 'yes' === get_option( 'woocommerce_prepend_category_to_products' ) ) {
  58. $product_base .= trailingslashit( '%product_cat%' );
  59. }
  60. $permalinks = array(
  61. 'product_base' => untrailingslashit( $product_base ),
  62. 'category_base' => untrailingslashit( $category_base . $category_slug ),
  63. 'attribute_base' => untrailingslashit( $category_base ),
  64. 'tag_base' => untrailingslashit( $category_base . $tag_slug ),
  65. );
  66. update_option( 'woocommerce_permalinks', $permalinks );
  67. }
  68. }
  69. /**
  70. * Update sub-category display options for 2.0
  71. *
  72. * @return void
  73. */
  74. function wc_update_200_subcat_display() {
  75. // Update subcat display settings.
  76. if ( 'yes' === get_option( 'woocommerce_shop_show_subcategories' ) ) {
  77. if ( 'yes' === get_option( 'woocommerce_hide_products_when_showing_subcategories' ) ) {
  78. update_option( 'woocommerce_shop_page_display', 'subcategories' );
  79. } else {
  80. update_option( 'woocommerce_shop_page_display', 'both' );
  81. }
  82. }
  83. if ( 'yes' === get_option( 'woocommerce_show_subcategories' ) ) {
  84. if ( 'yes' === get_option( 'woocommerce_hide_products_when_showing_subcategories' ) ) {
  85. update_option( 'woocommerce_category_archive_display', 'subcategories' );
  86. } else {
  87. update_option( 'woocommerce_category_archive_display', 'both' );
  88. }
  89. }
  90. }
  91. /**
  92. * Update tax rates for 2.0
  93. *
  94. * @return void
  95. */
  96. function wc_update_200_taxrates() {
  97. global $wpdb;
  98. // Update tax rates.
  99. $loop = 0;
  100. $tax_rates = get_option( 'woocommerce_tax_rates' );
  101. if ( $tax_rates ) {
  102. foreach ( $tax_rates as $tax_rate ) {
  103. foreach ( $tax_rate['countries'] as $country => $states ) {
  104. $states = array_reverse( $states );
  105. foreach ( $states as $state ) {
  106. if ( '*' === $state ) {
  107. $state = '';
  108. }
  109. $wpdb->insert(
  110. $wpdb->prefix . 'woocommerce_tax_rates',
  111. array(
  112. 'tax_rate_country' => $country,
  113. 'tax_rate_state' => $state,
  114. 'tax_rate' => $tax_rate['rate'],
  115. 'tax_rate_name' => $tax_rate['label'],
  116. 'tax_rate_priority' => 1,
  117. 'tax_rate_compound' => ( 'yes' === $tax_rate['compound'] ) ? 1 : 0,
  118. 'tax_rate_shipping' => ( 'yes' === $tax_rate['shipping'] ) ? 1 : 0,
  119. 'tax_rate_order' => $loop,
  120. 'tax_rate_class' => $tax_rate['class'],
  121. )
  122. );
  123. $loop++;
  124. }
  125. }
  126. }
  127. }
  128. $local_tax_rates = get_option( 'woocommerce_local_tax_rates' );
  129. if ( $local_tax_rates ) {
  130. foreach ( $local_tax_rates as $tax_rate ) {
  131. $location_type = ( 'postcode' === $tax_rate['location_type'] ) ? 'postcode' : 'city';
  132. if ( '*' === $tax_rate['state'] ) {
  133. $tax_rate['state'] = '';
  134. }
  135. $wpdb->insert(
  136. $wpdb->prefix . 'woocommerce_tax_rates',
  137. array(
  138. 'tax_rate_country' => $tax_rate['country'],
  139. 'tax_rate_state' => $tax_rate['state'],
  140. 'tax_rate' => $tax_rate['rate'],
  141. 'tax_rate_name' => $tax_rate['label'],
  142. 'tax_rate_priority' => 2,
  143. 'tax_rate_compound' => ( 'yes' === $tax_rate['compound'] ) ? 1 : 0,
  144. 'tax_rate_shipping' => ( 'yes' === $tax_rate['shipping'] ) ? 1 : 0,
  145. 'tax_rate_order' => $loop,
  146. 'tax_rate_class' => $tax_rate['class'],
  147. )
  148. );
  149. $tax_rate_id = $wpdb->insert_id;
  150. if ( $tax_rate['locations'] ) {
  151. foreach ( $tax_rate['locations'] as $location ) {
  152. $wpdb->insert(
  153. $wpdb->prefix . 'woocommerce_tax_rate_locations',
  154. array(
  155. 'location_code' => $location,
  156. 'tax_rate_id' => $tax_rate_id,
  157. 'location_type' => $location_type,
  158. )
  159. );
  160. }
  161. }
  162. $loop++;
  163. }
  164. }
  165. update_option( 'woocommerce_tax_rates_backup', $tax_rates );
  166. update_option( 'woocommerce_local_tax_rates_backup', $local_tax_rates );
  167. delete_option( 'woocommerce_tax_rates' );
  168. delete_option( 'woocommerce_local_tax_rates' );
  169. }
  170. /**
  171. * Update order item line items for 2.0
  172. *
  173. * @return void
  174. */
  175. function wc_update_200_line_items() {
  176. global $wpdb;
  177. // Now its time for the massive update to line items - move them to the new DB tables.
  178. // Reverse with UPDATE `wpwc_postmeta` SET meta_key = '_order_items' WHERE meta_key = '_order_items_old'.
  179. $order_item_rows = $wpdb->get_results(
  180. "SELECT meta_value, post_id FROM {$wpdb->postmeta} WHERE meta_key = '_order_items'"
  181. );
  182. foreach ( $order_item_rows as $order_item_row ) {
  183. $order_items = (array) maybe_unserialize( $order_item_row->meta_value );
  184. foreach ( $order_items as $order_item ) {
  185. if ( ! isset( $order_item['line_total'] ) && isset( $order_item['taxrate'] ) && isset( $order_item['cost'] ) ) {
  186. $order_item['line_tax'] = number_format( ( $order_item['cost'] * $order_item['qty'] ) * ( $order_item['taxrate'] / 100 ), 2, '.', '' );
  187. $order_item['line_total'] = $order_item['cost'] * $order_item['qty'];
  188. $order_item['line_subtotal_tax'] = $order_item['line_tax'];
  189. $order_item['line_subtotal'] = $order_item['line_total'];
  190. }
  191. $order_item['line_tax'] = isset( $order_item['line_tax'] ) ? $order_item['line_tax'] : 0;
  192. $order_item['line_total'] = isset( $order_item['line_total'] ) ? $order_item['line_total'] : 0;
  193. $order_item['line_subtotal_tax'] = isset( $order_item['line_subtotal_tax'] ) ? $order_item['line_subtotal_tax'] : 0;
  194. $order_item['line_subtotal'] = isset( $order_item['line_subtotal'] ) ? $order_item['line_subtotal'] : 0;
  195. $item_id = wc_add_order_item(
  196. $order_item_row->post_id,
  197. array(
  198. 'order_item_name' => $order_item['name'],
  199. 'order_item_type' => 'line_item',
  200. )
  201. );
  202. // Add line item meta.
  203. if ( $item_id ) {
  204. wc_add_order_item_meta( $item_id, '_qty', absint( $order_item['qty'] ) );
  205. wc_add_order_item_meta( $item_id, '_tax_class', $order_item['tax_class'] );
  206. wc_add_order_item_meta( $item_id, '_product_id', $order_item['id'] );
  207. wc_add_order_item_meta( $item_id, '_variation_id', $order_item['variation_id'] );
  208. wc_add_order_item_meta( $item_id, '_line_subtotal', wc_format_decimal( $order_item['line_subtotal'] ) );
  209. wc_add_order_item_meta( $item_id, '_line_subtotal_tax', wc_format_decimal( $order_item['line_subtotal_tax'] ) );
  210. wc_add_order_item_meta( $item_id, '_line_total', wc_format_decimal( $order_item['line_total'] ) );
  211. wc_add_order_item_meta( $item_id, '_line_tax', wc_format_decimal( $order_item['line_tax'] ) );
  212. $meta_rows = array();
  213. // Insert meta.
  214. if ( ! empty( $order_item['item_meta'] ) ) {
  215. foreach ( $order_item['item_meta'] as $key => $meta ) {
  216. // Backwards compatibility.
  217. if ( is_array( $meta ) && isset( $meta['meta_name'] ) ) {
  218. $meta_rows[] = '(' . $item_id . ',"' . esc_sql( $meta['meta_name'] ) . '","' . esc_sql( $meta['meta_value'] ) . '")';
  219. } else {
  220. $meta_rows[] = '(' . $item_id . ',"' . esc_sql( $key ) . '","' . esc_sql( $meta ) . '")';
  221. }
  222. }
  223. }
  224. // Insert meta rows at once.
  225. if ( count( $meta_rows ) > 0 ) {
  226. $wpdb->query(
  227. $wpdb->prepare(
  228. "INSERT INTO {$wpdb->prefix}woocommerce_order_itemmeta ( order_item_id, meta_key, meta_value )
  229. VALUES " . implode( ',', $meta_rows ) . ';', // @codingStandardsIgnoreLine
  230. $order_item_row->post_id
  231. )
  232. );
  233. }
  234. // Delete from DB (rename).
  235. $wpdb->query(
  236. $wpdb->prepare(
  237. "UPDATE {$wpdb->postmeta}
  238. SET meta_key = '_order_items_old'
  239. WHERE meta_key = '_order_items'
  240. AND post_id = %d",
  241. $order_item_row->post_id
  242. )
  243. );
  244. }
  245. unset( $meta_rows, $item_id, $order_item );
  246. }
  247. }
  248. // Do the same kind of update for order_taxes - move to lines.
  249. // Reverse with UPDATE `wpwc_postmeta` SET meta_key = '_order_taxes' WHERE meta_key = '_order_taxes_old'.
  250. $order_tax_rows = $wpdb->get_results(
  251. "SELECT meta_value, post_id FROM {$wpdb->postmeta}
  252. WHERE meta_key = '_order_taxes'"
  253. );
  254. foreach ( $order_tax_rows as $order_tax_row ) {
  255. $order_taxes = (array) maybe_unserialize( $order_tax_row->meta_value );
  256. if ( ! empty( $order_taxes ) ) {
  257. foreach ( $order_taxes as $order_tax ) {
  258. if ( ! isset( $order_tax['label'] ) || ! isset( $order_tax['cart_tax'] ) || ! isset( $order_tax['shipping_tax'] ) ) {
  259. continue;
  260. }
  261. $item_id = wc_add_order_item(
  262. $order_tax_row->post_id,
  263. array(
  264. 'order_item_name' => $order_tax['label'],
  265. 'order_item_type' => 'tax',
  266. )
  267. );
  268. // Add line item meta.
  269. if ( $item_id ) {
  270. wc_add_order_item_meta( $item_id, 'compound', absint( isset( $order_tax['compound'] ) ? $order_tax['compound'] : 0 ) );
  271. wc_add_order_item_meta( $item_id, 'tax_amount', wc_clean( $order_tax['cart_tax'] ) );
  272. wc_add_order_item_meta( $item_id, 'shipping_tax_amount', wc_clean( $order_tax['shipping_tax'] ) );
  273. }
  274. // Delete from DB (rename).
  275. $wpdb->query(
  276. $wpdb->prepare(
  277. "UPDATE {$wpdb->postmeta}
  278. SET meta_key = '_order_taxes_old'
  279. WHERE meta_key = '_order_taxes'
  280. AND post_id = %d",
  281. $order_tax_row->post_id
  282. )
  283. );
  284. unset( $tax_amount );
  285. }
  286. }
  287. }
  288. }
  289. /**
  290. * Update image settings for 2.0
  291. *
  292. * @return void
  293. */
  294. function wc_update_200_images() {
  295. // Grab the pre 2.0 Image options and use to populate the new image options settings,
  296. // cleaning up afterwards like nice people do.
  297. foreach ( array( 'catalog', 'single', 'thumbnail' ) as $value ) {
  298. $old_settings = array_filter(
  299. array(
  300. 'width' => get_option( 'woocommerce_' . $value . '_image_width' ),
  301. 'height' => get_option( 'woocommerce_' . $value . '_image_height' ),
  302. 'crop' => get_option( 'woocommerce_' . $value . '_image_crop' ),
  303. )
  304. );
  305. if ( ! empty( $old_settings ) && update_option( 'shop_' . $value . '_image_size', $old_settings ) ) {
  306. delete_option( 'woocommerce_' . $value . '_image_width' );
  307. delete_option( 'woocommerce_' . $value . '_image_height' );
  308. delete_option( 'woocommerce_' . $value . '_image_crop' );
  309. }
  310. }
  311. }
  312. /**
  313. * Update DB version for 2.0
  314. *
  315. * @return void
  316. */
  317. function wc_update_200_db_version() {
  318. WC_Install::update_db_version( '2.0.0' );
  319. }
  320. /**
  321. * Update Brazilian States for 2.0.9
  322. *
  323. * @return void
  324. */
  325. function wc_update_209_brazillian_state() {
  326. global $wpdb;
  327. // phpcs:disable WordPress.DB.SlowDBQuery
  328. // Update brazillian state codes.
  329. $wpdb->update(
  330. $wpdb->postmeta,
  331. array(
  332. 'meta_value' => 'BA',
  333. ),
  334. array(
  335. 'meta_key' => '_billing_state',
  336. 'meta_value' => 'BH',
  337. )
  338. );
  339. $wpdb->update(
  340. $wpdb->postmeta,
  341. array(
  342. 'meta_value' => 'BA',
  343. ),
  344. array(
  345. 'meta_key' => '_shipping_state',
  346. 'meta_value' => 'BH',
  347. )
  348. );
  349. $wpdb->update(
  350. $wpdb->usermeta,
  351. array(
  352. 'meta_value' => 'BA',
  353. ),
  354. array(
  355. 'meta_key' => 'billing_state',
  356. 'meta_value' => 'BH',
  357. )
  358. );
  359. $wpdb->update(
  360. $wpdb->usermeta,
  361. array(
  362. 'meta_value' => 'BA',
  363. ),
  364. array(
  365. 'meta_key' => 'shipping_state',
  366. 'meta_value' => 'BH',
  367. )
  368. );
  369. // phpcs:enable WordPress.DB.SlowDBQuery
  370. }
  371. /**
  372. * Update DB version for 2.0.9
  373. *
  374. * @return void
  375. */
  376. function wc_update_209_db_version() {
  377. WC_Install::update_db_version( '2.0.9' );
  378. }
  379. /**
  380. * Remove pages for 2.1
  381. *
  382. * @return void
  383. */
  384. function wc_update_210_remove_pages() {
  385. // Pages no longer used.
  386. wp_trash_post( get_option( 'woocommerce_pay_page_id' ) );
  387. wp_trash_post( get_option( 'woocommerce_thanks_page_id' ) );
  388. wp_trash_post( get_option( 'woocommerce_view_order_page_id' ) );
  389. wp_trash_post( get_option( 'woocommerce_change_password_page_id' ) );
  390. wp_trash_post( get_option( 'woocommerce_edit_address_page_id' ) );
  391. wp_trash_post( get_option( 'woocommerce_lost_password_page_id' ) );
  392. }
  393. /**
  394. * Update file paths to support multiple files for 2.1
  395. *
  396. * @return void
  397. */
  398. function wc_update_210_file_paths() {
  399. global $wpdb;
  400. // Upgrade file paths to support multiple file paths + names etc.
  401. $existing_file_paths = $wpdb->get_results( "SELECT meta_value, meta_id FROM {$wpdb->postmeta} WHERE meta_key = '_file_paths' AND meta_value != '';" );
  402. if ( $existing_file_paths ) {
  403. foreach ( $existing_file_paths as $existing_file_path ) {
  404. $needs_update = false;
  405. $new_value = array();
  406. $value = maybe_unserialize( trim( $existing_file_path->meta_value ) );
  407. if ( $value ) {
  408. foreach ( $value as $key => $file ) {
  409. if ( ! is_array( $file ) ) {
  410. $needs_update = true;
  411. $new_value[ $key ] = array(
  412. 'file' => $file,
  413. 'name' => wc_get_filename_from_url( $file ),
  414. );
  415. } else {
  416. $new_value[ $key ] = $file;
  417. }
  418. }
  419. if ( $needs_update ) {
  420. // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize
  421. $new_value = serialize( $new_value );
  422. $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->postmeta} SET meta_key = %s, meta_value = %s WHERE meta_id = %d", '_downloadable_files', $new_value, $existing_file_path->meta_id ) );
  423. }
  424. }
  425. }
  426. }
  427. }
  428. /**
  429. * Update DB version for 2.1
  430. *
  431. * @return void
  432. */
  433. function wc_update_210_db_version() {
  434. WC_Install::update_db_version( '2.1.0' );
  435. }
  436. /**
  437. * Update shipping options for 2.2
  438. *
  439. * @return void
  440. */
  441. function wc_update_220_shipping() {
  442. $woocommerce_ship_to_destination = 'shipping';
  443. if ( get_option( 'woocommerce_ship_to_billing_address_only' ) === 'yes' ) {
  444. $woocommerce_ship_to_destination = 'billing_only';
  445. } elseif ( get_option( 'woocommerce_ship_to_billing' ) === 'yes' ) {
  446. $woocommerce_ship_to_destination = 'billing';
  447. }
  448. add_option( 'woocommerce_ship_to_destination', $woocommerce_ship_to_destination, '', 'no' );
  449. }
  450. /**
  451. * Update order statuses for 2.2
  452. *
  453. * @return void
  454. */
  455. function wc_update_220_order_status() {
  456. global $wpdb;
  457. $wpdb->query(
  458. "UPDATE {$wpdb->posts} as posts
  459. LEFT JOIN {$wpdb->term_relationships} AS rel ON posts.ID = rel.object_id
  460. LEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id )
  461. LEFT JOIN {$wpdb->terms} AS term USING( term_id )
  462. SET posts.post_status = 'wc-pending'
  463. WHERE posts.post_type = 'shop_order'
  464. AND posts.post_status = 'publish'
  465. AND tax.taxonomy = 'shop_order_status'
  466. AND term.slug LIKE 'pending%';"
  467. );
  468. $wpdb->query(
  469. "UPDATE {$wpdb->posts} as posts
  470. LEFT JOIN {$wpdb->term_relationships} AS rel ON posts.ID = rel.object_id
  471. LEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id )
  472. LEFT JOIN {$wpdb->terms} AS term USING( term_id )
  473. SET posts.post_status = 'wc-processing'
  474. WHERE posts.post_type = 'shop_order'
  475. AND posts.post_status = 'publish'
  476. AND tax.taxonomy = 'shop_order_status'
  477. AND term.slug LIKE 'processing%';"
  478. );
  479. $wpdb->query(
  480. "UPDATE {$wpdb->posts} as posts
  481. LEFT JOIN {$wpdb->term_relationships} AS rel ON posts.ID = rel.object_id
  482. LEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id )
  483. LEFT JOIN {$wpdb->terms} AS term USING( term_id )
  484. SET posts.post_status = 'wc-on-hold'
  485. WHERE posts.post_type = 'shop_order'
  486. AND posts.post_status = 'publish'
  487. AND tax.taxonomy = 'shop_order_status'
  488. AND term.slug LIKE 'on-hold%';"
  489. );
  490. $wpdb->query(
  491. "UPDATE {$wpdb->posts} as posts
  492. LEFT JOIN {$wpdb->term_relationships} AS rel ON posts.ID = rel.object_id
  493. LEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id )
  494. LEFT JOIN {$wpdb->terms} AS term USING( term_id )
  495. SET posts.post_status = 'wc-completed'
  496. WHERE posts.post_type = 'shop_order'
  497. AND posts.post_status = 'publish'
  498. AND tax.taxonomy = 'shop_order_status'
  499. AND term.slug LIKE 'completed%';"
  500. );
  501. $wpdb->query(
  502. "UPDATE {$wpdb->posts} as posts
  503. LEFT JOIN {$wpdb->term_relationships} AS rel ON posts.ID = rel.object_id
  504. LEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id )
  505. LEFT JOIN {$wpdb->terms} AS term USING( term_id )
  506. SET posts.post_status = 'wc-cancelled'
  507. WHERE posts.post_type = 'shop_order'
  508. AND posts.post_status = 'publish'
  509. AND tax.taxonomy = 'shop_order_status'
  510. AND term.slug LIKE 'cancelled%';"
  511. );
  512. $wpdb->query(
  513. "UPDATE {$wpdb->posts} as posts
  514. LEFT JOIN {$wpdb->term_relationships} AS rel ON posts.ID = rel.object_id
  515. LEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id )
  516. LEFT JOIN {$wpdb->terms} AS term USING( term_id )
  517. SET posts.post_status = 'wc-refunded'
  518. WHERE posts.post_type = 'shop_order'
  519. AND posts.post_status = 'publish'
  520. AND tax.taxonomy = 'shop_order_status'
  521. AND term.slug LIKE 'refunded%';"
  522. );
  523. $wpdb->query(
  524. "UPDATE {$wpdb->posts} as posts
  525. LEFT JOIN {$wpdb->term_relationships} AS rel ON posts.ID = rel.object_id
  526. LEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id )
  527. LEFT JOIN {$wpdb->terms} AS term USING( term_id )
  528. SET posts.post_status = 'wc-failed'
  529. WHERE posts.post_type = 'shop_order'
  530. AND posts.post_status = 'publish'
  531. AND tax.taxonomy = 'shop_order_status'
  532. AND term.slug LIKE 'failed%';"
  533. );
  534. }
  535. /**
  536. * Update variations for 2.2
  537. *
  538. * @return void
  539. */
  540. function wc_update_220_variations() {
  541. global $wpdb;
  542. // Update variations which manage stock.
  543. $update_variations = $wpdb->get_results(
  544. "SELECT DISTINCT posts.ID AS variation_id, posts.post_parent AS variation_parent FROM {$wpdb->posts} as posts
  545. LEFT OUTER JOIN {$wpdb->postmeta} AS postmeta ON posts.ID = postmeta.post_id AND postmeta.meta_key = '_stock'
  546. LEFT OUTER JOIN {$wpdb->postmeta} as postmeta2 ON posts.ID = postmeta2.post_id AND postmeta2.meta_key = '_manage_stock'
  547. WHERE posts.post_type = 'product_variation'
  548. AND postmeta.meta_value IS NOT NULL
  549. AND postmeta.meta_value != ''
  550. AND postmeta2.meta_value IS NULL"
  551. );
  552. foreach ( $update_variations as $variation ) {
  553. $parent_backorders = get_post_meta( $variation->variation_parent, '_backorders', true );
  554. add_post_meta( $variation->variation_id, '_manage_stock', 'yes', true );
  555. add_post_meta( $variation->variation_id, '_backorders', $parent_backorders ? $parent_backorders : 'no', true );
  556. }
  557. }
  558. /**
  559. * Update attributes for 2.2
  560. *
  561. * @return void
  562. */
  563. function wc_update_220_attributes() {
  564. global $wpdb;
  565. // Update taxonomy names with correct sanitized names.
  566. $attribute_taxonomies = $wpdb->get_results( 'SELECT attribute_name, attribute_id FROM ' . $wpdb->prefix . 'woocommerce_attribute_taxonomies' );
  567. foreach ( $attribute_taxonomies as $attribute_taxonomy ) {
  568. $sanitized_attribute_name = wc_sanitize_taxonomy_name( $attribute_taxonomy->attribute_name );
  569. if ( $sanitized_attribute_name !== $attribute_taxonomy->attribute_name ) {
  570. if ( ! $wpdb->get_var( $wpdb->prepare( "SELECT 1=1 FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_name = %s;", $sanitized_attribute_name ) ) ) {
  571. // Update attribute.
  572. $wpdb->update(
  573. "{$wpdb->prefix}woocommerce_attribute_taxonomies",
  574. array(
  575. 'attribute_name' => $sanitized_attribute_name,
  576. ),
  577. array(
  578. 'attribute_id' => $attribute_taxonomy->attribute_id,
  579. )
  580. );
  581. // Update terms.
  582. $wpdb->update(
  583. $wpdb->term_taxonomy,
  584. array( 'taxonomy' => wc_attribute_taxonomy_name( $sanitized_attribute_name ) ),
  585. array( 'taxonomy' => 'pa_' . $attribute_taxonomy->attribute_name )
  586. );
  587. }
  588. }
  589. }
  590. delete_transient( 'wc_attribute_taxonomies' );
  591. WC_Cache_Helper::invalidate_cache_group( 'woocommerce-attributes' );
  592. }
  593. /**
  594. * Update DB version for 2.2
  595. *
  596. * @return void
  597. */
  598. function wc_update_220_db_version() {
  599. WC_Install::update_db_version( '2.2.0' );
  600. }
  601. /**
  602. * Update options for 2.3
  603. *
  604. * @return void
  605. */
  606. function wc_update_230_options() {
  607. // _money_spent and _order_count may be out of sync - clear them
  608. delete_metadata( 'user', 0, '_money_spent', '', true );
  609. delete_metadata( 'user', 0, '_order_count', '', true );
  610. delete_metadata( 'user', 0, '_last_order', '', true );
  611. // To prevent taxes being hidden when using a default 'no address' in a store with tax inc prices, set the woocommerce_default_customer_address to use the store base address by default.
  612. if ( '' === get_option( 'woocommerce_default_customer_address', false ) && wc_prices_include_tax() ) {
  613. update_option( 'woocommerce_default_customer_address', 'base' );
  614. }
  615. }
  616. /**
  617. * Update DB version for 2.3
  618. *
  619. * @return void
  620. */
  621. function wc_update_230_db_version() {
  622. WC_Install::update_db_version( '2.3.0' );
  623. }
  624. /**
  625. * Update calc discount options for 2.4
  626. *
  627. * @return void
  628. */
  629. function wc_update_240_options() {
  630. /**
  631. * Coupon discount calculations.
  632. * Maintain the old coupon logic for upgrades.
  633. */
  634. update_option( 'woocommerce_calc_discounts_sequentially', 'yes' );
  635. }
  636. /**
  637. * Update shipping methods for 2.4
  638. *
  639. * @return void
  640. */
  641. function wc_update_240_shipping_methods() {
  642. /**
  643. * Flat Rate Shipping.
  644. * Update legacy options to new math based options.
  645. */
  646. $shipping_methods = array(
  647. 'woocommerce_flat_rates' => new WC_Shipping_Legacy_Flat_Rate(),
  648. 'woocommerce_international_delivery_flat_rates' => new WC_Shipping_Legacy_International_Delivery(),
  649. );
  650. foreach ( $shipping_methods as $flat_rate_option_key => $shipping_method ) {
  651. // Stop this running more than once if routine is repeated.
  652. if ( version_compare( $shipping_method->get_option( 'version', 0 ), '2.4.0', '<' ) ) {
  653. $shipping_classes = WC()->shipping()->get_shipping_classes();
  654. $has_classes = count( $shipping_classes ) > 0;
  655. $cost_key = $has_classes ? 'no_class_cost' : 'cost';
  656. $min_fee = $shipping_method->get_option( 'minimum_fee' );
  657. $math_cost_strings = array(
  658. 'cost' => array(),
  659. 'no_class_cost' => array(),
  660. );
  661. $math_cost_strings[ $cost_key ][] = $shipping_method->get_option( 'cost' );
  662. $fee = $shipping_method->get_option( 'fee' );
  663. if ( $fee ) {
  664. $math_cost_strings[ $cost_key ][] = strstr( $fee, '%' ) ? '[fee percent="' . str_replace( '%', '', $fee ) . '" min="' . esc_attr( $min_fee ) . '"]' : $fee;
  665. }
  666. foreach ( $shipping_classes as $shipping_class ) {
  667. $rate_key = 'class_cost_' . $shipping_class->slug;
  668. $math_cost_strings[ $rate_key ] = $math_cost_strings['no_class_cost'];
  669. }
  670. $flat_rates = array_filter( (array) get_option( $flat_rate_option_key, array() ) );
  671. if ( $flat_rates ) {
  672. foreach ( $flat_rates as $shipping_class => $rate ) {
  673. $rate_key = 'class_cost_' . $shipping_class;
  674. if ( $rate['cost'] || $rate['fee'] ) {
  675. $math_cost_strings[ $rate_key ][] = $rate['cost'];
  676. $math_cost_strings[ $rate_key ][] = strstr( $rate['fee'], '%' ) ? '[fee percent="' . str_replace( '%', '', $rate['fee'] ) . '" min="' . esc_attr( $min_fee ) . '"]' : $rate['fee'];
  677. }
  678. }
  679. }
  680. if ( 'item' === $shipping_method->type ) {
  681. foreach ( $math_cost_strings as $key => $math_cost_string ) {
  682. $math_cost_strings[ $key ] = array_filter( array_map( 'trim', $math_cost_strings[ $key ] ) );
  683. if ( ! empty( $math_cost_strings[ $key ] ) ) {
  684. $last_key = max( 0, count( $math_cost_strings[ $key ] ) - 1 );
  685. $math_cost_strings[ $key ][0] = '( ' . $math_cost_strings[ $key ][0];
  686. $math_cost_strings[ $key ][ $last_key ] .= ' ) * [qty]';
  687. }
  688. }
  689. }
  690. $math_cost_strings['cost'][] = $shipping_method->get_option( 'cost_per_order' );
  691. // Save settings.
  692. foreach ( $math_cost_strings as $option_id => $math_cost_string ) {
  693. $shipping_method->settings[ $option_id ] = implode( ' + ', array_filter( $math_cost_string ) );
  694. }
  695. $shipping_method->settings['version'] = '2.4.0';
  696. $shipping_method->settings['type'] = 'item' === $shipping_method->settings['type'] ? 'class' : $shipping_method->settings['type'];
  697. update_option( $shipping_method->plugin_id . $shipping_method->id . '_settings', $shipping_method->settings );
  698. }
  699. }
  700. }
  701. /**
  702. * Update API keys for 2.4
  703. *
  704. * @return void
  705. */
  706. function wc_update_240_api_keys() {
  707. global $wpdb;
  708. /**
  709. * Update the old user API keys to the new Apps keys.
  710. */
  711. $api_users = $wpdb->get_results( "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = 'woocommerce_api_consumer_key'" );
  712. $apps_keys = array();
  713. // Get user data.
  714. foreach ( $api_users as $_user ) {
  715. $user = get_userdata( $_user->user_id );
  716. $apps_keys[] = array(
  717. 'user_id' => $user->ID,
  718. 'permissions' => $user->woocommerce_api_key_permissions,
  719. 'consumer_key' => wc_api_hash( $user->woocommerce_api_consumer_key ),
  720. 'consumer_secret' => $user->woocommerce_api_consumer_secret,
  721. 'truncated_key' => substr( $user->woocommerce_api_consumer_secret, -7 ),
  722. );
  723. }
  724. if ( ! empty( $apps_keys ) ) {
  725. // Create new apps.
  726. foreach ( $apps_keys as $app ) {
  727. $wpdb->insert(
  728. $wpdb->prefix . 'woocommerce_api_keys',
  729. $app,
  730. array(
  731. '%d',
  732. '%s',
  733. '%s',
  734. '%s',
  735. '%s',
  736. )
  737. );
  738. }
  739. // Delete old user keys from usermeta.
  740. foreach ( $api_users as $_user ) {
  741. $user_id = intval( $_user->user_id );
  742. delete_user_meta( $user_id, 'woocommerce_api_consumer_key' );
  743. delete_user_meta( $user_id, 'woocommerce_api_consumer_secret' );
  744. delete_user_meta( $user_id, 'woocommerce_api_key_permissions' );
  745. }
  746. }
  747. }
  748. /**
  749. * Update webhooks for 2.4
  750. *
  751. * @return void
  752. */
  753. function wc_update_240_webhooks() {
  754. // phpcs:disable WordPress.DB.SlowDBQuery
  755. /**
  756. * Webhooks.
  757. * Make sure order.update webhooks get the woocommerce_order_edit_status hook.
  758. */
  759. $order_update_webhooks = get_posts(
  760. array(
  761. 'posts_per_page' => -1,
  762. 'post_type' => 'shop_webhook',
  763. 'meta_key' => '_topic',
  764. 'meta_value' => 'order.updated',
  765. )
  766. );
  767. foreach ( $order_update_webhooks as $order_update_webhook ) {
  768. $webhook = new WC_Webhook( $order_update_webhook->ID );
  769. $webhook->set_topic( 'order.updated' );
  770. }
  771. // phpcs:enable WordPress.DB.SlowDBQuery
  772. }
  773. /**
  774. * Update refunds for 2.4
  775. *
  776. * @return void
  777. */
  778. function wc_update_240_refunds() {
  779. global $wpdb;
  780. /**
  781. * Refunds for full refunded orders.
  782. * Update fully refunded orders to ensure they have a refund line item so reports add up.
  783. */
  784. $refunded_orders = get_posts(
  785. array(
  786. 'posts_per_page' => -1,
  787. 'post_type' => 'shop_order',
  788. 'post_status' => array( 'wc-refunded' ),
  789. )
  790. );
  791. // Ensure emails are disabled during this update routine.
  792. remove_all_actions( 'woocommerce_order_status_refunded_notification' );
  793. remove_all_actions( 'woocommerce_order_partially_refunded_notification' );
  794. remove_action( 'woocommerce_order_status_refunded', array( 'WC_Emails', 'send_transactional_email' ) );
  795. remove_action( 'woocommerce_order_partially_refunded', array( 'WC_Emails', 'send_transactional_email' ) );
  796. foreach ( $refunded_orders as $refunded_order ) {
  797. $order_total = get_post_meta( $refunded_order->ID, '_order_total', true );
  798. $refunded_total = $wpdb->get_var(
  799. $wpdb->prepare(
  800. "SELECT SUM( postmeta.meta_value )
  801. FROM $wpdb->postmeta AS postmeta
  802. INNER JOIN $wpdb->posts AS posts ON ( posts.post_type = 'shop_order_refund' AND posts.post_parent = %d )
  803. WHERE postmeta.meta_key = '_refund_amount'
  804. AND postmeta.post_id = posts.ID",
  805. $refunded_order->ID
  806. )
  807. );
  808. if ( $order_total > $refunded_total ) {
  809. wc_create_refund(
  810. array(
  811. 'amount' => $order_total - $refunded_total,
  812. 'reason' => __( 'Order fully refunded', 'woocommerce' ),
  813. 'order_id' => $refunded_order->ID,
  814. 'line_items' => array(),
  815. 'date' => $refunded_order->post_modified,
  816. )
  817. );
  818. }
  819. }
  820. wc_delete_shop_order_transients();
  821. }
  822. /**
  823. * Update DB version for 2.4
  824. *
  825. * @return void
  826. */
  827. function wc_update_240_db_version() {
  828. WC_Install::update_db_version( '2.4.0' );
  829. }
  830. /**
  831. * Update variations for 2.4.1
  832. *
  833. * @return void
  834. */
  835. function wc_update_241_variations() {
  836. global $wpdb;
  837. // Select variations that don't have any _stock_status implemented on WooCommerce 2.2.
  838. $update_variations = $wpdb->get_results(
  839. "SELECT DISTINCT posts.ID AS variation_id, posts.post_parent AS variation_parent
  840. FROM {$wpdb->posts} as posts
  841. LEFT OUTER JOIN {$wpdb->postmeta} AS postmeta ON posts.ID = postmeta.post_id AND postmeta.meta_key = '_stock_status'
  842. WHERE posts.post_type = 'product_variation'
  843. AND postmeta.meta_value IS NULL"
  844. );
  845. foreach ( $update_variations as $variation ) {
  846. // Get the parent _stock_status.
  847. $parent_stock_status = get_post_meta( $variation->variation_parent, '_stock_status', true );
  848. // Set the _stock_status.
  849. add_post_meta( $variation->variation_id, '_stock_status', $parent_stock_status ? $parent_stock_status : 'instock', true );
  850. // Delete old product children array.
  851. delete_transient( 'wc_product_children_' . $variation->variation_parent );
  852. }
  853. // Invalidate old transients such as wc_var_price.
  854. WC_Cache_Helper::get_transient_version( 'product', true );
  855. }
  856. /**
  857. * Update DB version for 2.4.1
  858. *
  859. * @return void
  860. */
  861. function wc_update_241_db_version() {
  862. WC_Install::update_db_version( '2.4.1' );
  863. }
  864. /**
  865. * Update currency settings for 2.5
  866. *
  867. * @return void
  868. */
  869. function wc_update_250_currency() {
  870. global $wpdb;
  871. // Fix currency settings for LAK currency.
  872. $current_currency = get_option( 'woocommerce_currency' );
  873. if ( 'KIP' === $current_currency ) {
  874. update_option( 'woocommerce_currency', 'LAK' );
  875. }
  876. // phpcs:disable WordPress.DB.SlowDBQuery
  877. // Update LAK currency code.
  878. $wpdb->update(
  879. $wpdb->postmeta,
  880. array(
  881. 'meta_value' => 'LAK',
  882. ),
  883. array(
  884. 'meta_key' => '_order_currency',
  885. 'meta_value' => 'KIP',
  886. )
  887. );
  888. // phpcs:enable WordPress.DB.SlowDBQuery
  889. }
  890. /**
  891. * Update DB version for 2.5
  892. *
  893. * @return void
  894. */
  895. function wc_update_250_db_version() {
  896. WC_Install::update_db_version( '2.5.0' );
  897. }
  898. /**
  899. * Update ship to countries options for 2.6
  900. *
  901. * @return void
  902. */
  903. function wc_update_260_options() {
  904. // woocommerce_calc_shipping option has been removed in 2.6.
  905. if ( 'no' === get_option( 'woocommerce_calc_shipping' ) ) {
  906. update_option( 'woocommerce_ship_to_countries', 'disabled' );
  907. }
  908. WC_Admin_Notices::add_notice( 'legacy_shipping' );
  909. }
  910. /**
  911. * Update term meta for 2.6
  912. *
  913. * @return void
  914. */
  915. function wc_update_260_termmeta() {
  916. global $wpdb;
  917. /**
  918. * Migrate term meta to WordPress tables.
  919. */
  920. if ( get_option( 'db_version' ) >= 34370 && $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}woocommerce_termmeta';" ) ) {
  921. if ( $wpdb->query( "INSERT INTO {$wpdb->termmeta} ( term_id, meta_key, meta_value ) SELECT woocommerce_term_id, meta_key, meta_value FROM {$wpdb->prefix}woocommerce_termmeta;" ) ) {
  922. $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}woocommerce_termmeta" );
  923. wp_cache_flush();
  924. }
  925. }
  926. }
  927. /**
  928. * Update zones for 2.6
  929. *
  930. * @return void
  931. */
  932. function wc_update_260_zones() {
  933. global $wpdb;
  934. /**
  935. * Old (table rate) shipping zones to new core shipping zones migration.
  936. * zone_enabled and zone_type are no longer used, but it's safe to leave them be.
  937. */
  938. if ( $wpdb->get_var( "SHOW COLUMNS FROM `{$wpdb->prefix}woocommerce_shipping_zones` LIKE 'zone_enabled';" ) ) {
  939. $wpdb->query( "ALTER TABLE {$wpdb->prefix}woocommerce_shipping_zones CHANGE `zone_type` `zone_type` VARCHAR(40) NOT NULL DEFAULT '';" );
  940. $wpdb->query( "ALTER TABLE {$wpdb->prefix}woocommerce_shipping_zones CHANGE `zone_enabled` `zone_enabled` INT(1) NOT NULL DEFAULT 1;" );
  941. }
  942. }
  943. /**
  944. * Update zone methods for 2.6
  945. *
  946. * @return void
  947. */
  948. function wc_update_260_zone_methods() {
  949. global $wpdb;
  950. /**
  951. * Shipping zones in WC 2.6.0 use a table named woocommerce_shipping_zone_methods.
  952. * Migrate the old data out of woocommerce_shipping_zone_shipping_methods into the new table and port over any known options (used by table rates and flat rate boxes).
  953. */
  954. if ( $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}woocommerce_shipping_zone_shipping_methods';" ) ) {
  955. $old_methods = $wpdb->get_results( "SELECT zone_id, shipping_method_type, shipping_method_order, shipping_method_id FROM {$wpdb->prefix}woocommerce_shipping_zone_shipping_methods;" );
  956. if ( $old_methods ) {
  957. $max_new_id = $wpdb->get_var( "SELECT MAX(instance_id) FROM {$wpdb->prefix}woocommerce_shipping_zone_methods" );
  958. $max_old_id = $wpdb->get_var( "SELECT MAX(shipping_method_id) FROM {$wpdb->prefix}woocommerce_shipping_zone_shipping_methods" );
  959. // Avoid ID conflicts.
  960. $wpdb->query( $wpdb->prepare( "ALTER TABLE {$wpdb->prefix}woocommerce_shipping_zone_methods AUTO_INCREMENT = %d;", max( $max_new_id, $max_old_id ) + 1 ) );
  961. // Store changes.
  962. $changes = array();
  963. // Move data.
  964. foreach ( $old_methods as $old_method ) {
  965. $wpdb->insert(
  966. $wpdb->prefix . 'woocommerce_shipping_zone_methods',
  967. array(
  968. 'zone_id' => $old_method->zone_id,
  969. 'method_id' => $old_method->shipping_method_type,
  970. 'method_order' => $old_method->shipping_method_order,
  971. )
  972. );
  973. $new_instance_id = $wpdb->insert_id;
  974. // Move main settings.
  975. $older_settings_key = 'woocommerce_' . $old_method->shipping_method_type . '-' . $old_method->shipping_method_id . '_settings';
  976. $old_settings_key = 'woocommerce_' . $old_method->shipping_method_type . '_' . $old_method->shipping_method_id . '_settings';
  977. add_option( 'woocommerce_' . $old_method->shipping_method_type . '_' . $new_instance_id . '_settings', get_option( $old_settings_key, get_option( $older_settings_key ) ) );
  978. // Handling for table rate and flat rate box shipping.
  979. if ( 'table_rate' === $old_method->shipping_method_type ) {
  980. // Move priority settings.
  981. add_option( 'woocommerce_table_rate_default_priority_' . $new_instance_id, get_option( 'woocommerce_table_rate_default_priority_' . $old_method->shipping_method_id ) );
  982. add_option( 'woocommerce_table_rate_priorities_' . $new_instance_id, get_option( 'woocommerce_table_rate_priorities_' . $old_method->shipping_method_id ) );
  983. // Move rates.
  984. $wpdb->update(
  985. $wpdb->prefix . 'woocommerce_shipping_table_rates',
  986. array(
  987. 'shipping_method_id' => $new_instance_id,
  988. ),
  989. array(
  990. 'shipping_method_id' => $old_method->shipping_method_id,
  991. )
  992. );
  993. } elseif ( 'flat_rate_boxes' === $old_method->shipping_method_type ) {
  994. $wpdb->update(
  995. $wpdb->prefix . 'woocommerce_shipping_flat_rate_boxes',
  996. array(
  997. 'shipping_method_id' => $new_instance_id,
  998. ),
  999. array(
  1000. 'shipping_method_id' => $old_method->shipping_method_id,
  1001. )
  1002. );
  1003. }
  1004. $changes[ $old_method->shipping_method_id ] = $new_instance_id;
  1005. }
  1006. // $changes contains keys (old method ids) and values (new instance ids) if extra processing is needed in plugins.
  1007. // Store this to an option so extensions can pick it up later, then fire an action.
  1008. update_option( 'woocommerce_updated_instance_ids', $changes );
  1009. do_action( 'woocommerce_updated_instance_ids', $changes );
  1010. }
  1011. }
  1012. // Change ranges used to ...
  1013. $wpdb->query( "UPDATE {$wpdb->prefix}woocommerce_shipping_zone_locations SET location_code = REPLACE( location_code, '-', '...' );" );
  1014. }
  1015. /**
  1016. * Update refunds for 2.6
  1017. *
  1018. * @return void
  1019. */
  1020. function wc_update_260_refunds() {
  1021. global $wpdb;
  1022. /**
  1023. * Refund item qty should be negative.
  1024. */
  1025. $wpdb->query(
  1026. "UPDATE {$wpdb->prefix}woocommerce_order_itemmeta as item_meta
  1027. LEFT JOIN {$wpdb->prefix}woocommerce_order_items as items ON item_meta.order_item_id = items.order_item_id
  1028. LEFT JOIN {$wpdb->posts} as posts ON items.order_id = posts.ID
  1029. SET item_meta.meta_value = item_meta.meta_value * -1
  1030. WHERE item_meta.meta_value > 0 AND item_meta.meta_key = '_qty' AND posts.post_type = 'shop_order_refund'"
  1031. );
  1032. }
  1033. /**
  1034. * Update DB version for 2.6
  1035. *
  1036. * @return void
  1037. */
  1038. function wc_update_260_db_version() {
  1039. WC_Install::update_db_version( '2.6.0' );
  1040. }
  1041. /**
  1042. * Update webhooks for 3.0
  1043. *
  1044. * @return void
  1045. */
  1046. function wc_update_300_webhooks() {
  1047. // phpcs:disable WordPress.DB.SlowDBQuery
  1048. /**
  1049. * Make sure product.update webhooks get the woocommerce_product_quick_edit_save
  1050. * and woocommerce_product_bulk_edit_save hooks.
  1051. */
  1052. $product_update_webhooks = get_posts(
  1053. array(
  1054. 'posts_per_page' => -1,
  1055. 'post_type' => 'shop_webhook',
  1056. 'meta_key' => '_topic',
  1057. 'meta_value' => 'product.updated',
  1058. )
  1059. );
  1060. foreach ( $product_update_webhooks as $product_update_webhook ) {
  1061. $webhook = new WC_Webhook( $product_update_webhook->ID );
  1062. $webhook->set_topic( 'product.updated' );
  1063. }
  1064. // phpcs:enable WordPress.DB.SlowDBQuery
  1065. }
  1066. /**
  1067. * Add an index to the field comment_type to improve the response time of the query
  1068. * used by WC_Comments::wp_count_comments() to get the number of comments by type.
  1069. */
  1070. function wc_update_300_comment_type_index() {
  1071. global $wpdb;
  1072. $index_exists = $wpdb->get_row( "SHOW INDEX FROM {$wpdb->comments} WHERE column_name = 'comment_type' and key_name = 'woo_idx_comment_type'" );
  1073. if ( is_null( $index_exists ) ) {
  1074. // Add an index to the field comment_type to improve the response time of the query
  1075. // used by WC_Comments::wp_count_comments() to get the number of comments by type.
  1076. $wpdb->query( "ALTER TABLE {$wpdb->comments} ADD INDEX woo_idx_comment_type (comment_type)" );
  1077. }
  1078. }
  1079. /**
  1080. * Update grouped products for 3.0
  1081. *
  1082. * @return void
  1083. */
  1084. function wc_update_300_grouped_products() {
  1085. global $wpdb;
  1086. $parents = $wpdb->get_col( "SELECT DISTINCT( post_parent ) FROM {$wpdb->posts} WHERE post_parent > 0 AND post_type = 'product';" );
  1087. foreach ( $parents as $parent_id ) {
  1088. $parent = wc_get_product( $parent_id );
  1089. if ( $parent && $parent->is_type( 'grouped' ) ) {
  1090. $children_ids = get_posts(
  1091. array(
  1092. 'post_parent' => $parent_id,
  1093. 'posts_per_page' => -1,
  1094. 'post_type' => 'product',
  1095. 'fields' => 'ids',
  1096. )
  1097. );
  1098. update_post_meta( $parent_id, '_children', $children_ids );
  1099. // Update children to remove the parent.
  1100. $wpdb->update(
  1101. $wpdb->posts,
  1102. array(
  1103. 'post_parent' => 0,
  1104. ),
  1105. array(
  1106. 'post_parent' => $parent_id,
  1107. )
  1108. );
  1109. }
  1110. }
  1111. }
  1112. /**
  1113. * Update shipping tax classes for 3.0
  1114. *
  1115. * @return void
  1116. */
  1117. function wc_update_300_settings() {
  1118. $woocommerce_shipping_tax_class = get_option( 'woocommerce_shipping_tax_class' );
  1119. if ( '' === $woocommerce_shipping_tax_class ) {
  1120. update_option( 'woocommerce_shipping_tax_class', 'inherit' );
  1121. } elseif ( 'standard' === $woocommerce_shipping_tax_class ) {
  1122. update_option( 'woocommerce_shipping_tax_class', '' );
  1123. }
  1124. }
  1125. /**
  1126. * Convert meta values into term for product visibility.
  1127. */
  1128. function wc_update_300_product_visibility() {
  1129. global $wpdb;
  1130. WC_Install::create_terms();
  1131. $featured_term = get_term_by( 'name', 'featured', 'product_visibility' );
  1132. if ( $featured_term ) {
  1133. $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO {$wpdb->term_relationships} SELECT post_id, %d, 0 FROM {$wpdb->postmeta} WHERE meta_key = '_featured' AND meta_value = 'yes';", $featured_term->term_taxonomy_id ) );
  1134. }
  1135. $exclude_search_term = get_term_by( 'name', 'exclude-from-search', 'product_visibility' );
  1136. if ( $exclude_search_term ) {
  1137. $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO {$wpdb->term_relationships} SELECT post_id, %d, 0 FROM {$wpdb->postmeta} WHERE meta_key = '_visibility' AND meta_value IN ('hidden', 'catalog');", $exclude_search_term->term_taxonomy_id ) );
  1138. }
  1139. $exclude_catalog_term = get_term_by( 'name', 'exclude-from-catalog', 'product_visibility' );
  1140. if ( $exclude_catalog_term ) {
  1141. $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO {$wpdb->term_relationships} SELECT post_id, %d, 0 FROM {$wpdb->postmeta} WHERE meta_key = '_visibility' AND meta_value IN ('hidden', 'search');", $exclude_catalog_term->term_taxonomy_id ) );
  1142. }
  1143. $outofstock_term = get_term_by( 'name', 'outofstock', 'product_visibility' );
  1144. if ( $outofstock_term ) {
  1145. $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO {$wpdb->term_relationships} SELECT post_id, %d, 0 FROM {$wpdb->postmeta} WHERE meta_key = '_stock_status' AND meta_value = 'outofstock';", $outofstock_term->term_taxonomy_id ) );
  1146. }
  1147. $rating_term = get_term_by( 'name', 'rated-1', 'product_visibility' );
  1148. if ( $rating_term ) {
  1149. $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO {$wpdb->term_relationships} SELECT post_id, %d, 0 FROM {$wpdb->postmeta} WHERE meta_key = '_wc_average_rating' AND ROUND( meta_value ) = 1;", $rating_term->term_taxonomy_id ) );
  1150. }
  1151. $rating_term = get_term_by( 'name', 'rated-2', 'product_visibility' );
  1152. if ( $rating_term ) {
  1153. $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO {$wpdb->term_relationships} SELECT post_id, %d, 0 FROM {$wpdb->postmeta} WHERE meta_key = '_wc_average_rating' AND ROUND( meta_value ) = 2;", $rating_term->term_taxonomy_id ) );
  1154. }
  1155. $rating_term = get_term_by( 'name', 'rated-3', 'product_visibility' );
  1156. if ( $rating_term ) {
  1157. $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO {$wpdb->term_relationships} SELECT post_id, %d, 0 FROM {$wpdb->postmeta} WHERE meta_key = '_wc_average_rating' AND ROUND( meta_value ) = 3;", $rating_term->term_taxonomy_id ) );
  1158. }
  1159. $rating_term = get_term_by( 'name', 'rated-4', 'product_visibility' );
  1160. if ( $rating_term ) {
  1161. $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO {$wpdb->term_relationships} SELECT post_id, %d, 0 FROM {$wpdb->postmeta} WHERE meta_key = '_wc_average_rating' AND ROUND( meta_value ) = 4;", $rating_term->term_taxonomy_id ) );
  1162. }
  1163. $rating_term = get_term_by( 'name', 'rated-5', 'product_visibility' );
  1164. if ( $rating_term ) {
  1165. $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO {$wpdb->term_relationships} SELECT post_id, %d, 0 FROM {$wpdb->postmeta} WHERE meta_key = '_wc_average_rating' AND ROUND( meta_value ) = 5;", $rating_term->term_taxonomy_id ) );
  1166. }
  1167. }
  1168. /**
  1169. * Update DB Version.
  1170. */
  1171. function wc_update_300_db_version() {
  1172. WC_Install::update_db_version( '3.0.0' );
  1173. }
  1174. /**
  1175. * Add an index to the downloadable product permissions table to improve performance of update_user_by_order_id.
  1176. */
  1177. function wc_update_310_downloadable_products() {
  1178. global $wpdb;
  1179. $index_exists = $wpdb->get_row( "SHOW INDEX FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions WHERE column_name = 'order_id' and key_name = 'order_id'" );
  1180. if ( is_null( $index_exists ) ) {
  1181. $wpdb->query( "ALTER TABLE {$wpdb->prefix}woocommerce_downloadable_product_permissions ADD INDEX order_id (order_id)" );
  1182. }
  1183. }
  1184. /**
  1185. * Find old order notes and ensure they have the correct type for exclusion.
  1186. */
  1187. function wc_update_310_old_comments() {
  1188. global $wpdb;
  1189. $wpdb->query( "UPDATE $wpdb->comments comments LEFT JOIN $wpdb->posts as posts ON comments.comment_post_ID = posts.ID SET comment_type = 'order_note' WHERE posts.post_type = 'shop_order' AND comment_type = '';" );
  1190. }
  1191. /**
  1192. * Update DB Version.
  1193. */
  1194. function wc_update_310_db_version() {
  1195. WC_Install::update_db_version( '3.1.0' );
  1196. }
  1197. /**
  1198. * Update shop_manager capabilities.
  1199. */
  1200. function wc_update_312_shop_manager_capabilities() {
  1201. $role = get_role( 'shop_manager' );
  1202. $role->remove_cap( 'unfiltered_html' );
  1203. }
  1204. /**
  1205. * Update DB Version.
  1206. */
  1207. function wc_update_312_db_version() {
  1208. WC_Install::update_db_version( '3.1.2' );
  1209. }
  1210. /**
  1211. * Update state codes for Mexico.
  1212. */
  1213. function wc_update_320_mexican_states() {
  1214. global $wpdb;
  1215. $mx_states = array(
  1216. 'Distrito Federal' => 'CMX',
  1217. 'Jalisco' => 'JAL',
  1218. 'Nuevo Leon' => 'NLE',
  1219. 'Aguascalientes' => 'AGS',
  1220. 'Baja California' => 'BCN',
  1221. 'Baja California Sur' => 'BCS',
  1222. 'Campeche' => 'CAM',
  1223. 'Chiapas' => 'CHP',
  1224. 'Chihuahua' => 'CHH',
  1225. 'Coahuila' => 'COA',
  1226. 'Colima' => 'COL',
  1227. 'Durango' => 'DGO',
  1228. 'Guanajuato' => 'GTO',
  1229. 'Guerrero' => 'GRO',
  1230. 'Hidalgo' => 'HGO',
  1231. 'Estado de Mexico' => 'MEX',
  1232. 'Michoacan' => 'MIC',
  1233. 'Morelos' => 'MOR',
  1234. 'Nayarit' => 'NAY',
  1235. 'Oaxaca' => 'OAX',
  1236. 'Puebla' => 'PUE',
  1237. 'Queretaro' => 'QRO',
  1238. 'Quintana Roo' => 'ROO',
  1239. 'San Luis Potosi' => 'SLP',
  1240. 'Sinaloa' => 'SIN',
  1241. 'Sonora' => 'SON',
  1242. 'Tabasco' => 'TAB',
  1243. 'Tamaulipas' => 'TMP',
  1244. 'Tlaxcala' => 'TLA',
  1245. 'Veracruz' => 'VER',
  1246. 'Yucatan' => 'YUC',
  1247. 'Zacatecas' => 'ZAC',
  1248. );
  1249. foreach ( $mx_states as $old => $new ) {
  1250. $wpdb->query(
  1251. $wpdb->prepare(
  1252. "UPDATE $wpdb->postmeta
  1253. SET meta_value = %s
  1254. WHERE meta_key IN ( '_billing_state', '_shipping_state' )
  1255. AND meta_value = %s",
  1256. $new,
  1257. $old
  1258. )
  1259. );
  1260. $wpdb->update(
  1261. "{$wpdb->prefix}woocommerce_shipping_zone_locations",
  1262. array(
  1263. 'location_code' => 'MX:' . $new,
  1264. ),
  1265. array(
  1266. 'location_code' => 'MX:' . $old,
  1267. )
  1268. );
  1269. $wpdb->update(
  1270. "{$wpdb->prefix}woocommerce_tax_rates",
  1271. array(
  1272. 'tax_rate_state' => strtoupper( $new ),
  1273. ),
  1274. array(
  1275. 'tax_rate_state' => strtoupper( $old ),
  1276. )
  1277. );
  1278. }
  1279. }
  1280. /**
  1281. * Update DB Version.
  1282. */
  1283. function wc_update_320_db_version() {
  1284. WC_Install::update_db_version( '3.2.0' );
  1285. }
  1286. /**
  1287. * Update image settings to use new aspect ratios and widths.
  1288. */
  1289. function wc_update_330_image_options() {
  1290. $old_thumbnail_size = get_option( 'shop_catalog_image_size', array() );
  1291. $old_single_size = get_option( 'shop_single_image_size', array() );
  1292. if ( ! empty( $old_thumbnail_size['width'] ) ) {
  1293. $width = absint( $old_thumbnail_size['width'] );
  1294. $height = absint( $old_thumbnail_size['height'] );
  1295. $hard_crop = ! empty( $old_thumbnail_size['crop'] );
  1296. if ( ! $width ) {
  1297. $width = 300;
  1298. }
  1299. if ( ! $height ) {
  1300. $height = $width;
  1301. }
  1302. update_option( 'woocommerce_thumbnail_image_width', $width );
  1303. // Calculate cropping mode from old image options.
  1304. if ( ! $hard_crop ) {
  1305. update_option( 'woocommerce_thumbnail_cropping', 'uncropped' );
  1306. } elseif ( $width === $height ) {
  1307. update_option( 'woocommerce_thumbnail_cropping', '1:1' );
  1308. } else {
  1309. $ratio = $width / $height;
  1310. $fraction = wc_decimal_to_fraction( $ratio );
  1311. if ( $fraction ) {
  1312. update_option( 'woocommerce_thumbnail_cropping', 'custom' );
  1313. update_option( 'woocommerce_thumbnail_cropping_custom_width', $fraction[0] );
  1314. update_option( 'woocommerce_thumbnail_cropping_custom_height', $fraction[1] );
  1315. }
  1316. }
  1317. }
  1318. // Single is uncropped.
  1319. if ( ! empty( $old_single_size['width'] ) ) {
  1320. update_option( 'woocommerce_single_image_width', absint( $old_single_size['width'] ) );
  1321. }
  1322. }
  1323. /**
  1324. * Migrate webhooks from post type to CRUD.
  1325. */
  1326. function wc_update_330_webhooks() {
  1327. register_post_type( 'shop_webhook' );
  1328. // Map statuses from post_type to Webhooks CRUD.
  1329. $statuses = array(
  1330. 'publish' => 'active',
  1331. 'draft' => 'paused',
  1332. 'pending' => 'disabled',
  1333. );
  1334. $posts = get_posts(
  1335. array(
  1336. 'posts_per_page' => -1,
  1337. 'post_type' => 'shop_webhook',
  1338. 'post_status' => 'any',
  1339. )
  1340. );
  1341. foreach ( $posts as $post ) {
  1342. $webhook = new WC_Webhook();
  1343. $webhook->set_name( $post->post_title );
  1344. $webhook->set_status( isset( $statuses[ $post->post_status ] ) ? $statuses[ $post->post_status ] : 'disabled' );
  1345. $webhook->set_delivery_url( get_post_meta( $post->ID, '_delivery_url', true ) );
  1346. $webhook->set_secret( get_post_meta( $post->ID, '_secret', true ) );
  1347. $webhook->set_topic( get_post_meta( $post->ID, '_topic', true ) );
  1348. $webhook->set_api_version( get_post_meta( $post->ID, '_api_version', true ) );
  1349. $webhook->set_user_id( $post->post_author );
  1350. $webhook->set_pending_delivery( false );
  1351. $webhook->save();
  1352. wp_delete_post( $post->ID, true );
  1353. }
  1354. unregister_post_type( 'shop_webhook' );
  1355. }
  1356. /**
  1357. * Assign default cat to all products with no cats.
  1358. */
  1359. function wc_update_330_set_default_product_cat() {
  1360. /*
  1361. * When a product category is deleted, we need to check
  1362. * if the product has no categories assigned. Then assign
  1363. * it a default category.
  1364. */
  1365. wc_get_container()->get( AssignDefaultCategory::class )->maybe_assign_default_product_cat();
  1366. }
  1367. /**
  1368. * Update product stock status to use the new onbackorder status.
  1369. */
  1370. function wc_update_330_product_stock_status() {
  1371. global $wpdb;
  1372. if ( 'yes' !== get_option( 'woocommerce_manage_stock' ) ) {
  1373. return;
  1374. }
  1375. $min_stock_amount = (int) get_option( 'woocommerce_notify_no_stock_amount', 0 );
  1376. // Get all products that have stock management enabled, stock less than or equal to min stock amount, and backorders enabled.
  1377. $post_ids = $wpdb->get_col(
  1378. $wpdb->prepare(
  1379. "SELECT t1.post_id FROM $wpdb->postmeta t1
  1380. INNER JOIN $wpdb->postmeta t2
  1381. ON t1.post_id = t2.post_id
  1382. AND t1.meta_key = '_manage_stock' AND t1.meta_value = 'yes'
  1383. AND t2.meta_key = '_stock' AND t2.meta_value <= %d
  1384. INNER JOIN $wpdb->postmeta t3
  1385. ON t2.post_id = t3.post_id
  1386. AND t3.meta_key = '_backorders' AND ( t3.meta_value = 'yes' OR t3.meta_value = 'notify' )",
  1387. $min_stock_amount
  1388. )
  1389. );
  1390. if ( empty( $post_ids ) ) {
  1391. return;
  1392. }
  1393. $post_ids = array_map( 'absint', $post_ids );
  1394. // phpcs:disable WordPress.DB.PreparedSQL.NotPrepared
  1395. // Set the status to onbackorder for those products.
  1396. $wpdb->query(
  1397. "UPDATE $wpdb->postmeta
  1398. SET meta_value = 'onbackorder'
  1399. WHERE meta_key = '_stock_status' AND post_id IN ( " . implode( ',', $post_ids ) . ' )'
  1400. );
  1401. // phpcs:enable WordPress.DB.PreparedSQL.NotPrepared
  1402. }
  1403. /**
  1404. * Clear addons page transients
  1405. */
  1406. function wc_update_330_clear_transients() {
  1407. delete_transient( 'wc_addons_sections' );
  1408. delete_transient( 'wc_addons_featured' );
  1409. }
  1410. /**
  1411. * Set PayPal's sandbox credentials.
  1412. */
  1413. function wc_update_330_set_paypal_sandbox_credentials() {
  1414. $paypal_settings = get_option( 'woocommerce_paypal_settings' );
  1415. if ( isset( $paypal_settings['testmode'] ) && 'yes' === $paypal_settings['testmode'] ) {
  1416. foreach ( array( 'api_username', 'api_password', 'api_signature' ) as $credential ) {
  1417. if ( ! empty( $paypal_settings[ $credential ] ) ) {
  1418. $paypal_settings[ 'sandbox_' . $credential ] = $paypal_settings[ $credential ];
  1419. }
  1420. }
  1421. update_option( 'woocommerce_paypal_settings', $paypal_settings );
  1422. }
  1423. }
  1424. /**
  1425. * Update DB Version.
  1426. */
  1427. function wc_update_330_db_version() {
  1428. WC_Install::update_db_version( '3.3.0' );
  1429. }
  1430. /**
  1431. * Update state codes for Ireland and BD.
  1432. */
  1433. function wc_update_340_states() {
  1434. $country_states = array(
  1435. 'IE' => array(
  1436. 'CK' => 'CO',
  1437. 'DN' => 'D',
  1438. 'GY' => 'G',
  1439. 'TY' => 'TA',
  1440. ),
  1441. 'BD' => array(
  1442. 'BAG' => 'BD-05',
  1443. 'BAN' => 'BD-01',
  1444. 'BAR' => 'BD-02',
  1445. 'BARI' => 'BD-06',
  1446. 'BHO' => 'BD-07',
  1447. 'BOG' => 'BD-03',
  1448. 'BRA' => 'BD-04',
  1449. 'CHA' => 'BD-09',
  1450. 'CHI' => 'BD-10',
  1451. 'CHU' => 'BD-12',
  1452. 'COX' => 'BD-11',
  1453. 'COM' => 'BD-08',
  1454. 'DHA' => 'BD-13',
  1455. 'DIN' => 'BD-14',
  1456. 'FAR' => 'BD-15',
  1457. 'FEN' => 'BD-16',
  1458. 'GAI' => 'BD-19',
  1459. 'GAZI' => 'BD-18',
  1460. 'GOP' => 'BD-17',
  1461. 'HAB' => 'BD-20',
  1462. 'JAM' => 'BD-21',
  1463. 'JES' => 'BD-22',
  1464. 'JHA' => 'BD-25',
  1465. 'JHE' => 'BD-23',
  1466. 'JOY' => 'BD-24',
  1467. 'KHA' => 'BD-29',
  1468. 'KHU' => 'BD-27',
  1469. 'KIS' => 'BD-26',
  1470. 'KUR' => 'BD-28',
  1471. 'KUS' => 'BD-30',
  1472. 'LAK' => 'BD-31',
  1473. 'LAL' => 'BD-32',
  1474. 'MAD' => 'BD-36',
  1475. 'MAG' => 'BD-37',
  1476. 'MAN' => 'BD-33',
  1477. 'MEH' => 'BD-39',
  1478. 'MOU' => 'BD-38',
  1479. 'MUN' => 'BD-35',
  1480. 'MYM' => 'BD-34',
  1481. 'NAO' => 'BD-48',
  1482. 'NAR' => 'BD-43',
  1483. 'NARG' => 'BD-40',
  1484. 'NARD' => 'BD-42',
  1485. 'NAT' => 'BD-44',
  1486. 'NAW' => 'BD-45',
  1487. 'NET' => 'BD-41',
  1488. 'NIL' => 'BD-46',
  1489. 'NOA' => 'BD-47',
  1490. 'PAB' => 'BD-49',
  1491. 'PAN' => 'BD-52',
  1492. 'PAT' => 'BD-51',
  1493. 'PIR' => 'BD-50',
  1494. 'RAJB' => 'BD-53',
  1495. 'RAJ' => 'BD-54',
  1496. 'RAN' => 'BD-56',
  1497. 'RANP' => 'BD-55',
  1498. 'SAT' => 'BD-58',
  1499. 'SHA' => 'BD-57',
  1500. 'SIR' => 'BD-59',
  1501. 'SUN' => 'BD-61',
  1502. 'SYL' => 'BD-60',
  1503. 'TAN' => 'BD-63',
  1504. 'THA' => 'BD-64',
  1505. ),
  1506. );
  1507. update_option( 'woocommerce_update_340_states', $country_states );
  1508. }
  1509. /**
  1510. * Update next state in the queue.
  1511. *
  1512. * @return bool True to run again, false if completed.
  1513. */
  1514. function wc_update_340_state() {
  1515. global $wpdb;
  1516. $country_states = array_filter( (array) get_option( 'woocommerce_update_340_states', array() ) );
  1517. if ( empty( $country_states ) ) {
  1518. return false;
  1519. }
  1520. foreach ( $country_states as $country => $states ) {
  1521. foreach ( $states as $old => $new ) {
  1522. $wpdb->query(
  1523. $wpdb->prepare(
  1524. "UPDATE $wpdb->postmeta
  1525. SET meta_value = %s
  1526. WHERE meta_key IN ( '_billing_state', '_shipping_state' )
  1527. AND meta_value = %s",
  1528. $new,
  1529. $old
  1530. )
  1531. );
  1532. $wpdb->update(
  1533. "{$wpdb->prefix}woocommerce_shipping_zone_locations",
  1534. array(
  1535. 'location_code' => $country . ':' . $new,
  1536. ),
  1537. array(
  1538. 'location_code' => $country . ':' . $old,
  1539. )
  1540. );
  1541. $wpdb->update(
  1542. "{$wpdb->prefix}woocommerce_tax_rates",
  1543. array(
  1544. 'tax_rate_state' => strtoupper( $new ),
  1545. ),
  1546. array(
  1547. 'tax_rate_state' => strtoupper( $old ),
  1548. )
  1549. );
  1550. unset( $country_states[ $country ][ $old ] );
  1551. if ( empty( $country_states[ $country ] ) ) {
  1552. unset( $country_states[ $country ] );
  1553. }
  1554. break 2;
  1555. }
  1556. }
  1557. if ( ! empty( $country_states ) ) {
  1558. return update_option( 'woocommerce_update_340_states', $country_states );
  1559. }
  1560. delete_option( 'woocommerce_update_340_states' );
  1561. return false;
  1562. }
  1563. /**
  1564. * Set last active prop for users.
  1565. */
  1566. function wc_update_340_last_active() {
  1567. global $wpdb;
  1568. // @codingStandardsIgnoreStart.
  1569. $wpdb->query(
  1570. $wpdb->prepare( "
  1571. INSERT INTO {$wpdb->usermeta} (user_id, meta_key, meta_value)
  1572. SELECT DISTINCT users.ID, 'wc_last_active', %s
  1573. FROM {$wpdb->users} as users
  1574. LEFT OUTER JOIN {$wpdb->usermeta} AS usermeta ON users.ID = usermeta.user_id AND usermeta.meta_key = 'wc_last_active'
  1575. WHERE usermeta.meta_value IS NULL
  1576. ",
  1577. (string) strtotime( date( 'Y-m-d', current_time( 'timestamp', true ) ) )
  1578. )
  1579. );
  1580. // @codingStandardsIgnoreEnd.
  1581. }
  1582. /**
  1583. * Update DB Version.
  1584. */
  1585. function wc_update_340_db_version() {
  1586. WC_Install::update_db_version( '3.4.0' );
  1587. }
  1588. /**
  1589. * Remove duplicate foreign keys
  1590. *
  1591. * @return void
  1592. */
  1593. function wc_update_343_cleanup_foreign_keys() {
  1594. global $wpdb;
  1595. $results = $wpdb->get_results(
  1596. "SELECT CONSTRAINT_NAME
  1597. FROM information_schema.TABLE_CONSTRAINTS
  1598. WHERE CONSTRAINT_SCHEMA = '{$wpdb->dbname}'
  1599. AND CONSTRAINT_NAME LIKE '%wc_download_log_ib%'
  1600. AND CONSTRAINT_TYPE = 'FOREIGN KEY'
  1601. AND TABLE_NAME = '{$wpdb->prefix}wc_download_log'"
  1602. );
  1603. if ( $results ) {
  1604. foreach ( $results as $fk ) {
  1605. $wpdb->query( "ALTER TABLE {$wpdb->prefix}wc_download_log DROP FOREIGN KEY {$fk->CONSTRAINT_NAME}" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
  1606. }
  1607. }
  1608. }
  1609. /**
  1610. * Update DB version.
  1611. *
  1612. * @return void
  1613. */
  1614. function wc_update_343_db_version() {
  1615. WC_Install::update_db_version( '3.4.3' );
  1616. }
  1617. /**
  1618. * Recreate user roles so existing users will get the new capabilities.
  1619. *
  1620. * @return void
  1621. */
  1622. function wc_update_344_recreate_roles() {
  1623. WC_Install::remove_roles();
  1624. WC_Install::create_roles();
  1625. }
  1626. /**
  1627. * Update DB version.
  1628. *
  1629. * @return void
  1630. */
  1631. function wc_update_344_db_version() {
  1632. WC_Install::update_db_version( '3.4.4' );
  1633. }
  1634. /**
  1635. * Set the comment type to 'review' for product reviews that don't have a comment type.
  1636. */
  1637. function wc_update_350_reviews_comment_type() {
  1638. global $wpdb;
  1639. $wpdb->query(
  1640. "UPDATE {$wpdb->prefix}comments JOIN {$wpdb->prefix}posts ON {$wpdb->prefix}posts.ID = {$wpdb->prefix}comments.comment_post_ID AND ( {$wpdb->prefix}posts.post_type = 'product' OR {$wpdb->prefix}posts.post_type = 'product_variation' ) SET {$wpdb->prefix}comments.comment_type = 'review' WHERE {$wpdb->prefix}comments.comment_type = ''"
  1641. );
  1642. }
  1643. /**
  1644. * Update DB Version.
  1645. */
  1646. function wc_update_350_db_version() {
  1647. WC_Install::update_db_version( '3.5.0' );
  1648. }
  1649. /**
  1650. * Drop the fk_wc_download_log_permission_id FK as we use a new one with the table and blog prefix for MS compatability.
  1651. *
  1652. * @return void
  1653. */
  1654. function wc_update_352_drop_download_log_fk() {
  1655. global $wpdb;
  1656. $results = $wpdb->get_results(
  1657. "SELECT CONSTRAINT_NAME
  1658. FROM information_schema.TABLE_CONSTRAINTS
  1659. WHERE CONSTRAINT_SCHEMA = '{$wpdb->dbname}'
  1660. AND CONSTRAINT_NAME = 'fk_wc_download_log_permission_id'
  1661. AND CONSTRAINT_TYPE = 'FOREIGN KEY'
  1662. AND TABLE_NAME = '{$wpdb->prefix}wc_download_log'"
  1663. );
  1664. // We only need to drop the old key as WC_Install::create_tables() takes care of creating the new FK.
  1665. if ( $results ) {
  1666. $wpdb->query( "ALTER TABLE {$wpdb->prefix}wc_download_log DROP FOREIGN KEY fk_wc_download_log_permission_id" ); // phpcs:ignore WordPress.WP.PreparedSQL.NotPrepared
  1667. }
  1668. }
  1669. /**
  1670. * Remove edit_user capabilities from shop managers and use "translated" capabilities instead.
  1671. * See wc_shop_manager_has_capability function.
  1672. */
  1673. function wc_update_354_modify_shop_manager_caps() {
  1674. global $wp_roles;
  1675. if ( ! class_exists( 'WP_Roles' ) ) {
  1676. return;
  1677. }
  1678. if ( ! isset( $wp_roles ) ) {
  1679. $wp_roles = new WP_Roles(); // @codingStandardsIgnoreLine
  1680. }
  1681. $wp_roles->remove_cap( 'shop_manager', 'edit_users' );
  1682. }
  1683. /**
  1684. * Update DB Version.
  1685. */
  1686. function wc_update_354_db_version() {
  1687. WC_Install::update_db_version( '3.5.4' );
  1688. }
  1689. /**
  1690. * Update product lookup tables in bulk.
  1691. */
  1692. function wc_update_360_product_lookup_tables() {
  1693. wc_update_product_lookup_tables();
  1694. }
  1695. /**
  1696. * Renames ordering meta to be consistent across taxonomies.
  1697. */
  1698. function wc_update_360_term_meta() {
  1699. global $wpdb;
  1700. $wpdb->query( "UPDATE {$wpdb->termmeta} SET meta_key = 'order' WHERE meta_key LIKE 'order_pa_%';" );
  1701. }
  1702. /**
  1703. * Add new user_order_remaining_expires to speed up user download permission fetching.
  1704. *
  1705. * @return void
  1706. */
  1707. function wc_update_360_downloadable_product_permissions_index() {
  1708. global $wpdb;
  1709. $index_exists = $wpdb->get_row( "SHOW INDEX FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions WHERE key_name = 'user_order_remaining_expires'" );
  1710. if ( is_null( $index_exists ) ) {
  1711. $wpdb->query( "ALTER TABLE {$wpdb->prefix}woocommerce_downloadable_product_permissions ADD INDEX user_order_remaining_expires (user_id,order_id,downloads_remaining,access_expires)" );
  1712. }
  1713. }
  1714. /**
  1715. * Update DB Version.
  1716. */
  1717. function wc_update_360_db_version() {
  1718. WC_Install::update_db_version( '3.6.0' );
  1719. }
  1720. /**
  1721. * Put tax classes into a DB table.
  1722. *
  1723. * @return void
  1724. */
  1725. function wc_update_370_tax_rate_classes() {
  1726. global $wpdb;
  1727. $classes = array_map( 'trim', explode( "\n", get_option( 'woocommerce_tax_classes' ) ) );
  1728. if ( $classes ) {
  1729. foreach ( $classes as $class ) {
  1730. if ( empty( $class ) ) {
  1731. continue;
  1732. }
  1733. WC_Tax::create_tax_class( $class );
  1734. }
  1735. }
  1736. delete_option( 'woocommerce_tax_classes' );
  1737. }
  1738. /**
  1739. * Update currency settings for 3.7.0
  1740. *
  1741. * @return void
  1742. */
  1743. function wc_update_370_mro_std_currency() {
  1744. global $wpdb;
  1745. // Fix currency settings for MRU and STN currency.
  1746. $current_currency = get_option( 'woocommerce_currency' );
  1747. if ( 'MRO' === $current_currency ) {
  1748. update_option( 'woocommerce_currency', 'MRU' );
  1749. }
  1750. if ( 'STD' === $current_currency ) {
  1751. update_option( 'woocommerce_currency', 'STN' );
  1752. }
  1753. // Update MRU currency code.
  1754. $wpdb->update(
  1755. $wpdb->postmeta,
  1756. array(
  1757. 'meta_value' => 'MRU', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
  1758. ),
  1759. array(
  1760. 'meta_key' => '_order_currency', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
  1761. 'meta_value' => 'MRO', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
  1762. )
  1763. );
  1764. // Update STN currency code.
  1765. $wpdb->update(
  1766. $wpdb->postmeta,
  1767. array(
  1768. 'meta_value' => 'STN', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
  1769. ),
  1770. array(
  1771. 'meta_key' => '_order_currency', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
  1772. 'meta_value' => 'STD', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
  1773. )
  1774. );
  1775. }
  1776. /**
  1777. * Update DB Version.
  1778. */
  1779. function wc_update_370_db_version() {
  1780. WC_Install::update_db_version( '3.7.0' );
  1781. }
  1782. /**
  1783. * We've moved the MaxMind database to a new location, as per the TOS' requirement that the database not
  1784. * be publicly accessible.
  1785. */
  1786. function wc_update_390_move_maxmind_database() {
  1787. // Make sure to use all of the correct filters to pull the local database path.
  1788. $old_path = apply_filters( 'woocommerce_geolocation_local_database_path', WP_CONTENT_DIR . '/uploads/GeoLite2-Country.mmdb', 2 );
  1789. // Generate a prefix for the old file and store it in the integration as it would expect it.
  1790. $prefix = wp_generate_password( 32, false );
  1791. update_option( 'woocommerce_maxmind_geolocation_settings', array( 'database_prefix' => $prefix ) );
  1792. // Generate the new path in the same way that the integration will.
  1793. $uploads_dir = wp_upload_dir();
  1794. $new_path = trailingslashit( $uploads_dir['basedir'] ) . 'woocommerce_uploads/' . $prefix . '-GeoLite2-Country.mmdb';
  1795. $new_path = apply_filters( 'woocommerce_geolocation_local_database_path', $new_path, 2 );
  1796. $new_path = apply_filters( 'woocommerce_maxmind_geolocation_database_path', $new_path );
  1797. // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
  1798. @rename( $old_path, $new_path );
  1799. }
  1800. /**
  1801. * So that we can best meet MaxMind's TOS, the geolocation database update cron should run once per 15 days.
  1802. */
  1803. function wc_update_390_change_geolocation_database_update_cron() {
  1804. wp_clear_scheduled_hook( 'woocommerce_geoip_updater' );
  1805. wp_schedule_event( time() + ( DAY_IN_SECONDS * 15 ), 'fifteendays', 'woocommerce_geoip_updater' );
  1806. }
  1807. /**
  1808. * Update DB version.
  1809. */
  1810. function wc_update_390_db_version() {
  1811. WC_Install::update_db_version( '3.9.0' );
  1812. }
  1813. /**
  1814. * Increase column size
  1815. */
  1816. function wc_update_400_increase_size_of_column() {
  1817. global $wpdb;
  1818. $wpdb->query( "ALTER TABLE {$wpdb->prefix}wc_product_meta_lookup MODIFY COLUMN `min_price` decimal(19,4) NULL default NULL" );
  1819. $wpdb->query( "ALTER TABLE {$wpdb->prefix}wc_product_meta_lookup MODIFY COLUMN `max_price` decimal(19,4) NULL default NULL" );
  1820. }
  1821. /**
  1822. * Reset ActionScheduler migration status. Needs AS >= 3.0 shipped with WC >= 4.0.
  1823. */
  1824. function wc_update_400_reset_action_scheduler_migration_status() {
  1825. if (
  1826. class_exists( 'ActionScheduler_DataController' ) &&
  1827. method_exists( 'ActionScheduler_DataController', 'mark_migration_incomplete' )
  1828. ) {
  1829. \ActionScheduler_DataController::mark_migration_incomplete();
  1830. }
  1831. }
  1832. /**
  1833. * Update DB version.
  1834. */
  1835. function wc_update_400_db_version() {
  1836. WC_Install::update_db_version( '4.0.0' );
  1837. }
  1838. /**
  1839. * Register attributes as terms for variable products, in increments of 100 products.
  1840. *
  1841. * This migration was added to support a new mechanism to improve the filtering of
  1842. * variable products by attribute (https://github.com/woocommerce/woocommerce/pull/26260),
  1843. * however that mechanism was later reverted (https://github.com/woocommerce/woocommerce/pull/27625)
  1844. * due to numerous issues found. Thus the migration is no longer needed.
  1845. *
  1846. * @return bool true if the migration needs to be run again.
  1847. */
  1848. function wc_update_440_insert_attribute_terms_for_variable_products() {
  1849. return false;
  1850. }
  1851. /**
  1852. * Update DB version.
  1853. */
  1854. function wc_update_440_db_version() {
  1855. WC_Install::update_db_version( '4.4.0' );
  1856. }
  1857. /**
  1858. * Update DB version to 4.5.0.
  1859. */
  1860. function wc_update_450_db_version() {
  1861. WC_Install::update_db_version( '4.5.0' );
  1862. }
  1863. /**
  1864. * Sanitize all coupons code.
  1865. *
  1866. * @return bool True to run again, false if completed.
  1867. */
  1868. function wc_update_450_sanitize_coupons_code() {
  1869. global $wpdb;
  1870. $coupon_id = 0;
  1871. $last_coupon_id = get_option( 'woocommerce_update_450_last_coupon_id', '0' );
  1872. $coupons = $wpdb->get_results(
  1873. $wpdb->prepare(
  1874. "SELECT ID, post_title FROM $wpdb->posts WHERE ID > %d AND post_type = 'shop_coupon' LIMIT 10",
  1875. $last_coupon_id
  1876. ),
  1877. ARRAY_A
  1878. );
  1879. if ( empty( $coupons ) ) {
  1880. delete_option( 'woocommerce_update_450_last_coupon_id' );
  1881. return false;
  1882. }
  1883. foreach ( $coupons as $key => $data ) {
  1884. $coupon_id = intval( $data['ID'] );
  1885. $code = trim( wp_filter_kses( $data['post_title'] ) );
  1886. if ( ! empty( $code ) && $data['post_title'] !== $code ) {
  1887. $wpdb->update(
  1888. $wpdb->posts,
  1889. array(
  1890. 'post_title' => $code,
  1891. ),
  1892. array(
  1893. 'ID' => $coupon_id,
  1894. ),
  1895. array(
  1896. '%s',
  1897. ),
  1898. array(
  1899. '%d',
  1900. )
  1901. );
  1902. // Clean cache.
  1903. clean_post_cache( $coupon_id );
  1904. wp_cache_delete( WC_Cache_Helper::get_cache_prefix( 'coupons' ) . 'coupon_id_from_code_' . $data['post_title'], 'coupons' );
  1905. }
  1906. }
  1907. // Start the run again.
  1908. if ( $coupon_id ) {
  1909. return update_option( 'woocommerce_update_450_last_coupon_id', $coupon_id );
  1910. }
  1911. delete_option( 'woocommerce_update_450_last_coupon_id' );
  1912. return false;
  1913. }
  1914. /**
  1915. * Fixes product review count that might have been incorrect.
  1916. *
  1917. * See @link https://github.com/woocommerce/woocommerce/issues/27688.
  1918. */
  1919. function wc_update_500_fix_product_review_count() {
  1920. global $wpdb;
  1921. $product_id = 0;
  1922. $last_product_id = get_option( 'woocommerce_update_500_last_product_id', '0' );
  1923. $products_data = $wpdb->get_results(
  1924. $wpdb->prepare(
  1925. "
  1926. SELECT post_id, meta_value
  1927. FROM $wpdb->postmeta
  1928. JOIN $wpdb->posts
  1929. ON $wpdb->postmeta.post_id = $wpdb->posts.ID
  1930. WHERE
  1931. post_type = 'product'
  1932. AND post_status = 'publish'
  1933. AND post_id > %d
  1934. AND meta_key = '_wc_review_count'
  1935. ORDER BY post_id ASC
  1936. LIMIT 10
  1937. ",
  1938. $last_product_id
  1939. ),
  1940. ARRAY_A
  1941. );
  1942. if ( empty( $products_data ) ) {
  1943. delete_option( 'woocommerce_update_500_last_product_id' );
  1944. return false;
  1945. }
  1946. $product_ids_to_check = array_column( $products_data, 'post_id' );
  1947. $actual_review_counts = WC_Comments::get_review_counts_for_product_ids( $product_ids_to_check );
  1948. foreach ( $products_data as $product_data ) {
  1949. $product_id = intval( $product_data['post_id'] );
  1950. $current_review_count = intval( $product_data['meta_value'] );
  1951. if ( intval( $actual_review_counts[ $product_id ] ) !== $current_review_count ) {
  1952. WC_Comments::clear_transients( $product_id );
  1953. }
  1954. }
  1955. // Start the run again.
  1956. if ( $product_id ) {
  1957. return update_option( 'woocommerce_update_500_last_product_id', $product_id );
  1958. }
  1959. delete_option( 'woocommerce_update_500_last_product_id' );
  1960. return false;
  1961. }
  1962. /**
  1963. * Update DB version to 5.0.0.
  1964. */
  1965. function wc_update_500_db_version() {
  1966. WC_Install::update_db_version( '5.0.0' );
  1967. }
  1968. /**
  1969. * Creates the refund and returns policy page.
  1970. *
  1971. * See @link https://github.com/woocommerce/woocommerce/issues/29235.
  1972. */
  1973. function wc_update_560_create_refund_returns_page() {
  1974. /**
  1975. * Filter on the pages created to return what we expect.
  1976. *
  1977. * @param array $pages The default WC pages.
  1978. */
  1979. function filter_created_pages( $pages ) {
  1980. $page_to_create = array( 'refund_returns' );
  1981. return array_intersect_key( $pages, array_flip( $page_to_create ) );
  1982. }
  1983. add_filter( 'woocommerce_create_pages', 'filter_created_pages' );
  1984. WC_Install::create_pages();
  1985. remove_filter( 'woocommerce_create_pages', 'filter_created_pages' );
  1986. }
  1987. /**
  1988. * Update DB version to 5.6.0.
  1989. */
  1990. function wc_update_560_db_version() {
  1991. WC_Install::update_db_version( '5.6.0' );
  1992. }