PluginProbe
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO / 1.3.54
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO v1.3.54
1.4.13 1.4.12 1.4.11 1.4.10 1.4.9 1.4.8 1.4.7 1.4.6 1.4.5 1.4.4 1.4.3 1.4.2 1.4.1 1.4.0 1.3.54 1.3.53 1.3.52 1.3.51 1.3.50 1.3.49 1.3.48 1.3.47 1.3.46 1.3.45 1.3.44 All 176 releases
disco / app / Campaign.php

Campaign.php in Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO 1.3.54, at app/Campaign.php

365 lines 9.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * This class is responsible for all campaign related database operation.
5 *
6 * @package Disco
7 */
8
9 namespace Disco\App;
10
11 use Disco\App\Utility\Config;
12
13 /**
14 * Class Campaign
15 *
16 * @package Disco
17 * @subpackage App
18 * @author Ohidul Islam <wahid0003@gmail.com>
19 * @link https://webappick.com
20 * @license https://opensource.org/licenses/gpl-license.php GNU Public License
21 * @category Campaign
22 */
23 class Campaign {
24
25 /**
26 * Get campaigns from database.
27 *
28 * @param string $status Campaign status.
29 * @param string $intent Campaign intent.
30 * @return array
31 * @since 1.0.0
32 */
33 public function get_campaigns( $status = null, $intent = null ) {//phpcs:ignore
34 $campaigns = $this->get_rows();
35
36 if ( ! is_array( $campaigns ) ) {
37 return array();
38 }
39
40 $filtered_campaigns = array_filter(
41 $campaigns,
42 static function ( $campaign ) use ( $status, $intent ) {
43 if ( ! isset( $campaign->status ) ) {
44 return false;
45 }
46
47 if ( ! isset( $campaign->intent ) ) {
48 return false;
49 }
50
51 if ( ! is_object( $campaign ) ) {
52 return false;
53 }
54
55 if ( ! is_null( $status ) && $status !== $campaign->status ) {
56 return false;
57 }
58
59 if ( is_array( $intent ) ) {
60 return in_array( $campaign->intent, $intent, true );
61 }
62
63 return is_null( $intent ) || $intent === $campaign->intent;
64 }
65 );
66
67 $results = array_map(
68 static function ( $campaign ) {
69 if ( isset( $campaign->data, $campaign->id ) ) {
70 $data = json_decode( $campaign->data, true );
71 $data = (array) $data;
72 $data['id'] = $campaign->id;
73
74 return new Config( $data );
75 }
76
77 return false;
78 },
79 $filtered_campaigns
80 );
81
82 $results = array_combine( array_column( $results, 'id' ), $results );
83
84 if ( is_array( $results ) ) {
85 return $results;
86 }
87
88 return array();
89 }
90
91 /**
92 * Get a campaign by id.
93 *
94 * @param int|mixed $id Campaign id.
95 * @return \Disco\App\Utility\Config|\WP_Error
96 * @since 1.0.0
97 */
98 public function get_campaign( $id ) {
99 $campaigns = $this->get_rows();
100 $campaigns = (array) $campaigns;
101 $id = absint( $id );
102
103 if ( isset( $campaigns[ $id ] ) ) {
104 $campaign = $campaigns[ $id ];
105 } else {
106 // Get Campaign from cache or fetch from DB.
107 $campaign = $this->get_row( $id );
108 }
109
110 // Return the campaign from cache.
111 if ( $campaign instanceof Config ) {
112 return $campaign;
113 }
114
115 if ( ! $campaign ) {
116 return new \WP_Error(
117 'rest_not_found',
118 __( 'Sorry, Invalid campaign id.', 'disco' ),
119 array( 'status' => 400 )
120 );
121 }
122
123 if ( isset( $campaign->data ) ) {
124 $campaign = json_decode( $campaign->data, true );
125 $campaign = (array) $campaign;
126 $campaign['id'] = $id;
127
128 return new Config( $campaign );
129 }
130
131 return new \WP_Error(
132 'rest_not_found',
133 __( 'Sorry, Invalid campaign data.', 'disco' ),
134 array( 'status' => 400 )
135 );
136 }
137
138 /**
139 * Save campaign into database.
140 *
141 * @param array $config Campaign config.
142 * @return \WP_Error|\Disco\App\Utility\Config
143 * @since 1.0.0
144 */
145 public function save_campaign( $config ) {//phpcs:ignore
146 if ( is_user_logged_in() ) {
147 $config['created_by'] = get_current_user_id();
148 $config['modified_by'] = get_current_user_id();
149 }
150
151 if ( empty( $config['status'] ) ) {
152 $config['status'] = '0';
153 }
154
155 $config['created_date'] = gmdate( 'Y-m-d H:i:s' );
156 $config['modified_date'] = gmdate( 'Y-m-d H:i:s' );
157 // ==================================.
158 $campaign_id = $this->insert( $config );
159 // ==================================.
160 // Get Campaign from cache or fetch from DB.
161 $campaign = $this->get_row( $campaign_id );
162
163 if ( ! $campaign ) {
164 return new \WP_Error(
165 'rest_not_added',
166 __( 'Sorry, Failed to Save Campaign.', 'disco' ),
167 array( 'status' => 400 )
168 );
169 }
170
171 $data = array();
172
173 if ( isset( $campaign->data, $campaign->id ) ) {
174 $data = (array) json_decode( $campaign->data, true );
175 $data['id'] = absint( $campaign->id );
176 }
177
178 return new Config( $data );
179 }
180
181 /**
182 * Update campaign into database.
183 *
184 * @param int $id Campaign id.
185 * @param array $config Campaign config.
186 * @return \Disco\App\Utility\Config|\WP_Error
187 * @throws \Exception If the campaign is not found.
188 * @since 1.0.0
189 */
190 public function update_campaign( $id, $config ) {
191 $id = absint( $id );
192
193 if ( is_user_logged_in() ) {
194 $config['modified_by'] = get_current_user_id();
195 }
196
197 $config['modified_date'] = gmdate( 'Y-m-d H:i:s' );
198 // Get Campaign from cache or fetch from DB.
199 $campaign = $this->get_row( $id );
200
201 if ( ! $campaign ) {
202 return new \WP_Error(
203 'rest_not_found',
204 __( 'Sorry, Invalid campaign id.', 'disco' ),
205 array( 'status' => 400 )
206 );
207 }
208
209 if ( ! isset( $config['status'] ) && ! empty( $campaign->status ) ) {
210 $config['status'] = $campaign->status;
211 }
212
213 if ( ! isset( $config['priority'] ) && ! empty( $campaign->priority ) ) {
214 $config['priority'] = $campaign->priority;
215 }
216
217 // Update campaign into database.
218 $update = $this->update( $config, $id );
219
220 if ( ! $update ) {
221 return new \WP_Error(
222 'rest_not_added',
223 __( 'Sorry, the campaign could not be updated.', 'disco' ),
224 array( 'status' => 400 )
225 );
226 }
227
228 $data = array();
229
230 if ( isset( $campaign->data ) && isset( $campaign->id ) ) {
231 $data = (array) json_decode( $campaign->data, true );
232 $data['id'] = absint( $campaign->id );
233 }
234
235 return new Config( $data );
236 }
237
238 /**
239 * Delete campaign from database.
240 *
241 * @param int $id Campaign id.
242 * @return int|bool
243 * @since 1.0.0
244 */
245 public function delete_campaign( $id ) {
246 return $this->delete( $id );
247 }
248
249 /**
250 * Insert campaign into database.
251 *
252 * @param array $config Campaign config.
253 * @return int
254 * @since 1.0.0
255 */
256 public function insert( $config ) {
257 global $wpdb;
258
259 $insert = $wpdb->insert( //phpcs:ignore
260 "{$wpdb->prefix}disco_campaigns",
261 array(
262 'intent' => $config['discount_intent'],
263 'status' => $config['status'],
264 'data' => wp_json_encode( $config ),
265 ),
266 array(
267 '%s',
268 '%s',
269 '%s',
270 )
271 );
272
273 if ( $insert ) {
274 return $wpdb->insert_id;
275 }
276
277 return 0;
278 }
279
280 /**
281 * Update campaign into database.
282 *
283 * @param array $config Campaign config.
284 * @param int $id Campaign id.
285 * @return bool|int
286 * @since 1.0.0
287 */
288 public function update( $config, $id ) {
289 global $wpdb;
290
291 $table_name = $wpdb->prefix . 'disco_campaigns';
292
293 return $wpdb->update( //phpcs:ignore
294 $table_name,
295 array(
296 'intent' => $config['discount_intent'],
297 'status' => $config['status'],
298 'priority' => $config['priority'],
299 'data' => wp_json_encode( $config ),
300 ),
301 array( 'id' => $id ),
302 array(
303 '%s',
304 '%s',
305 '%s',
306 '%s',
307 ),
308 array( '%d' )
309 );
310 }
311
312 /**
313 * Delete campaign from database.
314 *
315 * @param int $id Campaign id.
316 * @return bool|int
317 * @since 1.0.0
318 */
319 public function delete( $id ) {
320 global $wpdb;
321
322 $table_name = $wpdb->prefix . 'disco_campaigns';
323
324 return $wpdb->delete( $table_name, array( 'id' => $id ), array( '%d' ) );//phpcs:ignore
325 }
326
327 /**
328 * Get all rows from database.
329 *
330 * @noinspection SqlResolve
331 * @return array|null
332 * @since 1.0.0
333 */
334 public function get_rows() {
335 global $wpdb;
336 // Get all rows
337 $get_campaigns = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}disco_campaigns", OBJECT ); //phpcs:ignore
338 $campaigns = array();
339
340 foreach ( $get_campaigns as $result ) {
341 $campaigns[ $result->id ] = $result;
342 }
343
344 return $campaigns;
345 }
346
347 /**
348 * Single Row Query by id.
349 * First check if the row is available in cache.
350 * If not, then fetch from the database.
351 *
352 * @noinspection SqlResolve
353 * @param int $id Campaign id.
354 * @return object|null
355 * @since 1.0.0
356 */
357 public function get_row( $id ) {
358 global $wpdb;
359 $query = $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}disco_campaigns WHERE id = %d", $id ); //phpcs:ignore
360
361 return $wpdb->get_row( $query, OBJECT );//phpcs:ignore
362 }
363
364 }
365