| @@ -10,9 +10,9 @@ | ||
| 10 | 10 | |
| 11 | 11 | class LlmsTxtGenerator { |
| 12 | 12 | |
| 13 | 13 | public const HOSTINGER_LLMSTXT_SIGNATURE = '[comment]: # (Generated by Hostinger Tools Plugin)'; |
| 14 | - protected const UTF8_BOM = "\xEF\xBB\xBF"; | |
| 14 | + public const HOSTINGER_LLMSTXT_GENERATED_VERSION = 'hostinger_llmstxt_generated_version'; | |
| 15 | 15 | public const HOSTINGER_LLMSTXT_SUPPORTED_POST_TYPES = array( |
| 16 | 16 | 'post' => 'Posts', |
| 17 | 17 | 'page' => 'Pages', |
| 18 | 18 | 'product' => 'Products', |
| @@ -61,8 +61,21 @@ | ||
| 61 | 61 | $this->generate(); |
| 62 | 62 | } |
| 63 | 63 | } |
| 64 | 64 | |
| 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(); | |
| 76 | + } | |
| 77 | + | |
| 65 | 78 | public function on_settings_update( bool $is_enabled ): void { |
| 66 | 79 | if ( $is_enabled ) { |
| 67 | 80 | $this->generate(); |
| 68 | 81 | } else { |
| @@ -86,10 +99,9 @@ | ||
| 86 | 99 | } |
| 87 | 100 | } |
| 88 | 101 | |
| 89 | 102 | public function get_content(): string { |
| 90 | - $content = self::UTF8_BOM; | |
| 91 | - $content .= $this->parser->inject_title(); | |
| 103 | + $content = $this->parser->inject_title(); | |
| 92 | 104 | $content .= $this->parser->inject_site_description(); |
| 93 | 105 | $content .= $this->parser->inject_items( $this->parser->get_by_post_type( 'post' ), 'Posts' ); |
| 94 | 106 | $content .= $this->parser->inject_items( $this->parser->get_by_post_type( 'page' ), 'Pages' ); |
| 95 | 107 | $content .= $this->maybe_inject_woocommerce_products(); |
| @@ -126,8 +138,11 @@ | ||
| 126 | 138 | return; |
| 127 | 139 | } |
| 128 | 140 | |
| 129 | 141 | $this->file_helper->create( $this->get_content() ); |
| 142 | + | |
| 143 | + $this->record_generated_version(); | |
| 144 | + | |
| 130 | 145 | do_action( LlmsTxtInjectContentJob::JOB_NAME, array( 'post_type' => 'post' ) ); |
| 131 | 146 | do_action( LlmsTxtInjectContentJob::JOB_NAME, array( 'post_type' => 'page' ) ); |
| 132 | 147 | |
| 133 | 148 | if ( $this->is_woocommerce_active() ) { |
| @@ -136,8 +151,16 @@ | ||
| 136 | 151 | } |
| 137 | 152 | |
| 138 | 153 | protected function delete(): void { |
| 139 | 154 | $this->file_helper->delete(); |
| 155 | + } | |
| 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 ); | |
| 140 | 163 | } |
| 141 | 164 | |
| 142 | 165 | protected function is_post_type_supported( string $post_type ): bool { |
| 143 | 166 | return array_key_exists( $post_type, self::HOSTINGER_LLMSTXT_SUPPORTED_POST_TYPES ); |