All Fields Template

Modify the {all_fields} merge tag output via a template file.

Instructions

See “Where do I put snippets?” in our documentation for installation instructions.
Read the Walkthrough

Usage

  • {all_fields}

    By default, enabling this plugin will look for this template: <theme>/gravity-forms/all-fields.php

    Override the {all_fields} template for a specific form by specifying the form ID: <theme>/gravity-forms/all-fields-<formId>.php

  • {all_fields:template[custom]}

    Specify a custom template suffix. This is useful for allowing specific forms to use the same template. <theme>/gravity-forms/all-fields-custom.php

  • {all_fields:notemplate}

    Will always load the default Gravity Forms {all_fields} markup.

  • {all_fields:nopricingfields}

    Hide all pricing fields (i.e. order summary).

Filtering Usage

  • :filter

    Filtering will only include the specified fields and exclude all others. It cannot be combined with the include exclude filters.

    {all_fields:filter[1]} {all_fields:filter[1,2]}

  • :include

    Including will include fields with types that are typically not supported by the {all_fields} merge tag (e.g., HTML fields).

    {all_fields:include[3]} {all_fields:include[3,4]} {all_fields:include[3,4],exclude[5]}

  • :exclude

    Excluding will exclude specific fields from being included in the {all_fields} output.

    {all_fields:exclude[5]} {all_fields:exclude[5,6]} {all_fields:exclude[5],include[3,4]}

  • :updated

    Only show fields that were most recently updated.

    {all_fields:updated}

Code

Filename: gw-all-fields-template.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
<?php
/**
 * Gravity Wiz // Gravity Forms // All Fields Template
 *
 * Modify the {all_fields} merge tag output via a template file.
 *
 * Plugin Name:  Gravity Forms All Fields Template
 * Plugin URI:   https://gravitywiz.com/gravity-forms-all-fields-template/
 * Description:  Modify the {all_fields} merge tag output via a template file.
 * Author:       Gravity Wiz
 * Version:      0.13
 * Author URI:   https://gravitywiz.com
 *
 * Usage:
 *
 * - **`{all_fields}`**
 *
 *     By default, enabling this plugin will look for this template:
 *     `<theme>/gravity-forms/all-fields.php`
 *
 *     Override the `{all_fields}` template for a specific form by specifying the form ID:
 *     `<theme>/gravity-forms/all-fields-<formId>.php`
 *
 * - **`{all_fields:template[custom]}`**
 *
 *     Specify a custom template suffix. This is useful for allowing specific forms to use the same template.
 *     `<theme>/gravity-forms/all-fields-custom.php`
 *
 * - **`{all_fields:notemplate}`**
 *
 *     Will always load the default Gravity Forms `{all_fields}` markup.
 *
 * - **`{all_fields:nopricingfields}`**
 *
 *     Hide all pricing fields (i.e. order summary).
 *
 * ## Filtering Usage
 *
 * - **`:filter`**
 *
 *    Filtering will only include the specified fields and exclude all others. It cannot be combined with the include
 *    exclude filters.
 *
 *    `{all_fields:filter[1]}`
 *    `{all_fields:filter[1,2]}`
 *
 * - **`:include`**
 *
 *    Including will include fields with types that are typically not supported by the `{all_fields}` merge tag
 *    (e.g., HTML fields).
 *
 *    `{all_fields:include[3]}`
 *    `{all_fields:include[3,4]}`
 *    `{all_fields:include[3,4],exclude[5]}`
 *
 * - **`:exclude`**
 *
 *    Excluding will exclude specific fields from being included in the `{all_fields}` output.
 *
 *    `{all_fields:exclude[5]}`
 *    `{all_fields:exclude[5,6]}`
 *    `{all_fields:exclude[5],include[3,4]}`
 *
 * - **`:updated`**
 *
 *    Only show fields that were most recently updated.
 *
 *    `{all_fields:updated}`
 *
 */
class GW_All_Fields_Template {

	private static $instance = null;
	private $original_entry  = array();

	public static function get_instance() {
		if ( self::$instance == null ) {
			self::$instance = new self;
		}
		return self::$instance;
	}

	private function __construct() {

		add_action( 'init', array( $this, 'init' ) );

	}

