Added package title as page title to package detail page.
authorChuck Scott <cscott@gaslightmedia.com>
Fri, 8 Jul 2016 14:59:35 +0000 (10:59 -0400)
committerChuck Scott <cscott@gaslightmedia.com>
Fri, 8 Jul 2016 14:59:35 +0000 (10:59 -0400)
index.php
setup/frontHooks.php

index 8052a07..d760cf1 100644 (file)
--- a/index.php
+++ b/index.php
@@ -3,8 +3,8 @@
  * Plugin Name: GLM Members Database Packaging
  * Plugin URI: http://www.gaslightmedia.com/
  * Description: Gaslight Media Members Database.
- * Version: 1.1.5
- * Author: Chuck Scott
+ * Version: 1.1.7
+ * Author: Gaslight Media
  * Author URI: http://www.gaslightmedia.com/
  * License: GPL2
  */
@@ -19,7 +19,7 @@
  * @package glmMembersDatabasePackagingAddOn
  * @author Chuck Scott <cscott@gaslightmedia.com>
  * @license http://www.gaslightmedia.com Gaslightmedia
- * @version 0.0.3
+ * @version 1.1.7
  */
 
 /*
@@ -37,8 +37,8 @@
  *  so that we're sure the other add-ons see an up to date
  *  version from this plugin.
  */
-define('GLM_MEMBERS_PACKAGING_PLUGIN_VERSION', '1.1.5');
-define('GLM_MEMBERS_PACKAGING_PLUGIN_DB_VERSION', '1.1.0');
+define('GLM_MEMBERS_PACKAGING_PLUGIN_VERSION', '1.1.7');
+define('GLM_MEMBERS_PACKAGING_PLUGIN_DB_VERSION', '1.1.2');
 
 // This is the minimum version of the GLM Members DB plugin require for this plugin.
 define('GLM_MEMBERS_PACKAGING_PLUGIN_MIN_MEMBERS_REQUIRED_VERSION', '2.0.0');
index 888dbb4..9bf8c81 100644 (file)
  *  *** Also note that parameters will be in the context of the main front controller constructor. ***
  */
 
-// Setup rewrite for package detail pages
-add_filter('rewrite_rules_array', function($rules) {
-    $newrules = array();
-    $newrules['('.$this->config['settings']['canonical_package_page'].')/([^/]*)$'] = 'index.php?pagename=$matches[1]&packageslug=$matches[2]';
-    return $newrules + $rules;
-});
+// Check if the canonical package page rewrite rule is not currently in place.
+$rules = get_option( 'rewrite_rules' );
+if ( ! isset( $rules['('.$this->config['settings']['canonical_package_page'].')/([^/]*)$'] ) ) {
+
+    // Setup rewrite for member detail pages
+    add_filter('rewrite_rules_array', function($rules) {
+        $newrules = array();
+        $newrules['('.$this->config['settings']['canonical_package_page'].')/([^/]*)$']='index.php?pagename=$matches[1]&packageslug=$matches[2]';
+        return $newrules + $rules;
+    });
+
+    // Rewrite is not in place, so setup init call to add it and flush the rewrite cache
+    add_filter('init', function() {
+        global $wp_rewrite;
+        $wp_rewrite->flush_rules();
+    });
+
+}
+
+/*
+ * Package Detail Page Canonical URL Fix
+ *
+ * Since the package detail pages are actually all the same page, and since WordPress has a specific
+ * canonical URL for that page, we need to override the rel='canonical' value in the page header.
+ *
+ * The code below checks if we're on a package detail page and if so returns the package slug so that
+ * can be added to the URL. The matching apply_filters is in the main plugin in setup/frontHooks.php.
+ *
+ * We're also going to set the page title to the package name in here while we're at it.
+ */
+if (strpos(GLM_MEMBERS_PLUGIN_CURRENT_URL, $this->config['settings']['canonical_package_page']) > 0) {
+
+    // Parse out only the package slug from the current URL
+    $m = array();
+    preg_match('|.*/'.$this->config['settings']['canonical_package_page'].'/([^/]*).*|', GLM_MEMBERS_PLUGIN_CURRENT_URL, $m );
+    $GLOBALS['glmDetailSlug'] = $m[1];
+
+    // Return the current package slug to the glm_rel_canonical_slug filter for use in the canonical page link.
+    add_filter('glm_rel_canonical_slug', function($detailSlug) {
+
+        // If another add-on already supplied the detail slug, then just return that.
+        if ($detailSlug != false) {
+            return $detailSlug;
+        }
+        return $GLOBALS['glmDetailSlug'];
+
+    });
+
+    // Get the package name for use in the page title
+    global $wpdb;
+    $GLOBALS['glmPackageName'] = $wpdb->get_var( "SELECT title FROM ".GLM_MEMBERS_PACKAGING_PLUGIN_DB_PREFIX . "packages WHERE package_slug = '".$GLOBALS['glmDetailSlug']."'" );
+
+    // Set the page title to the event name
+    add_filter('wp_title', function() {
+        return $GLOBALS['glmPackageName'];
+    });
+}
 
 // Add package slug query var
 add_filter('query_vars', function($vars) {
@@ -89,7 +140,7 @@ if (isset($this->config['addOns']['glm-member-db'])) {
         10,
         2
     );
-        
+
     add_filter('glm-member-db-front-members-detail-sidemenuBottom', function($content, $id) {
             $packageData = do_shortcode('[glm-members-packaging-list member='.$id.', template="detail-sidemenu" order="title"]');
             $content .= $packageData;