| 1 |
<?php |
| 2 |
/** |
| 3 |
* Resolves a `required_pro_templately` import failure into a purchase |
| 4 |
* reference (FR-010), reusing the existing `Templately\API\Checkout` logic |
| 5 |
* — the same checkout flow the browser dashboard already uses. |
| 6 |
* |
| 7 |
* @package Templately\Modules\McpCore\Support |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace Templately\Modules\McpCore\Support; |
| 11 |
|
| 12 |
use WP_Error; |
| 13 |
|
| 14 |
class ProGateResolver { |
| 15 |
|
| 16 |
/** |
| 17 |
* If $error is the Pro-entitlement gate, re-wrap it with a live checkout |
| 18 |
* URL attached. Any other error is passed through unchanged. |
| 19 |
* |
| 20 |
* @param WP_Error $error |
| 21 |
* @param int $template_id |
| 22 |
* @return WP_Error |
| 23 |
*/ |
| 24 |
public static function to_purchase_error( WP_Error $error, int $template_id ): WP_Error { |
| 25 |
if ( $error->get_error_code() !== 'required_pro_templately' ) { |
| 26 |
return $error; |
| 27 |
} |
| 28 |
|
| 29 |
return new WP_Error( |
| 30 |
'required_pro_templately', |
| 31 |
$error->get_error_message(), |
| 32 |
[ 'purchase' => [ 'checkout_url' => self::resolve_checkout_url( $template_id ) ] ] |
| 33 |
); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* @param int $template_id |
| 38 |
* @return string|null |
| 39 |
*/ |
| 40 |
private static function resolve_checkout_url( int $template_id ) { |
| 41 |
$response = RestDispatcher::dispatch( 'POST', '/templately/v1/checkout', [ |
| 42 |
'purchase_type' => 'template', |
| 43 |
'id' => $template_id, |
| 44 |
] ); |
| 45 |
|
| 46 |
if ( is_wp_error( $response ) ) { |
| 47 |
return null; |
| 48 |
} |
| 49 |
|
| 50 |
$data = $response->get_data(); |
| 51 |
|
| 52 |
return $data['url'] ?? null; |
| 53 |
} |
| 54 |
} |
| 55 |
|