Skip to content

Instantly share code, notes, and snippets.

@xnau
Last active April 1, 2024 21:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xnau/fb72cff14cbba49c13e67263c88d818e to your computer and use it in GitHub Desktop.
Save xnau/fb72cff14cbba49c13e67263c88d818e to your computer and use it in GitHub Desktop.
Plugin to enable the exclusive option feature in the admin record edit in Participants Database
<?php
/**
* Plugin Name: PDB Admin Exclusive Options
* Description: enables exclusive options in the admin record editor
* Version: 1.1
*/
class pdb_admin_exclusive_options {
/**
* sets up the filters
*/
public function __construct()
{
if ( is_admin() )
{
add_action( 'participants-database_initialized', [$this, 'setup_field_filters'], 5 );
}
}
/**
* sets up the list of fields with exclusive options
*/
public function setup_field_filters()
{
foreach( Participants_Db::$fields as $field_def )
{
/** @var \PDb_Form_Field_Def $field_def */
if ( $field_def->is_value_set() && array_key_exists( 'exclusive', $field_def->attributes ) )
{
// for every field that has the exclusive attribute, we add the filter that esclused the alread-chosen options
add_filter( 'pdb-' . $field_def->name() . '_selector_option_attribute_list', [$this,'set_disabled_atts'], 10, 4 );
}
}
}
/**
* handles setting the disabled attributes
*
*
*
* @param array $attributes
* @param string $fieldname
* @param string $value
* @return array of attributes
*/
public function set_disabled_atts( $attributes, $fieldname, $value, $record_id )
{
if ( empty( $record_id ) ) // make sure we have the record ID
{
$record_id = filter_input( INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT );
}
return \PDb_fields\exclusive_options::filter_handler( $attributes, $fieldname, $value, $record_id );
}
}
new pdb_admin_exclusive_options();
@xnau
Copy link
Author

xnau commented Mar 24, 2024

This code is installed as a plugin How to Install a WordPress Plugin from a Gist

@xnau
Copy link
Author

xnau commented Apr 1, 2024

Version 1.1 fixes a bug where the previously selected value would be deselected when the record was updated.

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