| 1 |
<?php |
| 2 |
/* |
| 3 |
* OpenAI Rest Controller |
| 4 |
*/ |
| 5 |
|
| 6 |
if ( ! defined( 'ABSPATH' ) ) { |
| 7 |
die; |
| 8 |
} |
| 9 |
|
| 10 |
class BDTHEMES_OPENAI_REST_CONTROLLER extends WP_REST_Controller { |
| 11 |
public function __construct() { |
| 12 |
$this->namespace = 'bdthemes/v1'; |
| 13 |
$this->rest_base = 'openai'; |
| 14 |
add_action( 'rest_api_init', [ $this, 'register_rest_routes' ] ); |
| 15 |
} |
| 16 |
|
| 17 |
public function register_rest_routes() { |
| 18 |
$permission = [ $this, 'api_key_permission_check' ]; |
| 19 |
register_rest_route( $this->namespace, $this->rest_base . '/api-key', [ |
| 20 |
[ |
| 21 |
'methods' => 'GET', |
| 22 |
'callback' => [ $this, 'get_openai_api_key' ], |
| 23 |
'permission_callback' => $permission, |
| 24 |
], |
| 25 |
[ |
| 26 |
'methods' => 'POST', |
| 27 |
'callback' => [ $this, 'get_openai_api_key' ], |
| 28 |
'permission_callback' => $permission, |
| 29 |
], |
| 30 |
] ); |
| 31 |
} |
| 32 |
|
| 33 |
public function image_generation_permission_check( $request ) { |
| 34 |
return current_user_can( 'edit_posts' ); |
| 35 |
} |
| 36 |
|
| 37 |
public function api_key_permission_check( $request ) { |
| 38 |
$nonce = $request->get_header( 'X-WP-Nonce' ); |
| 39 |
if ( ! wp_verify_nonce( $nonce, 'wp_rest' ) ) { |
| 40 |
return new WP_Error( 'rest_forbidden', esc_html__( 'Nonce verification failed', 'ai-image' ), array( 'status' => 403 ) ); |
| 41 |
} |
| 42 |
|
| 43 |
// Check if the user has the required capability |
| 44 |
return current_user_can( 'manage_options' ); |
| 45 |
} |
| 46 |
|
| 47 |
public function get_openai_api_key( WP_REST_Request $request ) { |
| 48 |
$api_key = get_option( 'bdthemes_openai_api_key' ); |
| 49 |
$api_key = is_string( $api_key ) ? trim( $api_key ) : ''; |
| 50 |
return new WP_REST_Response( [ |
| 51 |
'api_key' => $api_key ? sanitize_text_field( $api_key ) : null, |
| 52 |
], 200 ); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
new BDTHEMES_OPENAI_REST_CONTROLLER(); |
| 57 |
|
| 58 |
/** |
| 59 |
* Unsplash & Giphy API keys REST controller |
| 60 |
*/ |
| 61 |
class BDTHEMES_AI_IMAGE_KEYS_REST_CONTROLLER extends WP_REST_Controller { |
| 62 |
|
| 63 |
public function __construct() { |
| 64 |
$this->namespace = 'bdthemes/v1'; |
| 65 |
add_action( 'rest_api_init', [ $this, 'register_rest_routes' ] ); |
| 66 |
} |
| 67 |
|
| 68 |
public function register_rest_routes() { |
| 69 |
$permission = [ $this, 'api_key_permission_check' ]; |
| 70 |
register_rest_route( $this->namespace, 'pexels/api-key', [ |
| 71 |
[ |
| 72 |
'methods' => 'GET', |
| 73 |
'callback' => [ $this, 'get_pexels_api_key' ], |
| 74 |
'permission_callback' => $permission, |
| 75 |
], |
| 76 |
[ |
| 77 |
'methods' => 'POST', |
| 78 |
'callback' => [ $this, 'get_pexels_api_key' ], |
| 79 |
'permission_callback' => $permission, |
| 80 |
], |
| 81 |
] ); |
| 82 |
register_rest_route( $this->namespace, 'unsplash/api-key', [ |
| 83 |
[ |
| 84 |
'methods' => 'GET', |
| 85 |
'callback' => [ $this, 'get_unsplash_api_key' ], |
| 86 |
'permission_callback' => $permission, |
| 87 |
], |
| 88 |
[ |
| 89 |
'methods' => 'POST', |
| 90 |
'callback' => [ $this, 'get_unsplash_api_key' ], |
| 91 |
'permission_callback' => $permission, |
| 92 |
], |
| 93 |
] ); |
| 94 |
register_rest_route( $this->namespace, 'pixabay/api-key', [ |
| 95 |
[ |
| 96 |
'methods' => 'GET', |
| 97 |
'callback' => [ $this, 'get_pixabay_api_key' ], |
| 98 |
'permission_callback' => $permission, |
| 99 |
], |
| 100 |
[ |
| 101 |
'methods' => 'POST', |
| 102 |
'callback' => [ $this, 'get_pixabay_api_key' ], |
| 103 |
'permission_callback' => $permission, |
| 104 |
], |
| 105 |
] ); |
| 106 |
register_rest_route( $this->namespace, 'giphy/api-key', [ |
| 107 |
[ |
| 108 |
'methods' => 'GET', |
| 109 |
'callback' => [ $this, 'get_giphy_api_key' ], |
| 110 |
'permission_callback' => $permission, |
| 111 |
], |
| 112 |
[ |
| 113 |
'methods' => 'POST', |
| 114 |
'callback' => [ $this, 'get_giphy_api_key' ], |
| 115 |
'permission_callback' => $permission, |
| 116 |
], |
| 117 |
] ); |
| 118 |
register_rest_route( $this->namespace, 'gemini/api-key', [ |
| 119 |
[ |
| 120 |
'methods' => 'GET', |
| 121 |
'callback' => [ $this, 'get_gemini_api_key' ], |
| 122 |
'permission_callback' => $permission, |
| 123 |
], |
| 124 |
[ |
| 125 |
'methods' => 'POST', |
| 126 |
'callback' => [ $this, 'get_gemini_api_key' ], |
| 127 |
'permission_callback' => $permission, |
| 128 |
], |
| 129 |
] ); |
| 130 |
} |
| 131 |
|
| 132 |
public function api_key_permission_check( $request ) { |
| 133 |
$nonce = $request->get_header( 'X-WP-Nonce' ); |
| 134 |
if ( ! wp_verify_nonce( $nonce, 'wp_rest' ) ) { |
| 135 |
return new WP_Error( 'rest_forbidden', esc_html__( 'Nonce verification failed', 'ai-image' ), [ 'status' => 403 ] ); |
| 136 |
} |
| 137 |
return current_user_can( 'manage_options' ); |
| 138 |
} |
| 139 |
|
| 140 |
public function get_pexels_api_key( WP_REST_Request $request ) { |
| 141 |
$api_key = get_option( 'bdthemes_pexels_api_key' ); |
| 142 |
$api_key = is_string( $api_key ) ? trim( $api_key ) : ''; |
| 143 |
return new WP_REST_Response( [ |
| 144 |
'api_key' => $api_key ? sanitize_text_field( $api_key ) : null, |
| 145 |
], 200 ); |
| 146 |
} |
| 147 |
|
| 148 |
public function get_unsplash_api_key( WP_REST_Request $request ) { |
| 149 |
$api_key = get_option( 'bdthemes_unsplash_access_key' ); |
| 150 |
$api_key = is_string( $api_key ) ? trim( $api_key ) : ''; |
| 151 |
return new WP_REST_Response( [ |
| 152 |
'api_key' => $api_key ? sanitize_text_field( $api_key ) : null, |
| 153 |
], 200 ); |
| 154 |
} |
| 155 |
|
| 156 |
public function get_pixabay_api_key( WP_REST_Request $request ) { |
| 157 |
$api_key = get_option( 'bdthemes_pixabay_api_key' ); |
| 158 |
$api_key = is_string( $api_key ) ? trim( $api_key ) : ''; |
| 159 |
return new WP_REST_Response( [ |
| 160 |
'api_key' => $api_key ? sanitize_text_field( $api_key ) : null, |
| 161 |
], 200 ); |
| 162 |
} |
| 163 |
|
| 164 |
public function get_giphy_api_key( WP_REST_Request $request ) { |
| 165 |
$api_key = get_option( 'bdthemes_giphy_api_key' ); |
| 166 |
$api_key = is_string( $api_key ) ? trim( $api_key ) : ''; |
| 167 |
return new WP_REST_Response( [ |
| 168 |
'api_key' => $api_key ? sanitize_text_field( $api_key ) : null, |
| 169 |
], 200 ); |
| 170 |
} |
| 171 |
|
| 172 |
public function get_gemini_api_key( WP_REST_Request $request ) { |
| 173 |
$api_key = get_option( 'bdthemes_gemini_api_key' ); |
| 174 |
$api_key = is_string( $api_key ) ? trim( $api_key ) : ''; |
| 175 |
return new WP_REST_Response( [ |
| 176 |
'api_key' => $api_key ? sanitize_text_field( $api_key ) : null, |
| 177 |
], 200 ); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
new BDTHEMES_AI_IMAGE_KEYS_REST_CONTROLLER(); |
| 182 |
|