wc-update-functions.php 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837
  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. /**
  12. * Update file paths for 2.0
  13. *
  14. * @return void
  15. */
  16. function wc_update_200_file_paths() {
  17. global $wpdb;
  18. // Upgrade old style files paths to support multiple file paths.
  19. $existing_file_paths = $wpdb->get_results( "SELECT meta_value, meta_id, post_id FROM {$wpdb->postmeta} WHERE meta_key = '_file_path' AND meta_value != '';" );
  20. if ( $existing_file_paths ) {
  21. foreach ( $existing_file_paths as $existing_file_path ) {
  22. $old_file_path = trim( $existing_file_path->meta_value );
  23. if ( ! empty( $old_file_path ) ) {
  24. $file_paths = serialize( array( md5( $old_file_path ) => $old_file_path ) );
  25. $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 ) );
  26. $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 ) );
  27. }
  28. }
  29. }
  30. }
  31. /**
  32. * Update permalinks for 2.0
  33. *
  34. * @return void
  35. */
  36. function wc_update_200_permalinks() {
  37. // Setup default permalinks if shop page is defined.
  38. $permalinks = get_option( 'woocommerce_permalinks' );
  39. $shop_page_id = wc_get_page_id( 'shop' );
  40. if ( empty( $permalinks ) && $shop_page_id > 0 ) {
  41. $base_slug = $shop_page_id > 0 && get_post( $shop_page_id ) ? get_page_uri( $shop_page_id ) : 'shop';
  42. $category_base = get_option( 'woocommerce_prepend_shop_page_to_urls' ) == 'yes' ? trailingslashit( $base_slug ) : '';
  43. $category_slug = get_option( 'woocommerce_product_category_slug' ) ? get_option( 'woocommerce_product_category_slug' ) : _x( 'product-category', 'slug', 'woocommerce' );
  44. $tag_slug = get_option( 'woocommerce_product_tag_slug' ) ? get_option( 'woocommerce_product_tag_slug' ) : _x( 'product-tag', 'slug', 'woocommerce' );
  45. if ( 'yes' == get_option( 'woocommerce_prepend_shop_page_to_products' ) ) {
  46. $product_base = trailingslashit( $base_slug );
  47. } else {
  48. $product_slug = get_option( 'woocommerce_product_slug' );
  49. if ( false !== $product_slug && ! empty( $product_slug ) ) {
  50. $product_base = trailingslashit( $product_slug );
  51. } else {
  52. $product_base = trailingslashit( _x( 'product', 'slug', 'woocommerce' ) );
  53. }
  54. }
  55. if ( get_option( 'woocommerce_prepend_category_to_products' ) == 'yes' ) {
  56. $product_base .= trailingslashit( '%product_cat%' );
  57. }
  58. $permalinks = array(
  59. 'product_base' => untrailingslashit( $product_base ),
  60. 'category_base' => untrailingslashit( $category_base . $category_slug ),
  61. 'attribute_base' => untrailingslashit( $category_base ),
  62. 'tag_base' => untrailingslashit( $category_base . $tag_slug ),
  63. );
  64. update_option( 'woocommerce_permalinks', $permalinks );
  65. }
  66. }
  67. /**
  68. * Update sub-category display options for 2.0
  69. *
  70. * @return void
  71. */
  72. function wc_update_200_subcat_display() {
  73. // Update subcat display settings.
  74. if ( get_option( 'woocommerce_shop_show_subcategories' ) == 'yes' ) {
  75. if ( get_option( 'woocommerce_hide_products_when_showing_subcategories' ) == 'yes' ) {
  76. update_option( 'woocommerce_shop_page_display', 'subcategories' );
  77. } else {
  78. update_option( 'woocommerce_shop_page_display', 'both' );
  79. }
  80. }
  81. if ( get_option( 'woocommerce_show_subcategories' ) == 'yes' ) {
  82. if ( get_option( 'woocommerce_hide_products_when_showing_subcategories' ) == 'yes' ) {
  83. update_option( 'woocommerce_category_archive_display', 'subcategories' );
  84. } else {
  85. update_option( 'woocommerce_category_archive_display', 'both' );
  86. }
  87. }
  88. }
  89. /**
  90. * Update tax rates for 2.0
  91. *
  92. * @return void
  93. */
  94. function wc_update_200_taxrates() {
  95. global $wpdb;
  96. // Update tax rates.
  97. $loop = 0;
  98. $tax_rates = get_option( 'woocommerce_tax_rates' );
  99. if ( $tax_rates ) {
  100. foreach ( $tax_rates as $tax_rate ) {
  101. foreach ( $tax_rate['countries'] as $country => $states ) {
  102. $states = array_reverse( $states );
  103. foreach ( $states as $state ) {
  104. if ( '*' == $state ) {
  105. $state = '';
  106. }
  107. $wpdb->insert(
  108. $wpdb->prefix . 'woocommerce_tax_rates',
  109. array(
  110. 'tax_rate_country' => $country,
  111. 'tax_rate_state' => $state,
  112. 'tax_rate' => $tax_rate['rate'],
  113. 'tax_rate_name' => $tax_rate['label'],
  114. 'tax_rate_priority' => 1,
  115. 'tax_rate_compound' => ( 'yes' === $tax_rate['compound'] ) ? 1 : 0,
  116. 'tax_rate_shipping' => ( 'yes' === $tax_rate['shipping'] ) ? 1 : 0,
  117. 'tax_rate_order' => $loop,
  118. 'tax_rate_class' => $tax_rate['class'],
  119. )
  120. );
  121. $loop++;
  122. }
  123. }
  124. }
  125. }
  126. $local_tax_rates = get_option( 'woocommerce_local_tax_rates' );
  127. if ( $local_tax_rates ) {
  128. foreach ( $local_tax_rates as $tax_rate ) {
  129. $location_type = ( 'postcode' === $tax_rate['location_type'] ) ? 'postcode' : 'city';
  130. if ( '*' == $tax_rate['state'] ) {
  131. $tax_rate['state'] = '';
  132. }
  133. $wpdb->insert(
  134. $wpdb->prefix . 'woocommerce_tax_rates',
  135. array(
  136. 'tax_rate_country' => $tax_rate['country'],
  137. 'tax_rate_state' => $tax_rate['state'],
  138. 'tax_rate' => $tax_rate['rate'],
  139. 'tax_rate_name' => $tax_rate['label'],
  140. 'tax_rate_priority' => 2,
  141. 'tax_rate_compound' => ( 'yes' === $tax_rate['compound'] ) ? 1 : 0,
  142. 'tax_rate_shipping' => ( 'yes' === $tax_rate['shipping'] ) ? 1 : 0,
  143. 'tax_rate_order' => $loop,
  144. 'tax_rate_class' => $tax_rate['class'],
  145. )
  146. );
  147. $tax_rate_id = $wpdb->insert_id;
  148. if ( $tax_rate['locations'] ) {
  149. foreach ( $tax_rate['locations'] as $location ) {
  150. $wpdb->insert(
  151. $wpdb->prefix . 'woocommerce_tax_rate_locations',
  152. array(
  153. 'location_code' => $location,
  154. 'tax_rate_id' => $tax_rate_id,
  155. 'location_type' => $location_type,
  156. )
  157. );
  158. }
  159. }
  160. $loop++;
  161. }
  162. }
  163. update_option( 'woocommerce_tax_rates_backup', $tax_rates );
  164. update_option( 'woocommerce_local_tax_rates_backup', $local_tax_rates );
  165. delete_option( 'woocommerce_tax_rates' );
  166. delete_option( 'woocommerce_local_tax_rates' );
  167. }
  168. /**
  169. * Update order item line items for 2.0
  170. *
  171. * @return void
  172. */
  173. function wc_update_200_line_items() {
  174. global $wpdb;
  175. // Now its time for the massive update to line items - move them to the new DB tables.
  176. // Reverse with UPDATE `wpwc_postmeta` SET meta_key = '_order_items' WHERE meta_key = '_order_items_old'.
  177. $order_item_rows = $wpdb->get_results(
  178. "SELECT meta_value, post_id FROM {$wpdb->postmeta} WHERE meta_key = '_order_items'"
  179. );
  180. foreach ( $order_item_rows as $order_item_row ) {
  181. $order_items = (array) maybe_unserialize( $order_item_row->meta_value );
  182. foreach ( $order_items as $order_item ) {
  183. if ( ! isset( $order_item['line_total'] ) && isset( $order_item['taxrate'] ) && isset( $order_item['cost'] ) ) {
  184. $order_item['line_tax'] = number_format( ( $order_item['cost'] * $order_item['qty'] ) * ( $order_item['taxrate'] / 100 ), 2, '.', '' );
  185. $order_item['line_total'] = $order_item['cost'] * $order_item['qty'];
  186. $order_item['line_subtotal_tax'] = $order_item['line_tax'];
  187. $order_item['line_subtotal'] = $order_item['line_total'];
  188. }
  189. $order_item['line_tax'] = isset( $order_item['line_tax'] ) ? $order_item['line_tax'] : 0;
  190. $order_item['line_total'] = isset( $order_item['line_total'] ) ? $order_item['line_total'] : 0;
  191. $order_item['line_subtotal_tax'] = isset( $order_item['line_subtotal_tax'] ) ? $order_item['line_subtotal_tax'] : 0;
  192. $order_item['line_subtotal'] = isset( $order_item['line_subtotal'] ) ? $order_item['line_subtotal'] : 0;
  193. $item_id = wc_add_order_item(
  194. $order_item_row->post_id,
  195. array(
  196. 'order_item_name' => $order_item['name'],
  197. 'order_item_type' => 'line_item',
  198. )
  199. );
  200. // Add line item meta.
  201. if ( $item_id ) {
  202. wc_add_order_item_meta( $item_id, '_qty', absint( $order_item['qty'] ) );
  203. wc_add_order_item_meta( $item_id, '_tax_class', $order_item['tax_class'] );
  204. wc_add_order_item_meta( $item_id, '_product_id', $order_item['id'] );
  205. wc_add_order_item_meta( $item_id, '_variation_id', $order_item['variation_id'] );
  206. wc_add_order_item_meta( $item_id, '_line_subtotal', wc_format_decimal( $order_item['line_subtotal'] ) );
  207. wc_add_order_item_meta( $item_id, '_line_subtotal_tax', wc_format_decimal( $order_item['line_subtotal_tax'] ) );
  208. wc_add_order_item_meta( $item_id, '_line_total', wc_format_decimal( $order_item['line_total'] ) );
  209. wc_add_order_item_meta( $item_id, '_line_tax', wc_format_decimal( $order_item['line_tax'] ) );
  210. $meta_rows = array();
  211. // Insert meta.
  212. if ( ! empty( $order_item['item_meta'] ) ) {
  213. foreach ( $order_item['item_meta'] as $key => $meta ) {
  214. // Backwards compatibility.
  215. if ( is_array( $meta ) && isset( $meta['meta_name'] ) ) {
  216. $meta_rows[] = '(' . $item_id . ',"' . esc_sql( $meta['meta_name'] ) . '","' . esc_sql( $meta['meta_value'] ) . '")';
  217. } else {
  218. $meta_rows[] = '(' . $item_id . ',"' . esc_sql( $key ) . '","' . esc_sql( $meta ) . '")';
  219. }
  220. }
  221. }
  222. // Insert meta rows at once.
  223. if ( count( $meta_rows ) > 0 ) {
  224. $wpdb->query(
  225. $wpdb->prepare(
  226. "INSERT INTO {$wpdb->prefix}woocommerce_order_itemmeta ( order_item_id, meta_key, meta_value )
  227. VALUES " . implode( ',', $meta_rows ) . ';', // @codingStandardsIgnoreLine
  228. $order_item_row->post_id
  229. )
  230. );
  231. }
  232. // Delete from DB (rename).
  233. $wpdb->query(
  234. $wpdb->prepare(
  235. "UPDATE {$wpdb->postmeta}
  236. SET meta_key = '_order_items_old'
  237. WHERE meta_key = '_order_items'
  238. AND post_id = %d",
  239. $order_item_row->post_id
  240. )
  241. );
  242. }
  243. unset( $meta_rows, $item_id, $order_item );
  244. }
  245. }
  246. // Do the same kind of update for order_taxes - move to lines.
  247. // Reverse with UPDATE `wpwc_postmeta` SET meta_key = '_order_taxes' WHERE meta_key = '_order_taxes_old'.
  248. $order_tax_rows = $wpdb->get_results(
  249. "SELECT meta_value, post_id FROM {$wpdb->postmeta}
  250. WHERE meta_key = '_order_taxes'"
  251. );
  252. foreach ( $order_tax_rows as $order_tax_row ) {
  253. $order_taxes = (array) maybe_unserialize( $order_tax_row->meta_value );
  254. if ( ! empty( $order_taxes ) ) {
  255. foreach ( $order_taxes as $order_tax ) {
  256. if ( ! isset( $order_tax['label'] ) || ! isset( $order_tax['cart_tax'] ) || ! isset( $order_tax['shipping_tax'] ) ) {
  257. continue;
  258. }
  259. $item_id = wc_add_order_item(
  260. $order_tax_row->post_id, array(
  261. 'order_item_name' => $order_tax['label'],
  262. 'order_item_type' => 'tax',
  263. )
  264. );
  265. // Add line item meta.
  266. if ( $item_id ) {
  267. wc_add_order_item_meta( $item_id, 'compound', absint( isset( $order_tax['compound'] ) ? $order_tax['compound'] : 0 ) );
  268. wc_add_order_item_meta( $item_id, 'tax_amount', wc_clean( $order_tax['cart_tax'] ) );
  269. wc_add_order_item_meta( $item_id, 'shipping_tax_amount', wc_clean( $order_tax['shipping_tax'] ) );
  270. }
  271. // Delete from DB (rename).
  272. $wpdb->query(
  273. $wpdb->prepare(
  274. "UPDATE {$wpdb->postmeta}
  275. SET meta_key = '_order_taxes_old'
  276. WHERE meta_key = '_order_taxes'
  277. AND post_id = %d",
  278. $order_tax_row->post_id
  279. )
  280. );
  281. unset( $tax_amount );
  282. }
  283. }
  284. }
  285. }
  286. /**
  287. * Update image settings for 2.0
  288. *
  289. * @return void
  290. */
  291. function wc_update_200_images() {
  292. // Grab the pre 2.0 Image options and use to populate the new image options settings,
  293. // cleaning up afterwards like nice people do.
  294. foreach ( array( 'catalog', 'single', 'thumbnail' ) as $value ) {
  295. $old_settings = array_filter(
  296. array(
  297. 'width' => get_option( 'woocommerce_' . $value . '_image_width' ),
  298. 'height' => get_option( 'woocommerce_' . $value . '_image_height' ),
  299. 'crop' => get_option( 'woocommerce_' . $value . '_image_crop' ),
  300. )
  301. );
  302. if ( ! empty( $old_settings ) && update_option( 'shop_' . $value . '_image_size', $old_settings ) ) {
  303. delete_option( 'woocommerce_' . $value . '_image_width' );
  304. delete_option( 'woocommerce_' . $value . '_image_height' );
  305. delete_option( 'woocommerce_' . $value . '_image_crop' );
  306. }
  307. }
  308. }
  309. /**
  310. * Update DB version for 2.0
  311. *
  312. * @return void
  313. */
  314. function wc_update_200_db_version() {
  315. WC_Install::update_db_version( '2.0.0' );
  316. }
  317. /**
  318. * Update Brazilian States for 2.0.9
  319. *
  320. * @return void
  321. */
  322. function wc_update_209_brazillian_state() {
  323. global $wpdb;
  324. // Update brazillian state codes.
  325. $wpdb->update(
  326. $wpdb->postmeta,
  327. array(
  328. 'meta_value' => 'BA',
  329. ),
  330. array(
  331. 'meta_key' => '_billing_state',
  332. 'meta_value' => 'BH',
  333. )
  334. );
  335. $wpdb->update(
  336. $wpdb->postmeta,
  337. array(
  338. 'meta_value' => 'BA',
  339. ),
  340. array(
  341. 'meta_key' => '_shipping_state',
  342. 'meta_value' => 'BH',
  343. )
  344. );
  345. $wpdb->update(
  346. $wpdb->usermeta,
  347. array(
  348. 'meta_value' => 'BA',
  349. ),
  350. array(
  351. 'meta_key' => 'billing_state',
  352. 'meta_value' => 'BH',
  353. )
  354. );
  355. $wpdb->update(
  356. $wpdb->usermeta,
  357. array(
  358. 'meta_value' => 'BA',
  359. ),
  360. array(
  361. 'meta_key' => 'shipping_state',
  362. 'meta_value' => 'BH',
  363. )
  364. );
  365. }
  366. /**
  367. * Update DB version for 2.0.9
  368. *
  369. * @return void
  370. */
  371. function wc_update_209_db_version() {
  372. WC_Install::update_db_version( '2.0.9' );
  373. }
  374. /**
  375. * Remove pages for 2.1
  376. *
  377. * @return void
  378. */
  379. function wc_update_210_remove_pages() {
  380. // Pages no longer used.
  381. wp_trash_post( get_option( 'woocommerce_pay_page_id' ) );
  382. wp_trash_post( get_option( 'woocommerce_thanks_page_id' ) );
  383. wp_trash_post( get_option( 'woocommerce_view_order_page_id' ) );
  384. wp_trash_post( get_option( 'woocommerce_change_password_page_id' ) );
  385. wp_trash_post( get_option( 'woocommerce_edit_address_page_id' ) );
  386. wp_trash_post( get_option( 'woocommerce_lost_password_page_id' ) );
  387. }
  388. /**
  389. * Update file paths to support multiple files for 2.1
  390. *
  391. * @return void
  392. */
  393. function wc_update_210_file_paths() {
  394. global $wpdb;
  395. // Upgrade file paths to support multiple file paths + names etc.
  396. $existing_file_paths = $wpdb->get_results( "SELECT meta_value, meta_id FROM {$wpdb->postmeta} WHERE meta_key = '_file_paths' AND meta_value != '';" );
  397. if ( $existing_file_paths ) {
  398. foreach ( $existing_file_paths as $existing_file_path ) {
  399. $needs_update = false;
  400. $new_value = array();
  401. $value = maybe_unserialize( trim( $existing_file_path->meta_value ) );
  402. if ( $value ) {
  403. foreach ( $value as $key => $file ) {
  404. if ( ! is_array( $file ) ) {
  405. $needs_update = true;
  406. $new_value[ $key ] = array(
  407. 'file' => $file,
  408. 'name' => wc_get_filename_from_url( $file ),
  409. );
  410. } else {
  411. $new_value[ $key ] = $file;
  412. }
  413. }
  414. if ( $needs_update ) {
  415. $new_value = serialize( $new_value );
  416. $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 ) );
  417. }
  418. }
  419. }
  420. }
  421. }
  422. /**
  423. * Update DB version for 2.1
  424. *
  425. * @return void
  426. */
  427. function wc_update_210_db_version() {
  428. WC_Install::update_db_version( '2.1.0' );
  429. }
  430. /**
  431. * Update shipping options for 2.2
  432. *
  433. * @return void
  434. */
  435. function wc_update_220_shipping() {
  436. $woocommerce_ship_to_destination = 'shipping';
  437. if ( get_option( 'woocommerce_ship_to_billing_address_only' ) === 'yes' ) {
  438. $woocommerce_ship_to_destination = 'billing_only';
  439. } elseif ( get_option( 'woocommerce_ship_to_billing' ) === 'yes' ) {
  440. $woocommerce_ship_to_destination = 'billing';
  441. }
  442. add_option( 'woocommerce_ship_to_destination', $woocommerce_ship_to_destination, '', 'no' );
  443. }
  444. /**
  445. * Update order statuses for 2.2
  446. *
  447. * @return void
  448. */
  449. function wc_update_220_order_status() {
  450. global $wpdb;
  451. $wpdb->query(
  452. "UPDATE {$wpdb->posts} as posts
  453. LEFT JOIN {$wpdb->term_relationships} AS rel ON posts.ID = rel.object_id
  454. LEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id )
  455. LEFT JOIN {$wpdb->terms} AS term USING( term_id )
  456. SET posts.post_status = 'wc-pending'
  457. WHERE posts.post_type = 'shop_order'
  458. AND posts.post_status = 'publish'
  459. AND tax.taxonomy = 'shop_order_status'
  460. AND term.slug LIKE 'pending%';"
  461. );
  462. $wpdb->query(
  463. "UPDATE {$wpdb->posts} as posts
  464. LEFT JOIN {$wpdb->term_relationships} AS rel ON posts.ID = rel.object_id
  465. LEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id )
  466. LEFT JOIN {$wpdb->terms} AS term USING( term_id )
  467. SET posts.post_status = 'wc-processing'
  468. WHERE posts.post_type = 'shop_order'
  469. AND posts.post_status = 'publish'
  470. AND tax.taxonomy = 'shop_order_status'
  471. AND term.slug LIKE 'processing%';"
  472. );
  473. $wpdb->query(
  474. "UPDATE {$wpdb->posts} as posts
  475. LEFT JOIN {$wpdb->term_relationships} AS rel ON posts.ID = rel.object_id
  476. LEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id )
  477. LEFT JOIN {$wpdb->terms} AS term USING( term_id )
  478. SET posts.post_status = 'wc-on-hold'
  479. WHERE posts.post_type = 'shop_order'
  480. AND posts.post_status = 'publish'
  481. AND tax.taxonomy = 'shop_order_status'
  482. AND term.slug LIKE 'on-hold%';"
  483. );
  484. $wpdb->query(
  485. "UPDATE {$wpdb->posts} as posts
  486. LEFT JOIN {$wpdb->term_relationships} AS rel ON posts.ID = rel.object_id
  487. LEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id )
  488. LEFT JOIN {$wpdb->terms} AS term USING( term_id )
  489. SET posts.post_status = 'wc-completed'
  490. WHERE posts.post_type = 'shop_order'
  491. AND posts.post_status = 'publish'
  492. AND tax.taxonomy = 'shop_order_status'
  493. AND term.slug LIKE 'completed%';"
  494. );
  495. $wpdb->query(
  496. "UPDATE {$wpdb->posts} as posts
  497. LEFT JOIN {$wpdb->term_relationships} AS rel ON posts.ID = rel.object_id
  498. LEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id )
  499. LEFT JOIN {$wpdb->terms} AS term USING( term_id )
  500. SET posts.post_status = 'wc-cancelled'
  501. WHERE posts.post_type = 'shop_order'
  502. AND posts.post_status = 'publish'
  503. AND tax.taxonomy = 'shop_order_status'
  504. AND term.slug LIKE 'cancelled%';"
  505. );
  506. $wpdb->query(
  507. "UPDATE {$wpdb->posts} as posts
  508. LEFT JOIN {$wpdb->term_relationships} AS rel ON posts.ID = rel.object_id
  509. LEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id )
  510. LEFT JOIN {$wpdb->terms} AS term USING( term_id )
  511. SET posts.post_status = 'wc-refunded'
  512. WHERE posts.post_type = 'shop_order'
  513. AND posts.post_status = 'publish'
  514. AND tax.taxonomy = 'shop_order_status'
  515. AND term.slug LIKE 'refunded%';"
  516. );
  517. $wpdb->query(
  518. "UPDATE {$wpdb->posts} as posts
  519. LEFT JOIN {$wpdb->term_relationships} AS rel ON posts.ID = rel.object_id
  520. LEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id )
  521. LEFT JOIN {$wpdb->terms} AS term USING( term_id )
  522. SET posts.post_status = 'wc-failed'
  523. WHERE posts.post_type = 'shop_order'
  524. AND posts.post_status = 'publish'
  525. AND tax.taxonomy = 'shop_order_status'
  526. AND term.slug LIKE 'failed%';"
  527. );
  528. }
  529. /**
  530. * Update variations for 2.2
  531. *
  532. * @return void
  533. */
  534. function wc_update_220_variations() {
  535. global $wpdb;
  536. // Update variations which manage stock.
  537. $update_variations = $wpdb->get_results(
  538. "SELECT DISTINCT posts.ID AS variation_id, posts.post_parent AS variation_parent FROM {$wpdb->posts} as posts
  539. LEFT OUTER JOIN {$wpdb->postmeta} AS postmeta ON posts.ID = postmeta.post_id AND postmeta.meta_key = '_stock'
  540. LEFT OUTER JOIN {$wpdb->postmeta} as postmeta2 ON posts.ID = postmeta2.post_id AND postmeta2.meta_key = '_manage_stock'
  541. WHERE posts.post_type = 'product_variation'
  542. AND postmeta.meta_value IS NOT NULL
  543. AND postmeta.meta_value != ''
  544. AND postmeta2.meta_value IS NULL"
  545. );
  546. foreach ( $update_variations as $variation ) {
  547. $parent_backorders = get_post_meta( $variation->variation_parent, '_backorders', true );
  548. add_post_meta( $variation->variation_id, '_manage_stock', 'yes', true );
  549. add_post_meta( $variation->variation_id, '_backorders', $parent_backorders ? $parent_backorders : 'no', true );
  550. }
  551. }
  552. /**
  553. * Update attributes for 2.2
  554. *
  555. * @return void
  556. */
  557. function wc_update_220_attributes() {
  558. global $wpdb;
  559. // Update taxonomy names with correct sanitized names.
  560. $attribute_taxonomies = $wpdb->get_results( 'SELECT attribute_name, attribute_id FROM ' . $wpdb->prefix . 'woocommerce_attribute_taxonomies' );
  561. foreach ( $attribute_taxonomies as $attribute_taxonomy ) {
  562. $sanitized_attribute_name = wc_sanitize_taxonomy_name( $attribute_taxonomy->attribute_name );
  563. if ( $sanitized_attribute_name !== $attribute_taxonomy->attribute_name ) {
  564. if ( ! $wpdb->get_var( $wpdb->prepare( "SELECT 1=1 FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_name = %s;", $sanitized_attribute_name ) ) ) {
  565. // Update attribute.
  566. $wpdb->update(
  567. "{$wpdb->prefix}woocommerce_attribute_taxonomies",
  568. array(
  569. 'attribute_name' => $sanitized_attribute_name,
  570. ),
  571. array(
  572. 'attribute_id' => $attribute_taxonomy->attribute_id,
  573. )
  574. );
  575. // Update terms.
  576. $wpdb->update(
  577. $wpdb->term_taxonomy,
  578. array( 'taxonomy' => wc_attribute_taxonomy_name( $sanitized_attribute_name ) ),
  579. array( 'taxonomy' => 'pa_' . $attribute_taxonomy->attribute_name )
  580. );
  581. }
  582. }
  583. }
  584. }
  585. /**
  586. * Update DB version for 2.2
  587. *
  588. * @return void
  589. */
  590. function wc_update_220_db_version() {
  591. WC_Install::update_db_version( '2.2.0' );
  592. }
  593. /**
  594. * Update options for 2.3
  595. *
  596. * @return void
  597. */
  598. function wc_update_230_options() {
  599. // _money_spent and _order_count may be out of sync - clear them
  600. delete_metadata( 'user', 0, '_money_spent', '', true );
  601. delete_metadata( 'user', 0, '_order_count', '', true );
  602. // 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.
  603. if ( '' === get_option( 'woocommerce_default_customer_address', false ) && wc_prices_include_tax() ) {
  604. update_option( 'woocommerce_default_customer_address', 'base' );
  605. }
  606. }
  607. /**
  608. * Update DB version for 2.3
  609. *
  610. * @return void
  611. */
  612. function wc_update_230_db_version() {
  613. WC_Install::update_db_version( '2.3.0' );
  614. }
  615. /**
  616. * Update calc discount options for 2.4
  617. *
  618. * @return void
  619. */
  620. function wc_update_240_options() {
  621. /**
  622. * Coupon discount calculations.
  623. * Maintain the old coupon logic for upgrades.
  624. */
  625. update_option( 'woocommerce_calc_discounts_sequentially', 'yes' );
  626. }
  627. /**
  628. * Update shipping methods for 2.4
  629. *
  630. * @return void
  631. */
  632. function wc_update_240_shipping_methods() {
  633. /**
  634. * Flat Rate Shipping.
  635. * Update legacy options to new math based options.
  636. */
  637. $shipping_methods = array(
  638. 'woocommerce_flat_rates' => new WC_Shipping_Legacy_Flat_Rate(),
  639. 'woocommerce_international_delivery_flat_rates' => new WC_Shipping_Legacy_International_Delivery(),
  640. );
  641. foreach ( $shipping_methods as $flat_rate_option_key => $shipping_method ) {
  642. // Stop this running more than once if routine is repeated.
  643. if ( version_compare( $shipping_method->get_option( 'version', 0 ), '2.4.0', '<' ) ) {
  644. $has_classes = count( WC()->shipping->get_shipping_classes() ) > 0;
  645. $cost_key = $has_classes ? 'no_class_cost' : 'cost';
  646. $min_fee = $shipping_method->get_option( 'minimum_fee' );
  647. $math_cost_strings = array(
  648. 'cost' => array(),
  649. 'no_class_cost' => array(),
  650. );
  651. $math_cost_strings[ $cost_key ][] = $shipping_method->get_option( 'cost' );
  652. $fee = $shipping_method->get_option( 'fee' );
  653. if ( $fee ) {
  654. $math_cost_strings[ $cost_key ][] = strstr( $fee, '%' ) ? '[fee percent="' . str_replace( '%', '', $fee ) . '" min="' . esc_attr( $min_fee ) . '"]' : $fee;
  655. }
  656. foreach ( WC()->shipping->get_shipping_classes() as $shipping_class ) {
  657. $rate_key = 'class_cost_' . $shipping_class->slug;
  658. $math_cost_strings[ $rate_key ] = $math_cost_strings['no_class_cost'];
  659. }
  660. $flat_rates = array_filter( (array) get_option( $flat_rate_option_key, array() ) );
  661. if ( $flat_rates ) {
  662. foreach ( $flat_rates as $shipping_class => $rate ) {
  663. $rate_key = 'class_cost_' . $shipping_class;
  664. if ( $rate['cost'] || $rate['fee'] ) {
  665. $math_cost_strings[ $rate_key ][] = $rate['cost'];
  666. $math_cost_strings[ $rate_key ][] = strstr( $rate['fee'], '%' ) ? '[fee percent="' . str_replace( '%', '', $rate['fee'] ) . '" min="' . esc_attr( $min_fee ) . '"]' : $rate['fee'];
  667. }
  668. }
  669. }
  670. if ( 'item' === $shipping_method->type ) {
  671. foreach ( $math_cost_strings as $key => $math_cost_string ) {
  672. $math_cost_strings[ $key ] = array_filter( array_map( 'trim', $math_cost_strings[ $key ] ) );
  673. if ( ! empty( $math_cost_strings[ $key ] ) ) {
  674. $last_key = max( 0, count( $math_cost_strings[ $key ] ) - 1 );
  675. $math_cost_strings[ $key ][0] = '( ' . $math_cost_strings[ $key ][0];
  676. $math_cost_strings[ $key ][ $last_key ] .= ' ) * [qty]';
  677. }
  678. }
  679. }
  680. $math_cost_strings['cost'][] = $shipping_method->get_option( 'cost_per_order' );
  681. // Save settings.
  682. foreach ( $math_cost_strings as $option_id => $math_cost_string ) {
  683. $shipping_method->settings[ $option_id ] = implode( ' + ', array_filter( $math_cost_string ) );
  684. }
  685. $shipping_method->settings['version'] = '2.4.0';
  686. $shipping_method->settings['type'] = 'item' === $shipping_method->settings['type'] ? 'class' : $shipping_method->settings['type'];
  687. update_option( $shipping_method->plugin_id . $shipping_method->id . '_settings', $shipping_method->settings );
  688. }
  689. }
  690. }
  691. /**
  692. * Update API keys for 2.4
  693. *
  694. * @return void
  695. */
  696. function wc_update_240_api_keys() {
  697. global $wpdb;
  698. /**
  699. * Update the old user API keys to the new Apps keys.
  700. */
  701. $api_users = $wpdb->get_results( "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = 'woocommerce_api_consumer_key'" );
  702. $apps_keys = array();
  703. // Get user data.
  704. foreach ( $api_users as $_user ) {
  705. $user = get_userdata( $_user->user_id );
  706. $apps_keys[] = array(
  707. 'user_id' => $user->ID,
  708. 'permissions' => $user->woocommerce_api_key_permissions,
  709. 'consumer_key' => wc_api_hash( $user->woocommerce_api_consumer_key ),
  710. 'consumer_secret' => $user->woocommerce_api_consumer_secret,
  711. 'truncated_key' => substr( $user->woocommerce_api_consumer_secret, -7 ),
  712. );
  713. }
  714. if ( ! empty( $apps_keys ) ) {
  715. // Create new apps.
  716. foreach ( $apps_keys as $app ) {
  717. $wpdb->insert(
  718. $wpdb->prefix . 'woocommerce_api_keys',
  719. $app,
  720. array(
  721. '%d',
  722. '%s',
  723. '%s',
  724. '%s',
  725. '%s',
  726. )
  727. );
  728. }
  729. // Delete old user keys from usermeta.
  730. foreach ( $api_users as $_user ) {
  731. $user_id = intval( $_user->user_id );
  732. delete_user_meta( $user_id, 'woocommerce_api_consumer_key' );
  733. delete_user_meta( $user_id, 'woocommerce_api_consumer_secret' );
  734. delete_user_meta( $user_id, 'woocommerce_api_key_permissions' );
  735. }
  736. }
  737. }
  738. /**
  739. * Update webhooks for 2.4
  740. *
  741. * @return void
  742. */
  743. function wc_update_240_webhooks() {
  744. /**
  745. * Webhooks.
  746. * Make sure order.update webhooks get the woocommerce_order_edit_status hook.
  747. */
  748. $order_update_webhooks = get_posts(
  749. array(
  750. 'posts_per_page' => -1,
  751. 'post_type' => 'shop_webhook',
  752. 'meta_key' => '_topic',
  753. 'meta_value' => 'order.updated',
  754. )
  755. );
  756. foreach ( $order_update_webhooks as $order_update_webhook ) {
  757. $webhook = new WC_Webhook( $order_update_webhook->ID );
  758. $webhook->set_topic( 'order.updated' );
  759. }
  760. }
  761. /**
  762. * Update refunds for 2.4
  763. *
  764. * @return void
  765. */
  766. function wc_update_240_refunds() {
  767. global $wpdb;
  768. /**
  769. * Refunds for full refunded orders.
  770. * Update fully refunded orders to ensure they have a refund line item so reports add up.
  771. */
  772. $refunded_orders = get_posts(
  773. array(
  774. 'posts_per_page' => -1,
  775. 'post_type' => 'shop_order',
  776. 'post_status' => array( 'wc-refunded' ),
  777. )
  778. );
  779. // Ensure emails are disabled during this update routine.
  780. remove_all_actions( 'woocommerce_order_status_refunded_notification' );
  781. remove_all_actions( 'woocommerce_order_partially_refunded_notification' );
  782. remove_action( 'woocommerce_order_status_refunded', array( 'WC_Emails', 'send_transactional_email' ) );
  783. remove_action( 'woocommerce_order_partially_refunded', array( 'WC_Emails', 'send_transactional_email' ) );
  784. foreach ( $refunded_orders as $refunded_order ) {
  785. $order_total = get_post_meta( $refunded_order->ID, '_order_total', true );
  786. $refunded_total = $wpdb->get_var(
  787. $wpdb->prepare(
  788. "SELECT SUM( postmeta.meta_value )
  789. FROM $wpdb->postmeta AS postmeta
  790. INNER JOIN $wpdb->posts AS posts ON ( posts.post_type = 'shop_order_refund' AND posts.post_parent = %d )
  791. WHERE postmeta.meta_key = '_refund_amount'
  792. AND postmeta.post_id = posts.ID",
  793. $refunded_order->ID
  794. )
  795. );
  796. if ( $order_total > $refunded_total ) {
  797. wc_create_refund(
  798. array(
  799. 'amount' => $order_total - $refunded_total,
  800. 'reason' => __( 'Order fully refunded', 'woocommerce' ),
  801. 'order_id' => $refunded_order->ID,
  802. 'line_items' => array(),
  803. 'date' => $refunded_order->post_modified,
  804. )
  805. );
  806. }
  807. }
  808. wc_delete_shop_order_transients();
  809. }
  810. /**
  811. * Update DB version for 2.4
  812. *
  813. * @return void
  814. */
  815. function wc_update_240_db_version() {
  816. WC_Install::update_db_version( '2.4.0' );
  817. }
  818. /**
  819. * Update variations for 2.4.1
  820. *
  821. * @return void
  822. */
  823. function wc_update_241_variations() {
  824. global $wpdb;
  825. // Select variations that don't have any _stock_status implemented on WooCommerce 2.2.
  826. $update_variations = $wpdb->get_results(
  827. "SELECT DISTINCT posts.ID AS variation_id, posts.post_parent AS variation_parent
  828. FROM {$wpdb->posts} as posts
  829. LEFT OUTER JOIN {$wpdb->postmeta} AS postmeta ON posts.ID = postmeta.post_id AND postmeta.meta_key = '_stock_status'
  830. WHERE posts.post_type = 'product_variation'
  831. AND postmeta.meta_value IS NULL"
  832. );
  833. foreach ( $update_variations as $variation ) {
  834. // Get the parent _stock_status.
  835. $parent_stock_status = get_post_meta( $variation->variation_parent, '_stock_status', true );
  836. // Set the _stock_status.
  837. add_post_meta( $variation->variation_id, '_stock_status', $parent_stock_status ? $parent_stock_status : 'instock', true );
  838. // Delete old product children array.
  839. delete_transient( 'wc_product_children_' . $variation->variation_parent );
  840. }
  841. // Invalidate old transients such as wc_var_price.
  842. WC_Cache_Helper::get_transient_version( 'product', true );
  843. }
  844. /**
  845. * Update DB version for 2.4.1
  846. *
  847. * @return void
  848. */
  849. function wc_update_241_db_version() {
  850. WC_Install::update_db_version( '2.4.1' );
  851. }
  852. /**
  853. * Update currency settings for 2.5
  854. *
  855. * @return void
  856. */
  857. function wc_update_250_currency() {
  858. global $wpdb;
  859. // Fix currency settings for LAK currency.
  860. $current_currency = get_option( 'woocommerce_currency' );
  861. if ( 'KIP' === $current_currency ) {
  862. update_option( 'woocommerce_currency', 'LAK' );
  863. }
  864. // Update LAK currency code.
  865. $wpdb->update(
  866. $wpdb->postmeta,
  867. array(
  868. 'meta_value' => 'LAK',
  869. ),
  870. array(
  871. 'meta_key' => '_order_currency',
  872. 'meta_value' => 'KIP',
  873. )
  874. );
  875. }
  876. /**
  877. * Update DB version for 2.5
  878. *
  879. * @return void
  880. */
  881. function wc_update_250_db_version() {
  882. WC_Install::update_db_version( '2.5.0' );
  883. }
  884. /**
  885. * Update ship to countries options for 2.6
  886. *
  887. * @return void
  888. */
  889. function wc_update_260_options() {
  890. // woocommerce_calc_shipping option has been removed in 2.6.
  891. if ( 'no' === get_option( 'woocommerce_calc_shipping' ) ) {
  892. update_option( 'woocommerce_ship_to_countries', 'disabled' );
  893. }
  894. WC_Admin_Notices::add_notice( 'legacy_shipping' );
  895. }
  896. /**
  897. * Update term meta for 2.6
  898. *
  899. * @return void
  900. */
  901. function wc_update_260_termmeta() {
  902. global $wpdb;
  903. /**
  904. * Migrate term meta to WordPress tables.
  905. */
  906. if ( get_option( 'db_version' ) >= 34370 && $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}woocommerce_termmeta';" ) ) {
  907. 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;" ) ) {
  908. $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}woocommerce_termmeta" );
  909. wp_cache_flush();
  910. }
  911. }
  912. }
  913. /**
  914. * Update zones for 2.6
  915. *
  916. * @return void
  917. */
  918. function wc_update_260_zones() {
  919. global $wpdb;
  920. /**
  921. * Old (table rate) shipping zones to new core shipping zones migration.
  922. * zone_enabled and zone_type are no longer used, but it's safe to leave them be.
  923. */
  924. if ( $wpdb->get_var( "SHOW COLUMNS FROM `{$wpdb->prefix}woocommerce_shipping_zones` LIKE 'zone_enabled';" ) ) {
  925. $wpdb->query( "ALTER TABLE {$wpdb->prefix}woocommerce_shipping_zones CHANGE `zone_type` `zone_type` VARCHAR(40) NOT NULL DEFAULT '';" );
  926. $wpdb->query( "ALTER TABLE {$wpdb->prefix}woocommerce_shipping_zones CHANGE `zone_enabled` `zone_enabled` INT(1) NOT NULL DEFAULT 1;" );
  927. }
  928. }
  929. /**
  930. * Update zone methods for 2.6
  931. *
  932. * @return void
  933. */
  934. function wc_update_260_zone_methods() {
  935. global $wpdb;
  936. /**
  937. * Shipping zones in WC 2.6.0 use a table named woocommerce_shipping_zone_methods.
  938. * 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).
  939. */
  940. if ( $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}woocommerce_shipping_zone_shipping_methods';" ) ) {
  941. $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;" );
  942. if ( $old_methods ) {
  943. $max_new_id = $wpdb->get_var( "SELECT MAX(instance_id) FROM {$wpdb->prefix}woocommerce_shipping_zone_methods" );
  944. $max_old_id = $wpdb->get_var( "SELECT MAX(shipping_method_id) FROM {$wpdb->prefix}woocommerce_shipping_zone_shipping_methods" );
  945. // Avoid ID conflicts.
  946. $wpdb->query( $wpdb->prepare( "ALTER TABLE {$wpdb->prefix}woocommerce_shipping_zone_methods AUTO_INCREMENT = %d;", max( $max_new_id, $max_old_id ) + 1 ) );
  947. // Store changes.
  948. $changes = array();
  949. // Move data.
  950. foreach ( $old_methods as $old_method ) {
  951. $wpdb->insert(
  952. $wpdb->prefix . 'woocommerce_shipping_zone_methods', array(
  953. 'zone_id' => $old_method->zone_id,
  954. 'method_id' => $old_method->shipping_method_type,
  955. 'method_order' => $old_method->shipping_method_order,
  956. )
  957. );
  958. $new_instance_id = $wpdb->insert_id;
  959. // Move main settings.
  960. $older_settings_key = 'woocommerce_' . $old_method->shipping_method_type . '-' . $old_method->shipping_method_id . '_settings';
  961. $old_settings_key = 'woocommerce_' . $old_method->shipping_method_type . '_' . $old_method->shipping_method_id . '_settings';
  962. add_option( 'woocommerce_' . $old_method->shipping_method_type . '_' . $new_instance_id . '_settings', get_option( $old_settings_key, get_option( $older_settings_key ) ) );
  963. // Handling for table rate and flat rate box shipping.
  964. if ( 'table_rate' === $old_method->shipping_method_type ) {
  965. // Move priority settings.
  966. add_option( 'woocommerce_table_rate_default_priority_' . $new_instance_id, get_option( 'woocommerce_table_rate_default_priority_' . $old_method->shipping_method_id ) );
  967. add_option( 'woocommerce_table_rate_priorities_' . $new_instance_id, get_option( 'woocommerce_table_rate_priorities_' . $old_method->shipping_method_id ) );
  968. // Move rates.
  969. $wpdb->update(
  970. $wpdb->prefix . 'woocommerce_shipping_table_rates',
  971. array(
  972. 'shipping_method_id' => $new_instance_id,
  973. ),
  974. array(
  975. 'shipping_method_id' => $old_method->shipping_method_id,
  976. )
  977. );
  978. } elseif ( 'flat_rate_boxes' === $old_method->shipping_method_type ) {
  979. $wpdb->update(
  980. $wpdb->prefix . 'woocommerce_shipping_flat_rate_boxes',
  981. array(
  982. 'shipping_method_id' => $new_instance_id,
  983. ),
  984. array(
  985. 'shipping_method_id' => $old_method->shipping_method_id,
  986. )
  987. );
  988. }
  989. $changes[ $old_method->shipping_method_id ] = $new_instance_id;
  990. }
  991. // $changes contains keys (old method ids) and values (new instance ids) if extra processing is needed in plugins.
  992. // Store this to an option so extensions can pick it up later, then fire an action.
  993. update_option( 'woocommerce_updated_instance_ids', $changes );
  994. do_action( 'woocommerce_updated_instance_ids', $changes );
  995. }
  996. }
  997. // Change ranges used to ...
  998. $wpdb->query( "UPDATE {$wpdb->prefix}woocommerce_shipping_zone_locations SET location_code = REPLACE( location_code, '-', '...' );" );
  999. }
  1000. /**
  1001. * Update refunds for 2.6
  1002. *
  1003. * @return void
  1004. */
  1005. function wc_update_260_refunds() {
  1006. global $wpdb;
  1007. /**
  1008. * Refund item qty should be negative.
  1009. */
  1010. $wpdb->query(
  1011. "UPDATE {$wpdb->prefix}woocommerce_order_itemmeta as item_meta
  1012. LEFT JOIN {$wpdb->prefix}woocommerce_order_items as items ON item_meta.order_item_id = items.order_item_id
  1013. LEFT JOIN {$wpdb->posts} as posts ON items.order_id = posts.ID
  1014. SET item_meta.meta_value = item_meta.meta_value * -1
  1015. WHERE item_meta.meta_value > 0 AND item_meta.meta_key = '_qty' AND posts.post_type = 'shop_order_refund'"
  1016. );
  1017. }
  1018. /**
  1019. * Update DB version for 2.6
  1020. *
  1021. * @return void
  1022. */
  1023. function wc_update_260_db_version() {
  1024. WC_Install::update_db_version( '2.6.0' );
  1025. }
  1026. /**
  1027. * Update webhooks for 3.0
  1028. *
  1029. * @return void
  1030. */
  1031. function wc_update_300_webhooks() {
  1032. /**
  1033. * Make sure product.update webhooks get the woocommerce_product_quick_edit_save
  1034. * and woocommerce_product_bulk_edit_save hooks.
  1035. */
  1036. $product_update_webhooks = get_posts(
  1037. array(
  1038. 'posts_per_page' => -1,
  1039. 'post_type' => 'shop_webhook',
  1040. 'meta_key' => '_topic',
  1041. 'meta_value' => 'product.updated',
  1042. )
  1043. );
  1044. foreach ( $product_update_webhooks as $product_update_webhook ) {
  1045. $webhook = new WC_Webhook( $product_update_webhook->ID );
  1046. $webhook->set_topic( 'product.updated' );
  1047. }
  1048. }
  1049. /**
  1050. * Add an index to the field comment_type to improve the response time of the query
  1051. * used by WC_Comments::wp_count_comments() to get the number of comments by type.
  1052. */
  1053. function wc_update_300_comment_type_index() {
  1054. global $wpdb;
  1055. $index_exists = $wpdb->get_row( "SHOW INDEX FROM {$wpdb->comments} WHERE column_name = 'comment_type' and key_name = 'woo_idx_comment_type'" );
  1056. if ( is_null( $index_exists ) ) {
  1057. // Add an index to the field comment_type to improve the response time of the query
  1058. // used by WC_Comments::wp_count_comments() to get the number of comments by type.
  1059. $wpdb->query( "ALTER TABLE {$wpdb->comments} ADD INDEX woo_idx_comment_type (comment_type)" );
  1060. }
  1061. }
  1062. /**
  1063. * Update grouped products for 3.0
  1064. *
  1065. * @return void
  1066. */
  1067. function wc_update_300_grouped_products() {
  1068. global $wpdb;
  1069. $parents = $wpdb->get_col( "SELECT DISTINCT( post_parent ) FROM {$wpdb->posts} WHERE post_parent > 0 AND post_type = 'product';" );
  1070. foreach ( $parents as $parent_id ) {
  1071. $parent = wc_get_product( $parent_id );
  1072. if ( $parent && $parent->is_type( 'grouped' ) ) {
  1073. $children_ids = get_posts(
  1074. array(
  1075. 'post_parent' => $parent_id,
  1076. 'posts_per_page' => -1,
  1077. 'post_type' => 'product',
  1078. 'fields' => 'ids',
  1079. )
  1080. );
  1081. update_post_meta( $parent_id, '_children', $children_ids );
  1082. // Update children to remove the parent.
  1083. $wpdb->update(
  1084. $wpdb->posts,
  1085. array(
  1086. 'post_parent' => 0,
  1087. ),
  1088. array(
  1089. 'post_parent' => $parent_id,
  1090. )
  1091. );
  1092. }
  1093. }
  1094. }
  1095. /**
  1096. * Update shipping tax classes for 3.0
  1097. *
  1098. * @return void
  1099. */
  1100. function wc_update_300_settings() {
  1101. $woocommerce_shipping_tax_class = get_option( 'woocommerce_shipping_tax_class' );
  1102. if ( '' === $woocommerce_shipping_tax_class ) {
  1103. update_option( 'woocommerce_shipping_tax_class', 'inherit' );
  1104. } elseif ( 'standard' === $woocommerce_shipping_tax_class ) {
  1105. update_option( 'woocommerce_shipping_tax_class', '' );
  1106. }
  1107. }
  1108. /**
  1109. * Convert meta values into term for product visibility.
  1110. */
  1111. function wc_update_300_product_visibility() {
  1112. global $wpdb;
  1113. WC_Install::create_terms();
  1114. $featured_term = get_term_by( 'name', 'featured', 'product_visibility' );
  1115. if ( $featured_term ) {
  1116. $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 ) );
  1117. }
  1118. $exclude_search_term = get_term_by( 'name', 'exclude-from-search', 'product_visibility' );
  1119. if ( $exclude_search_term ) {
  1120. $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 ) );
  1121. }
  1122. $exclude_catalog_term = get_term_by( 'name', 'exclude-from-catalog', 'product_visibility' );
  1123. if ( $exclude_catalog_term ) {
  1124. $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 ) );
  1125. }
  1126. $outofstock_term = get_term_by( 'name', 'outofstock', 'product_visibility' );
  1127. if ( $outofstock_term ) {
  1128. $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 ) );
  1129. }
  1130. $rating_term = get_term_by( 'name', 'rated-1', 'product_visibility' );
  1131. if ( $rating_term ) {
  1132. $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 ) );
  1133. }
  1134. $rating_term = get_term_by( 'name', 'rated-2', 'product_visibility' );
  1135. if ( $rating_term ) {
  1136. $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 ) );
  1137. }
  1138. $rating_term = get_term_by( 'name', 'rated-3', 'product_visibility' );
  1139. if ( $rating_term ) {
  1140. $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 ) );
  1141. }
  1142. $rating_term = get_term_by( 'name', 'rated-4', 'product_visibility' );
  1143. if ( $rating_term ) {
  1144. $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 ) );
  1145. }
  1146. $rating_term = get_term_by( 'name', 'rated-5', 'product_visibility' );
  1147. if ( $rating_term ) {
  1148. $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 ) );
  1149. }
  1150. }
  1151. /**
  1152. * Update DB Version.
  1153. */
  1154. function wc_update_300_db_version() {
  1155. WC_Install::update_db_version( '3.0.0' );
  1156. }
  1157. /**
  1158. * Add an index to the downloadable product permissions table to improve performance of update_user_by_order_id.
  1159. */
  1160. function wc_update_310_downloadable_products() {
  1161. global $wpdb;
  1162. $index_exists = $wpdb->get_row( "SHOW INDEX FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions WHERE column_name = 'order_id' and key_name = 'order_id'" );
  1163. if ( is_null( $index_exists ) ) {
  1164. $wpdb->query( "ALTER TABLE {$wpdb->prefix}woocommerce_downloadable_product_permissions ADD INDEX order_id (order_id)" );
  1165. }
  1166. }
  1167. /**
  1168. * Find old order notes and ensure they have the correct type for exclusion.
  1169. */
  1170. function wc_update_310_old_comments() {
  1171. global $wpdb;
  1172. $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 = '';" );
  1173. }
  1174. /**
  1175. * Update DB Version.
  1176. */
  1177. function wc_update_310_db_version() {
  1178. WC_Install::update_db_version( '3.1.0' );
  1179. }
  1180. /**
  1181. * Update shop_manager capabilities.
  1182. */
  1183. function wc_update_312_shop_manager_capabilities() {
  1184. $role = get_role( 'shop_manager' );
  1185. $role->remove_cap( 'unfiltered_html' );
  1186. }
  1187. /**
  1188. * Update DB Version.
  1189. */
  1190. function wc_update_312_db_version() {
  1191. WC_Install::update_db_version( '3.1.2' );
  1192. }
  1193. /**
  1194. * Update state codes for Mexico.
  1195. */
  1196. function wc_update_320_mexican_states() {
  1197. global $wpdb;
  1198. $mx_states = array(
  1199. 'Distrito Federal' => 'CMX',
  1200. 'Jalisco' => 'JAL',
  1201. 'Nuevo Leon' => 'NLE',
  1202. 'Aguascalientes' => 'AGS',
  1203. 'Baja California' => 'BCN',
  1204. 'Baja California Sur' => 'BCS',
  1205. 'Campeche' => 'CAM',
  1206. 'Chiapas' => 'CHP',
  1207. 'Chihuahua' => 'CHH',
  1208. 'Coahuila' => 'COA',
  1209. 'Colima' => 'COL',
  1210. 'Durango' => 'DGO',
  1211. 'Guanajuato' => 'GTO',
  1212. 'Guerrero' => 'GRO',
  1213. 'Hidalgo' => 'HGO',
  1214. 'Estado de Mexico' => 'MEX',
  1215. 'Michoacan' => 'MIC',
  1216. 'Morelos' => 'MOR',
  1217. 'Nayarit' => 'NAY',
  1218. 'Oaxaca' => 'OAX',
  1219. 'Puebla' => 'PUE',
  1220. 'Queretaro' => 'QRO',
  1221. 'Quintana Roo' => 'ROO',
  1222. 'San Luis Potosi' => 'SLP',
  1223. 'Sinaloa' => 'SIN',
  1224. 'Sonora' => 'SON',
  1225. 'Tabasco' => 'TAB',
  1226. 'Tamaulipas' => 'TMP',
  1227. 'Tlaxcala' => 'TLA',
  1228. 'Veracruz' => 'VER',
  1229. 'Yucatan' => 'YUC',
  1230. 'Zacatecas' => 'ZAC',
  1231. );
  1232. foreach ( $mx_states as $old => $new ) {
  1233. $wpdb->query(
  1234. $wpdb->prepare(
  1235. "UPDATE $wpdb->postmeta
  1236. SET meta_value = %s
  1237. WHERE meta_key IN ( '_billing_state', '_shipping_state' )
  1238. AND meta_value = %s",
  1239. $new, $old
  1240. )
  1241. );
  1242. $wpdb->update(
  1243. "{$wpdb->prefix}woocommerce_shipping_zone_locations",
  1244. array(
  1245. 'location_code' => 'MX:' . $new,
  1246. ),
  1247. array(
  1248. 'location_code' => 'MX:' . $old,
  1249. )
  1250. );
  1251. $wpdb->update(
  1252. "{$wpdb->prefix}woocommerce_tax_rates",
  1253. array(
  1254. 'tax_rate_state' => strtoupper( $new ),
  1255. ),
  1256. array(
  1257. 'tax_rate_state' => strtoupper( $old ),
  1258. )
  1259. );
  1260. }
  1261. }
  1262. /**
  1263. * Update DB Version.
  1264. */
  1265. function wc_update_320_db_version() {
  1266. WC_Install::update_db_version( '3.2.0' );
  1267. }
  1268. /**
  1269. * Update image settings to use new aspect ratios and widths.
  1270. */
  1271. function wc_update_330_image_options() {
  1272. $old_thumbnail_size = get_option( 'shop_catalog_image_size', array() );
  1273. $old_single_size = get_option( 'shop_single_image_size', array() );
  1274. if ( ! empty( $old_thumbnail_size['width'] ) ) {
  1275. $width = absint( $old_thumbnail_size['width'] );
  1276. $height = absint( $old_thumbnail_size['height'] );
  1277. $hard_crop = ! empty( $old_thumbnail_size['crop'] );
  1278. if ( ! $width ) {
  1279. $width = 300;
  1280. }
  1281. if ( ! $height ) {
  1282. $height = $width;
  1283. }
  1284. update_option( 'woocommerce_thumbnail_image_width', $width );
  1285. // Calculate cropping mode from old image options.
  1286. if ( ! $hard_crop ) {
  1287. update_option( 'woocommerce_thumbnail_cropping', 'uncropped' );
  1288. } elseif ( $width === $height ) {
  1289. update_option( 'woocommerce_thumbnail_cropping', '1:1' );
  1290. } else {
  1291. $ratio = $width / $height;
  1292. $fraction = wc_decimal_to_fraction( $ratio );
  1293. if ( $fraction ) {
  1294. update_option( 'woocommerce_thumbnail_cropping', 'custom' );
  1295. update_option( 'woocommerce_thumbnail_cropping_custom_width', $fraction[0] );
  1296. update_option( 'woocommerce_thumbnail_cropping_custom_height', $fraction[1] );
  1297. }
  1298. }
  1299. }
  1300. // Single is uncropped.
  1301. if ( ! empty( $old_single_size['width'] ) ) {
  1302. update_option( 'woocommerce_single_image_width', absint( $old_single_size['width'] ) );
  1303. }
  1304. }
  1305. /**
  1306. * Migrate webhooks from post type to CRUD.
  1307. */
  1308. function wc_update_330_webhooks() {
  1309. register_post_type( 'shop_webhook' );
  1310. // Map statuses from post_type to Webhooks CRUD.
  1311. $statuses = array(
  1312. 'publish' => 'active',
  1313. 'draft' => 'paused',
  1314. 'pending' => 'disabled',
  1315. );
  1316. $posts = get_posts( array(
  1317. 'posts_per_page' => -1,
  1318. 'post_type' => 'shop_webhook',
  1319. 'post_status' => 'any',
  1320. ) );
  1321. foreach ( $posts as $post ) {
  1322. $webhook = new WC_Webhook();
  1323. $webhook->set_name( $post->post_title );
  1324. $webhook->set_status( isset( $statuses[ $post->post_status ] ) ? $statuses[ $post->post_status ] : 'disabled' );
  1325. $webhook->set_delivery_url( get_post_meta( $post->ID, '_delivery_url', true ) );
  1326. $webhook->set_secret( get_post_meta( $post->ID, '_secret', true ) );
  1327. $webhook->set_topic( get_post_meta( $post->ID, '_topic', true ) );
  1328. $webhook->set_api_version( get_post_meta( $post->ID, '_api_version', true ) );
  1329. $webhook->set_user_id( $post->post_author );
  1330. $webhook->set_pending_delivery( false );
  1331. $webhook->save();
  1332. wp_delete_post( $post->ID, true );
  1333. }
  1334. unregister_post_type( 'shop_webhook' );
  1335. }
  1336. /**
  1337. * Assign default cat to all products with no cats.
  1338. */
  1339. function wc_update_330_set_default_product_cat() {
  1340. global $wpdb;
  1341. $default_category = get_option( 'default_product_cat', 0 );
  1342. if ( $default_category ) {
  1343. $result = $wpdb->query( $wpdb->prepare( "
  1344. INSERT INTO {$wpdb->term_relationships} (object_id, term_taxonomy_id)
  1345. SELECT DISTINCT posts.ID, %s FROM {$wpdb->posts} posts
  1346. LEFT JOIN
  1347. (
  1348. SELECT object_id FROM {$wpdb->term_relationships} term_relationships
  1349. LEFT JOIN {$wpdb->term_taxonomy} term_taxonomy ON term_relationships.term_taxonomy_id = term_taxonomy.term_taxonomy_id
  1350. WHERE term_taxonomy.taxonomy = 'product_cat'
  1351. ) AS tax_query
  1352. ON posts.ID = tax_query.object_id
  1353. WHERE posts.post_type = 'product'
  1354. AND tax_query.object_id IS NULL
  1355. ", $default_category ) );
  1356. wp_cache_flush();
  1357. delete_transient( 'wc_term_counts' );
  1358. wp_update_term_count_now( array( $default_category ), 'product_cat' );
  1359. }
  1360. }
  1361. /**
  1362. * Update product stock status to use the new onbackorder status.
  1363. */
  1364. function wc_update_330_product_stock_status() {
  1365. global $wpdb;
  1366. if ( 'yes' !== get_option( 'woocommerce_manage_stock' ) ) {
  1367. return;
  1368. }
  1369. $min_stock_amount = (int) get_option( 'woocommerce_notify_no_stock_amount', 0 );
  1370. // Get all products that have stock management enabled, stock less than or equal to min stock amount, and backorders enabled.
  1371. $post_ids = $wpdb->get_col( $wpdb->prepare( "
  1372. SELECT t1.post_id FROM $wpdb->postmeta t1
  1373. INNER JOIN $wpdb->postmeta t2
  1374. ON t1.post_id = t2.post_id
  1375. AND t1.meta_key = '_manage_stock' AND t1.meta_value = 'yes'
  1376. AND t2.meta_key = '_stock' AND t2.meta_value <= %d
  1377. INNER JOIN $wpdb->postmeta t3
  1378. ON t2.post_id = t3.post_id
  1379. AND t3.meta_key = '_backorders' AND ( t3.meta_value = 'yes' OR t3.meta_value = 'notify' )
  1380. ", $min_stock_amount ) ); // WPCS: db call ok, unprepared SQL ok, cache ok.
  1381. if ( empty( $post_ids ) ) {
  1382. return;
  1383. }
  1384. $post_ids = array_map( 'absint', $post_ids );
  1385. // Set the status to onbackorder for those products.
  1386. $wpdb->query( "
  1387. UPDATE $wpdb->postmeta
  1388. SET meta_value = 'onbackorder'
  1389. WHERE meta_key = '_stock_status' AND post_id IN ( " . implode( ',', $post_ids ) . ' )
  1390. ' ); // WPCS: db call ok, unprepared SQL ok, cache ok.
  1391. }
  1392. /**
  1393. * Clear addons page transients
  1394. */
  1395. function wc_update_330_clear_transients() {
  1396. delete_transient( 'wc_addons_sections' );
  1397. delete_transient( 'wc_addons_featured' );
  1398. }
  1399. /**
  1400. * Set PayPal's sandbox credentials.
  1401. */
  1402. function wc_update_330_set_paypal_sandbox_credentials() {
  1403. $paypal_settings = get_option( 'woocommerce_paypal_settings' );
  1404. if ( isset( $paypal_settings['testmode'] ) && 'yes' === $paypal_settings['testmode'] ) {
  1405. foreach ( array( 'api_username', 'api_password', 'api_signature' ) as $credential ) {
  1406. if ( ! empty( $paypal_settings[ $credential ] ) ) {
  1407. $paypal_settings[ 'sandbox_' . $credential ] = $paypal_settings[ $credential ];
  1408. }
  1409. }
  1410. update_option( 'woocommerce_paypal_settings', $paypal_settings );
  1411. }
  1412. }
  1413. /**
  1414. * Update DB Version.
  1415. */
  1416. function wc_update_330_db_version() {
  1417. WC_Install::update_db_version( '3.3.0' );
  1418. }
  1419. /**
  1420. * Update state codes for Ireland and BD.
  1421. */
  1422. function wc_update_340_states() {
  1423. $country_states = array(
  1424. 'IE' => array(
  1425. 'CK' => 'CO',
  1426. 'DN' => 'D',
  1427. 'GY' => 'G',
  1428. 'TY' => 'TA',
  1429. ),
  1430. 'BD' => array(
  1431. 'BAG' => 'BD-05',
  1432. 'BAN' => 'BD-01',
  1433. 'BAR' => 'BD-02',
  1434. 'BARI' => 'BD-06',
  1435. 'BHO' => 'BD-07',
  1436. 'BOG' => 'BD-03',
  1437. 'BRA' => 'BD-04',
  1438. 'CHA' => 'BD-09',
  1439. 'CHI' => 'BD-10',
  1440. 'CHU' => 'BD-12',
  1441. 'COX' => 'BD-11',
  1442. 'COM' => 'BD-08',
  1443. 'DHA' => 'BD-13',
  1444. 'DIN' => 'BD-14',
  1445. 'FAR' => 'BD-15',
  1446. 'FEN' => 'BD-16',
  1447. 'GAI' => 'BD-19',
  1448. 'GAZI' => 'BD-18',
  1449. 'GOP' => 'BD-17',
  1450. 'HAB' => 'BD-20',
  1451. 'JAM' => 'BD-21',
  1452. 'JES' => 'BD-22',
  1453. 'JHA' => 'BD-25',
  1454. 'JHE' => 'BD-23',
  1455. 'JOY' => 'BD-24',
  1456. 'KHA' => 'BD-29',
  1457. 'KHU' => 'BD-27',
  1458. 'KIS' => 'BD-26',
  1459. 'KUR' => 'BD-28',
  1460. 'KUS' => 'BD-30',
  1461. 'LAK' => 'BD-31',
  1462. 'LAL' => 'BD-32',
  1463. 'MAD' => 'BD-36',
  1464. 'MAG' => 'BD-37',
  1465. 'MAN' => 'BD-33',
  1466. 'MEH' => 'BD-39',
  1467. 'MOU' => 'BD-38',
  1468. 'MUN' => 'BD-35',
  1469. 'MYM' => 'BD-34',
  1470. 'NAO' => 'BD-48',
  1471. 'NAR' => 'BD-43',
  1472. 'NARG' => 'BD-40',
  1473. 'NARD' => 'BD-42',
  1474. 'NAT' => 'BD-44',
  1475. 'NAW' => 'BD-45',
  1476. 'NET' => 'BD-41',
  1477. 'NIL' => 'BD-46',
  1478. 'NOA' => 'BD-47',
  1479. 'PAB' => 'BD-49',
  1480. 'PAN' => 'BD-52',
  1481. 'PAT' => 'BD-51',
  1482. 'PIR' => 'BD-50',
  1483. 'RAJB' => 'BD-53',
  1484. 'RAJ' => 'BD-54',
  1485. 'RAN' => 'BD-56',
  1486. 'RANP' => 'BD-55',
  1487. 'SAT' => 'BD-58',
  1488. 'SHA' => 'BD-57',
  1489. 'SIR' => 'BD-59',
  1490. 'SUN' => 'BD-61',
  1491. 'SYL' => 'BD-60',
  1492. 'TAN' => 'BD-63',
  1493. 'THA' => 'BD-64',
  1494. ),
  1495. );
  1496. update_option( 'woocommerce_update_340_states', $country_states );
  1497. }
  1498. /**
  1499. * Update next state in the queue.
  1500. *
  1501. * @return bool True to run again, false if completed.
  1502. */
  1503. function wc_update_340_state() {
  1504. global $wpdb;
  1505. $country_states = array_filter( (array) get_option( 'woocommerce_update_340_states', array() ) );
  1506. if ( empty( $country_states ) ) {
  1507. return false;
  1508. }
  1509. foreach ( $country_states as $country => $states ) {
  1510. foreach ( $states as $old => $new ) {
  1511. $wpdb->query(
  1512. $wpdb->prepare(
  1513. "UPDATE $wpdb->postmeta
  1514. SET meta_value = %s
  1515. WHERE meta_key IN ( '_billing_state', '_shipping_state' )
  1516. AND meta_value = %s",
  1517. $new, $old
  1518. )
  1519. );
  1520. $wpdb->update(
  1521. "{$wpdb->prefix}woocommerce_shipping_zone_locations",
  1522. array(
  1523. 'location_code' => $country . ':' . $new,
  1524. ),
  1525. array(
  1526. 'location_code' => $country . ':' . $old,
  1527. )
  1528. );
  1529. $wpdb->update(
  1530. "{$wpdb->prefix}woocommerce_tax_rates",
  1531. array(
  1532. 'tax_rate_state' => strtoupper( $new ),
  1533. ),
  1534. array(
  1535. 'tax_rate_state' => strtoupper( $old ),
  1536. )
  1537. );
  1538. unset( $country_states[ $country ][ $old ] );
  1539. if ( empty( $country_states[ $country ] ) ) {
  1540. unset( $country_states[ $country ] );
  1541. }
  1542. break 2;
  1543. }
  1544. }
  1545. if ( ! empty( $country_states ) ) {
  1546. return update_option( 'woocommerce_update_340_states', $country_states );
  1547. }
  1548. delete_option( 'woocommerce_update_340_states' );
  1549. return false;
  1550. }
  1551. /**
  1552. * Set last active prop for users.
  1553. */
  1554. function wc_update_340_last_active() {
  1555. global $wpdb;
  1556. // @codingStandardsIgnoreStart.
  1557. $wpdb->query(
  1558. $wpdb->prepare( "
  1559. INSERT INTO {$wpdb->usermeta} (user_id, meta_key, meta_value)
  1560. SELECT DISTINCT users.ID, 'wc_last_active', %s
  1561. FROM {$wpdb->users} as users
  1562. LEFT OUTER JOIN {$wpdb->usermeta} AS usermeta ON users.ID = usermeta.user_id AND usermeta.meta_key = 'wc_last_active'
  1563. WHERE usermeta.meta_value IS NULL
  1564. ",
  1565. (string) strtotime( date( 'Y-m-d', current_time( 'timestamp', true ) ) )
  1566. )
  1567. );
  1568. // @codingStandardsIgnoreEnd.
  1569. }
  1570. /**
  1571. * Update DB Version.
  1572. */
  1573. function wc_update_340_db_version() {
  1574. WC_Install::update_db_version( '3.4.0' );
  1575. }
  1576. /**
  1577. * Remove duplicate foreign keys
  1578. *
  1579. * @return void
  1580. */
  1581. function wc_update_343_cleanup_foreign_keys() {
  1582. global $wpdb;
  1583. $results = $wpdb->get_results( "
  1584. SELECT CONSTRAINT_NAME
  1585. FROM information_schema.TABLE_CONSTRAINTS
  1586. WHERE CONSTRAINT_SCHEMA = '{$wpdb->dbname}'
  1587. AND CONSTRAINT_NAME LIKE '%wc_download_log_ib%'
  1588. AND CONSTRAINT_TYPE = 'FOREIGN KEY'
  1589. AND TABLE_NAME = '{$wpdb->prefix}wc_download_log'
  1590. " );
  1591. if ( $results ) {
  1592. foreach ( $results as $fk ) {
  1593. $wpdb->query( "ALTER TABLE {$wpdb->prefix}wc_download_log DROP FOREIGN KEY {$fk->CONSTRAINT_NAME}" ); // phpcs:ignore WordPress.WP.PreparedSQL.NotPrepared
  1594. }
  1595. }
  1596. }
  1597. /**
  1598. * Update DB version.
  1599. *
  1600. * @return void
  1601. */
  1602. function wc_update_343_db_version() {
  1603. WC_Install::update_db_version( '3.4.3' );
  1604. }