*/ class Ai_For_Wordpress_By_Upcasted_Admin { /** * 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; /** @var UpcastedEncryption $encryption */ private $encryption; /** * Initialize the class and set its properties. * * @since 1.0.0 * @param string $plugin_name The name of this plugin. * @param string $version The version of this plugin. */ public function __construct($plugin_name, $version) { $this->plugin_name = $plugin_name; $this->version = $version; $this->encryption = UpcastedEncryption::getInstance(); } function add_admin_page() { add_submenu_page( 'upload.php', __('AI Image Generator', 'ai-wp-upcasted'), __('AI Generator', 'ai-wp-upcasted'), 'manage_options', 'ai-wp-upcasted', array($this, 'main_app') ); add_options_page( __('AI Image Generator Settings', 'ai-wp-upcasted'), __('AI Image Generator', 'ai-wp-upcasted'), 'manage_options', 'ai-wp-upcasted-options', array($this, 'settings_page') ); } function main_app() { ?>
validate_api_key($apiKey); if (isset($apiKeyIsValid['error'])) { $error = new WP_Error($apiKeyIsValid['error']['code'], $apiKeyIsValid['error']['message']); wp_send_json_error($error); } update_option('ai_wp_upcasted_api_key', UpcastedEncryption::getInstance()->encrypt($apiKey)); wp_send_json_success(); } else { $error = new WP_Error('Error', __('API key cannot be empty', 'ai-wp-upcasted')); wp_send_json_error($error); } } function set_image_variations_settings() { check_ajax_referer('_wpnonce', 'security'); if (!current_user_can('manage_options')) { return new WP_Error('unauthorized', __('You are not authorized to perform this action.', 'ai-wp-upcasted')); } $errors = new WP_Error(); $response = array('success' => false); if (isset($_POST['variationsResolution'])) { $resolution = sanitize_text_field($_POST['variationsResolution']); if (!in_array($resolution, array('256x256', '512x512', '1024x1024'))) { $errors->add('invalid_resolution', __('Invalid image resolution selected.', 'ai-wp-upcasted')); } else { update_option('ai_wp_upcasted_resolution', $resolution); } } if (isset($_POST['noOfVariations'])) { $no_of_variations = intval($_POST['noOfVariations']); if ($no_of_variations < 1 || $no_of_variations > 10) { $errors->add('invalid_variations_number', __('Invalid number of image variations selected.', 'ai-wp-upcasted')); } else { update_option('ai_wp_upcasted_variations_num', $no_of_variations); } } if (is_wp_error($errors) && count($errors->get_error_codes()) > 0) { $response['data'] = array( 'success' => false, 'message' => $errors->get_error_message() ); } else { $response['success'] = true; $response['data'] = array( 'success' => true, 'message' => __('Image variations settings saved successfully.') ); } wp_send_json($response); } // Function to display the API Key input field in the OpenAI Image Generation page function settings_page() { if (!current_user_can('manage_options')) { return; } if (isset($_POST['ai_wp_upcasted_api_key']) && !empty($_POST['ai_wp_upcasted_api_key'])) { update_option( 'ai_wp_upcasted_api_key', UpcastedEncryption::getInstance()->encrypt( sanitize_text_field( $_POST['ai_wp_upcasted_api_key'] ) ) ); } if (isset($_POST['ai_wp_upcasted_resolution'])) { update_option( 'ai_wp_upcasted_resolution', sanitize_text_field( $_POST['ai_wp_upcasted_resolution'] ) ); } if (isset($_POST['ai_wp_upcasted_variations_num'])) { update_option( 'ai_wp_upcasted_variations_num', sanitize_text_field( intval($_POST['ai_wp_upcasted_variations_num']) ) ); } $api_key = get_option('ai_wp_upcasted_api_key', ''); $selected_option = get_option('ai_wp_upcasted_resolution'); $variations_num = get_option('ai_wp_upcasted_variations_num'); ?>

Where do I find my Secret API Key?

To get an API key you will need to create an account on OpenAI.com

OpenAI API Key set and ready! Change API key

'256x256', '512x512' => '512x512', '1024x1024' => '1024x1024' ); ?>
' . __('Configure the OpenAI Image Generation API settings', 'ai-wp-upcasted') . '

'; // } /** * Render the API key setting. */ // public function render_api_key_setting() // { // $option = get_option('ai_wp_upcasted_api_key'); // echo ''; // } /** * Render the resolution setting. */ // public function render_resolution_setting() // { // $options = array( // '256x256' => '256x256', // '512x512' => '512x512', // '1024x1024' => '1024x1024' // ); // $selected_option = get_option('ai_wp_upcasted_resolution'); // echo ''; // } /** * Render the number of images setting. */ // public function render_variations_num_setting() // { // $option = get_option('ai_wp_upcasted_variations_num'); // echo ''; // } /** * Register the stylesheets for the admin area. * * @since 1.0.0 */ public function enqueue_styles() { /** * This function is provided for demonstration purposes only. * * An instance of this class should be passed to the run() function * defined in Ai_For_Wordpress_By_Upcasted_Loader as all of the hooks are defined * in that particular class. * * The Ai_For_Wordpress_By_Upcasted_Loader will then create the relationship * between the defined hooks and the functions defined in this * class. */ wp_enqueue_style($this->plugin_name, plugin_dir_url(__FILE__) . 'css/ai-wp-upcasted-admin.css', array(), $this->version, 'all'); } /** * Register the JavaScript for the admin area. * * @since 1.0.0 */ public function enqueue_scripts() { /** * This function is provided for demonstration purposes only. * * An instance of this class should be passed to the run() function * defined in Ai_For_Wordpress_By_Upcasted_Loader as all of the hooks are defined * in that particular class. * * The Ai_For_Wordpress_By_Upcasted_Loader will then create the relationship * between the defined hooks and the functions defined in this * class. */ $currentScreen = get_current_screen(); wp_enqueue_media(); wp_enqueue_script($this->plugin_name, plugin_dir_url(__FILE__) . 'js/ai-wp-upcasted-admin.js', array('jquery'), $this->version, false); if ($currentScreen->base == 'media_page_ai-wp-upcasted') { $pluginDirUrl = plugin_dir_url(__DIR__); $noOfVariations = get_option('ai_wp_upcasted_variations_num', ''); $variationsResolution = get_option('ai_wp_upcasted_resolution', ''); $apiIsSet = get_option('ai_wp_upcasted_api_key', ''); wp_enqueue_script('ai-wp-upcasted-react', plugin_dir_url(__DIR__) . 'dist/ai-wp-upcasted-react.js', array('wp-element', 'wp-api'), $this->version, false); wp_localize_script( 'ai-wp-upcasted-react', 'ai_wp_upcasted_object', array( 'noOfVariations' => $noOfVariations, 'variationsResolution' => $variationsResolution, 'apiIsSet' => !empty($apiIsSet) ? true : false, 'security' => wp_create_nonce('_wpnonce'), 'pluginDirUrl' => $pluginDirUrl, ) ); } if ($currentScreen->id === 'upload') { wp_enqueue_script('ai-wp-upcasted-media-library', plugin_dir_url(__DIR__) . 'admin/js/ai-wp-upcasted-media-library.js', array('jquery'), $this->version, true); wp_localize_script( 'ai-wp-upcasted-media-library', 'upcasted_media_library', array( 'buttonTitle' => __('Generate with AI', 'ai-wp-upcasted'), ) ); } } }