PluginProbe
AI / trunk
AI vtrunk
1.3.0 1.2.0 1.1.0 1.0.2 1.0.1 1.0.0 0.9.0 trunk 0.1.1 0.2.0 0.2.1 0.3.0 0.3.1 0.4.0 0.4.1 0.5.0 0.6.0 0.7.0 0.8.0
ai / includes / Experiments / Title_Generation / Title_Generation.php

Title_Generation.php in AI trunk, at includes/Experiments/Title_Generation/Title_Generation.php

110 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Title generation experiment implementation.
4 *
5 * @package WordPress\AI
6 */
7
8 declare( strict_types=1 );
9
10 namespace WordPress\AI\Experiments\Title_Generation;
11
12 use WordPress\AI\Abilities\Title_Generation\Title_Generation as Title_Generation_Ability;
13 use WordPress\AI\Abstracts\Abstract_Feature;
14 use WordPress\AI\Asset_Loader;
15 use WordPress\AI\Experiments\Experiment_Category;
16
17 use function WordPress\AI\get_min_content_length;
18
19 if ( ! defined( 'ABSPATH' ) ) {
20 exit;
21 }
22
23 /**
24 * Title generation experiment.
25 *
26 * @since 0.1.0
27 */
28 class Title_Generation extends Abstract_Feature {
29
30 /**
31 * {@inheritDoc}
32 */
33 public static function get_id(): string {
34 return 'title-generation';
35 }
36
37 /**
38 * {@inheritDoc}
39 */
40 protected function load_metadata(): array {
41 return array(
42 'label' => __( 'Title Generation', 'ai' ),
43 'description' => __( 'Generates title suggestions from content. Requires an AI connector that includes support for text generation models.', 'ai' ),
44 'category' => Experiment_Category::EDITOR,
45 );
46 }
47
48 /**
49 * {@inheritDoc}
50 *
51 * @since 0.1.0
52 */
53 public function register(): void {
54 add_action( 'wp_abilities_api_init', array( $this, 'register_abilities' ) );
55 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
56 }
57
58 /**
59 * Registers any needed abilities.
60 *
61 * @since 0.1.0
62 */
63 public function register_abilities(): void {
64 wp_register_ability(
65 'ai/' . $this->get_id(),
66 array(
67 'label' => $this->get_label(),
68 'description' => $this->get_description(),
69 'ability_class' => Title_Generation_Ability::class,
70 ),
71 );
72 }
73
74 /**
75 * Enqueues and localizes the admin script.
76 *
77 * @since 0.1.0
78 *
79 * @param string $hook_suffix The current admin page hook suffix.
80 */
81 public function enqueue_assets( string $hook_suffix ): void {
82 // Load asset in new post and edit post screens only.
83 if ( 'post.php' !== $hook_suffix && 'post-new.php' !== $hook_suffix ) {
84 return;
85 }
86
87 $screen = get_current_screen();
88
89 // Load the assets only if the post type supports titles and is not an attachment.
90 if (
91 ! $screen ||
92 ! post_type_supports( $screen->post_type, 'title' ) ||
93 in_array( $screen->post_type, array( 'attachment' ), true )
94 ) {
95 return;
96 }
97
98 Asset_Loader::enqueue_script( 'title_generation', 'experiments/title-generation', array( 'include_core_abilities' => true ) );
99 Asset_Loader::enqueue_style( 'title_generation', 'experiments/title-generation' );
100 Asset_Loader::localize_script(
101 'title_generation',
102 'TitleGenerationData',
103 array(
104 'enabled' => $this->is_enabled(),
105 'minContentLength' => get_min_content_length( 'title-generation', 250 ),
106 )
107 );
108 }
109 }
110