Skip to content
This repository
Browse code

wp-cli support with an htaccess importer

Update the create_redirect() method to return WP_Error if the redirect already exists. Duplicate redirects don't make sense.
  • Loading branch information...
commit 57ea43a3a12ae2fa6ec3908193c9a404fcf89a3a 1 parent 24e7900
Daniel Bachhuber authored November 25, 2012
63  inc/wp-cli.php
... ...
@@ -0,0 +1,63 @@
  1
+<?php
  2
+/**
  3
+ * wp-cli integration
  4
+ */
  5
+
  6
+WP_CLI::add_command( 'safe-redirect-manager', 'Safe_Redirect_Manager_CLI' );
  7
+
  8
+class Safe_Redirect_Manager_CLI extends WP_CLI_Command {
  9
+
  10
+
  11
+	/**
  12
+	 * Import .htaccess file redirects
  13
+	 *
  14
+	 * @subcommand import-htaccess
  15
+	 * @synopsis <file>
  16
+	 */
  17
+	public function import_htaccess( $args, $assoc_args ) {
  18
+		global $safe_redirect_manager;
  19
+
  20
+		list( $file ) = $args;
  21
+
  22
+		$contents = file_get_contents( $file );
  23
+		if ( ! $contents )
  24
+			WP_CLI::error( "Error retrieving .htaccess file" );
  25
+
  26
+		$pieces = explode( PHP_EOL, $contents );
  27
+		$created = 0;
  28
+		$skipped = 0;
  29
+		foreach( $pieces as $piece ) {
  30
+
  31
+			// Ignore if this line isn't a redirect
  32
+			if ( 0 !== stripos( $piece, 'Redirect permanent' ) )
  33
+				continue;
  34
+
  35
+			// Parse the redirect
  36
+			$redirect = str_ireplace( 'Redirect permanent ', '', $piece );
  37
+			$redirect = explode( ' ', $redirect );
  38
+			$from = $redirect[0];
  39
+			$to = $redirect[1];
  40
+
  41
+			// Validate
  42
+			if ( ! $from || ! $to ) {
  43
+				WP_CLI::warning( "Skipping - '{$piece}' is formatted improperly." );
  44
+				continue;
  45
+			}
  46
+
  47
+			$sanitized_redirect_from = $safe_redirect_manager->sanitize_redirect_from( $from );
  48
+			$sanitized_redirect_to = $safe_redirect_manager->sanitize_redirect_to( $to );
  49
+
  50
+			$id = $safe_redirect_manager->create_redirect( $sanitized_redirect_from, $sanitized_redirect_to, 301 );
  51
+			if ( is_wp_error( $id ) ) {
  52
+				WP_CLI::warning( "Error - " . $id->get_error_message() );
  53
+				$skipped++;
  54
+			} else {
  55
+				WP_CLI::line( "Success - Created redirect from '{$sanitized_redirect_from}' to '{$sanitized_redirect_to}" );
  56
+				$created++;
  57
+			}
  58
+		}
  59
+		WP_CLI::success( "All done! {$created} redirects were created, {$skipped} were skipped" );
  60
+	}
  61
+
  62
+
  63
+}
9  safe-redirect-manager.php
@@ -25,6 +25,9 @@
25 25
 
26 26
 */
27 27
 
  28
+if ( defined( 'WP_CLI' ) && WP_CLI )
  29
+	require_once dirname( __FILE__ ) . '/inc/wp-cli.php';
  30
+
28 31
 class SRM_Safe_Redirect_Manager {
29 32
 
30 33
 	public $redirect_post_type = 'redirect_rule';
@@ -208,6 +211,8 @@ public function filter_bulk_actions() {
208 211
 	 * @return int
209 212
 	 */
210 213
 	public function create_redirect( $redirect_from, $redirect_to, $status_code ) {
  214
+		global $wpdb;
  215
+
211 216
 		$sanitized_redirect_from = $this->sanitize_redirect_from( $redirect_from );
212 217
 		$sanitized_redirect_to = $this->sanitize_redirect_to( $redirect_to );
213 218
 		$sanitized_status_code = absint( $status_code );
@@ -216,6 +221,10 @@ public function create_redirect( $redirect_from, $redirect_to, $status_code ) {
216 221
 		if ( empty( $sanitized_redirect_from ) || empty( $sanitized_redirect_to ) || ! in_array( $sanitized_status_code, $this->valid_status_codes ) )
217 222
 			return 0;
218 223
 
  224
+		// Check to ensure this redirect doesn't already exist
  225
+		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 ) ) )
  226
+			return new WP_Error( 'duplicate-redirect', sprintf( __( 'Redirect already exists for %s', 'safe-redirect-manager' ), $sanitized_redirect_from ) );
  227
+
219 228
 		// create the post
220 229
 		$post_args = array(
221 230
 			'post_type' => $this->redirect_post_type,

0 notes on commit 57ea43a

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