Completed
Push — develop ( a9c20e...34aa31 )
by Gennady
28:10 queued 09:19
created

Page_Size::render_frontend()   B

Complexity

Conditions 8
Paths 9

Size

Total Lines 55

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 8.1419

Importance

Changes 0
Metric Value
cc 8
nc 9
nop 3
dl 0
loc 55
ccs 20
cts 23
cp 0.8696
crap 8.1419
rs 7.7373
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
							if ( is_array( $value ) ) {
125
								foreach ( $value as $_key => $_value ) {
126
									printf( '<input type="hidden" name="%s[%s]" value="%s" />', esc_attr( $key ), esc_attr( $_key ), esc_attr( $_value ) );
127
								}
128
							} else {
129 1
								printf( '<input type="hidden" name="%s" value="%s" />', esc_attr( $key ), esc_attr( $value ) );
130
							}
131
	                    }
132
                    }
133
                    ?>
134 2
                </div>
135
            </form>
136
        </div>
137
		<?php
138 2
	}
139
140
	/**
141
	 * Override the View settings and inject the needed page size.
142
	 *
143
	 * This might be too early, seeing that there's lack of full context, but we should
144
	 * be fine for now.
145
	 *
146
	 * @param \GV\View $view The View.
147
	 */
148 168
	public function override_view_page_size( &$view ) {
149
150 168
		if ( ! $view->widgets->by_id( 'page_size' )->count() ) {
151 165
			return;
152
		}
153
154 3
		$page_size = \GV\Utils::_GET( 'page_size' );
155
156 3
		if ( empty( $page_size ) ) {
157 3
			return;
158
		}
159
160
		// Already overridden
161 2
		if ( (int) $page_size === (int) $view->settings->get( 'page_size' ) ) {
162 1
			return;
163
		}
164
165 2
		$context = \GV\Template_Context::from_template( array(
166 2
			'view' => $view,
167
		) );
168
169 2
		if ( ! in_array( (int) $page_size, wp_list_pluck( self::get_page_sizes( $context ), 'value' ), true ) ) {
170
			gravityview()->log->warning( 'The passed page size is not allowed: {page_size}. Not modifying result.', array( 'page_size' => $page_size ) );
171
			return;
172
		}
173
174 2
		$view->settings->update( array( 'page_size' => $page_size ) );
175 2
	}
176
}
177
178
new Page_Size;
179