| 1 |
<?php |
| 2 |
/** |
| 3 |
* Global Checkout. |
| 4 |
* |
| 5 |
* @package CartFlows |
| 6 |
*/ |
| 7 |
|
| 8 |
// Exit if accessed directly. |
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
/** |
| 13 |
* Global Checkout |
| 14 |
* |
| 15 |
* @since 1.0.0 |
| 16 |
*/ |
| 17 |
class Cartflows_Global_Checkout { |
| 18 |
|
| 19 |
|
| 20 |
/** |
| 21 |
* Member Variable |
| 22 |
* |
| 23 |
* @var object instance |
| 24 |
*/ |
| 25 |
private static $instance; |
| 26 |
|
| 27 |
/** |
| 28 |
* Initiator |
| 29 |
*/ |
| 30 |
public static function get_instance() { |
| 31 |
if ( ! isset( self::$instance ) ) { |
| 32 |
self::$instance = new self(); |
| 33 |
} |
| 34 |
return self::$instance; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Constructor |
| 39 |
*/ |
| 40 |
public function __construct() { |
| 41 |
|
| 42 |
/* Global Checkout */ |
| 43 |
add_action( 'wp', array( $this, 'override_global_checkout' ), 0 ); |
| 44 |
add_action( 'wp', array( $this, 'maybe_override_order_pay_page' ), 0 ); |
| 45 |
add_action( 'template_redirect', array( $this, 'global_checkout_template_redirect' ), 1 ); |
| 46 |
add_action( 'admin_bar_menu', array( $this, 'update_checkout_link_for_global_checkout' ), 999 ); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Update the checkout page link for global checkout. |
| 51 |
* |
| 52 |
* @since 1.10.0 |
| 53 |
*/ |
| 54 |
public function update_checkout_link_for_global_checkout() { |
| 55 |
|
| 56 |
if ( _is_wcf_checkout_type() ) { |
| 57 |
|
| 58 |
global $wp_admin_bar; |
| 59 |
global $post; |
| 60 |
|
| 61 |
$common = Cartflows_Helper::get_common_settings(); |
| 62 |
$global_checkout = intval( $common['global_checkout'] ); |
| 63 |
$override_global_checkout = $common['override_global_checkout']; |
| 64 |
|
| 65 |
if ( $post && $global_checkout === $post->ID && 'enable' === $override_global_checkout ) { |
| 66 |
|
| 67 |
$edit_node = $wp_admin_bar->get_node( 'edit' ); |
| 68 |
|
| 69 |
if ( $edit_node ) { |
| 70 |
$edit_node->href = wcf()->flow->get_prepared_edit_step_url( $post->ID ); |
| 71 |
$wp_admin_bar->add_node( $edit_node ); |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Override global checkout page |
| 79 |
* |
| 80 |
* @since 1.10.0 |
| 81 |
*/ |
| 82 |
public function override_global_checkout() { |
| 83 |
|
| 84 |
if ( wcf()->utils->is_step_post_type() ) { |
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
if ( is_product() || ! is_checkout() || is_order_received_page() || is_checkout_pay_page() ) { |
| 89 |
return; |
| 90 |
} |
| 91 |
|
| 92 |
// Return if the key OR Order parameter is found in the URL for certain Payment gateways. |
| 93 |
if ( $this->should_show_global_checkout() ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 94 |
return; |
| 95 |
} |
| 96 |
|
| 97 |
$common = Cartflows_Helper::get_common_settings(); |
| 98 |
|
| 99 |
$global_checkout = $common['global_checkout']; |
| 100 |
$override_global_checkout = $common['override_global_checkout']; |
| 101 |
|
| 102 |
// Override only WooCommerce checkout page with CartFlows checkout page. |
| 103 |
if ( ! empty( $global_checkout ) && 'enable' === $override_global_checkout ) { |
| 104 |
|
| 105 |
$checkout_post = get_post( $global_checkout ); |
| 106 |
|
| 107 |
if ( $checkout_post && 'publish' === $checkout_post->post_status ) { |
| 108 |
|
| 109 |
if ( isset( $GLOBALS['posts'][0] ) ) { |
| 110 |
$GLOBALS['posts'][0] = $checkout_post; //phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
| 111 |
} |
| 112 |
|
| 113 |
if ( isset( $GLOBALS['wp_the_query']->post ) ) { |
| 114 |
$GLOBALS['wp_the_query']->post = $checkout_post; //phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
| 115 |
} |
| 116 |
|
| 117 |
$GLOBALS['post'] = $checkout_post; //phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Redirect from default to the global checkout page |
| 124 |
* |
| 125 |
* @since 1.10.0 |
| 126 |
*/ |
| 127 |
public function global_checkout_template_redirect() { |
| 128 |
|
| 129 |
if ( wcf()->utils->is_step_post_type() ) { |
| 130 |
return; |
| 131 |
} |
| 132 |
|
| 133 |
if ( is_product() || ! is_checkout() || is_order_received_page() || is_checkout_pay_page() ) { |
| 134 |
return; |
| 135 |
} |
| 136 |
|
| 137 |
// Return if the key OR Order parameter is found in the URL for certain Payment gateways. |
| 138 |
if ( $this->should_show_global_checkout() ) { |
| 139 |
return; |
| 140 |
} |
| 141 |
|
| 142 |
// redirect only for cartflows checkout pages. |
| 143 |
$common = Cartflows_Helper::get_common_settings(); |
| 144 |
|
| 145 |
$global_checkout = $common['global_checkout']; |
| 146 |
$override_global_checkout = $common['override_global_checkout']; |
| 147 |
|
| 148 |
if ( ! empty( $global_checkout ) && 'enable' !== $override_global_checkout ) { |
| 149 |
|
| 150 |
$link = apply_filters( 'cartflows_global_checkout_url', get_permalink( $global_checkout ) ); |
| 151 |
|
| 152 |
if ( ! empty( $link ) ) { |
| 153 |
|
| 154 |
wp_safe_redirect( $link ); |
| 155 |
exit; |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Decide weather to show the global checkout page or not. |
| 162 |
* |
| 163 |
* @since 2.0.3 |
| 164 |
* |
| 165 |
* @return bool |
| 166 |
*/ |
| 167 |
public static function should_show_global_checkout() { |
| 168 |
global $post; |
| 169 |
$checkout_id = ! empty( $post ) && ! empty( $post->ID ) ? intval( $post->ID ) : 0; |
| 170 |
|
| 171 |
$is_allow = ( isset( $_GET['key'] ) || isset( $_GET['order'] ) ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 172 |
return apply_filters( 'cartflows_allow_display_global_checkout', $is_allow, $checkout_id ); |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Override order-pay page with store checkout template. |
| 177 |
* |
| 178 |
* @since 2.2.2 |
| 179 |
* |
| 180 |
* @return void |
| 181 |
*/ |
| 182 |
public function maybe_override_order_pay_page() { |
| 183 |
|
| 184 |
if ( ! is_checkout_pay_page() ) { |
| 185 |
return; |
| 186 |
} |
| 187 |
|
| 188 |
if ( wcf()->utils->is_step_post_type() ) { |
| 189 |
return; |
| 190 |
} |
| 191 |
|
| 192 |
$common = Cartflows_Helper::get_common_settings(); |
| 193 |
|
| 194 |
$global_checkout = $common['global_checkout']; |
| 195 |
$override_global_checkout = $common['override_global_checkout']; |
| 196 |
$override_store_order_pay = $common['override_store_order_pay']; |
| 197 |
|
| 198 |
if ( empty( $global_checkout ) || 'enable' !== $override_global_checkout || 'enable' !== $override_store_order_pay ) { |
| 199 |
return; |
| 200 |
} |
| 201 |
|
| 202 |
// Ensure this is the store checkout and not a funnel. |
| 203 |
$store_checkout = Cartflows_Helper::get_global_setting( '_cartflows_store_checkout' ); |
| 204 |
|
| 205 |
if ( empty( $store_checkout ) ) { |
| 206 |
return; |
| 207 |
} |
| 208 |
|
| 209 |
$flow_id = wcf()->utils->get_flow_id_from_step_id( $global_checkout ); |
| 210 |
|
| 211 |
if ( intval( $store_checkout ) !== intval( $flow_id ) ) { |
| 212 |
return; |
| 213 |
} |
| 214 |
|
| 215 |
$checkout_post = get_post( $global_checkout ); |
| 216 |
|
| 217 |
if ( ! $checkout_post || 'publish' !== $checkout_post->post_status ) { |
| 218 |
return; |
| 219 |
} |
| 220 |
|
| 221 |
if ( isset( $GLOBALS['posts'][0] ) ) { |
| 222 |
$GLOBALS['posts'][0] = $checkout_post; //phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
| 223 |
} |
| 224 |
|
| 225 |
if ( isset( $GLOBALS['wp_the_query']->post ) ) { |
| 226 |
$GLOBALS['wp_the_query']->post = $checkout_post; //phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
| 227 |
} |
| 228 |
|
| 229 |
$GLOBALS['post'] = $checkout_post; //phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Kicking this off by calling 'get_instance()' method |
| 235 |
*/ |
| 236 |
Cartflows_Global_Checkout::get_instance(); |
| 237 |
|