NewOrderNotification.php
4 weeks ago
NewReviewNotification.php
4 weeks ago
Notification.php
4 weeks ago
StockNotification.php
4 weeks ago
NewReviewNotification.php
136 lines
| 1 | <?php |
| 2 | |
| 3 | declare( strict_types = 1 ); |
| 4 | |
| 5 | namespace Automattic\WooCommerce\Internal\PushNotifications\Notifications; |
| 6 | |
| 7 | use WP_Comment; |
| 8 | |
| 9 | defined( 'ABSPATH' ) || exit; |
| 10 | |
| 11 | /** |
| 12 | * Notification for new product reviews. |
| 13 | * |
| 14 | * @since 10.7.0 |
| 15 | */ |
| 16 | class NewReviewNotification extends Notification { |
| 17 | /** |
| 18 | * The notification type identifier for new reviews. |
| 19 | */ |
| 20 | const TYPE = 'store_review'; |
| 21 | |
| 22 | /** |
| 23 | * {@inheritDoc} |
| 24 | */ |
| 25 | public function get_type(): string { |
| 26 | return self::TYPE; |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * {@inheritDoc} |
| 31 | * |
| 32 | * Extends the base enabled-toggle check with a maximum-rating threshold. |
| 33 | * When `max_rating` is set in the user's preferences, reviews rated above |
| 34 | * the threshold do not trigger a notification. |
| 35 | * |
| 36 | * @param mixed $pref_value The user's stored preference value, or null. |
| 37 | * @return bool |
| 38 | * |
| 39 | * @since 10.9.0 |
| 40 | */ |
| 41 | public function should_send_to_user( $pref_value ): bool { |
| 42 | if ( ! parent::should_send_to_user( $pref_value ) ) { |
| 43 | return false; |
| 44 | } |
| 45 | |
| 46 | if ( ! is_array( $pref_value ) || ! isset( $pref_value['max_rating'] ) ) { |
| 47 | return true; |
| 48 | } |
| 49 | |
| 50 | $comment = WC()->call_function( 'get_comment', $this->get_resource_id() ); |
| 51 | if ( ! $comment instanceof WP_Comment ) { |
| 52 | return false; |
| 53 | } |
| 54 | |
| 55 | $rating = WC()->call_function( 'get_comment_meta', $this->get_resource_id(), 'rating', true ); |
| 56 | |
| 57 | if ( '' === $rating ) { |
| 58 | return true; |
| 59 | } |
| 60 | |
| 61 | return (int) $rating <= (int) $pref_value['max_rating']; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Returns the WPCOM-ready payload for this notification. |
| 66 | * |
| 67 | * Returns null if the comment no longer exists. |
| 68 | * |
| 69 | * @return array|null |
| 70 | * |
| 71 | * @since 10.7.0 |
| 72 | */ |
| 73 | public function to_payload(): ?array { |
| 74 | $comment = WC()->call_function( 'get_comment', $this->get_resource_id() ); |
| 75 | |
| 76 | if ( ! $comment || ! $comment instanceof WP_Comment ) { |
| 77 | return null; |
| 78 | } |
| 79 | |
| 80 | return array( |
| 81 | 'type' => $this->get_type(), |
| 82 | // This represents the time the notification was triggered, so we can monitor age of notification at delivery. |
| 83 | 'timestamp' => gmdate( 'c' ), |
| 84 | 'resource_id' => $this->get_resource_id(), |
| 85 | 'title' => array( |
| 86 | /** |
| 87 | * This will be translated in WordPress.com, format: |
| 88 | * 1: reviewer name, 2: product name |
| 89 | */ |
| 90 | 'format' => '%1$s left a review on %2$s', |
| 91 | 'args' => array( |
| 92 | wp_strip_all_tags( $comment->comment_author ), |
| 93 | wp_strip_all_tags( get_the_title( (int) $comment->comment_post_ID ) ), |
| 94 | ), |
| 95 | ), |
| 96 | 'message' => array( |
| 97 | 'format' => '%1$s', |
| 98 | 'args' => array( |
| 99 | wp_strip_all_tags( $comment->comment_content ), |
| 100 | ), |
| 101 | ), |
| 102 | 'icon' => get_avatar_url( $comment->comment_author_email ), |
| 103 | 'meta' => array( |
| 104 | 'comment_id' => $this->get_resource_id(), |
| 105 | ), |
| 106 | ); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * {@inheritDoc} |
| 111 | * |
| 112 | * @param string $key The meta key. |
| 113 | */ |
| 114 | public function has_meta( string $key ): bool { |
| 115 | return WC()->call_function( 'metadata_exists', 'comment', $this->get_resource_id(), $key ); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * {@inheritDoc} |
| 120 | * |
| 121 | * @param string $key The meta key. |
| 122 | */ |
| 123 | public function write_meta( string $key ): void { |
| 124 | WC()->call_function( 'update_comment_meta', $this->get_resource_id(), $key, (string) time() ); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * {@inheritDoc} |
| 129 | * |
| 130 | * @param string $key The meta key. |
| 131 | */ |
| 132 | public function delete_meta( string $key ): void { |
| 133 | WC()->call_function( 'delete_comment_meta', $this->get_resource_id(), $key ); |
| 134 | } |
| 135 | } |
| 136 |