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