1
|
|
|
<?php |
|
|
|
|
2
|
|
|
namespace GV\Widgets; |
3
|
|
|
/** |
4
|
|
|
* Widget to display page size |
5
|
|
|
* |
6
|
|
|
* @since 2.1 |
7
|
|
|
* |
8
|
|
|
* @extends GV\Widget |
9
|
|
|
*/ |
10
|
|
|
class Page_Size extends \GV\Widget { |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Does this get displayed on a single entry? |
14
|
|
|
* @var boolean |
15
|
|
|
*/ |
16
|
|
|
protected $show_on_single = false; |
17
|
|
|
|
18
|
3 |
|
function __construct() { |
|
|
|
|
19
|
|
|
|
20
|
3 |
|
$this->widget_description = __( 'Allow users to modify the number of results shown per page.', 'gravityview' ); |
21
|
|
|
|
22
|
|
|
$default_values = array( |
23
|
3 |
|
'header' => 1, |
24
|
|
|
'footer' => 1, |
25
|
|
|
); |
26
|
|
|
|
27
|
3 |
|
$settings = array(); |
28
|
|
|
|
29
|
3 |
|
if ( ! $this->is_registered() ) { |
30
|
3 |
|
add_action( 'gravityview/view/get', array( $this, 'override_view_page_size' ) ); |
31
|
|
|
} |
32
|
|
|
|
33
|
3 |
|
parent::__construct( __( 'Page Size', 'gravityview' ), 'page_size', $default_values, $settings ); |
34
|
3 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Get an array of page sizes. |
38
|
|
|
* |
39
|
|
|
* @param \GV\Template_Context|string $context The context, if available |
40
|
|
|
* |
41
|
|
|
* @return array The page sizes in an array with `value` and `text` keys. |
42
|
|
|
*/ |
43
|
3 |
|
public static function get_page_sizes( $context ) { |
44
|
|
|
|
45
|
3 |
|
$default_size = 25; |
46
|
|
|
|
47
|
3 |
|
if ( $context instanceof \GV\Template_Context ) { |
48
|
3 |
|
$default_size = (int) $context->view->settings->get( 'page_size' ); |
49
|
|
|
} |
50
|
|
|
|
51
|
3 |
|
$sizes = array( 10, 25, $default_size, 50, 100 ); |
52
|
|
|
|
53
|
3 |
|
$sizes = array_unique( array_filter( $sizes ) ); |
54
|
|
|
|
55
|
3 |
|
sort( $sizes ); |
56
|
|
|
|
57
|
3 |
|
$page_sizes = array(); |
58
|
3 |
|
foreach ( $sizes as $size ) { |
59
|
3 |
|
$page_sizes [] = array( |
60
|
3 |
|
'value' => $size, |
61
|
3 |
|
'text' => $size |
|
|
|
|
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @filter `gravityview/widget/page_size/page_sizes` Filter the available page sizes as needed |
67
|
|
|
* @param[in,out] array $sizes The sizes, with `value` and `text` keys. `text` key used as HTML option label. |
68
|
|
|
* @param \GV\Template_Context $context The context. |
69
|
|
|
*/ |
70
|
3 |
|
$page_sizes = apply_filters( 'gravityview/widget/page_size/page_sizes', $page_sizes, $context ); |
71
|
|
|
|
72
|
3 |
|
return $page_sizes; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Render the page size widget |
77
|
|
|
* |
78
|
|
|
* @param array $widget_args The Widget shortcode args. |
79
|
|
|
* @param string $content The content. |
80
|
|
|
* @param string|\GV\Template_Context $context The context, if available. |
81
|
|
|
*/ |
82
|
2 |
|
public function render_frontend( $widget_args, $content = '', $context = null ) { |
83
|
|
|
|
84
|
2 |
|
if( ! $this->pre_render_frontend() ) { |
85
|
|
|
return; |
86
|
|
|
} |
87
|
|
|
|
88
|
2 |
|
$page_size = (int) \GV\Utils::_GET( 'page_size', $context->view->settings->get( 'page_size' ) ); |
89
|
|
|
|
90
|
2 |
|
$settings = shortcode_atts( array( |
91
|
2 |
|
'label' => __( 'Page Size', 'gravityview' ), |
92
|
2 |
|
'choices' => self::get_page_sizes( $context ), |
93
|
2 |
|
'default_choice_text' => __( 'Results Per Page', 'gravityview' ), |
94
|
2 |
|
), $widget_args, 'gravityview_widget_page_size' ); |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @filter `gravityview/widget/page_size/settings` Filter the settings for the widget |
98
|
|
|
* @param array $settings Configuration for how output will display, with `label`, `choices`, `default_choice_text` keys |
99
|
|
|
* @param \GV\Template_Context $context The context. |
100
|
|
|
*/ |
101
|
2 |
|
$settings = apply_filters( 'gravityview/widget/page_size/settings', $settings, $context ); |
102
|
|
|
|
103
|
|
|
?> |
104
|
2 |
|
<div class="gv-widget-page-size"> |
105
|
|
|
<form method="get" action="<?php echo esc_url( add_query_arg( array() ) ); ?>" onchange="this.submit();"> |
106
|
|
|
<div> |
107
|
|
|
<?php if( ! empty( $settings['label'] ) ) { ?> |
108
|
|
|
<label for="gv-page_size"><?php echo esc_html( $settings['label'] ); ?></label> |
109
|
|
|
<?php } ?> |
110
|
2 |
|
<select name="page_size" id="gv-page_size"> |
111
|
|
|
<option value=""><?php echo esc_html( $settings['default_choice_text'] ); ?></option> |
112
|
|
|
<?php |
113
|
|
|
foreach ( $settings['choices'] as $choice ) { ?> |
114
|
|
|
<option value='<?php echo esc_attr( $choice['value'] ); ?>'<?php gv_selected( esc_attr( $choice['value'] ), esc_attr( $page_size ), true ); ?>><?php echo esc_html( $choice['text'] ); ?></option> |
115
|
|
|
<?php } ?> |
116
|
2 |
|
</select> |
117
|
|
|
<input type="submit" value="Submit" style="visibility: hidden; position: absolute;" /><?php |
118
|
2 |
|
if( ! empty( $_GET ) ) { |
119
|
2 |
|
$get = $_GET; |
|
|
|
|
120
|
2 |
|
unset( $get['page_size'] ); |
121
|
2 |
|
foreach ( $get as $key => $value ) { |
122
|
1 |
|
printf( '<input type="hidden" name="%s" value="%s" />', esc_attr( $key ), esc_attr( $value ) ); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
?> |
126
|
2 |
|
</div> |
127
|
|
|
</form> |
128
|
|
|
</div> |
129
|
|
|
<?php |
130
|
2 |
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Override the View settings and inject the needed page size. |
134
|
|
|
* |
135
|
|
|
* This might be too early, seeing that there's lack of full context, but we should |
136
|
|
|
* be fine for now. |
137
|
|
|
* |
138
|
|
|
* @param \GV\View $view The View. |
139
|
|
|
*/ |
140
|
86 |
|
public function override_view_page_size( &$view ) { |
141
|
|
|
|
142
|
86 |
|
if ( ! $view->widgets->by_id( 'page_size' )->count() ) { |
143
|
83 |
|
return; |
144
|
|
|
} |
145
|
|
|
|
146
|
3 |
|
$page_size = \GV\Utils::_GET( 'page_size' ); |
147
|
|
|
|
148
|
3 |
|
if ( empty( $page_size ) ) { |
149
|
3 |
|
return; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
// Already overridden |
153
|
2 |
|
if ( (int) $page_size === (int) $view->settings->get( 'page_size' ) ) { |
154
|
2 |
|
return; |
155
|
|
|
} |
156
|
|
|
|
157
|
2 |
|
$context = \GV\Template_Context::from_template( array( |
158
|
2 |
|
'view' => $view, |
159
|
|
|
) ); |
160
|
|
|
|
161
|
2 |
|
if ( ! in_array( (int) $page_size, wp_list_pluck( self::get_page_sizes( $context ), 'value' ), true ) ) { |
162
|
|
|
gravityview()->log->warning( 'The passed page size is not allowed: {page_size}. Not modifying result.', array( 'page_size' => $page_size ) ); |
163
|
|
|
return; |
164
|
|
|
} |
165
|
|
|
|
166
|
2 |
|
$view->settings->update( array( 'page_size' => $page_size ) ); |
167
|
2 |
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
new Page_Size; |
171
|
|
|
|
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.