| @@ -3,8 +3,9 @@ | ||
| 3 | 3 | namespace Hostinger\LlmsTxtGenerator; |
| 4 | 4 | |
| 5 | 5 | use Hostinger\Admin\Jobs\LlmsTxtInjectContentJob; |
| 6 | 6 | use Hostinger\Admin\PluginSettings; |
| 7 | +use Hostinger\Helper; | |
| 7 | 8 | |
| 8 | 9 | defined( 'ABSPATH' ) || exit; |
| 9 | 10 | |
| 10 | 11 | class LlmsTxtGenerator { |
| @@ -9,9 +10,9 @@ | ||
| 9 | 10 | |
| 10 | 11 | class LlmsTxtGenerator { |
| 11 | 12 | |
| 12 | 13 | public const HOSTINGER_LLMSTXT_SIGNATURE = '[comment]: # (Generated by Hostinger Tools Plugin)'; |
| 13 | - protected const UTF8_BOM = "\xEF\xBB\xBF"; | |
| 14 | + public const HOSTINGER_LLMSTXT_GENERATED_VERSION = 'hostinger_llmstxt_generated_version'; | |
| 14 | 15 | public const HOSTINGER_LLMSTXT_SUPPORTED_POST_TYPES = array( |
| 15 | 16 | 'post' => 'Posts', |
| 16 | 17 | 'page' => 'Pages', |
| 17 | 18 | 'product' => 'Products', |
| @@ -47,8 +48,10 @@ | ||
| 47 | 48 | $this->generate(); |
| 48 | 49 | } |
| 49 | 50 | |
| 50 | 51 | public function init(): void { |
| 52 | + $this->init_hooks(); | |
| 53 | + | |
| 51 | 54 | if ( wp_doing_ajax() || wp_doing_cron() || ! current_user_can( 'manage_options' ) ) { |
| 52 | 55 | return; |
| 53 | 56 | } |
| 54 | 57 | |
| @@ -56,10 +59,21 @@ | ||
| 56 | 59 | |
| 57 | 60 | if ( $settings->get_enable_llms_txt() && ! $this->file_helper->llmstxt_file_exists() ) { |
| 58 | 61 | $this->generate(); |
| 59 | 62 | } |
| 63 | + } | |
| 60 | 64 | |
| 61 | - $this->init_hooks(); | |
| 65 | + public function maybe_regenerate_after_update(): void { | |
| 66 | + if ( wp_doing_ajax() || wp_doing_cron() || ! current_user_can( 'manage_options' ) ) { | |
| 67 | + return; | |
| 68 | + } | |
| 69 | + | |
| 70 | + if ( $this->has_generated_current_version() ) { | |
| 71 | + return; | |
| 72 | + } | |
| 73 | + | |
| 74 | + $this->record_generated_version(); | |
| 75 | + $this->generate(); | |
| 62 | 76 | } |
| 63 | 77 | |
| 64 | 78 | public function on_settings_update( bool $is_enabled ): void { |
| 65 | 79 | if ( $is_enabled ) { |
| @@ -85,10 +99,9 @@ | ||
| 85 | 99 | } |
| 86 | 100 | } |
| 87 | 101 | |
| 88 | 102 | public function get_content(): string { |
| 89 | - $content = self::UTF8_BOM; | |
| 90 | - $content .= $this->parser->inject_title(); | |
| 103 | + $content = $this->parser->inject_title(); | |
| 91 | 104 | $content .= $this->parser->inject_site_description(); |
| 92 | 105 | $content .= $this->parser->inject_items( $this->parser->get_by_post_type( 'post' ), 'Posts' ); |
| 93 | 106 | $content .= $this->parser->inject_items( $this->parser->get_by_post_type( 'page' ), 'Pages' ); |
| 94 | 107 | $content .= $this->maybe_inject_woocommerce_products(); |
| @@ -125,8 +138,11 @@ | ||
| 125 | 138 | return; |
| 126 | 139 | } |
| 127 | 140 | |
| 128 | 141 | $this->file_helper->create( $this->get_content() ); |
| 142 | + | |
| 143 | + $this->record_generated_version(); | |
| 144 | + | |
| 129 | 145 | do_action( LlmsTxtInjectContentJob::JOB_NAME, array( 'post_type' => 'post' ) ); |
| 130 | 146 | do_action( LlmsTxtInjectContentJob::JOB_NAME, array( 'post_type' => 'page' ) ); |
| 131 | 147 | |
| 132 | 148 | if ( $this->is_woocommerce_active() ) { |
| @@ -137,14 +153,22 @@ | ||
| 137 | 153 | protected function delete(): void { |
| 138 | 154 | $this->file_helper->delete(); |
| 139 | 155 | } |
| 140 | 156 | |
| 157 | + protected function has_generated_current_version(): bool { | |
| 158 | + return get_option( self::HOSTINGER_LLMSTXT_GENERATED_VERSION ) === HOSTINGER_WORDPRESS_PLUGIN_VERSION; | |
| 159 | + } | |
| 160 | + | |
| 161 | + protected function record_generated_version(): void { | |
| 162 | + update_option( self::HOSTINGER_LLMSTXT_GENERATED_VERSION, HOSTINGER_WORDPRESS_PLUGIN_VERSION ); | |
| 163 | + } | |
| 164 | + | |
| 141 | 165 | protected function is_post_type_supported( string $post_type ): bool { |
| 142 | 166 | return array_key_exists( $post_type, self::HOSTINGER_LLMSTXT_SUPPORTED_POST_TYPES ); |
| 143 | 167 | } |
| 144 | 168 | |
| 145 | 169 | protected function is_woocommerce_active(): bool { |
| 146 | - return ( is_null( $this->woocommerce_status ) && is_plugin_active( 'woocommerce/woocommerce.php' ) ) || $this->woocommerce_status === 'active'; | |
| 170 | + return ( is_null( $this->woocommerce_status ) && Helper::is_plugin_active( 'woocommerce/woocommerce' ) ) || $this->woocommerce_status === 'active'; | |
| 147 | 171 | } |
| 148 | 172 | |
| 149 | 173 | protected function maybe_inject_woocommerce_products(): string { |
| 150 | 174 | if ( ! $this->is_woocommerce_active() ) { |