NewOrderNotificationTrigger.php
3 months ago
NewReviewNotificationTrigger.php
3 months ago
StockNotificationRecoveryHandler.php
1 month ago
StockNotificationTrigger.php
1 month ago
NewReviewNotificationTrigger.php
61 lines
| 1 | <?php |
| 2 | |
| 3 | declare( strict_types = 1 ); |
| 4 | |
| 5 | namespace Automattic\WooCommerce\Internal\PushNotifications\Triggers; |
| 6 | |
| 7 | use Automattic\WooCommerce\Internal\PushNotifications\Notifications\NewReviewNotification; |
| 8 | use Automattic\WooCommerce\Internal\PushNotifications\Services\PendingNotificationStore; |
| 9 | |
| 10 | defined( 'ABSPATH' ) || exit; |
| 11 | |
| 12 | /** |
| 13 | * Listens for new approved product reviews and feeds notifications into |
| 14 | * the PendingNotificationStore. |
| 15 | * |
| 16 | * @since 10.7.0 |
| 17 | */ |
| 18 | class NewReviewNotificationTrigger { |
| 19 | /** |
| 20 | * Registers WordPress hooks for review events. |
| 21 | * |
| 22 | * @return void |
| 23 | * |
| 24 | * @since 10.7.0 |
| 25 | */ |
| 26 | public function register(): void { |
| 27 | add_action( 'comment_post', array( $this, 'on_comment_post' ), 10, 3 ); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Handles the comment_post hook. |
| 32 | * |
| 33 | * Only creates a notification for non-spam reviews on products. |
| 34 | * |
| 35 | * @param int $comment_id The comment ID. |
| 36 | * @param int|string $comment_approved 1 if approved, 0 if not, 'spam' if spam. |
| 37 | * @param array $commentdata The comment data. |
| 38 | * @return void |
| 39 | * |
| 40 | * @since 10.7.0 |
| 41 | */ |
| 42 | public function on_comment_post( int $comment_id, $comment_approved, array $commentdata ): void { |
| 43 | if ( |
| 44 | 'spam' === $comment_approved |
| 45 | || 'review' !== ( $commentdata['comment_type'] ?? '' ) |
| 46 | ) { |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | $commented_on = get_post_type( (int) ( $commentdata['comment_post_ID'] ?? 0 ) ); |
| 51 | |
| 52 | if ( 'product' !== $commented_on ) { |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | wc_get_container()->get( PendingNotificationStore::class )->add( |
| 57 | new NewReviewNotification( $comment_id ) |
| 58 | ); |
| 59 | } |
| 60 | } |
| 61 |