Skip to content

Commit

Permalink
Move code from it's own class into the DALLE class. Few text tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
dkotter committed Jul 14, 2023
1 parent 82158d0 commit c084b3d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 81 deletions.
74 changes: 0 additions & 74 deletions includes/Classifai/Admin/GenerateImage.php

This file was deleted.

4 changes: 0 additions & 4 deletions includes/Classifai/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
42 changes: 41 additions & 1 deletion includes/Classifai/Providers/OpenAI/DallE.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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' ) ),
]
);
}
}
}

/**
Expand Down Expand Up @@ -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
*/
Expand Down
4 changes: 2 additions & 2 deletions src/js/media-modal/views/generate-image-media-upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
} );
Expand Down

0 comments on commit c084b3d

Please sign in to comment.