@@ -16,495 +16,495 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class GetPaid_Subscriptions_Query { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * Query vars, after parsing |
|
| 21 | - * |
|
| 22 | - * @since 1.0.19 |
|
| 23 | - * @var array |
|
| 24 | - */ |
|
| 25 | - public $query_vars = array(); |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * List of found subscriptions. |
|
| 29 | - * |
|
| 30 | - * @since 1.0.19 |
|
| 31 | - * @var array |
|
| 32 | - */ |
|
| 33 | - private $results; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * Total number of found subscriptions for the current query |
|
| 37 | - * |
|
| 38 | - * @since 1.0.19 |
|
| 39 | - * @var int |
|
| 40 | - */ |
|
| 41 | - private $total_subscriptions = 0; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * The SQL query used to fetch matching subscriptions. |
|
| 45 | - * |
|
| 46 | - * @since 1.0.19 |
|
| 47 | - * @var string |
|
| 48 | - */ |
|
| 49 | - public $request; |
|
| 50 | - |
|
| 51 | - // SQL clauses |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * Contains the 'FIELDS' sql clause |
|
| 55 | - * |
|
| 56 | - * @since 1.0.19 |
|
| 57 | - * @var string |
|
| 58 | - */ |
|
| 59 | - public $query_fields; |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * Contains the 'FROM' sql clause |
|
| 63 | - * |
|
| 64 | - * @since 1.0.19 |
|
| 65 | - * @var string |
|
| 66 | - */ |
|
| 67 | - public $query_from; |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * Contains the 'WHERE' sql clause |
|
| 71 | - * |
|
| 72 | - * @since 1.0.19 |
|
| 73 | - * @var string |
|
| 74 | - */ |
|
| 75 | - public $query_where; |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * Contains the 'ORDER BY' sql clause |
|
| 79 | - * |
|
| 80 | - * @since 1.0.19 |
|
| 81 | - * @var string |
|
| 82 | - */ |
|
| 83 | - public $query_orderby; |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * Contains the 'LIMIT' sql clause |
|
| 87 | - * |
|
| 88 | - * @since 1.0.19 |
|
| 89 | - * @var string |
|
| 90 | - */ |
|
| 91 | - public $query_limit; |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * Class constructor. |
|
| 95 | - * |
|
| 96 | - * @since 1.0.19 |
|
| 97 | - * |
|
| 98 | - * @param null|string|array $query Optional. The query variables. |
|
| 99 | - */ |
|
| 100 | - public function __construct( $query = null ) { |
|
| 101 | - if ( ! is_null( $query ) ) { |
|
| 102 | - $this->prepare_query( $query ); |
|
| 103 | - $this->query(); |
|
| 104 | - } |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * Fills in missing query variables with default values. |
|
| 109 | - * |
|
| 110 | - * @since 1.0.19 |
|
| 111 | - * |
|
| 112 | - * @param string|array $args Query vars, as passed to `GetPaid_Subscriptions_Query`. |
|
| 113 | - * @return array Complete query variables with undefined ones filled in with defaults. |
|
| 114 | - */ |
|
| 115 | - public static function fill_query_vars( $args ) { |
|
| 116 | - $defaults = array( |
|
| 117 | - 'status' => 'all', |
|
| 118 | - 'customer_in' => array(), |
|
| 119 | - 'customer_not_in' => array(), |
|
| 120 | - 'product_in' => array(), |
|
| 121 | - 'product_not_in' => array(), |
|
| 122 | - 'include' => array(), |
|
| 123 | - 'exclude' => array(), |
|
| 124 | - 'orderby' => 'id', |
|
| 125 | - 'order' => 'DESC', |
|
| 126 | - 'offset' => '', |
|
| 127 | - 'number' => 10, |
|
| 128 | - 'paged' => 1, |
|
| 129 | - 'count_total' => true, |
|
| 130 | - 'fields' => 'all', |
|
| 131 | - ); |
|
| 132 | - |
|
| 133 | - return wp_parse_args( $args, $defaults ); |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Prepare the query variables. |
|
| 138 | - * |
|
| 139 | - * @since 1.0.19 |
|
| 140 | - * |
|
| 141 | - * @global wpdb $wpdb WordPress database abstraction object. |
|
| 142 | - * |
|
| 143 | - * @param string|array $query { |
|
| 144 | - * Optional. Array or string of Query parameters. |
|
| 145 | - * |
|
| 146 | - * @type string|array $status The subscription status to filter by. Can either be a single status or an array of statuses. |
|
| 147 | - * Default is all. |
|
| 148 | - * @type int[] $customer_in An array of customer ids to filter by. |
|
| 149 | - * @type int[] $customer_not_in An array of customer ids whose subscriptions should be excluded. |
|
| 150 | - * @type int[] $invoice_in An array of invoice ids to filter by. |
|
| 151 | - * @type int[] $invoice_not_in An array of invoice ids whose subscriptions should be excluded. |
|
| 152 | - * @type int[] $product_in An array of product ids to filter by. |
|
| 153 | - * @type int[] $product_not_in An array of product ids whose subscriptions should be excluded. |
|
| 154 | - * @type array $date_created_query A WP_Date_Query compatible array use to filter subscriptions by their date of creation. |
|
| 155 | - * @type array $date_expires_query A WP_Date_Query compatible array use to filter subscriptions by their expiration date. |
|
| 156 | - * @type array $include An array of subscription IDs to include. Default empty array. |
|
| 157 | - * @type array $exclude An array of subscription IDs to exclude. Default empty array. |
|
| 158 | - * @type string|array $orderby Field(s) to sort the retrieved subscription by. May be a single value, |
|
| 159 | - * an array of values, or a multi-dimensional array with fields as |
|
| 160 | - * keys and orders ('ASC' or 'DESC') as values. Accepted values are |
|
| 161 | - * 'id', 'customer_id', 'frequency', 'period', 'initial_amount, |
|
| 162 | - * 'recurring_amount', 'bill_times', 'parent_payment_id', 'created', 'expiration' |
|
| 163 | - * 'transaction_id', 'product_id', 'trial_period', 'include', 'status', 'profile_id'. Default array( 'id' ). |
|
| 164 | - * @type string $order Designates ascending or descending order of subscriptions. Order values |
|
| 165 | - * passed as part of an `$orderby` array take precedence over this |
|
| 166 | - * parameter. Accepts 'ASC', 'DESC'. Default 'DESC'. |
|
| 167 | - * @type int $offset Number of subscriptions to offset in retrieved results. Can be used in |
|
| 168 | - * conjunction with pagination. Default 0. |
|
| 169 | - * @type int $number Number of subscriptions to limit the query for. Can be used in |
|
| 170 | - * conjunction with pagination. Value -1 (all) is supported, but |
|
| 171 | - * should be used with caution on larger sites. |
|
| 172 | - * Default 10. |
|
| 173 | - * @type int $paged When used with number, defines the page of results to return. |
|
| 174 | - * Default 1. |
|
| 175 | - * @type bool $count_total Whether to count the total number of subscriptions found. If pagination |
|
| 176 | - * is not needed, setting this to false can improve performance. |
|
| 177 | - * Default true. |
|
| 178 | - * @type string|array $fields Which fields to return. Single or all fields (string), or array |
|
| 179 | - * of fields. Accepts 'id', 'customer_id', 'frequency', 'period', 'initial_amount, |
|
| 180 | - * 'recurring_amount', 'bill_times', 'parent_payment_id', 'created', 'expiration' |
|
| 181 | - * 'transaction_id', 'product_id', 'trial_period', 'status', 'profile_id'. |
|
| 182 | - * Use 'all' for all fields. Default 'all'. |
|
| 183 | - * } |
|
| 184 | - */ |
|
| 185 | - public function prepare_query( $query = array() ) { |
|
| 186 | - global $wpdb; |
|
| 187 | - |
|
| 188 | - if ( empty( $this->query_vars ) || ! empty( $query ) ) { |
|
| 189 | - $this->query_limit = null; |
|
| 190 | - $this->query_vars = $this->fill_query_vars( $query ); |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - if ( ! empty( $this->query_vars['fields'] ) && 'all' !== $this->query_vars['fields'] ) { |
|
| 194 | - $this->query_vars['fields'] = wpinv_parse_list( $this->query_vars['fields'] ); |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - do_action( 'getpaid_pre_get_subscriptions', array( &$this ) ); |
|
| 198 | - |
|
| 199 | - // Ensure that query vars are filled after 'getpaid_pre_get_subscriptions'. |
|
| 200 | - $qv =& $this->query_vars; |
|
| 201 | - $qv = $this->fill_query_vars( $qv ); |
|
| 202 | - $table = $wpdb->prefix . 'wpinv_subscriptions'; |
|
| 203 | - $this->query_from = "FROM $table"; |
|
| 204 | - |
|
| 205 | - // Prepare query fields. |
|
| 206 | - $this->prepare_query_fields( $qv, $table ); |
|
| 207 | - |
|
| 208 | - // Prepare query where. |
|
| 209 | - $this->prepare_query_where( $qv, $table ); |
|
| 210 | - |
|
| 211 | - // Prepare query order. |
|
| 212 | - $this->prepare_query_order( $qv, $table ); |
|
| 213 | - |
|
| 214 | - // limit |
|
| 215 | - if ( isset( $qv['number'] ) && $qv['number'] > 0 ) { |
|
| 216 | - if ( $qv['offset'] ) { |
|
| 217 | - $this->query_limit = $wpdb->prepare( 'LIMIT %d, %d', $qv['offset'], $qv['number'] ); |
|
| 218 | - } else { |
|
| 219 | - $this->query_limit = $wpdb->prepare( 'LIMIT %d, %d', $qv['number'] * ( $qv['paged'] - 1 ), $qv['number'] ); |
|
| 220 | - } |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - do_action_ref_array( 'getpaid_after_subscriptions_query', array( &$this ) ); |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - /** |
|
| 227 | - * Prepares the query fields. |
|
| 228 | - * |
|
| 229 | - * @since 1.0.19 |
|
| 230 | - * |
|
| 231 | - * @param array $qv Query vars. |
|
| 232 | - * @param string $table Table name. |
|
| 233 | - */ |
|
| 234 | - protected function prepare_query_fields( &$qv, $table ) { |
|
| 235 | - |
|
| 236 | - if ( is_array( $qv['fields'] ) ) { |
|
| 237 | - $qv['fields'] = array_unique( $qv['fields'] ); |
|
| 238 | - |
|
| 239 | - $query_fields = array(); |
|
| 240 | - foreach ( $qv['fields'] as $field ) { |
|
| 241 | - $field = sanitize_key( $field ); |
|
| 242 | - $query_fields[] = "$table.`$field`"; |
|
| 243 | - } |
|
| 244 | - $this->query_fields = implode( ',', $query_fields ); |
|
| 245 | - } else { |
|
| 246 | - $this->query_fields = "$table.*"; |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - if ( isset( $qv['count_total'] ) && $qv['count_total'] ) { |
|
| 250 | - $this->query_fields = 'SQL_CALC_FOUND_ROWS ' . $this->query_fields; |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - /** |
|
| 256 | - * Prepares the query where. |
|
| 257 | - * |
|
| 258 | - * @since 1.0.19 |
|
| 259 | - * |
|
| 260 | - * @param array $qv Query vars. |
|
| 261 | - * @param string $table Table name. |
|
| 262 | - */ |
|
| 263 | - protected function prepare_query_where( &$qv, $table ) { |
|
| 264 | - global $wpdb; |
|
| 265 | - $this->query_where = 'WHERE 1=1'; |
|
| 266 | - |
|
| 267 | - // Status. |
|
| 268 | - if ( 'all' !== $qv['status'] ) { |
|
| 269 | - $statuses = wpinv_clean( wpinv_parse_list( $qv['status'] ) ); |
|
| 270 | - $prepared_statuses = join( ',', array_fill( 0, count( $statuses ), '%s' ) ); |
|
| 271 | - $this->query_where .= $wpdb->prepare( " AND $table.`status` IN ( $prepared_statuses )", $statuses ); |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - if ( ! empty( $qv['customer_in'] ) ) { |
|
| 275 | - $customer_in = implode( ',', wp_parse_id_list( $qv['customer_in'] ) ); |
|
| 276 | - $this->query_where .= " AND $table.`customer_id` IN ($customer_in)"; |
|
| 277 | - } elseif ( ! empty( $qv['customer_not_in'] ) ) { |
|
| 278 | - $customer_not_in = implode( ',', wp_parse_id_list( $qv['customer_not_in'] ) ); |
|
| 279 | - $this->query_where .= " AND $table.`customer_id` NOT IN ($customer_not_in)"; |
|
| 280 | - } |
|
| 281 | - |
|
| 282 | - if ( ! empty( $qv['product_in'] ) ) { |
|
| 283 | - $product_in = implode( ',', wp_parse_id_list( $qv['product_in'] ) ); |
|
| 284 | - $this->query_where .= " AND $table.`product_id` IN ($product_in)"; |
|
| 285 | - } elseif ( ! empty( $qv['product_not_in'] ) ) { |
|
| 286 | - $product_not_in = implode( ',', wp_parse_id_list( $qv['product_not_in'] ) ); |
|
| 287 | - $this->query_where .= " AND $table.`product_id` NOT IN ($product_not_in)"; |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - if ( ! empty( $qv['invoice_in'] ) ) { |
|
| 291 | - $invoice_in = implode( ',', wp_parse_id_list( $qv['invoice_in'] ) ); |
|
| 292 | - $this->query_where .= " AND $table.`parent_payment_id` IN ($invoice_in)"; |
|
| 293 | - } elseif ( ! empty( $qv['invoice_not_in'] ) ) { |
|
| 294 | - $invoice_not_in = implode( ',', wp_parse_id_list( $qv['invoice_not_in'] ) ); |
|
| 295 | - $this->query_where .= " AND $table.`parent_payment_id` NOT IN ($invoice_not_in)"; |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - if ( ! empty( $qv['include'] ) ) { |
|
| 299 | - $include = implode( ',', wp_parse_id_list( $qv['include'] ) ); |
|
| 300 | - $this->query_where .= " AND $table.`id` IN ($include)"; |
|
| 301 | - } elseif ( ! empty( $qv['exclude'] ) ) { |
|
| 302 | - $exclude = implode( ',', wp_parse_id_list( $qv['exclude'] ) ); |
|
| 303 | - $this->query_where .= " AND $table.`id` NOT IN ($exclude)"; |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - // Date queries are allowed for the subscription creation date. |
|
| 307 | - if ( ! empty( $qv['date_created_query'] ) && is_array( $qv['date_created_query'] ) ) { |
|
| 308 | - $date_created_query = new WP_Date_Query( $qv['date_created_query'], "$table.created" ); |
|
| 309 | - $this->query_where .= $date_created_query->get_sql(); |
|
| 310 | - } |
|
| 311 | - |
|
| 312 | - // Date queries are also allowed for the subscription expiration date. |
|
| 313 | - if ( ! empty( $qv['date_expires_query'] ) && is_array( $qv['date_expires_query'] ) ) { |
|
| 314 | - $date_expires_query = new WP_Date_Query( $qv['date_expires_query'], "$table.expiration" ); |
|
| 315 | - $this->query_where .= $date_expires_query->get_sql(); |
|
| 316 | - } |
|
| 317 | - |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - /** |
|
| 321 | - * Prepares the query order. |
|
| 322 | - * |
|
| 323 | - * @since 1.0.19 |
|
| 324 | - * |
|
| 325 | - * @param array $qv Query vars. |
|
| 326 | - * @param string $table Table name. |
|
| 327 | - */ |
|
| 328 | - protected function prepare_query_order( &$qv, $table ) { |
|
| 329 | - |
|
| 330 | - // sorting. |
|
| 331 | - $qv['order'] = isset( $qv['order'] ) ? strtoupper( $qv['order'] ) : ''; |
|
| 332 | - $order = $this->parse_order( $qv['order'] ); |
|
| 333 | - |
|
| 334 | - // Default order is by 'id' (latest subscriptions). |
|
| 335 | - if ( empty( $qv['orderby'] ) ) { |
|
| 336 | - $qv['orderby'] = array( 'id' ); |
|
| 337 | - } |
|
| 338 | - |
|
| 339 | - // 'orderby' values may be an array, comma- or space-separated list. |
|
| 340 | - $ordersby = array_filter( wpinv_parse_list( $qv['orderby'] ) ); |
|
| 341 | - |
|
| 342 | - $orderby_array = array(); |
|
| 343 | - foreach ( $ordersby as $_key => $_value ) { |
|
| 344 | - |
|
| 345 | - if ( is_int( $_key ) ) { |
|
| 346 | - // Integer key means this is a flat array of 'orderby' fields. |
|
| 347 | - $_orderby = $_value; |
|
| 348 | - $_order = $order; |
|
| 349 | - } else { |
|
| 350 | - // Non-integer key means that the key is the field and the value is ASC/DESC. |
|
| 351 | - $_orderby = $_key; |
|
| 352 | - $_order = $_value; |
|
| 353 | - } |
|
| 354 | - |
|
| 355 | - $parsed = $this->parse_orderby( $_orderby, $table ); |
|
| 356 | - |
|
| 357 | - if ( $parsed ) { |
|
| 358 | - $orderby_array[] = $parsed . ' ' . $this->parse_order( $_order ); |
|
| 359 | - } |
|
| 360 | - |
|
| 361 | - } |
|
| 362 | - |
|
| 363 | - // If no valid clauses were found, order by id. |
|
| 364 | - if ( empty( $orderby_array ) ) { |
|
| 365 | - $orderby_array[] = "id $order"; |
|
| 366 | - } |
|
| 367 | - |
|
| 368 | - $this->query_orderby = 'ORDER BY ' . implode( ', ', $orderby_array ); |
|
| 369 | - |
|
| 370 | - } |
|
| 371 | - |
|
| 372 | - /** |
|
| 373 | - * Execute the query, with the current variables. |
|
| 374 | - * |
|
| 375 | - * @since 1.0.19 |
|
| 376 | - * |
|
| 377 | - * @global wpdb $wpdb WordPress database abstraction object. |
|
| 378 | - */ |
|
| 379 | - public function query() { |
|
| 380 | - global $wpdb; |
|
| 381 | - |
|
| 382 | - $qv =& $this->query_vars; |
|
| 383 | - |
|
| 384 | - // Return a non-null value to bypass the default GetPaid subscriptions query and remember to set the |
|
| 385 | - // total_subscriptions property. |
|
| 386 | - $this->results = apply_filters_ref_array( 'getpaid_subscriptions_pre_query', array( null, &$this ) ); |
|
| 387 | - |
|
| 388 | - if ( null === $this->results ) { |
|
| 389 | - $this->request = "SELECT $this->query_fields $this->query_from $this->query_where $this->query_orderby $this->query_limit"; |
|
| 390 | - |
|
| 391 | - if ( ( is_array( $qv['fields'] ) && 1 != count( $qv['fields'] ) ) || 'all' == $qv['fields'] ) { |
|
| 392 | - $this->results = $wpdb->get_results( $this->request ); |
|
| 393 | - } else { |
|
| 394 | - $this->results = $wpdb->get_col( $this->request ); |
|
| 395 | - } |
|
| 396 | - |
|
| 397 | - if ( isset( $qv['count_total'] ) && $qv['count_total'] ) { |
|
| 398 | - $found_subscriptions_query = apply_filters( 'getpaid_found_subscriptions_query', 'SELECT FOUND_ROWS()', $this ); |
|
| 399 | - $this->total_subscriptions = (int) $wpdb->get_var( $found_subscriptions_query ); |
|
| 400 | - } |
|
| 401 | - } |
|
| 402 | - |
|
| 403 | - if ( 'all' == $qv['fields'] ) { |
|
| 404 | - foreach ( $this->results as $key => $subscription ) { |
|
| 405 | - wp_cache_set( $subscription->id, $subscription, 'getpaid_subscriptions' ); |
|
| 406 | - wp_cache_set( $subscription->profile_id, $subscription->id, 'getpaid_subscription_profile_ids_to_subscription_ids' ); |
|
| 407 | - wp_cache_set( $subscription->transaction_id, $subscription->id, 'getpaid_subscription_transaction_ids_to_subscription_ids' ); |
|
| 408 | - wp_cache_set( $subscription->transaction_id, $subscription->id, 'getpaid_subscription_transaction_ids_to_subscription_ids' ); |
|
| 409 | - $this->results[ $key ] = new WPInv_Subscription( $subscription ); |
|
| 410 | - } |
|
| 411 | - } |
|
| 412 | - |
|
| 413 | - } |
|
| 414 | - |
|
| 415 | - /** |
|
| 416 | - * Retrieve query variable. |
|
| 417 | - * |
|
| 418 | - * @since 1.0.19 |
|
| 419 | - * |
|
| 420 | - * @param string $query_var Query variable key. |
|
| 421 | - * @return mixed |
|
| 422 | - */ |
|
| 423 | - public function get( $query_var ) { |
|
| 424 | - if ( isset( $this->query_vars[ $query_var ] ) ) { |
|
| 425 | - return $this->query_vars[ $query_var ]; |
|
| 426 | - } |
|
| 427 | - |
|
| 428 | - return null; |
|
| 429 | - } |
|
| 430 | - |
|
| 431 | - /** |
|
| 432 | - * Set query variable. |
|
| 433 | - * |
|
| 434 | - * @since 1.0.19 |
|
| 435 | - * |
|
| 436 | - * @param string $query_var Query variable key. |
|
| 437 | - * @param mixed $value Query variable value. |
|
| 438 | - */ |
|
| 439 | - public function set( $query_var, $value ) { |
|
| 440 | - $this->query_vars[ $query_var ] = $value; |
|
| 441 | - } |
|
| 442 | - |
|
| 443 | - /** |
|
| 444 | - * Return the list of subscriptions. |
|
| 445 | - * |
|
| 446 | - * @since 1.0.19 |
|
| 447 | - * |
|
| 448 | - * @return WPInv_Subscription[]|array Found subscriptions. |
|
| 449 | - */ |
|
| 450 | - public function get_results() { |
|
| 451 | - return $this->results; |
|
| 452 | - } |
|
| 453 | - |
|
| 454 | - /** |
|
| 455 | - * Return the total number of subscriptions for the current query. |
|
| 456 | - * |
|
| 457 | - * @since 1.0.19 |
|
| 458 | - * |
|
| 459 | - * @return int Number of total subscriptions. |
|
| 460 | - */ |
|
| 461 | - public function get_total() { |
|
| 462 | - return $this->total_subscriptions; |
|
| 463 | - } |
|
| 464 | - |
|
| 465 | - /** |
|
| 466 | - * Parse and sanitize 'orderby' keys passed to the subscriptions query. |
|
| 467 | - * |
|
| 468 | - * @since 1.0.19 |
|
| 469 | - * |
|
| 470 | - * @param string $orderby Alias for the field to order by. |
|
| 471 | - * @param string $table The current table. |
|
| 472 | - * @return string Value to use in the ORDER clause, if `$orderby` is valid. |
|
| 473 | - */ |
|
| 474 | - protected function parse_orderby( $orderby, $table ) { |
|
| 475 | - |
|
| 476 | - $_orderby = ''; |
|
| 477 | - if ( in_array( $orderby, array( 'customer_id', 'frequency', 'period', 'initial_amount', 'recurring_amount', 'bill_times', 'transaction_id', 'parent_payment_id', 'product_id', 'created', 'expiration', 'trial_period', 'status', 'profile_id' ) ) ) { |
|
| 478 | - $_orderby = "$table.`$orderby`"; |
|
| 479 | - } elseif ( 'id' === strtolower( $orderby ) ) { |
|
| 480 | - $_orderby = "$table.id"; |
|
| 481 | - } elseif ( 'include' === $orderby && ! empty( $this->query_vars['include'] ) ) { |
|
| 482 | - $include = wp_parse_id_list( $this->query_vars['include'] ); |
|
| 483 | - $include_sql = implode( ',', $include ); |
|
| 484 | - $_orderby = "FIELD( $table.id, $include_sql )"; |
|
| 485 | - } |
|
| 486 | - |
|
| 487 | - return $_orderby; |
|
| 488 | - } |
|
| 489 | - |
|
| 490 | - /** |
|
| 491 | - * Parse an 'order' query variable and cast it to ASC or DESC as necessary. |
|
| 492 | - * |
|
| 493 | - * @since 1.0.19 |
|
| 494 | - * |
|
| 495 | - * @param string $order The 'order' query variable. |
|
| 496 | - * @return string The sanitized 'order' query variable. |
|
| 497 | - */ |
|
| 498 | - protected function parse_order( $order ) { |
|
| 499 | - if ( ! is_string( $order ) || empty( $order ) ) { |
|
| 500 | - return 'DESC'; |
|
| 501 | - } |
|
| 502 | - |
|
| 503 | - if ( 'ASC' === strtoupper( $order ) ) { |
|
| 504 | - return 'ASC'; |
|
| 505 | - } else { |
|
| 506 | - return 'DESC'; |
|
| 507 | - } |
|
| 508 | - } |
|
| 19 | + /** |
|
| 20 | + * Query vars, after parsing |
|
| 21 | + * |
|
| 22 | + * @since 1.0.19 |
|
| 23 | + * @var array |
|
| 24 | + */ |
|
| 25 | + public $query_vars = array(); |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * List of found subscriptions. |
|
| 29 | + * |
|
| 30 | + * @since 1.0.19 |
|
| 31 | + * @var array |
|
| 32 | + */ |
|
| 33 | + private $results; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * Total number of found subscriptions for the current query |
|
| 37 | + * |
|
| 38 | + * @since 1.0.19 |
|
| 39 | + * @var int |
|
| 40 | + */ |
|
| 41 | + private $total_subscriptions = 0; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * The SQL query used to fetch matching subscriptions. |
|
| 45 | + * |
|
| 46 | + * @since 1.0.19 |
|
| 47 | + * @var string |
|
| 48 | + */ |
|
| 49 | + public $request; |
|
| 50 | + |
|
| 51 | + // SQL clauses |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * Contains the 'FIELDS' sql clause |
|
| 55 | + * |
|
| 56 | + * @since 1.0.19 |
|
| 57 | + * @var string |
|
| 58 | + */ |
|
| 59 | + public $query_fields; |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * Contains the 'FROM' sql clause |
|
| 63 | + * |
|
| 64 | + * @since 1.0.19 |
|
| 65 | + * @var string |
|
| 66 | + */ |
|
| 67 | + public $query_from; |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * Contains the 'WHERE' sql clause |
|
| 71 | + * |
|
| 72 | + * @since 1.0.19 |
|
| 73 | + * @var string |
|
| 74 | + */ |
|
| 75 | + public $query_where; |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * Contains the 'ORDER BY' sql clause |
|
| 79 | + * |
|
| 80 | + * @since 1.0.19 |
|
| 81 | + * @var string |
|
| 82 | + */ |
|
| 83 | + public $query_orderby; |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * Contains the 'LIMIT' sql clause |
|
| 87 | + * |
|
| 88 | + * @since 1.0.19 |
|
| 89 | + * @var string |
|
| 90 | + */ |
|
| 91 | + public $query_limit; |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Class constructor. |
|
| 95 | + * |
|
| 96 | + * @since 1.0.19 |
|
| 97 | + * |
|
| 98 | + * @param null|string|array $query Optional. The query variables. |
|
| 99 | + */ |
|
| 100 | + public function __construct( $query = null ) { |
|
| 101 | + if ( ! is_null( $query ) ) { |
|
| 102 | + $this->prepare_query( $query ); |
|
| 103 | + $this->query(); |
|
| 104 | + } |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * Fills in missing query variables with default values. |
|
| 109 | + * |
|
| 110 | + * @since 1.0.19 |
|
| 111 | + * |
|
| 112 | + * @param string|array $args Query vars, as passed to `GetPaid_Subscriptions_Query`. |
|
| 113 | + * @return array Complete query variables with undefined ones filled in with defaults. |
|
| 114 | + */ |
|
| 115 | + public static function fill_query_vars( $args ) { |
|
| 116 | + $defaults = array( |
|
| 117 | + 'status' => 'all', |
|
| 118 | + 'customer_in' => array(), |
|
| 119 | + 'customer_not_in' => array(), |
|
| 120 | + 'product_in' => array(), |
|
| 121 | + 'product_not_in' => array(), |
|
| 122 | + 'include' => array(), |
|
| 123 | + 'exclude' => array(), |
|
| 124 | + 'orderby' => 'id', |
|
| 125 | + 'order' => 'DESC', |
|
| 126 | + 'offset' => '', |
|
| 127 | + 'number' => 10, |
|
| 128 | + 'paged' => 1, |
|
| 129 | + 'count_total' => true, |
|
| 130 | + 'fields' => 'all', |
|
| 131 | + ); |
|
| 132 | + |
|
| 133 | + return wp_parse_args( $args, $defaults ); |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Prepare the query variables. |
|
| 138 | + * |
|
| 139 | + * @since 1.0.19 |
|
| 140 | + * |
|
| 141 | + * @global wpdb $wpdb WordPress database abstraction object. |
|
| 142 | + * |
|
| 143 | + * @param string|array $query { |
|
| 144 | + * Optional. Array or string of Query parameters. |
|
| 145 | + * |
|
| 146 | + * @type string|array $status The subscription status to filter by. Can either be a single status or an array of statuses. |
|
| 147 | + * Default is all. |
|
| 148 | + * @type int[] $customer_in An array of customer ids to filter by. |
|
| 149 | + * @type int[] $customer_not_in An array of customer ids whose subscriptions should be excluded. |
|
| 150 | + * @type int[] $invoice_in An array of invoice ids to filter by. |
|
| 151 | + * @type int[] $invoice_not_in An array of invoice ids whose subscriptions should be excluded. |
|
| 152 | + * @type int[] $product_in An array of product ids to filter by. |
|
| 153 | + * @type int[] $product_not_in An array of product ids whose subscriptions should be excluded. |
|
| 154 | + * @type array $date_created_query A WP_Date_Query compatible array use to filter subscriptions by their date of creation. |
|
| 155 | + * @type array $date_expires_query A WP_Date_Query compatible array use to filter subscriptions by their expiration date. |
|
| 156 | + * @type array $include An array of subscription IDs to include. Default empty array. |
|
| 157 | + * @type array $exclude An array of subscription IDs to exclude. Default empty array. |
|
| 158 | + * @type string|array $orderby Field(s) to sort the retrieved subscription by. May be a single value, |
|
| 159 | + * an array of values, or a multi-dimensional array with fields as |
|
| 160 | + * keys and orders ('ASC' or 'DESC') as values. Accepted values are |
|
| 161 | + * 'id', 'customer_id', 'frequency', 'period', 'initial_amount, |
|
| 162 | + * 'recurring_amount', 'bill_times', 'parent_payment_id', 'created', 'expiration' |
|
| 163 | + * 'transaction_id', 'product_id', 'trial_period', 'include', 'status', 'profile_id'. Default array( 'id' ). |
|
| 164 | + * @type string $order Designates ascending or descending order of subscriptions. Order values |
|
| 165 | + * passed as part of an `$orderby` array take precedence over this |
|
| 166 | + * parameter. Accepts 'ASC', 'DESC'. Default 'DESC'. |
|
| 167 | + * @type int $offset Number of subscriptions to offset in retrieved results. Can be used in |
|
| 168 | + * conjunction with pagination. Default 0. |
|
| 169 | + * @type int $number Number of subscriptions to limit the query for. Can be used in |
|
| 170 | + * conjunction with pagination. Value -1 (all) is supported, but |
|
| 171 | + * should be used with caution on larger sites. |
|
| 172 | + * Default 10. |
|
| 173 | + * @type int $paged When used with number, defines the page of results to return. |
|
| 174 | + * Default 1. |
|
| 175 | + * @type bool $count_total Whether to count the total number of subscriptions found. If pagination |
|
| 176 | + * is not needed, setting this to false can improve performance. |
|
| 177 | + * Default true. |
|
| 178 | + * @type string|array $fields Which fields to return. Single or all fields (string), or array |
|
| 179 | + * of fields. Accepts 'id', 'customer_id', 'frequency', 'period', 'initial_amount, |
|
| 180 | + * 'recurring_amount', 'bill_times', 'parent_payment_id', 'created', 'expiration' |
|
| 181 | + * 'transaction_id', 'product_id', 'trial_period', 'status', 'profile_id'. |
|
| 182 | + * Use 'all' for all fields. Default 'all'. |
|
| 183 | + * } |
|
| 184 | + */ |
|
| 185 | + public function prepare_query( $query = array() ) { |
|
| 186 | + global $wpdb; |
|
| 187 | + |
|
| 188 | + if ( empty( $this->query_vars ) || ! empty( $query ) ) { |
|
| 189 | + $this->query_limit = null; |
|
| 190 | + $this->query_vars = $this->fill_query_vars( $query ); |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + if ( ! empty( $this->query_vars['fields'] ) && 'all' !== $this->query_vars['fields'] ) { |
|
| 194 | + $this->query_vars['fields'] = wpinv_parse_list( $this->query_vars['fields'] ); |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + do_action( 'getpaid_pre_get_subscriptions', array( &$this ) ); |
|
| 198 | + |
|
| 199 | + // Ensure that query vars are filled after 'getpaid_pre_get_subscriptions'. |
|
| 200 | + $qv =& $this->query_vars; |
|
| 201 | + $qv = $this->fill_query_vars( $qv ); |
|
| 202 | + $table = $wpdb->prefix . 'wpinv_subscriptions'; |
|
| 203 | + $this->query_from = "FROM $table"; |
|
| 204 | + |
|
| 205 | + // Prepare query fields. |
|
| 206 | + $this->prepare_query_fields( $qv, $table ); |
|
| 207 | + |
|
| 208 | + // Prepare query where. |
|
| 209 | + $this->prepare_query_where( $qv, $table ); |
|
| 210 | + |
|
| 211 | + // Prepare query order. |
|
| 212 | + $this->prepare_query_order( $qv, $table ); |
|
| 213 | + |
|
| 214 | + // limit |
|
| 215 | + if ( isset( $qv['number'] ) && $qv['number'] > 0 ) { |
|
| 216 | + if ( $qv['offset'] ) { |
|
| 217 | + $this->query_limit = $wpdb->prepare( 'LIMIT %d, %d', $qv['offset'], $qv['number'] ); |
|
| 218 | + } else { |
|
| 219 | + $this->query_limit = $wpdb->prepare( 'LIMIT %d, %d', $qv['number'] * ( $qv['paged'] - 1 ), $qv['number'] ); |
|
| 220 | + } |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + do_action_ref_array( 'getpaid_after_subscriptions_query', array( &$this ) ); |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + /** |
|
| 227 | + * Prepares the query fields. |
|
| 228 | + * |
|
| 229 | + * @since 1.0.19 |
|
| 230 | + * |
|
| 231 | + * @param array $qv Query vars. |
|
| 232 | + * @param string $table Table name. |
|
| 233 | + */ |
|
| 234 | + protected function prepare_query_fields( &$qv, $table ) { |
|
| 235 | + |
|
| 236 | + if ( is_array( $qv['fields'] ) ) { |
|
| 237 | + $qv['fields'] = array_unique( $qv['fields'] ); |
|
| 238 | + |
|
| 239 | + $query_fields = array(); |
|
| 240 | + foreach ( $qv['fields'] as $field ) { |
|
| 241 | + $field = sanitize_key( $field ); |
|
| 242 | + $query_fields[] = "$table.`$field`"; |
|
| 243 | + } |
|
| 244 | + $this->query_fields = implode( ',', $query_fields ); |
|
| 245 | + } else { |
|
| 246 | + $this->query_fields = "$table.*"; |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + if ( isset( $qv['count_total'] ) && $qv['count_total'] ) { |
|
| 250 | + $this->query_fields = 'SQL_CALC_FOUND_ROWS ' . $this->query_fields; |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + /** |
|
| 256 | + * Prepares the query where. |
|
| 257 | + * |
|
| 258 | + * @since 1.0.19 |
|
| 259 | + * |
|
| 260 | + * @param array $qv Query vars. |
|
| 261 | + * @param string $table Table name. |
|
| 262 | + */ |
|
| 263 | + protected function prepare_query_where( &$qv, $table ) { |
|
| 264 | + global $wpdb; |
|
| 265 | + $this->query_where = 'WHERE 1=1'; |
|
| 266 | + |
|
| 267 | + // Status. |
|
| 268 | + if ( 'all' !== $qv['status'] ) { |
|
| 269 | + $statuses = wpinv_clean( wpinv_parse_list( $qv['status'] ) ); |
|
| 270 | + $prepared_statuses = join( ',', array_fill( 0, count( $statuses ), '%s' ) ); |
|
| 271 | + $this->query_where .= $wpdb->prepare( " AND $table.`status` IN ( $prepared_statuses )", $statuses ); |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + if ( ! empty( $qv['customer_in'] ) ) { |
|
| 275 | + $customer_in = implode( ',', wp_parse_id_list( $qv['customer_in'] ) ); |
|
| 276 | + $this->query_where .= " AND $table.`customer_id` IN ($customer_in)"; |
|
| 277 | + } elseif ( ! empty( $qv['customer_not_in'] ) ) { |
|
| 278 | + $customer_not_in = implode( ',', wp_parse_id_list( $qv['customer_not_in'] ) ); |
|
| 279 | + $this->query_where .= " AND $table.`customer_id` NOT IN ($customer_not_in)"; |
|
| 280 | + } |
|
| 281 | + |
|
| 282 | + if ( ! empty( $qv['product_in'] ) ) { |
|
| 283 | + $product_in = implode( ',', wp_parse_id_list( $qv['product_in'] ) ); |
|
| 284 | + $this->query_where .= " AND $table.`product_id` IN ($product_in)"; |
|
| 285 | + } elseif ( ! empty( $qv['product_not_in'] ) ) { |
|
| 286 | + $product_not_in = implode( ',', wp_parse_id_list( $qv['product_not_in'] ) ); |
|
| 287 | + $this->query_where .= " AND $table.`product_id` NOT IN ($product_not_in)"; |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + if ( ! empty( $qv['invoice_in'] ) ) { |
|
| 291 | + $invoice_in = implode( ',', wp_parse_id_list( $qv['invoice_in'] ) ); |
|
| 292 | + $this->query_where .= " AND $table.`parent_payment_id` IN ($invoice_in)"; |
|
| 293 | + } elseif ( ! empty( $qv['invoice_not_in'] ) ) { |
|
| 294 | + $invoice_not_in = implode( ',', wp_parse_id_list( $qv['invoice_not_in'] ) ); |
|
| 295 | + $this->query_where .= " AND $table.`parent_payment_id` NOT IN ($invoice_not_in)"; |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + if ( ! empty( $qv['include'] ) ) { |
|
| 299 | + $include = implode( ',', wp_parse_id_list( $qv['include'] ) ); |
|
| 300 | + $this->query_where .= " AND $table.`id` IN ($include)"; |
|
| 301 | + } elseif ( ! empty( $qv['exclude'] ) ) { |
|
| 302 | + $exclude = implode( ',', wp_parse_id_list( $qv['exclude'] ) ); |
|
| 303 | + $this->query_where .= " AND $table.`id` NOT IN ($exclude)"; |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + // Date queries are allowed for the subscription creation date. |
|
| 307 | + if ( ! empty( $qv['date_created_query'] ) && is_array( $qv['date_created_query'] ) ) { |
|
| 308 | + $date_created_query = new WP_Date_Query( $qv['date_created_query'], "$table.created" ); |
|
| 309 | + $this->query_where .= $date_created_query->get_sql(); |
|
| 310 | + } |
|
| 311 | + |
|
| 312 | + // Date queries are also allowed for the subscription expiration date. |
|
| 313 | + if ( ! empty( $qv['date_expires_query'] ) && is_array( $qv['date_expires_query'] ) ) { |
|
| 314 | + $date_expires_query = new WP_Date_Query( $qv['date_expires_query'], "$table.expiration" ); |
|
| 315 | + $this->query_where .= $date_expires_query->get_sql(); |
|
| 316 | + } |
|
| 317 | + |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + /** |
|
| 321 | + * Prepares the query order. |
|
| 322 | + * |
|
| 323 | + * @since 1.0.19 |
|
| 324 | + * |
|
| 325 | + * @param array $qv Query vars. |
|
| 326 | + * @param string $table Table name. |
|
| 327 | + */ |
|
| 328 | + protected function prepare_query_order( &$qv, $table ) { |
|
| 329 | + |
|
| 330 | + // sorting. |
|
| 331 | + $qv['order'] = isset( $qv['order'] ) ? strtoupper( $qv['order'] ) : ''; |
|
| 332 | + $order = $this->parse_order( $qv['order'] ); |
|
| 333 | + |
|
| 334 | + // Default order is by 'id' (latest subscriptions). |
|
| 335 | + if ( empty( $qv['orderby'] ) ) { |
|
| 336 | + $qv['orderby'] = array( 'id' ); |
|
| 337 | + } |
|
| 338 | + |
|
| 339 | + // 'orderby' values may be an array, comma- or space-separated list. |
|
| 340 | + $ordersby = array_filter( wpinv_parse_list( $qv['orderby'] ) ); |
|
| 341 | + |
|
| 342 | + $orderby_array = array(); |
|
| 343 | + foreach ( $ordersby as $_key => $_value ) { |
|
| 344 | + |
|
| 345 | + if ( is_int( $_key ) ) { |
|
| 346 | + // Integer key means this is a flat array of 'orderby' fields. |
|
| 347 | + $_orderby = $_value; |
|
| 348 | + $_order = $order; |
|
| 349 | + } else { |
|
| 350 | + // Non-integer key means that the key is the field and the value is ASC/DESC. |
|
| 351 | + $_orderby = $_key; |
|
| 352 | + $_order = $_value; |
|
| 353 | + } |
|
| 354 | + |
|
| 355 | + $parsed = $this->parse_orderby( $_orderby, $table ); |
|
| 356 | + |
|
| 357 | + if ( $parsed ) { |
|
| 358 | + $orderby_array[] = $parsed . ' ' . $this->parse_order( $_order ); |
|
| 359 | + } |
|
| 360 | + |
|
| 361 | + } |
|
| 362 | + |
|
| 363 | + // If no valid clauses were found, order by id. |
|
| 364 | + if ( empty( $orderby_array ) ) { |
|
| 365 | + $orderby_array[] = "id $order"; |
|
| 366 | + } |
|
| 367 | + |
|
| 368 | + $this->query_orderby = 'ORDER BY ' . implode( ', ', $orderby_array ); |
|
| 369 | + |
|
| 370 | + } |
|
| 371 | + |
|
| 372 | + /** |
|
| 373 | + * Execute the query, with the current variables. |
|
| 374 | + * |
|
| 375 | + * @since 1.0.19 |
|
| 376 | + * |
|
| 377 | + * @global wpdb $wpdb WordPress database abstraction object. |
|
| 378 | + */ |
|
| 379 | + public function query() { |
|
| 380 | + global $wpdb; |
|
| 381 | + |
|
| 382 | + $qv =& $this->query_vars; |
|
| 383 | + |
|
| 384 | + // Return a non-null value to bypass the default GetPaid subscriptions query and remember to set the |
|
| 385 | + // total_subscriptions property. |
|
| 386 | + $this->results = apply_filters_ref_array( 'getpaid_subscriptions_pre_query', array( null, &$this ) ); |
|
| 387 | + |
|
| 388 | + if ( null === $this->results ) { |
|
| 389 | + $this->request = "SELECT $this->query_fields $this->query_from $this->query_where $this->query_orderby $this->query_limit"; |
|
| 390 | + |
|
| 391 | + if ( ( is_array( $qv['fields'] ) && 1 != count( $qv['fields'] ) ) || 'all' == $qv['fields'] ) { |
|
| 392 | + $this->results = $wpdb->get_results( $this->request ); |
|
| 393 | + } else { |
|
| 394 | + $this->results = $wpdb->get_col( $this->request ); |
|
| 395 | + } |
|
| 396 | + |
|
| 397 | + if ( isset( $qv['count_total'] ) && $qv['count_total'] ) { |
|
| 398 | + $found_subscriptions_query = apply_filters( 'getpaid_found_subscriptions_query', 'SELECT FOUND_ROWS()', $this ); |
|
| 399 | + $this->total_subscriptions = (int) $wpdb->get_var( $found_subscriptions_query ); |
|
| 400 | + } |
|
| 401 | + } |
|
| 402 | + |
|
| 403 | + if ( 'all' == $qv['fields'] ) { |
|
| 404 | + foreach ( $this->results as $key => $subscription ) { |
|
| 405 | + wp_cache_set( $subscription->id, $subscription, 'getpaid_subscriptions' ); |
|
| 406 | + wp_cache_set( $subscription->profile_id, $subscription->id, 'getpaid_subscription_profile_ids_to_subscription_ids' ); |
|
| 407 | + wp_cache_set( $subscription->transaction_id, $subscription->id, 'getpaid_subscription_transaction_ids_to_subscription_ids' ); |
|
| 408 | + wp_cache_set( $subscription->transaction_id, $subscription->id, 'getpaid_subscription_transaction_ids_to_subscription_ids' ); |
|
| 409 | + $this->results[ $key ] = new WPInv_Subscription( $subscription ); |
|
| 410 | + } |
|
| 411 | + } |
|
| 412 | + |
|
| 413 | + } |
|
| 414 | + |
|
| 415 | + /** |
|
| 416 | + * Retrieve query variable. |
|
| 417 | + * |
|
| 418 | + * @since 1.0.19 |
|
| 419 | + * |
|
| 420 | + * @param string $query_var Query variable key. |
|
| 421 | + * @return mixed |
|
| 422 | + */ |
|
| 423 | + public function get( $query_var ) { |
|
| 424 | + if ( isset( $this->query_vars[ $query_var ] ) ) { |
|
| 425 | + return $this->query_vars[ $query_var ]; |
|
| 426 | + } |
|
| 427 | + |
|
| 428 | + return null; |
|
| 429 | + } |
|
| 430 | + |
|
| 431 | + /** |
|
| 432 | + * Set query variable. |
|
| 433 | + * |
|
| 434 | + * @since 1.0.19 |
|
| 435 | + * |
|
| 436 | + * @param string $query_var Query variable key. |
|
| 437 | + * @param mixed $value Query variable value. |
|
| 438 | + */ |
|
| 439 | + public function set( $query_var, $value ) { |
|
| 440 | + $this->query_vars[ $query_var ] = $value; |
|
| 441 | + } |
|
| 442 | + |
|
| 443 | + /** |
|
| 444 | + * Return the list of subscriptions. |
|
| 445 | + * |
|
| 446 | + * @since 1.0.19 |
|
| 447 | + * |
|
| 448 | + * @return WPInv_Subscription[]|array Found subscriptions. |
|
| 449 | + */ |
|
| 450 | + public function get_results() { |
|
| 451 | + return $this->results; |
|
| 452 | + } |
|
| 453 | + |
|
| 454 | + /** |
|
| 455 | + * Return the total number of subscriptions for the current query. |
|
| 456 | + * |
|
| 457 | + * @since 1.0.19 |
|
| 458 | + * |
|
| 459 | + * @return int Number of total subscriptions. |
|
| 460 | + */ |
|
| 461 | + public function get_total() { |
|
| 462 | + return $this->total_subscriptions; |
|
| 463 | + } |
|
| 464 | + |
|
| 465 | + /** |
|
| 466 | + * Parse and sanitize 'orderby' keys passed to the subscriptions query. |
|
| 467 | + * |
|
| 468 | + * @since 1.0.19 |
|
| 469 | + * |
|
| 470 | + * @param string $orderby Alias for the field to order by. |
|
| 471 | + * @param string $table The current table. |
|
| 472 | + * @return string Value to use in the ORDER clause, if `$orderby` is valid. |
|
| 473 | + */ |
|
| 474 | + protected function parse_orderby( $orderby, $table ) { |
|
| 475 | + |
|
| 476 | + $_orderby = ''; |
|
| 477 | + if ( in_array( $orderby, array( 'customer_id', 'frequency', 'period', 'initial_amount', 'recurring_amount', 'bill_times', 'transaction_id', 'parent_payment_id', 'product_id', 'created', 'expiration', 'trial_period', 'status', 'profile_id' ) ) ) { |
|
| 478 | + $_orderby = "$table.`$orderby`"; |
|
| 479 | + } elseif ( 'id' === strtolower( $orderby ) ) { |
|
| 480 | + $_orderby = "$table.id"; |
|
| 481 | + } elseif ( 'include' === $orderby && ! empty( $this->query_vars['include'] ) ) { |
|
| 482 | + $include = wp_parse_id_list( $this->query_vars['include'] ); |
|
| 483 | + $include_sql = implode( ',', $include ); |
|
| 484 | + $_orderby = "FIELD( $table.id, $include_sql )"; |
|
| 485 | + } |
|
| 486 | + |
|
| 487 | + return $_orderby; |
|
| 488 | + } |
|
| 489 | + |
|
| 490 | + /** |
|
| 491 | + * Parse an 'order' query variable and cast it to ASC or DESC as necessary. |
|
| 492 | + * |
|
| 493 | + * @since 1.0.19 |
|
| 494 | + * |
|
| 495 | + * @param string $order The 'order' query variable. |
|
| 496 | + * @return string The sanitized 'order' query variable. |
|
| 497 | + */ |
|
| 498 | + protected function parse_order( $order ) { |
|
| 499 | + if ( ! is_string( $order ) || empty( $order ) ) { |
|
| 500 | + return 'DESC'; |
|
| 501 | + } |
|
| 502 | + |
|
| 503 | + if ( 'ASC' === strtoupper( $order ) ) { |
|
| 504 | + return 'ASC'; |
|
| 505 | + } else { |
|
| 506 | + return 'DESC'; |
|
| 507 | + } |
|
| 508 | + } |
|
| 509 | 509 | |
| 510 | 510 | } |
@@ -13,36 +13,36 @@ discard block |
||
| 13 | 13 | class GetPaid_Notification_Email { |
| 14 | 14 | |
| 15 | 15 | /** |
| 16 | - * Contains the type of this notification email. |
|
| 17 | - * |
|
| 18 | - * @var string |
|
| 19 | - */ |
|
| 16 | + * Contains the type of this notification email. |
|
| 17 | + * |
|
| 18 | + * @var string |
|
| 19 | + */ |
|
| 20 | 20 | public $id; |
| 21 | 21 | |
| 22 | 22 | /** |
| 23 | - * Contains any object to use in filters. |
|
| 24 | - * |
|
| 25 | - * @var false|WPInv_Invoice|WPInv_Item|WPInv_Subscription |
|
| 26 | - */ |
|
| 23 | + * Contains any object to use in filters. |
|
| 24 | + * |
|
| 25 | + * @var false|WPInv_Invoice|WPInv_Item|WPInv_Subscription |
|
| 26 | + */ |
|
| 27 | 27 | public $object; |
| 28 | 28 | |
| 29 | 29 | /** |
| 30 | - * Class constructor. |
|
| 31 | - * |
|
| 30 | + * Class constructor. |
|
| 31 | + * |
|
| 32 | 32 | * @param string $id Email Type. |
| 33 | 33 | * @param mixed $object Optional. Associated object. |
| 34 | - */ |
|
| 35 | - public function __construct( $id, $object = false ) { |
|
| 34 | + */ |
|
| 35 | + public function __construct( $id, $object = false ) { |
|
| 36 | 36 | $this->id = $id; |
| 37 | 37 | $this->object = $object; |
| 38 | 38 | } |
| 39 | 39 | |
| 40 | 40 | /** |
| 41 | - * Retrieves an option |
|
| 42 | - * |
|
| 41 | + * Retrieves an option |
|
| 42 | + * |
|
| 43 | 43 | * @return mixed |
| 44 | - */ |
|
| 45 | - public function get_option( $key ) { |
|
| 44 | + */ |
|
| 45 | + public function get_option( $key ) { |
|
| 46 | 46 | |
| 47 | 47 | $key = "email_{$this->id}_$key"; |
| 48 | 48 | $value = wpinv_get_option( $key, null ); |
@@ -60,80 +60,80 @@ discard block |
||
| 60 | 60 | } |
| 61 | 61 | |
| 62 | 62 | /** |
| 63 | - * Retrieves the email body. |
|
| 64 | - * |
|
| 63 | + * Retrieves the email body. |
|
| 64 | + * |
|
| 65 | 65 | * @return string |
| 66 | - */ |
|
| 67 | - public function get_body() { |
|
| 66 | + */ |
|
| 67 | + public function get_body() { |
|
| 68 | 68 | $body = $this->get_option( 'body' ); |
| 69 | 69 | return apply_filters( 'getpaid_get_email_body', $body, $this->id, $this->object ); |
| 70 | 70 | } |
| 71 | 71 | |
| 72 | 72 | /** |
| 73 | - * Retrieves the email subject. |
|
| 74 | - * |
|
| 73 | + * Retrieves the email subject. |
|
| 74 | + * |
|
| 75 | 75 | * @return string |
| 76 | - */ |
|
| 77 | - public function get_subject() { |
|
| 76 | + */ |
|
| 77 | + public function get_subject() { |
|
| 78 | 78 | $subject = $this->get_option( 'subject' ); |
| 79 | 79 | return apply_filters( 'getpaid_get_email_subject', $subject, $this->id, $this->object ); |
| 80 | 80 | } |
| 81 | 81 | |
| 82 | 82 | /** |
| 83 | - * Retrieves the email heading. |
|
| 84 | - * |
|
| 83 | + * Retrieves the email heading. |
|
| 84 | + * |
|
| 85 | 85 | * @return string |
| 86 | - */ |
|
| 87 | - public function get_heading() { |
|
| 86 | + */ |
|
| 87 | + public function get_heading() { |
|
| 88 | 88 | $heading = $this->get_option( 'heading' ); |
| 89 | 89 | return apply_filters( 'getpaid_get_email_heading', $heading, $this->id, $this->object ); |
| 90 | 90 | } |
| 91 | 91 | |
| 92 | 92 | /** |
| 93 | - * Checks if an email is active. |
|
| 94 | - * |
|
| 93 | + * Checks if an email is active. |
|
| 94 | + * |
|
| 95 | 95 | * @return bool |
| 96 | - */ |
|
| 97 | - public function is_active() { |
|
| 96 | + */ |
|
| 97 | + public function is_active() { |
|
| 98 | 98 | $is_active = ! empty( $this->get_option( 'active' ) ); |
| 99 | 99 | return apply_filters( 'getpaid_email_type_is_active', $is_active, $this->id, $this->object ); |
| 100 | 100 | } |
| 101 | 101 | |
| 102 | 102 | /** |
| 103 | - * Checks if the site's admin should receive email notifications. |
|
| 104 | - * |
|
| 103 | + * Checks if the site's admin should receive email notifications. |
|
| 104 | + * |
|
| 105 | 105 | * @return bool |
| 106 | - */ |
|
| 107 | - public function include_admin_bcc() { |
|
| 106 | + */ |
|
| 107 | + public function include_admin_bcc() { |
|
| 108 | 108 | $include_admin_bcc = ! empty( $this->get_option( 'admin_bcc' ) ); |
| 109 | 109 | return apply_filters( 'getpaid_email_type_include_admin_bcc', $include_admin_bcc, $this->id, $this->object ); |
| 110 | 110 | } |
| 111 | 111 | |
| 112 | 112 | /** |
| 113 | - * Checks whether this email should be sent to the customer or admin. |
|
| 114 | - * |
|
| 113 | + * Checks whether this email should be sent to the customer or admin. |
|
| 114 | + * |
|
| 115 | 115 | * @return bool |
| 116 | - */ |
|
| 117 | - public function is_admin_email() { |
|
| 116 | + */ |
|
| 117 | + public function is_admin_email() { |
|
| 118 | 118 | $is_admin_email = in_array( $this->id, array( 'new_invoice', 'cancelled_invoice', 'failed_invoice' ) ); |
| 119 | 119 | return apply_filters( 'getpaid_email_type_is_admin_email', $is_admin_email, $this->id, $this->object ); |
| 120 | 120 | } |
| 121 | 121 | |
| 122 | 122 | /** |
| 123 | - * Returns email attachments. |
|
| 124 | - * |
|
| 123 | + * Returns email attachments. |
|
| 124 | + * |
|
| 125 | 125 | * @return array |
| 126 | - */ |
|
| 127 | - public function get_attachments() { |
|
| 126 | + */ |
|
| 127 | + public function get_attachments() { |
|
| 128 | 128 | return apply_filters( 'getpaid_get_email_attachments', array(), $this->id, $this->object ); |
| 129 | 129 | } |
| 130 | 130 | |
| 131 | 131 | /** |
| 132 | - * Returns an array of merge tags. |
|
| 133 | - * |
|
| 132 | + * Returns an array of merge tags. |
|
| 133 | + * |
|
| 134 | 134 | * @return array |
| 135 | - */ |
|
| 136 | - public function get_merge_tags() { |
|
| 135 | + */ |
|
| 136 | + public function get_merge_tags() { |
|
| 137 | 137 | |
| 138 | 138 | $merge_tags = array( |
| 139 | 139 | '{site_title}' => wpinv_get_blogname(), |
@@ -144,13 +144,13 @@ discard block |
||
| 144 | 144 | } |
| 145 | 145 | |
| 146 | 146 | /** |
| 147 | - * Adds merge tags to a text. |
|
| 148 | - * |
|
| 147 | + * Adds merge tags to a text. |
|
| 148 | + * |
|
| 149 | 149 | * @param string string $text |
| 150 | 150 | * @param array $merge_tags |
| 151 | 151 | * @return string |
| 152 | - */ |
|
| 153 | - public function add_merge_tags( $text, $merge_tags = array() ) { |
|
| 152 | + */ |
|
| 153 | + public function add_merge_tags( $text, $merge_tags = array() ) { |
|
| 154 | 154 | |
| 155 | 155 | foreach ( $merge_tags as $key => $value ) { |
| 156 | 156 | $text = str_replace( $key, $value, $text ); |
@@ -160,13 +160,13 @@ discard block |
||
| 160 | 160 | } |
| 161 | 161 | |
| 162 | 162 | /** |
| 163 | - * Returns the email content |
|
| 164 | - * |
|
| 163 | + * Returns the email content |
|
| 164 | + * |
|
| 165 | 165 | * @param array $merge_tags |
| 166 | 166 | * @param array $extra_args Extra template args |
| 167 | 167 | * @return string |
| 168 | - */ |
|
| 169 | - public function get_content( $merge_tags = array(), $extra_args = array() ) { |
|
| 168 | + */ |
|
| 169 | + public function get_content( $merge_tags = array(), $extra_args = array() ) { |
|
| 170 | 170 | |
| 171 | 171 | $content = wpinv_get_template_html( |
| 172 | 172 | "emails/wpinv-email-{$this->id}.php", |
@@ -110,8 +110,8 @@ discard block |
||
| 110 | 110 | $css = getpaid_get_email_css(); |
| 111 | 111 | |
| 112 | 112 | // include css inliner |
| 113 | - if ( ! class_exists( 'Emogrifier' ) ) { |
|
| 114 | - include_once( WPINV_PLUGIN_DIR . 'includes/libraries/class-emogrifier.php' ); |
|
| 113 | + if ( ! class_exists( 'Emogrifier' ) ) { |
|
| 114 | + include_once( WPINV_PLUGIN_DIR . 'includes/libraries/class-emogrifier.php' ); |
|
| 115 | 115 | } |
| 116 | 116 | |
| 117 | 117 | // Inline the css. |
@@ -190,7 +190,7 @@ discard block |
||
| 190 | 190 | $message = wpinv_email_style_body( $message ); |
| 191 | 191 | $to = array_merge( wpinv_parse_list( $to ), wpinv_parse_list( $cc ) ); |
| 192 | 192 | |
| 193 | - return $mailer->send( |
|
| 193 | + return $mailer->send( |
|
| 194 | 194 | $to, |
| 195 | 195 | $subject, |
| 196 | 196 | $message, |
@@ -117,14 +117,14 @@ |
||
| 117 | 117 | */ |
| 118 | 118 | function getpaid_doing_it_wrong( $function, $message, $version ) { |
| 119 | 119 | |
| 120 | - $message .= ' Backtrace: ' . wp_debug_backtrace_summary(); |
|
| 121 | - |
|
| 122 | - if ( wp_doing_ajax() || defined( 'REST_REQUEST' ) ) { |
|
| 123 | - do_action( 'doing_it_wrong_run', $function, $message, $version ); |
|
| 124 | - error_log( "{$function} was called incorrectly. {$message}. This message was added in version {$version}." ); |
|
| 125 | - } else { |
|
| 126 | - _doing_it_wrong( $function, $message, $version ); |
|
| 127 | - } |
|
| 120 | + $message .= ' Backtrace: ' . wp_debug_backtrace_summary(); |
|
| 121 | + |
|
| 122 | + if ( wp_doing_ajax() || defined( 'REST_REQUEST' ) ) { |
|
| 123 | + do_action( 'doing_it_wrong_run', $function, $message, $version ); |
|
| 124 | + error_log( "{$function} was called incorrectly. {$message}. This message was added in version {$version}." ); |
|
| 125 | + } else { |
|
| 126 | + _doing_it_wrong( $function, $message, $version ); |
|
| 127 | + } |
|
| 128 | 128 | |
| 129 | 129 | } |
| 130 | 130 | |
@@ -13,128 +13,128 @@ |
||
| 13 | 13 | class GetPaid_Payment_Forms { |
| 14 | 14 | |
| 15 | 15 | /** |
| 16 | - * Class constructor |
|
| 17 | - * |
|
| 18 | - */ |
|
| 19 | - public function __construct() { |
|
| 20 | - |
|
| 21 | - // Update a payment form's revenue whenever an invoice is paid for or refunded. |
|
| 22 | - add_action( 'getpaid_invoice_payment_status_changed', array( $this, 'increment_form_revenue' ) ); |
|
| 23 | - add_action( 'getpaid_invoice_payment_status_reversed', array( $this, 'decrease_form_revenue' ) ); |
|
| 24 | - |
|
| 25 | - // Sync form amount whenever invoice statuses change. |
|
| 26 | - add_action( 'getpaid_invoice_status_changed', array( $this, 'update_form_failed_amount' ), 10, 3 ); |
|
| 27 | - add_action( 'getpaid_invoice_status_changed', array( $this, 'update_form_refunded_amount' ), 10, 3 ); |
|
| 28 | - add_action( 'getpaid_invoice_status_changed', array( $this, 'update_form_cancelled_amount' ), 10, 3 ); |
|
| 29 | - |
|
| 30 | - } |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * Increments a form's revenue whenever there is a payment. |
|
| 34 | - * |
|
| 35 | - * @param WPInv_Invoice $invoice |
|
| 36 | - */ |
|
| 37 | - public function increment_form_revenue( $invoice ) { |
|
| 38 | - |
|
| 39 | - $form = new GetPaid_Payment_Form( $invoice->get_payment_form() ); |
|
| 40 | - if ( $form->get_id() ) { |
|
| 41 | - $form->set_earned( $form->get_earned() + $invoice->get_total() ); |
|
| 42 | - $form->save(); |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * Decreases form revenue whenever invoice payment changes. |
|
| 49 | - * |
|
| 50 | - * @param WPInv_Invoice $invoice |
|
| 51 | - */ |
|
| 52 | - public function decrease_form_revenue( $invoice ) { |
|
| 53 | - |
|
| 54 | - $form = new GetPaid_Payment_Form( $invoice->get_payment_form() ); |
|
| 55 | - if ( $form->get_id() ) { |
|
| 56 | - $form->set_earned( $form->get_earned() - $invoice->get_total() ); |
|
| 57 | - $form->save(); |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * Updates a form's failed amount. |
|
| 64 | - * |
|
| 65 | - * @param WPInv_Invoice $invoice |
|
| 66 | - * @param string $from |
|
| 67 | - * @param string $to |
|
| 68 | - */ |
|
| 69 | - public function update_form_failed_amount( $invoice, $from, $to ) { |
|
| 70 | - |
|
| 71 | - $form = new GetPaid_Payment_Form( $invoice->get_payment_form() ); |
|
| 72 | - if ( $form->get_id() ) { |
|
| 73 | - return; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - if ( 'wpi-failed' == $from ) { |
|
| 77 | - $form->set_failed( $form->get_failed() - $invoice->get_total() ); |
|
| 78 | - $form->save(); |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - if ( 'wpi-failed' == $to ) { |
|
| 82 | - $form->set_failed( $form->get_failed() + $invoice->get_total() ); |
|
| 83 | - $form->save(); |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * Updates a form's refunded amount. |
|
| 90 | - * |
|
| 91 | - * @param WPInv_Invoice $invoice |
|
| 92 | - * @param string $from |
|
| 93 | - * @param string $to |
|
| 94 | - */ |
|
| 95 | - public function update_form_refunded_amount( $invoice, $from, $to ) { |
|
| 96 | - |
|
| 97 | - $form = new GetPaid_Payment_Form( $invoice->get_payment_form() ); |
|
| 98 | - if ( $form->get_id() ) { |
|
| 99 | - return; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - if ( 'wpi-refunded' == $from ) { |
|
| 103 | - $form->set_refunded( $form->get_refunded() - $invoice->get_total() ); |
|
| 104 | - $form->save(); |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - if ( 'wpi-refunded' == $to ) { |
|
| 108 | - $form->set_refunded( $form->get_refunded() + $invoice->get_total() ); |
|
| 109 | - $form->save(); |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * Updates a form's cancelled amount. |
|
| 116 | - * |
|
| 117 | - * @param WPInv_Invoice $invoice |
|
| 118 | - * @param string $from |
|
| 119 | - * @param string $to |
|
| 120 | - */ |
|
| 121 | - public function update_form_cancelled_amount( $invoice, $from, $to ) { |
|
| 122 | - |
|
| 123 | - $form = new GetPaid_Payment_Form( $invoice->get_payment_form() ); |
|
| 124 | - if ( $form->get_id() ) { |
|
| 125 | - return; |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - if ( 'wpi-cancelled' == $from ) { |
|
| 129 | - $form->set_cancelled( $form->get_cancelled() - $invoice->get_total() ); |
|
| 130 | - $form->save(); |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - if ( 'wpi-cancelled' == $to ) { |
|
| 134 | - $form->set_cancelled( $form->get_cancelled() + $invoice->get_total() ); |
|
| 135 | - $form->save(); |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - } |
|
| 16 | + * Class constructor |
|
| 17 | + * |
|
| 18 | + */ |
|
| 19 | + public function __construct() { |
|
| 20 | + |
|
| 21 | + // Update a payment form's revenue whenever an invoice is paid for or refunded. |
|
| 22 | + add_action( 'getpaid_invoice_payment_status_changed', array( $this, 'increment_form_revenue' ) ); |
|
| 23 | + add_action( 'getpaid_invoice_payment_status_reversed', array( $this, 'decrease_form_revenue' ) ); |
|
| 24 | + |
|
| 25 | + // Sync form amount whenever invoice statuses change. |
|
| 26 | + add_action( 'getpaid_invoice_status_changed', array( $this, 'update_form_failed_amount' ), 10, 3 ); |
|
| 27 | + add_action( 'getpaid_invoice_status_changed', array( $this, 'update_form_refunded_amount' ), 10, 3 ); |
|
| 28 | + add_action( 'getpaid_invoice_status_changed', array( $this, 'update_form_cancelled_amount' ), 10, 3 ); |
|
| 29 | + |
|
| 30 | + } |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * Increments a form's revenue whenever there is a payment. |
|
| 34 | + * |
|
| 35 | + * @param WPInv_Invoice $invoice |
|
| 36 | + */ |
|
| 37 | + public function increment_form_revenue( $invoice ) { |
|
| 38 | + |
|
| 39 | + $form = new GetPaid_Payment_Form( $invoice->get_payment_form() ); |
|
| 40 | + if ( $form->get_id() ) { |
|
| 41 | + $form->set_earned( $form->get_earned() + $invoice->get_total() ); |
|
| 42 | + $form->save(); |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * Decreases form revenue whenever invoice payment changes. |
|
| 49 | + * |
|
| 50 | + * @param WPInv_Invoice $invoice |
|
| 51 | + */ |
|
| 52 | + public function decrease_form_revenue( $invoice ) { |
|
| 53 | + |
|
| 54 | + $form = new GetPaid_Payment_Form( $invoice->get_payment_form() ); |
|
| 55 | + if ( $form->get_id() ) { |
|
| 56 | + $form->set_earned( $form->get_earned() - $invoice->get_total() ); |
|
| 57 | + $form->save(); |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * Updates a form's failed amount. |
|
| 64 | + * |
|
| 65 | + * @param WPInv_Invoice $invoice |
|
| 66 | + * @param string $from |
|
| 67 | + * @param string $to |
|
| 68 | + */ |
|
| 69 | + public function update_form_failed_amount( $invoice, $from, $to ) { |
|
| 70 | + |
|
| 71 | + $form = new GetPaid_Payment_Form( $invoice->get_payment_form() ); |
|
| 72 | + if ( $form->get_id() ) { |
|
| 73 | + return; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + if ( 'wpi-failed' == $from ) { |
|
| 77 | + $form->set_failed( $form->get_failed() - $invoice->get_total() ); |
|
| 78 | + $form->save(); |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + if ( 'wpi-failed' == $to ) { |
|
| 82 | + $form->set_failed( $form->get_failed() + $invoice->get_total() ); |
|
| 83 | + $form->save(); |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * Updates a form's refunded amount. |
|
| 90 | + * |
|
| 91 | + * @param WPInv_Invoice $invoice |
|
| 92 | + * @param string $from |
|
| 93 | + * @param string $to |
|
| 94 | + */ |
|
| 95 | + public function update_form_refunded_amount( $invoice, $from, $to ) { |
|
| 96 | + |
|
| 97 | + $form = new GetPaid_Payment_Form( $invoice->get_payment_form() ); |
|
| 98 | + if ( $form->get_id() ) { |
|
| 99 | + return; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + if ( 'wpi-refunded' == $from ) { |
|
| 103 | + $form->set_refunded( $form->get_refunded() - $invoice->get_total() ); |
|
| 104 | + $form->save(); |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + if ( 'wpi-refunded' == $to ) { |
|
| 108 | + $form->set_refunded( $form->get_refunded() + $invoice->get_total() ); |
|
| 109 | + $form->save(); |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * Updates a form's cancelled amount. |
|
| 116 | + * |
|
| 117 | + * @param WPInv_Invoice $invoice |
|
| 118 | + * @param string $from |
|
| 119 | + * @param string $to |
|
| 120 | + */ |
|
| 121 | + public function update_form_cancelled_amount( $invoice, $from, $to ) { |
|
| 122 | + |
|
| 123 | + $form = new GetPaid_Payment_Form( $invoice->get_payment_form() ); |
|
| 124 | + if ( $form->get_id() ) { |
|
| 125 | + return; |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + if ( 'wpi-cancelled' == $from ) { |
|
| 129 | + $form->set_cancelled( $form->get_cancelled() - $invoice->get_total() ); |
|
| 130 | + $form->save(); |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + if ( 'wpi-cancelled' == $to ) { |
|
| 134 | + $form->set_cancelled( $form->get_cancelled() + $invoice->get_total() ); |
|
| 135 | + $form->save(); |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + } |
|
| 139 | 139 | |
| 140 | 140 | } |
@@ -1,7 +1,7 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | - exit; // Exit if accessed directly |
|
| 4 | + exit; // Exit if accessed directly |
|
| 5 | 5 | } |
| 6 | 6 | |
| 7 | 7 | /** |
@@ -11,77 +11,77 @@ discard block |
||
| 11 | 11 | */ |
| 12 | 12 | class AUI_Component_Pagination { |
| 13 | 13 | |
| 14 | - /** |
|
| 15 | - * Build the component. |
|
| 16 | - * |
|
| 17 | - * @param array $args |
|
| 18 | - * |
|
| 19 | - * @return string The rendered component. |
|
| 20 | - */ |
|
| 21 | - public static function get( $args = array() ) { |
|
| 22 | - global $wp_query; |
|
| 23 | - |
|
| 24 | - $defaults = array( |
|
| 25 | - 'class' => '', |
|
| 26 | - 'mid_size' => 2, |
|
| 27 | - 'prev_text' => '<i class="fas fa-chevron-left"></i>', |
|
| 28 | - 'next_text' => '<i class="fas fa-chevron-right"></i>', |
|
| 29 | - 'screen_reader_text' => __( 'Posts navigation','aui' ), |
|
| 30 | - 'before_paging' => '', |
|
| 31 | - 'after_paging' => '', |
|
| 32 | - 'type' => 'array', |
|
| 33 | - 'total' => isset( $wp_query->max_num_pages ) ? $wp_query->max_num_pages : 1, |
|
| 34 | - 'links' => array() // an array of links if using custom links, this includes the a tag. |
|
| 35 | - ); |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * Parse incoming $args into an array and merge it with $defaults |
|
| 39 | - */ |
|
| 40 | - $args = wp_parse_args( $args, $defaults ); |
|
| 41 | - |
|
| 42 | - $output = ''; |
|
| 43 | - |
|
| 44 | - // Don't print empty markup if there's only one page. |
|
| 45 | - if ( $args['total'] > 1 ) { |
|
| 46 | - |
|
| 47 | - // Set up paginated links. |
|
| 48 | - $links = !empty( $args['links'] ) ? $args['links'] : paginate_links( $args ); |
|
| 49 | - |
|
| 50 | - $class = !empty($args['class']) ? $args['class'] : ''; |
|
| 51 | - |
|
| 52 | - // make the output bootstrap ready |
|
| 53 | - $links_html = "<ul class='pagination m-0 p-0 $class'>"; |
|
| 54 | - if ( ! empty( $links ) ) { |
|
| 55 | - foreach ( $links as $link ) { |
|
| 56 | - $active = strpos( $link, 'current' ) !== false ? 'active' : ''; |
|
| 57 | - $links_html .= "<li class='page-item $active'>"; |
|
| 58 | - $links_html .= str_replace( "page-numbers", "page-link", $link ); |
|
| 59 | - $links_html .= "</li>"; |
|
| 60 | - } |
|
| 61 | - } |
|
| 62 | - $links_html .= "</ul>"; |
|
| 63 | - |
|
| 64 | - if ( $links ) { |
|
| 65 | - $output .= '<section class="px-0 py-2 w-100">'; |
|
| 66 | - $output .= _navigation_markup( $links_html, 'aui-pagination', $args['screen_reader_text'] ); |
|
| 67 | - $output .= '</section>'; |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - $output = str_replace( "screen-reader-text", "screen-reader-text sr-only", $output ); |
|
| 71 | - $output = str_replace( "nav-links", "aui-nav-links", $output ); |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - if ( $output ) { |
|
| 75 | - if ( ! empty( $args['before_paging'] ) ) { |
|
| 76 | - $output = $args['before_paging'] . $output; |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - if ( ! empty( $args['after_paging'] ) ) { |
|
| 80 | - $output = $output . $args['after_paging']; |
|
| 81 | - } |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - return $output; |
|
| 85 | - } |
|
| 14 | + /** |
|
| 15 | + * Build the component. |
|
| 16 | + * |
|
| 17 | + * @param array $args |
|
| 18 | + * |
|
| 19 | + * @return string The rendered component. |
|
| 20 | + */ |
|
| 21 | + public static function get( $args = array() ) { |
|
| 22 | + global $wp_query; |
|
| 23 | + |
|
| 24 | + $defaults = array( |
|
| 25 | + 'class' => '', |
|
| 26 | + 'mid_size' => 2, |
|
| 27 | + 'prev_text' => '<i class="fas fa-chevron-left"></i>', |
|
| 28 | + 'next_text' => '<i class="fas fa-chevron-right"></i>', |
|
| 29 | + 'screen_reader_text' => __( 'Posts navigation','aui' ), |
|
| 30 | + 'before_paging' => '', |
|
| 31 | + 'after_paging' => '', |
|
| 32 | + 'type' => 'array', |
|
| 33 | + 'total' => isset( $wp_query->max_num_pages ) ? $wp_query->max_num_pages : 1, |
|
| 34 | + 'links' => array() // an array of links if using custom links, this includes the a tag. |
|
| 35 | + ); |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * Parse incoming $args into an array and merge it with $defaults |
|
| 39 | + */ |
|
| 40 | + $args = wp_parse_args( $args, $defaults ); |
|
| 41 | + |
|
| 42 | + $output = ''; |
|
| 43 | + |
|
| 44 | + // Don't print empty markup if there's only one page. |
|
| 45 | + if ( $args['total'] > 1 ) { |
|
| 46 | + |
|
| 47 | + // Set up paginated links. |
|
| 48 | + $links = !empty( $args['links'] ) ? $args['links'] : paginate_links( $args ); |
|
| 49 | + |
|
| 50 | + $class = !empty($args['class']) ? $args['class'] : ''; |
|
| 51 | + |
|
| 52 | + // make the output bootstrap ready |
|
| 53 | + $links_html = "<ul class='pagination m-0 p-0 $class'>"; |
|
| 54 | + if ( ! empty( $links ) ) { |
|
| 55 | + foreach ( $links as $link ) { |
|
| 56 | + $active = strpos( $link, 'current' ) !== false ? 'active' : ''; |
|
| 57 | + $links_html .= "<li class='page-item $active'>"; |
|
| 58 | + $links_html .= str_replace( "page-numbers", "page-link", $link ); |
|
| 59 | + $links_html .= "</li>"; |
|
| 60 | + } |
|
| 61 | + } |
|
| 62 | + $links_html .= "</ul>"; |
|
| 63 | + |
|
| 64 | + if ( $links ) { |
|
| 65 | + $output .= '<section class="px-0 py-2 w-100">'; |
|
| 66 | + $output .= _navigation_markup( $links_html, 'aui-pagination', $args['screen_reader_text'] ); |
|
| 67 | + $output .= '</section>'; |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + $output = str_replace( "screen-reader-text", "screen-reader-text sr-only", $output ); |
|
| 71 | + $output = str_replace( "nav-links", "aui-nav-links", $output ); |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + if ( $output ) { |
|
| 75 | + if ( ! empty( $args['before_paging'] ) ) { |
|
| 76 | + $output = $args['before_paging'] . $output; |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + if ( ! empty( $args['after_paging'] ) ) { |
|
| 80 | + $output = $output . $args['after_paging']; |
|
| 81 | + } |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + return $output; |
|
| 85 | + } |
|
| 86 | 86 | |
| 87 | 87 | } |
| 88 | 88 | \ No newline at end of file |
@@ -31,16 +31,16 @@ discard block |
||
| 31 | 31 | } |
| 32 | 32 | |
| 33 | 33 | function wpinv_can_checkout() { |
| 34 | - $can_checkout = true; // Always true for now |
|
| 34 | + $can_checkout = true; // Always true for now |
|
| 35 | 35 | |
| 36 | - return (bool) apply_filters( 'wpinv_can_checkout', $can_checkout ); |
|
| 36 | + return (bool) apply_filters( 'wpinv_can_checkout', $can_checkout ); |
|
| 37 | 37 | } |
| 38 | 38 | |
| 39 | 39 | function wpinv_get_success_page_uri() { |
| 40 | - $page_id = wpinv_get_option( 'success_page', 0 ); |
|
| 41 | - $page_id = absint( $page_id ); |
|
| 40 | + $page_id = wpinv_get_option( 'success_page', 0 ); |
|
| 41 | + $page_id = absint( $page_id ); |
|
| 42 | 42 | |
| 43 | - return apply_filters( 'wpinv_get_success_page_uri', get_permalink( $page_id ) ); |
|
| 43 | + return apply_filters( 'wpinv_get_success_page_uri', get_permalink( $page_id ) ); |
|
| 44 | 44 | } |
| 45 | 45 | |
| 46 | 46 | /** |
@@ -51,22 +51,22 @@ discard block |
||
| 51 | 51 | */ |
| 52 | 52 | function wpinv_get_history_page_uri( $post_type = 'wpi_invoice' ) { |
| 53 | 53 | $post_type = sanitize_key( str_replace( 'wpi_', '', $post_type ) ); |
| 54 | - $page_id = wpinv_get_option( "{$post_type}_history_page", 0 ); |
|
| 55 | - $page_id = absint( $page_id ); |
|
| 56 | - return apply_filters( 'wpinv_get_history_page_uri', get_permalink( $page_id ), $post_type ); |
|
| 54 | + $page_id = wpinv_get_option( "{$post_type}_history_page", 0 ); |
|
| 55 | + $page_id = absint( $page_id ); |
|
| 56 | + return apply_filters( 'wpinv_get_history_page_uri', get_permalink( $page_id ), $post_type ); |
|
| 57 | 57 | } |
| 58 | 58 | |
| 59 | 59 | function wpinv_is_success_page() { |
| 60 | - $is_success_page = wpinv_get_option( 'success_page', false ); |
|
| 61 | - $is_success_page = ! empty( $is_success_page ) ? is_page( $is_success_page ) : false; |
|
| 60 | + $is_success_page = wpinv_get_option( 'success_page', false ); |
|
| 61 | + $is_success_page = ! empty( $is_success_page ) ? is_page( $is_success_page ) : false; |
|
| 62 | 62 | |
| 63 | - return apply_filters( 'wpinv_is_success_page', $is_success_page ); |
|
| 63 | + return apply_filters( 'wpinv_is_success_page', $is_success_page ); |
|
| 64 | 64 | } |
| 65 | 65 | |
| 66 | 66 | function wpinv_is_invoice_history_page() { |
| 67 | - $ret = wpinv_get_option( 'invoice_history_page', false ); |
|
| 68 | - $ret = $ret ? is_page( $ret ) : false; |
|
| 69 | - return apply_filters( 'wpinv_is_invoice_history_page', $ret ); |
|
| 67 | + $ret = wpinv_get_option( 'invoice_history_page', false ); |
|
| 68 | + $ret = $ret ? is_page( $ret ) : false; |
|
| 69 | + return apply_filters( 'wpinv_is_invoice_history_page', $ret ); |
|
| 70 | 70 | } |
| 71 | 71 | |
| 72 | 72 | function wpinv_is_subscriptions_history_page() { |
@@ -89,7 +89,7 @@ discard block |
||
| 89 | 89 | } |
| 90 | 90 | |
| 91 | 91 | function wpinv_send_to_failed_page( $args = null ) { |
| 92 | - $redirect = wpinv_get_failed_transaction_uri(); |
|
| 92 | + $redirect = wpinv_get_failed_transaction_uri(); |
|
| 93 | 93 | |
| 94 | 94 | if ( !empty( $args ) ) { |
| 95 | 95 | // Check for backward compatibility |
@@ -109,55 +109,55 @@ discard block |
||
| 109 | 109 | } |
| 110 | 110 | |
| 111 | 111 | function wpinv_get_checkout_uri( $args = array() ) { |
| 112 | - $uri = wpinv_get_option( 'checkout_page', false ); |
|
| 113 | - $uri = isset( $uri ) ? get_permalink( $uri ) : NULL; |
|
| 112 | + $uri = wpinv_get_option( 'checkout_page', false ); |
|
| 113 | + $uri = isset( $uri ) ? get_permalink( $uri ) : NULL; |
|
| 114 | 114 | |
| 115 | - if ( !empty( $args ) ) { |
|
| 116 | - // Check for backward compatibility |
|
| 117 | - if ( is_string( $args ) ) |
|
| 118 | - $args = str_replace( '?', '', $args ); |
|
| 115 | + if ( !empty( $args ) ) { |
|
| 116 | + // Check for backward compatibility |
|
| 117 | + if ( is_string( $args ) ) |
|
| 118 | + $args = str_replace( '?', '', $args ); |
|
| 119 | 119 | |
| 120 | - $args = wp_parse_args( $args ); |
|
| 120 | + $args = wp_parse_args( $args ); |
|
| 121 | 121 | |
| 122 | - $uri = add_query_arg( $args, $uri ); |
|
| 123 | - } |
|
| 122 | + $uri = add_query_arg( $args, $uri ); |
|
| 123 | + } |
|
| 124 | 124 | |
| 125 | - $scheme = defined( 'FORCE_SSL_ADMIN' ) && FORCE_SSL_ADMIN ? 'https' : 'admin'; |
|
| 125 | + $scheme = defined( 'FORCE_SSL_ADMIN' ) && FORCE_SSL_ADMIN ? 'https' : 'admin'; |
|
| 126 | 126 | |
| 127 | - $ajax_url = admin_url( 'admin-ajax.php', $scheme ); |
|
| 127 | + $ajax_url = admin_url( 'admin-ajax.php', $scheme ); |
|
| 128 | 128 | |
| 129 | - if ( ( ! preg_match( '/^https/', $uri ) && preg_match( '/^https/', $ajax_url ) ) || wpinv_is_ssl_enforced() ) { |
|
| 130 | - $uri = preg_replace( '/^http:/', 'https:', $uri ); |
|
| 131 | - } |
|
| 129 | + if ( ( ! preg_match( '/^https/', $uri ) && preg_match( '/^https/', $ajax_url ) ) || wpinv_is_ssl_enforced() ) { |
|
| 130 | + $uri = preg_replace( '/^http:/', 'https:', $uri ); |
|
| 131 | + } |
|
| 132 | 132 | |
| 133 | - return apply_filters( 'wpinv_get_checkout_uri', $uri ); |
|
| 133 | + return apply_filters( 'wpinv_get_checkout_uri', $uri ); |
|
| 134 | 134 | } |
| 135 | 135 | |
| 136 | 136 | function wpinv_get_success_page_url( $query_string = null ) { |
| 137 | - $success_page = wpinv_get_option( 'success_page', 0 ); |
|
| 138 | - $success_page = get_permalink( $success_page ); |
|
| 137 | + $success_page = wpinv_get_option( 'success_page', 0 ); |
|
| 138 | + $success_page = get_permalink( $success_page ); |
|
| 139 | 139 | |
| 140 | - if ( $query_string ) |
|
| 141 | - $success_page .= $query_string; |
|
| 140 | + if ( $query_string ) |
|
| 141 | + $success_page .= $query_string; |
|
| 142 | 142 | |
| 143 | - return apply_filters( 'wpinv_success_page_url', $success_page ); |
|
| 143 | + return apply_filters( 'wpinv_success_page_url', $success_page ); |
|
| 144 | 144 | } |
| 145 | 145 | |
| 146 | 146 | function wpinv_get_failed_transaction_uri( $extras = false ) { |
| 147 | - $uri = wpinv_get_option( 'failure_page', '' ); |
|
| 148 | - $uri = ! empty( $uri ) ? trailingslashit( get_permalink( $uri ) ) : home_url(); |
|
| 147 | + $uri = wpinv_get_option( 'failure_page', '' ); |
|
| 148 | + $uri = ! empty( $uri ) ? trailingslashit( get_permalink( $uri ) ) : home_url(); |
|
| 149 | 149 | |
| 150 | - if ( $extras ) |
|
| 151 | - $uri .= $extras; |
|
| 150 | + if ( $extras ) |
|
| 151 | + $uri .= $extras; |
|
| 152 | 152 | |
| 153 | - return apply_filters( 'wpinv_get_failed_transaction_uri', $uri ); |
|
| 153 | + return apply_filters( 'wpinv_get_failed_transaction_uri', $uri ); |
|
| 154 | 154 | } |
| 155 | 155 | |
| 156 | 156 | function wpinv_is_failed_transaction_page() { |
| 157 | - $ret = wpinv_get_option( 'failure_page', false ); |
|
| 158 | - $ret = isset( $ret ) ? is_page( $ret ) : false; |
|
| 157 | + $ret = wpinv_get_option( 'failure_page', false ); |
|
| 158 | + $ret = isset( $ret ) ? is_page( $ret ) : false; |
|
| 159 | 159 | |
| 160 | - return apply_filters( 'wpinv_is_failure_page', $ret ); |
|
| 160 | + return apply_filters( 'wpinv_is_failure_page', $ret ); |
|
| 161 | 161 | } |
| 162 | 162 | |
| 163 | 163 | function wpinv_transaction_query( $type = 'start' ) { |
@@ -232,36 +232,36 @@ discard block |
||
| 232 | 232 | $require_billing_details = apply_filters( 'wpinv_checkout_required_billing_details', wpinv_use_taxes() ); |
| 233 | 233 | |
| 234 | 234 | if ( $require_billing_details ) { |
| 235 | - if ( (bool)wpinv_get_option( 'fname_mandatory' ) ) { |
|
| 236 | - $required_fields['first_name'] = array( |
|
| 237 | - 'error_id' => 'invalid_first_name', |
|
| 238 | - 'error_message' => __( 'Please enter your first name', 'invoicing' ) |
|
| 239 | - ); |
|
| 240 | - } |
|
| 241 | - if ( (bool)wpinv_get_option( 'address_mandatory' ) ) { |
|
| 242 | - $required_fields['address'] = array( |
|
| 243 | - 'error_id' => 'invalid_address', |
|
| 244 | - 'error_message' => __( 'Please enter your address', 'invoicing' ) |
|
| 245 | - ); |
|
| 246 | - } |
|
| 247 | - if ( (bool)wpinv_get_option( 'city_mandatory' ) ) { |
|
| 248 | - $required_fields['city'] = array( |
|
| 249 | - 'error_id' => 'invalid_city', |
|
| 250 | - 'error_message' => __( 'Please enter your billing city', 'invoicing' ) |
|
| 251 | - ); |
|
| 252 | - } |
|
| 253 | - if ( (bool)wpinv_get_option( 'state_mandatory' ) ) { |
|
| 254 | - $required_fields['state'] = array( |
|
| 255 | - 'error_id' => 'invalid_state', |
|
| 256 | - 'error_message' => __( 'Please enter billing state / province', 'invoicing' ) |
|
| 257 | - ); |
|
| 258 | - } |
|
| 259 | - if ( (bool)wpinv_get_option( 'country_mandatory' ) ) { |
|
| 260 | - $required_fields['country'] = array( |
|
| 261 | - 'error_id' => 'invalid_country', |
|
| 262 | - 'error_message' => __( 'Please select your billing country', 'invoicing' ) |
|
| 263 | - ); |
|
| 264 | - } |
|
| 235 | + if ( (bool)wpinv_get_option( 'fname_mandatory' ) ) { |
|
| 236 | + $required_fields['first_name'] = array( |
|
| 237 | + 'error_id' => 'invalid_first_name', |
|
| 238 | + 'error_message' => __( 'Please enter your first name', 'invoicing' ) |
|
| 239 | + ); |
|
| 240 | + } |
|
| 241 | + if ( (bool)wpinv_get_option( 'address_mandatory' ) ) { |
|
| 242 | + $required_fields['address'] = array( |
|
| 243 | + 'error_id' => 'invalid_address', |
|
| 244 | + 'error_message' => __( 'Please enter your address', 'invoicing' ) |
|
| 245 | + ); |
|
| 246 | + } |
|
| 247 | + if ( (bool)wpinv_get_option( 'city_mandatory' ) ) { |
|
| 248 | + $required_fields['city'] = array( |
|
| 249 | + 'error_id' => 'invalid_city', |
|
| 250 | + 'error_message' => __( 'Please enter your billing city', 'invoicing' ) |
|
| 251 | + ); |
|
| 252 | + } |
|
| 253 | + if ( (bool)wpinv_get_option( 'state_mandatory' ) ) { |
|
| 254 | + $required_fields['state'] = array( |
|
| 255 | + 'error_id' => 'invalid_state', |
|
| 256 | + 'error_message' => __( 'Please enter billing state / province', 'invoicing' ) |
|
| 257 | + ); |
|
| 258 | + } |
|
| 259 | + if ( (bool)wpinv_get_option( 'country_mandatory' ) ) { |
|
| 260 | + $required_fields['country'] = array( |
|
| 261 | + 'error_id' => 'invalid_country', |
|
| 262 | + 'error_message' => __( 'Please select your billing country', 'invoicing' ) |
|
| 263 | + ); |
|
| 264 | + } |
|
| 265 | 265 | } |
| 266 | 266 | |
| 267 | 267 | return apply_filters( 'wpinv_checkout_required_fields', $required_fields ); |
@@ -15,136 +15,136 @@ |
||
| 15 | 15 | class WPInv_REST_Invoice_Controller extends GetPaid_REST_Posts_Controller { |
| 16 | 16 | |
| 17 | 17 | /** |
| 18 | - * Post type. |
|
| 19 | - * |
|
| 20 | - * @var string |
|
| 21 | - */ |
|
| 22 | - protected $post_type = 'wpi_invoice'; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * The base of this controller's route. |
|
| 26 | - * |
|
| 27 | - * @since 1.0.13 |
|
| 28 | - * @var string |
|
| 29 | - */ |
|
| 30 | - protected $rest_base = 'invoices'; |
|
| 31 | - |
|
| 32 | - /** Contains this controller's class name. |
|
| 33 | - * |
|
| 34 | - * @var string |
|
| 35 | - */ |
|
| 36 | - public $crud_class = 'WPInv_Invoice'; |
|
| 18 | + * Post type. |
|
| 19 | + * |
|
| 20 | + * @var string |
|
| 21 | + */ |
|
| 22 | + protected $post_type = 'wpi_invoice'; |
|
| 37 | 23 | |
| 38 | 24 | /** |
| 39 | - * Retrieves the query params for the invoices collection. |
|
| 40 | - * |
|
| 41 | - * @since 1.0.13 |
|
| 42 | - * |
|
| 43 | - * @return array Collection parameters. |
|
| 44 | - */ |
|
| 45 | - public function get_collection_params() { |
|
| 46 | - |
|
| 47 | - $params = array_merge( |
|
| 48 | - |
|
| 49 | - parent::get_collection_params(), |
|
| 50 | - |
|
| 51 | - array( |
|
| 52 | - |
|
| 53 | - |
|
| 54 | - 'customers' => array( |
|
| 55 | - 'description' => __( 'Limit result set to invoices for specific user ids.', 'invoicing' ), |
|
| 56 | - 'type' => 'array', |
|
| 57 | - 'items' => array( |
|
| 58 | - 'type' => 'integer', |
|
| 59 | - ), |
|
| 60 | - 'default' => array(), |
|
| 61 | - 'sanitize_callback' => 'wp_parse_id_list', |
|
| 62 | - ), |
|
| 63 | - |
|
| 64 | - 'exclude_customers' => array( |
|
| 65 | - 'description' => __( 'Exclude invoices to specific users.', 'invoicing' ), |
|
| 66 | - 'type' => 'array', |
|
| 67 | - 'items' => array( |
|
| 68 | - 'type' => 'integer', |
|
| 69 | - ), |
|
| 70 | - 'default' => array(), |
|
| 71 | - 'sanitize_callback' => 'wp_parse_id_list', |
|
| 72 | - ), |
|
| 73 | - |
|
| 74 | - 'parent' => array( |
|
| 75 | - 'description' => __( 'Limit result set to those of particular parent IDs.', 'invoicing' ), |
|
| 76 | - 'type' => 'array', |
|
| 77 | - 'items' => array( |
|
| 78 | - 'type' => 'integer', |
|
| 79 | - ), |
|
| 80 | - 'sanitize_callback' => 'wp_parse_id_list', |
|
| 81 | - 'default' => array(), |
|
| 82 | - ), |
|
| 83 | - |
|
| 84 | - 'parent_exclude' => array( |
|
| 85 | - 'description' => __( 'Limit result set to all items except those of a particular parent ID.', 'invoicing' ), |
|
| 86 | - 'type' => 'array', |
|
| 87 | - 'items' => array( |
|
| 88 | - 'type' => 'integer', |
|
| 89 | - ), |
|
| 90 | - 'sanitize_callback' => 'wp_parse_id_list', |
|
| 91 | - 'default' => array(), |
|
| 92 | - ), |
|
| 93 | - |
|
| 94 | - ) |
|
| 95 | - |
|
| 96 | - ); |
|
| 97 | - |
|
| 98 | - // Filter collection parameters for the invoices controller. |
|
| 99 | - return apply_filters( 'getpaid_rest_invoices_collection_params', $params, $this ); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * Determine the allowed query_vars for a get_items() response and |
|
| 104 | - * prepare for WP_Query. |
|
| 105 | - * |
|
| 106 | - * @param array $prepared_args Prepared arguments. |
|
| 107 | - * @param WP_REST_Request $request Request object. |
|
| 108 | - * @return array $query_args |
|
| 109 | - */ |
|
| 110 | - protected function prepare_items_query( $prepared_args = array(), $request = null ) { |
|
| 111 | - |
|
| 112 | - $query_args = parent::prepare_items_query( $prepared_args ); |
|
| 113 | - |
|
| 114 | - // Retrieve invoices for specific customers. |
|
| 115 | - if ( ! empty( $request['customers'] ) ) { |
|
| 116 | - $query_args['author__in'] = $request['customers']; |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - // Skip invoices for specific customers. |
|
| 120 | - if ( ! empty( $request['exclude_customers'] ) ) { |
|
| 121 | - $query_args['author__not_in'] = $request['exclude_customers']; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - return apply_filters( 'getpaid_rest_invoices_prepare_items_query', $query_args, $request, $this ); |
|
| 125 | - |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * Retrieves a valid list of post statuses. |
|
| 130 | - * |
|
| 131 | - * @since 1.0.15 |
|
| 132 | - * |
|
| 133 | - * @return array A list of registered item statuses. |
|
| 134 | - */ |
|
| 135 | - public function get_post_statuses() { |
|
| 136 | - return array_keys( wpinv_get_invoice_statuses( true, false, $this->post_type ) ); |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - /** |
|
| 140 | - * Saves a single invoice. |
|
| 141 | - * |
|
| 142 | - * @param WPInv_Invoice $invoice Invoice to save. |
|
| 143 | - * @return WP_Error|WPInv_Invoice |
|
| 144 | - */ |
|
| 145 | - protected function save_object( $invoice ) { |
|
| 146 | - $invoice->recalculate_total(); |
|
| 147 | - return parent::save_object( $invoice ); |
|
| 148 | - } |
|
| 25 | + * The base of this controller's route. |
|
| 26 | + * |
|
| 27 | + * @since 1.0.13 |
|
| 28 | + * @var string |
|
| 29 | + */ |
|
| 30 | + protected $rest_base = 'invoices'; |
|
| 31 | + |
|
| 32 | + /** Contains this controller's class name. |
|
| 33 | + * |
|
| 34 | + * @var string |
|
| 35 | + */ |
|
| 36 | + public $crud_class = 'WPInv_Invoice'; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * Retrieves the query params for the invoices collection. |
|
| 40 | + * |
|
| 41 | + * @since 1.0.13 |
|
| 42 | + * |
|
| 43 | + * @return array Collection parameters. |
|
| 44 | + */ |
|
| 45 | + public function get_collection_params() { |
|
| 46 | + |
|
| 47 | + $params = array_merge( |
|
| 48 | + |
|
| 49 | + parent::get_collection_params(), |
|
| 50 | + |
|
| 51 | + array( |
|
| 52 | + |
|
| 53 | + |
|
| 54 | + 'customers' => array( |
|
| 55 | + 'description' => __( 'Limit result set to invoices for specific user ids.', 'invoicing' ), |
|
| 56 | + 'type' => 'array', |
|
| 57 | + 'items' => array( |
|
| 58 | + 'type' => 'integer', |
|
| 59 | + ), |
|
| 60 | + 'default' => array(), |
|
| 61 | + 'sanitize_callback' => 'wp_parse_id_list', |
|
| 62 | + ), |
|
| 63 | + |
|
| 64 | + 'exclude_customers' => array( |
|
| 65 | + 'description' => __( 'Exclude invoices to specific users.', 'invoicing' ), |
|
| 66 | + 'type' => 'array', |
|
| 67 | + 'items' => array( |
|
| 68 | + 'type' => 'integer', |
|
| 69 | + ), |
|
| 70 | + 'default' => array(), |
|
| 71 | + 'sanitize_callback' => 'wp_parse_id_list', |
|
| 72 | + ), |
|
| 73 | + |
|
| 74 | + 'parent' => array( |
|
| 75 | + 'description' => __( 'Limit result set to those of particular parent IDs.', 'invoicing' ), |
|
| 76 | + 'type' => 'array', |
|
| 77 | + 'items' => array( |
|
| 78 | + 'type' => 'integer', |
|
| 79 | + ), |
|
| 80 | + 'sanitize_callback' => 'wp_parse_id_list', |
|
| 81 | + 'default' => array(), |
|
| 82 | + ), |
|
| 83 | + |
|
| 84 | + 'parent_exclude' => array( |
|
| 85 | + 'description' => __( 'Limit result set to all items except those of a particular parent ID.', 'invoicing' ), |
|
| 86 | + 'type' => 'array', |
|
| 87 | + 'items' => array( |
|
| 88 | + 'type' => 'integer', |
|
| 89 | + ), |
|
| 90 | + 'sanitize_callback' => 'wp_parse_id_list', |
|
| 91 | + 'default' => array(), |
|
| 92 | + ), |
|
| 93 | + |
|
| 94 | + ) |
|
| 95 | + |
|
| 96 | + ); |
|
| 97 | + |
|
| 98 | + // Filter collection parameters for the invoices controller. |
|
| 99 | + return apply_filters( 'getpaid_rest_invoices_collection_params', $params, $this ); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * Determine the allowed query_vars for a get_items() response and |
|
| 104 | + * prepare for WP_Query. |
|
| 105 | + * |
|
| 106 | + * @param array $prepared_args Prepared arguments. |
|
| 107 | + * @param WP_REST_Request $request Request object. |
|
| 108 | + * @return array $query_args |
|
| 109 | + */ |
|
| 110 | + protected function prepare_items_query( $prepared_args = array(), $request = null ) { |
|
| 111 | + |
|
| 112 | + $query_args = parent::prepare_items_query( $prepared_args ); |
|
| 113 | + |
|
| 114 | + // Retrieve invoices for specific customers. |
|
| 115 | + if ( ! empty( $request['customers'] ) ) { |
|
| 116 | + $query_args['author__in'] = $request['customers']; |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + // Skip invoices for specific customers. |
|
| 120 | + if ( ! empty( $request['exclude_customers'] ) ) { |
|
| 121 | + $query_args['author__not_in'] = $request['exclude_customers']; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + return apply_filters( 'getpaid_rest_invoices_prepare_items_query', $query_args, $request, $this ); |
|
| 125 | + |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * Retrieves a valid list of post statuses. |
|
| 130 | + * |
|
| 131 | + * @since 1.0.15 |
|
| 132 | + * |
|
| 133 | + * @return array A list of registered item statuses. |
|
| 134 | + */ |
|
| 135 | + public function get_post_statuses() { |
|
| 136 | + return array_keys( wpinv_get_invoice_statuses( true, false, $this->post_type ) ); |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + /** |
|
| 140 | + * Saves a single invoice. |
|
| 141 | + * |
|
| 142 | + * @param WPInv_Invoice $invoice Invoice to save. |
|
| 143 | + * @return WP_Error|WPInv_Invoice |
|
| 144 | + */ |
|
| 145 | + protected function save_object( $invoice ) { |
|
| 146 | + $invoice->recalculate_total(); |
|
| 147 | + return parent::save_object( $invoice ); |
|
| 148 | + } |
|
| 149 | 149 | |
| 150 | 150 | } |
@@ -56,7 +56,7 @@ |
||
| 56 | 56 | } |
| 57 | 57 | |
| 58 | 58 | function wpinv_admin_messages() { |
| 59 | - settings_errors( 'wpinv-notices' ); |
|
| 59 | + settings_errors( 'wpinv-notices' ); |
|
| 60 | 60 | } |
| 61 | 61 | add_action( 'admin_notices', 'wpinv_admin_messages' ); |
| 62 | 62 | |