PluginProbe
bBlocks – Essential Gutenberg Blocks & Patterns Collection / 2.0.43
bBlocks – Essential Gutenberg Blocks & Patterns Collection v2.0.43
2.1.6 2.1.5 2.1.4 2.1.3 2.1.2 2.1.1 2.1.0 2.0.43 2.0.42 2.0.41 2.0.40 2.0.39 2.0.38 trunk 1.0 1.1 1.2 1.3 1.4 1.5 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 All 106 releases
b-blocks / includes / ApiKeys.php

ApiKeys.php in bBlocks – Essential Gutenberg Blocks & Patterns Collection 2.0.43, at includes/ApiKeys.php

41 lines 1.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace BBlocks\Inc;
3 class BBlocksApiKeys{
4 function __construct(){
5 add_action( 'wp_ajax_bblocks_api_keys', [$this, 'bBlocksApiKeys'] );
6 add_action( 'wp_ajax_nopriv_bblocks_api_keys', [$this, 'bBlocksApiKeys'] );
7 }
8
9 function bBlocksTestApiKeys($key,$value){
10 if ($key == 'openAi') {
11 return BBlocksAi::get_instance()->test_api_key($value);
12 }
13 }
14
15 function bBlocksApiKeys(){
16 $nonce = sanitize_text_field( wp_unslash( $_POST['_wpnonce']?? null ) ) ;
17
18 if( !wp_verify_nonce( $nonce, 'wp_ajax' ) ){
19 wp_send_json_error( ['message'=>'Invalid Request'] );
20 }
21
22 if (!current_user_can('manage_options')) {
23 wp_send_json_error( ['message'=>'Unauthorized'] );
24 }
25
26 $key = sanitize_text_field(wp_unslash($_POST['key'] ?? ''));
27 $value = sanitize_text_field(wp_unslash($_POST['value'] ?? ''));
28 $apiKeys = get_option('bBlocksApiKeys',[]);
29 $error = $this->bBlocksTestApiKeys($key,$value);
30
31 if (isset($error['valid']) && !$error['valid']) {
32 wp_send_json_error([$key=>$error]);
33 }
34 if ($key) {
35 $apiKeys[$key] = ['key'=> $value];
36 update_option( 'bBlocksApiKeys',$apiKeys);
37 }
38 wp_send_json_success($apiKeys);
39 }
40 }
41 new BBlocksApiKeys();