Completed
Pull Request — master (#1201)
by Ravinder
23:20
created

Give_Forms_Widget::form()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 70
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 47
nc 4
nop 1
dl 0
loc 70
ccs 0
cts 17
cp 0
crap 12
rs 9.1724
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
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 22 and the first side effect is on line 14.

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