$value ) { printf( "$this->red_open%-'.16s %s $this->color_close\n", $stat, $value ); } // Display caution warnings next foreach ( $stats['caution'] as $stat => $value ) { printf( "$this->yellow_open%-'.16s %s $this->color_close\n", $stat, $value ); } // The rest of the results are good! foreach ( $stats['good'] as $stat => $value ) { // Modules should get special spacing for aestetics if ( strpos( $stat, 'odule-' ) ) { printf( "%-'.30s %s\n", $stat, $value ); usleep( 4000 ); // For dramatic effect lolz continue; } printf( "%-'.16s %s\n", $stat, $value ); usleep( 4000 ); // For dramatic effect lolz } } else { // Just the basics WP_CLI::line( "\n" . _x( "View full status with 'wp jetpack status full'", '"wp jetpack status full" is a command - do not translate', 'jetpack' ) ); } } /** * Tests the active connection * * Does a two-way test to verify that the local site can communicate with remote Jetpack/WP.com servers and that Jetpack/WP.com servers can talk to the local site. * * ## EXAMPLES * * wp jetpack test-connection * * @subcommand test-connection */ public function test_connection( $args, $assoc_args ) { WP_CLI::line( sprintf( __( 'Testing connection for %s', 'jetpack' ), esc_url( get_site_url() ) ) ); if ( ! Jetpack::is_active() ) { WP_CLI::error( __( 'Jetpack is not currently connected to WordPress.com', 'jetpack' ) ); } $response = Jetpack_Client::wpcom_json_api_request_as_blog( sprintf( '/jetpack-blogs/%d/test-connection', Jetpack_Options::get_option( 'id' ) ), Jetpack_Client::WPCOM_JSON_API_VERSION ); if ( is_wp_error( $response ) ) { /* translators: %1$s is the error code, %2$s is the error message */ WP_CLI::error( sprintf( __( 'Failed to test connection (#%1$s: %2$s)', 'jetpack' ), $response->get_error_code(), $response->get_error_message() ) ); } $body = wp_remote_retrieve_body( $response ); if ( ! $body ) { WP_CLI::error( __( 'Failed to test connection (empty response body)', 'jetpack' ) ); } $result = json_decode( $body ); $is_connected = (bool) $result->connected; $message = $result->message; if ( $is_connected ) { WP_CLI::success( $message ); } else { WP_CLI::error( $message ); } } /** * Disconnect Jetpack Blogs or Users * * ## OPTIONS * * blog: Disconnect the entire blog. * * user : Disconnect a specific user from WordPress.com. * * Please note, the primary account that the blog is connected * to WordPress.com with cannot be disconnected without * disconnecting the entire blog. * * ## EXAMPLES * * wp jetpack disconnect blog * wp jetpack disconnect user 13 * wp jetpack disconnect user username * wp jetpack disconnect user email@domain.com * * @synopsis [] */ public function disconnect( $args, $assoc_args ) { if ( ! Jetpack::is_active() ) { WP_CLI::error( __( 'You cannot disconnect, without having first connected.', 'jetpack' ) ); } $action = isset( $args[0] ) ? $args[0] : 'prompt'; if ( ! in_array( $action, array( 'blog', 'user', 'prompt' ) ) ) { /* translators: %s is a command like "prompt" */ WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $action ) ); } if ( in_array( $action, array( 'user' ) ) ) { if ( isset( $args[1] ) ) { $user_id = $args[1]; if ( ctype_digit( $user_id ) ) { $field = 'id'; $user_id = (int) $user_id; } elseif ( is_email( $user_id ) ) { $field = 'email'; $user_id = sanitize_user( $user_id, true ); } else { $field = 'login'; $user_id = sanitize_user( $user_id, true ); } if ( ! $user = get_user_by( $field, $user_id ) ) { WP_CLI::error( __( 'Please specify a valid user.', 'jetpack' ) ); } } else { WP_CLI::error( __( 'Please specify a user by either ID, username, or email.', 'jetpack' ) ); } } switch ( $action ) { case 'blog': Jetpack::log( 'disconnect' ); Jetpack::disconnect(); WP_CLI::success( sprintf( __( 'Jetpack has been successfully disconnected for %s.', 'jetpack' ), esc_url( get_site_url() ) ) ); break; case 'user': if ( Jetpack::unlink_user( $user->ID ) ) { Jetpack::log( 'unlink', $user->ID ); WP_CLI::success( __( 'User has been successfully disconnected.', 'jetpack' ) ); } else { /* translators: %s is a username */ WP_CLI::error( sprintf( __( "User %s could not be disconnected. Are you sure they're connected currently?", 'jetpack' ), "{$user->login} <{$user->email}>" ) ); } break; case 'prompt': WP_CLI::error( __( 'Please specify if you would like to disconnect a blog or user.', 'jetpack' ) ); break; } } /** * Reset Jetpack options and settings to default * * ## OPTIONS * * modules: Resets modules to default state ( get_default_modules() ) * * options: Resets all Jetpack options except: * - All private options (Blog token, user token, etc...) * - id (The Client ID/WP.com Blog ID of this site) * - master_user * - version * - activated * * ## EXAMPLES * * wp jetpack reset options * wp jetpack reset modules * * @synopsis */ public function reset( $args, $assoc_args ) { $action = isset( $args[0] ) ? $args[0] : 'prompt'; if ( ! in_array( $action, array( 'options', 'modules' ) ) ) { /* translators: %s is a command like "prompt" */ WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $action ) ); } // Are you sure? jetpack_cli_are_you_sure(); switch ( $action ) { case 'options': $options_to_reset = Jetpack_Options::get_options_for_reset(); // Reset the Jetpack options WP_CLI::line( sprintf( __( "Resetting Jetpack Options for %s...\n", "jetpack" ), esc_url( get_site_url() ) ) ); sleep(1); // Take a breath foreach ( $options_to_reset['jp_options'] as $option_to_reset ) { Jetpack_Options::delete_option( $option_to_reset ); usleep( 100000 ); /* translators: This is the result of an action. The option named %s was reset */ WP_CLI::success( sprintf( __( '%s option reset', 'jetpack' ), $option_to_reset ) ); } // Reset the WP options WP_CLI::line( __( "Resetting the jetpack options stored in wp_options...\n", "jetpack" ) ); usleep( 500000 ); // Take a breath foreach ( $options_to_reset['wp_options'] as $option_to_reset ) { delete_option( $option_to_reset ); usleep( 100000 ); /* translators: This is the result of an action. The option named %s was reset */ WP_CLI::success( sprintf( __( '%s option reset', 'jetpack' ), $option_to_reset ) ); } // Reset to default modules WP_CLI::line( __( "Resetting default modules...\n", "jetpack" ) ); usleep( 500000 ); // Take a breath $default_modules = Jetpack::get_default_modules(); Jetpack::update_active_modules( $default_modules ); WP_CLI::success( __( 'Modules reset to default.', 'jetpack' ) ); // Jumpstart option is special Jetpack_Options::update_option( 'jumpstart', 'new_connection' ); WP_CLI::success( __( 'jumpstart option reset', 'jetpack' ) ); break; case 'modules': $default_modules = Jetpack::get_default_modules(); Jetpack::update_active_modules( $default_modules ); WP_CLI::success( __( 'Modules reset to default.', 'jetpack' ) ); break; case 'prompt': WP_CLI::error( __( 'Please specify if you would like to reset your options, or modules', 'jetpack' ) ); break; } } /** * Manage Jetpack Modules * * ## OPTIONS * * * : The action to take. * --- * default: list * options: * - list * - activate * - deactivate * - toggle * --- * * [] * : The slug of the module to perform an action on. * * [--format=] * : Allows overriding the output of the command when listing modules. * --- * default: table * options: * - table * - json * - csv * - yaml * - ids * - count * --- * * ## EXAMPLES * * wp jetpack module list * wp jetpack module list --format=json * wp jetpack module activate stats * wp jetpack module deactivate stats * wp jetpack module toggle stats * wp jetpack module activate all * wp jetpack module deactivate all */ public function module( $args, $assoc_args ) { $action = isset( $args[0] ) ? $args[0] : 'list'; if ( isset( $args[1] ) ) { $module_slug = $args[1]; if ( 'all' !== $module_slug && ! Jetpack::is_module( $module_slug ) ) { /* translators: %s is a module slug like "stats" */ WP_CLI::error( sprintf( __( '%s is not a valid module.', 'jetpack' ), $module_slug ) ); } if ( 'toggle' === $action ) { $action = Jetpack::is_module_active( $module_slug ) ? 'deactivate' : 'activate'; } if ( 'all' === $args[1] ) { $action = ( 'deactivate' === $action ) ? 'deactivate_all' : 'activate_all'; } } elseif ( 'list' !== $action ) { WP_CLI::line( __( 'Please specify a valid module.', 'jetpack' ) ); $action = 'list'; } switch ( $action ) { case 'list': $modules_list = array(); $modules = Jetpack::get_available_modules(); sort( $modules ); foreach ( (array) $modules as $module_slug ) { if ( 'vaultpress' === $module_slug ) { continue; } $modules_list[] = array( 'slug' => $module_slug, 'status' => Jetpack::is_module_active( $module_slug ) ? __( 'Active', 'jetpack' ) : __( 'Inactive', 'jetpack' ), ); } WP_CLI\Utils\format_items( $assoc_args['format'], $modules_list, array( 'slug', 'status' ) ); break; case 'activate': $module = Jetpack::get_module( $module_slug ); Jetpack::log( 'activate', $module_slug ); if ( Jetpack::activate_module( $module_slug, false, false ) ) { WP_CLI::success( sprintf( __( '%s has been activated.', 'jetpack' ), $module['name'] ) ); } else { WP_CLI::error( sprintf( __( '%s could not be activated.', 'jetpack' ), $module['name'] ) ); } break; case 'activate_all': $modules = Jetpack::get_available_modules(); Jetpack::update_active_modules( $modules ); WP_CLI::success( __( 'All modules activated!', 'jetpack' ) ); break; case 'deactivate': $module = Jetpack::get_module( $module_slug ); Jetpack::log( 'deactivate', $module_slug ); Jetpack::deactivate_module( $module_slug ); WP_CLI::success( sprintf( __( '%s has been deactivated.', 'jetpack' ), $module['name'] ) ); break; case 'deactivate_all': Jetpack::delete_active_modules(); WP_CLI::success( __( 'All modules deactivated!', 'jetpack' ) ); break; case 'toggle': // Will never happen, should have been handled above and changed to activate or deactivate. break; } } /** * Manage Protect Settings * * ## OPTIONS * * whitelist: Whitelist an IP address. You can also read or clear the whitelist. * * * ## EXAMPLES * * wp jetpack protect whitelist * wp jetpack protect whitelist list * wp jetpack protect whitelist clear * * @synopsis [] */ public function protect( $args, $assoc_args ) { $action = isset( $args[0] ) ? $args[0] : 'prompt'; if ( ! in_array( $action, array( 'whitelist' ) ) ) { /* translators: %s is a command like "prompt" */ WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $action ) ); } // Check if module is active if ( ! Jetpack::is_module_active( __FUNCTION__ ) ) { WP_CLI::error( sprintf( _x( '%s is not active. You can activate it with "wp jetpack module activate %s"', '"wp jetpack module activate" is a command - do not translate', 'jetpack' ), __FUNCTION__, __FUNCTION__ ) ); } if ( in_array( $action, array( 'whitelist' ) ) ) { if ( isset( $args[1] ) ) { $action = 'whitelist'; } else { $action = 'prompt'; } } switch ( $action ) { case 'whitelist': $whitelist = array(); $new_ip = $args[1]; $current_whitelist = get_site_option( 'jetpack_protect_whitelist', array() ); // Build array of IPs that are already whitelisted. // Re-build manually instead of using jetpack_protect_format_whitelist() so we can easily get // low & high range params for jetpack_protect_ip_address_is_in_range(); foreach( $current_whitelist as $whitelisted ) { // IP ranges if ( $whitelisted->range ) { // Is it already whitelisted? if ( jetpack_protect_ip_address_is_in_range( $new_ip, $whitelisted->range_low, $whitelisted->range_high ) ) { /* translators: %s is an IP address */ WP_CLI::error( sprintf( __( '%s has already been whitelisted', 'jetpack' ), $new_ip ) ); break; } $whitelist[] = $whitelisted->range_low . " - " . $whitelisted->range_high; } else { // Individual IPs // Check if the IP is already whitelisted (single IP only) if ( $new_ip == $whitelisted->ip_address ) { /* translators: %s is an IP address */ WP_CLI::error( sprintf( __( '%s has already been whitelisted', 'jetpack' ), $new_ip ) ); break; } $whitelist[] = $whitelisted->ip_address; } } /* * List the whitelist * Done here because it's easier to read the $whitelist array after it's been rebuilt */ if ( isset( $args[1] ) && 'list' == $args[1] ) { if ( ! empty( $whitelist ) ) { WP_CLI::success( __( 'Here are your whitelisted IPs:', 'jetpack' ) ); foreach ( $whitelist as $ip ) { WP_CLI::line( "\t" . str_pad( $ip, 24 ) ) ; } } else { WP_CLI::line( __( 'Whitelist is empty.', "jetpack" ) ) ; } break; } /* * Clear the whitelist */ if ( isset( $args[1] ) && 'clear' == $args[1] ) { if ( ! empty( $whitelist ) ) { $whitelist = array(); jetpack_protect_save_whitelist( $whitelist ); WP_CLI::success( __( 'Cleared all whitelisted IPs', 'jetpack' ) ); } else { WP_CLI::line( __( 'Whitelist is empty.', "jetpack" ) ) ; } break; } // Append new IP to whitelist array array_push( $whitelist, $new_ip ); // Save whitelist if there are no errors $result = jetpack_protect_save_whitelist( $whitelist ); if ( is_wp_error( $result ) ) { WP_CLI::error( __( $result, 'jetpack' ) ); } /* translators: %s is an IP address */ WP_CLI::success( sprintf( __( '%s has been whitelisted.', 'jetpack' ), $new_ip ) ); break; case 'prompt': WP_CLI::error( __( 'No command found.', 'jetpack' ) . "\n" . __( 'Please enter the IP address you want to whitelist.', 'jetpack' ) . "\n" . _x( 'You can save a range of IPs {low_range}-{high_range}. No spaces allowed. (example: 1.1.1.1-2.2.2.2)', 'Instructions on how to whitelist IP ranges - low_range/high_range should be translated.', 'jetpack' ) . "\n" . _x( "You can also 'list' or 'clear' the whitelist.", "'list' and 'clear' are commands and should not be translated", 'jetpack' ) . "\n" ); break; } } /** * Manage Jetpack Options * * ## OPTIONS * * list : List all jetpack options and their values * delete : Delete an option * - can only delete options that are white listed. * update : update an option * - can only update option strings * get : get the value of an option * * ## EXAMPLES * * wp jetpack options list * wp jetpack options get * wp jetpack options delete * wp jetpack options update [] * * @synopsis [] [] */ public function options( $args, $assoc_args ) { $action = isset( $args[0] ) ? $args[0] : 'list'; $safe_to_modify = Jetpack_Options::get_options_for_reset(); // Jumpstart is special array_push( $safe_to_modify, 'jumpstart' ); // Is the option flagged as unsafe? $flagged = ! in_array( $args[1], $safe_to_modify ); if ( ! in_array( $action, array( 'list', 'get', 'delete', 'update' ) ) ) { /* translators: %s is a command like "prompt" */ WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $action ) ); } if ( isset( $args[0] ) ) { if ( 'get' == $args[0] && isset( $args[1] ) ) { $action = 'get'; } else if ( 'delete' == $args[0] && isset( $args[1] ) ) { $action = 'delete'; } else if ( 'update' == $args[0] && isset( $args[1] ) ) { $action = 'update'; } else { $action = 'list'; } } // Bail if the option isn't found $option = isset( $args[1] ) ? Jetpack_Options::get_option( $args[1] ) : false; if ( isset( $args[1] ) && ! $option && 'update' !== $args[0] ) { WP_CLI::error( __( 'Option not found or is empty. Use "list" to list option names', 'jetpack' ) ); } // Let's print_r the option if it's an array // Used in the 'get' and 'list' actions $option = is_array( $option ) ? print_r( $option ) : $option; switch ( $action ) { case 'get': WP_CLI::success( "\t" . $option ); break; case 'delete': jetpack_cli_are_you_sure( $flagged ); Jetpack_Options::delete_option( $args[1] ); WP_CLI::success( sprintf( __( 'Deleted option: %s', 'jetpack' ), $args[1] ) ); break; case 'update': jetpack_cli_are_you_sure( $flagged ); // Updating arrays would get pretty tricky... $value = Jetpack_Options::get_option( $args[1] ); if ( $value && is_array( $value ) ) { WP_CLI::error( __( 'Sorry, no updating arrays at this time', 'jetpack' ) ); } Jetpack_Options::update_option( $args[1], $args[2] ); WP_CLI::success( sprintf( _x( 'Updated option: %s to "%s"', 'Updating an option from "this" to "that".', 'jetpack' ), $args[1], $args[2] ) ); break; case 'list': $options_compact = Jetpack_Options::get_option_names(); $options_non_compact = Jetpack_Options::get_option_names( 'non_compact' ); $options_private = Jetpack_Options::get_option_names( 'private' ); $options = array_merge( $options_compact, $options_non_compact, $options_private ); // Table headers WP_CLI::line( "\t" . str_pad( __( 'Option', 'jetpack' ), 30 ) . __( 'Value', 'jetpack' ) ); // List out the options and their values // Tell them if the value is empty or not // Tell them if it's an array foreach ( $options as $option ) { $value = Jetpack_Options::get_option( $option ); if ( ! $value ) { WP_CLI::line( "\t" . str_pad( $option, 30 ) . 'Empty' ); continue; } if ( ! is_array( $value ) ) { WP_CLI::line( "\t" . str_pad( $option, 30 ) . $value ); } else if ( is_array( $value ) ) { WP_CLI::line( "\t" . str_pad( $option, 30 ) . 'Array - Use "get