-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsite-ai-assistant.php
306 lines (251 loc) · 10.4 KB
/
website-ai-assistant.php
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
<?php
declare(strict_types=1);
/**
* Plugin Name: Website AI Assistant
* Plugin URI:
* Description: An AI-powered chat assistant for WordPress websites using Google's Gemini API
* Version: 3.2.0
* Author: Gerald Haygood
* Author URI:
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: website-ai-assistant
* Domain Path: /languages
*
* @package WebsiteAiAssistant
*/
namespace Website_Ai_Assistant;
if (!defined('ABSPATH')) {
exit;
}
define('WAA_VERSION', '3.2.0');
define('WAA_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('WAA_PLUGIN_URL', plugin_dir_url(__FILE__));
define('WAA_PLUGIN_BASENAME', plugin_basename(__FILE__));
// Autoloader
spl_autoload_register(function ($class) {
$prefix = 'Website_Ai_Assistant\\';
// Check if the class uses our namespace
$len = strlen($prefix);
if (strncmp($prefix, $class, $len) !== 0) {
return;
}
// Get the relative class name
$relative_class = substr($class, $len);
// Replace namespace separators with directory separators
$file = str_replace('\\', '/', $relative_class);
// Map namespace parts to directories
if (strpos($file, 'Admin/') === 0) {
$file = WAA_PLUGIN_DIR . 'admin/class-' . strtolower(str_replace('Admin/', '', $file)) . '.php';
} elseif (strpos($file, 'Models/') === 0) {
$file = WAA_PLUGIN_DIR . 'includes/models/class-' . strtolower(str_replace('Models/', '', $file)) . '.php';
} else {
$file = WAA_PLUGIN_DIR . 'includes/class-' . strtolower($file) . '.php';
}
// If the file exists, require it
if (file_exists($file)) {
require $file;
}
});
// Load activation files
if (!class_exists('Website_Ai_Assistant\Activator')) {
require_once WAA_PLUGIN_DIR . 'includes/class-activator.php';
}
if (!class_exists('Website_Ai_Assistant\Deactivator')) {
require_once WAA_PLUGIN_DIR . 'includes/class-deactivator.php';
}
// Then the activation hooks
register_activation_hook(__FILE__, ['Website_Ai_Assistant\Activator', 'activate']);
register_deactivation_hook(__FILE__, ['Website_Ai_Assistant\Deactivator', 'deactivate']);
// Load Debug_Logger first since it's used by functions.php
require_once WAA_PLUGIN_DIR . 'includes/class-debug-logger.php';
require_once WAA_PLUGIN_DIR . 'includes/functions.php';
/**
* Main plugin class
*/
final class Website_AI_Assistant {
private static ?Website_AI_Assistant $instance = null;
private function __construct() {
waa_debug_log("Constructor called");
// Load plugin dependencies
$this->load_dependencies();
// Initialize admin settings if in admin area
if (is_admin()) {
waa_debug_log("Initializing admin settings");
$admin_settings = new Admin\Admin_Settings();
}
try {
// Initialize chat handler
new Chat_Handler();
// Initialize lead handler
new Lead_Handler();
} catch (\Exception $e) {
waa_debug_log('Error initializing handlers: ' . $e->getMessage());
// Continue plugin initialization despite Algolia error
}
// Add actions
add_action('plugins_loaded', [$this, 'load_plugin_textdomain']);
add_action('wp_enqueue_scripts', [$this, 'enqueue_public_assets']);
add_action('wp_footer', [$this, 'render_chat_widget']);
}
public static function get_instance(): Website_AI_Assistant {
if (null === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
private function load_dependencies(): void {
// Core utilities
require_once WAA_PLUGIN_DIR . 'includes/class-debug-logger.php';
// Admin classes
require_once WAA_PLUGIN_DIR . 'admin/class-admin-settings.php';
// Model classes
require_once WAA_PLUGIN_DIR . 'includes/models/class-base-model-service.php';
require_once WAA_PLUGIN_DIR . 'includes/models/class-openai-service.php';
require_once WAA_PLUGIN_DIR . 'includes/models/class-gemini-service.php';
require_once WAA_PLUGIN_DIR . 'includes/models/class-deepseek-service.php';
// Service classes
require_once WAA_PLUGIN_DIR . 'includes/class-algolia-service.php';
require_once WAA_PLUGIN_DIR . 'includes/class-chat-handler.php';
// Lead handler
require_once WAA_PLUGIN_DIR . 'includes/class-lead-handler.php';
require_once WAA_PLUGIN_DIR . 'includes/class-lead-handler.php';
}
public function load_plugin_textdomain(): void {
load_plugin_textdomain(
'website-ai-assistant',
false,
dirname(WAA_PLUGIN_BASENAME) . '/languages/'
);
}
public function enqueue_public_assets(): void {
wp_enqueue_style(
'waa-chat-interface',
WAA_PLUGIN_URL . 'public/css/chat-interface.css',
[],
WAA_VERSION
);
wp_enqueue_script(
'waa-chat-interface',
WAA_PLUGIN_URL . 'public/js/chat-interface.js',
['jquery'],
WAA_VERSION,
true
);
$options = get_option('waa_options', []);
waa_debug_log('Lead Collection Settings: ' . print_r([
'enabled' => !empty($options['enable_lead_collection']),
'timing' => $options['lead_collection_timing'] ?? 'immediate'
], true));
wp_localize_script('waa-chat-interface', 'waaData', [
'ajaxUrl' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('waa_chat_nonce'),
'enableLeadCollection' => !empty($options['enable_lead_collection']),
'leadCollectionTiming' => $options['lead_collection_timing'] ?? 'immediate',
'emailRequired' => __('Please enter your email address.', 'website-ai-assistant'),
'ajaxError' => __('An error occurred. Please try again.', 'website-ai-assistant'),
'privacyUrl' => $options['privacy_page_url'] ?? '',
'privacyText' => $options['privacy_text'] ?? __('By continuing, you agree to our {privacy_policy}.', 'website-ai-assistant')
]);
}
public function render_chat_widget(): void {
// Get display settings
$options = get_option('waa_options', []);
waa_debug_log('Privacy Settings: ' . print_r([
'privacy_url' => $options['privacy_page_url'] ?? 'not set',
'privacy_text' => $options['privacy_text'] ?? 'not set'
], true));
$locations = $options['display_locations'] ?? ['all'];
// Check if we should display the widget
if (!$this->should_display_chat($locations)) {
return;
}
require_once WAA_PLUGIN_DIR . 'public/templates/chat-widget.php';
}
private function should_display_chat(array $locations): bool {
// If 'all' is selected, always show
if (in_array('all', $locations)) {
return true;
}
// Check specific locations
if (in_array('home', $locations) && is_front_page()) {
return true;
}
if (in_array('posts', $locations) && is_single() && !is_singular('product')) {
return true;
}
if (in_array('pages', $locations) && is_page()) {
return true;
}
// Check for WooCommerce product pages
if (in_array('products', $locations) && is_singular('product')) {
return true;
}
return false;
}
}
// Initialize the plugin
Website_AI_Assistant::get_instance();
add_action('wp_ajax_waa_test_api', function() {
check_ajax_referer('waa_admin_nonce', 'nonce');
if (!current_user_can('manage_options')) {
wp_send_json_error(['message' => __('Unauthorized access', 'website-ai-assistant')]);
}
$api_type = sanitize_text_field($_POST['api_type'] ?? '');
$api_key = sanitize_text_field($_POST['api_key'] ?? '');
if ($api_type === 'gemini') {
// Test Gemini API
try {
// Make a simple test call to Gemini API
$response = wp_remote_post('https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=' . $api_key, [
'headers' => ['Content-Type' => 'application/json'],
'body' => json_encode([
'contents' => [
['parts' => [['text' => 'Say "Hello, testing Gemini API!"']]]
]
])
]);
if (is_wp_error($response)) {
throw new Exception($response->get_error_message());
}
$body = json_decode(wp_remote_retrieve_body($response), true);
if (isset($body['error'])) {
throw new Exception($body['error']['message']);
}
wp_send_json_success(['message' => __('Gemini API connection successful!', 'website-ai-assistant')]);
} catch (Exception $e) {
wp_send_json_error(['message' => $e->getMessage()]);
}
} elseif ($api_type === 'search') {
// Test Search API
$search_engine_id = sanitize_text_field($_POST['search_engine_id'] ?? '');
$query = sanitize_text_field($_POST['query'] ?? '');
try {
$url = add_query_arg([
'key' => $api_key,
'cx' => $search_engine_id,
'q' => $query
], 'https://www.googleapis.com/customsearch/v1');
$response = wp_remote_get($url);
if (is_wp_error($response)) {
throw new Exception($response->get_error_message());
}
$body = json_decode(wp_remote_retrieve_body($response), true);
if (isset($body['error'])) {
throw new Exception($body['error']['message']);
}
wp_send_json_success([
'message' => sprintf(
/* translators: 1: search query, 2: number of results */
__('Search API connection successful! Found %2$d results for "%1$s".', 'website-ai-assistant'),
$query,
$body['searchInformation']['totalResults'] ?? 0
)
]);
} catch (Exception $e) {
wp_send_json_error(['message' => $e->getMessage()]);
}
} else {
wp_send_json_error(['message' => __('Invalid API type', 'website-ai-assistant')]);
}
});