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