Skip to content

Instantly share code, notes, and snippets.

@xnau
Last active December 1, 2023 03:38
Show Gist options
  • Save xnau/11f5fd16b3601c2c58e9bb95116f4ee6 to your computer and use it in GitHub Desktop.
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
<?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();
@xnau
Copy link
Author

xnau commented Nov 25, 2023

This demo plugin is set up so that all standard Participants Database searches use the "Strict User Searching" setting unless there is a wildcard present in the search term.

In this case, a wildcard is the "*" character, which represents one or more characters.

This should not be used in situation where you want to prevent someone seeing all the records, which is the usual reason for using the "Strict User Searching" setting. You can modify how this works on line 39.

@xnau
Copy link
Author

xnau commented Dec 1, 2023

Update to also automatically set the strict mode with searches made with a URL

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment