Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ This repository contains a [WP-CLI command](https://github.com/wp-cli/wp-cli) f
* `wp rocket regenerate --file=<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=<enable> --host=<host> --zone=<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=<file_path>` -- 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
Expand Down
18 changes: 13 additions & 5 deletions command.php
Original file line number Diff line number Diff line change
Expand Up @@ -455,10 +455,18 @@ public function cdn( $args = array(), $assoc_args = array() ) {

/**
* Export Settings.
*
* ## OPTIONS
*
* [--output-file=<file_path>]
* : 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
*/
Expand All @@ -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 );

}

/**
Expand Down