Skip to content

Commit 42b7718

Browse files
committed
Update version to 1.1.0 and improve capability handling in custom JavaScript settings
- Updated plugin version from 1.0.1 to 1.1.0 in additional-javascript.php. - Added a new comment for the version definition to clarify its purpose for cache-busting assets. - Changed capability from 'edit_css' to 'unfiltered_html' in class-custom-javascript-control.php to allow broader editing permissions. - Enhanced constructor documentation to specify exceptions and parameter types more clearly. - Improved code formatting for better readability and consistency.
1 parent 886969c commit 42b7718

File tree

2 files changed

+37
-27
lines changed

2 files changed

+37
-27
lines changed

additional-javascript.php

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* Plugin URI: https://github.com/soderlind/additional-javascript
1313
* GitHub Plugin URI: https://github.com/soderlind/additional-javascript
1414
* Description: Add additional JavaScript using the WordPress Customizer.
15-
* Version: 1.0.1
15+
* Version: 1.1.0
1616
* Author: Per Soderlind
1717
* Author URI: https://soderlind.no
1818
* Text Domain: additional-javascript
@@ -31,20 +31,24 @@
3131
add_action( 'customize_preview_init', __NAMESPACE__ . '\customize_preview_additional_javascript' );
3232
add_action( 'customize_controls_enqueue_scripts', __NAMESPACE__ . '\on_customize_controls_enqueue_scripts' );
3333

34+
/**
35+
* Plugin version - used for cache-busting assets
36+
*/
37+
define( 'ADDITIONAL_JAVASCRIPT_VERSION', '1.1.0' );
38+
3439
/**
3540
* Add a default JavaScript code.
3641
*
3742
* @return string
3843
*/
3944
function default_js_template(): string {
4045
return <<<EOTEMPLATE
41-
(function( $ ) {
42-
43-
"use strict";
46+
// Run code when the DOM is ready
47+
document.addEventListener('DOMContentLoaded', function () {
4448
45-
// JavaScript code here.
49+
// JavaScript code here.
4650
47-
})(jQuery);
51+
});
4852
EOTEMPLATE;
4953
}
5054

@@ -306,7 +310,7 @@ function soderlind_update_custom_javascript_post( $javascript, $args = [] ) {
306310
$post = soderlind_get_custom_javascript_post( $args[ 'stylesheet' ] );
307311
if ( $post ) {
308312
$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 );
310314
} else {
311315
$r = wp_insert_post( wp_slash( $post_data ), true );
312316

@@ -334,21 +338,27 @@ function soderlind_update_custom_javascript_post( $javascript, $args = [] ) {
334338
* @return void
335339
*/
336340
function customize_preview_additional_javascript() {
337-
$handle = 'customize-preview-additional-javascript';
341+
$handle = 'additional-javascript-preview';
338342
$src = plugins_url( '/js/additional-javascript-preview.js', __FILE__ );
339343
$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+
}
341348
}
342349

343350
/**
344-
* Load script for customizer control.
351+
* Load styles for customizer control.
345352
*
346353
* @return void
347354
*/
348355
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;
351358
$src = plugins_url( "/css/customize-controls-custom-javascript${suffix}.css", __FILE__ );
352359
$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+
}
354364
}

class-custom-javascript-control.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ final class Soderlind_Customize_Custom_JavaScript_Setting extends \WP_Customize_
3535
* @since 4.7.0
3636
* @var string
3737
*/
38-
public $capability = 'edit_css';
38+
public $capability = 'unfiltered_html';
3939

4040
/**
4141
* Stylesheet
@@ -46,26 +46,26 @@ final class Soderlind_Customize_Custom_JavaScript_Setting extends \WP_Customize_
4646
public $stylesheet = '';
4747

4848
/**
49-
* WP_Customize_Custom_JavaScript_Setting constructor.
49+
* Soderlind_Customize_Custom_JavaScript_Setting constructor.
5050
*
5151
* @since 4.7.0
5252
*
53-
* @throws Exception If the setting ID does not match the pattern `custom_javascript[$stylesheet]`.
53+
* @throws \Exception If the setting ID does not match the pattern `custom_javascript[$stylesheet]`.
5454
*
55-
* @param WP_Customize_Manager $manager The Customize Manager class.
56-
* @param string $id An specific ID of the setting. Can be a
57-
* theme mod or option name.
58-
* @param array $args Setting arguments.
55+
* @param \WP_Customize_Manager $manager The Customize Manager class.
56+
* @param string $id An specific ID of the setting. Can be a
57+
* theme mod or option name.
58+
* @param array $args Setting arguments.
5959
*/
6060
public function __construct( $manager, $id, $args = [] ) {
6161
parent::__construct( $manager, $id, $args );
62-
if ( 'custom_javascript' !== $this->id_data['base'] ) {
63-
throw new Exception( 'Expected custom_javascript id_base.' );
62+
if ( 'custom_javascript' !== $this->id_data[ 'base' ] ) {
63+
throw new \Exception( 'Setting ID must have custom_javascript as its base.' );
6464
}
65-
if ( 1 !== count( $this->id_data['keys'] ) || empty( $this->id_data['keys'][0] ) ) {
66-
throw new Exception( 'Expected single stylesheet key.' );
65+
if ( 1 !== count( $this->id_data[ 'keys' ] ) || empty( $this->id_data[ 'keys' ][ 0 ] ) ) {
66+
throw new \Exception( 'Setting ID must contain a single stylesheet key.' );
6767
}
68-
$this->stylesheet = $this->id_data['keys'][0];
68+
$this->stylesheet = $this->id_data[ 'keys' ][ 0 ];
6969
}
7070

7171
/**
@@ -121,7 +121,7 @@ public function value() {
121121
return $post_value;
122122
}
123123
}
124-
$id_base = $this->id_data['base'];
124+
$id_base = $this->id_data[ 'base' ];
125125
$value = '';
126126
$post = soderlind_get_custom_javascript_post( $this->stylesheet );
127127
if ( $post ) {
@@ -178,7 +178,7 @@ public function update( $javascript ) {
178178

179179
$r = soderlind_update_custom_javascript_post(
180180
$javascript,
181-
[
181+
[
182182
'stylesheet' => $this->stylesheet,
183183
]
184184
);

0 commit comments

Comments
 (0)