Commit inicial powerservice
This commit is contained in:
@@ -0,0 +1,355 @@
|
||||
<?php
|
||||
/**
|
||||
* The llms.txt module.
|
||||
*
|
||||
* @package RankMath
|
||||
* @subpackage RankMath\LLMS
|
||||
*/
|
||||
|
||||
namespace RankMath\LLMS;
|
||||
|
||||
use RankMath\Helper;
|
||||
use RankMath\Traits\Hooker;
|
||||
use RankMath\Helpers\Arr;
|
||||
use RankMath\Sitemap\Sitemap;
|
||||
use RankMath\Sitemap\Router;
|
||||
use RankMath\Helpers\Url;
|
||||
use WP_Query;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* LLMS_Txt class.
|
||||
*/
|
||||
class LLMS_Txt {
|
||||
use Hooker;
|
||||
|
||||
/**
|
||||
* Class constructor.
|
||||
*
|
||||
* Registers hooks and filters for the llms.txt module.
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->action( 'init', 'add_rewrite_rule' );
|
||||
$this->action( 'template_redirect', 'maybe_serve_llms_txt' );
|
||||
$this->filter( 'rank_math/settings/general', 'add_settings' );
|
||||
$this->action( 'wp_loaded', 'remove_canonical_redirect' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the canonical redirect for the llms.txt file.
|
||||
*
|
||||
* @hook wp_loaded
|
||||
* @return void
|
||||
*/
|
||||
public function remove_canonical_redirect() {
|
||||
if ( strpos( Url::get_current_url(), '/llms.txt' ) !== false ) {
|
||||
remove_filter( 'template_redirect', 'redirect_canonical' );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the llms.txt settings tab to the General settings panel.
|
||||
*
|
||||
* @hook rank_math/settings/general
|
||||
*
|
||||
* @param array $tabs Option panel tabs.
|
||||
* @return array Modified tabs array with llms.txt tab added.
|
||||
*/
|
||||
public function add_settings( $tabs ) {
|
||||
Arr::insert(
|
||||
$tabs,
|
||||
[
|
||||
'llms' => [
|
||||
'icon' => 'rm-icon rm-icon-bot',
|
||||
'title' => esc_html__( 'Edit llms.txt', 'seo-by-rank-math' ),
|
||||
'desc' => esc_html__( 'Configure your llms.txt file for custom crawling/indexing rules.', 'seo-by-rank-math' ),
|
||||
'file' => __DIR__ . '/options.php',
|
||||
'classes' => 'rank-math-advanced-option',
|
||||
'json' => [
|
||||
'llmsUrl' => esc_url( home_url( '/llms.txt' ) ),
|
||||
],
|
||||
],
|
||||
],
|
||||
5
|
||||
);
|
||||
return $tabs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the rewrite rule and query var for llms.txt.
|
||||
*
|
||||
* @hook init
|
||||
* @return void
|
||||
*/
|
||||
public function add_rewrite_rule() {
|
||||
add_rewrite_rule( '^llms\.txt$', 'index.php?llms_txt=1', 'top' );
|
||||
add_rewrite_tag( '%llms_txt%', '1' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Serve the llms.txt file if the endpoint is hit.
|
||||
*
|
||||
* @hook template_redirect
|
||||
* @return void
|
||||
*/
|
||||
public function maybe_serve_llms_txt() {
|
||||
if ( intval( get_query_var( 'llms_txt' ) ) !== 1 ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( substr( Url::get_current_url(), -1 ) === '/' ) {
|
||||
wp_safe_redirect( home_url( '/llms.txt' ) );
|
||||
exit;
|
||||
}
|
||||
|
||||
$this->output();
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Output the llms.txt file content in Markdown format.
|
||||
*
|
||||
* @action rank_math/llms_txt/before_output Fires before llms.txt output is sent.
|
||||
* @action rank_math/llms_txt/after_output Fires after llms.txt output is sent.
|
||||
* @filter rank_math/llms_txt/posts_query_args Filter WP_Query args for posts.
|
||||
* @filter rank_math/llms_txt/posts Filter post IDs to include.
|
||||
* @filter rank_math/llms_txt/terms Filter term IDs to include.
|
||||
* @filter rank_math/llms_txt/extra_content Filter extra content output.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function output() {
|
||||
if ( ! headers_sent() ) {
|
||||
header( 'Content-Type: text/plain; charset=utf-8' );
|
||||
header( 'X-Robots-Tag: noindex, nofollow', true );
|
||||
}
|
||||
|
||||
/**
|
||||
* Fires before the llms.txt output is sent to the browser.
|
||||
*
|
||||
* @since 1.0.250
|
||||
*/
|
||||
$this->do_action( 'llms_txt/before_output' );
|
||||
|
||||
$limit = absint( Helper::get_settings( 'general.llms_limit', 100 ) );
|
||||
$this->add_header_content();
|
||||
$this->add_post_types_data( $limit );
|
||||
$this->add_taxonomies_data( $limit );
|
||||
$this->add_extra_content();
|
||||
|
||||
/**
|
||||
* Fires after the llms.txt output is sent to the browser.
|
||||
*
|
||||
* @since 1.0.250
|
||||
*/
|
||||
$this->do_action( 'llms_txt/after_output' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds header content to the llms.txt output.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function add_header_content() {
|
||||
if ( ! $this->do_filter( 'llms_txt/remove_credit', false ) ) {
|
||||
$this->output_line( 'Generated by Rank Math SEO, this is an llms.txt file designed to help LLMs better understand and index this website.' );
|
||||
$this->output_line( '' );
|
||||
}
|
||||
|
||||
$site_title = $this->format_text( Helper::get_settings( 'titles.knowledgegraph_name', get_bloginfo( 'name' ) ) );
|
||||
$summary = $this->format_text( Helper::replace_vars( Helper::get_settings( 'general.llms_summary', '' ) ) );
|
||||
|
||||
$this->output_line( '# ' . $site_title );
|
||||
$this->output_line( '' );
|
||||
|
||||
if ( $summary ) {
|
||||
$this->output_line( '> ' . $summary );
|
||||
$this->output_line( '' );
|
||||
}
|
||||
|
||||
if ( ! Helper::is_module_active( 'sitemap' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$sitemap_url = Router::get_base_url( Sitemap::get_sitemap_index_slug() . '.xml' );
|
||||
$this->output_line( '## Sitemaps' );
|
||||
$this->output_line( '- [XML Sitemap](' . esc_url_raw( $sitemap_url ) . '): Includes all crawlable and indexable pages.' );
|
||||
$this->output_line( '' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds post type data to the llms.txt output.
|
||||
*
|
||||
* @param int $limit The maximum number of posts to include per post type.
|
||||
* @return void
|
||||
*/
|
||||
private function add_post_types_data( $limit ) {
|
||||
$post_types = Helper::get_settings( 'general.llms_post_types', [] );
|
||||
if ( empty( $post_types ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ( $post_types as $post_type ) {
|
||||
$args = [
|
||||
'post_type' => $post_type,
|
||||
'post_status' => 'publish',
|
||||
'posts_per_page' => $limit,
|
||||
'no_found_rows' => true,
|
||||
'has_password' => false,
|
||||
];
|
||||
|
||||
/**
|
||||
* Filter the WP_Query arguments used to fetch posts for llms.txt.
|
||||
*
|
||||
* @since 1.0.250
|
||||
* @param array $args The WP_Query arguments.
|
||||
* @return array Modified WP_Query arguments.
|
||||
*/
|
||||
$args = $this->do_filter( 'llms_txt/posts_query_args', $args );
|
||||
$query = new \WP_Query( $args );
|
||||
|
||||
/**
|
||||
* Filter the list of post IDs to be included in llms.txt for a post type.
|
||||
*
|
||||
* @since 1.0.250
|
||||
* @param array $posts List of posts.
|
||||
* @param array $args The WP_Query arguments.
|
||||
* @return array Modified list of post IDs.
|
||||
*/
|
||||
$posts = $this->do_filter( 'llms_txt/posts', $query->posts, $args );
|
||||
if ( empty( $posts ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$label = $this->format_text( get_post_type_object( $post_type )->labels->name );
|
||||
$this->output_line( '## ' . $label );
|
||||
|
||||
foreach ( $posts as $object ) {
|
||||
if ( ! Helper::is_post_indexable( $object ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$title = $this->format_text( get_the_title( $object ) );
|
||||
$link = esc_url_raw( get_permalink( $object ) );
|
||||
$object->post_content = do_shortcode( $object->post_content );
|
||||
$desc = Helper::replace_vars( '%excerpt%', $object );
|
||||
|
||||
/**
|
||||
* Filter the description used for a post entry in llms.txt. Defaults to the post excerpt.
|
||||
*
|
||||
* @since 1.0.276
|
||||
* @param string $desc The default description (post excerpt).
|
||||
* @param \WP_Post $object The post object.
|
||||
* @return string Modified description.
|
||||
*/
|
||||
$desc = $this->format_text( $this->do_filter( 'llms_txt/post_description', $desc, $object ) );
|
||||
|
||||
$this->output_line(
|
||||
$desc
|
||||
? '- [' . $title . '](' . $link . '): ' . $desc
|
||||
: '- [' . $title . '](' . $link . ')'
|
||||
);
|
||||
}
|
||||
$this->output_line( '' );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds taxonomy data to the llms.txt output.
|
||||
*
|
||||
* @param int $limit The maximum number of terms to include per taxonomy.
|
||||
* @return void
|
||||
*/
|
||||
private function add_taxonomies_data( $limit ) {
|
||||
$taxonomies = Helper::get_settings( 'general.llms_taxonomies', [] );
|
||||
if ( empty( $taxonomies ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ( $taxonomies as $taxonomy ) {
|
||||
$tax_obj = get_taxonomy( $taxonomy );
|
||||
if ( ! $tax_obj ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$terms = get_terms(
|
||||
[
|
||||
'taxonomy' => $taxonomy,
|
||||
'hide_empty' => true,
|
||||
'number' => $limit,
|
||||
]
|
||||
);
|
||||
|
||||
/**
|
||||
* Filter the list of terms to be included in llms.txt for a taxonomy.
|
||||
*
|
||||
* @since 1.0.250
|
||||
* @param array $terms List of terms.
|
||||
* @param string $taxonomy Taxonomy name.
|
||||
* @return array Modified list of term IDs.
|
||||
*/
|
||||
$terms = $this->do_filter( 'llms_txt/terms', $terms, $taxonomy );
|
||||
if ( empty( $terms ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$label = $this->format_text( $tax_obj->labels->name );
|
||||
$this->output_line( '## ' . $label );
|
||||
foreach ( $terms as $term ) {
|
||||
if ( $term && Helper::is_term_indexable( $term ) ) {
|
||||
$name = $this->format_text( $term->name );
|
||||
$link = esc_url_raw( get_term_link( $term ) );
|
||||
$desc = $this->format_text( $term->description );
|
||||
|
||||
$this->output_line(
|
||||
$desc
|
||||
? '- [' . $name . '](' . $link . '): ' . $desc
|
||||
: '- [' . $name . '](' . $link . ')'
|
||||
);
|
||||
}
|
||||
}
|
||||
$this->output_line( '' );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds extra content to the end of the llms.txt output.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function add_extra_content() {
|
||||
/**
|
||||
* Filter the extra content output at the end of llms.txt.
|
||||
*
|
||||
* @since 1.0.250
|
||||
* @param string $extra The extra content string.
|
||||
* @return string Modified extra content.
|
||||
*/
|
||||
$extra = $this->do_filter( 'llms_txt/extra_content', Helper::get_settings( 'general.llms_extra_content', '' ) );
|
||||
if ( ! empty( $extra ) ) {
|
||||
$this->output_line( $this->format_text( $extra ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalizes text for plain-text output: strips tags, decodes entities.
|
||||
* Decode first, so an entity-encoded tag (e.g. <script>) can't survive stripping.
|
||||
*
|
||||
* @param string $text The text to normalize.
|
||||
* @return string Normalized plain text.
|
||||
*/
|
||||
private function format_text( $text ) {
|
||||
return trim( wp_strip_all_tags( html_entity_decode( (string) $text, ENT_QUOTES, 'UTF-8' ) ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Outputs a line with a trailing newline.
|
||||
*
|
||||
* @param string $content Plain text content; must already be sanitized (see format_text()).
|
||||
* @return void
|
||||
*/
|
||||
private function output_line( $content ) {
|
||||
echo $content . "\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Plain text, not HTML; see format_text().
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
<?php // Silence is golden.
|
||||
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
/**
|
||||
* The llms.txt settings.
|
||||
*
|
||||
* @package RankMath
|
||||
* @subpackage RankMath\Settings
|
||||
*/
|
||||
|
||||
use RankMath\Helper;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
$cmb->add_field(
|
||||
[
|
||||
'id' => 'llms_info',
|
||||
'type' => 'notice',
|
||||
'what' => 'info',
|
||||
'classes' => 'nob nopt rank-math-notice',
|
||||
'content' => sprintf(
|
||||
// Translators: placeholder is the llms.txt file URL.
|
||||
__( 'Your <code>llms.txt</code> file is available at <a href="%1$s" target="_blank">%2$s</a>.', 'seo-by-rank-math' ),
|
||||
esc_url( home_url( '/llms.txt' ) ),
|
||||
esc_html( home_url( '/llms.txt' ) )
|
||||
),
|
||||
]
|
||||
);
|
||||
|
||||
$post_types = Helper::choices_post_types();
|
||||
if ( isset( $post_types['attachment'] ) ) {
|
||||
unset( $post_types['attachment'] );
|
||||
}
|
||||
|
||||
$cmb->add_field(
|
||||
[
|
||||
'id' => 'llms_post_types',
|
||||
'name' => esc_html__( 'Select Post Types', 'seo-by-rank-math' ),
|
||||
'desc' => esc_html__( 'Select the post types to be included in the llms.txt file.', 'seo-by-rank-math' ),
|
||||
'type' => 'multicheck',
|
||||
'options' => $post_types,
|
||||
'classes' => 'rank-math-advanced-option',
|
||||
]
|
||||
);
|
||||
|
||||
$cmb->add_field(
|
||||
[
|
||||
'id' => 'llms_taxonomies',
|
||||
'name' => esc_html__( 'Select Taxonomies', 'seo-by-rank-math' ),
|
||||
'desc' => esc_html__( 'Select the taxonomies to be included in the llms.txt file.', 'seo-by-rank-math' ),
|
||||
'type' => 'multicheck',
|
||||
'options' => wp_list_pluck( Helper::get_accessible_taxonomies(), 'label', 'name' ),
|
||||
'classes' => 'rank-math-advanced-option',
|
||||
]
|
||||
);
|
||||
|
||||
$cmb->add_field(
|
||||
[
|
||||
'id' => 'llms_limit',
|
||||
'name' => esc_html__( 'Posts/Terms Limit', 'seo-by-rank-math' ),
|
||||
'desc' => esc_html__( 'Maximum number of links to include for each post type.', 'seo-by-rank-math' ),
|
||||
'type' => 'text',
|
||||
'classes' => 'small-text',
|
||||
'default' => 50,
|
||||
'attributes' => [
|
||||
'type' => 'number',
|
||||
'min' => 1,
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
$cmb->add_field(
|
||||
[
|
||||
'id' => 'llms_extra_content',
|
||||
'name' => esc_html__( 'Additional Content', 'seo-by-rank-math' ),
|
||||
'desc' => esc_html__( 'Add any extra text or links you\'d like to include in your llms.txt file manually.', 'seo-by-rank-math' ),
|
||||
'type' => 'textarea',
|
||||
'attributes' => [ 'rows' => 6 ],
|
||||
]
|
||||
);
|
||||
Reference in New Issue
Block a user