Completed
Push — master ( ef9b46...d54d1c )
by
unknown
08:53
created

Assets::__construct()   B

Complexity

Conditions 9
Paths 108

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 20
rs 7.4054
cc 9
eloc 11
nc 108
nop 0
1
<?php
2
/**
3
 * Front End Assets
4
 *
5
 * @package SimpleCalendar;
6
 */
7
namespace SimpleCalendar;
8
9
use SimpleCalendar\Abstracts\Calendar_View;
10
11
if ( ! defined( 'ABSPATH' ) ) {
12
	exit;
13
}
14
15
/**
16
 * Front end scripts and styles.
17
 *
18
 * Loads scripts and styles based on the requested calendar view.
19
 *
20
 * @since 3.0.0
21
 */
22
class Assets {
23
24
	/**
25
	 * Load minified assets.
26
	 *
27
	 * @access private
28
	 * @var string
29
	 */
30
	private $min = '.min';
31
32
	/**
33
	 * Scripts.
34
	 *
35
	 * @access private
36
	 * @var array
37
	 */
38
	private $scripts = array();
39
40
	/**
41
	 * Styles.
42
	 *
43
	 * @access private
44
	 * @var array
45
	 */
46
	private $styles = array();
47
48
	/**
49
	 * Disable scripts.
50
	 *
51
	 * @access public
52
	 * @var bool
53
	 */
54
	public $disable_scripts = false;
55
56
	/**
57
	 * Disable styles.
58
	 *
59
	 * @access public
60
	 * @var bool
61
	 */
62
	public $disable_styles = false;
63
64
	/**
65
	 * Disable styles.
66
	 *
67
	 * @access public
68
	 * @var bool
69
	 */
70
	public $always_enqueue = false;
71
72
	/**
73
	 * Hook in tabs.
74
	 *
75
	 * @since 3.0.0
76
	 */
77
	public function __construct() {
78
79
		$this->min = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG == true ) ? '' : '.min';
80
81
		$settings = get_option( 'simple-calendar_settings_advanced' );
82
		if ( isset( $settings['assets']['disable_js'] ) ) {
83
			$this->disable_scripts = 'yes' == $settings['assets']['disable_js'] ? true : false;
84
		}
85
86
		if ( isset( $settings['assets']['disable_css'] ) ) {
87
			$this->disable_styles = 'yes' == $settings['assets']['disable_css'] ? true : false;
88
		}
89
90
		if ( isset( $settings['assets']['always_enqueue'] ) ) {
91
			$this->always_enqueue = 'yes' == $settings['assets']['always_enqueue'] ? true : false;
92
		}
93
94
		add_action( 'init', array( $this, 'register' ), 20 );
95
		add_action( 'init', array( $this, 'enqueue' ), 40 );
96
	}
97
98
	/**
99
	 * Register scripts and styles.
100
	 *
101
	 * @since 3.0.0
102
	 */
103
	public function register() {
104
		do_action( 'simcal_register_assets', $this->min );
105
	}
106
107
	/**
108
	 * Enqueue scripts and styles.
109
	 *
110
	 * @since 3.0.0
111
	 */
112
	public function enqueue() {
113
114
		add_action( 'wp_enqueue_scripts', array( $this, 'load' ), 10 );
115
116
		do_action( 'simcal_enqueue_assets', $this->min );
117
118
		if ( false === $this->disable_scripts ) {
119
			$min = $this->min;
120
			// Improves compatibility with themes and plugins using Isotope and Masonry.
121
			add_action( 'wp_enqueue_scripts',
122
				function () use ( $min ) {
123
					if ( wp_script_is( 'simcal-qtip', 'enqueued' ) ) {
124
						wp_enqueue_script(
125
							'simplecalendar-imagesloaded',
126
							SIMPLE_CALENDAR_ASSETS . 'js/vendor/imagesloaded' . $min . '.js',
127
							array( 'simcal-qtip' ),
128
							'3.1.8',
129
							true
130
						);
131
					}
132
				}, 1000 );
133
		}
134
	}
135
136
	/**
137
	 * Load scripts and styles.
138
	 *
139
	 * @since 3.0.0
140
	 */
