-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrontend-loader.php
137 lines (119 loc) · 4.58 KB
/
frontend-loader.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
<?php
/*
Plugin Name: Frontend Loader
Description: A minimal loading spinner for WordPress that just works from initial click to page load to ajax requests.
Version: 1.3.6
Author: Billy Wilcosky
Author URI: https://wilcosky.com/skywolf
License: GPL v2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Text Domain: frontend-loader
Domain Path: /languages
*/
// If accessed directly, exit
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// Secure plugin by checking for WP environment
if ( ! function_exists( 'add_action' ) ) {
die( 'No script kiddies please!' );
}
// Enqueue scripts and styles
function feload_custom_enqueue_scripts_and_styles() {
wp_enqueue_script( 'load-script', plugin_dir_url( __FILE__ ) . 'js/script.js', array( 'jquery' ), '1.3.6', true );
wp_enqueue_style( 'load-style', plugin_dir_url( __FILE__ ) . 'css/style.css', array(), '1.3.6' );
// Retrieve settings or set default values
$ignore_click_selectors = get_option('feload_ignore_click_selectors', '.cld-like-trigger, .wp_ulike_btn');
$ignore_ajax_selectors = get_option('feload_ignore_ajax_selectors', '#distractionFreeCheckbox, .bod-block-popup-overlay.active, .cld-like-trigger, .wp_ulike_btn');
// Localize the script with your array of settings
wp_localize_script( 'load-script', 'feloadSettings', array(
'ignoreClickSelectors' => $ignore_click_selectors,
'ignoreAjaxSelectors' => $ignore_ajax_selectors,
));
}
add_action( 'wp_enqueue_scripts', 'feload_custom_enqueue_scripts_and_styles' );
// Add custom div for the loader
function feload_add_custom_div() {
echo '<div class="fe-loader-overlay"><div class="fe-loader"></div></div>';
}
add_action( 'wp_footer', 'feload_add_custom_div' );
// Load plugin textdomain for internationalization
function feload_load_textdomain() {
load_plugin_textdomain( 'frontend-loader', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
add_action( 'plugins_loaded', 'feload_load_textdomain' );
// Rest of file is for the Admin Settings page
// Register settings, section, and fields
add_action('admin_init', 'feload_register_settings');
function feload_register_settings() {
register_setting('feload_settings', 'feload_ignore_click_selectors');
register_setting('feload_settings', 'feload_ignore_ajax_selectors');
add_settings_section(
'feload_settings_section',
'Frontend Loader Settings',
'feload_settings_section_cb',
'feload_settings'
);
add_settings_field(
'feload_field_ignore_click_selectors',
'Ignore Click Selectors',
'feload_field_ignore_click_selectors_cb',
'feload_settings',
'feload_settings_section'
);
add_settings_field(
'feload_field_ignore_ajax_selectors',
'Ignore AJAX Selectors',
'feload_field_ignore_ajax_selectors_cb',
'feload_settings',
'feload_settings_section'
);
}
function feload_settings_section_cb() {
echo '<p>Customize the selectors to ignore specific link clicks and AJAX requests. Input multiple selectors comma separated. Example: .one, #two, .three</p>';
}
function feload_field_ignore_click_selectors_cb() {
$setting = get_option('feload_ignore_click_selectors');
echo "<input type='text' name='feload_ignore_click_selectors' value='" . esc_attr($setting) . "' style='width: 100%;'>";
}
function feload_field_ignore_ajax_selectors_cb() {
$setting = get_option('feload_ignore_ajax_selectors');
echo "<input type='text' name='feload_ignore_ajax_selectors' value='" . esc_attr($setting) . "' style='width: 100%;'>";
}
add_action('admin_menu', 'feload_settings_page');
function feload_settings_page() {
add_options_page(
'Frontend Loader Settings',
'Frontend Loader',
'manage_options',
'feload_settings',
'feload_settings_page_html'
);
}
function feload_settings_page_html() {
if (!current_user_can('manage_options')) {
return;
}
if (isset($_GET['settings-updated'])) {
global $wp_settings_errors;
foreach ((array) $wp_settings_errors as $error) {
if ($error['code'] == 'feload_message') {
$already_added = true;
break;
}
}
}
settings_errors('feload_messages');
?>
<div class="wrap">
<h1><?php echo esc_html(get_admin_page_title()); ?></h1>
<form action="options.php" method="post">
<?php
settings_fields('feload_settings');
do_settings_sections('feload_settings');
submit_button('Save Settings');
?>
</form>
</div>
<?php
}