PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 4.14.2
GiveWP – Donation Plugin and Fundraising Platform v4.14.2
4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 2.31.0 2.31.1 2.32.0 2.33.0 2.33.1 2.33.2 2.33.3 2.33.4 2.33.5 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.5.0 2.5.1 2.5.10 2.5.11 2.5.12 2.5.13 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.5.8 2.5.9 2.6.0 2.6.1 2.6.2 2.6.3 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.9.0 2.9.1 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.10.0 3.11.0 3.12.0 3.12.1 3.12.2 3.12.3 3.13.0 3.14.0 3.14.1 3.14.2 3.15.0 3.15.1 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.17.0 3.17.1 3.17.2 3.18.0 3.19.0 3.19.1 3.19.2 3.19.3 3.19.4 3.2.0 3.2.1 3.2.2 3.20.0 3.21.0 3.21.1 3.22.0 3.22.1 3.22.2 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.7.0 3.8.0 3.9.0 4.0.0 4.1.0 4.1.1 4.10.0 4.10.1 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.14.5 4.14.6 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.1 4.7.0 4.7.1 4.8.0 4.8.1 4.9.0 trunk 1.9.0 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.2 2.11.3 2.12.0 2.12.1 2.12.2 2.12.3 2.13.0 2.13.1 2.13.2 2.13.3 2.13.4 2.14.0 2.15.0 2.16.0 2.16.1 2.17.0 2.17.1 2.17.3 2.18.0 2.18.1 2.19.1 2.19.2 2.19.3 2.19.4 2.19.5 2.19.6 2.19.7 2.19.8 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.20.0 2.20.1 2.20.2 2.21.0 2.21.1 2.21.2 2.21.3 2.21.4 2.22.0 2.22.1 2.22.2 2.22.3 2.23.0 2.23.1 2.23.2 2.24.0 2.24.1 2.24.2 2.25.0 2.25.1 2.25.2 2.25.3 2.26.0 2.27.0 2.27.1 2.27.2 2.27.3 2.28.0 2.29.0 2.29.1 2.29.2
give / src / LegacySubscriptions / includes / give-subscriptions-api.php
give / src / LegacySubscriptions / includes Last commit date
give-recurring-cache.php 4 years ago give-recurring-cron.php 4 years ago give-recurring-db-subscription-meta.php 4 years ago give-recurring-helpers.php 4 years ago give-recurring-subscriber.php 3 years ago give-subscription.php 8 months ago give-subscriptions-api.php 4 years ago give-subscriptions-db.php 8 months ago
give-subscriptions-api.php
274 lines
1 <?php
2 /**
3 * Subscribers REST API
4 *
5 * @package Give
6 * @subpackage Subscriber API Class
7 * @copyright Copyright (c) 2017
8 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9 */
10
11 // Exit if accessed directly.
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit;
14 }
15
16 /**
17 * Give_Subscriptions_API
18 *
19 * Extends the Give_API to make the /subscriptions endpoint
20 *
21 * @class Give_Subscriptions_API
22 * @since 2.19.0 - migrated from give-recurring
23 * @since 1.4
24 */
25 class Give_Subscriptions_API extends Give_API {
26
27 /**
28 * User ID Performing the API Request
29 *
30 * @var int
31 */
32 public $user_id = 0;
33
34 /**
35 * @var bool
36 */
37 public $override = true;
38
39 /**
40 * Adds to the allowed query vars list from Give Core for API access
41 *
42 * @access public
43 * @since 2.4.3
44 * @author Topher
45 *
46 * @param array $vars Query vars.
47 *
48 * @return string[] $vars New query vars
49 */
50 public function query_vars( $vars ) {
51
52 $vars[] = 'status';
53
54 return $vars;
55 }
56
57 /**
58 * Safely gets the status from the URL
59 *
60 * @access private
61 * @since 2.4.3
62 * @author Topher
63 */
64 private function get_status_from_url() {
65
66 // Get the query vars.
67 global $wp_query;
68
69 $status = '';
70
71 $allowed = array(
72 'active',
73 'pending',
74 'failing',
75 'completed',
76 'cancelled',
77 'expired',
78 );
79
80 // Get status information from the input.
81 $input_status = isset( $wp_query->query_vars['status'] ) ? $wp_query->query_vars['status'] : null;
82
83 if ( in_array( $input_status, $allowed ) ) {
84 $status = $input_status;
85 }
86
87 if ( null !== $input_status && '' === $status ) {
88 $error['error'] = sprintf( __( '\'%s\' is not a valid status.', 'give' ), $input_status );
89
90 return $error;
91 } else {
92 return $status;
93 }
94 }
95
96
97 /**
98 * Give_Subscriptions_API constructor.
99 */
100 public function __construct() {
101 add_filter( 'give_api_valid_query_modes', array( $this, 'add_valid_subscriptions_query' ) );
102 add_filter( 'give_api_output_data', array( $this, 'add_give_subscription_endpoint' ), 10, 3 );
103 }
104
105 /**
106 * Add 'subscriptions' api endpoint.
107 *
108 * @param $queries
109 *
110 * @return array
111 */
112 public function add_valid_subscriptions_query( $queries ) {
113 $queries[] .= 'subscriptions';
114 $this->override = false;
115
116 return $queries;
117 }
118
119 /**
120 * Add Subscribers Endpoint
121 *
122 * @param $data
123 * @param $query_mode
124 * @param $api_object
125 *
126 * @return array $subscriptions
127 */
128 public function add_give_subscription_endpoint( $data, $query_mode, $api_object ) {
129
130 // Sanity check: don't mess with other API queries!
131 if ( 'subscriptions' !== $query_mode ) {
132 return $data;
133 }
134
135 // Get query vars.
136 global $wp_query;
137
138 // Get the status from input.
139 $status = $this->get_status_from_url();
140
141 if ( is_array( $status ) && array_key_exists( 'error', $status ) ) {
142 $error = $status;
143
144 return $error;
145 }
146
147 // Get the donor information from the input.
148 $queried_c = isset( $wp_query->query_vars['donor'] ) ? sanitize_text_field( $wp_query->query_vars['customer'] ) : null;
149 $donor = new Give_Donor( $queried_c );
150
151 if ( ! empty( $queried_c ) && ( ! $donor || ! $donor->id > 0 ) ) {
152 $error['error'] = sprintf( __( 'No donor found for %s!', 'give' ), $queried_c );
153
154 return $error;
155 }
156
157 $count = 0;
158 $response_data = array();
159 if ( isset( $wp_query->query_vars['id'] ) && is_numeric( $wp_query->query_vars['id'] ) ) {
160
161 $subscriptions = array(
162 new Give_Subscription( $wp_query->query_vars['id'] ),
163 );
164
165 } else {
166 $paged = $this->get_paged();
167 $per_page = $this->per_page();
168 $offset = $per_page * ( $paged - 1 );
169 $db = new Give_Subscriptions_DB();
170 $subscriptions = $db->get_subscriptions( array(
171 'number' => $per_page,
172 'offset' => $offset,
173 'customer_id' => $donor->id,
174 'status' => $status,
175 ) );
176 }
177
178 if ( $subscriptions ) {
179
180 $unset_info_keys = array(
181 'product_id',
182 'customer_id',
183 'subs_db',
184 );
185
186 /** @var Give_Subscription $subscription */
187 foreach ( $subscriptions as $subscription ) {
188 // Subscription object to array.
189 $response_data['subscriptions'][ $count ]['info'] = $subscription->to_array();
190 $tmp_donor = (array) $response_data['subscriptions'][ $count ]['info']['donor'];
191
192 // Remove `:protected` index key from array
193 $donor = array();
194 foreach ( $tmp_donor as $donor_key => $donor_data ){
195 if( false !== strpos( $donor_key, '*') ) {
196 continue;
197 }
198
199 $donor[$donor_key] = $donor_data;
200 }
201 $response_data['subscriptions'][ $count ]['info']['donor'] = $donor;
202
203 // Remove legacy array keys.
204 foreach ($unset_info_keys as $info_key ) {
205 if ( isset( $response_data['subscriptions'][ $count ]['info'][$info_key] ) ) {
206 unset( $response_data['subscriptions'][ $count ]['info'][$info_key] );
207 }
208 }
209
210 // Format amount.
211 $response_data['subscriptions'][ $count ]['info']['initial_amount'] = give_format_decimal( array(
212 'donation_id' => $response_data['subscriptions'][ $count ]['info']['parent_payment_id'],
213 'amount' => $response_data['subscriptions'][ $count ]['info']['initial_amount'],
214 'dp' => true
215 ) );
216
217 // Format amount.
218 $response_data['subscriptions'][ $count ]['info']['recurring_amount'] = give_format_decimal( array(
219 'donation_id' => $response_data['subscriptions'][ $count ]['info']['parent_payment_id'],
220 'amount' => $response_data['subscriptions'][ $count ]['info']['recurring_amount'],
221 'dp' => true
222 ) );
223
224 // Format amount.
225 $response_data['subscriptions'][ $count ]['info']['donor']['purchase_value'] = give_format_decimal( array(
226 'donation_id' => $response_data['subscriptions'][ $count ]['info']['parent_payment_id'],
227 'amount' => $response_data['subscriptions'][ $count ]['info']['donor']['purchase_value'],
228 'dp' => true,
229 ) );
230
231 $response_data['subscriptions'][ $count ]['info']['currency'] = give_get_payment_currency_code( $response_data['subscriptions'][ $count ]['info']['parent_payment_id'] );
232 $response_data['subscriptions'][ $count ]['info']['form_title'] = get_the_title( $response_data['subscriptions'][ $count ]['info']['form_id'] );
233
234 // Subscription Payments.
235 $subscription_payments = $subscription->get_child_payments();
236 $response_data['subscriptions'][ $count ]['payments'] = array();
237
238 if ( ! empty( $subscription_payments ) ) :
239
240 foreach ( $subscription_payments as $payment ) {
241
242 array_push( $response_data['subscriptions'][ $count ]['payments'], array(
243 'id' => $payment->ID,
244 'amount' => $payment->total,
245 'date' => date_i18n( get_option( 'date_format' ), strtotime( $payment->date ) ),
246 'status' => $payment->status_nicename,
247 ) );
248
249 }
250
251 endif;
252
253 $count ++;
254
255 }
256 } elseif ( ! empty( $queried_c ) ) {
257
258 $error['error'] = sprintf( __( 'No subscriptions found for %s!', 'give' ), $queried_c );
259
260 return $error;
261
262 } else {
263
264 $error['error'] = __( 'No subscriptions found!', 'give' );
265
266 return $error;
267
268 }// End if().
269
270 return $response_data;
271
272 }
273 }
274