Skip to content

Commit 20e7323

Browse files
committed
Lint code
1 parent 849ee7d commit 20e7323

4 files changed

+91
-52
lines changed

additional-javascript.php

+68-36
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.0
15+
* Version: 1.0.1
1616
* Author: Per Soderlind
1717
* Author URI: https://soderlind.no
1818
* Text Domain: additional-javascript
@@ -31,7 +31,12 @@
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-
function default_js_template() {
34+
/**
35+
* Add a default JavaScript code.
36+
*
37+
* @return string
38+
*/
39+
function default_js_template() : string {
3540
return <<<EOTEMPLATE
3641
(function( $ ) {
3742
@@ -43,23 +48,29 @@ function default_js_template() {
4348
EOTEMPLATE;
4449
}
4550

51+
/**
52+
* Register cpt JavaScript.
53+
*
54+
* @return void
55+
*/
4656
function register_post_type_javascript() {
4757

4858
register_post_type(
49-
'custom_javascript', array(
50-
'labels' => array(
59+
'custom_javascript',
60+
[
61+
'labels' => [
5162
'name' => __( 'Custom JavaScript' ),
5263
'singular_name' => __( 'Custom JavaScript' ),
53-
),
64+
],
5465
'public' => false,
5566
'hierarchical' => false,
5667
'rewrite' => false,
5768
'query_var' => false,
5869
'delete_with_user' => false,
5970
'can_export' => true,
6071
// '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
61-
'supports' => array( 'title', 'revisions' ),
62-
'capabilities' => array(
72+
'supports' => [ 'title', 'revisions' ],
73+
'capabilities' => [
6374
'delete_posts' => 'edit_theme_options',
6475
'delete_post' => 'edit_theme_options',
6576
'delete_published_posts' => 'edit_theme_options',
@@ -72,8 +83,8 @@ function register_post_type_javascript() {
7283
'read_post' => 'read',
7384
'read_private_posts' => 'read',
7485
'publish_posts' => 'edit_theme_options',
75-
),
76-
)
86+
],
87+
]
7788
);
7889
}
7990

@@ -88,37 +99,47 @@ function soderlind_custom_javascript_cb() {
8899
if ( $javascript || is_customize_preview() ) {
89100
?>
90101
<script id="soderlind-custom-javascript">
91-
<?php echo $javascript; // ?>
102+
<?php echo $javascript; ?>
92103
</script>
93104
<?php
94105
}
95106
}
96107

97-
98-
function register_additional_javascript( $wp_customize ) {
108+
/**
109+
* Add section, settings and control.
110+
*
111+
* @param \WP_Customize_Manager $wp_customize
112+
* @return void
113+
*/
114+
function register_additional_javascript( \WP_Customize_Manager $wp_customize ) {
99115
$wp_customize->add_section(
100-
'custom_javascript', array(
116+
'custom_javascript',
117+
[
101118
'title' => _x( 'Additional JavaScript', 'customizer menu', 'dss-wp' ),
102119
'priority' => 999,
103-
)
120+
]
104121
);
105122

106123
require_once dirname( __FILE__ ) . '/class-custom-javascript-control.php';
107124
$custom_javascript_setting = new Soderlind_Customize_Custom_JavaScript_Setting(
108-
$wp_customize, sprintf( 'custom_javascript[%s]', get_stylesheet() ), array(
125+
$wp_customize,
126+
sprintf( 'custom_javascript[%s]', get_stylesheet() ),
127+
[
109128
'capability' => 'unfiltered_html',
110129
'default' => default_js_template(),
111-
)
130+
]
112131
);
113132

114133
$wp_customize->add_setting( $custom_javascript_setting );
115134
$control = new \WP_Customize_Code_Editor_Control(
116-
$wp_customize, 'custom_javascript', array(
135+
$wp_customize,
136+
'custom_javascript',
137+
[
117138
'label' => 'Custom Javascript',
118139
'code_type' => 'application/javascript',
119-
'settings' => array( 'default' => $custom_javascript_setting->id ),
140+
'settings' => [ 'default' => $custom_javascript_setting->id ],
120141
'section' => 'custom_javascript', // Site Identity section
121-
)
142+
]
122143
);
123144
$wp_customize->add_control( $control );
124145
}
@@ -131,12 +152,12 @@ function register_additional_javascript( $wp_customize ) {
131152
* @param string $stylesheet Optional. A theme object stylesheet name. Defaults to the current theme.
132153
* @return WP_Post|null The custom_javascript post or null if none exists.
133154
*/
134-
function soderlind_get_custom_javascript_post( $stylesheet = '' ) {
155+
function soderlind_get_custom_javascript_post( string $stylesheet = '' ) {
135156
if ( empty( $stylesheet ) ) {
136157
$stylesheet = get_stylesheet();
137158
}
138159

139-
$custom_javascript_query_vars = array(
160+
$custom_javascript_query_vars = [
140161
'post_type' => 'custom_javascript',
141162
'post_status' => get_post_stati(),
142163
'name' => sanitize_title( $stylesheet ),
@@ -146,7 +167,7 @@ function soderlind_get_custom_javascript_post( $stylesheet = '' ) {
146167
'update_post_meta_cache' => false,
147168
'update_post_term_cache' => false,
148169
'lazy_load_term_meta' => false,
149-
);
170+
];
150171

151172
$post = null;
152173
if ( get_stylesheet() === $stylesheet ) {
@@ -195,13 +216,13 @@ function soderlind_get_custom_javascript( $stylesheet = '' ) {
195216
}
196217

197218
/**
198-
* Filters the Custom JavaScript Output into the <head>.
199-
*
200-
* @since 4.7.0
201-
*
202-
* @param string $javascript JavaScript pulled in from the Custom JavaScript CPT.
203-
* @param string $stylesheet The theme stylesheet name.
204-
*/
219+
* Filters the Custom JavaScript Output into the <head>.
220+
*
221+
* @since 4.7.0
222+
*
223+
* @param string $javascript JavaScript pulled in from the Custom JavaScript CPT.
224+
* @param string $stylesheet The theme stylesheet name.
225+
*/
205226
$javascript = apply_filters( 'soderlind_get_custom_javascript', $javascript, $stylesheet );
206227

207228
return $javascript;
@@ -223,18 +244,19 @@ function soderlind_get_custom_javascript( $stylesheet = '' ) {
223244
* }
224245
* @return WP_Post|WP_Error Post on success, error on failure.
225246
*/
226-
function soderlind_update_custom_javascript_post( $javascript, $args = array() ) {
247+
function soderlind_update_custom_javascript_post( $javascript, $args = [] ) {
227248
$args = wp_parse_args(
228-
$args, array(
249+
$args,
250+
[
229251
'preprocessed' => '',
230252
'stylesheet' => get_stylesheet(),
231-
)
253+
]
232254
);
233255

234-
$data = array(
256+
$data = [
235257
'javascript' => $javascript,
236258
'preprocessed' => $args['preprocessed'],
237-
);
259+
];
238260

239261
/**
240262
* Filters the `javascript` (`post_content`) and `preprocessed` (`post_content_filtered`) args for a `custom_javascript` post being updated.
@@ -271,14 +293,14 @@ function soderlind_update_custom_javascript_post( $javascript, $args = array() )
271293
*/
272294
$data = apply_filters( 'soderlind_update_custom_javascript_data', $data, array_merge( $args, compact( 'javascript' ) ) );
273295

274-
$post_data = array(
296+
$post_data = [
275297
'post_title' => $args['stylesheet'],
276298
'post_name' => sanitize_title( $args['stylesheet'] ),
277299
'post_type' => 'custom_javascript',
278300
'post_status' => 'publish',
279301
'post_content' => $data['javascript'],
280302
'post_content_filtered' => $data['preprocessed'],
281-
);
303+
];
282304

283305
// Update post if it already exists, otherwise create a new one.
284306
$post = soderlind_get_custom_javascript_post( $args['stylesheet'] );
@@ -306,13 +328,23 @@ function soderlind_update_custom_javascript_post( $javascript, $args = array() )
306328
return get_post( $r );
307329
}
308330

331+
/**
332+
* Load script for customizer preview.
333+
*
334+
* @return void
335+
*/
309336
function customize_preview_additional_javascript() {
310337
$handle = 'customize-preview-additional-javascript';
311338
$src = plugins_url( '/js/additional-javascript-preview.js', __FILE__ );
312339
$deps = [ 'customize-preview', 'jquery' ];
313340
wp_enqueue_script( $handle, $src, $deps, rand(), true );
314341
}
315342

343+
/**
344+
* Load script for customizer control.
345+
*
346+
* @return void
347+
*/
316348
function on_customize_controls_enqueue_scripts() {
317349
$suffix = function_exists( 'is_rtl' ) && is_rtl() ? '-rtl' : '';
318350
$handle = "custom-javascript${suffix}";

class-custom-javascript-control.php

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22
namespace Soderlind\Customizer;
3+
34
/**
45
* From customize/class-wp-customize-custom-css-setting.php, adjusted for javascript
56
*/
@@ -56,7 +57,7 @@ final class Soderlind_Customize_Custom_JavaScript_Setting extends \WP_Customize_
5657
* theme mod or option name.
5758
* @param array $args Setting arguments.
5859
*/
59-
public function __construct( $manager, $id, $args = array() ) {
60+
public function __construct( $manager, $id, $args = [] ) {
6061
parent::__construct( $manager, $id, $args );
6162
if ( 'custom_javascript' !== $this->id_data['base'] ) {
6263
throw new Exception( 'Expected custom_javascript id_base.' );
@@ -79,7 +80,7 @@ public function preview() {
7980
return false;
8081
}
8182
$this->is_previewed = true;
82-
add_filter( 'soderlind_get_custom_javascript', array( $this, 'filter_previewed_wp_get_custom_javascript' ), 9, 2 );
83+
add_filter( 'soderlind_get_custom_javascript', [ $this, 'filter_previewed_wp_get_custom_javascript' ], 9, 2 );
8384
return true;
8485
}
8586

@@ -152,7 +153,7 @@ public function validate( $javascript ) {
152153
$validity = new \WP_Error();
153154

154155
// if ( preg_match( '#</?\w+#', $javascript ) ) {
155-
// $validity->add( 'illegal_markup', __( 'Markup is not allowed in JavaScript.' ) );
156+
// $validity->add( 'illegal_markup', __( 'Markup is not allowed in JavaScript.' ) );
156157
// }
157158

158159
if ( empty( $validity->errors ) ) {
@@ -176,9 +177,10 @@ public function update( $javascript ) {
176177
}
177178

178179
$r = soderlind_update_custom_javascript_post(
179-
$javascript, array(
180+
$javascript,
181+
[
180182
'stylesheet' => $this->stylesheet,
181-
)
183+
]
182184
);
183185

184186
if ( $r instanceof WP_Error ) {

js/.config/rome.rjson

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// For configuration documentation see http://romefrontend.dev/#project-configuration
2+
name: "js"
3+
root: true

js/additional-javascript-preview.js

+13-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* Script run inside a Customizer preview frame.
33
*/
4-
(function( exports, $ ){
4+
(function(exports, $) {
55
var api = wp.customize;
66

77
api.settingPreviewHandlers = {
@@ -11,17 +11,19 @@
1111
* @param {string} value Custom CSS..
1212
* @returns {void}
1313
*/
14-
custom_javascript: function( value ) {
15-
$( '#soderlind-custom-javascript' ).text( value );
16-
}
14+
custom_javascript: function(value) {
15+
$("#soderlind-custom-javascript").text(value);
16+
},
1717
};
1818

19-
$( function() {
20-
api( 'custom_javascript[' + api.settings.theme.stylesheet + ']', function( setting ) {
21-
setting.bind( api.settingPreviewHandlers.custom_javascript );
22-
} );
19+
$(function() {
20+
api(
21+
`custom_javascript[${api.settings.theme.stylesheet}]`,
22+
function(setting) {
23+
setting.bind(api.settingPreviewHandlers.custom_javascript);
24+
},
25+
);
2326

24-
api.trigger( 'preview-ready' );
27+
api.trigger("preview-ready");
2528
});
26-
27-
})( wp, jQuery );
29+
})(wp, jQuery);

0 commit comments

Comments
 (0)