*/ 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'); ?>