PluginProbe
Pay with Vipps and MobilePay for WooCommerce / 6.1.2
Pay with Vipps and MobilePay for WooCommerce v6.1.2
6.2.5 6.2.4 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 All 187 releases
woo-vipps / payment / Blocks / woo-vipps-blocks.php

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

155 lines 7.5 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 ($store_language == 'da') {
50 $store_language = 'dk';
51 }
52 if (!in_array($store_language, ['en', 'no', 'dk', 'fi'])) {
53 $store_language = 'en'; // english default fallback
54 }
55
56 $languages = [
57 ['label' => __('Default', 'woo-vipps'), 'value' => $store_language],
58 ['label' => __('English', 'woo-vipps'), 'value' => 'en'],
59 ['label' => __('Norwegian', 'woo-vipps'), 'value' => 'no'],
60 ['label' => __('Finnish', 'woo-vipps'), 'value' => 'fi'],
61 ['label' => __('Danish', 'woo-vipps'), 'value' => 'dk'],
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
85 switch ($payment_method) {
86 case 'Vipps':
87 $buy_now_languages = [
88 ['label' => __('Store language', 'woo-vipps'), 'value' => "store"],
89 ['label' => __('English', 'woo-vipps'), 'value' => 'en'],
90 ['label' => __('Norwegian', 'woo-vipps'), 'value' => 'no'],
91 ['label' => __('Swedish', 'woo-vipps'), 'value' => 'sv'],
92 ];
93 break;
94 case 'MobilePay':
95 $buy_now_languages = [
96 ['label' => __('Store language', 'woo-vipps'), 'value' => "store"],
97 ['label' => __('English', 'woo-vipps'), 'value' => 'en'],
98 ['label' => __('Finnish', 'woo-vipps'), 'value' => 'fi'],
99 ['label' => __('Danish', 'woo-vipps'), 'value' => 'dk'],
100 ];
101 break;
102 }
103
104 $buy_now_variants = [
105 ['label' => __('Primary', 'woo-vipps'), 'value' => 'primary'],
106 ['label' => __('Dark', 'woo-vipps'), 'value' => 'dark'],
107 ['label' => __('Light', 'woo-vipps'), 'value' => 'light'],
108 ];
109
110 $buy_now_verbs = [
111 ['label' => __('Buy', 'woo-vipps'), 'value' => 'buy'],
112 ['label' => __('Pay', 'woo-vipps'), 'value' => 'pay'],
113 ['label' => __('Continue', 'woo-vipps'), 'value' => 'continue'],
114 ['label' => __('Confirm', 'woo-vipps'), 'value' => 'confirm'],
115 ['label' => __('Donate', 'woo-vipps'), 'value' => 'donate'],
116 ['label' => __('Express', 'woo-vipps'), 'value' => 'express'],
117 ];
118
119 // Migrate from old variants to new config array by sending a map. LP 2026-07-01
120 $variant_migration_map = [];
121 foreach(array_keys(Vipps::instance()->get_express_logo_variants()) as $old_variant) {
122 $new_config = Vipps::instance()->migrate_button_variant_to_config($old_variant);
123 unset($new_config['brand']); // brand needs to be dynamic from payment method! LP 2026-07-01
124 unset($new_config['language']);
125 unset($new_config['stretched']);
126 $variant_migration_map[$old_variant] = $new_config;
127 }
128
129 $buy_now_config = [
130 'BuyNowWithVipps' => Vipps::instance()->vippsJSConfig['vippssmileurl'],
131 'vippssmileurl' => Vipps::instance()->vippsJSConfig['vippssmileurl'],
132 'vippsbuynowbutton' => Vipps::instance()->vippsJSConfig['vippsbuynowbutton'],
133 'vippsbuynowdescription' => Vipps::instance()->vippsJSConfig['vippsbuynowdescription'],
134 'languages' => $buy_now_languages,
135 'variants' => $buy_now_variants,
136 'verbs' => $buy_now_verbs,
137 'vippsresturl' => '/woo-vipps/v1',
138 'paymentMethod' => Vipps::instance()->get_payment_method_name(),
139 'storeLanguage' => Vipps::instance()->get_customer_language(),
140 'variantMigrationMap' => $variant_migration_map,
141 ];
142 wp_add_inline_script('woo-vipps-buy-now-editor-script', 'const vippsBuyNowBlockConfig = ' . json_encode($buy_now_config), 'before');
143
144 // Buy now block for the minicart only. LP 2026-02-09
145 $buy_now_cart_config = [
146 'vippsbuynowdescription' => sprintf(__( 'Add a %1$s Buy Now-button to the mini-cart', 'woo-vipps'), $vipps->get_payment_method_name()),
147 'vippsbuynowbutton' => $buy_now_config['vippsbuynowbutton'],
148 'vippssmileurl' => $buy_now_config['vippssmileurl'],
149 'BuyNowWithVipps' => $buy_now_config['BuyNowWithVipps'],
150 'buttonHtml' => $vipps->get_html_button_for_context('minicart'),
151 ];
152 wp_add_inline_script('woo-vipps-buy-now-cart-editor-script', 'const vippsBuyNowCartBlockConfig = ' . json_encode($buy_now_cart_config), 'before');
153 }
154 });
155