diff --git a/app/PriorPrice/Hooks.php b/app/PriorPrice/Hooks.php index 447edff..a667096 100644 --- a/app/PriorPrice/Hooks.php +++ b/app/PriorPrice/Hooks.php @@ -66,5 +66,11 @@ public function plugins_loaded() : void { $ajax = new Ajax(); $ajax->register_hooks(); + + if ( class_exists( 'WP_CLI' ) ) { + $wpcli = new WpCli\AddHistory( $history_storage, $settings_data, new Taxes() ); + $wpcli->register_hooks(); + } + } } diff --git a/app/PriorPrice/WpCli/AddHistory.php b/app/PriorPrice/WpCli/AddHistory.php new file mode 100644 index 0000000..d172cd9 --- /dev/null +++ b/app/PriorPrice/WpCli/AddHistory.php @@ -0,0 +1,95 @@ +history_storage = $history_storage; + $this->settings_data = $settings_data; + $this->taxes = $taxes; + } + + /** + * Register hooks. + * + * @return void + */ + public function register_hooks() : void { + add_action( 'cli_init', [ $this, 'register_command' ] ); + } + + /** + * Register command. + * + * @return void + */ + public function register_command() : void { + WP_CLI::add_command( 'wc-price-history add', [ $this, 'add_history' ] ); + } + + /** + * Add history to product. + * + * ## OPTIONS + * + * + * : Product ID. + * + * [--price=] + * : Price. + * + * ## EXAMPLES + * + * wp wc-price-history add 123 --price=200 + * + * @param array $args Arguments. + * @param array $assoc_args Associative arguments. + * + * @return void + */ + public function add_history( $args, $assoc_args ) : void { + + if ( ! isset( $assoc_args['price'] ) ) { + WP_CLI::error( 'Price is required.' ); + } + + $product = wc_get_product( $args[0] ); + + if ( ! $product ) { + WP_CLI::error( 'Product not found.' ); + } + + $product_id = (int) $args[0]; + + $this->history_storage->add_first_price( $product_id, (float) $assoc_args['price'] ); + } +}