From 354e9c180f463a772de5528b110aa3987771ae7f Mon Sep 17 00:00:00 2001
From: Edward Hibbert <edward@ehibbert.org.uk>
Date: Mon, 1 Mar 2021 14:02:11 +0000
Subject: [PATCH] Pass other rd_ URL parameters into IFRAME

---
 repair-directory.php | 28 +++++++++++++++++++++++++---
 1 file changed, 25 insertions(+), 3 deletions(-)

diff --git a/repair-directory.php b/repair-directory.php
index a40492a..2467759 100644
--- a/repair-directory.php
+++ b/repair-directory.php
@@ -37,12 +37,34 @@ function repair_directory_plugin_add_shortcode_cb($atts)
         }
     }
 
-    // Construct source, which needs to include the region.
-    $atts['src'] = esc_url($atts['src']) . '?rd_region=' . esc_attr($atts['rd_region']) . '&rd_parenturl=' . esc_attr($atts['rd_parenturl']);
+    // Construct source URL, which needs to include the region and parent from the attributes on this shortcode.
+    // It also needs to include any URL parameters starting with rd_ - this allows sharing of results.
+    //
+    // The standard WordPress way to do this is using get_query_var, but that requires us to know in advance which query
+    // variables the Repair Directory will use, and we don't want to introduce that dependency.
+    $params = [
+        'rd_region' => esc_attr($atts['rd_region']),
+        'rd_parenturl' => esc_attr($atts['rd_parenturl'])
+    ];
+
     unset($atts['rd_region']);
     unset($atts['rd_parenturl']);
 
-    // Construct HTML to return
+    foreach ($_GET as $var => $value) {
+        if (strpos($var, 'rd_') === 0) {
+            $params[esc_attr($var)] = esc_attr($value);
+        }
+    }
+
+    $url = esc_url($atts['src']) . '?';
+
+    foreach ($params as $var => $value) {
+        $url .= "&$var=$value";
+    }
+
+    $atts['src'] = $url;
+
+    // Construct HTML to return.
     $html = "\n<!-- Repair Directory plugin v" . REPAIR_DIRECTORY_PLUGIN_VERSION . ' -->' . "\n";
     $html .= '<IFRAME';