PageRenderTime 57ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/shop quần áo starloveshop.com/wp-content/plugins/woocommerce-predictive-search/classes/class-wc-predictive-search-filter.php

https://gitlab.com/phamngsinh/baitaplon_sinhvien
PHP | 285 lines | 224 code | 33 blank | 28 comment | 61 complexity | b057693ed94a42a2e142721a4513fb85 MD5 | raw file
  1. <?php
  2. /**
  3. * WooCommerce Predictive Search Hook Filter
  4. *
  5. * Hook anf Filter into woocommerce plugin
  6. *
  7. * Table Of Contents
  8. *
  9. * plugins_loaded()
  10. * get_result_popup()
  11. * add_frontend_script()
  12. * add_frontend_style()
  13. * add_query_vars()
  14. * add_rewrite_rules()
  15. * custom_rewrite_rule()
  16. * search_by_title_only()
  17. * posts_request_unconflict_role_scoper_plugin()
  18. * a3_wp_admin()
  19. * plugin_extra_links()
  20. */
  21. class WC_Predictive_Search_Hook_Filter
  22. {
  23. public static function plugins_loaded() {
  24. global $wc_predictive_id_excludes;
  25. WC_Predictive_Search::get_id_excludes();
  26. }
  27. public static function get_result_popup() {
  28. check_ajax_referer( 'woops-get-result-popup', 'security' );
  29. add_filter( 'posts_search', array('WC_Predictive_Search_Hook_Filter', 'search_by_title_only'), 500, 2 );
  30. add_filter( 'posts_orderby', array('WC_Predictive_Search_Hook_Filter', 'predictive_posts_orderby'), 500, 2 );
  31. add_filter( 'posts_request', array('WC_Predictive_Search_Hook_Filter', 'posts_request_unconflict_role_scoper_plugin'), 500, 2);
  32. global $wc_predictive_id_excludes;
  33. $row = 6;
  34. $text_lenght = 100;
  35. $show_price = 1;
  36. $search_keyword = '';
  37. $cat_slug = '';
  38. $tag_slug = '';
  39. $extra_parameter = '';
  40. if (isset($_REQUEST['row']) && $_REQUEST['row'] > 0) $row = stripslashes( strip_tags( $_REQUEST['row'] ) );
  41. if (isset($_REQUEST['text_lenght']) && $_REQUEST['text_lenght'] >= 0) stripslashes( strip_tags( $text_lenght = $_REQUEST['text_lenght'] ) );
  42. if (isset($_REQUEST['show_price']) && trim($_REQUEST['show_price']) != '') $show_price = stripslashes( strip_tags( $_REQUEST['show_price'] ) );
  43. if (isset($_REQUEST['q']) && trim($_REQUEST['q']) != '') $search_keyword = stripslashes( strip_tags( $_REQUEST['q'] ) );
  44. $end_row = $row;
  45. if ($search_keyword != '') {
  46. $args = array( 's' => $search_keyword, 'numberposts' => $row+1, 'offset'=> 0, 'orderby' => 'predictive', 'order' => 'ASC', 'post_type' => 'product', 'post_status' => 'publish', 'exclude' => $wc_predictive_id_excludes['exclude_products'], 'suppress_filters' => FALSE);
  47. if ($cat_slug != '') {
  48. $args['tax_query'] = array( array('taxonomy' => 'product_cat', 'field' => 'slug', 'terms' => $cat_slug) );
  49. if (get_option('permalink_structure') == '')
  50. $extra_parameter .= '&scat='.$cat_slug;
  51. else
  52. $extra_parameter .= '/scat/'.$cat_slug;
  53. } elseif($tag_slug != '') {
  54. $args['tax_query'] = array( array('taxonomy' => 'product_tag', 'field' => 'slug', 'terms' => $tag_slug) );
  55. if (get_option('permalink_structure') == '')
  56. $extra_parameter .= '&stag='.$tag_slug;
  57. else
  58. $extra_parameter .= '/stag/'.$tag_slug;
  59. }
  60. $total_args = $args;
  61. $total_args['numberposts'] = -1;
  62. //$search_all_products = get_posts($total_args);
  63. $search_products = get_posts($args);
  64. if ( $search_products && count($search_products) > 0 ) {
  65. echo "<div class='ajax_search_content_title'>".__('Products', 'woops')."</div>[|]#[|]$search_keyword\n";
  66. foreach ( $search_products as $product ) {
  67. $link_detail = get_permalink($product->ID);
  68. $avatar = WC_Predictive_Search::woops_get_product_thumbnail($product->ID,'shop_catalog',64,64);
  69. $product_description = WC_Predictive_Search::woops_limit_words(strip_tags( WC_Predictive_Search::strip_shortcodes( strip_shortcodes( str_replace("\n", "", $product->post_content) ) ) ),$text_lenght,'...');
  70. if (trim($product_description) == '') $product_description = WC_Predictive_Search::woops_limit_words(strip_tags( WC_Predictive_Search::strip_shortcodes( strip_shortcodes( str_replace("\n", "", $product->post_excerpt) ) ) ),$text_lenght,'...');
  71. $price_html = '';
  72. if ( $show_price == 1)
  73. $price_html = WC_Predictive_Search_Shortcodes::get_product_price_dropdown($product->ID);
  74. $item = '<div class="ajax_search_content"><div class="result_row"><a href="'.$link_detail.'"><span class="rs_avatar">'.$avatar.'</span><div class="rs_content_popup"><span class="rs_name">'.stripslashes( $product->post_title).'</span>'.$price_html.'<span class="rs_description">'.$product_description.'</span></div></a></div></div>';
  75. echo $item.'[|]'.$link_detail.'[|]'.stripslashes( $product->post_title)."\n";
  76. $end_row--;
  77. if ($end_row < 1) break;
  78. }
  79. $rs_item = '';
  80. if ( count($search_products) > $row ) {
  81. if (get_option('permalink_structure') == '')
  82. $link_search = get_permalink(get_option('woocommerce_search_page_id')).'&rs='. urlencode($search_keyword) .$extra_parameter;
  83. else
  84. $link_search = rtrim( get_permalink(get_option('woocommerce_search_page_id')), '/' ).'/keyword/'. urlencode($search_keyword) .$extra_parameter;
  85. $rs_item .= '<div class="more_result" rel="more_result"><a href="'.$link_search.'">'.__('See more results for', 'woops').' '.$search_keyword.' <span class="see_more_arrow"></span></a><span>'.__('Displaying top', 'woops').' '.$row.' '.__('results', 'woops').'</span></div>';
  86. echo $rs_item.'[|]'.$link_search.'[|]'.$search_keyword."\n";
  87. }
  88. } else {
  89. echo '<div class="ajax_no_result">'.__('Nothing found for that name. Try a different spelling or name.', 'woops').'</div>';
  90. }
  91. }
  92. die();
  93. }
  94. /*
  95. * Include the script for widget search and Search page
  96. */
  97. public static function add_frontend_script() {
  98. wp_enqueue_script('jquery');
  99. wp_enqueue_script( 'ajax-woo-autocomplete-script', WOOPS_JS_URL . '/ajax-autocomplete/jquery.autocomplete.js', array(), false, true );
  100. }
  101. public static function add_frontend_style() {
  102. wp_enqueue_style( 'ajax-woo-autocomplete-style', WOOPS_JS_URL . '/ajax-autocomplete/jquery.autocomplete.css' );
  103. }
  104. public static function add_query_vars($aVars) {
  105. $aVars[] = "keyword"; // represents the name of the product category as shown in the URL
  106. $aVars[] = "scat";
  107. $aVars[] = "stag";
  108. return $aVars;
  109. }
  110. public static function add_rewrite_rules($aRules) {
  111. //var_dump($_SERVER);
  112. $woocommerce_search_page_id = get_option('woocommerce_search_page_id');
  113. $search_page = get_page($woocommerce_search_page_id);
  114. if (!empty($search_page)) {
  115. $search_page_slug = $search_page->post_name;
  116. if (stristr($_SERVER['REQUEST_URI'], $search_page_slug) !== FALSE) {
  117. //$url_text = stristr($_SERVER['REQUEST_URI'], $search_page_slug);
  118. $position = strpos($_SERVER['REQUEST_URI'], $search_page_slug);
  119. $new_url = substr($_SERVER['REQUEST_URI'], ($position + strlen($search_page_slug.'/') ) );
  120. $parameters_array = explode("/", $new_url);
  121. if (is_array($parameters_array) && count($parameters_array) > 1) {
  122. $array_key = array();
  123. $array_value = array();
  124. $number = 0;
  125. foreach ($parameters_array as $parameter) {
  126. $number++;
  127. if (trim($parameter) == '') continue;
  128. if ($number%2 == 0) $array_value[] = $parameter;
  129. else $array_key[] = $parameter;
  130. }
  131. if (count($array_key) > 0 && count($array_value) > 0 ) {
  132. $rewrite_rule = '';
  133. $original_url = '';
  134. $number_matches = 0;
  135. foreach ($array_key as $key) {
  136. $number_matches++;
  137. $rewrite_rule .= $key.'/([^/]*)/';
  138. $original_url .= '&'.$key.'=$matches['.$number_matches.']';
  139. }
  140. $aNewRules = array($search_page_slug.'/'.$rewrite_rule.'?$' => 'index.php?pagename='.$search_page_slug.$original_url);
  141. $aRules = $aNewRules + $aRules;
  142. }
  143. }
  144. }
  145. }
  146. return $aRules;
  147. }
  148. public static function custom_rewrite_rule() {
  149. // BEGIN rewrite
  150. // hook add_query_vars function into query_vars
  151. add_filter('query_vars', array('WC_Predictive_Search_Hook_Filter', 'add_query_vars') );
  152. add_filter('rewrite_rules_array', array('WC_Predictive_Search_Hook_Filter', 'add_rewrite_rules') );
  153. $woocommerce_search_page_id = get_option('woocommerce_search_page_id');
  154. $search_page = get_page($woocommerce_search_page_id);
  155. if (!empty($search_page)) {
  156. $search_page_slug = $search_page->post_name;
  157. if (stristr($_SERVER['REQUEST_URI'], $search_page_slug) !== FALSE) {
  158. global $wp_rewrite;
  159. $wp_rewrite->flush_rules();
  160. }
  161. }
  162. // END rewrite
  163. }
  164. public static function remove_special_characters_in_mysql( $field_name ) {
  165. if ( trim( $field_name ) == '' ) return '';
  166. $field_name = 'REPLACE( '.$field_name.', "(", "")';
  167. $field_name = 'REPLACE( '.$field_name.', ")", "")';
  168. $field_name = 'REPLACE( '.$field_name.', "{", "")';
  169. $field_name = 'REPLACE( '.$field_name.', "}", "")';
  170. $field_name = 'REPLACE( '.$field_name.', "<", "")';
  171. $field_name = 'REPLACE( '.$field_name.', ">", "")';
  172. $field_name = 'REPLACE( '.$field_name.', "©", "")'; // copyright
  173. $field_name = 'REPLACE( '.$field_name.', "®", "")'; // registered
  174. $field_name = 'REPLACE( '.$field_name.', "™", "")'; // trademark
  175. $field_name = 'REPLACE( '.$field_name.', "£", "")';
  176. $field_name = 'REPLACE( '.$field_name.', "¥", "")';
  177. $field_name = 'REPLACE( '.$field_name.', "§", "")';
  178. $field_name = 'REPLACE( '.$field_name.', "¢", "")';
  179. $field_name = 'REPLACE( '.$field_name.', "µ", "")';
  180. $field_name = 'REPLACE( '.$field_name.', "¶", "")';
  181. $field_name = 'REPLACE( '.$field_name.', "–", "")';
  182. $field_name = 'REPLACE( '.$field_name.', "¿", "")';
  183. $field_name = 'REPLACE( '.$field_name.', "«", "")';
  184. $field_name = 'REPLACE( '.$field_name.', "»", "")';
  185. $field_name = 'REPLACE( '.$field_name.', "&lsquo;", "")'; // left single curly quote
  186. $field_name = 'REPLACE( '.$field_name.', "&rsquo;", "")'; // right single curly quote
  187. $field_name = 'REPLACE( '.$field_name.', "&ldquo;", "")'; // left double curly quote
  188. $field_name = 'REPLACE( '.$field_name.', "&rdquo;", "")'; // right double curly quote
  189. $field_name = 'REPLACE( '.$field_name.', "&quot;", "")'; // quotation mark
  190. $field_name = 'REPLACE( '.$field_name.', "&ndash;", "")'; // en dash
  191. $field_name = 'REPLACE( '.$field_name.', "&mdash;", "")'; // em dash
  192. $field_name = 'REPLACE( '.$field_name.', "&iexcl;", "")'; // inverted exclamation
  193. $field_name = 'REPLACE( '.$field_name.', "&iquest;", "")'; // inverted question mark
  194. $field_name = 'REPLACE( '.$field_name.', "&laquo;", "")'; // guillemets
  195. $field_name = 'REPLACE( '.$field_name.', "&raquo;", "")'; // guillemets
  196. $field_name = 'REPLACE( '.$field_name.', "&gt;", "")'; // greater than
  197. $field_name = 'REPLACE( '.$field_name.', "&lt;", "")'; // less than
  198. return $field_name;
  199. }
  200. public static function search_by_title_only( $search, &$wp_query ) {
  201. global $wpdb;
  202. global $wp_version;
  203. $q = $wp_query->query_vars;
  204. if ( empty( $search) || !isset($q['s']) )
  205. return $search; // skip processing - no search term in query
  206. $search = '';
  207. if ( version_compare( $wp_version, '4.0', '<' ) ) {
  208. $term = esc_sql( like_escape( trim($q['s'] ) ) );
  209. } else {
  210. $term = esc_sql( $wpdb->esc_like( trim($q['s'] ) ) );
  211. }
  212. $term_nospecial = preg_replace( "/[^a-zA-Z0-9_.\s]/", "", $term );
  213. $search_nospecial = false;
  214. if ( $term != $term_nospecial ) $search_nospecial = true;
  215. $search .= "( ".WC_Predictive_Search_Hook_Filter::remove_special_characters_in_mysql( "$wpdb->posts.post_title" )." LIKE '{$term}%' OR ".WC_Predictive_Search_Hook_Filter::remove_special_characters_in_mysql( "$wpdb->posts.post_title" )." LIKE '% {$term}%')";
  216. if ( $search_nospecial ) $search .= " OR ( ".WC_Predictive_Search_Hook_Filter::remove_special_characters_in_mysql( "$wpdb->posts.post_title" )." LIKE '{$term_nospecial}%' OR ".WC_Predictive_Search_Hook_Filter::remove_special_characters_in_mysql( "$wpdb->posts.post_title" )." LIKE '% {$term_nospecial}%')";
  217. if ( ! empty( $search ) ) {
  218. $search = " AND ({$search}) ";
  219. }
  220. return $search;
  221. }
  222. public static function predictive_posts_orderby( $orderby, &$wp_query ) {
  223. global $wpdb;
  224. global $wp_version;
  225. $q = $wp_query->query_vars;
  226. if (isset($q['orderby']) && $q['orderby'] == 'predictive' && isset($q['s']) ) {
  227. if ( version_compare( $wp_version, '4.0', '<' ) ) {
  228. $term = esc_sql( like_escape( trim($q['s'] ) ) );
  229. } else {
  230. $term = esc_sql( $wpdb->esc_like( trim($q['s'] ) ) );
  231. }
  232. $orderby = "$wpdb->posts.post_title NOT LIKE '{$term}%' ASC, $wpdb->posts.post_title ASC";
  233. }
  234. return $orderby;
  235. }
  236. public static function posts_request_unconflict_role_scoper_plugin( $posts_request, &$wp_query ) {
  237. $posts_request = str_replace('1=2', '2=2', $posts_request);
  238. return $posts_request;
  239. }
  240. public static function a3_wp_admin() {
  241. wp_enqueue_style( 'a3rev-wp-admin-style', WOOPS_CSS_URL . '/a3_wp_admin.css' );
  242. }
  243. public static function plugin_extra_links($links, $plugin_name) {
  244. if ( $plugin_name != WOOPS_NAME) {
  245. return $links;
  246. }
  247. $links[] = '<a href="'.WOO_PREDICTIVE_SEARCH_DOCS_URI.'" target="_blank">'.__('Documentation', 'woops').'</a>';
  248. $links[] = '<a href="http://wordpress.org/support/plugin/woocommerce-predictive-search/" target="_blank">'.__('Support', 'woops').'</a>';
  249. return $links;
  250. }
  251. }
  252. ?>