Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save saifsultanc/4c6e3dc6f2d304deecf5e3bea0f71e85 to your computer and use it in GitHub Desktop.
Save saifsultanc/4c6e3dc6f2d304deecf5e3bea0f71e85 to your computer and use it in GitHub Desktop.
gwiz_entry_count_shortcode_excluding_unsubscribed.php
<?php
add_filter( 'gform_shortcode_entry_count', 'gwiz_entry_count_shortcode', 10, 2 );
function gwiz_entry_count_shortcode( $output, $atts ) {
$email_field_id = 1; // Replace with your the email field ID.
// phpcs:ignore WordPress.PHP.DontExtract.extract_extract
extract( shortcode_atts( array(
'id' => false,
'status' => 'total', // accepts 'total', 'unread', 'starred', 'trash', 'spam'
'format' => false, // should be 'comma', 'decimal'
), $atts ) );
$valid_statuses = array( 'total', 'unread', 'starred', 'trash', 'spam' );
if ( ! $id || ! in_array( $status, $valid_statuses ) ) {
return current_user_can( 'update_core' ) ? __( 'Invalid "id" (the form ID) or "status" (i.e. "total", "trash", etc.) parameter passed.' ) : '';
}
$counts = GFFormsModel::get_form_counts( $id );
$output = rgar( $counts, $status );
if ( $format ) {
$format = $format == 'decimal' ? '.' : ',';
$output = number_format( $output, 0, false, $format );
}
$entries = GFAPI::get_entries( $id);
$unsubscribed = 0;
global $wpdb;
foreach ( $entries as $entry ) {
$count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->gpns_unsubscribes}
WHERE `email` = %s
AND (
`scope` = 'all'
OR (`scope` = 'form_id' AND form_id = %d)
OR (`scope` = 'nid' AND form_id = %d )
)", $entry[ $email_field_id ], $id, $id ) );
if ( $count > 0 ) {
$unsubscribed++;
}
}
return $output - $unsubscribed;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment