• File: NewReviewNotification.php
  • Full Path: /home/slvgwrnz/public_html/wp-content/plugins/woocommerce/src/Internal/PushNotifications/Notifications/NewReviewNotification.php
  • Date Modified: 10/26/2026 10:38 AM
  • File size: 3.86 KB
  • MIME-type: text/x-php
  • Charset: utf-8
<?php

declare( strict_types = 1 );

namespace Automattic\WooCommerce\Internal\PushNotifications\Notifications;

use Automattic\WooCommerce\Internal\PushNotifications\Enums\SuppressionReason;
use WP_Comment;

defined( 'ABSPATH' ) || exit;

/**
 * Notification for new product reviews.
 *
 * @since 10.7.0
 */
class NewReviewNotification extends Notification {
	/**
	 * The notification type identifier for new reviews.
	 */
	const TYPE = 'store_review';

	/**
	 * {@inheritDoc}
	 */
	public function get_type(): string {
		return self::TYPE;
	}

	/**
	 * {@inheritDoc}
	 *
	 * Extends the base enabled-toggle check with a maximum-rating threshold.
	 * When `max_rating` is set in the user's preferences, reviews rated above
	 * the threshold do not trigger a notification.
	 *
	 * @param mixed $pref_value The user's stored preference value, or null.
	 * @return string|null One of the SuppressionReason constants, or null to send.
	 *
	 * @since 11.3.0
	 */
	public function get_suppression_reason( $pref_value ): ?string {
		$reason = parent::get_suppression_reason( $pref_value );

		if ( null !== $reason ) {
			return $reason;
		}

		if ( ! is_array( $pref_value ) || ! isset( $pref_value['max_rating'] ) ) {
			return null;
		}

		$comment = WC()->call_function( 'get_comment', $this->get_resource_id() );
		if ( ! $comment instanceof WP_Comment ) {
			return SuppressionReason::REVIEW_MISSING;
		}

		$rating = WC()->call_function( 'get_comment_meta', $this->get_resource_id(), 'rating', true );

		if ( '' === $rating ) {
			return null;
		}

		return (int) $rating <= (int) $pref_value['max_rating'] ? null : SuppressionReason::ABOVE_MAX_RATING;
	}

	/**
	 * Returns the WPCOM-ready payload for this notification.
	 *
	 * Returns null if the comment no longer exists.
	 *
	 * @return array|null
	 *
	 * @since 10.7.0
	 */
	public function to_payload(): ?array {
		$comment = WC()->call_function( 'get_comment', $this->get_resource_id() );

		if ( ! $comment || ! $comment instanceof WP_Comment ) {
			return null;
		}

		return array(
			'type'        => $this->get_type(),
			// This represents the time the notification was triggered, so we can monitor age of notification at delivery.
			'timestamp'   => $this->get_triggered_timestamp(),
			'resource_id' => $this->get_resource_id(),
			'title'       => array(
				/**
				 * This will be translated in WordPress.com, format:
				 * 1: reviewer name, 2: product name
				 */
				'format' => '%1$s left a review on %2$s',
				'args'   => array(
					wp_strip_all_tags( $comment->comment_author ),
					wp_strip_all_tags( get_the_title( (int) $comment->comment_post_ID ) ),
				),
			),
			'message'     => array(
				'format' => '%1$s',
				'args'   => array(
					wp_strip_all_tags( $comment->comment_content ),
				),
			),
			'icon'        => get_avatar_url( $comment->comment_author_email ),
			'meta'        => array(
				'comment_id' => $this->get_resource_id(),
			),
		);
	}

	/**
	 * {@inheritDoc}
	 *
	 * @param string $key The meta key.
	 */
	public function has_meta( string $key ): bool {
		return WC()->call_function( 'metadata_exists', 'comment', $this->get_resource_id(), $key );
	}

	/**
	 * {@inheritDoc}
	 *
	 * @param string $key The meta key.
	 */
	public function read_meta( string $key ): string {
		return (string) WC()->call_function( 'get_comment_meta', $this->get_resource_id(), $key, true );
	}

	/**
	 * {@inheritDoc}
	 *
	 * @param string   $key       The meta key.
	 * @param int|null $timestamp Unix timestamp to record. Defaults to the current time.
	 */
	public function write_meta( string $key, ?int $timestamp = null ): void {
		WC()->call_function( 'update_comment_meta', $this->get_resource_id(), $key, (string) ( $timestamp ?? time() ) );
	}

	/**
	 * {@inheritDoc}
	 *
	 * @param string $key The meta key.
	 */
	public function delete_meta( string $key ): void {
		WC()->call_function( 'delete_comment_meta', $this->get_resource_id(), $key );
	}
}