Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/tutor-pro/addons/subscription/src/HookHandler.php

Keine Baseline-Datei – Diff nur gegen leer.
Zur Liste
1 -
1 + <?php
2 + /**
3 + * Hook Handler for Subscriptions.
4 + *
5 + * @package TutorPro\Subscription
6 + * @author Themeum <support@themeum.com>
7 + * @link https://themeum.com
8 + * @since 3.5.0
9 + */
10 +
11 + namespace TutorPro\Subscription;
12 +
13 + use TUTOR\Course;
14 + use Tutor\Models\CouponModel;
15 + use TutorPro\Subscription\Models\PlanModel;
16 + use TutorPro\Subscription\Models\SubscriptionModel;
17 +
18 + /**
19 + * HookHandler Class.
20 + *
21 + * @since 3.5.0
22 + */
23 + class HookHandler {
24 + /**
25 + * Register hooks and dependency
26 + *
27 + * @since 3.5.0
28 + *
29 + * @param bool $register_hooks register hooks if true.
30 + */
31 + public function __construct( $register_hooks = true ) {
32 + if ( ! $register_hooks ) {
33 + return;
34 + }
35 +
36 + add_filter( 'tutor_coupon_details_applies_to_items_response', array( $this, 'filter_applies_to_items_response' ), 10, 2 );
37 + add_filter( 'tutor_coupon_applies_to', array( $this, 'filter_coupon_applies_to' ) );
38 + add_filter( 'tutor_enrollment_buttons', array( $this, 'filter_enrollment_buttons' ), 10, 3 );
39 +
40 + }
41 +
42 + /**
43 + * Filter applies to items response for specific membership plans.
44 + *
45 + * @since 3.5.0
46 + *
47 + * @param array $response response.
48 + * @param object $coupon coupon object.
49 + *
50 + * @return array
51 + */
52 + public function filter_applies_to_items_response( $response, $coupon ) {
53 + if ( ! in_array( $coupon->applies_to, array( CouponModel::APPLIES_TO_SPECIFIC_MEMBERSHIP_PLANS ), true ) ) {
54 + return $response;
55 + }
56 +
57 + $coupon_model = new CouponModel();
58 + $plan_model = new PlanModel();
59 + $application_ids = $coupon_model->get_coupon_applications( $coupon->coupon_code );
60 +
61 + $response = $plan_model->get_all( array( 'id' => $application_ids ) );
62 +
63 + return $response;
64 + }
65 +
66 + /**
67 + * Filter coupon applies to.
68 + *
69 + * @since 3.5.0
70 + *
71 + * @param array $list list of applies to items.
72 + *
73 + * @return array
74 + */
75 + public function filter_coupon_applies_to( $list ) {
76 + $list[ CouponModel::APPLIES_TO_ALL_MEMBERSHIP_PLANS ] = __( 'All Membership Plans', 'tutor-pro' );
77 + $list[ CouponModel::APPLIES_TO_SPECIFIC_MEMBERSHIP_PLANS ] = __( 'Specific Membership Plans', 'tutor-pro' );
78 +
79 + return $list;
80 + }
81 +
82 + /**
83 + * Filter enrollment buttons to add subscription and membership buttons.
84 + *
85 + * @since 3.9.3
86 + *
87 + * @param object $buttons buttons.
88 + * @param int $course_id course id.
89 + * @param int $user_id user id.
90 + *
91 + * @return object
92 + */
93 + public function filter_enrollment_buttons( $buttons, $course_id, $user_id ) {
94 + $buttons->show_subscribe_now_btn = false;
95 + $buttons->show_membership_btn = false;
96 +
97 + $subscription_model = new SubscriptionModel();
98 + $plan_model = new PlanModel();
99 + $selling_option = Course::get_selling_option( $course_id );
100 + $has_course_access = $subscription_model->has_course_access( $course_id, $user_id );
101 + $is_paid_course = Course::PRICE_TYPE_PAID === tutor_utils()->price_type( $course_id );
102 +
103 + if ( $has_course_access ) {
104 + $buttons->show_enroll_btn = tutor_utils()->is_enrolled( $course_id, $user_id ) ? false : true;
105 + return $buttons;
106 + }
107 +
108 + // Membership button.
109 + $membership_only_mode_enabled = Settings::membership_only_mode_enabled();
110 + if ( $membership_only_mode_enabled || in_array( $selling_option, array( Course::SELLING_OPTION_MEMBERSHIP, Course::SELLING_OPTION_ALL ), true ) ) {
111 + if ( $membership_only_mode_enabled ) {
112 + $buttons->show_add_to_cart_btn = false;
113 + $buttons->show_enroll_btn = $has_course_access ? true : false;
114 + $buttons->show_membership_btn = $has_course_access ? false : true;
115 + return $buttons;
116 + }
117 +
118 + // Hybrid mode.
119 + if ( $is_paid_course ) {
120 + $buttons->show_membership_btn = $has_course_access ? false : true;
121 + $buttons->show_enroll_btn = $has_course_access ? true : false;
122 + }
123 + }
124 +
125 + // Subscription button.
126 + if ( in_array( $selling_option, array( Course::SELLING_OPTION_SUBSCRIPTION, Course::SELLING_OPTION_BOTH, Course::SELLING_OPTION_ALL ), true ) ) {
127 + if ( $is_paid_course && ! $has_course_access ) {
128 + $active_plans = $plan_model->get_subscription_plans( $course_id, PlanModel::STATUS_ACTIVE );
129 + $buttons->show_subscribe_now_btn = count( $active_plans ) ? true : false;
130 + }
131 + }
132 +
133 + return $buttons;
134 + }
135 +
136 + }
137 +