141
	public function load() {
142
143
		if ( $this->always_enqueue ) {
144
			$scripts = $this->get_default_scripts();
145
			$styles  = $this->get_default_styles();
146
147
			$this->scripts = apply_filters( 'simcal_front_end_scripts', $scripts, $this->min );
148
			$this->styles  = apply_filters( 'simcal_front_end_styles', $styles, $this->min );
149
150
			$this->load_scripts( $this->scripts );
151
			$this->load_styles( $this->styles );
152
153
			return;
154
		}
155
156
		$id = 0;
0 ignored issues
show
Unused Code introduced by
$id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
157
		$cal_id = array();
158
		$scripts = $styles = array();
159
160
		if ( is_singular() ) {
161
162
			global $post, $post_type;
163
164
			if ( 'calendar' == $post_type ) {
165
166
				$id = get_queried_object_id();
167
168
				$view = simcal_get_calendar_view( $id );
169
				if ( $view instanceof Calendar_View ) {
170
					$scripts[] = $view->scripts( $this->min );
171
					$styles[]  = $view->styles( $this->min );
172
				}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
173
174
			} else {
175
176
				$id = absint( get_post_meta( $post->ID, '_simcal_attach_calendar_id', true ) );
177
178
				if ( $id === 0 ) {
0 ignored issues
show
introduced by
Found "=== 0". Use Yoda Condition checks, you must
Loading history...
179
180
					preg_match_all( '/' . get_shortcode_regex() . '/s', $post->post_content, $matches, PREG_SET_ORDER );
181
182
					if ( ! empty( $matches ) && is_array( $matches ) ) {
183
						foreach ( $matches as $shortcode ) {
184
							if ( 'calendar' === $shortcode[2] || 'gcal' === $shortcode[2] ) {
185
								$atts = shortcode_parse_atts( $shortcode[3] );
186
								$cal_id[]   = isset( $atts['id'] ) ? intval( $atts['id'] ) : 0;
187
							}
188
						}
189
					}
190
				}
191
			}
192
		}
193
194
		foreach( $cal_id as $i ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
195
196
			if ( $i > 0 ) {
197
198
				$view = simcal_get_calendar_view( $i );
199
200
				if ( $view instanceof Calendar_View ) {
201
					$scripts[] = $view->scripts( $this->min );
202
					$styles[] = $view->styles( $this->min );
203
				}
204
			}
205
		}
206
207
		$this->get_widgets_assets();
208
209
		$this->scripts = apply_filters( 'simcal_front_end_scripts', $scripts, $this->min );
210
211
		// First check if there is a multi-dimensional array of scripts
212
		if ( isset( $this->scripts[0] ) ) {
213
			foreach ( $this->scripts as $script ) {
214
				$this->load_scripts ( $script );
0 ignored issues
show
Coding Style introduced by
Space before opening parenthesis of function call prohibited
Loading history...
215
			}
216
		} else {
217
			$this->load_scripts( $this->scripts );
218
		}
219
220
		$this->styles = apply_filters( 'simcal_front_end_styles', $styles, $this->min );
221
222
		// First check if there is a multi-dimensional array of styles
223
		if ( isset( $this->styles[0] ) ) {
224
			foreach( $this->styles as $style ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
225
				$this->load_styles( $style );
226
			}
227
		} else {
228
			$this->load_styles( $this->styles );
229
		}
230
	}
231
232
	/**
233
	 * Get widgets assets.
234
	 *
235
	 * @since 3.0.0
236
	 */
237
	public function get_widgets_assets() {
238
239
		$widgets = get_option( 'widget_gce_widget' );
240
241
		if ( ! empty( $widgets ) && is_array( $widgets ) ) {
242
243
			foreach ( $widgets as $settings ) {
244
245
				if ( ! empty( $settings ) && is_array( $settings ) ) {
246
247
					if ( isset( $settings['calendar_id'] ) ) {
248
249
						$view = simcal_get_calendar_view( absint( $settings['calendar_id'] ) );
250
251
						if ( $view instanceof Calendar_View ) {
252
							add_filter( 'simcal_front_end_scripts', function ( $scripts, $min ) use ( $view ) {
253
								return array_merge( $scripts, $view->scripts( $min ) );
254
							}, 100, 2 );
255
							add_filter( 'simcal_front_end_styles', function ( $styles, $min ) use ( $view ) {
256
								return array_merge( $styles, $view->styles( $min ) );
257
							}, 100, 2 );
258
						}
1 ignored issue
show
introduced by
Blank line found after control structure
Loading history...
259
260
					}
1 ignored issue
show
introduced by
Blank line found after control structure
Loading history...
261
262
				}
263
			}
1 ignored issue
show
introduced by
Blank line found after control structure
Loading history...
264
265
		}
266
	}
267
268
	/**
269
	 * Scripts.
270
	 *
271
	 * @since 3.0.0
272
	 *
273
	 * @param array $scripts
274
	 */
