JezK
Edit File: AccelerateWpCliTrait.php
<?php /** * Copyright (с) Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2023 All Rights Reserved * * Shared functionality for AccelerateWP CLI commands. */ namespace WP_Rocket\Cli; use Exception; use WP_CLI; /** * Trait for common AccelerateWP CLI functionality. * * @note CL. */ trait AccelerateWpCliTrait { /** * Run CLI subcommand. * * @param array $args Positional arguments. * @param array $assoc_args Associative arguments. * * @return void * @throws Exception Something went wrong. */ public function run( $args = [], $assoc_args = [] ) { $subcommand = ''; if ( array_key_exists( 0, $args ) ) { $subcommand = $args[0]; } if ( ! empty( $subcommand ) && 0 !== strcasecmp( $subcommand, __FUNCTION__ ) && method_exists( $this, $subcommand ) ) { $this->$subcommand( $args, $assoc_args ); } else { WP_CLI::error( 'Subcommand not found' ); } } /** * Validate CLI arguments and return unique_id. * * In standalone mode: use consumer_key from settings or --api_key from CLI. * In CLOS mode: require --unique_id. * * @param array $assoc_args Associative arguments. * * @return array Array with 'unique_id' key. * @throws Exception When validation fails. */ protected function validate_cli_args( $assoc_args = [] ) { if ( clsop_is_standalone() ) { return $this->validate_standalone_args( $assoc_args ); } return $this->validate_clos_args( $assoc_args ); } /** * Validate arguments for standalone mode. * * @param array $assoc_args Associative arguments. * * @return array Array with 'unique_id' key. * @throws Exception When validation fails. */ private function validate_standalone_args( $assoc_args ) { // First check if consumer_key is already set in plugin settings. $consumer_key = get_rocket_option( 'consumer_key', '' ); // If --api_key is passed, validate and save it. if ( array_key_exists( 'api_key', $assoc_args ) ) { $api_key = sanitize_text_field( wp_unslash( $assoc_args['api_key'] ) ); if ( ! empty( $api_key ) ) { // CL: Clear previous errors and throttle on new key. delete_transient( 'rocket_check_key_errors' ); delete_transient( 'rocket_license_last_check' ); delete_transient( 'rocket_license_check_lock' ); // Temporarily set the key so rocket_check_key() can use it. update_rocket_option( 'consumer_key', $api_key ); // Validate the license. $result = rocket_check_key( true ); if ( false === $result || ( is_array( $result ) && empty( $result['secret_key'] ) ) ) { // Reset on failure. update_rocket_option( 'consumer_key', '' ); throw new Exception( 'License validation failed. Please check your API key.' ); } $consumer_key = $api_key; } } if ( empty( $consumer_key ) ) { throw new Exception( 'License not activated. Please activate your license in plugin settings or pass --api_key' ); } // CL: unique_id is not used for API auth in standalone. return [ 'unique_id' => '', ]; } /** * Validate arguments for CLOS mode. * * @param array $assoc_args Associative arguments. * * @return array Array with 'unique_id' key. * @throws Exception When --unique_id is missing. */ private function validate_clos_args( $assoc_args ) { $unique_id = array_key_exists( 'unique_id', $assoc_args ) ? sanitize_text_field( wp_unslash( $assoc_args['unique_id'] ) ) : ''; if ( empty( $unique_id ) ) { throw new Exception( '--unique_id is empty' ); } return [ 'unique_id' => (string) $unique_id, ]; } }