	public function init() {

		add_filter( 'gform_pre_replace_merge_tags', array( $this, 'replace_merge_tags' ), 9, 7 );
		add_filter( 'gform_merge_tag_filter', array( $this, 'all_fields_extra_options' ), 21, 6 );
		add_action( 'gform_post_update_entry', array( $this, 'save_original_entry' ), 10, 2 );
	}

	/**
	 * to exclude field from notification add 'exclude[ID]' option to {all_fields} tag
	 * 'include[ID]' option includes HTML field / Section Break field description / Signature image in notification
	 * see http://www.gravityhelp.com/documentation/page/Merge_Tags for a list of standard options
	 *
	 * include: Include a field that is not included by default (i.e. HTML fields).
	 * exclude: Exclude a field that is included by default.
	 * filter:  Only include the specified field IDs. This takes precedence over both the 'include' and 'exclude' modifiers.
	 *
	 * example: {all_fields:exclude[2,3]}
	 * example: {all_fields:include[6]}
	 * example: {all_fields:include[6],exclude[2,3]}
	 */
	public function all_fields_extra_options( $value, $merge_tag, $modifiers, $field, $raw_value, $format ) {

		if ( ! is_a( $field, 'GF_Field' ) ) {
			$field       = new GF_Field();
			$field->type = $field;
		}

		if ( $merge_tag !== 'all_fields' && $field->type === 'form' ) {
			return $value;
		}

		$modifiers = $this->parse_modifiers( $modifiers );
		$whitelist = array( 'filter', 'include', 'exclude', 'nopricingfields', 'updated' );
		$context   = rgar( $modifiers, 'context', false );

		foreach ( $modifiers as $modifier => $mod_values ) {

			if ( ! in_array( $modifier, $whitelist ) ) {
				continue;
			}

			if ( ! is_array( $mod_values ) ) {
				$mod_values = array( $mod_values );
			}

			foreach ( $mod_values as $mod_value ) {
				if ( ! is_numeric( $mod_value ) ) {
					/**
					 * Filter an individual value specified for the given modifier.
					 *
					 * The primary intent is to provide a mechanism for specifying a custom value that will be replaced
					 * with an array of field IDs. For example, if you want to exclude fields with personal information
					 * from some instances of the {all_fields} merge tag, you could specify a custom modifier value like
					 * so:
					 *
					 * ```
					 * {all_fields:exclude[persInfoFields]}
					 * ```
					 *
					 * And then create a custom function to identify the IDs of those fields.
					 *
					 * ```
					 * add_filter( 'gwaft_modifier_value_persInfoFields', function() {
					 *     return array( 1, 2, 3 );
					 * } );
					 * ```
					 *
					 * @since 0.9.9
					 *
					 * @param string    $mod_value The custom modifier value to be replaced.
					 * @param string    $modifier  The current modifier.
					 * @param array     $modifiers All modifiers specified for the current merge tag.
					 * @param \GF_Field $field     The current field.
					 */
					$new_value = gf_apply_filters( array( 'gwaft_modifier_value', $mod_value, $field->formId ), $mod_value, $modifier, $modifiers, $field );
					if ( $new_value !== $mod_value ) {
						array_splice( $mod_values, array_search( $mod_value, $mod_values, true ), 1, $new_value );
					}
				}
			}

			if ( $modifier === 'nopricingfields' && ! has_filter( 'gform_order_summary', array( $this, 'clear_order_summary' ) ) ) {
				add_filter( 'gform_order_summary', array( $this, 'clear_order_summary' ) );
			}

			/**
			 * Integrate w/ GP Nested Forms to allow filtering which fields are displayed for nested entries in the
			 * Nested Form field's value. GPNF will pass custom modifiers (e.g., context[nested],parent[fieldId]).
			 */
			if ( $context == 'nested' ) {

				$input_ids = array();
				$nested_form_field_id = rgar( $modifiers, 'parent', false );
				if ( ! $nested_form_field_id ) {
					break;
				}

				$field_ids = array();
				foreach ( $mod_values as $field_id ) {
					if ( intval( $field_id ) == $nested_form_field_id && $field_id !== intval( $field_id ) ) {
						$field_id_bits = explode( '.', $field_id );
						$field_ids[]   = (int) array_pop( $field_id_bits );
					}
				}
			} else {

				$input_ids = $mod_values;
				$field_ids = array_map( 'intval', $mod_values );

			}

			switch ( $modifier ) {
				case 'filter':
					if ( in_array( $field->id, $field_ids ) ) {
						// Check for input-specific filters.
						if ( is_array( $raw_value ) && ! empty( $input_ids ) && ! in_array( $field->id, $input_ids ) ) {
							$filtered_values = array();

							foreach ( $input_ids as $input_id ) {
								if ( in_array( $input_id, $input_ids ) ) {
									$filtered_values[ $input_id ] = $raw_value[ $input_id ];
								}
							}

							$value = GFCommon::get_lead_field_display( $field, $filtered_values );
						}

						$value = $this->get_all_fields_field_value( $field, $value );
					} else {
						$value = false;
					}
					break;
				case 'include':
					if ( in_array( $field->id, $field_ids ) ) {
						$value = $this->get_all_fields_field_value( $field, $value );
					}
					break;
				case 'exclude':
					// exclude the fields marked to be excluded, or exclude all pricing fields if 'nopricingfields' merge tag is used.
					if ( in_array( (int) $field->id, $field_ids, true ) ) {

						$exclude_full_value = true;

						// Check for input-specific exclusions.
						if ( is_array( $raw_value ) ) {
							$values = $raw_value;
							foreach ( $input_ids as $input_id ) {
								$target_field_id = intval( $input_id );
								if ( (string) $target_field_id !== (string) $input_id && $target_field_id === intval( $field->id ) ) {
									$exclude_full_value  = false;
									$values[ $input_id ] = '';
								}
							}
							$value = GFCommon::get_lead_field_display( $field, $values );

							// Consent form requires special treatment to remove the description HTML that's output into {all_fields}
							// phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
							if ( $field->type === 'consent' && in_array( $field->id . '.3', $input_ids ) ) {
								$revision_id = ! is_array( $value ) || empty( $value[ $input_id ] )
									? GFFormsModel::get_latest_form_revisions_id( $field['formId'] ) : $value[ $input_id ];

								$consent_description = $field->get_field_description_from_revision( $revision_id );
								$description_html    = '<br /><div class="gfield_consent_description">' . nl2br( $consent_description ) . '</div>';
								$value               = str_replace( $description_html, '', $value );
							}
							// Check for exclusions excluding a specific child field from a Nested Form field.
						} elseif ( $field->get_input_type() === 'form' && (int) $mod_value != $mod_value ) {
							// GPNF assumes Nested Form field should be excluded if $value is false. Prevent this and
							// allow GPNF to load the {all_fields} markup for its children.
							$exclude_full_value = false;
						}

						if ( $exclude_full_value ) {
							$value = false;
						}
					}
					break;
				case 'updated':
					if ( ! is_array( $raw_value ) ) {
						$raw_value = array( $field->id => $raw_value );
					}
					$has_change = false;
					foreach ( $raw_value as $input_id => $input_value ) {
						if ( rgar( $this->original_entry, $input_id ) != $input_value ) {
							$has_change = true;
						}
					}
					if ( ! $has_change ) {
						$value = false;
					}
					break;
			}
		}

		// echo '<pre>';
		// $field_id = $field->id;
		// print_r( compact( 'modifiers', 'field_ids', 'field_id', 'value' ) );
		// echo '<pre>';
		return $value;
	}

	public function save_original_entry( $entry, $original_entry ) {
		$this->original_entry = $original_entry;
	}

	public function get_original_entry() {
		return $this->original_entry;
	}

	public function replace_merge_tags( $text, $form, $entry, $url_encode, $esc_html, $nl2br, $format ) {

		$matches = array();

		preg_match_all( '/{all_fields(?::(.*?))?}/i', $text, $matches, PREG_SET_ORDER );

		foreach ( $matches as $match ) {

			// Replace each unique merge tag only once.
			if ( strpos( $text, $match[0] ) === false ) {
				continue;
			}

			$modifiers = rgar( $match, 1 );
			$options   = compact( 'url_encode', 'esc_html', 'nl2br', 'format' );
			$content   = $this->parse_template( 'all-fields', $modifiers, $form, $entry, $options );

			if ( $content !== false ) {
				$text = str_replace( $match[0], $content, $text );
			}
		}

		return $text;
	}

	public function parse_template( $template, $modifiers, $form, $entry, $options = array() ) {

		$_modifiers = $modifiers;
		$modifiers  = $this->parse_modifiers( $modifiers );

		if ( in_array( 'notemplate', $modifiers ) ) {
			return false;
		}

		$use_value       = in_array( 'value', $modifiers );
		$display_empty   = in_array( 'empty', $modifiers );
		$use_admin_label = in_array( 'admin', $modifiers );

		$suffixes = array( $form['id'] );
		if ( array_key_exists( 'template', $modifiers ) ) {
			array_unshift( $suffixes, $modifiers['template'] );
		}

		$items = rgar( $options, 'items' );
		if ( empty( $items ) ) {
			$items = $this->get_items( $form, $entry, $display_empty, ! $use_value, $options['format'], $use_admin_label, 'all_fields', $_modifiers );
		}

		// Add meta fields to the items array.
		if ( ! empty( $modifiers ) && array_key_exists( 'meta', $modifiers ) ) {
			$meta_keys = $modifiers['meta'];
			foreach ( $meta_keys as $key ) {
				$items[] = array(
					'label' => $key,
					'value' => gform_get_meta( $entry['id'], $key ),
				);
			}
		}

		$output = $this->load_template( $template, null, array(
			'form'  => $form,
			'entry' => $entry,
			'items' => $items,
		), $suffixes );

		if ( $output === false && rgar( $options, 'fallback' ) ) {
			if ( is_callable( $options['fallback'] ) ) {
				$output = call_user_func( $options['fallback'], $items );
			}
		}

		return $output;
	}

