gpls_before_query

  1. Description
  2. Usage
  3. Parameters
  4. Since
  5. Examples
    1. Set Limit Period by User Meta
    2. Scope Feeds to the Current Nested Forms Parent Entry

Description

Do something (like modify the GPLS_RuleTest object) before the query is executed.

Usage

add_action( 'gpls_before_query', 'my_custom_function' );

Parameters

  • gpls_rule_test \GPLS_RuleTest

    The current instance of the GPLS_RuleTest object.

Since

This action is available since Gravity Forms Limit Submissions 1.0-beta-1.20.

Examples

Set Limit Period by User Meta

<?php
/**
 * Gravity Perks // Limit Submissions // Set Limit Period by User Meta
 * https://gravitywiz.com/documentation/gravity-forms-limit-submissions/
 */
add_action( 'gpls_before_query', function( $ruletest ) {
	global $wpdb;

	// Update "123" to your form ID.
	if ( $ruletest->form_id != 123 ) {
		return;
	}

	// Update "subscription_start_date" to your user meta key.
	$subscription_start_date = get_user_meta( get_current_user_id(), 'subscription_start_date', true );
	$time_period_sql         = $wpdb->prepare( 'date_created BETWEEN %s AND DATE_ADD( CURDATE(), INTERVAL 1 DAY )', $subscription_start_date );
	$ruletest->where[]       = $time_period_sql;

} );

Scope Feeds to the Current Nested Forms Parent Entry

Configure a Limit Submissions feed for a child form and then scope that feed to only apply to child entries submitted for the same parent entry.

For example, if we create a Limit Submissions feed that limits by the field value of a Name field, “David Smiff” could not be specified in this field for two child entries of the same parent entry. However, if I submitted a second parent entry, I could enter “David Smiff” as a child entry again.

<?php
/**
 * Gravity Perks // Limit Submissions // Scope Feeds to the Current Nested Forms Parent Entry.
 * https://gravitywiz.com/documentation/gravity-forms-limit-submissions/
 *
 * Set GP Limit Submissions feed to only apply to child entries submitted for the same parent entry.
 */
add_action( 'gpls_before_query', function( $ruletest ) {
	global $wpdb;

	$parent_form_id = rgpost( 'gpnf_parent_form_id' );

	// Update "123" to your child form ID.
	if ( $ruletest->form_id == 123 && class_exists( 'GPNF_Session' ) && $parent_form_id ) {
		$gpnf_session      = new GPNF_Session( $parent_form_id );
		$ruletest->join[]  = "INNER JOIN {$wpdb->prefix}gf_entry_meta em_gpnf ON em_gpnf.entry_id = e.id";
		$ruletest->where[] = sprintf( "\n( em_gpnf.meta_key = '%s' AND em_gpnf.meta_value = '%s' )", GPNF_Entry::ENTRY_PARENT_KEY, $gpnf_session->get( 'hash' ) );
	}

} );