| 1 |
<?php |
| 2 |
/** |
| 3 |
* Content Translation experiment class. |
| 4 |
* |
| 5 |
* @package WordPress\AI |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace WordPress\AI\Experiments\Content_Translation; |
| 11 |
|
| 12 |
use WordPress\AI\Abilities\Content_Translation\Content_Translation as Content_Translation_Ability; |
| 13 |
use WordPress\AI\Abstracts\Abstract_Feature; |
| 14 |
use WordPress\AI\Asset_Loader; |
| 15 |
use WordPress\AI\Experiments\Experiment_Category; |
| 16 |
use function WordPress\AI\get_min_content_length; |
| 17 |
|
| 18 |
// Exit if accessed directly. |
| 19 |
if ( ! defined( 'ABSPATH' ) ) { |
| 20 |
exit; |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Content Translation experiment. |
| 25 |
* |
| 26 |
* @since 1.3.0 |
| 27 |
*/ |
| 28 |
class Content_Translation extends Abstract_Feature { |
| 29 |
|
| 30 |
/** |
| 31 |
* {@inheritDoc} |
| 32 |
* |
| 33 |
* @since 1.3.0 |
| 34 |
*/ |
| 35 |
public static function get_id(): string { |
| 36 |
return 'content-translation'; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* {@inheritDoc} |
| 41 |
* |
| 42 |
* @since 1.3.0 |
| 43 |
*/ |
| 44 |
protected function load_metadata(): array { |
| 45 |
return array( |
| 46 |
'label' => __( 'Content Translation', 'ai' ), |
| 47 |
'description' => __( 'Translate paragraph and heading blocks into a different language. Requires an AI connector that includes support for text generation models.', 'ai' ), |
| 48 |
'category' => Experiment_Category::EDITOR, |
| 49 |
); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* {@inheritDoc} |
| 54 |
* |
| 55 |
* @since 1.3.0 |
| 56 |
*/ |
| 57 |
public function register(): void { |
| 58 |
add_action( 'wp_abilities_api_init', array( $this, 'register_abilities' ) ); |
| 59 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) ); |
| 60 |
add_action( 'enqueue_block_assets', array( $this, 'enqueue_block_assets' ) ); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Registers required abilities. |
| 65 |
* |
| 66 |
* @since 1.3.0 |
| 67 |
*/ |
| 68 |
public function register_abilities(): void { |
| 69 |
wp_register_ability( |
| 70 |
sprintf( 'ai/%s', $this->get_id() ), |
| 71 |
array( |
| 72 |
'label' => $this->get_label(), |
| 73 |
'description' => $this->get_description(), |
| 74 |
'ability_class' => Content_Translation_Ability::class, |
| 75 |
) |
| 76 |
); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Enqueues and localizes the admin script. |
| 81 |
* |
| 82 |
* @since 1.3.0 |
| 83 |
* |
| 84 |
* @param string $hook_suffix The current admin page hook suffix. |
| 85 |
*/ |
| 86 |
public function enqueue_assets( string $hook_suffix ): void { |
| 87 |
// Enqueue assets only on the post editor screen. |
| 88 |
if ( 'post.php' !== $hook_suffix && 'post-new.php' !== $hook_suffix ) { |
| 89 |
return; |
| 90 |
} |
| 91 |
|
| 92 |
Asset_Loader::enqueue_script( |
| 93 |
'content_translation', |
| 94 |
'experiments/content-translation', |
| 95 |
array( |
| 96 |
'include_core_abilities' => true, |
| 97 |
) |
| 98 |
); |
| 99 |
|
| 100 |
Asset_Loader::enqueue_style( |
| 101 |
'content_translation', |
| 102 |
'experiments/content-translation' |
| 103 |
); |
| 104 |
|
| 105 |
Asset_Loader::localize_script( |
| 106 |
'content_translation', |
| 107 |
'ContentTranslationData', |
| 108 |
array( |
| 109 |
'enabled' => $this->is_enabled(), |
| 110 |
'languages' => Languages::get_supported_languages_for_js(), |
| 111 |
'minContentLength' => get_min_content_length( |
| 112 |
'content-translation', |
| 113 |
Content_Translation_Ability::DEFAULT_MIN_CONTENT_LENGTH |
| 114 |
), |
| 115 |
) |
| 116 |
); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Enqueues the block stylesheet for the editor canvas. |
| 121 |
* |
| 122 |
* @since 1.3.0 |
| 123 |
*/ |
| 124 |
public function enqueue_block_assets(): void { |
| 125 |
Asset_Loader::enqueue_style( |
| 126 |
'content_translation', |
| 127 |
'experiments/content-translation' |
| 128 |
); |
| 129 |
} |
| 130 |
} |
| 131 |
|