← Back
Editing: class-licensing-failure.php
<?php namespace Uncanny_Automator; if ( ! defined( 'ABSPATH' ) ) { exit; } /** * A licensing request that was not answered with a 200. * * The redirect to the License page carries only a short code and the HTTP * status, never the response body: it can be a whole error page holding the * server's IP address. The body goes to the licensing log. * * The one body shown to the owner is a 409 refusal, a short sentence such as * "Your license key has reached its activation limit.". It is held in a * transient between the redirect and the License page. * * The code travels as `licensing_error`, not `error_message`: Pro prints * `error_message` as it is, so a code there would reach the owner. * * @since 8.1.2 */ class Licensing_Failure { const UNAVAILABLE = 'licensing_unavailable'; const BLOCKED = 'licensing_blocked'; const REFUSED = 'licensing_refused'; /** * The longest response body read as a refusal sentence. */ const REASON_LIMIT = 200; /** * @var string One of the codes above. */ private $code; /** * @var int HTTP status, 0 when no response arrived. */ private $status; /** * @var string The response body, or the transport error. */ private $detail; /** * @param array|\WP_Error $response The result of the licensing request. */ public function __construct( $response ) { if ( is_wp_error( $response ) ) { $this->code = self::UNAVAILABLE; $this->status = 0; $this->detail = $response->get_error_message(); return; } $this->status = (int) wp_remote_retrieve_response_code( $response ); $this->detail = (string) wp_remote_retrieve_body( $response ); $codes = array( 403 => self::BLOCKED, 406 => self::BLOCKED, 409 => self::REFUSED, ); $this->code = isset( $codes[ $this->status ] ) ? $codes[ $this->status ] : self::UNAVAILABLE; } /** * The query arguments for the License page. * * @return array */ public function query_args() { return array( 'sl_activation' => 'false', 'licensing_error' => $this->code, 'licensing_status' => $this->status, ); } /** * The sentence for the owner. * * @return string */ public function message() { $reason = $this->reason(); return '' !== $reason ? $reason : self::sentence( $this->code ); } /** * Write the whole response to the licensing log, without the license key. * * @param string $endpoint The licensing endpoint called. * @param string $license_key The license key sent. * * @return void */ public function log( $endpoint, $license_key ) { automator_log( str_replace( $license_key, '[license key]', $this->detail ), sprintf( 'Licensing request "%s" failed with HTTP status %d', $endpoint, $this->status ), true, 'licensing' ); } /** * Keep the refusal sentence for the License page the owner is sent to. * * @return void */ public function remember_reason() { $reason = $this->reason(); if ( '' === $reason ) { delete_transient( self::reason_key() ); return; } set_transient( self::reason_key(), $reason, 5 * MINUTE_IN_SECONDS ); } /** * The sentence the License page shows for a code from its address. * * @param string $code The `licensing_error` query argument. * * @return string Empty when there is no code. */ public static function notice_for( $code ) { if ( '' === $code ) { return ''; } if ( self::REFUSED === $code ) { $reason = (string) get_transient( self::reason_key() ); if ( '' !== $reason ) { return $reason; } } return self::sentence( $code ); } /** * The refusal sentence the server sent, if the body is one. * * @return string Empty for any other response, a page of markup or a long dump. */ private function reason() { if ( self::REFUSED !== $this->code || strlen( $this->detail ) > self::REASON_LIMIT || false !== strpos( $this->detail, '<' ) ) { return ''; } return sanitize_text_field( $this->detail ); } /** * The plugin's own sentence for a code. An unknown code reads as unavailable. * * @param string $code One of the codes above. * * @return string */ private static function sentence( $code ) { if ( self::BLOCKED === $code ) { return esc_html_x( 'The request to the licensing server was blocked. Please contact support.', 'licensing error', 'uncanny-automator' ); } if ( self::REFUSED === $code ) { return esc_html_x( 'The licensing server refused the request. Please check your license and try again.', 'licensing error', 'uncanny-automator' ); } return esc_html_x( 'The licensing server did not answer. Try again in a few minutes.', 'licensing error', 'uncanny-automator' ); } /** * One refusal per user, so one administrator never reads another's. * * @return string */ private static function reason_key() { return 'automator_licensing_refusal_' . get_current_user_id(); } }
Save File
Cancel