|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace BeansWoo\Server; |
| 4 | + |
| 5 | +use Beans\BeansError; |
| 6 | +use BeansWoo\Helper; |
| 7 | + |
| 8 | +class ConnectorRESTController extends \WP_REST_Controller |
| 9 | +{ |
| 10 | + // The `wc-` prefix helps to use the WooCommerce Rest authentication on our routes. |
| 11 | + protected $namespace = "wc-beans/v1"; |
| 12 | + protected $rest_base = 'connector'; |
| 13 | + |
| 14 | + public function register_routes() |
| 15 | + { |
| 16 | + register_rest_route( |
| 17 | + $this->namespace, |
| 18 | + '/' . $this->rest_base . '/current', |
| 19 | + array( |
| 20 | + array( |
| 21 | + 'methods' => \WP_REST_Server::READABLE, |
| 22 | + 'callback' => array($this, 'get_item'), |
| 23 | + 'permission_callback' => array($this, 'get_item_permissions_check'), |
| 24 | + 'args' => $this->get_args(\WP_REST_Server::READABLE) |
| 25 | + ), |
| 26 | + array( |
| 27 | + 'methods' => \WP_REST_Server::EDITABLE, |
| 28 | + 'callback' => array($this, 'update_item'), |
| 29 | + 'permission_callback' => array($this, 'update_item_permissions_check'), |
| 30 | + 'args' => $this->get_args(\WP_REST_Server::EDITABLE) |
| 31 | + ) |
| 32 | + ) |
| 33 | + ); |
| 34 | + |
| 35 | + register_rest_route( |
| 36 | + $this->namespace, |
| 37 | + '/' . $this->rest_base . '/install', |
| 38 | + array( |
| 39 | + array( |
| 40 | + 'methods' => \WP_REST_Server::CREATABLE, |
| 41 | + 'callback' => array($this, 'install'), |
| 42 | + 'permission_callback' => array($this, 'install_item_permissions_check'), |
| 43 | + 'args' => $this->get_args(\WP_REST_Server::CREATABLE) |
| 44 | + ), |
| 45 | + ) |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + public function get_item($request) |
| 50 | + { |
| 51 | + $response = new \WP_REST_Response(self::get_item_data()); |
| 52 | + $response->set_status(200); |
| 53 | + return $response; |
| 54 | + } |
| 55 | + public function install($request) |
| 56 | + { |
| 57 | + if (isset($request['card']) && isset($request['token'])) { |
| 58 | + $card_id = $request['card']; |
| 59 | + $token = $request['token']; |
| 60 | + // Using `Connector::processSetup()` doesn't work. I had an error about the |
| 61 | + // `Class BeansWoo\Admin\Connector` doesn't exist. I tried to investigate but I am not able to find out |
| 62 | + // what is the bug. |
| 63 | + // todo; Use `Connector::processSetup()` instead of duplicating the logic; |
| 64 | + Helper::$key = $card_id; |
| 65 | + |
| 66 | + try { |
| 67 | + $integration_key = Helper::API()->get('core/auth/integration_key/' . $token); |
| 68 | + } catch (BeansError $e) { |
| 69 | + Helper::log('Connecting failed: ' . $e->getMessage()); |
| 70 | + return new \WP_Error( |
| 71 | + "beans_rest_cannot_setup", |
| 72 | + __("Unable to setup Beans plugin", 'beans'), |
| 73 | + array('status' => 400) |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + Helper::setConfig('key', $integration_key['id']); |
| 78 | + Helper::setConfig('card', $integration_key['card']['id']); |
| 79 | + Helper::setConfig('secret', $integration_key['secret']); |
| 80 | + Helper::clearTransients(); |
| 81 | + } |
| 82 | + |
| 83 | + $response = new \WP_REST_Response(self::get_item_data()); |
| 84 | + $response->set_status(201); |
| 85 | + return $response; |
| 86 | + } |
| 87 | + |
| 88 | + public function update_item($request) |
| 89 | + { |
| 90 | + if (isset($request['riper_version'])) { |
| 91 | + Helper::setConfig('riper_version', $request['riper_version']); |
| 92 | + } |
| 93 | + |
| 94 | + $response = new \WP_REST_Response(self::get_item_data()); |
| 95 | + $response->set_status(202); |
| 96 | + return $response; |
| 97 | + } |
| 98 | + |
| 99 | + public function get_item_permissions_check($request) |
| 100 | + { |
| 101 | + return $this->check_permissions($request, 'view'); |
| 102 | + } |
| 103 | + |
| 104 | + public function update_item_permissions_check($request) |
| 105 | + { |
| 106 | + return $this->check_permissions($request, 'edit'); |
| 107 | + } |
| 108 | + |
| 109 | + public function install_item_permissions_check($request) |
| 110 | + { |
| 111 | + return $this->check_permissions($request, 'install'); |
| 112 | + } |
| 113 | + |
| 114 | + public function get_args($action) |
| 115 | + { |
| 116 | + if ($action === \WP_REST_Server::EDITABLE) { |
| 117 | + return array( |
| 118 | + 'riper_version' => array( |
| 119 | + 'required' => false, |
| 120 | + 'sanitize_callback' => array(__CLASS__, 'sanitize_value'), |
| 121 | + 'validate_callback' => function ($param, $request, $key) { |
| 122 | + return is_string($param) and in_array($param, array('lts', 'edge')); |
| 123 | + }, |
| 124 | + ), |
| 125 | + ); |
| 126 | + } elseif ($action == \WP_REST_Server::CREATABLE) { |
| 127 | + return array( |
| 128 | + 'token' => array( |
| 129 | + 'required' => true, |
| 130 | + 'sanitize_callback' => array(__CLASS__, 'sanitize_value'), |
| 131 | + 'validate_callback' => function ($param, $request, $key) { |
| 132 | + return is_string($param); |
| 133 | + }, |
| 134 | + ), |
| 135 | + 'card' => array( |
| 136 | + 'required' => true, |
| 137 | + 'sanitize_callback' => array(__CLASS__, 'sanitize_value'), |
| 138 | + 'validate_callback' => function ($param, $request, $key) { |
| 139 | + return is_string($param) and substr($param, 0, strlen($key)) === $key; |
| 140 | + }, |
| 141 | + ), |
| 142 | + ); |
| 143 | + } |
| 144 | + return array(); |
| 145 | + } |
| 146 | + |
| 147 | + public static function sanitize_value($param, $request, $key) |
| 148 | + { |
| 149 | + return htmlspecialchars($param); |
| 150 | + } |
| 151 | + |
| 152 | + private function check_permissions($request, $action) |
| 153 | + { |
| 154 | + if (!current_user_can('manage_options')) { |
| 155 | + return new \WP_Error( |
| 156 | + "beans_rest_cannot_$action", |
| 157 | + __("Sorry, you are not allowed to $action this resource.", 'woocommerce'), |
| 158 | + array('status' => rest_authorization_required_code()) |
| 159 | + ); |
| 160 | + } |
| 161 | + return true; |
| 162 | + } |
| 163 | + |
| 164 | + public static function get_item_data() |
| 165 | + { |
| 166 | + return array( |
| 167 | + 'card' => Helper::getConfig('card'), |
| 168 | + 'is_setup' => Helper::isSetup(), |
| 169 | + 'riper_version' => Helper::getConfig('riper_version'), |
| 170 | + 'pages' => Helper::getBeansPages(), |
| 171 | + ); |
| 172 | + } |
| 173 | +} |
0 commit comments