-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdewey.php
More file actions
606 lines (536 loc) · 17.1 KB
/
Copy pathdewey.php
File metadata and controls
606 lines (536 loc) · 17.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
<?php
/**
* Plugin Name: Dewey AI Search Assistant
* Plugin URI: https://github.com/regionally-famous/dewey
* Description: Dewey brings live archive query and AI answers into wp-admin, with citations and built-in guardrails powered by WordPress 7.0 core capabilities.
* Version: 1.0.23
* Requires at least: 7.0
* Requires PHP: 8.1
* Author: Regionally Famous
* Author URI: https://regionallyfamous.com
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: dewey
* Domain Path: /languages
*
* @package Dewey
*/
defined( 'ABSPATH' ) || exit;
define( 'DEWEY_VERSION', '1.0.23' );
define( 'DEWEY_PLUGIN_FILE', __FILE__ );
define( 'DEWEY_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'DEWEY_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
require_once DEWEY_PLUGIN_DIR . 'includes/class-dewey-settings.php';
require_once DEWEY_PLUGIN_DIR . 'includes/class-dewey-intent-router.php';
require_once DEWEY_PLUGIN_DIR . 'includes/class-dewey-action-handler.php';
require_once DEWEY_PLUGIN_DIR . 'includes/class-dewey-indexer.php';
require_once DEWEY_PLUGIN_DIR . 'includes/class-dewey-knowledge.php';
require_once DEWEY_PLUGIN_DIR . 'includes/class-dewey-engine.php';
require_once DEWEY_PLUGIN_DIR . 'includes/class-dewey-rest-controller.php';
/**
* Register script and style handles for admin usage.
*
* @return void
*/
function dewey_register_assets() {
$asset_path = DEWEY_PLUGIN_DIR . 'build/index.asset.php';
$asset = file_exists( $asset_path )
? require $asset_path
: array(
'dependencies' => array( 'wp-element', 'wp-i18n' ),
'version' => DEWEY_VERSION,
);
$asset = dewey_normalize_asset_meta( $asset );
wp_register_script(
'dewey-admin',
DEWEY_PLUGIN_URL . 'build/index.js',
$asset['dependencies'],
$asset['version'],
true
);
wp_register_style(
'dewey-admin',
DEWEY_PLUGIN_URL . 'build/index.css',
array( 'wp-components' ),
$asset['version']
);
}
add_action( 'admin_init', 'dewey_register_assets' );
/**
* Normalize asset metadata loaded from build/index.asset.php.
*
* @param mixed $asset Asset metadata payload.
* @return array
*/
function dewey_normalize_asset_meta( $asset ) {
$defaults = array(
'dependencies' => array( 'wp-element', 'wp-i18n' ),
'version' => DEWEY_VERSION,
);
if ( ! is_array( $asset ) ) {
return $defaults;
}
$deps = $asset['dependencies'] ?? $defaults['dependencies'];
if ( ! is_array( $deps ) ) {
$deps = $defaults['dependencies'];
}
$deps = array_values(
array_filter(
array_map(
static function ( $value ) {
if ( ! is_scalar( $value ) ) {
return '';
}
return sanitize_key( (string) $value );
},
$deps
)
)
);
if ( empty( $deps ) ) {
$deps = $defaults['dependencies'];
}
$version = $asset['version'] ?? $defaults['version'];
if ( ! is_scalar( $version ) ) {
$version = $defaults['version'];
}
return array(
'dependencies' => $deps,
'version' => sanitize_text_field( (string) $version ),
);
}
/**
* Determine whether Dewey assets should load on the current admin page.
*
* @param string $hook_suffix Current admin hook suffix.
* @return bool
*/
function dewey_should_enqueue_admin_assets( string $hook_suffix ): bool {
if ( wp_doing_ajax() || wp_doing_cron() ) {
return false;
}
if ( ! current_user_can( 'edit_posts' ) ) {
return false;
}
$allowed_hooks = apply_filters(
'dewey_allowed_admin_hooks',
array(
'index.php',
'edit.php',
'post.php',
'post-new.php',
'upload.php',
'edit-tags.php',
'term.php',
'site-editor.php',
)
);
$allowed_hooks = is_array( $allowed_hooks ) ? $allowed_hooks : array();
$is_allowed_hook = in_array( $hook_suffix, $allowed_hooks, true );
$is_allowed_hook = $is_allowed_hook || 0 === strpos( $hook_suffix, 'toplevel_page_' );
$is_allowed_hook = $is_allowed_hook || 0 === strpos( $hook_suffix, 'dewey_page_' );
$is_allowed_hook = $is_allowed_hook || 0 === strpos( $hook_suffix, 'settings_page_' );
$is_allowed_hook = $is_allowed_hook || 0 === strpos( $hook_suffix, 'tools_page_' );
if ( ! $is_allowed_hook ) {
return false;
}
return (bool) apply_filters( 'dewey_can_enqueue_admin_assets', true );
}
/**
* Load Dewey assets across wp-admin screens.
*
* @return void
*/
function dewey_enqueue_admin_assets( string $hook_suffix ) {
if ( ! is_admin() ) {
return;
}
if ( ! dewey_should_enqueue_admin_assets( $hook_suffix ) ) {
return;
}
if ( ! wp_script_is( 'dewey-admin', 'registered' ) || ! wp_style_is( 'dewey-admin', 'registered' ) ) {
return;
}
wp_enqueue_script( 'dewey-admin' );
wp_enqueue_style( 'dewey-admin' );
wp_set_script_translations(
'dewey-admin',
'dewey',
DEWEY_PLUGIN_DIR . 'languages'
);
// wp_add_inline_script + wp_json_encode preserves PHP types (bool, int, etc.)
// wp_localize_script must be avoided here because it coerces all values to
// strings — PHP false becomes "" — which breaks truthiness checks in JS.
$current_user = wp_get_current_user();
wp_add_inline_script(
'dewey-admin',
'window.deweyConfig = ' . wp_json_encode(
array(
'restBase' => esc_url_raw( rest_url( 'dewey/v1' ) ),
'nonce' => wp_create_nonce( 'wp_rest' ),
'aiConnected' => dewey_ai_connection_status(),
'connectorsUrl' => esc_url_raw( dewey_get_connectors_admin_url() ),
'debugEnabled' => current_user_can( 'manage_options' ),
'citationStyle' => (string) ( Dewey_Settings::get_all()['citation_style'] ?? 'titles' ),
'currentUser' => sanitize_text_field( (string) ( $current_user->display_name ?? '' ) ),
'screenContext' => dewey_build_admin_screen_context(),
)
) . ';',
'before'
);
}
add_action( 'admin_enqueue_scripts', 'dewey_enqueue_admin_assets' );
add_action( 'rest_api_init', array( 'Dewey_REST_Controller', 'register_routes' ) );
/**
* Build normalized wp-admin screen context for Dewey.
*
* This context is passed to the frontend and then to the REST query payload so
* Dewey can reason about the current screen with structured signals.
*
* @return array<string,mixed>
*/
function dewey_build_admin_screen_context(): array {
$context = array(
'id' => '',
'base' => '',
'parent_base' => '',
'post_type' => '',
'taxonomy' => '',
'title' => '',
'capabilities' => array(
'edit_posts' => current_user_can( 'edit_posts' ),
'manage_options' => current_user_can( 'manage_options' ),
'list_users' => current_user_can( 'list_users' ),
),
'stats' => array(),
);
if ( ! function_exists( 'get_current_screen' ) ) {
return $context;
}
$screen = get_current_screen();
if ( ! class_exists( 'WP_Screen' ) || ! $screen instanceof WP_Screen ) {
return $context;
}
$context['id'] = sanitize_key( (string) $screen->id );
$context['base'] = sanitize_key( (string) $screen->base );
$context['parent_base'] = sanitize_key( (string) $screen->parent_base );
$context['post_type'] = sanitize_key( (string) $screen->post_type );
$context['taxonomy'] = sanitize_key( (string) $screen->taxonomy );
if ( function_exists( 'get_admin_page_title' ) ) {
$context['title'] = sanitize_text_field( (string) get_admin_page_title() );
}
$base = (string) $context['base'];
if ( 0 === strpos( $base, 'settings_page' ) ) {
$context['stats'] = array(
'site_title' => sanitize_text_field( (string) get_option( 'blogname', '' ) ),
'timezone' => sanitize_text_field( (string) get_option( 'timezone_string', '' ) ),
'posts_per_page' => (int) get_option( 'posts_per_page', 10 ),
'default_category' => (int) get_option( 'default_category', 0 ),
);
return $context;
}
switch ( $base ) {
case 'dashboard':
$post_counts = wp_count_posts( 'post' );
$context['stats'] = array(
'published_posts' => isset( $post_counts->publish ) ? (int) $post_counts->publish : 0,
);
break;
case 'plugins':
if ( function_exists( 'get_plugins' ) ) {
$plugins = get_plugins();
$active_plugins = (array) get_option( 'active_plugins', array() );
$context['stats'] = array(
'total_plugins' => count( $plugins ),
'active_plugins' => count( $active_plugins ),
);
}
break;
case 'themes':
if ( function_exists( 'wp_get_themes' ) ) {
$themes = wp_get_themes();
$active = wp_get_theme();
$context['stats'] = array(
'total_themes' => is_array( $themes ) ? count( $themes ) : 0,
'active_theme' => sanitize_text_field( (string) $active->get( 'Name' ) ),
);
}
break;
case 'users':
if ( function_exists( 'count_users' ) ) {
$users = count_users();
$total = is_array( $users ) ? (int) ( $users['total_users'] ?? 0 ) : 0;
$context['stats'] = array(
'total_users' => $total,
);
}
break;
case 'upload':
$attachment_counts = wp_count_posts( 'attachment' );
$context['stats'] = array(
'media_items' => isset( $attachment_counts->inherit ) ? (int) $attachment_counts->inherit : 0,
);
break;
case 'edit':
case 'post':
$post_type = '' !== $context['post_type'] ? $context['post_type'] : 'post';
$counts = wp_count_posts( $post_type );
$context['stats'] = array(
'published' => isset( $counts->publish ) ? (int) $counts->publish : 0,
'draft' => isset( $counts->draft ) ? (int) $counts->draft : 0,
);
break;
case 'options-general':
$context['stats'] = array(
'site_title' => sanitize_text_field( (string) get_option( 'blogname', '' ) ),
'timezone' => sanitize_text_field( (string) get_option( 'timezone_string', '' ) ),
'posts_per_page' => (int) get_option( 'posts_per_page', 10 ),
'default_category' => (int) get_option( 'default_category', 0 ),
);
break;
}
return $context;
}
/**
* Get the wp-admin URL for the Connectors screen.
*
* @return string
*/
function dewey_get_connectors_admin_url() {
$url = apply_filters(
'dewey_connectors_admin_url',
admin_url( 'options-general.php?page=connectors-wp-admin' )
);
if ( ! is_string( $url ) || '' === trim( $url ) ) {
return admin_url( 'options-general.php?page=connectors-wp-admin' );
}
return $url;
}
/**
* Detect whether a value contains any non-empty scalar leaf.
*
* @param mixed $value Value to inspect recursively.
* @return bool
*/
function dewey_has_non_empty_secret_value( $value ): bool {
if ( is_string( $value ) ) {
return '' !== trim( $value );
}
if ( is_numeric( $value ) ) {
return '' !== trim( (string) $value );
}
if ( is_array( $value ) ) {
foreach ( $value as $child ) {
if ( dewey_has_non_empty_secret_value( $child ) ) {
return true;
}
}
}
return false;
}
/**
* Build safe diagnostics for AI connection checks (no secrets exposed).
*
* @return array<string,mixed>
*/
function dewey_ai_connection_debug(): array {
$connector_option_keys = apply_filters(
'dewey_connector_option_keys',
array(
'connectors_ai_openai_api_key',
'connectors_ai_anthropic_api_key',
'connectors_ai_gemini_api_key',
'connectors_ai_google_api_key',
'connectors_openai_api_key',
'connectors_anthropic_api_key',
'connectors_gemini_api_key',
)
);
$connector_option_keys = is_array( $connector_option_keys ) ? $connector_option_keys : array();
$option_checks = array();
$detected_option_key = '';
foreach ( $connector_option_keys as $option_key ) {
if ( ! is_string( $option_key ) || '' === trim( $option_key ) ) {
continue;
}
$value = get_option( $option_key, '' );
$has_credential = dewey_has_non_empty_secret_value( $value );
if ( '' === $detected_option_key && $has_credential ) {
$detected_option_key = $option_key;
}
$option_checks[] = array(
'key' => $option_key,
'type' => gettype( $value ),
'has_credential' => $has_credential,
);
}
$credentials = get_option( 'wp_ai_client_provider_credentials', array() );
$legacy_has_any = dewey_has_non_empty_secret_value( $credentials );
$legacy_provider_keys = is_array( $credentials ) ? array_keys( $credentials ) : array();
$core_helper_available = function_exists( 'wp_ai_has_connected_provider' );
$core_helper_result = null;
$core_helper_error = '';
if ( $core_helper_available ) {
try {
$core_helper_result = (bool) wp_ai_has_connected_provider();
} catch ( Throwable $e ) {
$core_helper_error = sanitize_text_field( $e->getMessage() );
}
}
return array(
'checked_at' => gmdate( 'c' ),
'option_checks' => $option_checks,
'detected_option_key' => $detected_option_key,
'legacy_credentials_type' => gettype( $credentials ),
'legacy_provider_keys' => is_array( $legacy_provider_keys ) ? array_values( $legacy_provider_keys ) : array(),
'legacy_has_any' => $legacy_has_any,
'core_helper_available' => $core_helper_available,
'core_helper_result' => $core_helper_result,
'core_helper_error' => $core_helper_error,
);
}
/**
* Determine whether an AI provider is configured and ready.
*
* Uses connector option keys registered by core Connectors and keeps a
* compatibility fallback for the earlier credentials array storage shape.
*
* The `dewey_ai_connected` filter can override the detected value when needed.
*
* @return bool
*/
function dewey_ai_connection_status() {
$filter_override = apply_filters( 'dewey_ai_connected', null );
if ( null !== $filter_override ) {
return (bool) $filter_override;
}
$debug = dewey_ai_connection_debug();
if ( true === ( $debug['core_helper_result'] ?? null ) ) {
return true;
}
foreach ( $debug['option_checks'] as $check ) {
if ( ! empty( $check['has_credential'] ) ) {
return true;
}
}
return ! empty( $debug['legacy_has_any'] );
}
/**
* Register translations for the Dewey text domain.
*
* @return void
*/
function dewey_load_textdomain() {
load_plugin_textdomain( 'dewey', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}
add_action( 'plugins_loaded', 'dewey_load_textdomain' );
// ---------------------------------------------------------------------------
// Auto-reindex on content changes
// ---------------------------------------------------------------------------
/**
* Schedule a background reindex after a post is saved or deleted, with a
* 60-second cooldown to prevent thrashing during bulk imports.
*
* @param int $post_id
* @param WP_Post $post
* @param bool $require_indexed_status Whether the post must currently be in
* an indexed status to schedule rebuild.
* @return void
*/
function dewey_maybe_schedule_reindex( int $post_id, WP_Post $post, bool $require_indexed_status = true ): void {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
$settings = Dewey_Settings::get_all();
$post_types = is_array( $settings['indexed_post_types'] ?? null )
? $settings['indexed_post_types']
: array( 'post', 'page' );
$statuses = is_array( $settings['indexed_statuses'] ?? null )
? $settings['indexed_statuses']
: array( 'publish' );
if ( ! in_array( $post->post_type, $post_types, true ) ) {
return;
}
if ( $require_indexed_status && ! in_array( $post->post_status, $statuses, true ) ) {
return;
}
// Cooldown: skip if a reindex completed or was scheduled within the last 60 seconds.
$status = Dewey_Indexer::status();
$last_completed = (string) ( $status['last_completed'] ?? '' );
if ( '' !== $last_completed ) {
$elapsed = time() - (int) strtotime( $last_completed );
if ( $elapsed < 60 ) {
return;
}
}
if ( ! wp_next_scheduled( 'dewey_scheduled_reindex' ) ) {
wp_schedule_single_event( time() + 5, 'dewey_scheduled_reindex' );
}
}
add_action(
'save_post',
static function ( int $post_id ) {
$post = get_post( $post_id );
if ( $post instanceof WP_Post ) {
dewey_maybe_schedule_reindex( $post_id, $post );
}
}
);
/**
* Reindex when a post transitions into or out of an indexed status.
*
* This keeps the index accurate when content is unpublished (e.g. publish ->
* draft), where relying only on current status checks would leave stale hits.
*
* @param string $new_status
* @param string $old_status
* @param WP_Post $post
* @return void
*/
add_action(
'transition_post_status',
static function ( string $new_status, string $old_status, WP_Post $post ) {
$settings = Dewey_Settings::get_all();
$statuses = is_array( $settings['indexed_statuses'] ?? null )
? $settings['indexed_statuses']
: array( 'publish' );
if (
! in_array( $old_status, $statuses, true ) &&
! in_array( $new_status, $statuses, true )
) {
return;
}
dewey_maybe_schedule_reindex( (int) $post->ID, $post, false );
},
10,
3
);
/**
* Schedule a background reindex when a post is permanently deleted.
*
* @param int $post_id
* @return void
*/
add_action(
'delete_post',
static function ( int $post_id ) {
$post = get_post( $post_id );
if ( $post instanceof WP_Post ) {
// Deletions should always trigger index cleanup, even if current status
// is no longer in the indexed allowlist.
dewey_maybe_schedule_reindex( $post_id, $post, false );
}
}
);
/**
* WP-Cron callback to run the Dewey index rebuild in the background.
*
* @return void
*/
function dewey_run_scheduled_reindex(): void {
if ( ! class_exists( 'Dewey_Indexer' ) ) {
return;
}
Dewey_Indexer::rebuild();
}
add_action( 'dewey_scheduled_reindex', 'dewey_run_scheduled_reindex' );