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.0
15
+ * Version: 1.0.1
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
- function default_js_template () {
34
+ /**
35
+ * Add a default JavaScript code.
36
+ *
37
+ * @return string
38
+ */
39
+ function default_js_template () : string {
35
40
return <<<EOTEMPLATE
36
41
(function( $ ) {
37
42
@@ -43,23 +48,29 @@ function default_js_template() {
43
48
EOTEMPLATE ;
44
49
}
45
50
51
+ /**
52
+ * Register cpt JavaScript.
53
+ *
54
+ * @return void
55
+ */
46
56
function register_post_type_javascript () {
47
57
48
58
register_post_type (
49
- 'custom_javascript ' , array (
50
- 'labels ' => array (
59
+ 'custom_javascript ' ,
60
+ [
61
+ 'labels ' => [
51
62
'name ' => __ ( 'Custom JavaScript ' ),
52
63
'singular_name ' => __ ( 'Custom JavaScript ' ),
53
- ) ,
64
+ ] ,
54
65
'public ' => false ,
55
66
'hierarchical ' => false ,
56
67
'rewrite ' => false ,
57
68
'query_var ' => false ,
58
69
'delete_with_user ' => false ,
59
70
'can_export ' => true ,
60
71
// '_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 ' => [
63
74
'delete_posts ' => 'edit_theme_options ' ,
64
75
'delete_post ' => 'edit_theme_options ' ,
65
76
'delete_published_posts ' => 'edit_theme_options ' ,
@@ -72,8 +83,8 @@ function register_post_type_javascript() {
72
83
'read_post ' => 'read ' ,
73
84
'read_private_posts ' => 'read ' ,
74
85
'publish_posts ' => 'edit_theme_options ' ,
75
- ) ,
76
- )
86
+ ] ,
87
+ ]
77
88
);
78
89
}
79
90
@@ -88,37 +99,47 @@ function soderlind_custom_javascript_cb() {
88
99
if ( $ javascript || is_customize_preview () ) {
89
100
?>
90
101
<script id="soderlind-custom-javascript">
91
- <?php echo $ javascript ; // ?>
102
+ <?php echo $ javascript ; ?>
92
103
</script>
93
104
<?php
94
105
}
95
106
}
96
107
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 ) {
99
115
$ wp_customize ->add_section (
100
- 'custom_javascript ' , array (
116
+ 'custom_javascript ' ,
117
+ [
101
118
'title ' => _x ( 'Additional JavaScript ' , 'customizer menu ' , 'dss-wp ' ),
102
119
'priority ' => 999 ,
103
- )
120
+ ]
104
121
);
105
122
106
123
require_once dirname ( __FILE__ ) . '/class-custom-javascript-control.php ' ;
107
124
$ 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
+ [
109
128
'capability ' => 'unfiltered_html ' ,
110
129
'default ' => default_js_template (),
111
- )
130
+ ]
112
131
);
113
132
114
133
$ wp_customize ->add_setting ( $ custom_javascript_setting );
115
134
$ control = new \WP_Customize_Code_Editor_Control (
116
- $ wp_customize , 'custom_javascript ' , array (
135
+ $ wp_customize ,
136
+ 'custom_javascript ' ,
137
+ [
117
138
'label ' => 'Custom Javascript ' ,
118
139
'code_type ' => 'application/javascript ' ,
119
- 'settings ' => array ( 'default ' => $ custom_javascript_setting ->id ) ,
140
+ 'settings ' => [ 'default ' => $ custom_javascript_setting ->id ] ,
120
141
'section ' => 'custom_javascript ' , // Site Identity section
121
- )
142
+ ]
122
143
);
123
144
$ wp_customize ->add_control ( $ control );
124
145
}
@@ -131,12 +152,12 @@ function register_additional_javascript( $wp_customize ) {
131
152
* @param string $stylesheet Optional. A theme object stylesheet name. Defaults to the current theme.
132
153
* @return WP_Post|null The custom_javascript post or null if none exists.
133
154
*/
134
- function soderlind_get_custom_javascript_post ( $ stylesheet = '' ) {
155
+ function soderlind_get_custom_javascript_post ( string $ stylesheet = '' ) {
135
156
if ( empty ( $ stylesheet ) ) {
136
157
$ stylesheet = get_stylesheet ();
137
158
}
138
159
139
- $ custom_javascript_query_vars = array (
160
+ $ custom_javascript_query_vars = [
140
161
'post_type ' => 'custom_javascript ' ,
141
162
'post_status ' => get_post_stati (),
142
163
'name ' => sanitize_title ( $ stylesheet ),
@@ -146,7 +167,7 @@ function soderlind_get_custom_javascript_post( $stylesheet = '' ) {
146
167
'update_post_meta_cache ' => false ,
147
168
'update_post_term_cache ' => false ,
148
169
'lazy_load_term_meta ' => false ,
149
- ) ;
170
+ ] ;
150
171
151
172
$ post = null ;
152
173
if ( get_stylesheet () === $ stylesheet ) {
@@ -195,13 +216,13 @@ function soderlind_get_custom_javascript( $stylesheet = '' ) {
195
216
}
196
217
197
218
/**
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
+ */
205
226
$ javascript = apply_filters ( 'soderlind_get_custom_javascript ' , $ javascript , $ stylesheet );
206
227
207
228
return $ javascript ;
@@ -223,18 +244,19 @@ function soderlind_get_custom_javascript( $stylesheet = '' ) {
223
244
* }
224
245
* @return WP_Post|WP_Error Post on success, error on failure.
225
246
*/
226
- function soderlind_update_custom_javascript_post ( $ javascript , $ args = array () ) {
247
+ function soderlind_update_custom_javascript_post ( $ javascript , $ args = [] ) {
227
248
$ args = wp_parse_args (
228
- $ args , array (
249
+ $ args ,
250
+ [
229
251
'preprocessed ' => '' ,
230
252
'stylesheet ' => get_stylesheet (),
231
- )
253
+ ]
232
254
);
233
255
234
- $ data = array (
256
+ $ data = [
235
257
'javascript ' => $ javascript ,
236
258
'preprocessed ' => $ args ['preprocessed ' ],
237
- ) ;
259
+ ] ;
238
260
239
261
/**
240
262
* 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() )
271
293
*/
272
294
$ data = apply_filters ( 'soderlind_update_custom_javascript_data ' , $ data , array_merge ( $ args , compact ( 'javascript ' ) ) );
273
295
274
- $ post_data = array (
296
+ $ post_data = [
275
297
'post_title ' => $ args ['stylesheet ' ],
276
298
'post_name ' => sanitize_title ( $ args ['stylesheet ' ] ),
277
299
'post_type ' => 'custom_javascript ' ,
278
300
'post_status ' => 'publish ' ,
279
301
'post_content ' => $ data ['javascript ' ],
280
302
'post_content_filtered ' => $ data ['preprocessed ' ],
281
- ) ;
303
+ ] ;
282
304
283
305
// Update post if it already exists, otherwise create a new one.
284
306
$ post = soderlind_get_custom_javascript_post ( $ args ['stylesheet ' ] );
@@ -306,13 +328,23 @@ function soderlind_update_custom_javascript_post( $javascript, $args = array() )
306
328
return get_post ( $ r );
307
329
}
308
330
331
+ /**
332
+ * Load script for customizer preview.
333
+ *
334
+ * @return void
335
+ */
309
336
function customize_preview_additional_javascript () {
310
337
$ handle = 'customize-preview-additional-javascript ' ;
311
338
$ src = plugins_url ( '/js/additional-javascript-preview.js ' , __FILE__ );
312
339
$ deps = [ 'customize-preview ' , 'jquery ' ];
313
340
wp_enqueue_script ( $ handle , $ src , $ deps , rand (), true );
314
341
}
315
342
343
+ /**
344
+ * Load script for customizer control.
345
+ *
346
+ * @return void
347
+ */
316
348
function on_customize_controls_enqueue_scripts () {
317
349
$ suffix = function_exists ( 'is_rtl ' ) && is_rtl () ? '-rtl ' : '' ;
318
350
$ handle = "custom-javascript $ {suffix}" ;
0 commit comments