updates = $updates; } /** * Register the public update-check route. * * @since 1.0.0 * * @return void */ public function registerRoutes(): void { register_rest_route( self::API_NAMESPACE, '/update-check', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'check' ), // Public because customers' sites call it; the license key is the credential. 'permission_callback' => '__return_true', 'args' => array( 'product_slug' => array( 'type' => 'string', 'required' => true, 'description' => __( 'The product\'s catalog slug, the same identifier the licensing endpoints match on.', 'releasecart' ), 'sanitize_callback' => 'sanitize_text_field', ), 'license_key' => array( 'type' => 'string', 'required' => true, 'description' => __( 'The license key credential.', 'releasecart' ), 'sanitize_callback' => 'sanitize_text_field', ), 'site_url' => array( 'type' => 'string', 'required' => false, 'description' => __( 'The requesting site\'s URL; without it no package URL can be issued.', 'releasecart' ), 'sanitize_callback' => 'sanitize_text_field', ), 'installed_version' => array( 'type' => 'string', 'required' => false, 'description' => __( 'The currently installed version, for the update_available verdict.', 'releasecart' ), 'sanitize_callback' => 'sanitize_text_field', ), ), ), ) ); } /** * Return update data and a package URL when allowed. * * @since 1.0.0 * * @param WP_REST_Request $request The incoming request. * @return WP_REST_Response|WP_Error The update-check payload, or an error envelope. */ public function check( WP_REST_Request $request ) { $productSlug = trim( (string) $request->get_param( 'product_slug' ) ); $licenseKey = trim( (string) $request->get_param( 'license_key' ) ); if ( '' === $productSlug || '' === $licenseKey ) { return $this->invalidParams( array( __( 'Both product_slug and license_key are required.', 'releasecart' ) ) ); } $siteUrl = trim( (string) $request->get_param( 'site_url' ) ); $installed = trim( (string) $request->get_param( 'installed_version' ) ); try { $payload = $this->updates->check( $productSlug, $licenseKey, '' !== $siteUrl ? $siteUrl : null, '' !== $installed ? $installed : null, null, $request->get_params() ); } catch ( ProductNotFoundException $e ) { // An unknown slug, or a draft or archived product without a valid key, // gets the generic 404. return $this->notFound(); } catch ( LicensePersistenceException | ProductPersistenceException | ReleasePersistenceException $e ) { // Let the client use its grace period. return $this->serverError(); } return new WP_REST_Response( $payload, 200 ); } }