diff --git a/includes/Classifai/Admin/GenerateImage.php b/includes/Classifai/Admin/GenerateImage.php deleted file mode 100644 index 5bd1f3a0a..000000000 --- a/includes/Classifai/Admin/GenerateImage.php +++ /dev/null @@ -1,74 +0,0 @@ -dalle = new DallE( false ); - - add_action( 'admin_menu', [ $this, 'register_generate_media_page' ], 0 ); - add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_admin_scripts' ], 5, 1 ); - } - - /** - * Enqueue the admin scripts. - * - * @param string $hook_suffix The current admin page. - */ - public function enqueue_admin_scripts( $hook_suffix = '' ) { - $hook_suffix; - if ( 'upload.php' !== $hook_suffix ) { - return; - } - - $action = isset( $_GET['action'] ) ? sanitize_key( $_GET['action'] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended - if ( - 'classifai-generate-image' === $action - && $this->dalle->can_generate_image() - ) { - wp_enqueue_media(); - wp_enqueue_script( - 'classifai-generate-images-media-upload', - CLASSIFAI_PLUGIN_URL . 'dist/generate-image-media-upload.js', - [ 'jquery' ], - get_asset_info( 'classifai-generate-images-media-upload', 'version' ), - true - ); - - wp_localize_script( - 'classifai-generate-images-media-upload', - 'classifaiGenerateImages', - [ - 'upload_url' => esc_url( admin_url( 'upload.php' ) ), - ] - ); - } - } - - /** - * Registers Media > Generate Image submenu - */ - public function register_generate_media_page() { - if ( $this->dalle->can_generate_image() ) { - add_submenu_page( - 'upload.php', - esc_attr__( 'Generate Image', 'classifai' ), - esc_attr__( 'Generate Image', 'classifai' ), - apply_filters( 'classifai_generate_image_menu_capability', 'upload_files' ), - esc_url( admin_url( 'upload.php?action=classifai-generate-image' ) ), - '', - ); - } - } -} diff --git a/includes/Classifai/Plugin.php b/includes/Classifai/Plugin.php index 5b5760d7c..4587dfa18 100644 --- a/includes/Classifai/Plugin.php +++ b/includes/Classifai/Plugin.php @@ -73,10 +73,6 @@ public function init() { $onboarding = new Admin\Onboarding(); $onboarding->init(); - // Initialize the classifAI Generate Image media dashboard. - $onboarding = new Admin\GenerateImage(); - $onboarding->init(); - /** * Fires after ClassifAI services are loaded. * diff --git a/includes/Classifai/Providers/OpenAI/DallE.php b/includes/Classifai/Providers/OpenAI/DallE.php index 268eff6fb..a8d37983f 100644 --- a/includes/Classifai/Providers/OpenAI/DallE.php +++ b/includes/Classifai/Providers/OpenAI/DallE.php @@ -58,11 +58,29 @@ public function __construct( $service ) { */ public function register() { if ( $this->can_generate_image() ) { + add_action( 'admin_menu', [ $this, 'register_generate_media_page' ], 0 ); add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_admin_scripts' ] ); add_action( 'print_media_templates', [ $this, 'print_media_templates' ] ); } } + /** + * Registers a Media > Generate Image submenu + */ + public function register_generate_media_page() { + $settings = $this->get_settings(); + $number_of_images = absint( $settings['number'] ); + + add_submenu_page( + 'upload.php', + $number_of_images > 1 ? esc_html__( 'Generate Images', 'classifai' ) : esc_html__( 'Generate Image', 'classifai' ), + $number_of_images > 1 ? esc_html__( 'Generate Images', 'classifai' ) : esc_html__( 'Generate Image', 'classifai' ), + 'upload_files', + esc_url( admin_url( 'upload.php?action=classifai-generate-image' ) ), + '' + ); + } + /** * Enqueue the admin scripts. * @@ -124,6 +142,28 @@ public function enqueue_admin_scripts( $hook_suffix = '' ) { 'caption' => $caption, ] ); + + if ( 'upload.php' === $hook_suffix ) { + $action = isset( $_GET['action'] ) ? sanitize_key( wp_unslash( $_GET['action'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended + + if ( 'classifai-generate-image' === $action ) { + wp_enqueue_script( + 'classifai-generate-images-media-upload', + CLASSIFAI_PLUGIN_URL . 'dist/generate-image-media-upload.js', + [ 'jquery' ], + get_asset_info( 'classifai-generate-images-media-upload', 'version' ), + true + ); + + wp_localize_script( + 'classifai-generate-images-media-upload', + 'classifaiGenerateImages', + [ + 'upload_url' => esc_url( admin_url( 'upload.php' ) ), + ] + ); + } + } } /** @@ -474,7 +514,7 @@ public function generate_image_callback( string $prompt = '', array $args = [] ) } /** - * Checks whether the current screen can generate image + * Checks whether we can generate images. * * @return bool */ diff --git a/src/js/media-modal/views/generate-image-media-upload.js b/src/js/media-modal/views/generate-image-media-upload.js index 73bea5aae..3c1e27ed2 100644 --- a/src/js/media-modal/views/generate-image-media-upload.js +++ b/src/js/media-modal/views/generate-image-media-upload.js @@ -4,8 +4,8 @@ import { __ } from '@wordpress/i18n'; document.addEventListener( 'DOMContentLoaded', function () { if ( wp.media ) { const frame = wp.media( { - title: __( 'Generate Images', 'classifai' ), - button: { text: __( 'View Details', 'classifai' ) }, + title: __( 'Generate images', 'classifai' ), + button: { text: __( 'View details', 'classifai' ) }, multiple: false, frame: 'select', } );