public function create_key() { include_once plugin_dir_path(dirname(__FILE__)) . '/vendor/tenbucks_registration_client/lib/TenbucksRegistrationClient.php'; $form_is_valid = true; $required_fields = array('email', 'email_confirmation'); foreach ($required_fields as $key) { if (!array_key_exists($key, $_POST) || empty($_POST[$key])) { $format = __('Field %s is missing.', 'tenbucks'); return wp_send_json_error(array('message' => sprintf($format, $key), 'field' => $key)); } } $post_data = array_map('strtolower', $_POST); $email = $post_data['email']; $email_confirmation = $post_data['email_confirmation']; $sponsor = empty($post_data['sponsor']) ? null : $post_data['sponsor']; $error_msg = false; if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { $error_msg = __('Invalid email.', 'tenbucks'); } if ($email !== $email_confirmation) { $error_msg = __('Email and confirmation are different.', 'tenbucks'); } if ($error_msg) { return wp_send_json_error(array('message' => $error_msg, 'field' => 'email')); } try { global $wpdb; // If API disabled, active it if (get_option('woocommerce_api_enabled') !== 'yes') { update_option('woocommerce_api_enabled', 'yes'); } $key_id = (int) get_option('tenbucks_ak_id'); $consumer_key = 'ck_' . wc_rand_hash(); $consumer_secret = 'cs_' . wc_rand_hash(); $table = $wpdb->prefix . 'woocommerce_api_keys'; $data = array('user_id' => get_current_user_id(), 'consumer_key' => wc_api_hash($consumer_key), 'consumer_secret' => $consumer_secret, 'truncated_key' => substr($consumer_key, -7)); if (!$key_id) { $data['description'] = 'tenbucks'; $data['permissions'] = 'read_write'; $wpdb->insert($table, $data, array('%d', '%s', '%s', '%s', '%s', '%s')); update_option('tenbucks_ak_id', $wpdb->insert_id); } else { $updated_rows = $wpdb->update($table, $data, array('key_id' => $key_id), array('%d', '%s', '%s', '%s'), array('%d')); if (!$updated_rows) { update_option('tenbucks_ak_id', 0); return wp_send_json_error(array('message' => __('Keys update failed, please try again.', 'tenbucks'))); } } unset($data); $client = new TenbucksRegistrationClient(); $url = get_site_url(); $lang_infos = explode('_', get_locale()); $opts = array('email' => $email, 'sponsor' => $sponsor, 'company' => get_bloginfo('name'), 'platform' => 'WooCommerce', 'locale' => $lang_infos[0], 'country' => $lang_infos[1], 'url' => get_site_url(), 'credentials' => array('api_key' => $consumer_key, 'api_secret' => $consumer_secret)); $query = $client->send($opts); $success = array_key_exists('success', $query) && (bool) $query['success']; if ($success) { // success update_option('tenbucks_registration_complete', true); if ($query['new_account']) { $msg = __('New account created. Please check your emails to confirm your address and start using tenbucks.', 'tenbucks'); $need_reload = false; } else { $msg = __('Shop added to your existing account. Page will reload shortly.', 'tenbucks'); $need_reload = true; } return wp_send_json_success(array('message' => $msg, 'needReload' => $need_reload)); } else { return wp_send_json_error(array('message' => __('Creation failed, please try again.', 'tenbucks'))); } } catch (Exception $e) { return wp_send_json_error(array('message' => $e->getMessage())); } }
* When populating this file, consider the following flow * of control: * * - This method should be static * - Check if the $_REQUEST content actually is the plugin name * - Run an admin referrer check to make sure it goes through authentication * - Verify the output of $_GET makes sense * - Repeat with other user roles. Best directly by using the links/query string parameters. * - Repeat things for multisite. Once for a single site in the network, once sitewide. * * This file may be updated more in future version of the Boilerplate; however, this is the * general skeleton and outline for how the file should work. * * For more information, see the following discussion: * https://github.com/tommcfarlin/WordPress-Plugin-Boilerplate/pull/123#issuecomment-28541913 * * @link https://www.tenbucks.io * @since 1.0.0 */ // If uninstall not called from WordPress, then exit. if (!defined('WP_UNINSTALL_PLUGIN')) { exit; } // Send uninstall notification include dirname(__FILE__) . '/vendor/tenbucks_registration_client/src/TenbucksRegistrationClient.php'; $client = new TenbucksRegistrationClient(); try { $client->uninstall(get_site_url()); } catch (Exception $exc) { echo $exc->getTraceAsString(); }