*/ class Web3devs_ABNFT_Public { /** * The cookie name used to store individual user's connection id */ const COOKIE_NAME = 'web3devs-abnft'; /** * The ID of this plugin. * * @since 1.0.0 * @access private * @var string $plugin_name The ID of this plugin. */ private $plugin_name; /** * The version of this plugin. * * @since 1.0.0 * @access private * @var string $version The current version of this plugin. */ private $version; /** * Initialize the class and set its properties. * * @since 1.0.0 * @param string $plugin_name The name of the plugin. * @param string $version The version of this plugin. */ public function __construct( $plugin_name, $version ) { $this->plugin_name = $plugin_name; $this->version = $version; add_filter( 'request', array( $this, 'handleCallback' )); add_action( "the_content", array( $this, 'handleContentAccess' )); add_filter( "get_the_excerpt", array( $this, 'handleExcerptAccess' )); add_filter( "comments_open", array( $this, 'handleCommentsAccess' )); add_filter( "wp_list_comments_args", array( $this, 'handleCommentsListAccess' )); add_filter( "get_comments_number", array( $this, 'handleCommentsNumberAccess' )); add_action( 'add_meta_boxes', array($this, 'addWeb3devsABNFTBox')); add_action( 'save_post', array($this, 'handleSavePost')); add_action('init', array($this, 'initiateConnection')); } public function addWeb3devsABNFTBox( ) { add_meta_box( 'web3devs_abnft_box_id', 'Access by NFT', array($this, 'renderWeb3devsABNFTBox'), '', // 'post', // leave empty to add to all post types // 'side', ); } public function renderWeb3devsABNFTBox( $post ) { $abnft_access = get_post_meta($post->ID, '_web3devs_abnft_meta_key', true); $coins = get_option('web3devs_abnft_configured_coins_setting'); ?> $error ]; $selected = get_option('web3devs_abnft_denial_page_setting'); $p = get_permalink($selected); if ($p) { $resp['redirect'] = $p; } header("Access-Control-Allow-Origin: *"); header("Access-Control-Allow-Headers: content-type"); header('Content-Type: application/json; charset=utf-8'); echo json_encode($resp); exit; } public function handleCallback($vars) { if (isset($_GET['web3devs-abnft-callback'])) { if (strtoupper($_SERVER['REQUEST_METHOD']) === 'OPTIONS') { header("Access-Control-Allow-Origin: *"); header("Access-Control-Allow-Headers: content-type"); exit; } if (strtoupper($_SERVER['REQUEST_METHOD']) !== 'POST') { header("Access-Control-Allow-Origin: *"); header("Access-Control-Allow-Headers: content-type"); exit; } //See if we have token associated with this page $pid = url_to_postid(sanitize_url($_SERVER['HTTP_ORIGIN'].$_SERVER['REQUEST_URI'])); $token = get_post_meta($pid, '_web3devs_abnft_meta_key', true); if (!$token) { header("Access-Control-Allow-Origin: *"); header("Access-Control-Allow-Headers: content-type"); exit; } //Grab request body $req = $entityBody = file_get_contents('php://input'); $data = json_decode($req, true); //Validate required fields: $required = array( 'network', 'walletAddress', 'publicKey', 'token', 'signature', ); foreach ($required as $field) { if (!isset($data[$field]) || strlen($data[$field]) == 0) { $this->responseError('Missing required field: '.$field); } } //Verify signature if (!$this->verifyAccess( trim($data['network']), trim($data['walletAddress']), trim($data['publicKey']), trim($data['token']), trim($data['signature']), )) { $this->responseError('Signature verification failed'); } //You've proven you have what we want, yay! Let's store it so you don't need to keep doing that every time! if (!$this->cacheToken($token)) { $this->responseError('Failed caching the token'); } header("Access-Control-Allow-Origin: *"); header("Access-Control-Allow-Headers: content-type"); header('Content-Type: application/json; charset=utf-8'); echo json_encode(array('message' => 'OK')); exit; } return $vars; } private function verifyAccess($network, $walletAddress, $publicKey, $token, $signature) { $rpc = 'https://api.accessbynft.com/check-token/wordpress'; $apiKey = get_option('web3devs_abnft_api_key_setting'); $options = [ 'headers' => [ 'Content-Type' => 'application/json', 'x-api-key' => $apiKey, ], 'timeout' => 60, 'redirection' => 5, 'blocking' => true, 'httpversion' => '1.1', 'sslverify' => false, 'body' => wp_json_encode([ 'network' => $network, 'walletAddress' => $walletAddress, 'publicKey' => $publicKey, 'token' => $token, 'signature' => $signature, ]), ]; $resp = wp_remote_post($rpc, $options); if (is_wp_error($resp)) { die("Error: call to URL $rpc failed with status $status, response $resp"); } $body = json_decode(wp_remote_retrieve_body($resp), true); if ($body['error'] === null) { return true; } return false; } private function renderComponent() { global $post; $p = get_permalink($post); $access_token = get_post_meta($post->ID, '_web3devs_abnft_meta_key', true); $coins = get_option('web3devs_abnft_configured_coins_setting'); $token = null; foreach ($coins as $coin) { if ($coin['network'].'|'.$coin['contract'] === $access_token) { $token = $coin; } } if ($token === null) { return null; } $url_parts = parse_url($p); $params = []; if (isset($url_parts['query'])) { parse_str($url_parts['query'], $params); } $params['web3devs-abnft-callback'] = ''; $url_parts['query'] = http_build_query($params); $port = isset($url_parts['port']) ? ':'.$url_parts['port'] : ''; $callback_url = $url_parts['scheme'].'://'.$url_parts['host'].$port.$url_parts['path'].'?'.$url_parts['query']; $data = [ 'message' => hash('sha256', $this->getConnectionID()), //secret message to sign 'callback' => sanitize_url($callback_url), 'tokenName' => $token['symbol'], 'tokenAddress' => $token['contract'], 'network' => $token['network'], ]; return '