*/ final class PushCapabilityReporter { /** * Base-uri key of the Actirise\Includes\Api client used for the call. * * The endpoint is reached through the WordPress API gateway base * (ACTIRISE_URL_API_V2), the same base used for * `authentication_token` and `websites/{uuid}/contract`. * * @since 4.3.0 * @var string */ const ENDPOINT_KEY = 'api'; /** * Collection segment of the capability endpoint, relative to the API base. * * @since 4.3.0 * @var string */ const RESOURCE = 'websites'; /** * Sub-resource segment carrying the push capability of a website. * * @since 4.3.0 * @var string */ const CAPABILITY = 'wordpress_push_capability'; /** * Body field carrying the push base URL. * * @since 4.3.0 * @var string */ const FIELD_REST_URL = 'restUrl'; /** * Body field carrying the REST-enabled boolean. * * @since 4.3.0 * @var string */ const FIELD_REST_API_ENABLED = 'restApiEnabled'; /** * Body field carrying the push events this plugin serves. * * @since 4.4.0 * @var string */ const FIELD_SUPPORTED_EVENTS = 'supportedEvents'; /** * Logger context (file) used for reporter log entries. * * @since 4.3.0 * @var string */ const LOG_CONTEXT = 'include/push'; /** * The Actirise API client. * * @since 4.3.0 * @var Api */ private $api; /** * Initialize the reporter. * * @since 4.3.0 */ public function __construct() { $this->api = new Api(); } /** * Report the current REST availability, push URL and served events. * * The events ride along on every report (the hourly self-test, the * activation, a uuid change), because every report replaces the previous * claim and an absent field would clear it (contract § 6) — an updated * plugin that stopped sending the list would silently stop receiving the * events the claim gates. * * @since 4.3.0 * @since 4.4.0 Also claims the served push events (`supportedEvents`). * @param bool $rest_api_enabled Whether the plugin's REST API is reachable. * @param string $rest_url Full base push URL (rest_url('actirise/v1/push')). * @return bool True when the capability was persisted API-side. */ public function report( $rest_api_enabled, $rest_url ) { return $this->patch_capability( array( self::FIELD_REST_URL => (string) $rest_url, self::FIELD_REST_API_ENABLED => (bool) $rest_api_enabled, self::FIELD_SUPPORTED_EVENTS => self::supported_events(), ) ); } /** * The push events this plugin serves, read from the handler registry. * * Never hard-coded: the routes and the claim must come from the same list, * because a claimed event whose endpoint answers 404 suspends every * delivery to the site (contract § 5.3). Registering a handler in * {@see PushController::build_handlers()} extends the claim by itself. * * @since 4.4.0 * @return array */ private static function supported_events() { $events = array(); foreach ( PushController::build_handlers() as $handler ) { $events[] = $handler->event_type(); } return $events; } /** * Report that the plugin must no longer receive pushes (e.g. on * deactivation): sets `restApiEnabled` to false. As a merge-patch, the * stored `restUrl` is left untouched. * * `supportedEvents` is left out deliberately, which **clears the claim** * (contract § 6: every report replaces it, an absent field included). A * deactivated plugin serves no endpoint, so it must not keep claiming any * event; the reactivation report re-establishes the full list. * * @since 4.3.0 * @return bool True when the capability was persisted API-side. */ public function report_disabled() { return $this->patch_capability( array( self::FIELD_REST_API_ENABLED => false, ) ); } /** * PATCH the website's push capability with the given merge-patch body. * * @since 4.3.0 * @param array $body The merge-patch body to send. * @return bool True only when the token and website id are known and the * PATCH did not return a WP_Error. */ private function patch_capability( $body ) { $token = Options::get( 'settings-analytics-token' ); if ( ! is_string( $token ) || '' === $token ) { Logger::add_log( 'Push capability report skipped: no API token', self::LOG_CONTEXT, 'error' ); return false; } $website_uuid = Options::get( 'website-uuid' ); if ( ! is_scalar( $website_uuid ) || '' === (string) $website_uuid ) { Logger::add_log( 'Push capability report skipped: no website uuid', self::LOG_CONTEXT, 'error' ); return false; } $uri = self::RESOURCE . '/' . (string) $website_uuid . '/' . self::CAPABILITY; $response = $this->api->patch( self::ENDPOINT_KEY, $uri, $body, $token ); if ( is_wp_error( $response ) ) { Logger::add_log( 'Push capability report: PATCH failed', self::LOG_CONTEXT, 'error' ); return false; } return true; } }