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 / Excerpt_Generation / Excerpt_Generation.php

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

107 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Excerpt generation experiment implementation.
4 *
5 * @package WordPress\AI
6 */
7
8 declare( strict_types=1 );
9
10 namespace WordPress\AI\Experiments\Excerpt_Generation;
11
12 use WordPress\AI\Abilities\Excerpt_Generation\Excerpt_Generation as Excerpt_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 * Excerpt generation experiment.
25 *
26 * @since 0.2.0
27 */
28 class Excerpt_Generation extends Abstract_Feature {
29
30 /**
31 * {@inheritDoc}
32 */
33 public static function get_id(): string {
34 return 'excerpt-generation';
35 }
36
37 /**
38 * {@inheritDoc}
39 */
40 protected function load_metadata(): array {
41 return array(
42 'label' => __( 'Excerpt Generation', 'ai' ),
43 'description' => __( 'Generates excerpt 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 public function register(): void {
52 add_action( 'wp_abilities_api_init', array( $this, 'register_abilities' ) );
53 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
54 }
55
56 /**
57 * Registers any needed abilities.
58 *
59 * @since 0.2.0
60 */
61 public function register_abilities(): void {
62 wp_register_ability(
63 'ai/' . $this->get_id(),
64 array(
65 'label' => $this->get_label(),
66 'description' => $this->get_description(),
67 'ability_class' => Excerpt_Generation_Ability::class,
68 ),
69 );
70 }
71
72 /**
73 * Enqueues and localizes the admin script.
74 *
75 * @since 0.2.0
76 *
77 * @param string $hook_suffix The current admin page hook suffix.
78 */
79 public function enqueue_assets( string $hook_suffix ): void {
80 // Load asset in new post and edit post screens only.
81 if ( 'post.php' !== $hook_suffix && 'post-new.php' !== $hook_suffix ) {
82 return;
83 }
84
85 $screen = get_current_screen();
86
87 // Load the assets only if the post type supports excerpts and is not an attachment.
88 if (
89 ! $screen ||
90 ! post_type_supports( $screen->post_type, 'excerpt' ) ||
91 in_array( $screen->post_type, array( 'attachment' ), true )
92 ) {
93 return;
94 }
95
96 Asset_Loader::enqueue_script( 'excerpt_generation', 'experiments/excerpt-generation', array( 'include_core_abilities' => true ) );
97 Asset_Loader::localize_script(
98 'excerpt_generation',
99 'ExcerptGenerationData',
100 array(
101 'enabled' => $this->is_enabled(),
102 'minContentLength' => get_min_content_length( 'excerpt-generation', 250 ),
103 )
104 );
105 }
106 }
107