Added XML sitemap.
authorChuck Scott <cscott@gaslightmedia.com>
Fri, 12 Aug 2016 17:48:29 +0000 (13:48 -0400)
committerChuck Scott <cscott@gaslightmedia.com>
Fri, 12 Aug 2016 17:48:29 +0000 (13:48 -0400)
classes/glmSearch.php
models/admin/ajax/xmlSitemap.php [new file with mode: 0644]
models/front/search/sitemap.php [new file with mode: 0644]
setup/shortcodes.php
setup/validActions.php
views/front/ajax/xmlSitemap.html [new file with mode: 0644]
views/front/search/sitemap.html [new file with mode: 0644]

index ee79ecc..820ebb0 100644 (file)
@@ -236,28 +236,36 @@ class GLMSearch
         // Clear all the results data\r
         $this->clearResults();\r
 \r
-        // Filter and validate supplied parameters\r
-        if (\r
-            (filter_var($query, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH)) == false ||\r
-            (in_array($operator, array('OR', 'AND'))) == false\r
-        ) {\r
-            return false;\r
-        }\r
+        // If query isn't false\r
+        if ($query !== false) {\r
+\r
+            // Filter and validate supplied parameters\r
+            if (\r
+                (filter_var($query, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH)) == false ||\r
+                (in_array($operator, array('OR', 'AND'))) == false\r
+            ) {\r
+                return false;\r
+            }\r
 \r
-        // strip any characters that cause a problem. Add more characters to array if needed.\r
-        $remove = array("'");\r
-        $query = htmlspecialchars_decode($query, ENT_QUOTES);\r
-        $query = str_replace("'", "", $query);\r
+            // strip any characters that cause a problem. Add more characters to array if needed.\r
+            $query = htmlspecialchars_decode($query, ENT_QUOTES);\r
+            $query = str_replace("'", "", $query);\r
 \r
-        $start = ($start - 0);\r
-        $rows = ($rows - 0);\r
+            // Check if we have a valid query\r
+            if (trim($query) == '') {\r
+                return false;\r
+            }\r
+\r
+        } else {\r
 \r
+            // Otherwise just make it a blank query to get all results.\r
+            $query = '';\r
 \r
-        // Check if we have a valid query\r
-        if (trim($query) == '') {\r
-            return false;\r
         }\r
 \r
+        $start = ($start - 0);\r
+        $rows = ($rows - 0);\r
+\r
         // Create request URL\r
         switch ($this->type) {\r
                case 'field':\r
@@ -448,7 +456,7 @@ class GLMSearch
                 );\r
 \r
                 // Process possible multiple snippet values\r
-                if (is_array($ds['values']) && count($ds['values']) > 0) {\r
+                if (isset($ds['values']) && is_array($ds['values']) && count($ds['values']) > 0) {\r
                     foreach ($ds['values'] as $sv) {\r
                         $doc[$ds['fieldName']]['snippets'][] = $sv;\r
                     }\r
diff --git a/models/admin/ajax/xmlSitemap.php b/models/admin/ajax/xmlSitemap.php
new file mode 100644 (file)
index 0000000..09ea41f
--- /dev/null
@@ -0,0 +1,132 @@
+<?php
+/**
+ * Gaslight Media Members Database
+ * XML Sitemap via AJAX
+ *
+ * PHP version 5.5
+ *
+ * @category glmWordPressPlugin
+ * @package  glmMembersDatabase
+ * @author   Chuck Scott <cscott@gaslightmedia.com>
+ * @license  http://www.gaslightmedia.com Gaslightmedia
+ * @version  0.1
+ */
+
+// Load GLM Search class
+require_once(GLM_MEMBERS_SEARCH_PLUGIN_CLASS_PATH.'/glmSearch.php');
+
+/*
+ * This class uses the Gaslight Media Open Search Server to
+ * produce an XML sitemap for this Website based on the OSS
+ * Index.
+ *
+ * The URL for a site map is the following...
+ *
+ * {site}/wp-admin/admin-ajax.php?action=glm_members_admin_ajax&glm_action=xmlSitemap
+ */
+class GlmMembersAdmin_ajax_xmlSitemap extends GlmSearch
+{
+
+    /**
+     * WordPress Database Object
+     *
+     * @var $wpdb
+     * @access public
+     */
+    public $wpdb;
+    /**
+     * Plugin Configuration Data
+     *
+     * @var $config
+     * @access public
+     */
+    public $config;
+
+    /*
+     * Constructor
+     *
+     * This contructor sets up this model. At this time that only includes
+     * storing away the WordPress data object.
+     *
+     * @return object Class object
+     *
+     */
+    public function __construct ($wpdb, $config)
+    {
+
+        // Save WordPress Database object
+        $this->wpdb = $wpdb;
+
+        // Save plugin configuration object
+        $this->config = $config;
+
+        // Get the Open Search Server parameters from management settings
+        $index = $this->config['settings']['search_index'];
+        $website = $this->config['settings']['website'];
+        $login = $this->config['settings']['login'];
+        $key = $this->config['settings']['login_key'];
+        $exclude_home_page = false;
+
+        // Run constructor for members data class
+        parent::__construct($index, $website, $login, $key, $exclude_home_page);
+
+    }
+
+    /*
+     * Perform Model Action
+     *
+     * This modelAction takes an AJAX image upload and stores the image in the
+     * media/images directory of the plugin.
+     *
+     * This model action does not return, it simply does it's work then calls die();
+     *
+     * @param $actionData
+     *
+     * Echos JSON string as response and does not return
+     */
+    public function modelAction ($actionData = false)
+    {
+
+        $haveSearchRequest = false;
+        $haveSearchResult = false;
+        $status = true;
+        $query = false;
+        $start = 0;
+        $rows = 99999;
+        $searchResult = array();
+
+        $haveSearchRequest = true;
+
+        // Send request to OpenSearchServer and get results
+        $searchResult = $this->glmSearch(false, 'OR', $start, $rows);
+        if (is_array($searchResult) && isset($searchResult['totalResults']) && $searchResult['totalResults'] > 0 ) {
+            $haveSearchResult = true;
+        }
+// echo "<pre>".print_r($searchResult,1)."</pre>";exit;
+        // Compile template data
+        $templateData = array(
+            'haveSearchRequest' => $haveSearchRequest,
+            'haveSearchResult'  => $haveSearchResult,
+            'searchResult'      => $searchResult,
+            'query'             => $query
+        );
+
+        $view = 'xmlSitemap.html';
+
+        header("Content-Type: text/xml");
+        header("Content-Disposition: attachment; filename=sitemap.xml");
+
+        // Return status, suggested view, and data to controller - also return any modified settings
+        return array(
+            'status'            => $status,
+            'menuItemRedirect'  => false,
+            'modelRedirect'     => false,
+            'view'              => 'front/ajax/'.$view,
+            'data'              => $templateData
+        );
+
+        wp_die();
+
+    }
+
+}
diff --git a/models/front/search/sitemap.php b/models/front/search/sitemap.php
new file mode 100644 (file)
index 0000000..34c3506
--- /dev/null
@@ -0,0 +1,147 @@
+<?php
+/**
+ * Gaslight Media Members Database
+ * HTML Site-Map using OSS
+ *
+ * PHP version 5.5
+ *
+ * @category glmWordPressPlugin
+ * @package  glmMembersDatabase
+ * @author   Chuck Scott <cscott@gaslightmedia.com>
+ * @license  http://www.gaslightmedia.com Gaslightmedia
+ * @version  0.1
+ */
+
+// Load GLM Search class
+require_once(GLM_MEMBERS_SEARCH_PLUGIN_CLASS_PATH.'/glmSearch.php');
+
+/*
+ * Perform searches using the Gaslight Media Open Search Server
+ * and display results
+ */
+class GlmMembersFront_search_sitemap extends GLMSearch
+{
+
+    /**
+     * WordPress Database Object
+     *
+     * @var $wpdb
+     * @access public
+     */
+    public $wpdb;
+    /**
+     * Plugin Configuration Data
+     *
+     * @var $config
+     * @access public
+     */
+    public $config;
+
+    /*
+     * Constructor
+     *
+     * This contructor sets up this model. At this time that only includes
+     * storing away the WordPress data object.
+     *
+     * @return object Class object
+     *
+     */
+    public function __construct ($wpdb, $config)
+    {
+
+        // Save WordPress Database object
+        $this->wpdb = $wpdb;
+
+        // Save plugin configuration object
+        $this->config = $config;
+
+        // Get the Open Search Server parameters from management settings
+        $index = $this->config['settings']['search_index'];
+        $website = $this->config['settings']['website'];
+        $login = $this->config['settings']['login'];
+        $key = $this->config['settings']['login_key'];
+        $exclude_home_page = $this->config['settings']['exclude_home_page'];
+
+        // Run constructor for members data class
+        parent::__construct($index, $website, $login, $key, $exclude_home_page);
+
+    }
+
+    /*
+     * Perform Model Action
+     *
+     * This method does the work for this model and returns any resulting data
+     *
+     * @return array Status and data array
+     *
+     * 'status'
+     *
+     * True if successfull and false if there was a fatal failure.
+     *
+     * 'menuItemRedirect'
+     *
+     * If not false, provides a menu item the controller should
+     * execute after this one. Normally if this is used, there would also be a
+     * modelRedirect value supplied as well.
+     *
+     * 'modelRedirect'
+     *
+     * If not false, provides an action the controller should execute after
+     * this one.
+     *
+     * 'view'
+     *
+     * A suggested view name that the contoller should use instead of the
+     * default view for this model or false to indicate that the default view
+     * should be used.
+     *
+     * 'data'
+     *
+     * Data that the model is returning for use in merging with the view to
+     * produce output.
+     *
+     */
+    public function modelAction ($actionData = false)
+    {
+
+        $haveSearchRequest = false;
+        $haveSearchResult = false;
+        $status = true;
+        $query = false;
+        $start = 0;
+        $rows = 99999;
+        $searchResult = array();
+
+        $haveSearchRequest = true;
+
+        // Send request to OpenSearchServer and get results
+        $searchResult = $this->glmSearch(false, 'OR', $start, $rows);
+        if (is_array($searchResult) && isset($searchResult['totalResults']) && $searchResult['totalResults'] > 0 ) {
+            $haveSearchResult = true;
+        }
+
+        // Compile template data
+        $templateData = array(
+            'haveSearchRequest' => $haveSearchRequest,
+            'haveSearchResult'  => $haveSearchResult,
+            'searchResult'      => $searchResult,
+            'query'             => $query
+        );
+
+        $view = 'sitemap.html';
+
+        // Return status, suggested view, and data to controller - also return any modified settings
+        return array(
+            'status'            => $status,
+            'menuItemRedirect'  => false,
+            'modelRedirect'     => false,
+            'view'              => 'front/search/'.$view,
+            'data'              => $templateData
+        );
+
+    }
+
+
+}
+
+?>
\ No newline at end of file
index f3c2c51..9721b28 100644 (file)
@@ -99,10 +99,15 @@ $glmMembersSearchShortcodes = array(
         'action' => 'index',
         'table' => false,
         'attributes' => array(
-            'type' => 'standard',                       // 'standard', 'xml-sitemap'
-            'order' => 'relevance',                     // 'relevance', 'date'
             'search' => false                           // Text to search for or false
         )
+    ),
+    'glm-members-search-sitemap' => array(
+        'plugin' => GLM_MEMBERS_SEARCH_PLUGIN_SLUG,
+        'menu' => 'search',
+        'action' => 'sitemap',
+        'table' => false,
+        'attributes' => array()
     )
 );
 
@@ -120,38 +125,16 @@ $glmMembersSearchShortcodesDescription = '
     </tr>
     <tr>
         <td>&nbsp;</td>
-        <th>type="{search type}" NOT YET IMPLEMENTED</th>
-        <td>
-            The type of search
-            <p>
-                <table width="100%">
-                    <tr><th colspan=3">Search Types</th></tr>
-                    <tr><td>standard</td><td>Typical search engine results</td></tr>
-                    <tr><td>sitemap</td><td>A user-readable list of all pages in the site with links to the page.</td></tr>
-                    <tr><td>xml-sitemap</td><td>An XML sitemap of all pages for this Web site</td></tr>
-                </table>
-            </p>
-        </td>
-    </tr>
-    <tr>
-        <td>&nbsp;</td>
-        <th>order="{order type}" NOT YET IMPLEMENTED</th>
+        <th>search="{search string}"</th>
         <td>
-            Order of search results
-            <p>
-                <table width="100%">
-                    <tr><th colspan=3">Order Types</th></tr>
-                    <tr><td>relevance</td><td>Sort results by relevance with most relevant result first</td></tr>
-                    <tr><td>date/td><td>Sort results by last date update with most recent first.</td></tr>
-                </table>
-            </p>
+            Specified text to search for. Only use if you want to start with a particular search.
         </td>
     </tr>
     <tr>
+        <th>[glm-members-search-sitemap]</th>
         <td>&nbsp;</td>
-        <th>search="{search string}"</th>
-        <td>
-            Specified text to search for. Only use if you want to start with a particular search.
+        <td width="50%">
+            Displays a sitemap of all pages indexed for this site.
         </td>
     </tr>
 ';
index 28b06c4..3563bda 100644 (file)
@@ -40,16 +40,20 @@ $glmMembersSampleAddOnValidActions = array(
 
 $glmMembersSearchAddOnValidActions = array(
     'adminActions' => array(
+        'ajax' => array(
+            'xmlSitemap'       => GLM_MEMBERS_SEARCH_PLUGIN_SLUG
+        ),
         'management' => array(
-            'search' => GLM_MEMBERS_SEARCH_PLUGIN_SLUG
+            'search'            => GLM_MEMBERS_SEARCH_PLUGIN_SLUG
         ),
         'dashboardWidget' => array(
-            'search' => GLM_MEMBERS_SEARCH_PLUGIN_SLUG
+            'search'            => GLM_MEMBERS_SEARCH_PLUGIN_SLUG
         )
     ),
     'frontActions' => array(
         'search' => array(
-            'index' => GLM_MEMBERS_SEARCH_PLUGIN_SLUG
+            'index'             => GLM_MEMBERS_SEARCH_PLUGIN_SLUG,
+            'sitemap'           => GLM_MEMBERS_SEARCH_PLUGIN_SLUG
         )
     )
 );
diff --git a/views/front/ajax/xmlSitemap.html b/views/front/ajax/xmlSitemap.html
new file mode 100644 (file)
index 0000000..5c49d79
--- /dev/null
@@ -0,0 +1,12 @@
+{if $haveSearchResult}<?xml version="1.0" encoding="UTF-8"?>
+<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
+{foreach $searchResult.documents as $doc}
+    <url>
+        <loc>{$doc.url.value}</loc>
+{if $doc.lastModifiedDate.textValue}
+        <lastmod>{$doc.lastModifiedDate.textValue}</lastmod>
+{/if}
+        <changefreq>weekly</changefreq>
+    </url>
+{/foreach}
+</urlset>{/if}
diff --git a/views/front/search/sitemap.html b/views/front/search/sitemap.html
new file mode 100644 (file)
index 0000000..8d340ce
--- /dev/null
@@ -0,0 +1,28 @@
+{include file='front/search/header.html'}
+    
+{if $haveSearchRequest}
+  
+  {if $haveSearchResult}
+                          
+        
+            <!-- Search results -->
+            
+            <div id="GLMSresultsContainer">
+
+        {foreach $searchResult.documents as $doc}
+                <a href="{$doc.url.value}">{$doc.title.value}</a><br>
+        {/foreach}
+                
+            </div>
+                
+  {else}
+            <h4>No results matching your search.</h4>
+  {/if}
+  
+{else}
+    (Please enter your desired search above.)
+{/if}
+
+</div> <!-- /search wrapper -->
+
+{include file='front/footer.html'}