Gf_Tokens::gf_token( $value,  $pieces,  $recipe_id,  $trigger_data,  $user_id,  $replace_args )


Parameters Parameters

$value

(Required)

$pieces

(Required)

$recipe_id

(Required)

$trigger_data

(Required)

$user_id

(Required)


Top ↑

Return Return

(string|null)


Source Source

File: src/integrations/gravity-forms/tokens/gf-tokens.php

	public function gf_token( $value, $pieces, $recipe_id, $trigger_data, $user_id, $replace_args ) {
		if ( $pieces ) {
			if ( in_array( 'GFFORMS', $pieces ) || in_array( 'ANONGFFORMS', $pieces ) ) {
				global $wpdb;
				$token_info = explode( '|', $pieces[2] );
				$form_id    = $token_info[0];
				$meta_key   = $token_info[1];

				if ( method_exists( 'RGFormsModel', 'get_entry_table_name' ) ) {
					$table_name = RGFormsModel::get_entry_table_name();
				} else {
					$table_name = RGFormsModel::get_lead_table_name();
				}
				$where_user_id = 0 === absint( $user_id ) ? 'created_by IS NULL' : 'created_by=' . $user_id;
				$qq            = $wpdb->prepare( "SELECT id FROM {$table_name} WHERE $where_user_id AND form_id = %d ORDER BY date_created DESC LIMIT 0,1", $form_id );
				$lead_id       = (int) $wpdb->get_var( $qq );
				if ( $lead_id ) {

					if ( method_exists( 'RGFormsModel', 'get_entry_meta_table_name' ) ) {
						$table_name = RGFormsModel::get_entry_meta_table_name();
					} else {
						$table_name = RGFormsModel::get_lead_meta_table_name();
					}

					$value = $wpdb->get_var( $wpdb->prepare( "SELECT meta_value FROM {$table_name} WHERE form_id = %d AND entry_id = %d AND meta_key LIKE %s", $form_id, $lead_id, $meta_key ) );
				} else {
					if ( 0 !== (int) $user_id && is_user_logged_in() ) {
						//fallback.. ... attempt to find them by email??
						if ( method_exists( 'RGFormsModel', 'get_entry_meta_table_name' ) ) {
							$table_name = RGFormsModel::get_entry_meta_table_name();
						} else {
							$table_name = RGFormsModel::get_lead_meta_table_name();
						}
						$where_user_email = get_user_by( 'ID', $user_id )->user_email;
						$aa               = $wpdb->prepare( "SELECT entry_id FROM {$table_name} WHERE meta_value LIKE '$where_user_email' AND form_id = %d ORDER BY entry_id DESC LIMIT 0,1", $form_id );
						$lead_id          = $wpdb->get_var( $aa );
						if ( $lead_id ) {
							if ( method_exists( 'RGFormsModel', 'get_entry_meta_table_name' ) ) {
								$table_name = RGFormsModel::get_entry_meta_table_name();
							} else {
								$table_name = RGFormsModel::get_lead_meta_table_name();
							}

							$value = $wpdb->get_var( $wpdb->prepare( "SELECT meta_value FROM {$table_name} WHERE form_id = %d AND entry_id = %d AND meta_key LIKE %s", $form_id, $lead_id, $meta_key ) );
						} else {
							// Try again for anonymous user when its using a different email address
							if ( method_exists( 'RGFormsModel', 'get_entry_table_name' ) ) {
								$table_name = RGFormsModel::get_entry_table_name();
							} else {
								$table_name = RGFormsModel::get_lead_table_name();
							}
							$where_user_id = 'created_by IS NULL';
							$qq            = $wpdb->prepare( "SELECT id FROM {$table_name} WHERE $where_user_id AND form_id = %d ORDER BY date_created DESC LIMIT 0,1", $form_id );
							$lead_id       = (int) $wpdb->get_var( $qq );
							if ( $lead_id ) {
								if ( method_exists( 'RGFormsModel', 'get_entry_meta_table_name' ) ) {
									$table_name = RGFormsModel::get_entry_meta_table_name();
								} else {
									$table_name = RGFormsModel::get_lead_meta_table_name();
								}

								$value = $wpdb->get_var( $wpdb->prepare( "SELECT meta_value FROM {$table_name} WHERE form_id = %d AND entry_id = %d AND meta_key LIKE %s", $form_id, $lead_id, $meta_key ) );
							}
						}
					} elseif ( 0 !== (int) $user_id && ! is_user_logged_in() ) {
						// Try again for anonymous user when its using a different email address
						if ( method_exists( 'RGFormsModel', 'get_entry_table_name' ) ) {
							$table_name = RGFormsModel::get_entry_table_name();
						} else {
							$table_name = RGFormsModel::get_lead_table_name();
						}
						$where_user_id = 'created_by IS NULL';
						$qq            = $wpdb->prepare( "SELECT id FROM {$table_name} WHERE $where_user_id AND form_id = %d ORDER BY date_created DESC LIMIT 0,1", $form_id );
						$lead_id       = (int) $wpdb->get_var( $qq );
						if ( $lead_id ) {
							if ( method_exists( 'RGFormsModel', 'get_entry_meta_table_name' ) ) {
								$table_name = RGFormsModel::get_entry_meta_table_name();
							} else {
								$table_name = RGFormsModel::get_lead_meta_table_name();
							}

							$value = $wpdb->get_var( $wpdb->prepare( "SELECT meta_value FROM {$table_name} WHERE form_id = %d AND entry_id = %d AND meta_key LIKE %s", $form_id, $lead_id, $meta_key ) );
						}
					} else {
						$value = '';
					}
				}
			}
		}

		return $value;
	}