275
	public function load_scripts( $scripts ) {
276
277
		// Only load if not disabled in the settings
278
		if ( ! empty( $scripts ) && is_array( $scripts ) && false === $this->disable_scripts ) {
279
280
			foreach ( $scripts as $script => $v ) {
281
282
				if ( ! empty( $v['src'] ) ) {
283
284
					$src        = esc_url( $v['src'] );
285
					$deps       = isset( $v['deps'] )        ? $v['deps']       : array();
286
					$ver        = isset( $v['ver'] )         ? $v['ver']        : SIMPLE_CALENDAR_VERSION;
287
					$in_footer  = isset( $v['in_footer'] )   ? $v['in_footer']  : false;
288
289
					wp_enqueue_script( $script, $src, $deps, $ver, $in_footer );
290
291
					if ( ! empty( $v['localize'] ) && is_array( $v['localize'] ) ) {
292
						foreach ( $v['localize'] as $object => $l10n ) {
293
							wp_localize_script( $script, $object, $l10n );
294
						}
295
					}
1 ignored issue
show
introduced by
Blank line found after control structure
Loading history...
296
297
				} elseif ( is_string( $v ) && ! empty( $v ) ) {
298
299
					wp_enqueue_script( $v );
300
				}
301
			}
1 ignored issue
show
introduced by
Blank line found after control structure
Loading history...
302
303
		}
304
	}
305
306
	/**
307
	 * Styles.
308
	 *
309
	 * @since 3.0.0
310
	 *
311
	 * @param array $styles
312
	 */
313
	public function load_styles( $styles ) {
314
315
		// Only load if not disabled in the settings
316
		if ( ! empty( $styles ) && is_array( $styles ) && false === $this->disable_styles ) {
317
318
			foreach ( $styles as $style => $v ) {
319
320
				if ( ! empty( $v['src'] ) ) {
321
322
					$src    = esc_url( $v['src'] );
323
					$deps   = isset( $v['deps'] )   ? $v['deps']    : array();
324
					$ver    = isset( $v['ver'] )    ? $v['ver']     : SIMPLE_CALENDAR_VERSION;
325
					$media  = isset( $v['media'] )  ? $v['media']   : 'all';
326
327
					wp_enqueue_style( $style, $src, $deps, $ver, $media );
328
329
				} elseif ( is_string( $v ) && ! empty( $v ) ) {
330
331
					wp_enqueue_style( $v );
332
				}
1 ignored issue
show
introduced by
Blank line found after control structure
Loading history...
333
334
			}
1 ignored issue
show
introduced by
Blank line found after control structure
Loading history...
335
336
		}
337
	}
338
339
	/**
340
	 * Return the default scripts that are loaded. Used mainly for the always enqueue scripts option.
341
	 *
342
	 * This can be improved.
343
	 */
344
	public function get_default_scripts() {
345
		return array(
346
			'simcal-qtip' => array(
347
				'src'       => SIMPLE_CALENDAR_ASSETS . 'js/vendor/qtip' . $this->min . '.js',
348
				'deps'      => array( 'jquery' ),
349
				'ver'       => '2.2.1',
350
				'in_footer' => true,
351
			),
352
			'simcal-default-calendar' => array(
353
				'src'       => SIMPLE_CALENDAR_ASSETS . 'js/default-calendar' . $this->min . '.js',
354
				'deps'      => array(
355
					'jquery',
356
					'simcal-qtip',
357
				),
358
				'var'       => SIMPLE_CALENDAR_VERSION,
359
				'in_footer' => true,
360
				'localize'  => array(
361
					'simcal_default_calendar' => simcal_common_scripts_variables(),
362
				),
363
			),
364
		);
365
	}
366
367
	/**
368
	 * Return the default styles that are loaded. Used mainly for the always enqueue scripts option.
369
	 *
370
	 * This can be improved.
371
	 */
372
	public function get_default_styles() {
373
		return array(
374
			'simcal-qtip' => array(
375
				'src'   => SIMPLE_CALENDAR_ASSETS . 'css/vendor/qtip' . $this->min . '.css',
376
				'ver'   => '2.2.1',
377
				'media' => 'all',
378
			),
379
			'simcal-default-calendar-grid' => array(
380
				'src'   => SIMPLE_CALENDAR_ASSETS . 'css/default-calendar-grid' . $this->min . '.css',
381
				'deps'  => array(
382
					'simcal-qtip',
383
				),
384
				'ver'   => SIMPLE_CALENDAR_VERSION,
385
				'media' => 'all',
386
			),
387
			'simcal-default-calendar-list' => array(
388
				'src'   => SIMPLE_CALENDAR_ASSETS . 'css/default-calendar-list' . $this->min . '.css',
389
				'deps'  => array(
390
					'simcal-qtip',
391
				),
392
				'ver'   => SIMPLE_CALENDAR_VERSION,
393
				'media' => 'all',
394
			),
395
		);
396
	}
397
398
}
399