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