WpcomNotificationDispatcher.php
172 lines
| 1 | <?php |
| 2 | |
| 3 | declare( strict_types = 1 ); |
| 4 | |
| 5 | namespace Automattic\WooCommerce\Internal\PushNotifications\Dispatchers; |
| 6 | |
| 7 | defined( 'ABSPATH' ) || exit; |
| 8 | |
| 9 | use Automattic\Jetpack\Connection\Client as Jetpack_Connection_Client; |
| 10 | use Automattic\WooCommerce\Internal\PushNotifications\Entities\PushToken; |
| 11 | use Automattic\WooCommerce\Internal\PushNotifications\Notifications\Notification; |
| 12 | use Automattic\WooCommerce\Internal\PushNotifications\PushNotifications; |
| 13 | use Jetpack_Options; |
| 14 | use WP_Error; |
| 15 | use WP_Http; |
| 16 | |
| 17 | /** |
| 18 | * Sends a notification to WPCOM via the Jetpack connection. |
| 19 | * |
| 20 | * Called directly by the NotificationProcessor. Combines the notification |
| 21 | * payload with formatted push tokens and sends to the WPCOM push endpoint. |
| 22 | * Returns a result array indicating success/failure and an optional retry-after |
| 23 | * value. |
| 24 | * |
| 25 | * @internal |
| 26 | * @since 10.7.0 |
| 27 | */ |
| 28 | class WpcomNotificationDispatcher { |
| 29 | |
| 30 | /** |
| 31 | * WPCOM API version. |
| 32 | */ |
| 33 | const WPCOM_API_VERSION = '2'; |
| 34 | |
| 35 | /** |
| 36 | * WPCOM endpoint path (appended after /sites/{id}/). |
| 37 | */ |
| 38 | const SEND_ENDPOINT = 'push-notifications'; |
| 39 | |
| 40 | /** |
| 41 | * HTTP request timeout in seconds. |
| 42 | */ |
| 43 | const REQUEST_TIMEOUT = 15; |
| 44 | |
| 45 | /** |
| 46 | * Dispatches a notification with push tokens to WPCOM. |
| 47 | * |
| 48 | * @param Notification $notification The notification to send. |
| 49 | * @param PushToken[] $tokens The push tokens to send to. |
| 50 | * @return array{success: bool, retry_after: int|null} |
| 51 | * |
| 52 | * @since 10.7.0 |
| 53 | */ |
| 54 | public function dispatch( Notification $notification, array $tokens ): array { |
| 55 | $site_id = class_exists( Jetpack_Options::class ) ? Jetpack_Options::get_option( 'id' ) : null; |
| 56 | |
| 57 | if ( empty( $site_id ) ) { |
| 58 | wc_get_logger()->error( |
| 59 | 'Cannot send push notifications: Jetpack site ID unavailable.', |
| 60 | array( 'source' => PushNotifications::FEATURE_NAME ) |
| 61 | ); |
| 62 | |
| 63 | return array( |
| 64 | 'success' => false, |
| 65 | 'retry_after' => null, |
| 66 | ); |
| 67 | } |
| 68 | |
| 69 | $payload = $notification->to_payload(); |
| 70 | |
| 71 | if ( null === $payload ) { |
| 72 | wc_get_logger()->error( |
| 73 | sprintf( |
| 74 | 'Cannot send push notification: resource no longer exists (type=%s, resource_id=%d).', |
| 75 | $notification->get_type(), |
| 76 | $notification->get_resource_id() |
| 77 | ), |
| 78 | array( 'source' => PushNotifications::FEATURE_NAME ) |
| 79 | ); |
| 80 | |
| 81 | return array( |
| 82 | 'success' => false, |
| 83 | 'retry_after' => null, |
| 84 | ); |
| 85 | } |
| 86 | |
| 87 | $response = $this->make_request( $site_id, $payload, $tokens ); |
| 88 | |
| 89 | if ( is_wp_error( $response ) ) { |
| 90 | wc_get_logger()->error( |
| 91 | sprintf( |
| 92 | 'Push notification request failed: %s', |
| 93 | $response->get_error_message() |
| 94 | ), |
| 95 | array( 'source' => PushNotifications::FEATURE_NAME ) |
| 96 | ); |
| 97 | |
| 98 | return array( |
| 99 | 'success' => false, |
| 100 | 'retry_after' => null, |
| 101 | ); |
| 102 | } |
| 103 | |
| 104 | $status_code = (int) wp_remote_retrieve_response_code( $response ); |
| 105 | |
| 106 | if ( WP_Http::OK === $status_code ) { |
| 107 | return array( |
| 108 | 'success' => true, |
| 109 | 'retry_after' => null, |
| 110 | ); |
| 111 | } |
| 112 | |
| 113 | $retry_after = wp_remote_retrieve_header( $response, 'retry-after' ); |
| 114 | |
| 115 | wc_get_logger()->error( |
| 116 | sprintf( |
| 117 | 'Push notification request returned HTTP %d.', |
| 118 | $status_code |
| 119 | ), |
| 120 | array( 'source' => PushNotifications::FEATURE_NAME ) |
| 121 | ); |
| 122 | |
| 123 | return array( |
| 124 | 'success' => false, |
| 125 | 'retry_after' => '' !== $retry_after ? (int) $retry_after : null, |
| 126 | ); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Makes the WPCOM API request via the Jetpack connection. |
| 131 | * |
| 132 | * @param int $site_id The Jetpack site ID. |
| 133 | * @param array $payload The notification payload. |
| 134 | * @param PushToken[] $tokens The push tokens. |
| 135 | * @return array|WP_Error |
| 136 | * |
| 137 | * @since 10.7.0 |
| 138 | * |
| 139 | * @phpstan-ignore return.unusedType (Jetpack stubs lack array return type.) |
| 140 | */ |
| 141 | private function make_request( int $site_id, array $payload, array $tokens ) { |
| 142 | $body = wp_json_encode( |
| 143 | array_merge( |
| 144 | $payload, |
| 145 | array( |
| 146 | 'tokens' => array_map( |
| 147 | fn ( PushToken $token ) => $token->to_wpcom_format(), |
| 148 | $tokens |
| 149 | ), |
| 150 | ) |
| 151 | ) |
| 152 | ); |
| 153 | |
| 154 | if ( false === $body ) { |
| 155 | return new WP_Error( 'json_encode_failed', 'Failed to encode push notification payload.' ); |
| 156 | } |
| 157 | |
| 158 | // @phpstan-ignore return.type |
| 159 | return Jetpack_Connection_Client::wpcom_json_api_request_as_blog( |
| 160 | sprintf( '/sites/%d/%s', $site_id, self::SEND_ENDPOINT ), |
| 161 | self::WPCOM_API_VERSION, |
| 162 | array( |
| 163 | 'headers' => array( 'Content-Type' => 'application/json' ), |
| 164 | 'method' => 'POST', |
| 165 | 'timeout' => self::REQUEST_TIMEOUT, |
| 166 | ), |
| 167 | $body, |
| 168 | 'wpcom' |
| 169 | ); |
| 170 | } |
| 171 | } |
| 172 |