GravityView  2.17
The best, easiest way to display Gravity Forms entries on your website.
class-gravityview-shortcode.php
Go to the documentation of this file.
1 <?php
2 /**
3  * [gravityview] Shortcode class
4  *
5  * @package GravityView
6  * @license GPL2+
7  * @author GravityView <[email protected]>
8  * @link http://gravityview.co
9  * @copyright Copyright 2015, Katz Web Services, Inc.
10  *
11  * @since 1.13
12  */
13 
14 /**
15  * Handle the [gravityview] shortcode
16  *
17  * @since 1.13
18  * @deprecated since 2.0.3
19  * @see \GV\Shortcodes\gravityview
20  */
22 
23  function __construct() {
24  $this->add_hooks();
25  }
26 
27  private function add_hooks() {
28  }
29 
30  /**
31  * Callback function for add_shortcode()
32  *
33  * @since 1.13
34  *
35  * @static
36  * @param mixed $passed_atts
37  * @param string|null $content Content passed inside the shortcode
38  * @return null|string If admin, null. Otherwise, output of $this->render_view()
39  */
40  function shortcode( $passed_atts, $content = null ) {
41 
42  // Don't process when saving post.
43  if ( is_admin() ) {
44  return null;
45  }
46 
47  gravityview()->log->debug( '$passed_atts: ', array( 'data' => $passed_atts ) );
48 
49  // Get details about the current View
50  if( !empty( $passed_atts['detail'] ) ) {
51  return $this->get_view_detail( $passed_atts['detail'] );
52  }
53 
54  $atts = $this->parse_and_sanitize_atts( $passed_atts );
55 
56  return GravityView_frontend::getInstance()->render_view( $atts );
57  }
58 
59  /**
60  * Validate attributes passed to the [gravityview] shortcode. Supports {get} Merge Tags values.
61  *
62  * Attributes passed to the shortcode are compared to registered attributes {@see \GV\View_Settings::defaults}
63  * Only attributes that are defined will be allowed through.
64  *
65  * Then, {get} merge tags are replaced with their $_GET values, if passed
66  *
67  * Then, attributes are sanitized based on the type of setting (number, checkbox, select, radio, text)
68  *
69  * @since 1.15.1
70  *
71  * @see \GV\View_Settings::defaults() Only attributes defined in default() are valid to be passed via the shortcode
72  *
73  * @param array $passed_atts Attribute pairs defined to render the View
74  *
75  * @return array Valid and sanitized attribute pairs
76  */
77  private function parse_and_sanitize_atts( $passed_atts ) {
78 
79  $defaults = \GV\View_Settings::defaults( true );
80 
81  $supported_atts = array_fill_keys( array_keys( $defaults ), '' );
82 
83  // Whittle down the attributes to only valid pairs
84  $filtered_atts = shortcode_atts( $supported_atts, $passed_atts, 'gravityview' );
85 
86  // Only keep the passed attributes after making sure that they're valid pairs
87  $filtered_atts = array_intersect_key( (array) $passed_atts, $filtered_atts );
88 
89  $atts = array();
90 
91  foreach( $filtered_atts as $key => $passed_value ) {
92 
93  // Allow using GravityView merge tags in shortcode attributes, like {get} and {created_by}
94  $passed_value = GravityView_Merge_Tags::replace_variables( $passed_value );
95 
96  switch( $defaults[ $key ]['type'] ) {
97 
98  /**
99  * Make sure number fields are numeric.
100  * Also, convert mixed number strings to numbers
101  * @see http://php.net/manual/en/function.is-numeric.php#107326
102  */
103  case 'number':
104  if( is_numeric( $passed_value ) ) {
105  $atts[ $key ] = ( $passed_value + 0 );
106  }
107  break;
108 
109  /** @since 2.1 */
110  case 'operator':
111  if( GFFormsModel::is_valid_operator( $passed_value ) ) {
112  $atts[ $key ] = $passed_value;
113  }
114  break;
115 
116  // Checkboxes should be 1 or 0
117  case 'checkbox':
118  $atts[ $key ] = gv_empty( $passed_value, true, false ) ? 0 : 1;
119  break;
120 
121  /**
122  * Only allow values that are defined in the settings
123  */
124  case 'select':
125  case 'radio':
126  $options = isset( $defaults[ $key ]['choices'] ) ? $defaults[ $key ]['choices'] : $defaults[ $key ]['options'];
127  if( in_array( $passed_value, array_keys( $options ) ) ) {
128  $atts[ $key ] = $passed_value;
129  }
130  break;
131 
132  case 'text':
133  default:
134  $atts[ $key ] = $passed_value;
135  break;
136  }
137  }
138 
139  return $atts;
140  }
141 
142  /**
143  * Display details for the current View
144  *
145  * @since 1.13
146  *
147  * @param string $detail The information requested about the current View. Accepts `total_entries`, `first_entry` (entry #), `last_entry` (entry #), and `page_size`
148  *
149  * @return string Detail information
150  */
151  private function get_view_detail( $detail = '' ) {
152 
154  $return = '';
155 
156  switch( $detail ) {
157  case 'total_entries':
158  $return = number_format_i18n( $gravityview_view->getTotalEntries() );
159  break;
160  case 'first_entry':
161  $paging = $gravityview_view->getPaginationCounts();
162  $return = empty( $paging ) ? '' : number_format_i18n( \GV\Utils::get( $paging, 'first', 0 ) );
163  break;
164  case 'last_entry':
165  $paging = $gravityview_view->getPaginationCounts();
166  $return = empty( $paging ) ? '' : number_format_i18n( \GV\Utils::get( $paging, 'last', 0 ) );
167  break;
168  case 'page_size':
169  $paging = $gravityview_view->getPaging();
170  $return = number_format_i18n( \GV\Utils::get( $paging, 'page_size', 0 ) );
171  break;
172  }
173 
174  /**
175  * @filter `gravityview/shortcode/detail/{$detail}` Filter the detail output returned from `[gravityview detail="$detail"]`
176  * @since 1.13
177  * @param string $return Existing output
178  */
179  $return = apply_filters( 'gravityview/shortcode/detail/' . $detail, $return );
180 
181  return $return;
182  }
183 }
184 
shortcode( $passed_atts, $content=null)
Callback function for add_shortcode()
static getInstance( $passed_post=NULL)
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()
get_view_detail( $detail='')
Display details for the current View.
if(empty( $field_settings['content'])) $content
Definition: custom.php:37
Handle the [gravityview] shortcode.
gravityview()
The main GravityView wrapper function.
static defaults( $detailed=false, $group=null)
Retrieve the default View settings.
gv_empty( $value, $zero_is_empty=true, $allow_string_booleans=true)
Is the value empty?
parse_and_sanitize_atts( $passed_atts)
Validate attributes passed to the [gravityview] shortcode.
static getInstance()
Get the one true instantiated self.