Last active
November 16, 2021 13:51
-
-
Save Sean12mps/8e47d9778defc2d60b2dcfadd2a16f9a to your computer and use it in GitHub Desktop.
Restrict entry for GF forms per user. 1 form, 1 entry per user.
This file contains hidden or 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 | |
| /** | |
| * Gravity Forms functions | |
| * | |
| * @package @WordPress | |
| */ | |
| /** | |
| * Get list of form IDs to restrict. | |
| * | |
| * @return array | |
| **/ | |
| function dg_gform_get_entry_restricted_forms() { | |
| $restricted_forms = get_field( 'gf_entry_limited_forms', 'options' ); | |
| $_restricted_forms = array(); | |
| if ( $restricted_forms ) { | |
| foreach ( $restricted_forms as $restricted_form ) { | |
| $_restricted_forms[ $restricted_form['form_id'] ] = $restricted_form['text']; | |
| } | |
| } | |
| return $_restricted_forms; | |
| } | |
| /** | |
| * Check if current user have an entry | |
| * | |
| * @param int $form_id GF Form ID. | |
| * @param int $user_id User ID. | |
| * @return bool | |
| **/ | |
| function dg_gform_user_has_form_entry( $form_id, $user_id ) { | |
| $search_args = array( | |
| 'field_filters' => array( | |
| array( | |
| 'key' => 'created_by', | |
| 'value' => $user_id, | |
| ), | |
| ), | |
| ); | |
| return GFAPI::get_entries( $form_id, $search_args ) ? true : false; | |
| } | |
| /** | |
| * Check if current user have an entry | |
| * | |
| * @param string $form_html HTML String of the Form. | |
| * @param array $form Form array args. | |
| * @return mixed Array if has no entry, string if does. | |
| **/ | |
| function dg_gform_get_form_filter( $form_html, $form ) { | |
| $restricted_forms = dg_gform_get_entry_restricted_forms(); | |
| $current_form_id = $form['id']; | |
| $current_user_id = get_current_user_id(); | |
| if ( | |
| isset( $restricted_forms[ $current_form_id ] ) && | |
| dg_gform_user_has_form_entry( $current_form_id, $current_user_id ) | |
| ) { | |
| ob_start(); | |
| ?> | |
| <div class="gf-limit-message"> | |
| <?php echo esc_html( $restricted_forms[ $current_form_id ] ); ?> | |
| </div> | |
| <?php | |
| $form_html = ob_get_clean(); | |
| } | |
| return $form_html; | |
| } | |
| add_filter( 'gform_get_form_filter', 'dg_gform_get_form_filter', 10, 2 ); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This gist was meant to be used alongside ACF repeater, so that this function returns form ids and limit notices.