views
5 months ago
class-wc-helper-admin.php
4 months ago
class-wc-helper-api.php
1 year ago
class-wc-helper-compat.php
5 years ago
class-wc-helper-options.php
3 years ago
class-wc-helper-orders-api.php
2 years ago
class-wc-helper-sanitization.php
1 year ago
class-wc-helper-subscriptions-api.php
4 months ago
class-wc-helper-updater.php
2 months ago
class-wc-helper.php
4 months ago
class-wc-plugin-api-updater.php
1 year ago
class-wc-product-usage-notice.php
1 year ago
class-wc-woo-helper-connection.php
4 months ago
class-wc-woo-update-manager-plugin.php
2 years ago
class-wc-helper-orders-api.php
108 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce Admin Helper - React admin interface |
| 4 | * |
| 5 | * @package WooCommerce\Admin\Helper |
| 6 | */ |
| 7 | |
| 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | exit; |
| 10 | } |
| 11 | |
| 12 | /** |
| 13 | * WC_Helper_Orders_API |
| 14 | * |
| 15 | * Pings WooCommerce.com to create an order and pull in the necessary data to start the installation process. |
| 16 | */ |
| 17 | class WC_Helper_Orders_API { |
| 18 | /** |
| 19 | * Loads the class, runs on init |
| 20 | * |
| 21 | * @return void |
| 22 | */ |
| 23 | public static function load() { |
| 24 | add_filter( 'rest_api_init', array( __CLASS__, 'register_rest_routes' ) ); |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Registers the REST routes for the Marketplace Orders API. |
| 29 | * These endpoints are used by the Marketplace Subscriptions React UI. |
| 30 | */ |
| 31 | public static function register_rest_routes() { |
| 32 | register_rest_route( |
| 33 | 'wc/v3', |
| 34 | '/marketplace/create-order', |
| 35 | array( |
| 36 | 'methods' => 'POST', |
| 37 | 'callback' => array( __CLASS__, 'create_order' ), |
| 38 | 'permission_callback' => array( __CLASS__, 'get_permission' ), |
| 39 | 'args' => array( |
| 40 | 'product_id' => array( |
| 41 | 'required' => true, |
| 42 | 'validate_callback' => function( $argument ) { |
| 43 | return is_int( $argument ); |
| 44 | }, |
| 45 | ), |
| 46 | ), |
| 47 | ) |
| 48 | ); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * The Extensions page can only be accessed by users with the manage_woocommerce |
| 53 | * capability. So the API mimics that behavior. |
| 54 | * |
| 55 | * @return bool |
| 56 | */ |
| 57 | public static function get_permission() { |
| 58 | return WC_Helper_Subscriptions_API::get_permission(); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Core function to create an order on WooCommerce.com. Pings the API and catches the exceptions if any. |
| 63 | * |
| 64 | * @param WP_REST_Request $request Request object. |
| 65 | * |
| 66 | * @return WP_REST_Response |
| 67 | */ |
| 68 | public static function create_order( $request ) { |
| 69 | if ( ! current_user_can( 'install_plugins' ) ) { |
| 70 | return new \WP_REST_Response( |
| 71 | array( |
| 72 | 'message' => __( 'You do not have permission to install plugins.', 'woocommerce' ), |
| 73 | ), |
| 74 | 403 |
| 75 | ); |
| 76 | } |
| 77 | |
| 78 | try { |
| 79 | $response = WC_Helper_API::post( |
| 80 | 'create-order', |
| 81 | array( |
| 82 | 'authenticated' => true, |
| 83 | 'body' => http_build_query( |
| 84 | array( |
| 85 | 'product_id' => $request['product_id'], |
| 86 | ), |
| 87 | ), |
| 88 | ) |
| 89 | ); |
| 90 | |
| 91 | return new \WP_REST_Response( |
| 92 | json_decode( wp_remote_retrieve_body( $response ), true ), |
| 93 | wp_remote_retrieve_response_code( $response ) |
| 94 | ); |
| 95 | } catch ( Exception $e ) { |
| 96 | return new \WP_REST_Response( |
| 97 | array( |
| 98 | 'message' => __( 'Could not start the installation process. Reason: ', 'woocommerce' ) . $e->getMessage(), |
| 99 | 'code' => 'could-not-install', |
| 100 | ), |
| 101 | 500 |
| 102 | ); |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | WC_Helper_Orders_API::load(); |
| 108 |