PluginProbe
Year Make Model Search for WooCommerce / trunk
Year Make Model Search for WooCommerce vtrunk
ymm-search / Controller / Product.php

Product.php in Year Make Model Search for WooCommerce trunk, at Controller/Product.php

178 lines 5.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit; // Exit if accessed directly
5 }
6
7 class Pektsekye_Ymm_Controller_Product {
8
9
10 protected $_db;
11 protected $_config;
12
13 protected $_selectedValues;
14 protected $_foundProductIds;
15
16
17 public function __construct() {
18 include_once( Pektsekye_YMM()->getPluginPath() . 'Model/Db.php');
19 $this->_db = new Pektsekye_Ymm_Model_Db();
20
21 include_once( Pektsekye_YMM()->getPluginPath() . 'etc/config.php');
22 $this->_config = new Pektsekye_Ymm_Config();
23
24 add_filter('single_term_title', array($this, 'add_selected_vehicle_to_category_title'));
25 add_filter('woocommerce_layered_nav_link', array($this, 'add_selected_params_to_layered_nav_link'));
26 add_filter('woocommerce_get_filtered_term_product_counts_query', array($this, 'add_found_product_ids_to_product_counts_query'));
27 add_filter('get_search_query', array($this, 'shop_order_search_label' ));
28 }
29
30
31 public function applyFilter(){
32 global $wp_query;
33
34 if (is_product_category() || isset($_GET['ymm_search'])){
35
36 $values = $this->getSelectedValues();
37 $pIds = $this->getFoundProductIds();
38
39 if (!is_product_category() || count($values) > 0){//filter category page only when values selected
40 $wp_query->query_vars['post__in'] = count($pIds) > 0 ? $pIds : array(-1); // -1 to display a "no products found" message
41 }
42
43 if (count($values) > 0){
44 Pektsekye_YMM()->register('selected_values', $values);
45 }
46 }
47 }
48
49
50 public function getSelectedValues(){
51 if (!isset($this->_selectedValues)){
52 $values = array();
53 foreach ((array) $this->_config->getLevels() as $level){
54 $parameter = $level['url_parameter'];
55 if (isset($_GET[$parameter])){
56 $values[] = sanitize_text_field(stripslashes($_GET[$parameter]));
57 }
58 }
59 $this->_selectedValues = $values;
60 }
61 return $this->_selectedValues;
62 }
63
64
65 public function getFoundProductIds(){
66 if (!isset($this->_foundProductIds)){
67 $pIds = array();
68 $values = $this->getSelectedValues();
69 if (count($values) > 0){
70 $pIds = $this->_db->getProductIds($values);
71 }
72 $this->_foundProductIds = $pIds;
73 }
74 return $this->_foundProductIds;
75 }
76
77
78 public function product_query($query) {
79
80 if ($query->is_main_query() && function_exists('is_product_category') && (is_product_category() || isset($_GET['ymm_search']))) {
81 //call wc product_query before PartFinder filter
82 WC()->query->product_query($query);
83
84 remove_action( 'pre_get_posts', array( WC()->query, 'pre_get_posts' ) );
85
86 if (function_exists('tr_sku_search_helper')){// WooCommerce Search by Product SKU
87 remove_filter('pre_get_posts', 'tr_sku_search_helper', 15);
88 }
89
90 $this->applyFilter();
91 }
92 }
93
94
95 public function shop_order_search_label($label) {
96 global $pagenow;
97
98 if ('index.php' == $pagenow && isset($_GET['s']) && isset($_GET['ymm_search'])) {
99 $trace = debug_backtrace(defined('DEBUG_BACKTRACE_IGNORE_ARGS') ? DEBUG_BACKTRACE_IGNORE_ARGS : false, 6);
100 if (
101 (isset($trace[3]) && in_array(basename($trace[3]['file']), array('class-wc-breadcrumb.php','wc-template-functions.php','general-template.php')) && $trace[3]['function'] == 'get_search_query' && (!isset($trace[4]['function']) || $trace[4]['function'] != 'get_search_form')) //WP 4.3
102 || (isset($trace[4]) && in_array(basename($trace[4]['file']), array('class-wc-breadcrumb.php','wc-template-functions.php','general-template.php')) && $trace[4]['function'] == 'get_search_query' && (!isset($trace[5]['function']) || $trace[5]['function'] != 'get_search_form')) //WP 4.9
103 ){
104 $label = implode(' ', $this->getSelectedValues());
105
106 if (!empty($_GET['s'])){
107 $label .= ' ' . sanitize_text_field(stripslashes($_GET['s']));
108 }
109 }
110
111 }
112
113 return $label;
114 }
115
116
117 public function add_selected_vehicle_to_category_title($title) {
118 if (!is_product_category()){
119 return $title;
120 }
121
122 $values = $this->getSelectedValues();
123 if (count($values) > 0){
124 $title .= ' '. __('for', 'ymm-search') .' '. implode(' ', $values);
125 }
126
127 return $title;
128 }
129
130
131 public function add_selected_params_to_layered_nav_link($link) {
132 if (!is_product_category() && !is_search()){
133 return $link;
134 }
135
136 $params = array();
137 foreach ((array) $this->_config->getLevels() as $level){
138 $pr = $level['url_parameter'];
139 if (isset($_GET[$pr])){
140 $params[$pr] = sanitize_text_field(stripslashes($_GET[$pr]));
141 }
142 }
143
144 if (isset($_GET['s']) && $_GET['s'] == ''){//we need the empty "s" GET parameter to display the search results page
145 $params['s'] = '';
146 }
147
148 if (count($params) > 0){
149 $link .= '&' . http_build_query($params);
150 }
151
152 if (isset($_GET['ymm_search'])){
153 $urlParts = explode('?', $link);
154 $link = home_url( '/' ) . '?' . $urlParts[1] . '&ymm_search=1'; //return back to the main search results page
155 }
156
157 return $link;
158 }
159
160
161 public function add_found_product_ids_to_product_counts_query($query) {
162 if (!is_product_category() && !is_search()){
163 return $query;
164 }
165
166 global $wpdb;
167
168 $pIds = $this->getFoundProductIds();
169 if (count($pIds) > 0){
170 $query['where'] .= " AND {$wpdb->posts}.ID IN (". implode(',', $pIds) .")";
171 }
172
173 return $query;
174 }
175
176
177 }
178