class-wc-cli-com-command.php
1 year ago
class-wc-cli-com-extension-command.php
2 years ago
class-wc-cli-rest-command.php
1 year ago
class-wc-cli-runner.php
2 years ago
class-wc-cli-tool-command.php
6 years ago
class-wc-cli-tracker-command.php
4 years ago
class-wc-cli-update-command.php
7 months ago
class-wc-cli-com-command.php
204 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WC_CLI_COM_Command class file. |
| 4 | * |
| 5 | * @package WooCommerce\CLI |
| 6 | */ |
| 7 | |
| 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | exit; |
| 10 | } |
| 11 | |
| 12 | /** |
| 13 | * Allows to interact with extensions from WCCOM marketplace via CLI. |
| 14 | * |
| 15 | * @version 6.8 |
| 16 | * @package WooCommerce |
| 17 | */ |
| 18 | class WC_CLI_COM_Command { |
| 19 | const APPLICATION_PASSWORD_SECTION_URL = 'https://woocommerce.com/my-account/#application-passwords'; |
| 20 | |
| 21 | /** |
| 22 | * Registers a commands for managing WooCommerce.com extensions. |
| 23 | */ |
| 24 | public static function register_commands() { |
| 25 | WP_CLI::add_command( 'wc com extension list', array( 'WC_CLI_COM_Command', 'list_extensions' ) ); |
| 26 | WP_CLI::add_command( 'wc com disconnect', array( 'WC_CLI_COM_Command', 'disconnect' ) ); |
| 27 | WP_CLI::add_command( 'wc com connect', array( 'WC_CLI_COM_Command', 'connect' ) ); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * List extensions owned by the connected site |
| 32 | * |
| 33 | * [--format] |
| 34 | * : If set, the command will use the specified format. Possible values are table, json, csv and yaml. By default the table format will be used. |
| 35 | * |
| 36 | * [--fields] |
| 37 | * : If set, the command will show only the specified fields instead of showing all the fields in the output. |
| 38 | * |
| 39 | * ## EXAMPLES |
| 40 | * |
| 41 | * # List extensions owned by the connected site in table format with all the fields |
| 42 | * $ wp wc com extension list |
| 43 | * |
| 44 | * # List the product slug of the extension owned by the connected site in csv format |
| 45 | * $ wp wc com extension list --format=csv --fields=product_slug |
| 46 | * |
| 47 | * @param array $args WP-CLI positional arguments. |
| 48 | * @param array $assoc_args WP-CLI associative arguments. |
| 49 | */ |
| 50 | public static function list_extensions( array $args, array $assoc_args ) { |
| 51 | try { |
| 52 | $data = WC_Helper::get_subscriptions(); |
| 53 | } catch ( Exception $e ) { |
| 54 | $data = array(); |
| 55 | } |
| 56 | |
| 57 | $data = array_values( $data ); |
| 58 | |
| 59 | $formatter = new \WP_CLI\Formatter( |
| 60 | $assoc_args, |
| 61 | array( |
| 62 | 'product_slug', |
| 63 | 'product_name', |
| 64 | 'auto_renew', |
| 65 | 'expires_on', |
| 66 | 'expired', |
| 67 | 'sites_max', |
| 68 | 'sites_active', |
| 69 | 'maxed', |
| 70 | ) |
| 71 | ); |
| 72 | |
| 73 | $data = array_map( |
| 74 | function( $item ) { |
| 75 | $product_slug = ''; |
| 76 | $product_url_parts = explode( '/', $item['product_url'] ); |
| 77 | if ( count( $product_url_parts ) > 2 ) { |
| 78 | $product_slug = $product_url_parts[ count( $product_url_parts ) - 2 ]; |
| 79 | } |
| 80 | return array( |
| 81 | 'product_slug' => $product_slug, |
| 82 | 'product_name' => htmlspecialchars_decode( $item['product_name'] ), |
| 83 | 'auto_renew' => $item['autorenew'] ? 'On' : 'Off', |
| 84 | 'expires_on' => gmdate( 'Y-m-d', $item['expires'] ), |
| 85 | 'expired' => $item['expired'] ? 'Yes' : 'No', |
| 86 | 'sites_max' => $item['sites_max'], |
| 87 | 'sites_active' => $item['sites_active'], |
| 88 | 'maxed' => $item['maxed'] ? 'Yes' : 'No', |
| 89 | ); |
| 90 | }, |
| 91 | $data |
| 92 | ); |
| 93 | |
| 94 | $formatter->display_items( $data ); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * ## OPTIONS |
| 99 | * |
| 100 | * [--yes] |
| 101 | * : Do not prompt for confirmation. |
| 102 | * |
| 103 | * ## EXAMPLES |
| 104 | * |
| 105 | * # Disconnect from site. |
| 106 | * $ wp wc com disconnect |
| 107 | * |
| 108 | * # Disconnect without prompt for confirmation. |
| 109 | * $ wp wc com disconnect --yes |
| 110 | * |
| 111 | * @param array $args Positional arguments to include when calling the command. |
| 112 | * @param array $assoc_args Associative arguments to include when calling the command. |
| 113 | |
| 114 | * @return void |
| 115 | * @throws \WP_CLI\ExitException If WP_CLI::$capture_exit is true. |
| 116 | */ |
| 117 | public static function disconnect( array $args, array $assoc_args ) { |
| 118 | if ( ! WC_Helper::is_site_connected() ) { |
| 119 | WP_CLI::error( __( 'Your store is not connected to WooCommerce.com. Run `wp wc com connect` command.', 'woocommerce' ) ); |
| 120 | } |
| 121 | |
| 122 | WP_CLI::confirm( __( 'Are you sure you want to disconnect your store from WooCommerce.com?', 'woocommerce' ), $assoc_args ); |
| 123 | WC_Helper::disconnect(); |
| 124 | WP_CLI::success( __( 'You have successfully disconnected your store from WooCommerce.com', 'woocommerce' ) ); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Connects to WooCommerce.com with application-password. |
| 129 | * |
| 130 | * [--password] |
| 131 | * : If set, password won't be prompt. |
| 132 | * |
| 133 | * [--force] |
| 134 | * : If set, site will be disconnected and a new connection will be forced. |
| 135 | * |
| 136 | * ## EXAMPLES |
| 137 | * |
| 138 | * # Connect to WCCOM using password. |
| 139 | * $ wp wc com connect |
| 140 | * |
| 141 | * # force connecting to WCCOM even if site is already connected. |
| 142 | * $ wp wc com connect --force |
| 143 | * |
| 144 | * # Pass password to command. |
| 145 | * $ wp wc com connect --password=PASSWORD |
| 146 | * |
| 147 | * @param array $args Positional arguments to include when calling the command. |
| 148 | * @param array $assoc_args Associative arguments to include when calling the command. |
| 149 | * |
| 150 | * @return void |
| 151 | * @throws \WP_CLI\ExitException If WP_CLI::$capture_exit is true. |
| 152 | */ |
| 153 | public static function connect( array $args, array $assoc_args ) { |
| 154 | $password = \WP_CLI\Utils\get_flag_value( $assoc_args, 'password' ); |
| 155 | $force = \WP_CLI\Utils\get_flag_value( $assoc_args, 'force', false ); |
| 156 | |
| 157 | if ( WC_Helper::is_site_connected() ) { |
| 158 | if ( $force ) { |
| 159 | WC_Helper::disconnect(); |
| 160 | } else { |
| 161 | WP_CLI::error( __( 'Your store is already connected.', 'woocommerce' ) ); |
| 162 | |
| 163 | return; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | if ( empty( $password ) ) { |
| 168 | // translators: %s is the URL for the application-password section in WooCommerce.com. |
| 169 | WP_CLI::log( sprintf( __( 'If you don\'t have an application password (not your account password), generate a password from %s', 'woocommerce' ), esc_url( self::APPLICATION_PASSWORD_SECTION_URL ) ) ); |
| 170 | $password = self::ask( __( 'Connection password:', 'woocommerce' ) ); |
| 171 | } |
| 172 | $password = sanitize_text_field( $password ); |
| 173 | if ( empty( $password ) ) { |
| 174 | // translators: %s is the URL for the application-password section in WooCommerce.com. |
| 175 | WP_CLI::error( sprintf( __( 'Invalid password. Generate a new one from %s.', 'woocommerce' ), esc_url( self::APPLICATION_PASSWORD_SECTION_URL ) ) ); |
| 176 | } |
| 177 | |
| 178 | $auth = WC_Helper::connect_with_password( $password ); |
| 179 | if ( is_wp_error( $auth ) ) { |
| 180 | WP_CLI::error( $auth->get_error_message() ); |
| 181 | } |
| 182 | |
| 183 | if ( WC_Helper::is_site_connected() ) { |
| 184 | WP_CLI::success( __( 'Store connected successfully.', 'woocommerce' ) ); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * We are asking a question and returning an answer as a string. |
| 190 | * |
| 191 | * @param string $question The question being prompt. |
| 192 | * |
| 193 | * @return string |
| 194 | */ |
| 195 | protected static function ask( $question ) { |
| 196 | // phpcs:disable WordPress.WP.AlternativeFunctions.file_system_read_fwrite |
| 197 | // Adding space to question and showing it. |
| 198 | fwrite( STDOUT, $question . ' ' ); |
| 199 | |
| 200 | return trim( fgets( STDIN ) ); |
| 201 | // phpcs:enable |
| 202 | } |
| 203 | } |
| 204 |