| 1 |
<?php |
| 2 |
/** |
| 3 |
* WP-CLI Commands for ZipWP MCP. |
| 4 |
* |
| 5 |
* @package zip-ai |
| 6 |
* @since 0.0.1 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace ZipAI\Classes\Cli; |
| 10 |
|
| 11 |
// Exit if accessed directly. |
| 12 |
if ( ! defined( 'ABSPATH' ) ) { |
| 13 |
exit; |
| 14 |
} |
| 15 |
|
| 16 |
use ZipAI\Classes\Core\Utils; |
| 17 |
use WP_CLI; |
| 18 |
|
| 19 |
/** |
| 20 |
* ZipWP MCP CLI Commands. |
| 21 |
* |
| 22 |
* @since 0.0.1 |
| 23 |
*/ |
| 24 |
class CLI_Commands { |
| 25 |
|
| 26 |
/** |
| 27 |
* Setup the authentication token for ZipWP MCP. |
| 28 |
* |
| 29 |
* Encrypts the token using local Sodium key and saves to zip_ai_settings. |
| 30 |
* |
| 31 |
* ## OPTIONS |
| 32 |
* |
| 33 |
* <token> |
| 34 |
* : The plain text auth token to encrypt and store. |
| 35 |
* |
| 36 |
* [--email=<email>] |
| 37 |
* : The user email to associate with the settings. |
| 38 |
* |
| 39 |
* ## EXAMPLES |
| 40 |
* |
| 41 |
* wp zip-ai setup-token "your-auth-token" --email="user@example.com" |
| 42 |
* |
| 43 |
* @subcommand setup-token |
| 44 |
* @when after_wp_load |
| 45 |
* |
| 46 |
* @param array $args Positional arguments. |
| 47 |
* @param array $assoc_args Associative arguments. |
| 48 |
* @return void |
| 49 |
*/ |
| 50 |
public function setup_token( $args, $assoc_args ) { |
| 51 |
$token = $args[0] ?? ''; |
| 52 |
$email = $assoc_args['email'] ?? ''; |
| 53 |
|
| 54 |
if ( empty( $token ) ) { |
| 55 |
WP_CLI::error( 'Token is required.' ); |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
// Encrypt the token using local Sodium key. |
| 60 |
$encrypted_token = Utils::encrypt( $token ); |
| 61 |
|
| 62 |
if ( empty( $encrypted_token ) ) { |
| 63 |
WP_CLI::error( 'Failed to encrypt token. Sodium may not be available.' ); |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
// Get existing settings or create new. |
| 68 |
$mcp_settings = get_option( 'zip_ai_settings', array() ); |
| 69 |
|
| 70 |
// Update with encrypted token. |
| 71 |
$mcp_settings['auth_token'] = $encrypted_token; |
| 72 |
$mcp_settings['auth_token_server'] = defined( 'ZIP_AI_CREDIT_SERVER_API' ) ? untrailingslashit( ZIP_AI_CREDIT_SERVER_API ) : ''; |
| 73 |
$mcp_settings['enabled'] = true; |
| 74 |
|
| 75 |
if ( ! empty( $email ) ) { |
| 76 |
$mcp_settings['user_email'] = $email; |
| 77 |
} |
| 78 |
|
| 79 |
// Save settings. |
| 80 |
$result = update_option( 'zip_ai_settings', $mcp_settings, false ); |
| 81 |
|
| 82 |
if ( $result ) { |
| 83 |
WP_CLI::success( 'Auth token has been encrypted and saved.' ); |
| 84 |
} else { |
| 85 |
// Check if the option was unchanged (same value). |
| 86 |
$current = get_option( 'zip_ai_settings', array() ); |
| 87 |
if ( $current['auth_token'] === $encrypted_token ) { |
| 88 |
WP_CLI::success( 'Auth token is already set (unchanged).' ); |
| 89 |
} else { |
| 90 |
WP_CLI::error( 'Failed to save auth token.' ); |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Revoke/remove the authentication token. |
| 97 |
* |
| 98 |
* ## EXAMPLES |
| 99 |
* |
| 100 |
* wp zip-ai revoke-token |
| 101 |
* |
| 102 |
* @subcommand revoke-token |
| 103 |
* @when after_wp_load |
| 104 |
* |
| 105 |
* @return void |
| 106 |
*/ |
| 107 |
public function revoke_token() { |
| 108 |
$mcp_settings = get_option( 'zip_ai_settings', array() ); |
| 109 |
|
| 110 |
unset( $mcp_settings['auth_token'] ); |
| 111 |
$mcp_settings['enabled'] = false; |
| 112 |
|
| 113 |
update_option( 'zip_ai_settings', $mcp_settings, false ); |
| 114 |
|
| 115 |
WP_CLI::success( 'Auth token has been revoked.' ); |
| 116 |
} |
| 117 |
} |
| 118 |
|