secret(); } /** Trades a one-time connect code for the site's keys. */ public function exchange( $code ) { $response = $this->request( 'POST', '/merchant/connect', array( 'code' => $code, 'site_url' => home_url( '/' ) ), false ); if ( is_wp_error( $response ) ) { return $response; } if ( empty( $response['site_key'] ) || empty( $response['secret'] ) ) { return new WP_Error( 'thisbig_connect', __( 'That code has expired or was already used. Connect again.', 'thisbig' ) ); } update_option( self::OPTION_SITE_KEY, sanitize_text_field( $response['site_key'] ), true ); // The secret is only needed in the admin, so it isn't loaded on every page. update_option( self::OPTION_SECRET, sanitize_text_field( $response['secret'] ), false ); do_action( 'thisbig_connected' ); return true; } public function disconnect() { delete_option( self::OPTION_SITE_KEY ); delete_option( self::OPTION_SECRET ); delete_transient( self::STATUS_CACHE ); } /** * The site as ThisBig sees it: live or not, this month's use, and what the plan includes. Cached ten * minutes; fresh asks ThisBig again, for a plan just chosen there. */ public function site_status( $fresh = false ) { $cached = $fresh ? false : get_transient( self::STATUS_CACHE ); if ( false !== $cached ) { return $cached; } $status = $this->request( 'GET', '/merchant/site' ); if ( ! is_wp_error( $status ) ) { set_transient( self::STATUS_CACHE, $status, 10 * MINUTE_IN_SECONDS ); } return $status; } /** @param array> $products */ public function import( array $products ) { return $this->request( 'POST', '/merchant/products/import', array( 'products' => $products ) ); } /** * What kind of thing each product is, from its name and shelf. * * @param list}> $products */ public function product_kinds( array $products ) { return $this->request( 'POST', '/merchant/products/kinds', array( 'products' => $products ) ); } /** * The sizes products state in their own words, with a unit. * * @param list $products */ public function stated_sizes( array $products ) { return $this->request( 'POST', '/merchant/products/sizes', array( 'products' => $products ) ); } private function secret() { return (string) get_option( self::OPTION_SECRET, '' ); } private function request( $method, $path, $body = null, $authenticated = true ) { $args = array( 'method' => $method, 'timeout' => 20, 'headers' => array( 'Accept' => 'application/json' ), ); if ( $authenticated ) { $args['headers']['Authorization'] = 'Bearer ' . $this->secret(); } if ( null !== $body ) { $args['headers']['Content-Type'] = 'application/json'; $args['body'] = wp_json_encode( $body ); } $response = wp_remote_request( THISBIG_API_BASE . $path, $args ); if ( is_wp_error( $response ) ) { return new WP_Error( 'thisbig_unreachable', __( 'Couldn’t reach ThisBig. Check your connection and try again.', 'thisbig' ), array( 'status' => 502 ) ); } $code = (int) wp_remote_retrieve_response_code( $response ); $decoded = json_decode( (string) wp_remote_retrieve_body( $response ), true ); if ( $code >= 400 ) { $message = is_array( $decoded ) && ! empty( $decoded['error'] ) ? $decoded['error'] : __( 'ThisBig didn’t accept that. Try again.', 'thisbig' ); return new WP_Error( 'thisbig_' . $code, $message, array( 'status' => $code ) ); } return is_array( $decoded ) ? $decoded : array(); } }