PluginProbe ʕ •ᴥ•ʔ
WooCommerce Square / 2.2.5
WooCommerce Square v2.2.5
5.5.0 5.4.3 5.4.2 5.4.1 5.4.0 trunk 1.0.25 1.0.26 1.0.27 1.0.28 1.0.29 1.0.30 1.0.31 1.0.32 1.0.33 1.0.34 1.0.35 1.0.36 1.0.37 1.0.38 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.4.0 2.4.1 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.7.0 2.8.0 2.9.0 2.9.1 3.0.0 3.0.1 3.0.2 3.0.3 3.1.0 3.2.0 3.3.0 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.6.1 3.7.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.9.0 4.0.0 4.1.0 4.2.0 4.2.1 4.2.2 4.2.3 4.3.0 4.3.1 4.3.2 4.4.0 4.4.1 4.4.2 4.5.0 4.5.1 4.5.2 4.6.0 4.6.1 4.6.2 4.6.3 4.6.4 4.7.0 4.7.1 4.7.2 4.7.3 4.7.4 4.8.0 4.8.1 4.8.2 4.8.3 4.8.4 4.8.5 4.8.6 4.8.7 4.8.8 4.9.0 4.9.1 4.9.2 4.9.3 4.9.4 4.9.5 4.9.6 4.9.7 4.9.8 4.9.9 5.0.0 5.0.1 5.1.0 5.1.1 5.1.2 5.2.0 5.3.0 5.3.1 5.3.2 5.3.3
woocommerce-square / includes / Sync / Records.php
woocommerce-square / includes / Sync Last commit date
Records 6 years ago Catalog_Item.php 6 years ago Interval_Polling.php 5 years ago Job.php 6 years ago Manual_Synchronization.php 5 years ago Product_Import.php 5 years ago Records.php 6 years ago Stepped_Job.php 6 years ago
Records.php
394 lines
1 <?php
2 /**
3 * WooCommerce Square
4 *
5 * This source file is subject to the GNU General Public License v3.0
6 * that is bundled with this package in the file license.txt.
7 * It is also available through the world-wide-web at this URL:
8 * http://www.gnu.org/licenses/gpl-3.0.html
9 * If you did not receive a copy of the license and are unable to
10 * obtain it through the world-wide-web, please send an email
11 * to license@woocommerce.com so we can send you a copy immediately.
12 *
13 * DISCLAIMER
14 *
15 * Do not edit or add to this file if you wish to upgrade WooCommerce Square to newer
16 * versions in the future. If you wish to customize WooCommerce Square for your
17 * needs please refer to https://docs.woocommerce.com/document/woocommerce-square/
18 *
19 * @author WooCommerce
20 * @copyright Copyright: (c) 2019, Automattic, Inc.
21 * @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0
22 */
23
24 namespace WooCommerce\Square\Sync;
25
26 defined( 'ABSPATH' ) || exit;
27
28 use SkyVerge\WooCommerce\PluginFramework\v5_4_0 as Framework;
29 use WooCommerce\Square\Sync\Records\Record;
30
31 /**
32 * The sync records handler.
33 *
34 * @since 2.0.0
35 */
36 class Records {
37
38
39 /** @var string WordPress option key where sync records are stored */
40 private static $records_option_key = 'wc_square_sync_records';
41
42
43 /**
44 * Checks if there are existing records.
45 *
46 * @since 2.0.0
47 *
48 * @return bool
49 */
50 public static function has_records() {
51
52 return ! empty( self::has_records() );
53 }
54
55
56 /**
57 * Gets a specific sync record.
58 *
59 * @since 2.0.0
60 *
61 * @param int $id the record's identifier
62 * @return null|Record
63 */
64 public static function get_record( $id ) {
65
66 $records = self::get_records( array( 'id' => $id ) );
67
68 return ! empty( $records ) ? current( $records ) : null;
69 }
70
71
72 /**
73 * Gets an array of record objects.
74 *
75 * @since 2.0.0
76 *
77 * @param array $args associative array of arguments to query records
78 * @return Record[]
79 */
80 public static function get_records( array $args = array() ) {
81
82 $args = wp_parse_args(
83 $args,
84 array(
85 'id' => null,
86 'type' => null,
87 'product' => null,
88 'orderby' => 'date',
89 'sort' => 'DESC',
90 'limit' => 50,
91 )
92 );
93
94 $records = array();
95 $raw_records = get_option( self::$records_option_key, array() );
96
97 foreach ( $raw_records as $raw_record_data ) {
98
99 $record = new Record( $raw_record_data );
100
101 if ( ! empty( $args['id'] ) ) {
102
103 $id = is_array( $args['id'] ) ? $args['id'] : explode( ',', $args['id'] );
104
105 if ( ! in_array( $record->get_id(), $id, false ) ) {
106 continue;
107 }
108 }
109
110 if ( ! empty( $args['type'] ) ) {
111
112 $type = is_array( $args['type'] ) ? $args['type'] : explode( ',', $args['type'] );
113
114 if ( ! $record->is_type( $type ) ) {
115 continue;
116 }
117 }
118
119 if ( ! empty( $args['product'] ) ) {
120
121 $product = is_array( $args['product'] ) ? $args['product'] : explode( ',', $args['product'] );
122
123 if ( ! in_array( $record->get_product_id(), $product, false ) ) {
124 continue;
125 }
126 }
127
128 $records[ $record->get_id() ] = $record;
129 }
130
131 if ( ! empty( $records ) ) {
132
133 switch ( $args['orderby'] ) {
134 case 'date':
135 uasort( $records, array( 'self', 'sort_records_by_date' ) );
136 break;
137 case 'type':
138 uasort( $records, array( 'self', 'sort_records_by_type' ) );
139 break;
140 }
141
142 if ( 'DESC' === $args['sort'] ) {
143 $records = array_reverse( $records, true );
144 }
145
146 $records = array_slice( $records, 0, max( 50, absint( $args['limit'] ) ) );
147 }
148
149 return $records;
150 }
151
152
153 /**
154 * Compares two records for sorting by date.
155 *
156 * @see usort() callback
157 * @see Records::get_records()
158 *
159 * @since 2.0.0
160 *
161 * @param Record $record_1 first record
162 * @param Record $record_2 second record
163 * @return int should return 0, -1 or +1
164 */
165 private static function sort_records_by_date( $record_1, $record_2 ) {
166
167 $compare = 0;
168
169 if ( $record_1 instanceof Record && $record_2 instanceof Record ) {
170
171 $timestamp_1 = $record_1->get_timestamp();
172 $timestamp_2 = $record_2->get_timestamp();
173
174 if ( $timestamp_1 > $timestamp_2 ) {
175 $compare = 1;
176 } elseif ( $timestamp_1 < $timestamp_2 ) {
177 $compare = -1;
178 } else { // compare by ID as a fallback to keep order consistency
179 $compare = strnatcmp( $record_1->get_id(), $record_2->get_id() );
180 }
181 }
182
183 return $compare;
184 }
185
186
187 /**
188 * Compares two records for sorting by type.
189 *
190 * @see usort() callback
191 * @see Records::get_records()
192 *
193 * @since 2.0.0
194 *
195 * @param Record $record_1 first record
196 * @param Record $record_2 second record
197 * @return int should return 0, -1 or +1
198 */
199 private static function sort_records_by_type( $record_1, $record_2 ) {
200
201 $compare = 0;
202
203 if ( $record_1 instanceof Record && $record_2 instanceof Record ) {
204
205 $compare = strnatcmp( $record_1->get_type(), $record_2->get_type() );
206
207 // if they are equal, sort by date within the same type group
208 if ( 0 === $compare ) {
209
210 $timestamp_1 = $record_1->get_timestamp();
211 $timestamp_2 = $record_2->get_timestamp();
212
213 if ( $timestamp_1 > $timestamp_2 ) {
214 $compare = 1;
215 } elseif ( $timestamp_1 < $timestamp_2 ) {
216 $compare = -1;
217 } else { // compare by ID as a fallback to keep order consistency
218 $compare = strnatcmp( $record_1->get_id(), $record_2->get_id() );
219 }
220 }
221 }
222
223 return $compare;
224 }
225
226
227 /**
228 * Saves a new record.
229 *
230 * @since 2.0.0
231 *
232 * @param array|Record $data raw data or record object
233 * @return bool success
234 */
235 public static function set_record( $data ) {
236
237 if ( is_array( $data ) ) {
238 $new_record = new Record( $data );
239 } else {
240 $new_record = $data;
241 }
242
243 if ( $new_record instanceof Record ) {
244
245 // ensures there are never more than 50 records, leaving behind the older ones
246 $existing_records = self::get_records( array( 'limit' => 49 ) );
247 $raw_records = array();
248
249 foreach ( $existing_records as $existing_record ) {
250 $raw_records[ $existing_record->get_id() ] = $existing_record->get_data();
251 }
252
253 $raw_records[ $new_record->get_id() ] = $new_record->get_data();
254
255 $success = update_option( self::$records_option_key, $raw_records );
256
257 } else {
258
259 $success = false;
260 }
261
262 return $success;
263 }
264
265
266 /**
267 * Saves multiple records.
268 *
269 * @since 2.0.0
270 *
271 * @param array|Record[] $data array of raw record data or array of record objects
272 * @return bool success
273 */
274 public static function set_records( array $data ) {
275
276 $success = false;
277 $raw_records = array();
278
279 foreach ( $data as $record ) {
280
281 if ( is_array( $record ) ) {
282 $record = new Record( $record );
283 }
284
285 if ( $record instanceof Record ) {
286 $raw_records[ $record->get_id() ] = $record->get_data();
287 }
288 }
289
290 if ( ! empty( $raw_records ) ) {
291
292 $records = self::get_records( array( 'limit' => 50 - count( $raw_records ) ) );
293
294 foreach ( $records as $record ) {
295 $raw_records[ $record->get_id() ] = $record->get_data();
296 }
297
298 $success = update_option( self::$records_option_key, $raw_records );
299 }
300
301 return $success;
302 }
303
304
305 /**
306 * Removes a record permanently.
307 *
308 * @since 2.0.0
309 *
310 * @param int $id record identifier
311 * @return bool success
312 */
313 public static function delete_record( $id ) {
314
315 $success = false;
316 $raw_records = get_option( self::$records_option_key, array() );
317
318 if ( array_key_exists( $id, $raw_records ) ) {
319
320 unset( $raw_records[ $id ] );
321
322 $success = update_option( self::$records_option_key, $raw_records );
323 }
324
325 return $success;
326 }
327
328
329 /**
330 * Delete multiple records.
331 *
332 * @since 2.0.0
333 *
334 * @param array $args query arguments
335 * @return int count of records removed
336 */
337 public static function delete_records( array $args ) {
338
339 $removed = 0;
340 $raw_records = get_option( self::$records_option_key, array() );
341
342 foreach ( $raw_records as $raw_record ) {
343
344 $record = new Record( $raw_record );
345
346 if ( isset( $args['id'] ) ) {
347
348 $id = is_array( $args['id'] ) ? $args['id'] : explode( ',', $args['id'] );
349
350 if ( in_array( $record->get_id(), $id, false ) ) {
351 unset( $raw_records[ $record->get_id() ] );
352 $removed++;
353 }
354 }
355
356 if ( isset( $args['type'] ) ) {
357
358 $type = $args['type'];
359
360 if ( $record->is_type( $type ) ) {
361 unset( $raw_records[ $record->get_id() ] );
362 $removed++;
363 }
364 }
365 }
366
367 if ( $removed > 0 ) {
368
369 $success = update_option( self::$records_option_key, $raw_records );
370
371 if ( ! $success ) {
372 $removed = 0;
373 }
374 }
375
376 return $removed;
377 }
378
379
380 /**
381 * Removes all records.
382 *
383 * @since 2.0.0
384 *
385 * @return bool
386 */
387 public static function clean_records() {
388
389 return update_option( self::$records_option_key, array() );
390 }
391
392
393 }
394