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