PluginProbe
Subscriptions for WooCommerce with Stripe Recurring Payments / 1.3.2
Subscriptions for WooCommerce with Stripe Recurring Payments v1.3.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 / Admin / Subscriptions.php

Subscriptions.php in Subscriptions for WooCommerce with Stripe Recurring Payments 1.3.2, at includes/Admin/Subscriptions.php

476 lines 13.9 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\Admin;
4
5 use SpringDevs\Subscription\Illuminate\Action;
6 use SpringDevs\Subscription\Illuminate\Helper;
7
8 /**
9 * Subscriptions class
10 *
11 * @package SpringDevs\Subscription\Admin
12 */
13 class Subscriptions {
14
15
16 /**
17 * Initialize the class.
18 */
19 public function __construct() {
20 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
21 add_filter( 'post_row_actions', array( $this, 'post_row_actions' ) );
22 add_filter( 'manage_subscrpt_order_posts_columns', array( $this, 'add_custom_columns' ) );
23 add_action( 'manage_subscrpt_order_posts_custom_column', array( $this, 'add_custom_columns_data' ), 10, 2 );
24 add_action( 'add_meta_boxes', array( $this, 'create_meta_boxes' ) );
25 add_action( 'admin_head-post.php', array( $this, 'some_styles' ) );
26 add_action( 'admin_head-post-new.php', array( $this, 'some_styles' ) );
27 add_action( 'admin_footer-post.php', array( $this, 'some_scripts' ) );
28 add_action( 'admin_footer-post-new.php', array( $this, 'some_scripts' ) );
29 add_action( 'save_post', array( $this, 'save_subscrpt_order' ) );
30 add_filter( 'woocommerce_order_item_get_formatted_meta_data', array( $this, 'remove_order_meta' ), 10, 1 );
31 add_filter( 'bulk_actions-edit-subscrpt_order', array( $this, 'remove_bulk_actions' ) );
32 }
33
34 /**
35 * Remove 'Edit` and 'Trash' from bulk actions.
36 *
37 * @param array $actions Action list.
38 *
39 * @return array
40 */
41 public function remove_bulk_actions( $actions ) {
42 unset( $actions['edit'] );
43 unset( $actions['trash'] );
44
45 return $actions;
46 }
47
48 /**
49 * Hide order meta key from custom fields.
50 *
51 * @param array $formatted_meta Data with key-value.
52 *
53 * @return array
54 */
55 public function remove_order_meta( $formatted_meta ): array {
56 $temp_metas = array();
57 foreach ( $formatted_meta as $key => $meta ) {
58 if ( isset( $meta->key ) && '_renew_subscrpt' !== $meta->key ) {
59 $temp_metas[ $key ] = $meta;
60 }
61 }
62
63 return $temp_metas;
64 }
65
66 /**
67 * Enqueue admin assets.
68 *
69 * @return void
70 */
71 public function enqueue_scripts() {
72 wp_enqueue_style( 'subscrpt_admin_css' );
73 wp_enqueue_style( 'subscrpt_status_css' );
74 }
75
76 /**
77 * Remove default post actions.
78 *
79 * @param array $actions Actions.
80 *
81 * @return array
82 */
83 public function post_row_actions( $actions ) {
84 global $current_screen;
85 if ( 'subscrpt_order' !== $current_screen->post_type ) {
86 return $actions;
87 }
88 unset( $actions['inline hide-if-no-js'] );
89 unset( $actions['view'] );
90 unset( $actions['trash'] );
91 unset( $actions['edit'] );
92
93 return $actions;
94 }
95
96 /**
97 * Register custom columns.
98 *
99 * @param array $columns Columns.
100 *
101 * @return array
102 */
103 public function add_custom_columns( $columns ) {
104 $columns['subscrpt_start_date'] = __( 'Start Date', 'sdevs_subscrpt' );
105 $columns['subscrpt_customer'] = __( 'Customer', 'sdevs_subscrpt' );
106 $columns['subscrpt_next_date'] = __( 'Next Date', 'sdevs_subscrpt' );
107 $columns['subscrpt_status'] = __( 'Status', 'sdevs_subscrpt' );
108 unset( $columns['date'] );
109 unset( $columns['cb'] );
110
111 return $columns;
112 }
113
114 /**
115 * Display column data.
116 *
117 * @param string $column Column.
118 * @param int $post_id Post Id.
119 *
120 * @return void
121 */
122 public function add_custom_columns_data( $column, $post_id ) {
123 $order_id = get_post_meta( $post_id, '_subscrpt_order_id', true );
124 $order = wc_get_order( $order_id );
125 if ( $order ) {
126 if ( 'subscrpt_start_date' === $column ) {
127 $start_date = get_post_meta( $post_id, '_subscrpt_start_date', true );
128 echo ! empty( $start_date ) ? esc_html( gmdate( 'F d, Y', $start_date ) ) : '-';
129 } elseif ( 'subscrpt_customer' === $column ) {
130 ?>
131 <?php echo wp_kses_post( $order->get_formatted_billing_full_name() ); ?>
132 <br />
133 <a href="mailto:<?php echo wp_kses_post( $order->get_billing_email() ); ?>"><?php echo wp_kses_post( $order->get_billing_email() ); ?></a>
134 <br />
135 <?php if ( ! empty( $order->get_billing_phone() ) ) : ?>
136 Phone : <a
137 href="tel:<?php echo esc_js( $order->get_billing_phone() ); ?>"><?php echo esc_js( $order->get_billing_phone() ); ?></a>
138 <?php endif; ?>
139 <?php
140 } elseif ( 'subscrpt_next_date' === $column ) {
141 $next_date = get_post_meta( $post_id, '_subscrpt_next_date', true );
142 echo ! empty( $next_date ) ? esc_html( gmdate( 'F d, Y', $next_date ) ) : '-';
143 } elseif ( 'subscrpt_status' === $column ) {
144 $status_obj = get_post_status_object( get_post_status( $post_id ) );
145 ?>
146 <span
147 class="subscrpt-<?php echo esc_html( $status_obj->name ); ?>"><?php echo esc_html( $status_obj->label ); ?></span>
148 <?php
149 }
150 } else {
151 esc_html_e( 'Order not found !!', 'sdevs_subscrpt' );
152 }
153 }
154
155 /**
156 * Create meta boxes for admin subscriptions.
157 */
158 public function create_meta_boxes() {
159 remove_meta_box( 'submitdiv', 'subscrpt_order', 'side' );
160 add_meta_box(
161 'subscrpt_order_save_post',
162 __( 'Subscription Action', 'sdevs_subscrpt' ),
163 array( $this, 'subscrpt_order_save_post' ),
164 'subscrpt_order',
165 'side',
166 'default'
167 );
168
169 add_meta_box(
170 'subscrpt_customer_info',
171 __( 'Customer Info', 'sdevs_subscrpt' ),
172 array( $this, 'customer_info' ),
173 'subscrpt_order',
174 'side',
175 'default'
176 );
177
178 add_meta_box(
179 'subscrpt_order_info',
180 __( 'Subscription Info', 'sdevs_subscrpt' ),
181 array( $this, 'subscrpt_order_info' ),
182 'subscrpt_order',
183 'normal',
184 'default'
185 );
186
187 add_meta_box(
188 'subscrpt_order_history',
189 __( 'Subscription History', 'sdevs_subscrpt' ),
190 array( $this, 'order_histories' ),
191 'subscrpt_order',
192 'normal',
193 'default'
194 );
195
196 add_meta_box(
197 'subscrpt_order_activities',
198 __( 'Subscription Activities', 'sdevs_subscrpt' ),
199 array( $this, 'order_activities' ),
200 'subscrpt_order',
201 'normal',
202 'default'
203 );
204 }
205
206 /**
207 * Display Order Histories.
208 *
209 * @param \WP_Post $post Post Object.
210 *
211 * @return void
212 */
213 public function order_histories( $post ) {
214 $subscription_id = $post->ID;
215 global $wpdb;
216 $table_name = $wpdb->prefix . 'subscrpt_order_relation';
217 // @phpcs:ignore
218 $order_histories = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %i WHERE subscription_id=%d', array(
219 $table_name,
220 $subscription_id,
221 )
222 )
223 );
224
225 include 'views/order-history.php';
226 }
227
228 /**
229 * Display Order Activities
230 *
231 * @param \WP_Post $post Post Object.
232 *
233 * @return void
234 */
235 public function order_activities( $post ) {
236 if ( function_exists( 'subscrpt_pro_activated' ) ) :
237 if ( subscrpt_pro_activated() ) :
238 do_action( 'subscrpt_order_activities', $post->ID );
239 else :
240 ?>
241 <a href="https://springdevs.com/subscription" target="_blank">
242 <img style="width: 100%;"
243 src="<?php echo esc_html( SUBSCRPT_ASSETS . '/images/subscrpt-ads.png' ); ?>" />
244 </a>
245 <?php
246 endif;
247 endif;
248 }
249
250 /**
251 * Save subscription HTML.
252 */
253 public function subscrpt_order_save_post() {
254 $actions_data = array(
255 'active' => array(
256 'label' => __( 'Activate Subscription', 'sdevs_subscrpt' ),
257 'value' => 'active',
258 ),
259 'pending' => array(
260 'label' => __( 'Pending Subscription', 'sdevs_subscrpt' ),
261 'value' => 'pending',
262 ),
263 'expire' => array(
264 'label' => __( 'Expire Subscription', 'sdevs_subscrpt' ),
265 'value' => 'expired',
266 ),
267 'pe_cancelled' => array(
268 'label' => __( 'Pending Cancel Subscription', 'sdevs_subscrpt' ),
269 'value' => 'pe_cancelled',
270 ),
271 'cancelled' => array(
272 'label' => __( 'Cancel Subscription', 'sdevs_subscrpt' ),
273 'value' => 'cancelled',
274 ),
275 );
276
277 $map_actions = array(
278 'pending' => array( 'active', 'cancelled' ),
279 'active' => array( 'pe_cancelled', 'cancelled' ),
280 'pe_cancelled' => array( 'active', 'cancelled' ),
281 'cancelled' => array( 'active' ),
282 'expired' => array(),
283 );
284
285 $status = get_post_status( get_the_ID() );
286 $actions = $map_actions[ $status ];
287
288 include 'views/subscription-save-meta.php';
289 }
290
291 /**
292 * Display Customer Info
293 *
294 * @param \WP_Post $post Post Object.
295 *
296 * @return void
297 */
298 public function customer_info( $post ) {
299 $order_id = get_post_meta( $post->ID, '_subscrpt_order_id', true );
300 $order = wc_get_order( $order_id );
301 if ( ! $order ) {
302 return;
303 }
304 include 'views/subscription-customer.php';
305 }
306
307 /**
308 * Display subscription info.
309 *
310 * @return void
311 */
312 public function subscrpt_order_info() {
313 $order_id = get_post_meta( get_the_ID(), '_subscrpt_order_id', true );
314 $order_item_id = get_post_meta( get_the_ID(), '_subscrpt_order_item_id', true );
315 $trial = get_post_meta( get_the_ID(), '_subscrpt_trial', true );
316 $start_date = get_post_meta( get_the_ID(), '_subscrpt_start_date', true );
317 $next_date = get_post_meta( get_the_ID(), '_subscrpt_next_date', true );
318 $trial_start_date = get_post_meta( get_the_ID(), '_subscrpt_trial_started', true );
319 $trial_end_date = get_post_meta( get_the_ID(), '_subscrpt_trial_ended', true );
320 $trial_mode = get_post_meta( get_the_ID(), '_subscrpt_trial_mode', true );
321 $order = wc_get_order( $order_id );
322 if ( ! $order ) {
323 return;
324 }
325 $order_item = $order->get_item( $order_item_id );
326
327 $product_name = $order_item->get_name();
328 $product_link = get_the_permalink( $order_item->get_product_id() );
329 $rows = array(
330 'product' => array(
331 'label' => __( 'Product', 'sdevs_subscrpt' ),
332 'value' => '<a href="' . esc_html( $product_link ) . '" target="_blank">' . esc_html( $product_name ) . '</a>',
333 ),
334 'cost' => array(
335 'label' => __( 'Cost', 'sdevs_subscrpt' ),
336 'value' => Helper::format_price_with_order_item( get_post_meta( get_the_ID(), '_subscrpt_price', true ), $order_item->get_id() ),
337 ),
338 'quantity' => array(
339 'label' => __( 'Qty', 'sdevs_subscrpt' ),
340 'value' => "x{$order_item->get_quantity()}",
341 ),
342 'start_date' => array(
343 'label' => __( 'Started date', 'sdevs_subscrpt' ),
344 'value' => ! empty( $start_date ) ? gmdate( 'F d, Y', $trial && $trial_start_date ? $trial_start_date : $start_date ) : '-',
345 ),
346 'next_date' => array(
347 'label' => __( 'Payment due date', 'sdevs_subscrpt' ),
348 'value' => ! empty( $next_date ) ? gmdate( 'F d, Y', $trial && $trial_end_date && 'on' === $trial_mode ? $trial_end_date : ( $next_date ?? '-' ) ) : '-',
349 ),
350 'status' => array(
351 'label' => __( 'Status', 'sdevs_subscrpt' ),
352 'value' => '<span class="subscrpt-' . get_post_status() . '">' . get_post_status_object( get_post_status() )->label . '</span>',
353 ),
354 'payment_method' => array(
355 'label' => __( 'Payment Method', 'sdevs_subscrpt' ),
356 'value' => empty( $order->get_payment_method_title() ) ? '-' : $order->get_payment_method_title(),
357 ),
358 'billing_address' => array(
359 'label' => __( 'Billing', 'sdevs_subscrpt' ),
360 'value' => $order->get_formatted_billing_address() ? $order->get_formatted_billing_address() : __( 'No billing address set.', 'sdevs_subscrpt' ),
361 ),
362 'shipping_address' => array(
363 'label' => __( 'Shipping', 'sdevs_subscrpt' ),
364 'value' => $order->get_formatted_shipping_address() ? $order->get_formatted_shipping_address() : __( 'No shipping address set.', 'sdevs_subscrpt' ),
365 ),
366 );
367 if ( $trial ) {
368 $rows = array_slice( $rows, 0, 3, true ) + array(
369 'trial' => array(
370 'label' => __( 'Trial', 'sdevs_subscrpt' ),
371 'value' => $trial,
372 ),
373 'trial_period' => array(
374 'label' => __( 'Trial Period', 'sdevs_subscrpt' ),
375 'value' => ( $trial_start_date && $trial_end_date ? ' [ ' . gmdate( 'F d, Y', $trial_start_date ) . ' - ' . gmdate( 'F d, Y', $trial_end_date ) . ' ] ' : __( 'Trial isn\'t activated yet! ', 'sdevs_subscrpt' ) ),
376 ),
377 ) + array_slice( $rows, 3, count( $rows ) - 1, true );
378 }
379
380 if ( class_exists( 'WC_Stripe' ) && 'stripe' === $order->get_payment_method() ) {
381 $is_auto_renew = get_post_meta( get_the_ID(), '_subscrpt_auto_renew', true );
382 $new_rows = array();
383 foreach ( $rows as $key => $value ) {
384 $new_rows[ $key ] = $value;
385 if ( 'payment_method' === $key ) {
386 $new_rows['stripe_auto_renewal'] = array(
387 'label' => __( 'Stripe Auto Renewal', 'sdevs_subscrpt' ),
388 'value' => '0' !== $is_auto_renew ? 'On' : 'Off',
389 );
390 }
391 }
392
393 $rows = $new_rows;
394 }
395
396 $rows = apply_filters( 'subscrpt_admin_info_rows', $rows, get_the_ID(), $order );
397
398 include 'views/subscription-info.php';
399 }
400
401 /**
402 * Include some styles.
403 *
404 * @return void
405 */
406 public function some_styles() {
407 global $post;
408 if ( 'subscrpt_order' === $post->post_type ) :
409 ?>
410 <style>
411 .submitbox {
412 display: flex;
413 justify-content: space-around;
414 }
415
416 .subscrpt_sub_box {
417 display: grid;
418 line-height: 2;
419 }
420 </style>
421 <?php
422 endif;
423 }
424
425 /**
426 * Disable changes popup.
427 *
428 * @return void
429 */
430 public function some_scripts() {
431 global $post;
432 if ( 'subscrpt_order' === $post->post_type ) :
433 ?>
434 <script>
435 jQuery(document).ready(function() {
436 jQuery(window).off('beforeunload', null);
437 });
438 </script>
439 <?php
440 endif;
441 }
442
443 public function save_subscrpt_order( $post_id ) {
444 if ( wp_is_post_revision( $post_id ) || ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || ! isset( $_POST['subscrpt_order_action'] ) ) {
445 return;
446 }
447 remove_all_actions( 'save_post' );
448
449 $action = sanitize_text_field( wp_unslash( $_POST['subscrpt_order_action'] ) );
450 $old_status = get_post_status( $post_id );
451
452 wp_update_post(
453 array(
454 'ID' => $post_id,
455 'post_status' => $action,
456 )
457 );
458
459 if ( $old_status !== $action ) {
460 $old_status_object = get_post_status_object( $old_status );
461 $new_status_object = get_post_status_object( $action );
462 WC()->mailer();
463 do_action( 'subscrpt_status_changed_admin_email_notification', $post_id, $old_status_object->label, $new_status_object->label );
464 }
465
466 $order_id = get_post_meta( $post_id, '_subscrpt_order_id', true );
467 if ( 'active' === $action ) {
468 $order = wc_get_order( $order_id );
469 $order->update_status( 'completed' );
470 Action::status( $action, $post_id );
471 } else {
472 Action::status( $action, $post_id );
473 }
474 }
475 }
476