GravityView  2.17
The best, easiest way to display Gravity Forms entries on your website.
class-gravityview-merge-tags.php
Go to the documentation of this file.
1 <?php
2 
3 /**
4  * Enhance Gravity Forms' merge tag functionality by adding additional merge tags
5  * @since 1.8.4
6  */
8 
9  /**
10  * @since 1.8.4
11  */
12  public function __construct() {
13  $this->add_hooks();
14  }
15 
16  /**
17  * Tap in to gform_replace_merge_tags to add merge tags
18  * @since 1.8.4
19  */
20  private function add_hooks() {
21 
22  /** @see GFCommon::replace_variables_prepopulate **/
23  add_filter( 'gform_replace_merge_tags', array( 'GravityView_Merge_Tags', 'replace_gv_merge_tags' ), 10, 7 );
24 
25  // Process after 10 priority
26  add_filter( 'gform_merge_tag_filter', array( 'GravityView_Merge_Tags', 'process_modifiers' ), 20, 5 );
27 
28  }
29 
30  /**
31  * Process custom GravityView modifiers for Merge Tags
32  *
33  * Is not processed on `{all_fields}` Merge Tag.
34  *
35  * @since 1.17
36  *
37  * @param string $value The current merge tag value to be filtered.
38  * @param string $merge_tag If the merge tag being executed is an individual field merge tag (i.e. {Name:3}), this variable will contain the field's ID. If not, this variable will contain the name of the merge tag (i.e. all_fields).
39  * @param string $modifier The string containing any modifiers for this merge tag. For example, "maxwords:10" would be the modifiers for the following merge tag: `{Text:2:maxwords:10}`.
40  * @param GF_Field $field The current field.
41  * @param mixed $raw_value The raw value submitted for this field.
42  *
43  * @return string If no modifiers passed, $raw_value is not a string, or {all_fields} Merge Tag is used, original value. Otherwise, output from modifier methods.
44  */
45  public static function process_modifiers( $value, $merge_tag, $modifier, $field, $raw_value ) {
46 
47  // No modifier was set or the raw value was empty
48  if ( 'all_fields' === $merge_tag || '' === $modifier || ! is_string( $raw_value ) || '' === $raw_value ) {
49  return $value;
50  }
51 
52  // matching regex => the value is the method to call to replace the value.
53  $gv_modifiers = array(
54  'maxwords:(\d+)' => 'modifier_maxwords', /** @see modifier_maxwords */
55  'timestamp' => 'modifier_timestamp', /** @see modifier_timestamp */
56  'explode' => 'modifier_explode', /** @see modifier_explode */
57 
58  /** @see modifier_strings */
59  'urlencode' => 'modifier_strings',
60  'wpautop' => 'modifier_strings',
61  'esc_html' => 'modifier_strings',
62  'sanitize_html_class' => 'modifier_strings',
63  'sanitize_title' => 'modifier_strings',
64  'strtolower' => 'modifier_strings',
65  'strtoupper' => 'modifier_strings',
66  'ucfirst' => 'modifier_strings',
67  'ucwords' => 'modifier_strings',
68  'wptexturize' => 'modifier_strings',
69  );
70 
71  $modifiers = explode( ',', $modifier );
72 
73  $return = $raw_value;
74 
75  $unserialized = maybe_unserialize( $raw_value );
76 
77  if ( method_exists( $field, 'get_value_merge_tag' ) && is_array( $unserialized ) ) {
78 
79  $non_gv_modifiers = array_diff( $modifiers, array_keys( $gv_modifiers ) );
80 
81  $return = $field->get_value_merge_tag( $value, '', array( 'currency' => '' ), array(), implode( '', $non_gv_modifiers ), $raw_value, false, false, 'text', false);
82  }
83 
84  foreach ( $modifiers as $passed_modifier ) {
85 
86  foreach( $gv_modifiers as $gv_modifier => $method ) {
87 
88  // Uses ^ to only match the first modifier, to enforce same order as passed by GF
89  preg_match( '/^' . $gv_modifier . '/ism', $passed_modifier, $matches );
90 
91  if ( empty( $matches ) ) {
92  continue;
93  }
94 
95  // The called method is passed the raw value and the full matches array
96  $return = self::$method( $return, $matches, $value, $field );
97  break;
98  }
99  }
100 
101  // No GravityView modifications were made; return the (default) original value
102  if ( $raw_value === $return ) {
103  return $value;
104  }
105 
106  /**
107  * @filter `gravityview/merge_tags/modifiers/value` Modify the merge tag modifier output
108  * @since 2.0
109  * @param string $return The current merge tag value to be filtered.
110  * @param string $raw_value The raw value submitted for this field. May be CSV or JSON-encoded.
111  * @param string $value The original merge tag value, passed from Gravity Forms
112  * @param string $merge_tag If the merge tag being executed is an individual field merge tag (i.e. {Name:3}), this variable will contain the field's ID. If not, this variable will contain the name of the merge tag (i.e. all_fields).
113  * @param string $modifier The string containing any modifiers for this merge tag. For example, "maxwords:10" would be the modifiers for the following merge tag: `{Text:2:maxwords:10}`.
114  * @param GF_Field $field The current field.
115  */
116  $return = apply_filters( 'gravityview/merge_tags/modifiers/value', $return, $raw_value, $value, $merge_tag, $modifier, $field );
117 
118  return $return;
119  }
120 
121  /**
122  * Convert Date field values to timestamp int
123  *
124  * @since 1.17
125  *
126  * @uses strtotime()
127  *
128  * @param string $raw_value Value to filter
129  * @param array $matches Regex matches group
130  *
131  * @return int Timestamp value of date. `-1` if not a valid timestamp.
132  */
133  private static function modifier_timestamp( $raw_value, $matches ) {
134 
135  if( empty( $matches[0] ) ) {
136  return $raw_value;
137  }
138 
139  $timestamp = strtotime( $raw_value );
140 
141  // Can return false or -1, depending on PHP version.
142  return ( $timestamp && $timestamp > 0 ) ? $timestamp : -1;
143  }
144 
145  /**
146  * Trim the Merge Tag's length in words.
147  *
148  * Notes:
149  * - HTML tags are preserved
150  * - HTML entities are encoded, but if they are separated by word breaks, they will be counted as words
151  * Example: "one & two" will be counted as three words, but "one& two" will be counted as two words
152  *
153  * @since 1.17
154  * @since 2.0 Added $field param and support for urlencode
155  *
156  * @param string $raw_value Value to filter
157  * @param array $matches Regex matches group
158  * @param GF_Field|false $field
159  *
160  * @return string Modified value, if longer than the passed `maxwords` modifier
161  */
162  private static function modifier_maxwords( $raw_value, $matches, $field = null ) {
163 
164  if( ! is_string( $raw_value ) || empty( $matches[1] ) || ! function_exists( 'wp_trim_words' ) ) {
165  return $raw_value;
166  }
167 
168  $max = intval( $matches[1] );
169 
170  $more_placeholder = '[GVMORE]';
171 
172  /**
173  * Use htmlentities instead, so that entities are double-encoded, and decoding restores original values.
174  * @see https://core.trac.wordpress.org/ticket/29533#comment:3
175  */
176  $return = force_balance_tags( wp_specialchars_decode( wp_trim_words( htmlentities( $raw_value ), $max, $more_placeholder ) ) );
177 
178  $return = str_replace( $more_placeholder, '&hellip;', $return );
179 
180  return self::maybe_urlencode( $field, $return );
181  }
182 
183  /**
184  * GF 2.3 adds GF_Field::get_modifers(), which allows us to check if a field has urlencode applied to it
185  *
186  * @since 2.0
187  *
188  * @see GFCommon::replace_field_variable
189  *
190  * Here's the relevant code:
191  *
192  * <code>
193  * $modifier = strtolower( rgar( $match, $i ) );
194  * $modifiers = array_map( 'trim', explode( ',', $modifier ) );
195  * $field->set_modifiers( $modifiers );
196  * </code>
197  *
198  * @param GF_Field|false $field
199  * @param string $value
200  *
201  * @return mixed|string
202  */
203  private static function maybe_urlencode( $field = false, $value = '' ) {
204 
205  $return = $value;
206 
207  if ( $field && method_exists( $field, 'get_modifiers' ) ) {
208 
209  $modifiers = $field->get_modifiers();
210 
211  if ( in_array( 'urlencode', $modifiers ) ) {
212  $return = urlencode( $return );
213  }
214  }
215 
216  return $return;
217  }
218 
219 
220  /**
221  * Convert JSON or CSV values into space-separated string
222  *
223  * Useful for Multiple Select field data, like categories
224  *
225  * @since 2.0
226  *
227  * @param mixed $raw_value The raw value submitted for this field. May be CSV or JSON-encoded.
228  * @param array $matches Regex matches group
229  * @param string $value The value as passed by Gravity Forms
230  * @param GF_Field|false $field Gravity Forms field, if any
231  *
232  * @return string
233  */
234  private static function modifier_explode( $raw_value, $matches, $value, $field = null ) {
235 
236  // For JSON-encoded arrays
237  if( $json_array = json_decode( $raw_value, true ) ) {
238  return implode( ' ', $json_array );
239  }
240 
241  return implode( ' ', explode( ',', $raw_value ) );
242  }
243 
244  /**
245  * Process strings with common PHP string manipulations
246  *
247  * @since 2.0
248  *
249  * @param mixed $raw_value The raw value submitted for this field. May be CSV or JSON-encoded.
250  * @param array $matches Regex matches group
251  * @param string $value The value as passed by Gravity Forms
252  * @param GF_Field|false $field Gravity Forms field, if any
253  *
254  * @return string
255  */
256  private static function modifier_strings( $raw_value, $matches, $value = '', $field = null ) {
257 
258  if( empty( $matches[0] ) ) {
259  return $raw_value;
260  }
261 
262  $return = $raw_value;
263 
264  switch( $matches[0] ) {
265  case 'urlencode':
266  $return = urlencode( $raw_value );
267  break;
268  case 'wpautop':
269  $return = trim( wpautop( $raw_value ) );
270  break;
271  case 'esc_html':
272  $return = esc_html( $raw_value );
273  break;
274  case 'sanitize_html_class':
275  $return = function_exists( 'gravityview_sanitize_html_class' ) ? gravityview_sanitize_html_class( $raw_value ) : sanitize_html_class( $raw_value );
276  break;
277  case 'sanitize_title':
278  $return = sanitize_title( $raw_value, '', 'gravityview/merge-tags/modifier' );
279  break;
280  case 'strtoupper':
281  $return = function_exists( 'mb_strtoupper' ) ? mb_strtoupper( $raw_value ) : strtoupper( $raw_value );
282  break;
283  case 'strtolower':
284  $return = function_exists( 'mb_strtolower' ) ? mb_strtolower( $raw_value ) : strtolower( $raw_value );
285  break;
286  case 'ucwords':
287  $return = ucwords( $raw_value );
288  break;
289  case 'ucfirst':
290  $return = ucfirst( $raw_value );
291  break;
292  case 'wptexturize':
293  $return = wptexturize( $raw_value );
294  break;
295  }
296 
297  return $return;
298  }
299 
300  /**
301  * Alias for GFCommon::replace_variables()
302  *
303  * Before 1.15.3, it would check for merge tags before passing to Gravity Forms to improve speed.
304  *
305  * @since 1.15.3 - Now that Gravity Forms added speed improvements in 1.9.15, it's more of an alias with a filter
306  * to disable or enable replacements.
307  * @since 1.8.4 - Moved to GravityView_Merge_Tags
308  * @since 1.15.1 - Add support for $url_encode and $esc_html arguments
309  * @since 1.22.4 - Added $nl2br, $format, $aux_data args
310  *
311  * @param string $text Text to replace variables in.
312  * @param array $form GF Form array
313  * @param array $entry GF Entry array
314  * @param bool $url_encode Pass return value through `url_encode()`
315  * @param bool $esc_html Pass return value through `esc_html()`
316  * @param bool $nl2br Convert newlines to <br> HTML tags
317  * @param string $format The format requested for the location the merge is being used. Possible values: html, text or url.
318  * @param array $aux_data Additional data to be used to replace merge tags {@see https://www.gravityhelp.com/documentation/article/gform_merge_tag_data/}
319  * @return string Text with variables maybe replaced
320  */
321  public static function replace_variables( $text, $form = array(), $entry = array(), $url_encode = false, $esc_html = true, $nl2br = true, $format = 'html', $aux_data = array() ) {
322 
323  if ( ! is_string( $text ) ) {
324  gravityview()->log->notice( '$text is not a string.', array( 'data' => $text ) );
325  return $text;
326  }
327 
328  /**
329  * @filter `gravityview_do_replace_variables` Turn off merge tag variable replacements.\n
330  * Useful where you want to process variables yourself. We do this in the Math Extension.
331  * @since 1.13
332  *
333  * @param boolean $do_replace_variables True: yes, replace variables for this text; False: do not replace variables.
334  * @param string $text Text to replace variables in
335  * @param array $form GF Form array
336  * @param array $entry GF Entry array
337  */
338  $do_replace_variables = apply_filters( 'gravityview/merge_tags/do_replace_variables', true, $text, $form, $entry );
339 
340  if ( strpos( $text, '{' ) === false || ! $do_replace_variables ) {
341  return $text;
342  }
343 
344  /**
345  * Make sure the required keys are set for GFCommon::replace_variables
346  *
347  * @internal Reported to GF Support on 12/3/2016
348  * @internal Fixed $form['title'] in Gravity Forms
349  * @see https://github.com/gravityforms/gravityforms/pull/27/files
350  */
351  $form['title'] = isset( $form['title'] ) ? $form['title'] : '';
352  $form['id'] = isset( $form['id'] ) ? $form['id'] : '';
353  $form['fields'] = isset( $form['fields'] ) ? $form['fields'] : array();
354 
355  return GFCommon::replace_variables( $text, $form, $entry, $url_encode, $esc_html, $nl2br, $format, $aux_data );
356  }
357 
358  /**
359  * Run GravityView filters when using GFCommon::replace_variables()
360  *
361  * Instead of adding multiple hooks, add all hooks into this one method to improve speed
362  *
363  * @since 1.8.4
364  *
365  * @param string $text Text to replace
366  * @param array|bool $form Gravity Forms form array. When called inside {@see GFCommon::replace_variables()} (now deprecated), `false`
367  * @param array|bool $entry Entry array. When called inside {@see GFCommon::replace_variables()} (now deprecated), `false`
368  * @param bool $url_encode Whether to URL-encode output
369  * @param bool $esc_html Whether to apply `esc_html()` to output
370  *
371  * @return mixed
372  */
373  public static function replace_gv_merge_tags( $text, $form = array(), $entry = array(), $url_encode = false, $esc_html = false ) {
374 
375  if ( '' === $text ) {
376  return $text;
377  }
378 
379  /**
380  * This prevents the gform_replace_merge_tags filter from being called twice, as defined in:
381  * @see GFCommon::replace_variables()
382  * @see GFCommon::replace_variables_prepopulate()
383  * @todo Remove eventually: Gravity Forms fixed this issue in 1.9.14
384  */
385  if ( false === $form ) {
386  return $text;
387  }
388 
389  $text = self::replace_is_starred( $text, $form, $entry, $url_encode, $esc_html );
390 
391  $text = self::replace_site_url( $text, $form, $entry, $url_encode, $esc_html );
392 
393  $text = self::replace_get_variables( $text, $form, $entry, $url_encode );
394 
395  $text = self::replace_current_post( $text, $form, $entry, $url_encode, $esc_html );
396 
397  $text = self::replace_entry_link( $text, $form, $entry, $url_encode, $esc_html );
398 
399  return $text;
400  }
401 
402  /**
403  * Add a {is_starred} Merge Tag
404  *
405  * @since 2.14
406  *
407  * @param string $original_text Text to replace
408  * @param array $form Gravity Forms form array
409  * @param array $entry Entry array
410  * @param bool $url_encode Whether to URL-encode output
411  * @param bool $esc_html Indicates if the esc_html function should be applied.
412  *
413  * @return string Original text, if no {site_url} Merge Tags found, otherwise text with Merge Tag replaced
414  */
415  public static function replace_is_starred( $original_text, $form = array(), $entry = array(), $url_encode = false, $esc_html = false ) {
416 
417  if ( false === strpos( $original_text, '{is_starred}' ) ) {
418  return $original_text;
419  }
420 
421  return str_replace( '{is_starred}', rgar( $entry, 'is_starred', '' ), $original_text );
422  }
423 
424  /**
425  * Add a {site_url} Merge Tag
426  *
427  * @since 2.10.1
428  *
429  * @param string $original_text Text to replace
430  * @param array $form Gravity Forms form array
431  * @param array $entry Entry array
432  * @param bool $url_encode Whether to URL-encode output
433  * @param bool $esc_html Indicates if the esc_html function should be applied.
434  *
435  * @return string Original text, if no {site_url} Merge Tags found, otherwise text with Merge Tag replaced
436  */
437  public static function replace_site_url( $original_text, $form = array(), $entry = array(), $url_encode = false, $esc_html = false ) {
438 
439  if ( false === strpos( $original_text, '{site_url}' ) ) {
440  return $original_text;
441  }
442 
443  $site_url = get_site_url();
444 
445  if( $url_encode ) {
446  $site_url = urlencode( $site_url );
447  }
448 
449  if ( $esc_html ) {
450  $site_url = esc_html( $site_url );
451  }
452 
453  return str_replace( '{site_url}', $site_url, $original_text );
454  }
455 
456  /**
457  * Add a {gv_entry_link} Merge Tag, alias of [gv_entry_link] shortcode in {gv_entry_link:[post id]:[action]} format
458  *
459  * @param string $original_text Text to replace
460  * @param array $form Gravity Forms form array
461  * @param array $entry Entry array
462  * @param bool $url_encode Whether to URL-encode output
463  * @param bool $esc_html Indicates if the esc_html function should be applied.
464  *
465  * @return string Original text, if no {gv_entry_link} Merge Tags found, otherwise text with Merge Tags replaced
466  */
467  public static function replace_entry_link( $original_text, $form = array(), $entry = array(), $url_encode = false, $esc_html = false ) {
468 
469  // Is there is {gv_entry_link} or {gv_entry_link:[post id]} or {gv_entry_link:[post id]:[action]} merge tag?
470  preg_match_all( "/{gv_entry_link(?:\:(\d+)\:?(.*?))?}/ism", $original_text, $matches, PREG_SET_ORDER );
471 
472  if( empty( $matches ) ) {
473  return $original_text;
474  }
475 
476  if ( ! class_exists( 'GravityView_Entry_Link_Shortcode' ) ) {
477  gravityview()->log->error( 'GravityView_Entry_Link_Shortcode not found' );
478  return $original_text;
479  }
480 
481  $Shortcode = new GravityView_Entry_Link_Shortcode;
482 
483  $return = $original_text;
484 
485  /**
486  * @param array $match {
487  * $match[0] Full tag
488  * $match[1] Post ID (optional)
489  * $match[2] Action (optional)
490  * }
491  */
492  foreach ( $matches as $match ) {
493  $full_tag = $match[0];
494 
495  $link_args = array(
496  'return' => 'url',
497  'entry_id' => $entry['id'],
498  'post_id' => \GV\Utils::get( $match, 1, null ),
499  'action' => \GV\Utils::get( $match, 2, 'read' ),
500  );
501 
502  $entry_link = $Shortcode->read_shortcode( $link_args, null, 'gv_entry_link_merge_tag' );
503 
504  if( $url_encode ) {
505  $entry_link = urlencode( $entry_link );
506  }
507 
508  if ( $esc_html ) {
509  $entry_link = esc_html( $entry_link );
510  }
511 
512  $return = str_replace( $full_tag, $entry_link, $return );
513  }
514 
515  return $return;
516  }
517 
518  /**
519  * Format Merge Tags using GVCommon::format_date()
520  *
521  * @uses GVCommon::format_date()
522  *
523  * @see https://docs.gravityview.co/article/331-date-created-merge-tag for documentation
524  * @todo Once Gravity Forms 2.5 becomes the minimum requirement, this is no longer needed.
525  *
526  * @param string $date_created The Gravity Forms date created format
527  * @param string $property Any modifiers for the merge tag (`human`, `format:m/d/Y`)
528  *
529  * @return int|string If timestamp requested, timestamp int. Otherwise, string output.
530  */
531  public static function format_date( $date_created = '', $property = '' ) {
532 
533  // Expand all modifiers, skipping escaped colons. str_replace worked better than preg_split( "/(?<!\\):/" )
534  $exploded = explode( ':', str_replace( '\:', '|COLON|', $property ) );
535 
536  $atts = array(
537  'format' => self::get_format_from_modifiers( $exploded, false ),
538  'human' => in_array( 'human', $exploded ), // {date_created:human}
539  'diff' => in_array( 'diff', $exploded ), // {date_created:diff}
540  'raw' => in_array( 'raw', $exploded ), // {date_created:raw}
541  'timestamp' => in_array( 'timestamp', $exploded ), // {date_created:timestamp}
542  'time' => in_array( 'time', $exploded ), // {date_created:time}
543  );
544 
545  $formatted_date = GVCommon::format_date( $date_created, $atts );
546 
547  return $formatted_date;
548  }
549 
550  /**
551  * If there is a `:format` modifier in a merge tag, grab the formatting
552  *
553  * The `:format` modifier should always have the format follow it; it's the next item in the array
554  * In `foo:format:bar`, "bar" will be the returned format
555  *
556  * @since 1.16
557  *
558  * @param array $exploded Array of modifiers with a possible `format` value
559  * @param string $backup The backup value to use, if not found
560  *
561  * @return string If format is found, the passed format. Otherwise, the backup.
562  */
563  private static function get_format_from_modifiers( $exploded, $backup = '' ) {
564 
565  $return = $backup;
566 
567  $format_key_index = array_search( 'format', $exploded );
568 
569  // If there's a "format:[php date format string]" date format, grab it
570  if ( false !== $format_key_index && isset( $exploded[ $format_key_index + 1 ] ) ) {
571  // Return escaped colons placeholder
572  $return = str_replace( '|COLON|', ':', $exploded[ $format_key_index + 1 ] );
573  }
574 
575  return $return;
576  }
577 
578  /**
579  * Add a {current_post} Merge Tag for information about the current post (in the loop or singular)
580  *
581  * {current_post} is replaced with the current post's permalink by default, when no modifiers are passed.
582  * Pass WP_Post properties as :modifiers to access.
583  *
584  * {current_post} is the same as {embed_post}, except:
585  *
586  * - Adds support for {current_post:permalink}
587  * - Works on post archives, as well as singular
588  *
589  * @see https://www.gravityhelp.com/documentation/article/merge-tags/#embed-post for examples
590  * @see GFCommon::replace_variables_prepopulate - Code is there for {custom_field} and {embed_post} Merge Tags
591  *
592  * @param string $original_text Text to replace
593  * @param array $form Gravity Forms form array
594  * @param array $entry Entry array
595  * @param bool $url_encode Whether to URL-encode output
596  * @param bool $esc_html Indicates if the esc_html function should be applied.
597  *
598  * @return string Original text, if no {current_post} Merge Tags found, otherwise text with Merge Tags replaced
599  */
600  public static function replace_current_post( $original_text, $form = array(), $entry = array(), $url_encode = false, $esc_html = false ) {
601 
602  $return = $original_text;
603 
604  // Is there a {current_post} or {current_post:[xyz]} merge tag?
605  preg_match_all( "/{current_post(:(.*?))?}/ism", $original_text, $matches, PREG_SET_ORDER );
606 
607  // If there are no matches OR the Entry `created_by` isn't set or is 0 (no user)
608  if ( empty( $matches ) ) {
609  return $original_text;
610  }
611 
612  $current_post = get_post();
613 
614  // WP_Error, arrays and NULL aren't welcome here.
615  if ( ! $current_post || ! is_a( $current_post, 'WP_Post' ) ) {
616  return $original_text;
617  }
618 
619  foreach ( (array) $matches as $match ) {
620  $full_tag = $match[0];
621  $modifier = \GV\Utils::get( $match, 2, 'permalink' );
622 
623  $replacement = false;
624 
625  if ( 'permalink' === $modifier ) {
626  $replacement = get_permalink( $current_post );
627  } elseif ( isset( $current_post->{$modifier} ) ) {
628  /** @see WP_Post Post properties */
629  $replacement = $current_post->{$modifier};
630  }
631 
632  if ( $replacement ) {
633 
634  if ( $esc_html ) {
635  $replacement = esc_html( $replacement );
636  }
637 
638  if( $url_encode ) {
639  $replacement = urlencode( $replacement );
640  }
641 
642  $return = str_replace( $full_tag, $replacement, $return );
643  }
644  }
645 
646  return $return;
647  }
648 
649  /**
650  * Allow passing variables via URL to be displayed in Merge Tags
651  *
652  * Works with `[gvlogic]`:
653  * [gvlogic if="{get:example}" is="false"]
654  * ?example=false
655  * [else]
656  * ?example wasn't "false". It's {get:example}!
657  * [/gvlogic]
658  *
659  * Supports passing arrays:
660  * URL: `example[]=Example+One&example[]=Example+(with+comma)%2C+Two`
661  * Merge Tag: `{get:example}`
662  * Output: `Example One, Example (with comma), Two`
663  *
664  * @since 1.15
665  * @param string $text Text to replace
666  * @param array $form Gravity Forms form array
667  * @param array $entry Entry array
668  * @param bool $url_encode Whether to URL-encode output
669  *
670  * @return string Original text, if no Merge Tags found, otherwise text with Merge Tags replaced
671  */
672  public static function replace_get_variables( $text, $form = array(), $entry = array(), $url_encode = false ) {
673 
674  // Is there is {get:[xyz]} merge tag?
675  preg_match_all( "/{get:(.*?)}/ism", $text, $matches, PREG_SET_ORDER );
676 
677  // If there are no matches OR the Entry `created_by` isn't set or is 0 (no user)
678  if( empty( $matches ) ) {
679  return $text;
680  }
681 
682  foreach ( $matches as $match ) {
683 
684  $full_tag = $match[0];
685  $property = $match[1];
686 
687  $value = stripslashes_deep( \GV\Utils::_GET( $property ) );
688 
689  /**
690  * @filter `gravityview/merge_tags/get/glue/` Modify the glue used to convert an array of `{get}` values from an array to string
691  * @since 1.15
692  * @param string $glue String used to `implode()` $_GET values Default: ', '
693  * @param string $property The current name of the $_GET parameter being combined
694  */
695  $glue = apply_filters( 'gravityview/merge_tags/get/glue/', ', ', $property );
696 
697  $value = is_array( $value ) ? implode( $glue, $value ) : $value;
698 
699  $value = $url_encode ? urlencode( $value ) : $value;
700 
701  /**
702  * @filter `gravityview/merge_tags/get/esc_html/{url parameter name}` Disable esc_html() from running on `{get}` merge tag
703  * By default, all values passed through URLs will be escaped for security reasons. If for some reason you want to
704  * pass HTML in the URL, for example, you will need to return false on this filter. It is strongly recommended that you do
705  * not disable this filter.
706  * @since 1.15
707  * @param bool $esc_html Whether to esc_html() the value. Default: `true`
708  */
709  $esc_html = apply_filters('gravityview/merge_tags/get/esc_html/' . $property, true );
710 
711  $value = $esc_html ? esc_html( $value ) : $value;
712 
713  /**
714  * @filter `gravityview/merge_tags/get/esc_html/{url parameter name}` Modify the value of the `{get}` replacement before being used
715  * @param string $value Value that will replace `{get}`
716  * @param string $text Text that contains `{get}` (before replacement)
717  * @param array $form Gravity Forms form array
718  * @param array $entry Entry array
719  */
720  $value = apply_filters('gravityview/merge_tags/get/value/' . $property, $value, $text, $form, $entry );
721 
722  $text = str_replace( $full_tag, $value, $text );
723  }
724 
725  unset( $value, $glue, $matches );
726 
727  return $text;
728  }
729 }
730 
static replace_current_post( $original_text, $form=array(), $entry=array(), $url_encode=false, $esc_html=false)
Add a {current_post} Merge Tag for information about the current post (in the loop or singular) ...
static replace_gv_merge_tags( $text, $form=array(), $entry=array(), $url_encode=false, $esc_html=false)
Run GravityView filters when using GFCommon::replace_variables()
static replace_is_starred( $original_text, $form=array(), $entry=array(), $url_encode=false, $esc_html=false)
Add a {is_starred} Merge Tag.
static get_format_from_modifiers( $exploded, $backup='')
If there is a :format modifier in a merge tag, grab the formatting.
if(gv_empty( $field['value'], false, false)) $format
Enhance Gravity Forms&#39; merge tag functionality by adding additional merge tags.
static modifier_strings( $raw_value, $matches, $value='', $field=null)
Process strings with common PHP string manipulations.
static replace_get_variables( $text, $form=array(), $entry=array(), $url_encode=false)
Allow passing variables via URL to be displayed in Merge Tags.
static replace_variables( $text, $form=array(), $entry=array(), $url_encode=false, $esc_html=true, $nl2br=true, $format='html', $aux_data=array())
Alias for GFCommon::replace_variables()
static maybe_urlencode( $field=false, $value='')
GF 2.3 adds GF_Field::get_modifers(), which allows us to check if a field has urlencode applied to it...
static replace_site_url( $original_text, $form=array(), $entry=array(), $url_encode=false, $esc_html=false)
Add a {site_url} Merge Tag.
if(gravityview() ->plugin->is_GF_25()) $form
static modifier_maxwords( $raw_value, $matches, $field=null)
Trim the Merge Tag&#39;s length in words.
static format_date( $date_string='', $args=array())
Allow formatting date and time based on GravityView standards.
static modifier_timestamp( $raw_value, $matches)
Convert Date field values to timestamp int.
static format_date( $date_created='', $property='')
Format Merge Tags using GVCommon::format_date()
static process_modifiers( $value, $merge_tag, $modifier, $field, $raw_value)
Process custom GravityView modifiers for Merge Tags.
static get( $array, $key, $default=null)
Grab a value from an array or an object or default.
gravityview()
The main GravityView wrapper function.
$entry
Definition: notes.php:27
static replace_entry_link( $original_text, $form=array(), $entry=array(), $url_encode=false, $esc_html=false)
Add a {gv_entry_link} Merge Tag, alias of [gv_entry_link] shortcode in {gv_entry_link:[post id]:[acti...
add_hooks()
Tap in to gform_replace_merge_tags to add merge tags.
static modifier_explode( $raw_value, $matches, $value, $field=null)
Convert JSON or CSV values into space-separated string.
new GravityView_Entry_Link_Shortcode