PluginProbe
Subscriptions for WooCommerce with Stripe Recurring Payments / 1.10.2
Subscriptions for WooCommerce with Stripe Recurring Payments v1.10.2
2.0.0 1.11.2 1.11.1 1.11.0 1.10.9 1.10.8 1.10.7 1.10.6 1.10.5 1.10.4 1.10.3 1.10.2 1.10.1 1.10.0 1.9.6 1.9.5 trunk 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 All 61 releases
subscription / includes / Frontend / MyAccount.php

MyAccount.php in Subscriptions for WooCommerce with Stripe Recurring Payments 1.10.2, at includes/Frontend/MyAccount.php

333 lines 11.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace SpringDevs\Subscription\Frontend;
4
5 use SpringDevs\Subscription\Illuminate\Helper;
6 use SpringDevs\Subscription\Illuminate\Subscription\Subscription;
7
8 // HPOS: This file is compatible with WooCommerce High-Performance Order Storage (HPOS).
9 // All WooCommerce order data is accessed via WooCommerce CRUD methods (wc_get_order, wc_get_order_item_meta, etc.).
10 // All direct post meta access is for subscription data only, not WooCommerce order data.
11 // If you add new order data access, use WooCommerce CRUD for HPOS compatibility.
12
13 /**
14 * Class MyAccount
15 *
16 * @package SpringDevs\Subscription\Frontend
17 */
18 class MyAccount {
19 /**
20 * Subscriptions endpoint slug.
21 *
22 * @var string
23 */
24 private $subscriptions_endpoint = 'subscriptions';
25
26 /**
27 * View Subscriptions endpoint slug.
28 *
29 * @var string
30 */
31 private $view_subscriptions_endpoint = 'view-subscription';
32
33
34 /**
35 * Initialize the class
36 */
37 public function __construct() {
38 // Assign endpoint slug from settings.
39 $this->subscriptions_endpoint = Subscription::get_user_endpoint( 'subs_list' );
40 $this->view_subscriptions_endpoint = Subscription::get_user_endpoint( 'view_subs' );
41
42 // Flush rewrite rules on init.
43 add_action( 'init', array( $this, 'flush_rewrite_rules' ) );
44
45 // Add My Subscriptions menu item.
46 add_filter( 'woocommerce_account_menu_items', array( $this, 'custom_my_account_menu_items' ), 200 );
47
48 // Add subscription url endpoints to query vars.
49 add_filter( 'woocommerce_get_query_vars', array( $this, 'custom_query_vars' ) );
50
51 // Subscription EndPoint Content.
52 add_action( "woocommerce_account_{$this->subscriptions_endpoint}_endpoint", array( $this, 'subscrpt_endpoint_content' ) );
53 add_action( "woocommerce_account_{$this->view_subscriptions_endpoint}_endpoint", array( $this, 'view_subscrpt_content' ) );
54
55 // Subscription page titles
56 add_filter( "woocommerce_endpoint_{$this->subscriptions_endpoint}_title", array( $this, 'change_subscriptions_title' ) );
57 add_filter( "woocommerce_endpoint_{$this->view_subscriptions_endpoint}_title", array( $this, 'change_single_subscription_title' ) );
58
59 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_styles' ) );
60 }
61
62 /**
63 * Add custom url on MyAccount.
64 *
65 * @param array $query_vars query_vars.
66 *
67 * @return array
68 */
69 public function custom_query_vars( array $query_vars ): array {
70 $query_vars[ $this->subscriptions_endpoint ] = $this->subscriptions_endpoint;
71 $query_vars[ $this->view_subscriptions_endpoint ] = $this->view_subscriptions_endpoint;
72
73 return $query_vars;
74 }
75
76 /**
77 * Display Subscription Content.
78 *
79 * @param Int $id Post ID.
80 */
81 public function view_subscrpt_content( int $id ) {
82 $subs_post = get_post( $id );
83 $author_id = $subs_post ? (int) $subs_post->post_author : 0;
84 $current_user_id = get_current_user_id();
85 $user_is_admin = current_user_can( 'manage_options' );
86
87 if ( ! $user_is_admin && (int) $author_id !== (int) $current_user_id ) {
88 return wp_safe_redirect( '/404' );
89 }
90
91 $subscription_id = $id;
92 $subscription_data = Helper::get_subscription_data( $subscription_id );
93 $related_orders = Helper::get_related_orders( $subscription_id );
94
95 $status = $subscription_data['status'] ?? '';
96 $verbose_status = Helper::get_verbose_status( $status );
97
98 $order_id = $subscription_data['order']['order_id'] ?? 0;
99 $order_item_id = $subscription_data['order']['order_item_id'] ?? 0;
100
101 $order = wc_get_order( $order_id );
102 $order_item = $order ? $order->get_item( $order_item_id ) : null;
103
104 if ( ! $order || ! $order_item ) {
105 return wp_safe_redirect( '/404' );
106 }
107
108 $user_cancel = $subscription_data['can_user_cancel'] ?? false;
109
110 $start_date = $subscription_data['start_date'] ?? '';
111 $start_date = ! empty( $start_date ) ? wp_date( 'F j, Y', strtotime( $start_date ) ) : '-';
112
113 $next_date = $subscription_data['next_date'] ?? '';
114 $next_date = ! empty( $next_date ) ? wp_date( 'F j, Y', strtotime( $next_date ) ) : '-';
115
116 $trial = get_post_meta( $id, '_subscrpt_trial', true );
117 $trial_mode = get_post_meta( $id, '_subscrpt_trial_mode', true );
118
119 $quantity = (int) $order_item->get_quantity();
120 $price = (float) ( $subscription_data['price'] ?? 0 ) * max( 1, $quantity );
121 $price_excl_tax = (float) $order_item->get_total();
122 $tax_amount = (float) $order_item->get_total_tax();
123
124 if ( $tax_amount > 0 ) {
125 $price = $price_excl_tax + $tax_amount;
126 $price = number_format( (float) $price, 2, '.', '' );
127 } else {
128 $tax_amount = 0;
129 }
130
131 $is_grace_period = isset( $subscription_data['grace_period'] );
132 $grace_remaining = $subscription_data['grace_period']['remaining_days'] ?? 0;
133 $grace_end_date = $subscription_data['grace_period']['end_date'] ?? '';
134 $grace_end_date = ! empty( $grace_end_date ) ? wp_date( 'F j, Y', strtotime( $grace_end_date ) ) : '';
135
136 $subscrpt_nonce = wp_create_nonce( 'subscrpt_nonce' );
137 $action_buttons = array();
138
139 if ( 'cancelled' !== $status ) {
140 if ( in_array( $status, array( 'pending', 'active', 'on_hold' ), true ) && $user_cancel ) {
141 $label = __( 'Cancel', 'subscription' );
142 $label = apply_filters( 'subscrpt_split_payment_button_text', $label, 'cancel', $id, $status );
143
144 $action_buttons['cancel'] = array(
145 'url' => subscrpt_get_action_url( 'cancelled', $subscrpt_nonce, $id ),
146 'label' => $label,
147 'class' => 'cancel',
148 );
149 } elseif ( trim( $status ) === trim( 'pe_cancelled' ) ) {
150 $label = __( 'Reactivate', 'subscription' );
151 $label = apply_filters( 'subscrpt_split_payment_button_text', $label, 'reactivate', $id, $status );
152
153 $action_buttons['reactivate'] = array(
154 'url' => subscrpt_get_action_url( 'reactivate', $subscrpt_nonce, $id ),
155 'label' => $label,
156 );
157 } elseif ( 'expired' === $status && 'pending' !== $order->get_status() ) {
158 // Check if maximum payments reached before showing renew button
159 if ( ! subscrpt_is_max_payments_reached( $id ) ) {
160 $label = __( 'Renew', 'subscription' );
161 $label = apply_filters( 'subscrpt_split_payment_button_text', $label, 'renew', $id, $status );
162
163 $action_buttons['renew'] = array(
164 'url' => subscrpt_get_action_url( 'renew', $subscrpt_nonce, $id ),
165 'label' => $label,
166 );
167 }
168 }
169
170 if ( 'pending' === $order->get_status() ) {
171 $label = __( 'Pay now', 'subscription' );
172 $label = apply_filters( 'subscrpt_split_payment_button_text', $label, 'pay_now', $id, $status );
173
174 $action_buttons['pay_now'] = array(
175 'url' => $order->get_checkout_payment_url(),
176 'label' => $label,
177 );
178 }
179 }
180
181 $is_auto_renew = $subscription_data['is_auto_renew'];
182 $renewal_setting = in_array( get_option( 'wp_subscription_auto_renewal_toggle', '1' ), [ 1, '1', 'true', 'yes' ], true );
183
184 $saved_methods = wc_get_customer_saved_methods_list( get_current_user_id() );
185 $has_methods = isset( $saved_methods['cc'] );
186 if ( $has_methods && $renewal_setting && class_exists( 'WC_Stripe' ) && $order && 'stripe' === $order->get_payment_method() ) {
187 // Check maximum payment limit for auto-renewal buttons too
188 if ( ! subscrpt_is_max_payments_reached( $id ) ) {
189 if ( ! $is_auto_renew ) {
190 $label = __( 'Turn on Auto Renewal', 'subscription' );
191 $label = apply_filters( 'subscrpt_split_payment_button_text', $label, 'auto-renew-on', $id, $status );
192
193 $action_buttons['auto-renew-on'] = array(
194 'url' => subscrpt_get_action_url( 'renew-on', $subscrpt_nonce, $id ),
195 'label' => $label,
196 );
197 } else {
198 $label = __( 'Turn off Auto Renewal', 'subscription' );
199 $label = apply_filters( 'subscrpt_split_payment_button_text', $label, 'auto-renew-off', $id, $status );
200
201 $action_buttons['auto-renew-off'] = array(
202 'url' => subscrpt_get_action_url( 'renew-off', $subscrpt_nonce, $id ),
203 'label' => $label,
204 );
205 }
206 }
207 }
208
209 // Allow programmatically disabling cancel button
210 $disable_cancel = apply_filters( 'subscrpt_split_payment_disable_cancel', false, $id, $status );
211 if ( $disable_cancel && isset( $action_buttons['cancel'] ) ) {
212 unset( $action_buttons['cancel'] );
213 }
214
215 $action_buttons = apply_filters( 'subscrpt_single_action_buttons', $action_buttons, $id, $subscrpt_nonce, $status );
216
217 wc_get_template(
218 'myaccount/single.php',
219 array(
220 'id' => $id,
221 'status' => $status,
222 'verbose_status' => $verbose_status,
223 'start_date' => $start_date,
224 'next_date' => $next_date,
225 'is_grace_period' => $is_grace_period,
226 'grace_remaining' => $grace_remaining,
227 'grace_end_date' => $grace_end_date,
228 'trial' => $trial,
229 'trial_mode' => empty( $trial_mode ) ? 'off' : $trial_mode,
230 'order' => $order,
231 'order_item' => $order_item,
232 'related_orders' => $related_orders,
233 'price' => $price,
234 'price_excl_tax' => $price_excl_tax,
235 'tax' => $tax_amount,
236 'user_cancel' => $user_cancel,
237 'action_buttons' => $action_buttons,
238 'wp_button_class' => wc_wp_theme_get_element_class_name( 'button' ) ? ' ' . wc_wp_theme_get_element_class_name( 'button' ) : '',
239 ),
240 'subscription',
241 SUBSCRPT_TEMPLATES
242 );
243 }
244
245 /**
246 * Re-write flush
247 */
248 public function flush_rewrite_rules() {
249 add_rewrite_endpoint( $this->subscriptions_endpoint, EP_ROOT | EP_PAGES );
250 flush_rewrite_rules();
251 }
252
253 /**
254 * Change All Subscriptions Title
255 *
256 * @param string $title Title.
257 *
258 * @return string
259 */
260 public function change_subscriptions_title( string $title ): string {
261 $title = __( 'My Subscriptions', 'subscription' );
262 return $title;
263 }
264
265 /**
266 * Change View Subscription Title
267 *
268 * @param string $title Title.
269 *
270 * @return string
271 */
272 public function change_single_subscription_title( string $title ): string {
273 $subs_id = get_query_var( $this->view_subscriptions_endpoint, 0 );
274
275 /* translators: %s: Subscription ID */
276 $title = sprintf( __( 'Subscription #%s', 'subscription' ), $subs_id );
277 return $title;
278 }
279
280 /**
281 * Filter menu items.
282 *
283 * @param array $items MyAccount menu items.
284 * @return array
285 */
286 public function custom_my_account_menu_items( array $items ): array {
287 // Check if subscriptions menu item already exists to prevent duplicates
288 if ( ! isset( $items[ $this->subscriptions_endpoint ] ) ) {
289 $logout = $items['customer-logout'];
290 unset( $items['customer-logout'] );
291
292 $items[ $this->subscriptions_endpoint ] = __( 'Subscriptions', 'subscription' );
293 $items['customer-logout'] = $logout;
294 }
295 return $items;
296 }
297
298 /**
299 * Subscription Single EndPoint Content.
300 *
301 * @param int $current_page Current Page.
302 */
303 public function subscrpt_endpoint_content( $current_page ) {
304 $current_page = empty( $current_page ) ? 1 : absint( $current_page );
305 $args = array(
306 'author' => get_current_user_id(),
307 'posts_per_page' => 10,
308 'paged' => $current_page,
309 'post_type' => 'subscrpt_order',
310 'post_status' => array( 'pending', 'active', 'on_hold', 'cancelled', 'expired', 'pe_cancelled' ),
311 );
312
313 $postslist = new \WP_Query( $args );
314 wc_get_template(
315 'myaccount/subscriptions.php',
316 array(
317 'wp_button_class' => wc_wp_theme_get_element_class_name( 'button' ) ? ' ' . wc_wp_theme_get_element_class_name( 'button' ) : '',
318 'postslist' => $postslist,
319 'current_page' => $current_page,
320 ),
321 'subscription',
322 SUBSCRPT_TEMPLATES
323 );
324 }
325
326 /**
327 * Enqueue assets
328 */
329 public function enqueue_styles() {
330 wp_enqueue_style( 'subscrpt_status_css' );
331 }
332 }
333