Record.php
614 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 ) ); |
| 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 | ); |
| 210 | } |
| 211 | |
| 212 | |
| 213 | /** |
| 214 | * Gets the record's label. |
| 215 | * |
| 216 | * @since 2.0.0 |
| 217 | * |
| 218 | * @return string |
| 219 | */ |
| 220 | public function get_label() { |
| 221 | |
| 222 | $type = $this->get_type(); |
| 223 | $types = $this->get_valid_types(); |
| 224 | |
| 225 | return isset( $types[ $type ] ) ? $types[ $type ] : $types[ $this->get_default_type() ]; |
| 226 | } |
| 227 | |
| 228 | |
| 229 | /** |
| 230 | * Gets the record status. |
| 231 | * |
| 232 | * @since 2.0.0 |
| 233 | * |
| 234 | * @return string |
| 235 | */ |
| 236 | public function get_type() { |
| 237 | |
| 238 | return array_key_exists( $this->type, $this->get_valid_types() ) ? $this->type : $this->get_default_type(); |
| 239 | } |
| 240 | |
| 241 | |
| 242 | /** |
| 243 | * Set's the record status. |
| 244 | * |
| 245 | * @since 2.0.0 |
| 246 | * |
| 247 | * @param string $type a known type |
| 248 | * @return string set type |
| 249 | */ |
| 250 | public function set_type( $type ) { |
| 251 | |
| 252 | if ( array_key_exists( $type, $this->get_valid_types() ) ) { |
| 253 | $this->type = $type; |
| 254 | } else { |
| 255 | $this->type = $this->get_default_type(); |
| 256 | } |
| 257 | |
| 258 | return $this->type; |
| 259 | } |
| 260 | |
| 261 | |
| 262 | /** |
| 263 | * Checks if the record is of a given type. |
| 264 | * |
| 265 | * @since 2.0.0 |
| 266 | * |
| 267 | * @param array|string $type one or more types to check |
| 268 | * @return bool |
| 269 | */ |
| 270 | public function is_type( $type ) { |
| 271 | |
| 272 | return is_array( $type ) ? in_array( $this->type, $type, true ) : $this->type === $type; |
| 273 | } |
| 274 | |
| 275 | |
| 276 | /** |
| 277 | * Checks whether the record is resolved. |
| 278 | * |
| 279 | * @since 2.0.0 |
| 280 | * |
| 281 | * @return bool |
| 282 | */ |
| 283 | public function is_resolved() { |
| 284 | |
| 285 | return $this->is_type( 'resolved' ); |
| 286 | } |
| 287 | |
| 288 | |
| 289 | /** |
| 290 | * Resolves the record. |
| 291 | * |
| 292 | * @since 2.0.0 |
| 293 | */ |
| 294 | public function resolve() { |
| 295 | |
| 296 | $this->set_type( 'resolved' ); |
| 297 | } |
| 298 | |
| 299 | |
| 300 | /** |
| 301 | * Gets the record's timestamp, in UTC. |
| 302 | * |
| 303 | * @since 2.0.0 |
| 304 | * |
| 305 | if ( |
| 306 | * @return int |
| 307 | */ |
| 308 | public function get_timestamp() { |
| 309 | |
| 310 | return (int) strtotime( $this->date ); |
| 311 | } |
| 312 | |
| 313 | |
| 314 | /** |
| 315 | * Gets the record's date, in UTC. |
| 316 | * |
| 317 | * @since 2.0.0 |
| 318 | * |
| 319 | * @param string $format defaults to MySQL format |
| 320 | * @return string |
| 321 | */ |
| 322 | public function get_date( $format = 'Y-m-d H:i:s' ) { |
| 323 | |
| 324 | return date( (string) $format, $this->get_timestamp() ); |
| 325 | } |
| 326 | |
| 327 | |
| 328 | /** |
| 329 | * Gets the record's date, in the local timezone. |
| 330 | * |
| 331 | * @since 2.0.0 |
| 332 | * |
| 333 | * @param null|string $format optional PHP date format (defaults to the site date/time format) |
| 334 | * @return string |
| 335 | */ |
| 336 | public function get_local_date( $format = null ) { |
| 337 | |
| 338 | if ( ! is_string( $format ) ) { |
| 339 | $format = wc_date_format() . ' ' . wc_time_format(); |
| 340 | } |
| 341 | |
| 342 | try { |
| 343 | |
| 344 | $date = new \DateTime( date( (string) $format, $this->get_timestamp() ), new \DateTimeZone( 'UTC' ) ); |
| 345 | $timezone = new \DateTimeZone( wc_timezone_string() ); |
| 346 | $offset = $timezone->getOffset( $date ); |
| 347 | $timestamp = $date->getTimestamp() + $offset; |
| 348 | |
| 349 | } catch ( \Exception $e ) { |
| 350 | |
| 351 | $timestamp = $this->get_timestamp(); |
| 352 | } |
| 353 | |
| 354 | return date( (string) $format, $timestamp ); |
| 355 | } |
| 356 | |
| 357 | |
| 358 | /** |
| 359 | * Gets the record's message. |
| 360 | * |
| 361 | * @since 2.0.0 |
| 362 | * |
| 363 | * @return string |
| 364 | */ |
| 365 | public function get_message() { |
| 366 | |
| 367 | $message = trim( $this->message ); |
| 368 | |
| 369 | if ( '' === $message && ( $product = $this->get_product() ) ) { |
| 370 | |
| 371 | if ( 'variation' === $product->get_type() ) { |
| 372 | $message = sprintf( |
| 373 | /* translators: Placeholder: %s - product name */ |
| 374 | esc_html__( '%s variation not found in Square.', 'woocommerce-square' ), |
| 375 | '<a href="' . esc_url( get_edit_post_link( $product->get_parent_id() ) ) . '">' . $product->get_formatted_name() . '</a>' |
| 376 | ); |
| 377 | } else { |
| 378 | $message = sprintf( |
| 379 | /* translators: Placeholder: %s - product name */ |
| 380 | esc_html__( '%s not found in Square.', 'woocommerce-square' ), |
| 381 | '<a href="' . esc_url( get_edit_post_link( $product->get_id() ) ) . '">' . $product->get_formatted_name() . '</a>' |
| 382 | ); |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | return $message; |
| 387 | } |
| 388 | |
| 389 | |
| 390 | /** |
| 391 | * Sets the record's message. |
| 392 | * |
| 393 | * @since 2.0.0 |
| 394 | * |
| 395 | * @param string $message may contain HTML |
| 396 | * @return bool success |
| 397 | */ |
| 398 | public function set_message( $message ) { |
| 399 | |
| 400 | if ( is_string( $message ) ) { |
| 401 | $this->message = trim( $message ); |
| 402 | } else { |
| 403 | $this->message = ''; |
| 404 | } |
| 405 | |
| 406 | return ! empty( $this->message ); |
| 407 | } |
| 408 | |
| 409 | |
| 410 | /** |
| 411 | * Removes the record's message. |
| 412 | * |
| 413 | * @since 2.0.0 |
| 414 | */ |
| 415 | public function remove_message() { |
| 416 | |
| 417 | $this->message = ''; |
| 418 | } |
| 419 | |
| 420 | |
| 421 | /** |
| 422 | * Sets the record's product. |
| 423 | * |
| 424 | * @since 2.0.0 |
| 425 | * |
| 426 | * @param int|\WC_Product $product |
| 427 | * @return bool success |
| 428 | */ |
| 429 | public function set_product( $product ) { |
| 430 | |
| 431 | if ( $product instanceof \WC_Product ) { |
| 432 | $product_id = $product->get_id(); |
| 433 | } else { |
| 434 | $product_id = $product; |
| 435 | } |
| 436 | |
| 437 | if ( is_numeric( $product_id ) ) { |
| 438 | $this->product_id = $product_id; |
| 439 | } else { |
| 440 | $this->product_id = 0; |
| 441 | } |
| 442 | |
| 443 | return $this->product_id > 0; |
| 444 | } |
| 445 | |
| 446 | |
| 447 | /** |
| 448 | * Removes a related product from the record. |
| 449 | * |
| 450 | * @since 2.0.0 |
| 451 | */ |
| 452 | public function remove_product() { |
| 453 | |
| 454 | $this->product_id = 0; |
| 455 | } |
| 456 | |
| 457 | |
| 458 | /** |
| 459 | * Checks whether there is a valid product associated with the record. |
| 460 | * |
| 461 | * @since 2.0.0 |
| 462 | * |
| 463 | * @return bool |
| 464 | */ |
| 465 | public function has_product() { |
| 466 | |
| 467 | return $this->get_product() instanceof \WC_Product; |
| 468 | } |
| 469 | |
| 470 | |
| 471 | /** |
| 472 | * Gets the product associated with the record. |
| 473 | * |
| 474 | * @since 2.0.0 |
| 475 | * |
| 476 | * @return null|\WC_Product |
| 477 | */ |
| 478 | public function get_product() { |
| 479 | |
| 480 | $product = wc_get_product( $this->get_product_id() ); |
| 481 | |
| 482 | return $product instanceof \WC_Product ? $product : null; |
| 483 | } |
| 484 | |
| 485 | |
| 486 | /** |
| 487 | * Gets the product ID associated with the record. |
| 488 | * |
| 489 | * @since 2.0.0 |
| 490 | * |
| 491 | * @return int |
| 492 | */ |
| 493 | public function get_product_id() { |
| 494 | |
| 495 | return absint( $this->product_id ); |
| 496 | } |
| 497 | |
| 498 | |
| 499 | /** |
| 500 | * Sets a flag whether when adding the record the product was contextually hidden from catalog. |
| 501 | * |
| 502 | * 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. |
| 503 | * |
| 504 | * @since 2.0.0 |
| 505 | * |
| 506 | * @param bool $was_hidden whether product was hidden when creating the record |
| 507 | * @return bool success |
| 508 | */ |
| 509 | public function set_product_hidden( $was_hidden = true ) { |
| 510 | |
| 511 | $set = false; |
| 512 | |
| 513 | if ( is_bool( $was_hidden ) ) { |
| 514 | $this->product_hidden = $was_hidden; |
| 515 | $set = true; |
| 516 | } |
| 517 | |
| 518 | return $set; |
| 519 | } |
| 520 | |
| 521 | |
| 522 | /** |
| 523 | * Checks whether a flag was set for hiding the product from catalog when the record was created. |
| 524 | * |
| 525 | * This may not reflect the actual product visibility status, it is only for historical purposes. |
| 526 | * |
| 527 | * @since 2.0.0 |
| 528 | * |
| 529 | * @return bool |
| 530 | */ |
| 531 | public function was_product_hidden() { |
| 532 | |
| 533 | return true === $this->product_hidden; |
| 534 | } |
| 535 | |
| 536 | |
| 537 | /** |
| 538 | * Gets the record's actions. |
| 539 | * |
| 540 | * @since 2.0.0 |
| 541 | * |
| 542 | * @return \stdClass[] associative array of action names and action properties as objects |
| 543 | */ |
| 544 | public function get_actions() { |
| 545 | |
| 546 | $actions = array(); |
| 547 | |
| 548 | if ( ! $this->is_resolved() ) { |
| 549 | |
| 550 | $actions['delete'] = (object) array( |
| 551 | 'name' => 'delete', |
| 552 | 'label' => __( 'Delete', 'woocommerce-square' ), |
| 553 | 'icon' => '<span class="dashicons dashicons-trash"></span>', |
| 554 | ); |
| 555 | |
| 556 | if ( ! $this->is_type( 'info' ) ) { |
| 557 | |
| 558 | $actions['resolve'] = (object) array( |
| 559 | 'name' => 'resolve', |
| 560 | 'label' => __( 'Ignore', 'woocommerce-square' ), |
| 561 | 'icon' => '<span class="dashicons dashicons-hidden"></span>', |
| 562 | ); |
| 563 | } |
| 564 | |
| 565 | if ( $this->has_product() ) { |
| 566 | |
| 567 | $actions['unsync'] = (object) array( |
| 568 | 'name' => 'unsync', |
| 569 | 'label' => __( 'Unlink', 'woocommerce-square' ), |
| 570 | 'icon' => '<span class="dashicons dashicons-editor-unlink"></span>', |
| 571 | ); |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | /** |
| 576 | * Filters the sync record action. |
| 577 | * |
| 578 | * @since 2.0.0 |
| 579 | * |
| 580 | * @param \stdClass[] array of action names and objects |
| 581 | * @param Record instance of the current record object |
| 582 | */ |
| 583 | return (array) apply_filters( 'wc_square_sync_record_actions', $actions, $this ); |
| 584 | } |
| 585 | |
| 586 | |
| 587 | /** |
| 588 | * Saves the record to storage. |
| 589 | * |
| 590 | * @since 2.0.0 |
| 591 | * |
| 592 | * @return bool success |
| 593 | */ |
| 594 | public function save() { |
| 595 | |
| 596 | return Records::set_record( $this ); |
| 597 | } |
| 598 | |
| 599 | |
| 600 | /** |
| 601 | * Deletes the record from storage. |
| 602 | * |
| 603 | * @since 2.0.0 |
| 604 | * |
| 605 | * @return bool success |
| 606 | */ |
| 607 | public function destroy() { |
| 608 | |
| 609 | return Records::delete_record( $this->get_id() ); |
| 610 | } |
| 611 | |
| 612 | |
| 613 | } |
| 614 |