Completed
Push — develop ( 82fa3b...268ce1 )
by Gennady
14:11
created

GravityView_Widget_Page_Links   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 35.14%

Importance

Changes 0
Metric Value
dl 0
loc 67
ccs 13
cts 37
cp 0.3514
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
B render_frontend() 0 46 5
1
<?php
2
3
/**
4
 * Widget to display page links
5
 *
6
 * @extends GravityView_Widget
7
 */
8
class GravityView_Widget_Page_Links extends \GV\Widget {
9
10
	protected $show_on_single = false;
11
12 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...
13
14 3
		$this->widget_description = __('Links to multiple pages of results.', 'gravityview' );
15
16 3
		$default_values = array( 'header' => 1, 'footer' => 1 );
17 3
		$settings = array( 'show_all' => array(
18 3
			'type' => 'checkbox',
19 3
			'label' => __( 'Show each page number', 'gravityview' ),
20 3
			'desc' => __('Show every page number instead of summary (eg: 1 2 3 ... 8 »)', 'gravityview'),
21
			'value' => false
22
		));
23 3
		parent::__construct( __( 'Page Links', 'gravityview' ) , 'page_links', $default_values, $settings );
24
25 3
	}
26
27 1
	public function render_frontend( $widget_args, $content = '', $context = '') {
28 1
		$gravityview_view = GravityView_View::getInstance();
29
30 1
		if( !$this->pre_render_frontend() ) {
31 1
			return;
32
		}
33
34
		$atts = shortcode_atts( array(
35
			'page_size' => \GV\Utils::get( $gravityview_view->paging, 'page_size' ),
0 ignored issues
show
Documentation introduced by
The property $paging is declared protected in GravityView_View. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
36
			'total' => $gravityview_view->total_entries,
0 ignored issues
show
Documentation introduced by
The property $total_entries is declared protected in GravityView_View. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
37
			'show_all' => !empty( $this->settings['show_all']['default'] ),
38
			'current' => (int) \GV\Utils::_GET( 'pagenum', 1 ),
39
		), $widget_args, 'gravityview_widget_page_links' );
40
		
41
		$page_link_args = array(
42
			'base' => add_query_arg('pagenum','%#%', gv_directory_link() ),
43
			'format' => '&pagenum=%#%',
44
			'add_args' => array(), //
45
			'prev_text' => '&laquo;',
46
			'next_text' => '&raquo;',
47
			'type' => 'list',
48
			'end_size' => 1,
49
			'mid_size' => 2,
50
			'total' => empty( $atts['page_size'] ) ? 0 : ceil( $atts['total'] / $atts['page_size'] ),
51
			'current' => $atts['current'],
52
			'show_all' => !empty( $atts['show_all'] ), // to be available at backoffice
53
		);
54
55
		/**
56
		 * @filter `gravityview_page_links_args` Filter the pagination options
57
		 * @since 1.1.4
58
		 * @param array  $page_link_args Array of arguments for the `paginate_links()` function. [Read more about `paginate_links()`](http://developer.wordpress.org/reference/functions/paginate_links/)
59
		 */
60
		$page_link_args = apply_filters('gravityview_page_links_args', $page_link_args );
61
62
		$page_links = paginate_links( $page_link_args );
63
64
		if( !empty( $page_links )) {
65
			$class = !empty( $widget_args['custom_class'] ) ? $widget_args['custom_class'] : '';
66
			$class = gravityview_sanitize_html_class( 'gv-widget-page-links ' . $class );
67
			echo '<div class="'.$class.'">'. $page_links .'</div>';
68
		} else {
69
			gravityview()->log->debug( 'No page links; paginate_links() returned empty response.' );
70
		}
71
72
	}
73
74
}
75
76
new GravityView_Widget_Page_Links;