Skip to content

Commit 886969c

Browse files
committed
Refactor JavaScript and PHP code for consistency and clarity
- Updated function signatures and array formatting in `additional-javascript.php` for improved readability. - Changed `default_js_template` function declaration style for consistency. - Modified array syntax to use consistent spacing in `register_post_type_javascript` and `register_additional_javascript` functions. - Refactored jQuery usage to vanilla JavaScript in `additional-javascript-preview.js` for better performance and compatibility. - Enhanced comments to accurately describe functionality, particularly in the `custom_javascript` preview handler.
1 parent 20e7323 commit 886969c

File tree

2 files changed

+62
-62
lines changed

2 files changed

+62
-62
lines changed

additional-javascript.php

Lines changed: 55 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
*
3737
* @return string
3838
*/
39-
function default_js_template() : string {
39+
function default_js_template(): string {
4040
return <<<EOTEMPLATE
4141
(function( $ ) {
4242
@@ -57,8 +57,8 @@ function register_post_type_javascript() {
5757

5858
register_post_type(
5959
'custom_javascript',
60-
[
61-
'labels' => [
60+
[
61+
'labels' => [
6262
'name' => __( 'Custom JavaScript' ),
6363
'singular_name' => __( 'Custom JavaScript' ),
6464
],
@@ -70,7 +70,7 @@ function register_post_type_javascript() {
7070
'can_export' => true,
7171
// '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
7272
'supports' => [ 'title', 'revisions' ],
73-
'capabilities' => [
73+
'capabilities' => [
7474
'delete_posts' => 'edit_theme_options',
7575
'delete_post' => 'edit_theme_options',
7676
'delete_published_posts' => 'edit_theme_options',
@@ -114,7 +114,7 @@ function soderlind_custom_javascript_cb() {
114114
function register_additional_javascript( \WP_Customize_Manager $wp_customize ) {
115115
$wp_customize->add_section(
116116
'custom_javascript',
117-
[
117+
[
118118
'title' => _x( 'Additional JavaScript', 'customizer menu', 'dss-wp' ),
119119
'priority' => 999,
120120
]
@@ -124,7 +124,7 @@ function register_additional_javascript( \WP_Customize_Manager $wp_customize ) {
124124
$custom_javascript_setting = new Soderlind_Customize_Custom_JavaScript_Setting(
125125
$wp_customize,
126126
sprintf( 'custom_javascript[%s]', get_stylesheet() ),
127-
[
127+
[
128128
'capability' => 'unfiltered_html',
129129
'default' => default_js_template(),
130130
]
@@ -134,7 +134,7 @@ function register_additional_javascript( \WP_Customize_Manager $wp_customize ) {
134134
$control = new \WP_Customize_Code_Editor_Control(
135135
$wp_customize,
136136
'custom_javascript',
137-
[
137+
[
138138
'label' => 'Custom Javascript',
139139
'code_type' => 'application/javascript',
140140
'settings' => [ 'default' => $custom_javascript_setting->id ],
@@ -157,7 +157,7 @@ function soderlind_get_custom_javascript_post( string $stylesheet = '' ) {
157157
$stylesheet = get_stylesheet();
158158
}
159159

160-
$custom_javascript_query_vars = [
160+
$custom_javascript_query_vars = [
161161
'post_type' => 'custom_javascript',
162162
'post_status' => get_post_stati(),
163163
'name' => sanitize_title( $stylesheet ),
@@ -182,9 +182,9 @@ function soderlind_get_custom_javascript_post( string $stylesheet = '' ) {
182182
$query = new \WP_Query( $custom_javascript_query_vars );
183183
$post = $query->post;
184184
/*
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-
*/
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+
*/
188188
set_theme_mod( 'custom_javascript_post_id', $post ? $post->ID : -1 );
189189
}
190190
} else {
@@ -247,71 +247,71 @@ function soderlind_get_custom_javascript( $stylesheet = '' ) {
247247
function soderlind_update_custom_javascript_post( $javascript, $args = [] ) {
248248
$args = wp_parse_args(
249249
$args,
250-
[
250+
[
251251
'preprocessed' => '',
252252
'stylesheet' => get_stylesheet(),
253253
]
254254
);
255255

256-
$data = [
256+
$data = [
257257
'javascript' => $javascript,
258-
'preprocessed' => $args['preprocessed'],
258+
'preprocessed' => $args[ 'preprocessed' ],
259259
];
260260

261261
/**
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-
*/
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+
*/
294294
$data = apply_filters( 'soderlind_update_custom_javascript_data', $data, array_merge( $args, compact( 'javascript' ) ) );
295295

296-
$post_data = [
297-
'post_title' => $args['stylesheet'],
298-
'post_name' => sanitize_title( $args['stylesheet'] ),
296+
$post_data = [
297+
'post_title' => $args[ 'stylesheet' ],
298+
'post_name' => sanitize_title( $args[ 'stylesheet' ] ),
299299
'post_type' => 'custom_javascript',
300300
'post_status' => 'publish',
301-
'post_content' => $data['javascript'],
302-
'post_content_filtered' => $data['preprocessed'],
301+
'post_content' => $data[ 'javascript' ],
302+
'post_content_filtered' => $data[ 'preprocessed' ],
303303
];
304304

305305
// Update post if it already exists, otherwise create a new one.
306-
$post = soderlind_get_custom_javascript_post( $args['stylesheet'] );
306+
$post = soderlind_get_custom_javascript_post( $args[ 'stylesheet' ] );
307307
if ( $post ) {
308-
$post_data['ID'] = $post->ID;
308+
$post_data[ 'ID' ] = $post->ID;
309309
$r = wp_update_post( wp_slash( $post_data ), true );
310310
} else {
311311
$r = wp_insert_post( wp_slash( $post_data ), true );
312312

313313
if ( ! is_wp_error( $r ) ) {
314-
if ( get_stylesheet() === $args['stylesheet'] ) {
314+
if ( get_stylesheet() === $args[ 'stylesheet' ] ) {
315315
set_theme_mod( 'custom_javascript_post_id', $r );
316316
}
317317

@@ -336,7 +336,7 @@ function soderlind_update_custom_javascript_post( $javascript, $args = [] ) {
336336
function customize_preview_additional_javascript() {
337337
$handle = 'customize-preview-additional-javascript';
338338
$src = plugins_url( '/js/additional-javascript-preview.js', __FILE__ );
339-
$deps = [ 'customize-preview', 'jquery' ];
339+
$deps = [ 'customize-preview' ];
340340
wp_enqueue_script( $handle, $src, $deps, rand(), true );
341341
}
342342

js/additional-javascript-preview.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
/*
22
* Script run inside a Customizer preview frame.
33
*/
4-
(function(exports, $) {
5-
var api = wp.customize;
4+
(function(exports) {
5+
const api = wp.customize;
66

77
api.settingPreviewHandlers = {
88
/**
9-
* Preview changes to custom css.
9+
* Preview changes to custom javascript.
1010
*
11-
* @param {string} value Custom CSS..
11+
* @param {string} value Custom JavaScript.
1212
* @returns {void}
1313
*/
1414
custom_javascript: function(value) {
15-
$("#soderlind-custom-javascript").text(value);
15+
document.getElementById('soderlind-custom-javascript').textContent = value;
1616
},
1717
};
1818

19-
$(function() {
19+
document.addEventListener('DOMContentLoaded', function() {
2020
api(
2121
`custom_javascript[${api.settings.theme.stylesheet}]`,
2222
function(setting) {
@@ -26,4 +26,4 @@
2626

2727
api.trigger("preview-ready");
2828
});
29-
})(wp, jQuery);
29+
})(wp);

0 commit comments

Comments
 (0)