PluginProbe
Subscriptions for WooCommerce with Stripe Recurring Payments / 1.3.0
Subscriptions for WooCommerce with Stripe Recurring Payments v1.3.0
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 / Illuminate / Action.php

Action.php in Subscriptions for WooCommerce with Stripe Recurring Payments 1.3.0, at includes/Illuminate/Action.php

166 lines 4.5 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\Illuminate;
4
5 /**
6 * Action [ helper class ]
7 *
8 * @package SpringDevs\Subscription\Illuminate
9 */
10 class Action {
11
12 /**
13 * Did when status changes.
14 *
15 * @param string $status Status.
16 * @param int $subscription_id Subscription ID.
17 * @param bool $write_comment Write comment?.
18 */
19 public static function status( string $status, int $subscription_id, bool $write_comment = true ) {
20 wp_update_post(
21 array(
22 'ID' => $subscription_id,
23 'post_status' => $status,
24 )
25 );
26
27 if ( $write_comment ) {
28 self::write_comment( $status, $subscription_id );
29 }
30
31 self::user( $subscription_id );
32 }
33
34 /**
35 * Write Comment based on status.
36 *
37 * @param string $status Status.
38 * @param Int $subscription_id Subscription ID.
39 */
40 public static function write_comment( string $status, int $subscription_id ) {
41 switch ( $status ) {
42 case 'expired':
43 self::expired( $subscription_id );
44 break;
45 case 'active':
46 self::active( $subscription_id );
47 break;
48 case 'pending':
49 self::pending( $subscription_id );
50 break;
51 case 'cancelled':
52 self::cancelled( $subscription_id );
53 break;
54 case 'pe_cancelled':
55 self::pe_cancelled( $subscription_id );
56 break;
57 }
58 }
59
60 /**
61 * Write Comment About expired Subscription.
62 *
63 * @param int $subscription_id Subscription ID.
64 */
65 private static function expired( int $subscription_id ) {
66 $comment_id = wp_insert_comment(
67 array(
68 'comment_author' => 'Subscription for WooCommerce',
69 'comment_content' => 'Subscription is Expired',
70 'comment_post_ID' => $subscription_id,
71 'comment_type' => 'order_note',
72 )
73 );
74 update_comment_meta( $comment_id, '_subscrpt_activity', 'Subscription Expired' );
75
76 do_action( 'subscrpt_subscription_expired', $subscription_id );
77 }
78
79 /**
80 * Write Comment About Active Subscription.
81 *
82 * @param int $subscription_id Subscription ID.
83 */
84 private static function active( int $subscription_id ) {
85 $comment_id = wp_insert_comment(
86 array(
87 'comment_author' => 'Subscription for WooCommerce',
88 'comment_content' => 'Subscription activated.Next payment due date set.',
89 'comment_post_ID' => $subscription_id,
90 'comment_type' => 'order_note',
91 )
92 );
93 update_comment_meta( $comment_id, '_subscrpt_activity', 'Subscription Activated' );
94 do_action( 'subscrpt_subscription_activated', $subscription_id );
95 }
96
97 /**
98 * Write Comment About Subscription Pending.
99 *
100 * @param int $subscription_id Subscription ID.
101 */
102 private static function pending( int $subscription_id ) {
103 $comment_id = wp_insert_comment(
104 array(
105 'comment_author' => 'Subscription for WooCommerce',
106 'comment_content' => 'Subscription is pending.',
107 'comment_post_ID' => $subscription_id,
108 'comment_type' => 'order_note',
109 )
110 );
111 update_comment_meta( $comment_id, '_subscrpt_activity', 'Subscription Pending' );
112 }
113
114 /**
115 * Write Comment About Subscription Cancelled.
116 *
117 * @param int $subscription_id Subscription ID.
118 */
119 private static function cancelled( int $subscription_id ) {
120 $comment_id = wp_insert_comment(
121 array(
122 'comment_author' => 'Subscription for WooCommerce',
123 'comment_content' => 'Subscription is Cancelled.',
124 'comment_post_ID' => $subscription_id,
125 'comment_type' => 'order_note',
126 )
127 );
128 update_comment_meta( $comment_id, '_subscrpt_activity', 'Subscription Cancelled' );
129 }
130
131 /**
132 * Write Comment About Pending Cancellation.
133 *
134 * @param int $subscription_id Subscription ID.
135 */
136 private static function pe_cancelled( int $subscription_id ) {
137 $comment_id = wp_insert_comment(
138 array(
139 'comment_author' => 'Subscription for WooCommerce',
140 'comment_content' => 'Subscription is Pending Cancellation.',
141 'comment_post_ID' => $subscription_id,
142 'comment_type' => 'order_note',
143 )
144 );
145 update_comment_meta( $comment_id, '_subscrpt_activity', 'Subscription Pending Cancellation' );
146 }
147
148 /**
149 * Update user role.
150 *
151 * @param Int $subscription_id Subscription ID.
152 */
153 private static function user( $subscription_id ) {
154 $user = new \WP_User( get_current_user_id() );
155 if ( ! empty( $user->roles ) && is_array( $user->roles ) && in_array( 'administrator', $user->roles, true ) ) {
156 return;
157 }
158
159 if ( Helper::subscription_exists( $subscription_id, 'active' ) ) {
160 $user->set_role( get_option( 'subscrpt_active_role', 'subscriber' ) );
161 } elseif ( Helper::subscription_exists( $subscription_id, array( 'cancelled', 'expired' ) ) ) {
162 $user->set_role( get_option( 'subscrpt_unactive_role', 'customer' ) );
163 }
164 }
165 }
166