-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorbexa-agentic-commerce.php
More file actions
373 lines (311 loc) · 14.1 KB
/
orbexa-agentic-commerce.php
File metadata and controls
373 lines (311 loc) · 14.1 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
<?php
/**
* Plugin Name: ORBEXA Agentic Commerce
* Plugin URI: https://orbexa.io/wordpress
* Description: Connect your WooCommerce store to ORBEXA Open Trust Registry (OTR) for AI agentic commerce. Let AI shopping agents discover, recommend, and sell your products across ChatGPT, Gemini, Copilot, and more.
* Version: 1.0.0
* Author: ORBEXA
* Author URI: https://orbexa.io
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: orbexa-agentic-commerce
* Requires at least: 6.0
* Tested up to: 6.9
* Requires PHP: 7.4
* Requires Plugins: woocommerce
* WC requires at least: 5.0
* WC tested up to: 9.6
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
define( 'ORBEXA_AC_VERSION', '1.0.0' );
define( 'ORBEXA_AC_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'ORBEXA_AC_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'ORBEXA_AC_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
define( 'ORBEXA_AC_API_BASE', 'https://orbexa.io' );
define( 'ORBEXA_AC_SITE_BASE', 'https://orbexa.io' );
/**
* Main plugin class — singleton.
*/
final class Orbexa_Agentic_Commerce {
private static $instance = null;
public static function instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
private function __construct() {
$this->load_dependencies();
$this->init_hooks();
}
private function load_dependencies() {
require_once ORBEXA_AC_PLUGIN_DIR . 'includes/class-orbexa-crypto.php';
require_once ORBEXA_AC_PLUGIN_DIR . 'includes/class-orbexa-admin.php';
require_once ORBEXA_AC_PLUGIN_DIR . 'includes/class-orbexa-api.php';
require_once ORBEXA_AC_PLUGIN_DIR . 'includes/class-orbexa-sync.php';
require_once ORBEXA_AC_PLUGIN_DIR . 'includes/class-orbexa-domain.php';
require_once ORBEXA_AC_PLUGIN_DIR . 'includes/class-orbexa-badge.php';
require_once ORBEXA_AC_PLUGIN_DIR . 'includes/class-orbexa-webhook.php';
}
private function init_hooks() {
register_activation_hook( __FILE__, array( $this, 'activate' ) );
register_deactivation_hook( __FILE__, array( $this, 'deactivate' ) );
add_action( 'init', array( $this, 'maybe_flush_rewrite_rules' ) );
add_action( 'plugins_loaded', array( $this, 'on_plugins_loaded' ) );
add_filter( 'plugin_action_links_' . ORBEXA_AC_PLUGIN_BASENAME, array( $this, 'add_settings_link' ) );
// AJAX: one-click connect via email + password.
add_action( 'wp_ajax_orbexa_connect', array( $this, 'handle_connect' ) );
// Declare WooCommerce HPOS and Blocks compatibility.
add_action( 'before_woocommerce_init', array( $this, 'declare_wc_compatibility' ) );
}
/**
* Declare compatibility with WooCommerce HPOS and Cart/Checkout Blocks.
*/
public function declare_wc_compatibility() {
if ( class_exists( '\Automattic\WooCommerce\Utilities\FeaturesUtil' ) ) {
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'cart_checkout_blocks', __FILE__, true );
}
}
/* ─── Lifecycle ─── */
public function activate() {
// Generate secrets.
if ( ! get_option( 'orbexa_webhook_secret' ) ) {
update_option( 'orbexa_webhook_secret', wp_generate_password( 40, false ), false );
}
if ( ! get_option( 'orbexa_encryption_key' ) ) {
update_option( 'orbexa_encryption_key', wp_generate_password( 32, true, true ), false );
}
// Schedule daily sync.
if ( ! wp_next_scheduled( 'orbexa_daily_sync' ) ) {
wp_schedule_event( time() + 300, 'daily', 'orbexa_daily_sync' );
}
// Flag to flush rewrite rules on next init.
update_option( 'orbexa_flush_rules', true );
// Set activation redirect flag.
set_transient( 'orbexa_activation_redirect', true, 60 );
}
public function deactivate() {
wp_clear_scheduled_hook( 'orbexa_daily_sync' );
flush_rewrite_rules();
}
public function maybe_flush_rewrite_rules() {
if ( get_option( 'orbexa_flush_rules' ) ) {
flush_rewrite_rules();
delete_option( 'orbexa_flush_rules' );
}
}
public function on_plugins_loaded() {
// WooCommerce dependency check.
if ( ! class_exists( 'WooCommerce' ) ) {
add_action( 'admin_notices', function () {
echo '<div class="notice notice-error"><p>';
esc_html_e( 'ORBEXA Agentic Commerce requires WooCommerce to be installed and active.', 'orbexa-agentic-commerce' );
echo '</p></div>';
} );
return;
}
// Boot subsystems.
Orbexa_Admin::instance();
Orbexa_API::instance();
Orbexa_Sync::instance();
Orbexa_Domain::instance();
Orbexa_Badge::instance();
Orbexa_Webhook::instance();
// Activation redirect — send user to the onboarding page.
add_action( 'admin_init', array( $this, 'maybe_redirect_after_activation' ) );
}
/* ─── Activation Redirect ─── */
public function maybe_redirect_after_activation() {
if ( ! get_transient( 'orbexa_activation_redirect' ) ) {
return;
}
delete_transient( 'orbexa_activation_redirect' );
// Don't redirect on multi-site bulk activation or AJAX.
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Standard WP activation check.
if ( wp_doing_ajax() || is_network_admin() || isset( $_GET['activate-multi'] ) ) {
return;
}
$api_key = self::get_api_key();
if ( ! empty( $api_key ) ) {
wp_safe_redirect( admin_url( 'admin.php?page=orbexa-settings' ) );
} else {
wp_safe_redirect( admin_url( 'admin.php?page=orbexa-settings&tab=onboarding' ) );
}
exit;
}
/* ─── One-Click Connect Handler (AJAX) ─── */
/**
* Handle the AJAX connect request: email + password → platform API → store credentials.
*/
public function handle_connect() {
check_ajax_referer( 'orbexa_admin_nonce', 'nonce' );
if ( ! current_user_can( 'manage_woocommerce' ) ) {
wp_send_json_error( array( 'message' => __( 'Permission denied.', 'orbexa-agentic-commerce' ) ) );
}
$email = isset( $_POST['email'] ) ? sanitize_email( wp_unslash( $_POST['email'] ) ) : '';
$password = isset( $_POST['password'] ) ? sanitize_text_field( wp_unslash( $_POST['password'] ) ) : '';
if ( empty( $email ) || ! is_email( $email ) ) {
wp_send_json_error( array( 'message' => __( 'Please enter a valid email address.', 'orbexa-agentic-commerce' ) ) );
}
if ( empty( $password ) || strlen( $password ) < 6 ) {
wp_send_json_error( array( 'message' => __( 'Password must be at least 6 characters.', 'orbexa-agentic-commerce' ) ) );
}
$domain = wp_parse_url( home_url(), PHP_URL_HOST );
$store_url = home_url();
// Call the platform's one-click connect endpoint.
$response = wp_remote_post(
trailingslashit( ORBEXA_AC_API_BASE ) . 'api/integrations/wordpress/connect',
array(
'timeout' => 30,
'sslverify' => true,
'headers' => array(
'Content-Type' => 'application/json',
'X-Plugin-Platform' => 'wordpress',
'X-Plugin-Version' => ORBEXA_AC_VERSION,
),
'body' => wp_json_encode( array(
'email' => $email,
'password' => $password,
'store_url' => $store_url,
'domain' => $domain,
'platform' => 'woocommerce',
'plugin_version' => ORBEXA_AC_VERSION,
) ),
)
);
if ( is_wp_error( $response ) ) {
wp_send_json_error( array( 'message' => $response->get_error_message() ) );
}
$code = wp_remote_retrieve_response_code( $response );
$body = json_decode( wp_remote_retrieve_body( $response ), true );
if ( $code !== 200 || empty( $body['success'] ) ) {
$msg = $body['error'] ?? $body['message'] ?? __( 'Connection failed. Please check your credentials.', 'orbexa-agentic-commerce' );
wp_send_json_error( array( 'message' => $msg ) );
}
// Store credentials securely.
$api_key = $body['api_key'] ?? '';
if ( ! empty( $api_key ) ) {
self::set_api_key( $api_key );
}
$merchant_id = absint( $body['merchant_id'] ?? 0 );
if ( $merchant_id ) {
update_option( 'orbexa_merchant_id', $merchant_id );
}
update_option( 'orbexa_brand_domain', $domain );
if ( ! empty( $body['verification_code'] ) ) {
update_option( 'orbexa_verification_code', sanitize_text_field( $body['verification_code'] ) );
}
if ( ! empty( $body['webhook_secret'] ) ) {
update_option( 'orbexa_webhook_secret', sanitize_text_field( $body['webhook_secret'] ), false );
}
if ( ! empty( $body['merchant_name'] ) ) {
update_option( 'orbexa_merchant_name', sanitize_text_field( $body['merchant_name'] ) );
}
// Store trust data if returned.
if ( isset( $body['trust_score'] ) ) {
update_option( 'orbexa_trust_score', intval( $body['trust_score'] ) );
}
if ( ! empty( $body['badge'] ) ) {
update_option( 'orbexa_badge', sanitize_text_field( $body['badge'] ) );
}
if ( ! empty( $body['otr_listed'] ) ) {
update_option( 'orbexa_otr_listed', true );
}
wp_send_json_success( array(
'message' => __( 'Connected to ORBEXA!', 'orbexa-agentic-commerce' ),
'trust_score' => $body['trust_score'] ?? null,
'badge' => $body['badge'] ?? 'UNRATED',
'merchant_id' => $merchant_id,
) );
}
/* ─── Plugin Action Links ─── */
public function add_settings_link( $links ) {
$url = admin_url( 'admin.php?page=orbexa-settings' );
$link = '<a href="' . esc_url( $url ) . '">' . esc_html__( 'Settings', 'orbexa-agentic-commerce' ) . '</a>';
array_unshift( $links, $link );
return $links;
}
/* ─── Encrypted API Key Helpers ─── */
public static function get_api_key() {
$encrypted = get_option( 'orbexa_api_key_enc', '' );
if ( empty( $encrypted ) ) {
// Fallback: check for legacy unencrypted key and migrate.
$plain = get_option( 'orbexa_api_key', '' );
if ( ! empty( $plain ) ) {
self::set_api_key( $plain );
delete_option( 'orbexa_api_key' );
return $plain;
}
return '';
}
return Orbexa_Crypto::decrypt( $encrypted );
}
public static function set_api_key( $plain_key ) {
$encrypted = Orbexa_Crypto::encrypt( $plain_key );
update_option( 'orbexa_api_key_enc', $encrypted, false );
// Remove any legacy plain key.
delete_option( 'orbexa_api_key' );
}
/* ─── Signed Platform Request ─── */
public static function remote_request( $endpoint, $args = array() ) {
$api_key = self::get_api_key();
if ( empty( $api_key ) ) {
return new WP_Error( 'no_api_key', __( 'ORBEXA API key is not configured. Please connect your store first.', 'orbexa-agentic-commerce' ) );
}
$url = trailingslashit( ORBEXA_AC_API_BASE ) . ltrim( $endpoint, '/' );
// Generate request signature for integrity.
$timestamp = time();
$nonce = wp_generate_password( 16, false );
$merchant = get_option( 'orbexa_merchant_id', '' );
$sign_data = $timestamp . '.' . $nonce . '.' . $endpoint;
$signature = hash_hmac( 'sha256', $sign_data, $api_key );
$defaults = array(
'timeout' => 30,
'headers' => array(
'Authorization' => 'Bearer ' . $api_key,
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'X-Plugin-Version' => ORBEXA_AC_VERSION,
'X-Store-URL' => home_url(),
'X-Merchant-Id' => $merchant,
'X-Timestamp' => $timestamp,
'X-Nonce' => $nonce,
'X-Signature' => $signature,
),
);
$args = wp_parse_args( $args, $defaults );
$args['headers'] = array_merge( $defaults['headers'], $args['headers'] ?? array() );
// Ensure SSL verification is on.
$args['sslverify'] = true;
$response = wp_remote_request( $url, $args );
if ( is_wp_error( $response ) ) {
return $response;
}
$code = wp_remote_retrieve_response_code( $response );
$body = json_decode( wp_remote_retrieve_body( $response ), true );
if ( $code === 401 ) {
return new WP_Error( 'auth_expired', __( 'API key expired or invalid. Please reconnect.', 'orbexa-agentic-commerce' ) );
}
if ( $code >= 400 ) {
$message = $body['message'] ?? __( 'Unknown API error', 'orbexa-agentic-commerce' );
return new WP_Error( 'api_error', $message, array( 'status' => $code ) );
}
return $body;
}
/**
* Check if the plugin is fully connected and operational.
*/
public static function is_connected() {
return ! empty( self::get_api_key() ) && ! empty( get_option( 'orbexa_merchant_id' ) );
}
}
/**
* Boot — earliest possible priority.
*/
function orbexa_agentic_commerce() {
return Orbexa_Agentic_Commerce::instance();
}
add_action( 'plugins_loaded', 'orbexa_agentic_commerce', 5 );