Completed
Push — develop ( d6ccfe...9184f8 )
by Gennady
26:52 queued 11:52
created

Page_Size   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 92.98%

Importance

Changes 0
Metric Value
dl 0
loc 161
ccs 53
cts 57
cp 0.9298
rs 10
c 0
b 0
f 0
wmc 16
lcom 1
cbo 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 2
A get_page_sizes() 0 31 3
B render_frontend() 0 49 6
A override_view_page_size() 0 28 5
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
	protected $widget_id = 'page_size';
19
20 3
	function __construct() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

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