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
|
|
|
|
|
|
|
|
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
|
|
|
$types = simcal_get_calendar_types(); |
120
|
|
|
|
121
|
|
|
foreach ( $types as $calendar => $views ) { |
122
|
|
|
foreach( $views as $key => $view ) { |
|
|
|
|
123
|
|
|
|
124
|
|
|
$view = simcal_get_calendar_view( 0, $calendar . '-' . $view ); |
125
|
|
|
|
126
|
|
|
$scripts[] = $view->scripts( $this->min ); |
|
|
|
|
127
|
|
|
$styles[] = $view->styles( $this->min ); |
|
|
|
|
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$this->get_widgets_assets(); |
132
|
|
|
$this->scripts = apply_filters( 'simcal_front_end_scripts', $scripts, $this->min ); |
|
|
|
|
133
|
|
|
// First check if there is a multi-dimensional array of scripts |
134
|
|
|
if ( isset( $this->scripts[0] ) ) { |
135
|
|
|
foreach ( $this->scripts as $script ) { |
136
|
|
|
$this->load_scripts ( $script ); |
|
|
|
|
137
|
|
|
} |
138
|
|
|
} else { |
139
|
|
|
$this->load_scripts( $this->scripts ); |
140
|
|
|
} |
141
|
|
|
$this->styles = apply_filters( 'simcal_front_end_styles', $styles, $this->min ); |
|
|
|
|
142
|
|
|
// First check if there is a multi-dimensional array of styles |
143
|
|
|
if ( isset( $this->styles[0] ) ) { |
144
|
|
|
foreach( $this->styles as $style ) { |
|
|
|
|
145
|
|
|
$this->load_styles( $style ); |
146
|
|
|
} |
147
|
|
|
} else { |
148
|
|
|
$this->load_styles( $this->styles ); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Get widgets assets. |
154
|
|
|
* |
155
|
|
|
* @since 3.0.0 |
156
|
|
|
*/ |
157
|
|
|
public function get_widgets_assets() { |
158
|
|
|
|
159
|
|
|
$widgets = get_option( 'widget_gce_widget' ); |
160
|
|
|
|
161
|
|
|
if ( ! empty( $widgets ) && is_array( $widgets ) ) { |
162
|
|
|
|
163
|
|
|
foreach ( $widgets as $settings ) { |
164
|
|
|
|
165
|
|
|
if ( ! empty( $settings ) && is_array( $settings ) ) { |
166
|
|
|
|
167
|
|
|
if ( isset( $settings['calendar_id'] ) ) { |
168
|
|
|
|
169
|
|
|
$view = simcal_get_calendar_view( absint( $settings['calendar_id'] ) ); |
170
|
|
|
|
171
|
|
|
if ( $view instanceof Calendar_View ) { |
172
|
|
|
add_filter( 'simcal_front_end_scripts', function ( $scripts, $min ) use ( $view ) { |
173
|
|
|
return array_merge( $scripts, $view->scripts( $min ) ); |
174
|
|
|
}, 100, 2 ); |
175
|
|
|
add_filter( 'simcal_front_end_styles', function ( $styles, $min ) use ( $view ) { |
176
|
|
|
return array_merge( $styles, $view->styles( $min ) ); |
177
|
|
|
}, 100, 2 ); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Scripts. |
190
|
|
|
* |
191
|
|
|
* @since 3.0.0 |
192
|
|
|
* |
193
|
|
|
* @param array $scripts |
194
|
|
|
*/ |
195
|
|
|
public function load_scripts( $scripts ) { |
196
|
|
|
|
197
|
|
|
// Only load if not disabled in the settings |
198
|
|
|
if ( ! empty( $scripts ) && is_array( $scripts ) ) { |
199
|
|
|
|
200
|
|
|
foreach ( $scripts as $script => $v ) { |
201
|
|
|
|
202
|
|
|
if ( ! empty( $v['src'] ) ) { |
203
|
|
|
|
204
|
|
|
$src = esc_url( $v['src'] ); |
205
|
|
|
$deps = isset( $v['deps'] ) ? $v['deps'] : array(); |
206
|
|
|
$ver = isset( $v['ver'] ) ? $v['ver'] : SIMPLE_CALENDAR_VERSION; |
207
|
|
|
$in_footer = isset( $v['in_footer'] ) ? $v['in_footer'] : false; |
208
|
|
|
|
209
|
|
|
wp_enqueue_script( $script, $src, $deps, $ver, $in_footer ); |
210
|
|
|
|
211
|
|
|
if ( ! empty( $v['localize'] ) && is_array( $v['localize'] ) ) { |
212
|
|
|
foreach ( $v['localize'] as $object => $l10n ) { |
213
|
|
|
wp_localize_script( $script, $object, $l10n ); |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
} elseif ( is_string( $v ) && ! empty( $v ) ) { |
218
|
|
|
|
219
|
|
|
wp_enqueue_script( $v ); |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Styles. |
228
|
|
|
* |
229
|
|
|
* @since 3.0.0 |
230
|
|
|
* |
231
|
|
|
* @param array $styles |
232
|
|
|
*/ |
233
|
|
|
public function load_styles( $styles ) { |
234
|
|
|
|
235
|
|
|
// Only load if not disabled in the settings |
236
|
|
|
if ( ! empty( $styles ) && is_array( $styles ) && false === $this->disable_styles ) { |
237
|
|
|
|
238
|
|
|
foreach ( $styles as $style => $v ) { |
239
|
|
|
|
240
|
|
|
if ( ! empty( $v['src'] ) ) { |
241
|
|
|
|
242
|
|
|
$src = esc_url( $v['src'] ); |
243
|
|
|
$deps = isset( $v['deps'] ) ? $v['deps'] : array(); |
244
|
|
|
$ver = isset( $v['ver'] ) ? $v['ver'] : SIMPLE_CALENDAR_VERSION; |
245
|
|
|
$media = isset( $v['media'] ) ? $v['media'] : 'all'; |
246
|
|
|
|
247
|
|
|
wp_enqueue_style( $style, $src, $deps, $ver, $media ); |
248
|
|
|
|
249
|
|
|
} elseif ( is_string( $v ) && ! empty( $v ) ) { |
250
|
|
|
|
251
|
|
|
wp_enqueue_style( $v ); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|