| 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_Experiment; |
| 13 |
|
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Reference experiment demonstrating hooks and REST endpoints. |
| 20 |
* |
| 21 |
* @since 0.1.0 |
| 22 |
*/ |
| 23 |
class Example_Experiment extends Abstract_Experiment { |
| 24 |
/** |
| 25 |
* {@inheritDoc} |
| 26 |
* |
| 27 |
* @since 0.1.0 |
| 28 |
*/ |
| 29 |
protected function load_experiment_metadata(): array { |
| 30 |
return array( |
| 31 |
'id' => 'example-experiment', |
| 32 |
'label' => __( 'Example Experiment', 'ai' ), |
| 33 |
'description' => __( 'Demonstrates the AI experiment system with example hooks and functionality.', 'ai' ), |
| 34 |
); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* {@inheritDoc} |
| 39 |
* |
| 40 |
* @since 0.1.0 |
| 41 |
*/ |
| 42 |
public function register(): void { |
| 43 |
add_action( 'wp_footer', array( $this, 'add_footer_content' ), 20 ); |
| 44 |
add_filter( 'document_title_parts', array( $this, 'modify_title' ), 10, 1 ); |
| 45 |
add_action( 'rest_api_init', array( $this, 'register_rest_route' ) ); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Adds example content to the footer for logged-in users. |
| 50 |
* |
| 51 |
* @since 0.1.0 |
| 52 |
*/ |
| 53 |
public function add_footer_content(): void { |
| 54 |
if ( ! is_user_logged_in() ) { |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
echo '<!-- Example Experiment: AI Plugin Active -->'; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Modifies the document title parts when debugging. |
| 63 |
* |
| 64 |
* @since 0.1.0 |
| 65 |
* |
| 66 |
* @param array<string, string> $title Title parts. |
| 67 |
* @return array<string, string> |
| 68 |
*/ |
| 69 |
public function modify_title( array $title ): array { |
| 70 |
if ( defined( 'WP_DEBUG' ) && WP_DEBUG && isset( $title['site'] ) ) { |
| 71 |
$title['site'] = $title['site'] . ' [AI]'; |
| 72 |
} |
| 73 |
return $title; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Registers the example REST API route. |
| 78 |
* |
| 79 |
* @since 0.1.0 |
| 80 |
*/ |
| 81 |
public function register_rest_route(): void { |
| 82 |
register_rest_route( |
| 83 |
'ai/v1', |
| 84 |
'/example', |
| 85 |
array( |
| 86 |
'methods' => 'GET', |
| 87 |
'callback' => array( $this, 'rest_endpoint_callback' ), |
| 88 |
'permission_callback' => array( $this, 'rest_permission_callback' ), |
| 89 |
) |
| 90 |
); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Callback for the example REST endpoint. |
| 95 |
* |
| 96 |
* @since 0.1.0 |
| 97 |
* |
| 98 |
* @return array<string, mixed> |
| 99 |
*/ |
| 100 |
public function rest_endpoint_callback(): array { |
| 101 |
return array( |
| 102 |
'experiment_id' => $this->get_id(), |
| 103 |
'label' => $this->get_label(), |
| 104 |
'description' => $this->get_description(), |
| 105 |
'enabled' => $this->is_enabled(), |
| 106 |
'message' => __( 'Example experiment is active!', 'ai' ), |
| 107 |
); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Permission check for the REST endpoint. |
| 112 |
* |
| 113 |
* @since 0.1.0 |
| 114 |
* |
| 115 |
* @return bool |
| 116 |
*/ |
| 117 |
public function rest_permission_callback(): bool { |
| 118 |
return current_user_can( 'manage_options' ); |
| 119 |
} |
| 120 |
} |
| 121 |
|