| 1 |
<?php |
| 2 |
|
| 3 |
namespace Hostinger; |
| 4 |
|
| 5 |
use Hostinger\Admin\PluginSettings; |
| 6 |
use Hostinger\LlmsTxtGenerator\LlmsTxtFileHelper; |
| 7 |
use Hostinger\LlmsTxtGenerator\LlmsTxtGenerator; |
| 8 |
use Hostinger\LlmsTxtGenerator\LlmsTxtParser; |
| 9 |
use Hostinger\LlmsTxtGenerator\LlmsTxtSummaryProvider; |
| 10 |
|
| 11 |
defined( 'ABSPATH' ) || exit; |
| 12 |
|
| 13 |
class Activator { |
| 14 |
public const INSTALLATION_OPTION_NAME = 'hts_new_installation'; |
| 15 |
|
| 16 |
public static function activate(): void { |
| 17 |
$plugin_settings = new PluginSettings(); |
| 18 |
|
| 19 |
$options = new DefaultOptions( $plugin_settings ); |
| 20 |
$options->add_options(); |
| 21 |
|
| 22 |
self::generate_llms_txt( $plugin_settings ); |
| 23 |
self::update_installation_state_on_activation(); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* The generator's own self-heal on `init` only runs for a logged-in administrator, so a site |
| 28 |
* provisioned headlessly would serve no llms.txt until someone opened wp-admin. |
| 29 |
*/ |
| 30 |
private static function generate_llms_txt( PluginSettings $plugin_settings ): void { |
| 31 |
$file_helper = new LlmsTxtFileHelper(); |
| 32 |
|
| 33 |
if ( $file_helper->llmstxt_file_exists() ) { |
| 34 |
return; |
| 35 |
} |
| 36 |
|
| 37 |
$llms_txt_parser = new LlmsTxtParser( new LlmsTxtSummaryProvider() ); |
| 38 |
$generator = new LlmsTxtGenerator( $plugin_settings, $file_helper, $llms_txt_parser ); |
| 39 |
$generator->generate(); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Saves installation state. |
| 44 |
* |
| 45 |
* @return void |
| 46 |
*/ |
| 47 |
public static function update_installation_state_on_activation(): void { |
| 48 |
$installation_state = get_option( self::INSTALLATION_OPTION_NAME, false ); |
| 49 |
|
| 50 |
if ( $installation_state !== 'old' ) { |
| 51 |
add_option( self::INSTALLATION_OPTION_NAME, 'new' ); |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
|