|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Gravity Wiz // Gravity Forms // Display HTML Field on Entry Details |
| 4 | + * |
| 5 | + * Save and display HTML field content (including Live Merge Tags and shortcodes) in the entry detail view. |
| 6 | + * Useful for retaining dynamic HTML field output as it appeared when the entry was submitted. |
| 7 | + * |
| 8 | + * Plugin Name: Display HTML Field on Entry Details |
| 9 | + * Plugin URI: http://gravitywiz.com/ |
| 10 | + * Description: Save and display HTML field content (including Live Merge Tags and shortcodes) in the entry detail view. |
| 11 | + * Author: Gravity Wiz |
| 12 | + * Version: 0.1 |
| 13 | + * Author URI: http://gravitywiz.com |
| 14 | + */ |
| 15 | +class GW_Display_HTML_Field_Entry_Detail { |
| 16 | + |
| 17 | + private $_args = array(); |
| 18 | + |
| 19 | + public function __construct( $args = array() ) { |
| 20 | + $this->_args = wp_parse_args( |
| 21 | + $args, |
| 22 | + array( |
| 23 | + 'form_id' => false, |
| 24 | + 'field_id' => false, |
| 25 | + ) |
| 26 | + ); |
| 27 | + |
| 28 | + add_action( 'init', array( $this, 'init' ) ); |
| 29 | + } |
| 30 | + |
| 31 | + public function init() { |
| 32 | + |
| 33 | + add_filter( 'gform_entry_post_save', array( $this, 'save_html_field_content' ), 10, 1 ); |
| 34 | + add_action( 'gform_entry_detail', array( $this, 'display_html_field_content' ), 10, 2 ); |
| 35 | + } |
| 36 | + |
| 37 | + public function is_applicable_form( $form ) { |
| 38 | + $form_id = is_array( $form ) && isset( $form['id'] ) ? $form['id'] : (int) $form; |
| 39 | + |
| 40 | + return empty( $this->_args['form_id'] ) || (int) $form_id === (int) $this->_args['form_id']; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Save HTML field content to entry meta. |
| 45 | + * |
| 46 | + * @param array $entry The entry object. |
| 47 | + * @return array |
| 48 | + */ |
| 49 | + public function save_html_field_content( $entry ) { |
| 50 | + |
| 51 | + $form = GFAPI::get_form( rgar( $entry, 'form_id' ) ); |
| 52 | + |
| 53 | + if ( ! $this->is_applicable_form( $form ) ) { |
| 54 | + return $entry; |
| 55 | + } |
| 56 | + |
| 57 | + foreach ( $form['fields'] as $field ) { |
| 58 | + if ( $field->get_input_type() === 'html' ) { |
| 59 | + |
| 60 | + // Only process matching field_id if defined. |
| 61 | + if ( ! empty( $this->_args['field_id'] ) && (int) $field->id !== (int) $this->_args['field_id'] ) { |
| 62 | + continue; |
| 63 | + } |
| 64 | + |
| 65 | + gform_update_meta( $entry['id'], 'html_field_' . $field->id, $field->content ); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + return $entry; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Display HTML content on entry detail page. |
| 74 | + * |
| 75 | + * @param array $form The form object. |
| 76 | + * @param array $entry The entry object. |
| 77 | + */ |
| 78 | + public function display_html_field_content( $form, $entry ) { |
| 79 | + |
| 80 | + if ( ! $this->is_applicable_form( $form ) ) { |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + foreach ( $form['fields'] as $field ) { |
| 85 | + |
| 86 | + if ( $field->get_input_type() !== 'html' ) { |
| 87 | + continue; |
| 88 | + } |
| 89 | + |
| 90 | + // Only process matching field_id if defined. |
| 91 | + if ( ! empty( $this->_args['field_id'] ) && (int) $field->id !== (int) $this->_args['field_id'] ) { |
| 92 | + continue; |
| 93 | + } |
| 94 | + |
| 95 | + $content = gform_get_meta( $entry['id'], 'html_field_' . $field->id ); |
| 96 | + |
| 97 | + // Process GPPA Live Merge Tags if present. |
| 98 | + if ( |
| 99 | + method_exists( 'GP_Populate_Anything_Live_Merge_Tags', 'has_live_merge_tag' ) |
| 100 | + && GP_Populate_Anything_Live_Merge_Tags::get_instance()->has_live_merge_tag( $content ) |
| 101 | + ) { |
| 102 | + $content = gp_populate_anything()->live_merge_tags->replace_live_merge_tags_static( $content, $form, $entry ); |
| 103 | + } |
| 104 | + |
| 105 | + // Process shortcodes. |
| 106 | + $content = do_shortcode( $content ); |
| 107 | + |
| 108 | + if ( ! empty( $content ) ) { |
| 109 | + printf( |
| 110 | + '<h4>%s</h4><div>%s</div><hr>', |
| 111 | + esc_html( $field->label ), |
| 112 | + wp_kses_post( $content ) |
| 113 | + ); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +# Configuration |
| 120 | +new GW_Display_HTML_Field_Entry_Detail( array( |
| 121 | + 'form_id' => 846, // Replace with your form ID or leave false for all. |
| 122 | + 'field_id' => 4, // Replace with your HTML field ID or leave false to process all HTML fields. |
| 123 | +) ); |
0 commit comments