PluginProbe
FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment / 3.1.12
FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment v3.1.12
3.1.13 3.1.12 3.1.11 3.1.10 3.1.9 3.1.8 3.1.7 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 All 122 releases
firebox / Inc / Core / Analytics / QueryBuilders / BaseQueryStrategy.php

BaseQueryStrategy.php in FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment 3.1.12, at Inc/Core/Analytics/QueryBuilders/BaseQueryStrategy.php

214 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package FireBox
4 * @version 3.1.12 Free
5 *
6 * @author FirePlugins <info@fireplugins.com>
7 * @link https://www.fireplugins.com
8 * @copyright Copyright © 2026 FirePlugins All Rights Reserved
9 * @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later
10 */
11
12 namespace FireBox\Core\Analytics\QueryBuilders;
13
14 use FireBox\Core\Analytics\QueryBuilders\Interfaces\QueryStrategyInterface;
15
16 if (!defined('ABSPATH'))
17 {
18 exit; // Exit if accessed directly.
19 }
20
21 abstract class BaseQueryStrategy implements QueryStrategyInterface
22 {
23 protected $metric;
24
25 public function __construct($metric)
26 {
27 $this->metric = $metric;
28 }
29
30 /**
31 * Get the FROM/JOIN clauses for the query
32 * Default implementation - should be overridden by subclasses for specific table structures
33 */
34 public function getFromAndJoins(): string
35 {
36 return $this->getDefaultFromAndJoins();
37 }
38
39 /**
40 * Get the WHERE period clause for the query
41 * Uses the configured date column and actual date values
42 */
43 public function getWherePeriod(): string
44 {
45 $options = $this->metric->getOptions();
46
47 if (empty($options['start_date']) || empty($options['end_date']))
48 {
49 return '';
50 }
51
52 $dateColumn = $this->getDateColumn();
53 $startDate = $options['start_date'];
54 $endDate = $options['end_date'];
55
56 return $this->metric->getWpdb()->prepare(" AND {$dateColumn} BETWEEN %s AND %s", $startDate, $endDate);
57 }
58
59 /**
60 * Get the filters clause for the query
61 * Uses the configured table alias for filtering
62 */
63 public function getFilters(): string
64 {
65 $filters = $this->metric->getFilters();
66
67 if (empty($filters))
68 {
69 return '';
70 }
71
72 // Build filters using the FilterBuilder with correct table aliases
73 $table_aliases = $this->getTableAliases();
74 $filter_builder = new \FireBox\Core\Analytics\Filters\FilterBuilder([], $table_aliases);
75 $result = $filter_builder->build($filters);
76
77 if (empty($result['sql']) || empty($result['placeholders']))
78 {
79 return $result['sql'];
80 }
81
82 return $this->metric->getWpdb()->prepare($result['sql'], $result['placeholders']); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber
83 }
84
85 /**
86 * Get table aliases for FilterBuilder
87 * Can be overridden by subclasses to provide metric-specific aliases
88 */
89 protected function getTableAliases(): array
90 {
91 // Default aliases for Views/ConversionRate pattern
92 return [
93 'logs' => 'l',
94 'details' => 'bld'
95 ];
96 }
97
98 /**
99 * Get the LIMIT/OFFSET clause for the query
100 */
101 public function getLimitOffset(): string
102 {
103 $limit = $this->getLimit();
104 $offset = $this->getOffset();
105
106 $result = '';
107 if ($limit) {
108 $result .= $limit;
109 }
110 if ($offset) {
111 $result .= ' ' . $offset;
112 }
113
114 return $result;
115 }
116
117 /**
118 * Get the LIMIT clause
119 */
120 protected function getLimit(): string
121 {
122 $limit = $this->metric->getOptions()['limit'] ?? null;
123
124 if (!is_scalar($limit))
125 {
126 return '';
127 }
128
129 return $this->metric->getWpdb()->prepare("LIMIT %d", $limit);
130 }
131
132 /**
133 * Get the OFFSET clause
134 */
135 protected function getOffset(): string
136 {
137 $offset = $this->metric->getOptions()['offset'] ?? null;
138
139 if (!is_scalar($offset))
140 {
141 return '';
142 }
143
144 return $this->metric->getWpdb()->prepare("OFFSET %d", $offset);
145 }
146
147 /**
148 * Default HAVING implementation - can be overridden
149 */
150 public function getHaving(): string
151 {
152 return '';
153 }
154
155 /**
156 * Default ORDER BY implementation - can be overridden
157 */
158 public function getOrderBy(): string
159 {
160 $dateColumn = $this->getDateColumn();
161 return "ORDER BY {$dateColumn} DESC";
162 }
163
164 /**
165 * Default WHERE implementation - can be overridden
166 */
167 public function getWhere(): string
168 {
169 return '';
170 }
171
172 /**
173 * Default GROUP BY implementation - can be overridden
174 */
175 public function getGroupBy(): string
176 {
177 return '';
178 }
179
180 // Abstract methods that subclasses must implement to define their specific behavior
181
182 /**
183 * Get the default FROM/JOIN structure for this metric type
184 * Should be implemented by metric-specific base classes
185 */
186 protected abstract function getDefaultFromAndJoins(): string;
187
188 /**
189 * Get the date column used for period filtering and ordering
190 * Should be implemented by metric-specific base classes (e.g., 'l.date', 'bld.date')
191 */
192 protected abstract function getDateColumn(): string;
193
194 /**
195 * Must be implemented by concrete strategy classes
196 */
197 public abstract function getSelect(): string;
198
199 /**
200 * Template for standard ORDER BY with total DESC fallback and options support
201 */
202 protected function getTotalOrderBy(): string
203 {
204 $options = $this->metric->getOptions();
205
206 if (isset($options['orderby']))
207 {
208 return 'ORDER BY ' . $options['orderby'];
209 }
210
211 return 'ORDER BY total DESC';
212 }
213 }
214