-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathExample_Experiment.php
More file actions
116 lines (105 loc) · 2.56 KB
/
Example_Experiment.php
File metadata and controls
116 lines (105 loc) · 2.56 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
106
107
108
109
110
111
112
113
114
115
116
<?php
/**
* Example experiment implementation.
*
* @package WordPress\AI
*/
declare( strict_types=1 );
namespace WordPress\AI\Experiments\Example_Experiment;
use WordPress\AI\Abstracts\Abstract_Experiment;
/**
* Reference experiment demonstrating hooks and REST endpoints.
*
* @since 0.1.0
*/
class Example_Experiment extends Abstract_Experiment {
/**
* {@inheritDoc}
*
* @since 0.1.0
*/
protected function load_experiment_metadata(): array {
return array(
'id' => 'example-experiment',
'label' => __( 'Example Experiment', 'ai' ),
'description' => __( 'Demonstrates the AI experiment system with example hooks and functionality.', 'ai' ),
);
}
/**
* {@inheritDoc}
*
* @since 0.1.0
*/
public function init(): void {
add_action( 'wp_footer', array( $this, 'add_footer_content' ), 20 );
add_filter( 'document_title_parts', array( $this, 'modify_title' ), 10, 1 );
add_action( 'rest_api_init', array( $this, 'register_rest_route' ) );
}
/**
* Adds example content to the footer for logged-in users.
*
* @since 0.1.0
*/
public function add_footer_content(): void {
if ( ! is_user_logged_in() ) {
return;
}
echo '<!-- Example Experiment: AI Plugin Active -->';
}
/**
* Modifies the document title parts when debugging.
*
* @since 0.1.0
*
* @param array<string, string> $title Title parts.
* @return array<string, string>
*/
public function modify_title( array $title ): array {
if ( defined( 'WP_DEBUG' ) && WP_DEBUG && isset( $title['site'] ) ) {
$title['site'] = $title['site'] . ' [AI]';
}
return $title;
}
/**
* Registers the example REST API route.
*
* @since 0.1.0
*/
public function register_rest_route(): void {
register_rest_route(
'ai/v1',
'/example',
array(
'methods' => 'GET',
'callback' => array( $this, 'rest_endpoint_callback' ),
'permission_callback' => array( $this, 'rest_permission_callback' ),
)
);
}
/**
* Callback for the example REST endpoint.
*
* @since 0.1.0
*
* @return array<string, mixed>
*/
public function rest_endpoint_callback(): array {
return array(
'experiment_id' => $this->get_id(),
'label' => $this->get_label(),
'description' => $this->get_description(),
'enabled' => $this->is_enabled(),
'message' => __( 'Example experiment is active!', 'ai' ),
);
}
/**
* Permission check for the REST endpoint.
*
* @since 0.1.0
*
* @return bool
*/
public function rest_permission_callback(): bool {
return current_user_can( 'manage_options' );
}
}