AI.php
151 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Hostinger\Cli\Commands; |
| 4 | |
| 5 | use Exception; |
| 6 | use Hostinger\Admin\PluginSettings; |
| 7 | use WP_CLI; |
| 8 | |
| 9 | defined( 'ABSPATH' ) || exit; |
| 10 | |
| 11 | class AI { |
| 12 | protected const WEB2AGENT_FEATURE_NAME = 'Web2Agent feature'; |
| 13 | protected const LLMS_TXT_FEATURE_NAME = 'LLMS.txt file generation feature'; |
| 14 | |
| 15 | public static function define_command(): void { |
| 16 | if ( ! class_exists( 'WP_CLI' ) ) { |
| 17 | return; |
| 18 | } |
| 19 | |
| 20 | WP_CLI::add_command( |
| 21 | 'hostinger ai', |
| 22 | self::class, |
| 23 | array( |
| 24 | 'shortdesc' => 'Check the status of AI Discovery Features', |
| 25 | 'longdesc' => 'Available Hostinger AI commands:' . "\n\n" . |
| 26 | ' wp hostinger ai llmstxt <0|1>' . "\n" . |
| 27 | ' Manage the ' . self::LLMS_TXT_FEATURE_NAME . '. Use 1 to enable and 0 to disable it.' . "\n\n" . |
| 28 | ' wp hostinger ai web2agent <0|1>' . "\n" . |
| 29 | ' Manage the ' . self::WEB2AGENT_FEATURE_NAME . '. Use 1 to enable and 0 to disable it.' . "\n\n" . |
| 30 | ' wp hostinger ai status' . "\n" . |
| 31 | ' Display the current status for AI Discovery features.' . "\n\n" . |
| 32 | '## EXAMPLES' . "\n\n" . |
| 33 | ' wp hostinger ai status' . "\n" . |
| 34 | ' Display the current status for AI Discovery features.' . "\n\n" . |
| 35 | ' wp hostinger ai llmstxt 0' . "\n" . |
| 36 | ' Disables ' . self::LLMS_TXT_FEATURE_NAME . '.' . "\n\n" . |
| 37 | ' wp hostinger ai llmstxt 1' . "\n" . |
| 38 | ' Enables ' . self::LLMS_TXT_FEATURE_NAME . '.' . "\n", |
| 39 | |
| 40 | ) |
| 41 | ); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Command allows enable/disable Web2Agent feature. |
| 46 | * |
| 47 | * @param array $args |
| 48 | * |
| 49 | * @return bool |
| 50 | * @throws Exception |
| 51 | */ |
| 52 | public function web2agent( array $args ): bool { |
| 53 | $plugin_settings = new PluginSettings(); |
| 54 | |
| 55 | if ( ! empty( $args ) ) { |
| 56 | $this->validate_args( $args ); |
| 57 | $this->set_setting_status( $plugin_settings, self::WEB2AGENT_FEATURE_NAME, (bool) $args[0] ); |
| 58 | } |
| 59 | |
| 60 | return $this->get_setting_status( $plugin_settings, self::WEB2AGENT_FEATURE_NAME ); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Command allows enable/disable LLMS.txt file generation feature. |
| 65 | * |
| 66 | * @param array $args |
| 67 | * |
| 68 | * @return bool |
| 69 | * @throws Exception |
| 70 | */ |
| 71 | public function llmstxt( array $args ): bool { |
| 72 | $plugin_settings = new PluginSettings(); |
| 73 | |
| 74 | if ( ! empty( $args ) ) { |
| 75 | $this->validate_args( $args ); |
| 76 | $this->set_setting_status( $plugin_settings, self::LLMS_TXT_FEATURE_NAME, (bool) $args[0] ); |
| 77 | } |
| 78 | |
| 79 | return $this->get_setting_status( $plugin_settings, self::LLMS_TXT_FEATURE_NAME ); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Get the current status of AI Discovery features. |
| 84 | * @return string |
| 85 | */ |
| 86 | public function status(): string { |
| 87 | $plugin_settings = new PluginSettings(); |
| 88 | $plugin_options = $plugin_settings->get_plugin_settings(); |
| 89 | |
| 90 | $data = array( |
| 91 | 'llmstxt' => $plugin_options->get_enable_llms_txt(), |
| 92 | 'web2agent' => $plugin_options->get_optin_mcp(), |
| 93 | ); |
| 94 | |
| 95 | $status = wp_json_encode( $data ); |
| 96 | WP_CLI::line( $status ); |
| 97 | return $status; |
| 98 | } |
| 99 | |
| 100 | private function set_setting_status( PluginSettings $plugin_settings, string $setting, bool $is_enabled ): void { |
| 101 | $plugin_options = $plugin_settings->get_plugin_settings(); |
| 102 | |
| 103 | switch ( $setting ) { |
| 104 | case self::WEB2AGENT_FEATURE_NAME: |
| 105 | $plugin_options->set_optin_mcp( $is_enabled ); |
| 106 | break; |
| 107 | case self::LLMS_TXT_FEATURE_NAME: |
| 108 | $plugin_options->set_enable_llms_txt( $is_enabled ); |
| 109 | break; |
| 110 | default: |
| 111 | throw new Exception( 'Invalid setting' ); |
| 112 | } |
| 113 | |
| 114 | $saved_settings = $plugin_settings->save_plugin_settings( $plugin_options ); |
| 115 | $this->clear_lightspeed_cache(); |
| 116 | |
| 117 | do_action( 'hostinger_tools_settings_saved', $saved_settings->to_array() ); |
| 118 | } |
| 119 | |
| 120 | private function get_setting_status( PluginSettings $plugin_settings, string $setting ): bool { |
| 121 | $plugin_options = $plugin_settings->get_plugin_settings(); |
| 122 | switch ( $setting ) { |
| 123 | case self::WEB2AGENT_FEATURE_NAME: |
| 124 | $is_enabled = $plugin_options->get_optin_mcp(); |
| 125 | break; |
| 126 | case self::LLMS_TXT_FEATURE_NAME: |
| 127 | $is_enabled = $plugin_options->get_enable_llms_txt(); |
| 128 | break; |
| 129 | default: |
| 130 | throw new Exception( 'Invalid setting' ); |
| 131 | } |
| 132 | |
| 133 | $enabled = $is_enabled ? 'ENABLED' : 'DISABLED'; |
| 134 | |
| 135 | WP_CLI::success( $setting . ' ' . $enabled ); |
| 136 | return $is_enabled; |
| 137 | } |
| 138 | |
| 139 | private function validate_args( mixed $args ): void { |
| 140 | if ( $args[0] !== '0' && $args[0] !== '1' ) { |
| 141 | throw new Exception( 'Invalid argument. Use 0 or 1' ); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | private function clear_lightspeed_cache(): void { |
| 146 | if ( has_action( 'litespeed_purge_all' ) ) { |
| 147 | do_action( 'litespeed_purge_all' ); |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 |