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

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

131 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Suggest reply experiment implementation.
4 *
5 * @package WordPress\AI
6 */
7
8 declare(strict_types=1);
9
10 namespace WordPress\AI\Experiments\Suggest_Reply;
11
12 use WordPress\AI\Abilities\Suggest_Reply\Suggest_Reply as Suggest_Reply_Ability;
13 use WordPress\AI\Abstracts\Abstract_Feature;
14 use WordPress\AI\Asset_Loader;
15 use WordPress\AI\Experiments\Experiment_Category;
16
17 // Exit if accessed directly.
18 defined( 'ABSPATH' ) || exit;
19
20 /**
21 * Suggest reply experiment.
22 *
23 * Generates reply suggestions for a comment.
24 *
25 * @since 1.2.0
26 */
27 class Suggest_Reply extends Abstract_Feature {
28
29 /**
30 * {@inheritDoc}
31 *
32 * @since 1.2.0
33 */
34 public static function get_id(): string {
35 return 'suggest-reply';
36 }
37
38 /**
39 * {@inheritDoc}
40 *
41 * @since 1.2.0
42 */
43 protected function load_metadata(): array {
44 return array(
45 'label' => __( 'Suggest Reply', 'ai' ),
46 'description' => __( 'Adds a "Suggest Reply" action to the Comments screen and Activity widget, enabling moderators to quickly generate comment reply suggestions.', 'ai' ),
47 'category' => Experiment_Category::ADMIN,
48 );
49 }
50
51 /**
52 * {@inheritDoc}
53 *
54 * @since 1.2.0
55 */
56 public function register(): void {
57 add_action( 'wp_abilities_api_init', array( $this, 'register_abilities' ) );
58 add_filter( 'comment_row_actions', array( $this, 'add_row_action' ), 10, 2 );
59 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
60 }
61
62 /**
63 * Registers the reply suggestion ability.
64 *
65 * @since 1.2.0
66 */
67 public function register_abilities(): void {
68 wp_register_ability(
69 'ai/suggest-reply',
70 array(
71 'label' => __( 'Reply Suggestion', 'ai' ),
72 'description' => __( 'Generates reply suggestions to a comment.', 'ai' ),
73 'ability_class' => Suggest_Reply_Ability::class,
74 )
75 );
76 }
77
78 /**
79 * Adds a "Suggest reply" action link to the comment row actions.
80 *
81 * @since 1.2.0
82 *
83 * @param mixed $actions The existing comment row actions.
84 * @param \WP_Comment $comment The comment object.
85 * @return array<string, string> The modified actions array.
86 */
87 public function add_row_action( $actions, $comment ): array {
88 if (
89 ! is_array( $actions ) ||
90 ! $comment ||
91 ! is_a( $comment, '\WP_Comment' )
92 ) {
93 return $actions;
94 }
95
96 $actions['wpai_suggest_reply'] = sprintf(
97 '<a href="#" class="wpai-suggest-reply" data-comment-id="%d" aria-label="%s">%s</a>',
98 absint( $comment->comment_ID ),
99 esc_attr__( 'Suggest a reply to this comment', 'ai' ),
100 esc_html__( 'Suggest reply', 'ai' )
101 );
102
103 return $actions;
104 }
105
106 /**
107 * Enqueues assets for the Suggest Reply experiment.
108 *
109 * @since 1.2.0
110 *
111 * @param string $hook_suffix The current admin page hook suffix.
112 */
113 public function enqueue_assets( string $hook_suffix ): void {
114 if ( ! in_array( $hook_suffix, array( 'edit-comments.php', 'index.php' ), true ) ) {
115 return;
116 }
117
118 Asset_Loader::enqueue_script(
119 'suggest_reply',
120 'experiments/suggest-reply',
121 array( 'include_core_abilities' => true )
122 );
123 Asset_Loader::enqueue_style( 'suggest_reply', 'experiments/suggest-reply' );
124 Asset_Loader::localize_script(
125 'suggest_reply',
126 'SuggestReplyData',
127 array( 'enabled' => $this->is_enabled() )
128 );
129 }
130 }
131