Completed
Pull Request — master (#1085)
by Devin
19:12
created

Give_Forms_Widget::widget()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 11
nc 6
nop 2
dl 0
loc 22
ccs 0
cts 11
cp 0
crap 30
rs 8.6737
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 20 and the first side effect is on line 13.

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
 * Give Form Widget
4
 *
5
 * @package     WordImpress
6
 * @subpackage  Admin/Forms
7
 * @copyright   Copyright (c) 2016, WordImpress
8
 * @license     http://opensource.org/licenses/gpl-2.0.php GNU Public License
9
 * @since       1.0
10
 */
11
12
// Exit if accessed directly
13
defined( 'ABSPATH' ) or exit;
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
14
15
/**
16
 * Give Form widget
17
 *
18
 * @since 1.0
19
 */
20
class Give_Forms_Widget extends WP_Widget{
21
	/**
22
	 * The widget class name
23
	 *
24
	 * @var string
25
	 */
26
	protected $self;
27
28
	/**
29
	 * Instantiate the class
30
	 */
31
	public function __construct(){
32
		$this->self = get_class( $this );
33
34
		parent::__construct(
35
			strtolower( $this->self ),
36
			esc_html__( 'Give - Donation Form', 'give' ),
37
			array(
38
				'description' => esc_html__( 'Display a Give Donation Form in your theme\'s widget powered sidebar.', 'give' )
39
			)
40
		);
41
42
		add_action( 'widgets_init',          array( $this, 'widget_init' ) );
43
		add_action( 'admin_enqueue_scripts', array( $this, 'admin_widget_scripts' ) );
44
	}
45
46
	/**
47
	 * Load widget assets only on the widget page
48
	 *
49
	 * @param string $hook
50
	 *
51
	 * @return void
52
	 */
53
	public function admin_widget_scripts( $hook ){
54
		// Directories of assets
55
		$js_dir     = GIVE_PLUGIN_URL . 'assets/js/admin/';
56
		$js_plugins = GIVE_PLUGIN_URL . 'assets/js/plugins/';
57
		$css_dir    = GIVE_PLUGIN_URL . 'assets/css/';
58
59
		// Use minified libraries if SCRIPT_DEBUG is turned off
60
		$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
61
62
		// Widget Script
63
		if ( $hook == 'widgets.php' ) {
64
65
			wp_enqueue_style( 'give-qtip-css', $css_dir . 'jquery.qtip' . $suffix . '.css' );
66
67
			wp_enqueue_script( 'give-qtip', $js_plugins . 'jquery.qtip' . $suffix . '.js', array( 'jquery' ), GIVE_VERSION );
68
69
			wp_enqueue_script( 'give-admin-widgets-scripts', $js_dir . 'admin-widgets' . $suffix . '.js', array( 'jquery' ), GIVE_VERSION, false );
70
		}
71
	}
72
73
	/**
74
	 * Echo the widget content.
75
	 *
76
	 * @param array $args     Display arguments including before_title, after_title,
77
	 *                        before_widget, and after_widget.
78
	 * @param array $instance The settings for the particular instance of the widget.
79
	 */
80
	public function widget( $args, $instance ){
81
		$title = !empty( $instance['title'] ) ? $instance['title'] : '';
82
		$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
83
84
85
		// If user set float labels to global then check global float label setting and update donation form widget accordingly.
86
		if( ( 'global' === $instance['float_labels'] ) ) {
87
			$instance['float_labels'] = ( 'on' === give_get_option( 'enable_floatlabels', '' ) ) ? 'enabled' : 'disabled';
88
		}
89
90
		echo $args['before_widget'];
91
92
		do_action( 'give_before_forms_widget' );
93
94
		echo $title ? $args['before_title'] . $title . $args['after_title'] : '';
95
96
		give_get_donation_form( $instance );
97
98
		echo $args['after_widget'];
99
100
		do_action( 'give_after_forms_widget' );
101
	}
102
103
	/**
104
	 * Output the settings update form.
105
	 *
106
	 * @param array $instance Current settings.
107
	 *
108
	 * @return string
109
	 */
110
	public function form( $instance ){
111
		$defaults = array(
112
			'title'         => '',
113
			'id'            => '',
114
			'float_labels'  => 'global',
115
			'display_style' => 'modal',
116
		);
117
118
		$instance = wp_parse_args( (array) $instance, $defaults );
119
120
		// Backward compatibility: Set float labels as default if, it was set as empty previous.
121
		$instance['float_labels'] = empty( $instance['float_labels'] ) ? 'global' : $instance['float_labels'];
122
123
		// Query Give Forms
124
		$args = array(
125
			'post_type'      => 'give_forms',
126
			'posts_per_page' => - 1,
127
			'post_status'    => 'publish',
128
		);
129
130
		$give_forms = get_posts( $args );
131
132
		// Widget: Title
133
134
		?><p>
135
			<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php esc_html_e( 'Title:', 'give' ); ?></label>
136
			<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php esc_attr_e( $instance['title'] ); ?>" /><br>
137
			<small class="give-field-description"><?php esc_html_e( 'Leave blank to hide the widget title.', 'give' ); ?></small>
138
		</p><?php
139
140
		// Widget: Give Form
141
142
		?><p>
143
			<label for="<?php echo esc_attr( $this->get_field_id( 'id' ) ); ?>"><?php
144
				printf(
145
					/* translators: %s: form singular label */
146
					esc_html__( 'Give %s:', 'give' ),
147
					give_get_forms_label_singular()
148
				);
149
			?></label>
150
			<select class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'id' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'id' ) ); ?>">
151
				<option value="current"><?php esc_html_e( '— Select —', 'give' ); ?></option>
152
				<?php foreach ( $give_forms as $give_form ) { ?>
153
					<option <?php selected( absint( $instance['id'] ), $give_form->ID ); ?> value="<?php echo esc_attr( $give_form->ID ); ?>"><?php echo $give_form->post_title; ?></option>
154
				<?php } ?>
155
			</select><br>
156
			<small class="give-field-description"><?php esc_html_e( 'Select a Give Form to embed in this widget.', 'give' ); ?></small>
157
		</p>
158
159
		<?php // Widget: Display Style ?>
160
		<p>
161
			<label for="<?php echo esc_attr( $this->get_field_id( 'display_style' ) ); ?>"><?php esc_html_e( 'Display style:', 'give' ); ?></label><br>
162
			<label for="<?php echo $this->get_field_id( 'display_style' ); ?>-onpage"><input type="radio" class="widefat" id="<?php echo $this->get_field_id( 'display_style' ); ?>-onpage" name="<?php echo $this->get_field_name( 'display_style' ); ?>" value="onpage" <?php checked( $instance['display_style'], 'onpage' ); ?>> <?php echo esc_html__( 'All Fields', 'give' ); ?></label>
163
			&nbsp;&nbsp;<label for="<?php echo $this->get_field_id( 'display_style' ); ?>-reveal"><input type="radio" class="widefat" id="<?php echo $this->get_field_id( 'display_style' ); ?>-reveal" name="<?php echo $this->get_field_name( 'display_style' ); ?>" value="reveal" <?php checked( $instance['display_style'], 'reveal' ); ?>> <?php echo esc_html__( 'Reveal', 'give' ); ?></label>
164
			&nbsp;&nbsp;<label for="<?php echo $this->get_field_id( 'display_style' ); ?>-modal"><input type="radio" class="widefat" id="<?php echo $this->get_field_id( 'display_style' ); ?>-modal" name="<?php echo $this->get_field_name( 'display_style' ); ?>" value="modal" <?php checked( $instance['display_style'], 'modal' ); ?>> <?php echo esc_html__( 'Modal', 'give' ); ?></label><br>
165
			 <small class="give-field-description">
166
				<?php echo esc_html__( 'Select a Give Form style.', 'give' ); ?>
167
			</small>
168
		</p>
169
170
		<?php // Widget: Floating Labels ?>
171
		<p>
172
			<label for="<?php echo esc_attr( $this->get_field_id( 'float_labels' ) ); ?>"><?php esc_html_e( 'Floating Labels (optional):', 'give' ); ?></label><br>
173
			<label for="<?php echo $this->get_field_id( 'float_labels' ); ?>-global"><input type="radio" class="widefat" id="<?php echo $this->get_field_id( 'float_labels' ); ?>-global" name="<?php echo $this->get_field_name( 'float_labels' ); ?>" value="global" <?php checked( $instance['float_labels'], 'global' ); ?>> <?php echo esc_html__( 'Global Options', 'give' ); ?></label>
174
			&nbsp;&nbsp;<label for="<?php echo $this->get_field_id( 'float_labels' ); ?>-enabled"><input type="radio" class="widefat" id="<?php echo $this->get_field_id( 'float_labels' ); ?>-enabled" name="<?php echo $this->get_field_name( 'float_labels' ); ?>" value="enabled" <?php checked( $instance['float_labels'], 'enabled' ); ?>> <?php echo esc_html__( 'Yes', 'give' ); ?></label>
175
			&nbsp;&nbsp;<label for="<?php echo $this->get_field_id( 'float_labels' ); ?>-disabled"><input type="radio" class="widefat" id="<?php echo $this->get_field_id( 'float_labels' ); ?>-disabled" name="<?php echo $this->get_field_name( 'float_labels' ); ?>" value="disabled" <?php checked( $instance['float_labels'], 'disabled' ); ?>> <?php echo esc_html__( 'No', 'give' ); ?></label><br>
176
			<small class="give-field-description">
177
				<?php
178
				printf(
179
					/* translators: %s: https://givewp.com/documentation/core/give-forms/creating-give-forms/#floating-labels */
180
					__( 'Override the <a href="%s" target="_blank">floating labels</a> setting for this Give form.', 'give' ),
181
					esc_url( 'https://givewp.com/documentation/core/give-forms/creating-give-forms/#floating-labels' )
182
				);
183
			?></small>
184
		</p><?php
185
	}
186
187
	/**
188
	 * Register the widget
189
	 *
190
	 * @return void
191
	 */
192
	function widget_init(){
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...
193
		register_widget( $this->self );
194
	}
195
196
	/**
197
	 * Update the widget
198
	 *
199
	 * @param array $new_instance
200
	 * @param array $old_instance
201
	 *
202
	 * @return array
203
	 */
204
	public function update( $new_instance, $old_instance ){
205
		$this->flush_widget_cache();
206
207
		return $new_instance;
208
	}
209
210
	/**
211
	 * Flush widget cache
212
	 *
213
	 * @return void
214
	 */
215
	public function flush_widget_cache(){
216
		wp_cache_delete( $this->self, 'widget' );
217
	}
218
}
219
220
new Give_Forms_Widget;
221