EqualHousingWidget::update()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 5

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 4
nop 2
dl 10
loc 10
rs 9.4285
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 8 and the first side effect is on line 132.

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
/**
4
 * EqualHousingWidget class.
5
 *
6
 * @extends WP_Widget
7
 */
8 View Code Duplication
class EqualHousingWidget extends WP_Widget {
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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...
9
10
11
	/**
12
	 * __construct function.
13
	 *
14
	 * @access public
15
	 * @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...
16
	 */
17
	public function __construct() {
18
19
		parent::__construct(
20
			'equal_housing_logo',
21
			__( 'Equal Housing Opportunity', 're-pro' ),
22
			array(
23
				'description' => __( 'Display the Equal Housing Opportunity Logo.', 're-pro' ),
24
				'classname'   => 're-pro re-pro-widget equal-housing',
25
				'customize_selective_refresh' => true,
26
			)
27
		);
28
29
	}
30
31
	/**
32
	 * Display Equal Housing Widget.
33
	 *
34
	 * @access public
35
	 * @param mixed $args Arguments.
36
	 * @param mixed $instance Instance.
37
	 * @return void
38
	 */
39
	public function widget( $args, $instance ) {
40
41
		if ( ! empty( $instance['title'] ) ) {
42
			$title = $instance['title'];
43
		} else {
44
			$title = '';
45
		}
46
		$logo_size = $instance['logo_size'];
47
48
		echo $args['before_widget'];
49
50
		echo $args['before_title'] . esc_attr( $title ) . $args['after_title'];
51
52
		echo '<img src="'. esc_url( plugins_url( '../../../assets/images/equalhousing/equal_housing.svg', __FILE__ ) ) .'" alt="'. __( 'Equal Housing Opportunity', 're-pro' ) .'" height="' . $logo_size . '" width="' . $logo_size . '" class="re-pro equal-housing-opportunity">';
53
54
		echo $args['after_widget'];
55
56
	}
57
58
	/**
59
	 * Widget Form.
60
	 *
61
	 * @access public
62
	 * @param mixed $instance Instance.
63
	 * @return void
64
	 */
65
	public function form( $instance ) {
66
67
		// Set default values
68
		$instance = wp_parse_args( (array) $instance, array(
69
			'logo-size' => '32',
70
			'title' => '',
71
		) );
72
73
		// Retrieve an existing value from the database
74
		$title = ! empty( $instance['title'] ) ? $instance['title'] : '';
75
		$logo_size = ! empty( $instance['logo_size'] ) ? $instance['logo_size'] : '';
76
77
		// Form fields
78
		echo '<p>';
79
		echo '	<label for="' . $this->get_field_id( 'title' ) . '" class="title-label">' . __( 'Tile', 're-pro' ) . '</label>';
80
		echo '	<input id="' . $this->get_field_id( 'title' ) . '" name="' . $this->get_field_name( 'title' ) . '" value="' . $title  . '" class="widefat">';
81
		echo '</p>';
82
83
		echo '<p>';
84
		echo '	<label for="' . $this->get_field_id( 'logo_size' ) . '" class="logo-size-label">' . __( 'Logo Size', 're-pro' ) . '</label>';
85
		echo '	<select id="' . $this->get_field_id( 'logo_size' ) . '" name="' . $this->get_field_name( 'logo_size' ) . '" class="widefat">';
86
		echo '		<option value="" ' . selected( $logo_size, '', false ) . '> ' . __( 'Choose', 're-pro' ) . '</option>';
87
		echo '		<option value="512" ' . selected( $logo_size, '512', false ) . '> ' . __( '512px', 're-pro' ) . '</option>';
88
		echo '		<option value="256" ' . selected( $logo_size, '256', false ) . '> ' . __( '256px', 're-pro' ) . '</option>';
89
		echo '		<option value="128" ' . selected( $logo_size, '128', false ) . '> ' . __( '128px', 're-pro' ) . '</option>';
90
		echo '		<option value="64" ' . selected( $logo_size, '64', false ) . '> ' . __( '64px', 're-pro' ) . '</option>';
91
		echo '		<option value="32" ' . selected( $logo_size, '32', false ) . '> ' . __( '32px', 're-pro' ) . '</option>';
92
		echo '		<option value="24" ' . selected( $logo_size, '24', false ) . '> ' . __( '24px', 're-pro' ) . '</option>';
93
		echo '		<option value="16" ' . selected( $logo_size, '16', false ) . '> ' . __( '16px', 're-pro' ) . '</option>';
94
		echo '		<option value="14" ' . selected( $logo_size, '14', false ) . '> ' . __( '14px', 're-pro' ) . '</option>';
95
		echo '	</select>';
96
		echo '	<span class="description">' . __( 'Choose the size of the logo you want to display.', 're-pro' ) . '</span>';
97
		echo '</p>';
98
99
	}
100
101
102
	/**
103
	 * Update Widget.
104
	 *
105
	 * @access public
106
	 * @param mixed $new_instance New Widget Instance.
107
	 * @param mixed $old_instance Old Widget instance.
108
	 * @return void
109
	 */
110
	public function update( $new_instance, $old_instance ) {
111
112
		$instance = $old_instance;
113
114
		$instance['title'] = ! empty( $new_instance['title'] ) ? strip_tags( $new_instance['title'] ) : '';
115
		$instance['logo_size'] = ! empty( $new_instance['logo_size'] ) ? strip_tags( $new_instance['logo_size'] ) : '';
116
117
		return $instance;
118
119
	}
120
}
121
122
123
/**
124
 * Equal Housing Widget.
125
 *
126
 * @access public
127
 * @return void
128
 */
129
function repro_register_widgets() {
130
	register_widget( 'EqualHousingWidget' );
131
}
132
add_action( 'widgets_init', 'repro_register_widgets' );
133