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
3 years ago
give-subscriptions-api.php
4 years ago
give-subscriptions-db.php
3 years 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 |