Skip to content
Browse files

Merge pull request #31 from tlovett1/htaccess-importer

wp-cli support with an htaccess importer
  • Loading branch information...
2 parents 7df4a00 + b1a5d28 commit 4d67c4cb52725797f31d04fadd5852352bc986e2 @danielbachhuber danielbachhuber committed
Showing with 84 additions and 0 deletions.
  1. +75 −0 inc/wp-cli.php
  2. +9 −0 safe-redirect-manager.php
View
75 inc/wp-cli.php
@@ -0,0 +1,75 @@
+<?php
+/**
+ * wp-cli integration
+ */
+
+WP_CLI::add_command( 'safe-redirect-manager', 'Safe_Redirect_Manager_CLI' );
+
+class Safe_Redirect_Manager_CLI extends WP_CLI_Command {
+
+
+ /**
+ * Import .htaccess file redirects
+ *
+ * @subcommand import-htaccess
+ * @synopsis <file>
+ */
+ public function import_htaccess( $args, $assoc_args ) {
+ global $safe_redirect_manager;
+
+ list( $file ) = $args;
+
+ $contents = file_get_contents( $file );
+ if ( ! $contents )
+ WP_CLI::error( "Error retrieving .htaccess file" );
+
+ $pieces = explode( PHP_EOL, $contents );
+ $created = 0;
+ $skipped = 0;
+ foreach( $pieces as $piece ) {
+
+ // Ignore if this line isn't a redirect
+ if ( ! preg_match( '/^Redirect( permanent)?/i', $piece ) )
+ continue;
+
+ // Parse the redirect
+ $redirect = preg_replace( '/\s{2,}/', ' ', $piece );
+ $redirect = preg_replace( '/^Redirect( permanent)? (.*)$/i', '$2', trim( $redirect ) );
+ $redirect = explode( ' ', $redirect );
+
+ // if there are three parts to the redirect, we assume the first part is a status code
+ if ( 2 == count( $redirect ) ) {
+ $from = $redirect[0];
+ $to = $redirect[1];
+ $http_status = 301;
+ } elseif ( 3 == count( $redirect ) ) {
+ $http_status = $redirect[0];
+ $from = $redirect[1];
+ $to = $redirect[2];
+ } else {
+ continue;
+ }
+
+ // Validate
+ if ( ! $from || ! $to ) {
+ WP_CLI::warning( "Skipping - '{$piece}' is formatted improperly." );
+ continue;
+ }
+
+ $sanitized_redirect_from = $safe_redirect_manager->sanitize_redirect_from( $from );
+ $sanitized_redirect_to = $safe_redirect_manager->sanitize_redirect_to( $to );
+
+ $id = $safe_redirect_manager->create_redirect( $sanitized_redirect_from, $sanitized_redirect_to, $http_status );
+ if ( is_wp_error( $id ) ) {
+ WP_CLI::warning( "Error - " . $id->get_error_message() );
+ $skipped++;
+ } else {
+ WP_CLI::line( "Success - Created redirect from '{$sanitized_redirect_from}' to '{$sanitized_redirect_to}'" );
+ $created++;
+ }
+ }
+ WP_CLI::success( "All done! {$created} redirects were created, {$skipped} were skipped" );
+ }
+
+
+}
View
9 safe-redirect-manager.php
@@ -25,6 +25,9 @@
*/
+if ( defined( 'WP_CLI' ) && WP_CLI )
+ require_once dirname( __FILE__ ) . '/inc/wp-cli.php';
+
class SRM_Safe_Redirect_Manager {
public $redirect_post_type = 'redirect_rule';
@@ -208,6 +211,8 @@ public function filter_bulk_actions() {
* @return int
*/
public function create_redirect( $redirect_from, $redirect_to, $status_code ) {
+ global $wpdb;
+
$sanitized_redirect_from = $this->sanitize_redirect_from( $redirect_from );
$sanitized_redirect_to = $this->sanitize_redirect_to( $redirect_to );
$sanitized_status_code = absint( $status_code );
@@ -216,6 +221,10 @@ public function create_redirect( $redirect_from, $redirect_to, $status_code ) {
if ( empty( $sanitized_redirect_from ) || empty( $sanitized_redirect_to ) || ! in_array( $sanitized_status_code, $this->valid_status_codes ) )
return 0;
+ // Check to ensure this redirect doesn't already exist
+ if ( $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key=%s AND meta_value=%s", $this->meta_key_redirect_from, $sanitized_redirect_from ) ) )
+ return new WP_Error( 'duplicate-redirect', sprintf( __( 'Redirect already exists for %s', 'safe-redirect-manager' ), $sanitized_redirect_from ) );
+
// create the post
$post_args = array(
'post_type' => $this->redirect_post_type,

0 comments on commit 4d67c4c

Please sign in to comment.
Something went wrong with that request. Please try again.