Skip to content

Commit

Permalink
Merge pull request #1819 from skaut/wp_add_inline_script
Browse files Browse the repository at this point in the history
Using wp_add_inline_script instead of wp_localize_script
  • Loading branch information
marekdedic authored Feb 5, 2023
2 parents a80624c + b4ed413 commit 56375ce
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 15 deletions.
12 changes: 6 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/php/admin/class-tinymce-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public static function register_scripts_styles() {

Script_And_Style_Helpers::register_and_enqueue_style( 'sgdg_tinymce', 'admin/css/tinymce.min.css' );
Script_And_Style_Helpers::register_and_enqueue_script( 'sgdg_tinymce', 'admin/js/tinymce.min.js' );
wp_localize_script(
Script_And_Style_Helpers::add_script_configuration(
'sgdg_tinymce',
'sgdgTinymceLocalize',
array(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public static function register_scripts_styles( $hook ) {
'admin/js/root_selection.min.js',
array( 'jquery' )
);
wp_localize_script(
Script_And_Style_Helpers::add_script_configuration(
'sgdg_root_selection_ajax',
'sgdgRootpathLocalize',
array(
Expand Down
12 changes: 6 additions & 6 deletions src/php/frontend/class-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,19 @@ public static function add() {
$options = new Options_Proxy();
$get_option = static function( $name ) use ( $options ) {
return array(
'default' => $options->get( $name ),
'name' => $options->get_title( $name ),
'default' => strval( $options->get( $name ) ),
'name' => strval( $options->get_title( $name ) ),
);
};
$get_ordering_option = static function( $name ) use ( $options ) {
return array(
'default_by' => $options->get_by( $name ),
'default_order' => $options->get_order( $name ),
'name' => $options->get_title( $name ),
'default_by' => strval( $options->get_by( $name ) ),
'default_order' => strval( $options->get_order( $name ) ),
'name' => strval( $options->get_title( $name ) ),
);
};

wp_localize_script(
Script_And_Style_Helpers::add_script_configuration(
'sgdg_block',
'sgdgBlockLocalize',
array(
Expand Down
2 changes: 1 addition & 1 deletion src/php/frontend/class-shortcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public static function html( $atts ) {
$options = new Options_Proxy( $atts );

wp_enqueue_script( 'sgdg_gallery_init' );
wp_localize_script(
Script_And_Style_Helpers::add_script_configuration(
'sgdg_gallery_init',
'sgdgShortcodeLocalize',
array(
Expand Down
13 changes: 13 additions & 0 deletions src/php/helpers/class-script-and-style-helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,17 @@ public static function register_and_enqueue_style( $handle, $src, $deps = array(
wp_enqueue_style( $handle );
}

/**
* Adds a configuration to an already registered/enqueued script.
*
* @param string $handle A unique handle to identify the script with.
* @param string $js_var_name The name of the JavaScript variable that the configuration will be accessible in.
* @param array<string, string|array<string, string>> $data The actual configuration data.
*
* @return void
*/
public static function add_script_configuration( $handle, $js_var_name, $data ) {
wp_add_inline_script( $handle, 'const ' . $js_var_name . ' = ' . wp_json_encode( $data ) . ';', 'before' );
}

}

0 comments on commit 56375ce

Please sign in to comment.