PluginProbe
Pay with Vipps and MobilePay for WooCommerce / 6.1.9
Pay with Vipps and MobilePay for WooCommerce v6.1.9
6.2.3 6.2.2 6.2.1 6.2.0 6.1.10 6.1.9 6.1.8 6.1.7 6.1.6 6.1.5 6.1.4 6.1.3 6.1.2 6.1.1 6.1.0 6.0.5 6.0.4 6.0.3 6.0.2 6.0.1 6.0.0 5.4.3 5.4.2 5.4.1 5.4.0 All 185 releases
woo-vipps / payment / Blocks / woo-vipps-blocks.php

woo-vipps-blocks.php in Pay with Vipps and MobilePay for WooCommerce 6.1.9, at payment/Blocks/woo-vipps-blocks.php

161 lines 8.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // This is the script to register blocks built into ./dist. LP 19.11.2024
4
5 // Register blocks
6 add_action('init', function () {
7 // vipps-badge block. LP 29.11.2025
8 register_block_type(__DIR__ . '/dist/vipps-badge');
9
10 // Buy-now product block uses the JS event 'wc-blocks_product_list_rendered', introduced in woocommerce 9.4. LP 29.11.2024
11 // See vipp.js - this event is used to initialize the buy-now buttons IOK 2026-01-14
12 if (version_compare(WC_VERSION, '9.4', '>=')) {
13 register_block_type(__DIR__ . '/dist/buy-now');
14 register_block_type(__DIR__ . '/dist/buy-now-cart');
15 }
16 });
17
18 // Add scripts for web components to the block editor. You would expect this to work with block.json or enqueue_block_editor_assets, but no,
19 // that doesn't work at all. THIS works though. IOK 2026-02-25
20 // https://developer.wordpress.org/block-editor/how-to-guides/enqueueing-assets-in-the-editor/
21 add_action('enqueue_block_assets', function () {
22 // CSS common for several blocks etc. Enqued both in admin and frontend. IOK 2025-02-25
23 wp_enqueue_style('vipps-block-editor-css', plugins_url('../css/blocks.css', __FILE__), [], filemtime(dirname(dirname(__FILE__)) . "/css/blocks.css"));
24
25 if (is_admin()) {
26 // Add the on-site-messaging web component if we are in the admin area.
27 wp_enqueue_script('vipps-onsite-messageing');
28 wp_enqueue_script('vipps-button-webcomponent');
29 }
30 });
31
32 // Inject block config variables to block editor assets
33 add_action('enqueue_block_editor_assets', function () {
34 wp_enqueue_script('vipps-button-webcomponent');
35
36 // vipps-badge config
37 $vipps = Vipps::instance();
38 $badge_variants = [
39 ['label' => __('White', 'woo-vipps'), 'value' => 'white'],
40 ['label' => __('Grey', 'woo-vipps'), 'value' => 'grey'],
41 ['label' => __('Filled', 'woo-vipps'), 'value' => 'filled'],
42 ['label' => __('Light', 'woo-vipps'), 'value' => 'light'],
43 ['label' => __('Purple', 'woo-vipps'), 'value' => 'purple']];
44
45 $store_language = substr(get_bloginfo('language'), 0, 2);
46 if ($store_language == 'nb' || $store_language == 'nn') {
47 $store_language = 'no';
48 }
49 if (!in_array($store_language, ['en', 'no', 'dk', 'da', 'fi'])) {
50 $store_language = 'en'; // english default fallback
51 }
52 // Looks like button and badge web components now use 'da' instead of 'dk' for danish. LP 2026-08-11
53 if ('dk' === $store_language) $store_language = 'da';
54
55 $languages = [
56 ['label' => __('Default', 'woo-vipps'), 'value' => $store_language],
57 ['label' => __('English', 'woo-vipps'), 'value' => 'en'],
58 ['label' => __('Norwegian', 'woo-vipps'), 'value' => 'no'],
59 ['label' => __('Swedish', 'woo-vipps'), 'value' => 'sv'],
60 ['label' => __('Finnish', 'woo-vipps'), 'value' => 'fi'],
61 ['label' => __('Danish', 'woo-vipps'), 'value' => 'da'],
62 ];
63
64 $badge_config = [
65 'title' => sprintf(__('%1$s On-Site Messaging Badge', 'woo-vipps'), Vipps::CompanyName()),
66 'variants' => $badge_variants,
67 'iconSrc' => plugins_url('../img/vipps-mobilepay-logo-only.png', __FILE__),
68 'brand' => strtolower($vipps->get_payment_method_name()),
69 'languages' => $languages,
70 ];
71
72 // Inject block config to vipps-badge editor script. LP 15.11.2024
73 wp_add_inline_script(
74 'woo-vipps-vipps-badge-editor-script',
75 'const injectedVippsBadgeBlockConfig = ' . json_encode($badge_config),
76 'before'
77 );
78
79 // Inject config from Vipps.class.php to buy-now editor script. LP 29.11.2024
80 // Buy-now product block uses the JS event 'wc-blocks_product_list_rendered', introduced in woocommerce 9.4. LP 29.11.2024
81 if (version_compare(WC_VERSION, '9.4', '>=')) {
82 $payment_method = Vipps::instance()->get_payment_method_name();
83 $store_language = Vipps::instance()->get_customer_language();
84 // Looks like button and badge web components now use 'da' instead of 'dk' for danish. LP 2026-08-11
85 if ('dk' === $store_language) $store_language = 'da';
86
87
88 switch ($payment_method) {
89 case 'Vipps':
90 $buy_now_languages = [
91 ['label' => __('Store language', 'woo-vipps'), 'value' => "store"],
92 ['label' => __('English', 'woo-vipps'), 'value' => 'en'],
93 ['label' => __('Norwegian', 'woo-vipps'), 'value' => 'no'],
94 ['label' => __('Swedish', 'woo-vipps'), 'value' => 'sv'],
95 ['label' => __('Danish', 'woo-vipps'), 'value' => 'da'],
96 ];
97 break;
98 case 'MobilePay':
99 $buy_now_languages = [
100 ['label' => __('Store language', 'woo-vipps'), 'value' => "store"],
101 ['label' => __('English', 'woo-vipps'), 'value' => 'en'],
102 ['label' => __('Norwegian', 'woo-vipps'), 'value' => 'no'],
103 ['label' => __('Swedish', 'woo-vipps'), 'value' => 'sv'],
104 ['label' => __('Danish', 'woo-vipps'), 'value' => 'da'],
105 ['label' => __('Finnish', 'woo-vipps'), 'value' => 'fi'],
106 ];
107 break;
108 }
109
110 $buy_now_variants = [
111 ['label' => __('Primary', 'woo-vipps'), 'value' => 'primary'],
112 ['label' => __('Dark', 'woo-vipps'), 'value' => 'dark'],
113 ['label' => __('Light', 'woo-vipps'), 'value' => 'light'],
114 ];
115
116 $buy_now_verbs = [
117 ['label' => __('Buy', 'woo-vipps'), 'value' => 'buy'],
118 ['label' => __('Pay', 'woo-vipps'), 'value' => 'pay'],
119 ['label' => __('Continue', 'woo-vipps'), 'value' => 'continue'],
120 ['label' => __('Confirm', 'woo-vipps'), 'value' => 'confirm'],
121 ['label' => __('Donate', 'woo-vipps'), 'value' => 'donate'],
122 ['label' => __('Express', 'woo-vipps'), 'value' => 'express'],
123 ];
124
125 // Migrate from old variants to new config array by sending a map. LP 2026-07-01
126 $variant_migration_map = [];
127 foreach(array_keys(Vipps::instance()->get_express_logo_variants()) as $old_variant) {
128 $new_config = Vipps::instance()->migrate_button_variant_to_config($old_variant);
129 unset($new_config['brand']); // brand needs to be dynamic from payment method! LP 2026-07-01
130 unset($new_config['language']);
131 unset($new_config['stretched']);
132 $variant_migration_map[$old_variant] = $new_config;
133 }
134
135 $buy_now_config = [
136 'BuyNowWithVipps' => Vipps::instance()->vippsJSConfig['vippssmileurl'],
137 'vippssmileurl' => Vipps::instance()->vippsJSConfig['vippssmileurl'],
138 'vippsbuynowbutton' => Vipps::instance()->vippsJSConfig['vippsbuynowbutton'],
139 'vippsbuynowdescription' => Vipps::instance()->vippsJSConfig['vippsbuynowdescription'],
140 'languages' => $buy_now_languages,
141 'variants' => $buy_now_variants,
142 'verbs' => $buy_now_verbs,
143 'vippsresturl' => '/woo-vipps/v1',
144 'paymentMethod' => Vipps::instance()->get_payment_method_name(),
145 'storeLanguage' => Vipps::instance()->get_customer_language(),
146 'variantMigrationMap' => $variant_migration_map,
147 ];
148 wp_add_inline_script('woo-vipps-buy-now-editor-script', 'const vippsBuyNowBlockConfig = ' . json_encode($buy_now_config), 'before');
149
150 // Buy now block for the minicart only. LP 2026-02-09
151 $buy_now_cart_config = [
152 'vippsbuynowdescription' => sprintf(__( 'Add a %1$s Buy Now-button to the mini-cart', 'woo-vipps'), $vipps->get_payment_method_name()),
153 'vippsbuynowbutton' => $buy_now_config['vippsbuynowbutton'],
154 'vippssmileurl' => $buy_now_config['vippssmileurl'],
155 'BuyNowWithVipps' => $buy_now_config['BuyNowWithVipps'],
156 'buttonHtml' => $vipps->get_html_button_for_context('minicart'),
157 ];
158 wp_add_inline_script('woo-vipps-buy-now-cart-editor-script', 'const vippsBuyNowCartBlockConfig = ' . json_encode($buy_now_cart_config), 'before');
159 }
160 });
161