Skip to content

Commit

Permalink
Merge pull request #299 from 10up/feature/export-redirect-list
Browse files Browse the repository at this point in the history
Export redirects to csv
  • Loading branch information
jeffpaul committed Jun 12, 2023
2 parents 1a6c9a0 + 7ba127a commit 832ca34
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions inc/classes/class-srm-wp-cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* @package safe-redirect-manager
*/

use \WP_CLI\Utils;

/**
* WP CLI command class
*/
Expand Down Expand Up @@ -292,4 +294,56 @@ public function import( $args, $assoc_args ) {

WP_CLI::success( "All done! {$created} redirects were imported, {$skipped} were skipped" );
}

/**
* Export redirects to CSV file.
*
* ## EXAMPLE
*
* wp safe-redirect-manager export
* wp safe-redirect-manager export --filename=sample-redirects
*
* @since 1.11.2
*
* @access public
* @param array $args Arguments.
* @param array $assoc_args Associated aeguments.
*/
public function export_csv( $args, $assoc_args ) {

$assoc_args = wp_parse_args(
$assoc_args,
[
'filename' => 'srm-redirects',
]
);

$redirects = srm_get_redirects( [ 'post_status' => 'any' ], true );

if ( empty( $redirects ) ) {
WP_CLI::error( 'There are no redirects available. Please add some first and then try again.' );
}

$fields = [
'ID',
'redirect_from',
'redirect_to',
'status_code',
'enable_regex',
'post_status',
];

$file_name = $assoc_args['filename'] . '.csv';

if ( file_exists( $file_name ) ) {
WP_CLI::warning( sprintf( 'File already exists. The following file will be rewritten %s', $file_name ) );
WP_CLI::confirm( 'Proceed with rewritting the existing file?' );
}

$file_resource = fopen( $file_name, 'w' ); //phpcs:ignore

Utils\write_csv( $file_resource, $redirects, $fields );

WP_CLI::success( sprintf( 'Redirects exported to csv file %s', $file_name ) );
}
}

0 comments on commit 832ca34

Please sign in to comment.