Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export redirects to csv #299

Merged
merged 3 commits into from
Jun 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 ) );
}
}