12
12
* Plugin URI: https://github.com/soderlind/additional-javascript
13
13
* GitHub Plugin URI: https://github.com/soderlind/additional-javascript
14
14
* Description: Add additional JavaScript using the WordPress Customizer.
15
- * Version: 1.0.1
15
+ * Version: 1.1.0
16
16
* Author: Per Soderlind
17
17
* Author URI: https://soderlind.no
18
18
* Text Domain: additional-javascript
31
31
add_action ( 'customize_preview_init ' , __NAMESPACE__ . '\customize_preview_additional_javascript ' );
32
32
add_action ( 'customize_controls_enqueue_scripts ' , __NAMESPACE__ . '\on_customize_controls_enqueue_scripts ' );
33
33
34
+ /**
35
+ * Plugin version - used for cache-busting assets
36
+ */
37
+ define ( 'ADDITIONAL_JAVASCRIPT_VERSION ' , '1.1.0 ' );
38
+
34
39
/**
35
40
* Add a default JavaScript code.
36
41
*
37
42
* @return string
38
43
*/
39
- function default_js_template () : string {
44
+ function default_js_template (): string {
40
45
return <<<EOTEMPLATE
41
- (function( $ ) {
42
-
43
- "use strict";
46
+ // Run code when the DOM is ready
47
+ document.addEventListener('DOMContentLoaded', function () {
44
48
45
- // JavaScript code here.
49
+ // JavaScript code here.
46
50
47
- })(jQuery) ;
51
+ });
48
52
EOTEMPLATE ;
49
53
}
50
54
@@ -57,8 +61,8 @@ function register_post_type_javascript() {
57
61
58
62
register_post_type (
59
63
'custom_javascript ' ,
60
- [
61
- 'labels ' => [
64
+ [
65
+ 'labels ' => [
62
66
'name ' => __ ( 'Custom JavaScript ' ),
63
67
'singular_name ' => __ ( 'Custom JavaScript ' ),
64
68
],
@@ -70,7 +74,7 @@ function register_post_type_javascript() {
70
74
'can_export ' => true ,
71
75
// '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
72
76
'supports ' => [ 'title ' , 'revisions ' ],
73
- 'capabilities ' => [
77
+ 'capabilities ' => [
74
78
'delete_posts ' => 'edit_theme_options ' ,
75
79
'delete_post ' => 'edit_theme_options ' ,
76
80
'delete_published_posts ' => 'edit_theme_options ' ,
@@ -114,7 +118,7 @@ function soderlind_custom_javascript_cb() {
114
118
function register_additional_javascript ( \WP_Customize_Manager $ wp_customize ) {
115
119
$ wp_customize ->add_section (
116
120
'custom_javascript ' ,
117
- [
121
+ [
118
122
'title ' => _x ( 'Additional JavaScript ' , 'customizer menu ' , 'dss-wp ' ),
119
123
'priority ' => 999 ,
120
124
]
@@ -124,7 +128,7 @@ function register_additional_javascript( \WP_Customize_Manager $wp_customize ) {
124
128
$ custom_javascript_setting = new Soderlind_Customize_Custom_JavaScript_Setting (
125
129
$ wp_customize ,
126
130
sprintf ( 'custom_javascript[%s] ' , get_stylesheet () ),
127
- [
131
+ [
128
132
'capability ' => 'unfiltered_html ' ,
129
133
'default ' => default_js_template (),
130
134
]
@@ -134,7 +138,7 @@ function register_additional_javascript( \WP_Customize_Manager $wp_customize ) {
134
138
$ control = new \WP_Customize_Code_Editor_Control (
135
139
$ wp_customize ,
136
140
'custom_javascript ' ,
137
- [
141
+ [
138
142
'label ' => 'Custom Javascript ' ,
139
143
'code_type ' => 'application/javascript ' ,
140
144
'settings ' => [ 'default ' => $ custom_javascript_setting ->id ],
@@ -157,7 +161,7 @@ function soderlind_get_custom_javascript_post( string $stylesheet = '' ) {
157
161
$ stylesheet = get_stylesheet ();
158
162
}
159
163
160
- $ custom_javascript_query_vars = [
164
+ $ custom_javascript_query_vars = [
161
165
'post_type ' => 'custom_javascript ' ,
162
166
'post_status ' => get_post_stati (),
163
167
'name ' => sanitize_title ( $ stylesheet ),
@@ -182,9 +186,9 @@ function soderlind_get_custom_javascript_post( string $stylesheet = '' ) {
182
186
$ query = new \WP_Query ( $ custom_javascript_query_vars );
183
187
$ post = $ query ->post ;
184
188
/*
185
- * Cache the lookup. See soderlind_update_custom_javascript_post().
186
- * @todo This should get cleared if a custom_javascript post is added/removed.
187
- */
189
+ * Cache the lookup. See soderlind_update_custom_javascript_post().
190
+ * @todo This should get cleared if a custom_javascript post is added/removed.
191
+ */
188
192
set_theme_mod ( 'custom_javascript_post_id ' , $ post ? $ post ->ID : -1 );
189
193
}
190
194
} else {
@@ -247,71 +251,71 @@ function soderlind_get_custom_javascript( $stylesheet = '' ) {
247
251
function soderlind_update_custom_javascript_post ( $ javascript , $ args = [] ) {
248
252
$ args = wp_parse_args (
249
253
$ args ,
250
- [
254
+ [
251
255
'preprocessed ' => '' ,
252
256
'stylesheet ' => get_stylesheet (),
253
257
]
254
258
);
255
259
256
- $ data = [
260
+ $ data = [
257
261
'javascript ' => $ javascript ,
258
- 'preprocessed ' => $ args ['preprocessed ' ],
262
+ 'preprocessed ' => $ args [ 'preprocessed ' ],
259
263
];
260
264
261
265
/**
262
- * Filters the `javascript` (`post_content`) and `preprocessed` (`post_content_filtered`) args for a `custom_javascript` post being updated.
263
- *
264
- * This filter can be used by plugin that offer JavaScript pre-processors, to store the original
265
- * pre-processed JavaScript in `post_content_filtered` and then store processed JavaScript in `post_content`.
266
- * When used in this way, the `post_content_filtered` should be supplied as the setting value
267
- * instead of `post_content` via a the `customize_value_custom_javascript` filter, for example:
268
- *
269
- * <code>
270
- * add_filter( 'customize_value_custom_javascript', function( $value, $setting ) {
271
- * $post = soderlind_get_custom_javascript_post( $setting->stylesheet );
272
- * if ( $post && ! empty( $post->post_content_filtered ) ) {
273
- * $javascript = $post->post_content_filtered;
274
- * }
275
- * return $javascript;
276
- * }, 10, 2 );
277
- * </code>
278
- *
279
- * @since 4.7.0
280
- * @param array $data {
281
- * Custom JavaScript data.
282
- *
283
- * @type string $javascript JavaScript stored in `post_content`.
284
- * @type string $preprocessed Pre-processed JavaScript stored in `post_content_filtered`. Normally empty string.
285
- * }
286
- * @param array $args {
287
- * The args passed into `wp_update_custom_javascript_post()` merged with defaults.
288
- *
289
- * @type string $javascript The original JavaScript passed in to be updated.
290
- * @type string $preprocessed The original preprocessed JavaScript passed in to be updated.
291
- * @type string $stylesheet The stylesheet (theme) being updated.
292
- * }
293
- */
266
+ * Filters the `javascript` (`post_content`) and `preprocessed` (`post_content_filtered`) args for a `custom_javascript` post being updated.
267
+ *
268
+ * This filter can be used by plugin that offer JavaScript pre-processors, to store the original
269
+ * pre-processed JavaScript in `post_content_filtered` and then store processed JavaScript in `post_content`.
270
+ * When used in this way, the `post_content_filtered` should be supplied as the setting value
271
+ * instead of `post_content` via a the `customize_value_custom_javascript` filter, for example:
272
+ *
273
+ * <code>
274
+ * add_filter( 'customize_value_custom_javascript', function( $value, $setting ) {
275
+ * $post = soderlind_get_custom_javascript_post( $setting->stylesheet );
276
+ * if ( $post && ! empty( $post->post_content_filtered ) ) {
277
+ * $javascript = $post->post_content_filtered;
278
+ * }
279
+ * return $javascript;
280
+ * }, 10, 2 );
281
+ * </code>
282
+ *
283
+ * @since 4.7.0
284
+ * @param array $data {
285
+ * Custom JavaScript data.
286
+ *
287
+ * @type string $javascript JavaScript stored in `post_content`.
288
+ * @type string $preprocessed Pre-processed JavaScript stored in `post_content_filtered`. Normally empty string.
289
+ * }
290
+ * @param array $args {
291
+ * The args passed into `wp_update_custom_javascript_post()` merged with defaults.
292
+ *
293
+ * @type string $javascript The original JavaScript passed in to be updated.
294
+ * @type string $preprocessed The original preprocessed JavaScript passed in to be updated.
295
+ * @type string $stylesheet The stylesheet (theme) being updated.
296
+ * }
297
+ */
294
298
$ data = apply_filters ( 'soderlind_update_custom_javascript_data ' , $ data , array_merge ( $ args , compact ( 'javascript ' ) ) );
295
299
296
- $ post_data = [
297
- 'post_title ' => $ args ['stylesheet ' ],
298
- 'post_name ' => sanitize_title ( $ args ['stylesheet ' ] ),
300
+ $ post_data = [
301
+ 'post_title ' => $ args [ 'stylesheet ' ],
302
+ 'post_name ' => sanitize_title ( $ args [ 'stylesheet ' ] ),
299
303
'post_type ' => 'custom_javascript ' ,
300
304
'post_status ' => 'publish ' ,
301
- 'post_content ' => $ data ['javascript ' ],
302
- 'post_content_filtered ' => $ data ['preprocessed ' ],
305
+ 'post_content ' => $ data [ 'javascript ' ],
306
+ 'post_content_filtered ' => $ data [ 'preprocessed ' ],
303
307
];
304
308
305
309
// Update post if it already exists, otherwise create a new one.
306
- $ post = soderlind_get_custom_javascript_post ( $ args ['stylesheet ' ] );
310
+ $ post = soderlind_get_custom_javascript_post ( $ args [ 'stylesheet ' ] );
307
311
if ( $ post ) {
308
- $ post_data ['ID ' ] = $ post ->ID ;
309
- $ r = wp_update_post ( wp_slash ( $ post_data ), true );
312
+ $ post_data [ 'ID ' ] = $ post ->ID ;
313
+ $ r = wp_update_post ( wp_slash ( $ post_data ), true );
310
314
} else {
311
315
$ r = wp_insert_post ( wp_slash ( $ post_data ), true );
312
316
313
317
if ( ! is_wp_error ( $ r ) ) {
314
- if ( get_stylesheet () === $ args ['stylesheet ' ] ) {
318
+ if ( get_stylesheet () === $ args [ 'stylesheet ' ] ) {
315
319
set_theme_mod ( 'custom_javascript_post_id ' , $ r );
316
320
}
317
321
@@ -334,21 +338,27 @@ function soderlind_update_custom_javascript_post( $javascript, $args = [] ) {
334
338
* @return void
335
339
*/
336
340
function customize_preview_additional_javascript () {
337
- $ handle = 'customize-preview- additional-javascript ' ;
341
+ $ handle = 'additional-javascript-preview ' ;
338
342
$ src = plugins_url ( '/js/additional-javascript-preview.js ' , __FILE__ );
339
- $ deps = [ 'customize-preview ' , 'jquery ' ];
340
- wp_enqueue_script ( $ handle , $ src , $ deps , rand (), true );
343
+ $ deps = [ 'customize-preview ' ];
344
+
345
+ if ( file_exists ( plugin_dir_path ( __FILE__ ) . 'js/additional-javascript-preview.js ' ) ) {
346
+ wp_enqueue_script ( $ handle , $ src , $ deps , ADDITIONAL_JAVASCRIPT_VERSION , true );
347
+ }
341
348
}
342
349
343
350
/**
344
- * Load script for customizer control.
351
+ * Load styles for customizer control.
345
352
*
346
353
* @return void
347
354
*/
348
355
function on_customize_controls_enqueue_scripts () {
349
- $ suffix = function_exists ( ' is_rtl ' ) && is_rtl () ? '-rtl ' : '' ;
350
- $ handle = " custom -javascript$ { suffix}" ;
356
+ $ suffix = is_rtl () ? '-rtl ' : '' ;
357
+ $ handle = ' additional -javascript-controls ' . $ suffix ;
351
358
$ src = plugins_url ( "/css/customize-controls-custom-javascript $ {suffix}.css " , __FILE__ );
352
359
$ deps = [ 'customize-controls ' ];
353
- wp_enqueue_style ( $ handle , $ src , $ deps );
360
+
361
+ if ( file_exists ( plugin_dir_path ( __FILE__ ) . "css/customize-controls-custom-javascript $ {suffix}.css " ) ) {
362
+ wp_enqueue_style ( $ handle , $ src , $ deps , ADDITIONAL_JAVASCRIPT_VERSION );
363
+ }
354
364
}
0 commit comments