notifications->send( $notification, $subscriber_token ); } catch ( NotifyPressException $exception ) { return self::fail( $exception->getMessage() ); } log( 'Notification sent', array( 'message' => $result->message ) ); return true; } /** * Build the SDK payload from the plugin's loosely-shaped payload array. * * @param array $payload The notification payload. * * @return SendNotification */ private static function to_notification( array $payload ): SendNotification { $notification = SendNotification::make( sanitize_text_field( $payload['title'] ?? '' ), sanitize_text_field( $payload['message'] ?? '' ) ); // 'url' is the older name for the click-through target; both are still // accepted so an existing integration keeps working. $target_url = $payload['target_url'] ?? $payload['url'] ?? ''; if ( ! empty( $target_url ) ) { $notification = $notification->targetUrl( esc_url_raw( $target_url ) ); } if ( ! empty( $payload['icon'] ) ) { $notification = $notification->iconUrl( esc_url_raw( $payload['icon'] ) ); } return $notification; } /** * Record why a send failed, and report it as a failure. * * @param string $error The failure reason. * * @return false */ private static function fail( string $error ): bool { self::$last_error = $error; // The SDK keeps credentials out of exception messages, so this is safe // to log — unlike the token and endpoint this used to write out. log( 'Notification send failed', array( 'error' => $error ) ); return false; } }