	public function replace_nested_forms_all_fields( $value, $field, $nested_form, $entry, $modifiers ) {

		$output = $this->parse_template( $modifiers, $nested_form, $entry, array( 'format' => 'html' ) );
		if ( $output ) {
			$value = $output;
		}

		return $value;
	}

	public function parse_modifiers( $modifiers_str ) {

		preg_match_all( '/([a-z_]+)(?:(?:\[(.+?)\])|,?)/i', $modifiers_str, $modifiers, PREG_SET_ORDER );
		$parsed = array();

		foreach ( $modifiers as $modifier ) {

			list( $match, $modifier, $value ) = array_pad( $modifier, 3, null );
			if ( $value === null ) {
				$value = $modifier;
			}

			// Split '1,2,3' into array( 1, 2, 3 ).
			if ( strpos( $value, ',' ) !== false ) {
				$value = array_map( 'trim', explode( ',', $value ) );
			}

			$parsed[ strtolower( $modifier ) ] = $value;

		}

		return $parsed;
	}

	public function get_items( $form, $lead, $display_empty = false, $use_text = false, $format = 'html', $use_admin_label = false, $merge_tag = '', $modifiers = '' ) {
		return $this->get_submitted_fields( $form, $lead, $display_empty, $use_text, $format, $use_admin_label, $merge_tag, $modifiers );
	}

