@@ -19,6 +19,7 @@ class Assets {
1919 */
2020 public static function init () {
2121 add_action ( 'init ' , array ( __CLASS__ , 'register_assets ' ) );
22+ add_action ( 'admin_print_scripts ' , array ( __CLASS__ , 'print_shared_settings ' ), 1 );
2223 add_action ( 'admin_print_scripts ' , array ( __CLASS__ , 'maybe_add_asset_data ' ), 1 );
2324 add_action ( 'admin_print_footer_scripts ' , array ( __CLASS__ , 'maybe_add_asset_data ' ), 1 );
2425 add_action ( 'wp_print_scripts ' , array ( __CLASS__ , 'maybe_add_asset_data ' ), 1 );
@@ -57,17 +58,21 @@ public static function register_assets() {
5758 self ::register_script ( 'wc-product-search ' , plugins_url ( 'build/product-search.js ' , __DIR__ ), array ( 'wc-vendors ' , 'wc-blocks ' ) );
5859 }
5960
61+ /**
62+ * Print wcSettings in all pages. This is a temporary fix until we find a better
63+ * solution to share settings between WooCommerce Admin and WooCommerce Blocks.
64+ * See https://github.com/woocommerce/woocommerce-gutenberg-products-block/issues/932
65+ */
66+ public static function print_shared_settings () {
67+ echo '<script> ' ;
68+ echo "var wcSettings = wcSettings || JSON.parse( decodeURIComponent( ' " . esc_js ( self ::get_wc_settings_data () ) . "' ) ); " ;
69+ echo '</script> ' ;
70+ }
71+
6072 /**
6173 * Attach data to registered assets using inline scripts.
6274 */
6375 public static function maybe_add_asset_data () {
64- if ( wp_script_is ( 'wc-shared-settings ' , 'enqueued ' ) ) {
65- wp_add_inline_script (
66- 'wc-shared-settings ' ,
67- self ::get_wc_settings_data (),
68- 'before '
69- );
70- }
7176 if ( wp_script_is ( 'wc-block-settings ' , 'enqueued ' ) ) {
7277 wp_add_inline_script (
7378 'wc-block-settings ' ,
@@ -121,8 +126,7 @@ protected static function get_wc_settings_data() {
121126 ),
122127 )
123128 );
124- $ settings = rawurlencode ( wp_json_encode ( $ settings ) );
125- return "var wcSettings = wcSettings || JSON.parse( decodeURIComponent( ' " . $ settings . "' ) ); " ;
129+ return rawurlencode ( wp_json_encode ( $ settings ) );
126130 }
127131
128132 /**
0 commit comments