payload = $payload; $this->error = $error; $this->status = (int) $status; } /** * @param mixed $payload * @param int $status * @return RemoteResponse */ public static function success( $payload, $status = 200 ) { return new self( $payload, null, $status ); } /** * @param TemplatelyError $error * @param int $status Upstream HTTP status; defaults to the error's own. * @return RemoteResponse */ public static function failure( TemplatelyError $error, $status = null ) { return new self( null, $error, null === $status ? $error->status() : $status ); } /** * @return bool */ public function is_error() { return $this->error instanceof TemplatelyError; } /** * The successful payload, or `null` when this is an error. * * @return mixed */ public function payload() { return $this->is_error() ? null : $this->payload; } /** * @return TemplatelyError|null */ public function error() { return $this->error; } /** * @return int upstream HTTP status. */ public function status() { return $this->status; } /** * Back-compat bridge for call sites that still expect * `array|WP_Error` from the old helpers. New code should branch on * `is_error()` instead. * * @return mixed|TemplatelyError */ public function unwrap() { return $this->is_error() ? $this->error : $this->payload; } }