Wpff_Tokens::wpff_possible_tokens( array $tokens = array(), array $args = array() )


Parameters Parameters

$tokens

(array) (Optional)

Default value: array()

$args

(array) (Optional)

Default value: array()


Top ↑

Return Return

(array)


Source Source

File: src/integrations/wp-fluent-forms/tokens/wpff-tokens.php

	function wpff_possible_tokens( $tokens = array(), $args = array() ) {

		$form_id      = $args['value'];
		$trigger_meta = $args['meta'];

		$forms = array();
		global $wpdb;
		$fluent_active = true;
		if ( $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}fluentform_forms'" ) !== "{$wpdb->prefix}fluentform_forms" ) {
			$fluent_active = false;
		}
		if ( true === $fluent_active && ! empty( $form_id ) && 0 !== $form_id && is_numeric( $form_id ) ) {
			$form = wpFluent()->table( 'fluentform_forms' )->where( 'id', '=', $form_id )
			                  ->select( [ 'id', 'title', 'form_fields' ] )
			                  ->orderBy( 'id', 'DESC' )
			                  ->get();
			if ( $form ) {
				$form               = array_pop( $form );
				$forms[ $form->id ] = json_decode( $form->form_fields, true );
			}
		}

		if ( ! empty( $forms ) ) {
			foreach ( $forms as $id => $meta ) {
				$fields_tokens = array();
				$raw_fields    = isset( $meta['fields'] ) ? $meta['fields'] : array();
				if ( is_array( $meta ) && ! empty( $raw_fields ) ) {
					foreach ( $raw_fields as $raw_field ) {

						if ( isset( $raw_field['columns'] ) ) {
							// Fields are in a column
							foreach ( $raw_field['columns'] as $columns ) {
								foreach ( $columns as $fields_or_multi_inputs ) {

									foreach ( $fields_or_multi_inputs as $field_or_multi_input ) {

										// Skip html only feilds that are not actual form inputs
										if ( isset( $fields_or_multi_inputs['element'] ) && 'custom_html' === $fields_or_multi_inputs['element'] ) {
											continue;
										}

										// Skip file upload fields. Not supported
										if ( isset( $fields_or_multi_inputs['element'] ) && 'input_file' === $fields_or_multi_inputs['element'] ) {
											continue;
										}

										if ( isset( $field_or_multi_input['fields'] ) ) {
											// Multiple grouped fields in a column
											$field_group_name = $field_or_multi_input['attributes']['name'];

											$multi_input = $field_or_multi_input['fields'];


											foreach ( $multi_input as $field ) {

												// Skip html only feilds that are not actual form inputs
												if ( isset( $field['element'] ) && 'custom_html' === $field['element'] ) {
													continue;
												}

												// Skip file upload fields. Not supported
												if ( isset( $field['element'] ) && 'input_file' === $field['element'] ) {
													continue;
												}

												// is the field visible
												if ( false === $field['settings']['visible'] ) {
													continue;
												}

												$fields_tokens[] = $this->create_token( $form_id, $field, $trigger_meta, $field_group_name );
											}

										} else {

											// Multiple fields are in a column and are NOT grouped

											$field = $field_or_multi_input;

											// Skip html only feilds that are not actual form inputs
											if ( isset( $field['element'] ) && 'custom_html' === $field['element'] ) {
												continue;
											}

											// Skip file upload fields. Not supported
											if ( isset( $field['element'] ) && 'input_file' === $field['element'] ) {
												continue;
											}

											// Single field in column
											$fields_tokens[] = $this->create_token( $form_id, $field, $trigger_meta );
										}
									}
								}
							}
						} elseif ( isset( $raw_field['fields'] ) ) {
							$field_group_name = $raw_field['attributes']['name'];
							foreach ( $raw_field['fields'] as $field ) {
								if ( 1 === (int) $field['settings']['visible'] ) {
									if ( isset( $field['uniqElKey'] ) ) {
										$input_id    = $field['uniqElKey'];
										$input_title = $field['settings']['label'];
										$token_id    = "$form_id|$input_id";

										$type = 'text';
										if ( isset( $f_fields['attributes']['type'] ) ) {
											if ( 'number' === $field['attributes']['type'] ) {
												$type = 'int';
											} else {
												$type = $field['attributes']['type'];
											}
										}
										$fields_tokens[] = [
											'tokenId'         => $token_id,
											'tokenName'       => $input_title,
											'tokenType'       => $type,
											'tokenIdentifier' => $trigger_meta,
										];
									} else {
										$fields_tokens[] = $this->create_token( $form_id, $field, $trigger_meta, $field_group_name );
									}
								}
							}
						} elseif ( isset( $raw_field['attributes']['name'] ) ) {

							// Skip html only feilds that are not actual form inputs
							if ( isset( $raw_field['element'] ) && 'custom_html' === $raw_field['element'] ) {
								continue;
							}

							// Skip file upload fields. Not supported
							if ( isset( $raw_field['element'] ) && 'input_file' === $raw_field['element'] ) {
								continue;
							}

							$field = $raw_field;

							$fields_tokens[] = $this->create_token( $form_id, $field, $trigger_meta );
						}


					}
				}


				$tokens = array_merge( $tokens, $fields_tokens );
			}
		}

		return $tokens;
	}