	public function get_submitted_fields( $form, $lead, $display_empty = false, $use_text = false, $format = 'html', $use_admin_label = false, $merge_tag = '', $modifiers = '' ) {

		$items = array();

		//$field_data = '';

		$modifiers_array         = explode( ',', $modifiers );
		$no_admin                = in_array( 'noadmin', $modifiers_array );
		$no_hidden               = in_array( 'nohidden', $modifiers_array );
		$display_product_summary = false;

		foreach ( $form['fields'] as $field ) {

			$field_value = '';
			$field_label = $use_admin_label && ! empty( $field->adminLabel ) ? $field->adminLabel : esc_html( GFCommon::get_label( $field, 0, false, $use_admin_label ) );

			switch ( $field->type ) {
				case 'captcha':
					break;
				case 'section':
					$field_value = false;

					if ( GFFormsModel::is_field_hidden( $form, $field, array(), $lead ) ) {
						break;
					}

					if ( ( ! GFCommon::is_section_empty( $field, $form, $lead ) || $display_empty ) && ! $field->adminOnly ) {

						switch ( $format ) {
							case 'text':
								$field_value = "--------------------------------\n{$field_label}\n\n";
								break;

							default:
								$field_value = '';
								break;
						}
					}

					$field_value = apply_filters( 'gform_merge_tag_filter', $field_value, $merge_tag, $modifiers, $field, $field_label, $format );

					//$field_data .= $field_value;

					if ( $field_value !== false ) {
						$item = array(
							'label' => $field_label,
							'value' => $field_value,
							'field' => $field,
						);
					}

					break;
				case 'password':
					//ignore password fields
					break;

				default:
					if ( GFCommon::is_product_field( $field->type ) ) {

						// ignore product fields as they will be grouped together at the end of the grid
						$display_product_summary = apply_filters( 'gform_display_product_summary', true, $field, $form, $lead );
						if ( $display_product_summary ) {
							break;
						}
					} elseif ( GFFormsModel::is_field_hidden( $form, $field, array(), $lead ) ) {
						// ignore fields hidden by conditional logic
						break;
					}

					$raw_field_value = RGFormsModel::get_lead_field_value( $lead, $field );
					$field_value     = GFCommon::get_lead_field_display( $field, $raw_field_value, rgar( $lead, 'currency' ), $use_text, $format, 'email' );

					$display_field = true;
					//depending on parameters, don't display adminOnly or hidden fields
					if ( $no_admin && $field->adminOnly ) {
						$display_field = false;
					} elseif ( $no_hidden && RGFormsModel::get_input_type( $field ) == 'hidden' ) {
						$display_field = false;
					}

					//if field is not supposed to be displayed, pass false to filter. otherwise, pass field's value
					if ( ! $display_field ) {
						$field_value = false;
					}

					$field_value = apply_filters( 'gform_merge_tag_filter', $field_value, $merge_tag, $modifiers, $field, $raw_field_value, $format );

					if ( $field_value === false ) {
						break;
					}

					if ( ! empty( $field_value ) || strlen( $field_value ) > 0 || $display_empty ) {

						switch ( $format ) {
							case 'text':
								//$field_data .= "{$field_label}: {$field_value}\n\n";
								break;

							default:
								//                              $field_data .= sprintf(
								//                                  '<tr bgcolor="%3$s">
								//                                                            <td colspan="2">
								//                                                                <font style="font-family: sans-serif; font-size:12px;"><strong>%1$s</strong></font>
								//                                                            </td>
								//                                                       </tr>
								//                                                       <tr bgcolor="%4$s">
								//                                                            <td width="20">&nbsp;</td>
								//                                                            <td>
								//                                                                <font style="font-family: sans-serif; font-size:12px;">%2$s</font>
								//                                                            </td>
								//                                                       </tr>
								//                                                       ', $field_label, empty( $field_value ) && strlen( $field_value ) == 0 ? '&nbsp;' : $field_value, apply_filters( 'gform_email_background_color_label', '#EAF2FA', $field, $lead ), apply_filters( 'gform_email_background_color_data', '#FFFFFF', $field, $lead )
								//                              );
								break;
						}

						$item = array(
							'label' => $field_label,
							'value' => $field_value,
							'field' => $field,
						);

					}
			}

			if ( isset( $item ) ) {
				$items[] = $item;
				unset( $item );
			}
		}

		if ( $display_product_summary ) {
			$value = $this->all_fields_extra_options( GFCommon::get_submitted_pricing_fields( $form, $lead, $format, $use_text, $use_admin_label ), $merge_tag, $modifiers, 'order_summary', null, $format );
			if ( $value !== false ) {
				$items[] = array(
					'label' => apply_filters( 'gwaft_order_summary_label', 'Order Summary' ),
					'value' => $value,
				);
			}
		}

		return $items;
	}

