PluginProbe
Hostinger Tools / 3.0.66
Hostinger Tools v3.0.66
3.0.77 3.0.76 3.0.75 3.0.74 3.0.73 3.0.72 3.0.71 3.0.70 3.0.69 3.0.68 3.0.67 3.0.66 1.8.1 1.8.2 1.8.3 1.9.1 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0.0 2.0.1 2.0.4 All 108 releases
hostinger / includes / Cli / Commands / AI.php

AI.php in Hostinger Tools 3.0.66, at includes/Cli/Commands/AI.php

149 lines 5.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 $plugin_settings->save_plugin_settings( $plugin_options );
115 $this->clear_lightspeed_cache();
116 }
117
118 private function get_setting_status( PluginSettings $plugin_settings, string $setting ): bool {
119 $plugin_options = $plugin_settings->get_plugin_settings();
120 switch ( $setting ) {
121 case self::WEB2AGENT_FEATURE_NAME:
122 $is_enabled = $plugin_options->get_optin_mcp();
123 break;
124 case self::LLMS_TXT_FEATURE_NAME:
125 $is_enabled = $plugin_options->get_enable_llms_txt();
126 break;
127 default:
128 throw new Exception( 'Invalid setting' );
129 }
130
131 $enabled = $is_enabled ? 'ENABLED' : 'DISABLED';
132
133 WP_CLI::success( $setting . ' ' . $enabled );
134 return $is_enabled;
135 }
136
137 private function validate_args( mixed $args ): void {
138 if ( $args[0] !== '0' && $args[0] !== '1' ) {
139 throw new Exception( 'Invalid argument. Use 0 or 1' );
140 }
141 }
142
143 private function clear_lightspeed_cache(): void {
144 if ( has_action( 'litespeed_purge_all' ) ) {
145 do_action( 'litespeed_purge_all' );
146 }
147 }
148 }
149