| 1 |
<?php |
| 2 |
/** |
| 3 |
* Admin page hosting the Connector Approval UI. |
| 4 |
* |
| 5 |
* @package WordPress\AI\Experiments\Connector_Approval |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace WordPress\AI\Experiments\Connector_Approval; |
| 11 |
|
| 12 |
use WordPress\AI\Asset_Loader; |
| 13 |
|
| 14 |
// Exit if accessed directly. |
| 15 |
defined( 'ABSPATH' ) || exit; |
| 16 |
|
| 17 |
/** |
| 18 |
* Registers and renders the Connector Approval admin page. |
| 19 |
* |
| 20 |
* @since 1.0.0 |
| 21 |
*/ |
| 22 |
final class Admin_Page { |
| 23 |
/** |
| 24 |
* Menu slug used by the admin page. |
| 25 |
* |
| 26 |
* @since 1.0.0 |
| 27 |
* |
| 28 |
* @var string |
| 29 |
*/ |
| 30 |
public const PAGE_SLUG = 'ai-connector-approval'; |
| 31 |
|
| 32 |
/** |
| 33 |
* Parent menu used to anchor this page. |
| 34 |
* |
| 35 |
* @since 1.0.0 |
| 36 |
* |
| 37 |
* @var string |
| 38 |
*/ |
| 39 |
private const PARENT_SLUG = 'tools.php'; |
| 40 |
|
| 41 |
/** |
| 42 |
* Expected `load-*` hook suffix for this page. |
| 43 |
* |
| 44 |
* @since 1.0.0 |
| 45 |
* |
| 46 |
* @var string |
| 47 |
*/ |
| 48 |
private const HOOK_SUFFIX = 'tools_page_ai-connector-approval'; |
| 49 |
|
| 50 |
/** |
| 51 |
* Registers the admin menu entry and asset enqueueing. |
| 52 |
* |
| 53 |
* @since 1.0.0 |
| 54 |
*/ |
| 55 |
public function register(): void { |
| 56 |
add_action( 'admin_menu', array( $this, 'add_submenu' ) ); |
| 57 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) ); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Returns the absolute admin URL for this page. |
| 62 |
* |
| 63 |
* @since 1.0.0 |
| 64 |
* |
| 65 |
* @return string |
| 66 |
*/ |
| 67 |
public static function url(): string { |
| 68 |
return admin_url( 'tools.php?page=' . self::PAGE_SLUG ); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Adds the submenu under Settings. |
| 73 |
* |
| 74 |
* @since 1.0.0 |
| 75 |
*/ |
| 76 |
public function add_submenu(): void { |
| 77 |
add_submenu_page( |
| 78 |
self::PARENT_SLUG, |
| 79 |
__( 'Connector Approvals', 'ai' ), |
| 80 |
__( 'Connector Approvals', 'ai' ), |
| 81 |
'manage_options', |
| 82 |
self::PAGE_SLUG, |
| 83 |
array( $this, 'render' ) |
| 84 |
); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Enqueues the admin page's script and styles. |
| 89 |
* |
| 90 |
* @since 1.0.0 |
| 91 |
* |
| 92 |
* @param string $hook_suffix The current admin page hook suffix. |
| 93 |
*/ |
| 94 |
public function enqueue_assets( string $hook_suffix ): void { |
| 95 |
if ( self::HOOK_SUFFIX !== $hook_suffix ) { |
| 96 |
return; |
| 97 |
} |
| 98 |
|
| 99 |
Asset_Loader::enqueue_script( 'connector_approval', 'experiments/connector-approval' ); |
| 100 |
Asset_Loader::enqueue_style( 'connector_approval', 'experiments/connector-approval' ); |
| 101 |
Asset_Loader::localize_script( |
| 102 |
'connector_approval', |
| 103 |
'ConnectorApproval', |
| 104 |
array( |
| 105 |
'restUrl' => esc_url_raw( rest_url( 'ai/v1/connector-approvals' ) ), |
| 106 |
'nonce' => wp_create_nonce( 'wp_rest' ), |
| 107 |
) |
| 108 |
); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Renders the page container. |
| 113 |
* |
| 114 |
* The actual UI is rendered by the React app mounted into the container. |
| 115 |
* |
| 116 |
* @since 1.0.0 |
| 117 |
*/ |
| 118 |
public function render(): void { |
| 119 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 120 |
wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'ai' ) ); |
| 121 |
} |
| 122 |
|
| 123 |
echo '<div class="wrap">'; |
| 124 |
echo '<h1>' . esc_html__( 'Connector Approvals', 'ai' ) . '</h1>'; |
| 125 |
echo '<p>' . esc_html__( 'Control which plugins and themes are allowed to use each AI connector on this site. Prompts from unapproved callers are prevented and listed below for review.', 'ai' ) . '</p>'; |
| 126 |
echo '<div id="ai-connector-approval-root"></div>'; |
| 127 |
echo '</div>'; |
| 128 |
} |
| 129 |
} |
| 130 |
|