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

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

101 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Content resizing experiment implementation.
4 *
5 * @package WordPress\AI
6 */
7
8 declare( strict_types=1 );
9
10 namespace WordPress\AI\Experiments\Content_Resizing;
11
12 use WordPress\AI\Abilities\Content_Resizing\Content_Resizing as Content_Resizing_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 // Exit if accessed directly.
20 if ( ! defined( 'ABSPATH' ) ) {
21 exit;
22 }
23
24 /**
25 * Content resizing experiment.
26 *
27 * @since 0.9.0
28 */
29 class Content_Resizing extends Abstract_Feature {
30
31 /**
32 * {@inheritDoc}
33 */
34 public static function get_id(): string {
35 return 'content-resizing';
36 }
37
38 /**
39 * {@inheritDoc}
40 */
41 protected function load_metadata(): array {
42 return array(
43 'label' => __( 'Content Resizing', 'ai' ),
44 'description' => __( 'Shorten, expand, or rephrase selected block content. Requires an AI connector that includes support for text generation models.', 'ai' ),
45 'category' => Experiment_Category::EDITOR,
46 );
47 }
48
49 /**
50 * {@inheritDoc}
51 */
52 public function register(): void {
53 add_action( 'wp_abilities_api_init', array( $this, 'register_abilities' ) );
54 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
55 }
56
57 /**
58 * Registers any needed abilities.
59 *
60 * @since 0.9.0
61 */
62 public function register_abilities(): void {
63 wp_register_ability(
64 'ai/' . $this->get_id(),
65 array(
66 'label' => $this->get_label(),
67 'description' => $this->get_description(),
68 'ability_class' => Content_Resizing_Ability::class,
69 ),
70 );
71 }
72
73 /**
74 * Enqueues and localizes the admin script.
75 *
76 * @since 0.9.0
77 *
78 * @param string $hook_suffix The current admin page hook suffix.
79 */
80 public function enqueue_assets( string $hook_suffix ): void {
81 // Load asset in new post and edit post screens only.
82 if ( 'post.php' !== $hook_suffix && 'post-new.php' !== $hook_suffix ) {
83 return;
84 }
85
86 Asset_Loader::enqueue_script( 'content_resizing', 'experiments/content-resizing', array( 'include_core_abilities' => true ) );
87 Asset_Loader::enqueue_style( 'content_resizing', 'experiments/content-resizing' );
88 Asset_Loader::localize_script(
89 'content_resizing',
90 'ContentResizingData',
91 array(
92 'enabled' => $this->is_enabled(),
93 'minContentLength' => get_min_content_length(
94 'content-resizing',
95 Content_Resizing_Ability::DEFAULT_MIN_CONTENT_LENGTH
96 ),
97 )
98 );
99 }
100 }
101