PluginProbe ʕ •ᴥ•ʔ
WooCommerce Square / 5.4.3
WooCommerce Square v5.4.3
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 3 months ago
Record.php
615 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 GNU General Public License v3.0 or later
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 or later
22 */
23
24 namespace WooCommerce\Square\Sync\Records;
25
26 defined( 'ABSPATH' ) || exit;
27
28 use WooCommerce\Square\Sync\Records;
29
30 /**
31 * The sync record object.
32 *
33 * @since 2.0.0
34 */
35 class Record {
36
37
38 /** @var string unique identifier */
39 private $id = '';
40
41 /** @var string date in UTC */
42 private $date = '';
43
44 /** @var string the record status */
45 private $type = '';
46
47 /** @var string message (optional, may contain HTML) */
48 private $message = '';
49
50 /** @var int associated product ID (optional) */
51 private $product_id = 0;
52
53 /** @var bool whether the associated product was hidden when the record was created */
54 private $product_hidden = false;
55
56
57 /**
58 * Sync record constructor.
59 *
60 * @since 2.0.0
61 *
62 * @param array|int $data raw data or related product ID
63 */
64 public function __construct( $data ) {
65
66 foreach ( $this->parse_data( $data ) as $key => $value ) {
67 $this->$key = $value;
68 }
69 }
70
71
72 /**
73 * Parses data from the store into the object properties
74 *
75 * @since 2.0.0
76 *
77 * @param int|array $data associative array or product ID
78 * @return array
79 */
80 private function parse_data( $data ) {
81
82 if ( is_numeric( $data ) ) {
83 $data = array(
84 'type' => 'alert',
85 'product_id' => absint( $data ),
86 );
87 }
88
89 $date = date( 'Y-m-d H:i:s', current_time( 'timestamp', true ) ); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date, WordPress.DateTime.CurrentTimeTimestamp.RequestedUTC
90 $data = wp_parse_args(
91 (array) $data,
92 array(
93 'type' => $this->get_default_type(),
94 'date' => $date,
95 'message' => '',
96 'product_id' => 0,
97 'product_hidden' => false,
98 )
99 );
100
101 if ( empty( $data['id'] ) ) {
102 $data['id'] = uniqid( 'wc_square_sync_record_', false );
103 }
104
105 if ( ! strtotime( $data['date'] ) ) {
106 $data['date'] = $date;
107 }
108
109 if ( ! is_string( $data['message'] ) ) {
110 $data['message'] = '';
111 }
112
113 if ( ! is_numeric( $data['product_id'] ) ) {
114 $data['product_id'] = $data['product_id'] instanceof \WC_Product ? $data['product_id']->get_id() : 0;
115 }
116
117 if ( 0 === $data['product_id'] ) {
118 $data['product_hidden'] = false;
119 }
120
121 return $data;
122 }
123
124
125 /**
126 * Gets the record's raw data.
127 *
128 * @since 2.0.0
129 *
130 * @return array associative array
131 */
132 public function get_data() {
133
134 return array(
135 'id' => (string) $this->id,
136 'type' => (string) $this->type,
137 'date' => (string) $this->date,
138 'message' => (string) $this->message,
139 'product_id' => (int) $this->product_id,
140 'product_hidden' => (bool) $this->product_hidden,
141 );
142 }
143
144
145 /**
146 * Gets the record's ID.
147 *
148 * @since 2.0.0
149 *
150 * @return string
151 */
152 public function get_id() {
153
154 return (string) $this->id;
155 }
156
157
158 /**
159 * Sets an ID for the record.
160 *
161 * @since 2.0.0
162 *
163 * @param null|string $id
164 * @return string set ID
165 */
166 public function set_id( $id = null ) {
167
168 if ( is_string( $id ) ) {
169 $id = trim( $id );
170 } else {
171 $id = null;
172 }
173
174 if ( empty( $id ) ) {
175 $id = uniqid( 'wc_square_sync_record_', false );
176 }
177
178 return (string) $this->id = $id;
179 }
180
181
182 /**
183 * Gets a record's default status.
184 *
185 * @since 2.0.0
186 *
187 * @return string
188 */
189 private function get_default_type() {
190
191 return 'info';
192 }
193
194
195 /**
196 * Gets a record's possible statuses.
197 *
198 * @since 2.0.0
199 *
200 * @return array associative array of types and labels
201 */
202 private function get_valid_types() {
203
204 return array(
205 'info' => __( 'Info', 'woocommerce-square' ),
206 'notice' => __( 'Notice', 'woocommerce-square' ),
207 'alert' => __( 'Alert', 'woocommerce-square' ),
208 'resolved' => __( 'Resolved', 'woocommerce-square' ),
209 'failed' => __( 'Failed', '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() ); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
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' ) ); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
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 ); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
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 if ( 'variation' === $product->get_type() ) {
373 $message = sprintf(
374 /* translators: Placeholder: %s - product name */
375 esc_html__( '%s variation not found in Square.', 'woocommerce-square' ),
376 '<a href="' . esc_url( get_edit_post_link( $product->get_parent_id() ) ) . '">' . $product->get_formatted_name() . '</a>'
377 );
378 } else {
379 $message = sprintf(
380 /* translators: Placeholder: %s - product name */
381 esc_html__( '%s not found in Square.', 'woocommerce-square' ),
382 '<a href="' . esc_url( get_edit_post_link( $product->get_id() ) ) . '">' . $product->get_formatted_name() . '</a>'
383 );
384 }
385 }
386
387 return $message;
388 }
389
390
391 /**
392 * Sets the record's message.
393 *
394 * @since 2.0.0
395 *
396 * @param string $message may contain HTML
397 * @return bool success
398 */
399 public function set_message( $message ) {
400
401 if ( is_string( $message ) ) {
402 $this->message = trim( $message );
403 } else {
404 $this->message = '';
405 }
406
407 return ! empty( $this->message );
408 }
409
410
411 /**
412 * Removes the record's message.
413 *
414 * @since 2.0.0
415 */
416 public function remove_message() {
417
418 $this->message = '';
419 }
420
421
422 /**
423 * Sets the record's product.
424 *
425 * @since 2.0.0
426 *
427 * @param int|\WC_Product $product
428 * @return bool success
429 */
430 public function set_product( $product ) {
431
432 if ( $product instanceof \WC_Product ) {
433 $product_id = $product->get_id();
434 } else {
435 $product_id = $product;
436 }
437
438 if ( is_numeric( $product_id ) ) {
439 $this->product_id = $product_id;
440 } else {
441 $this->product_id = 0;
442 }
443
444 return $this->product_id > 0;
445 }
446
447
448 /**
449 * Removes a related product from the record.
450 *
451 * @since 2.0.0
452 */
453 public function remove_product() {
454
455 $this->product_id = 0;
456 }
457
458
459 /**
460 * Checks whether there is a valid product associated with the record.
461 *
462 * @since 2.0.0
463 *
464 * @return bool
465 */
466 public function has_product() {
467
468 return $this->get_product() instanceof \WC_Product;
469 }
470
471
472 /**
473 * Gets the product associated with the record.
474 *
475 * @since 2.0.0
476 *
477 * @return null|\WC_Product
478 */
479 public function get_product() {
480
481 $product = wc_get_product( $this->get_product_id() );
482
483 return $product instanceof \WC_Product ? $product : null;
484 }
485
486
487 /**
488 * Gets the product ID associated with the record.
489 *
490 * @since 2.0.0
491 *
492 * @return int
493 */
494 public function get_product_id() {
495
496 return absint( $this->product_id );
497 }
498
499
500 /**
501 * Sets a flag whether when adding the record the product was contextually hidden from catalog.
502 *
503 * 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.
504 *
505 * @since 2.0.0
506 *
507 * @param bool $was_hidden whether product was hidden when creating the record
508 * @return bool success
509 */
510 public function set_product_hidden( $was_hidden = true ) {
511
512 $set = false;
513
514 if ( is_bool( $was_hidden ) ) {
515 $this->product_hidden = $was_hidden;
516 $set = true;
517 }
518
519 return $set;
520 }
521
522
523 /**
524 * Checks whether a flag was set for hiding the product from catalog when the record was created.
525 *
526 * This may not reflect the actual product visibility status, it is only for historical purposes.
527 *
528 * @since 2.0.0
529 *
530 * @return bool
531 */
532 public function was_product_hidden() {
533
534 return true === $this->product_hidden;
535 }
536
537
538 /**
539 * Gets the record's actions.
540 *
541 * @since 2.0.0
542 *
543 * @return \stdClass[] associative array of action names and action properties as objects
544 */
545 public function get_actions() {
546
547 $actions = array();
548
549 if ( ! $this->is_resolved() ) {
550
551 $actions['delete'] = (object) array(
552 'name' => 'delete',
553 'label' => __( 'Delete', 'woocommerce-square' ),
554 'icon' => '<span class="dashicons dashicons-trash"></span>',
555 );
556
557 if ( ! $this->is_type( 'info' ) ) {
558
559 $actions['resolve'] = (object) array(
560 'name' => 'resolve',
561 'label' => __( 'Ignore', 'woocommerce-square' ),
562 'icon' => '<span class="dashicons dashicons-hidden"></span>',
563 );
564 }
565
566 if ( $this->has_product() ) {
567
568 $actions['unsync'] = (object) array(
569 'name' => 'unsync',
570 'label' => __( 'Unlink', 'woocommerce-square' ),
571 'icon' => '<span class="dashicons dashicons-editor-unlink"></span>',
572 );
573 }
574 }
575
576 /**
577 * Filters the sync record action.
578 *
579 * @since 2.0.0
580 *
581 * @param \stdClass[] array of action names and objects
582 * @param Record instance of the current record object
583 */
584 return (array) apply_filters( 'wc_square_sync_record_actions', $actions, $this );
585 }
586
587
588 /**
589 * Saves the record to storage.
590 *
591 * @since 2.0.0
592 *
593 * @return bool success
594 */
595 public function save() {
596
597 return Records::set_record( $this );
598 }
599
600
601 /**
602 * Deletes the record from storage.
603 *
604 * @since 2.0.0
605 *
606 * @return bool success
607 */
608 public function destroy() {
609
610 return Records::delete_record( $this->get_id() );
611 }
612
613
614 }
615