Skip to content

Commit 56375ce

Browse files
authored
Merge pull request #1819 from skaut/wp_add_inline_script
Using wp_add_inline_script instead of wp_localize_script
2 parents a80624c + b4ed413 commit 56375ce

File tree

6 files changed

+28
-15
lines changed

6 files changed

+28
-15
lines changed

composer.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/php/admin/class-tinymce-plugin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public static function register_scripts_styles() {
7575

7676
Script_And_Style_Helpers::register_and_enqueue_style( 'sgdg_tinymce', 'admin/css/tinymce.min.css' );
7777
Script_And_Style_Helpers::register_and_enqueue_script( 'sgdg_tinymce', 'admin/js/tinymce.min.js' );
78-
wp_localize_script(
78+
Script_And_Style_Helpers::add_script_configuration(
7979
'sgdg_tinymce',
8080
'sgdgTinymceLocalize',
8181
array(

src/php/admin/settings-pages/basic/class-root-selection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public static function register_scripts_styles( $hook ) {
8989
'admin/js/root_selection.min.js',
9090
array( 'jquery' )
9191
);
92-
wp_localize_script(
92+
Script_And_Style_Helpers::add_script_configuration(
9393
'sgdg_root_selection_ajax',
9494
'sgdgRootpathLocalize',
9595
array(

src/php/frontend/class-block.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,19 @@ public static function add() {
4949
$options = new Options_Proxy();
5050
$get_option = static function( $name ) use ( $options ) {
5151
return array(
52-
'default' => $options->get( $name ),
53-
'name' => $options->get_title( $name ),
52+
'default' => strval( $options->get( $name ) ),
53+
'name' => strval( $options->get_title( $name ) ),
5454
);
5555
};
5656
$get_ordering_option = static function( $name ) use ( $options ) {
5757
return array(
58-
'default_by' => $options->get_by( $name ),
59-
'default_order' => $options->get_order( $name ),
60-
'name' => $options->get_title( $name ),
58+
'default_by' => strval( $options->get_by( $name ) ),
59+
'default_order' => strval( $options->get_order( $name ) ),
60+
'name' => strval( $options->get_title( $name ) ),
6161
);
6262
};
6363

64-
wp_localize_script(
64+
Script_And_Style_Helpers::add_script_configuration(
6565
'sgdg_block',
6666
'sgdgBlockLocalize',
6767
array(

src/php/frontend/class-shortcode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public static function html( $atts ) {
122122
$options = new Options_Proxy( $atts );
123123

124124
wp_enqueue_script( 'sgdg_gallery_init' );
125-
wp_localize_script(
125+
Script_And_Style_Helpers::add_script_configuration(
126126
'sgdg_gallery_init',
127127
'sgdgShortcodeLocalize',
128128
array(

src/php/helpers/class-script-and-style-helpers.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,17 @@ public static function register_and_enqueue_style( $handle, $src, $deps = array(
7878
wp_enqueue_style( $handle );
7979
}
8080

81+
/**
82+
* Adds a configuration to an already registered/enqueued script.
83+
*
84+
* @param string $handle A unique handle to identify the script with.
85+
* @param string $js_var_name The name of the JavaScript variable that the configuration will be accessible in.
86+
* @param array<string, string|array<string, string>> $data The actual configuration data.
87+
*
88+
* @return void
89+
*/
90+
public static function add_script_configuration( $handle, $js_var_name, $data ) {
91+
wp_add_inline_script( $handle, 'const ' . $js_var_name . ' = ' . wp_json_encode( $data ) . ';', 'before' );
92+
}
93+
8194
}

0 commit comments

Comments
 (0)