class-license-exchange.php
2 days ago
class-license-package-installer.php
2 days ago
class-license-product-map.php
2 days ago
class-license-shop-client.php
2 days ago
class-license-site-activation.php
2 days ago
class-license.php
2 days ago
utils.php
2 days ago
class-license-exchange.php
151 lines
| 1 | <?php |
| 2 | /** |
| 3 | * HTTP client for license exchange API. |
| 4 | * |
| 5 | * @package AdvancedAds |
| 6 | * @since 2.0.9 |
| 7 | * @author Advanced Ads <info@wpadvancedads.com> |
| 8 | */ |
| 9 | |
| 10 | namespace AdvancedAds\License; |
| 11 | |
| 12 | use WP_Error; |
| 13 | |
| 14 | defined( 'ABSPATH' ) || exit; |
| 15 | |
| 16 | /** |
| 17 | * Exchange legacy keys or one-time tokens for rich license records. |
| 18 | */ |
| 19 | final class License_Exchange { |
| 20 | |
| 21 | /** |
| 22 | * POST legacy map; return rich list or WP_Error. |
| 23 | * |
| 24 | * @param array<string, string> $addon_id_to_key Legacy map (addon id => license key). |
| 25 | * @return array<int, array<string, mixed>>|\WP_Error |
| 26 | */ |
| 27 | public static function request( array $addon_id_to_key ) { |
| 28 | $license_key = is_array( $addon_id_to_key ) ? ( array_values( $addon_id_to_key )[0] ?? null ) : null; |
| 29 | |
| 30 | if ( null === $license_key || '' === trim( (string) $license_key ) ) { |
| 31 | return new WP_Error( |
| 32 | 'advanced_ads_license_exchange_empty', |
| 33 | __( 'No license keys to exchange.', 'advanced-ads' ) |
| 34 | ); |
| 35 | } |
| 36 | |
| 37 | License_Shop_Client::add_local_development_http_filter(); |
| 38 | |
| 39 | $response = wp_remote_post( |
| 40 | self::exchange_endpoint(), |
| 41 | License_Shop_Client::http_request_args( |
| 42 | [ |
| 43 | 'body' => wp_json_encode( |
| 44 | [ |
| 45 | 'site' => home_url(), |
| 46 | 'license' => $license_key, |
| 47 | ] |
| 48 | ), |
| 49 | ] |
| 50 | ) |
| 51 | ); |
| 52 | |
| 53 | License_Shop_Client::remove_local_development_http_filter(); |
| 54 | |
| 55 | return self::parse_exchange_response( $response ); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Exchange a one-time shop token for rich license rows. |
| 60 | * |
| 61 | * @param string $token One-time token from shop redirect. |
| 62 | * @return array<int, array<string, mixed>>|\WP_Error |
| 63 | */ |
| 64 | public static function request_by_token( string $token ) { |
| 65 | $token = sanitize_text_field( $token ); |
| 66 | if ( '' === $token ) { |
| 67 | return new WP_Error( |
| 68 | 'advanced_ads_license_exchange_token', |
| 69 | __( 'Missing exchange token.', 'advanced-ads' ) |
| 70 | ); |
| 71 | } |
| 72 | |
| 73 | License_Shop_Client::add_local_development_http_filter(); |
| 74 | |
| 75 | $response = wp_remote_post( |
| 76 | self::exchange_endpoint(), |
| 77 | License_Shop_Client::http_request_args( |
| 78 | [ |
| 79 | 'body' => wp_json_encode( |
| 80 | [ |
| 81 | 'token' => $token, |
| 82 | 'site' => site_url(), |
| 83 | ] |
| 84 | ), |
| 85 | ] |
| 86 | ) |
| 87 | ); |
| 88 | |
| 89 | License_Shop_Client::remove_local_development_http_filter(); |
| 90 | |
| 91 | return self::parse_exchange_response( $response ); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Activate a license on the shop for this site. |
| 96 | * |
| 97 | * @param string $license_key License key. |
| 98 | * @param int $license_id EDD SL license post ID. |
| 99 | * @return array<int, array<string, mixed>>|\WP_Error |
| 100 | */ |
| 101 | public static function request_activate( string $license_key, int $license_id = 0 ) { |
| 102 | return License::request_shop_activate( $license_key, $license_id ); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Shop REST exchange endpoint URL. |
| 107 | * |
| 108 | * @return string |
| 109 | */ |
| 110 | private static function exchange_endpoint(): string { |
| 111 | $base_url = defined( 'AA_SHOP_URL' ) ? AA_SHOP_URL : 'https://wpadvancedads.com'; |
| 112 | |
| 113 | return untrailingslashit( esc_url_raw( $base_url ) ) . '/wp-json/advanced-ads/v2/license/exchange'; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Decode exchange HTTP response into license rows or WP_Error. |
| 118 | * |
| 119 | * @param array|\WP_Error $response wp_remote_post result. |
| 120 | * @return array<int, array<string, mixed>>|\WP_Error |
| 121 | */ |
| 122 | private static function parse_exchange_response( $response ) { |
| 123 | if ( is_wp_error( $response ) ) { |
| 124 | return $response; |
| 125 | } |
| 126 | |
| 127 | $code = wp_remote_retrieve_response_code( $response ); |
| 128 | if ( 200 !== $code ) { |
| 129 | $body = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 130 | $message = is_array( $body ) ? (string) ( $body['message'] ?? '' ) : ''; |
| 131 | $api_code = is_array( $body ) ? (string) ( $body['code'] ?? '' ) : ''; |
| 132 | |
| 133 | return new WP_Error( |
| 134 | '' !== $api_code ? $api_code : 'advanced_ads_license_exchange_http', |
| 135 | '' !== $message ? $message : __( 'License exchange request failed.', 'advanced-ads' ), |
| 136 | [ 'status' => $code ] |
| 137 | ); |
| 138 | } |
| 139 | |
| 140 | $data = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 141 | if ( ! is_array( $data ) ) { |
| 142 | return new WP_Error( |
| 143 | 'advanced_ads_license_exchange_parse', |
| 144 | __( 'Invalid license exchange response.', 'advanced-ads' ) |
| 145 | ); |
| 146 | } |
| 147 | |
| 148 | return $data; |
| 149 | } |
| 150 | } |
| 151 |