Jetpack_Sync_Module_WooCommerce

The WordPress Core Jetpack Sync Module WooCommerce class.

Defined (1)

The class is defined in the following location(s).

/sync/class.jetpack-sync-module-woocommerce.php  
  1. class Jetpack_Sync_Module_WooCommerce extends Jetpack_Sync_Module { 
  2.  
  3. private $meta_whitelist = array( 
  4. '_product_id',  
  5. '_variation_id',  
  6. '_qty',  
  7. '_tax_class',  
  8. '_line_subtotal',  
  9. '_line_subtotal_tax',  
  10. '_line_total',  
  11. '_line_tax',  
  12. '_line_tax_data',  
  13. '_visibility',  
  14. ); 
  15.  
  16. private $order_item_table_name; 
  17.  
  18. public function __construct() { 
  19. global $wpdb; 
  20. $this->order_item_table_name = $wpdb->prefix . 'woocommerce_order_items'; 
  21.  
  22. // options, constants and post meta whitelists 
  23. add_filter( 'jetpack_sync_options_whitelist', array( $this, 'add_woocommerce_options_whitelist' ), 10 ); 
  24. add_filter( 'jetpack_sync_constants_whitelist', array( $this, 'add_woocommerce_constants_whitelist' ), 10 ); 
  25. add_filter( 'jetpack_sync_post_meta_whitelist', array( $this, 'add_woocommerce_post_meta_whitelist' ), 10 ); 
  26.  
  27. add_filter( 'jetpack_sync_before_enqueue_woocommerce_new_order_item', array( $this, 'filter_order_item' ) ); 
  28. add_filter( 'jetpack_sync_before_enqueue_woocommerce_update_order_item', array( $this, 'filter_order_item' ) ); 
  29.  
  30. function name() { 
  31. return "woocommerce"; 
  32.  
  33. public function init_listeners( $callable ) { 
  34. // orders 
  35. add_action( 'woocommerce_new_order', $callable, 10, 1 ); 
  36. add_action( 'woocommerce_order_status_changed', $callable, 10, 3 ); 
  37. add_action( 'woocommerce_payment_complete', $callable, 10, 1 ); 
  38.  
  39. // order items 
  40. add_action( 'woocommerce_new_order_item', $callable, 10, 4 ); 
  41. add_action( 'woocommerce_update_order_item', $callable, 10, 4 ); 
  42.  
  43. // order item meta 
  44. $this->init_listeners_for_meta_type( 'order_item', $callable ); 
  45.  
  46. public function init_full_sync_listeners( $callable ) { 
  47. add_action( 'jetpack_full_sync_woocommerce_order_items', $callable ); // also sends post meta 
  48.  
  49. public function get_full_sync_actions() { 
  50. return array( 'jetpack_full_sync_woocommerce_order_items' ); 
  51.  
  52. public function init_before_send() { 
  53. // full sync 
  54. add_filter( 'jetpack_sync_before_send_jetpack_full_sync_woocommerce_order_items', array( $this, 'expand_order_item_ids' ) ); 
  55.  
  56. public function filter_order_item( $args ) { 
  57. $args[1] = $this->build_order_item( $args[1] ); 
  58. return $args; 
  59.  
  60. public function expand_order_item_ids( $args ) { 
  61. $order_item_ids = $args[0]; 
  62.  
  63. global $wpdb; 
  64.  
  65. $order_item_ids_sql = implode( ', ', array_map( 'intval', $order_item_ids ) ); 
  66.  
  67. $order_items = $wpdb->get_results( 
  68. "SELECT * FROM $this->order_item_table_name WHERE order_item_id IN ( $order_item_ids_sql )" 
  69. ); 
  70.  
  71. return array( 
  72. $order_items,  
  73. $this->get_metadata( $order_item_ids, 'order_item', $this->meta_whitelist ) 
  74. ); 
  75.  
  76. public function build_order_item( $order_item ) { 
  77. if ( is_numeric( $order_item ) ) { 
  78. global $wpdb; 
  79. return $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $this->order_item_table_name WHERE order_item_id = %d", $order_item ) ); 
  80. } elseif ( is_array( $order_item ) ) { 
  81. return $order_item; 
  82. } else { 
  83. return (object)array( 
  84. 'order_item_id' => $order_item->get_id(),  
  85. 'order_item_type' => $order_item->get_type(),  
  86. 'order_item_name' => $order_item->get_name(),  
  87. 'order_id' => $order_item->get_order_id(),  
  88. ); 
  89.  
  90. public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { 
  91. global $wpdb; 
  92.  
  93. return $this->enqueue_all_ids_as_action( 'jetpack_full_sync_woocommerce_order_items', $this->order_item_table_name, 'order_item_id', $this->get_where_sql( $config ), $max_items_to_enqueue, $state ); 
  94.  
  95. public function estimate_full_sync_actions( $config ) { 
  96. global $wpdb; 
  97.  
  98. $query = "SELECT count(*) FROM $this->order_item_table_name WHERE " . $this->get_where_sql( $config ); 
  99. $count = $wpdb->get_var( $query ); 
  100.  
  101. return (int) ceil( $count / self::ARRAY_CHUNK_SIZE ); 
  102.  
  103. private function get_where_sql( $config ) { 
  104. return '1=1'; 
  105.  
  106. public function add_woocommerce_options_whitelist( $list ) { 
  107. return array_merge( $list, self::$wc_options_whitelist ); 
  108.  
  109. public function add_woocommerce_constants_whitelist( $list ) { 
  110. return array_merge( $list, self::$wc_constants_whitelist ); 
  111.  
  112. public function add_woocommerce_post_meta_whitelist( $list ) { 
  113. return array_merge( $list, self::$wc_post_meta_whitelist ); 
  114.  
  115. private static $wc_options_whitelist = array( 
  116. 'woocommerce_currency',  
  117. 'woocommerce_db_version',  
  118. 'woocommerce_weight_unit',  
  119. 'woocommerce_version',  
  120. 'woocommerce_unforce_ssl_checkout',  
  121. 'woocommerce_tax_total_display',  
  122. 'woocommerce_tax_round_at_subtotal',  
  123. 'woocommerce_tax_display_shop',  
  124. 'woocommerce_tax_display_cart',  
  125. 'woocommerce_prices_include_tax',  
  126. 'woocommerce_price_thousand_sep',  
  127. 'woocommerce_price_num_decimals',  
  128. 'woocommerce_price_decimal_sep',  
  129. 'woocommerce_notify_low_stock',  
  130. 'woocommerce_notify_low_stock_amount',  
  131. 'woocommerce_notify_no_stock',  
  132. 'woocommerce_notify_no_stock_amount',  
  133. 'woocommerce_manage_stock',  
  134. 'woocommerce_force_ssl_checkout',  
  135. 'woocommerce_hide_out_of_stock_items',  
  136. 'woocommerce_file_download_method',  
  137. 'woocommerce_enable_signup_and_login_from_checkout',  
  138. 'woocommerce_enable_shipping_calc',  
  139. 'woocommerce_enable_review_rating',  
  140. 'woocommerce_enable_guest_checkout',  
  141. 'woocommerce_enable_coupons',  
  142. 'woocommerce_enable_checkout_login_reminder',  
  143. 'woocommerce_enable_ajax_add_to_cart',  
  144. 'woocommerce_dimension_unit',  
  145. 'woocommerce_default_country',  
  146. 'woocommerce_default_customer_address',  
  147. 'woocommerce_currency_pos',  
  148. 'woocommerce_api_enabled',  
  149. 'woocommerce_allow_tracking',  
  150. ); 
  151.  
  152. private static $wc_constants_whitelist = array( 
  153. //woocommerce options 
  154. 'WC_PLUGIN_FILE',  
  155. 'WC_ABSPATH',  
  156. 'WC_PLUGIN_BASENAME',  
  157. 'WC_VERSION',  
  158. 'WOOCOMMERCE_VERSION',  
  159. 'WC_ROUNDING_PRECISION',  
  160. 'WC_DISCOUNT_ROUNDING_MODE',  
  161. 'WC_TAX_ROUNDING_MODE',  
  162. 'WC_DELIMITER',  
  163. 'WC_LOG_DIR',  
  164. 'WC_SESSION_CACHE_GROUP',  
  165. 'WC_TEMPLATE_DEBUG_MODE',  
  166. ); 
  167.  
  168. private static $wc_post_meta_whitelist = array( 
  169. //woocommerce products 
  170. '_stock_status',  
  171. '_visibility',  
  172. 'total_sales',  
  173. '_downloadable',  
  174. '_virtual',  
  175. '_regular_price',  
  176. '_sale_price',  
  177. '_tax_status',  
  178. '_tax_class',  
  179. '_featured',  
  180. '_price',  
  181. '_stock',  
  182. '_backorders',  
  183. '_manage_stock',  
  184.  
  185. //woocommerce orders 
  186. '_order_currency',  
  187. '_prices_include_tax',  
  188. '_created_via',  
  189. '_billing_country',  
  190. '_billing_city',  
  191. '_billing_state',  
  192. '_billing_postcode',  
  193. '_shipping_country',  
  194. '_shipping_city',  
  195. '_shipping_state',  
  196. '_shipping_postcode',  
  197. '_payment_method',  
  198. '_payment_method_title',  
  199. '_order_shipping',  
  200. '_cart_discount',  
  201. '_cart_discount_tax',  
  202. '_order_tax',  
  203. '_order_shipping_tax',  
  204. '_order_total',  
  205. '_download_permissions_granted',  
  206. '_recorded_sales',  
  207. '_order_stock_reduced',  
  208. );