WC_Predictive_Search_Synch

The WooCommerce Predictive Search LITE WC Predictive Search Synch class.

Defined (1)

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

/classes/class-wc-predictive-search-synch.php  
  1. class WC_Predictive_Search_Synch 
  2. public function __construct() { 
  3.  
  4. // Synch for post 
  5. add_action( 'init', array( $this, 'sync_process_post' ), 1 ); 
  6.  
  7. /** 
  8. * Synch for custom mysql query from 3rd party plugin 
  9. * Call below code on 3rd party plugin when create post by mysql query 
  10. * do_action( 'mysql_inserted_post', $post_id ); 
  11. */ 
  12. add_action( 'mysql_inserted_post', array( $this, 'synch_mysql_inserted_post' ) ); 
  13.  
  14. public function sync_process_post() { 
  15. add_action( 'save_post', array( $this, 'synch_save_post' ), 10, 2 ); 
  16. add_action( 'delete_post', array( $this, 'synch_delete_post' ) ); 
  17.  
  18. public function migrate_posts() { 
  19. global $wpdb; 
  20. global $wc_ps_posts_data; 
  21. global $wc_ps_postmeta_data; 
  22. global $wc_ps_product_sku_data; 
  23.  
  24. // Check if synch data is stopped at latest run then continue synch without empty all the tables 
  25. $synched_data = get_option( 'wc_predictive_search_synched_data', 0 ); 
  26.  
  27. if ( 0 == $synched_data ) { 
  28. // continue synch data from stopped post ID 
  29. $stopped_ID = $wc_ps_posts_data->get_latest_post_id(); 
  30. if ( empty( $stopped_ID ) || is_null( $stopped_ID ) ) { 
  31. $stopped_ID = 0; 
  32. } else { 
  33. // Empty all tables 
  34. $wc_ps_posts_data->empty_table(); 
  35. $wc_ps_postmeta_data->empty_table(); 
  36. $wc_ps_product_sku_data->empty_table(); 
  37.  
  38. update_option( 'wc_predictive_search_synched_data', 0 ); 
  39.  
  40. $stopped_ID = 0; 
  41.  
  42. $post_types = apply_filters( 'predictive_search_post_types_support', array( 'post', 'page', 'product' ) ); 
  43.  
  44. $all_posts = $wpdb->get_results( 
  45. $wpdb->prepare( 
  46. "SELECT ID, post_title, post_type FROM {$wpdb->posts} WHERE ID > %d AND post_status = %s AND post_type IN ('". implode("', '", $post_types ) ."') ORDER BY ID ASC" , $stopped_ID, 'publish' 
  47. ); 
  48.  
  49. if ( $all_posts ) { 
  50. foreach ( $all_posts as $item ) { 
  51. $post_id = $item->ID; 
  52.  
  53. $item_existed = $wc_ps_posts_data->get_item( $post_id ); 
  54. if ( NULL == $item_existed ) { 
  55. $wc_ps_posts_data->insert_item( $post_id, $item->post_title, $item->post_type ); 
  56.  
  57. //if ( in_array( $item->post_type, array( 'product', 'product_variation' ) ) ) { 
  58. if ( in_array( $item->post_type, array( 'product' ) ) ) { 
  59. $sku = get_post_meta( $post_id, '_sku', true ); 
  60. if ( ! empty( $sku ) && '' != trim( $sku ) ) { 
  61. $item_existed = $wc_ps_product_sku_data->get_item( $post_id ); 
  62. if ( NULL == $item_existed ) { 
  63. $wc_ps_product_sku_data->insert_item( $post_id, $sku ); 
  64.  
  65. // Migrate Product Out of Stock 
  66. $outofstock = get_post_meta( $post_id, '_stock_status', true ); 
  67. if ( ! empty( $outofstock ) && 'outofstock' == trim( $outofstock ) ) { 
  68. $wc_ps_postmeta_data->update_item_meta( $post_id, '_stock_status', 'outofstock' ); 
  69. } else { 
  70. $wc_ps_postmeta_data->delete_item_meta( $post_id, '_stock_status' ); 
  71.  
  72.  
  73. update_option( 'wc_predictive_search_synched_data', 1 ); 
  74.  
  75. public function migrate_products_out_of_stock() { 
  76. global $wpdb; 
  77. global $wc_ps_postmeta_data; 
  78.  
  79. $all_out_of_stock = $wpdb->get_results( 
  80. $wpdb->prepare( 
  81. "SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = %s AND meta_value = %s" , '_stock_status', 'outofstock' 
  82. ); 
  83.  
  84. if ( $all_out_of_stock ) { 
  85. foreach ( $all_out_of_stock as $item ) { 
  86. $wc_ps_postmeta_data->update_item_meta( $item->post_id, '_stock_status', 'outofstock' ); 
  87.  
  88. public function synch_full_database() { 
  89. $this->migrate_posts(); 
  90.  
  91. public function delete_post_data( $post_id ) { 
  92. global $wc_ps_posts_data; 
  93. global $wc_ps_postmeta_data; 
  94. global $wc_ps_product_sku_data; 
  95.  
  96. $wc_ps_posts_data->delete_item( $post_id ); 
  97. $wc_ps_postmeta_data->delete_item_metas( $post_id ); 
  98. $wc_ps_product_sku_data->delete_item( $post_id ); 
  99.  
  100. public function synch_save_post( $post_id, $post ) { 
  101. global $wpdb; 
  102. global $wc_ps_posts_data; 
  103. global $wc_ps_postmeta_data; 
  104. global $wc_ps_product_sku_data; 
  105.  
  106. $this->delete_post_data( $post_id ); 
  107.  
  108. if ( 'publish' == $post->post_status ) { 
  109.  
  110. $wc_ps_posts_data->update_item( $post_id, $post->post_title, $post->post_type ); 
  111.  
  112. if ( 'product' == $post->post_type ) { 
  113. $sku = get_post_meta( $post_id, '_sku', true ); 
  114. if ( ! empty( $sku ) && '' != trim( $sku ) ) { 
  115. $wc_ps_product_sku_data->update_item( $post_id, $sku ); 
  116.  
  117. // Migrate Product Out of Stock 
  118. $outofstock = get_post_meta( $post_id, '_stock_status', true ); 
  119. if ( ! empty( $outofstock ) && 'outofstock' == trim( $outofstock ) ) { 
  120. $wc_ps_postmeta_data->update_item_meta( $post_id, '_stock_status', 'outofstock' ); 
  121. } else { 
  122. $wc_ps_postmeta_data->delete_item_meta( $post_id, '_stock_status' ); 
  123.  
  124. if ( 'page' == $post->post_type ) { 
  125. global $woocommerce_search_page_id; 
  126.  
  127. // flush rewrite rules if page is editing is WooCommerce Search Result page 
  128. if ( $post_id == $woocommerce_search_page_id ) { 
  129. flush_rewrite_rules(); 
  130.  
  131.  
  132. public function synch_delete_post( $post_id ) { 
  133. global $wc_ps_exclude_data; 
  134.  
  135. $this->delete_post_data( $post_id ); 
  136.  
  137. $post_type = get_post_type( $post_id ); 
  138.  
  139. $wc_ps_exclude_data->delete_item( $post_id, $post_type ); 
  140.  
  141. public function synch_mysql_inserted_post( $post_id = 0 ) { 
  142. if ( $post_id < 1 ) return; 
  143.  
  144. global $wpdb; 
  145. $post_types = apply_filters( 'predictive_search_post_types_support', array( 'post', 'page', 'product' ) ); 
  146.  
  147. $item = $wpdb->get_row( 
  148. $wpdb->prepare( 
  149. "SELECT * FROM {$wpdb->posts} WHERE ID = %d AND post_status = %s AND post_type IN ('". implode("', '", $post_types ) ."')" , $post_id, 'publish' 
  150. ); 
  151.  
  152. if ( $item ) { 
  153. global $wc_ps_posts_data; 
  154. global $wc_ps_postmeta_data; 
  155. global $wc_ps_product_sku_data; 
  156.  
  157. $item_existed = $wc_ps_posts_data->get_item( $post_id ); 
  158. if ( NULL == $item_existed ) { 
  159. $wc_ps_posts_data->insert_item( $post_id, $item->post_title, $item->post_type ); 
  160.  
  161. if ( 'product' == $item->post_type ) { 
  162. $sku = get_post_meta( $post_id, '_sku', true ); 
  163. if ( ! empty( $sku ) && '' != trim( $sku ) ) { 
  164. $item_existed = $wc_ps_product_sku_data->get_item( $post_id ); 
  165. if ( NULL == $item_existed ) { 
  166. $wc_ps_product_sku_data->insert_item( $post_id, $sku ); 
  167.  
  168. // Migrate Product Out of Stock 
  169. $outofstock = get_post_meta( $post_id, '_stock_status', true ); 
  170. if ( ! empty( $outofstock ) && 'outofstock' == trim( $outofstock ) ) { 
  171. $wc_ps_postmeta_data->update_item_meta( $post_id, '_stock_status', 'outofstock' ); 
  172. } else { 
  173. $wc_ps_postmeta_data->delete_item_meta( $post_id, '_stock_status' );