Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
95.73% |
202 / 211 |
|
90.00% |
27 / 30 |
CRAP | |
0.00% |
0 / 1 |
| Llms_Txt | |
95.73% |
202 / 211 |
|
90.00% |
27 / 30 |
86 | |
0.00% |
0 / 1 |
| init | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| register_redirect_filters | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| register_always_include | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
2 | |||
| clear_always_include_if_hidden | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
4 | |||
| register_invalidation | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
4 | |||
| clear_cache_for_option | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
5 | |||
| clear_cache | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
3 | |||
| clear_cache_for_post | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
3 | |||
| clear_cache_for_meta | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| shapes_llms_txt | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
3 | |||
| is_enabled | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| can_serve | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
| request_path | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
2.03 | |||
| preserve_llms_txt_request | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| should_serve | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
4 | |||
| maybe_serve | |
22.22% |
2 / 9 |
|
0.00% |
0 / 1 |
7.23 | |||
| get_stored | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
3 | |||
| generate | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
7 | |||
| get_llms_post_type_objects | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
| get_posts_for_type | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
3 | |||
| get_limit | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| get_pages | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
3 | |||
| get_always_included_posts | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
2 | |||
| get_pages_by_order | |
90.91% |
10 / 11 |
|
0.00% |
0 / 1 |
2.00 | |||
| get_pages_in_order | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
| query_args | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
1 | |||
| section_title | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| link_list | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
5 | |||
| summary | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
5 | |||
| plain_text | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Generation and serving of the site's llms.txt. |
| 4 | * |
| 5 | * Serves a root-level `/llms.txt` — the emerging convention (llmstxt.org) for |
| 6 | * giving AI assistants a curated, Markdown map of a site's key content. Gated on |
| 7 | * the durable option `jetpack_seo_llms_txt_enabled` (round-tripped through core's |
| 8 | * `/wp/v2/settings` endpoint by the GEO tab). |
| 9 | * |
| 10 | * Serving approach: this hooks `template_redirect` and inspects the raw request |
| 11 | * path rather than registering a rewrite rule, so it needs no rewrite flush and |
| 12 | * works under any permalink structure. The tradeoff — it matches a root-level |
| 13 | * request only, and like robots.txt won't apply on a multisite subdirectory or |
| 14 | * a site fronted by a static file. A rewrite rule is tracked separately in |
| 15 | * JETPACK-1830. |
| 16 | * |
| 17 | * @package automattic/jetpack-seo-package |
| 18 | */ |
| 19 | |
| 20 | namespace Automattic\Jetpack\SEO; |
| 21 | |
| 22 | /** |
| 23 | * Builds and serves the site's llms.txt. |
| 24 | */ |
| 25 | class Llms_Txt { |
| 26 | |
| 27 | /** |
| 28 | * Option recording whether llms.txt generation is enabled. Mirrored as a |
| 29 | * literal in the plugin's settings endpoint whitelist (`jp_group => |
| 30 | * 'seo-tools'`). |
| 31 | * |
| 32 | * @var string |
| 33 | */ |
| 34 | const OPTION = 'jetpack_seo_llms_txt_enabled'; |
| 35 | |
| 36 | /** |
| 37 | * Default max items listed per supported content type, so a large site doesn't dump |
| 38 | * its whole tree. |
| 39 | */ |
| 40 | const MAX_POSTS_PER_TYPE = 50; |
| 41 | |
| 42 | /** |
| 43 | * Post meta an owner sets to always list supported content in llms.txt. |
| 44 | * |
| 45 | * @var string |
| 46 | */ |
| 47 | const META_ALWAYS_INCLUDE = 'jetpack_seo_llms_txt_always_include'; |
| 48 | |
| 49 | /** |
| 50 | * Transient holding the last built llms.txt until {@see clear_cache()} runs. |
| 51 | */ |
| 52 | const CACHE_KEY = 'jetpack_seo_llms_txt'; |
| 53 | |
| 54 | /** |
| 55 | * How long a stored llms.txt is kept at most, for changes no hook reports. |
| 56 | */ |
| 57 | const CACHE_TTL = 12 * HOUR_IN_SECONDS; |
| 58 | |
| 59 | /** |
| 60 | * How long CDNs and page caches may keep a served llms.txt. |
| 61 | */ |
| 62 | const HTTP_MAX_AGE = 15 * MINUTE_IN_SECONDS; |
| 63 | |
| 64 | /** |
| 65 | * Options whose changes alter llms.txt, besides its own on/off setting. |
| 66 | */ |
| 67 | const CACHE_OPTIONS = array( 'blogname', 'blogdescription', 'blog_public', 'show_on_front', 'page_on_front', 'permalink_structure', 'home', 'siteurl', 'WPLANG' ); |
| 68 | |
| 69 | /** |
| 70 | * Post meta that changes whether or how a post appears in llms.txt, including the |
| 71 | * always-include switch and page template the menu ranking reads. |
| 72 | */ |
| 73 | const CACHE_META = array( |
| 74 | Content_Coverage::META_NOINDEX, |
| 75 | Content_Gate::META_CONTAINS_PAID_CONTENT, |
| 76 | Content_Gate::META_CONTAINS_PAYWALLED_CONTENT, |
| 77 | Content_Gate::META_NEWSLETTER_ACCESS, |
| 78 | self::META_ALWAYS_INCLUDE, |
| 79 | '_wp_page_template', |
| 80 | ); |
| 81 | |
| 82 | /** |
| 83 | * Non-public post types that still shape llms.txt: menus and the theme parts that hold them. |
| 84 | */ |
| 85 | const CACHE_POST_TYPES = array( 'nav_menu_item', 'wp_navigation', 'wp_template', 'wp_template_part', 'wp_block' ); |
| 86 | |
| 87 | /** |
| 88 | * Wire the front-end request handler. |
| 89 | * |
| 90 | * @return void |
| 91 | */ |
| 92 | public static function init() { |
| 93 | add_action( 'template_redirect', array( __CLASS__, 'maybe_serve' ) ); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Protect the document's URL before SEO is enabled or the dashboard is visible. |
| 98 | * |
| 99 | * @return void |
| 100 | */ |
| 101 | public static function register_redirect_filters() { |
| 102 | add_filter( 'redirect_canonical', array( __CLASS__, 'preserve_llms_txt_request' ) ); |
| 103 | add_filter( 'old_slug_redirect_url', array( __CLASS__, 'preserve_llms_txt_request' ) ); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Register the always-include meta for core REST and keep it off on hidden content. |
| 108 | * |
| 109 | * @return void |
| 110 | */ |
| 111 | public static function register_always_include() { |
| 112 | // Core REST saves meta in registration order; noindex must be written first. |
| 113 | unregister_meta_key( 'post', self::META_ALWAYS_INCLUDE ); |
| 114 | register_meta( |
| 115 | 'post', |
| 116 | self::META_ALWAYS_INCLUDE, |
| 117 | array( |
| 118 | 'type' => 'boolean', |
| 119 | // Not translated: this runs before `init`, too early to load the text domain. |
| 120 | 'description' => 'Whether to always list the content in llms.txt.', |
| 121 | 'single' => true, |
| 122 | 'default' => false, |
| 123 | 'show_in_rest' => true, |
| 124 | ) |
| 125 | ); |
| 126 | foreach ( array( 'added_post_meta', 'updated_post_meta' ) as $hook ) { |
| 127 | add_action( $hook, array( __CLASS__, 'clear_always_include_if_hidden' ), 10, 4 ); |
| 128 | } |
| 129 | add_action( 'rest_api_init', array( __CLASS__, 'register_always_include' ), 0 ); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Keep "Always include" off while content is hidden from search engines. |
| 134 | * |
| 135 | * @param int $meta_id Meta ID. |
| 136 | * @param int $post_id Post ID. |
| 137 | * @param string $meta_key Meta key. |
| 138 | * @param mixed $meta_value Meta value. |
| 139 | * @return void |
| 140 | */ |
| 141 | public static function clear_always_include_if_hidden( $meta_id, $post_id, $meta_key, $meta_value ) { |
| 142 | if ( |
| 143 | $meta_value |
| 144 | && in_array( $meta_key, array( Content_Coverage::META_NOINDEX, self::META_ALWAYS_INCLUDE ), true ) |
| 145 | && get_post_meta( $post_id, Content_Coverage::META_NOINDEX, true ) |
| 146 | ) { |
| 147 | delete_post_meta( $post_id, self::META_ALWAYS_INCLUDE ); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Clear the stored copy when something it lists changes. Registered even while llms.txt |
| 153 | * can't be served, so a copy stored earlier is not served stale once it can again. |
| 154 | * |
| 155 | * @return void |
| 156 | */ |
| 157 | public static function register_invalidation() { |
| 158 | foreach ( array( 'wp_update_nav_menu', 'switch_theme', 'customize_save_after', 'activated_plugin', 'deactivated_plugin' ) as $hook ) { |
| 159 | add_action( $hook, array( __CLASS__, 'clear_cache' ) ); |
| 160 | } |
| 161 | add_action( 'clean_post_cache', array( __CLASS__, 'clear_cache_for_post' ), 10, 2 ); |
| 162 | // Any taxonomy: besides menus, a category's slug or parent can be part of post URLs. |
| 163 | add_action( 'clean_term_cache', array( __CLASS__, 'clear_cache' ) ); |
| 164 | foreach ( array( 'added_post_meta', 'updated_post_meta', 'deleted_post_meta' ) as $hook ) { |
| 165 | add_action( $hook, array( __CLASS__, 'clear_cache_for_meta' ), 10, 3 ); |
| 166 | } |
| 167 | foreach ( array( 'added_option', 'updated_option', 'deleted_option' ) as $hook ) { |
| 168 | add_action( $hook, array( __CLASS__, 'clear_cache_for_option' ) ); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Drop the stored llms.txt after a write to an option it reads. Matched when the option is |
| 174 | * saved, since WordPress.com API requests register these hooks before switching to the site. |
| 175 | * |
| 176 | * @param string $option Option name. |
| 177 | * @return void |
| 178 | */ |
| 179 | public static function clear_cache_for_option( $option ) { |
| 180 | if ( self::OPTION === $option ) { |
| 181 | // Turning llms.txt off or on is the owner's reset, so fire even when nothing is stored: |
| 182 | // an edge or CDN copy can outlive an expired stored one. |
| 183 | delete_transient( self::CACHE_KEY ); |
| 184 | /** This action is documented in projects/packages/seo/src/class-llms-txt.php */ |
| 185 | do_action( 'jetpack_seo_llms_txt_cleared' ); |
| 186 | } elseif ( |
| 187 | in_array( $option, self::CACHE_OPTIONS, true ) |
| 188 | || ( 0 === strpos( $option, 'theme_mods_' ) && 'theme_mods_' . get_stylesheet() === $option ) |
| 189 | ) { |
| 190 | self::clear_cache(); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Drop the stored llms.txt so the next request rebuilds it. |
| 196 | * |
| 197 | * @return void |
| 198 | */ |
| 199 | public static function clear_cache() { |
| 200 | // The lookup is cached when nothing is stored, so bulk edits and imports don't query on every write. |
| 201 | if ( false !== get_transient( self::CACHE_KEY ) && delete_transient( self::CACHE_KEY ) ) { |
| 202 | /** |
| 203 | * Fires after the stored llms.txt is dropped, so hosts can purge cached copies of /llms.txt. |
| 204 | * |
| 205 | * @since 0.12.0 |
| 206 | */ |
| 207 | do_action( 'jetpack_seo_llms_txt_cleared' ); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Drop the stored llms.txt after a change to a post llms.txt can list or a menu it reads. |
| 213 | * |
| 214 | * @param int $post_id Post ID. |
| 215 | * @param \WP_Post|null $post Post object; still set after a permanent delete, when the ID no longer resolves. |
| 216 | * @return void |
| 217 | */ |
| 218 | public static function clear_cache_for_post( $post_id, $post = null ) { |
| 219 | if ( self::shapes_llms_txt( $post instanceof \WP_Post ? $post->post_type : get_post_type( $post_id ) ) ) { |
| 220 | self::clear_cache(); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Drop the stored llms.txt after a write to post meta that llms.txt reads. |
| 226 | * |
| 227 | * @param int|int[] $meta_id Meta ID or IDs. |
| 228 | * @param int $post_id Post ID. |
| 229 | * @param string $meta_key Meta key. |
| 230 | * @return void |
| 231 | */ |
| 232 | public static function clear_cache_for_meta( $meta_id, $post_id, $meta_key ) { |
| 233 | if ( in_array( $meta_key, self::CACHE_META, true ) ) { |
| 234 | self::clear_cache(); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Whether posts of a type can appear in llms.txt or hold a menu, so a sitemap, oEmbed |
| 240 | * cache or revision write leaves the stored copy alone. |
| 241 | * |
| 242 | * @param string|false $type Post type slug. |
| 243 | * @return bool |
| 244 | */ |
| 245 | private static function shapes_llms_txt( $type ) { |
| 246 | return $type && ( is_post_type_viewable( $type ) || in_array( $type, self::CACHE_POST_TYPES, true ) ); |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Whether llms.txt generation is currently switched on. |
| 251 | * |
| 252 | * @return bool |
| 253 | */ |
| 254 | public static function is_enabled() { |
| 255 | return (bool) get_option( self::OPTION, false ); |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Whether Jetpack can serve llms.txt without a plugin, static-file or hosting conflict. |
| 260 | * |
| 261 | * Hosts can signal path restrictions through the jetpack_seo_llms_txt_can_serve filter. |
| 262 | * |
| 263 | * @return bool |
| 264 | */ |
| 265 | public static function can_serve() { |
| 266 | if ( Seo_Tools::is_disabled() ) { |
| 267 | return false; |
| 268 | } |
| 269 | |
| 270 | $has_static_file = defined( 'ABSPATH' ) && Static_Files::exists( 'llms.txt' ); |
| 271 | |
| 272 | /** |
| 273 | * Filters whether WordPress can serve the dynamic `/llms.txt`. Hosts that |
| 274 | * intercept root-level paths before WordPress runs can return false to |
| 275 | * surface the honest "can't take effect" state in the SEO dashboard. |
| 276 | * |
| 277 | * @since 0.7.0 |
| 278 | * |
| 279 | * @param bool $can_serve Whether WordPress can serve `/llms.txt`. |
| 280 | */ |
| 281 | return (bool) apply_filters( 'jetpack_seo_llms_txt_can_serve', ! $has_static_file ); |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * The site-root-relative request path, normalized without surrounding |
| 286 | * slashes (e.g. `llms.txt`). |
| 287 | * |
| 288 | * @return string |
| 289 | */ |
| 290 | private static function request_path() { |
| 291 | if ( empty( $_SERVER['REQUEST_URI'] ) ) { |
| 292 | return ''; |
| 293 | } |
| 294 | $uri = esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ); |
| 295 | $path = (string) wp_parse_url( $uri, PHP_URL_PATH ); |
| 296 | return trim( $path, '/' ); |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * Prevent cached post redirects from outliving the document's disabled state. |
| 301 | * |
| 302 | * @param string|false $redirect_url The proposed redirect destination. |
| 303 | * @return string|false |
| 304 | */ |
| 305 | public static function preserve_llms_txt_request( $redirect_url ) { |
| 306 | return 'llms.txt' === self::request_path() ? false : $redirect_url; |
| 307 | } |
| 308 | |
| 309 | /** |
| 310 | * Whether this request can receive the generated document. |
| 311 | * |
| 312 | * @return bool |
| 313 | */ |
| 314 | private static function should_serve() { |
| 315 | return 'llms.txt' === self::request_path() |
| 316 | && self::is_enabled() |
| 317 | && self::can_serve() |
| 318 | && (bool) get_option( 'blog_public' ); |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * Serve llms.txt when enabled and the request is for it; otherwise no-op. |
| 323 | * |
| 324 | * @return void |
| 325 | */ |
| 326 | public static function maybe_serve() { |
| 327 | if ( ! self::should_serve() ) { |
| 328 | return; |
| 329 | } |
| 330 | |
| 331 | // A private site (`blog_public` -1) serves members only, so shared caches must not keep it. |
| 332 | if ( 1 === (int) get_option( 'blog_public' ) ) { |
| 333 | header( 'Cache-Control: public, max-age=' . self::HTTP_MAX_AGE ); |
| 334 | } else { |
| 335 | nocache_headers(); |
| 336 | } |
| 337 | status_header( 200 ); |
| 338 | header( 'Content-Type: text/plain; charset=utf-8' ); |
| 339 | |
| 340 | // Content is plain-text Markdown; not HTML, so it's emitted as-is. |
| 341 | echo self::get_stored(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 342 | exit; |
| 343 | } |
| 344 | |
| 345 | /** |
| 346 | * The stored llms.txt, built and stored first when there is none. |
| 347 | * |
| 348 | * @return string |
| 349 | */ |
| 350 | public static function get_stored() { |
| 351 | $body = get_transient( self::CACHE_KEY ); |
| 352 | if ( is_string( $body ) && '' !== $body ) { |
| 353 | return $body; |
| 354 | } |
| 355 | |
| 356 | // Every visitor gets the stored copy, so build it as a logged-out visitor sees it. |
| 357 | $user_id = get_current_user_id(); |
| 358 | wp_set_current_user( 0 ); |
| 359 | try { |
| 360 | $body = self::generate(); |
| 361 | } finally { |
| 362 | wp_set_current_user( $user_id ); |
| 363 | } |
| 364 | |
| 365 | set_transient( self::CACHE_KEY, $body, self::CACHE_TTL ); |
| 366 | return $body; |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * Build the llms.txt Markdown document from site identity and published |
| 371 | * supported content types. |
| 372 | * |
| 373 | * @return string |
| 374 | */ |
| 375 | public static function generate() { |
| 376 | $name = self::plain_text( get_bloginfo( 'name' ) ); |
| 377 | $description = self::plain_text( get_bloginfo( 'description' ) ); |
| 378 | |
| 379 | $blocks = array(); |
| 380 | |
| 381 | $blocks[] = '# ' . ( '' !== $name ? $name : home_url() ); |
| 382 | if ( '' !== $description ) { |
| 383 | $blocks[] = '> ' . $description; |
| 384 | } |
| 385 | |
| 386 | foreach ( self::get_llms_post_type_objects() as $post_type ) { |
| 387 | $list = self::link_list( self::get_posts_for_type( $post_type->name ) ); |
| 388 | if ( '' !== $list ) { |
| 389 | $blocks[] = '## ' . self::section_title( $post_type ) . "\n\n" . $list; |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | $text = implode( "\n\n", $blocks ) . "\n"; |
| 394 | |
| 395 | /** |
| 396 | * Filters the llms.txt Markdown, e.g. to add a section. |
| 397 | * |
| 398 | * The result is stored for up to 12 hours; call Llms_Txt::clear_cache() when your section's data |
| 399 | * changes. Anything but a non-empty string keeps the unfiltered text. |
| 400 | * |
| 401 | * @since 0.12.0 |
| 402 | * |
| 403 | * @param string $text The llms.txt Markdown. |
| 404 | */ |
| 405 | $filtered = apply_filters( 'jetpack_seo_llms_txt', $text ); |
| 406 | |
| 407 | return is_string( $filtered ) && '' !== $filtered ? $filtered : $text; |
| 408 | } |
| 409 | |
| 410 | /** |
| 411 | * Return supported post types in llms.txt reading order. |
| 412 | * |
| 413 | * @return \WP_Post_Type[] |
| 414 | */ |
| 415 | private static function get_llms_post_type_objects() { |
| 416 | $post_types = Post_Types::get_supported_content_type_objects(); |
| 417 | $ordered = array(); |
| 418 | |
| 419 | foreach ( array( 'page', 'post' ) as $post_type ) { |
| 420 | if ( isset( $post_types[ $post_type ] ) ) { |
| 421 | $ordered[ $post_type ] = $post_types[ $post_type ]; |
| 422 | unset( $post_types[ $post_type ] ); |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | return array_merge( $ordered, $post_types ); |
| 427 | } |
| 428 | |
| 429 | /** |
| 430 | * Fetch published content for a supported post type. |
| 431 | * |
| 432 | * @param string $post_type Post type slug. |
| 433 | * @return \WP_Post[] |
| 434 | */ |
| 435 | private static function get_posts_for_type( $post_type ) { |
| 436 | $limit = self::get_limit( $post_type ); |
| 437 | if ( 'page' === $post_type ) { |
| 438 | return self::get_pages( $limit ); |
| 439 | } |
| 440 | |
| 441 | $always_included = self::get_always_included_posts( $post_type ); |
| 442 | $room = max( 0, $limit - count( $always_included ) ); |
| 443 | if ( 0 === $room ) { |
| 444 | return $always_included; |
| 445 | } |
| 446 | |
| 447 | $args = self::query_args( $post_type ); |
| 448 | $args['posts_per_page'] = $room; |
| 449 | $args['post__not_in'] = wp_list_pluck( $always_included, 'ID' ); |
| 450 | $args['orderby'] = 'date'; |
| 451 | $args['order'] = 'DESC'; |
| 452 | |
| 453 | return array_merge( $always_included, get_posts( $args ) ); |
| 454 | } |
| 455 | |
| 456 | /** |
| 457 | * How many items to list for a post type. |
| 458 | * |
| 459 | * @param string $post_type Post type slug. |
| 460 | * @return int |
| 461 | */ |
| 462 | private static function get_limit( $post_type ) { |
| 463 | /** |
| 464 | * Filters how many items llms.txt lists for a post type. Items the owner always |
| 465 | * includes are listed even past it. |
| 466 | * |
| 467 | * @since $$next-version$$ |
| 468 | * |
| 469 | * @param int $limit Max items to list. Default 50. |
| 470 | * @param string $post_type Post type slug. |
| 471 | */ |
| 472 | $limit = apply_filters( 'jetpack_seo_llms_txt_items_per_type', self::MAX_POSTS_PER_TYPE, $post_type ); |
| 473 | $limit = filter_var( $limit, FILTER_VALIDATE_INT, array( 'options' => array( 'min_range' => 0 ) ) ); |
| 474 | |
| 475 | return false === $limit ? self::MAX_POSTS_PER_TYPE : $limit; |
| 476 | } |
| 477 | |
| 478 | /** |
| 479 | * Pages ranked: the ones the owner always includes, then the main menu, then other |
| 480 | * displayed menus, then the rest by {@see get_pages_by_order()}. |
| 481 | * |
| 482 | * @param int $limit Max pages to list. |
| 483 | * @return \WP_Post[] |
| 484 | */ |
| 485 | private static function get_pages( $limit ) { |
| 486 | $always_included = self::get_always_included_posts( 'page' ); |
| 487 | $room = $limit - count( $always_included ); |
| 488 | if ( $room <= 0 ) { |
| 489 | return $always_included; |
| 490 | } |
| 491 | |
| 492 | $skip = wp_list_pluck( $always_included, 'ID' ); |
| 493 | $menus = Nav_Menu_Pages::get(); |
| 494 | $main = self::get_pages_in_order( array_values( array_diff( $menus['main'], $skip ) ), $room ); |
| 495 | $room -= count( $main ); |
| 496 | $other = self::get_pages_in_order( array_values( array_diff( $menus['other'], $skip ) ), $room ); |
| 497 | $room -= count( $other ); |
| 498 | if ( $room <= 0 ) { |
| 499 | return array_merge( $always_included, $main, $other ); |
| 500 | } |
| 501 | |
| 502 | $args = self::query_args( 'page' ); |
| 503 | $args['posts_per_page'] = $room; |
| 504 | $args['post__not_in'] = array_merge( $skip, $menus['main'], $menus['other'] ); |
| 505 | |
| 506 | return array_merge( $always_included, $main, $other, self::get_pages_by_order( $args ) ); |
| 507 | } |
| 508 | |
| 509 | /** |
| 510 | * Fetch eligible content the owner switched on to always include. |
| 511 | * |
| 512 | * @param string $post_type Post type slug. |
| 513 | * @return \WP_Post[] |
| 514 | */ |
| 515 | private static function get_always_included_posts( $post_type ) { |
| 516 | $args = self::query_args( $post_type ); |
| 517 | $args['meta_query'] = array( |
| 518 | // @phan-suppress-next-line PhanPluginMixedKeyNoKey |
| 519 | 'relation' => 'AND', |
| 520 | $args['meta_query'], |
| 521 | array( |
| 522 | 'key' => self::META_ALWAYS_INCLUDE, |
| 523 | 'value' => '1', |
| 524 | ), |
| 525 | ); |
| 526 | $args['posts_per_page'] = -1; |
| 527 | $args['orderby'] = 'date'; |
| 528 | $args['order'] = 'DESC'; |
| 529 | |
| 530 | return 'page' === $post_type ? self::get_pages_by_order( $args ) : get_posts( $args ); |
| 531 | } |
| 532 | |
| 533 | /** |
| 534 | * Run a page query sorted by the page Order, lowest first, with pages whose Order was |
| 535 | * set ahead of untouched ones (Order 0), then by title. |
| 536 | * |
| 537 | * @param array $args Query arguments. |
| 538 | * @return \WP_Post[] |
| 539 | */ |
| 540 | private static function get_pages_by_order( array $args ) { |
| 541 | $flag = 'jetpack_seo_llms_txt_order'; |
| 542 | $args[ $flag ] = true; |
| 543 | $orderby = static function ( $sql, $query ) use ( $flag ) { |
| 544 | global $wpdb; |
| 545 | if ( ! $query->get( $flag ) ) { |
| 546 | return $sql; |
| 547 | } |
| 548 | // `menu_order = 0` is 0 for pages with an Order set and 1 for untouched ones, so ASC lists |
| 549 | // pages with an Order first; each group then sorts by Order, then title. |
| 550 | return "({$wpdb->posts}.menu_order = 0) ASC, {$wpdb->posts}.menu_order ASC, {$wpdb->posts}.post_title ASC"; |
| 551 | }; |
| 552 | |
| 553 | add_filter( 'posts_orderby', $orderby, 10, 2 ); |
| 554 | $posts = get_posts( $args ); |
| 555 | remove_filter( 'posts_orderby', $orderby, 10 ); |
| 556 | |
| 557 | return $posts; |
| 558 | } |
| 559 | |
| 560 | /** |
| 561 | * Fetch listable pages by ID, keeping the given order. |
| 562 | * |
| 563 | * @param int[] $ids Page IDs. |
| 564 | * @param int $limit Max pages to return. |
| 565 | * @return \WP_Post[] |
| 566 | */ |
| 567 | private static function get_pages_in_order( array $ids, $limit ) { |
| 568 | if ( empty( $ids ) || $limit <= 0 ) { |
| 569 | return array(); |
| 570 | } |
| 571 | |
| 572 | $args = self::query_args( 'page' ); |
| 573 | $args['post__in'] = $ids; |
| 574 | $args['orderby'] = 'post__in'; |
| 575 | $args['posts_per_page'] = $limit; |
| 576 | |
| 577 | return get_posts( $args ); |
| 578 | } |
| 579 | |
| 580 | /** |
| 581 | * Query arguments for published content that isn't password protected or hidden from |
| 582 | * search engines. |
| 583 | * |
| 584 | * @param string $post_type Post type slug. |
| 585 | * @return array |
| 586 | */ |
| 587 | private static function query_args( $post_type ) { |
| 588 | return array( |
| 589 | 'post_type' => $post_type, |
| 590 | 'post_status' => 'publish', |
| 591 | 'has_password' => false, |
| 592 | 'suppress_filters' => false, |
| 593 | 'meta_query' => array( |
| 594 | // WP_Query's meta_query takes its `relation` as a keyed entry alongside |
| 595 | // the positional clauses below; the mixed shape is required, not a slip. |
| 596 | // @phan-suppress-next-line PhanPluginMixedKeyNoKey |
| 597 | 'relation' => 'OR', |
| 598 | array( |
| 599 | 'key' => Content_Coverage::META_NOINDEX, |
| 600 | 'compare' => 'NOT EXISTS', |
| 601 | ), |
| 602 | array( |
| 603 | 'key' => Content_Coverage::META_NOINDEX, |
| 604 | 'value' => '1', |
| 605 | 'compare' => '!=', |
| 606 | ), |
| 607 | ), |
| 608 | ); |
| 609 | } |
| 610 | |
| 611 | /** |
| 612 | * Section title for a supported post type. |
| 613 | * |
| 614 | * @param \WP_Post_Type $post_type Post type object. |
| 615 | * @return string |
| 616 | */ |
| 617 | private static function section_title( $post_type ) { |
| 618 | if ( 'page' === $post_type->name ) { |
| 619 | return __( 'Pages', 'jetpack-seo' ); |
| 620 | } |
| 621 | if ( 'post' === $post_type->name ) { |
| 622 | return __( 'Posts', 'jetpack-seo' ); |
| 623 | } |
| 624 | return self::plain_text( $post_type->label ); |
| 625 | } |
| 626 | |
| 627 | /** |
| 628 | * Render a list of posts as llms.txt link lines: |
| 629 | * `- [Title](url): summary`. |
| 630 | * |
| 631 | * @param \WP_Post[] $posts Posts to render. |
| 632 | * @return string Newline-joined link lines, or '' when there are none. |
| 633 | */ |
| 634 | private static function link_list( $posts ) { |
| 635 | $lines = array(); |
| 636 | foreach ( $posts as $post ) { |
| 637 | // Password-protected posts are `publish` status but their content is |
| 638 | // intentionally gated; keep them out of the public llms.txt entirely. |
| 639 | if ( ! empty( $post->post_password ) ) { |
| 640 | continue; |
| 641 | } |
| 642 | |
| 643 | $title = self::plain_text( get_the_title( $post ) ); |
| 644 | if ( '' === $title ) { |
| 645 | $title = __( '(untitled)', 'jetpack-seo' ); |
| 646 | } |
| 647 | // Escape brackets so a title can't break the `[title](url)` link syntax. |
| 648 | $title = str_replace( array( '[', ']' ), array( '\[', '\]' ), $title ); |
| 649 | $url = esc_url_raw( (string) get_permalink( $post ) ); |
| 650 | $line = '- [' . $title . '](' . $url . ')'; |
| 651 | |
| 652 | $summary = self::summary( $post ); |
| 653 | if ( '' !== $summary ) { |
| 654 | $line .= ': ' . $summary; |
| 655 | } |
| 656 | $lines[] = $line; |
| 657 | } |
| 658 | return implode( "\n", $lines ); |
| 659 | } |
| 660 | |
| 661 | /** |
| 662 | * A short, single-line summary for a post: its SEO description when set, then |
| 663 | * its excerpt, otherwise a trimmed slice of the content. |
| 664 | * |
| 665 | * @param \WP_Post $post Post to summarize. |
| 666 | * @return string |
| 667 | */ |
| 668 | private static function summary( $post ) { |
| 669 | $seo_description = wp_trim_words( strip_shortcodes( wp_strip_all_tags( Post_Seo::get_custom_description( $post ) ) ), Post_Schema_Node::DESCRIPTION_MAX_WORDS, '' ); |
| 670 | |
| 671 | // The SEO description and manual excerpt are deliberate public summaries; a body gate does not withhold them. |
| 672 | if ( '' !== $seo_description ) { |
| 673 | $raw = $seo_description; |
| 674 | } elseif ( has_excerpt( $post ) ) { |
| 675 | $raw = get_the_excerpt( $post ); |
| 676 | } elseif ( Content_Gate::is_gated( $post ) ) { |
| 677 | $teaser = Content_Gate::public_teaser( $post ); |
| 678 | if ( '' === $teaser ) { |
| 679 | return ''; |
| 680 | } |
| 681 | $raw = wp_trim_words( wp_strip_all_tags( strip_shortcodes( $teaser ) ), 30, '' ); |
| 682 | } else { |
| 683 | $raw = wp_trim_words( wp_strip_all_tags( strip_shortcodes( get_the_excerpt( $post ) ) ), 30, '' ); |
| 684 | } |
| 685 | |
| 686 | // Collapse whitespace so a link line stays on one row. |
| 687 | return self::plain_text( $raw ); |
| 688 | } |
| 689 | |
| 690 | /** |
| 691 | * Reduce a WordPress display string to one line of plain text. |
| 692 | * |
| 693 | * @param string $text Text that may hold tags and HTML entities. |
| 694 | * @return string |
| 695 | */ |
| 696 | private static function plain_text( $text ) { |
| 697 | // Do not reorder: decoding can reveal whitespace (` `), and text like `<b>` isn't a tag. |
| 698 | $text = \WP_HTML_Decoder::decode_text_node( wp_strip_all_tags( $text ) ); |
| 699 | return trim( preg_replace( '/\s+/', ' ', $text ) ); |
| 700 | } |
| 701 | } |