# b-blocks/2.0.39/includes/ApiKeys.php

bBlocks – Essential Gutenberg Blocks &amp; Patterns Collection, version 2.0.39. 41 lines.

- Page: https://pluginprobe.com/plugins/b-blocks/2.0.39/code/includes/ApiKeys.php
- Raw: https://pluginprobe.com/plugins/b-blocks/2.0.39/raw/includes/ApiKeys.php
- Modified: 2026-04-27T12:46:42+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/b-blocks/2.0.39/code/includes/ApiKeys.php#L10-L20`.

```php
<?php
namespace BBlocks\Inc;
class BBlocksApiKeys{
	function __construct(){
		add_action( 'wp_ajax_bblocks_api_keys', [$this, 'bBlocksApiKeys'] );
		add_action( 'wp_ajax_nopriv_bblocks_api_keys', [$this, 'bBlocksApiKeys'] );
	}

    function bBlocksTestApiKeys($key,$value){
        if ($key == 'openAi') {
            return BBlocksAi::get_instance()->test_api_key($value);
        }
    }

    function bBlocksApiKeys(){
        $nonce = sanitize_text_field( wp_unslash( $_POST['_wpnonce']?? null ) ) ;

        if( !wp_verify_nonce( $nonce, 'wp_ajax' ) ){
            wp_send_json_error( ['message'=>'Invalid Request'] );
        }

        if (!current_user_can('manage_options')) {
            wp_send_json_error( ['message'=>'Unauthorized'] );
        }

        $key   = sanitize_text_field(wp_unslash($_POST['key'] ?? ''));
        $value = sanitize_text_field(wp_unslash($_POST['value'] ?? ''));
        $apiKeys = get_option('bBlocksApiKeys',[]);
        $error = $this->bBlocksTestApiKeys($key,$value);
        
        if (isset($error['valid']) && !$error['valid']) {
            wp_send_json_error([$key=>$error]);
        }
        if ($key) {
            $apiKeys[$key] = ['key'=> $value];
            update_option( 'bBlocksApiKeys',$apiKeys);
        }
        wp_send_json_success($apiKeys);
    }
}
new BBlocksApiKeys();
```
