-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagenium-agent.php
More file actions
153 lines (132 loc) · 5.19 KB
/
Copy pathagenium-agent.php
File metadata and controls
153 lines (132 loc) · 5.19 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
/**
* Plugin Name: Agenium Agent
* Plugin URI: https://agenium.net/wordpress-plugin
* Description: Turns any WordPress site into an agent:// protocol endpoint. Register, discover, and communicate with other agents on the Agenium network.
* Version: 1.0.0
* Requires at least: 6.0
* Requires PHP: 8.0
* Author: Agenium
* Author URI: https://agenium.net
* License: MIT
* License URI: https://opensource.org/licenses/MIT
* Text Domain: agenium-agent
* Domain Path: /languages
*/
defined( 'ABSPATH' ) || exit;
define( 'AGENIUM_AGENT_VERSION', '1.0.0' );
define( 'AGENIUM_AGENT_FILE', __FILE__ );
define( 'AGENIUM_AGENT_DIR', plugin_dir_path( __FILE__ ) );
define( 'AGENIUM_AGENT_URL', plugin_dir_url( __FILE__ ) );
define( 'AGENIUM_DNS_SERVER', 'https://dns.agenium.net' );
// Autoload classes.
require_once AGENIUM_AGENT_DIR . 'includes/class-settings.php';
require_once AGENIUM_AGENT_DIR . 'includes/class-registrar.php';
require_once AGENIUM_AGENT_DIR . 'includes/class-agent-card.php';
require_once AGENIUM_AGENT_DIR . 'includes/class-message-handler.php';
require_once AGENIUM_AGENT_DIR . 'includes/class-resolver.php';
require_once AGENIUM_AGENT_DIR . 'includes/class-rest-api.php';
require_once AGENIUM_AGENT_DIR . 'includes/class-integration-loader.php';
/**
* Main plugin class.
*/
final class Agenium_Agent {
private static ?self $instance = null;
public Agenium_Settings $settings;
public Agenium_Registrar $registrar;
public Agenium_Agent_Card $agent_card;
public Agenium_Message_Handler $message_handler;
public Agenium_Resolver $resolver;
public Agenium_REST_API $rest_api;
public Agenium_Integration_Loader $integration_loader;
public static function instance(): self {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
private function __construct() {
$this->settings = new Agenium_Settings();
$this->registrar = new Agenium_Registrar();
$this->agent_card = new Agenium_Agent_Card();
$this->message_handler = new Agenium_Message_Handler();
$this->resolver = new Agenium_Resolver();
$this->rest_api = new Agenium_REST_API();
$this->integration_loader = new Agenium_Integration_Loader();
add_action( 'init', [ $this, 'load_textdomain' ] );
add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_public_assets' ] );
add_shortcode( 'agenium_chat', [ $this, 'render_chat_shortcode' ] );
// Dashboard widget.
add_action( 'wp_dashboard_setup', [ $this, 'register_dashboard_widget' ] );
}
public function load_textdomain(): void {
load_plugin_textdomain( 'agenium-agent', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}
public function enqueue_public_assets(): void {
if ( ! is_singular() ) {
return;
}
global $post;
if ( $post && has_shortcode( $post->post_content, 'agenium_chat' ) ) {
wp_enqueue_style( 'agenium-chat', AGENIUM_AGENT_URL . 'public/chat-widget.css', [], AGENIUM_AGENT_VERSION );
wp_enqueue_script( 'agenium-chat', AGENIUM_AGENT_URL . 'public/chat-widget.js', [], AGENIUM_AGENT_VERSION, true );
wp_localize_script( 'agenium-chat', 'ageniumChat', [
'restUrl' => esc_url_raw( rest_url( 'agenium/v1/' ) ),
'nonce' => wp_create_nonce( 'wp_rest' ),
] );
}
}
/**
* [agenium_chat agent="search.agenium"] shortcode.
*/
public function render_chat_shortcode( array $atts ): string {
$atts = shortcode_atts( [
'agent' => '',
'placeholder' => __( 'Type a message…', 'agenium-agent' ),
'height' => '400px',
], $atts, 'agenium_chat' );
if ( empty( $atts['agent'] ) ) {
return '<p class="agenium-error">' . esc_html__( 'Missing agent attribute.', 'agenium-agent' ) . '</p>';
}
$agent_uri = 'agent://' . sanitize_text_field( $atts['agent'] );
return sprintf(
'<div class="agenium-chat-widget" data-agent="%s" style="height:%s">'
. '<div class="agenium-chat-messages"></div>'
. '<form class="agenium-chat-form">'
. '<input type="text" class="agenium-chat-input" placeholder="%s" autocomplete="off" />'
. '<button type="submit" class="agenium-chat-send">%s</button>'
. '</form></div>',
esc_attr( $agent_uri ),
esc_attr( $atts['height'] ),
esc_attr( $atts['placeholder'] ),
esc_html__( 'Send', 'agenium-agent' )
);
}
public function register_dashboard_widget(): void {
wp_add_dashboard_widget(
'agenium_agent_status',
__( 'Agenium Agent Status', 'agenium-agent' ),
function () {
require AGENIUM_AGENT_DIR . 'admin/dashboard-widget.php';
}
);
}
}
// Boot.
add_action( 'plugins_loaded', [ 'Agenium_Agent', 'instance' ] );
// Activation: auto-register with DNS.
register_activation_hook( __FILE__, function () {
// Schedule registration after options are available.
add_option( 'agenium_agent_name', wp_parse_url( home_url(), PHP_URL_HOST ) );
add_option( 'agenium_api_key', '' );
add_option( 'agenium_capabilities', "general\nsearch" );
add_option( 'agenium_messages_log', [] );
// Attempt registration.
$registrar = new Agenium_Registrar();
$registrar->register();
} );
// Deactivation: optionally unregister.
register_deactivation_hook( __FILE__, function () {
$registrar = new Agenium_Registrar();
$registrar->unregister();
} );