Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Gravity Perks // GP Populate Anything // Tiny Mailing List
<?php
/**
* Gravity Perks // GP Populate Anything // Tiny Mailing List
* http://gravitywiz.com/documentation/gravity-forms-populate-anything/
*
* Send an email all users who have submitted Form A when Form B is submitted. This *should not* be used
* to large numbers of notifications.
*
* # Instructions
*
* These instructions assume that Form A contains an Email field.
*
* 1. Add a Hidden field to Form B.
* 2. Enable "Populate value dynamically" setting and configure settings (https://gwiz.io/2LgCdZS):
* - Type: Gravity Forms Entry
* - Form: Select Form A.
* - Value Templates / Value: Select Email field.
* 3. Create a new notification.
* 4. Set the Sent To / Send to Email value as the merge tag for the Hidden field we added in Step 1 (https://gwiz.io/2CfXccB).
*
* That's it. Populate Anything combined with this snippet will populate a comma-delimited list of emails in to the Hidden
* field. This snippet will parse the comma-delimited list and send a separate copy of the notification to each email.
*
* @version 0.2
* @author Gravity Wiz <support@gravitywiz.com>
* @license GPL-2.0+
* @link http://gravitywiz.com/
*
* Plugin Name: GPPA Tiny Mailing List
* Plugin URI: http://gravitywiz.com/documentation/gravity-forms-populate-anything/
* Description: Send an email all users who have submitted Form A when Form B is submitted.
* Author: Gravity Wiz
* Version: 0.2
* Author URI: http://gravitywiz.com
*/
class GPPA_Tiny_Mailing_List {
public function __construct( $args = array() ) {
// set our default arguments, parse against the provided arguments, and store for use throughout the class
$this->_args = wp_parse_args( $args, array(
'target_form_id' => false,
'target_field_id' => false,
'source_field_id' => false,
) );
// do version check in the init to make sure if GF is going to be loaded, it is already loaded
add_action( 'init', array( $this, 'init' ) );
}
public function init() {
add_filter( 'gppa_process_template', array( $this, 'filter_emails_template' ), 10, 6 );
add_filter( 'gform_notification', array( $this, 'send_individual_notifications' ), 10, 3 );
}
public function filter_emails_template( $value, $field, $template, $object, $populate, $objects ) {
if( ! is_array( $field->choices ) && $field->formId == $this->_args['target_form_id'] && $field->id == $this->_args['target_field_id'] ) {
$value = implode( ',', wp_list_pluck( $objects, $this->_args['source_field_id'] ) );
}
return $value;
}
public function send_individual_notifications( $notification, $form, $entry ) {
global $_gppa_processing_notifications;
if( $_gppa_processing_notifications ) {
return $notification;
}
if( ! $this->is_applicable_form( $form ) ) {
return $notification;
}
$tos = explode( ',', GFCommon::replace_variables( $notification['to'], $form, $entry, false, false, false, 'text' ) );
if( count( $tos ) <= 1 ) {
return $notification;
}
$_gppa_processing_notifications = true;
foreach( $tos as $to ) {
$notification['to'] = $to;
GFCommon::send_notification( $notification, $form, $entry );
}
$_gppa_processing_notifications = false;
$notification['to'] = false;
return $notification;
}
public function is_applicable_form( $form ) {
$form_id = isset( $form['id'] ) ? $form['id'] : $form;
return empty( $this->_args['target_form_id'] ) || $form_id == $this->_args['target_form_id'];
}
}
# Configuration
new GPPA_Tiny_Mailing_List( array(
'target_form_id' => 177,
'target_field_id' => 3,
'source_field_id' => 1,
) );
@phillipwilhelm

This comment has been minimized.

Copy link

phillipwilhelm commented May 2, 2019

Fatal error: Cannot use object of type GPPA_Object_Type_GF_Entry as array in /public_html/wp-includes/class-wp-list-util.php on line 152

@phillipwilhelm

This comment has been minimized.

Copy link

phillipwilhelm commented May 3, 2019

This isn't working when trying to populate values into a text field.

I have two entries on FORM A and only one email value shows in the on FORM B. I've update the function to match appropiate field IDs and added it to the site.

Additionally only one email is also sent even when I add the right merge tag in FORM B's Notification.

Using GF: 2.4.8.13 and Gravity Populate Anything: 1.0-beta-2.46

Any ideas?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.