@relvehq/agent-ready 0.0.0-stage → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +364 -3
- package/SETUP.md +354 -0
- package/dist/bin/agent-ready.d.ts +23 -0
- package/dist/bin/agent-ready.js +444 -0
- package/dist/cli/init.d.ts +70 -0
- package/dist/cli/init.js +366 -0
- package/dist/core/accept.d.ts +8 -0
- package/dist/core/accept.js +76 -0
- package/dist/core/config.d.ts +145 -0
- package/dist/core/config.js +103 -0
- package/dist/core/convert.d.ts +22 -0
- package/dist/core/convert.js +334 -0
- package/dist/core/detect.d.ts +30 -0
- package/dist/core/detect.js +82 -0
- package/dist/core/easy.d.ts +19 -0
- package/dist/core/easy.js +117 -0
- package/dist/core/enrich.d.ts +30 -0
- package/dist/core/enrich.js +155 -0
- package/dist/core/llms.d.ts +11 -0
- package/dist/core/llms.js +71 -0
- package/dist/core/report.d.ts +15 -0
- package/dist/core/report.js +46 -0
- package/dist/core/resolve.d.ts +43 -0
- package/dist/core/resolve.js +204 -0
- package/dist/core/respond.d.ts +18 -0
- package/dist/core/respond.js +59 -0
- package/dist/core/sitemap.d.ts +31 -0
- package/dist/core/sitemap.js +140 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +76 -0
- package/dist/next/dashboard.d.ts +14 -0
- package/dist/next/dashboard.js +56 -0
- package/dist/next/index.d.ts +42 -0
- package/dist/next/index.js +100 -0
- package/dist/next/route.d.ts +26 -0
- package/dist/next/route.js +167 -0
- package/dist/node/index.d.ts +8 -0
- package/dist/node/index.js +191 -0
- package/dist/proof/audit.d.ts +47 -0
- package/dist/proof/audit.js +253 -0
- package/dist/proof/bots.d.ts +24 -0
- package/dist/proof/bots.js +60 -0
- package/dist/proof/dashboard.d.ts +8 -0
- package/dist/proof/dashboard.js +77 -0
- package/dist/proof/hits.d.ts +18 -0
- package/dist/proof/hits.js +51 -0
- package/dist/proof/jsonl.d.ts +5 -0
- package/dist/proof/jsonl.js +80 -0
- package/dist/proof/stats.d.ts +39 -0
- package/dist/proof/stats.js +127 -0
- package/dist/refine/check.d.ts +17 -0
- package/dist/refine/check.js +134 -0
- package/dist/refine/extract.d.ts +7 -0
- package/dist/refine/extract.js +120 -0
- package/dist/refine/file-store.d.ts +6 -0
- package/dist/refine/file-store.js +87 -0
- package/dist/refine/model.d.ts +30 -0
- package/dist/refine/model.js +105 -0
- package/dist/refine/prompts.d.ts +49 -0
- package/dist/refine/prompts.js +178 -0
- package/dist/refine/refine.d.ts +38 -0
- package/dist/refine/refine.js +75 -0
- package/dist/refine/remote.d.ts +57 -0
- package/dist/refine/remote.js +238 -0
- package/dist/refine/run.d.ts +52 -0
- package/dist/refine/run.js +66 -0
- package/dist/refine/types.d.ts +17 -0
- package/dist/refine/types.js +8 -0
- package/dist/wp/relve-agent-ready/includes/class-relve-ar-admin.php +550 -0
- package/dist/wp/relve-agent-ready/includes/class-relve-ar-converter.php +762 -0
- package/dist/wp/relve-agent-ready/includes/class-relve-ar-llms.php +192 -0
- package/dist/wp/relve-agent-ready/includes/class-relve-ar-markdown-builder.php +455 -0
- package/dist/wp/relve-agent-ready/includes/class-relve-ar-negotiation.php +334 -0
- package/dist/wp/relve-agent-ready/includes/class-relve-ar-refiner.php +894 -0
- package/dist/wp/relve-agent-ready/includes/class-relve-ar-response.php +226 -0
- package/dist/wp/relve-agent-ready/includes/class-relve-ar-router.php +421 -0
- package/dist/wp/relve-agent-ready/includes/class-relve-ar-settings.php +201 -0
- package/dist/wp/relve-agent-ready/includes/class-relve-ar-store.php +583 -0
- package/dist/wp/relve-agent-ready/includes/index.php +2 -0
- package/dist/wp/relve-agent-ready/index.php +2 -0
- package/dist/wp/relve-agent-ready/readme.txt +108 -0
- package/dist/wp/relve-agent-ready/relve-agent-ready.php +81 -0
- package/dist/wp/relve-agent-ready/uninstall.php +35 -0
- package/package.json +64 -6
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
/**
|
|
3
|
+
* Response: headers, ETag / 304, Cache-Control, token estimate.
|
|
4
|
+
*
|
|
5
|
+
* PHP port of @relvehq/agent-ready core/respond.ts and the adapters' header
|
|
6
|
+
* choices. build() is pure (an array in, an array out) so it is unit-tested;
|
|
7
|
+
* send() writes it with header() and echo.
|
|
8
|
+
*
|
|
9
|
+
* @package Relve_Agent_Ready
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
if ( ! defined( 'ABSPATH' ) ) {
|
|
13
|
+
exit;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Builds and sends Markdown / llms.txt / 404 responses.
|
|
18
|
+
*/
|
|
19
|
+
class Relve_AR_Response {
|
|
20
|
+
|
|
21
|
+
const MARKDOWN_CONTENT_TYPE = 'text/markdown; charset=utf-8';
|
|
22
|
+
const TEXT_CONTENT_TYPE = 'text/plain; charset=utf-8';
|
|
23
|
+
|
|
24
|
+
/** `.md`, `?format=md` and llms.txt: their own URLs, safe for shared caches. */
|
|
25
|
+
const CACHE_CONTROL = 'public, s-maxage=300, stale-while-revalidate=86400';
|
|
26
|
+
|
|
27
|
+
/** Negotiated by Accept on the page's own URL: never in a shared cache. */
|
|
28
|
+
const NEGOTIATED_CACHE_CONTROL = 'private, max-age=300';
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Cache-Control for a request that asked via $via.
|
|
32
|
+
*
|
|
33
|
+
* @param string $via suffix|query|accept|llms.
|
|
34
|
+
* @return string
|
|
35
|
+
*/
|
|
36
|
+
public static function cache_control_for( $via ) {
|
|
37
|
+
return 'accept' === $via ? self::NEGOTIATED_CACHE_CONTROL : self::CACHE_CONTROL;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Rough token count, as the package: ceil(length / 4), where length is
|
|
42
|
+
* the JavaScript string length (UTF-16 code units).
|
|
43
|
+
*
|
|
44
|
+
* @param string $text Text.
|
|
45
|
+
* @return int
|
|
46
|
+
*/
|
|
47
|
+
public static function estimate_tokens( $text ) {
|
|
48
|
+
return (int) ceil( self::utf16_length( $text ) / 4 );
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Length in UTF-16 code units (what JavaScript's .length counts).
|
|
53
|
+
*
|
|
54
|
+
* @param string $text UTF-8 text.
|
|
55
|
+
* @return int
|
|
56
|
+
*/
|
|
57
|
+
public static function utf16_length( $text ) {
|
|
58
|
+
$text = (string) $text;
|
|
59
|
+
$chars = preg_match_all( '/./su', $text );
|
|
60
|
+
if ( false === $chars ) {
|
|
61
|
+
return strlen( $text ); // Not valid UTF-8: count bytes.
|
|
62
|
+
}
|
|
63
|
+
$astral = preg_match_all( '/[\x{10000}-\x{10FFFF}]/u', $text );
|
|
64
|
+
return $chars + (int) $astral;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Strong ETag: first 32 hex chars of SHA-256(body).
|
|
69
|
+
*
|
|
70
|
+
* @param string $body Body.
|
|
71
|
+
* @return string
|
|
72
|
+
*/
|
|
73
|
+
public static function etag_for( $body ) {
|
|
74
|
+
return '"' . substr( hash( 'sha256', (string) $body ), 0, 32 ) . '"';
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* ETag for a body. A `generated_at:` front-matter line is left out of the
|
|
79
|
+
* hash (it changes every time) and the ETag is then weak, as the package.
|
|
80
|
+
*
|
|
81
|
+
* @param string $body Body.
|
|
82
|
+
* @return string
|
|
83
|
+
*/
|
|
84
|
+
public static function etag_for_body( $body ) {
|
|
85
|
+
$stable = preg_replace( '/^generated_at:.*\n?/m', '', (string) $body, 1 );
|
|
86
|
+
return $stable === $body ? self::etag_for( $body ) : 'W/' . self::etag_for( $stable );
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* RFC 9110 weak comparison; "*" matches any current representation.
|
|
91
|
+
*
|
|
92
|
+
* @param string|null $header If-None-Match value.
|
|
93
|
+
* @param string $etag Our ETag.
|
|
94
|
+
* @return bool
|
|
95
|
+
*/
|
|
96
|
+
public static function if_none_match_hits( $header, $etag ) {
|
|
97
|
+
if ( ! is_string( $header ) || '' === $header ) {
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
$bare = preg_replace( '#^W/#', '', $etag );
|
|
101
|
+
foreach ( explode( ',', $header ) as $raw ) {
|
|
102
|
+
$t = trim( $raw );
|
|
103
|
+
if ( '*' === $t || preg_replace( '#^W/#', '', $t ) === $bare ) {
|
|
104
|
+
return true;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Build a Markdown (or llms.txt) response.
|
|
112
|
+
*
|
|
113
|
+
* @param string $body Body.
|
|
114
|
+
* @param array $args {
|
|
115
|
+
* @type string $method GET or HEAD.
|
|
116
|
+
* @type string|null $if_none_match If-None-Match header.
|
|
117
|
+
* @type string|null $canonical Absolute canonical URL; null leaves Link out.
|
|
118
|
+
* @type string $cache_control Cache-Control.
|
|
119
|
+
* @type string $content_type Content-Type (default text/markdown).
|
|
120
|
+
* @type string $source x-agent-ready-source value.
|
|
121
|
+
* @type bool $noindex Send X-Robots-Tag: noindex (default true).
|
|
122
|
+
* @type int|null $tokens Token count (default: estimated from body).
|
|
123
|
+
* }
|
|
124
|
+
* @return array { status: int, headers: array<string,string>, body: string }
|
|
125
|
+
*/
|
|
126
|
+
public static function build( $body, $args ) {
|
|
127
|
+
$args = array_merge(
|
|
128
|
+
array(
|
|
129
|
+
'method' => 'GET',
|
|
130
|
+
'if_none_match' => null,
|
|
131
|
+
'canonical' => null,
|
|
132
|
+
'cache_control' => self::CACHE_CONTROL,
|
|
133
|
+
'content_type' => self::MARKDOWN_CONTENT_TYPE,
|
|
134
|
+
'source' => 'native',
|
|
135
|
+
'noindex' => true,
|
|
136
|
+
'tokens' => null,
|
|
137
|
+
),
|
|
138
|
+
(array) $args
|
|
139
|
+
);
|
|
140
|
+
$body = (string) $body;
|
|
141
|
+
$etag = self::etag_for_body( $body );
|
|
142
|
+
$tokens = null === $args['tokens'] ? self::estimate_tokens( $body ) : (int) $args['tokens'];
|
|
143
|
+
|
|
144
|
+
$headers = array( 'Content-Type' => $args['content_type'] );
|
|
145
|
+
$headers['Vary'] = 'Accept';
|
|
146
|
+
if ( $args['canonical'] ) {
|
|
147
|
+
$headers['Link'] = '<' . $args['canonical'] . '>; rel="canonical"';
|
|
148
|
+
}
|
|
149
|
+
if ( $args['noindex'] ) {
|
|
150
|
+
$headers['X-Robots-Tag'] = 'noindex';
|
|
151
|
+
}
|
|
152
|
+
$headers['x-markdown-tokens'] = (string) $tokens;
|
|
153
|
+
$headers['Cache-Control'] = $args['cache_control'];
|
|
154
|
+
$headers['ETag'] = $etag;
|
|
155
|
+
$headers['x-agent-ready-source'] = $args['source'];
|
|
156
|
+
|
|
157
|
+
if ( self::if_none_match_hits( $args['if_none_match'], $etag ) ) {
|
|
158
|
+
unset( $headers['Content-Type'] );
|
|
159
|
+
return array(
|
|
160
|
+
'status' => 304,
|
|
161
|
+
'headers' => $headers,
|
|
162
|
+
'body' => '',
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
$headers['Content-Length'] = (string) strlen( $body );
|
|
166
|
+
return array(
|
|
167
|
+
'status' => 200,
|
|
168
|
+
'headers' => $headers,
|
|
169
|
+
'body' => 'HEAD' === strtoupper( (string) $args['method'] ) ? '' : $body,
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* A plain-text error (the 404 for `.md` / `?format=md` with nothing to serve).
|
|
175
|
+
*
|
|
176
|
+
* @param int $status Status code.
|
|
177
|
+
* @param string $method GET or HEAD.
|
|
178
|
+
* @param string|null $canonical Canonical URL of the page, if known.
|
|
179
|
+
* @return array
|
|
180
|
+
*/
|
|
181
|
+
public static function plain( $status, $method = 'GET', $canonical = null ) {
|
|
182
|
+
$body = "No Markdown version of this page is available.\n";
|
|
183
|
+
$headers = array(
|
|
184
|
+
'Content-Type' => self::TEXT_CONTENT_TYPE,
|
|
185
|
+
'Cache-Control' => 'no-store',
|
|
186
|
+
'Vary' => 'Accept',
|
|
187
|
+
'X-Robots-Tag' => 'noindex',
|
|
188
|
+
);
|
|
189
|
+
if ( $canonical ) {
|
|
190
|
+
$headers['Link'] = '<' . $canonical . '>; rel="canonical"';
|
|
191
|
+
}
|
|
192
|
+
$headers['Content-Length'] = (string) strlen( $body );
|
|
193
|
+
return array(
|
|
194
|
+
'status' => (int) $status,
|
|
195
|
+
'headers' => $headers,
|
|
196
|
+
'body' => 'HEAD' === strtoupper( (string) $method ) ? '' : $body,
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Write a built response. Removes headers WordPress already queued that
|
|
202
|
+
* do not belong on Markdown (HTML Link headers, no-cache from nocache_headers()).
|
|
203
|
+
* The caller exits afterwards.
|
|
204
|
+
*
|
|
205
|
+
* @param array $response From build() or plain().
|
|
206
|
+
* @return void
|
|
207
|
+
*/
|
|
208
|
+
public static function send( $response ) {
|
|
209
|
+
if ( ! headers_sent() ) {
|
|
210
|
+
foreach ( array( 'Link', 'X-Pingback', 'Expires', 'Pragma', 'Last-Modified', 'Cache-Control', 'Content-Type', 'X-Robots-Tag' ) as $name ) {
|
|
211
|
+
header_remove( $name );
|
|
212
|
+
}
|
|
213
|
+
if ( function_exists( 'status_header' ) ) {
|
|
214
|
+
status_header( $response['status'] );
|
|
215
|
+
} else {
|
|
216
|
+
http_response_code( $response['status'] );
|
|
217
|
+
}
|
|
218
|
+
foreach ( $response['headers'] as $name => $value ) {
|
|
219
|
+
header( $name . ': ' . $value, true );
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
if ( '' !== $response['body'] ) {
|
|
223
|
+
echo $response['body']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- text/markdown or text/plain body, never HTML.
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
@@ -0,0 +1,421 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
/**
|
|
3
|
+
* Router: when to answer with Markdown, and with what.
|
|
4
|
+
*
|
|
5
|
+
* do_parse_request (filter, priority 1), before WordPress parses the URL:
|
|
6
|
+
* - detect the request (Relve_AR_Negotiation::detect): `.md` suffix,
|
|
7
|
+
* `?format=md`, Accept, or llms.txt; GET/HEAD only
|
|
8
|
+
* - llms.txt: answered right here
|
|
9
|
+
* - excluded path: `.md` / `?format=md` get a plain 404, Accept goes on
|
|
10
|
+
* - `.md`: REQUEST_URI is rewritten without the suffix, so WordPress
|
|
11
|
+
* resolves the post as usual (no rewrite rules involved, works with any
|
|
12
|
+
* permalink structure and never reaches WordPress's 404 for ".md")
|
|
13
|
+
* template_redirect (priority 1), after the main query, before
|
|
14
|
+
* redirect_canonical:
|
|
15
|
+
* - a published, public, non-password post/page of an enabled type, the
|
|
16
|
+
* posts front page or a category / tag / author archive -> Markdown
|
|
17
|
+
* (a stored refined file first, else built from WordPress data)
|
|
18
|
+
* - anything else: Accept requests carry on untouched; `.md` and
|
|
19
|
+
* `?format=md` get a plain-text 404
|
|
20
|
+
*
|
|
21
|
+
* Nothing at all happens while the "Enabled" setting is off.
|
|
22
|
+
*
|
|
23
|
+
* @package Relve_Agent_Ready
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
if ( ! defined( 'ABSPATH' ) ) {
|
|
27
|
+
exit;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Front-end request handling.
|
|
32
|
+
*/
|
|
33
|
+
class Relve_AR_Router {
|
|
34
|
+
|
|
35
|
+
/** Detection for the current request, set in do_parse_request. @var array|null */
|
|
36
|
+
private static $detection = null;
|
|
37
|
+
|
|
38
|
+
/** Settings for the current request. @var array|null */
|
|
39
|
+
private static $settings = null;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Register hooks.
|
|
43
|
+
*
|
|
44
|
+
* @return void
|
|
45
|
+
*/
|
|
46
|
+
public static function init() {
|
|
47
|
+
add_filter( 'do_parse_request', array( __CLASS__, 'on_parse_request' ), 1, 1 );
|
|
48
|
+
add_action( 'template_redirect', array( __CLASS__, 'on_template_redirect' ), 1 );
|
|
49
|
+
add_filter( 'rocket_cache_reject_uri', array( __CLASS__, 'rocket_reject_uri' ) );
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// ── paths ─────────────────────────────────────────────────────────────
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Path of home_url() without trailing slash: "" or e.g. "/blog".
|
|
56
|
+
*
|
|
57
|
+
* @return string
|
|
58
|
+
*/
|
|
59
|
+
public static function home_path() {
|
|
60
|
+
$path = (string) wp_parse_url( home_url(), PHP_URL_PATH );
|
|
61
|
+
return rtrim( $path, '/' );
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* scheme://host[:port] of home_url().
|
|
66
|
+
*
|
|
67
|
+
* @return string
|
|
68
|
+
*/
|
|
69
|
+
public static function home_origin() {
|
|
70
|
+
$p = wp_parse_url( home_url() );
|
|
71
|
+
if ( ! is_array( $p ) || empty( $p['host'] ) ) {
|
|
72
|
+
return '';
|
|
73
|
+
}
|
|
74
|
+
return ( isset( $p['scheme'] ) ? $p['scheme'] : 'https' ) . '://' . $p['host'] . ( isset( $p['port'] ) ? ':' . $p['port'] : '' );
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Static prefix of the permalink structure ("/blog" for
|
|
79
|
+
* "/blog/%postname%/"), "" when there is none.
|
|
80
|
+
*
|
|
81
|
+
* @return string
|
|
82
|
+
*/
|
|
83
|
+
public static function permalink_prefix() {
|
|
84
|
+
global $wp_rewrite;
|
|
85
|
+
$front = ( $wp_rewrite && isset( $wp_rewrite->front ) ) ? (string) $wp_rewrite->front : '/';
|
|
86
|
+
if ( false !== strpos( $front, '%' ) ) {
|
|
87
|
+
return '';
|
|
88
|
+
}
|
|
89
|
+
$front = rtrim( $front, '/' );
|
|
90
|
+
return null === Relve_AR_Negotiation::normalize_path( '' === $front ? '/' : $front ) ? '' : $front;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Paths llms.txt is answered at: "<home path>/llms.txt", plus
|
|
95
|
+
* "<home path><permalink prefix>/llms.txt" when posts live under a static
|
|
96
|
+
* prefix such as "/blog" (a site whose home is the root but whose blog is
|
|
97
|
+
* routed to WordPress under /blog). Empty when the plugin should not
|
|
98
|
+
* answer llms.txt.
|
|
99
|
+
*
|
|
100
|
+
* @param array $settings Settings.
|
|
101
|
+
* @return string[]
|
|
102
|
+
*/
|
|
103
|
+
public static function llms_paths( $settings ) {
|
|
104
|
+
if ( ! Relve_AR_Llms::should_serve( $settings['llms_mode'], Relve_AR_Llms::yoast_serves(), Relve_AR_Llms::physical_file_exists() ) ) {
|
|
105
|
+
return array();
|
|
106
|
+
}
|
|
107
|
+
$home = self::home_path();
|
|
108
|
+
$paths = array( $home . '/llms.txt' );
|
|
109
|
+
$pre = self::permalink_prefix();
|
|
110
|
+
if ( '' !== $pre ) {
|
|
111
|
+
$paths[] = $home . $pre . '/llms.txt';
|
|
112
|
+
}
|
|
113
|
+
return array_values( array_unique( $paths ) );
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// ── hooks ─────────────────────────────────────────────────────────────
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* do_parse_request: detect, answer llms.txt, strip ".md" for WordPress.
|
|
120
|
+
*
|
|
121
|
+
* @param bool $do Whether WordPress should parse the request.
|
|
122
|
+
* @return bool
|
|
123
|
+
*/
|
|
124
|
+
public static function on_parse_request( $do ) {
|
|
125
|
+
self::$detection = null;
|
|
126
|
+
if ( ! $do || is_admin() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) {
|
|
127
|
+
return $do;
|
|
128
|
+
}
|
|
129
|
+
$settings = Relve_AR_Settings::get();
|
|
130
|
+
if ( ! $settings['enabled'] ) {
|
|
131
|
+
return $do;
|
|
132
|
+
}
|
|
133
|
+
$method = isset( $_SERVER['REQUEST_METHOD'] ) ? strtoupper( sanitize_text_field( wp_unslash( $_SERVER['REQUEST_METHOD'] ) ) ) : 'GET';
|
|
134
|
+
if ( 'GET' !== $method && 'HEAD' !== $method ) {
|
|
135
|
+
return $do;
|
|
136
|
+
}
|
|
137
|
+
$uri = isset( $_SERVER['REQUEST_URI'] ) ? wp_unslash( $_SERVER['REQUEST_URI'] ) : '/'; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- only matched, never output.
|
|
138
|
+
$accept = isset( $_SERVER['HTTP_ACCEPT'] ) ? wp_unslash( $_SERVER['HTTP_ACCEPT'] ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- only parsed.
|
|
139
|
+
$home = self::home_path();
|
|
140
|
+
|
|
141
|
+
$d = Relve_AR_Negotiation::detect( $method, (string) $uri, (string) $accept, $home, self::llms_paths( $settings ) );
|
|
142
|
+
if ( ! $d['markdown'] ) {
|
|
143
|
+
return $do;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if ( $d['llms'] ) {
|
|
147
|
+
self::serve_llms( $settings, $method );
|
|
148
|
+
exit;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if ( Relve_AR_Negotiation::is_excluded( $d['path'], $settings['excluded'], $home ) ) {
|
|
152
|
+
if ( 'accept' === $d['via'] ) {
|
|
153
|
+
return $do;
|
|
154
|
+
}
|
|
155
|
+
self::no_page_cache();
|
|
156
|
+
Relve_AR_Response::send( Relve_AR_Response::plain( 404, $method ) );
|
|
157
|
+
exit;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
if ( 'suffix' === $d['via'] && null !== $d['uri'] ) {
|
|
161
|
+
$_SERVER['REQUEST_URI'] = wp_slash( $d['uri'] );
|
|
162
|
+
if ( ! empty( $_SERVER['PATH_INFO'] ) ) {
|
|
163
|
+
$_SERVER['PATH_INFO'] = self::strip_md_from_path_info( wp_unslash( $_SERVER['PATH_INFO'] ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
self::$detection = $d;
|
|
168
|
+
self::$settings = $settings;
|
|
169
|
+
return $do;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* PATH_INFO without a trailing ".md" (some servers set it).
|
|
174
|
+
*
|
|
175
|
+
* @param string $path_info PATH_INFO.
|
|
176
|
+
* @return string
|
|
177
|
+
*/
|
|
178
|
+
private static function strip_md_from_path_info( $path_info ) {
|
|
179
|
+
$lower = strtolower( $path_info );
|
|
180
|
+
if ( '/index.md' === substr( $lower, -9 ) ) {
|
|
181
|
+
return substr( $path_info, 0, -8 );
|
|
182
|
+
}
|
|
183
|
+
if ( '.md' === substr( $lower, -3 ) ) {
|
|
184
|
+
return substr( $path_info, 0, -3 );
|
|
185
|
+
}
|
|
186
|
+
return $path_info;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* template_redirect: serve the Markdown, or step aside.
|
|
191
|
+
*
|
|
192
|
+
* @return void
|
|
193
|
+
*/
|
|
194
|
+
public static function on_template_redirect() {
|
|
195
|
+
if ( null === self::$detection ) {
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
$d = self::$detection;
|
|
199
|
+
$settings = self::$settings;
|
|
200
|
+
$method = isset( $_SERVER['REQUEST_METHOD'] ) ? strtoupper( sanitize_text_field( wp_unslash( $_SERVER['REQUEST_METHOD'] ) ) ) : 'GET';
|
|
201
|
+
$inm = isset( $_SERVER['HTTP_IF_NONE_MATCH'] ) ? wp_unslash( $_SERVER['HTTP_IF_NONE_MATCH'] ) : null; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- compared only.
|
|
202
|
+
$negotiated = ( 'accept' === $d['via'] );
|
|
203
|
+
|
|
204
|
+
$target = self::resolve_target( $settings );
|
|
205
|
+
if ( null === $target ) {
|
|
206
|
+
if ( $negotiated ) {
|
|
207
|
+
return; // WordPress serves its normal response.
|
|
208
|
+
}
|
|
209
|
+
self::no_page_cache();
|
|
210
|
+
Relve_AR_Response::send( Relve_AR_Response::plain( 404, $method ) );
|
|
211
|
+
exit;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
try {
|
|
215
|
+
$source = 'native';
|
|
216
|
+
$store = new Relve_AR_Store();
|
|
217
|
+
$stored = $store->get( $d['path'], self::home_path() );
|
|
218
|
+
if ( null !== $stored ) {
|
|
219
|
+
$source = 'stored';
|
|
220
|
+
$body = Relve_AR_Markdown_Builder::for_stored( $stored, $target['canonical'], isset( $target['post'] ) ? $target['post'] : null, $target['title'] );
|
|
221
|
+
} elseif ( 'post' === $target['kind'] ) {
|
|
222
|
+
$body = Relve_AR_Markdown_Builder::for_post( $target['post'], $target['canonical'] );
|
|
223
|
+
} else {
|
|
224
|
+
$body = Relve_AR_Markdown_Builder::for_archive( $target['kind'], $target['id'], $target['canonical'] );
|
|
225
|
+
}
|
|
226
|
+
} catch ( Throwable $e ) {
|
|
227
|
+
$body = null;
|
|
228
|
+
}
|
|
229
|
+
if ( null === $body ) {
|
|
230
|
+
if ( $negotiated ) {
|
|
231
|
+
return; // An agent asking by Accept is never worse off than a browser.
|
|
232
|
+
}
|
|
233
|
+
self::no_page_cache();
|
|
234
|
+
Relve_AR_Response::send( Relve_AR_Response::plain( 500, $method ) );
|
|
235
|
+
exit;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
self::no_page_cache();
|
|
239
|
+
Relve_AR_Response::send(
|
|
240
|
+
Relve_AR_Response::build(
|
|
241
|
+
$body,
|
|
242
|
+
array(
|
|
243
|
+
'method' => $method,
|
|
244
|
+
'if_none_match' => $inm,
|
|
245
|
+
'canonical' => $target['canonical'],
|
|
246
|
+
'cache_control' => Relve_AR_Response::cache_control_for( $d['via'] ),
|
|
247
|
+
'source' => $source,
|
|
248
|
+
)
|
|
249
|
+
)
|
|
250
|
+
);
|
|
251
|
+
exit;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* What the main query resolved to, if it is something we serve.
|
|
256
|
+
*
|
|
257
|
+
* @param array $settings Settings.
|
|
258
|
+
* @return array|null { kind: post|home|category|post_tag|author, post?, id, canonical, title }
|
|
259
|
+
*/
|
|
260
|
+
public static function resolve_target( $settings ) {
|
|
261
|
+
if ( is_404() || is_feed() || is_search() || is_preview() || is_embed() || is_robots() || is_trackback() || is_attachment() ) {
|
|
262
|
+
return null;
|
|
263
|
+
}
|
|
264
|
+
$types = (array) $settings['post_types'];
|
|
265
|
+
if ( is_singular() ) {
|
|
266
|
+
$post = get_queried_object();
|
|
267
|
+
if ( ! self::servable_post( $post, $types ) ) {
|
|
268
|
+
return null;
|
|
269
|
+
}
|
|
270
|
+
$canonical = get_permalink( $post );
|
|
271
|
+
if ( ! $canonical ) {
|
|
272
|
+
return null;
|
|
273
|
+
}
|
|
274
|
+
return array(
|
|
275
|
+
'kind' => 'post',
|
|
276
|
+
'post' => $post,
|
|
277
|
+
'id' => $post->ID,
|
|
278
|
+
'canonical' => $canonical,
|
|
279
|
+
'title' => '',
|
|
280
|
+
);
|
|
281
|
+
}
|
|
282
|
+
if ( ! in_array( 'post', $types, true ) ) {
|
|
283
|
+
return null; // Lists of posts need posts enabled.
|
|
284
|
+
}
|
|
285
|
+
if ( is_home() ) {
|
|
286
|
+
$page = (int) get_option( 'page_for_posts' );
|
|
287
|
+
$canonical = ( 'page' === get_option( 'show_on_front' ) && $page ) ? get_permalink( $page ) : home_url( '/' );
|
|
288
|
+
return array(
|
|
289
|
+
'kind' => 'home',
|
|
290
|
+
'id' => 0,
|
|
291
|
+
'canonical' => $canonical,
|
|
292
|
+
'title' => html_entity_decode( get_bloginfo( 'name' ), ENT_QUOTES, 'UTF-8' ),
|
|
293
|
+
);
|
|
294
|
+
}
|
|
295
|
+
if ( is_category() || is_tag() ) {
|
|
296
|
+
$term = get_queried_object();
|
|
297
|
+
if ( ! $term || ! isset( $term->term_id ) ) {
|
|
298
|
+
return null;
|
|
299
|
+
}
|
|
300
|
+
$link = get_term_link( $term );
|
|
301
|
+
if ( is_wp_error( $link ) ) {
|
|
302
|
+
return null;
|
|
303
|
+
}
|
|
304
|
+
return array(
|
|
305
|
+
'kind' => is_category() ? 'category' : 'post_tag',
|
|
306
|
+
'id' => (int) $term->term_id,
|
|
307
|
+
'canonical' => $link,
|
|
308
|
+
'title' => html_entity_decode( $term->name, ENT_QUOTES, 'UTF-8' ),
|
|
309
|
+
);
|
|
310
|
+
}
|
|
311
|
+
if ( is_author() ) {
|
|
312
|
+
$user = get_queried_object();
|
|
313
|
+
if ( ! $user || ! isset( $user->ID ) ) {
|
|
314
|
+
return null;
|
|
315
|
+
}
|
|
316
|
+
return array(
|
|
317
|
+
'kind' => 'author',
|
|
318
|
+
'id' => (int) $user->ID,
|
|
319
|
+
'canonical' => get_author_posts_url( $user->ID ),
|
|
320
|
+
'title' => html_entity_decode( (string) $user->display_name, ENT_QUOTES, 'UTF-8' ),
|
|
321
|
+
);
|
|
322
|
+
}
|
|
323
|
+
return null;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* Published, public, not password-protected, of an enabled type.
|
|
328
|
+
*
|
|
329
|
+
* @param mixed $post Queried object.
|
|
330
|
+
* @param string[] $types Enabled post types.
|
|
331
|
+
* @return bool
|
|
332
|
+
*/
|
|
333
|
+
public static function servable_post( $post, $types ) {
|
|
334
|
+
if ( ! ( $post instanceof WP_Post ) ) {
|
|
335
|
+
return false;
|
|
336
|
+
}
|
|
337
|
+
if ( 'publish' !== $post->post_status || '' !== (string) $post->post_password ) {
|
|
338
|
+
return false;
|
|
339
|
+
}
|
|
340
|
+
if ( ! in_array( $post->post_type, (array) $types, true ) || ! is_post_type_viewable( $post->post_type ) ) {
|
|
341
|
+
return false;
|
|
342
|
+
}
|
|
343
|
+
return true;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* Answer llms.txt.
|
|
348
|
+
*
|
|
349
|
+
* @param array $settings Settings.
|
|
350
|
+
* @param string $method GET or HEAD.
|
|
351
|
+
* @return void
|
|
352
|
+
*/
|
|
353
|
+
private static function serve_llms( $settings, $method ) {
|
|
354
|
+
$inm = isset( $_SERVER['HTTP_IF_NONE_MATCH'] ) ? wp_unslash( $_SERVER['HTTP_IF_NONE_MATCH'] ) : null; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- compared only.
|
|
355
|
+
$body = Relve_AR_Llms::generate( $settings );
|
|
356
|
+
self::no_page_cache();
|
|
357
|
+
Relve_AR_Response::send(
|
|
358
|
+
Relve_AR_Response::build(
|
|
359
|
+
$body,
|
|
360
|
+
array(
|
|
361
|
+
'method' => $method,
|
|
362
|
+
'if_none_match' => $inm,
|
|
363
|
+
'canonical' => null, // llms.txt is meant to be found and read.
|
|
364
|
+
'noindex' => false,
|
|
365
|
+
'content_type' => Relve_AR_Response::TEXT_CONTENT_TYPE,
|
|
366
|
+
'cache_control' => Relve_AR_Response::cache_control_for( 'llms' ),
|
|
367
|
+
'source' => 'llms',
|
|
368
|
+
)
|
|
369
|
+
)
|
|
370
|
+
);
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
// ── caches ────────────────────────────────────────────────────────────
|
|
374
|
+
|
|
375
|
+
/**
|
|
376
|
+
* Tell page caches not to store this response as the page.
|
|
377
|
+
*
|
|
378
|
+
* @return void
|
|
379
|
+
*/
|
|
380
|
+
public static function no_page_cache() {
|
|
381
|
+
if ( ! defined( 'DONOTCACHEPAGE' ) ) {
|
|
382
|
+
define( 'DONOTCACHEPAGE', true ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound -- shared page-cache convention.
|
|
383
|
+
}
|
|
384
|
+
add_filter( 'do_rocket_generate_caching_files', '__return_false', 999 );
|
|
385
|
+
do_action( 'litespeed_control_set_nocache', 'relve-agent-ready: markdown response' ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- LiteSpeed Cache's hook.
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/**
|
|
389
|
+
* WP Rocket: never cache ".md" URLs or llms.txt.
|
|
390
|
+
*
|
|
391
|
+
* @param mixed $uris Reject patterns.
|
|
392
|
+
* @return array
|
|
393
|
+
*/
|
|
394
|
+
public static function rocket_reject_uri( $uris ) {
|
|
395
|
+
$uris = is_array( $uris ) ? $uris : array();
|
|
396
|
+
$uris[] = '/(.+)\.md';
|
|
397
|
+
$uris[] = '/(.*)llms\.txt';
|
|
398
|
+
return array_values( array_unique( $uris ) );
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* Regenerate WP Rocket's config so the reject list is current.
|
|
403
|
+
*
|
|
404
|
+
* @return void
|
|
405
|
+
*/
|
|
406
|
+
public static function refresh_rocket() {
|
|
407
|
+
if ( function_exists( 'rocket_generate_config_file' ) ) {
|
|
408
|
+
rocket_generate_config_file();
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* Reset per-request state (tests).
|
|
414
|
+
*
|
|
415
|
+
* @return void
|
|
416
|
+
*/
|
|
417
|
+
public static function reset() {
|
|
418
|
+
self::$detection = null;
|
|
419
|
+
self::$settings = null;
|
|
420
|
+
}
|
|
421
|
+
}
|