CredentialManager.php
10 months ago
MigratorTracker.php
9 months ago
PlatformRegistry.php
9 months ago
ProductsController.php
5 months ago
WooCommerceProductImporter.php
5 months ago
CredentialManager.php
120 lines
| 1 | <?php |
| 2 | |
| 3 | declare( strict_types=1 ); |
| 4 | |
| 5 | namespace Automattic\WooCommerce\Internal\CLI\Migrator\Core; |
| 6 | |
| 7 | use WP_CLI; |
| 8 | |
| 9 | /** |
| 10 | * Manages platform credentials. |
| 11 | */ |
| 12 | class CredentialManager { |
| 13 | /** |
| 14 | * Retrieves the stored credentials for a given platform. |
| 15 | * |
| 16 | * @param string $platform_slug The slug for the platform. |
| 17 | * |
| 18 | * @return array|null An associative array of credentials, or null if not found. |
| 19 | */ |
| 20 | public function get_credentials( string $platform_slug ): ?array { |
| 21 | $option_name = "wc_migrator_credentials_{$platform_slug}"; |
| 22 | $credentials_json = get_option( $option_name, false ); |
| 23 | if ( ! $credentials_json ) { |
| 24 | return null; |
| 25 | } |
| 26 | |
| 27 | $credentials = json_decode( $credentials_json, true ); |
| 28 | |
| 29 | return is_array( $credentials ) ? $credentials : null; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Checks if credentials exist for a given platform. |
| 34 | * |
| 35 | * @param string $platform_slug The slug for the platform. |
| 36 | * |
| 37 | * @return bool True if credentials exist, false otherwise. |
| 38 | */ |
| 39 | public function has_credentials( string $platform_slug ): bool { |
| 40 | $credentials = $this->get_credentials( $platform_slug ); |
| 41 | |
| 42 | return ! empty( $credentials ); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Prompts the user for credentials via the command line. |
| 47 | * |
| 48 | * @param array $fields An associative array of fields to prompt for. |
| 49 | * |
| 50 | * @return array The collected credentials. |
| 51 | */ |
| 52 | public function prompt_for_credentials( array $fields ): array { |
| 53 | $credentials = array(); |
| 54 | foreach ( $fields as $key => $prompt ) { |
| 55 | $credentials[ $key ] = $this->readline( $prompt . ' ' ); |
| 56 | } |
| 57 | |
| 58 | return $credentials; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Saves credentials to the database for a given platform. |
| 63 | * |
| 64 | * @param string $platform_slug The slug for the platform. |
| 65 | * @param array $credentials An associative array of credentials. |
| 66 | */ |
| 67 | public function save_credentials( string $platform_slug, array $credentials ): void { |
| 68 | $option_name = "wc_migrator_credentials_{$platform_slug}"; |
| 69 | update_option( $option_name, wp_json_encode( $credentials ) ); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Deletes credentials from the database for a given platform. |
| 74 | * |
| 75 | * @param string $platform_slug The slug for the platform. |
| 76 | */ |
| 77 | public function delete_credentials( string $platform_slug ): void { |
| 78 | $option_name = "wc_migrator_credentials_{$platform_slug}"; |
| 79 | delete_option( $option_name ); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Handles the interactive credential setup process for a platform. |
| 84 | * |
| 85 | * @param string $platform_slug The platform slug to set up credentials for. |
| 86 | * @param array $required_fields An array of field_key => prompt_text for credentials to collect. |
| 87 | * |
| 88 | * @return void |
| 89 | */ |
| 90 | public function setup_credentials( string $platform_slug, array $required_fields ): void { |
| 91 | if ( empty( $required_fields ) ) { |
| 92 | WP_CLI::error( 'No credential fields specified for setup.' ); |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | WP_CLI::log( 'Configuring credentials for ' . ucfirst( $platform_slug ) . '...' ); |
| 97 | |
| 98 | $credentials = $this->prompt_for_credentials( $required_fields ); |
| 99 | $this->save_credentials( $platform_slug, $credentials ); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Reads a line from STDIN. |
| 104 | * |
| 105 | * A backward-compatible wrapper for WP_CLI::readline(). |
| 106 | * |
| 107 | * @param string $prompt The prompt to show to the user. |
| 108 | * |
| 109 | * @return string |
| 110 | */ |
| 111 | private function readline( string $prompt ): string { |
| 112 | if ( method_exists( 'WP_CLI', 'readline' ) ) { |
| 113 | return WP_CLI::readline( $prompt ); |
| 114 | } |
| 115 | |
| 116 | WP_CLI::line( $prompt ); |
| 117 | return trim( fgets( STDIN ) ); |
| 118 | } |
| 119 | } |
| 120 |