| 1 |
<?php |
| 2 |
/** |
| 3 |
* CartFlows Frontend. |
| 4 |
* |
| 5 |
* @package CartFlows |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Class Cartflows_Frontend. |
| 10 |
*/ |
| 11 |
class Cartflows_Frontend { |
| 12 |
|
| 13 |
/** |
| 14 |
* Member Variable |
| 15 |
* |
| 16 |
* @var instance |
| 17 |
*/ |
| 18 |
private static $instance; |
| 19 |
|
| 20 |
/** |
| 21 |
* Initiator |
| 22 |
*/ |
| 23 |
public static function get_instance() { |
| 24 |
if ( ! isset( self::$instance ) ) { |
| 25 |
self::$instance = new self; |
| 26 |
} |
| 27 |
return self::$instance; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Constructor |
| 32 |
*/ |
| 33 |
public function __construct() { |
| 34 |
|
| 35 |
/* Set / Destroy Flow Sessions. Set data */ |
| 36 |
add_action( 'wp', array( $this, 'init_actions' ), 1 ); |
| 37 |
|
| 38 |
/* Enqueue global required scripts */ |
| 39 |
add_action( 'wp', array( $this, 'wp_actions' ), 55 ); |
| 40 |
|
| 41 |
/* Modify the checkout order received url to go thank you page in our flow */ |
| 42 |
add_filter( 'woocommerce_get_checkout_order_received_url', array( $this, 'redirect_to_thankyou_page' ), 10, 2 ); |
| 43 |
|
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Redirect to thank page if upsell not exists |
| 48 |
* |
| 49 |
* @param string $order_recieve_url url. |
| 50 |
* @param object $order order object. |
| 51 |
* @since 1.0.0 |
| 52 |
*/ |
| 53 |
function redirect_to_thankyou_page( $order_recieve_url, $order ) { |
| 54 |
|
| 55 |
/* Only for thank you page */ |
| 56 |
wcf()->logger->log( 'Start-' . __CLASS__ . '::' . __FUNCTION__ ); |
| 57 |
wcf()->logger->log( 'Only for thank you page' ); |
| 58 |
|
| 59 |
if ( wcf()->flow->is_thankyou_page_exists( $order ) ) { |
| 60 |
|
| 61 |
if ( _is_wcf_doing_checkout_ajax() ) { |
| 62 |
|
| 63 |
$checkout_id = wcf()->utils->get_checkout_id_from_post_data(); |
| 64 |
|
| 65 |
if ( ! $checkout_id ) { |
| 66 |
$checkout_id = wcf()->utils->get_checkout_id_from_order( $order->get_id() ); |
| 67 |
} |
| 68 |
} else { |
| 69 |
$checkout_id = wcf()->utils->get_checkout_id_from_order( $order->get_id() ); |
| 70 |
} |
| 71 |
|
| 72 |
wcf()->logger->log( 'Checkout ID : ' . $checkout_id ); |
| 73 |
|
| 74 |
if ( $checkout_id ) { |
| 75 |
|
| 76 |
$thankyou_step_id = wcf()->flow->get_thankyou_page_id( $order ); |
| 77 |
|
| 78 |
if ( $thankyou_step_id ) { |
| 79 |
|
| 80 |
$order_recieve_url = get_permalink( $thankyou_step_id ); |
| 81 |
|
| 82 |
$order_recieve_url = add_query_arg( |
| 83 |
array( |
| 84 |
'wcf-order' => $order->get_id(), |
| 85 |
'wcf-key' => $order->get_order_key(), |
| 86 |
), |
| 87 |
$order_recieve_url |
| 88 |
); |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
wcf()->logger->log( 'End-' . __CLASS__ . '::' . __FUNCTION__ ); |
| 94 |
|
| 95 |
return $order_recieve_url; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Cancel and redirect to checkout |
| 100 |
* |
| 101 |
* @param string $return_url url. |
| 102 |
* @since 1.0.0 |
| 103 |
*/ |
| 104 |
function redirect_to_checkout_on_cancel( $return_url ) { |
| 105 |
|
| 106 |
if ( _is_wcf_doing_checkout_ajax() ) { |
| 107 |
|
| 108 |
$checkout_id = wcf()->utils->get_checkout_id_from_post_data(); |
| 109 |
|
| 110 |
if ( ! $checkout_id ) { |
| 111 |
$checkout_id = wcf()->utils->get_checkout_id_from_order( $order->get_id() ); |
| 112 |
} |
| 113 |
} else { |
| 114 |
$checkout_id = wcf()->utils->get_checkout_id_from_order( $order->get_id() ); |
| 115 |
} |
| 116 |
|
| 117 |
if ( $checkout_id ) { |
| 118 |
|
| 119 |
$return_url = add_query_arg( |
| 120 |
array( |
| 121 |
'cancel_order' => 'true', |
| 122 |
'_wpnonce' => wp_create_nonce( 'woocommerce-cancel_order' ), |
| 123 |
), |
| 124 |
get_permalink( $checkout_id ) |
| 125 |
); |
| 126 |
} |
| 127 |
|
| 128 |
return $return_url; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
/** |
| 133 |
* Remove theme styles. |
| 134 |
* |
| 135 |
* @since 1.0.0 |
| 136 |
*/ |
| 137 |
function remove_theme_styles() { |
| 138 |
|
| 139 |
if ( Cartflows_Compatibility::get_instance()->is_compatibility_theme_enabled() ) { |
| 140 |
return; |
| 141 |
} |
| 142 |
|
| 143 |
// get all styles data. |
| 144 |
global $wp_styles; |
| 145 |
global $wp_scripts; |
| 146 |
|
| 147 |
$get_stylesheet = 'themes/' . get_stylesheet() . '/'; |
| 148 |
$get_template = 'themes/' . get_template() . '/'; |
| 149 |
|
| 150 |
$remove_styles = apply_filters( 'cartflows_remove_theme_styles', true ); |
| 151 |
|
| 152 |
if ( $remove_styles ) { |
| 153 |
|
| 154 |
// loop over all of the registered scripts.. |
| 155 |
foreach ( $wp_styles->registered as $handle => $data ) { |
| 156 |
|
| 157 |
if ( strpos( $data->src, $get_template ) !== false || strpos( $data->src, $get_stylesheet ) !== false ) { |
| 158 |
|
| 159 |
// remove it. |
| 160 |
wp_deregister_style( $handle ); |
| 161 |
wp_dequeue_style( $handle ); |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
$remove_scripts = apply_filters( 'cartflows_remove_theme_scripts', true ); |
| 167 |
|
| 168 |
if ( $remove_scripts ) { |
| 169 |
|
| 170 |
// loop over all of the registered scripts. |
| 171 |
foreach ( $wp_scripts->registered as $handle => $data ) { |
| 172 |
|
| 173 |
if ( strpos( $data->src, $get_template ) !== false || strpos( $data->src, $get_stylesheet ) !== false ) { |
| 174 |
|
| 175 |
// remove it. |
| 176 |
wp_deregister_script( $handle ); |
| 177 |
wp_dequeue_script( $handle ); |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Update main order data in transient. |
| 186 |
* |
| 187 |
* @param array $woo_styles new styles array. |
| 188 |
* @since 1.0.0 |
| 189 |
* @return array. |
| 190 |
*/ |
| 191 |
function woo_default_css( $woo_styles ) { |
| 192 |
|
| 193 |
$woo_styles = array( |
| 194 |
'woocommerce-layout' => array( |
| 195 |
'src' => plugins_url( 'assets/css/woocommerce-layout.css', WC_PLUGIN_FILE ), |
| 196 |
'deps' => '', |
| 197 |
'version' => WC_VERSION, |
| 198 |
'media' => 'all', |
| 199 |
'has_rtl' => true, |
| 200 |
), |
| 201 |
'woocommerce-smallscreen' => array( |
| 202 |
'src' => plugins_url( 'assets/css/woocommerce-smallscreen.css', WC_PLUGIN_FILE ), |
| 203 |
'deps' => 'woocommerce-layout', |
| 204 |
'version' => WC_VERSION, |
| 205 |
'media' => 'only screen and (max-width: ' . apply_filters( 'woocommerce_style_smallscreen_breakpoint', '768px' ) . ')', |
| 206 |
'has_rtl' => true, |
| 207 |
), |
| 208 |
'woocommerce-general' => array( |
| 209 |
'src' => plugins_url( 'assets/css/woocommerce.css', WC_PLUGIN_FILE ), |
| 210 |
'deps' => '', |
| 211 |
'version' => WC_VERSION, |
| 212 |
'media' => 'all', |
| 213 |
'has_rtl' => true, |
| 214 |
), |
| 215 |
); |
| 216 |
|
| 217 |
return $woo_styles; |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* Init Actions. |
| 222 |
* |
| 223 |
* @since 1.0.0 |
| 224 |
*/ |
| 225 |
function init_actions() { |
| 226 |
|
| 227 |
$this->set_flow_session(); |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* Set flow session. |
| 232 |
* |
| 233 |
* @since 1.0.0 |
| 234 |
*/ |
| 235 |
function set_flow_session() { |
| 236 |
|
| 237 |
if ( wcf()->utils->is_step_post_type() ) { |
| 238 |
|
| 239 |
add_action( 'wp_head', array( $this, 'noindex_flow' ) ); |
| 240 |
|
| 241 |
wcf()->utils->do_not_cache(); |
| 242 |
|
| 243 |
/* Set key to support pixel */ |
| 244 |
if ( isset( $_GET['wcf-key'] ) ) { |
| 245 |
$_GET['key'] = $_GET['wcf-key']; |
| 246 |
$_REQUEST['key'] = $_GET['wcf-key']; |
| 247 |
} |
| 248 |
|
| 249 |
if ( isset( $_GET['wcf-order'] ) ) { |
| 250 |
$_GET['order'] = $_GET['wcf-order']; |
| 251 |
$_REQUEST['order'] = $_GET['wcf-order']; |
| 252 |
$_GET['order-received'] = $_GET['wcf-order']; |
| 253 |
$_REQUEST['order-received'] = $_GET['wcf-order']; |
| 254 |
} |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
/** |
| 259 |
* Add noindex, nofollow. |
| 260 |
* |
| 261 |
* @since 1.0.0 |
| 262 |
*/ |
| 263 |
function noindex_flow() { |
| 264 |
|
| 265 |
$common = Cartflows_Helper::get_common_settings(); |
| 266 |
|
| 267 |
if ( 'enable' === $common['disallow_indexing'] ) { |
| 268 |
echo '<meta name="robots" content="noindex,nofollow">'; |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
/** |
| 273 |
* WP Actions. |
| 274 |
* |
| 275 |
* @since 1.0.0 |
| 276 |
*/ |
| 277 |
function wp_actions() { |
| 278 |
|
| 279 |
if ( wcf()->utils->is_step_post_type() ) { |
| 280 |
|
| 281 |
/* CSS Compatibility for All theme */ |
| 282 |
add_filter( 'woocommerce_enqueue_styles', array( $this, 'woo_default_css' ), 9999 ); |
| 283 |
|
| 284 |
add_action( 'wp_enqueue_scripts', array( $this, 'remove_theme_styles' ), 9999 ); |
| 285 |
add_action( 'wp_enqueue_scripts', array( $this, 'global_flow_scripts' ), 20 ); |
| 286 |
|
| 287 |
/* Load woo templates from plugin */ |
| 288 |
add_filter( 'woocommerce_locate_template', array( $this, 'override_woo_template' ), 20, 3 ); |
| 289 |
|
| 290 |
/* Add version class to body in frontend. */ |
| 291 |
add_filter( 'body_class', array( $this, 'add_cartflows_lite_version_to_body' ) ); |
| 292 |
|
| 293 |
/* Custom Script Option */ |
| 294 |
add_action( 'wp_head', array( $this, 'custom_script_option' ) ); |
| 295 |
|
| 296 |
/* Remove the action applied by the Flatsome theme */ |
| 297 |
if ( Cartflows_Compatibility::get_instance()->is_flatsome_enabled() ) { |
| 298 |
$this->remove_flatsome_action(); |
| 299 |
} |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
/** |
| 304 |
* Global flow scripts. |
| 305 |
* |
| 306 |
* @since 1.0.0 |
| 307 |
*/ |
| 308 |
function global_flow_scripts() { |
| 309 |
|
| 310 |
global $post; |
| 311 |
|
| 312 |
$flow = get_post_meta( $post->ID, 'wcf-flow-id', true ); |
| 313 |
$current_step = $post->ID; |
| 314 |
$next_step_link = ''; |
| 315 |
$compatibility = Cartflows_Compatibility::get_instance(); |
| 316 |
|
| 317 |
if ( _is_wcf_landing_type() ) { |
| 318 |
|
| 319 |
$next_step_id = wcf()->utils->get_next_step_id( $flow, $current_step ); |
| 320 |
$next_step_link = get_permalink( $next_step_id ); |
| 321 |
} |
| 322 |
|
| 323 |
$localize = array( |
| 324 |
'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 325 |
'is_pb_preview' => $compatibility->is_page_builder_preview(), |
| 326 |
'current_theme' => $compatibility->get_current_theme(), |
| 327 |
'current_flow' => $flow, |
| 328 |
'current_step' => $current_step, |
| 329 |
'next_step' => $next_step_link, |
| 330 |
); |
| 331 |
|
| 332 |
wp_localize_script( 'jquery', 'cartflows', apply_filters( 'global_cartflows_js_localize', $localize ) ); |
| 333 |
|
| 334 |
wp_enqueue_style( 'wcf-frontend-global', CARTFLOWS_URL . 'assets/css/frontend.css', array(), CARTFLOWS_VER ); |
| 335 |
wp_style_add_data( 'wcf-frontend-global', 'rtl', 'replace' ); |
| 336 |
|
| 337 |
wp_enqueue_script( |
| 338 |
'wcf-frontend-global', |
| 339 |
CARTFLOWS_URL . 'assets/js/frontend.js', |
| 340 |
array( 'jquery' ), |
| 341 |
CARTFLOWS_VER, |
| 342 |
true |
| 343 |
); |
| 344 |
} |
| 345 |
|
| 346 |
/** |
| 347 |
* Custom Script in head. |
| 348 |
* |
| 349 |
* @since 1.0.0 |
| 350 |
*/ |
| 351 |
function custom_script_option() { |
| 352 |
|
| 353 |
/* Add custom script to header in frontend. */ |
| 354 |
$script = $this->get_custom_script(); |
| 355 |
if ( '' !== $script ) { |
| 356 |
if ( false === strpos( $script, '<script' ) ) { |
| 357 |
$script = '<script>' . $script . '</script>'; |
| 358 |
} |
| 359 |
echo '<!-- Custom CartFlows Script -->'; |
| 360 |
echo $script; |
| 361 |
echo '<!-- End Custom CartFlows Script -->'; |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
/** |
| 366 |
* Override woo templates. |
| 367 |
* |
| 368 |
* @param string $template new Template full path. |
| 369 |
* @param string $template_name Template name. |
| 370 |
* @param string $template_path Template Path. |
| 371 |
* @since 1.1.5 |
| 372 |
* @return string. |
| 373 |
*/ |
| 374 |
function override_woo_template( $template, $template_name, $template_path ) { |
| 375 |
|
| 376 |
global $woocommerce; |
| 377 |
|
| 378 |
$_template = $template; |
| 379 |
|
| 380 |
$plugin_path = CARTFLOWS_DIR . 'woocommerce/template/'; |
| 381 |
|
| 382 |
if ( file_exists( $plugin_path . $template_name ) ) { |
| 383 |
$template = $plugin_path . $template_name; |
| 384 |
} |
| 385 |
|
| 386 |
if ( ! $template ) { |
| 387 |
$template = $_template; |
| 388 |
} |
| 389 |
|
| 390 |
return $template; |
| 391 |
} |
| 392 |
|
| 393 |
/** |
| 394 |
* Remove the action applied by the Flatsome theme. |
| 395 |
* |
| 396 |
* @since 1.1.5 |
| 397 |
* @return void. |
| 398 |
*/ |
| 399 |
function remove_flatsome_action() { |
| 400 |
|
| 401 |
// Remove action where flatsome dequeued the woocommerce's default styles. |
| 402 |
remove_action( 'wp_enqueue_scripts', 'flatsome_woocommerce_scripts_styles', 98 ); |
| 403 |
} |
| 404 |
|
| 405 |
/** |
| 406 |
* Add version class to body in frontend. |
| 407 |
* |
| 408 |
* @since 1.1.5 |
| 409 |
* @param array $classes classes. |
| 410 |
* @return array $classes classes. |
| 411 |
*/ |
| 412 |
function add_cartflows_lite_version_to_body( $classes ) { |
| 413 |
|
| 414 |
$classes[] = 'cartflows-' . CARTFLOWS_VER; |
| 415 |
|
| 416 |
return $classes; |
| 417 |
|
| 418 |
} |
| 419 |
|
| 420 |
/** |
| 421 |
* Get custom script data. |
| 422 |
* |
| 423 |
* @since 1.0.0 |
| 424 |
*/ |
| 425 |
function get_custom_script() { |
| 426 |
|
| 427 |
global $post; |
| 428 |
|
| 429 |
$script = get_post_meta( $post->ID, 'wcf-custom-script', true ); |
| 430 |
|
| 431 |
return $script; |
| 432 |
} |
| 433 |
} |
| 434 |
|
| 435 |
/** |
| 436 |
* Prepare if class 'Cartflows_Frontend' exist. |
| 437 |
* Kicking this off by calling 'get_instance()' method |
| 438 |
*/ |
| 439 |
Cartflows_Frontend::get_instance(); |
| 440 |
|