WC_PS_Posts_Data

"Copyright 2012 A3 Revolution Web Design" This software is distributed under the terms of GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007.

Defined (1)

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

/classes/data/class-wc-ps-posts-data.php  
  1. class WC_PS_Posts_Data 
  2. public function install_database() { 
  3. global $wpdb; 
  4. $collate = ''; 
  5. if ( $wpdb->has_cap( 'collation' ) ) { 
  6. if( ! empty($wpdb->charset ) ) $collate .= "DEFAULT CHARACTER SET $wpdb->charset"; 
  7. if( ! empty($wpdb->collate ) ) $collate .= " COLLATE $wpdb->collate"; 
  8.  
  9. require_once ABSPATH . 'wp-admin/includes/upgrade.php'; 
  10.  
  11. $table_ps_posts = $wpdb->prefix. "ps_posts"; 
  12.  
  13. if ($wpdb->get_var("SHOW TABLES LIKE '$table_ps_posts'") != $table_ps_posts) { 
  14. $sql = "CREATE TABLE IF NOT EXISTS `{$table_ps_posts}` ( 
  15. post_id bigint(20) NOT NULL,  
  16. post_title mediumtext NOT NULL,  
  17. post_type VARCHAR(20) NOT NULL DEFAULT 'post',  
  18. PRIMARY KEY (post_id),  
  19. KEY post_type (post_type) 
  20. ) $collate; "; 
  21.  
  22. $wpdb->query($sql); 
  23.  
  24.  
  25. /** 
  26. * Predictive Search Post Table - set table name 
  27. * @return void 
  28. */ 
  29. public function set_table_wpdbfix() { 
  30. global $wpdb; 
  31. $meta_name = 'ps_posts'; 
  32.  
  33. $wpdb->ps_posts = $wpdb->prefix . $meta_name; 
  34.  
  35. $wpdb->tables[] = 'ps_posts'; 
  36.  
  37. /** 
  38. * Predictive Search Post Table - return sql 
  39. * @return void 
  40. */ 
  41. public function get_sql( $search_keyword = '', $search_keyword_nospecial = '', $post_type = 'product', $number_row, $start = 0, $check_existed = false ) { 
  42. if ( '' == $search_keyword && '' == $search_keyword_nospecial ) { 
  43. return false; 
  44.  
  45. global $wpdb; 
  46. global $wc_ps_exclude_data; 
  47.  
  48. $sql = array(); 
  49. $join = array(); 
  50. $where = array(); 
  51. $groupby = array(); 
  52. $orderby = array(); 
  53.  
  54. $items_excluded = $wc_ps_exclude_data->get_array_items( $post_type ); 
  55. if ( 'page' == $post_type ) { 
  56. global $woocommerce_search_page_id; 
  57. $items_excluded = array_merge( array( (int) $woocommerce_search_page_id ), $items_excluded ); 
  58.  
  59. $woocommerce_search_exclude_out_stock = get_option('woocommerce_search_exclude_out_stock'); 
  60. if ( 'yes' == $woocommerce_search_exclude_out_stock && in_array( $post_type, array( 'product', 'product_variation' ) ) ) { 
  61. global $wc_ps_postmeta_data; 
  62. $items_out_of_stock = $wc_ps_postmeta_data->get_array_products_out_of_stock(); 
  63. $items_excluded = array_merge( $items_out_of_stock, $items_excluded ); 
  64.  
  65. $id_excluded = ''; 
  66. if ( ! empty( $items_excluded ) ) { 
  67. $id_excluded = implode( ', ', $items_excluded ); 
  68.  
  69. $sql['select'] = array(); 
  70. if ( $check_existed ) { 
  71. $sql['select'][] = " 1 "; 
  72. } else { 
  73. $sql['select'][] = " pp.* "; 
  74.  
  75. $sql['from'] = array(); 
  76. $sql['from'][] = " {$wpdb->ps_posts} AS pp "; 
  77.  
  78. $sql['join'] = $join; 
  79.  
  80. $where[] = $wpdb->prepare( " pp.post_type = %s", $post_type ); 
  81.  
  82. if ( '' != trim( $id_excluded ) ) { 
  83. $where[] = " AND pp.post_id NOT IN ({$id_excluded}) "; 
  84.  
  85. $where_title = ' ( '; 
  86. $where_title .= WC_Predictive_Search_Functions::remove_special_characters_in_mysql( 'pp.post_title', $search_keyword ); 
  87. if ( '' != $search_keyword_nospecial ) { 
  88. $where_title .= " OR ". WC_Predictive_Search_Functions::remove_special_characters_in_mysql( 'pp.post_title', $search_keyword_nospecial ); 
  89. $search_keyword_no_s_letter = WC_Predictive_Search_Functions::remove_s_letter_at_end_word( $search_keyword ); 
  90. if ( $search_keyword_no_s_letter != false ) { 
  91. $where_title .= " OR ". WC_Predictive_Search_Functions::remove_special_characters_in_mysql( 'pp.post_title', $search_keyword_no_s_letter ); 
  92. $where_title .= ' ) '; 
  93.  
  94. $where['search'] = array(); 
  95. $where['search'][] = ' ( ' . $where_title . ' ) '; 
  96.  
  97. $sql['where'] = $where; 
  98.  
  99. $sql['groupby'] = array(); 
  100. $sql['groupby'][] = ' pp.post_id '; 
  101.  
  102. $sql['orderby'] = array(); 
  103. if ( $check_existed ) { 
  104. $sql['limit'] = " 0 , 1 "; 
  105. } else { 
  106. global $predictive_search_mode; 
  107.  
  108. $multi_keywords = explode( ' ', trim( $search_keyword ) ); 
  109. if ( 'broad' != $predictive_search_mode ) { 
  110. $sql['orderby'][] = $wpdb->prepare( " pp.post_title NOT LIKE '%s' ASC, pp.post_title ASC ", $search_keyword.'%' ); 
  111. foreach ( $multi_keywords as $single_keyword ) { 
  112. $sql['orderby'][] = $wpdb->prepare( " pp.post_title NOT LIKE '%s' ASC, pp.post_title ASC ", $single_keyword.'%' ); 
  113. } else { 
  114. $sql['orderby'][] = $wpdb->prepare( " pp.post_title NOT LIKE '%s' ASC, pp.post_title NOT LIKE '%s' ASC, pp.post_title ASC ", $search_keyword.'%', '% '.$search_keyword.'%' ); 
  115. foreach ( $multi_keywords as $single_keyword ) { 
  116. $sql['orderby'][] = $wpdb->prepare( " pp.post_title NOT LIKE '%s' ASC, pp.post_title NOT LIKE '%s' ASC, pp.post_title ASC ", $single_keyword.'%', '% '.$single_keyword.'%' ); 
  117.  
  118. $sql['limit'] = " {$start} , {$number_row} "; 
  119.  
  120. return $sql; 
  121.  
  122. /** 
  123. * Insert Predictive Search Post 
  124. */ 
  125. public function insert_item( $post_id, $post_title = '', $post_type = 'post' ) { 
  126. global $wpdb; 
  127. return $wpdb->query( $wpdb->prepare( "INSERT INTO {$wpdb->ps_posts} VALUES(%d, %s, %s)", $post_id, stripslashes( $post_title ), stripslashes( $post_type ) ) ); 
  128.  
  129. /** 
  130. * Update Predictive Search Post 
  131. */ 
  132. public function update_item( $post_id, $post_title = '', $post_type = 'post' ) { 
  133. global $wpdb; 
  134.  
  135. $value = $this->is_item_existed( $post_id ); 
  136. if ( '0' == $value ) { 
  137. return $this->insert_item( $post_id, $post_title, $post_type ); 
  138. } else { 
  139. return $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->ps_posts} SET post_title = %s, post_type = %s WHERE post_id = %d ", stripslashes( $post_title ), stripslashes( $post_type ), $post_id ) ); 
  140.  
  141. /** 
  142. * Get Predictive Search Post 
  143. */ 
  144. public function get_item( $post_id ) { 
  145. global $wpdb; 
  146. return $wpdb->get_var( $wpdb->prepare( "SELECT post_title FROM {$wpdb->ps_posts} WHERE post_id = %d LIMIT 0, 1", $post_id ) ); 
  147.  
  148. /** 
  149. * Check Predictive Search Post Existed 
  150. */ 
  151. public function is_item_existed( $post_id ) { 
  152. global $wpdb; 
  153. return $wpdb->get_var( $wpdb->prepare( "SELECT EXISTS( SELECT 1 FROM {$wpdb->ps_posts} WHERE post_id = %d LIMIT 0, 1 )", $post_id ) ); 
  154.  
  155. /** 
  156. * Get Predictive Search Latest Post ID 
  157. */ 
  158. public function get_latest_post_id( $post_types = array() ) { 
  159. global $wpdb; 
  160.  
  161. if ( empty( $post_types ) ) { 
  162. return $wpdb->get_var( "SELECT post_id FROM {$wpdb->ps_posts} ORDER BY post_id DESC LIMIT 0, 1" ); 
  163. } else { 
  164. return $wpdb->get_var( "SELECT post_id FROM {$wpdb->ps_posts} WHERE post_type IN ('". implode("', '", $post_types ) ."') ORDER BY post_id DESC LIMIT 0, 1" ); 
  165.  
  166. /** 
  167. * Check Latest Post ID is newest from WP database 
  168. */ 
  169. public function is_newest_id( $post_types = array() ) { 
  170. global $wpdb; 
  171.  
  172. if ( ! is_array( $post_types ) ) { 
  173. $post_types = array( $post_types ); 
  174.  
  175. if ( empty( $post_types ) ) { 
  176. $post_types = array( 'post', 'page', 'product', 'product_variation' ); 
  177.  
  178. $latest_id = $this->get_latest_post_id( $post_types ); 
  179. if ( empty( $latest_id ) || is_null( $latest_id ) ) { 
  180. $latest_id = 0; 
  181.  
  182. $is_not_newest = $wpdb->get_var( 
  183. $wpdb->prepare( 
  184. "SELECT EXISTS( SELECT 1 FROM {$wpdb->posts} WHERE ID > %d AND post_type IN ('". implode("', '", $post_types ) ."') AND post_status = %s LIMIT 0, 1 )",  
  185. $latest_id,  
  186. 'publish' 
  187. ); 
  188.  
  189. if ( '1' != $is_not_newest ) { 
  190. return true; 
  191.  
  192. return false; 
  193.  
  194. /** 
  195. * Get Total Items Synched 
  196. */ 
  197. public function get_total_items_synched( $post_type = '' ) { 
  198. global $wpdb; 
  199. if ( '' == trim( $post_type ) ) { 
  200. return $wpdb->get_var( "SELECT COUNT(post_id) FROM {$wpdb->ps_posts} " ); 
  201. } else { 
  202. return $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(post_id) FROM {$wpdb->ps_posts} WHERE post_type = %s ", $post_type ) ); 
  203.  
  204. /** 
  205. * Delete Predictive Search Post 
  206. */ 
  207. public function delete_item( $post_id ) { 
  208. global $wpdb; 
  209. return $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->ps_posts} WHERE post_id = %d ", $post_id ) ); 
  210.  
  211. /** 
  212. * Empty Predictive Search Posts 
  213. */ 
  214. public function empty_table() { 
  215. global $wpdb; 
  216. return $wpdb->query( "TRUNCATE {$wpdb->ps_posts}" );