36
36
*
37
37
* @return string
38
38
*/
39
- function default_js_template () : string {
39
+ function default_js_template (): string {
40
40
return <<<EOTEMPLATE
41
41
(function( $ ) {
42
42
@@ -57,8 +57,8 @@ function register_post_type_javascript() {
57
57
58
58
register_post_type (
59
59
'custom_javascript ' ,
60
- [
61
- 'labels ' => [
60
+ [
61
+ 'labels ' => [
62
62
'name ' => __ ( 'Custom JavaScript ' ),
63
63
'singular_name ' => __ ( 'Custom JavaScript ' ),
64
64
],
@@ -70,7 +70,7 @@ function register_post_type_javascript() {
70
70
'can_export ' => true ,
71
71
// '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
72
72
'supports ' => [ 'title ' , 'revisions ' ],
73
- 'capabilities ' => [
73
+ 'capabilities ' => [
74
74
'delete_posts ' => 'edit_theme_options ' ,
75
75
'delete_post ' => 'edit_theme_options ' ,
76
76
'delete_published_posts ' => 'edit_theme_options ' ,
@@ -114,7 +114,7 @@ function soderlind_custom_javascript_cb() {
114
114
function register_additional_javascript ( \WP_Customize_Manager $ wp_customize ) {
115
115
$ wp_customize ->add_section (
116
116
'custom_javascript ' ,
117
- [
117
+ [
118
118
'title ' => _x ( 'Additional JavaScript ' , 'customizer menu ' , 'dss-wp ' ),
119
119
'priority ' => 999 ,
120
120
]
@@ -124,7 +124,7 @@ function register_additional_javascript( \WP_Customize_Manager $wp_customize ) {
124
124
$ custom_javascript_setting = new Soderlind_Customize_Custom_JavaScript_Setting (
125
125
$ wp_customize ,
126
126
sprintf ( 'custom_javascript[%s] ' , get_stylesheet () ),
127
- [
127
+ [
128
128
'capability ' => 'unfiltered_html ' ,
129
129
'default ' => default_js_template (),
130
130
]
@@ -134,7 +134,7 @@ function register_additional_javascript( \WP_Customize_Manager $wp_customize ) {
134
134
$ control = new \WP_Customize_Code_Editor_Control (
135
135
$ wp_customize ,
136
136
'custom_javascript ' ,
137
- [
137
+ [
138
138
'label ' => 'Custom Javascript ' ,
139
139
'code_type ' => 'application/javascript ' ,
140
140
'settings ' => [ 'default ' => $ custom_javascript_setting ->id ],
@@ -157,7 +157,7 @@ function soderlind_get_custom_javascript_post( string $stylesheet = '' ) {
157
157
$ stylesheet = get_stylesheet ();
158
158
}
159
159
160
- $ custom_javascript_query_vars = [
160
+ $ custom_javascript_query_vars = [
161
161
'post_type ' => 'custom_javascript ' ,
162
162
'post_status ' => get_post_stati (),
163
163
'name ' => sanitize_title ( $ stylesheet ),
@@ -182,9 +182,9 @@ function soderlind_get_custom_javascript_post( string $stylesheet = '' ) {
182
182
$ query = new \WP_Query ( $ custom_javascript_query_vars );
183
183
$ post = $ query ->post ;
184
184
/*
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
+ */
188
188
set_theme_mod ( 'custom_javascript_post_id ' , $ post ? $ post ->ID : -1 );
189
189
}
190
190
} else {
@@ -247,71 +247,71 @@ function soderlind_get_custom_javascript( $stylesheet = '' ) {
247
247
function soderlind_update_custom_javascript_post ( $ javascript , $ args = [] ) {
248
248
$ args = wp_parse_args (
249
249
$ args ,
250
- [
250
+ [
251
251
'preprocessed ' => '' ,
252
252
'stylesheet ' => get_stylesheet (),
253
253
]
254
254
);
255
255
256
- $ data = [
256
+ $ data = [
257
257
'javascript ' => $ javascript ,
258
- 'preprocessed ' => $ args ['preprocessed ' ],
258
+ 'preprocessed ' => $ args [ 'preprocessed ' ],
259
259
];
260
260
261
261
/**
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
+ */
294
294
$ data = apply_filters ( 'soderlind_update_custom_javascript_data ' , $ data , array_merge ( $ args , compact ( 'javascript ' ) ) );
295
295
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 ' ] ),
299
299
'post_type ' => 'custom_javascript ' ,
300
300
'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 ' ],
303
303
];
304
304
305
305
// 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 ' ] );
307
307
if ( $ post ) {
308
- $ post_data ['ID ' ] = $ post ->ID ;
308
+ $ post_data [ 'ID ' ] = $ post ->ID ;
309
309
$ r = wp_update_post ( wp_slash ( $ post_data ), true );
310
310
} else {
311
311
$ r = wp_insert_post ( wp_slash ( $ post_data ), true );
312
312
313
313
if ( ! is_wp_error ( $ r ) ) {
314
- if ( get_stylesheet () === $ args ['stylesheet ' ] ) {
314
+ if ( get_stylesheet () === $ args [ 'stylesheet ' ] ) {
315
315
set_theme_mod ( 'custom_javascript_post_id ' , $ r );
316
316
}
317
317
@@ -336,7 +336,7 @@ function soderlind_update_custom_javascript_post( $javascript, $args = [] ) {
336
336
function customize_preview_additional_javascript () {
337
337
$ handle = 'customize-preview-additional-javascript ' ;
338
338
$ src = plugins_url ( '/js/additional-javascript-preview.js ' , __FILE__ );
339
- $ deps = [ 'customize-preview ' , ' jquery ' ];
339
+ $ deps = [ 'customize-preview ' ];
340
340
wp_enqueue_script ( $ handle , $ src , $ deps , rand (), true );
341
341
}
342
342
0 commit comments