Completed
Push — master ( a00b53...3dc4d8 )
by
unknown
21:18
created

Assets::get_default_styles()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 25
rs 8.8571
cc 1
eloc 18
nc 1
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 styles.
50
	 *
51
	 * @access public
52
	 * @var bool
53
	 */
54
	public $disable_styles = false;
55
56
	/**
57
	 * Hook in tabs.
58
	 *
59
	 * @since 3.0.0
60
	 */
61
	public function __construct() {
62
63
		$this->min = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG == true ) ? '' : '.min';
64
65
		$settings = get_option( 'simple-calendar_settings_advanced' );
66
67
		if ( isset( $settings['assets']['disable_css'] ) ) {
68
			$this->disable_styles = 'yes' == $settings['assets']['disable_css'] ? true : false;
69
		}
70
71
		add_action( 'init', array( $this, 'register' ), 20 );
72
		add_action( 'init', array( $this, 'enqueue' ), 40 );
73
	}
74
75
	/**
76
	 * Register scripts and styles.
77
	 *
78
	 * @since 3.0.0
79
	 */
80
	public function register() {
81
		do_action( 'simcal_register_assets', $this->min );
82
	}
83
84
	/**
85
	 * Enqueue scripts and styles.
86
	 *
87
	 * @since 3.0.0
88
	 */
89
	public function enqueue() {
90
91
		add_action( 'wp_enqueue_scripts', array( $this, 'load' ), 10 );
92
93
		do_action( 'simcal_enqueue_assets', $this->min );
94
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
95
96
		$min = $this->min;
97
		// Improves compatibility with themes and plugins using Isotope and Masonry.
98
		add_action( 'wp_enqueue_scripts',
99
			function () use ( $min ) {
100
				if ( wp_script_is( 'simcal-qtip', 'enqueued' ) ) {
101
					wp_enqueue_script(
102
						'simplecalendar-imagesloaded',
103
						SIMPLE_CALENDAR_ASSETS . 'js/vendor/imagesloaded.pkgd' . $min . '.js',
104
						array( 'simcal-qtip' ),
105
						'3.1.8',
106
						true
107
					);
108
				}
109
			}, 1000 );
110
	}
111
112
	/**
113
	 * Load scripts and styles.
114
	 *
115
	 * @since 3.0.0
116
	 */
117
	public function load() {
118
119
		$scripts = $this->get_default_scripts();
120
		$styles  = $this->get_default_styles();
121
122
		$this->scripts = apply_filters( 'simcal_front_end_scripts', $scripts, $this->min );
123
		$this->styles  = apply_filters( 'simcal_front_end_styles', $styles, $this->min );
124
125
		$this->load_scripts( $this->scripts );
126
		$this->load_styles( $this->styles );
127
	}
128
129
	/**
130
	 * Get widgets assets.
131
	 *
132
	 * @since 3.0.0
133
	 */
134
	public function get_widgets_assets() {
135
136
		$widgets = get_option( 'widget_gce_widget' );
137
138
		if ( ! empty( $widgets ) && is_array( $widgets ) ) {
139
140
			foreach ( $widgets as $settings ) {
141
142
				if ( ! empty( $settings ) && is_array( $settings ) ) {
143
144
					if ( isset( $settings['calendar_id'] ) ) {
145
146
						$view = simcal_get_calendar_view( absint( $settings['calendar_id'] ) );
147
148
						if ( $view instanceof Calendar_View ) {
149
							add_filter( 'simcal_front_end_scripts', function ( $scripts, $min ) use ( $view ) {
150
								return array_merge( $scripts, $view->scripts( $min ) );
151
							}, 100, 2 );
152
							add_filter( 'simcal_front_end_styles', function ( $styles, $min ) use ( $view ) {
153
								return array_merge( $styles, $view->styles( $min ) );
154
							}, 100, 2 );
155
						}
156
157
					}
158
159
				}
160
			}
161
162
		}
163
	}
164
165
	/**
166
	 * Scripts.
167
	 *
168
	 * @since 3.0.0
169
	 *
170
	 * @param array $scripts
171
	 */
172
	public function load_scripts( $scripts ) {
173
174
		// Only load if not disabled in the settings
175
		if ( ! empty( $scripts ) && is_array( $scripts ) ) {
176
177
			foreach ( $scripts as $script => $v ) {
178
179
				if ( ! empty( $v['src'] ) ) {
180
181
					$src        = esc_url( $v['src'] );
182
					$deps       = isset( $v['deps'] )        ? $v['deps']       : array();
183
					$ver        = isset( $v['ver'] )         ? $v['ver']        : SIMPLE_CALENDAR_VERSION;
184
					$in_footer  = isset( $v['in_footer'] )   ? $v['in_footer']  : false;
185
186
					wp_enqueue_script( $script, $src, $deps, $ver, $in_footer );
187
188
					if ( ! empty( $v['localize'] ) && is_array( $v['localize'] ) ) {
189
						foreach ( $v['localize'] as $object => $l10n ) {
190
							wp_localize_script( $script, $object, $l10n );
191
						}
192
					}
193
194
				} elseif ( is_string( $v ) && ! empty( $v ) ) {
195
196
					wp_enqueue_script( $v );
197
				}
198
			}
199
200
		}
201
	}
202
203
	/**
204
	 * Styles.
205
	 *
206
	 * @since 3.0.0
207
	 *
208
	 * @param array $styles
209
	 */
210
	public function load_styles( $styles ) {
211
212
		// Only load if not disabled in the settings
213
		if ( ! empty( $styles ) && is_array( $styles ) && false === $this->disable_styles ) {
214
215
			foreach ( $styles as $style => $v ) {
216
217
				if ( ! empty( $v['src'] ) ) {
218
219
					$src    = esc_url( $v['src'] );
220
					$deps   = isset( $v['deps'] )   ? $v['deps']    : array();
221
					$ver    = isset( $v['ver'] )    ? $v['ver']     : SIMPLE_CALENDAR_VERSION;
222
					$media  = isset( $v['media'] )  ? $v['media']   : 'all';
223
224
					wp_enqueue_style( $style, $src, $deps, $ver, $media );
225
226
				} elseif ( is_string( $v ) && ! empty( $v ) ) {
227
228
					wp_enqueue_style( $v );
229
				}
230
231
			}
232
233
		}
234
	}
235
236
	/**
237
	 * Return the default scripts that are loaded. Used mainly for the always enqueue scripts option.
238
	 *
239
	 * This can be improved.
240
	 */
241
	public function get_default_scripts() {
242
		return array(
243
			'simcal-qtip' => array(
244
				'src'       => SIMPLE_CALENDAR_ASSETS . 'js/vendor/jquery.qtip' . $this->min . '.js',
245
				'deps'      => array( 'jquery' ),
246
				'ver'       => '2.2.1',
247
				'in_footer' => true,
248
			),
249
			'simcal-default-calendar' => array(
250
				'src'       => SIMPLE_CALENDAR_ASSETS . 'js/default-calendar' . $this->min . '.js',
251
				'deps'      => array(
252
					'jquery',
253
					'simcal-qtip',
254
				),
255
				'var'       => SIMPLE_CALENDAR_VERSION,
256
				'in_footer' => true,
257
				'localize'  => array(
258
					'simcal_default_calendar' => simcal_common_scripts_variables(),
259
				),
260
			),
261
		);
262
	}
263
264
	/**
265
	 * Return the default styles that are loaded. Used mainly for the always enqueue scripts option.
266
	 *
267
	 * This can be improved.
268
	 */
269
	public function get_default_styles() {
270
		return array(
271
			'simcal-qtip' => array(
272
				'src'   => SIMPLE_CALENDAR_ASSETS . 'css/vendor/jquery.qtip' . $this->min . '.css',
273
				'ver'   => '2.2.1',
274
				'media' => 'all',
275
			),
276
			'simcal-default-calendar-grid' => array(
277
				'src'   => SIMPLE_CALENDAR_ASSETS . 'css/default-calendar-grid' . $this->min . '.css',
278
				'deps'  => array(
279
					'simcal-qtip',
280
				),
281
				'ver'   => SIMPLE_CALENDAR_VERSION,
282
				'media' => 'all',
283
			),
284
			'simcal-default-calendar-list' => array(
285
				'src'   => SIMPLE_CALENDAR_ASSETS . 'css/default-calendar-list' . $this->min . '.css',
286
				'deps'  => array(
287
					'simcal-qtip',
288
				),
289
				'ver'   => SIMPLE_CALENDAR_VERSION,
290
				'media' => 'all',
291
			),
292
		);
293
	}
294
295
}
296