PluginProbe
Pay with Vipps and MobilePay for WooCommerce / 5.4.2
Pay with Vipps and MobilePay for WooCommerce v5.4.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 5.3.4 trunk All 183 releases
woo-vipps / payment / Blocks / woo-vipps-blocks.php

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

148 lines 6.8 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 }
29 });
30
31 // Inject block config variables to block editor assets
32 add_action('enqueue_block_editor_assets', function () {
33
34
35
36
37
38 // vipps-badge config
39 $vipps = Vipps::instance();
40 $badge_variants = [
41 ['label' => __('White', 'woo-vipps'), 'value' => 'white'],
42 ['label' => __('Grey', 'woo-vipps'), 'value' => 'grey'],
43 ['label' => __('Filled', 'woo-vipps'), 'value' => 'filled'],
44 ['label' => __('Light', 'woo-vipps'), 'value' => 'light'],
45 ['label' => __('Purple', 'woo-vipps'), 'value' => 'purple']];
46
47 $store_language = substr(get_bloginfo('language'), 0, 2);
48 if ($store_language == 'nb' || $store_language == 'nn') {
49 $store_language = 'no';
50 }
51 if ($store_language == 'da') {
52 $store_language = 'dk';
53 }
54 if (!in_array($store_language, ['en', 'no', 'dk', 'fi'])) {
55 $store_language = 'en'; // english default fallback
56 }
57
58 $languages = [
59 ['label' => __('Default', 'woo-vipps'), 'value' => $store_language],
60 ['label' => __('English', 'woo-vipps'), 'value' => 'en'],
61 ['label' => __('Norwegian', 'woo-vipps'), 'value' => 'no'],
62 ['label' => __('Finnish', 'woo-vipps'), 'value' => 'fi'],
63 ['label' => __('Danish', 'woo-vipps'), 'value' => 'dk'],
64 ];
65
66 $badge_config = [
67 'title' => sprintf(__('%1$s On-Site Messaging Badge', 'woo-vipps'), Vipps::CompanyName()),
68 'variants' => $badge_variants,
69 'iconSrc' => plugins_url('../img/vipps-mobilepay-logo-only.png', __FILE__),
70 'brand' => strtolower($vipps->get_payment_method_name()),
71 'languages' => $languages,
72 ];
73
74 // Inject block config to vipps-badge editor script. LP 15.11.2024
75 wp_add_inline_script(
76 'woo-vipps-vipps-badge-editor-script',
77 'const injectedVippsBadgeBlockConfig = ' . json_encode($badge_config),
78 'before'
79 );
80
81 // Inject config from Vipps.class.php to buy-now editor script. LP 29.11.2024
82 // Buy-now product block uses the JS event 'wc-blocks_product_list_rendered', introduced in woocommerce 9.4. LP 29.11.2024
83 if (version_compare(WC_VERSION, '9.4', '>=')) {
84 $payment_method = Vipps::instance()->get_payment_method_name();
85 $store_language = Vipps::instance()->get_customer_language();
86
87 switch ($payment_method) {
88 case 'Vipps':
89 $buy_now_languages = [
90 ['label' => __('Store language', 'woo-vipps'), 'value' => "store"],
91 ['label' => __('English', 'woo-vipps'), 'value' => 'en'],
92 ['label' => __('Norwegian', 'woo-vipps'), 'value' => 'no'],
93 ['label' => __('Swedish', 'woo-vipps'), 'value' => 'se'],
94 ];
95 break;
96 case 'MobilePay':
97 $buy_now_languages = [
98 ['label' => __('Store language', 'woo-vipps'), 'value' => "store"],
99 ['label' => __('English', 'woo-vipps'), 'value' => 'en'],
100 ['label' => __('Finnish', 'woo-vipps'), 'value' => 'fi'],
101 ['label' => __('Danish', 'woo-vipps'), 'value' => 'dk'],
102 ];
103 break;
104 }
105
106 // Array of associative arrays with keys 'label' and 'value'. LP 2026-01-16
107 $buy_now_variants = [
108 ['label' => __('Default', 'woo-vipps'), 'value' => 'default-mini'],
109 ];
110 foreach (Vipps::instance()->get_express_logo_variants() as $value => $label) {
111 $buy_now_variants[] = ['label' => $label, 'value' => $value];
112 }
113
114 // Create array of language => variant => logo_url. LP 2026-01-16
115 $logos = [];
116 foreach ($buy_now_languages as $lang_arr) {
117 $lang_key = $lang_arr['value'];
118 $lang = $lang_key === 'store' ? $store_language : $lang_key;
119 foreach ($buy_now_variants as $variant_arr) {
120 $variant = $variant_arr['value'];
121 $logos[$lang_key][$variant] = Vipps::instance()->get_express_logo($payment_method, $lang, $variant);
122 }
123 }
124
125 $buy_now_config = [
126 'BuyNowWithVipps' => Vipps::instance()->vippsJSConfig['vippssmileurl'],
127 'logos' => $logos,
128 'vippssmileurl' => Vipps::instance()->vippsJSConfig['vippssmileurl'],
129 'vippsbuynowbutton' => Vipps::instance()->vippsJSConfig['vippsbuynowbutton'],
130 'vippsbuynowdescription' => Vipps::instance()->vippsJSConfig['vippsbuynowdescription'],
131 'languages' => $buy_now_languages,
132 'variants' => $buy_now_variants,
133 'vippsresturl' => '/woo-vipps/v1',
134 ];
135 wp_add_inline_script('woo-vipps-buy-now-editor-script', 'const vippsBuyNowBlockConfig = ' . json_encode($buy_now_config), 'before');
136
137 // Buy now block for the minicart only. LP 2026-02-09
138 $buy_now_cart_config = [
139 'vippsbuynowdescription' => sprintf(__( 'Add a %1$s Buy Now-button to the mini-cart', 'woo-vipps'), $vipps->get_payment_method_name()),
140 'vippsbuynowbutton' => $buy_now_config['vippsbuynowbutton'],
141 'vippssmileurl' => $buy_now_config['vippssmileurl'],
142 'BuyNowWithVipps' => $buy_now_config['BuyNowWithVipps'],
143 'minicartLogo' => $vipps->get_payment_logo('minicart'),
144 ];
145 wp_add_inline_script('woo-vipps-buy-now-cart-editor-script', 'const vippsBuyNowCartBlockConfig = ' . json_encode($buy_now_cart_config), 'before');
146 }
147 });
148