-
Notifications
You must be signed in to change notification settings - Fork 1
/
flat-preloader.php
163 lines (138 loc) · 5.27 KB
/
flat-preloader.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
<?php
/**
* The main file of the Flat Preloader
*
* @package flat-preloader
* @version 1.16.0
*
* Plugin Name: Flat Preloader
* Plugin URI: https://wordpress.org/plugins/flat-preloader/
* Description: Create preloading page with many various styles
* Version: 1.16.0
* Author: Thien Nguyen
* Author URI: https://thien.dev
* License: GPL2
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/
defined( 'ABSPATH' ) || die;
define( 'FLAT_PRELOADER_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
define( 'FLAT_PRELOADER_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'FLAT_PRELOADER_VERSION', '1.16.0' );
require_once dirname( __FILE__ ) . '/includes/utils.php';
require_once dirname( __FILE__ ) . '/flat-preloader-settings.php';
/**
* Add scripts and styles for plugin settings
*/
function flat_preloader_add_admin_scripts() {
wp_enqueue_style( 'flat-preloader-admin', untrailingslashit( FLAT_PRELOADER_PLUGIN_URL ) . '/assets/css/flat-preloader.css', array(), FLAT_PRELOADER_VERSION, 'all' );
}
add_action( 'admin_enqueue_scripts', 'flat_preloader_add_admin_scripts' );
/**
* Add scripts and styles for front page
*/
function flat_preloader_add_public_scripts() {
$settings = flat_preloader_get_settings();
$display = get_option( 'preloader-display' );
$ignoreLinks = apply_filters('flat_preloader_ignore_links', array(
'^https?:\/\/[^\/]+' . preg_quote( wp_unslash( $_SERVER['REQUEST_URI'] ), '/' ) . '(#.*)?$',
'^' . preg_quote( admin_url(), '/' ),
'^' . preg_quote( site_url(), '/' ) . '[^?#]+\.php',
preg_quote( wp_parse_url( content_url(), PHP_URL_PATH ), '/' ),
'.*\?.+',
'^' . preg_quote( wp_unslash( $_SERVER['REQUEST_URI'] ), '/' ) . '(#.*)?$',
'^#.*',
'^mailto:.*',
'^tel:.*',
),
);
$showPreloaderInstantly = apply_filters('flat_preloader_show_preloader_instantly', $settings['show_preloader_instantly'] === '1' && $display === 'all' ? true : false);
wp_enqueue_style( 'flat-preloader', untrailingslashit( FLAT_PRELOADER_PLUGIN_URL ) . '/assets/css/flat-preloader-public.css', array(), FLAT_PRELOADER_VERSION, 'all' );
wp_enqueue_script( 'flat-preloader-js', untrailingslashit( FLAT_PRELOADER_PLUGIN_URL ) . '/assets/js/flat-preloader.js', array( 'jquery' ), FLAT_PRELOADER_VERSION, true );
wp_localize_script(
'flat-preloader-js',
'flatPreloader',
array(
'delayTime' => $settings['delay_time'] ? $settings['delay_time'] : 1000,
'showPreloaderInstantly' => $showPreloaderInstantly,
'host' => $_SERVER['HTTP_HOST'],
'ignores' => $ignoreLinks,
'display' => $display,
)
);
}
add_action( 'wp_enqueue_scripts', 'flat_preloader_add_public_scripts' );
/**
* Preloader output
*/
function flat_preloader_output() {
global $post;
$style = get_option( 'preloader-style' );
if ( ! $style ) {
$style = 'flat/flat_8.gif';
}
$display = get_option( 'preloader-display' );
$settings = flat_preloader_get_settings();
$custom_image_url = $settings['custom_image_url'];
$has_custom_image_url = ! empty( $custom_image_url );
$image_url = $has_custom_image_url ? $custom_image_url : untrailingslashit( FLAT_PRELOADER_PLUGIN_URL ) . '/assets/img/' . $style;
$text = $settings['text_under_icon'];
$alt = esc_attr( $settings['alt'] );
$post_id = esc_attr( $settings['post_id'] );
$overlay_class = $has_custom_image_url ? 'fpo-custom' : 'fpo-default';
ob_start();
?>
<div id="flat-preloader-overlay" class="<?php echo $overlay_class; ?>">
<?php do_action( 'flat_preloader_output_before_overlay', $settings ); ?>
<img src="<?php echo $image_url; ?>" alt="<?php echo $alt; ?>">
<small><?php echo $text; ?></small>
<?php do_action( 'flat_preloader_output_after_overlay', $settings ); ?>
</div>
<?php
$content = ob_get_clean();
$ids = explode( ',', $post_id );
$ids = array_map(
function ( $id) {
return (int) trim( $id );
},
$ids
);
if ( $display === 'home' && ( is_home() || is_front_page() ) ) {
echo $content;
} elseif ( $display === 'all' ) {
echo $content;
} elseif ( $display === 'custom' && in_array( $post->ID, $ids, true ) && is_singular() ) {
echo $content;
}
}
add_action( 'wp_head', 'flat_preloader_output', 1000 );
/**
* Add custom classes to body
*
* @since 1.1.2
*
* @param array $classes The body classes.
*
* @return array $classes The body classes
*/
function flat_preloader_body_classes( $classes ) {
$display = get_option( 'preloader-display' );
if ( $display === 'home' && ( is_home() || is_front_page() ) ) {
$classes[] = 'flat-preloader-active';
} elseif ( $display === 'all' ) {
$classes[] = 'flat-preloader-active';
}
return $classes;
}
add_filter( 'body_class', 'flat_preloader_body_classes' );
/**
* Add plugin action links
*
* @param array $links The plugin action links.
*
* @return array $links
*/
function flat_preloader_plugin_action_links( $links ) {
$links[] = '<a href="' . esc_url( get_admin_url( null, 'options-general.php?page=flat-preloader' ) ) . '">' . esc_html__( 'Settings', 'flat-preloader' ) . '</a>';
return $links;
}
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'flat_preloader_plugin_action_links', 100 );