Last active
December 1, 2023 03:38
-
-
Save xnau/11f5fd16b3601c2c58e9bb95116f4ee6 to your computer and use it in GitHub Desktop.
Demonstrates a Participants Database plugin that sets the "strict user searches" mode according to the presence of a wildcard in the search term
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 Auto Search Mode | |
* Description: switches the "strict user search" mode off if there is a wildcard in the search term | |
* Author: xnau webdesign | |
* Version: 1.2 | |
*/ | |
class pdb_auto_search_mode { | |
/** | |
* @var string strict mode setting value | |
*/ | |
private $mode = 1; | |
/** | |
* @var int the input we are checking | |
*/ | |
private $input; | |
/** | |
* | |
*/ | |
public function __construct() | |
{ | |
add_action( 'pdb-shortcode_set', [$this, 'set_search_mode'] ); | |
add_filter( 'pdb-strict_search_setting_value', [ $this, 'set_mode' ] ); | |
} | |
/** | |
* | |
* @param PDb_Shortcode $shortcode | |
* @return array | |
*/ | |
public function set_search_mode( $shortcode ) | |
{ | |
// check for the GET search variables | |
$this->input = array_key_exists( 'search_field', $_GET ) && array_key_exists( 'value', $_GET ) ? INPUT_GET : INPUT_POST; | |
// check if the submission is a list search | |
if ( $this->is_pdb_search() ) | |
{ | |
// get the search term | |
$term = filter_input( $this->input, 'value', FILTER_DEFAULT, Participants_Db::string_sanitize() ); | |
// get the setting value according to whether there is a wildcard present or not | |
$this->mode = strpos( $term, '*' ) === false ? 1 : 0; | |
} | |
} | |
/** | |
* sets the mode setting | |
* | |
* @param string $setting | |
* @return string | |
*/ | |
public function set_mode( $setting ) | |
{ | |
return $this->mode; | |
} | |
/** | |
* check to see if the current submission is a Participants Database search | |
* | |
* @return bool | |
*/ | |
private function is_pdb_search() | |
{ | |
if ( $this->input === INPUT_POST ) | |
{ | |
return filter_input( $this->input, 'action', FILTER_DEFAULT, Participants_Db::string_sanitize() ) === 'pdb_list_filter' && array_key_exists( 'search_field', $_POST ) && array_key_exists( 'value', $_POST ); | |
} | |
else { | |
return array_key_exists( 'search_field', $_GET ) && array_key_exists( 'value', $_GET ); | |
} | |
} | |
} | |
new pdb_auto_search_mode(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Update to also automatically set the strict mode with searches made with a URL