Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/hostinge.../includes
File: LlmsTxtGenerator.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace Hostinger;
[2] Fix | Delete
[3] Fix | Delete
use Hostinger\Admin\PluginSettings;
[4] Fix | Delete
[5] Fix | Delete
defined( 'ABSPATH' ) || exit;
[6] Fix | Delete
[7] Fix | Delete
class LlmsTxtGenerator {
[8] Fix | Delete
[9] Fix | Delete
public const HOSTINGER_LLMSTXT_FILENAME = 'llms.txt';
[10] Fix | Delete
public const HOSTINGER_LLMSTXT_SIGNATURE = '[comment]: # (Generated by Hostinger Tools Plugin)';
[11] Fix | Delete
[12] Fix | Delete
protected const HOSTINGER_LLMSTXT_SUPPORTED_POST_TYPES = array(
[13] Fix | Delete
'post',
[14] Fix | Delete
'page',
[15] Fix | Delete
);
[16] Fix | Delete
protected const UTF8_BOM = "\xEF\xBB\xBF";
[17] Fix | Delete
[18] Fix | Delete
protected PluginSettings $plugin_settings;
[19] Fix | Delete
[20] Fix | Delete
[21] Fix | Delete
public function __construct( PluginSettings $plugin_settings ) {
[22] Fix | Delete
$this->plugin_settings = $plugin_settings;
[23] Fix | Delete
add_action( 'init', array( $this, 'init' ) );
[24] Fix | Delete
}
[25] Fix | Delete
[26] Fix | Delete
public function init(): void {
[27] Fix | Delete
if ( wp_doing_ajax() || wp_doing_cron() || ! current_user_can( 'manage_options' ) ) {
[28] Fix | Delete
return;
[29] Fix | Delete
}
[30] Fix | Delete
[31] Fix | Delete
$settings = $this->plugin_settings->get_plugin_settings();
[32] Fix | Delete
[33] Fix | Delete
if ( $settings->get_enable_llms_txt() && ! $this->llmstxt_file_exists() ) {
[34] Fix | Delete
$this->generate();
[35] Fix | Delete
}
[36] Fix | Delete
[37] Fix | Delete
$this->init_hooks();
[38] Fix | Delete
}
[39] Fix | Delete
[40] Fix | Delete
public function on_settings_update( bool $is_enabled ): void {
[41] Fix | Delete
if ( $is_enabled ) {
[42] Fix | Delete
$this->generate();
[43] Fix | Delete
} else {
[44] Fix | Delete
$this->delete();
[45] Fix | Delete
}
[46] Fix | Delete
}
[47] Fix | Delete
[48] Fix | Delete
public function is_user_generated_file(): bool {
[49] Fix | Delete
if ( ! $this->llmstxt_file_exists() ) {
[50] Fix | Delete
return false;
[51] Fix | Delete
}
[52] Fix | Delete
[53] Fix | Delete
global $wp_filesystem;
[54] Fix | Delete
$this->init_wp_filesystem();
[55] Fix | Delete
$content = $wp_filesystem->get_contents( $this->get_llmstxt_file_path() );
[56] Fix | Delete
[57] Fix | Delete
if ( $content === false ) {
[58] Fix | Delete
return false;
[59] Fix | Delete
}
[60] Fix | Delete
[61] Fix | Delete
return ! str_contains( $content, self::HOSTINGER_LLMSTXT_SIGNATURE );
[62] Fix | Delete
}
[63] Fix | Delete
[64] Fix | Delete
public function on_post_status_change( string $new_status, string $old_status, \WP_Post $post ): void {
[65] Fix | Delete
if ( ! $this->is_post_type_supported( $post->post_type ) ) {
[66] Fix | Delete
return;
[67] Fix | Delete
}
[68] Fix | Delete
[69] Fix | Delete
if ( $new_status === 'publish' || $old_status === 'publish' ) {
[70] Fix | Delete
$this->generate();
[71] Fix | Delete
}
[72] Fix | Delete
}
[73] Fix | Delete
[74] Fix | Delete
public function on_blog_change( mixed $old_value, mixed $new_value ): void {
[75] Fix | Delete
if ( $old_value !== $new_value ) {
[76] Fix | Delete
$this->generate();
[77] Fix | Delete
}
[78] Fix | Delete
}
[79] Fix | Delete
[80] Fix | Delete
public function get_content(): string {
[81] Fix | Delete
$content = self::UTF8_BOM;
[82] Fix | Delete
$content .= $this->inject_title();
[83] Fix | Delete
$content .= $this->inject_site_description();
[84] Fix | Delete
$content .= $this->inject_items( $this->get_by_post_type( 'post' ), 'Posts' );
[85] Fix | Delete
$content .= $this->inject_items( $this->get_by_post_type( 'page' ), 'Pages' );
[86] Fix | Delete
$content .= $this->inject_signature();
[87] Fix | Delete
[88] Fix | Delete
return $content;
[89] Fix | Delete
}
[90] Fix | Delete
[91] Fix | Delete
public function init_hooks(): void {
[92] Fix | Delete
add_action( 'transition_post_status', array( $this, 'on_post_status_change' ), 10, 3 );
[93] Fix | Delete
add_action( 'hostinger_tools_setting_enable_llms_txt_update', array( $this, 'on_settings_update' ) );
[94] Fix | Delete
add_action( 'update_option_blogname', array( $this, 'on_blog_change' ), 10, 2 );
[95] Fix | Delete
add_action( 'update_option_blogdescription', array( $this, 'on_blog_change' ), 10, 2 );
[96] Fix | Delete
}
[97] Fix | Delete
[98] Fix | Delete
protected function generate(): void {
[99] Fix | Delete
if ( $this->is_user_generated_file() ) {
[100] Fix | Delete
return;
[101] Fix | Delete
}
[102] Fix | Delete
[103] Fix | Delete
global $wp_filesystem;
[104] Fix | Delete
$this->init_wp_filesystem();
[105] Fix | Delete
$wp_filesystem->put_contents( $this->get_llmstxt_file_path(), $this->get_content() );
[106] Fix | Delete
}
[107] Fix | Delete
[108] Fix | Delete
protected function delete(): void {
[109] Fix | Delete
if ( $this->llmstxt_file_exists() && ! $this->is_user_generated_file() ) {
[110] Fix | Delete
global $wp_filesystem;
[111] Fix | Delete
$this->init_wp_filesystem();
[112] Fix | Delete
$wp_filesystem->delete( $this->get_llmstxt_file_path() );
[113] Fix | Delete
}
[114] Fix | Delete
}
[115] Fix | Delete
[116] Fix | Delete
protected function get_llmstxt_file_path(): string {
[117] Fix | Delete
return ABSPATH . self::HOSTINGER_LLMSTXT_FILENAME;
[118] Fix | Delete
}
[119] Fix | Delete
[120] Fix | Delete
protected function llmstxt_file_exists(): bool {
[121] Fix | Delete
return file_exists( $this->get_llmstxt_file_path() );
[122] Fix | Delete
}
[123] Fix | Delete
[124] Fix | Delete
protected function is_post_type_supported( string $post_type ): bool {
[125] Fix | Delete
return in_array( $post_type, self::HOSTINGER_LLMSTXT_SUPPORTED_POST_TYPES, true );
[126] Fix | Delete
}
[127] Fix | Delete
[128] Fix | Delete
protected function get_by_post_type( string $post_type, int $limit = 100 ): array {
[129] Fix | Delete
$args = array(
[130] Fix | Delete
'post_type' => $post_type,
[131] Fix | Delete
'post_status' => 'publish',
[132] Fix | Delete
'fields' => 'ids',
[133] Fix | Delete
'posts_per_page' => apply_filters( 'hostinger_llmstext_item_limit', $limit, $post_type ),
[134] Fix | Delete
);
[135] Fix | Delete
[136] Fix | Delete
return get_posts( $args );
[137] Fix | Delete
}
[138] Fix | Delete
[139] Fix | Delete
protected function inject_site_description(): string {
[140] Fix | Delete
$description = get_bloginfo( 'description' );
[141] Fix | Delete
return $description ? "> $description\n\n" : '';
[142] Fix | Delete
}
[143] Fix | Delete
[144] Fix | Delete
protected function inject_title(): string {
[145] Fix | Delete
$title = get_bloginfo( 'name' ) ? get_bloginfo( 'name' ) : site_url();
[146] Fix | Delete
return "# $title\n\n";
[147] Fix | Delete
}
[148] Fix | Delete
[149] Fix | Delete
protected function inject_signature(): string {
[150] Fix | Delete
return "\n\n" . self::HOSTINGER_LLMSTXT_SIGNATURE;
[151] Fix | Delete
}
[152] Fix | Delete
[153] Fix | Delete
protected function inject_items( array $items, string $title ): string {
[154] Fix | Delete
if ( empty( $items ) ) {
[155] Fix | Delete
return '';
[156] Fix | Delete
}
[157] Fix | Delete
[158] Fix | Delete
$content = "\n## $title\n\n";
[159] Fix | Delete
[160] Fix | Delete
foreach ( $items as $item ) {
[161] Fix | Delete
$post = get_post( $item );
[162] Fix | Delete
$title = $post->post_title;
[163] Fix | Delete
$permalink = get_permalink( $post );
[164] Fix | Delete
$excerpt = $this->prepare_excerpt( $post );
[165] Fix | Delete
[166] Fix | Delete
$content .= "- [$title]($permalink)";
[167] Fix | Delete
if ( $excerpt ) {
[168] Fix | Delete
$content .= ": $excerpt";
[169] Fix | Delete
}
[170] Fix | Delete
[171] Fix | Delete
$content .= "\n";
[172] Fix | Delete
}
[173] Fix | Delete
[174] Fix | Delete
return $content;
[175] Fix | Delete
}
[176] Fix | Delete
[177] Fix | Delete
protected function prepare_excerpt( \WP_Post $item ): string {
[178] Fix | Delete
return html_entity_decode( wp_trim_excerpt( $item->post_excerpt, $item ) );
[179] Fix | Delete
}
[180] Fix | Delete
[181] Fix | Delete
protected function init_wp_filesystem() {
[182] Fix | Delete
global $wp_filesystem;
[183] Fix | Delete
require_once ABSPATH . '/wp-admin/includes/file.php';
[184] Fix | Delete
WP_Filesystem();
[185] Fix | Delete
}
[186] Fix | Delete
}
[187] Fix | Delete
[188] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function