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 / Record.php
woocommerce-square / includes / Sync / Records Last commit date
Record.php 6 years ago
Record.php
607 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\Records;
25
26 defined( 'ABSPATH' ) || exit;
27
28 use SkyVerge\WooCommerce\PluginFramework\v5_4_0 as Framework;
29 use WooCommerce\Square\Sync\Records;
30
31 /**
32 * The sync record object.
33 *
34 * @since 2.0.0
35 */
36 class Record {
37
38
39 /** @var string unique identifier */
40 private $id = '';
41
42 /** @var string date in UTC */
43 private $date = '';
44
45 /** @var string the record status */
46 private $type = '';
47
48 /** @var string message (optional, may contain HTML) */
49 private $message = '';
50
51 /** @var int associated product ID (optional) */
52 private $product_id = 0;
53
54 /** @var bool whether the associated product was hidden when the record was created */
55 private $product_hidden = false;
56
57
58 /**
59 * Sync record constructor.
60 *
61 * @since 2.0.0
62 *
63 * @param array|int $data raw data or related product ID
64 */
65 public function __construct( $data ) {
66
67 foreach ( $this->parse_data( $data ) as $key => $value ) {
68 $this->$key = $value;
69 }
70 }
71
72
73 /**
74 * Parses data from the store into the object properties
75 *
76 * @since 2.0.0
77 *
78 * @param int|array $data associative array or product ID
79 * @return array
80 */
81 private function parse_data( $data ) {
82
83 if ( is_numeric( $data ) ) {
84 $data = array(
85 'type' => 'alert',
86 'product_id' => absint( $data ),
87 );
88 }
89
90 $date = date( 'Y-m-d H:i:s', current_time( 'timestamp', true ) );
91 $data = wp_parse_args(
92 (array) $data,
93 array(
94 'type' => $this->get_default_type(),
95 'date' => $date,
96 'message' => '',
97 'product_id' => 0,
98 'product_hidden' => false,
99 )
100 );
101
102 if ( empty( $data['id'] ) ) {
103 $data['id'] = uniqid( 'wc_square_sync_record_', false );
104 }
105
106 if ( ! strtotime( $data['date'] ) ) {
107 $data['date'] = $date;
108 }
109
110 if ( ! is_string( $data['message'] ) ) {
111 $data['message'] = '';
112 }
113
114 if ( ! is_numeric( $data['product_id'] ) ) {
115 $data['product_id'] = $data['product_id'] instanceof \WC_Product ? $data['product_id']->get_id() : 0;
116 }
117
118 if ( 0 === $data['product_id'] ) {
119 $data['product_hidden'] = false;
120 }
121
122 return $data;
123 }
124
125
126 /**
127 * Gets the record's raw data.
128 *
129 * @since 2.0.0
130 *
131 * @return array associative array
132 */
133 public function get_data() {
134
135 return array(
136 'id' => (string) $this->id,
137 'type' => (string) $this->type,
138 'date' => (string) $this->date,
139 'message' => (string) $this->message,
140 'product_id' => (int) $this->product_id,
141 'product_hidden' => (bool) $this->product_hidden,
142 );
143 }
144
145
146 /**
147 * Gets the record's ID.
148 *
149 * @since 2.0.0
150 *
151 * @return string
152 */
153 public function get_id() {
154
155 return (string) $this->id;
156 }
157
158
159 /**
160 * Sets an ID for the record.
161 *
162 * @since 2.0.0
163 *
164 * @param null|string $id
165 * @return string set ID
166 */
167 public function set_id( $id = null ) {
168
169 if ( is_string( $id ) ) {
170 $id = trim( $id );
171 } else {
172 $id = null;
173 }
174
175 if ( empty( $id ) ) {
176 $id = uniqid( 'wc_square_sync_record_', false );
177 }
178
179 return (string) $this->id = $id;
180 }
181
182
183 /**
184 * Gets a record's default status.
185 *
186 * @since 2.0.0
187 *
188 * @return string
189 */
190 private function get_default_type() {
191
192 return 'info';
193 }
194
195
196 /**
197 * Gets a record's possible statuses.
198 *
199 * @since 2.0.0
200 *
201 * @return array associative array of types and labels
202 */
203 private function get_valid_types() {
204
205 return array(
206 'info' => __( 'Info', 'woocommerce-square' ),
207 'notice' => __( 'Notice', 'woocommerce-square' ),
208 'alert' => __( 'Alert', 'woocommerce-square' ),
209 'resolved' => __( 'Resolved', 'woocommerce-square' ),
210 );
211 }
212
213
214 /**
215 * Gets the record's label.
216 *
217 * @since 2.0.0
218 *
219 * @return string
220 */
221 public function get_label() {
222
223 $type = $this->get_type();
224 $types = $this->get_valid_types();
225
226 return isset( $types[ $type ] ) ? $types[ $type ] : $types[ $this->get_default_type() ];
227 }
228
229
230 /**
231 * Gets the record status.
232 *
233 * @since 2.0.0
234 *
235 * @return string
236 */
237 public function get_type() {
238
239 return array_key_exists( $this->type, $this->get_valid_types() ) ? $this->type : $this->get_default_type();
240 }
241
242
243 /**
244 * Set's the record status.
245 *
246 * @since 2.0.0
247 *
248 * @param string $type a known type
249 * @return string set type
250 */
251 public function set_type( $type ) {
252
253 if ( array_key_exists( $type, $this->get_valid_types() ) ) {
254 $this->type = $type;
255 } else {
256 $this->type = $this->get_default_type();
257 }
258
259 return $this->type;
260 }
261
262
263 /**
264 * Checks if the record is of a given type.
265 *
266 * @since 2.0.0
267 *
268 * @param array|string $type one or more types to check
269 * @return bool
270 */
271 public function is_type( $type ) {
272
273 return is_array( $type ) ? in_array( $this->type, $type, true ) : $this->type === $type;
274 }
275
276
277 /**
278 * Checks whether the record is resolved.
279 *
280 * @since 2.0.0
281 *
282 * @return bool
283 */
284 public function is_resolved() {
285
286 return $this->is_type( 'resolved' );
287 }
288
289
290 /**
291 * Resolves the record.
292 *
293 * @since 2.0.0
294 */
295 public function resolve() {
296
297 $this->set_type( 'resolved' );
298 }
299
300
301 /**
302 * Gets the record's timestamp, in UTC.
303 *
304 * @since 2.0.0
305 *
306 if (
307 * @return int
308 */
309 public function get_timestamp() {
310
311 return (int) strtotime( $this->date );
312 }
313
314
315 /**
316 * Gets the record's date, in UTC.
317 *
318 * @since 2.0.0
319 *
320 * @param string $format defaults to MySQL format
321 * @return string
322 */
323 public function get_date( $format = 'Y-m-d H:i:s' ) {
324
325 return date( (string) $format, $this->get_timestamp() );
326 }
327
328
329 /**
330 * Gets the record's date, in the local timezone.
331 *
332 * @since 2.0.0
333 *
334 * @param null|string $format optional PHP date format (defaults to the site date/time format)
335 * @return string
336 */
337 public function get_local_date( $format = null ) {
338
339 if ( ! is_string( $format ) ) {
340 $format = wc_date_format() . ' ' . wc_time_format();
341 }
342
343 try {
344
345 $date = new \DateTime( date( (string) $format, $this->get_timestamp() ), new \DateTimeZone( 'UTC' ) );
346 $timezone = new \DateTimeZone( wc_timezone_string() );
347 $offset = $timezone->getOffset( $date );
348 $timestamp = $date->getTimestamp() + $offset;
349
350 } catch ( \Exception $e ) {
351
352 $timestamp = $this->get_timestamp();
353 }
354
355 return date( (string) $format, $timestamp );
356 }
357
358
359 /**
360 * Gets the record's message.
361 *
362 * @since 2.0.0
363 *
364 * @return string
365 */
366 public function get_message() {
367
368 $message = trim( $this->message );
369
370 if ( '' === $message && ( $product = $this->get_product() ) ) {
371
372 $message = sprintf(
373 /* translators: Placeholder: %s - product name */
374 esc_html__( '%s not found in Square.', 'woocommerce-square' ),
375 '<a href="' . esc_url( get_edit_post_link( $product->get_id() ) ) . '">' . $product->get_formatted_name() . '</a>'
376 );
377 }
378
379 return $message;
380 }
381
382
383 /**
384 * Sets the record's message.
385 *
386 * @since 2.0.0
387 *
388 * @param string $message may contain HTML
389 * @return bool success
390 */
391 public function set_message( $message ) {
392
393 if ( is_string( $message ) ) {
394 $this->message = trim( $message );
395 } else {
396 $this->message = '';
397 }
398
399 return ! empty( $this->message );
400 }
401
402
403 /**
404 * Removes the record's message.
405 *
406 * @since 2.0.0
407 */
408 public function remove_message() {
409
410 $this->message = '';
411 }
412
413
414 /**
415 * Sets the record's product.
416 *
417 * @since 2.0.0
418 *
419 * @param int|\WC_Product $product
420 * @return bool success
421 */
422 public function set_product( $product ) {
423
424 if ( $product instanceof \WC_Product ) {
425 $product_id = $product->get_id();
426 } else {
427 $product_id = $product;
428 }
429
430 if ( is_numeric( $product_id ) ) {
431 $this->product_id = $product_id;
432 } else {
433 $this->product_id = 0;
434 }
435
436 return $this->product_id > 0;
437 }
438
439
440 /**
441 * Removes a related product from the record.
442 *
443 * @since 2.0.0
444 */
445 public function remove_product() {
446
447 $this->product_id = 0;
448 }
449
450
451 /**
452 * Checks whether there is a valid product associated with the record.
453 *
454 * @since 2.0.0
455 *
456 * @return bool
457 */
458 public function has_product() {
459
460 return $this->get_product() instanceof \WC_Product;
461 }
462
463
464 /**
465 * Gets the product associated with the record.
466 *
467 * @since 2.0.0
468 *
469 * @return null|\WC_Product
470 */
471 public function get_product() {
472
473 $product = wc_get_product( $this->get_product_id() );
474
475 return $product instanceof \WC_Product ? $product : null;
476 }
477
478
479 /**
480 * Gets the product ID associated with the record.
481 *
482 * @since 2.0.0
483 *
484 * @return int
485 */
486 public function get_product_id() {
487
488 return absint( $this->product_id );
489 }
490
491
492 /**
493 * Sets a flag whether when adding the record the product was contextually hidden from catalog.
494 *
495 * Note: this is for historical record purposes and may not correspond to the effective product visibility, nor does not affect the product visibility when called.
496 *
497 * @since 2.0.0
498 *
499 * @param bool $was_hidden whether product was hidden when creating the record
500 * @return bool success
501 */
502 public function set_product_hidden( $was_hidden = true ) {
503
504 $set = false;
505
506 if ( is_bool( $was_hidden ) ) {
507 $this->product_hidden = $was_hidden;
508 $set = true;
509 }
510
511 return $set;
512 }
513
514
515 /**
516 * Checks whether a flag was set for hiding the product from catalog when the record was created.
517 *
518 * This may not reflect the actual product visibility status, it is only for historical purposes.
519 *
520 * @since 2.0.0
521 *
522 * @return bool
523 */
524 public function was_product_hidden() {
525
526 return true === $this->product_hidden;
527 }
528
529
530 /**
531 * Gets the record's actions.
532 *
533 * @since 2.0.0
534 *
535 * @return \stdClass[] associative array of action names and action properties as objects
536 */
537 public function get_actions() {
538
539 $actions = array();
540
541 if ( ! $this->is_resolved() ) {
542
543 $actions['delete'] = (object) array(
544 'name' => 'delete',
545 'label' => __( 'Delete', 'woocommerce-square' ),
546 'icon' => '<span class="dashicons dashicons-trash"></span>',
547 );
548
549 if ( ! $this->is_type( 'info' ) ) {
550
551 $actions['resolve'] = (object) array(
552 'name' => 'resolve',
553 'label' => __( 'Ignore', 'woocommerce-square' ),
554 'icon' => '<span class="dashicons dashicons-hidden"></span>',
555 );
556 }
557
558 if ( $this->has_product() ) {
559
560 $actions['unsync'] = (object) array(
561 'name' => 'unsync',
562 'label' => __( 'Unlink', 'woocommerce-square' ),
563 'icon' => '<span class="dashicons dashicons-editor-unlink"></span>',
564 );
565 }
566 }
567
568 /**
569 * Filters the sync record action.
570 *
571 * @since 2.0.0
572 *
573 * @param \stdClass[] array of action names and objects
574 * @param Record instance of the current record object
575 */
576 return (array) apply_filters( 'wc_square_sync_record_actions', $actions, $this );
577 }
578
579
580 /**
581 * Saves the record to storage.
582 *
583 * @since 2.0.0
584 *
585 * @return bool success
586 */
587 public function save() {
588
589 return Records::set_record( $this );
590 }
591
592
593 /**
594 * Deletes the record from storage.
595 *
596 * @since 2.0.0
597 *
598 * @return bool success
599 */
600 public function destroy() {
601
602 return Records::delete_record( $this->get_id() );
603 }
604
605
606 }
607