Last active
May 20, 2021 17:47
-
-
Save xnau/fb961393fc675cf802a509a2a1c888d1 to your computer and use it in GitHub Desktop.
Demonstrates how to show a custom summary value for a Participants Database Participant Log
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 Custom Summary | |
* Description: provides a customized sum value for the "Work Log" log | |
* Version: 1.0 | |
* | |
*/ | |
class pdb_work_log_summary { | |
/** | |
* @var string name of the log | |
*/ | |
private $log_name = 'work_log'; | |
/** | |
* @var string name of the project we want to get the total hours for | |
*/ | |
private $project_name = 'Mailer Assembly'; | |
/** | |
* sets up the filter handler | |
*/ | |
public function __construct() | |
{ | |
add_filter( 'pdblog-' . $this->log_name . '_sum', array( $this, 'sum_value' ), 10, 2 ); | |
} | |
/** | |
* provides the custom sum value | |
* | |
* @param int $sum | |
* @param array $log_data | |
* @return int sum | |
*/ | |
public function sum_value( $sum, $log_data ) | |
{ | |
return $this->calc_sum( $log_data ); | |
} | |
/** | |
* calculates the sum | |
* | |
* @param array $log_data | |
* @return int sum value | |
*/ | |
private function calc_sum( $log_data ) | |
{ | |
/* | |
* the $log_data argument will come in as an array of std objects, | |
* each object is a log entry | |
* | |
* for your application, you would calculate your sum based on the values in | |
* this array | |
* | |
* In this example, we will iterate through the array to build our sum, which | |
* is all the hours logged while working on the "Mailer Assembly" project | |
*/ | |
$sum = 0; | |
// check each entry, if they logged time on the mailer project, add the hours | |
foreach ( $log_data as $entry ) | |
{ | |
if ( $entry->project === $this->project_name ) { | |
$sum += $entry->hours; | |
} | |
} | |
return $sum; | |
} | |
} | |
new pdb_work_log_summary(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment