Last active
September 10, 2020 01:41
-
-
Save xnau/ffef0599bbb769fe95be3ca813062aca to your computer and use it in GitHub Desktop.
Demonstrates how to set up a simple "with selected" action in the Participants Database admin record list.
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 Set Paid Status Tutorial | |
* Version: 0.1 | |
* Description: Demonstrates how to set up a simple "with selected" action in the Participants Database admin record list. | |
* Author: Roland Barker, xnau webdesign https://xnau.com | |
* | |
*/ | |
/** | |
* defines our action and adds it to the dropdown | |
*/ | |
class PDb_Add_Payment_Status_Action { | |
/** | |
* @var string name of the action | |
* | |
* this is used as the "key" to identify the action | |
*/ | |
private $action = 'set_paid_status'; | |
/** | |
* sets up the filters and action handler | |
*/ | |
public function __construct() | |
{ | |
add_filter( 'pdb-admin_list_with_selected_actions', array( $this, 'add_dropdown_actions' ) ); | |
add_filter( 'pdb-admin_list_with_selected_action_conf_messages', array( $this, 'set_confirmation_message' ) ); | |
add_action( 'pdb_admin_list_with_selected/' . $this->action, array( $this, 'do_operation' ) ); | |
} | |
/** | |
* adds the action to the dropdown | |
* | |
* @param array $action_list list of the defined actions | |
* @return array | |
*/ | |
public function add_dropdown_actions( $action_list ) | |
{ | |
// add the action with it's visible title as the key | |
$action_list[ __( 'Mark as Paid' ) ] = $this->action; | |
return $action_list; | |
} | |
/** | |
* defines the confirmation message | |
* | |
* @param array $message_list list of confirmation messages | |
* @return array | |
*/ | |
public function set_confirmation_message( $message_list ) | |
{ | |
// this message is going to accept a yes/no answer | |
$message_list[ $this->action ] = array( | |
"singular" => __( "Mark the selected record as paid?" ), | |
"plural" => __( "Mark the selected records as paid?" ), | |
); | |
return $message_list; | |
} | |
/** | |
* performs the operation on the selected records | |
* | |
* @param array $id_list list of all the selected record ids | |
* @global wpdb $wpdb | |
*/ | |
public function do_operation( $id_list ) | |
{ | |
global $wpdb; | |
foreach ( $id_list as $id ) { | |
/* | |
* this is where we change the value in the record | |
*/ | |
$wpdb->update( Participants_Db::$participants_table, array( 'payment_status' => 'paid' ), array( 'id' => $id ) ); | |
// note: this is inefficient for a very large list of ids to process | |
} | |
} | |
} | |
// instantiate our plugin class here | |
new PDb_Add_Payment_Status_Action(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment