| 1 |
<?php |
| 2 |
/** |
| 3 |
* Tutor Compatibility |
| 4 |
* |
| 5 |
* @package WPFunnels\Compatibility\Plugin |
| 6 |
*/ |
| 7 |
namespace WPFunnels\Compatibility\Plugin; |
| 8 |
|
| 9 |
use WPFunnels\Wpfnl_functions; |
| 10 |
use WPFunnels\Traits\SingletonTrait; |
| 11 |
|
| 12 |
/** |
| 13 |
* Tutor Compatibility |
| 14 |
* |
| 15 |
* @package WPFunnels\Compatibility\Tutor |
| 16 |
*/ |
| 17 |
class Tutor extends PluginCompatibility{ |
| 18 |
use SingletonTrait; |
| 19 |
|
| 20 |
/** |
| 21 |
* Filters/Hook from Tutor |
| 22 |
* to initiate the necessary updates. |
| 23 |
* |
| 24 |
* @since 3.4.10 |
| 25 |
*/ |
| 26 |
public function init() { |
| 27 |
|
| 28 |
add_action( 'wp', array( $this, 'remove_tutor_redirection_for_wpfnl_step'), 9999 ); |
| 29 |
add_action( 'wpfunnels/funnel_order_placed', array( $this, 'funnel_order_placed' ), 10, 3 ); |
| 30 |
add_action( 'wpfunnels/offer_accepted', array( $this, 'enroll_to_tutor_course' ), 10, 2 ); |
| 31 |
add_filter( 'tutor_woocommerce_redirect_url', array( $this, 'tutor_maybe_allow_to_redirect' ), 99 ); |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
/** |
| 36 |
* Remove tutor redirection for WPFunnels steps |
| 37 |
* |
| 38 |
* @since 3.4.10 |
| 39 |
* @return void |
| 40 |
*/ |
| 41 |
public function remove_tutor_redirection_for_wpfnl_step() { |
| 42 |
|
| 43 |
if( Wpfnl_functions::is_funnel_step_page() && $this->maybe_activate() ){ |
| 44 |
if ( class_exists( '\TUTOR\WooCommerce' ) ) { |
| 45 |
$instance = new \TUTOR\WooCommerce(); |
| 46 |
remove_action( 'woocommerce_thankyou', array( $instance, 'redirect_to_enrolled_courses' ) ); |
| 47 |
} |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
/** |
| 53 |
* Save order meta data for tutor course |
| 54 |
* |
| 55 |
* @param int $order_id |
| 56 |
* @param int $funnel_id |
| 57 |
* @param int $checkout_id |
| 58 |
* |
| 59 |
* @since 3.4.10 |
| 60 |
*/ |
| 61 |
public function funnel_order_placed( $order_id, $funnel_id, $checkout_id ){ |
| 62 |
if( $this->maybe_activate() && $funnel_id && $checkout_id && $order_id ){ |
| 63 |
$order = \wc_get_order( $order_id ); |
| 64 |
$order->update_meta_data( '_is_tutor_order_for_course', tutor_time() ); |
| 65 |
$order->save(); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
/** |
| 71 |
* Enroll user to tutor course |
| 72 |
* |
| 73 |
* @param WC_Order $order |
| 74 |
* @param array $offer_product |
| 75 |
* |
| 76 |
* @since 3.4.10 |
| 77 |
*/ |
| 78 |
public function enroll_to_tutor_course( $order, $offer_product ){ |
| 79 |
if( $this->maybe_activate() && function_exists( 'tutor_utils' ) ){ |
| 80 |
if ( is_array($offer_product) && !empty($offer_product)) { |
| 81 |
$customer_id = $order->get_customer_id(); |
| 82 |
do_action( 'woocommerce_new_order', $order->get_id(), $order ); |
| 83 |
$product_id = $offer_product['id']; |
| 84 |
$order_id = $order->get_id(); |
| 85 |
$order->update_meta_data( '_is_tutor_order_for_course', tutor_time() ); |
| 86 |
|
| 87 |
$if_has_course = tutor_utils()->product_belongs_with_course( $product_id ); |
| 88 |
if ( $if_has_course ) { |
| 89 |
$order->update_status('completed'); |
| 90 |
$order->save(); |
| 91 |
|
| 92 |
$course_id = $if_has_course->post_id; |
| 93 |
$is_enrolled = tutor_utils()->is_enrolled( $course_id, $customer_id ); |
| 94 |
if( !$is_enrolled ){ |
| 95 |
tutor_utils()->do_enroll( $course_id, $order_id, $customer_id ); |
| 96 |
foreach ( $order->get_items() as $item_id => $item ) { |
| 97 |
do_action( 'woocommerce_new_order_item', $item_id, $item, $order_id ); |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
/** |
| 107 |
* Determines whether to allow redirection for the tutor. |
| 108 |
* |
| 109 |
* @param bool $is_allow Whether redirection is allowed. |
| 110 |
* |
| 111 |
* @return bool |
| 112 |
* @since 3.4.10 |
| 113 |
*/ |
| 114 |
public function tutor_maybe_allow_to_redirect( $is_allow ){ |
| 115 |
if (Wpfnl_functions::is_funnel_step_page()) { |
| 116 |
return false; |
| 117 |
} |
| 118 |
return $is_allow; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
/** |
| 123 |
* Check if Tutor is activated |
| 124 |
* |
| 125 |
* @return bool |
| 126 |
* @since 3.4.10 |
| 127 |
*/ |
| 128 |
public function maybe_activate() |
| 129 |
{ |
| 130 |
return defined('TUTOR_VERSION'); |
| 131 |
} |
| 132 |
} |
| 133 |
|