Builder
4 weeks ago
MyAccount
4 weeks ago
EditProfileTag.php
1 year ago
FormProcessor.php
1 year ago
FrontendProfileTag.php
1 year ago
Init.php
3 years ago
LoginFormTag.php
2 years ago
MelangeTag.php
1 year ago
MemberDirectoryTag.php
1 year ago
MembershipShortcodes.php
1 year ago
PasswordResetTag.php
4 weeks ago
RegistrationFormTag.php
1 year ago
index.php
5 years ago
MembershipShortcodes.php
203 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ProfilePress\Core\ShortcodeParser; |
| 4 | |
| 5 | use ProfilePress\Core\Membership\Controllers\CheckoutSessionData; |
| 6 | use ProfilePress\Core\Membership\Models\Coupon\CouponFactory; |
| 7 | use ProfilePress\Core\Membership\Models\Customer\CustomerFactory; |
| 8 | use ProfilePress\Core\Membership\Models\Group\GroupFactory; |
| 9 | use ProfilePress\Core\Membership\Models\Order\OrderFactory; |
| 10 | use ProfilePress\Core\Membership\Models\Order\OrderType; |
| 11 | use ProfilePress\Core\Membership\Models\Subscription\SubscriptionFactory; |
| 12 | use ProfilePress\Core\Membership\PaymentMethods\BankTransfer\BankTransfer; |
| 13 | |
| 14 | class MembershipShortcodes |
| 15 | { |
| 16 | public function __construct() |
| 17 | { |
| 18 | add_shortcode('profilepress-checkout', [$this, 'checkout_page_wrapper']); |
| 19 | add_shortcode('profilepress-receipt', [$this, 'success_page']); |
| 20 | |
| 21 | add_filter('the_content', [$this, 'filter_success_page_content'], 99999); |
| 22 | |
| 23 | // Avada theme incompatibility fix |
| 24 | add_action('awb_remove_third_party_the_content_changes', function () { |
| 25 | remove_filter('the_content', [$this, 'filter_success_page_content'], 99999); |
| 26 | }); |
| 27 | |
| 28 | add_action('awb_readd_third_party_the_content_changes', function () { |
| 29 | add_filter('the_content', [$this, 'filter_success_page_content'], 99999); |
| 30 | }); |
| 31 | } |
| 32 | |
| 33 | function filter_success_page_content($content) |
| 34 | { |
| 35 | if (isset($_GET['order_key'], $_GET['payment_method']) && ppress_is_success_page()) { |
| 36 | $order = OrderFactory::fromOrderKey(sanitize_key($_GET['order_key'])); |
| 37 | if ($order->exists() && $order->is_pending() && $_GET['payment_method'] != BankTransfer::get_instance()->get_id()) { |
| 38 | ob_start(); |
| 39 | ppress_render_view('order-processing', [ |
| 40 | 'order_success_page' => ppress_get_success_url($order->order_key) |
| 41 | ]); |
| 42 | |
| 43 | $content = ob_get_clean(); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | return $content; |
| 48 | } |
| 49 | |
| 50 | public function checkout_page_wrapper() |
| 51 | { |
| 52 | if (ppress_is_redirect_to_referrer_after_checkout()) { |
| 53 | |
| 54 | $referrer = wp_get_referer(); |
| 55 | |
| 56 | if ( ! empty($referrer)) { |
| 57 | ppress_session()->set('ppress_checkout_referrer', esc_url_raw($referrer)); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | ob_start(); |
| 62 | |
| 63 | echo '<div class="ppress-checkout__form">'; |
| 64 | $this->checkout_page(); |
| 65 | echo '</div>'; |
| 66 | |
| 67 | return ob_get_clean(); |
| 68 | } |
| 69 | |
| 70 | public function checkout_page() |
| 71 | { |
| 72 | if ( |
| 73 | ( ! isset($_GET['plan']) || ! is_numeric($_GET['plan'])) && |
| 74 | ( ! isset($_GET['group']) || ! is_numeric($_GET['group'])) |
| 75 | ) { |
| 76 | |
| 77 | do_action('ppress_membership_checkout_empty_cart'); |
| 78 | |
| 79 | echo '<p>'; |
| 80 | printf( |
| 81 | __('Your cart is currently empty. Click <a href="%s">here</a> to get started.', 'wp-user-avatar'), |
| 82 | /** @todo add setting to set url to redirect to when cart is empty */ |
| 83 | apply_filters('ppress_membership_checkout_empty_cart_url', home_url()) |
| 84 | ); |
| 85 | echo '</p>'; |
| 86 | |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | $isGroupCheckout = ! empty($_GET['group']); |
| 91 | |
| 92 | $isChangePlanCheckout = ! empty($_GET['change_plan']); |
| 93 | |
| 94 | if ($isChangePlanCheckout) { |
| 95 | |
| 96 | if ( ! is_user_logged_in()) { |
| 97 | echo '<p>'; |
| 98 | esc_html_e('You must be logged in to switch to another plan.', 'wp-user-avatar'); |
| 99 | echo '</p>'; |
| 100 | |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | $sub = SubscriptionFactory::fromId(absint($_GET['change_plan'])); |
| 105 | |
| 106 | if ( ! $sub->exists() || ! ppress_get_plan($sub->plan_id)->get_group_id()) { |
| 107 | |
| 108 | echo '<p>'; |
| 109 | esc_html_e('You can not switch to another plan because this plan does not belong to any group.', 'wp-user-avatar'); |
| 110 | echo '</p>'; |
| 111 | |
| 112 | return; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | if ($isGroupCheckout) { |
| 117 | |
| 118 | $group = GroupFactory::fromId(absint($_GET['group'])); |
| 119 | |
| 120 | if ( ! $group->exists()) { |
| 121 | esc_html_e('Invalid plan group.', 'wp-user-avatar'); |
| 122 | |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | if (empty($group->get_plan_ids())) { |
| 127 | esc_html_e('Error: group has no membership plans.', 'wp-user-avatar'); |
| 128 | |
| 129 | return; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | $planObj = ppress_get_plan(absint(ppressGET_var('plan', 0))); |
| 134 | $groupObj = GroupFactory::fromId(absint(ppressGET_var('group', 0))); |
| 135 | |
| 136 | if ( |
| 137 | is_user_logged_in() && |
| 138 | ! $isGroupCheckout && |
| 139 | ! $isChangePlanCheckout && |
| 140 | CustomerFactory::fromUserId(get_current_user_id())->has_active_subscription($planObj->id)) { |
| 141 | echo '<p>'; |
| 142 | printf( |
| 143 | esc_html__('You have an active subscription to this plan. Please go to %syour account%s to manage your subscription.', 'wp-user-avatar'), |
| 144 | '<a href="' . ppress_my_account_url() . '">', '</a>' |
| 145 | ); |
| 146 | echo '</p>'; |
| 147 | |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | if ( ! $planObj->is_active()) { |
| 152 | do_action('ppress_membership_checkout_invalid_plan'); |
| 153 | echo '<p>' . esc_html__('Invalid membership plan.', 'wp-user-avatar') . '</p>'; |
| 154 | |
| 155 | return; |
| 156 | } |
| 157 | |
| 158 | add_filter('ppress_logout_url_enable_redirect_get_query', '__return_true'); |
| 159 | |
| 160 | if ( ! empty($_GET['coupon'])) { |
| 161 | |
| 162 | $coupon = CouponFactory::fromCode(sanitize_text_field($_GET['coupon'])); |
| 163 | |
| 164 | $order_type = CheckoutSessionData::get_order_type($planObj->get_id()); |
| 165 | |
| 166 | if ( ! $order_type) $order_type = OrderType::NEW_ORDER; |
| 167 | |
| 168 | if ($coupon->exists() && $coupon->is_valid($planObj->get_id(), $order_type)) { |
| 169 | |
| 170 | ppress_session()->set(CheckoutSessionData::COUPON_CODE, [ |
| 171 | 'plan_id' => $planObj->id, |
| 172 | 'coupon_code' => $coupon->code, |
| 173 | ]); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | ppress_render_view('checkout/form-checkout', [ |
| 178 | 'planObj' => $planObj, |
| 179 | 'groupObj' => $groupObj, |
| 180 | 'changePlanSubId' => absint(ppressGET_var('change_plan', 0)), |
| 181 | ]); |
| 182 | } |
| 183 | |
| 184 | public function success_page() |
| 185 | { |
| 186 | ob_start(); |
| 187 | require apply_filters('ppress_order_receipt_template', dirname(__FILE__) . '/MyAccount/view-order.tmpl.php'); |
| 188 | |
| 189 | return ob_get_clean(); |
| 190 | } |
| 191 | |
| 192 | public static function get_instance() |
| 193 | { |
| 194 | static $instance = null; |
| 195 | |
| 196 | if (is_null($instance)) { |
| 197 | $instance = new self(); |
| 198 | } |
| 199 | |
| 200 | return $instance; |
| 201 | } |
| 202 | } |
| 203 |