Completed
Push — master ( 6310a0...90c9e3 )
by Brandon
39s
created

RentbitsRentalComparisonWidget::update()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 8
nc 32
nop 2
dl 0
loc 12
rs 8.8571
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 16 and the first side effect is on line 9.

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.

Loading history...
2
/**
3
 * Rentbits.com Rental Comparison Widget (http://rentbits.com/rb/page/rental-comparison-widget.html)
4
 *
5
 * @package RE-PRO
6
 */
7
8
	/* Exit if accessed directly. */
9
if ( ! defined( 'ABSPATH' ) ) { exit; }
10
11
/**
12
 * RentbitsRentalComparisonWidget class.
13
 *
14
 * @extends WP_Widget
15
 */
16
class RentbitsRentalComparisonWidget extends WP_Widget {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
17
18
	/**
19
	 * __construct function.
20
	 *
21
	 * @access public
22
	 * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
23
	 */
24
	public function __construct() {
25
26
		parent::__construct(
27
			'rentbits_rental_comparison',
28
			__( 'Rentbits Rental Comparison', 're-pro' ),
29
			array(
30
				'description' => __( 'Display the current average rental price for up to any four locations.', 're-pro' ),
31
				'classname'   => 're-pro re-pro-widget rentbits-widget rentbits-rental-comparison',
32
				'customize_selective_refresh' => true,
33
			)
34
		);
35
	}
36
37
	/**
38
	 * Widget function.
39
	 *
40
	 * @access public
41
	 * @param mixed $args Arguments.
42
	 * @param mixed $instance Instance.
43
	 */
44
	public function widget( $args, $instance ) {
45
46
		$title = ! empty( $instance['title'] ) ? $instance['title'] : '';
47
		$location1 = ! empty( $instance['location1'] ) ? $instance['location1'] : '';
48
		$location2 = ! empty( $instance['location2'] ) ? $instance['location2'] : '';
49
		$location3 = ! empty( $instance['location3'] ) ? $instance['location3'] : '';
50
		$location4 = ! empty( $instance['location4'] ) ? $instance['location4'] : '';
51
52
		echo $args['before_widget'];
53
54
		echo $args['before_title'] . esc_attr( $title ) . $args['after_title'];
55
56
		$rentbits_widgets = new RentbitsWidgets();
57
58
		$rentbits_widgets->get_rental_comparison_widget( $location1, $location2, $location3, $location4 );
59
60
		echo $args['after_widget'];
61
	}
62
63
	/**
64
	 * Form function.
65
	 *
66
	 * @access public
67
	 * @param mixed $instance Instance.
68
	 * @return void
69
	 */
70
	public function form( $instance ) {
71
72
		// Set default values.
73
		$instance = wp_parse_args( (array) $instance, array(
74
			'title' => '',
75
			'location1' => '',
76
			'location2' => '',
77
			'location3' => '',
78
			'location4' => '',
79
		) );
80
81
		// Retrieve an existing value from the database.
82
		$title = ! empty( $instance['title'] ) ? $instance['title'] : '';
83
		$location1 = ! empty( $instance['location1'] ) ? $instance['location1'] : '';
84
		$location2 = ! empty( $instance['location2'] ) ? $instance['location2'] : '';
85
		$location3 = ! empty( $instance['location3'] ) ? $instance['location3'] : '';
86
		$location4 = ! empty( $instance['location4'] ) ? $instance['location4'] : '';
87
88
		// Title.
89
		echo '<p>';
90
		echo '	<label for="' . $this->get_field_id( 'title' ) . '" class="title-label">' . __( 'Tile:', 're-pro' ) . '</label>';
91
		echo '	<input id="' . $this->get_field_id( 'title' ) . '" name="' . $this->get_field_name( 'title' ) . '" value="' . $title  . '" class="widefat">';
92
		echo '</p>';
93
94
		// Location 1.
95
		echo '<p>';
96
		echo '	<label for="' . $this->get_field_id( 'location1' ) . '" class="title-label">' . __( 'Location:', 're-pro' ) . '</label>';
97
		echo '	<input id="' . $this->get_field_id( 'location1' ) . '" name="' . $this->get_field_name( 'location1' ) . '" value="' . $location1 . '" class="widefat">';
98
		echo '</p>';
99
100
		// Location 2.
101
		echo '<p>';
102
		echo '	<label for="' . $this->get_field_id( 'location2' ) . '" class="title-label">' . __( 'Location:', 're-pro' ) . '</label>';
103
		echo '	<input id="' . $this->get_field_id( 'location2' ) . '" name="' . $this->get_field_name( 'location2' ) . '" value="' . $location2 . '" class="widefat">';
104
		echo '</p>';
105
106
		// Location 3.
107
		echo '<p>';
108
		echo '	<label for="' . $this->get_field_id( 'location3' ) . '" class="title-label">' . __( 'Location:', 're-pro' ) . '</label>';
109
		echo '	<input id="' . $this->get_field_id( 'location3' ) . '" name="' . $this->get_field_name( 'location3' ) . '" value="' . $location3 . '" class="widefat">';
110
		echo '</p>';
111
112
		// Location 4.
113
		echo '<p>';
114
		echo '	<label for="' . $this->get_field_id( 'location4' ) . '" class="title-label">' . __( 'Location:', 're-pro' ) . '</label>';
115
		echo '	<input id="' . $this->get_field_id( 'location4' ) . '" name="' . $this->get_field_name( 'location4' ) . '" value="' . $location4 . '" class="widefat">';
116
		echo '</p>';
117
118
	}
119
120
	/**
121
	 * Update Widget Instance.
122
	 *
123
	 * @access public
124
	 * @param mixed $new_instance New Instance.
125
	 * @param mixed $old_instance Old Instance.
126
	 * @return $instance
0 ignored issues
show
Documentation introduced by
The doc-type $instance could not be parsed: Unknown type name "$instance" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
127
	 */
128
	public function update( $new_instance, $old_instance ) {
129
130
		$instance = $old_instance;
131
132
		$instance['title'] = ! empty( $new_instance['title'] ) ? strip_tags( $new_instance['title'] ) : '';
133
		$instance['location1'] = ! empty( $new_instance['location1'] ) ? strip_tags( $new_instance['location1'] ) : '';
134
		$instance['location2'] = ! empty( $new_instance['location2'] ) ? strip_tags( $new_instance['location2'] ) : '';
135
		$instance['location3'] = ! empty( $new_instance['location3'] ) ? strip_tags( $new_instance['location3'] ) : '';
136
		$instance['location4'] = ! empty( $new_instance['location4'] ) ? strip_tags( $new_instance['location4'] ) : '';
137
138
		return $instance;
139
	}
140
}
141
142
/**
143
 * Register Rentbits.com Rental Comparison Widget.
144
 *
145
 * @access public
146
 * @return void
147
 */
148
function repro_rentbits_rental_comparison() {
149
	if ( ! is_ssl() ) {
150
		register_widget( 'RentbitsRentalComparisonWidget' );
151
	}
152
}
153
add_action( 'widgets_init', 'repro_rentbits_rental_comparison' );
154