-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWidgetAssetsLoader.php
More file actions
104 lines (91 loc) · 3.44 KB
/
Copy pathWidgetAssetsLoader.php
File metadata and controls
104 lines (91 loc) · 3.44 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
<?php
/**
* Widget Assets Loader.
*
* Registers and enqueues the front-end CSS/JS for the AI Accessibility Toolbar
* widget, and localizes the configuration object consumed by the JS.
*
* NOTE: This file is intended to REPLACE the inline `ai_toolbar_enqueue_assets()`
* implementation that currently lives in AIAccessibilityToolbarPlugin.php.
* Remove that duplicate before including this file, or a fatal
* "Cannot redeclare function" error will occur.
*
* @package ai-accessibility-toolbar
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
if ( ! function_exists( 'ai_toolbar_asset_version' ) ) {
/**
* Resolve a cache-busting version string for a bundled asset.
*
* Uses filemtime() when available, falling back to the plugin version so
* the returned value is always a truthy string (never `false`, which would
* make WordPress fall back to its own core version and break busting).
*
* @param string $absolute_path Absolute filesystem path to the asset.
* @return string
*/
function ai_toolbar_asset_version( $absolute_path ) {
$default = defined( 'AI_TOOLBAR_VERSION' ) ? AI_TOOLBAR_VERSION : '1.0';
if ( ! is_string( $absolute_path ) || '' === $absolute_path || ! file_exists( $absolute_path ) ) {
return $default;
}
$mtime = filemtime( $absolute_path );
return ( false === $mtime ) ? $default : (string) $mtime;
}
}
if ( ! function_exists( 'ai_toolbar_enqueue_assets' ) ) {
/**
* Enqueue the widget's stylesheet and script on the public-facing site.
*
* Hooked to `wp_enqueue_scripts` from the main plugin file.
*
* @return void
*/
function ai_toolbar_enqueue_assets() {
if ( is_admin() ) {
return;
}
// Guard against being loaded without the main plugin's constants.
if ( ! defined( 'AI_TOOLBAR_DIR' ) || ! defined( 'AI_TOOLBAR_URL' ) ) {
return;
}
$css_rel = 'assets/css/AIWidgetStyles.css';
$js_rel = 'assets/js/AIAccessibilityToolbar.js';
$css_abs = AI_TOOLBAR_DIR . $css_rel;
$js_abs = AI_TOOLBAR_DIR . $js_rel;
wp_enqueue_style(
'ai-toolbar-widget',
AI_TOOLBAR_URL . $css_rel,
array(),
ai_toolbar_asset_version( $css_abs )
);
wp_enqueue_script(
'ai-toolbar-widget',
AI_TOOLBAR_URL . $js_rel,
array(),
ai_toolbar_asset_version( $js_abs ),
true
);
$max_input = defined( 'AI_TOOLBAR_MAX_INPUT' ) ? (int) AI_TOOLBAR_MAX_INPUT : 12000;
$config = array(
'endpoint' => esc_url_raw( rest_url( 'ai-toolbar/v1/summarize' ) ),
'nonce' => wp_create_nonce( 'wp_rest' ),
'maxInput' => $max_input,
'i18n' => array(
'processing' => __( '✨ AI is reading and processing this page…', 'ai-accessibility-toolbar' ),
'summary' => __( '📝 Webpage Summary:', 'ai-accessibility-toolbar' ),
'requestFail' => __( 'Request failed.', 'ai-accessibility-toolbar' ),
'connError' => __( '❌ Connection error contacting the API endpoint.', 'ai-accessibility-toolbar' ),
'rateLimited' => __( '⏳ Too many requests — please try again shortly.', 'ai-accessibility-toolbar' ),
),
);
// Prefer wp_add_inline_script over wp_localize_script for structured JSON:
// it avoids the historical string-casting quirks of wp_localize_script.
$inline = 'window.AI_TOOLBAR = ' . wp_json_encode( $config ) . ';';
wp_add_inline_script( 'ai-toolbar-widget', $inline, 'before' );
}
}
// Register the hook (WordPress guarantees add_action exists once ABSPATH is defined).
add_action( 'wp_enqueue_scripts', 'ai_toolbar_enqueue_assets' );