| 1 |
<?php |
| 2 |
|
| 3 |
namespace Hostinger\LlmsTxtGenerator; |
| 4 |
|
| 5 |
use Hostinger\Admin\Jobs\LlmsTxtInjectContentJob; |
| 6 |
use Hostinger\Admin\PluginSettings; |
| 7 |
|
| 8 |
defined( 'ABSPATH' ) || exit; |
| 9 |
|
| 10 |
class LlmsTxtGenerator { |
| 11 |
|
| 12 |
public const HOSTINGER_LLMSTXT_SIGNATURE = '[comment]: # (Generated by Hostinger Tools Plugin)'; |
| 13 |
protected const UTF8_BOM = "\xEF\xBB\xBF"; |
| 14 |
public const HOSTINGER_LLMSTXT_SUPPORTED_POST_TYPES = array( |
| 15 |
'post' => 'Posts', |
| 16 |
'page' => 'Pages', |
| 17 |
'product' => 'Products', |
| 18 |
); |
| 19 |
|
| 20 |
/** |
| 21 |
* Activation and deactivation hooks appears too early, |
| 22 |
* so the plugin status is not available yet. |
| 23 |
* |
| 24 |
* That's why we use a flag this->woocommerce_status to know the plugin status. |
| 25 |
* |
| 26 |
* @var string|null |
| 27 |
*/ |
| 28 |
protected ?string $woocommerce_status = null; |
| 29 |
protected PluginSettings $plugin_settings; |
| 30 |
protected LlmsTxtFileHelper $file_helper; |
| 31 |
protected LlmsTxtParser $parser; |
| 32 |
|
| 33 |
public function __construct( PluginSettings $plugin_settings, LlmsTxtFileHelper $llmstxt_file_helper, LlmsTxtParser $llms_txt_parser ) { |
| 34 |
$this->plugin_settings = $plugin_settings; |
| 35 |
$this->file_helper = $llmstxt_file_helper; |
| 36 |
$this->parser = $llms_txt_parser; |
| 37 |
add_action( 'init', array( $this, 'init' ) ); |
| 38 |
} |
| 39 |
|
| 40 |
public function on_woocommerce_activation(): void { |
| 41 |
$this->woocommerce_status = 'active'; |
| 42 |
$this->generate(); |
| 43 |
} |
| 44 |
|
| 45 |
public function on_woocommerce_deactivation(): void { |
| 46 |
$this->woocommerce_status = 'inactive'; |
| 47 |
$this->generate(); |
| 48 |
} |
| 49 |
|
| 50 |
public function init(): void { |
| 51 |
if ( wp_doing_ajax() || wp_doing_cron() || ! current_user_can( 'manage_options' ) ) { |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
$settings = $this->plugin_settings->get_plugin_settings(); |
| 56 |
|
| 57 |
if ( $settings->get_enable_llms_txt() && ! $this->file_helper->llmstxt_file_exists() ) { |
| 58 |
$this->generate(); |
| 59 |
} |
| 60 |
|
| 61 |
$this->init_hooks(); |
| 62 |
} |
| 63 |
|
| 64 |
public function on_settings_update( bool $is_enabled ): void { |
| 65 |
if ( $is_enabled ) { |
| 66 |
$this->generate(); |
| 67 |
} else { |
| 68 |
$this->delete(); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
public function on_post_status_change( string $new_status, string $old_status, \WP_Post $post ): void { |
| 73 |
if ( ! $this->is_post_type_supported( $post->post_type ) ) { |
| 74 |
return; |
| 75 |
} |
| 76 |
|
| 77 |
if ( $new_status === 'publish' || $old_status === 'publish' ) { |
| 78 |
$this->generate(); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
public function on_blog_change( mixed $old_value, mixed $new_value ): void { |
| 83 |
if ( $old_value !== $new_value ) { |
| 84 |
$this->generate(); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
public function get_content(): string { |
| 89 |
$content = self::UTF8_BOM; |
| 90 |
$content .= $this->parser->inject_title(); |
| 91 |
$content .= $this->parser->inject_site_description(); |
| 92 |
$content .= $this->parser->inject_items( $this->parser->get_by_post_type( 'post' ), 'Posts' ); |
| 93 |
$content .= $this->parser->inject_items( $this->parser->get_by_post_type( 'page' ), 'Pages' ); |
| 94 |
$content .= $this->maybe_inject_woocommerce_products(); |
| 95 |
$content .= $this->maybe_inject_optional_entries(); |
| 96 |
$content .= $this->parser->inject_signature(); |
| 97 |
|
| 98 |
return $content; |
| 99 |
} |
| 100 |
|
| 101 |
public function init_hooks(): void { |
| 102 |
add_action( 'transition_post_status', array( $this, 'on_post_status_change' ), 10, 3 ); |
| 103 |
add_action( 'hostinger_tools_settings_saved', array( $this, 'on_settings_saved' ) ); |
| 104 |
add_action( 'update_option_blogname', array( $this, 'on_blog_change' ), 10, 2 ); |
| 105 |
add_action( 'update_option_blogdescription', array( $this, 'on_blog_change' ), 10, 2 ); |
| 106 |
add_action( 'activate_woocommerce/woocommerce.php', array( $this, 'on_woocommerce_activation' ) ); |
| 107 |
add_action( 'deactivate_woocommerce/woocommerce.php', array( $this, 'on_woocommerce_deactivation' ) ); |
| 108 |
} |
| 109 |
|
| 110 |
public function on_settings_saved( array $settings ): void { |
| 111 |
if ( $settings['enable_llms_txt'] ) { |
| 112 |
$this->generate(); |
| 113 |
} else { |
| 114 |
$this->delete(); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
public function generate(): void { |
| 119 |
$settings = $this->plugin_settings->get_plugin_settings(); |
| 120 |
if ( ! $settings->get_enable_llms_txt() ) { |
| 121 |
return; |
| 122 |
} |
| 123 |
|
| 124 |
if ( $this->file_helper->is_user_generated_file() ) { |
| 125 |
return; |
| 126 |
} |
| 127 |
|
| 128 |
$this->file_helper->create( $this->get_content() ); |
| 129 |
do_action( LlmsTxtInjectContentJob::JOB_NAME, array( 'post_type' => 'post' ) ); |
| 130 |
do_action( LlmsTxtInjectContentJob::JOB_NAME, array( 'post_type' => 'page' ) ); |
| 131 |
|
| 132 |
if ( $this->is_woocommerce_active() ) { |
| 133 |
do_action( LlmsTxtInjectContentJob::JOB_NAME, array( 'post_type' => 'product' ) ); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
protected function delete(): void { |
| 138 |
$this->file_helper->delete(); |
| 139 |
} |
| 140 |
|
| 141 |
protected function is_post_type_supported( string $post_type ): bool { |
| 142 |
return array_key_exists( $post_type, self::HOSTINGER_LLMSTXT_SUPPORTED_POST_TYPES ); |
| 143 |
} |
| 144 |
|
| 145 |
protected function is_woocommerce_active(): bool { |
| 146 |
return ( is_null( $this->woocommerce_status ) && is_plugin_active( 'woocommerce/woocommerce.php' ) ) || $this->woocommerce_status === 'active'; |
| 147 |
} |
| 148 |
|
| 149 |
protected function maybe_inject_woocommerce_products(): string { |
| 150 |
if ( ! $this->is_woocommerce_active() ) { |
| 151 |
return ''; |
| 152 |
} |
| 153 |
|
| 154 |
return $this->parser->inject_items( $this->parser->get_by_post_type( 'product' ), 'Products' ); |
| 155 |
} |
| 156 |
|
| 157 |
protected function maybe_inject_optional_entries(): string { |
| 158 |
$entries = array(); |
| 159 |
$mcp_entry = $this->maybe_inject_mcp_agent_entry(); |
| 160 |
|
| 161 |
if ( $mcp_entry ) { |
| 162 |
$entries[] = $mcp_entry; |
| 163 |
} |
| 164 |
|
| 165 |
return $this->parser->inject_optional_entries( $entries ); |
| 166 |
} |
| 167 |
|
| 168 |
protected function maybe_inject_mcp_agent_entry(): string { |
| 169 |
$settings = $this->plugin_settings->get_plugin_settings(); |
| 170 |
if ( ! $settings->get_optin_mcp() ) { |
| 171 |
return ''; |
| 172 |
} |
| 173 |
|
| 174 |
return $this->parser->inject_mcp_agent_entry(); |
| 175 |
} |
| 176 |
} |
| 177 |
|