PluginProbe
Subscriptions for WooCommerce with Stripe Recurring Payments / 1.4.2
Subscriptions for WooCommerce with Stripe Recurring Payments v1.4.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.4.2, at includes/Frontend/MyAccount.php

223 lines 7.4 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 // HPOS: This file is compatible with WooCommerce High-Performance Order Storage (HPOS).
6 // All WooCommerce order data is accessed via WooCommerce CRUD methods (wc_get_order, wc_get_order_item_meta, etc.).
7 // All direct post meta access is for subscription data only, not WooCommerce order data.
8 // If you add new order data access, use WooCommerce CRUD for HPOS compatibility.
9
10 /**
11 * Class MyAccount
12 *
13 * @package SpringDevs\Subscription\Frontend
14 */
15 class MyAccount {
16
17
18 /**
19 * Initialize the class
20 */
21 public function __construct() {
22 add_action( 'init', array( $this, 'flush_rewrite_rules' ) );
23 add_filter( 'woocommerce_account_menu_items', array( $this, 'custom_my_account_menu_items' ) );
24 add_filter( 'woocommerce_endpoint_view-subscription_title', array( $this, 'change_single_title' ) );
25 add_filter( 'the_title', array( $this, 'change_lists_title' ), 10 );
26 add_filter( 'woocommerce_get_query_vars', array( $this, 'custom_query_vars' ) );
27 add_action( 'woocommerce_account_view-subscription_endpoint', array( $this, 'view_subscrpt_content' ) );
28 add_action( 'woocommerce_account_subscriptions_endpoint', array( $this, 'subscrpt_endpoint_content' ) );
29 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_styles' ) );
30 }
31
32 /**
33 * Add custom url on MyAccount.
34 *
35 * @param array $query_vars query_vars.
36 *
37 * @return array
38 */
39 public function custom_query_vars( array $query_vars ): array {
40 $query_vars['view-subscription'] = 'view-subscription';
41 return $query_vars;
42 }
43
44 /**
45 * Display Subscription Content.
46 *
47 * @param Int $id Post ID.
48 */
49 public function view_subscrpt_content( int $id ) {
50 $order_id = get_post_meta( $id, '_subscrpt_order_id', true );
51 $order_item_id = get_post_meta( $id, '_subscrpt_order_item_id', true );
52 if ( ! $order_id || ! $order_item_id ) {
53 return wp_safe_redirect( '/404' );
54 }
55 $order = wc_get_order( $order_id );
56 $order_item = $order->get_item( $order_item_id );
57 $status = get_post_status( $id );
58 $user_cancel = get_post_meta( $id, '_subscrpt_user_cancel', true );
59 $start_date = get_post_meta( $id, '_subscrpt_start_date', true );
60 $next_date = get_post_meta( $id, '_subscrpt_next_date', true );
61 $trial = get_post_meta( $id, '_subscrpt_trial', true );
62 $trial_mode = get_post_meta( $id, '_subscrpt_trial_mode', true );
63
64 $subscrpt_nonce = wp_create_nonce( 'subscrpt_nonce' );
65 $action_buttons = array();
66
67 if ( 'cancelled' !== $status ) {
68 if ( in_array( $status, array( 'pending', 'active', 'on_hold' ), true ) && 'yes' === $user_cancel ) {
69 $action_buttons['cancel'] = array(
70 'url' => subscrpt_get_action_url( 'cancelled', $subscrpt_nonce, $id ),
71 'label' => __( 'Cancel', 'sdevs_subscrpt' ),
72 'class' => 'cancel',
73 );
74 } elseif ( trim( $status ) === trim( 'pe_cancelled' ) ) {
75 $action_buttons['reactive'] = array(
76 'url' => subscrpt_get_action_url( 'reactive', $subscrpt_nonce, $id ),
77 'label' => __( 'Reactive', 'sdevs_subscrpt' ),
78 );
79 } elseif ( 'expired' === $status && 'pending' !== $order->get_status() ) {
80 $action_buttons['renew'] = array(
81 'url' => subscrpt_get_action_url( 'renew', $subscrpt_nonce, $id ),
82 'label' => __( 'Renew', 'sdevs_subscrpt' ),
83 );
84 }
85
86 if ( 'pending' === $order->get_status() ) {
87 $action_buttons['pay_now'] = array(
88 'url' => $order->get_checkout_payment_url(),
89 'label' => __( 'Pay now', 'sdevs_subscrpt' ),
90 );
91 }
92 }
93
94 $is_auto_renew = get_post_meta( $id, '_subscrpt_auto_renew', true );
95 $renewal_setting = get_option( 'subscrpt_auto_renewal_toggle', '1' );
96 if ( '' === $is_auto_renew && '1' === $renewal_setting ) {
97 update_post_meta( $id, '_subscrpt_auto_renew', 1 );
98 }
99 $saved_methods = wc_get_customer_saved_methods_list( get_current_user_id() );
100 $has_methods = isset( $saved_methods['cc'] );
101 if ( $has_methods && '1' === $renewal_setting && class_exists( 'WC_Stripe' ) && $order && 'stripe' === $order->get_payment_method() ) {
102 if ( '0' === $is_auto_renew ) {
103 $action_buttons['auto-renew-on'] = array(
104 'url' => subscrpt_get_action_url( 'renew-on', $subscrpt_nonce, $id ),
105 'label' => __( 'Turn on Auto Renewal', 'sdevs_subscrpt' ),
106 );
107 } else {
108 $action_buttons['auto-renew-off'] = array(
109 'url' => subscrpt_get_action_url( 'renew-off', $subscrpt_nonce, $id ),
110 'label' => __( 'Turn off Auto Renewal', 'sdevs_subscrpt' ),
111 );
112 }
113 }
114
115 $post_status_object = get_post_status_object( $status );
116 $action_buttons = apply_filters( 'subscrpt_single_action_buttons', $action_buttons, $id, $subscrpt_nonce, $status );
117
118 wc_get_template(
119 'myaccount/single.php',
120 array(
121 'id' => $id,
122 'start_date' => $start_date,
123 'next_date' => $next_date,
124 'trial' => $trial,
125 'trial_mode' => empty( $trial_mode ) ? 'off' : $trial_mode,
126 'order' => $order,
127 'order_item' => $order_item,
128 'status' => $post_status_object,
129 'user_cancel' => $user_cancel,
130 'action_buttons' => $action_buttons,
131 'wp_button_class' => wc_wp_theme_get_element_class_name( 'button' ) ? ' ' . wc_wp_theme_get_element_class_name( 'button' ) : '',
132 ),
133 'subscription',
134 WP_SUBSCRIPTION_TEMPLATES
135 );
136 }
137
138 /**
139 * Re-write flush
140 */
141 public function flush_rewrite_rules() {
142 add_rewrite_endpoint( 'subscriptions', EP_ROOT | EP_PAGES );
143 flush_rewrite_rules();
144 }
145
146 /**
147 * Change View Subscription Title
148 *
149 * @param string $title Title.
150 *
151 * @return string
152 */
153 public function change_single_title( string $title ): string {
154 /* translators: %s: Subscription ID */
155 return sprintf( __( 'Subscription #%s', 'sdevs_subscrpt' ), get_query_var( 'view-subscription' ) );
156 }
157
158 /**
159 * Change Subscription Lists Title
160 *
161 * @param string $title Title.
162 *
163 * @return string
164 */
165 public function change_lists_title( string $title ): string {
166 global $wp_query;
167 $is_endpoint = isset( $wp_query->query_vars['subscriptions'] );
168 if ( $is_endpoint && ! is_admin() && is_account_page() ) {
169 $title = __( 'My Subscriptions', 'sdevs_subscrpt' );
170 }
171 return $title;
172 }
173
174 /**
175 * Filter menu items.
176 *
177 * @param array $items MyAccount menu items.
178 * @return array
179 */
180 public function custom_my_account_menu_items( array $items ): array {
181 $logout = $items['customer-logout'];
182 unset( $items['customer-logout'] );
183 $items['subscriptions'] = __( 'Subscriptions', 'sdevs_subscrpt' );
184 $items['customer-logout'] = $logout;
185 return $items;
186 }
187
188 /**
189 * Subscription Single EndPoint Content.
190 *
191 * @param int $current_page Current Page.
192 */
193 public function subscrpt_endpoint_content( $current_page ) {
194 $current_page = empty( $current_page ) ? 1 : absint( $current_page );
195 $args = array(
196 'author' => get_current_user_id(),
197 'posts_per_page' => 10,
198 'paged' => $current_page,
199 'post_type' => 'subscrpt_order',
200 'post_status' => array( 'pending', 'active', 'on_hold', 'cancelled', 'expired', 'pe_cancelled' ),
201 );
202
203 $postslist = new \WP_Query( $args );
204 wc_get_template(
205 'myaccount/subscriptions.php',
206 array(
207 'wp_button_class' => wc_wp_theme_get_element_class_name( 'button' ) ? ' ' . wc_wp_theme_get_element_class_name( 'button' ) : '',
208 'postslist' => $postslist,
209 'current_page' => $current_page,
210 ),
211 'subscription',
212 WP_SUBSCRIPTION_TEMPLATES
213 );
214 }
215
216 /**
217 * Enqueue assets
218 */
219 public function enqueue_styles() {
220 wp_enqueue_style( 'subscrpt_status_css' );
221 }
222 }
223