| @@ -1,19 +1,23 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace Hostinger; |
| 4 | 4 | |
| 5 | -use Hostinger\Admin\Options\PluginOptions; | |
| 5 | +use Hostinger\Admin\PluginSettings; | |
| 6 | 6 | use Hostinger\Helper; |
| 7 | 7 | |
| 8 | 8 | defined( 'ABSPATH' ) || exit; |
| 9 | 9 | |
| 10 | 10 | class DefaultOptions { |
| 11 | - /** | |
| 12 | - * @return void | |
| 13 | - */ | |
| 11 | + | |
| 12 | + private PluginSettings $plugin_settings; | |
| 13 | + | |
| 14 | + public function __construct( PluginSettings $plugin_settings ) { | |
| 15 | + $this->plugin_settings = $plugin_settings; | |
| 16 | + } | |
| 17 | + | |
| 14 | 18 | public function add_options(): void { |
| 15 | - $this->configure_security_settings(); | |
| 19 | + $this->configure_plugin_settings(); | |
| 16 | 20 | |
| 17 | 21 | foreach ( $this->options() as $key => $option ) { |
| 18 | 22 | update_option( $key, $option ); |
| 19 | 23 | } |
| @@ -18,20 +22,29 @@ | ||
| 18 | 22 | update_option( $key, $option ); |
| 19 | 23 | } |
| 20 | 24 | } |
| 21 | 25 | |
| 22 | - public function configure_security_settings(): void { | |
| 23 | - $hostinger_plugin_settings = get_option( HOSTINGER_PLUGIN_SETTINGS_OPTION, array() ); | |
| 26 | + /** | |
| 27 | + * A later activation must never override a choice the site owner has already made. | |
| 28 | + */ | |
| 29 | + private function configure_plugin_settings(): void { | |
| 30 | + $is_first_setup = ! $this->plugin_settings->has_stored_settings(); | |
| 31 | + $plugin_options = $this->plugin_settings->get_plugin_settings(); | |
| 32 | + $needs_bypass_code = empty( $plugin_options->get_bypass_code() ); | |
| 24 | 33 | |
| 25 | - if ( empty( $hostinger_plugin_settings['bypass_code'] ) ) { | |
| 26 | - $hostinger_plugin_settings['bypass_code'] = Helper::generate_bypass_code( 16 ); | |
| 27 | - $this->update_plugin_settings( $hostinger_plugin_settings ); | |
| 34 | + if ( ! $is_first_setup && ! $needs_bypass_code ) { | |
| 35 | + return; | |
| 28 | 36 | } |
| 29 | - } | |
| 30 | 37 | |
| 31 | - private function update_plugin_settings( array $settings ): void { | |
| 32 | - $plugin_options = new PluginOptions( $settings ); | |
| 33 | - update_option( HOSTINGER_PLUGIN_SETTINGS_OPTION, $plugin_options->to_array(), false ); | |
| 38 | + if ( $is_first_setup ) { | |
| 39 | + $plugin_options->set_enable_llms_txt( true ); | |
| 40 | + } | |
| 41 | + | |
| 42 | + if ( $needs_bypass_code ) { | |
| 43 | + $plugin_options->set_bypass_code( Helper::generate_bypass_code( 16 ) ); | |
| 44 | + } | |
| 45 | + | |
| 46 | + $this->plugin_settings->save_plugin_settings( $plugin_options ); | |
| 34 | 47 | } |
| 35 | 48 | |
| 36 | 49 | /** |
| 37 | 50 | * @return string[] |