GFPDFHelperHelper_Abstract_Fields

Helper fields can be extended to allow each Gravity Form field type to be displayed correctly We found the default GF display functionality isn't quite up to par for the Gravity PDF requirements.

Defined (1)

The class is defined in the following location(s).

/src/helper/abstract/Helper_Abstract_Fields.php  
  1. abstract class Helper_Abstract_Fields { 
  2.  
  3. /** 
  4. * Contains the field array 
  5. * @var array|object 
  6. * @since 4.0 
  7. */ 
  8. public $field; 
  9.  
  10. /** 
  11. * Contains the form information 
  12. * @var array 
  13. * @since 4.0 
  14. */ 
  15. public $form; 
  16.  
  17. /** 
  18. * Holds the abstracted Gravity Forms API specific to Gravity PDF 
  19. * @var \GFPDF\Helper\Helper_Form 
  20. * @since 4.0 
  21. */ 
  22. public $gform; 
  23.  
  24. /** 
  25. * Contains the entry information 
  26. * @var array 
  27. * @since 4.0 
  28. */ 
  29. public $entry; 
  30.  
  31. /** 
  32. * Used to cache the $this->value() results 
  33. * @var array 
  34. * @since 4.0 
  35. */ 
  36. private $cached_results; 
  37.  
  38. /** 
  39. * As come fields can have multiple field types we'll use $fieldObject to store the object 
  40. * @var object 
  41. * @since 4.0 
  42. */ 
  43. public $fieldObject; 
  44.  
  45. /** 
  46. * Holds our Helper_Misc object 
  47. * Makes it easy to access common methods throughout the plugin 
  48. * @var \GFPDF\Helper\Helper_Misc 
  49. * @since 4.0 
  50. */ 
  51. protected $misc; 
  52.  
  53. /** 
  54. * Set up the object 
  55. * Check the $entry is an array, or throw exception 
  56. * The $field is validated in the child classes 
  57. * @param object $field The GF_Field_* Object 
  58. * @param array $entry The Gravity Forms Entry 
  59. * @param \GFPDF\Helper\Helper_Abstract_Form $gform 
  60. * @param \GFPDF\Helper\Helper_Misc $misc 
  61. * @throws Exception 
  62. * @since 4.0 
  63. */ 
  64. public function __construct( $field, $entry, Helper_Abstract_Form $gform, Helper_Misc $misc ) { 
  65.  
  66. /** Assign our internal variables */ 
  67. $this->misc = $misc; 
  68.  
  69. /** Throw error if not dependacies not met */ 
  70. if ( ! class_exists( 'GFFormsModel' ) ) { 
  71. throw new Exception( 'Gravity Forms is not correctly loaded.' ); 
  72.  
  73. if ( ! is_object( $field ) || ! ( $field instanceof GF_Field ) ) { 
  74. throw new Exception( '$field needs to be in instance of GF_Field' ); 
  75.  
  76. /** Throw error if $entry is not an array */ 
  77. if ( ! is_array( $entry ) ) { 
  78. throw new Exception( '$entry needs to be an array' ); 
  79.  
  80. $this->field = $field; 
  81. $this->entry = $entry; 
  82. $this->form = $gform->get_form( $entry['form_id'] ); 
  83. $this->gform = $gform; 
  84.  
  85.  
  86. /** 
  87. * Control the getting and setting of the cache 
  88. * @param mixed $value is passed in it will set a new cache 
  89. * @return mixed The current cached_results 
  90. * @since 4.0 
  91. */ 
  92. final public function cache( $value = null ) { 
  93. if ( ! is_null( $value ) ) { 
  94. $this->cached_results = $value; 
  95.  
  96. return $this->cached_results; 
  97.  
  98. /** 
  99. * Check if we currently have a cache 
  100. * @return boolean True is we have a cache and false if we do not 
  101. * @since 4.0 
  102. */ 
  103. final public function has_cache() { 
  104. if ( ! is_null( $this->cached_results ) ) { 
  105. return true; 
  106.  
  107. return false; 
  108.  
  109. /** 
  110. * Reset the cache 
  111. * @since 4.0 
  112. */ 
  113. final public function remove_cache() { 
  114. $this->cached_results = null; 
  115.  
  116. /** 
  117. * Used to process the Gravity Forms value extracted from the entry array 
  118. * Each value is then passed to the value method set up by the child objects 
  119. * @since 4.0 
  120. */ 
  121. final public function get_value() { 
  122.  
  123. /** 
  124. * Gravity Forms' GFCache function was thrashing the database, causing double the amount of time for the field_value() method to run. 
  125. * The reason is that the cache was checking against a field value stored in a transient every time `GFFormsModel::get_lead_field_value()` is called. 
  126. * We're forcing the cache to skip the extra database lookup and just get the value. 
  127. * @hack 
  128. * @since 4.0 
  129. * @credit Zack Katz (Gravity View author) 
  130. * @fixed Gravity Forms 1.9.13.25 
  131. */ 
  132. if ( class_exists( 'GFCache' ) && version_compare( GFCommon::$version, '1.9.13.25', '<' ) ) { 
  133. GFCache::set( 'GFFormsModel::get_lead_field_value_' . $this->entry['id'] . '_' . $this->field->id, false, false, 0 ); 
  134.  
  135. /** 
  136. * Get the Gravity Forms field value 
  137. * See https://gravitypdf.com/documentation/v4/gfpdf_field_value/ for more details about this filter 
  138. */ 
  139.  
  140. return apply_filters( 'gfpdf_field_value', GFFormsModel::get_lead_field_value( $this->entry, $this->field ), $this->field, $this->entry ); 
  141.  
  142. /** 
  143. * Used to check if the current field has a value 
  144. * @since 4.0 
  145. * @internal Child classes can override this method when dealing with a specific use case 
  146. * @return boolean Return true if the field is empty, false if it has a value 
  147. */ 
  148. public function is_empty() { 
  149. $value = $this->value(); 
  150.  
  151. if ( is_array( $value ) && sizeof( array_filter( $value ) ) === 0 ) { /** check for an array */ 
  152. return true; 
  153. } else if ( is_string( $value ) && strlen( trim( $value ) ) === 0 ) { /** check for a string */ 
  154. return true; 
  155.  
  156. return false; 
  157.  
  158. /** 
  159. * Standardised method for returning the field's correct $form_data['field'] keys 
  160. * @return array 
  161. * @since 4.0 
  162. */ 
  163. public function form_data() { 
  164.  
  165. $value = $this->value(); 
  166. $label = GFFormsModel::get_label( $this->field ); 
  167. $field_id = (int) $this->field->id; 
  168. $data = []; 
  169.  
  170. /** Add field data using standardised naming convesion */ 
  171. $data[ $field_id . '.' . $label ] = $value; 
  172.  
  173. /** Add field data using standardised naming convesion */ 
  174. $data[ $field_id ] = $value; 
  175.  
  176. /** Keep backwards compatibility */ 
  177. $data[ $label ] = $value; 
  178.  
  179. return [ 'field' => $data ]; 
  180.  
  181. /** 
  182. * Get the default HTML output for this field 
  183. * @param string $value The field value to be displayed 
  184. * @param boolean $show_label Whether or not to show the field's label 
  185. * @since 4.0 
  186. * @return string 
  187. */ 
  188. public function html( $value = '', $show_label = true ) { 
  189.  
  190. $value = $this->encode_tags( $value, $this->field->type ); /** Prevent shortcodes and merge tags being processed from user input */ 
  191. $value = apply_filters( 'gfpdf_field_content', $value, $this->field, GFFormsModel::get_lead_field_value( $this->entry, $this->field ), $this->entry['id'], $this->form['id'] ); /** Backwards compat */ 
  192.  
  193. $label = esc_html( GFFormsModel::get_label( $this->field ) ); 
  194.  
  195. $type = ( ! empty( $this->field->inputType ) ) ? $this->field->inputType : $this->field->type; 
  196.  
  197. $html = '<div id="field-' . $this->field->id . '" class="gfpdf-' . $type . ' gfpdf-field ' . $this->field->cssClass . '"> 
  198. <div class="inner-container">'; 
  199.  
  200. if ( $show_label ) { 
  201. $html .= '<div class="label"><strong>' . $label . '</strong></div>'; 
  202.  
  203. /** If the field value is empty we'll add a non-breaking space to act like a character and maintain proper layout */ 
  204. if ( strlen( trim( $value ) ) === 0 ) { 
  205. $value = ' '; 
  206.  
  207. $html .= '<div class="value">' . $value . '</div>' 
  208. . '</div>' 
  209. . '</div>'; 
  210.  
  211. /** See https://gravitypdf.com/documentation/v4/gfpdf_field_html_value/ for more details about this filter */ 
  212.  
  213. return apply_filters( 'gfpdf_field_html_value', $html, $value, $show_label, $label, $this->field, $this->form, $this->entry, $this ); 
  214.  
  215. /** 
  216. * Used to process the Gravity Forms value extracted from the entry 
  217. * @since 4.0 
  218. */ 
  219. abstract public function value(); 
  220.  
  221. /** 
  222. * Prevent user-data shortcodes from being processed by the PDF templates 
  223. * @param string $value The text to be converted 
  224. * @param string $type The field type 
  225. * @return string 
  226. * @since 4.0 
  227. */ 
  228. public function encode_tags( $value, $type ) { 
  229.  
  230. $skip_fields = [ 'html', 'signature', 'section' ]; 
  231.  
  232. if ( ! in_array( $type, $skip_fields ) ) { 
  233.  
  234. $find = [ '[', ']', '{', '}' ]; 
  235. $converted = [ '[', ']', '{', '}' ]; 
  236. $value = str_replace( $find, $converted, $value ); 
  237.  
  238. return $value;