@@ -5,7 +5,7 @@ discard block |
||
5 | 5 | * |
6 | 6 | */ |
7 | 7 | if ( ! defined( 'ABSPATH' ) ) { |
8 | - exit; |
|
8 | + exit; |
|
9 | 9 | } |
10 | 10 | |
11 | 11 | /** |
@@ -15,198 +15,198 @@ discard block |
||
15 | 15 | */ |
16 | 16 | class GetPaid_Subscription_Data_Store { |
17 | 17 | |
18 | - /** |
|
19 | - * A map of database fields to data types. |
|
20 | - * |
|
21 | - * @since 1.0.19 |
|
22 | - * @var array |
|
23 | - */ |
|
24 | - protected $database_fields_to_data_type = array( |
|
25 | - 'id' => '%d', |
|
26 | - 'customer_id' => '%d', |
|
27 | - 'frequency' => '%d', |
|
28 | - 'period' => '%s', |
|
29 | - 'initial_amount' => '%s', |
|
30 | - 'recurring_amount' => '%s', |
|
31 | - 'bill_times' => '%d', |
|
32 | - 'transaction_id' => '%s', |
|
33 | - 'parent_payment_id' => '%d', |
|
34 | - 'product_id' => '%d', |
|
35 | - 'created' => '%s', |
|
36 | - 'expiration' => '%s', |
|
37 | - 'trial_period' => '%s', |
|
38 | - 'status' => '%s', |
|
39 | - 'profile_id' => '%s', |
|
40 | - ); |
|
41 | - |
|
42 | - /* |
|
18 | + /** |
|
19 | + * A map of database fields to data types. |
|
20 | + * |
|
21 | + * @since 1.0.19 |
|
22 | + * @var array |
|
23 | + */ |
|
24 | + protected $database_fields_to_data_type = array( |
|
25 | + 'id' => '%d', |
|
26 | + 'customer_id' => '%d', |
|
27 | + 'frequency' => '%d', |
|
28 | + 'period' => '%s', |
|
29 | + 'initial_amount' => '%s', |
|
30 | + 'recurring_amount' => '%s', |
|
31 | + 'bill_times' => '%d', |
|
32 | + 'transaction_id' => '%s', |
|
33 | + 'parent_payment_id' => '%d', |
|
34 | + 'product_id' => '%d', |
|
35 | + 'created' => '%s', |
|
36 | + 'expiration' => '%s', |
|
37 | + 'trial_period' => '%s', |
|
38 | + 'status' => '%s', |
|
39 | + 'profile_id' => '%s', |
|
40 | + ); |
|
41 | + |
|
42 | + /* |
|
43 | 43 | |-------------------------------------------------------------------------- |
44 | 44 | | CRUD Methods |
45 | 45 | |-------------------------------------------------------------------------- |
46 | 46 | */ |
47 | 47 | |
48 | - /** |
|
49 | - * Method to create a new subscription in the database. |
|
50 | - * |
|
51 | - * @param WPInv_Subscription $subscription Subscription object. |
|
52 | - */ |
|
53 | - public function create( &$subscription ) { |
|
54 | - global $wpdb; |
|
55 | - |
|
56 | - $values = array(); |
|
57 | - $formats = array(); |
|
58 | - |
|
59 | - $fields = $this->database_fields_to_data_type; |
|
60 | - unset( $fields['id'] ); |
|
61 | - |
|
62 | - foreach ( $fields as $key => $format ) { |
|
63 | - $method = "get_$key"; |
|
64 | - $values[$key] = $subscription->$method( 'edit' ); |
|
65 | - $formats[] = $format; |
|
66 | - } |
|
67 | - |
|
68 | - $result = $wpdb->insert( $wpdb->prefix . 'wpinv_subscriptions', $values, $formats ); |
|
69 | - |
|
70 | - if ( $result ) { |
|
71 | - $subscription->set_id( $wpdb->insert_id ); |
|
72 | - $subscription->apply_changes(); |
|
73 | - $subscription->clear_cache(); |
|
74 | - update_post_meta( $subscription->get_parent_invoice_id(), '_wpinv_subscription_id', $subscription->get_id() ); |
|
75 | - do_action( 'getpaid_new_subscription', $subscription ); |
|
76 | - return true; |
|
77 | - } |
|
78 | - |
|
79 | - return false; |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * Method to read a subscription from the database. |
|
84 | - * |
|
85 | - * @param WPInv_Subscription $subscription Subscription object. |
|
86 | - * |
|
87 | - */ |
|
88 | - public function read( &$subscription ) { |
|
89 | - global $wpdb; |
|
90 | - |
|
91 | - $subscription->set_defaults(); |
|
92 | - |
|
93 | - if ( ! $subscription->get_id() ) { |
|
94 | - $subscription->last_error = __( 'Invalid subscription ID.', 'invoicing' ); |
|
95 | - $subscription->set_id( 0 ); |
|
96 | - return false; |
|
97 | - } |
|
98 | - |
|
99 | - // Maybe retrieve from the cache. |
|
100 | - $raw_subscription = wp_cache_get( $subscription->get_id(), 'getpaid_subscriptions' ); |
|
101 | - |
|
102 | - // If not found, retrieve from the db. |
|
103 | - if ( false === $raw_subscription ) { |
|
104 | - |
|
105 | - $raw_subscription = $wpdb->get_row( |
|
106 | - $wpdb->prepare( |
|
107 | - "SELECT * FROM {$wpdb->prefix}wpinv_subscriptions WHERE id = %d", |
|
108 | - $subscription->get_id() |
|
109 | - ) |
|
110 | - ); |
|
111 | - |
|
112 | - // Update the cache with our data |
|
113 | - wp_cache_set( $subscription->get_id(), $raw_subscription, 'getpaid_subscriptions' ); |
|
114 | - |
|
115 | - } |
|
116 | - |
|
117 | - if ( ! $raw_subscription ) { |
|
118 | - $subscription->set_id( 0 ); |
|
119 | - $subscription->last_error = __( 'Invalid subscription ID.', 'invoicing' ); |
|
120 | - return false; |
|
121 | - } |
|
122 | - |
|
123 | - foreach ( array_keys( $this->database_fields_to_data_type ) as $key ) { |
|
124 | - $method = "set_$key"; |
|
125 | - $subscription->$method( $raw_subscription->$key ); |
|
126 | - } |
|
127 | - |
|
128 | - $subscription->set_object_read( true ); |
|
129 | - do_action( 'getpaid_read_subscription', $subscription ); |
|
130 | - |
|
131 | - } |
|
132 | - |
|
133 | - /** |
|
134 | - * Method to update a subscription in the database. |
|
135 | - * |
|
136 | - * @param WPInv_Subscription $subscription Subscription object. |
|
137 | - */ |
|
138 | - public function update( &$subscription ) { |
|
139 | - global $wpdb; |
|
140 | - |
|
141 | - $changes = $subscription->get_changes(); |
|
142 | - $values = array(); |
|
143 | - $formats = array(); |
|
144 | - |
|
145 | - foreach ( $this->database_fields_to_data_type as $key => $format ) { |
|
146 | - if ( array_key_exists( $key, $changes ) ) { |
|
147 | - $method = "get_$key"; |
|
148 | - $values[$key] = $subscription->$method( 'edit' ); |
|
149 | - $formats[] = $format; |
|
150 | - } |
|
151 | - } |
|
152 | - |
|
153 | - if ( empty( $values ) ) { |
|
154 | - return; |
|
155 | - } |
|
156 | - |
|
157 | - $wpdb->update( |
|
158 | - $wpdb->prefix . 'wpinv_subscriptions', |
|
159 | - $values, |
|
160 | - array( |
|
161 | - 'id' => $subscription->get_id(), |
|
162 | - ), |
|
163 | - $formats, |
|
164 | - '%d' |
|
165 | - ); |
|
166 | - |
|
167 | - // Apply the changes. |
|
168 | - $subscription->apply_changes(); |
|
169 | - |
|
170 | - // Delete cache. |
|
171 | - $subscription->clear_cache(); |
|
172 | - |
|
173 | - update_post_meta( $subscription->get_parent_invoice_id(), '_wpinv_subscr_profile_id', $subscription->get_profile_id() ); |
|
174 | - update_post_meta( $subscription->get_parent_invoice_id(), '_wpinv_subscription_id', $subscription->get_id() ); |
|
175 | - |
|
176 | - // Fire a hook. |
|
177 | - do_action( 'getpaid_update_subscription', $subscription ); |
|
178 | - |
|
179 | - } |
|
180 | - |
|
181 | - /** |
|
182 | - * Method to delete a subscription from the database. |
|
183 | - * |
|
184 | - * @param WPInv_Subscription $subscription |
|
185 | - */ |
|
186 | - public function delete( &$subscription ) { |
|
187 | - global $wpdb; |
|
188 | - |
|
189 | - $wpdb->query( |
|
190 | - $wpdb->prepare( |
|
191 | - "DELETE FROM {$wpdb->prefix}wpinv_subscriptions |
|
48 | + /** |
|
49 | + * Method to create a new subscription in the database. |
|
50 | + * |
|
51 | + * @param WPInv_Subscription $subscription Subscription object. |
|
52 | + */ |
|
53 | + public function create( &$subscription ) { |
|
54 | + global $wpdb; |
|
55 | + |
|
56 | + $values = array(); |
|
57 | + $formats = array(); |
|
58 | + |
|
59 | + $fields = $this->database_fields_to_data_type; |
|
60 | + unset( $fields['id'] ); |
|
61 | + |
|
62 | + foreach ( $fields as $key => $format ) { |
|
63 | + $method = "get_$key"; |
|
64 | + $values[$key] = $subscription->$method( 'edit' ); |
|
65 | + $formats[] = $format; |
|
66 | + } |
|
67 | + |
|
68 | + $result = $wpdb->insert( $wpdb->prefix . 'wpinv_subscriptions', $values, $formats ); |
|
69 | + |
|
70 | + if ( $result ) { |
|
71 | + $subscription->set_id( $wpdb->insert_id ); |
|
72 | + $subscription->apply_changes(); |
|
73 | + $subscription->clear_cache(); |
|
74 | + update_post_meta( $subscription->get_parent_invoice_id(), '_wpinv_subscription_id', $subscription->get_id() ); |
|
75 | + do_action( 'getpaid_new_subscription', $subscription ); |
|
76 | + return true; |
|
77 | + } |
|
78 | + |
|
79 | + return false; |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * Method to read a subscription from the database. |
|
84 | + * |
|
85 | + * @param WPInv_Subscription $subscription Subscription object. |
|
86 | + * |
|
87 | + */ |
|
88 | + public function read( &$subscription ) { |
|
89 | + global $wpdb; |
|
90 | + |
|
91 | + $subscription->set_defaults(); |
|
92 | + |
|
93 | + if ( ! $subscription->get_id() ) { |
|
94 | + $subscription->last_error = __( 'Invalid subscription ID.', 'invoicing' ); |
|
95 | + $subscription->set_id( 0 ); |
|
96 | + return false; |
|
97 | + } |
|
98 | + |
|
99 | + // Maybe retrieve from the cache. |
|
100 | + $raw_subscription = wp_cache_get( $subscription->get_id(), 'getpaid_subscriptions' ); |
|
101 | + |
|
102 | + // If not found, retrieve from the db. |
|
103 | + if ( false === $raw_subscription ) { |
|
104 | + |
|
105 | + $raw_subscription = $wpdb->get_row( |
|
106 | + $wpdb->prepare( |
|
107 | + "SELECT * FROM {$wpdb->prefix}wpinv_subscriptions WHERE id = %d", |
|
108 | + $subscription->get_id() |
|
109 | + ) |
|
110 | + ); |
|
111 | + |
|
112 | + // Update the cache with our data |
|
113 | + wp_cache_set( $subscription->get_id(), $raw_subscription, 'getpaid_subscriptions' ); |
|
114 | + |
|
115 | + } |
|
116 | + |
|
117 | + if ( ! $raw_subscription ) { |
|
118 | + $subscription->set_id( 0 ); |
|
119 | + $subscription->last_error = __( 'Invalid subscription ID.', 'invoicing' ); |
|
120 | + return false; |
|
121 | + } |
|
122 | + |
|
123 | + foreach ( array_keys( $this->database_fields_to_data_type ) as $key ) { |
|
124 | + $method = "set_$key"; |
|
125 | + $subscription->$method( $raw_subscription->$key ); |
|
126 | + } |
|
127 | + |
|
128 | + $subscription->set_object_read( true ); |
|
129 | + do_action( 'getpaid_read_subscription', $subscription ); |
|
130 | + |
|
131 | + } |
|
132 | + |
|
133 | + /** |
|
134 | + * Method to update a subscription in the database. |
|
135 | + * |
|
136 | + * @param WPInv_Subscription $subscription Subscription object. |
|
137 | + */ |
|
138 | + public function update( &$subscription ) { |
|
139 | + global $wpdb; |
|
140 | + |
|
141 | + $changes = $subscription->get_changes(); |
|
142 | + $values = array(); |
|
143 | + $formats = array(); |
|
144 | + |
|
145 | + foreach ( $this->database_fields_to_data_type as $key => $format ) { |
|
146 | + if ( array_key_exists( $key, $changes ) ) { |
|
147 | + $method = "get_$key"; |
|
148 | + $values[$key] = $subscription->$method( 'edit' ); |
|
149 | + $formats[] = $format; |
|
150 | + } |
|
151 | + } |
|
152 | + |
|
153 | + if ( empty( $values ) ) { |
|
154 | + return; |
|
155 | + } |
|
156 | + |
|
157 | + $wpdb->update( |
|
158 | + $wpdb->prefix . 'wpinv_subscriptions', |
|
159 | + $values, |
|
160 | + array( |
|
161 | + 'id' => $subscription->get_id(), |
|
162 | + ), |
|
163 | + $formats, |
|
164 | + '%d' |
|
165 | + ); |
|
166 | + |
|
167 | + // Apply the changes. |
|
168 | + $subscription->apply_changes(); |
|
169 | + |
|
170 | + // Delete cache. |
|
171 | + $subscription->clear_cache(); |
|
172 | + |
|
173 | + update_post_meta( $subscription->get_parent_invoice_id(), '_wpinv_subscr_profile_id', $subscription->get_profile_id() ); |
|
174 | + update_post_meta( $subscription->get_parent_invoice_id(), '_wpinv_subscription_id', $subscription->get_id() ); |
|
175 | + |
|
176 | + // Fire a hook. |
|
177 | + do_action( 'getpaid_update_subscription', $subscription ); |
|
178 | + |
|
179 | + } |
|
180 | + |
|
181 | + /** |
|
182 | + * Method to delete a subscription from the database. |
|
183 | + * |
|
184 | + * @param WPInv_Subscription $subscription |
|
185 | + */ |
|
186 | + public function delete( &$subscription ) { |
|
187 | + global $wpdb; |
|
188 | + |
|
189 | + $wpdb->query( |
|
190 | + $wpdb->prepare( |
|
191 | + "DELETE FROM {$wpdb->prefix}wpinv_subscriptions |
|
192 | 192 | WHERE id = %d", |
193 | - $subscription->get_id() |
|
194 | - ) |
|
195 | - ); |
|
193 | + $subscription->get_id() |
|
194 | + ) |
|
195 | + ); |
|
196 | 196 | |
197 | - delete_post_meta( $subscription->get_parent_invoice_id(), '_wpinv_subscr_profile_id' ); |
|
198 | - delete_post_meta( $subscription->get_parent_invoice_id(), '_wpinv_subscription_id' ); |
|
197 | + delete_post_meta( $subscription->get_parent_invoice_id(), '_wpinv_subscr_profile_id' ); |
|
198 | + delete_post_meta( $subscription->get_parent_invoice_id(), '_wpinv_subscription_id' ); |
|
199 | 199 | |
200 | - // Delete cache. |
|
201 | - $subscription->clear_cache(); |
|
200 | + // Delete cache. |
|
201 | + $subscription->clear_cache(); |
|
202 | 202 | |
203 | - // Fire a hook. |
|
204 | - do_action( 'getpaid_delete_subscription', $subscription ); |
|
203 | + // Fire a hook. |
|
204 | + do_action( 'getpaid_delete_subscription', $subscription ); |
|
205 | 205 | |
206 | - $subscription->set_id( 0 ); |
|
207 | - } |
|
206 | + $subscription->set_id( 0 ); |
|
207 | + } |
|
208 | 208 | |
209 | - /* |
|
209 | + /* |
|
210 | 210 | |-------------------------------------------------------------------------- |
211 | 211 | | Additional Methods |
212 | 212 | |-------------------------------------------------------------------------- |
@@ -50,7 +50,7 @@ discard block |
||
50 | 50 | */ |
51 | 51 | function getpaid_get_invoice_subscription_group( $invoice_id, $subscription_id ) { |
52 | 52 | $subscription_groups = getpaid_get_invoice_subscription_groups( $invoice_id ); |
53 | - $matching_group = wp_list_filter( $subscription_groups, compact( 'subscription_id' ) ); |
|
53 | + $matching_group = wp_list_filter( $subscription_groups, compact( 'subscription_id' ) ); |
|
54 | 54 | return reset( $matching_group ); |
55 | 55 | } |
56 | 56 | |
@@ -62,8 +62,8 @@ discard block |
||
62 | 62 | * @return WPInv_Subscription|false |
63 | 63 | */ |
64 | 64 | function getpaid_get_subscription( $subscription ) { |
65 | - $subscription = new WPInv_Subscription( $subscription ); |
|
66 | - return $subscription->exists() ? $subscription : false; |
|
65 | + $subscription = new WPInv_Subscription( $subscription ); |
|
66 | + return $subscription->exists() ? $subscription : false; |
|
67 | 67 | } |
68 | 68 | |
69 | 69 | /** |
@@ -77,28 +77,28 @@ discard block |
||
77 | 77 | */ |
78 | 78 | function getpaid_get_subscriptions( $args = array(), $return = 'results' ) { |
79 | 79 | |
80 | - // Do not retrieve all fields if we just want the count. |
|
81 | - if ( 'count' == $return ) { |
|
82 | - $args['fields'] = 'id'; |
|
83 | - $args['number'] = 1; |
|
84 | - } |
|
80 | + // Do not retrieve all fields if we just want the count. |
|
81 | + if ( 'count' == $return ) { |
|
82 | + $args['fields'] = 'id'; |
|
83 | + $args['number'] = 1; |
|
84 | + } |
|
85 | 85 | |
86 | - // Do not count all matches if we just want the results. |
|
87 | - if ( 'results' == $return ) { |
|
88 | - $args['count_total'] = false; |
|
89 | - } |
|
86 | + // Do not count all matches if we just want the results. |
|
87 | + if ( 'results' == $return ) { |
|
88 | + $args['count_total'] = false; |
|
89 | + } |
|
90 | 90 | |
91 | - $query = new GetPaid_Subscriptions_Query( $args ); |
|
91 | + $query = new GetPaid_Subscriptions_Query( $args ); |
|
92 | 92 | |
93 | - if ( 'results' == $return ) { |
|
94 | - return $query->get_results(); |
|
95 | - } |
|
93 | + if ( 'results' == $return ) { |
|
94 | + return $query->get_results(); |
|
95 | + } |
|
96 | 96 | |
97 | - if ( 'count' == $return ) { |
|
98 | - return $query->get_total(); |
|
99 | - } |
|
97 | + if ( 'count' == $return ) { |
|
98 | + return $query->get_total(); |
|
99 | + } |
|
100 | 100 | |
101 | - return $query; |
|
101 | + return $query; |
|
102 | 102 | } |
103 | 103 | |
104 | 104 | /** |
@@ -108,18 +108,18 @@ discard block |
||
108 | 108 | */ |
109 | 109 | function getpaid_get_subscription_statuses() { |
110 | 110 | |
111 | - return apply_filters( |
|
112 | - 'getpaid_get_subscription_statuses', |
|
113 | - array( |
|
114 | - 'pending' => __( 'Pending', 'invoicing' ), |
|
115 | - 'trialling' => __( 'Trialing', 'invoicing' ), |
|
116 | - 'active' => __( 'Active', 'invoicing' ), |
|
117 | - 'failing' => __( 'Failing', 'invoicing' ), |
|
118 | - 'expired' => __( 'Expired', 'invoicing' ), |
|
119 | - 'completed' => __( 'Complete', 'invoicing' ), |
|
120 | - 'cancelled' => __( 'Cancelled', 'invoicing' ), |
|
121 | - ) |
|
122 | - ); |
|
111 | + return apply_filters( |
|
112 | + 'getpaid_get_subscription_statuses', |
|
113 | + array( |
|
114 | + 'pending' => __( 'Pending', 'invoicing' ), |
|
115 | + 'trialling' => __( 'Trialing', 'invoicing' ), |
|
116 | + 'active' => __( 'Active', 'invoicing' ), |
|
117 | + 'failing' => __( 'Failing', 'invoicing' ), |
|
118 | + 'expired' => __( 'Expired', 'invoicing' ), |
|
119 | + 'completed' => __( 'Complete', 'invoicing' ), |
|
120 | + 'cancelled' => __( 'Cancelled', 'invoicing' ), |
|
121 | + ) |
|
122 | + ); |
|
123 | 123 | |
124 | 124 | } |
125 | 125 | |
@@ -129,8 +129,8 @@ discard block |
||
129 | 129 | * @return string |
130 | 130 | */ |
131 | 131 | function getpaid_get_subscription_status_label( $status ) { |
132 | - $statuses = getpaid_get_subscription_statuses(); |
|
133 | - return isset( $statuses[ $status ] ) ? $statuses[ $status ] : ucfirst( sanitize_text_field( $status ) ); |
|
132 | + $statuses = getpaid_get_subscription_statuses(); |
|
133 | + return isset( $statuses[ $status ] ) ? $statuses[ $status ] : ucfirst( sanitize_text_field( $status ) ); |
|
134 | 134 | } |
135 | 135 | |
136 | 136 | /** |
@@ -140,18 +140,18 @@ discard block |
||
140 | 140 | */ |
141 | 141 | function getpaid_get_subscription_status_classes() { |
142 | 142 | |
143 | - return apply_filters( |
|
144 | - 'getpaid_get_subscription_status_classes', |
|
145 | - array( |
|
146 | - 'pending' => 'badge-dark', |
|
147 | - 'trialling' => 'badge-info', |
|
148 | - 'active' => 'badge-success', |
|
149 | - 'failing' => 'badge-warning', |
|
150 | - 'expired' => 'badge-danger', |
|
151 | - 'completed' => 'badge-primary', |
|
152 | - 'cancelled' => 'badge-secondary', |
|
153 | - ) |
|
154 | - ); |
|
143 | + return apply_filters( |
|
144 | + 'getpaid_get_subscription_status_classes', |
|
145 | + array( |
|
146 | + 'pending' => 'badge-dark', |
|
147 | + 'trialling' => 'badge-info', |
|
148 | + 'active' => 'badge-success', |
|
149 | + 'failing' => 'badge-warning', |
|
150 | + 'expired' => 'badge-danger', |
|
151 | + 'completed' => 'badge-primary', |
|
152 | + 'cancelled' => 'badge-secondary', |
|
153 | + ) |
|
154 | + ); |
|
155 | 155 | |
156 | 156 | } |
157 | 157 | |
@@ -162,15 +162,15 @@ discard block |
||
162 | 162 | */ |
163 | 163 | function getpaid_get_subscription_status_counts( $args = array() ) { |
164 | 164 | |
165 | - $statuses = array_keys( getpaid_get_subscription_statuses() ); |
|
166 | - $counts = array(); |
|
165 | + $statuses = array_keys( getpaid_get_subscription_statuses() ); |
|
166 | + $counts = array(); |
|
167 | 167 | |
168 | - foreach ( $statuses as $status ) { |
|
169 | - $_args = wp_parse_args( "status=$status", $args ); |
|
170 | - $counts[ $status ] = getpaid_get_subscriptions( $_args, 'count' ); |
|
171 | - } |
|
168 | + foreach ( $statuses as $status ) { |
|
169 | + $_args = wp_parse_args( "status=$status", $args ); |
|
170 | + $counts[ $status ] = getpaid_get_subscriptions( $_args, 'count' ); |
|
171 | + } |
|
172 | 172 | |
173 | - return $counts; |
|
173 | + return $counts; |
|
174 | 174 | |
175 | 175 | } |
176 | 176 | |
@@ -181,32 +181,32 @@ discard block |
||
181 | 181 | */ |
182 | 182 | function getpaid_get_subscription_periods() { |
183 | 183 | |
184 | - return apply_filters( |
|
185 | - 'getpaid_get_subscription_periods', |
|
186 | - array( |
|
184 | + return apply_filters( |
|
185 | + 'getpaid_get_subscription_periods', |
|
186 | + array( |
|
187 | 187 | |
188 | - 'day' => array( |
|
189 | - 'singular' => __( '%s day', 'invoicing' ), |
|
190 | - 'plural' => __( '%d days', 'invoicing' ), |
|
191 | - ), |
|
188 | + 'day' => array( |
|
189 | + 'singular' => __( '%s day', 'invoicing' ), |
|
190 | + 'plural' => __( '%d days', 'invoicing' ), |
|
191 | + ), |
|
192 | 192 | |
193 | - 'week' => array( |
|
194 | - 'singular' => __( '%s week', 'invoicing' ), |
|
195 | - 'plural' => __( '%d weeks', 'invoicing' ), |
|
196 | - ), |
|
193 | + 'week' => array( |
|
194 | + 'singular' => __( '%s week', 'invoicing' ), |
|
195 | + 'plural' => __( '%d weeks', 'invoicing' ), |
|
196 | + ), |
|
197 | 197 | |
198 | - 'month' => array( |
|
199 | - 'singular' => __( '%s month', 'invoicing' ), |
|
200 | - 'plural' => __( '%d months', 'invoicing' ), |
|
201 | - ), |
|
198 | + 'month' => array( |
|
199 | + 'singular' => __( '%s month', 'invoicing' ), |
|
200 | + 'plural' => __( '%d months', 'invoicing' ), |
|
201 | + ), |
|
202 | 202 | |
203 | - 'year' => array( |
|
204 | - 'singular' => __( '%s year', 'invoicing' ), |
|
205 | - 'plural' => __( '%d years', 'invoicing' ), |
|
206 | - ), |
|
203 | + 'year' => array( |
|
204 | + 'singular' => __( '%s year', 'invoicing' ), |
|
205 | + 'plural' => __( '%d years', 'invoicing' ), |
|
206 | + ), |
|
207 | 207 | |
208 | - ) |
|
209 | - ); |
|
208 | + ) |
|
209 | + ); |
|
210 | 210 | |
211 | 211 | } |
212 | 212 | |
@@ -217,7 +217,7 @@ discard block |
||
217 | 217 | * @return int |
218 | 218 | */ |
219 | 219 | function getpaid_get_subscription_trial_period_interval( $trial_period ) { |
220 | - return (int) preg_replace( '/[^0-9]/', '', $trial_period ); |
|
220 | + return (int) preg_replace( '/[^0-9]/', '', $trial_period ); |
|
221 | 221 | } |
222 | 222 | |
223 | 223 | /** |
@@ -227,7 +227,7 @@ discard block |
||
227 | 227 | * @return string |
228 | 228 | */ |
229 | 229 | function getpaid_get_subscription_trial_period_period( $trial_period ) { |
230 | - return preg_replace( '/[^a-z]/', '', strtolower( $trial_period ) ); |
|
230 | + return preg_replace( '/[^a-z]/', '', strtolower( $trial_period ) ); |
|
231 | 231 | } |
232 | 232 | |
233 | 233 | /** |
@@ -238,8 +238,8 @@ discard block |
||
238 | 238 | * @return string |
239 | 239 | */ |
240 | 240 | function getpaid_get_subscription_period_label( $period, $interval = 1, $singular_prefix = '1' ) { |
241 | - $label = (int) $interval > 1 ? getpaid_get_plural_subscription_period_label( $period, $interval ) : getpaid_get_singular_subscription_period_label( $period, $singular_prefix ); |
|
242 | - return strtolower( sanitize_text_field( $label ) ); |
|
241 | + $label = (int) $interval > 1 ? getpaid_get_plural_subscription_period_label( $period, $interval ) : getpaid_get_singular_subscription_period_label( $period, $singular_prefix ); |
|
242 | + return strtolower( sanitize_text_field( $label ) ); |
|
243 | 243 | } |
244 | 244 | |
245 | 245 | /** |
@@ -250,22 +250,22 @@ discard block |
||
250 | 250 | */ |
251 | 251 | function getpaid_get_singular_subscription_period_label( $period, $singular_prefix = '1' ) { |
252 | 252 | |
253 | - $periods = getpaid_get_subscription_periods(); |
|
254 | - $period = strtolower( $period ); |
|
253 | + $periods = getpaid_get_subscription_periods(); |
|
254 | + $period = strtolower( $period ); |
|
255 | 255 | |
256 | - if ( isset( $periods[ $period ] ) ) { |
|
257 | - return sprintf( $periods[ $period ]['singular'], $singular_prefix ); |
|
258 | - } |
|
256 | + if ( isset( $periods[ $period ] ) ) { |
|
257 | + return sprintf( $periods[ $period ]['singular'], $singular_prefix ); |
|
258 | + } |
|
259 | 259 | |
260 | - // Backwards compatibility. |
|
261 | - foreach ( $periods as $key => $data ) { |
|
262 | - if ( strpos( $key, $period ) === 0 ) { |
|
263 | - return sprintf( $data['singular'], $singular_prefix ); |
|
264 | - } |
|
265 | - } |
|
260 | + // Backwards compatibility. |
|
261 | + foreach ( $periods as $key => $data ) { |
|
262 | + if ( strpos( $key, $period ) === 0 ) { |
|
263 | + return sprintf( $data['singular'], $singular_prefix ); |
|
264 | + } |
|
265 | + } |
|
266 | 266 | |
267 | - // Invalid string. |
|
268 | - return ''; |
|
267 | + // Invalid string. |
|
268 | + return ''; |
|
269 | 269 | } |
270 | 270 | |
271 | 271 | /** |
@@ -277,22 +277,22 @@ discard block |
||
277 | 277 | */ |
278 | 278 | function getpaid_get_plural_subscription_period_label( $period, $interval ) { |
279 | 279 | |
280 | - $periods = getpaid_get_subscription_periods(); |
|
281 | - $period = strtolower( $period ); |
|
280 | + $periods = getpaid_get_subscription_periods(); |
|
281 | + $period = strtolower( $period ); |
|
282 | 282 | |
283 | - if ( isset( $periods[ $period ] ) ) { |
|
284 | - return sprintf( $periods[ $period ]['plural'], $interval ); |
|
285 | - } |
|
283 | + if ( isset( $periods[ $period ] ) ) { |
|
284 | + return sprintf( $periods[ $period ]['plural'], $interval ); |
|
285 | + } |
|
286 | 286 | |
287 | - // Backwards compatibility. |
|
288 | - foreach ( $periods as $key => $data ) { |
|
289 | - if ( strpos( $key, $period ) === 0 ) { |
|
290 | - return sprintf( $data['plural'], $interval ); |
|
291 | - } |
|
292 | - } |
|
287 | + // Backwards compatibility. |
|
288 | + foreach ( $periods as $key => $data ) { |
|
289 | + if ( strpos( $key, $period ) === 0 ) { |
|
290 | + return sprintf( $data['plural'], $interval ); |
|
291 | + } |
|
292 | + } |
|
293 | 293 | |
294 | - // Invalid string. |
|
295 | - return ''; |
|
294 | + // Invalid string. |
|
295 | + return ''; |
|
296 | 296 | } |
297 | 297 | |
298 | 298 | /** |
@@ -303,101 +303,101 @@ discard block |
||
303 | 303 | */ |
304 | 304 | function getpaid_get_formatted_subscription_amount( $subscription ) { |
305 | 305 | |
306 | - $initial = wpinv_price( $subscription->get_initial_amount(), $subscription->get_parent_payment()->get_currency() ); |
|
307 | - $recurring = wpinv_price( $subscription->get_recurring_amount(), $subscription->get_parent_payment()->get_currency() ); |
|
308 | - $period = getpaid_get_subscription_period_label( $subscription->get_period(), $subscription->get_frequency(), '' ); |
|
309 | - $bill_times = $subscription->get_bill_times(); |
|
306 | + $initial = wpinv_price( $subscription->get_initial_amount(), $subscription->get_parent_payment()->get_currency() ); |
|
307 | + $recurring = wpinv_price( $subscription->get_recurring_amount(), $subscription->get_parent_payment()->get_currency() ); |
|
308 | + $period = getpaid_get_subscription_period_label( $subscription->get_period(), $subscription->get_frequency(), '' ); |
|
309 | + $bill_times = $subscription->get_bill_times(); |
|
310 | 310 | |
311 | - if ( ! empty( $bill_times ) ) { |
|
312 | - $bill_times = $subscription->get_frequency() * $bill_times; |
|
313 | - $bill_times = getpaid_get_subscription_period_label( $subscription->get_period(), $bill_times ); |
|
314 | - } |
|
311 | + if ( ! empty( $bill_times ) ) { |
|
312 | + $bill_times = $subscription->get_frequency() * $bill_times; |
|
313 | + $bill_times = getpaid_get_subscription_period_label( $subscription->get_period(), $bill_times ); |
|
314 | + } |
|
315 | 315 | |
316 | - // Trial periods. |
|
317 | - if ( $subscription->has_trial_period() ) { |
|
316 | + // Trial periods. |
|
317 | + if ( $subscription->has_trial_period() ) { |
|
318 | 318 | |
319 | - $trial_period = getpaid_get_subscription_trial_period_period( $subscription->get_trial_period() ); |
|
320 | - $trial_interval = getpaid_get_subscription_trial_period_interval( $subscription->get_trial_period() ); |
|
319 | + $trial_period = getpaid_get_subscription_trial_period_period( $subscription->get_trial_period() ); |
|
320 | + $trial_interval = getpaid_get_subscription_trial_period_interval( $subscription->get_trial_period() ); |
|
321 | 321 | |
322 | - if ( empty( $bill_times ) ) { |
|
322 | + if ( empty( $bill_times ) ) { |
|
323 | 323 | |
324 | - return sprintf( |
|
324 | + return sprintf( |
|
325 | 325 | |
326 | - // translators: $1: is the initial amount, $2: is the trial period, $3: is the recurring amount, $4: is the recurring period |
|
327 | - _x( '%1$s trial for %2$s then %3$s / %4$s', 'Subscription amount. (e.g.: $10 trial for 1 month then $120 / year)', 'invoicing' ), |
|
328 | - $initial, |
|
329 | - getpaid_get_subscription_period_label( $trial_period, $trial_interval ), |
|
330 | - $recurring, |
|
331 | - $period |
|
326 | + // translators: $1: is the initial amount, $2: is the trial period, $3: is the recurring amount, $4: is the recurring period |
|
327 | + _x( '%1$s trial for %2$s then %3$s / %4$s', 'Subscription amount. (e.g.: $10 trial for 1 month then $120 / year)', 'invoicing' ), |
|
328 | + $initial, |
|
329 | + getpaid_get_subscription_period_label( $trial_period, $trial_interval ), |
|
330 | + $recurring, |
|
331 | + $period |
|
332 | 332 | |
333 | - ); |
|
333 | + ); |
|
334 | 334 | |
335 | - } |
|
335 | + } |
|
336 | 336 | |
337 | - return sprintf( |
|
337 | + return sprintf( |
|
338 | 338 | |
339 | - // translators: $1: is the initial amount, $2: is the trial period, $3: is the recurring amount, $4: is the recurring period, $5: is the bill times |
|
340 | - _x( '%1$s trial for %2$s then %3$s / %4$s for %5$s', 'Subscription amount. (e.g.: $10 trial for 1 month then $120 / year for 4 years)', 'invoicing' ), |
|
341 | - $initial, |
|
342 | - getpaid_get_subscription_period_label( $trial_period, $trial_interval ), |
|
343 | - $recurring, |
|
344 | - $period, |
|
345 | - $bill_times |
|
346 | - ); |
|
339 | + // translators: $1: is the initial amount, $2: is the trial period, $3: is the recurring amount, $4: is the recurring period, $5: is the bill times |
|
340 | + _x( '%1$s trial for %2$s then %3$s / %4$s for %5$s', 'Subscription amount. (e.g.: $10 trial for 1 month then $120 / year for 4 years)', 'invoicing' ), |
|
341 | + $initial, |
|
342 | + getpaid_get_subscription_period_label( $trial_period, $trial_interval ), |
|
343 | + $recurring, |
|
344 | + $period, |
|
345 | + $bill_times |
|
346 | + ); |
|
347 | 347 | |
348 | - } |
|
348 | + } |
|
349 | 349 | |
350 | - if ( $initial != $recurring ) { |
|
350 | + if ( $initial != $recurring ) { |
|
351 | 351 | |
352 | - if ( empty( $bill_times ) ) { |
|
352 | + if ( empty( $bill_times ) ) { |
|
353 | 353 | |
354 | - return sprintf( |
|
354 | + return sprintf( |
|
355 | 355 | |
356 | - // translators: $1: is the initial amount, $2: is the recurring amount, $3: is the recurring period |
|
357 | - _x( 'Initial payment of %1$s which renews at %2$s / %3$s', 'Subscription amount. (e.g.:Initial payment of $100 which renews at $120 / year)', 'invoicing' ), |
|
358 | - $initial, |
|
359 | - $recurring, |
|
360 | - $period |
|
356 | + // translators: $1: is the initial amount, $2: is the recurring amount, $3: is the recurring period |
|
357 | + _x( 'Initial payment of %1$s which renews at %2$s / %3$s', 'Subscription amount. (e.g.:Initial payment of $100 which renews at $120 / year)', 'invoicing' ), |
|
358 | + $initial, |
|
359 | + $recurring, |
|
360 | + $period |
|
361 | 361 | |
362 | - ); |
|
362 | + ); |
|
363 | 363 | |
364 | - } |
|
364 | + } |
|
365 | 365 | |
366 | - return sprintf( |
|
366 | + return sprintf( |
|
367 | 367 | |
368 | - // translators: $1: is the initial amount, $2: is the recurring amount, $3: is the recurring period, $4: is the bill times |
|
369 | - _x( 'Initial payment of %1$s which renews at %2$s / %3$s for %4$s', 'Subscription amount. (e.g.:Initial payment of $100 which renews at $120 / year for 5 years)', 'invoicing' ), |
|
370 | - $initial, |
|
371 | - $recurring, |
|
372 | - $period, |
|
373 | - $bill_times |
|
368 | + // translators: $1: is the initial amount, $2: is the recurring amount, $3: is the recurring period, $4: is the bill times |
|
369 | + _x( 'Initial payment of %1$s which renews at %2$s / %3$s for %4$s', 'Subscription amount. (e.g.:Initial payment of $100 which renews at $120 / year for 5 years)', 'invoicing' ), |
|
370 | + $initial, |
|
371 | + $recurring, |
|
372 | + $period, |
|
373 | + $bill_times |
|
374 | 374 | |
375 | - ); |
|
375 | + ); |
|
376 | 376 | |
377 | - } |
|
377 | + } |
|
378 | 378 | |
379 | - if ( empty( $bill_times ) ) { |
|
379 | + if ( empty( $bill_times ) ) { |
|
380 | 380 | |
381 | - return sprintf( |
|
381 | + return sprintf( |
|
382 | 382 | |
383 | - // translators: $1: is the recurring amount, $2: is the recurring period |
|
384 | - _x( '%1$s / %2$s', 'Subscription amount. (e.g.: $120 / year)', 'invoicing' ), |
|
385 | - $initial, |
|
386 | - $period |
|
383 | + // translators: $1: is the recurring amount, $2: is the recurring period |
|
384 | + _x( '%1$s / %2$s', 'Subscription amount. (e.g.: $120 / year)', 'invoicing' ), |
|
385 | + $initial, |
|
386 | + $period |
|
387 | 387 | |
388 | - ); |
|
388 | + ); |
|
389 | 389 | |
390 | - } |
|
390 | + } |
|
391 | 391 | |
392 | - return sprintf( |
|
392 | + return sprintf( |
|
393 | 393 | |
394 | - // translators: $1: is the bill times, $2: is the recurring amount, $3: is the recurring period |
|
395 | - _x( '%2$s / %3$s for %1$s', 'Subscription amount. (e.g.: $120 / year for 5 years)', 'invoicing' ), |
|
396 | - $bill_times, |
|
397 | - $initial, |
|
398 | - $period |
|
394 | + // translators: $1: is the bill times, $2: is the recurring amount, $3: is the recurring period |
|
395 | + _x( '%2$s / %3$s for %1$s', 'Subscription amount. (e.g.: $120 / year for 5 years)', 'invoicing' ), |
|
396 | + $bill_times, |
|
397 | + $initial, |
|
398 | + $period |
|
399 | 399 | |
400 | - ); |
|
400 | + ); |
|
401 | 401 | |
402 | 402 | } |
403 | 403 | |
@@ -408,7 +408,7 @@ discard block |
||
408 | 408 | * @return WPInv_Subscription|bool |
409 | 409 | */ |
410 | 410 | function getpaid_get_invoice_subscription( $invoice ) { |
411 | - return getpaid_subscriptions()->get_invoice_subscription( $invoice ); |
|
411 | + return getpaid_subscriptions()->get_invoice_subscription( $invoice ); |
|
412 | 412 | } |
413 | 413 | |
414 | 414 | /** |
@@ -417,10 +417,10 @@ discard block |
||
417 | 417 | * @param WPInv_Invoice $invoice |
418 | 418 | */ |
419 | 419 | function getpaid_activate_invoice_subscription( $invoice ) { |
420 | - $subscription = getpaid_get_invoice_subscription( $invoice ); |
|
421 | - if ( is_a( $subscription, 'WPInv_Subscription' ) ) { |
|
422 | - $subscription->activate(); |
|
423 | - } |
|
420 | + $subscription = getpaid_get_invoice_subscription( $invoice ); |
|
421 | + if ( is_a( $subscription, 'WPInv_Subscription' ) ) { |
|
422 | + $subscription->activate(); |
|
423 | + } |
|
424 | 424 | } |
425 | 425 | |
426 | 426 | /** |
@@ -429,7 +429,7 @@ discard block |
||
429 | 429 | * @return WPInv_Subscriptions |
430 | 430 | */ |
431 | 431 | function getpaid_subscriptions() { |
432 | - return getpaid()->get( 'subscriptions' ); |
|
432 | + return getpaid()->get( 'subscriptions' ); |
|
433 | 433 | } |
434 | 434 | |
435 | 435 | /** |
@@ -448,15 +448,15 @@ discard block |
||
448 | 448 | return false; |
449 | 449 | } |
450 | 450 | |
451 | - // Fetch the invoice subscription. |
|
452 | - $subscription = getpaid_get_subscriptions( |
|
453 | - array( |
|
454 | - 'invoice_in' => $invoice->is_renewal() ? $invoice->get_parent_id() : $invoice->get_id(), |
|
455 | - 'number' => 1, |
|
456 | - ) |
|
457 | - ); |
|
451 | + // Fetch the invoice subscription. |
|
452 | + $subscription = getpaid_get_subscriptions( |
|
453 | + array( |
|
454 | + 'invoice_in' => $invoice->is_renewal() ? $invoice->get_parent_id() : $invoice->get_id(), |
|
455 | + 'number' => 1, |
|
456 | + ) |
|
457 | + ); |
|
458 | 458 | |
459 | - return empty( $subscription ) ? false : $subscription[0]; |
|
459 | + return empty( $subscription ) ? false : $subscription[0]; |
|
460 | 460 | |
461 | 461 | } |
462 | 462 | |
@@ -473,48 +473,48 @@ discard block |
||
473 | 473 | */ |
474 | 474 | function getpaid_get_recurring_item_key( $cart_item ) { |
475 | 475 | |
476 | - $cart_key = 'renews_'; |
|
477 | - $interval = $cart_item->get_recurring_interval(); |
|
478 | - $period = $cart_item->get_recurring_period( true ); |
|
479 | - $length = $cart_item->get_recurring_limit() * $interval; |
|
480 | - $trial_period = $cart_item->get_trial_period( true ); |
|
481 | - $trial_length = $cart_item->get_trial_interval(); |
|
482 | - |
|
483 | - // First start with the billing interval and period |
|
484 | - switch ( $interval ) { |
|
485 | - case 1 : |
|
486 | - if ( 'day' == $period ) { |
|
487 | - $cart_key .= 'daily'; |
|
488 | - } else { |
|
489 | - $cart_key .= sprintf( '%sly', $period ); |
|
490 | - } |
|
491 | - break; |
|
492 | - case 2 : |
|
493 | - $cart_key .= sprintf( 'every_2nd_%s', $period ); |
|
494 | - break; |
|
495 | - case 3 : |
|
496 | - $cart_key .= sprintf( 'every_3rd_%s', $period ); |
|
497 | - break; |
|
498 | - default: |
|
499 | - $cart_key .= sprintf( 'every_%dth_%s', $interval, $period ); |
|
500 | - break; |
|
501 | - } |
|
502 | - |
|
503 | - // Maybe add the optional maximum billing periods... |
|
504 | - if ( $length > 0 ) { |
|
505 | - $cart_key .= '_for_'; |
|
506 | - $cart_key .= sprintf( '%d_%s', $length, $period ); |
|
507 | - if ( $length > 1 ) { |
|
508 | - $cart_key .= 's'; |
|
509 | - } |
|
510 | - } |
|
511 | - |
|
512 | - // And an optional free trial. |
|
513 | - if ( $cart_item->has_free_trial() ) { |
|
514 | - $cart_key .= sprintf( '_after_a_%d_%s_trial', $trial_length, $trial_period ); |
|
515 | - } |
|
516 | - |
|
517 | - return apply_filters( 'getpaid_get_recurring_item_key', $cart_key, $cart_item ); |
|
476 | + $cart_key = 'renews_'; |
|
477 | + $interval = $cart_item->get_recurring_interval(); |
|
478 | + $period = $cart_item->get_recurring_period( true ); |
|
479 | + $length = $cart_item->get_recurring_limit() * $interval; |
|
480 | + $trial_period = $cart_item->get_trial_period( true ); |
|
481 | + $trial_length = $cart_item->get_trial_interval(); |
|
482 | + |
|
483 | + // First start with the billing interval and period |
|
484 | + switch ( $interval ) { |
|
485 | + case 1 : |
|
486 | + if ( 'day' == $period ) { |
|
487 | + $cart_key .= 'daily'; |
|
488 | + } else { |
|
489 | + $cart_key .= sprintf( '%sly', $period ); |
|
490 | + } |
|
491 | + break; |
|
492 | + case 2 : |
|
493 | + $cart_key .= sprintf( 'every_2nd_%s', $period ); |
|
494 | + break; |
|
495 | + case 3 : |
|
496 | + $cart_key .= sprintf( 'every_3rd_%s', $period ); |
|
497 | + break; |
|
498 | + default: |
|
499 | + $cart_key .= sprintf( 'every_%dth_%s', $interval, $period ); |
|
500 | + break; |
|
501 | + } |
|
502 | + |
|
503 | + // Maybe add the optional maximum billing periods... |
|
504 | + if ( $length > 0 ) { |
|
505 | + $cart_key .= '_for_'; |
|
506 | + $cart_key .= sprintf( '%d_%s', $length, $period ); |
|
507 | + if ( $length > 1 ) { |
|
508 | + $cart_key .= 's'; |
|
509 | + } |
|
510 | + } |
|
511 | + |
|
512 | + // And an optional free trial. |
|
513 | + if ( $cart_item->has_free_trial() ) { |
|
514 | + $cart_key .= sprintf( '_after_a_%d_%s_trial', $trial_length, $trial_period ); |
|
515 | + } |
|
516 | + |
|
517 | + return apply_filters( 'getpaid_get_recurring_item_key', $cart_key, $cart_item ); |
|
518 | 518 | } |
519 | 519 | |
520 | 520 | /** |
@@ -525,17 +525,17 @@ discard block |
||
525 | 525 | */ |
526 | 526 | function getpaid_get_subscription_groups( $invoice ) { |
527 | 527 | |
528 | - // Generate subscription groups. |
|
529 | - $subscription_groups = array(); |
|
530 | - foreach ( $invoice->get_items() as $item ) { |
|
528 | + // Generate subscription groups. |
|
529 | + $subscription_groups = array(); |
|
530 | + foreach ( $invoice->get_items() as $item ) { |
|
531 | 531 | |
532 | - if ( $item->is_recurring() ) { |
|
533 | - $subscription_groups[ getpaid_get_recurring_item_key( $item ) ][] = $item; |
|
534 | - } |
|
532 | + if ( $item->is_recurring() ) { |
|
533 | + $subscription_groups[ getpaid_get_recurring_item_key( $item ) ][] = $item; |
|
534 | + } |
|
535 | 535 | |
536 | - } |
|
536 | + } |
|
537 | 537 | |
538 | - return $subscription_groups; |
|
538 | + return $subscription_groups; |
|
539 | 539 | } |
540 | 540 | |
541 | 541 | /** |
@@ -549,57 +549,57 @@ discard block |
||
549 | 549 | */ |
550 | 550 | function getpaid_calculate_subscription_totals( $invoice ) { |
551 | 551 | |
552 | - // Generate subscription groups. |
|
553 | - $subscription_groups = getpaid_get_subscription_groups( $invoice ); |
|
552 | + // Generate subscription groups. |
|
553 | + $subscription_groups = getpaid_get_subscription_groups( $invoice ); |
|
554 | 554 | |
555 | - // Now let's calculate the totals for each group of subscriptions |
|
556 | - $subscription_totals = array(); |
|
555 | + // Now let's calculate the totals for each group of subscriptions |
|
556 | + $subscription_totals = array(); |
|
557 | 557 | |
558 | - foreach ( $subscription_groups as $subscription_key => $items ) { |
|
558 | + foreach ( $subscription_groups as $subscription_key => $items ) { |
|
559 | 559 | |
560 | - if ( empty( $subscription_totals[ $subscription_key ] ) ) { |
|
560 | + if ( empty( $subscription_totals[ $subscription_key ] ) ) { |
|
561 | 561 | |
562 | - $subscription_totals[ $subscription_key ] = array( |
|
563 | - 'initial_total' => 0, |
|
564 | - 'recurring_total' => 0, |
|
565 | - 'items' => array(), |
|
566 | - 'trialling' => false, |
|
567 | - ); |
|
562 | + $subscription_totals[ $subscription_key ] = array( |
|
563 | + 'initial_total' => 0, |
|
564 | + 'recurring_total' => 0, |
|
565 | + 'items' => array(), |
|
566 | + 'trialling' => false, |
|
567 | + ); |
|
568 | 568 | |
569 | - } |
|
569 | + } |
|
570 | 570 | |
571 | - /** |
|
572 | - * Get the totals of the group. |
|
573 | - * @var GetPaid_Form_Item $item |
|
574 | - */ |
|
575 | - foreach ( $items as $item ) { |
|
571 | + /** |
|
572 | + * Get the totals of the group. |
|
573 | + * @var GetPaid_Form_Item $item |
|
574 | + */ |
|
575 | + foreach ( $items as $item ) { |
|
576 | 576 | |
577 | - $subscription_totals[ $subscription_key ]['items'][$item->get_id()] = $item->prepare_data_for_saving(); |
|
578 | - $subscription_totals[ $subscription_key ]['item_id'] = $item->get_id(); |
|
579 | - $subscription_totals[ $subscription_key ]['period'] = $item->get_recurring_period( true ); |
|
580 | - $subscription_totals[ $subscription_key ]['interval'] = $item->get_recurring_interval(); |
|
581 | - $subscription_totals[ $subscription_key ]['initial_total'] += $item->get_sub_total(); |
|
582 | - $subscription_totals[ $subscription_key ]['recurring_total'] += $item->get_recurring_sub_total(); |
|
583 | - $subscription_totals[ $subscription_key ]['recurring_limit'] = $item->get_recurring_limit(); |
|
577 | + $subscription_totals[ $subscription_key ]['items'][$item->get_id()] = $item->prepare_data_for_saving(); |
|
578 | + $subscription_totals[ $subscription_key ]['item_id'] = $item->get_id(); |
|
579 | + $subscription_totals[ $subscription_key ]['period'] = $item->get_recurring_period( true ); |
|
580 | + $subscription_totals[ $subscription_key ]['interval'] = $item->get_recurring_interval(); |
|
581 | + $subscription_totals[ $subscription_key ]['initial_total'] += $item->get_sub_total(); |
|
582 | + $subscription_totals[ $subscription_key ]['recurring_total'] += $item->get_recurring_sub_total(); |
|
583 | + $subscription_totals[ $subscription_key ]['recurring_limit'] = $item->get_recurring_limit(); |
|
584 | 584 | |
585 | - // Calculate the next renewal date. |
|
586 | - $period = $item->get_recurring_period( true ); |
|
587 | - $interval = $item->get_recurring_interval(); |
|
585 | + // Calculate the next renewal date. |
|
586 | + $period = $item->get_recurring_period( true ); |
|
587 | + $interval = $item->get_recurring_interval(); |
|
588 | 588 | |
589 | - // If the subscription item has a trial period... |
|
590 | - if ( $item->has_free_trial() ) { |
|
591 | - $period = $item->get_trial_period( true ); |
|
592 | - $interval = $item->get_trial_interval(); |
|
593 | - $subscription_totals[ $subscription_key ]['trialling'] = $interval . ' ' . $period; |
|
594 | - } |
|
589 | + // If the subscription item has a trial period... |
|
590 | + if ( $item->has_free_trial() ) { |
|
591 | + $period = $item->get_trial_period( true ); |
|
592 | + $interval = $item->get_trial_interval(); |
|
593 | + $subscription_totals[ $subscription_key ]['trialling'] = $interval . ' ' . $period; |
|
594 | + } |
|
595 | 595 | |
596 | - $subscription_totals[ $subscription_key ]['renews_on'] = date( 'Y-m-d H:i:s', strtotime( "+$interval $period", current_time( 'timestamp' ) ) ); |
|
596 | + $subscription_totals[ $subscription_key ]['renews_on'] = date( 'Y-m-d H:i:s', strtotime( "+$interval $period", current_time( 'timestamp' ) ) ); |
|
597 | 597 | |
598 | - } |
|
598 | + } |
|
599 | 599 | |
600 | - } |
|
600 | + } |
|
601 | 601 | |
602 | - return apply_filters( 'getpaid_calculate_subscription_totals', $subscription_totals, $invoice ); |
|
602 | + return apply_filters( 'getpaid_calculate_subscription_totals', $subscription_totals, $invoice ); |
|
603 | 603 | } |
604 | 604 | |
605 | 605 | /** |
@@ -610,15 +610,15 @@ discard block |
||
610 | 610 | */ |
611 | 611 | function getpaid_should_group_subscriptions( $invoice ) { |
612 | 612 | |
613 | - $recurring_items = 0; |
|
613 | + $recurring_items = 0; |
|
614 | 614 | |
615 | - foreach ( $invoice->get_items() as $item ) { |
|
615 | + foreach ( $invoice->get_items() as $item ) { |
|
616 | 616 | |
617 | - if ( $item->is_recurring() ) { |
|
618 | - $recurring_items ++; |
|
619 | - } |
|
617 | + if ( $item->is_recurring() ) { |
|
618 | + $recurring_items ++; |
|
619 | + } |
|
620 | 620 | |
621 | - } |
|
621 | + } |
|
622 | 622 | |
623 | - return apply_filters( 'getpaid_should_group_subscriptions', $recurring_items > 1, $invoice ); |
|
623 | + return apply_filters( 'getpaid_should_group_subscriptions', $recurring_items > 1, $invoice ); |
|
624 | 624 | } |
@@ -6,7 +6,7 @@ discard block |
||
6 | 6 | if ( ! defined( 'ABSPATH' ) ) exit; |
7 | 7 | |
8 | 8 | if ( ! class_exists( 'WP_List_Table' ) ) { |
9 | - include_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; |
|
9 | + include_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; |
|
10 | 10 | } |
11 | 11 | |
12 | 12 | /** |
@@ -14,406 +14,406 @@ discard block |
||
14 | 14 | */ |
15 | 15 | class WPInv_Subscriptions_List_Table extends WP_List_Table { |
16 | 16 | |
17 | - /** |
|
18 | - * URL of this page |
|
19 | - * |
|
20 | - * @var string |
|
21 | - * @since 1.0.19 |
|
22 | - */ |
|
23 | - public $base_url; |
|
24 | - |
|
25 | - /** |
|
26 | - * Query |
|
27 | - * |
|
28 | - * @var GetPaid_Subscriptions_Query |
|
29 | - * @since 1.0.19 |
|
30 | - */ |
|
31 | - public $query; |
|
32 | - |
|
33 | - /** |
|
34 | - * Total subscriptions |
|
35 | - * |
|
36 | - * @var string |
|
37 | - * @since 1.0.0 |
|
38 | - */ |
|
39 | - public $total_count; |
|
40 | - |
|
41 | - /** |
|
42 | - * Current status subscriptions |
|
43 | - * |
|
44 | - * @var string |
|
45 | - * @since 1.0.0 |
|
46 | - */ |
|
47 | - public $current_total_count; |
|
48 | - |
|
49 | - /** |
|
50 | - * Status counts |
|
51 | - * |
|
52 | - * @var array |
|
53 | - * @since 1.0.19 |
|
54 | - */ |
|
55 | - public $status_counts; |
|
56 | - |
|
57 | - /** |
|
58 | - * Number of results to show per page |
|
59 | - * |
|
60 | - * @var int |
|
61 | - * @since 1.0.0 |
|
62 | - */ |
|
63 | - public $per_page = 10; |
|
64 | - |
|
65 | - /** |
|
66 | - * Constructor function. |
|
67 | - */ |
|
68 | - public function __construct() { |
|
69 | - |
|
70 | - parent::__construct( |
|
71 | - array( |
|
72 | - 'singular' => 'subscription', |
|
73 | - 'plural' => 'subscriptions', |
|
74 | - ) |
|
75 | - ); |
|
76 | - |
|
77 | - $this->process_bulk_action(); |
|
78 | - |
|
79 | - $this->prepare_query(); |
|
80 | - |
|
81 | - $this->base_url = remove_query_arg( 'status' ); |
|
82 | - |
|
83 | - } |
|
84 | - |
|
85 | - /** |
|
86 | - * Prepares the display query |
|
87 | - */ |
|
88 | - public function prepare_query() { |
|
89 | - |
|
90 | - // Prepare query args. |
|
91 | - $query = array( |
|
92 | - 'number' => $this->per_page, |
|
93 | - 'paged' => $this->get_paged(), |
|
94 | - 'status' => ( isset( $_GET['status'] ) && array_key_exists( $_GET['status'], getpaid_get_subscription_statuses() ) ) ? $_GET['status'] : 'all', |
|
95 | - 'orderby' => ( isset( $_GET['orderby'] ) ) ? $_GET['orderby'] : 'id', |
|
96 | - 'order' => ( isset( $_GET['order'] ) ) ? $_GET['order'] : 'DESC', |
|
97 | - ); |
|
98 | - |
|
99 | - // Prepare class properties. |
|
100 | - $this->query = new GetPaid_Subscriptions_Query( $query ); |
|
101 | - $this->total_count = $this->query->get_total(); |
|
102 | - $this->current_total_count = $this->query->get_total(); |
|
103 | - $this->items = $this->query->get_results(); |
|
104 | - $this->status_counts = getpaid_get_subscription_status_counts( $query ); |
|
105 | - |
|
106 | - if ( 'all' != $query['status'] ) { |
|
107 | - unset( $query['status'] ); |
|
108 | - $this->total_count = getpaid_get_subscriptions( $query, 'count' ); |
|
109 | - } |
|
110 | - |
|
111 | - } |
|
112 | - |
|
113 | - /** |
|
114 | - * Gets the list of views available on this table. |
|
115 | - * |
|
116 | - * The format is an associative array: |
|
117 | - * - `'id' => 'link'` |
|
118 | - * |
|
119 | - * @since 1.0.0 |
|
120 | - * |
|
121 | - * @return array |
|
122 | - */ |
|
123 | - public function get_views() { |
|
124 | - |
|
125 | - $current = isset( $_GET['status'] ) ? $_GET['status'] : 'all'; |
|
126 | - $views = array( |
|
127 | - |
|
128 | - 'all' => sprintf( |
|
129 | - '<a href="%s" %s>%s <span class="count">(%d)</span></a>', |
|
130 | - esc_url( add_query_arg( 'status', false, $this->base_url ) ), |
|
131 | - $current === 'all' ? ' class="current"' : '', |
|
132 | - __('All','invoicing' ), |
|
133 | - $this->total_count |
|
134 | - ) |
|
135 | - |
|
136 | - ); |
|
137 | - |
|
138 | - foreach ( array_filter( $this->status_counts ) as $status => $count ) { |
|
139 | - |
|
140 | - $views[ $status ] = sprintf( |
|
141 | - '<a href="%s" %s>%s <span class="count">(%d)</span></a>', |
|
142 | - esc_url( add_query_arg( 'status', urlencode( $status ), $this->base_url ) ), |
|
143 | - $current === $status ? ' class="current"' : '', |
|
144 | - sanitize_text_field( getpaid_get_subscription_status_label( $status ) ), |
|
145 | - $count |
|
146 | - ); |
|
147 | - |
|
148 | - } |
|
149 | - |
|
150 | - return $views; |
|
151 | - |
|
152 | - } |
|
153 | - |
|
154 | - /** |
|
155 | - * Render most columns |
|
156 | - * |
|
157 | - * @access private |
|
158 | - * @since 1.0.0 |
|
159 | - * @return string |
|
160 | - */ |
|
161 | - public function column_default( $item, $column_name ) { |
|
162 | - return apply_filters( "getpaid_subscriptions_table_column_$column_name", $item->$column_name ); |
|
163 | - } |
|
164 | - |
|
165 | - /** |
|
166 | - * This is how checkbox column renders. |
|
167 | - * |
|
168 | - * @param WPInv_Subscription $item |
|
169 | - * @return string |
|
170 | - */ |
|
171 | - public function column_cb( $item ) { |
|
172 | - return sprintf( '<input type="checkbox" name="id[]" value="%s" />', esc_html( $item->get_id() ) ); |
|
173 | - } |
|
174 | - |
|
175 | - /** |
|
176 | - * Status column |
|
177 | - * |
|
178 | - * @param WPInv_Subscription $item |
|
179 | - * @since 1.0.0 |
|
180 | - * @return string |
|
181 | - */ |
|
182 | - public function column_status( $item ) { |
|
183 | - return $item->get_status_label_html(); |
|
184 | - } |
|
185 | - |
|
186 | - /** |
|
187 | - * Subscription column |
|
188 | - * |
|
189 | - * @param WPInv_Subscription $item |
|
190 | - * @since 1.0.0 |
|
191 | - * @return string |
|
192 | - */ |
|
193 | - public function column_subscription( $item ) { |
|
194 | - |
|
195 | - $username = __( '(Missing User)', 'invoicing' ); |
|
196 | - |
|
197 | - $user = get_userdata( $item->get_customer_id() ); |
|
198 | - if ( $user ) { |
|
199 | - |
|
200 | - $username = sprintf( |
|
201 | - '<a href="user-edit.php?user_id=%s">%s</a>', |
|
202 | - absint( $user->ID ), |
|
203 | - ! empty( $user->display_name ) ? sanitize_text_field( $user->display_name ) : sanitize_email( $user->user_email ) |
|
204 | - ); |
|
205 | - |
|
206 | - } |
|
207 | - |
|
208 | - // translators: $1: is opening link, $2: is subscription id number, $3: is closing link tag, $4: is user's name |
|
209 | - $column_content = sprintf( |
|
210 | - _x( '%1$s#%2$s%3$s for %4$s', 'Subscription title on admin table. (e.g.: #211 for John Doe)', 'invoicing' ), |
|
211 | - '<a href="' . esc_url( admin_url( 'admin.php?page=wpinv-subscriptions&id=' . absint( $item->get_id() ) ) ) . '">', |
|
212 | - '<strong>' . esc_attr( $item->get_id() ) . '</strong>', '</a>', |
|
213 | - $username |
|
214 | - ); |
|
215 | - |
|
216 | - $row_actions = array(); |
|
217 | - |
|
218 | - // View subscription. |
|
219 | - $view_url = esc_url( add_query_arg( 'id', $item->get_id(), admin_url( 'admin.php?page=wpinv-subscriptions' ) )); |
|
220 | - $row_actions['view'] = '<a href="' . $view_url . '">' . __( 'View Subscription', 'invoicing' ) . '</a>'; |
|
221 | - |
|
222 | - // View invoice. |
|
223 | - $invoice = get_post( $item->get_parent_invoice_id() ); |
|
224 | - |
|
225 | - if ( ! empty( $invoice ) ) { |
|
226 | - $invoice_url = get_edit_post_link( $invoice ); |
|
227 | - $row_actions['invoice'] = '<a href="' . $invoice_url . '">' . __( 'View Invoice', 'invoicing' ) . '</a>'; |
|
228 | - } |
|
229 | - |
|
230 | - $delete_url = esc_url( |
|
231 | - wp_nonce_url( |
|
232 | - add_query_arg( |
|
233 | - array( |
|
234 | - 'getpaid-admin-action' => 'subscription_manual_delete', |
|
235 | - 'id' => $item->get_id(), |
|
236 | - ) |
|
237 | - ), |
|
238 | - 'getpaid-nonce', |
|
239 | - 'getpaid-nonce' |
|
240 | - ) |
|
241 | - ); |
|
242 | - $row_actions['delete'] = '<a class="text-danger" href="' . $delete_url . '">' . __( 'Delete Subscription', 'invoicing' ) . '</a>'; |
|
243 | - |
|
244 | - $row_actions = $this->row_actions( apply_filters( 'getpaid_subscription_table_row_actions', $row_actions, $item ) ); |
|
245 | - |
|
246 | - return "<strong>$column_content</strong>" . $this->column_amount( $item ) . $row_actions; |
|
247 | - } |
|
248 | - |
|
249 | - /** |
|
250 | - * Renewal date column |
|
251 | - * |
|
252 | - * @param WPInv_Subscription $item |
|
253 | - * @since 1.0.0 |
|
254 | - * @return string |
|
255 | - */ |
|
256 | - public function column_renewal_date( $item ) { |
|
257 | - return getpaid_format_date_value( $item->get_expiration() ); |
|
258 | - } |
|
259 | - |
|
260 | - /** |
|
261 | - * Start date column |
|
262 | - * |
|
263 | - * @param WPInv_Subscription $item |
|
264 | - * @since 1.0.0 |
|
265 | - * @return string |
|
266 | - */ |
|
267 | - public function column_start_date( $item ) { |
|
268 | - return getpaid_format_date_value( $item->get_date_created() ); |
|
269 | - } |
|
270 | - |
|
271 | - /** |
|
272 | - * Amount column |
|
273 | - * |
|
274 | - * @param WPInv_Subscription $item |
|
275 | - * @since 1.0.19 |
|
276 | - * @return string |
|
277 | - */ |
|
278 | - public function column_amount( $item ) { |
|
279 | - $amount = getpaid_get_formatted_subscription_amount( $item ); |
|
280 | - return "<span class='text-muted form-text mt-2 mb-2'>$amount</span>"; |
|
281 | - } |
|
282 | - |
|
283 | - /** |
|
284 | - * Billing Times column |
|
285 | - * |
|
286 | - * @param WPInv_Subscription $item |
|
287 | - * @since 1.0.0 |
|
288 | - * @return string |
|
289 | - */ |
|
290 | - public function column_renewals( $item ) { |
|
291 | - $max_bills = $item->get_bill_times(); |
|
292 | - return $item->get_times_billed() . ' / ' . ( empty( $max_bills ) ? "∞" : $max_bills ); |
|
293 | - } |
|
294 | - |
|
295 | - /** |
|
296 | - * Product ID column |
|
297 | - * |
|
298 | - * @param WPInv_Subscription $item |
|
299 | - * @since 1.0.0 |
|
300 | - * @return string |
|
301 | - */ |
|
302 | - public function column_item( $item ) { |
|
303 | - $subscription_group = getpaid_get_invoice_subscription_group( $item->get_parent_invoice_id(), $item->get_id() ); |
|
304 | - |
|
305 | - if ( empty( $subscription_group ) ) { |
|
306 | - return $this->generate_item_markup( $item->get_product_id() ); |
|
307 | - } |
|
308 | - |
|
309 | - $markup = array_map( array( $this, 'generate_item_markup' ), array_keys( $subscription_group['items'] ) ); |
|
310 | - return implode( ' | ', $markup ); |
|
311 | - |
|
312 | - } |
|
313 | - |
|
314 | - /** |
|
315 | - * Generates the items markup. |
|
316 | - * |
|
317 | - * @param int $item_id |
|
318 | - * @since 1.0.0 |
|
319 | - * @return string |
|
320 | - */ |
|
321 | - public function generate_item_markup( $item_id ) { |
|
322 | - $item = get_post( $item_id ); |
|
323 | - |
|
324 | - if ( ! empty( $item ) ) { |
|
325 | - $link = get_edit_post_link( $item ); |
|
326 | - $link = esc_url( $link ); |
|
327 | - $name = esc_html( get_the_title( $item ) ); |
|
328 | - return "<a href='$link'>$name</a>"; |
|
329 | - } else { |
|
330 | - return sprintf( __( 'Item #%s', 'invoicing' ), $item_id ); |
|
331 | - } |
|
332 | - |
|
333 | - } |
|
334 | - |
|
335 | - /** |
|
336 | - * Retrieve the current page number |
|
337 | - * |
|
338 | - * @return int |
|
339 | - */ |
|
340 | - public function get_paged() { |
|
341 | - return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1; |
|
342 | - } |
|
343 | - |
|
344 | - /** |
|
345 | - * Setup the final data for the table |
|
346 | - * |
|
347 | - */ |
|
348 | - public function prepare_items() { |
|
349 | - |
|
350 | - $columns = $this->get_columns(); |
|
351 | - $hidden = array(); |
|
352 | - $sortable = $this->get_sortable_columns(); |
|
353 | - |
|
354 | - $this->_column_headers = array( $columns, $hidden, $sortable ); |
|
355 | - |
|
356 | - $this->set_pagination_args( |
|
357 | - array( |
|
358 | - 'total_items' => $this->current_total_count, |
|
359 | - 'per_page' => $this->per_page, |
|
360 | - 'total_pages' => ceil( $this->current_total_count / $this->per_page ) |
|
361 | - ) |
|
362 | - ); |
|
363 | - } |
|
364 | - |
|
365 | - /** |
|
366 | - * Table columns |
|
367 | - * |
|
368 | - * @return array |
|
369 | - */ |
|
370 | - public function get_columns(){ |
|
371 | - $columns = array( |
|
372 | - 'cb' => '<input type="checkbox" />', |
|
373 | - 'subscription' => __( 'Subscription', 'invoicing' ), |
|
374 | - 'start_date' => __( 'Start Date', 'invoicing' ), |
|
375 | - 'renewal_date' => __( 'Next Payment', 'invoicing' ), |
|
376 | - 'renewals' => __( 'Payments', 'invoicing' ), |
|
377 | - 'item' => __( 'Items', 'invoicing' ), |
|
378 | - 'status' => __( 'Status', 'invoicing' ), |
|
379 | - ); |
|
380 | - |
|
381 | - return apply_filters( 'manage_getpaid_subscriptions_table_columns', $columns ); |
|
382 | - } |
|
383 | - |
|
384 | - /** |
|
385 | - * Sortable table columns. |
|
386 | - * |
|
387 | - * @return array |
|
388 | - */ |
|
389 | - public function get_sortable_columns() { |
|
390 | - $sortable = array( |
|
391 | - 'subscription' => array( 'id', true ), |
|
392 | - 'start_date' => array( 'created', true ), |
|
393 | - 'renewal_date' => array( 'expiration', true ), |
|
394 | - 'renewals' => array( 'bill_times', true ), |
|
395 | - 'item' => array( 'product_id', true ), |
|
396 | - 'status' => array( 'status', true ), |
|
397 | - ); |
|
398 | - |
|
399 | - return apply_filters( 'manage_getpaid_subscriptions_sortable_table_columns', $sortable ); |
|
400 | - } |
|
401 | - |
|
402 | - /** |
|
403 | - * Whether the table has items to display or not |
|
404 | - * |
|
405 | - * @return bool |
|
406 | - */ |
|
407 | - public function has_items() { |
|
408 | - return ! empty( $this->current_total_count ); |
|
409 | - } |
|
410 | - |
|
411 | - /** |
|
412 | - * Processes bulk actions. |
|
413 | - * |
|
414 | - */ |
|
415 | - public function process_bulk_action() { |
|
416 | - |
|
417 | - } |
|
17 | + /** |
|
18 | + * URL of this page |
|
19 | + * |
|
20 | + * @var string |
|
21 | + * @since 1.0.19 |
|
22 | + */ |
|
23 | + public $base_url; |
|
24 | + |
|
25 | + /** |
|
26 | + * Query |
|
27 | + * |
|
28 | + * @var GetPaid_Subscriptions_Query |
|
29 | + * @since 1.0.19 |
|
30 | + */ |
|
31 | + public $query; |
|
32 | + |
|
33 | + /** |
|
34 | + * Total subscriptions |
|
35 | + * |
|
36 | + * @var string |
|
37 | + * @since 1.0.0 |
|
38 | + */ |
|
39 | + public $total_count; |
|
40 | + |
|
41 | + /** |
|
42 | + * Current status subscriptions |
|
43 | + * |
|
44 | + * @var string |
|
45 | + * @since 1.0.0 |
|
46 | + */ |
|
47 | + public $current_total_count; |
|
48 | + |
|
49 | + /** |
|
50 | + * Status counts |
|
51 | + * |
|
52 | + * @var array |
|
53 | + * @since 1.0.19 |
|
54 | + */ |
|
55 | + public $status_counts; |
|
56 | + |
|
57 | + /** |
|
58 | + * Number of results to show per page |
|
59 | + * |
|
60 | + * @var int |
|
61 | + * @since 1.0.0 |
|
62 | + */ |
|
63 | + public $per_page = 10; |
|
64 | + |
|
65 | + /** |
|
66 | + * Constructor function. |
|
67 | + */ |
|
68 | + public function __construct() { |
|
69 | + |
|
70 | + parent::__construct( |
|
71 | + array( |
|
72 | + 'singular' => 'subscription', |
|
73 | + 'plural' => 'subscriptions', |
|
74 | + ) |
|
75 | + ); |
|
76 | + |
|
77 | + $this->process_bulk_action(); |
|
78 | + |
|
79 | + $this->prepare_query(); |
|
80 | + |
|
81 | + $this->base_url = remove_query_arg( 'status' ); |
|
82 | + |
|
83 | + } |
|
84 | + |
|
85 | + /** |
|
86 | + * Prepares the display query |
|
87 | + */ |
|
88 | + public function prepare_query() { |
|
89 | + |
|
90 | + // Prepare query args. |
|
91 | + $query = array( |
|
92 | + 'number' => $this->per_page, |
|
93 | + 'paged' => $this->get_paged(), |
|
94 | + 'status' => ( isset( $_GET['status'] ) && array_key_exists( $_GET['status'], getpaid_get_subscription_statuses() ) ) ? $_GET['status'] : 'all', |
|
95 | + 'orderby' => ( isset( $_GET['orderby'] ) ) ? $_GET['orderby'] : 'id', |
|
96 | + 'order' => ( isset( $_GET['order'] ) ) ? $_GET['order'] : 'DESC', |
|
97 | + ); |
|
98 | + |
|
99 | + // Prepare class properties. |
|
100 | + $this->query = new GetPaid_Subscriptions_Query( $query ); |
|
101 | + $this->total_count = $this->query->get_total(); |
|
102 | + $this->current_total_count = $this->query->get_total(); |
|
103 | + $this->items = $this->query->get_results(); |
|
104 | + $this->status_counts = getpaid_get_subscription_status_counts( $query ); |
|
105 | + |
|
106 | + if ( 'all' != $query['status'] ) { |
|
107 | + unset( $query['status'] ); |
|
108 | + $this->total_count = getpaid_get_subscriptions( $query, 'count' ); |
|
109 | + } |
|
110 | + |
|
111 | + } |
|
112 | + |
|
113 | + /** |
|
114 | + * Gets the list of views available on this table. |
|
115 | + * |
|
116 | + * The format is an associative array: |
|
117 | + * - `'id' => 'link'` |
|
118 | + * |
|
119 | + * @since 1.0.0 |
|
120 | + * |
|
121 | + * @return array |
|
122 | + */ |
|
123 | + public function get_views() { |
|
124 | + |
|
125 | + $current = isset( $_GET['status'] ) ? $_GET['status'] : 'all'; |
|
126 | + $views = array( |
|
127 | + |
|
128 | + 'all' => sprintf( |
|
129 | + '<a href="%s" %s>%s <span class="count">(%d)</span></a>', |
|
130 | + esc_url( add_query_arg( 'status', false, $this->base_url ) ), |
|
131 | + $current === 'all' ? ' class="current"' : '', |
|
132 | + __('All','invoicing' ), |
|
133 | + $this->total_count |
|
134 | + ) |
|
135 | + |
|
136 | + ); |
|
137 | + |
|
138 | + foreach ( array_filter( $this->status_counts ) as $status => $count ) { |
|
139 | + |
|
140 | + $views[ $status ] = sprintf( |
|
141 | + '<a href="%s" %s>%s <span class="count">(%d)</span></a>', |
|
142 | + esc_url( add_query_arg( 'status', urlencode( $status ), $this->base_url ) ), |
|
143 | + $current === $status ? ' class="current"' : '', |
|
144 | + sanitize_text_field( getpaid_get_subscription_status_label( $status ) ), |
|
145 | + $count |
|
146 | + ); |
|
147 | + |
|
148 | + } |
|
149 | + |
|
150 | + return $views; |
|
151 | + |
|
152 | + } |
|
153 | + |
|
154 | + /** |
|
155 | + * Render most columns |
|
156 | + * |
|
157 | + * @access private |
|
158 | + * @since 1.0.0 |
|
159 | + * @return string |
|
160 | + */ |
|
161 | + public function column_default( $item, $column_name ) { |
|
162 | + return apply_filters( "getpaid_subscriptions_table_column_$column_name", $item->$column_name ); |
|
163 | + } |
|
164 | + |
|
165 | + /** |
|
166 | + * This is how checkbox column renders. |
|
167 | + * |
|
168 | + * @param WPInv_Subscription $item |
|
169 | + * @return string |
|
170 | + */ |
|
171 | + public function column_cb( $item ) { |
|
172 | + return sprintf( '<input type="checkbox" name="id[]" value="%s" />', esc_html( $item->get_id() ) ); |
|
173 | + } |
|
174 | + |
|
175 | + /** |
|
176 | + * Status column |
|
177 | + * |
|
178 | + * @param WPInv_Subscription $item |
|
179 | + * @since 1.0.0 |
|
180 | + * @return string |
|
181 | + */ |
|
182 | + public function column_status( $item ) { |
|
183 | + return $item->get_status_label_html(); |
|
184 | + } |
|
185 | + |
|
186 | + /** |
|
187 | + * Subscription column |
|
188 | + * |
|
189 | + * @param WPInv_Subscription $item |
|
190 | + * @since 1.0.0 |
|
191 | + * @return string |
|
192 | + */ |
|
193 | + public function column_subscription( $item ) { |
|
194 | + |
|
195 | + $username = __( '(Missing User)', 'invoicing' ); |
|
196 | + |
|
197 | + $user = get_userdata( $item->get_customer_id() ); |
|
198 | + if ( $user ) { |
|
199 | + |
|
200 | + $username = sprintf( |
|
201 | + '<a href="user-edit.php?user_id=%s">%s</a>', |
|
202 | + absint( $user->ID ), |
|
203 | + ! empty( $user->display_name ) ? sanitize_text_field( $user->display_name ) : sanitize_email( $user->user_email ) |
|
204 | + ); |
|
205 | + |
|
206 | + } |
|
207 | + |
|
208 | + // translators: $1: is opening link, $2: is subscription id number, $3: is closing link tag, $4: is user's name |
|
209 | + $column_content = sprintf( |
|
210 | + _x( '%1$s#%2$s%3$s for %4$s', 'Subscription title on admin table. (e.g.: #211 for John Doe)', 'invoicing' ), |
|
211 | + '<a href="' . esc_url( admin_url( 'admin.php?page=wpinv-subscriptions&id=' . absint( $item->get_id() ) ) ) . '">', |
|
212 | + '<strong>' . esc_attr( $item->get_id() ) . '</strong>', '</a>', |
|
213 | + $username |
|
214 | + ); |
|
215 | + |
|
216 | + $row_actions = array(); |
|
217 | + |
|
218 | + // View subscription. |
|
219 | + $view_url = esc_url( add_query_arg( 'id', $item->get_id(), admin_url( 'admin.php?page=wpinv-subscriptions' ) )); |
|
220 | + $row_actions['view'] = '<a href="' . $view_url . '">' . __( 'View Subscription', 'invoicing' ) . '</a>'; |
|
221 | + |
|
222 | + // View invoice. |
|
223 | + $invoice = get_post( $item->get_parent_invoice_id() ); |
|
224 | + |
|
225 | + if ( ! empty( $invoice ) ) { |
|
226 | + $invoice_url = get_edit_post_link( $invoice ); |
|
227 | + $row_actions['invoice'] = '<a href="' . $invoice_url . '">' . __( 'View Invoice', 'invoicing' ) . '</a>'; |
|
228 | + } |
|
229 | + |
|
230 | + $delete_url = esc_url( |
|
231 | + wp_nonce_url( |
|
232 | + add_query_arg( |
|
233 | + array( |
|
234 | + 'getpaid-admin-action' => 'subscription_manual_delete', |
|
235 | + 'id' => $item->get_id(), |
|
236 | + ) |
|
237 | + ), |
|
238 | + 'getpaid-nonce', |
|
239 | + 'getpaid-nonce' |
|
240 | + ) |
|
241 | + ); |
|
242 | + $row_actions['delete'] = '<a class="text-danger" href="' . $delete_url . '">' . __( 'Delete Subscription', 'invoicing' ) . '</a>'; |
|
243 | + |
|
244 | + $row_actions = $this->row_actions( apply_filters( 'getpaid_subscription_table_row_actions', $row_actions, $item ) ); |
|
245 | + |
|
246 | + return "<strong>$column_content</strong>" . $this->column_amount( $item ) . $row_actions; |
|
247 | + } |
|
248 | + |
|
249 | + /** |
|
250 | + * Renewal date column |
|
251 | + * |
|
252 | + * @param WPInv_Subscription $item |
|
253 | + * @since 1.0.0 |
|
254 | + * @return string |
|
255 | + */ |
|
256 | + public function column_renewal_date( $item ) { |
|
257 | + return getpaid_format_date_value( $item->get_expiration() ); |
|
258 | + } |
|
259 | + |
|
260 | + /** |
|
261 | + * Start date column |
|
262 | + * |
|
263 | + * @param WPInv_Subscription $item |
|
264 | + * @since 1.0.0 |
|
265 | + * @return string |
|
266 | + */ |
|
267 | + public function column_start_date( $item ) { |
|
268 | + return getpaid_format_date_value( $item->get_date_created() ); |
|
269 | + } |
|
270 | + |
|
271 | + /** |
|
272 | + * Amount column |
|
273 | + * |
|
274 | + * @param WPInv_Subscription $item |
|
275 | + * @since 1.0.19 |
|
276 | + * @return string |
|
277 | + */ |
|
278 | + public function column_amount( $item ) { |
|
279 | + $amount = getpaid_get_formatted_subscription_amount( $item ); |
|
280 | + return "<span class='text-muted form-text mt-2 mb-2'>$amount</span>"; |
|
281 | + } |
|
282 | + |
|
283 | + /** |
|
284 | + * Billing Times column |
|
285 | + * |
|
286 | + * @param WPInv_Subscription $item |
|
287 | + * @since 1.0.0 |
|
288 | + * @return string |
|
289 | + */ |
|
290 | + public function column_renewals( $item ) { |
|
291 | + $max_bills = $item->get_bill_times(); |
|
292 | + return $item->get_times_billed() . ' / ' . ( empty( $max_bills ) ? "∞" : $max_bills ); |
|
293 | + } |
|
294 | + |
|
295 | + /** |
|
296 | + * Product ID column |
|
297 | + * |
|
298 | + * @param WPInv_Subscription $item |
|
299 | + * @since 1.0.0 |
|
300 | + * @return string |
|
301 | + */ |
|
302 | + public function column_item( $item ) { |
|
303 | + $subscription_group = getpaid_get_invoice_subscription_group( $item->get_parent_invoice_id(), $item->get_id() ); |
|
304 | + |
|
305 | + if ( empty( $subscription_group ) ) { |
|
306 | + return $this->generate_item_markup( $item->get_product_id() ); |
|
307 | + } |
|
308 | + |
|
309 | + $markup = array_map( array( $this, 'generate_item_markup' ), array_keys( $subscription_group['items'] ) ); |
|
310 | + return implode( ' | ', $markup ); |
|
311 | + |
|
312 | + } |
|
313 | + |
|
314 | + /** |
|
315 | + * Generates the items markup. |
|
316 | + * |
|
317 | + * @param int $item_id |
|
318 | + * @since 1.0.0 |
|
319 | + * @return string |
|
320 | + */ |
|
321 | + public function generate_item_markup( $item_id ) { |
|
322 | + $item = get_post( $item_id ); |
|
323 | + |
|
324 | + if ( ! empty( $item ) ) { |
|
325 | + $link = get_edit_post_link( $item ); |
|
326 | + $link = esc_url( $link ); |
|
327 | + $name = esc_html( get_the_title( $item ) ); |
|
328 | + return "<a href='$link'>$name</a>"; |
|
329 | + } else { |
|
330 | + return sprintf( __( 'Item #%s', 'invoicing' ), $item_id ); |
|
331 | + } |
|
332 | + |
|
333 | + } |
|
334 | + |
|
335 | + /** |
|
336 | + * Retrieve the current page number |
|
337 | + * |
|
338 | + * @return int |
|
339 | + */ |
|
340 | + public function get_paged() { |
|
341 | + return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1; |
|
342 | + } |
|
343 | + |
|
344 | + /** |
|
345 | + * Setup the final data for the table |
|
346 | + * |
|
347 | + */ |
|
348 | + public function prepare_items() { |
|
349 | + |
|
350 | + $columns = $this->get_columns(); |
|
351 | + $hidden = array(); |
|
352 | + $sortable = $this->get_sortable_columns(); |
|
353 | + |
|
354 | + $this->_column_headers = array( $columns, $hidden, $sortable ); |
|
355 | + |
|
356 | + $this->set_pagination_args( |
|
357 | + array( |
|
358 | + 'total_items' => $this->current_total_count, |
|
359 | + 'per_page' => $this->per_page, |
|
360 | + 'total_pages' => ceil( $this->current_total_count / $this->per_page ) |
|
361 | + ) |
|
362 | + ); |
|
363 | + } |
|
364 | + |
|
365 | + /** |
|
366 | + * Table columns |
|
367 | + * |
|
368 | + * @return array |
|
369 | + */ |
|
370 | + public function get_columns(){ |
|
371 | + $columns = array( |
|
372 | + 'cb' => '<input type="checkbox" />', |
|
373 | + 'subscription' => __( 'Subscription', 'invoicing' ), |
|
374 | + 'start_date' => __( 'Start Date', 'invoicing' ), |
|
375 | + 'renewal_date' => __( 'Next Payment', 'invoicing' ), |
|
376 | + 'renewals' => __( 'Payments', 'invoicing' ), |
|
377 | + 'item' => __( 'Items', 'invoicing' ), |
|
378 | + 'status' => __( 'Status', 'invoicing' ), |
|
379 | + ); |
|
380 | + |
|
381 | + return apply_filters( 'manage_getpaid_subscriptions_table_columns', $columns ); |
|
382 | + } |
|
383 | + |
|
384 | + /** |
|
385 | + * Sortable table columns. |
|
386 | + * |
|
387 | + * @return array |
|
388 | + */ |
|
389 | + public function get_sortable_columns() { |
|
390 | + $sortable = array( |
|
391 | + 'subscription' => array( 'id', true ), |
|
392 | + 'start_date' => array( 'created', true ), |
|
393 | + 'renewal_date' => array( 'expiration', true ), |
|
394 | + 'renewals' => array( 'bill_times', true ), |
|
395 | + 'item' => array( 'product_id', true ), |
|
396 | + 'status' => array( 'status', true ), |
|
397 | + ); |
|
398 | + |
|
399 | + return apply_filters( 'manage_getpaid_subscriptions_sortable_table_columns', $sortable ); |
|
400 | + } |
|
401 | + |
|
402 | + /** |
|
403 | + * Whether the table has items to display or not |
|
404 | + * |
|
405 | + * @return bool |
|
406 | + */ |
|
407 | + public function has_items() { |
|
408 | + return ! empty( $this->current_total_count ); |
|
409 | + } |
|
410 | + |
|
411 | + /** |
|
412 | + * Processes bulk actions. |
|
413 | + * |
|
414 | + */ |
|
415 | + public function process_bulk_action() { |
|
416 | + |
|
417 | + } |
|
418 | 418 | |
419 | 419 | } |
@@ -12,267 +12,267 @@ |
||
12 | 12 | */ |
13 | 13 | class GetPaid_Metaboxes { |
14 | 14 | |
15 | - /** |
|
16 | - * Only save metaboxes once. |
|
17 | - * |
|
18 | - * @var boolean |
|
19 | - */ |
|
20 | - private static $saved_meta_boxes = false; |
|
21 | - |
|
22 | 15 | /** |
23 | - * Hook in methods. |
|
24 | - */ |
|
25 | - public static function init() { |
|
26 | - |
|
27 | - // Register metaboxes. |
|
28 | - add_action( 'add_meta_boxes', 'GetPaid_Metaboxes::add_meta_boxes', 5, 2 ); |
|
29 | - |
|
30 | - // Remove metaboxes. |
|
31 | - add_action( 'add_meta_boxes', 'GetPaid_Metaboxes::remove_meta_boxes', 30 ); |
|
32 | - |
|
33 | - // Rename metaboxes. |
|
34 | - add_action( 'add_meta_boxes', 'GetPaid_Metaboxes::rename_meta_boxes', 45 ); |
|
35 | - |
|
36 | - // Save metaboxes. |
|
37 | - add_action( 'save_post', 'GetPaid_Metaboxes::save_meta_boxes', 1, 2 ); |
|
38 | - } |
|
16 | + * Only save metaboxes once. |
|
17 | + * |
|
18 | + * @var boolean |
|
19 | + */ |
|
20 | + private static $saved_meta_boxes = false; |
|
39 | 21 | |
40 | - /** |
|
41 | - * Register core metaboxes. |
|
42 | - */ |
|
43 | - public static function add_meta_boxes( $post_type, $post ) { |
|
44 | - |
|
45 | - // For invoices... |
|
46 | - self::add_invoice_meta_boxes( $post_type, $post ); |
|
22 | + /** |
|
23 | + * Hook in methods. |
|
24 | + */ |
|
25 | + public static function init() { |
|
47 | 26 | |
48 | - // For payment forms. |
|
49 | - self::add_payment_form_meta_boxes( $post_type ); |
|
27 | + // Register metaboxes. |
|
28 | + add_action( 'add_meta_boxes', 'GetPaid_Metaboxes::add_meta_boxes', 5, 2 ); |
|
50 | 29 | |
51 | - // For invoice items. |
|
52 | - self::add_item_meta_boxes( $post_type ); |
|
30 | + // Remove metaboxes. |
|
31 | + add_action( 'add_meta_boxes', 'GetPaid_Metaboxes::remove_meta_boxes', 30 ); |
|
53 | 32 | |
54 | - // For invoice discounts. |
|
55 | - if ( $post_type == 'wpi_discount' ) { |
|
56 | - add_meta_box( 'wpinv_discount_details', __( 'Discount Details', 'invoicing' ), 'GetPaid_Meta_Box_Discount_Details::output', 'wpi_discount', 'normal', 'high' ); |
|
57 | - } |
|
33 | + // Rename metaboxes. |
|
34 | + add_action( 'add_meta_boxes', 'GetPaid_Metaboxes::rename_meta_boxes', 45 ); |
|
58 | 35 | |
59 | - } |
|
36 | + // Save metaboxes. |
|
37 | + add_action( 'save_post', 'GetPaid_Metaboxes::save_meta_boxes', 1, 2 ); |
|
38 | + } |
|
60 | 39 | |
61 | - /** |
|
62 | - * Register core metaboxes. |
|
63 | - */ |
|
64 | - protected static function add_payment_form_meta_boxes( $post_type ) { |
|
40 | + /** |
|
41 | + * Register core metaboxes. |
|
42 | + */ |
|
43 | + public static function add_meta_boxes( $post_type, $post ) { |
|
65 | 44 | |
66 | - // For payment forms. |
|
67 | - if ( $post_type == 'wpi_payment_form' ) { |
|
45 | + // For invoices... |
|
46 | + self::add_invoice_meta_boxes( $post_type, $post ); |
|
68 | 47 | |
69 | - // Design payment form. |
|
70 | - add_meta_box( 'wpinv-payment-form-design', __( 'Payment Form', 'invoicing' ), 'GetPaid_Meta_Box_Payment_Form::output', 'wpi_payment_form', 'normal' ); |
|
48 | + // For payment forms. |
|
49 | + self::add_payment_form_meta_boxes( $post_type ); |
|
71 | 50 | |
72 | - // Payment form information. |
|
73 | - add_meta_box( 'wpinv-payment-form-info', __( 'Details', 'invoicing' ), 'GetPaid_Meta_Box_Payment_Form_Info::output', 'wpi_payment_form', 'side' ); |
|
51 | + // For invoice items. |
|
52 | + self::add_item_meta_boxes( $post_type ); |
|
74 | 53 | |
75 | - } |
|
54 | + // For invoice discounts. |
|
55 | + if ( $post_type == 'wpi_discount' ) { |
|
56 | + add_meta_box( 'wpinv_discount_details', __( 'Discount Details', 'invoicing' ), 'GetPaid_Meta_Box_Discount_Details::output', 'wpi_discount', 'normal', 'high' ); |
|
57 | + } |
|
76 | 58 | |
77 | - } |
|
59 | + } |
|
78 | 60 | |
79 | - /** |
|
80 | - * Register core metaboxes. |
|
81 | - */ |
|
82 | - protected static function add_item_meta_boxes( $post_type ) { |
|
61 | + /** |
|
62 | + * Register core metaboxes. |
|
63 | + */ |
|
64 | + protected static function add_payment_form_meta_boxes( $post_type ) { |
|
83 | 65 | |
84 | - if ( $post_type == 'wpi_item' ) { |
|
66 | + // For payment forms. |
|
67 | + if ( $post_type == 'wpi_payment_form' ) { |
|
85 | 68 | |
86 | - // Item details. |
|
87 | - add_meta_box( 'wpinv_item_details', __( 'Item Details', 'invoicing' ), 'GetPaid_Meta_Box_Item_Details::output', 'wpi_item', 'normal', 'high' ); |
|
69 | + // Design payment form. |
|
70 | + add_meta_box( 'wpinv-payment-form-design', __( 'Payment Form', 'invoicing' ), 'GetPaid_Meta_Box_Payment_Form::output', 'wpi_payment_form', 'normal' ); |
|
88 | 71 | |
89 | - // If taxes are enabled, register the tax metabox. |
|
90 | - if ( wpinv_use_taxes() ) { |
|
91 | - add_meta_box( 'wpinv_item_vat', __( 'Tax', 'invoicing' ), 'GetPaid_Meta_Box_Item_VAT::output', 'wpi_item', 'normal', 'high' ); |
|
92 | - } |
|
72 | + // Payment form information. |
|
73 | + add_meta_box( 'wpinv-payment-form-info', __( 'Details', 'invoicing' ), 'GetPaid_Meta_Box_Payment_Form_Info::output', 'wpi_payment_form', 'side' ); |
|
93 | 74 | |
94 | - // Item info. |
|
95 | - add_meta_box( 'wpinv_field_item_info', __( 'Item info', 'invoicing' ), 'GetPaid_Meta_Box_Item_Info::output', 'wpi_item', 'side', 'core' ); |
|
75 | + } |
|
96 | 76 | |
97 | - } |
|
77 | + } |
|
98 | 78 | |
99 | - } |
|
79 | + /** |
|
80 | + * Register core metaboxes. |
|
81 | + */ |
|
82 | + protected static function add_item_meta_boxes( $post_type ) { |
|
100 | 83 | |
101 | - /** |
|
102 | - * Register invoice metaboxes. |
|
103 | - */ |
|
104 | - protected static function add_invoice_meta_boxes( $post_type, $post ) { |
|
84 | + if ( $post_type == 'wpi_item' ) { |
|
105 | 85 | |
106 | - // For invoices... |
|
107 | - if ( getpaid_is_invoice_post_type( $post_type ) ) { |
|
108 | - $invoice = new WPInv_Invoice( $post ); |
|
86 | + // Item details. |
|
87 | + add_meta_box( 'wpinv_item_details', __( 'Item Details', 'invoicing' ), 'GetPaid_Meta_Box_Item_Details::output', 'wpi_item', 'normal', 'high' ); |
|
109 | 88 | |
110 | - // Resend invoice. |
|
111 | - if ( ! $invoice->is_draft() ) { |
|
89 | + // If taxes are enabled, register the tax metabox. |
|
90 | + if ( wpinv_use_taxes() ) { |
|
91 | + add_meta_box( 'wpinv_item_vat', __( 'Tax', 'invoicing' ), 'GetPaid_Meta_Box_Item_VAT::output', 'wpi_item', 'normal', 'high' ); |
|
92 | + } |
|
112 | 93 | |
113 | - add_meta_box( |
|
114 | - 'wpinv-mb-resend-invoice', |
|
115 | - sprintf( |
|
116 | - __( 'Resend %s', 'invoicing' ), |
|
117 | - ucfirst( $invoice->get_invoice_quote_type() ) |
|
118 | - ), |
|
119 | - 'GetPaid_Meta_Box_Resend_Invoice::output', |
|
120 | - $post_type, |
|
121 | - 'side', |
|
122 | - 'low' |
|
123 | - ); |
|
94 | + // Item info. |
|
95 | + add_meta_box( 'wpinv_field_item_info', __( 'Item info', 'invoicing' ), 'GetPaid_Meta_Box_Item_Info::output', 'wpi_item', 'side', 'core' ); |
|
124 | 96 | |
125 | - } |
|
97 | + } |
|
126 | 98 | |
127 | - // Subscriptions. |
|
128 | - $subscription = getpaid_get_invoice_subscription( $invoice ); |
|
129 | - if ( ! empty( $subscription ) && $subscription->exists() ) { |
|
130 | - add_meta_box( 'wpinv-mb-subscriptions', __( 'Subscription Details', 'invoicing' ), 'GetPaid_Meta_Box_Invoice_Subscription::output', $post_type, 'advanced' ); |
|
131 | - add_meta_box( 'wpinv-mb-subscription-invoices', __( 'Related Payments', 'invoicing' ), 'GetPaid_Meta_Box_Invoice_Subscription::output_invoices', $post_type, 'advanced' ); |
|
132 | - } |
|
133 | - |
|
134 | - // Invoice details. |
|
135 | - add_meta_box( |
|
136 | - 'wpinv-details', |
|
137 | - sprintf( |
|
138 | - __( '%s Details', 'invoicing' ), |
|
139 | - ucfirst( $invoice->get_invoice_quote_type() ) |
|
140 | - ), |
|
141 | - 'GetPaid_Meta_Box_Invoice_Details::output', |
|
142 | - $post_type, |
|
143 | - 'side' |
|
144 | - ); |
|
145 | - |
|
146 | - // Payment details. |
|
147 | - if ( ! $invoice->is_draft() ) { |
|
148 | - add_meta_box( 'wpinv-payment-meta', __( 'Payment Meta', 'invoicing' ), 'GetPaid_Meta_Box_Invoice_Payment_Meta::output', $post_type, 'side', 'default' ); |
|
149 | - } |
|
99 | + } |
|
150 | 100 | |
151 | - // Billing details. |
|
152 | - add_meta_box( 'wpinv-address', __( 'Billing Details', 'invoicing' ), 'GetPaid_Meta_Box_Invoice_Address::output', $post_type, 'normal', 'high' ); |
|
101 | + /** |
|
102 | + * Register invoice metaboxes. |
|
103 | + */ |
|
104 | + protected static function add_invoice_meta_boxes( $post_type, $post ) { |
|
105 | + |
|
106 | + // For invoices... |
|
107 | + if ( getpaid_is_invoice_post_type( $post_type ) ) { |
|
108 | + $invoice = new WPInv_Invoice( $post ); |
|
109 | + |
|
110 | + // Resend invoice. |
|
111 | + if ( ! $invoice->is_draft() ) { |
|
112 | + |
|
113 | + add_meta_box( |
|
114 | + 'wpinv-mb-resend-invoice', |
|
115 | + sprintf( |
|
116 | + __( 'Resend %s', 'invoicing' ), |
|
117 | + ucfirst( $invoice->get_invoice_quote_type() ) |
|
118 | + ), |
|
119 | + 'GetPaid_Meta_Box_Resend_Invoice::output', |
|
120 | + $post_type, |
|
121 | + 'side', |
|
122 | + 'low' |
|
123 | + ); |
|
124 | + |
|
125 | + } |
|
126 | + |
|
127 | + // Subscriptions. |
|
128 | + $subscription = getpaid_get_invoice_subscription( $invoice ); |
|
129 | + if ( ! empty( $subscription ) && $subscription->exists() ) { |
|
130 | + add_meta_box( 'wpinv-mb-subscriptions', __( 'Subscription Details', 'invoicing' ), 'GetPaid_Meta_Box_Invoice_Subscription::output', $post_type, 'advanced' ); |
|
131 | + add_meta_box( 'wpinv-mb-subscription-invoices', __( 'Related Payments', 'invoicing' ), 'GetPaid_Meta_Box_Invoice_Subscription::output_invoices', $post_type, 'advanced' ); |
|
132 | + } |
|
133 | + |
|
134 | + // Invoice details. |
|
135 | + add_meta_box( |
|
136 | + 'wpinv-details', |
|
137 | + sprintf( |
|
138 | + __( '%s Details', 'invoicing' ), |
|
139 | + ucfirst( $invoice->get_invoice_quote_type() ) |
|
140 | + ), |
|
141 | + 'GetPaid_Meta_Box_Invoice_Details::output', |
|
142 | + $post_type, |
|
143 | + 'side' |
|
144 | + ); |
|
145 | + |
|
146 | + // Payment details. |
|
147 | + if ( ! $invoice->is_draft() ) { |
|
148 | + add_meta_box( 'wpinv-payment-meta', __( 'Payment Meta', 'invoicing' ), 'GetPaid_Meta_Box_Invoice_Payment_Meta::output', $post_type, 'side', 'default' ); |
|
149 | + } |
|
150 | + |
|
151 | + // Billing details. |
|
152 | + add_meta_box( 'wpinv-address', __( 'Billing Details', 'invoicing' ), 'GetPaid_Meta_Box_Invoice_Address::output', $post_type, 'normal', 'high' ); |
|
153 | 153 | |
154 | - // Invoice items. |
|
155 | - add_meta_box( |
|
156 | - 'wpinv-items', |
|
157 | - sprintf( |
|
158 | - __( '%s Items', 'invoicing' ), |
|
159 | - ucfirst( $invoice->get_invoice_quote_type() ) |
|
160 | - ), |
|
161 | - 'GetPaid_Meta_Box_Invoice_Items::output', |
|
162 | - $post_type, |
|
163 | - 'normal', |
|
164 | - 'high' |
|
165 | - ); |
|
154 | + // Invoice items. |
|
155 | + add_meta_box( |
|
156 | + 'wpinv-items', |
|
157 | + sprintf( |
|
158 | + __( '%s Items', 'invoicing' ), |
|
159 | + ucfirst( $invoice->get_invoice_quote_type() ) |
|
160 | + ), |
|
161 | + 'GetPaid_Meta_Box_Invoice_Items::output', |
|
162 | + $post_type, |
|
163 | + 'normal', |
|
164 | + 'high' |
|
165 | + ); |
|
166 | 166 | |
167 | - // Invoice notes. |
|
168 | - add_meta_box( |
|
169 | - 'wpinv-notes', |
|
170 | - sprintf( |
|
171 | - __( '%s Notes', 'invoicing' ), |
|
172 | - ucfirst( $invoice->get_invoice_quote_type() ) |
|
173 | - ), |
|
174 | - 'WPInv_Meta_Box_Notes::output', |
|
175 | - $post_type, |
|
176 | - 'side', |
|
177 | - 'low' |
|
178 | - ); |
|
179 | - |
|
180 | - // Shipping Address. |
|
181 | - if ( get_post_meta( $invoice->get_id(), 'shipping_address', true ) ) { |
|
182 | - add_meta_box( 'wpinv-invoice-shipping-details', __( 'Shipping Address', 'invoicing' ), 'GetPaid_Meta_Box_Invoice_Shipping_Address::output', $post_type, 'side', 'high' ); |
|
183 | - } |
|
184 | - |
|
185 | - // Payment form information. |
|
186 | - if ( get_post_meta( $invoice->get_id(), 'payment_form_data', true ) ) { |
|
187 | - add_meta_box( 'wpinv-invoice-payment-form-details', __( 'Payment Form Details', 'invoicing' ), 'WPInv_Meta_Box_Payment_Form::output_details', $post_type, 'side', 'high' ); |
|
188 | - } |
|
189 | - |
|
190 | - } |
|
191 | - |
|
192 | - } |
|
193 | - |
|
194 | - /** |
|
195 | - * Remove some metaboxes. |
|
196 | - */ |
|
197 | - public static function remove_meta_boxes() { |
|
198 | - remove_meta_box( 'wpseo_meta', 'wpi_invoice', 'normal' ); |
|
199 | - } |
|
200 | - |
|
201 | - /** |
|
202 | - * Rename other metaboxes. |
|
203 | - */ |
|
204 | - public static function rename_meta_boxes() { |
|
167 | + // Invoice notes. |
|
168 | + add_meta_box( |
|
169 | + 'wpinv-notes', |
|
170 | + sprintf( |
|
171 | + __( '%s Notes', 'invoicing' ), |
|
172 | + ucfirst( $invoice->get_invoice_quote_type() ) |
|
173 | + ), |
|
174 | + 'WPInv_Meta_Box_Notes::output', |
|
175 | + $post_type, |
|
176 | + 'side', |
|
177 | + 'low' |
|
178 | + ); |
|
179 | + |
|
180 | + // Shipping Address. |
|
181 | + if ( get_post_meta( $invoice->get_id(), 'shipping_address', true ) ) { |
|
182 | + add_meta_box( 'wpinv-invoice-shipping-details', __( 'Shipping Address', 'invoicing' ), 'GetPaid_Meta_Box_Invoice_Shipping_Address::output', $post_type, 'side', 'high' ); |
|
183 | + } |
|
184 | + |
|
185 | + // Payment form information. |
|
186 | + if ( get_post_meta( $invoice->get_id(), 'payment_form_data', true ) ) { |
|
187 | + add_meta_box( 'wpinv-invoice-payment-form-details', __( 'Payment Form Details', 'invoicing' ), 'WPInv_Meta_Box_Payment_Form::output_details', $post_type, 'side', 'high' ); |
|
188 | + } |
|
189 | + |
|
190 | + } |
|
191 | + |
|
192 | + } |
|
193 | + |
|
194 | + /** |
|
195 | + * Remove some metaboxes. |
|
196 | + */ |
|
197 | + public static function remove_meta_boxes() { |
|
198 | + remove_meta_box( 'wpseo_meta', 'wpi_invoice', 'normal' ); |
|
199 | + } |
|
200 | + |
|
201 | + /** |
|
202 | + * Rename other metaboxes. |
|
203 | + */ |
|
204 | + public static function rename_meta_boxes() { |
|
205 | 205 | |
206 | - } |
|
207 | - |
|
208 | - /** |
|
209 | - * Check if we're saving, then trigger an action based on the post type. |
|
210 | - * |
|
211 | - * @param int $post_id Post ID. |
|
212 | - * @param object $post Post object. |
|
213 | - */ |
|
214 | - public static function save_meta_boxes( $post_id, $post ) { |
|
215 | - $post_id = absint( $post_id ); |
|
216 | - $data = wp_unslash( $_POST ); |
|
217 | - |
|
218 | - // Do not save for ajax requests. |
|
219 | - if ( ( defined( 'DOING_AJAX') && DOING_AJAX ) || isset( $_REQUEST['bulk_edit'] ) ) { |
|
220 | - return; |
|
221 | - } |
|
222 | - |
|
223 | - // $post_id and $post are required |
|
224 | - if ( empty( $post_id ) || empty( $post ) || self::$saved_meta_boxes ) { |
|
225 | - return; |
|
226 | - } |
|
227 | - |
|
228 | - // Dont' save meta boxes for revisions or autosaves. |
|
229 | - if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || is_int( wp_is_post_revision( $post ) ) || is_int( wp_is_post_autosave( $post ) ) ) { |
|
230 | - return; |
|
231 | - } |
|
232 | - |
|
233 | - // Check the nonce. |
|
234 | - if ( empty( $data['getpaid_meta_nonce'] ) || ! wp_verify_nonce( $data['getpaid_meta_nonce'], 'getpaid_meta_nonce' ) ) { |
|
235 | - return; |
|
236 | - } |
|
237 | - |
|
238 | - // Check the post being saved == the $post_id to prevent triggering this call for other save_post events. |
|
239 | - if ( empty( $data['post_ID'] ) || absint( $data['post_ID'] ) !== $post_id ) { |
|
240 | - return; |
|
241 | - } |
|
242 | - |
|
243 | - // Check user has permission to edit. |
|
244 | - if ( ! current_user_can( 'edit_post', $post_id ) ) { |
|
245 | - return; |
|
246 | - } |
|
247 | - |
|
248 | - if ( getpaid_is_invoice_post_type( $post->post_type ) ) { |
|
249 | - |
|
250 | - // We need this save event to run once to avoid potential endless loops. |
|
251 | - self::$saved_meta_boxes = true; |
|
252 | - |
|
253 | - return GetPaid_Meta_Box_Invoice_Address::save( $post_id ); |
|
254 | - |
|
255 | - } |
|
256 | - |
|
257 | - // Ensure this is our post type. |
|
258 | - $post_types_map = array( |
|
259 | - 'wpi_item' => 'GetPaid_Meta_Box_Item_Details', |
|
260 | - 'wpi_payment_form' => 'GetPaid_Meta_Box_Payment_Form', |
|
261 | - 'wpi_discount' => 'GetPaid_Meta_Box_Discount_Details', |
|
262 | - ); |
|
263 | - |
|
264 | - // Is this our post type? |
|
265 | - if ( ! isset( $post_types_map[ $post->post_type ] ) ) { |
|
266 | - return; |
|
267 | - } |
|
268 | - |
|
269 | - // We need this save event to run once to avoid potential endless loops. |
|
270 | - self::$saved_meta_boxes = true; |
|
206 | + } |
|
207 | + |
|
208 | + /** |
|
209 | + * Check if we're saving, then trigger an action based on the post type. |
|
210 | + * |
|
211 | + * @param int $post_id Post ID. |
|
212 | + * @param object $post Post object. |
|
213 | + */ |
|
214 | + public static function save_meta_boxes( $post_id, $post ) { |
|
215 | + $post_id = absint( $post_id ); |
|
216 | + $data = wp_unslash( $_POST ); |
|
217 | + |
|
218 | + // Do not save for ajax requests. |
|
219 | + if ( ( defined( 'DOING_AJAX') && DOING_AJAX ) || isset( $_REQUEST['bulk_edit'] ) ) { |
|
220 | + return; |
|
221 | + } |
|
222 | + |
|
223 | + // $post_id and $post are required |
|
224 | + if ( empty( $post_id ) || empty( $post ) || self::$saved_meta_boxes ) { |
|
225 | + return; |
|
226 | + } |
|
227 | + |
|
228 | + // Dont' save meta boxes for revisions or autosaves. |
|
229 | + if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || is_int( wp_is_post_revision( $post ) ) || is_int( wp_is_post_autosave( $post ) ) ) { |
|
230 | + return; |
|
231 | + } |
|
232 | + |
|
233 | + // Check the nonce. |
|
234 | + if ( empty( $data['getpaid_meta_nonce'] ) || ! wp_verify_nonce( $data['getpaid_meta_nonce'], 'getpaid_meta_nonce' ) ) { |
|
235 | + return; |
|
236 | + } |
|
237 | + |
|
238 | + // Check the post being saved == the $post_id to prevent triggering this call for other save_post events. |
|
239 | + if ( empty( $data['post_ID'] ) || absint( $data['post_ID'] ) !== $post_id ) { |
|
240 | + return; |
|
241 | + } |
|
242 | + |
|
243 | + // Check user has permission to edit. |
|
244 | + if ( ! current_user_can( 'edit_post', $post_id ) ) { |
|
245 | + return; |
|
246 | + } |
|
247 | + |
|
248 | + if ( getpaid_is_invoice_post_type( $post->post_type ) ) { |
|
249 | + |
|
250 | + // We need this save event to run once to avoid potential endless loops. |
|
251 | + self::$saved_meta_boxes = true; |
|
252 | + |
|
253 | + return GetPaid_Meta_Box_Invoice_Address::save( $post_id ); |
|
254 | + |
|
255 | + } |
|
256 | + |
|
257 | + // Ensure this is our post type. |
|
258 | + $post_types_map = array( |
|
259 | + 'wpi_item' => 'GetPaid_Meta_Box_Item_Details', |
|
260 | + 'wpi_payment_form' => 'GetPaid_Meta_Box_Payment_Form', |
|
261 | + 'wpi_discount' => 'GetPaid_Meta_Box_Discount_Details', |
|
262 | + ); |
|
263 | + |
|
264 | + // Is this our post type? |
|
265 | + if ( ! isset( $post_types_map[ $post->post_type ] ) ) { |
|
266 | + return; |
|
267 | + } |
|
268 | + |
|
269 | + // We need this save event to run once to avoid potential endless loops. |
|
270 | + self::$saved_meta_boxes = true; |
|
271 | 271 | |
272 | - // Save the post. |
|
273 | - $class = $post_types_map[ $post->post_type ]; |
|
274 | - $class::save( $post_id, $_POST, $post ); |
|
272 | + // Save the post. |
|
273 | + $class = $post_types_map[ $post->post_type ]; |
|
274 | + $class::save( $post_id, $_POST, $post ); |
|
275 | 275 | |
276 | - } |
|
276 | + } |
|
277 | 277 | |
278 | 278 | } |