spivurno
8/16/2016 - 3:16 AM

Gravity Wiz // Gravity Forms // User Registration // Ignore Update Feed for Non-logged-in Users

Gravity Wiz // Gravity Forms // User Registration // Ignore Update Feed for Non-logged-in Users

<?php
/**
 * Gravity Wiz // Gravity Forms // User Registration // Ignore Update Feed for Non-logged-in Users
 *
 * Ignore update feeds for users who are not logged in.
 *
 * @version   0.1
 * @author    David Smith <david@gravitywiz.com>
 * @license   GPL-2.0+
 * @link      http://gravitywiz.com/
 *
 * Plugin Name: Gravity Forms User Registration - Ignore Update Feeds for Non-logged-in Users
 * Plugin URI: http://gravitywiz.com/
 * Description: Ignore update feeds for users who are not logged in.
 * Author: Gravity Wiz
 * Version: 0.1
 * Author URI: http://gravitywiz.com
 */
class GW_Ignore_Update_Feeds {

	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(
			'form_ids'         => array(),
			'exclude_form_ids' => array()
		) );

		// 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() {

		// make sure we're running the required minimum version of Gravity Forms
		if( ! property_exists( 'GFCommon', 'version' ) || ! version_compare( GFCommon::$version, '1.8', '>=' ) ) {
			return;
		}

		if( is_user_logged_in() ) {
			return;
		}

		add_filter( 'gform_get_form_filter', array( $this, 'remove_hide_form_function' ), 10, 2 );
		add_action( 'gform_pre_process', array( $this, 'skip_validation' ) );
		add_filter( 'gform_gravityformsuserregistration_pre_process_feeds', array( $this, 'filter_update_feeds' ), 10, 3 );

	}

	public function remove_hide_form_function( $string, $form ) {

		if( $this->is_applicable_form( $form ) ) {
			remove_action( 'gform_get_form_filter_' . $form['id'], array( gf_user_registration(), 'hide_form' ) );
		}

		return $string;
	}

	public function skip_validation( $form ) {

		if( ! is_callable( 'gf_user_registration' ) || ! is_callable( array( gf_user_registration(), 'get_single_submission_feed' ) ) ) {
			return;
		}

		$feed = gf_user_registration()->get_single_submission_feed( GFFormsModel::get_current_lead(), $form );

		// let's just be safe and reset current lead to false so it doesn't disrupt the normal flow of things...
		GFFormsModel::set_current_lead( false );

		if ( $this->is_update_feed( $feed ) ) {
			remove_filter( 'gform_validation', array( gf_user_registration(), 'validate' ) );
		}

	}

	public function is_update_feed( $feed ) {
		return $feed && rgars( $feed, 'meta/feedType' ) == 'update';
	}

	public function filter_update_feeds( $feeds, $entry, $form ) {

		if( ! $this->is_applicable_form( $form ) ) {
			return $feeds;
		}

		$filtered = array();
		foreach( $feeds as $feed ) {
			if( ! $this->is_update_feed( $feed ) ) {
				$filtered[] = $feed;
			}
		}

		return $filtered;
	}

	public function is_applicable_form( $form ) {

		$form_id = isset( $form['id'] ) ? $form['id'] : $form;

		if( ! empty( $this->_args['exclude_form_ids'] ) ) {
			return ! in_array( $form_id, $this->_args['exclude_form_ids'] );
		} else if( ! empty( $this->_args['form_ids'] ) ) {
			return in_array( $form_id, $this->_args['form_ids'] );
		}

		return true;
	}

}

# Configuration

new GW_Ignore_Update_Feeds( array(
	'exclude_form_ids' => array( '1426' ),
) );