|
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 | 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 |
|
@@ -306,7 +310,7 @@ function soderlind_update_custom_javascript_post( $javascript, $args = [] ) {
|
306 | 310 | $post = soderlind_get_custom_javascript_post( $args[ 'stylesheet' ] );
|
307 | 311 | if ( $post ) {
|
308 | 312 | $post_data[ 'ID' ] = $post->ID;
|
309 |
| - $r = wp_update_post( wp_slash( $post_data ), true ); |
| 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 |
|
@@ -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 | 343 | $deps = [ 'customize-preview' ];
|
340 |
| - wp_enqueue_script( $handle, $src, $deps, rand(), true ); |
| 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