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