spivurno
1/31/2015 - 3:45 AM

Gravity Wiz // Gravity Forms User Registration // Conditionally Update the User Role on Submission

Gravity Wiz // Gravity Forms User Registration // Conditionally Update the User Role on Submission

<?php
/**
 * Gravity Wiz // Gravity Forms User Registration // Conditionally Update the User Role on Submission
 *
 * @version	  1.2
 * @author    David Smith <david@gravitywiz.com>
 * @license   GPL-2.0+
 * @link      http://gravitywiz.com/...
 */
class GW_GFUR_Conditional_User_Role {

	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_id' => false,
			'roles'   => 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;
		}

		// carry on

		add_action( 'gform_user_updated',    array( $this, 'maybe_update_user_role' ), 11, 3 );
		add_action( 'gform_user_registered', array( $this, 'maybe_set_user_role' ), 11, 3 );

	}

	public function maybe_set_user_role( $user_id, $feed, $entry ) {

		$this->maybe_update_user_role( $entry, GFAPI::get_form( $entry['form_id'] ), $user_id );

	}

	public function maybe_update_user_role( $user_id, $feed, $entry ) {

		if( ! $user_id ) {
			$user_id = $entry['created_by'];
		}

		if( ! $user_id ) {
			return;
		}

		$form = GFAPI::get_form( $entry['form_id'] );

		foreach( $this->_args['roles'] as $role_obj ) {

			$role_obj = $this->set_conditional_logic_defaults( $role_obj );

			if( $this->is_matching_role( $role_obj, $form, $entry ) ) {
				$user = new WP_User( $user_id );
				$user->set_role( $role_obj['role'] );
				break;
			}

		}

	}

	/**
	 * Populates default values into provided conditional logic object.
	 *
	 * A complete conditional logic object will have the following properties:
	 *
	 * 'conditionalLogic' => array(
	 *     'actionType' => 'show',
	 *     'logicType' => 'any',
	 *     'rules' => array(
	 *         array(
	 *             'fieldId' => 3,
	 *             'operator' => 'is',
	 *             'value' => 'Administrator'
	 *         )
	 *     )
	 * )
	 *
	 * @param $logic
	 */
	public function set_conditional_logic_defaults( $obj ) {

		$logic = $obj['conditionalLogic'];

		$logic = wp_parse_args( $logic, array(
			'actionType' => 'show',
			'logicType'  => 'all'
		) );

		foreach( $logic['rules'] as &$rule ) {
			$rule = wp_parse_args( $rule, array(
				'operator' => 'is'
			) );
		}

		$obj['conditionalLogic'] = $logic;

		return $obj;
	}

	public function is_matching_role( $role_obj, $form, $entry ) {
		return $this->get_field_display( $form, (object) $role_obj, array(), $entry ) == 'show';
	}

	/**
	 * Public port of private GFFormsModel::get_field_display() method.
	 *
	 * @param      $form
	 * @param      $field
	 * @param      $field_values
	 * @param null $lead
	 *
	 * @return string
	 */
	public static function get_field_display( $form, $field, $field_values, $lead = null ) {

		if ( empty( $field ) ) {
			return 'show';
		}

		$logic = $field->conditionalLogic;

		//if this field does not have any conditional logic associated with it, it won't be hidden
		if ( empty( $logic ) ) {
			return 'show';
		}


		$match_count = 0;
		foreach ( $logic['rules'] as $rule ) {
			$source_field = RGFormsModel::get_field( $form, $rule['fieldId'] );
			$field_value  = empty( $lead ) ? RGFormsModel::get_field_value( $source_field, $field_values ) : RGFormsModel::get_lead_field_value( $lead, $source_field );

			$is_value_match = RGFormsModel::is_value_match( $field_value, $rule['value'], $rule['operator'], $source_field, $rule, $form );

			if ( $is_value_match ) {
				$match_count ++;
			}
		}

		$do_action = ( $logic['logicType'] == 'all' && $match_count == sizeof( $logic['rules'] ) ) || ( $logic['logicType'] == 'any' && $match_count > 0 );
		$is_hidden = ( $do_action && $logic['actionType'] == 'hide' ) || ( ! $do_action && $logic['actionType'] == 'show' );

		return $is_hidden ? 'hide' : 'show';
	}

}

# Configuration

new GW_GFUR_Conditional_User_Role( array(
    'form_id' => 349,
    'roles'   => array(
        array(
            'role' => 'administrator',
            'conditionalLogic' => array(
                'rules' => array(
                    array(
                        'fieldId'  => 11,
                        'value'    => 'Administrator'
                    )
                )
            )
        ),
        array(
            'role' => 'contributor',
            'conditionalLogic' => array(
                'rules' => array(
                    array(
                        'fieldId'  => 11,
                        'value'    => 'Contributor'
                    )
                )
            )
        )
    )
) );