MobileAppMagicLink.php
| 1 | <?php |
| 2 | /** |
| 3 | * REST API Data countries controller. |
| 4 | * |
| 5 | * Handles requests to the /mobile-app endpoint. |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\WooCommerce\Admin\API; |
| 9 | |
| 10 | defined( 'ABSPATH' ) || exit; |
| 11 | |
| 12 | use Automattic\Jetpack\Connection\Manager as Jetpack_Connection_Manager; |
| 13 | |
| 14 | /** |
| 15 | * REST API Data countries controller class. |
| 16 | * |
| 17 | * @internal |
| 18 | * @extends WC_REST_Data_Controller |
| 19 | */ |
| 20 | class MobileAppMagicLink extends \WC_REST_Data_Controller { |
| 21 | |
| 22 | /** |
| 23 | * Endpoint namespace. |
| 24 | * |
| 25 | * @var string |
| 26 | */ |
| 27 | protected $namespace = 'wc-admin'; |
| 28 | |
| 29 | /** |
| 30 | * Route base. |
| 31 | * |
| 32 | * @var string |
| 33 | */ |
| 34 | protected $rest_base = 'mobile-app'; |
| 35 | |
| 36 | /** |
| 37 | * Register routes. |
| 38 | * |
| 39 | * @since 7.0.0 |
| 40 | */ |
| 41 | public function register_routes() { |
| 42 | register_rest_route( |
| 43 | $this->namespace, |
| 44 | '/' . $this->rest_base . '/send-magic-link', |
| 45 | array( |
| 46 | array( |
| 47 | 'methods' => \WP_REST_Server::READABLE, |
| 48 | 'callback' => array( $this, 'send_magic_link' ), |
| 49 | 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
| 50 | ), |
| 51 | 'schema' => array( $this, 'get_public_item_schema' ), |
| 52 | ) |
| 53 | ); |
| 54 | |
| 55 | parent::register_routes(); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Sends request to generate magic link email. |
| 60 | * |
| 61 | * @return \WP_REST_Response|\WP_Error |
| 62 | */ |
| 63 | public function send_magic_link() { |
| 64 | // Attempt to get email from Jetpack. |
| 65 | if ( class_exists( Jetpack_Connection_Manager::class ) ) { |
| 66 | $jetpack_connection_manager = new Jetpack_Connection_Manager(); |
| 67 | if ( $jetpack_connection_manager->is_active() ) { |
| 68 | if ( class_exists( 'Jetpack_IXR_Client' ) ) { |
| 69 | $xml = new \Jetpack_IXR_Client( |
| 70 | array( |
| 71 | 'user_id' => get_current_user_id(), |
| 72 | ) |
| 73 | ); |
| 74 | |
| 75 | $xml->query( 'jetpack.sendMobileMagicLink', array( 'app' => 'woocommerce' ) ); |
| 76 | if ( $xml->isError() ) { |
| 77 | return new \WP_Error( |
| 78 | 'error_sending_mobile_magic_link', |
| 79 | sprintf( |
| 80 | '%s: %s', |
| 81 | $xml->getErrorCode(), |
| 82 | $xml->getErrorMessage() |
| 83 | ) |
| 84 | ); |
| 85 | } |
| 86 | |
| 87 | return rest_ensure_response( |
| 88 | array( |
| 89 | 'code' => 'success', |
| 90 | ) |
| 91 | ); |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | return new \WP_Error( 'jetpack_not_connected', __( 'Jetpack is not connected.', 'woocommerce' ) ); |
| 97 | } |
| 98 | } |
| 99 |