From f88e87c79acf3a80f25b3cb47b1199a1cca123b6 Mon Sep 17 00:00:00 2001 From: Chuck Scott Date: Fri, 12 Aug 2016 13:48:29 -0400 Subject: [PATCH] Added XML sitemap. --- classes/glmSearch.php | 42 +++++---- models/admin/ajax/xmlSitemap.php | 132 +++++++++++++++++++++++++++ models/front/search/sitemap.php | 147 +++++++++++++++++++++++++++++++ setup/shortcodes.php | 41 +++------ setup/validActions.php | 10 ++- views/front/ajax/xmlSitemap.html | 12 +++ views/front/search/sitemap.html | 28 ++++++ 7 files changed, 363 insertions(+), 49 deletions(-) create mode 100644 models/admin/ajax/xmlSitemap.php create mode 100644 models/front/search/sitemap.php create mode 100644 views/front/ajax/xmlSitemap.html create mode 100644 views/front/search/sitemap.html diff --git a/classes/glmSearch.php b/classes/glmSearch.php index ee79ecc..820ebb0 100644 --- a/classes/glmSearch.php +++ b/classes/glmSearch.php @@ -236,28 +236,36 @@ class GLMSearch // Clear all the results data $this->clearResults(); - // Filter and validate supplied parameters - if ( - (filter_var($query, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH)) == false || - (in_array($operator, array('OR', 'AND'))) == false - ) { - return false; - } + // If query isn't false + if ($query !== false) { + + // Filter and validate supplied parameters + if ( + (filter_var($query, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH)) == false || + (in_array($operator, array('OR', 'AND'))) == false + ) { + return false; + } - // strip any characters that cause a problem. Add more characters to array if needed. - $remove = array("'"); - $query = htmlspecialchars_decode($query, ENT_QUOTES); - $query = str_replace("'", "", $query); + // strip any characters that cause a problem. Add more characters to array if needed. + $query = htmlspecialchars_decode($query, ENT_QUOTES); + $query = str_replace("'", "", $query); - $start = ($start - 0); - $rows = ($rows - 0); + // Check if we have a valid query + if (trim($query) == '') { + return false; + } + + } else { + // Otherwise just make it a blank query to get all results. + $query = ''; - // Check if we have a valid query - if (trim($query) == '') { - return false; } + $start = ($start - 0); + $rows = ($rows - 0); + // Create request URL switch ($this->type) { case 'field': @@ -448,7 +456,7 @@ class GLMSearch ); // Process possible multiple snippet values - if (is_array($ds['values']) && count($ds['values']) > 0) { + if (isset($ds['values']) && is_array($ds['values']) && count($ds['values']) > 0) { foreach ($ds['values'] as $sv) { $doc[$ds['fieldName']]['snippets'][] = $sv; } diff --git a/models/admin/ajax/xmlSitemap.php b/models/admin/ajax/xmlSitemap.php new file mode 100644 index 0000000..09ea41f --- /dev/null +++ b/models/admin/ajax/xmlSitemap.php @@ -0,0 +1,132 @@ + + * @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 "
".print_r($searchResult,1)."
";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 index 0000000..34c3506 --- /dev/null +++ b/models/front/search/sitemap.php @@ -0,0 +1,147 @@ + + * @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 diff --git a/setup/shortcodes.php b/setup/shortcodes.php index f3c2c51..9721b28 100644 --- a/setup/shortcodes.php +++ b/setup/shortcodes.php @@ -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 = '   - type="{search type}" NOT YET IMPLEMENTED - - The type of search -

- - - - - -
Search Types
standardTypical search engine results
sitemapA user-readable list of all pages in the site with links to the page.
xml-sitemapAn XML sitemap of all pages for this Web site
-

- - - -   - order="{order type}" NOT YET IMPLEMENTED + search="{search string}" - Order of search results -

- - - - -
Order Types
relevanceSort results by relevance with most relevant result first
date/td>Sort results by last date update with most recent first.
-

+ Specified text to search for. Only use if you want to start with a particular search. + [glm-members-search-sitemap]   - search="{search string}" - - Specified text to search for. Only use if you want to start with a particular search. + + Displays a sitemap of all pages indexed for this site. '; diff --git a/setup/validActions.php b/setup/validActions.php index 28b06c4..3563bda 100644 --- a/setup/validActions.php +++ b/setup/validActions.php @@ -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 index 0000000..5c49d79 --- /dev/null +++ b/views/front/ajax/xmlSitemap.html @@ -0,0 +1,12 @@ +{if $haveSearchResult} + +{foreach $searchResult.documents as $doc} + + {$doc.url.value} +{if $doc.lastModifiedDate.textValue} + {$doc.lastModifiedDate.textValue} +{/if} + weekly + +{/foreach} +{/if} diff --git a/views/front/search/sitemap.html b/views/front/search/sitemap.html new file mode 100644 index 0000000..8d340ce --- /dev/null +++ b/views/front/search/sitemap.html @@ -0,0 +1,28 @@ +{include file='front/search/header.html'} + +{if $haveSearchRequest} + + {if $haveSearchResult} + + + + +
+ + {foreach $searchResult.documents as $doc} + {$doc.title.value}
+ {/foreach} + +
+ + {else} +

No results matching your search.

+ {/if} + +{else} + (Please enter your desired search above.) +{/if} + + + +{include file='front/footer.html'} -- 2.17.1