Last active
November 21, 2023 18:26
-
-
Save xnau/f5505976eaea5e69a5afd85efbf509a1 to your computer and use it in GitHub Desktop.
Provides a "return to search results" shortcode for Participants Database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: PDB Return to Results Shortcode | |
* Description: adds a shortcode that displays a ling to return to the previous search result | |
* Author: xnau webdesign | |
* Version: 1.3 | |
*/ | |
class PDb_Search_Return_Link { | |
/** | |
* @var string name of the filter transient | |
*/ | |
private $filter_transient = 'pdb-returnlink-filter'; | |
/** | |
* @var holds the permalink to the list search page | |
*/ | |
private $listpage; | |
/** | |
* | |
*/ | |
public function __construct() | |
{ | |
add_shortcode( 'pdb_search_return_link', [$this,'show_link']); | |
add_action( 'pdb-list_query_object', [$this,'save_filter']); | |
} | |
/** | |
* displays the return to search result link | |
* | |
* @param array|null $shortcode_atts | |
*/ | |
public function show_link( $shortcode_atts ) | |
{ | |
$this->listpage = get_permalink( Participants_Db::get_id_by_slug($shortcode_atts['listpage']) ); | |
ob_start(); | |
?> | |
<div class="pdb-returnlink"> | |
<p><a href="<?php echo $this->return_url() ?>">Return to Search Results</a></p> | |
</div> | |
<?php | |
return ob_get_clean(); | |
} | |
/** | |
* saves the last used search filter | |
* | |
* @param PDb_List_Query $list_query | |
*/ | |
public function save_filter( $list_query ) | |
{ | |
$last_filter = $list_query->current_filter(); | |
if ( method_exists( $list_query, 'current_page' ) ) | |
{ | |
$listpage = $list_query->current_page(); | |
} | |
else | |
{ | |
$listpage = filter_input(INPUT_POST, Participants_Db::$list_page, FILTER_SANITIZE_NUMBER_INT ) ? : 1; | |
} | |
$last_filter[Participants_Db::$list_page] = $listpage; | |
$last_filter['instance_index'] = 1; | |
if ( method_exists( $list_query, 'instance_index' ) ) | |
{ | |
$last_filter['instance_index'] = $list_query->instance_index(); | |
} | |
set_transient( $this->filter_transient, $last_filter ); | |
} | |
/** | |
* builds the return URL | |
*/ | |
private function return_url() | |
{ | |
$baseurl = $this->listpage; | |
$join = strpos( $baseurl, '?' ) === false ? '?' : '&'; | |
return $baseurl . $join . $this->url_params(); | |
} | |
/** | |
* builds the URL params | |
* | |
* @return string | |
*/ | |
private function url_params() | |
{ | |
$filter = get_transient( $this->filter_transient ); | |
if ( ! is_array( $filter ) ) | |
{ | |
return []; | |
} | |
$operator = Participants_Db::plugin_setting( 'strict_search', 0 ) == 1 ? '=' : 'LIKE'; | |
$params = []; | |
foreach( $filter['search_fields'] as $i => $search_field ) | |
{ | |
$params[] = 'search_field=' . $search_field . '&value=' . urlencode( $filter['search_values'][$i] ) . '&operator=' . urlencode( $operator ); | |
} | |
$params[] = 'listpage=' . $filter[ Participants_Db::$list_page ]; | |
$params[] = 'instance=' . $filter['instance_index']; | |
return implode( '&', $params ); | |
} | |
} | |
new PDb_Search_Return_Link(); |
Updated to implement the "strict user searching" feature.
Updated to include support for retaining the page number and multiple tragets on the list page using the "instance index" value.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This places a "return to search results" link. Place it on the page with the [pdb_single] shortcode.
You must provide the name of the page where your searchable list is displayed in the "listpage" attribute of the shortcode.
For example:
[pdb_search_return_link listpage=participant-list]