false, 'temporary' => $temporary, 'error' => $error ]; } private static function request( $action, $key, $site ) { if ( ! self::valid_key_format( $key ) || '' === self::site_hash( $site ) ) { return self::failure( __( 'Enter a valid ChargeForms license key and site URL.', 'chargeforms' ) ); } $response = wp_remote_post( self::API_BASE . '/' . $action, [ 'timeout' => 10, 'headers' => [ 'Content-Type' => 'application/json' ], 'body' => wp_json_encode( [ 'license_key' => strtoupper( trim( $key ) ), 'site_url' => $site ] ), ] ); if ( is_wp_error( $response ) ) { return self::failure( __( 'License service is temporarily unreachable. Verification will retry.', 'chargeforms' ), true ); } $code = wp_remote_retrieve_response_code( $response ); $body = json_decode( wp_remote_retrieve_body( $response ), true ); if ( 429 === $code || $code >= 500 ) { return self::failure( is_array( $body ) && ! empty( $body['error'] ) ? (string) $body['error'] : __( 'License service is temporarily unavailable.', 'chargeforms' ), true ); } if ( ! is_array( $body ) || $code < 200 || $code >= 300 || true !== ( $body['success'] ?? false ) ) { return self::failure( is_array( $body ) && ! empty( $body['error'] ) ? (string) $body['error'] : __( 'This license is not valid on this site.', 'chargeforms' ) ); } if ( 'deactivate' === $action ) { return true === ( $body['deactivated'] ?? false ) ? [ 'success' => true ] : self::failure( __( 'Site deactivation was not confirmed.', 'chargeforms' ), true ); } if ( false === ( $body['valid'] ?? null ) ) { return self::failure( __( 'This license is not valid on this site.', 'chargeforms' ) ); } $plan = $body['plan'] ?? ''; $limits = [ 'single' => 1, 'agency' => 5, 'unlimited' => null ]; if ( ! is_string( $plan ) || ! array_key_exists( $plan, $limits ) || ! array_key_exists( 'activation_limit', $body ) || $limits[ $plan ] !== $body['activation_limit'] ) { return self::failure( __( 'The license service returned incomplete plan information.', 'chargeforms' ), true ); } if ( true !== ( $body['valid'] ?? false ) || 'active' !== ( $body['status'] ?? '' ) || true !== ( $body['activated_on_this_site'] ?? false ) || self::site_hash( $site ) !== ( $body['site_hash'] ?? '' ) ) { return self::failure( __( 'This license is not valid on this site.', 'chargeforms' ) ); } if ( ! array_key_exists( 'expires_at', $body ) || ( null !== $body['expires_at'] && ( ! is_string( $body['expires_at'] ) || false === strtotime( $body['expires_at'] ) || strtotime( $body['expires_at'] ) <= time() ) ) ) { return self::failure( __( 'This license has expired or its expiry could not be verified.', 'chargeforms' ) ); } return [ 'success' => true, 'plan' => $plan, 'activation_limit' => $body['activation_limit'], 'site_hash' => $body['site_hash'], 'expires_at' => $body['expires_at'] ]; } }