@@ -30,9 +30,7 @@ class WC_Dev_Helper_Subscriptions {
3030
3131
3232 /**
33- * Setup:
34- *
35- * 1) An easy-to-use action for triggering subscription renewals, useful for gateway testing
33+ * Setup an easy-to-use action for triggering subscription renewals, useful for gateway testing
3634 *
3735 * @since 0.1.0
3836 */
@@ -41,11 +39,20 @@ public function __construct() {
4139 // Without this, and when using forwarded URLs, WC Subscriptions believes the site to be "duplicate" and disables updating the payment method
4240 add_filter ( 'woocommerce_subscriptions_is_duplicate_site ' , '__return_false ' );
4341
44- // add the "renew" action to the Subscriptions list table
45- add_filter ( 'woocommerce_subscriptions_list_table_actions ' , array ( $ this , 'add_renew_action ' ), 10 , 2 );
42+ // add the renew action to the Subscriptions list table
43+ if ( $ this ->is_subs_gte_2_0 () ) {
44+ add_filter ( 'woocommerce_subscription_list_table_actions ' , array ( $ this , 'add_renew_action ' ), 10 , 2 );
45+ } else {
46+ add_filter ( 'woocommerce_subscriptions_list_table_actions ' , array ( $ this , 'add_renew_action ' ), 10 , 2 );
47+ }
4648
47- // process the "renew" action
48- add_action ( 'load-woocommerce_page_subscriptions ' , array ( $ this , 'process_renew_action ' ) );
49+ // process the renewa action
50+ if ( $ this ->is_subs_gte_2_0 () ) {
51+ add_action ( 'load-edit.php ' , array ( $ this , 'process_renew_action ' ) );
52+ add_action ( 'admin_notices ' , array ( $ this , 'maybe_render_renewal_success_message ' ) );
53+ } else {
54+ add_action ( 'load-woocommerce_page_subscriptions ' , array ( $ this , 'process_pre_subs_2_0_renew_action ' ) );
55+ }
4956 }
5057
5158
@@ -54,20 +61,32 @@ public function __construct() {
5461 *
5562 * @since 0.1.0
5663 * @param array $actions subscription actions
57- * @param array $item subscription item
64+ * @param array|\WC_Subscription $ subscription item
5865 * @return mixed
5966 */
60- public function add_renew_action ( $ actions , $ item ) {
61-
62- $ renew_url = add_query_arg (
63- array (
64- 'page ' => $ _REQUEST ['page ' ],
65- 'user ' => $ item ['user_id ' ],
66- 'subscription ' => $ item ['subscription_key ' ],
67- 'action ' => 'renew ' ,
68- '_wpnonce ' => wp_create_nonce ( $ item ['subscription_key ' ] )
69- )
70- );
67+ public function add_renew_action ( $ actions , $ subscription ) {
68+
69+ if ( $ this ->is_subs_gte_2_0 () ) {
70+
71+ $ renew_url = add_query_arg (
72+ array (
73+ 'post ' => $ subscription ->id ,
74+ 'action ' => 'renew ' ,
75+ '_wpnonce ' => wp_create_nonce ( 'bulk-posts ' ),
76+ )
77+ );
78+
79+ } else {
80+ $ renew_url = add_query_arg (
81+ array (
82+ 'page ' => $ _REQUEST ['page ' ],
83+ 'user ' => $ subscription ['user_id ' ],
84+ 'subscription ' => $ subscription ['subscription_key ' ],
85+ 'action ' => 'renew ' ,
86+ '_wpnonce ' => wp_create_nonce ( $ subscription ['subscription_key ' ] ),
87+ )
88+ );
89+ }
7190
7291 $ actions ['renew ' ] = sprintf ( '<a href="%s">%s</a> ' , esc_url ( $ renew_url ), __ ( 'Renew ' , 'woocommerce-dev-helper ' ) );
7392
@@ -82,8 +101,64 @@ public function add_renew_action( $actions, $item ) {
82101 */
83102 public function process_renew_action () {
84103
104+ // only subscriptions
105+ if ( ! isset ( $ _REQUEST ['post_type ' ] ) || 'shop_subscription ' !== $ _REQUEST ['post_type ' ] ) {
106+ return ;
107+ }
108+
109+ $ wp_list_table = _get_list_table ( 'WP_Posts_List_Table ' );
110+
111+ if ( 'renew ' !== $ wp_list_table ->current_action () ) {
112+ return ;
113+ }
114+
115+ // load gateways
116+ WC ()->payment_gateways ();
117+
118+ $ subscription_id = absint ( $ _REQUEST ['post ' ] );
119+
120+ // trigger the renewal
121+ do_action ( 'woocommerce_scheduled_subscription_payment ' , $ subscription_id );
122+
123+ wp_redirect ( remove_query_arg ( 'action ' , add_query_arg ( array ( 'post_type ' => 'shop_subscription ' , 'wcdh_subs_renew ' => true , 'id ' => $ subscription_id ) ) ) );
124+
125+ exit ();
126+ }
127+
128+
129+ /**
130+ * Maybe render a renewal success message
131+ *
132+ * @since 0.2.0
133+ */
134+ public function maybe_render_renewal_success_message () {
135+ global $ post_type , $ pagenow ;
136+
137+ if ( 'edit.php ' !== $ pagenow || 'shop_subscription ' !== $ post_type || empty ( $ _REQUEST ['wcdh_subs_renew ' ] ) || empty ( $ _REQUEST ['id ' ] ) ) {
138+ return ;
139+ }
140+
141+ $ subscription = wcs_get_subscription ( absint ( $ _REQUEST ['id ' ] ) );
142+
143+ if ( $ subscription instanceof WC_Subscription ) {
144+ echo '<div class="updated"><p> ' . sprintf ( esc_html__ ( 'Subscription renewal processed. %sView Renewal Order%s ' ), '<a href=" ' . wcs_get_edit_post_link ( $ subscription ->get_last_order () ) . '"> ' , ' →</a> ' ) . '</p></div> ' ;
145+ }
146+ }
147+
148+
149+ /** Pre Subs 2.0 **********************************************************/
150+
151+
152+ /**
153+ * Process the renewal action from the Subscriptions list table for
154+ * 1.5.x
155+ *
156+ * @since 0.2.0
157+ */
158+ public function process_pre_subs_2_0_renew_action () {
159+
85160 // data check
86- if ( empty ( $ _GET ['action ' ] ) || 'renew ' !== $ _GET ['action ' ] || empty ( $ _GET ['user ' ] ) || empty ( $ _GET ['subscription ' ] ) ) {
161+ if ( empty ( $ _GET ['action ' ] ) || empty ( $ _GET [ ' _wpnonce ' ] ) || 'renew ' !== $ _GET ['action ' ] || empty ( $ _GET ['user ' ] ) || empty ( $ _GET ['subscription ' ] ) ) {
87162 return ;
88163 }
89164
@@ -98,7 +173,8 @@ public function process_renew_action() {
98173 // trigger the renewal payment
99174 WC_Subscriptions_Payment_Gateways::gateway_scheduled_subscription_payment ( absint ( $ _GET ['user ' ] ), $ _GET ['subscription ' ] );
100175
101- add_filter ( 'woocommerce_subscriptions_list_table_pre_process_actions ' , array ( $ this , 'maybe_render_renewal_success_message ' ) );
176+ // success message
177+ add_filter ( 'woocommerce_subscriptions_list_table_pre_process_actions ' , array ( $ this , 'maybe_render_pre_subs_2_0_renewal_success_message ' ) );
102178 }
103179
104180
@@ -110,7 +186,7 @@ public function process_renew_action() {
110186 * @param array $args
111187 * @return mixed
112188 */
113- public function maybe_render_renewal_success_message ( $ args ) {
189+ public function maybe_render_pre_subs_2_0_renewal_success_message ( $ args ) {
114190
115191 if ( empty ( $ _GET ['action ' ] ) || 'renew ' !== $ _GET ['action ' ] ) {
116192 return $ args ;
@@ -123,4 +199,16 @@ public function maybe_render_renewal_success_message( $args ) {
123199 }
124200
125201
202+ /**
203+ * Returns true if the active version of Subscriptions is 2.0+
204+ *
205+ * @since 0.2.0
206+ * @return mixed
207+ */
208+ protected function is_subs_gte_2_0 () {
209+
210+ return version_compare ( WC_Subscriptions::$ version , '1.6.0 ' , '> ' );
211+ }
212+
213+
126214}
0 commit comments