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, see \GV\Shortcodes\gravityview |
19
|
|
|
*/ |
20
|
|
|
class GravityView_Shortcode { |
21
|
|
|
|
22
|
|
|
function __construct() { |
|
|
|
|
23
|
|
|
$this->add_hooks(); |
|
|
|
|
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
private function add_hooks() { |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Callback function for add_shortcode() |
31
|
|
|
* |
32
|
|
|
* @since 1.13 |
33
|
|
|
* |
34
|
|
|
* @access public |
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
|
1 |
|
function shortcode( $passed_atts, $content = null ) { |
|
|
|
|
41
|
|
|
|
42
|
|
|
// Don't process when saving post. |
43
|
1 |
|
if ( is_admin() ) { |
44
|
|
|
return null; |
45
|
|
|
} |
46
|
|
|
|
47
|
1 |
|
gravityview()->log->debug( '$passed_atts: ', array( 'data' => $passed_atts ) ); |
48
|
|
|
|
49
|
|
|
// Get details about the current View |
50
|
1 |
|
if( !empty( $passed_atts['detail'] ) ) { |
|
|
|
|
51
|
1 |
|
return $this->get_view_detail( $passed_atts['detail'] ); |
52
|
|
|
} |
53
|
|
|
|
54
|
1 |
|
$atts = $this->parse_and_sanitize_atts( $passed_atts ); |
55
|
|
|
|
56
|
1 |
|
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
|
1 |
|
private function parse_and_sanitize_atts( $passed_atts ) { |
78
|
|
|
|
79
|
1 |
|
$defaults = \GV\View_Settings::defaults( true ); |
80
|
|
|
|
81
|
1 |
|
$supported_atts = array_fill_keys( array_keys( $defaults ), '' ); |
82
|
|
|
|
83
|
|
|
// Whittle down the attributes to only valid pairs |
84
|
1 |
|
$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
|
1 |
|
$filtered_atts = function_exists( 'array_intersect_key' ) ? array_intersect_key( (array) $passed_atts, $filtered_atts ) : $filtered_atts; |
88
|
|
|
|
89
|
1 |
|
$atts = array(); |
90
|
|
|
|
91
|
1 |
|
foreach( $filtered_atts as $key => $passed_value ) { |
92
|
|
|
|
93
|
|
|
// Allow using GravityView merge tags in shortcode attributes, like {get} and {created_by} |
94
|
1 |
|
$passed_value = GravityView_Merge_Tags::replace_variables( $passed_value ); |
95
|
|
|
|
96
|
1 |
|
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
|
1 |
|
case 'number': |
104
|
1 |
|
if( is_numeric( $passed_value ) ) { |
105
|
1 |
|
$atts[ $key ] = ( $passed_value + 0 ); |
106
|
|
|
} |
107
|
1 |
|
break; |
108
|
|
|
|
109
|
|
|
// Checkboxes should be 1 or 0 |
110
|
1 |
|
case 'checkbox': |
111
|
1 |
|
$atts[ $key ] = gv_empty( $passed_value, true, false ) ? 0 : 1; |
112
|
1 |
|
break; |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Only allow values that are defined in the settings |
116
|
|
|
*/ |
117
|
1 |
|
case 'select': |
118
|
1 |
|
case 'radio': |
119
|
1 |
|
$options = isset( $defaults[ $key ]['choices'] ) ? $defaults[ $key ]['choices'] : $defaults[ $key ]['options']; |
120
|
1 |
|
if( in_array( $passed_value, array_keys( $options ) ) ) { |
121
|
1 |
|
$atts[ $key ] = $passed_value; |
122
|
|
|
} |
123
|
1 |
|
break; |
124
|
|
|
|
125
|
1 |
|
case 'text': |
126
|
|
|
default: |
127
|
1 |
|
$atts[ $key ] = $passed_value; |
128
|
1 |
|
break; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
1 |
|
return $atts; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Display details for the current View |
137
|
|
|
* |
138
|
|
|
* @since 1.13 |
139
|
|
|
* |
140
|
|
|
* @param string $detail The information requested about the current View. Accepts `total_entries`, `first_entry` (entry #), `last_entry` (entry #), and `page_size` |
141
|
|
|
* |
142
|
|
|
* @return string Detail information |
143
|
|
|
*/ |
144
|
|
|
private function get_view_detail( $detail = '' ) { |
145
|
|
|
|
146
|
|
|
$gravityview_view = GravityView_View::getInstance(); |
147
|
|
|
$return = ''; |
148
|
|
|
|
149
|
|
|
switch( $detail ) { |
150
|
|
|
case 'total_entries': |
151
|
|
|
$return = number_format_i18n( $gravityview_view->getTotalEntries() ); |
152
|
|
|
break; |
153
|
|
|
case 'first_entry': |
154
|
|
|
$paging = $gravityview_view->getPaginationCounts(); |
155
|
|
|
$return = empty( $paging ) ? '' : number_format_i18n( \GV\Utils::get( $paging, 'first', 0 ) ); |
156
|
|
|
break; |
157
|
|
|
case 'last_entry': |
158
|
|
|
$paging = $gravityview_view->getPaginationCounts(); |
159
|
|
|
$return = empty( $paging ) ? '' : number_format_i18n( \GV\Utils::get( $paging, 'last', 0 ) ); |
160
|
|
|
break; |
161
|
|
|
case 'page_size': |
162
|
|
|
$paging = $gravityview_view->getPaging(); |
163
|
|
|
$return = number_format_i18n( \GV\Utils::get( $paging, 'page_size', 0 ) ); |
164
|
|
|
break; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @filter `gravityview/shortcode/detail/{$detail}` Filter the detail output returned from `[gravityview detail="$detail"]` |
169
|
|
|
* @since 1.13 |
170
|
|
|
* @param string $return Existing output |
171
|
|
|
*/ |
172
|
|
|
$return = apply_filters( 'gravityview/shortcode/detail/' . $detail, $return ); |
173
|
|
|
|
174
|
|
|
return $return; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
new GravityView_Shortcode; |
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.