*/ // If this file is called directly, abort. if ( ! defined( 'ABSPATH' ) ) { exit; } class A2reviews_WC { /** * Define the core option to create res api key. * * @since 1.0.0 */ private $res_api_option = [ 'description' => '', 'user' => null, 'permissions' => 'read_write' ]; // static $_instance public static $_instance; /** * Define the core functionality of the plugin. * * Set the plugin name and the plugin version that can be used throughout the plugin. * Load the dependencies, define the locale, and set the hooks for the admin area and * the public-facing side of the site. * * @since 1.0.0 */ public function __construct() { $today = gmdate("F j, Y, g:i a"); $createdOn = "(created on $today)"; $this->res_api_option['description'] = "A2Reviews - API Read/Write $createdOn"; $this->res_api_option['user'] = $this->findAdmin(); } /** * Check instance and reset it * * @return $_instance */ public static function instance() { if ( is_null( self::$_instance ) ) { self::$_instance = new self(); } return self::$_instance; } /** * Check is woocommerce plugin is active * * @since 1.0.0 */ public function isWooCommerce(){ return class_exists( 'WooCommerce' ); } /** * Create RES API KEY * @return response array */ public function createResAPIKey($code){ global $wpdb; $code = (string) $code; $auth_code = (string) get_option( 'a2reviews_auth_code', '' ); // Fail closed: never create credentials without the one-time code. if ( '' === $code || '' === $auth_code || ! hash_equals( $auth_code, $code ) ) return ['error' => 'Invalid Code']; if(!$this->isWooCommerce()) return ['error' => 'WC Not Active']; $response = array(); try{ $user_id = $this->res_api_option['user']; $description = $this->res_api_option['description']; $permissions = $this->res_api_option['permissions']; $consumer_key = 'ck_' . wc_rand_hash(); $consumer_secret = 'cs_' . wc_rand_hash(); $data = array( 'user_id' => $user_id, 'description' => $description, 'permissions' => $permissions, 'consumer_key' => wc_api_hash( $consumer_key ), 'consumer_secret' => $consumer_secret, 'truncated_key' => substr( $consumer_key, -7 ), ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Mirrors WooCommerce core key creation; there is no higher-level API for inserting a REST API key. $wpdb->insert( $wpdb->prefix . 'woocommerce_api_keys', $data, array( '%d', '%s', '%s', '%s', '%s', '%s', ) ); if ( 0 === $wpdb->insert_id ) { throw new Exception( esc_html__( 'There was an error generating your API Key.', 'a2reviews' ) ); } $key_id = $wpdb->insert_id; $response = $data; $response['consumer_key'] = $consumer_key; $response['consumer_secret'] = $consumer_secret; }catch(Exception $e){ return array( 'error' => $e->getMessage() ); } return $response; } /** * Find admin user id * * @return id */ private function findAdmin(){ $users = get_users( array( 'role__in' => array( 'administrator' ) ) ); if($users){ return $users[0]->ID; } return 0; } } if(!function_exists('a2reviews_wc')){ function a2reviews_wc(){ return A2reviews_WC::instance(); } } if(!function_exists('A2WC')){ /** * Backwards-compatible alias for a2reviews_wc(). * * @return A2reviews_WC */ function A2WC(){ // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- Deprecated alias kept for backwards compatibility. return a2reviews_wc(); } }