-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathawesplash.php
490 lines (404 loc) · 14.4 KB
/
awesplash.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
<?php
/**
* Plugin Name: AweSplash - Just Splash Page
* Plugin URI: https://wordpress.org/plugins/awesplash/
* Description: A splash page for your WordPress site.
* Version: 1.0.3
* Author: Awethemes
* Author URI: http://awethemes.com/
* License: GNU General Public License v3 or later
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*
* Requires at least: 4.3
* Tested up to: 4.9
* Text Domain: awesplash
* Domain Path: /languages/
*
* @package awesplash
*/
if ( !class_exists( 'AweSplash' ) ) {
final class AweSplash {
private $is_acitve;
function __construct() {
$this->is_acitve = absint( get_theme_mod( 'awesplash_enable', 0 ) );
$this->defined();
$this->hook();
$this->includes();
do_action( 'awesplash_loaded' );
}
/**
* Allow to display splash page
* @return bool
*/
public function is_allow() {
if ( !$this->is_acitve ) {
return false;
}
if ( isset( $_COOKIE['awesplash'] ) && sanitize_text_field( $_COOKIE['awesplash'] ) == 'yes' ) {
return false;
}
if ( esc_attr( get_theme_mod( 'awesplash_display_type', '' ) ) == '' && !is_front_page() ) {
return false;
}
return true;
}
/**
* The single instance of the class.
*
* @var awesplash
* @since 1.0.0
*/
protected static $_instance = null;
/**
* Main Awe Splash Instance.
*
* Ensures only one instance of AweSplash is loaded or can be loaded.
*
* @since 1.0.0
* @static
* @see awesplash()
* @return AweSplash - Main instance.
*/
public static function instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new self();
}
return self::$_instance;
}
/**
* Call functions to WordPress hooks
* @since 1.0.0
* @return void
*/
public function hook() {
add_action( 'plugins_loaded', array( $this, 'load_plugin_textdomain' ) );
add_action( 'customize_save', array( $this, 'customize_save' ) );
add_action( 'customize_preview_init', array( $this, 'customize_preview_js' ) );
add_action( 'customize_controls_print_scripts', array( $this, 'customize_controls_js' ) );
add_action( 'wp_ajax_awesplash_enable', array( $this, 'enable' ) );
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'add_action_links' ) );
if ( $this->is_acitve ) {
add_action( 'template_include', array( $this, 'splashpage' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
add_action( 'wp_print_styles', array( $this, 'remove_styles' ), 100 );
add_action( 'wp_print_scripts', array( $this, 'remove_scripts' ), 100 );
add_action( 'wp_footer', 'awesplash_custom_js', 999 );
}
}
/**
* Clear cache for custom resource
*/
public function customize_save() {
delete_transient( 'awesplash_custom_css' );
delete_transient( 'awesplash_custom_font_url' );
}
/**
* Ajax turn on/off splash page
*/
public function enable() {
if ( !isset( $_POST['enable'] ) ) {
wp_send_json_error( 'invalid_value' );
}
if ( !check_ajax_referer( 'awesplash_enable', 'nonce', false ) ) {
wp_send_json_error( 'invalid_nonce' );
}
$enable = absint( $_POST['enable'] );
set_theme_mod( 'awesplash_enable', $enable );
setcookie( 'awesplash', '', time() - 3600, '/' );
wp_send_json_success( $enable );
}
/**
* Render page
* @since 1.0.0
*/
public function splashpage( $template ) {
if ( !$this->is_allow() ) {
return $template;
}
$no_validate = !get_theme_mod( 'awesplash_age_enable', 0 ) && !get_theme_mod( 'awesplash_opt_enable', 1 );
if ( !isset( $_COOKIE['awesplash'] ) && $no_validate && !is_customize_preview() ) {
$expiration = time() + (DAY_IN_SECONDS * absint( get_theme_mod( 'awesplash_expire_days', 30 ) ));
setCookie( 'awesplash', 'viewed', $expiration, '/' );
} else if ( isset( $_COOKIE['awesplash'] ) && sanitize_text_field( $_COOKIE['awesplash'] ) == 'viewed' && $no_validate ) {
$expiration = time() + (DAY_IN_SECONDS * absint( get_theme_mod( 'awesplash_expire_days', 30 ) ));
setCookie( 'awesplash', 'yes', $expiration, '/' );
$actual_link = awesplash_get_current_url();
wp_redirect( esc_url( $actual_link ) );
exit;
}
$template = get_template_directory() . '/splash-page.php';
if ( file_exists( $template ) ) {
return $template;
}
return apply_filters( 'awesplash_template', AWESPLASH_DIR . 'templates/' ) . 'splash-page.php';
}
public function local_var() {
$arr = array(
'background_color' => sanitize_hex_color( get_theme_mod( 'awesplash_background_color' ) ),
'heading_color' => sanitize_hex_color( get_theme_mod( 'awesplash_heading_color' ) ),
'content_color' => sanitize_hex_color( get_theme_mod( 'awesplash_content_color' ) ),
'button_color' => sanitize_hex_color( get_theme_mod( 'awesplash_button_color' ) ),
'button_color_hover' => sanitize_hex_color( get_theme_mod( 'awesplash_button_color_hover' ) ),
'button_bgcolor' => sanitize_hex_color( get_theme_mod( 'awesplash_button_bgcolor' ) ),
'button_bgcolor_hover' => sanitize_hex_color( get_theme_mod( 'awesplash_button_bgcolor_hover' ) ),
'confirm_on' => __( 'You need to reload the page to show splash page. Press OK to reload.', 'awesplash' ),
'confirm_off' => __( 'You need to reload the page to turn off splash page. Press OK to reload.', 'awesplash' ),
'nonce' => wp_create_nonce( 'awesplash_enable' ),
'ajaxurl' => admin_url( 'admin-ajax.php' ),
);
return apply_filters( 'awesplash_local_var', $arr );
}
/**
* Bind JS handlers to instantly live-preview changes.
* @since 1.0.0
*/
public function customize_controls_js() {
wp_enqueue_script( 'awesplash-customize-controls', AWESPLASH_URL . 'assets/js/customize-controls.js', array( 'customize-preview' ), AWESPLASH_VER, true );
wp_localize_script( 'awesplash-customize-controls', 'awesplash_var', $this->local_var() );
}
/**
* Bind JS handlers to instantly live-preview changes.
* @since 1.0.0
*/
public function customize_preview_js() {
wp_enqueue_script( 'awesplash-customize-preview', AWESPLASH_URL . 'assets/js/customize-preview.js', array( 'customize-preview' ), AWESPLASH_VER, true );
wp_localize_script( 'awesplash-customize-preview', 'awesplash_var', $this->local_var() );
}
/**
* Register Splash page font url
* @since 1.0.0
* @return string Font url
*/
public function font_url() {
$fonts_url = '';
$font_families = array();
$open_san = esc_html_x( 'on', 'Montserrat font', 'awesplash' );
if ( 'off' !== $open_san ) {
$font_families[] = 'Montserrat:300,300i,400,400i,500,500i,600,600i,700,700i';
}
if ( !empty( $font_families ) ) {
$query_args = array(
'family' => urlencode( implode( '|', $font_families ) ),
'subset' => urlencode( 'latin,latin-ext' ),
);
$fonts_url = add_query_arg( $query_args, 'https://fonts.googleapis.com/css' );
$fonts_url = apply_filters( 'awesplash_fonts_url', $fonts_url );
}
return esc_url_raw( $fonts_url );
}
/**
* Register custom font url
* @since 1.0.0
* @return string|bool Font url or False
*/
public function custom_font_url() {
$fonts_url = WP_DEBUG ? '' : get_transient( 'awesplash_custom_font_url' );
if ( empty( $fonts_url ) || is_customize_preview() ) {
$fonts_url = awesplash_get_font_url( array(
'awesplash_heading_typo',
'awesplash_content_typo',
'awesplash_button_typo',
'awesplash_opt_typo',
'awesplash_age_typo',
) );
if ( !WP_DEBUG ) {
set_transient( 'awesplash_custom_font_url', $fonts_url );
}
}
return esc_url_raw( $fonts_url );
}
/**
* Register scripts and style for splash page
* @since 1.0.0
*/
public function enqueue_scripts() {
if ( !$this->is_allow() ) {
return;
}
wp_enqueue_style( 'awesplash-fonts', $this->font_url(), array(), null );
wp_enqueue_style( 'awesplash-custom-fonts', $this->custom_font_url(), array(), null );
wp_enqueue_style( 'awesplash-style', AWESPLASH_URL . 'assets/css/main.css', array(), AWESPLASH_VER );
wp_enqueue_script( 'modernizr', AWESPLASH_URL . 'assets/js/vendor/modernizr-2.8.3.min.js', array(), AWESPLASH_VER, false );
wp_enqueue_script( 'slick', AWESPLASH_URL . 'assets/js/plugins/slick.min.js', array( 'jquery' ), AWESPLASH_VER, true );
wp_enqueue_script( 'ytplayer', AWESPLASH_URL . 'assets/js/plugins/jquery.mb.YTPlayer.min.js', array(), AWESPLASH_VER, true );
wp_enqueue_script( 'vimeoplayer', AWESPLASH_URL . 'assets/js/plugins/jquery.mb.vimeo_player.min.js', array(), AWESPLASH_VER, true );
wp_enqueue_script( 'animate-headline', AWESPLASH_URL . 'assets/js/plugins/animate-headline.js', array( 'jquery' ), AWESPLASH_VER, true );
wp_enqueue_script( 'awesplash-main', AWESPLASH_URL . 'assets/js/main.js', array( 'jquery' ), AWESPLASH_VER, true );
if ( $custom_css = awesplash_custom_css() ) {
wp_add_inline_style( 'awesplash-style', $custom_css );
}
if ( $custom_css = get_theme_mod( 'awesplash_custom_css', '' ) ) {
wp_add_inline_style( 'awesplash-fonts', $custom_css );
}
wp_localize_script( 'awesplash-main', 'awesplash', array(
'redirect_url' => sanitize_text_field( get_theme_mod( 'awesplash_redirect_url' ) ),
'redirect_time' => absint( get_theme_mod( 'awesplash_redirect_time' ) )
) );
}
/**
* Include functions
* @since 1.0.0
*/
public function includes() {
require AWESPLASH_DIR . 'includes/ctoolkit/ctoolkit.php';
require AWESPLASH_DIR . 'includes/helper-functions.php';
require AWESPLASH_DIR . 'includes/customizer-functions.php';
require AWESPLASH_DIR . 'includes/template-tags.php';
if ( $this->is_acitve ) {
require AWESPLASH_DIR . 'includes/class-awesplash-handle.php';
}
}
/**
* Defined
*/
public function defined() {
define( 'AWESPLASH_URL', plugin_dir_url( __FILE__ ) );
define( 'AWESPLASH_DIR', plugin_dir_path( __FILE__ ) );
define( 'AWESPLASH_VER', '1.0.4' );
}
/**
* Load Local files.
* @since 1.0.0
* @return void
*/
public function load_plugin_textdomain() {
/**
* Set filter for plugin's languages directory
*/
$dir = AWESPLASH_DIR . 'languages/';
$dir = apply_filters( 'awesplash_languages_directory', $dir );
/**
* Traditional WordPress plugin locale filter
*/
$locale = apply_filters( 'plugin_locale', get_locale(), 'awesplash' );
$mofile = sprintf( '%1$s-%2$s.mo', 'awesplash', $locale );
/**
* Setup paths to current locale file
*/
$mofile_local = $dir . $mofile;
$mofile_global = WP_LANG_DIR . '/awesplash/' . $mofile;
if ( file_exists( $mofile_global ) ) {
/**
* Look in global /wp-content/languages/awesplash
*/
load_textdomain( 'awesplash', $mofile_global );
} elseif ( file_exists( $mofile_local ) ) {
/**
* Look in local /wp-content/plugins/awesplash/languages/
*/
load_textdomain( 'awesplash', $mofile_local );
} else {
/**
* Load the default language files
*/
load_plugin_textdomain( 'awesplash', false, $dir );
}
}
/**
* Just use awesplash page style
* @since 1.0.0
*/
public function remove_styles() {
if ( $this->is_allow() && !is_admin() ) {
global $wp_styles;
$wp_styles->queue = array(
'admin-bar',
'awesplash-fonts',
'awesplash-custom-fonts',
'awesplash-style',
'animate-headline',
'customize-preview',
'awesplash-customize-controls',
'wp-mediaelement'
);
}
}
/**
* Just use awesplash page scripts
* @since 1.0.0
*/
public function remove_scripts() {
if ( $this->is_allow() && !is_admin() ) {
global $wp_scripts;
$wp_scripts->queue = array(
'admin-bar',
'modernizr',
'slick',
'ytplayer',
'vimeoplayer',
'animate-headline',
'awesplash-main',
'html5',
'customize-preview',
'awesplash-customize-controls',
'awesplash-customize-preview',
'wp-mediaelement',
'froogaloop',
'customize-selective-refresh',
'customize-preview-widgets',
'customize-preview-nav-menus'
);
/**
* Remove all action in wp_footer except the default
*/
global $wp_filter;
global $wp_version;
$callbacks = $wp_version > 4.3 ? $wp_filter['wp_footer']->callbacks : $wp_filter['wp_footer'];
$customizer_preview = array();
if ( is_customize_preview() ) {
$customizer_preview = $callbacks[1];
}
$save20 = array();
$save1000 = array();
foreach ( $callbacks[20] as $key => $value ) {
if ( $key == 'wp_print_footer_scripts' || strpos( $key, 'customize_preview_settings' ) >= 0 ) {
$save20[$key] = $value;
}
}
foreach ( $callbacks[1000] as $key => $value ) {
if ( $key == 'wp_admin_bar_render' || strpos( $key, 'export_preview_data' ) >= 0 ) {
$save20[$key] = $value;
}
}
if ( $wp_version > 4.3 ) {
if ( isset( $callbacks[1] ) ) {
$wp_filter['wp_footer']->callbacks[1] = $callbacks[1];
}
$wp_filter['wp_footer']->callbacks[20] = $save20;
$wp_filter['wp_footer']->callbacks[1000] = $save1000;
} else {
if ( isset( $callbacks[1] ) ) {
$wp_filter['wp_footer'][1] = $callbacks[1];
}
$wp_filter['wp_footer'][20] = $save20;
$wp_filter['wp_footer'][1000] = $save1000;
}
}
}
/**
* Add setting link
* @return array
*/
public function add_action_links( $links ) {
$plugin_links = array(
'page' => '<a href="' . esc_url( apply_filters( 'awesplash_settings_url', admin_url( 'customize.php' ) ) ) . '" aria-label="' . esc_attr__( 'Settings', 'awesplash' ) . '">' . esc_html__( 'Settings', 'awesplash' ) . '</a>',
);
return array_merge( $links, $plugin_links );
}
}
/**
* Main instance of AweSplash.
*
* Returns the main instance of awesplash to prevent the need to use globals.
*
* @since 1.0.0
* @return awesplash
*/
function awesplash() {
return AweSplash::instance();
}
/**
* Global for backwards compatibility.
*/
$GLOBALS['awesplash'] = awesplash();
}