PluginProbe
Hostinger Tools / 3.0.78
Hostinger Tools v3.0.78
3.0.78 3.0.77 3.0.76 3.0.75 3.0.74 3.0.73 3.0.72 3.0.71 3.0.70 3.0.69 3.0.68 3.0.67 3.0.66 1.8.1 1.8.2 1.8.3 1.9.1 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0.0 2.0.1 All 109 releases
hostinger / includes / LlmsTxtGenerator / LlmsTxtGenerator.php

LlmsTxtGenerator.php in Hostinger Tools 3.0.78, at includes/LlmsTxtGenerator/LlmsTxtGenerator.php

176 lines 5.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Hostinger\LlmsTxtGenerator;
4
5 use Hostinger\Admin\Jobs\LlmsTxtInjectContentJob;
6 use Hostinger\Admin\PluginSettings;
7 use Hostinger\Helper;
8
9 defined( 'ABSPATH' ) || exit;
10
11 class LlmsTxtGenerator {
12
13 public const HOSTINGER_LLMSTXT_SIGNATURE = '[comment]: # (Generated by Hostinger Tools Plugin)';
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 $this->init_hooks();
52
53 if ( wp_doing_ajax() || wp_doing_cron() || ! current_user_can( 'manage_options' ) ) {
54 return;
55 }
56
57 $settings = $this->plugin_settings->get_plugin_settings();
58
59 if ( $settings->get_enable_llms_txt() && ! $this->file_helper->llmstxt_file_exists() ) {
60 $this->generate();
61 }
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 = $this->parser->inject_title();
90 $content .= $this->parser->inject_site_description();
91 $content .= $this->parser->inject_items( $this->parser->get_by_post_type( 'post' ), 'Posts' );
92 $content .= $this->parser->inject_items( $this->parser->get_by_post_type( 'page' ), 'Pages' );
93 $content .= $this->maybe_inject_woocommerce_products();
94 $content .= $this->maybe_inject_optional_entries();
95 $content .= $this->parser->inject_signature();
96
97 return $content;
98 }
99
100 public function init_hooks(): void {
101 add_action( 'transition_post_status', array( $this, 'on_post_status_change' ), 10, 3 );
102 add_action( 'hostinger_tools_settings_saved', array( $this, 'on_settings_saved' ) );
103 add_action( 'update_option_blogname', array( $this, 'on_blog_change' ), 10, 2 );
104 add_action( 'update_option_blogdescription', array( $this, 'on_blog_change' ), 10, 2 );
105 add_action( 'activate_woocommerce/woocommerce.php', array( $this, 'on_woocommerce_activation' ) );
106 add_action( 'deactivate_woocommerce/woocommerce.php', array( $this, 'on_woocommerce_deactivation' ) );
107 }
108
109 public function on_settings_saved( array $settings ): void {
110 if ( $settings['enable_llms_txt'] ) {
111 $this->generate();
112 } else {
113 $this->delete();
114 }
115 }
116
117 public function generate(): void {
118 $settings = $this->plugin_settings->get_plugin_settings();
119 if ( ! $settings->get_enable_llms_txt() ) {
120 return;
121 }
122
123 if ( $this->file_helper->is_user_generated_file() ) {
124 return;
125 }
126
127 $this->file_helper->create( $this->get_content() );
128 do_action( LlmsTxtInjectContentJob::JOB_NAME, array( 'post_type' => 'post' ) );
129 do_action( LlmsTxtInjectContentJob::JOB_NAME, array( 'post_type' => 'page' ) );
130
131 if ( $this->is_woocommerce_active() ) {
132 do_action( LlmsTxtInjectContentJob::JOB_NAME, array( 'post_type' => 'product' ) );
133 }
134 }
135
136 protected function delete(): void {
137 $this->file_helper->delete();
138 }
139
140 protected function is_post_type_supported( string $post_type ): bool {
141 return array_key_exists( $post_type, self::HOSTINGER_LLMSTXT_SUPPORTED_POST_TYPES );
142 }
143
144 protected function is_woocommerce_active(): bool {
145 return ( is_null( $this->woocommerce_status ) && Helper::is_plugin_active( 'woocommerce/woocommerce' ) ) || $this->woocommerce_status === 'active';
146 }
147
148 protected function maybe_inject_woocommerce_products(): string {
149 if ( ! $this->is_woocommerce_active() ) {
150 return '';
151 }
152
153 return $this->parser->inject_items( $this->parser->get_by_post_type( 'product' ), 'Products' );
154 }
155
156 protected function maybe_inject_optional_entries(): string {
157 $entries = array();
158 $mcp_entry = $this->maybe_inject_mcp_agent_entry();
159
160 if ( $mcp_entry ) {
161 $entries[] = $mcp_entry;
162 }
163
164 return $this->parser->inject_optional_entries( $entries );
165 }
166
167 protected function maybe_inject_mcp_agent_entry(): string {
168 $settings = $this->plugin_settings->get_plugin_settings();
169 if ( ! $settings->get_optin_mcp() ) {
170 return '';
171 }
172
173 return $this->parser->inject_mcp_agent_entry();
174 }
175 }
176