	public function get_all_fields_field_value( $field, $value ) {

		switch ( $field->type ) {
			case 'html':
				$value = $field->content;
				break;
			// Sections are included by default; including them manually will append their description.
			case 'section':
				$value .= sprintf( '<tr bgcolor="#FFFFFF">
	                                                        <td width="20">&nbsp;</td>
	                                                        <td>
	                                                            <font style="font-family: sans-serif; font-size:12px;">%s</font>
	                                                        </td>
	                                                   </tr>
	                                                   ', $field->description );
				break;
			case 'signature':
				if ( version_compare( GF_SIGNATURE_VERSION, '4.0', '<' ) ) {
					$url   = is_callable( 'gf_signature' ) ? gf_signature()->get_url( $value ) : $value;
					$value = "<img alt='signature' src='{$url}' />";
				}
				break;
		}

		return $value;
	}

	public function log( $message ) {
		GFCommon::log_debug( $message );
	}

	public function clear_order_summary() {
		remove_filter( 'gform_order_summary', array( $this, 'clear_order_summary' ) );
		return '';
	}

	public function hide_order_summary_label() {
		remove_filter( 'gform_display_product_summary', array( $this, 'hide_order_summary_label' ) );
		return false;
	}

	// ### TEMPLATE SYSTEM (compliments of EDD) ###

	public function load_template( $slug, $name = null, $data = array(), $suffixes = array() ) {
		ob_start();
		// phpcs:ignore WordPress.PHP.DontExtract.extract_extract
		extract( $data );
		$template = $this->get_template_part( $slug, $name, false, $suffixes );
		if ( ! empty( $template ) ) {
			include( $template );
		}
		$content = ob_get_clean();

		/**
		 * Filters the output loaded by the template.
		 *
		 * @since 0.12
		 *
		 * @param string $content  The output content loaded by the template.
		 * @param string $slug     The slug name for the generic template.
		 * @param string $name     The name of the specialized template.
		 * @param array  $data     An array of data extracted for use in the template.
		 * @param array  $suffixes An array of suffixes used to locate the template.
		 */
		$content = apply_filters( 'gwaft_template_output', $content, $slug, $name, $data, $suffixes );
		return ! $template && ! $content ? false : $content;
	}

	public function get_template_part( $slug, $name = null, $load = true, $suffixes = array() ) {

		// Execute code for this part
		do_action( 'get_template_part_' . $slug, $slug, $name, $suffixes );

		// Setup possible parts
		$templates = array();

		if ( isset( $name ) ) {
			$suffixes[] = $name;
		}

		foreach ( $suffixes as $suffix ) {
			$templates[] = $slug . '-' . $suffix . '.php';
		}

		$templates[] = $slug . '.php';

		// Return the part that is found
		return $this->locate_template( $templates, $load, false );
	}

	public function locate_template( $template_names, $load = false, $require_once = true ) {

		// No file found yet
		$located = false;

		// Try to find a template file
		foreach ( (array) $template_names as $template_name ) {

			// Continue if template is empty
			if ( empty( $template_name ) ) {
				continue;
			}

			// Trim off any slashes from the template name
			$template_name = ltrim( $template_name, '/' );

			// try locating this template file by looping through the template paths
			foreach ( $this->get_theme_template_paths() as $template_path ) {

				if ( file_exists( $template_path . $template_name ) ) {
					$located = $template_path . $template_name;
					break;
				}
			}

			if ( $located ) {
				break;
			}
		}

		if ( ( true == $load ) && ! empty( $located ) ) {
			load_template( $located, $require_once );
		}

		return $located;
	}

	public function get_theme_template_paths() {

		$template_dir = $this->get_theme_template_dir_name();

		/**
		 * Filter the paths that will be checked for a template file.
		 *
		 * @since 0.9.12
		 *
		 * @param array $file_paths An array of file paths. Key is the priority of the path. Value is an absolute path to a directory.
		 */
		$file_paths = apply_filters( 'gwaft_template_paths', array(
			1  => trailingslashit( get_stylesheet_directory() ) . $template_dir,
			10 => trailingslashit( get_template_directory() ) . $template_dir,
		) );

		// sort the file paths based on priority
		ksort( $file_paths, SORT_NUMERIC );

		return array_map( 'trailingslashit', $file_paths );
	}

	public function get_theme_template_dir_name() {
		return trailingslashit( 'gravity-forms' );
	}

}

function gw_all_fields_template() {
	return GW_All_Fields_Template::get_instance();
}

gw_all_fields_template();
Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.