PageRenderTime 51ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-content/plugins/woocommerce/includes/updates/woocommerce-update-2.2.php

https://gitlab.com/webkod3r/tripolis
PHP | 196 lines | 163 code | 16 blank | 17 comment | 8 complexity | a7de492aee6f4db21193e0ba951045ae MD5 | raw file
  1. <?php
  2. /**
  3. * Update WC to 2.2.0
  4. *
  5. * @author WooThemes
  6. * @category Admin
  7. * @package WooCommerce/Admin/Updates
  8. * @version 2.2.0
  9. */
  10. if ( ! defined( 'ABSPATH' ) ) {
  11. exit; // Exit if accessed directly
  12. }
  13. global $wpdb;
  14. // Update options
  15. $woocommerce_ship_to_destination = 'shipping';
  16. if ( get_option( 'woocommerce_ship_to_billing_address_only' ) === 'yes' ) {
  17. $woocommerce_ship_to_destination = 'billing_only';
  18. } elseif ( get_option( 'woocommerce_ship_to_billing' ) === 'yes' ) {
  19. $woocommerce_ship_to_destination = 'billing';
  20. }
  21. add_option( 'woocommerce_ship_to_destination', $woocommerce_ship_to_destination, '', 'no' );
  22. // Update order statuses
  23. $wpdb->query( "
  24. UPDATE {$wpdb->posts} as posts
  25. LEFT JOIN {$wpdb->term_relationships} AS rel ON posts.ID = rel.object_ID
  26. LEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id )
  27. LEFT JOIN {$wpdb->terms} AS term USING( term_id )
  28. SET posts.post_status = 'wc-pending'
  29. WHERE posts.post_type = 'shop_order'
  30. AND posts.post_status = 'publish'
  31. AND tax.taxonomy = 'shop_order_status'
  32. AND term.slug LIKE 'pending%';
  33. "
  34. );
  35. $wpdb->query( "
  36. UPDATE {$wpdb->posts} as posts
  37. LEFT JOIN {$wpdb->term_relationships} AS rel ON posts.ID = rel.object_ID
  38. LEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id )
  39. LEFT JOIN {$wpdb->terms} AS term USING( term_id )
  40. SET posts.post_status = 'wc-processing'
  41. WHERE posts.post_type = 'shop_order'
  42. AND posts.post_status = 'publish'
  43. AND tax.taxonomy = 'shop_order_status'
  44. AND term.slug LIKE 'processing%';
  45. "
  46. );
  47. $wpdb->query( "
  48. UPDATE {$wpdb->posts} as posts
  49. LEFT JOIN {$wpdb->term_relationships} AS rel ON posts.ID = rel.object_ID
  50. LEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id )
  51. LEFT JOIN {$wpdb->terms} AS term USING( term_id )
  52. SET posts.post_status = 'wc-on-hold'
  53. WHERE posts.post_type = 'shop_order'
  54. AND posts.post_status = 'publish'
  55. AND tax.taxonomy = 'shop_order_status'
  56. AND term.slug LIKE 'on-hold%';
  57. "
  58. );
  59. $wpdb->query( "
  60. UPDATE {$wpdb->posts} as posts
  61. LEFT JOIN {$wpdb->term_relationships} AS rel ON posts.ID = rel.object_ID
  62. LEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id )
  63. LEFT JOIN {$wpdb->terms} AS term USING( term_id )
  64. SET posts.post_status = 'wc-completed'
  65. WHERE posts.post_type = 'shop_order'
  66. AND posts.post_status = 'publish'
  67. AND tax.taxonomy = 'shop_order_status'
  68. AND term.slug LIKE 'completed%';
  69. "
  70. );
  71. $wpdb->query( "
  72. UPDATE {$wpdb->posts} as posts
  73. LEFT JOIN {$wpdb->term_relationships} AS rel ON posts.ID = rel.object_ID
  74. LEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id )
  75. LEFT JOIN {$wpdb->terms} AS term USING( term_id )
  76. SET posts.post_status = 'wc-cancelled'
  77. WHERE posts.post_type = 'shop_order'
  78. AND posts.post_status = 'publish'
  79. AND tax.taxonomy = 'shop_order_status'
  80. AND term.slug LIKE 'cancelled%';
  81. "
  82. );
  83. $wpdb->query( "
  84. UPDATE {$wpdb->posts} as posts
  85. LEFT JOIN {$wpdb->term_relationships} AS rel ON posts.ID = rel.object_ID
  86. LEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id )
  87. LEFT JOIN {$wpdb->terms} AS term USING( term_id )
  88. SET posts.post_status = 'wc-refunded'
  89. WHERE posts.post_type = 'shop_order'
  90. AND posts.post_status = 'publish'
  91. AND tax.taxonomy = 'shop_order_status'
  92. AND term.slug LIKE 'refunded%';
  93. "
  94. );
  95. $wpdb->query( "
  96. UPDATE {$wpdb->posts} as posts
  97. LEFT JOIN {$wpdb->term_relationships} AS rel ON posts.ID = rel.object_ID
  98. LEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id )
  99. LEFT JOIN {$wpdb->terms} AS term USING( term_id )
  100. SET posts.post_status = 'wc-failed'
  101. WHERE posts.post_type = 'shop_order'
  102. AND posts.post_status = 'publish'
  103. AND tax.taxonomy = 'shop_order_status'
  104. AND term.slug LIKE 'failed%';
  105. "
  106. );
  107. // Update variations which manage stock
  108. $update_variations = $wpdb->get_results( "
  109. SELECT DISTINCT posts.ID AS variation_id, posts.post_parent AS variation_parent FROM {$wpdb->posts} as posts
  110. LEFT OUTER JOIN {$wpdb->postmeta} AS postmeta ON posts.ID = postmeta.post_id AND postmeta.meta_key = '_stock'
  111. LEFT OUTER JOIN {$wpdb->postmeta} as postmeta2 ON posts.ID = postmeta2.post_id AND postmeta2.meta_key = '_manage_stock'
  112. WHERE posts.post_type = 'product_variation'
  113. AND postmeta.meta_value IS NOT NULL
  114. AND postmeta.meta_value != ''
  115. AND postmeta2.meta_value IS NULL
  116. " );
  117. foreach ( $update_variations as $variation ) {
  118. $parent_backorders = get_post_meta( $variation->variation_parent, '_backorders', true );
  119. add_post_meta( $variation->variation_id, '_manage_stock', 'yes', true );
  120. add_post_meta( $variation->variation_id, '_backorders', $parent_backorders ? $parent_backorders : 'no', true );
  121. }
  122. // Update taxonomy names with correct sanitized names
  123. $attribute_taxonomies = $wpdb->get_results( "SELECT * FROM " . $wpdb->prefix . "woocommerce_attribute_taxonomies" );
  124. foreach ( $attribute_taxonomies as $attribute_taxonomy ) {
  125. $sanitized_attribute_name = wc_sanitize_taxonomy_name( $attribute_taxonomy->attribute_name );
  126. if ( $sanitized_attribute_name !== $attribute_taxonomy->attribute_name ) {
  127. if ( ! $wpdb->get_var( $wpdb->prepare( "SELECT 1=1 FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_name = %s;", $sanitized_attribute_name ) ) ) {
  128. // Update attribute
  129. $wpdb->update(
  130. "{$wpdb->prefix}woocommerce_attribute_taxonomies",
  131. array(
  132. 'attribute_name' => $sanitized_attribute_name
  133. ),
  134. array(
  135. 'attribute_id' => $attribute_taxonomy->attribute_id
  136. )
  137. );
  138. // Update terms
  139. $wpdb->update(
  140. $wpdb->term_taxonomy,
  141. array( 'taxonomy' => wc_attribute_taxonomy_name( $sanitized_attribute_name ) ),
  142. array( 'taxonomy' => 'pa_' . $attribute_taxonomy->attribute_name )
  143. );
  144. }
  145. }
  146. }
  147. // add webhook capabilities to shop_manager/administrator role
  148. global $wp_roles;
  149. if ( class_exists( 'WP_Roles' ) ) {
  150. if ( ! isset( $wp_roles ) ) {
  151. $wp_roles = new WP_Roles();
  152. }
  153. }
  154. if ( is_object( $wp_roles ) ) {
  155. $webhook_capabilities = array(
  156. // post type
  157. 'edit_shop_webhook',
  158. 'read_shop_webhook',
  159. 'delete_shop_webhook',
  160. 'edit_shop_webhooks',
  161. 'edit_others_shop_webhooks',
  162. 'publish_shop_webhooks',
  163. 'read_private_shop_webhooks',
  164. 'delete_shop_webhooks',
  165. 'delete_private_shop_webhooks',
  166. 'delete_published_shop_webhooks',
  167. 'delete_others_shop_webhooks',
  168. 'edit_private_shop_webhooks',
  169. 'edit_published_shop_webhooks',
  170. // terms
  171. 'manage_shop_webhook_terms',
  172. 'edit_shop_webhook_terms',
  173. 'delete_shop_webhook_terms',
  174. 'assign_shop_webhook_terms'
  175. );
  176. foreach ( $webhook_capabilities as $cap ) {
  177. $wp_roles->add_cap( 'shop_manager', $cap );
  178. $wp_roles->add_cap( 'administrator', $cap );
  179. }
  180. }