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

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

125 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Example experiment implementation.
4 *
5 * @package WordPress\AI
6 */
7
8 declare( strict_types=1 );
9
10 namespace WordPress\AI\Experiments\Example_Experiment;
11
12 use WordPress\AI\Abstracts\Abstract_Feature;
13 use WordPress\AI\Experiments\Experiment_Category;
14
15 if ( ! defined( 'ABSPATH' ) ) {
16 exit;
17 }
18
19 /**
20 * Reference experiment demonstrating hooks and REST endpoints.
21 *
22 * @since 0.1.0
23 */
24 class Example_Experiment extends Abstract_Feature {
25 /**
26 * {@inheritDoc}
27 */
28 public static function get_id(): string {
29 return 'example-experiment';
30 }
31
32 /**
33 * {@inheritDoc}
34 */
35 protected function load_metadata(): array {
36 return array(
37 'label' => __( 'Example Experiment', 'ai' ),
38 'description' => __( 'Demonstrates the AI experiment system with example hooks and functionality.', 'ai' ),
39 'category' => Experiment_Category::ADMIN,
40 );
41 }
42
43 /**
44 * {@inheritDoc}
45 */
46 public function register(): void {
47 add_action( 'wp_footer', array( $this, 'add_footer_content' ), 20 );
48 add_filter( 'document_title_parts', array( $this, 'modify_title' ), 10, 1 );
49 add_action( 'rest_api_init', array( $this, 'register_rest_route' ) );
50 }
51
52 /**
53 * Adds example content to the footer for logged-in users.
54 *
55 * @since 0.1.0
56 */
57 public function add_footer_content(): void {
58 if ( ! is_user_logged_in() ) {
59 return;
60 }
61
62 echo '<!-- Example Experiment: AI Plugin Active -->';
63 }
64
65 /**
66 * Modifies the document title parts when debugging.
67 *
68 * @since 0.1.0
69 *
70 * @param array<string, string> $title Title parts.
71 * @return array<string, string> Modified title parts.
72 */
73 public function modify_title( array $title ): array {
74 if ( defined( 'WP_DEBUG' ) && WP_DEBUG && isset( $title['site'] ) ) {
75 $title['site'] = $title['site'] . ' [AI]';
76 }
77 return $title;
78 }
79
80 /**
81 * Registers the example REST API route.
82 *
83 * @since 0.1.0
84 */
85 public function register_rest_route(): void {
86 register_rest_route(
87 'ai/v1',
88 '/example',
89 array(
90 'methods' => 'GET',
91 'callback' => array( $this, 'rest_endpoint_callback' ),
92 'permission_callback' => array( $this, 'rest_permission_callback' ),
93 )
94 );
95 }
96
97 /**
98 * Callback for the example REST endpoint.
99 *
100 * @since 0.1.0
101 *
102 * @return array<string, mixed> Response data.
103 */
104 public function rest_endpoint_callback(): array {
105 return array(
106 'experiment_id' => $this->get_id(),
107 'label' => $this->get_label(),
108 'description' => $this->get_description(),
109 'enabled' => $this->is_enabled(),
110 'message' => __( 'Example experiment is active!', 'ai' ),
111 );
112 }
113
114 /**
115 * Permission check for the REST endpoint.
116 *
117 * @since 0.1.0
118 *
119 * @return bool True if the user has permission, false otherwise.
120 */
121 public function rest_permission_callback(): bool {
122 return current_user_can( 'manage_options' );
123 }
124 }
125