| 1 |
<?php declare(strict_types=1); |
| 2 |
|
| 3 |
namespace Give\MCP\Actions; |
| 4 |
|
| 5 |
use Give\Framework\Support\Facades\Scripts\ScriptAsset; |
| 6 |
|
| 7 |
/** |
| 8 |
* Loads the GiveWP MCP server frontend assets into the dashboard. |
| 9 |
* |
| 10 |
* @since 4.9.0 |
| 11 |
*/ |
| 12 |
class RegisterMCPServer |
| 13 |
{ |
| 14 |
|
| 15 |
public const HANDLE = 'givewp-mcp-server'; |
| 16 |
|
| 17 |
/** |
| 18 |
* Register the GiveWP MCP server into Elementor's Angie SDK if the Angie plugin |
| 19 |
* is activated and the user has access. |
| 20 |
* |
| 21 |
* @action wp_enqueue_scripts |
| 22 |
* @action admin_enqueue_scripts |
| 23 |
* |
| 24 |
* @since 4.9.0 |
| 25 |
* |
| 26 |
* @return void |
| 27 |
*/ |
| 28 |
public function __invoke(): void |
| 29 |
{ |
| 30 |
// The angie plugin isn't installed or activated. |
| 31 |
if ( ! defined('ANGIE_VERSION') ) { |
| 32 |
return; |
| 33 |
} |
| 34 |
|
| 35 |
// The current user doesn't have permission to use angie. |
| 36 |
if ( ! current_user_can( 'use_angie' ) ) { |
| 37 |
return; |
| 38 |
} |
| 39 |
|
| 40 |
// If we're not logged in, no reason to include this large script. |
| 41 |
if ( ! is_user_logged_in() ) { |
| 42 |
return; |
| 43 |
} |
| 44 |
|
| 45 |
$scriptAsset = ScriptAsset::get(GIVE_PLUGIN_DIR . 'build/angieMcp.asset.php'); |
| 46 |
|
| 47 |
wp_register_script( |
| 48 |
self::HANDLE, |
| 49 |
GIVE_PLUGIN_URL . 'build/angieMcp.js', |
| 50 |
array_merge($scriptAsset['dependencies'], ['angie-app']), |
| 51 |
$scriptAsset['version'], |
| 52 |
true |
| 53 |
); |
| 54 |
|
| 55 |
wp_localize_script( self::HANDLE, 'GiveMcpServerOptions', |
| 56 |
[ |
| 57 |
'adminUrl' => admin_url(), |
| 58 |
] |
| 59 |
); |
| 60 |
|
| 61 |
wp_enqueue_script(self::HANDLE); |
| 62 |
} |
| 63 |
} |
| 64 |
|