Created
May 15, 2023 19:01
-
-
Save RadGH/7cf0f94efc03ce22159f40ced1e4a65d to your computer and use it in GitHub Desktop.
GF Entries By User
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 | |
/** | |
* Gets gravity form entries for the given user. This includes entries where the user was logged in when they submitted the entry, and entries | |
* that were used to create their account with the GF User Registration Addon. | |
* | |
* @param $user_id | |
* | |
* @return array | |
*/ | |
function gfue_get_entries_by_user( $user_id ) { | |
global $wpdb; | |
$entries = array(); | |
$where_extra = "and entry.status = 'active'"; | |
$where_extra = apply_filters( 'gfue/get_entries_by_user/where', $where_extra, $user_id ); | |
// 1. Get entries submitted when the user was logged in, by looking at the "created_by" column. Similar to post_author in normal post types. | |
$sql = $wpdb->prepare(" | |
SELECT entry.id | |
FROM {$wpdb->prefix}gf_entry entry | |
WHERE entry.created_by = %d | |
$where_extra | |
LIMIT 2000;", | |
$user_id | |
); | |
$sql = apply_filters( 'gfue/get_entries_by_user/sql_author', $sql ); | |
if ( $sql ) { | |
$e = $wpdb->get_col( $sql ); | |
if ( $e ) $entries = array_merge( $entries, $e ); | |
} | |
// 2. Get entries that created the user's account via User Registration Addon. | |
$sql = $wpdb->prepare(" | |
SELECT entry.id | |
FROM {$wpdb->prefix}gf_entry entry | |
LEFT JOIN {$wpdb->prefix}usermeta registration_entry | |
ON registration_entry.meta_key = '_gform-entry-id' AND registration_entry.meta_value = entry.id | |
WHERE registration_entry.user_id = %d | |
$where_extra | |
LIMIT 2000;", | |
$user_id | |
); | |
$sql = apply_filters( 'gfue/get_entries_by_user/sql_user_registration', $sql ); | |
if ( $sql ) { | |
$e = $wpdb->get_col( $sql ); | |
if ( $e ) $entries = array_merge( $entries, $e ); | |
} | |
// 3. Get entries based on gf_entry_meta where meta_key = 44 and meta_value matches the current user's email address. | |
$current_user_email = wp_get_current_user()->user_email; | |
$sql = $wpdb->prepare(" | |
SELECT entry.id | |
FROM {$wpdb->prefix}gf_entry entry | |
LEFT JOIN {$wpdb->prefix}gf_entry_meta entry_meta | |
ON entry_meta.entry_id = entry.id | |
WHERE entry_meta.meta_key = 44 AND entry_meta.meta_value = %s | |
$where_extra | |
LIMIT 2000;", | |
$current_user_email | |
); | |
$sql = apply_filters( 'gfue/get_entries_by_user/sql_user_email', $sql ); | |
if ( $sql ) { | |
$e = $wpdb->get_col( $sql ); | |
if ( $e ) $entries = array_merge( $entries, $e ); | |
} | |
$entries = apply_filters( 'gfue_get_entries_by_user/result', $entries, $user_id ); | |
// Now get all of those entry IDs sorted by date. | |
if ( $entries && $int_ids = array_map('intval', $entries) ) { | |
$sql = "SELECT entry.id FROM {$wpdb->prefix}gf_entry entry WHERE entry.id IN (". esc_sql(implode(',', $int_ids)) .") ORDER BY entry.date_created DESC LIMIT 2000;"; | |
$entries = $wpdb->get_col( $sql ); | |
} | |
return $entries; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment