-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathepico-custom-js-code.php
276 lines (243 loc) · 9.96 KB
/
epico-custom-js-code.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
<?php
/**
* Plugin Name: Épico Custom Javascript Code
* Plugin URI: https://epico.studio
* Description: Adds custom Javascript code to the site front-end.
* Version: 1.0.1
* Text Domain: epico-custom-js-code
* Author: Márcio Duarte
* Author URI: https://epico.studio
* License: GPL2
*/
/*
Copyright (C) 2023 Márcio Duarte [email protected]
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
namespace Epico;
// Exit if accessed directly.
if ( !defined('ABSPATH') ) {
exit;
}
/**
* CustomJSCode class for managing custom JavaScript code in WordPress.
*/
class CustomJSCode {
/**
* Constructor for the CustomJSCode class.
* Initializes hooks and actions.
*/
public function __construct() {
add_action('admin_menu', [$this, 'add_submenu_page']);
add_action('admin_post_epico_save_code', [$this, 'saveCode']);
add_action('wp_head', [$this, 'includeHeaderCode'], -999); // Add it right after the start of the <head> tag.
add_action('wp_body_open', [$this, 'includeBodyCode'], -999);
add_action('wp_footer', [$this, 'includeFooterCode'], 999); // Add it right before the closing of the </body> tag.
register_uninstall_hook(__FILE__, 'clearOptionsOnUninstallation');
add_action('admin_enqueue_scripts', [$this, 'enqueueCodeMirror']);
add_filter('safe_style_css', [$this, 'keepStyleProperties']);
}
/**
* Adds a submenu page to the WordPress admin menu.
*/
public function add_submenu_page() {
add_submenu_page(
'options-general.php',
__( 'Épico Custom Javascript Code', 'epico-custom-javascript-code' ),
__( 'Épico Custom Javascript Code', 'epico-custom-javascript-code' ),
'manage_options',
'epico-custom-js-code',
[$this, 'render_submenu_page']
);
}
/**
* Renders the content of the subpage for managing custom JavaScript code.
*/
public function render_submenu_page() {
?>
<div class="wrap">
<h1><?php _e( 'Épico Custom Javascript Code', 'epico-custom-javascript-code' ); ?></h1>
<form method="post" action="<?php echo esc_url(admin_url('admin-post.php')); ?>">
<?php wp_nonce_field('epico_save_code', 'epico_nonce'); ?>
<div>
<p>
<label for="epico_header_code"><?php _e( 'Insert Javascript code in the “head” tag of the website:', 'epico-custom-javascript-code' ); ?></label>
</p>
<p>
<textarea name="epico_header_code" id="epico_header_code" rows="15" cols="50"><?php echo esc_textarea(get_option('epico_head_code')); ?></textarea><br>
</p>
</div>
<div>
<p>
<label for="epico_body_code"><?php _e( 'Insert Javascript code right after the opening of the “body” tag:', 'epico-custom-javascript-code' ); ?></label>
</p>
<p>
<textarea name="epico_body_code" id="epico_body_code" rows="15" cols="50"><?php echo esc_textarea(get_option('epico_body_code')); ?></textarea><br>
</p>
</div>
<div>
<p>
<label for="epico_footer_code"><?php _e( 'Insert Javascript code right before the closing “body” tag:', 'epico-custom-javascript-code' ); ?></label>
</p>
<p>
<textarea name="epico_footer_code" id="epico_footer_code" rows="15" cols="50"><?php echo esc_textarea(get_option('epico_footer_code')); ?></textarea><br>
</p>
</div>
<div>
<p>
<label for="epico_clean_on_uninstall">
<input type="checkbox" id="epico_clean_on_uninstall" name="epico_clean_on_uninstall" value="1" <?php checked( get_option('epico_clean_on_uninstall'), 1, false ) ?>>
<?php _e('Remove the code from the database on plugin deactivation','epico-custom-javascript-code');?>
</label>
</p>
</div>
<input type="hidden" name="action" value="epico_save_code">
<input type="submit" value="Save" class="button-primary">
</form>
</div>
<?php
}
/**
* Saves the JavaScript code provided by the user.
*/
public function saveCode() {
if (current_user_can('manage_options') && isset($_POST['epico_nonce']) && wp_verify_nonce($_POST['epico_nonce'], 'epico_save_code')) {
if (isset($_POST['epico_header_code'])) {
update_option('epico_head_code', wp_kses(stripslashes($_POST['epico_header_code']), $this->allowedHTML(), $this->allowedProtocols()));
} else {
delete_option('epico_head_code');
}
if (isset($_POST['epico_body_code'])) {
update_option('epico_body_code', wp_kses(stripslashes($_POST['epico_body_code']), $this->allowedHTML(), $this->allowedProtocols()));
} else {
delete_option('epico_body_code');
}
if (isset($_POST['epico_footer_code'])) {
update_option('epico_footer_code', wp_kses(stripslashes($_POST['epico_footer_code']), $this->allowedHTML(), $this->allowedProtocols()));
} else {
delete_option('epico_footer_code');
}
if (isset($_POST['epico_clean_on_uninstall'])) {
update_option('epico_clean_on_uninstall', isset($_POST['epico_clean_on_uninstall']) ? 1 : 0);
} else {
delete_option('epico_clean_on_uninstall');
}
}
wp_safe_redirect(esc_url(admin_url('admin.php?page=epico-custom-js-code')));
exit();
}
/**
* Includes JavaScript code in the head section of the site.
*/
public function includeHeaderCode() {
$header_code = get_option('epico_head_code');
if ($header_code) {
echo wp_kses($header_code, $this->allowedHTML(), $this->allowedProtocols());
}
}
/**
* Includes JavaScript code right after the opening of the body tag.
*/
public function includeBodyCode() {
$body_code = get_option('epico_body_code');
if ($body_code) {
echo wp_kses($body_code, $this->allowedHTML(), $this->allowedProtocols());
}
}
/**
* Includes JavaScript code right before the closing body tag.
*/
public function includeFooterCode() {
$footer_code = get_option('epico_footer_code');
if ($footer_code) {
echo wp_kses($footer_code, $this->allowedHTML(), $this->allowedProtocols());
}
}
/**
* Clears options from the database on plugin uninstallation.
*/
public static function clearOptionsOnUninstallation() {
delete_option('epico_head_code');
delete_option('epico_footer_code');
delete_option('epico_clean_on_uninstall');
}
/**
* Enqueues the CodeMirror script for code editing.
*/
public function enqueueCodeMirror($hook) {
if ($hook != "settings_page_epico-custom-js-code") {
return;
}
// Work with Javascript only.
$settings = wp_enqueue_code_editor(['type' => 'application/javascript']);
// Accept <script> tags.
$settings['codemirror']['mode'] = 'htmlmixed';
if (false === $settings) {
wp_add_inline_script(
'wp-codemirror',
'jQuery(function($) { $("#epico_header_code, #epico_body_code, #epico_footer_code").prop("disabled", false); })'
);
return;
}
wp_add_inline_script(
'wp-codemirror',
sprintf(
'jQuery(function($) { var settings = %s; $("#epico_header_code, #epico_body_code, #epico_footer_code").each(function(index, element) { wp.codeEditor.initialize(element, settings); }); })',
wp_json_encode($settings)
)
);
wp_enqueue_script('wp-theme-plugin-editor');
wp_enqueue_style('wp-codemirror');
}
/**
* Adds style properties to the list of safe styles.
*/
public function keepStyleProperties($styles) {
$styles[] = 'display';
$styles[] = 'visibility';
$styles[] = 'width';
$styles[] = 'height';
return $styles;
}
/**
* Returns an array of allowed HTML elements and attributes.
*/
private function allowedHTML() {
return [
'script' => [
'async' => [],
'defer' => [],
'src' => [],
'type' => [],
'id' => [],
],
'iframe' => [
'src' => [],
'style' => [
'visibility' => [],
'display' => [],
],
'width' => [],
'height' => [],
'id' => [],
],
'noscript' => [],
];
}
/**
* Returns an array of allowed protocols.
*/
private function allowedProtocols() {
return ['https'];
}
}
// Create an instance of the CustomJSCode class to initiate the plugin.
new CustomJSCode();