'Email password and client id are required.')); } $client = new API_Client(); $site_url = home_url(); $token = $client->login($email, $password, $site_url, $client_id); if (is_wp_error($token)) { self::redirect(array('threeplnext_error' => $token->get_error_message())); } $key_result = API_Keys::create_api_key(get_current_user_id()); if (is_wp_error($key_result)) { self::redirect(array('threeplnext_error' => $key_result->get_error_message())); } \threeplnext_wc_update_option('threeplnext_wc_token', $token); \threeplnext_wc_update_option('threeplnext_wc_email', $email); \threeplnext_wc_update_option('threeplnext_wc_password', $password); \threeplnext_wc_update_option('threeplnext_wc_store_url', $site_url); \threeplnext_wc_update_option('threeplnext_wc_key_id', (int) $key_result['key_id']); \threeplnext_wc_update_option('threeplnext_wc_consumer_key', $key_result['consumer_key']); \threeplnext_wc_update_option('threeplnext_wc_consumer_secret', $key_result['consumer_secret']); \threeplnext_wc_update_option('threeplnext_wc_webhook_status', 'pending'); \threeplnext_wc_update_option('threeplnext_wc_client_id', $client_id); $store_data = array( 'store_url' => $site_url, 'client_id' => $client_id, 'consumer_key' => $key_result['consumer_key'], 'consumer_secret' => $key_result['consumer_secret'], 'wordpress_version' => get_bloginfo('version'), 'woocommerce_version' => defined('WC_VERSION') ? WC_VERSION : '', ); $register = $client->register_store($token, $store_data); if (is_wp_error($register)) { API_Keys::delete_api_key((int) $key_result['key_id']); self::clear_options(); self::redirect(array('threeplnext_error' => $register->get_error_message())); } self::redirect(array('threeplnext_message' => 'Connected to 3plnext successfully.')); } public static function handle_disconnect(): void { if (!current_user_can('manage_woocommerce')) { wp_die(esc_html__('You do not have permission to perform this action.', '3plnext')); } check_admin_referer('threeplnext_disconnect', 'threeplnext_disconnect_nonce'); //$token = (string) \threeplnext_wc_get_option('threeplnext_wc_token', ''); $email = (string) \threeplnext_wc_get_option('threeplnext_wc_email', ''); $password = (string) \threeplnext_wc_get_option('threeplnext_wc_password', ''); $client_id = (string) \threeplnext_wc_get_option('threeplnext_wc_client_id', ''); $store_url = (string) \threeplnext_wc_get_option('threeplnext_wc_store_url', home_url()); $key_id = (int) \threeplnext_wc_get_option('threeplnext_wc_key_id', 0); $client = new API_Client(); $token = $client->login($email, $password, $store_url, $client_id); if (!empty($token)) { $client->disconnect_store($token, $store_url, $client_id); } API_Keys::delete_api_key($key_id); self::clear_options(); self::redirect(array('threeplnext_message' => 'Disconnected from 3plnext.')); } private static function clear_options(): void { \threeplnext_wc_delete_option('threeplnext_wc_token'); \threeplnext_wc_delete_option('threeplnext_wc_email'); \threeplnext_wc_delete_option('threeplnext_wc_store_url'); \threeplnext_wc_delete_option('threeplnext_wc_key_id'); \threeplnext_wc_delete_option('threeplnext_wc_consumer_key'); \threeplnext_wc_delete_option('threeplnext_wc_consumer_secret'); \threeplnext_wc_delete_option('threeplnext_wc_webhook_status'); \threeplnext_wc_delete_option('threeplnext_wc_client_id'); } private static function redirect(array $args): void { $url = add_query_arg($args, admin_url('admin.php?page=threeplnext-connector')); wp_safe_redirect($url); exit; } }