Skip to content

Instantly share code, notes, and snippets.

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 zackkatz/8f38d732ed27f00444b1b6416268cdea to your computer and use it in GitHub Desktop.
Save zackkatz/8f38d732ed27f00444b1b6416268cdea to your computer and use it in GitHub Desktop.
GravityView - Filter Entry Notes by Gravity Flow Assignee.
<?php
/**
* GravityView - Filter Entry Notes by Gravity Flow Assignee.
* @license https://opensource.org/license/gpl-2-0/ GPL-2.0
*/
apply_filters( 'gravityview/entry_notes/get_notes', 'filter_gravityview_notes_by_gravity_flow_assignee', 10, 2 );
/**
* Filter the notes by Gravity Flow assignee.
*
* @param stdClass[]|null $notes Notes as returned by {@see GFFormsModel::get_lead_notes}
* @param int $entry_id The entry ID.
*
* @return array
*/
function filter_gravityview_notes_by_gravity_flow_assignee( $notes = array(), $entry_id = 0 ) {
if ( ! class_exists( 'Gravity_Flow_API' ) ) {
return $notes;
}
if ( empty( $notes ) ) {
return $notes;
}
$entry = GFAPI::get_entry( $entry_id );
if ( is_wp_error( $entry ) ) {
return $notes;
}
$api = new Gravity_Flow_API( $entry['form_id'] );
$step = $api->get_current_step( $entry );
if ( false === $step ) {
return $notes;
}
$assignees = $step->get_assignees();
if ( empty( $assignees ) ) {
return array();
}
$assignee_user_ids = array();
foreach ( $assignees as $assignee ) {
/** @var \WP_User $assignee_user */
$assignee_user = $assignee->get_user();
if ( ! $assignee_user ) {
continue;
}
$assignee_user_ids[] = $assignee_user->ID;
}
$notes = array_filter( $notes, function ( $note ) use ( $assignee_user_ids ) {
if ( empty( $note->user_id ) ) {
return false;
}
return in_array( $note->user_id, $assignee_user_ids, true );
} );
return $notes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment