Passed
Push — master ( 6f0a6d...90a9ca )
by
unknown
09:41
created

update()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
nc 2
nop 2
dl 0
loc 17
rs 9.9666
c 1
b 0
f 0
1
<?php
2
/**
3
 * Class used to add the Popular Posts widget to the Appearance > Widget area.
4
 */
5
6
/**
7
 * Class MonsterInsights_Popular_Posts_Widget_Sidebar
8
 */
9
class MonsterInsights_Popular_Posts_Widget_Sidebar extends WP_Widget {
10
	/**
11
	 * Hold widget settings defaults, populated in constructor.
12
	 *
13
	 * @since 7.12.0
14
	 *
15
	 * @var array
16
	 */
17
	protected $defaults;
18
	/**
19
	 * Hold widget options that are theme specific.
20
	 *
21
	 * @since 7.12.0
22
	 *
23
	 * @var array
24
	 */
25
	protected $conditional_options;
26
27
	/**
28
	 * Constructor
29
	 *
30
	 * @since 7.12.0
31
	 */
32
	public function __construct() {
33
34
		// Widget defaults.
35
		$this->defaults = array(
36
			'title'            => '',
37
			'display_title'    => 'on',
38
			'post_count'       => 5,
39
			'theme'            => 'alpha',
40
			'title_color'      => '#393F4C',
41
			'title_size'       => 12,
42
			'label_color'      => '#EB5757',
43
			'label_text'       => 'Trending',
44
			'meta_color'       => '#99A1B3',
45
			'meta_size'        => '12',
46
			'meta_author'      => 'on',
47
			'meta_date'        => 'on',
48
			'meta_comments'    => 'on',
49
			'background_color' => '#F0F2F4',
50
			'border_color'     => '#D3D7DE',
51
			'columns'          => '1',
52
		);
53
54
		$this->conditional_options = array(
55
			'title_color'       => array( 'title', 'color' ),
56
			'title_size'        => array( 'title', 'size' ),
57
			'label_color'       => array( 'label', 'color' ),
58
			'label_text'        => array( 'label', 'text' ),
59
			'background_color'  => array( 'background', 'color' ),
60
			'background_border' => array( 'background', 'border' ),
61
			'meta_color'        => array( 'meta', 'color' ),
62
			'meta_size'         => array( 'meta', 'size' ),
63
			'meta_author'       => array( 'meta', 'author' ),
64
			'meta_date'         => array( 'meta', 'date' ),
65
			'meta_comments'     => array( 'meta', 'comments' ),
66
			'comments_color'    => array( 'comments', 'color' ),
67
		);
68
69
		// Widget Slug.
70
		$widget_slug = 'monsterinsights-popular-posts-widget';
71
72
		// Widget basics.
73
		$widget_ops = array(
74
			'classname'   => $widget_slug,
75
			'description' => esc_html_x( 'Display popular posts.', 'Widget', 'google-analytics-for-wordpress' ),
76
		);
77
78
		// Widget controls.
79
		$control_ops = array(
80
			'id_base' => $widget_slug,
81
		);
82
83
		$this->add_scripts();
84
85
		// Load widget.
86
		parent::__construct( $widget_slug, esc_html_x( 'Popular Posts - MonsterInsights', 'Widget', 'google-analytics-for-wordpress' ), $widget_ops, $control_ops );
87
	}
88
89
	/**
90
	 * Output the HTML for this widget.
91
	 *
92
	 * @param array $args An array of standard parameters for widgets in this theme.
93
	 * @param array $instance An array of settings for this widget instance.
94
	 *
95
	 * @since 7.12.0
96
	 *
97
	 */
98
	public function widget( $args, $instance ) {
99
100
		echo $args['before_widget'];
101
102
		$title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );
103
104
		if ( $instance['display_title'] && ! empty( $instance['title'] ) ) {
105
			echo $args['before_title'];
106
			echo wp_kses_post( $title );
107
			echo $args['after_title'];
108
		}
109
110
		$atts = array(
111
			'theme'        => $instance['theme'],
112
			'post_count'   => $instance['post_count'],
113
			'columns'      => 1, // Sidebar is not wide so we always use the 1 column layout.
114
			'widget_title' => false, // Override this in favor of sidebar-specific markup above.
115
		);
116
117
		foreach ( $this->conditional_options as $key => $default ) {
118
			if ( ! empty( $instance[ $key ] ) ) {
119
				$atts[ $key ] = $instance[ $key ];
120
			}
121
		}
122
123
		echo MonsterInsights_Popular_Posts_Widget()->shortcode_output( $atts );
124
125
		echo $args['after_widget'];
126
127
	}
128
129
	/**
130
	 * Deal with the settings when they are saved by the admin. Here is
131
	 * where any validation should be dealt with.
132
	 *
133
	 * @param array $new_instance An array of new settings as submitted by the admin.
134
	 * @param array $old_instance An array of the previous settings.
135
	 *
136
	 * @return array The validated and (if necessary) amended settings
137
	 * @since 7.12.0
138
	 *
139
	 */
140
	public function update( $new_instance, $old_instance ) {
141
142
		$new_instance['title']         = wp_strip_all_tags( $new_instance['title'] );
143
		$new_instance['theme']         = wp_strip_all_tags( $new_instance['theme'] );
144
		$new_instance['display_title'] = wp_strip_all_tags( $new_instance['display_title'] );
145
		$new_instance['post_count']    = absint( $new_instance['post_count'] );
146
147
		// Theme-dependant options.
148
		$themes = new MonsterInsights_Popular_Posts_Themes( 'widget', ! empty( $old_instance['theme'] ) ? $old_instance['theme'] : '' );
149
		$theme  = $themes->get_theme();
150
151
152
		foreach ( $this->conditional_options as $key => $obj ) {
153
			$new_instance = $this->maybe_remove_option( ! empty( $theme['styles'][ $obj[0] ][ $obj[1] ] ), $key, $new_instance );
154
		}
155
156
		return $new_instance;
157
	}
158
159
	/**
160
	 * Process dynamic and checkbox values so they are stored correctly and specific to the current theme.
161
	 *
162
	 * @param bool   $is_used A check if this property is used in the currently selected theme.
163
	 * @param string $key The key of the property we're checking.
164
	 * @param array  $instance The current widget instance, new instance.
165
	 *
166
	 * @return mixed
167
	 */
168
	public function maybe_remove_option( $is_used, $key, $instance ) {
169
170
		$checkboxes = array(
171
			'meta_author',
172
			'meta_date',
173
			'meta_comments',
174
		);
175
176
		if ( $is_used && ! isset( $instance[ $key ] ) && in_array( $key, $checkboxes ) ) {
177
			$instance[ $key ] = 'off';
178
		} elseif ( ! $is_used && isset( $instance[ $key ] ) ) {
179
			unset( $instance[ $key ] );
180
		} elseif ( $is_used && isset( $instance[ $key ] ) ) {
181
			$instance[ $key ] = wp_strip_all_tags( $instance[ $key ] );
182
		}
183
184
		return $instance;
185
	}
186
187
	/**
188
	 * Display the form for this widget on the Widgets page of the WP Admin area.
189
	 *
190
	 * @param array $instance An array of the current settings for this widget.
191
	 *
192
	 * @since 7.12.0
193
	 */
194
	public function form( $instance ) {
195
196
		// Merge with defaults but use theme settings from Vue as defaults.
197
		$theme_name = empty( $instance['theme'] ) ? $this->defaults['theme'] : $instance['theme'];
198
		$themes     = new MonsterInsights_Popular_Posts_Themes( 'widget', $theme_name );
199
		$theme      = $themes->get_theme();
200
		$this->prepare_defaults_from_theme( $theme );
201
202
		$instance = wp_parse_args( (array) $instance, $this->defaults );
203
204
		$title_font_sizes = apply_filters( 'monsterinsights_popular_posts_widget_title_sizes', range( 10, 35 ) );
205
206
		$this->text_input( 'title', _x( 'Widget Title:', 'Widget', 'google-analytics-for-wordpress' ), $instance );
207
208
		$categories = array(
209
			array(
210
				'name'  => 'News',
211
				'value' => '0',
212
			),
213
			array(
214
				'name'  => 'Technology',
215
				'value' => '1',
216
			),
217
		);
218
		?>
219
		<p>
220
			<input type="checkbox"
221
				   id="<?php echo esc_attr( $this->get_field_id( 'display_title' ) ); ?>"
222
				   name="<?php echo esc_attr( $this->get_field_name( 'display_title' ) ); ?>"
223
				   value="on" <?php checked( $instance['display_title'], 'on' ); ?> />
224
			<label for="<?php echo esc_attr( $this->get_field_id( 'display_title' ) ); ?>">
225
				<?php echo esc_html( _x( 'Display Widget Title', 'Widget', 'google-analytics-for-wordpress' ) ); ?>
226
			</label>
227
		</p>
228
		<p>
229
			<label for="<?php echo esc_attr( $this->get_field_id( 'post_count' ) ); ?>">
230
				<?php echo esc_html( _x( 'Number of posts to display:', 'Widget', 'google-analytics-for-wordpress' ) ); ?>
231
			</label>
232
			<select id="<?php echo esc_attr( $this->get_field_id( 'post_count' ) ); ?>"
233
					name="<?php echo esc_attr( $this->get_field_name( 'post_count' ) ); ?>">
234
				<option value="5" <?php selected( $instance['post_count'], 5 ); ?>>5</option>
235
				<option value="10" <?php selected( $instance['post_count'], 10 ); ?>>10</option>
236
			</select>
237
		</p>
238
		<p>
239
			<label for="<?php echo esc_attr( $this->get_field_id( 'theme' ) ); ?>">
240
				<?php echo esc_html( _x( 'Theme:', 'Widget', 'google-analytics-for-wordpress' ) ); ?>
241
			</label>
242
			<select id="<?php echo esc_attr( $this->get_field_id( 'theme' ) ); ?>"
243
					class="widefat monsterinsights-save-on-change"
244
					name="<?php echo esc_attr( $this->get_field_name( 'theme' ) ); ?>">
245
				<?php foreach ( $themes->themes as $key => $details ) {
246
					if ( 'lite' !== $details['level'] ) {
247
						continue;
248
					}
249
					?>
250
					<option value="<?php echo esc_attr( $key ); ?>" <?php selected( $instance['theme'], $key ); ?>>
251
						<?php echo esc_html( ucfirst( $key ) ); ?>
252
					</option>
253
				<?php } ?>
254
			</select>
255
		</p>
256
		<div class="monsterinsights-widget-theme-preview">
257
			<span class="monsterinsights-widget-theme-preview-label">
258
				<?php esc_html_e( 'Theme Preview', 'google-analytics-for-wordpress' ); ?>
259
			</span>
260
			<div class="monsterinsights-widget-theme-preview-icon monsterinsights-widget-theme-preview-icon-<?php echo esc_attr( $instance['theme'] ); ?>"></div>
261
		</div>
262
		<?php if ( ! empty( $theme['styles']['title']['color'] ) ) {
263
			$this->color_input( 'title_color', _x( 'Title Color:', 'Widget', 'google-analytics-for-wordpress' ), $instance );
264
			?>
265
			<?php
266
		}
267
		if ( ! empty( $theme['styles']['title']['size'] ) ) {
268
			$this->size_input( 'title_size', _x( 'Title Font Size:', 'Widget', 'google-analytics-for-wordpress' ), $instance, $title_font_sizes );
269
		}
270
		if ( ! empty( $theme['styles']['label']['color'] ) ) {
271
			$this->color_input( 'label_color', _x( 'Label Color:', 'Widget', 'google-analytics-for-wordpress' ), $instance );
272
		}
273
		if ( ! empty( $theme['styles']['label']['editable'] ) && ! empty( $theme['styles']['label']['text'] ) ) {
274
			$this->text_input( 'label_text', _x( 'Label Text:', 'Widget', 'google-analytics-for-wordpress' ), $instance );
275
		}
276
		if ( ! empty( $theme['styles']['background']['border'] ) ) {
277
			$this->color_input( 'background_border', _x( 'Border Color:', 'Widget', 'google-analytics-for-wordpress' ), $instance );
278
		}
279
		if ( ! empty( $theme['styles']['background']['color'] ) ) {
280
			$this->color_input( 'background_color', _x( 'Background Color:', 'Widget', 'google-analytics-for-wordpress' ), $instance );
281
		}
282
		if ( ! empty( $theme['styles']['comments']['color'] ) ) {
283
			$this->color_input( 'comments_color', _x( 'Comments Count Color:', 'Widget', 'google-analytics-for-wordpress' ), $instance );
284
		}
285
		?>
286
		<p>
287
288
			<label class="monsterinsights-label-block">
289
				<?php echo esc_html( _x( 'Only Show Posts from These Categories:', 'Widget', 'google-analytics-for-wordpress' ) ); ?>
290
				<span class="monsterinsights-pro-pill">PRO</span>
291
			</label>
292
			<select disabled
293
					class="monsterinsights-multiselect" multiple>
294
				<?php
295
				foreach ( $categories as $category ) {
296
					?>
297
					<option value="<?php echo absint( $category['value'] ); ?>"
298
							selected="selected"><?php echo esc_html( $category['name'] ); ?></option>
299
					<?php
300
				}
301
				?>
302
			</select>
303
		</p>
304
		<?php
305
	}
306
307
	/**
308
	 * Colorpicker input element.
309
	 *
310
	 * @param string $name Name of the input, for saving/loading.
311
	 * @param string $label Label of the element.
312
	 * @param array  $instance The current widget instance.
313
	 */
314
	public function color_input( $name, $label, $instance ) {
315
		?>
316
		<p>
317
			<label class="monsterinsights-label-block"
318
				   for="<?php echo esc_attr( $this->get_field_id( $name ) ); ?>">
319
				<?php echo esc_html( $label ); ?>
320
			</label>
321
			<input type="text"
322
				   id="<?php echo esc_attr( $this->get_field_id( $name ) ); ?>"
323
				   name="<?php echo esc_attr( $this->get_field_name( $name ) ); ?>"
324
				   value="<?php echo esc_attr( $instance[ $name ] ); ?>"
325
				   class="widefat monsterinsights-color-field"/>
326
		</p>
327
		<?php
328
	}
329
330
	/**
331
	 * Regular text input.
332
	 *
333
	 * @param string $name Name of the input, for saving/loading.
334
	 * @param string $label Label of the element.
335
	 * @param array  $instance The current widget instance.
336
	 */
337
	public function text_input( $name, $label, $instance ) {
338
		?>
339
		<p>
340
			<label for="<?php echo esc_attr( $this->get_field_id( $name ) ); ?>">
341
				<?php echo esc_html( $label ); ?>
342
			</label>
343
			<input type="text"
344
				   id="<?php echo esc_attr( $this->get_field_id( $name ) ); ?>"
345
				   name="<?php echo esc_attr( $this->get_field_name( $name ) ); ?>"
346
				   value="<?php echo esc_attr( $instance[ $name ] ); ?>" class="widefat"/>
347
		</p>
348
		<?php
349
	}
350
351
	/**
352
	 * Size input - used for font size inputs.
353
	 *
354
	 * @param string $name Name of the input, for saving/loading.
355
	 * @param string $label Label of the element.
356
	 * @param array  $instance The current widget instance.
357
	 * @param array  $range The options available to select.
358
	 */
359
	public function size_input( $name, $label, $instance, $range = array() ) {
360
		?>
361
		<p>
362
			<label for="<?php echo esc_attr( $this->get_field_id( $name ) ); ?>">
363
				<?php echo esc_html( $label ); ?>
364
			</label>
365
			<select id="<?php echo esc_attr( $this->get_field_id( $name ) ); ?>"
366
					name="<?php echo esc_attr( $this->get_field_name( $name ) ); ?>" class="widefat">
367
				<?php foreach ( $range as $font_size ) { ?>
368
					<option value="<?php echo absint( $font_size ); ?>" <?php selected( $instance[ $name ], $font_size ); ?>><?php printf( esc_html_x( '%dpx', 'google-analytics-for-wordpress' ), $font_size ); ?></option>
369
				<?php } ?>
370
			</select>
371
		</p>
372
		<?php
373
	}
374
375
	/**
376
	 * Prepare theme specific options.
377
	 *
378
	 * @param array $theme The theme options.
379
	 */
380
	public function prepare_defaults_from_theme( $theme ) {
381
		foreach ( $this->conditional_options as $key => $obj ) {
382
			if ( ! empty( $theme['styles'][ $obj[0] ][ $obj[1] ] ) ) {
383
				$this->defaults[ $key ] = $theme['styles'][ $obj[0] ][ $obj[1] ];
384
			}
385
		}
386
	}
387
388
	/**
389
	 * Load specific widget scripts in the admin.
390
	 */
391
	public function add_scripts() {
392
		add_action( 'admin_enqueue_scripts', array( $this, 'load_widget_scripts' ) );
393
	}
394
395
	/**
396
	 * Load admin-specific widget scripts.
397
	 */
398
	public function load_widget_scripts() {
399
400
		$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : false;
0 ignored issues
show
Bug introduced by
Are you sure the usage of get_current_screen() is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
401
402
		if ( ! isset( $screen->id ) || 'widgets' !== $screen->id ) {
0 ignored issues
show
Bug introduced by
The property id does not exist on false.
Loading history...
403
			return;
404
		}
405
406
407
		$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
408
		wp_enqueue_style( 'monsterinsights-admin-widget-setting-styles', plugins_url( 'assets/css/admin-widget-settings' . $suffix . '.css', MONSTERINSIGHTS_PLUGIN_FILE ), array(
409
			'wp-color-picker',
410
		), monsterinsights_get_asset_version() );
411
412
413
		wp_register_script( 'monsterinsights-select2', plugins_url( 'assets/js/select2.min.js', MONSTERINSIGHTS_PLUGIN_FILE ), array(
414
			'jquery',
415
		), '4.0.13', true );
416
417
		wp_register_script( 'monsterinsights-admin-widget-settings', plugins_url( 'assets/js/admin-widget-settings' . $suffix . '.js', MONSTERINSIGHTS_PLUGIN_FILE ), array(
418
			'jquery',
419
			'wp-color-picker',
420
			'monsterinsights-select2',
421
		), monsterinsights_get_asset_version(), true );
422
		wp_enqueue_script( 'monsterinsights-admin-widget-settings' );
423
424
		wp_localize_script( 'monsterinsights-admin-widget-settings', 'monsterinsights_pp', array(
425
			'nonce' => wp_create_nonce( 'mi-admin-nonce' ),
426
		) );
427
	}
428
}
429