diff --git a/README.md b/README.md index 7eea2a3..30479fb 100755 --- a/README.md +++ b/README.md @@ -13,7 +13,8 @@ This repository contains a [WP-CLI command](https://github.com/wp-cli/wp-cli) f * `wp rocket regenerate --file=` -- Regenerate .htaccess, advanced-cache.php or the WP Rocket config file. * `wp rocket regenerate --file=config --nginx=true` -- regenerate the config file on Nginx hosts. * `wp rocket cdn --enable= --host= --zone=` -- Enable / Disable CDN with the specified host and zone. -* `wp rocket export` -- Exports the WP Rocket settings as a json file to the current directory. +* `wp rocket export --output-file=` -- Exports the WP Rocket settings as a json file to the current directory +or to the specified file path. * `wp rocket import --file=settings.json` -- Imports the settings contained in the provided json file. ## Installing diff --git a/command.php b/command.php index 1f01f3c..4513892 100644 --- a/command.php +++ b/command.php @@ -455,10 +455,18 @@ public function cdn( $args = array(), $assoc_args = array() ) { /** * Export Settings. + * + * ## OPTIONS + * + * [--output-file=] + * : The path where the exported json should be written: + * - example.json + * - /tmp/example.json * * ## EXAMPLES * * wp rocket export + * wp rocket export --output-file=wp-settings-site-1.json * * @subcommand export */ @@ -467,20 +475,20 @@ public function export( $args = array(), $assoc_args = array() ) { if ( ! is_plugin_active( 'wp-rocket/wp-rocket.php' ) ) { WP_CLI::error( 'WP Rocket is not enabled.' ); } - - $filename = sprintf( 'wp-rocket-settings-%s-%s.json', date( 'Y-m-d' ), uniqid() ); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date - $gz = 'gz' . strrev( 'etalfed' ); + $filename = $assoc_args['output-file'] ? $assoc_args['output-file'] : false; + if(!$filename){ + $filename = sprintf( 'wp-rocket-settings-%s-%s.json', date( 'Y-m-d' ), uniqid() ); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date + } $options = wp_json_encode( get_option( 'wp_rocket_settings' ) ); $file = fopen( $filename, 'w' ); $res = fwrite( $file, $options ); if ( false === $res ) { - WP_CLI::error( 'Export: error writing to export file.' ); + WP_CLI::error( 'Export: error writing to export file '. $filename ); } else { WP_CLI::success( 'Successfully exported in ' . $filename ); } fclose( $file ); - } /**