| @@ -310,20 +310,10 @@ | ||
| 310 | 310 | |
| 311 | 311 | |
| 312 | 312 | // We want this special page to have a certain title and maybe special scripts and so on, |
| 313 | 313 | // this gets run in template redirect for these pages. |
| 314 | - add_action('woo_vipps_before_handling_special_page', function ($action) { | |
| 315 | - // Change title dynamically depending on action. LP 2026-09-02 | |
| 316 | - add_filter('the_title', [$this, 'vipps_special_page_endpoint_title'], 10, 2); | |
| 314 | + add_action('woo_vipps_before_handling_special_page', array($this, 'pre_special_page_actions')); | |
| 317 | 315 | |
| 318 | - // If we are handling the 'wait for payment' action, we need to poll the order status before | |
| 319 | - // we start producing content IOK 2026-09-21 | |
| 320 | - if ($action == 'wait_for_payment') { | |
| 321 | - $this->handle_payment_poll_and_redirect(); | |
| 322 | - } | |
| 323 | - | |
| 324 | - }); | |
| 325 | - | |
| 326 | 316 | // Add an admin interface for this page as well IOK 2026-09-11 |
| 327 | 317 | add_action('woocommerce_settings_pages', array($this, 'woocommerce_settings_pages')); |
| 328 | 318 | |
| 329 | 319 | } |
| @@ -1053,12 +1043,30 @@ | ||
| 1053 | 1043 | |
| 1054 | 1044 | public function get_html_button_attrs_for_context($context = 'global') { |
| 1055 | 1045 | $options = get_option('vipps_button_options2', []); |
| 1056 | 1046 | if (!is_string($context)) $context = 'global'; |
| 1047 | + | |
| 1048 | + // Gutenberg express checkout buttons really want to be stretched, so we'll treat them somewhat differently. | |
| 1049 | + $gutenberg = false; | |
| 1050 | + if ($context == 'checkout_gutenberg') { | |
| 1051 | + $context = 'checkout'; | |
| 1052 | + $gutenberg = true; | |
| 1053 | + } | |
| 1054 | + if ($context == 'cart_gutenberg') { | |
| 1055 | + $context = 'cart'; | |
| 1056 | + $gutenberg = true; | |
| 1057 | + } | |
| 1058 | + | |
| 1057 | 1059 | $config = $options['express']['configs'][$context] ?? []; |
| 1058 | - if (!$config || ($config['use-global-config'] ?? false)) { | |
| 1060 | + $use_global = !$config || ($config['use-global-config'] ?? false); | |
| 1061 | + if ($use_global) { | |
| 1059 | 1062 | $config = $options['express']['configs']['global'] ?? $this->get_html_button_default_attrs(); |
| 1060 | 1063 | } |
| 1064 | + | |
| 1065 | + // see above. | |
| 1066 | + if ($gutenberg) { | |
| 1067 | + $config['stretched']='true'; | |
| 1068 | + } | |
| 1061 | 1069 | return $config; |
| 1062 | 1070 | } |
| 1063 | 1071 | |
| 1064 | 1072 | public function get_html_button_for_context($context = 'global') { |
| @@ -1145,8 +1153,10 @@ | ||
| 1145 | 1153 | private function button_menu_express_section() { |
| 1146 | 1154 | $options = get_option('vipps_button_options2', []); |
| 1147 | 1155 | $express = $options['express'] ?? []; |
| 1148 | 1156 | $configs = $express['configs'] ?? []; |
| 1157 | + | |
| 1158 | + | |
| 1149 | 1159 | $contexts = [ |
| 1150 | 1160 | 'global' => __('Global', 'woo-vipps'), |
| 1151 | 1161 | 'product' => __('Product', 'woo-vipps'), |
| 1152 | 1162 | 'catalog' => __('Catalog', 'woo-vipps'), |
| @@ -1319,8 +1329,9 @@ | ||
| 1319 | 1329 | |
| 1320 | 1330 | // Swap to new context: set all input fields to the stored values if exists. LP 2026-06-25 |
| 1321 | 1331 | const newContext = jQuery("#context").val(); |
| 1322 | 1332 | const newConfig = contextConfigs[newContext]; |
| 1333 | + | |
| 1323 | 1334 | setInputsFromConfig(newContext, newConfig); |
| 1324 | 1335 | currentContext = newContext; |
| 1325 | 1336 | } |
| 1326 | 1337 | |
| @@ -1921,16 +1932,15 @@ | ||
| 1921 | 1932 | public function minicart_express_checkout_button() { |
| 1922 | 1933 | $gw = $this->gateway(); |
| 1923 | 1934 | |
| 1924 | 1935 | if ($gw->show_express_checkout()){ |
| 1925 | - return $this->cart_express_checkout_button_html(true); | |
| 1936 | + return $this->cart_express_checkout_button_html('minicart'); | |
| 1926 | 1937 | } |
| 1927 | 1938 | } |
| 1928 | 1939 | |
| 1929 | - public function cart_express_checkout_button_html($minicart = false) { | |
| 1940 | + public function cart_express_checkout_button_html($context= 'cart') { | |
| 1930 | 1941 | $url = $this->express_checkout_url(); |
| 1931 | 1942 | $url = wp_nonce_url($url,'express','sec'); |
| 1932 | - $context = $minicart ? 'minicart' : 'cart'; | |
| 1933 | 1943 | $button= apply_filters('woo_vipps_express_checkout_button', $this->get_html_button_for_context($context)); |
| 1934 | 1944 | $method = $this->get_payment_method_name(); |
| 1935 | 1945 | $title = sprintf(__('Buy now with %1$s!', 'woo-vipps'), $method); |
| 1936 | 1946 | $html = "<a href='$url' class='vipps-express-checkout short $method' title='$title'>$button</a>"; |
| @@ -1973,9 +1983,9 @@ | ||
| 1973 | 1983 | public function express_checkout_button_shortcode() { |
| 1974 | 1984 | $gw = $this->gateway(); |
| 1975 | 1985 | if (!$gw->cart_supports_express_checkout()) return; |
| 1976 | 1986 | ob_start(); |
| 1977 | - $this->cart_express_checkout_button_html('shortcode'); | |
| 1987 | + $this->cart_express_checkout_button_html('cart'); | |
| 1978 | 1988 | return ob_get_clean(); |
| 1979 | 1989 | } |
| 1980 | 1990 | // Show a banner normally shown for non-logged-in-users at the checkout page. It does not need to check if we are to show the button, obviously, but needs to see if the cart works |
| 1981 | 1991 | public function express_checkout_banner_shortcode() { |
| @@ -2660,8 +2670,26 @@ | ||
| 2660 | 2670 | do_action('woo_vipps_before_handling_special_page', ($_GET['action'] ?? "")); |
| 2661 | 2671 | } |
| 2662 | 2672 | } |
| 2663 | 2673 | |
| 2674 | + // Ran in template redirect for the special page. IOK 2026-09-2 | |
| 2675 | + public function pre_special_page_actions ($action) { | |
| 2676 | + // Change title dynamically depending on action. LP 2026-09-02 | |
| 2677 | + add_filter('the_title', [$this, 'vipps_special_page_endpoint_title'], 10, 2); | |
| 2678 | + | |
| 2679 | + // If we are handling the 'wait for payment' action, we need to poll the order status before | |
| 2680 | + // we start producing content IOK 2026-09-21 | |
| 2681 | + if ($action == 'wait_for_payment') { | |
| 2682 | + $this->handle_payment_poll_and_redirect(); | |
| 2683 | + } | |
| 2684 | + | |
| 2685 | + // Some validation is required for this action | |
| 2686 | + if ($action == 'do_express_checkout') { | |
| 2687 | + $this->vipps_express_checkout_consistency_check(); | |
| 2688 | + } | |
| 2689 | + } | |
| 2690 | + | |
| 2691 | + | |
| 2664 | 2692 | // Dynamic special page title depending on endpoint/action, only frontend. LP 2026-09-02 |
| 2665 | 2693 | public function vipps_special_page_endpoint_title($title, $postid = 0) { |
| 2666 | 2694 | global $wp_query; |
| 2667 | 2695 | // Comment from woocommerce's wc_page_endpoint_title where this logic is from: LP 2026-09-02 |
| @@ -4140,9 +4168,9 @@ | ||
| 4140 | 4168 | WC()->cart->calculate_totals(); |
| 4141 | 4169 | WC()->cart->set_session(); |
| 4142 | 4170 | return true; |
| 4143 | 4171 | } catch (Exception $e) { |
| 4144 | - $this->log(sprintf(__("Error regenerating cart from order %1\$d: %2\$s", 'woo-vipps'), $order_id, $e->get_message()), 'error'); | |
| 4172 | + $this->log(sprintf(__("Error regenerating cart from order %1\$d: %2\$s", 'woo-vipps'), $order_id, $e->getMessage()), 'error'); | |
| 4145 | 4173 | return false; |
| 4146 | 4174 | } |
| 4147 | 4175 | } |
| 4148 | 4176 | |
| @@ -4252,9 +4280,11 @@ | ||
| 4252 | 4280 | if ( empty($_REQUEST['add-to-cart']) || ! is_numeric($_REQUEST['add-to-cart']) || empty($_REQUEST['vipps_compat_mode']) || !$_REQUEST['vipps_compat_mode']) { |
| 4253 | 4281 | return $url; |
| 4254 | 4282 | } |
| 4255 | 4283 | $url = $this->express_checkout_url(); |
| 4256 | - $url = wp_nonce_url($url,'express','sec'); | |
| 4284 | + // At this point, there is always a query argument here. IOK 2026-09-21 | |
| 4285 | + $nonce = wp_create_nonce('express'); | |
| 4286 | + $url = $url . "&sec=$nonce"; | |
| 4257 | 4287 | |
| 4258 | 4288 | return $url; |
| 4259 | 4289 | } |
| 4260 | 4290 | |
| @@ -4687,9 +4717,10 @@ | ||
| 4687 | 4717 | } |
| 4688 | 4718 | if (!$o) return; |
| 4689 | 4719 | if (!$o->get_meta('_vipps_single_product_express')) return; |
| 4690 | 4720 | if ($failed && !apply_filters('woo_vipps_restore_cart_on_express_checkout_failure', true, $o)) return; |
| 4691 | - if ($failed) WC()->cart->empty_cart(); | |
| 4721 | + // Restoring cart! But clear it first so we dont add this single product to the restored cart. LP 2026-09-22 | |
| 4722 | + WC()->cart->empty_cart(); | |
| 4692 | 4723 | $this->restore_cart($o); |
| 4693 | 4724 | } |
| 4694 | 4725 | |
| 4695 | 4726 | |
| @@ -5396,15 +5427,13 @@ | ||
| 5396 | 5427 | |
| 5397 | 5428 | return $this->express_checkout_page_html(true,'do_single_product_express_checkout',$args); |
| 5398 | 5429 | } |
| 5399 | 5430 | |
| 5400 | - // This is a landing page for the express checkout of then normal cart - it is done like this because this could take time on slower hosts. | |
| 5401 | - public function vipps_express_checkout() { | |
| 5431 | + public function vipps_express_checkout_consistency_check() { | |
| 5402 | 5432 | // We need a nonce to get here, but we should only get here when we have a cart, so this will not be cached. |
| 5403 | 5433 | // IOK 2018-05-28 |
| 5404 | 5434 | $ok = isset($_REQUEST['sec']) && wp_verify_nonce($_REQUEST['sec'],'express'); |
| 5405 | 5435 | |
| 5406 | - | |
| 5407 | 5436 | $backurl = wp_validate_redirect(@$_SERVER['HTTP_REFERER']); |
| 5408 | 5437 | if (!$backurl) $backurl = home_url(); |
| 5409 | 5438 | |
| 5410 | 5439 | if (!$ok) { |
| @@ -5416,8 +5445,19 @@ | ||
| 5416 | 5445 | if ( WC()->cart->get_cart_contents_count() == 0 ) { |
| 5417 | 5446 | wc_add_notice(__('Your shopping cart is empty','woo-vipps'),'error'); |
| 5418 | 5447 | wp_redirect($backurl); |
| 5419 | 5448 | exit(); |
| 5449 | + } | |
| 5450 | + | |
| 5451 | + add_filter('woo_vipps_express_checkout_consistent', '__return_true'); | |
| 5452 | + } | |
| 5453 | + | |
| 5454 | + // This is a landing page for the express checkout of then normal cart - it is done like this because this could take time on slower hosts. | |
| 5455 | + public function vipps_express_checkout() { | |
| 5456 | + // Some checks are made in template_redirect, we check here if they are ok IOK 2026-09-21 | |
| 5457 | + if (!apply_filters('woo_vipps_express_checkout_consistent', false)) { | |
| 5458 | + $content = __('Link expired, please try again', 'woo-vipps'); | |
| 5459 | + return $content; | |
| 5420 | 5460 | } |
| 5421 | 5461 | |
| 5422 | 5462 | add_filter('body_class', function ($classes) { |
| 5423 | 5463 | $classes[] = 'vipps-express-checkout'; |