client = $client; $this->sync = $sync; } public function register() { add_action( 'rest_api_init', array( $this, 'routes' ) ); } public function routes() { $admin = array( $this, 'can_manage' ); register_rest_route( self::NAMESPACE, '/state', array( 'methods' => 'GET', 'callback' => array( $this, 'state' ), 'permission_callback' => $admin, 'args' => array( 'fresh' => array( 'type' => 'boolean', 'default' => false, ), ), ) ); register_rest_route( self::NAMESPACE, '/sizes', array( array( 'methods' => 'GET', 'callback' => array( $this, 'find_sizes' ), 'permission_callback' => $admin, ), array( 'methods' => 'POST', 'callback' => array( $this, 'fill_sizes' ), 'permission_callback' => $admin, 'args' => array( 'ids' => array( 'type' => 'array', 'items' => array( 'type' => 'integer' ), 'required' => true, 'maxItems' => self::SIZES_LIMIT, ), ), ), ) ); register_rest_route( self::NAMESPACE, '/sync', array( 'methods' => 'POST', 'callback' => array( $this, 'sync' ), 'permission_callback' => $admin, ) ); } public function can_manage() { return current_user_can( 'manage_options' ); } /** Connected or not, the site as ThisBig sees it, where the chip goes in this theme, and the products with no size. */ public function state( $request ) { $plugin = ThisBig_Plugin::instance(); $state = array( 'connected' => $this->client->is_connected(), 'siteKey' => $this->client->site_key(), 'site' => null, 'error' => null, 'woocommerce' => null !== $plugin->woocommerce, 'placement' => $plugin->woocommerce ? $plugin->woocommerce->placement() : null, 'missing' => $plugin->woocommerce ? $plugin->woocommerce->count_missing_dimensions() : 0, 'canSync' => false, ); if ( $state['connected'] ) { $site = $this->client->site_status( (bool) $request->get_param( 'fresh' ) ); if ( is_wp_error( $site ) ) { $state['error'] = $site->get_error_message(); } else { unset( $site['config'] ); $state['site'] = $site; $state['canSync'] = $this->sync->available() && ! empty( $site['features']['catalogue'] ); } } return rest_ensure_response( $state ); } public function find_sizes() { $finder = $this->finder(); return is_wp_error( $finder ) ? $finder : rest_ensure_response( $finder->find() ); } public function fill_sizes( $request ) { $finder = $this->finder(); if ( is_wp_error( $finder ) ) { return $finder; } $filled = $finder->fill( array_map( 'absint', (array) $request->get_param( 'ids' ) ) ); return is_wp_error( $filled ) ? $filled : rest_ensure_response( array( 'filled' => $filled ) ); } public function sync() { $result = $this->sync->run(); if ( is_wp_error( $result ) ) { return $result; } $this->sync->schedule(); return rest_ensure_response( $result ); } /** @return ThisBig_Stated_Sizes|WP_Error */ private function finder() { $plugin = ThisBig_Plugin::instance(); if ( null === $plugin->woocommerce ) { return $this->needs_woocommerce(); } if ( ! $this->client->is_connected() ) { return new WP_Error( 'thisbig_not_connected', __( 'Connect to ThisBig first.', 'thisbig' ), array( 'status' => 409 ) ); } return new ThisBig_Stated_Sizes( $this->client, $plugin->woocommerce, $plugin->kinds ); } private function needs_woocommerce() { return new WP_Error( 'thisbig_no_woocommerce', __( 'This needs WooCommerce.', 'thisbig' ), array( 'status' => 409 ) ); } }