| 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 |
add_filter( 'woocommerce_get_cancel_order_url_raw', array( $this, 'redirect_to_checkout_on_cancel' ), 10 ); |
| 45 |
|
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Redirect to thank page if upsell not exists |
| 50 |
* |
| 51 |
* @param string $order_recieve_url url. |
| 52 |
* @param object $order order object. |
| 53 |
* @since 1.0.0 |
| 54 |
*/ |
| 55 |
function redirect_to_thankyou_page( $order_recieve_url, $order ) { |
| 56 |
|
| 57 |
/* Only for thank you page */ |
| 58 |
wcf()->logger->log( 'Start-' . __CLASS__ . '::' . __FUNCTION__ ); |
| 59 |
wcf()->logger->log( 'Only for thank you page' ); |
| 60 |
|
| 61 |
if ( wcf()->flow->is_thankyou_page_exists( $order ) ) { |
| 62 |
|
| 63 |
if ( _is_wcf_doing_checkout_ajax() ) { |
| 64 |
|
| 65 |
$checkout_id = wcf()->utils->get_checkout_id_from_post_data(); |
| 66 |
|
| 67 |
if ( ! $checkout_id ) { |
| 68 |
$checkout_id = wcf()->utils->get_checkout_id_from_order( $order->get_id() ); |
| 69 |
} |
| 70 |
} else { |
| 71 |
$checkout_id = wcf()->utils->get_checkout_id_from_order( $order->get_id() ); |
| 72 |
} |
| 73 |
|
| 74 |
wcf()->logger->log( 'Checkout ID : ' . $checkout_id ); |
| 75 |
|
| 76 |
if ( $checkout_id ) { |
| 77 |
|
| 78 |
$thankyou_step_id = wcf()->flow->get_thankyou_page_id( $order ); |
| 79 |
|
| 80 |
if ( $thankyou_step_id ) { |
| 81 |
|
| 82 |
$order_recieve_url = get_permalink( $thankyou_step_id ); |
| 83 |
|
| 84 |
$order_recieve_url = add_query_arg( |
| 85 |
array( |
| 86 |
'wcf-order' => $order->get_id(), |
| 87 |
'wcf-key' => $order->get_order_key(), |
| 88 |
), |
| 89 |
$order_recieve_url |
| 90 |
); |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
wcf()->logger->log( 'End-' . __CLASS__ . '::' . __FUNCTION__ ); |
| 96 |
|
| 97 |
return $order_recieve_url; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Cancel and redirect to checkout |
| 102 |
* |
| 103 |
* @param string $return_url url. |
| 104 |
* @since 1.0.0 |
| 105 |
*/ |
| 106 |
function redirect_to_checkout_on_cancel( $return_url ) { |
| 107 |
|
| 108 |
if ( _is_wcf_doing_checkout_ajax() ) { |
| 109 |
|
| 110 |
$checkout_id = wcf()->utils->get_checkout_id_from_post_data(); |
| 111 |
|
| 112 |
if ( ! $checkout_id ) { |
| 113 |
$checkout_id = wcf()->utils->get_checkout_id_from_order( $order->get_id() ); |
| 114 |
} |
| 115 |
} else { |
| 116 |
$checkout_id = wcf()->utils->get_checkout_id_from_order( $order->get_id() ); |
| 117 |
} |
| 118 |
|
| 119 |
if ( $checkout_id ) { |
| 120 |
|
| 121 |
$return_url = add_query_arg( |
| 122 |
array( |
| 123 |
'cancel_order' => 'true', |
| 124 |
'_wpnonce' => wp_create_nonce( 'woocommerce-cancel_order' ), |
| 125 |
), |
| 126 |
get_permalink( $checkout_id ) |
| 127 |
); |
| 128 |
} |
| 129 |
|
| 130 |
return $return_url; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
/** |
| 135 |
* Remove theme styles. |
| 136 |
* |
| 137 |
* @since 1.0.0 |
| 138 |
*/ |
| 139 |
function remove_theme_styles() { |
| 140 |
|
| 141 |
// get all styles data. |
| 142 |
global $wp_styles; |
| 143 |
global $wp_scripts; |
| 144 |
|
| 145 |
$get_stylesheet = get_stylesheet(); |
| 146 |
$get_template = get_template(); |
| 147 |
|
| 148 |
if ( Cartflows_Compatibility::get_instance()->is_divi_enabled() ) { |
| 149 |
return; |
| 150 |
} |
| 151 |
|
| 152 |
$get_stylesheet = 'themes/' . get_stylesheet(); |
| 153 |
$get_template = 'themes/' . get_template(); |
| 154 |
|
| 155 |
// loop over all of the registered scripts.. |
| 156 |
foreach ( $wp_styles->registered as $handle => $data ) { |
| 157 |
|
| 158 |
if ( strpos( $data->src, $get_template ) !== false || strpos( $data->src, $get_stylesheet ) !== false ) { |
| 159 |
|
| 160 |
// remove it. |
| 161 |
wp_deregister_style( $handle ); |
| 162 |
wp_dequeue_style( $handle ); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
// loop over all of the registered scripts. |
| 167 |
foreach ( $wp_scripts->registered as $handle => $data ) { |
| 168 |
|
| 169 |
if ( strpos( $data->src, $get_template ) !== false || strpos( $data->src, $get_stylesheet ) !== false ) { |
| 170 |
|
| 171 |
// remove it. |
| 172 |
wp_deregister_script( $handle ); |
| 173 |
wp_dequeue_script( $handle ); |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Update main order data in transient. |
| 180 |
* |
| 181 |
* @param array $woo_styles new styles array. |
| 182 |
* @since 1.0.0 |
| 183 |
* @return array. |
| 184 |
*/ |
| 185 |
function woo_default_css( $woo_styles ) { |
| 186 |
|
| 187 |
$woo_styles = array( |
| 188 |
'woocommerce-layout' => array( |
| 189 |
'src' => plugins_url( 'assets/css/woocommerce-layout.css', WC_PLUGIN_FILE ), |
| 190 |
'deps' => '', |
| 191 |
'version' => WC_VERSION, |
| 192 |
'media' => 'all', |
| 193 |
'has_rtl' => true, |
| 194 |
), |
| 195 |
'woocommerce-smallscreen' => array( |
| 196 |
'src' => plugins_url( 'assets/css/woocommerce-smallscreen.css', WC_PLUGIN_FILE ), |
| 197 |
'deps' => 'woocommerce-layout', |
| 198 |
'version' => WC_VERSION, |
| 199 |
'media' => 'only screen and (max-width: ' . apply_filters( 'woocommerce_style_smallscreen_breakpoint', '768px' ) . ')', |
| 200 |
'has_rtl' => true, |
| 201 |
), |
| 202 |
'woocommerce-general' => array( |
| 203 |
'src' => plugins_url( 'assets/css/woocommerce.css', WC_PLUGIN_FILE ), |
| 204 |
'deps' => '', |
| 205 |
'version' => WC_VERSION, |
| 206 |
'media' => 'all', |
| 207 |
'has_rtl' => true, |
| 208 |
), |
| 209 |
); |
| 210 |
|
| 211 |
return $woo_styles; |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* Init Actions. |
| 216 |
* |
| 217 |
* @since 1.0.0 |
| 218 |
*/ |
| 219 |
function init_actions() { |
| 220 |
|
| 221 |
$this->set_flow_session(); |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* Set flow session. |
| 226 |
* |
| 227 |
* @since 1.0.0 |
| 228 |
*/ |
| 229 |
function set_flow_session() { |
| 230 |
|
| 231 |
if ( wcf()->utils->is_step_post_type() ) { |
| 232 |
|
| 233 |
add_action( 'wp_head', array( $this, 'noindex_flow' ) ); |
| 234 |
|
| 235 |
wcf()->utils->do_not_cache(); |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Add noindex, nofollow. |
| 241 |
* |
| 242 |
* @since 1.0.0 |
| 243 |
*/ |
| 244 |
function noindex_flow() { |
| 245 |
|
| 246 |
$common = Cartflows_Helper::get_common_settings(); |
| 247 |
|
| 248 |
if ( 'enable' === $common['disallow_indexing'] ) { |
| 249 |
echo '<meta name="robots" content="noindex,nofollow">'; |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
/** |
| 254 |
* WP Actions. |
| 255 |
* |
| 256 |
* @since 1.0.0 |
| 257 |
*/ |
| 258 |
function wp_actions() { |
| 259 |
|
| 260 |
if ( wcf()->utils->is_step_post_type() ) { |
| 261 |
|
| 262 |
/* CSS Compatibility for All theme */ |
| 263 |
add_filter( 'woocommerce_enqueue_styles', array( $this, 'woo_default_css' ), 9999 ); |
| 264 |
|
| 265 |
add_action( 'wp_enqueue_scripts', array( $this, 'remove_theme_styles' ), 9999 ); |
| 266 |
|
| 267 |
add_action( 'wp_enqueue_scripts', array( $this, 'global_flow_scripts' ), 20 ); |
| 268 |
} |
| 269 |
|
| 270 |
} |
| 271 |
|
| 272 |
/** |
| 273 |
* Global flow scripts. |
| 274 |
* |
| 275 |
* @since 1.0.0 |
| 276 |
*/ |
| 277 |
function global_flow_scripts() { |
| 278 |
|
| 279 |
global $post; |
| 280 |
|
| 281 |
$flow = get_post_meta( $post->ID, 'wcf-flow-id', true ); |
| 282 |
$current_step = $post->ID; |
| 283 |
$next_step_link = ''; |
| 284 |
$compatibility = Cartflows_Compatibility::get_instance(); |
| 285 |
|
| 286 |
if ( _is_wcf_landing_type() ) { |
| 287 |
|
| 288 |
$next_step_id = wcf()->utils->get_next_step_id( $flow, $current_step ); |
| 289 |
$next_step_link = get_permalink( $next_step_id ); |
| 290 |
} |
| 291 |
|
| 292 |
$localize = array( |
| 293 |
'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 294 |
'is_pb_preview' => $compatibility->is_page_builder_preview(), |
| 295 |
'current_flow' => $flow, |
| 296 |
'current_step' => $current_step, |
| 297 |
'next_step' => $next_step_link, |
| 298 |
); |
| 299 |
|
| 300 |
wp_localize_script( 'jquery', 'cartflows', apply_filters( 'global_cartflows_js_localize', $localize ) ); |
| 301 |
|
| 302 |
wp_enqueue_style( 'wcf-frontend-global', CARTFLOWS_URL . 'assets/css/frontend.css', array(), CARTFLOWS_VER ); |
| 303 |
wp_style_add_data( 'wcf-frontend-global', 'rtl', 'replace' ); |
| 304 |
|
| 305 |
wp_enqueue_script( |
| 306 |
'wcf-frontend-global', |
| 307 |
CARTFLOWS_URL . 'assets/js/frontend.js', |
| 308 |
array( 'jquery' ), |
| 309 |
CARTFLOWS_VER, |
| 310 |
true |
| 311 |
); |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
/** |
| 316 |
* Prepare if class 'Cartflows_Frontend' exist. |
| 317 |
* Kicking this off by calling 'get_instance()' method |
| 318 |
*/ |
| 319 |
Cartflows_Frontend::get_instance(); |
| 320 |
|