From 86187884f3788d193afce20c61b0958b3451a954 Mon Sep 17 00:00:00 2001
From: Erick Hitter <ehitter@gmail.com>
Date: Sun, 3 Jun 2012 18:06:24 -0400
Subject: [PATCH] Version 0.5.1. Corrects problem with query string used with
 verbose page rules.

---
 readme.txt            |  8 +++++++-
 wp-print-friendly.php | 21 +++++++++++++++++++--
 2 files changed, 26 insertions(+), 3 deletions(-)

diff --git a/readme.txt b/readme.txt
index a4d522e..92d7e23 100644
--- a/readme.txt
+++ b/readme.txt
@@ -4,7 +4,7 @@ Donate link: http://www.thinkoomph.com/plugins-modules/wp-print-friendly/
 Tags: print, template, printer, printable
 Requires at least: 3.1
 Tested up to: 3.4
-Stable tag: 0.5
+Stable tag: 0.5.1
 
 Extends WordPress' template system to support printer-friendly templates. Works with permalink structures to support nice URLs.
 
@@ -57,6 +57,9 @@ This plugin is known to conflict with certain plugins, many pertaining to SEO an
 
 == Changelog ==
 
+= 0.5.1 =
+* Correct construction of query needed in situations where verbose page rules are required.
+
 = 0.5 =
 * Add additional rewrite rules for situations where verbose page rules are required.
 * Disable canonical redirect when print template is requested.
@@ -126,6 +129,9 @@ This plugin is known to conflict with certain plugins, many pertaining to SEO an
 
 == Upgrade Notice ==
 
+= 0.5.1 =
+Ensure that proper query string is built when verbose page rules are required.
+
 = 0.5 =
 Adds better support for sites that use verbose page rules, resolving situations where requests for print template redirect to the post.
 
diff --git a/wp-print-friendly.php b/wp-print-friendly.php
index f7484bb..0f1a779 100644
--- a/wp-print-friendly.php
+++ b/wp-print-friendly.php
@@ -4,7 +4,7 @@ Plugin Name: WP Print Friendly
 Plugin URI: http://www.thinkoomph.com/plugins-modules/wp-print-friendly/
 Description: Extends WordPress' template system to support printer-friendly templates. Works with permalink structures to support nice URLs.
 Author: Erick Hitter (Oomph, Inc.)
-Version: 0.5
+Version: 0.5.1
 Author URI: http://www.thinkoomph.com/
 */
 
@@ -108,11 +108,28 @@ class wp_print_friendly {
 
 		//Extra rules needed if verbose page rules are requested
 		if ( $wp_rewrite->use_verbose_page_rules ) {
+			//Build regex
 			$regex = substr( str_replace( $wp_rewrite->rewritecode, $wp_rewrite->rewritereplace, $wp_rewrite->permalink_structure ), 1 );
 			$regex = trailingslashit( $regex );
 			$regex .= $this->query_var . '(/([0-9]*))?/?$';
 
-			add_rewrite_rule( $regex, $wp_rewrite->index . '?category_name=$matches[1]&name=$matches[2]&' . $this->query_var . '=$matches[4]', 'top' );
+			//Build corresponding query string
+			$query = substr( str_replace( $wp_rewrite->rewritecode, $wp_rewrite->queryreplace, $wp_rewrite->permalink_structure ), 1 );
+			$query = explode( '/', $query );
+			$query = array_filter( $query );
+
+			$i = 1;
+			foreach ( $query as $key => $qv ) {
+				$query[ $key ] .= '$matches[' . $i . ']';
+				$i++;
+			}
+
+			$query[] = $this->query_var . '=$matches[' . ( $i + 1 ) . ']';
+
+			$query = implode( '&', $query );
+
+			//Add rule
+			add_rewrite_rule( $regex, $wp_rewrite->index . '?' . $query, 'top' );
 		}
 	}
 
-- 
GitLab