@@ -16,502 +16,502 @@ |
||
16 | 16 | */ |
17 | 17 | class GetPaid_Customers_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 customers. |
|
29 | - * |
|
30 | - * @since 1.0.19 |
|
31 | - * @var array |
|
32 | - */ |
|
33 | - private $results; |
|
34 | - |
|
35 | - /** |
|
36 | - * Total number of found customers for the current query |
|
37 | - * |
|
38 | - * @since 1.0.19 |
|
39 | - * @var int |
|
40 | - */ |
|
41 | - private $total_customers = 0; |
|
42 | - |
|
43 | - /** |
|
44 | - * The SQL query used to fetch matching customers. |
|
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 | - 'include' => array(), |
|
118 | - 'exclude' => array(), |
|
119 | - 'orderby' => 'id', |
|
120 | - 'order' => 'DESC', |
|
121 | - 'offset' => '', |
|
122 | - 'number' => 10, |
|
123 | - 'paged' => 1, |
|
124 | - 'count_total' => true, |
|
125 | - 'fields' => 'all', |
|
126 | - 's' => '', |
|
127 | - ); |
|
128 | - |
|
129 | - foreach ( GetPaid_Customer_Data_Store::get_database_fields() as $field => $type ) { |
|
130 | - $defaults[ $field ] = 'any'; |
|
131 | - |
|
132 | - if ( '%f' === $type || '%d' === $type ) { |
|
133 | - $defaults[ $field . '_min' ] = ''; |
|
134 | - $defaults[ $field . '_max' ] = ''; |
|
135 | - } |
|
136 | - } |
|
137 | - |
|
138 | - return wp_parse_args( $args, $defaults ); |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * Prepare the query variables. |
|
143 | - * |
|
144 | - * @since 1.0.19 |
|
145 | - * |
|
146 | - * @see self::fill_query_vars() For allowede args and their defaults. |
|
147 | - */ |
|
148 | - public function prepare_query( $query = array() ) { |
|
149 | - global $wpdb; |
|
150 | - |
|
151 | - if ( empty( $this->query_vars ) || ! empty( $query ) ) { |
|
152 | - $this->query_limit = null; |
|
153 | - $this->query_vars = $this->fill_query_vars( $query ); |
|
154 | - } |
|
155 | - |
|
156 | - if ( ! empty( $this->query_vars['fields'] ) && 'all' !== $this->query_vars['fields'] ) { |
|
157 | - $this->query_vars['fields'] = wpinv_parse_list( $this->query_vars['fields'] ); |
|
158 | - } |
|
159 | - |
|
160 | - do_action( 'getpaid_pre_get_customers', array( &$this ) ); |
|
161 | - |
|
162 | - // Ensure that query vars are filled after 'getpaid_pre_get_customers'. |
|
163 | - $qv = & $this->query_vars; |
|
164 | - $qv = $this->fill_query_vars( $qv ); |
|
165 | - $table = $wpdb->prefix . 'getpaid_customers'; |
|
166 | - $this->query_from = "FROM $table"; |
|
167 | - |
|
168 | - // Prepare query fields. |
|
169 | - $this->prepare_query_fields( $qv, $table ); |
|
170 | - |
|
171 | - // Prepare query where. |
|
172 | - $this->prepare_query_where( $qv, $table ); |
|
173 | - |
|
174 | - // Prepare query order. |
|
175 | - $this->prepare_query_order( $qv, $table ); |
|
176 | - |
|
177 | - // limit |
|
178 | - if ( isset( $qv['number'] ) && $qv['number'] > 0 ) { |
|
179 | - if ( $qv['offset'] ) { |
|
180 | - $this->query_limit = $wpdb->prepare( 'LIMIT %d, %d', $qv['offset'], $qv['number'] ); |
|
181 | - } else { |
|
182 | - $this->query_limit = $wpdb->prepare( 'LIMIT %d, %d', $qv['number'] * ( $qv['paged'] - 1 ), $qv['number'] ); |
|
183 | - } |
|
184 | - } |
|
185 | - |
|
186 | - do_action_ref_array( 'getpaid_after_customers_query', array( &$this ) ); |
|
187 | - } |
|
188 | - |
|
189 | - /** |
|
190 | - * Prepares the query fields. |
|
191 | - * |
|
192 | - * @since 1.0.19 |
|
193 | - * |
|
194 | - * @param array $qv Query vars. |
|
195 | - * @param string $table Table name. |
|
196 | - */ |
|
197 | - protected function prepare_query_fields( &$qv, $table ) { |
|
198 | - |
|
199 | - if ( is_array( $qv['fields'] ) ) { |
|
200 | - $qv['fields'] = array_unique( $qv['fields'] ); |
|
201 | - $allowed_fields = array_keys( GetPaid_Customer_Data_Store::get_database_fields() ); |
|
202 | - |
|
203 | - $query_fields = array(); |
|
204 | - foreach ( $qv['fields'] as $field ) { |
|
205 | - if ( ! in_array( $field, $allowed_fields ) ) { |
|
206 | - continue; |
|
207 | - } |
|
208 | - |
|
209 | - $field = sanitize_key( $field ); |
|
210 | - $query_fields[] = "$table.`$field`"; |
|
211 | - } |
|
212 | - $this->query_fields = implode( ',', $query_fields ); |
|
213 | - } else { |
|
214 | - $this->query_fields = "$table.*"; |
|
215 | - } |
|
216 | - |
|
217 | - if ( isset( $qv['count_total'] ) && $qv['count_total'] ) { |
|
218 | - $this->query_fields = 'SQL_CALC_FOUND_ROWS ' . $this->query_fields; |
|
219 | - } |
|
220 | - |
|
221 | - } |
|
222 | - |
|
223 | - /** |
|
224 | - * Prepares the query where. |
|
225 | - * |
|
226 | - * @since 1.0.19 |
|
227 | - * |
|
228 | - * @param array $qv Query vars. |
|
229 | - * @param string $table Table name. |
|
230 | - */ |
|
231 | - protected function prepare_query_where( &$qv, $table ) { |
|
232 | - global $wpdb; |
|
233 | - $this->query_where = 'WHERE 1=1'; |
|
234 | - |
|
235 | - // Fields. |
|
236 | - foreach ( GetPaid_Customer_Data_Store::get_database_fields() as $field => $type ) { |
|
237 | - if ( 'any' !== $qv[ $field ] ) { |
|
238 | - |
|
239 | - // In. |
|
240 | - if ( is_array( $qv[ $field ] ) ) { |
|
241 | - $in = join( ',', array_fill( 0, count( $qv[ $field ] ), $type ) ); |
|
242 | - $this->query_where .= $wpdb->prepare( " AND $table.`status` IN ( $in )", $qv[ $field ] ); |
|
243 | - } elseif ( ! empty( $qv[ $field ] ) ) { |
|
244 | - $this->query_where .= $wpdb->prepare( " AND $table.`$field` = $type", $qv[ $field ] ); |
|
245 | - } |
|
246 | - } |
|
247 | - |
|
248 | - // Min/Max. |
|
249 | - if ( '%f' === $type || '%d' === $type ) { |
|
250 | - |
|
251 | - // Min. |
|
252 | - if ( is_numeric( $qv[ $field . '_min' ] ) ) { |
|
253 | - $this->query_where .= $wpdb->prepare( " AND $table.`$field` >= $type", $qv[ $field . '_min' ] ); |
|
254 | - } |
|
255 | - |
|
256 | - // Max. |
|
257 | - if ( is_numeric( $qv[ $field . '_max' ] ) ) { |
|
258 | - $this->query_where .= $wpdb->prepare( " AND $table.`$field` <= $type", $qv[ $field . '_max' ] ); |
|
259 | - } |
|
260 | - } |
|
261 | - } |
|
262 | - |
|
263 | - if ( ! empty( $qv['include'] ) ) { |
|
264 | - $include = implode( ',', wp_parse_id_list( $qv['include'] ) ); |
|
265 | - $this->query_where .= " AND $table.`id` IN ($include)"; |
|
266 | - } elseif ( ! empty( $qv['exclude'] ) ) { |
|
267 | - $exclude = implode( ',', wp_parse_id_list( $qv['exclude'] ) ); |
|
268 | - $this->query_where .= " AND $table.`id` NOT IN ($exclude)"; |
|
269 | - } |
|
270 | - |
|
271 | - // Date queries are allowed for the customer creation date. |
|
272 | - if ( ! empty( $qv['date_created_query'] ) && is_array( $qv['date_created_query'] ) ) { |
|
273 | - $date_created_query = new WP_Date_Query( $qv['date_created_query'], "$table.date_created" ); |
|
274 | - $this->query_where .= $date_created_query->get_sql(); |
|
275 | - } |
|
276 | - |
|
277 | - // Search. |
|
278 | - if ( ! empty( $qv['s'] ) ) { |
|
279 | - $this->query_where .= $this->get_search_sql( $qv['s'] ); |
|
280 | - } |
|
281 | - } |
|
282 | - |
|
283 | - /** |
|
284 | - * Used internally to generate an SQL string for searching across multiple columns |
|
285 | - * |
|
286 | - * @since 1.2.7 |
|
287 | - * |
|
288 | - * @global wpdb $wpdb WordPress database abstraction object. |
|
289 | - * |
|
290 | - * @param string $string The string to search for. |
|
291 | - * @return string |
|
292 | - */ |
|
293 | - protected function get_search_sql( $string ) { |
|
294 | - global $wpdb; |
|
295 | - |
|
296 | - $searches = array(); |
|
297 | - $string = trim( $string, '%' ); |
|
298 | - $like = '%' . $wpdb->esc_like( $string ) . '%'; |
|
299 | - |
|
300 | - foreach ( array_keys( GetPaid_Customer_Data_Store::get_database_fields() ) as $col ) { |
|
301 | - if ( 'id' === $col || 'user_id' === $col ) { |
|
302 | - $searches[] = $wpdb->prepare( "$col = %s", $string ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
|
303 | - } else { |
|
304 | - $searches[] = $wpdb->prepare( "$col LIKE %s", $like ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
|
305 | - } |
|
306 | - } |
|
307 | - |
|
308 | - return ' AND (' . implode( ' OR ', $searches ) . ')'; |
|
309 | - } |
|
310 | - |
|
311 | - /** |
|
312 | - * Prepares the query order. |
|
313 | - * |
|
314 | - * @since 1.0.19 |
|
315 | - * |
|
316 | - * @param array $qv Query vars. |
|
317 | - * @param string $table Table name. |
|
318 | - */ |
|
319 | - protected function prepare_query_order( &$qv, $table ) { |
|
320 | - |
|
321 | - // sorting. |
|
322 | - $qv['order'] = isset( $qv['order'] ) ? strtoupper( $qv['order'] ) : ''; |
|
323 | - $order = $this->parse_order( $qv['order'] ); |
|
324 | - |
|
325 | - // Default order is by 'id' (latest customers). |
|
326 | - if ( empty( $qv['orderby'] ) ) { |
|
327 | - $qv['orderby'] = array( 'id' ); |
|
328 | - } |
|
329 | - |
|
330 | - // 'orderby' values may be an array, comma- or space-separated list. |
|
331 | - $ordersby = array_filter( wpinv_parse_list( $qv['orderby'] ) ); |
|
332 | - |
|
333 | - $orderby_array = array(); |
|
334 | - foreach ( $ordersby as $_key => $_value ) { |
|
335 | - |
|
336 | - if ( is_int( $_key ) ) { |
|
337 | - // Integer key means this is a flat array of 'orderby' fields. |
|
338 | - $_orderby = $_value; |
|
339 | - $_order = $order; |
|
340 | - } else { |
|
341 | - // Non-integer key means that the key is the field and the value is ASC/DESC. |
|
342 | - $_orderby = $_key; |
|
343 | - $_order = $_value; |
|
344 | - } |
|
345 | - |
|
346 | - $parsed = $this->parse_orderby( $_orderby, $table ); |
|
347 | - |
|
348 | - if ( $parsed ) { |
|
349 | - $orderby_array[] = $parsed . ' ' . $this->parse_order( $_order ); |
|
350 | - } |
|
351 | - } |
|
352 | - |
|
353 | - // If no valid clauses were found, order by id. |
|
354 | - if ( empty( $orderby_array ) ) { |
|
355 | - $orderby_array[] = "id $order"; |
|
356 | - } |
|
357 | - |
|
358 | - $this->query_orderby = 'ORDER BY ' . implode( ', ', $orderby_array ); |
|
359 | - |
|
360 | - } |
|
361 | - |
|
362 | - /** |
|
363 | - * Execute the query, with the current variables. |
|
364 | - * |
|
365 | - * @since 1.0.19 |
|
366 | - * |
|
367 | - * @global wpdb $wpdb WordPress database abstraction object. |
|
368 | - */ |
|
369 | - public function query() { |
|
370 | - global $wpdb; |
|
371 | - |
|
372 | - $qv =& $this->query_vars; |
|
373 | - |
|
374 | - // Return a non-null value to bypass the default GetPaid customers query and remember to set the |
|
375 | - // total_customers property. |
|
376 | - $this->results = apply_filters_ref_array( 'getpaid_customers_pre_query', array( null, &$this ) ); |
|
377 | - |
|
378 | - if ( null === $this->results ) { |
|
379 | - $this->request = "SELECT $this->query_fields $this->query_from $this->query_where $this->query_orderby $this->query_limit"; |
|
380 | - |
|
381 | - if ( ( is_array( $qv['fields'] ) && 1 !== count( $qv['fields'] ) ) || 'all' === $qv['fields'] ) { |
|
382 | - $this->results = $wpdb->get_results( $this->request ); |
|
383 | - } else { |
|
384 | - $this->results = $wpdb->get_col( $this->request ); |
|
385 | - } |
|
386 | - |
|
387 | - if ( isset( $qv['count_total'] ) && $qv['count_total'] ) { |
|
388 | - $found_customers_query = apply_filters( 'getpaid_found_customers_query', 'SELECT FOUND_ROWS()', $this ); |
|
389 | - $this->total_customers = (int) $wpdb->get_var( $found_customers_query ); |
|
390 | - } |
|
391 | - } |
|
392 | - |
|
393 | - if ( 'all' === $qv['fields'] ) { |
|
394 | - foreach ( $this->results as $key => $customer ) { |
|
395 | - $this->set_cache( $customer->id, $customer, 'getpaid_customers' ); |
|
396 | - $this->set_cache( $customer->user_id, $customer->id, 'getpaid_customer_ids_by_user_id' ); |
|
397 | - $this->set_cache( $customer->email, $customer->id, 'getpaid_customer_ids_by_email' ); |
|
398 | - $this->results[ $key ] = new GetPaid_Customer( $customer ); |
|
399 | - } |
|
400 | - } |
|
401 | - |
|
402 | - } |
|
403 | - |
|
404 | - /** |
|
405 | - * Set cache |
|
406 | - * |
|
407 | - * @param string $id |
|
408 | - * @param mixed $data |
|
409 | - * @param string $group |
|
410 | - * @param integer $expire |
|
411 | - * @return boolean |
|
412 | - */ |
|
413 | - public function set_cache( $key, $data, $group = '', $expire = 0 ) { |
|
414 | - |
|
415 | - if ( empty( $key ) ) { |
|
416 | - return false; |
|
417 | - } |
|
418 | - |
|
419 | - wp_cache_set( $key, $data, $group, $expire ); |
|
420 | - } |
|
421 | - |
|
422 | - /** |
|
423 | - * Retrieve query variable. |
|
424 | - * |
|
425 | - * @since 1.0.19 |
|
426 | - * |
|
427 | - * @param string $query_var Query variable key. |
|
428 | - * @return mixed |
|
429 | - */ |
|
430 | - public function get( $query_var ) { |
|
431 | - if ( isset( $this->query_vars[ $query_var ] ) ) { |
|
432 | - return $this->query_vars[ $query_var ]; |
|
433 | - } |
|
434 | - |
|
435 | - return null; |
|
436 | - } |
|
437 | - |
|
438 | - /** |
|
439 | - * Set query variable. |
|
440 | - * |
|
441 | - * @since 1.0.19 |
|
442 | - * |
|
443 | - * @param string $query_var Query variable key. |
|
444 | - * @param mixed $value Query variable value. |
|
445 | - */ |
|
446 | - public function set( $query_var, $value ) { |
|
447 | - $this->query_vars[ $query_var ] = $value; |
|
448 | - } |
|
449 | - |
|
450 | - /** |
|
451 | - * Return the list of customers. |
|
452 | - * |
|
453 | - * @since 1.0.19 |
|
454 | - * |
|
455 | - * @return GetPaid_Customer[]|array Found customers. |
|
456 | - */ |
|
457 | - public function get_results() { |
|
458 | - return $this->results; |
|
459 | - } |
|
460 | - |
|
461 | - /** |
|
462 | - * Return the total number of customers for the current query. |
|
463 | - * |
|
464 | - * @since 1.0.19 |
|
465 | - * |
|
466 | - * @return int Number of total customers. |
|
467 | - */ |
|
468 | - public function get_total() { |
|
469 | - return $this->total_customers; |
|
470 | - } |
|
471 | - |
|
472 | - /** |
|
473 | - * Parse and sanitize 'orderby' keys passed to the customers query. |
|
474 | - * |
|
475 | - * @since 1.0.19 |
|
476 | - * |
|
477 | - * @param string $orderby Alias for the field to order by. |
|
478 | - * @param string $table The current table. |
|
479 | - * @return string Value to use in the ORDER clause, if `$orderby` is valid. |
|
480 | - */ |
|
481 | - protected function parse_orderby( $orderby, $table ) { |
|
482 | - |
|
483 | - $_orderby = ''; |
|
484 | - if ( in_array( $orderby, array_keys( GetPaid_Customer_Data_Store::get_database_fields() ), true ) ) { |
|
485 | - $_orderby = "$table.`$orderby`"; |
|
486 | - } elseif ( 'id' === strtolower( $orderby ) ) { |
|
487 | - $_orderby = "$table.id"; |
|
488 | - } elseif ( 'include' === $orderby && ! empty( $this->query_vars['include'] ) ) { |
|
489 | - $include = wp_parse_id_list( $this->query_vars['include'] ); |
|
490 | - $include_sql = implode( ',', $include ); |
|
491 | - $_orderby = "FIELD( $table.id, $include_sql )"; |
|
492 | - } |
|
493 | - |
|
494 | - return $_orderby; |
|
495 | - } |
|
496 | - |
|
497 | - /** |
|
498 | - * Parse an 'order' query variable and cast it to ASC or DESC as necessary. |
|
499 | - * |
|
500 | - * @since 1.0.19 |
|
501 | - * |
|
502 | - * @param string $order The 'order' query variable. |
|
503 | - * @return string The sanitized 'order' query variable. |
|
504 | - */ |
|
505 | - protected function parse_order( $order ) { |
|
506 | - if ( ! is_string( $order ) || empty( $order ) ) { |
|
507 | - return 'DESC'; |
|
508 | - } |
|
509 | - |
|
510 | - if ( 'ASC' === strtoupper( $order ) ) { |
|
511 | - return 'ASC'; |
|
512 | - } else { |
|
513 | - return 'DESC'; |
|
514 | - } |
|
515 | - } |
|
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 customers. |
|
29 | + * |
|
30 | + * @since 1.0.19 |
|
31 | + * @var array |
|
32 | + */ |
|
33 | + private $results; |
|
34 | + |
|
35 | + /** |
|
36 | + * Total number of found customers for the current query |
|
37 | + * |
|
38 | + * @since 1.0.19 |
|
39 | + * @var int |
|
40 | + */ |
|
41 | + private $total_customers = 0; |
|
42 | + |
|
43 | + /** |
|
44 | + * The SQL query used to fetch matching customers. |
|
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 | + 'include' => array(), |
|
118 | + 'exclude' => array(), |
|
119 | + 'orderby' => 'id', |
|
120 | + 'order' => 'DESC', |
|
121 | + 'offset' => '', |
|
122 | + 'number' => 10, |
|
123 | + 'paged' => 1, |
|
124 | + 'count_total' => true, |
|
125 | + 'fields' => 'all', |
|
126 | + 's' => '', |
|
127 | + ); |
|
128 | + |
|
129 | + foreach ( GetPaid_Customer_Data_Store::get_database_fields() as $field => $type ) { |
|
130 | + $defaults[ $field ] = 'any'; |
|
131 | + |
|
132 | + if ( '%f' === $type || '%d' === $type ) { |
|
133 | + $defaults[ $field . '_min' ] = ''; |
|
134 | + $defaults[ $field . '_max' ] = ''; |
|
135 | + } |
|
136 | + } |
|
137 | + |
|
138 | + return wp_parse_args( $args, $defaults ); |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * Prepare the query variables. |
|
143 | + * |
|
144 | + * @since 1.0.19 |
|
145 | + * |
|
146 | + * @see self::fill_query_vars() For allowede args and their defaults. |
|
147 | + */ |
|
148 | + public function prepare_query( $query = array() ) { |
|
149 | + global $wpdb; |
|
150 | + |
|
151 | + if ( empty( $this->query_vars ) || ! empty( $query ) ) { |
|
152 | + $this->query_limit = null; |
|
153 | + $this->query_vars = $this->fill_query_vars( $query ); |
|
154 | + } |
|
155 | + |
|
156 | + if ( ! empty( $this->query_vars['fields'] ) && 'all' !== $this->query_vars['fields'] ) { |
|
157 | + $this->query_vars['fields'] = wpinv_parse_list( $this->query_vars['fields'] ); |
|
158 | + } |
|
159 | + |
|
160 | + do_action( 'getpaid_pre_get_customers', array( &$this ) ); |
|
161 | + |
|
162 | + // Ensure that query vars are filled after 'getpaid_pre_get_customers'. |
|
163 | + $qv = & $this->query_vars; |
|
164 | + $qv = $this->fill_query_vars( $qv ); |
|
165 | + $table = $wpdb->prefix . 'getpaid_customers'; |
|
166 | + $this->query_from = "FROM $table"; |
|
167 | + |
|
168 | + // Prepare query fields. |
|
169 | + $this->prepare_query_fields( $qv, $table ); |
|
170 | + |
|
171 | + // Prepare query where. |
|
172 | + $this->prepare_query_where( $qv, $table ); |
|
173 | + |
|
174 | + // Prepare query order. |
|
175 | + $this->prepare_query_order( $qv, $table ); |
|
176 | + |
|
177 | + // limit |
|
178 | + if ( isset( $qv['number'] ) && $qv['number'] > 0 ) { |
|
179 | + if ( $qv['offset'] ) { |
|
180 | + $this->query_limit = $wpdb->prepare( 'LIMIT %d, %d', $qv['offset'], $qv['number'] ); |
|
181 | + } else { |
|
182 | + $this->query_limit = $wpdb->prepare( 'LIMIT %d, %d', $qv['number'] * ( $qv['paged'] - 1 ), $qv['number'] ); |
|
183 | + } |
|
184 | + } |
|
185 | + |
|
186 | + do_action_ref_array( 'getpaid_after_customers_query', array( &$this ) ); |
|
187 | + } |
|
188 | + |
|
189 | + /** |
|
190 | + * Prepares the query fields. |
|
191 | + * |
|
192 | + * @since 1.0.19 |
|
193 | + * |
|
194 | + * @param array $qv Query vars. |
|
195 | + * @param string $table Table name. |
|
196 | + */ |
|
197 | + protected function prepare_query_fields( &$qv, $table ) { |
|
198 | + |
|
199 | + if ( is_array( $qv['fields'] ) ) { |
|
200 | + $qv['fields'] = array_unique( $qv['fields'] ); |
|
201 | + $allowed_fields = array_keys( GetPaid_Customer_Data_Store::get_database_fields() ); |
|
202 | + |
|
203 | + $query_fields = array(); |
|
204 | + foreach ( $qv['fields'] as $field ) { |
|
205 | + if ( ! in_array( $field, $allowed_fields ) ) { |
|
206 | + continue; |
|
207 | + } |
|
208 | + |
|
209 | + $field = sanitize_key( $field ); |
|
210 | + $query_fields[] = "$table.`$field`"; |
|
211 | + } |
|
212 | + $this->query_fields = implode( ',', $query_fields ); |
|
213 | + } else { |
|
214 | + $this->query_fields = "$table.*"; |
|
215 | + } |
|
216 | + |
|
217 | + if ( isset( $qv['count_total'] ) && $qv['count_total'] ) { |
|
218 | + $this->query_fields = 'SQL_CALC_FOUND_ROWS ' . $this->query_fields; |
|
219 | + } |
|
220 | + |
|
221 | + } |
|
222 | + |
|
223 | + /** |
|
224 | + * Prepares the query where. |
|
225 | + * |
|
226 | + * @since 1.0.19 |
|
227 | + * |
|
228 | + * @param array $qv Query vars. |
|
229 | + * @param string $table Table name. |
|
230 | + */ |
|
231 | + protected function prepare_query_where( &$qv, $table ) { |
|
232 | + global $wpdb; |
|
233 | + $this->query_where = 'WHERE 1=1'; |
|
234 | + |
|
235 | + // Fields. |
|
236 | + foreach ( GetPaid_Customer_Data_Store::get_database_fields() as $field => $type ) { |
|
237 | + if ( 'any' !== $qv[ $field ] ) { |
|
238 | + |
|
239 | + // In. |
|
240 | + if ( is_array( $qv[ $field ] ) ) { |
|
241 | + $in = join( ',', array_fill( 0, count( $qv[ $field ] ), $type ) ); |
|
242 | + $this->query_where .= $wpdb->prepare( " AND $table.`status` IN ( $in )", $qv[ $field ] ); |
|
243 | + } elseif ( ! empty( $qv[ $field ] ) ) { |
|
244 | + $this->query_where .= $wpdb->prepare( " AND $table.`$field` = $type", $qv[ $field ] ); |
|
245 | + } |
|
246 | + } |
|
247 | + |
|
248 | + // Min/Max. |
|
249 | + if ( '%f' === $type || '%d' === $type ) { |
|
250 | + |
|
251 | + // Min. |
|
252 | + if ( is_numeric( $qv[ $field . '_min' ] ) ) { |
|
253 | + $this->query_where .= $wpdb->prepare( " AND $table.`$field` >= $type", $qv[ $field . '_min' ] ); |
|
254 | + } |
|
255 | + |
|
256 | + // Max. |
|
257 | + if ( is_numeric( $qv[ $field . '_max' ] ) ) { |
|
258 | + $this->query_where .= $wpdb->prepare( " AND $table.`$field` <= $type", $qv[ $field . '_max' ] ); |
|
259 | + } |
|
260 | + } |
|
261 | + } |
|
262 | + |
|
263 | + if ( ! empty( $qv['include'] ) ) { |
|
264 | + $include = implode( ',', wp_parse_id_list( $qv['include'] ) ); |
|
265 | + $this->query_where .= " AND $table.`id` IN ($include)"; |
|
266 | + } elseif ( ! empty( $qv['exclude'] ) ) { |
|
267 | + $exclude = implode( ',', wp_parse_id_list( $qv['exclude'] ) ); |
|
268 | + $this->query_where .= " AND $table.`id` NOT IN ($exclude)"; |
|
269 | + } |
|
270 | + |
|
271 | + // Date queries are allowed for the customer creation date. |
|
272 | + if ( ! empty( $qv['date_created_query'] ) && is_array( $qv['date_created_query'] ) ) { |
|
273 | + $date_created_query = new WP_Date_Query( $qv['date_created_query'], "$table.date_created" ); |
|
274 | + $this->query_where .= $date_created_query->get_sql(); |
|
275 | + } |
|
276 | + |
|
277 | + // Search. |
|
278 | + if ( ! empty( $qv['s'] ) ) { |
|
279 | + $this->query_where .= $this->get_search_sql( $qv['s'] ); |
|
280 | + } |
|
281 | + } |
|
282 | + |
|
283 | + /** |
|
284 | + * Used internally to generate an SQL string for searching across multiple columns |
|
285 | + * |
|
286 | + * @since 1.2.7 |
|
287 | + * |
|
288 | + * @global wpdb $wpdb WordPress database abstraction object. |
|
289 | + * |
|
290 | + * @param string $string The string to search for. |
|
291 | + * @return string |
|
292 | + */ |
|
293 | + protected function get_search_sql( $string ) { |
|
294 | + global $wpdb; |
|
295 | + |
|
296 | + $searches = array(); |
|
297 | + $string = trim( $string, '%' ); |
|
298 | + $like = '%' . $wpdb->esc_like( $string ) . '%'; |
|
299 | + |
|
300 | + foreach ( array_keys( GetPaid_Customer_Data_Store::get_database_fields() ) as $col ) { |
|
301 | + if ( 'id' === $col || 'user_id' === $col ) { |
|
302 | + $searches[] = $wpdb->prepare( "$col = %s", $string ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
|
303 | + } else { |
|
304 | + $searches[] = $wpdb->prepare( "$col LIKE %s", $like ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
|
305 | + } |
|
306 | + } |
|
307 | + |
|
308 | + return ' AND (' . implode( ' OR ', $searches ) . ')'; |
|
309 | + } |
|
310 | + |
|
311 | + /** |
|
312 | + * Prepares the query order. |
|
313 | + * |
|
314 | + * @since 1.0.19 |
|
315 | + * |
|
316 | + * @param array $qv Query vars. |
|
317 | + * @param string $table Table name. |
|
318 | + */ |
|
319 | + protected function prepare_query_order( &$qv, $table ) { |
|
320 | + |
|
321 | + // sorting. |
|
322 | + $qv['order'] = isset( $qv['order'] ) ? strtoupper( $qv['order'] ) : ''; |
|
323 | + $order = $this->parse_order( $qv['order'] ); |
|
324 | + |
|
325 | + // Default order is by 'id' (latest customers). |
|
326 | + if ( empty( $qv['orderby'] ) ) { |
|
327 | + $qv['orderby'] = array( 'id' ); |
|
328 | + } |
|
329 | + |
|
330 | + // 'orderby' values may be an array, comma- or space-separated list. |
|
331 | + $ordersby = array_filter( wpinv_parse_list( $qv['orderby'] ) ); |
|
332 | + |
|
333 | + $orderby_array = array(); |
|
334 | + foreach ( $ordersby as $_key => $_value ) { |
|
335 | + |
|
336 | + if ( is_int( $_key ) ) { |
|
337 | + // Integer key means this is a flat array of 'orderby' fields. |
|
338 | + $_orderby = $_value; |
|
339 | + $_order = $order; |
|
340 | + } else { |
|
341 | + // Non-integer key means that the key is the field and the value is ASC/DESC. |
|
342 | + $_orderby = $_key; |
|
343 | + $_order = $_value; |
|
344 | + } |
|
345 | + |
|
346 | + $parsed = $this->parse_orderby( $_orderby, $table ); |
|
347 | + |
|
348 | + if ( $parsed ) { |
|
349 | + $orderby_array[] = $parsed . ' ' . $this->parse_order( $_order ); |
|
350 | + } |
|
351 | + } |
|
352 | + |
|
353 | + // If no valid clauses were found, order by id. |
|
354 | + if ( empty( $orderby_array ) ) { |
|
355 | + $orderby_array[] = "id $order"; |
|
356 | + } |
|
357 | + |
|
358 | + $this->query_orderby = 'ORDER BY ' . implode( ', ', $orderby_array ); |
|
359 | + |
|
360 | + } |
|
361 | + |
|
362 | + /** |
|
363 | + * Execute the query, with the current variables. |
|
364 | + * |
|
365 | + * @since 1.0.19 |
|
366 | + * |
|
367 | + * @global wpdb $wpdb WordPress database abstraction object. |
|
368 | + */ |
|
369 | + public function query() { |
|
370 | + global $wpdb; |
|
371 | + |
|
372 | + $qv =& $this->query_vars; |
|
373 | + |
|
374 | + // Return a non-null value to bypass the default GetPaid customers query and remember to set the |
|
375 | + // total_customers property. |
|
376 | + $this->results = apply_filters_ref_array( 'getpaid_customers_pre_query', array( null, &$this ) ); |
|
377 | + |
|
378 | + if ( null === $this->results ) { |
|
379 | + $this->request = "SELECT $this->query_fields $this->query_from $this->query_where $this->query_orderby $this->query_limit"; |
|
380 | + |
|
381 | + if ( ( is_array( $qv['fields'] ) && 1 !== count( $qv['fields'] ) ) || 'all' === $qv['fields'] ) { |
|
382 | + $this->results = $wpdb->get_results( $this->request ); |
|
383 | + } else { |
|
384 | + $this->results = $wpdb->get_col( $this->request ); |
|
385 | + } |
|
386 | + |
|
387 | + if ( isset( $qv['count_total'] ) && $qv['count_total'] ) { |
|
388 | + $found_customers_query = apply_filters( 'getpaid_found_customers_query', 'SELECT FOUND_ROWS()', $this ); |
|
389 | + $this->total_customers = (int) $wpdb->get_var( $found_customers_query ); |
|
390 | + } |
|
391 | + } |
|
392 | + |
|
393 | + if ( 'all' === $qv['fields'] ) { |
|
394 | + foreach ( $this->results as $key => $customer ) { |
|
395 | + $this->set_cache( $customer->id, $customer, 'getpaid_customers' ); |
|
396 | + $this->set_cache( $customer->user_id, $customer->id, 'getpaid_customer_ids_by_user_id' ); |
|
397 | + $this->set_cache( $customer->email, $customer->id, 'getpaid_customer_ids_by_email' ); |
|
398 | + $this->results[ $key ] = new GetPaid_Customer( $customer ); |
|
399 | + } |
|
400 | + } |
|
401 | + |
|
402 | + } |
|
403 | + |
|
404 | + /** |
|
405 | + * Set cache |
|
406 | + * |
|
407 | + * @param string $id |
|
408 | + * @param mixed $data |
|
409 | + * @param string $group |
|
410 | + * @param integer $expire |
|
411 | + * @return boolean |
|
412 | + */ |
|
413 | + public function set_cache( $key, $data, $group = '', $expire = 0 ) { |
|
414 | + |
|
415 | + if ( empty( $key ) ) { |
|
416 | + return false; |
|
417 | + } |
|
418 | + |
|
419 | + wp_cache_set( $key, $data, $group, $expire ); |
|
420 | + } |
|
421 | + |
|
422 | + /** |
|
423 | + * Retrieve query variable. |
|
424 | + * |
|
425 | + * @since 1.0.19 |
|
426 | + * |
|
427 | + * @param string $query_var Query variable key. |
|
428 | + * @return mixed |
|
429 | + */ |
|
430 | + public function get( $query_var ) { |
|
431 | + if ( isset( $this->query_vars[ $query_var ] ) ) { |
|
432 | + return $this->query_vars[ $query_var ]; |
|
433 | + } |
|
434 | + |
|
435 | + return null; |
|
436 | + } |
|
437 | + |
|
438 | + /** |
|
439 | + * Set query variable. |
|
440 | + * |
|
441 | + * @since 1.0.19 |
|
442 | + * |
|
443 | + * @param string $query_var Query variable key. |
|
444 | + * @param mixed $value Query variable value. |
|
445 | + */ |
|
446 | + public function set( $query_var, $value ) { |
|
447 | + $this->query_vars[ $query_var ] = $value; |
|
448 | + } |
|
449 | + |
|
450 | + /** |
|
451 | + * Return the list of customers. |
|
452 | + * |
|
453 | + * @since 1.0.19 |
|
454 | + * |
|
455 | + * @return GetPaid_Customer[]|array Found customers. |
|
456 | + */ |
|
457 | + public function get_results() { |
|
458 | + return $this->results; |
|
459 | + } |
|
460 | + |
|
461 | + /** |
|
462 | + * Return the total number of customers for the current query. |
|
463 | + * |
|
464 | + * @since 1.0.19 |
|
465 | + * |
|
466 | + * @return int Number of total customers. |
|
467 | + */ |
|
468 | + public function get_total() { |
|
469 | + return $this->total_customers; |
|
470 | + } |
|
471 | + |
|
472 | + /** |
|
473 | + * Parse and sanitize 'orderby' keys passed to the customers query. |
|
474 | + * |
|
475 | + * @since 1.0.19 |
|
476 | + * |
|
477 | + * @param string $orderby Alias for the field to order by. |
|
478 | + * @param string $table The current table. |
|
479 | + * @return string Value to use in the ORDER clause, if `$orderby` is valid. |
|
480 | + */ |
|
481 | + protected function parse_orderby( $orderby, $table ) { |
|
482 | + |
|
483 | + $_orderby = ''; |
|
484 | + if ( in_array( $orderby, array_keys( GetPaid_Customer_Data_Store::get_database_fields() ), true ) ) { |
|
485 | + $_orderby = "$table.`$orderby`"; |
|
486 | + } elseif ( 'id' === strtolower( $orderby ) ) { |
|
487 | + $_orderby = "$table.id"; |
|
488 | + } elseif ( 'include' === $orderby && ! empty( $this->query_vars['include'] ) ) { |
|
489 | + $include = wp_parse_id_list( $this->query_vars['include'] ); |
|
490 | + $include_sql = implode( ',', $include ); |
|
491 | + $_orderby = "FIELD( $table.id, $include_sql )"; |
|
492 | + } |
|
493 | + |
|
494 | + return $_orderby; |
|
495 | + } |
|
496 | + |
|
497 | + /** |
|
498 | + * Parse an 'order' query variable and cast it to ASC or DESC as necessary. |
|
499 | + * |
|
500 | + * @since 1.0.19 |
|
501 | + * |
|
502 | + * @param string $order The 'order' query variable. |
|
503 | + * @return string The sanitized 'order' query variable. |
|
504 | + */ |
|
505 | + protected function parse_order( $order ) { |
|
506 | + if ( ! is_string( $order ) || empty( $order ) ) { |
|
507 | + return 'DESC'; |
|
508 | + } |
|
509 | + |
|
510 | + if ( 'ASC' === strtoupper( $order ) ) { |
|
511 | + return 'ASC'; |
|
512 | + } else { |
|
513 | + return 'DESC'; |
|
514 | + } |
|
515 | + } |
|
516 | 516 | |
517 | 517 | } |
@@ -97,9 +97,9 @@ discard block |
||
97 | 97 | * |
98 | 98 | * @param null|string|array $query Optional. The query variables. |
99 | 99 | */ |
100 | - public function __construct( $query = null ) { |
|
101 | - if ( ! is_null( $query ) ) { |
|
102 | - $this->prepare_query( $query ); |
|
100 | + public function __construct($query = null) { |
|
101 | + if (!is_null($query)) { |
|
102 | + $this->prepare_query($query); |
|
103 | 103 | $this->query(); |
104 | 104 | } |
105 | 105 | } |
@@ -112,7 +112,7 @@ discard block |
||
112 | 112 | * @param string|array $args Query vars, as passed to `GetPaid_Subscriptions_Query`. |
113 | 113 | * @return array Complete query variables with undefined ones filled in with defaults. |
114 | 114 | */ |
115 | - public static function fill_query_vars( $args ) { |
|
115 | + public static function fill_query_vars($args) { |
|
116 | 116 | $defaults = array( |
117 | 117 | 'include' => array(), |
118 | 118 | 'exclude' => array(), |
@@ -126,16 +126,16 @@ discard block |
||
126 | 126 | 's' => '', |
127 | 127 | ); |
128 | 128 | |
129 | - foreach ( GetPaid_Customer_Data_Store::get_database_fields() as $field => $type ) { |
|
130 | - $defaults[ $field ] = 'any'; |
|
129 | + foreach (GetPaid_Customer_Data_Store::get_database_fields() as $field => $type) { |
|
130 | + $defaults[$field] = 'any'; |
|
131 | 131 | |
132 | - if ( '%f' === $type || '%d' === $type ) { |
|
133 | - $defaults[ $field . '_min' ] = ''; |
|
134 | - $defaults[ $field . '_max' ] = ''; |
|
132 | + if ('%f' === $type || '%d' === $type) { |
|
133 | + $defaults[$field . '_min'] = ''; |
|
134 | + $defaults[$field . '_max'] = ''; |
|
135 | 135 | } |
136 | 136 | } |
137 | 137 | |
138 | - return wp_parse_args( $args, $defaults ); |
|
138 | + return wp_parse_args($args, $defaults); |
|
139 | 139 | } |
140 | 140 | |
141 | 141 | /** |
@@ -145,45 +145,45 @@ discard block |
||
145 | 145 | * |
146 | 146 | * @see self::fill_query_vars() For allowede args and their defaults. |
147 | 147 | */ |
148 | - public function prepare_query( $query = array() ) { |
|
148 | + public function prepare_query($query = array()) { |
|
149 | 149 | global $wpdb; |
150 | 150 | |
151 | - if ( empty( $this->query_vars ) || ! empty( $query ) ) { |
|
151 | + if (empty($this->query_vars) || !empty($query)) { |
|
152 | 152 | $this->query_limit = null; |
153 | - $this->query_vars = $this->fill_query_vars( $query ); |
|
153 | + $this->query_vars = $this->fill_query_vars($query); |
|
154 | 154 | } |
155 | 155 | |
156 | - if ( ! empty( $this->query_vars['fields'] ) && 'all' !== $this->query_vars['fields'] ) { |
|
157 | - $this->query_vars['fields'] = wpinv_parse_list( $this->query_vars['fields'] ); |
|
156 | + if (!empty($this->query_vars['fields']) && 'all' !== $this->query_vars['fields']) { |
|
157 | + $this->query_vars['fields'] = wpinv_parse_list($this->query_vars['fields']); |
|
158 | 158 | } |
159 | 159 | |
160 | - do_action( 'getpaid_pre_get_customers', array( &$this ) ); |
|
160 | + do_action('getpaid_pre_get_customers', array(&$this)); |
|
161 | 161 | |
162 | 162 | // Ensure that query vars are filled after 'getpaid_pre_get_customers'. |
163 | 163 | $qv = & $this->query_vars; |
164 | - $qv = $this->fill_query_vars( $qv ); |
|
164 | + $qv = $this->fill_query_vars($qv); |
|
165 | 165 | $table = $wpdb->prefix . 'getpaid_customers'; |
166 | 166 | $this->query_from = "FROM $table"; |
167 | 167 | |
168 | 168 | // Prepare query fields. |
169 | - $this->prepare_query_fields( $qv, $table ); |
|
169 | + $this->prepare_query_fields($qv, $table); |
|
170 | 170 | |
171 | 171 | // Prepare query where. |
172 | - $this->prepare_query_where( $qv, $table ); |
|
172 | + $this->prepare_query_where($qv, $table); |
|
173 | 173 | |
174 | 174 | // Prepare query order. |
175 | - $this->prepare_query_order( $qv, $table ); |
|
175 | + $this->prepare_query_order($qv, $table); |
|
176 | 176 | |
177 | 177 | // limit |
178 | - if ( isset( $qv['number'] ) && $qv['number'] > 0 ) { |
|
179 | - if ( $qv['offset'] ) { |
|
180 | - $this->query_limit = $wpdb->prepare( 'LIMIT %d, %d', $qv['offset'], $qv['number'] ); |
|
178 | + if (isset($qv['number']) && $qv['number'] > 0) { |
|
179 | + if ($qv['offset']) { |
|
180 | + $this->query_limit = $wpdb->prepare('LIMIT %d, %d', $qv['offset'], $qv['number']); |
|
181 | 181 | } else { |
182 | - $this->query_limit = $wpdb->prepare( 'LIMIT %d, %d', $qv['number'] * ( $qv['paged'] - 1 ), $qv['number'] ); |
|
182 | + $this->query_limit = $wpdb->prepare('LIMIT %d, %d', $qv['number'] * ($qv['paged'] - 1), $qv['number']); |
|
183 | 183 | } |
184 | 184 | } |
185 | 185 | |
186 | - do_action_ref_array( 'getpaid_after_customers_query', array( &$this ) ); |
|
186 | + do_action_ref_array('getpaid_after_customers_query', array(&$this)); |
|
187 | 187 | } |
188 | 188 | |
189 | 189 | /** |
@@ -194,27 +194,27 @@ discard block |
||
194 | 194 | * @param array $qv Query vars. |
195 | 195 | * @param string $table Table name. |
196 | 196 | */ |
197 | - protected function prepare_query_fields( &$qv, $table ) { |
|
197 | + protected function prepare_query_fields(&$qv, $table) { |
|
198 | 198 | |
199 | - if ( is_array( $qv['fields'] ) ) { |
|
200 | - $qv['fields'] = array_unique( $qv['fields'] ); |
|
201 | - $allowed_fields = array_keys( GetPaid_Customer_Data_Store::get_database_fields() ); |
|
199 | + if (is_array($qv['fields'])) { |
|
200 | + $qv['fields'] = array_unique($qv['fields']); |
|
201 | + $allowed_fields = array_keys(GetPaid_Customer_Data_Store::get_database_fields()); |
|
202 | 202 | |
203 | 203 | $query_fields = array(); |
204 | - foreach ( $qv['fields'] as $field ) { |
|
205 | - if ( ! in_array( $field, $allowed_fields ) ) { |
|
204 | + foreach ($qv['fields'] as $field) { |
|
205 | + if (!in_array($field, $allowed_fields)) { |
|
206 | 206 | continue; |
207 | 207 | } |
208 | 208 | |
209 | - $field = sanitize_key( $field ); |
|
209 | + $field = sanitize_key($field); |
|
210 | 210 | $query_fields[] = "$table.`$field`"; |
211 | 211 | } |
212 | - $this->query_fields = implode( ',', $query_fields ); |
|
212 | + $this->query_fields = implode(',', $query_fields); |
|
213 | 213 | } else { |
214 | 214 | $this->query_fields = "$table.*"; |
215 | 215 | } |
216 | 216 | |
217 | - if ( isset( $qv['count_total'] ) && $qv['count_total'] ) { |
|
217 | + if (isset($qv['count_total']) && $qv['count_total']) { |
|
218 | 218 | $this->query_fields = 'SQL_CALC_FOUND_ROWS ' . $this->query_fields; |
219 | 219 | } |
220 | 220 | |
@@ -228,55 +228,55 @@ discard block |
||
228 | 228 | * @param array $qv Query vars. |
229 | 229 | * @param string $table Table name. |
230 | 230 | */ |
231 | - protected function prepare_query_where( &$qv, $table ) { |
|
231 | + protected function prepare_query_where(&$qv, $table) { |
|
232 | 232 | global $wpdb; |
233 | 233 | $this->query_where = 'WHERE 1=1'; |
234 | 234 | |
235 | 235 | // Fields. |
236 | - foreach ( GetPaid_Customer_Data_Store::get_database_fields() as $field => $type ) { |
|
237 | - if ( 'any' !== $qv[ $field ] ) { |
|
236 | + foreach (GetPaid_Customer_Data_Store::get_database_fields() as $field => $type) { |
|
237 | + if ('any' !== $qv[$field]) { |
|
238 | 238 | |
239 | 239 | // In. |
240 | - if ( is_array( $qv[ $field ] ) ) { |
|
241 | - $in = join( ',', array_fill( 0, count( $qv[ $field ] ), $type ) ); |
|
242 | - $this->query_where .= $wpdb->prepare( " AND $table.`status` IN ( $in )", $qv[ $field ] ); |
|
243 | - } elseif ( ! empty( $qv[ $field ] ) ) { |
|
244 | - $this->query_where .= $wpdb->prepare( " AND $table.`$field` = $type", $qv[ $field ] ); |
|
240 | + if (is_array($qv[$field])) { |
|
241 | + $in = join(',', array_fill(0, count($qv[$field]), $type)); |
|
242 | + $this->query_where .= $wpdb->prepare(" AND $table.`status` IN ( $in )", $qv[$field]); |
|
243 | + } elseif (!empty($qv[$field])) { |
|
244 | + $this->query_where .= $wpdb->prepare(" AND $table.`$field` = $type", $qv[$field]); |
|
245 | 245 | } |
246 | 246 | } |
247 | 247 | |
248 | 248 | // Min/Max. |
249 | - if ( '%f' === $type || '%d' === $type ) { |
|
249 | + if ('%f' === $type || '%d' === $type) { |
|
250 | 250 | |
251 | 251 | // Min. |
252 | - if ( is_numeric( $qv[ $field . '_min' ] ) ) { |
|
253 | - $this->query_where .= $wpdb->prepare( " AND $table.`$field` >= $type", $qv[ $field . '_min' ] ); |
|
252 | + if (is_numeric($qv[$field . '_min'])) { |
|
253 | + $this->query_where .= $wpdb->prepare(" AND $table.`$field` >= $type", $qv[$field . '_min']); |
|
254 | 254 | } |
255 | 255 | |
256 | 256 | // Max. |
257 | - if ( is_numeric( $qv[ $field . '_max' ] ) ) { |
|
258 | - $this->query_where .= $wpdb->prepare( " AND $table.`$field` <= $type", $qv[ $field . '_max' ] ); |
|
257 | + if (is_numeric($qv[$field . '_max'])) { |
|
258 | + $this->query_where .= $wpdb->prepare(" AND $table.`$field` <= $type", $qv[$field . '_max']); |
|
259 | 259 | } |
260 | 260 | } |
261 | 261 | } |
262 | 262 | |
263 | - if ( ! empty( $qv['include'] ) ) { |
|
264 | - $include = implode( ',', wp_parse_id_list( $qv['include'] ) ); |
|
263 | + if (!empty($qv['include'])) { |
|
264 | + $include = implode(',', wp_parse_id_list($qv['include'])); |
|
265 | 265 | $this->query_where .= " AND $table.`id` IN ($include)"; |
266 | - } elseif ( ! empty( $qv['exclude'] ) ) { |
|
267 | - $exclude = implode( ',', wp_parse_id_list( $qv['exclude'] ) ); |
|
266 | + } elseif (!empty($qv['exclude'])) { |
|
267 | + $exclude = implode(',', wp_parse_id_list($qv['exclude'])); |
|
268 | 268 | $this->query_where .= " AND $table.`id` NOT IN ($exclude)"; |
269 | 269 | } |
270 | 270 | |
271 | 271 | // Date queries are allowed for the customer creation date. |
272 | - if ( ! empty( $qv['date_created_query'] ) && is_array( $qv['date_created_query'] ) ) { |
|
273 | - $date_created_query = new WP_Date_Query( $qv['date_created_query'], "$table.date_created" ); |
|
272 | + if (!empty($qv['date_created_query']) && is_array($qv['date_created_query'])) { |
|
273 | + $date_created_query = new WP_Date_Query($qv['date_created_query'], "$table.date_created"); |
|
274 | 274 | $this->query_where .= $date_created_query->get_sql(); |
275 | 275 | } |
276 | 276 | |
277 | 277 | // Search. |
278 | - if ( ! empty( $qv['s'] ) ) { |
|
279 | - $this->query_where .= $this->get_search_sql( $qv['s'] ); |
|
278 | + if (!empty($qv['s'])) { |
|
279 | + $this->query_where .= $this->get_search_sql($qv['s']); |
|
280 | 280 | } |
281 | 281 | } |
282 | 282 | |
@@ -290,22 +290,22 @@ discard block |
||
290 | 290 | * @param string $string The string to search for. |
291 | 291 | * @return string |
292 | 292 | */ |
293 | - protected function get_search_sql( $string ) { |
|
293 | + protected function get_search_sql($string) { |
|
294 | 294 | global $wpdb; |
295 | 295 | |
296 | 296 | $searches = array(); |
297 | - $string = trim( $string, '%' ); |
|
298 | - $like = '%' . $wpdb->esc_like( $string ) . '%'; |
|
297 | + $string = trim($string, '%'); |
|
298 | + $like = '%' . $wpdb->esc_like($string) . '%'; |
|
299 | 299 | |
300 | - foreach ( array_keys( GetPaid_Customer_Data_Store::get_database_fields() ) as $col ) { |
|
301 | - if ( 'id' === $col || 'user_id' === $col ) { |
|
302 | - $searches[] = $wpdb->prepare( "$col = %s", $string ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
|
300 | + foreach (array_keys(GetPaid_Customer_Data_Store::get_database_fields()) as $col) { |
|
301 | + if ('id' === $col || 'user_id' === $col) { |
|
302 | + $searches[] = $wpdb->prepare("$col = %s", $string); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
|
303 | 303 | } else { |
304 | - $searches[] = $wpdb->prepare( "$col LIKE %s", $like ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
|
304 | + $searches[] = $wpdb->prepare("$col LIKE %s", $like); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
|
305 | 305 | } |
306 | 306 | } |
307 | 307 | |
308 | - return ' AND (' . implode( ' OR ', $searches ) . ')'; |
|
308 | + return ' AND (' . implode(' OR ', $searches) . ')'; |
|
309 | 309 | } |
310 | 310 | |
311 | 311 | /** |
@@ -316,24 +316,24 @@ discard block |
||
316 | 316 | * @param array $qv Query vars. |
317 | 317 | * @param string $table Table name. |
318 | 318 | */ |
319 | - protected function prepare_query_order( &$qv, $table ) { |
|
319 | + protected function prepare_query_order(&$qv, $table) { |
|
320 | 320 | |
321 | 321 | // sorting. |
322 | - $qv['order'] = isset( $qv['order'] ) ? strtoupper( $qv['order'] ) : ''; |
|
323 | - $order = $this->parse_order( $qv['order'] ); |
|
322 | + $qv['order'] = isset($qv['order']) ? strtoupper($qv['order']) : ''; |
|
323 | + $order = $this->parse_order($qv['order']); |
|
324 | 324 | |
325 | 325 | // Default order is by 'id' (latest customers). |
326 | - if ( empty( $qv['orderby'] ) ) { |
|
327 | - $qv['orderby'] = array( 'id' ); |
|
326 | + if (empty($qv['orderby'])) { |
|
327 | + $qv['orderby'] = array('id'); |
|
328 | 328 | } |
329 | 329 | |
330 | 330 | // 'orderby' values may be an array, comma- or space-separated list. |
331 | - $ordersby = array_filter( wpinv_parse_list( $qv['orderby'] ) ); |
|
331 | + $ordersby = array_filter(wpinv_parse_list($qv['orderby'])); |
|
332 | 332 | |
333 | 333 | $orderby_array = array(); |
334 | - foreach ( $ordersby as $_key => $_value ) { |
|
334 | + foreach ($ordersby as $_key => $_value) { |
|
335 | 335 | |
336 | - if ( is_int( $_key ) ) { |
|
336 | + if (is_int($_key)) { |
|
337 | 337 | // Integer key means this is a flat array of 'orderby' fields. |
338 | 338 | $_orderby = $_value; |
339 | 339 | $_order = $order; |
@@ -343,19 +343,19 @@ discard block |
||
343 | 343 | $_order = $_value; |
344 | 344 | } |
345 | 345 | |
346 | - $parsed = $this->parse_orderby( $_orderby, $table ); |
|
346 | + $parsed = $this->parse_orderby($_orderby, $table); |
|
347 | 347 | |
348 | - if ( $parsed ) { |
|
349 | - $orderby_array[] = $parsed . ' ' . $this->parse_order( $_order ); |
|
348 | + if ($parsed) { |
|
349 | + $orderby_array[] = $parsed . ' ' . $this->parse_order($_order); |
|
350 | 350 | } |
351 | 351 | } |
352 | 352 | |
353 | 353 | // If no valid clauses were found, order by id. |
354 | - if ( empty( $orderby_array ) ) { |
|
354 | + if (empty($orderby_array)) { |
|
355 | 355 | $orderby_array[] = "id $order"; |
356 | 356 | } |
357 | 357 | |
358 | - $this->query_orderby = 'ORDER BY ' . implode( ', ', $orderby_array ); |
|
358 | + $this->query_orderby = 'ORDER BY ' . implode(', ', $orderby_array); |
|
359 | 359 | |
360 | 360 | } |
361 | 361 | |
@@ -369,33 +369,33 @@ discard block |
||
369 | 369 | public function query() { |
370 | 370 | global $wpdb; |
371 | 371 | |
372 | - $qv =& $this->query_vars; |
|
372 | + $qv = & $this->query_vars; |
|
373 | 373 | |
374 | 374 | // Return a non-null value to bypass the default GetPaid customers query and remember to set the |
375 | 375 | // total_customers property. |
376 | - $this->results = apply_filters_ref_array( 'getpaid_customers_pre_query', array( null, &$this ) ); |
|
376 | + $this->results = apply_filters_ref_array('getpaid_customers_pre_query', array(null, &$this)); |
|
377 | 377 | |
378 | - if ( null === $this->results ) { |
|
378 | + if (null === $this->results) { |
|
379 | 379 | $this->request = "SELECT $this->query_fields $this->query_from $this->query_where $this->query_orderby $this->query_limit"; |
380 | 380 | |
381 | - if ( ( is_array( $qv['fields'] ) && 1 !== count( $qv['fields'] ) ) || 'all' === $qv['fields'] ) { |
|
382 | - $this->results = $wpdb->get_results( $this->request ); |
|
381 | + if ((is_array($qv['fields']) && 1 !== count($qv['fields'])) || 'all' === $qv['fields']) { |
|
382 | + $this->results = $wpdb->get_results($this->request); |
|
383 | 383 | } else { |
384 | - $this->results = $wpdb->get_col( $this->request ); |
|
384 | + $this->results = $wpdb->get_col($this->request); |
|
385 | 385 | } |
386 | 386 | |
387 | - if ( isset( $qv['count_total'] ) && $qv['count_total'] ) { |
|
388 | - $found_customers_query = apply_filters( 'getpaid_found_customers_query', 'SELECT FOUND_ROWS()', $this ); |
|
389 | - $this->total_customers = (int) $wpdb->get_var( $found_customers_query ); |
|
387 | + if (isset($qv['count_total']) && $qv['count_total']) { |
|
388 | + $found_customers_query = apply_filters('getpaid_found_customers_query', 'SELECT FOUND_ROWS()', $this); |
|
389 | + $this->total_customers = (int) $wpdb->get_var($found_customers_query); |
|
390 | 390 | } |
391 | 391 | } |
392 | 392 | |
393 | - if ( 'all' === $qv['fields'] ) { |
|
394 | - foreach ( $this->results as $key => $customer ) { |
|
395 | - $this->set_cache( $customer->id, $customer, 'getpaid_customers' ); |
|
396 | - $this->set_cache( $customer->user_id, $customer->id, 'getpaid_customer_ids_by_user_id' ); |
|
397 | - $this->set_cache( $customer->email, $customer->id, 'getpaid_customer_ids_by_email' ); |
|
398 | - $this->results[ $key ] = new GetPaid_Customer( $customer ); |
|
393 | + if ('all' === $qv['fields']) { |
|
394 | + foreach ($this->results as $key => $customer) { |
|
395 | + $this->set_cache($customer->id, $customer, 'getpaid_customers'); |
|
396 | + $this->set_cache($customer->user_id, $customer->id, 'getpaid_customer_ids_by_user_id'); |
|
397 | + $this->set_cache($customer->email, $customer->id, 'getpaid_customer_ids_by_email'); |
|
398 | + $this->results[$key] = new GetPaid_Customer($customer); |
|
399 | 399 | } |
400 | 400 | } |
401 | 401 | |
@@ -410,13 +410,13 @@ discard block |
||
410 | 410 | * @param integer $expire |
411 | 411 | * @return boolean |
412 | 412 | */ |
413 | - public function set_cache( $key, $data, $group = '', $expire = 0 ) { |
|
413 | + public function set_cache($key, $data, $group = '', $expire = 0) { |
|
414 | 414 | |
415 | - if ( empty( $key ) ) { |
|
415 | + if (empty($key)) { |
|
416 | 416 | return false; |
417 | 417 | } |
418 | 418 | |
419 | - wp_cache_set( $key, $data, $group, $expire ); |
|
419 | + wp_cache_set($key, $data, $group, $expire); |
|
420 | 420 | } |
421 | 421 | |
422 | 422 | /** |
@@ -427,9 +427,9 @@ discard block |
||
427 | 427 | * @param string $query_var Query variable key. |
428 | 428 | * @return mixed |
429 | 429 | */ |
430 | - public function get( $query_var ) { |
|
431 | - if ( isset( $this->query_vars[ $query_var ] ) ) { |
|
432 | - return $this->query_vars[ $query_var ]; |
|
430 | + public function get($query_var) { |
|
431 | + if (isset($this->query_vars[$query_var])) { |
|
432 | + return $this->query_vars[$query_var]; |
|
433 | 433 | } |
434 | 434 | |
435 | 435 | return null; |
@@ -443,8 +443,8 @@ discard block |
||
443 | 443 | * @param string $query_var Query variable key. |
444 | 444 | * @param mixed $value Query variable value. |
445 | 445 | */ |
446 | - public function set( $query_var, $value ) { |
|
447 | - $this->query_vars[ $query_var ] = $value; |
|
446 | + public function set($query_var, $value) { |
|
447 | + $this->query_vars[$query_var] = $value; |
|
448 | 448 | } |
449 | 449 | |
450 | 450 | /** |
@@ -478,16 +478,16 @@ discard block |
||
478 | 478 | * @param string $table The current table. |
479 | 479 | * @return string Value to use in the ORDER clause, if `$orderby` is valid. |
480 | 480 | */ |
481 | - protected function parse_orderby( $orderby, $table ) { |
|
481 | + protected function parse_orderby($orderby, $table) { |
|
482 | 482 | |
483 | 483 | $_orderby = ''; |
484 | - if ( in_array( $orderby, array_keys( GetPaid_Customer_Data_Store::get_database_fields() ), true ) ) { |
|
484 | + if (in_array($orderby, array_keys(GetPaid_Customer_Data_Store::get_database_fields()), true)) { |
|
485 | 485 | $_orderby = "$table.`$orderby`"; |
486 | - } elseif ( 'id' === strtolower( $orderby ) ) { |
|
486 | + } elseif ('id' === strtolower($orderby)) { |
|
487 | 487 | $_orderby = "$table.id"; |
488 | - } elseif ( 'include' === $orderby && ! empty( $this->query_vars['include'] ) ) { |
|
489 | - $include = wp_parse_id_list( $this->query_vars['include'] ); |
|
490 | - $include_sql = implode( ',', $include ); |
|
488 | + } elseif ('include' === $orderby && !empty($this->query_vars['include'])) { |
|
489 | + $include = wp_parse_id_list($this->query_vars['include']); |
|
490 | + $include_sql = implode(',', $include); |
|
491 | 491 | $_orderby = "FIELD( $table.id, $include_sql )"; |
492 | 492 | } |
493 | 493 | |
@@ -502,12 +502,12 @@ discard block |
||
502 | 502 | * @param string $order The 'order' query variable. |
503 | 503 | * @return string The sanitized 'order' query variable. |
504 | 504 | */ |
505 | - protected function parse_order( $order ) { |
|
506 | - if ( ! is_string( $order ) || empty( $order ) ) { |
|
505 | + protected function parse_order($order) { |
|
506 | + if (!is_string($order) || empty($order)) { |
|
507 | 507 | return 'DESC'; |
508 | 508 | } |
509 | 509 | |
510 | - if ( 'ASC' === strtoupper( $order ) ) { |
|
510 | + if ('ASC' === strtoupper($order)) { |
|
511 | 511 | return 'ASC'; |
512 | 512 | } else { |
513 | 513 | return 'DESC'; |
@@ -11,7 +11,7 @@ discard block |
||
11 | 11 | |
12 | 12 | // Load WP_List_Table if not loaded |
13 | 13 | if ( ! class_exists( 'WP_List_Table' ) ) { |
14 | - require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; |
|
14 | + require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; |
|
15 | 15 | } |
16 | 16 | |
17 | 17 | /** |
@@ -23,303 +23,303 @@ discard block |
||
23 | 23 | */ |
24 | 24 | class WPInv_Customers_Table extends WP_List_Table { |
25 | 25 | |
26 | - /** |
|
27 | - * @var int Number of items per page |
|
28 | - * @since 1.0.19 |
|
29 | - */ |
|
30 | - public $per_page = 25; |
|
31 | - |
|
32 | - /** |
|
33 | - * @var int Number of items |
|
34 | - * @since 1.0.19 |
|
35 | - */ |
|
36 | - public $total_count = 0; |
|
37 | - |
|
38 | - public $query; |
|
39 | - |
|
40 | - /** |
|
41 | - * Get things started |
|
42 | - * |
|
43 | - * @since 1.0.19 |
|
44 | - * @see WP_List_Table::__construct() |
|
45 | - */ |
|
46 | - public function __construct() { |
|
47 | - |
|
48 | - // Set parent defaults |
|
49 | - parent::__construct( |
|
26 | + /** |
|
27 | + * @var int Number of items per page |
|
28 | + * @since 1.0.19 |
|
29 | + */ |
|
30 | + public $per_page = 25; |
|
31 | + |
|
32 | + /** |
|
33 | + * @var int Number of items |
|
34 | + * @since 1.0.19 |
|
35 | + */ |
|
36 | + public $total_count = 0; |
|
37 | + |
|
38 | + public $query; |
|
39 | + |
|
40 | + /** |
|
41 | + * Get things started |
|
42 | + * |
|
43 | + * @since 1.0.19 |
|
44 | + * @see WP_List_Table::__construct() |
|
45 | + */ |
|
46 | + public function __construct() { |
|
47 | + |
|
48 | + // Set parent defaults |
|
49 | + parent::__construct( |
|
50 | 50 | array( |
51 | - 'singular' => 'id', |
|
52 | - 'plural' => 'ids', |
|
53 | - 'ajax' => false, |
|
51 | + 'singular' => 'id', |
|
52 | + 'plural' => 'ids', |
|
53 | + 'ajax' => false, |
|
54 | 54 | ) |
55 | 55 | ); |
56 | 56 | |
57 | - } |
|
58 | - |
|
59 | - /** |
|
60 | - * Gets the name of the primary column. |
|
61 | - * |
|
62 | - * @since 1.0.19 |
|
63 | - * @access protected |
|
64 | - * |
|
65 | - * @return string Name of the primary column. |
|
66 | - */ |
|
67 | - protected function get_primary_column_name() { |
|
68 | - return 'customer'; |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * This function renders most of the columns in the list table. |
|
73 | - * |
|
74 | - * @since 1.0.19 |
|
75 | - * |
|
76 | - * @param GetPaid_Customer $customer |
|
77 | - * @param string $column_name The name of the column |
|
78 | - * |
|
79 | - * @return string Column Name |
|
80 | - */ |
|
81 | - public function column_default( $customer, $column_name ) { |
|
82 | - $value = esc_html( $customer->get( $column_name ) ); |
|
83 | - return apply_filters( 'wpinv_customers_table_column' . $column_name, $value, $customer ); |
|
84 | - } |
|
85 | - |
|
86 | - /** |
|
87 | - * Displays the country column. |
|
88 | - * |
|
89 | - * @since 1.0.19 |
|
90 | - * |
|
91 | - * @param GetPaid_Customer $customer |
|
92 | - * |
|
93 | - * @return string Column Name |
|
94 | - */ |
|
95 | - public function column_country( $customer ) { |
|
96 | - $country = wpinv_sanitize_country( $customer->get( 'country' ) ); |
|
97 | - if ( $country ) { |
|
98 | - $country = wpinv_country_name( $country ); |
|
99 | - } |
|
100 | - return esc_html( $country ); |
|
101 | - } |
|
102 | - |
|
103 | - /** |
|
104 | - * Displays the state column. |
|
105 | - * |
|
106 | - * @since 1.0.19 |
|
107 | - * |
|
108 | - * @param GetPaid_Customer $customer |
|
109 | - * |
|
110 | - * @return string Column Name |
|
111 | - */ |
|
112 | - public function column_state( $customer ) { |
|
113 | - $country = wpinv_sanitize_country( $customer->get( 'country' ) ); |
|
114 | - $state = $customer->get( 'state' ); |
|
115 | - if ( $state ) { |
|
116 | - $state = wpinv_state_name( $state, $country ); |
|
117 | - } |
|
118 | - |
|
119 | - return esc_html( $state ); |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * Displays the signup column. |
|
124 | - * |
|
125 | - * @since 1.0.19 |
|
126 | - * |
|
127 | - * @param GetPaid_Customer $customer |
|
128 | - * |
|
129 | - * @return string Column Name |
|
130 | - */ |
|
131 | - public function column_date_created( $customer ) { |
|
132 | - return getpaid_format_date_value( $customer->get( 'date_created' ) ); |
|
133 | - } |
|
134 | - |
|
135 | - /** |
|
136 | - * Displays the total spent column. |
|
137 | - * |
|
138 | - * @since 1.0.19 |
|
139 | - * |
|
140 | - * @param GetPaid_Customer $customer |
|
141 | - * |
|
142 | - * @return string Column Name |
|
143 | - */ |
|
144 | - public function column_purchase_value( $customer ) { |
|
145 | - return wpinv_price( (float) $customer->get( 'purchase_value' ) ); |
|
146 | - } |
|
147 | - |
|
148 | - /** |
|
149 | - * Displays the total spent column. |
|
150 | - * |
|
151 | - * @since 1.0.19 |
|
152 | - * |
|
153 | - * @param GetPaid_Customer $customer |
|
154 | - * |
|
155 | - * @return string Column Name |
|
156 | - */ |
|
157 | - public function column_purchase_count( $customer ) { |
|
158 | - $value = $customer->get( 'purchase_count' ); |
|
159 | - $url = $customer->get( 'user_id' ) ? add_query_arg( array( 'post_type' => 'wpi_invoice', 'author' => $customer->get( 'user_id' ), ), admin_url( 'edit.php' ) ) : ''; |
|
160 | - |
|
161 | - return ( empty( $value ) || empty( $url ) ) ? (int) $value : '<a href="' . esc_url( $url ) . '">' . absint( $value ) . '</a>'; |
|
162 | - |
|
163 | - } |
|
164 | - |
|
165 | - /** |
|
166 | - * Displays the customers name |
|
167 | - * |
|
168 | - * @param GetPaid_Customer $customer customer. |
|
169 | - * @return string |
|
170 | - */ |
|
171 | - public function column_customer( $customer ) { |
|
172 | - |
|
173 | - $first_name = $customer->get( 'first_name' ); |
|
174 | - $last_name = $customer->get( 'last_name' ); |
|
175 | - $email = $customer->get( 'email' ); |
|
176 | - $avatar = get_avatar( $customer->get( 'user_id' ) ? $customer->get( 'user_id' ) : $email, 32 ); |
|
177 | - |
|
178 | - // Customer view URL. |
|
179 | - $view_url = $customer->get( 'user_id' ) ? esc_url( add_query_arg( 'user_id', $customer->get( 'user_id' ), admin_url( 'user-edit.php' ) ) ) : false; |
|
180 | - $row_actions = $view_url ? $this->row_actions( |
|
181 | - array( |
|
182 | - 'view' => '<a href="' . $view_url . '#getpaid-fieldset-billing">' . __( 'Edit Details', 'invoicing' ) . '</a>', |
|
183 | - ) |
|
184 | - ) : ''; |
|
185 | - |
|
186 | - // Customer's name. |
|
187 | - $name = esc_html( trim( "$first_name $last_name" ) ); |
|
188 | - |
|
189 | - if ( ! empty( $name ) ) { |
|
190 | - $name = "<div style='overflow: hidden;height: 18px;'>$name</div>"; |
|
191 | - } |
|
192 | - |
|
193 | - $email = "<div class='row-title'><a href='mailto:$email'>$email</a></div>"; |
|
194 | - |
|
195 | - return "<div style='display: flex;'><div>$avatar</div><div style='margin-left: 10px;'>$name<strong>$email</strong>$row_actions</div></div>"; |
|
196 | - |
|
197 | - } |
|
198 | - |
|
199 | - /** |
|
200 | - * Retrieve the current page number |
|
201 | - * |
|
202 | - * @since 1.0.19 |
|
203 | - * @return int Current page number |
|
204 | - */ |
|
205 | - public function get_paged() { |
|
206 | - return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1; |
|
207 | - } |
|
208 | - |
|
209 | - /** |
|
210 | - * Returns bulk actions. |
|
211 | - * |
|
212 | - * @since 1.0.19 |
|
213 | - * @return void |
|
214 | - */ |
|
215 | - public function bulk_actions( $which = '' ) { |
|
216 | - return array(); |
|
217 | - } |
|
218 | - |
|
219 | - /** |
|
220 | - * Prepares the display query |
|
221 | - */ |
|
222 | - public function prepare_query() { |
|
223 | - |
|
224 | - // Prepare query args. |
|
225 | - $query = array( |
|
226 | - 'number' => $this->per_page, |
|
227 | - 'paged' => $this->get_paged(), |
|
228 | - ); |
|
229 | - |
|
230 | - foreach ( array( 'orderby', 'order', 's' ) as $field ) { |
|
231 | - if ( isset( $_GET[ $field ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
232 | - $query[ $field ] = wpinv_clean( rawurlencode_deep( $_GET[ $field ] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
233 | - } |
|
234 | - } |
|
235 | - |
|
236 | - foreach ( GetPaid_Customer_Data_Store::get_database_fields() as $field => $type ) { |
|
237 | - |
|
238 | - if ( isset( $_GET[ $field ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
239 | - $query[ $field ] = wpinv_clean( rawurlencode_deep( $_GET[ $field ] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
240 | - } |
|
241 | - |
|
242 | - // Min max. |
|
243 | - if ( '%f' === $type || '%d' === $type ) { |
|
244 | - |
|
245 | - if ( isset( $_GET[ $field . '_min' ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
246 | - $query[ $field . '_min' ] = floatval( $_GET[ $field . '_min' ] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
247 | - } |
|
248 | - |
|
249 | - if ( isset( $_GET[ $field . '_max' ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
250 | - $query[ $field . '_max' ] = floatval( $_GET[ $field . '_max' ] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
251 | - } |
|
252 | - } |
|
253 | - } |
|
254 | - |
|
255 | - // Prepare class properties. |
|
256 | - $this->query = getpaid_get_customers( $query, 'query' ); |
|
257 | - $this->total_count = $this->query->get_total(); |
|
258 | - $this->items = $this->query->get_results(); |
|
259 | - } |
|
260 | - |
|
261 | - /** |
|
262 | - * Setup the final data for the table |
|
263 | - * |
|
264 | - */ |
|
265 | - public function prepare_items() { |
|
266 | - |
|
267 | - $columns = $this->get_columns(); |
|
268 | - $hidden = array(); |
|
269 | - $sortable = $this->get_sortable_columns(); |
|
270 | - $this->prepare_query(); |
|
271 | - |
|
272 | - $this->_column_headers = array( $columns, $hidden, $sortable ); |
|
273 | - |
|
274 | - $this->set_pagination_args( |
|
275 | - array( |
|
276 | - 'total_items' => $this->total_count, |
|
277 | - 'per_page' => $this->per_page, |
|
278 | - 'total_pages' => ceil( $this->total_count / $this->per_page ), |
|
279 | - ) |
|
280 | - ); |
|
281 | - } |
|
282 | - |
|
283 | - /** |
|
284 | - * Sortable table columns. |
|
285 | - * |
|
286 | - * @return array |
|
287 | - */ |
|
288 | - public function get_sortable_columns() { |
|
289 | - $sortable = array( |
|
290 | - 'customer' => array( 'first_name', true ), |
|
291 | - ); |
|
292 | - |
|
293 | - foreach ( GetPaid_Customer_Data_Store::get_database_fields() as $field => $type ) { |
|
294 | - $sortable[ $field ] = array( $field, true ); |
|
295 | - } |
|
296 | - |
|
297 | - return apply_filters( 'manage_getpaid_customers_sortable_table_columns', $sortable ); |
|
298 | - } |
|
299 | - |
|
300 | - /** |
|
301 | - * Table columns |
|
302 | - * |
|
303 | - * @return array |
|
304 | - */ |
|
305 | - public function get_columns() { |
|
306 | - $columns = array( |
|
307 | - 'customer' => __( 'Customer', 'invoicing' ), |
|
308 | - ); |
|
309 | - |
|
310 | - // Add address fields. |
|
311 | - foreach ( getpaid_user_address_fields() as $key => $value ) { |
|
312 | - |
|
313 | - // Skip id, user_id and email. |
|
314 | - if ( ! in_array( $key, array( 'id', 'user_id', 'email', 'purchase_value', 'purchase_count', 'date_created', 'date_modified', 'uuid', 'first_name', 'last_name' ), true ) ) { |
|
315 | - $columns[ $key ] = $value; |
|
316 | - } |
|
317 | - } |
|
318 | - |
|
319 | - $columns['purchase_value'] = __( 'Total Spend', 'invoicing' ); |
|
320 | - $columns['purchase_count'] = __( 'Invoices', 'invoicing' ); |
|
321 | - $columns['date_created'] = __( 'Date created', 'invoicing' ); |
|
322 | - |
|
323 | - return apply_filters( 'manage_getpaid_customers_table_columns', $columns ); |
|
324 | - } |
|
57 | + } |
|
58 | + |
|
59 | + /** |
|
60 | + * Gets the name of the primary column. |
|
61 | + * |
|
62 | + * @since 1.0.19 |
|
63 | + * @access protected |
|
64 | + * |
|
65 | + * @return string Name of the primary column. |
|
66 | + */ |
|
67 | + protected function get_primary_column_name() { |
|
68 | + return 'customer'; |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * This function renders most of the columns in the list table. |
|
73 | + * |
|
74 | + * @since 1.0.19 |
|
75 | + * |
|
76 | + * @param GetPaid_Customer $customer |
|
77 | + * @param string $column_name The name of the column |
|
78 | + * |
|
79 | + * @return string Column Name |
|
80 | + */ |
|
81 | + public function column_default( $customer, $column_name ) { |
|
82 | + $value = esc_html( $customer->get( $column_name ) ); |
|
83 | + return apply_filters( 'wpinv_customers_table_column' . $column_name, $value, $customer ); |
|
84 | + } |
|
85 | + |
|
86 | + /** |
|
87 | + * Displays the country column. |
|
88 | + * |
|
89 | + * @since 1.0.19 |
|
90 | + * |
|
91 | + * @param GetPaid_Customer $customer |
|
92 | + * |
|
93 | + * @return string Column Name |
|
94 | + */ |
|
95 | + public function column_country( $customer ) { |
|
96 | + $country = wpinv_sanitize_country( $customer->get( 'country' ) ); |
|
97 | + if ( $country ) { |
|
98 | + $country = wpinv_country_name( $country ); |
|
99 | + } |
|
100 | + return esc_html( $country ); |
|
101 | + } |
|
102 | + |
|
103 | + /** |
|
104 | + * Displays the state column. |
|
105 | + * |
|
106 | + * @since 1.0.19 |
|
107 | + * |
|
108 | + * @param GetPaid_Customer $customer |
|
109 | + * |
|
110 | + * @return string Column Name |
|
111 | + */ |
|
112 | + public function column_state( $customer ) { |
|
113 | + $country = wpinv_sanitize_country( $customer->get( 'country' ) ); |
|
114 | + $state = $customer->get( 'state' ); |
|
115 | + if ( $state ) { |
|
116 | + $state = wpinv_state_name( $state, $country ); |
|
117 | + } |
|
118 | + |
|
119 | + return esc_html( $state ); |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * Displays the signup column. |
|
124 | + * |
|
125 | + * @since 1.0.19 |
|
126 | + * |
|
127 | + * @param GetPaid_Customer $customer |
|
128 | + * |
|
129 | + * @return string Column Name |
|
130 | + */ |
|
131 | + public function column_date_created( $customer ) { |
|
132 | + return getpaid_format_date_value( $customer->get( 'date_created' ) ); |
|
133 | + } |
|
134 | + |
|
135 | + /** |
|
136 | + * Displays the total spent column. |
|
137 | + * |
|
138 | + * @since 1.0.19 |
|
139 | + * |
|
140 | + * @param GetPaid_Customer $customer |
|
141 | + * |
|
142 | + * @return string Column Name |
|
143 | + */ |
|
144 | + public function column_purchase_value( $customer ) { |
|
145 | + return wpinv_price( (float) $customer->get( 'purchase_value' ) ); |
|
146 | + } |
|
147 | + |
|
148 | + /** |
|
149 | + * Displays the total spent column. |
|
150 | + * |
|
151 | + * @since 1.0.19 |
|
152 | + * |
|
153 | + * @param GetPaid_Customer $customer |
|
154 | + * |
|
155 | + * @return string Column Name |
|
156 | + */ |
|
157 | + public function column_purchase_count( $customer ) { |
|
158 | + $value = $customer->get( 'purchase_count' ); |
|
159 | + $url = $customer->get( 'user_id' ) ? add_query_arg( array( 'post_type' => 'wpi_invoice', 'author' => $customer->get( 'user_id' ), ), admin_url( 'edit.php' ) ) : ''; |
|
160 | + |
|
161 | + return ( empty( $value ) || empty( $url ) ) ? (int) $value : '<a href="' . esc_url( $url ) . '">' . absint( $value ) . '</a>'; |
|
162 | + |
|
163 | + } |
|
164 | + |
|
165 | + /** |
|
166 | + * Displays the customers name |
|
167 | + * |
|
168 | + * @param GetPaid_Customer $customer customer. |
|
169 | + * @return string |
|
170 | + */ |
|
171 | + public function column_customer( $customer ) { |
|
172 | + |
|
173 | + $first_name = $customer->get( 'first_name' ); |
|
174 | + $last_name = $customer->get( 'last_name' ); |
|
175 | + $email = $customer->get( 'email' ); |
|
176 | + $avatar = get_avatar( $customer->get( 'user_id' ) ? $customer->get( 'user_id' ) : $email, 32 ); |
|
177 | + |
|
178 | + // Customer view URL. |
|
179 | + $view_url = $customer->get( 'user_id' ) ? esc_url( add_query_arg( 'user_id', $customer->get( 'user_id' ), admin_url( 'user-edit.php' ) ) ) : false; |
|
180 | + $row_actions = $view_url ? $this->row_actions( |
|
181 | + array( |
|
182 | + 'view' => '<a href="' . $view_url . '#getpaid-fieldset-billing">' . __( 'Edit Details', 'invoicing' ) . '</a>', |
|
183 | + ) |
|
184 | + ) : ''; |
|
185 | + |
|
186 | + // Customer's name. |
|
187 | + $name = esc_html( trim( "$first_name $last_name" ) ); |
|
188 | + |
|
189 | + if ( ! empty( $name ) ) { |
|
190 | + $name = "<div style='overflow: hidden;height: 18px;'>$name</div>"; |
|
191 | + } |
|
192 | + |
|
193 | + $email = "<div class='row-title'><a href='mailto:$email'>$email</a></div>"; |
|
194 | + |
|
195 | + return "<div style='display: flex;'><div>$avatar</div><div style='margin-left: 10px;'>$name<strong>$email</strong>$row_actions</div></div>"; |
|
196 | + |
|
197 | + } |
|
198 | + |
|
199 | + /** |
|
200 | + * Retrieve the current page number |
|
201 | + * |
|
202 | + * @since 1.0.19 |
|
203 | + * @return int Current page number |
|
204 | + */ |
|
205 | + public function get_paged() { |
|
206 | + return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1; |
|
207 | + } |
|
208 | + |
|
209 | + /** |
|
210 | + * Returns bulk actions. |
|
211 | + * |
|
212 | + * @since 1.0.19 |
|
213 | + * @return void |
|
214 | + */ |
|
215 | + public function bulk_actions( $which = '' ) { |
|
216 | + return array(); |
|
217 | + } |
|
218 | + |
|
219 | + /** |
|
220 | + * Prepares the display query |
|
221 | + */ |
|
222 | + public function prepare_query() { |
|
223 | + |
|
224 | + // Prepare query args. |
|
225 | + $query = array( |
|
226 | + 'number' => $this->per_page, |
|
227 | + 'paged' => $this->get_paged(), |
|
228 | + ); |
|
229 | + |
|
230 | + foreach ( array( 'orderby', 'order', 's' ) as $field ) { |
|
231 | + if ( isset( $_GET[ $field ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
232 | + $query[ $field ] = wpinv_clean( rawurlencode_deep( $_GET[ $field ] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
233 | + } |
|
234 | + } |
|
235 | + |
|
236 | + foreach ( GetPaid_Customer_Data_Store::get_database_fields() as $field => $type ) { |
|
237 | + |
|
238 | + if ( isset( $_GET[ $field ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
239 | + $query[ $field ] = wpinv_clean( rawurlencode_deep( $_GET[ $field ] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
240 | + } |
|
241 | + |
|
242 | + // Min max. |
|
243 | + if ( '%f' === $type || '%d' === $type ) { |
|
244 | + |
|
245 | + if ( isset( $_GET[ $field . '_min' ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
246 | + $query[ $field . '_min' ] = floatval( $_GET[ $field . '_min' ] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
247 | + } |
|
248 | + |
|
249 | + if ( isset( $_GET[ $field . '_max' ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
250 | + $query[ $field . '_max' ] = floatval( $_GET[ $field . '_max' ] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
251 | + } |
|
252 | + } |
|
253 | + } |
|
254 | + |
|
255 | + // Prepare class properties. |
|
256 | + $this->query = getpaid_get_customers( $query, 'query' ); |
|
257 | + $this->total_count = $this->query->get_total(); |
|
258 | + $this->items = $this->query->get_results(); |
|
259 | + } |
|
260 | + |
|
261 | + /** |
|
262 | + * Setup the final data for the table |
|
263 | + * |
|
264 | + */ |
|
265 | + public function prepare_items() { |
|
266 | + |
|
267 | + $columns = $this->get_columns(); |
|
268 | + $hidden = array(); |
|
269 | + $sortable = $this->get_sortable_columns(); |
|
270 | + $this->prepare_query(); |
|
271 | + |
|
272 | + $this->_column_headers = array( $columns, $hidden, $sortable ); |
|
273 | + |
|
274 | + $this->set_pagination_args( |
|
275 | + array( |
|
276 | + 'total_items' => $this->total_count, |
|
277 | + 'per_page' => $this->per_page, |
|
278 | + 'total_pages' => ceil( $this->total_count / $this->per_page ), |
|
279 | + ) |
|
280 | + ); |
|
281 | + } |
|
282 | + |
|
283 | + /** |
|
284 | + * Sortable table columns. |
|
285 | + * |
|
286 | + * @return array |
|
287 | + */ |
|
288 | + public function get_sortable_columns() { |
|
289 | + $sortable = array( |
|
290 | + 'customer' => array( 'first_name', true ), |
|
291 | + ); |
|
292 | + |
|
293 | + foreach ( GetPaid_Customer_Data_Store::get_database_fields() as $field => $type ) { |
|
294 | + $sortable[ $field ] = array( $field, true ); |
|
295 | + } |
|
296 | + |
|
297 | + return apply_filters( 'manage_getpaid_customers_sortable_table_columns', $sortable ); |
|
298 | + } |
|
299 | + |
|
300 | + /** |
|
301 | + * Table columns |
|
302 | + * |
|
303 | + * @return array |
|
304 | + */ |
|
305 | + public function get_columns() { |
|
306 | + $columns = array( |
|
307 | + 'customer' => __( 'Customer', 'invoicing' ), |
|
308 | + ); |
|
309 | + |
|
310 | + // Add address fields. |
|
311 | + foreach ( getpaid_user_address_fields() as $key => $value ) { |
|
312 | + |
|
313 | + // Skip id, user_id and email. |
|
314 | + if ( ! in_array( $key, array( 'id', 'user_id', 'email', 'purchase_value', 'purchase_count', 'date_created', 'date_modified', 'uuid', 'first_name', 'last_name' ), true ) ) { |
|
315 | + $columns[ $key ] = $value; |
|
316 | + } |
|
317 | + } |
|
318 | + |
|
319 | + $columns['purchase_value'] = __( 'Total Spend', 'invoicing' ); |
|
320 | + $columns['purchase_count'] = __( 'Invoices', 'invoicing' ); |
|
321 | + $columns['date_created'] = __( 'Date created', 'invoicing' ); |
|
322 | + |
|
323 | + return apply_filters( 'manage_getpaid_customers_table_columns', $columns ); |
|
324 | + } |
|
325 | 325 | } |
@@ -5,12 +5,12 @@ discard block |
||
5 | 5 | */ |
6 | 6 | |
7 | 7 | // Exit if accessed directly |
8 | -if ( ! defined( 'ABSPATH' ) ) { |
|
8 | +if (!defined('ABSPATH')) { |
|
9 | 9 | exit; |
10 | 10 | } |
11 | 11 | |
12 | 12 | // Load WP_List_Table if not loaded |
13 | -if ( ! class_exists( 'WP_List_Table' ) ) { |
|
13 | +if (!class_exists('WP_List_Table')) { |
|
14 | 14 | require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; |
15 | 15 | } |
16 | 16 | |
@@ -78,9 +78,9 @@ discard block |
||
78 | 78 | * |
79 | 79 | * @return string Column Name |
80 | 80 | */ |
81 | - public function column_default( $customer, $column_name ) { |
|
82 | - $value = esc_html( $customer->get( $column_name ) ); |
|
83 | - return apply_filters( 'wpinv_customers_table_column' . $column_name, $value, $customer ); |
|
81 | + public function column_default($customer, $column_name) { |
|
82 | + $value = esc_html($customer->get($column_name)); |
|
83 | + return apply_filters('wpinv_customers_table_column' . $column_name, $value, $customer); |
|
84 | 84 | } |
85 | 85 | |
86 | 86 | /** |
@@ -92,12 +92,12 @@ discard block |
||
92 | 92 | * |
93 | 93 | * @return string Column Name |
94 | 94 | */ |
95 | - public function column_country( $customer ) { |
|
96 | - $country = wpinv_sanitize_country( $customer->get( 'country' ) ); |
|
97 | - if ( $country ) { |
|
98 | - $country = wpinv_country_name( $country ); |
|
95 | + public function column_country($customer) { |
|
96 | + $country = wpinv_sanitize_country($customer->get('country')); |
|
97 | + if ($country) { |
|
98 | + $country = wpinv_country_name($country); |
|
99 | 99 | } |
100 | - return esc_html( $country ); |
|
100 | + return esc_html($country); |
|
101 | 101 | } |
102 | 102 | |
103 | 103 | /** |
@@ -109,14 +109,14 @@ discard block |
||
109 | 109 | * |
110 | 110 | * @return string Column Name |
111 | 111 | */ |
112 | - public function column_state( $customer ) { |
|
113 | - $country = wpinv_sanitize_country( $customer->get( 'country' ) ); |
|
114 | - $state = $customer->get( 'state' ); |
|
115 | - if ( $state ) { |
|
116 | - $state = wpinv_state_name( $state, $country ); |
|
112 | + public function column_state($customer) { |
|
113 | + $country = wpinv_sanitize_country($customer->get('country')); |
|
114 | + $state = $customer->get('state'); |
|
115 | + if ($state) { |
|
116 | + $state = wpinv_state_name($state, $country); |
|
117 | 117 | } |
118 | 118 | |
119 | - return esc_html( $state ); |
|
119 | + return esc_html($state); |
|
120 | 120 | } |
121 | 121 | |
122 | 122 | /** |
@@ -128,8 +128,8 @@ discard block |
||
128 | 128 | * |
129 | 129 | * @return string Column Name |
130 | 130 | */ |
131 | - public function column_date_created( $customer ) { |
|
132 | - return getpaid_format_date_value( $customer->get( 'date_created' ) ); |
|
131 | + public function column_date_created($customer) { |
|
132 | + return getpaid_format_date_value($customer->get('date_created')); |
|
133 | 133 | } |
134 | 134 | |
135 | 135 | /** |
@@ -141,8 +141,8 @@ discard block |
||
141 | 141 | * |
142 | 142 | * @return string Column Name |
143 | 143 | */ |
144 | - public function column_purchase_value( $customer ) { |
|
145 | - return wpinv_price( (float) $customer->get( 'purchase_value' ) ); |
|
144 | + public function column_purchase_value($customer) { |
|
145 | + return wpinv_price((float) $customer->get('purchase_value')); |
|
146 | 146 | } |
147 | 147 | |
148 | 148 | /** |
@@ -154,11 +154,11 @@ discard block |
||
154 | 154 | * |
155 | 155 | * @return string Column Name |
156 | 156 | */ |
157 | - public function column_purchase_count( $customer ) { |
|
158 | - $value = $customer->get( 'purchase_count' ); |
|
159 | - $url = $customer->get( 'user_id' ) ? add_query_arg( array( 'post_type' => 'wpi_invoice', 'author' => $customer->get( 'user_id' ), ), admin_url( 'edit.php' ) ) : ''; |
|
157 | + public function column_purchase_count($customer) { |
|
158 | + $value = $customer->get('purchase_count'); |
|
159 | + $url = $customer->get('user_id') ? add_query_arg(array('post_type' => 'wpi_invoice', 'author' => $customer->get('user_id'),), admin_url('edit.php')) : ''; |
|
160 | 160 | |
161 | - return ( empty( $value ) || empty( $url ) ) ? (int) $value : '<a href="' . esc_url( $url ) . '">' . absint( $value ) . '</a>'; |
|
161 | + return (empty($value) || empty($url)) ? (int) $value : '<a href="' . esc_url($url) . '">' . absint($value) . '</a>'; |
|
162 | 162 | |
163 | 163 | } |
164 | 164 | |
@@ -168,25 +168,25 @@ discard block |
||
168 | 168 | * @param GetPaid_Customer $customer customer. |
169 | 169 | * @return string |
170 | 170 | */ |
171 | - public function column_customer( $customer ) { |
|
171 | + public function column_customer($customer) { |
|
172 | 172 | |
173 | - $first_name = $customer->get( 'first_name' ); |
|
174 | - $last_name = $customer->get( 'last_name' ); |
|
175 | - $email = $customer->get( 'email' ); |
|
176 | - $avatar = get_avatar( $customer->get( 'user_id' ) ? $customer->get( 'user_id' ) : $email, 32 ); |
|
173 | + $first_name = $customer->get('first_name'); |
|
174 | + $last_name = $customer->get('last_name'); |
|
175 | + $email = $customer->get('email'); |
|
176 | + $avatar = get_avatar($customer->get('user_id') ? $customer->get('user_id') : $email, 32); |
|
177 | 177 | |
178 | 178 | // Customer view URL. |
179 | - $view_url = $customer->get( 'user_id' ) ? esc_url( add_query_arg( 'user_id', $customer->get( 'user_id' ), admin_url( 'user-edit.php' ) ) ) : false; |
|
179 | + $view_url = $customer->get('user_id') ? esc_url(add_query_arg('user_id', $customer->get('user_id'), admin_url('user-edit.php'))) : false; |
|
180 | 180 | $row_actions = $view_url ? $this->row_actions( |
181 | 181 | array( |
182 | - 'view' => '<a href="' . $view_url . '#getpaid-fieldset-billing">' . __( 'Edit Details', 'invoicing' ) . '</a>', |
|
182 | + 'view' => '<a href="' . $view_url . '#getpaid-fieldset-billing">' . __('Edit Details', 'invoicing') . '</a>', |
|
183 | 183 | ) |
184 | 184 | ) : ''; |
185 | 185 | |
186 | 186 | // Customer's name. |
187 | - $name = esc_html( trim( "$first_name $last_name" ) ); |
|
187 | + $name = esc_html(trim("$first_name $last_name")); |
|
188 | 188 | |
189 | - if ( ! empty( $name ) ) { |
|
189 | + if (!empty($name)) { |
|
190 | 190 | $name = "<div style='overflow: hidden;height: 18px;'>$name</div>"; |
191 | 191 | } |
192 | 192 | |
@@ -203,7 +203,7 @@ discard block |
||
203 | 203 | * @return int Current page number |
204 | 204 | */ |
205 | 205 | public function get_paged() { |
206 | - return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1; |
|
206 | + return isset($_GET['paged']) ? absint($_GET['paged']) : 1; |
|
207 | 207 | } |
208 | 208 | |
209 | 209 | /** |
@@ -212,7 +212,7 @@ discard block |
||
212 | 212 | * @since 1.0.19 |
213 | 213 | * @return void |
214 | 214 | */ |
215 | - public function bulk_actions( $which = '' ) { |
|
215 | + public function bulk_actions($which = '') { |
|
216 | 216 | return array(); |
217 | 217 | } |
218 | 218 | |
@@ -227,33 +227,33 @@ discard block |
||
227 | 227 | 'paged' => $this->get_paged(), |
228 | 228 | ); |
229 | 229 | |
230 | - foreach ( array( 'orderby', 'order', 's' ) as $field ) { |
|
231 | - if ( isset( $_GET[ $field ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
232 | - $query[ $field ] = wpinv_clean( rawurlencode_deep( $_GET[ $field ] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
230 | + foreach (array('orderby', 'order', 's') as $field) { |
|
231 | + if (isset($_GET[$field])) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
232 | + $query[$field] = wpinv_clean(rawurlencode_deep($_GET[$field])); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
233 | 233 | } |
234 | 234 | } |
235 | 235 | |
236 | - foreach ( GetPaid_Customer_Data_Store::get_database_fields() as $field => $type ) { |
|
236 | + foreach (GetPaid_Customer_Data_Store::get_database_fields() as $field => $type) { |
|
237 | 237 | |
238 | - if ( isset( $_GET[ $field ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
239 | - $query[ $field ] = wpinv_clean( rawurlencode_deep( $_GET[ $field ] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
238 | + if (isset($_GET[$field])) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
239 | + $query[$field] = wpinv_clean(rawurlencode_deep($_GET[$field])); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
240 | 240 | } |
241 | 241 | |
242 | 242 | // Min max. |
243 | - if ( '%f' === $type || '%d' === $type ) { |
|
243 | + if ('%f' === $type || '%d' === $type) { |
|
244 | 244 | |
245 | - if ( isset( $_GET[ $field . '_min' ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
246 | - $query[ $field . '_min' ] = floatval( $_GET[ $field . '_min' ] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
245 | + if (isset($_GET[$field . '_min'])) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
246 | + $query[$field . '_min'] = floatval($_GET[$field . '_min']); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
247 | 247 | } |
248 | 248 | |
249 | - if ( isset( $_GET[ $field . '_max' ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
250 | - $query[ $field . '_max' ] = floatval( $_GET[ $field . '_max' ] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
249 | + if (isset($_GET[$field . '_max'])) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
250 | + $query[$field . '_max'] = floatval($_GET[$field . '_max']); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
251 | 251 | } |
252 | 252 | } |
253 | 253 | } |
254 | 254 | |
255 | 255 | // Prepare class properties. |
256 | - $this->query = getpaid_get_customers( $query, 'query' ); |
|
256 | + $this->query = getpaid_get_customers($query, 'query'); |
|
257 | 257 | $this->total_count = $this->query->get_total(); |
258 | 258 | $this->items = $this->query->get_results(); |
259 | 259 | } |
@@ -269,13 +269,13 @@ discard block |
||
269 | 269 | $sortable = $this->get_sortable_columns(); |
270 | 270 | $this->prepare_query(); |
271 | 271 | |
272 | - $this->_column_headers = array( $columns, $hidden, $sortable ); |
|
272 | + $this->_column_headers = array($columns, $hidden, $sortable); |
|
273 | 273 | |
274 | 274 | $this->set_pagination_args( |
275 | 275 | array( |
276 | 276 | 'total_items' => $this->total_count, |
277 | 277 | 'per_page' => $this->per_page, |
278 | - 'total_pages' => ceil( $this->total_count / $this->per_page ), |
|
278 | + 'total_pages' => ceil($this->total_count / $this->per_page), |
|
279 | 279 | ) |
280 | 280 | ); |
281 | 281 | } |
@@ -287,14 +287,14 @@ discard block |
||
287 | 287 | */ |
288 | 288 | public function get_sortable_columns() { |
289 | 289 | $sortable = array( |
290 | - 'customer' => array( 'first_name', true ), |
|
290 | + 'customer' => array('first_name', true), |
|
291 | 291 | ); |
292 | 292 | |
293 | - foreach ( GetPaid_Customer_Data_Store::get_database_fields() as $field => $type ) { |
|
294 | - $sortable[ $field ] = array( $field, true ); |
|
293 | + foreach (GetPaid_Customer_Data_Store::get_database_fields() as $field => $type) { |
|
294 | + $sortable[$field] = array($field, true); |
|
295 | 295 | } |
296 | 296 | |
297 | - return apply_filters( 'manage_getpaid_customers_sortable_table_columns', $sortable ); |
|
297 | + return apply_filters('manage_getpaid_customers_sortable_table_columns', $sortable); |
|
298 | 298 | } |
299 | 299 | |
300 | 300 | /** |
@@ -304,22 +304,22 @@ discard block |
||
304 | 304 | */ |
305 | 305 | public function get_columns() { |
306 | 306 | $columns = array( |
307 | - 'customer' => __( 'Customer', 'invoicing' ), |
|
307 | + 'customer' => __('Customer', 'invoicing'), |
|
308 | 308 | ); |
309 | 309 | |
310 | 310 | // Add address fields. |
311 | - foreach ( getpaid_user_address_fields() as $key => $value ) { |
|
311 | + foreach (getpaid_user_address_fields() as $key => $value) { |
|
312 | 312 | |
313 | 313 | // Skip id, user_id and email. |
314 | - if ( ! in_array( $key, array( 'id', 'user_id', 'email', 'purchase_value', 'purchase_count', 'date_created', 'date_modified', 'uuid', 'first_name', 'last_name' ), true ) ) { |
|
315 | - $columns[ $key ] = $value; |
|
314 | + if (!in_array($key, array('id', 'user_id', 'email', 'purchase_value', 'purchase_count', 'date_created', 'date_modified', 'uuid', 'first_name', 'last_name'), true)) { |
|
315 | + $columns[$key] = $value; |
|
316 | 316 | } |
317 | 317 | } |
318 | 318 | |
319 | - $columns['purchase_value'] = __( 'Total Spend', 'invoicing' ); |
|
320 | - $columns['purchase_count'] = __( 'Invoices', 'invoicing' ); |
|
321 | - $columns['date_created'] = __( 'Date created', 'invoicing' ); |
|
319 | + $columns['purchase_value'] = __('Total Spend', 'invoicing'); |
|
320 | + $columns['purchase_count'] = __('Invoices', 'invoicing'); |
|
321 | + $columns['date_created'] = __('Date created', 'invoicing'); |
|
322 | 322 | |
323 | - return apply_filters( 'manage_getpaid_customers_table_columns', $columns ); |
|
323 | + return apply_filters('manage_getpaid_customers_table_columns', $columns); |
|
324 | 324 | } |
325 | 325 | } |
@@ -7,44 +7,44 @@ |
||
7 | 7 | * @version 1.0.19 |
8 | 8 | */ |
9 | 9 | |
10 | -defined( 'ABSPATH' ) || exit; |
|
10 | +defined('ABSPATH') || exit; |
|
11 | 11 | |
12 | 12 | ?> |
13 | 13 | |
14 | 14 | <div class='form-group mb-3'> |
15 | 15 | <label class="form-label d-block"> |
16 | - <span><?php esc_html_e( 'Input Label', 'invoicing' ); ?></span> |
|
16 | + <span><?php esc_html_e('Input Label', 'invoicing'); ?></span> |
|
17 | 17 | <input v-model='active_form_element.input_label' class='form-control' type="text"/> |
18 | 18 | </label> |
19 | 19 | </div> |
20 | 20 | |
21 | 21 | <div class='form-group mb-3'> |
22 | 22 | <label class="form-label d-block"> |
23 | - <span><?php esc_html_e( 'Button Label', 'invoicing' ); ?></span> |
|
23 | + <span><?php esc_html_e('Button Label', 'invoicing'); ?></span> |
|
24 | 24 | <input v-model='active_form_element.button_label' class='form-control' type="text"/> |
25 | 25 | </label> |
26 | 26 | </div> |
27 | 27 | |
28 | 28 | <div class='form-group mb-3'> |
29 | - <label class="form-label" :for="active_form_element.id + '_edit_type'"><?php esc_html_e( 'Button Type', 'invoicing' ); ?></label> |
|
29 | + <label class="form-label" :for="active_form_element.id + '_edit_type'"><?php esc_html_e('Button Type', 'invoicing'); ?></label> |
|
30 | 30 | <select class='form-control custom-select' :id="active_form_element.id + '_edit_type'" v-model='active_form_element.class'> |
31 | - <option disabled><?php esc_html_e( 'Select button type', 'invoicing' ); ?></option> |
|
32 | - <option value='btn-primary'><?php esc_html_e( 'Primary', 'invoicing' ); ?></option> |
|
33 | - <option value='btn-secondary'><?php esc_html_e( 'Secondary', 'invoicing' ); ?></option> |
|
34 | - <option value='btn-success'><?php esc_html_e( 'Success', 'invoicing' ); ?></option> |
|
35 | - <option value='btn-danger'><?php esc_html_e( 'Danger', 'invoicing' ); ?></option> |
|
36 | - <option value='btn-warning'><?php esc_html_e( 'Warning', 'invoicing' ); ?></option> |
|
37 | - <option value='btn-info'><?php esc_html_e( 'Info', 'invoicing' ); ?></option> |
|
38 | - <option value='btn-light'><?php esc_html_e( 'Light', 'invoicing' ); ?></option> |
|
39 | - <option value='btn-dark'><?php esc_html_e( 'Dark', 'invoicing' ); ?></option> |
|
40 | - <option value='btn-link'><?php esc_html_e( 'Link', 'invoicing' ); ?></option> |
|
31 | + <option disabled><?php esc_html_e('Select button type', 'invoicing'); ?></option> |
|
32 | + <option value='btn-primary'><?php esc_html_e('Primary', 'invoicing'); ?></option> |
|
33 | + <option value='btn-secondary'><?php esc_html_e('Secondary', 'invoicing'); ?></option> |
|
34 | + <option value='btn-success'><?php esc_html_e('Success', 'invoicing'); ?></option> |
|
35 | + <option value='btn-danger'><?php esc_html_e('Danger', 'invoicing'); ?></option> |
|
36 | + <option value='btn-warning'><?php esc_html_e('Warning', 'invoicing'); ?></option> |
|
37 | + <option value='btn-info'><?php esc_html_e('Info', 'invoicing'); ?></option> |
|
38 | + <option value='btn-light'><?php esc_html_e('Light', 'invoicing'); ?></option> |
|
39 | + <option value='btn-dark'><?php esc_html_e('Dark', 'invoicing'); ?></option> |
|
40 | + <option value='btn-link'><?php esc_html_e('Link', 'invoicing'); ?></option> |
|
41 | 41 | </select> |
42 | 42 | </div> |
43 | 43 | |
44 | 44 | <div class='form-group mb-3'> |
45 | 45 | <label class="form-label d-block"> |
46 | - <span><?php esc_html_e( 'Help Text', 'invoicing' ); ?></span> |
|
47 | - <textarea placeholder='<?php esc_attr_e( 'Add some help text for this field', 'invoicing' ); ?>' v-model='active_form_element.description' class='form-control' rows='3'></textarea> |
|
48 | - <small class="form-text text-muted"><?php esc_html_e( 'HTML is allowed', 'invoicing' ); ?></small> |
|
46 | + <span><?php esc_html_e('Help Text', 'invoicing'); ?></span> |
|
47 | + <textarea placeholder='<?php esc_attr_e('Add some help text for this field', 'invoicing'); ?>' v-model='active_form_element.description' class='form-control' rows='3'></textarea> |
|
48 | + <small class="form-text text-muted"><?php esc_html_e('HTML is allowed', 'invoicing'); ?></small> |
|
49 | 49 | </label> |
50 | 50 | </div> |
@@ -8,35 +8,35 @@ |
||
8 | 8 | * @var GetPaid_Payment_Form $form The current payment form |
9 | 9 | */ |
10 | 10 | |
11 | -defined( 'ABSPATH' ) || exit; |
|
11 | +defined('ABSPATH') || exit; |
|
12 | 12 | |
13 | -if ( ! getpaid_has_published_discount() ) { |
|
13 | +if (!getpaid_has_published_discount()) { |
|
14 | 14 | return; |
15 | 15 | } |
16 | 16 | |
17 | -if ( ! empty( $description ) ) { |
|
17 | +if (!empty($description)) { |
|
18 | 18 | $description = "<small class='form-text text-muted'>$description</small>"; |
19 | 19 | } else { |
20 | 20 | $description = ''; |
21 | 21 | } |
22 | 22 | |
23 | 23 | $discount_code = ''; |
24 | -if ( ! empty( $form->invoice ) ) { |
|
24 | +if (!empty($form->invoice)) { |
|
25 | 25 | $discount_code = $form->invoice->get_discount_code(); |
26 | 26 | } |
27 | 27 | |
28 | -$class = empty( $class ) ? 'btn-secondary' : sanitize_html_class( $class ); |
|
28 | +$class = empty($class) ? 'btn-secondary' : sanitize_html_class($class); |
|
29 | 29 | ?> |
30 | 30 | |
31 | 31 | <div class="form-group mb-3"> |
32 | 32 | <div class="getpaid-discount-field border rounded p-3"> |
33 | 33 | <div class="getpaid-discount-field-inner d-flex flex-column flex-md-row"> |
34 | - <input name="discount" placeholder="<?php echo esc_attr( $input_label ); ?>" value="<?php echo esc_attr( $discount_code ); ?>" class="form-control mr-2 mb-2 getpaid-discount-field-input" style="flex: 1;" type="text"> |
|
35 | - <a href="#" class="btn <?php echo esc_attr( $class ); ?> submit-button mb-2 getpaid-discount-button"><?php echo esc_html( $button_label ); ?></a> |
|
34 | + <input name="discount" placeholder="<?php echo esc_attr($input_label); ?>" value="<?php echo esc_attr($discount_code); ?>" class="form-control mr-2 mb-2 getpaid-discount-field-input" style="flex: 1;" type="text"> |
|
35 | + <a href="#" class="btn <?php echo esc_attr($class); ?> submit-button mb-2 getpaid-discount-button"><?php echo esc_html($button_label); ?></a> |
|
36 | 36 | </div> |
37 | - <?php echo wp_kses_post( $description ); ?> |
|
37 | + <?php echo wp_kses_post($description); ?> |
|
38 | 38 | <div class="getpaid-custom-payment-form-errors alert alert-danger d-none"></div> |
39 | - <div class="getpaid-custom-payment-form-success alert alert-success d-none"><?php esc_html_e( 'Discount code applied!', 'invoicing' ); ?></div> |
|
39 | + <div class="getpaid-custom-payment-form-success alert alert-success d-none"><?php esc_html_e('Discount code applied!', 'invoicing'); ?></div> |
|
40 | 40 | </div> |
41 | 41 | </div> |
42 | 42 |
@@ -8,15 +8,15 @@ |
||
8 | 8 | * @var WPInv_Subscription $object |
9 | 9 | */ |
10 | 10 | |
11 | -defined( 'ABSPATH' ) || exit; |
|
11 | +defined('ABSPATH') || exit; |
|
12 | 12 | |
13 | 13 | $invoice = $object->get_parent_payment(); |
14 | 14 | |
15 | 15 | // Print the email header. |
16 | -do_action( 'wpinv_email_header', $email_heading, $invoice, $email_type, $sent_to_admin ); |
|
16 | +do_action('wpinv_email_header', $email_heading, $invoice, $email_type, $sent_to_admin); |
|
17 | 17 | |
18 | 18 | // Generate the custom message body. |
19 | -echo wp_kses_post( $message_body ); |
|
19 | +echo wp_kses_post($message_body); |
|
20 | 20 | |
21 | 21 | // Print the email footer. |
22 | -do_action( 'wpinv_email_footer', $invoice, $email_type, $sent_to_admin ); |
|
22 | +do_action('wpinv_email_footer', $invoice, $email_type, $sent_to_admin); |
@@ -1,264 +1,264 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | if ( ! defined( 'ABSPATH' ) ) { |
3 | - exit; |
|
3 | + exit; |
|
4 | 4 | } |
5 | 5 | |
6 | 6 | if ( ! class_exists( "AyeCode_Connect_Helper" ) ) { |
7 | - /** |
|
8 | - * Allow the quick setup and connection of our AyeCode Connect plugin. |
|
9 | - * |
|
10 | - * Class AyeCode_Connect_Helper |
|
11 | - */ |
|
12 | - class AyeCode_Connect_Helper { |
|
13 | - |
|
14 | - // Hold the version number |
|
15 | - var $version = "1.0.4"; |
|
16 | - |
|
17 | - // Hold the default strings. |
|
18 | - var $strings = array(); |
|
19 | - |
|
20 | - // Hold the default pages. |
|
21 | - var $pages = array(); |
|
22 | - |
|
23 | - /** |
|
24 | - * The constructor. |
|
25 | - * |
|
26 | - * AyeCode_Connect_Helper constructor. |
|
27 | - * |
|
28 | - * @param array $strings |
|
29 | - * @param array $pages |
|
30 | - */ |
|
31 | - public function __construct( $strings = array(), $pages = array() ) { |
|
32 | - // Only fire if not localhost and the current user has the right permissions. |
|
33 | - if ( ! $this->is_localhost() && current_user_can( 'manage_options' ) ) { |
|
34 | - // set default strings |
|
35 | - $default_strings = array( |
|
36 | - 'connect_title' => __( "Thanks for choosing an AyeCode Product!", 'ayecode-connect' ), |
|
37 | - 'connect_external' => __( "Please confirm you wish to connect your site?", 'ayecode-connect' ), |
|
38 | - 'connect' => wp_sprintf( __( "<strong>Have a license?</strong> Forget about entering license keys or downloading zip files, connect your site for instant access. %slearn more%s", 'ayecode-connect' ), "<a href='https://ayecode.io/introducing-ayecode-connect/' target='_blank'>", "</a>" ), |
|
39 | - 'connect_button' => __( "Connect Site", 'ayecode-connect' ), |
|
40 | - 'connecting_button' => __( "Connecting...", 'ayecode-connect' ), |
|
41 | - 'error_localhost' => __( "This service will only work with a live domain, not a localhost.", 'ayecode-connect' ), |
|
42 | - 'error' => __( "Something went wrong, please refresh and try again.", 'ayecode-connect' ), |
|
43 | - ); |
|
44 | - $this->strings = array_merge( $default_strings, $strings ); |
|
45 | - |
|
46 | - // set default pages |
|
47 | - $default_pages = array(); |
|
48 | - $this->pages = array_merge( $default_pages, $pages ); |
|
49 | - |
|
50 | - // maybe show connect site notice |
|
51 | - add_action( 'admin_notices', array( $this, 'ayecode_connect_install_notice' ) ); |
|
52 | - |
|
53 | - // add ajax action if not already added |
|
54 | - if ( ! has_action( 'wp_ajax_ayecode_connect_helper' ) ) { |
|
55 | - add_action( 'wp_ajax_ayecode_connect_helper', array( $this, 'ayecode_connect_install' ) ); |
|
56 | - } |
|
57 | - } |
|
58 | - |
|
59 | - // add ajax action if not already added |
|
60 | - if ( ! has_action( 'wp_ajax_nopriv_ayecode_connect_helper_installed' ) ) { |
|
61 | - add_action( 'wp_ajax_nopriv_ayecode_connect_helper_installed', array( $this, 'ayecode_connect_helper_installed' ) ); |
|
62 | - } |
|
63 | - } |
|
64 | - |
|
65 | - /** |
|
66 | - * Give a way to check we can connect via a external redirect. |
|
67 | - */ |
|
68 | - public function ayecode_connect_helper_installed(){ |
|
69 | - $active = array( |
|
70 | - 'gd' => defined('GEODIRECTORY_VERSION') && version_compare(GEODIRECTORY_VERSION,'2.0.0.79','>') ? 1 : 0, |
|
71 | - 'uwp' => defined('USERSWP_VERSION') && version_compare(USERSWP_VERSION,'1.2.1.5','>') ? 1 : 0, |
|
72 | - 'wpi' => defined('WPINV_VERSION') && version_compare(WPINV_VERSION,'1.0.14','>') ? 1 : 0, |
|
73 | - ); |
|
74 | - wp_send_json_success( $active ); |
|
75 | - wp_die(); |
|
76 | - } |
|
77 | - |
|
78 | - /** |
|
79 | - * Get slug from path |
|
80 | - * |
|
81 | - * @param string $key |
|
82 | - * |
|
83 | - * @return string |
|
84 | - */ |
|
85 | - private function format_plugin_slug( $key ) { |
|
86 | - $slug = explode( '/', $key ); |
|
87 | - $slug = explode( '.', end( $slug ) ); |
|
88 | - |
|
89 | - return $slug[0]; |
|
90 | - } |
|
91 | - |
|
92 | - /** |
|
93 | - * Install and activate the AyeCode Connect Plugin |
|
94 | - */ |
|
95 | - public function ayecode_connect_install() { |
|
96 | - // bail if localhost |
|
97 | - if ( $this->is_localhost() ) { |
|
98 | - wp_send_json_error( $this->strings['error_localhost'] ); |
|
99 | - } |
|
100 | - |
|
101 | - // Explicitly clear the event. |
|
102 | - wp_clear_scheduled_hook( 'geodir_plugin_background_installer', func_get_args() ); |
|
103 | - |
|
104 | - $success = true; |
|
105 | - $plugin_slug = "ayecode-connect"; |
|
106 | - if ( ! empty( $plugin_slug ) ) { |
|
107 | - require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
|
108 | - require_once( ABSPATH . 'wp-admin/includes/plugin-install.php' ); |
|
109 | - require_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' ); |
|
110 | - require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); |
|
111 | - |
|
112 | - WP_Filesystem(); |
|
113 | - |
|
114 | - $skin = new Automatic_Upgrader_Skin; |
|
115 | - $upgrader = new WP_Upgrader( $skin ); |
|
116 | - $installed_plugins = array_map( array( $this, 'format_plugin_slug' ), array_keys( get_plugins() ) ); |
|
117 | - $plugin_slug = $plugin_slug; |
|
118 | - $plugin = $plugin_slug . '/' . $plugin_slug . '.php'; |
|
119 | - $installed = false; |
|
120 | - $activate = false; |
|
121 | - |
|
122 | - // See if the plugin is installed already |
|
123 | - if ( in_array( $plugin_slug, $installed_plugins ) ) { |
|
124 | - $installed = true; |
|
125 | - $activate = ! is_plugin_active( $plugin ); |
|
126 | - } |
|
127 | - |
|
128 | - // Install this thing! |
|
129 | - if ( ! $installed ) { |
|
130 | - |
|
131 | - // Suppress feedback |
|
132 | - ob_start(); |
|
133 | - |
|
134 | - try { |
|
135 | - $plugin_information = plugins_api( 'plugin_information', array( |
|
136 | - 'slug' => $plugin_slug, |
|
137 | - 'fields' => array( |
|
138 | - 'short_description' => false, |
|
139 | - 'sections' => false, |
|
140 | - 'requires' => false, |
|
141 | - 'rating' => false, |
|
142 | - 'ratings' => false, |
|
143 | - 'downloaded' => false, |
|
144 | - 'last_updated' => false, |
|
145 | - 'added' => false, |
|
146 | - 'tags' => false, |
|
147 | - 'homepage' => false, |
|
148 | - 'donate_link' => false, |
|
149 | - 'author_profile' => false, |
|
150 | - 'author' => false, |
|
151 | - ), |
|
152 | - ) ); |
|
153 | - |
|
154 | - if ( is_wp_error( $plugin_information ) ) { |
|
155 | - throw new Exception( $plugin_information->get_error_message() ); |
|
156 | - } |
|
157 | - |
|
158 | - $package = $plugin_information->download_link; |
|
159 | - $download = $upgrader->download_package( $package ); |
|
160 | - |
|
161 | - if ( is_wp_error( $download ) ) { |
|
162 | - throw new Exception( $download->get_error_message() ); |
|
163 | - } |
|
164 | - |
|
165 | - $working_dir = $upgrader->unpack_package( $download, true ); |
|
166 | - |
|
167 | - if ( is_wp_error( $working_dir ) ) { |
|
168 | - throw new Exception( $working_dir->get_error_message() ); |
|
169 | - } |
|
170 | - |
|
171 | - $result = $upgrader->install_package( array( |
|
172 | - 'source' => $working_dir, |
|
173 | - 'destination' => WP_PLUGIN_DIR, |
|
174 | - 'clear_destination' => false, |
|
175 | - 'abort_if_destination_exists' => false, |
|
176 | - 'clear_working' => true, |
|
177 | - 'hook_extra' => array( |
|
178 | - 'type' => 'plugin', |
|
179 | - 'action' => 'install', |
|
180 | - ), |
|
181 | - ) ); |
|
182 | - |
|
183 | - if ( is_wp_error( $result ) ) { |
|
184 | - throw new Exception( $result->get_error_message() ); |
|
185 | - } |
|
186 | - |
|
187 | - $activate = true; |
|
188 | - |
|
189 | - } catch ( Exception $e ) { |
|
190 | - $success = false; |
|
191 | - } |
|
192 | - |
|
193 | - // Discard feedback |
|
194 | - ob_end_clean(); |
|
195 | - } |
|
196 | - |
|
197 | - wp_clean_plugins_cache(); |
|
198 | - |
|
199 | - // Activate this thing |
|
200 | - if ( $activate ) { |
|
201 | - try { |
|
202 | - $result = activate_plugin( $plugin ); |
|
203 | - |
|
204 | - if ( is_wp_error( $result ) ) { |
|
205 | - $success = false; |
|
206 | - } else { |
|
207 | - $success = true; |
|
208 | - } |
|
209 | - } catch ( Exception $e ) { |
|
210 | - $success = false; |
|
211 | - } |
|
212 | - } |
|
213 | - } |
|
214 | - |
|
215 | - if ( $success && function_exists( 'ayecode_connect_args' ) ) { |
|
216 | - ayecode_connect();// init |
|
217 | - $args = ayecode_connect_args(); |
|
218 | - $client = new AyeCode_Connect( $args ); |
|
219 | - $redirect_to = ! empty( $_POST['redirect_to'] ) ? esc_url_raw( $_POST['redirect_to'] ) : ''; |
|
220 | - $redirect = $client->build_connect_url( $redirect_to ); |
|
221 | - wp_send_json_success( array( 'connect_url' => $redirect ) ); |
|
222 | - } else { |
|
223 | - wp_send_json_error( $this->strings['error_localhost'] ); |
|
224 | - } |
|
225 | - wp_die(); |
|
226 | - } |
|
227 | - |
|
228 | - /** |
|
229 | - * Check if maybe localhost. |
|
230 | - * |
|
231 | - * @return bool |
|
232 | - */ |
|
233 | - public function is_localhost() { |
|
234 | - $localhost = false; |
|
235 | - |
|
236 | - $host = isset( $_SERVER['HTTP_HOST'] ) ? $_SERVER['HTTP_HOST'] : ''; |
|
237 | - $localhost_domains = array( |
|
238 | - 'localhost', |
|
239 | - 'localhost.localdomain', |
|
240 | - '127.0.0.1', |
|
241 | - '::1' |
|
242 | - ); |
|
243 | - |
|
244 | - if ( in_array( $host, $localhost_domains ) ) { |
|
245 | - $localhost = true; |
|
246 | - } |
|
247 | - |
|
248 | - return $localhost; |
|
249 | - } |
|
250 | - |
|
251 | - /** |
|
252 | - * Show notice to connect site. |
|
253 | - */ |
|
254 | - public function ayecode_connect_install_notice() { |
|
255 | - if ( $this->maybe_show() ) { |
|
256 | - $connect_title_string = $this->strings['connect_title']; |
|
257 | - $connect_external_string = $this->strings['connect_external']; |
|
258 | - $connect_string = $this->strings['connect']; |
|
259 | - $connect_button_string = $this->strings['connect_button']; |
|
260 | - $connecting_button_string = $this->strings['connecting_button']; |
|
261 | - ?> |
|
7 | + /** |
|
8 | + * Allow the quick setup and connection of our AyeCode Connect plugin. |
|
9 | + * |
|
10 | + * Class AyeCode_Connect_Helper |
|
11 | + */ |
|
12 | + class AyeCode_Connect_Helper { |
|
13 | + |
|
14 | + // Hold the version number |
|
15 | + var $version = "1.0.4"; |
|
16 | + |
|
17 | + // Hold the default strings. |
|
18 | + var $strings = array(); |
|
19 | + |
|
20 | + // Hold the default pages. |
|
21 | + var $pages = array(); |
|
22 | + |
|
23 | + /** |
|
24 | + * The constructor. |
|
25 | + * |
|
26 | + * AyeCode_Connect_Helper constructor. |
|
27 | + * |
|
28 | + * @param array $strings |
|
29 | + * @param array $pages |
|
30 | + */ |
|
31 | + public function __construct( $strings = array(), $pages = array() ) { |
|
32 | + // Only fire if not localhost and the current user has the right permissions. |
|
33 | + if ( ! $this->is_localhost() && current_user_can( 'manage_options' ) ) { |
|
34 | + // set default strings |
|
35 | + $default_strings = array( |
|
36 | + 'connect_title' => __( "Thanks for choosing an AyeCode Product!", 'ayecode-connect' ), |
|
37 | + 'connect_external' => __( "Please confirm you wish to connect your site?", 'ayecode-connect' ), |
|
38 | + 'connect' => wp_sprintf( __( "<strong>Have a license?</strong> Forget about entering license keys or downloading zip files, connect your site for instant access. %slearn more%s", 'ayecode-connect' ), "<a href='https://ayecode.io/introducing-ayecode-connect/' target='_blank'>", "</a>" ), |
|
39 | + 'connect_button' => __( "Connect Site", 'ayecode-connect' ), |
|
40 | + 'connecting_button' => __( "Connecting...", 'ayecode-connect' ), |
|
41 | + 'error_localhost' => __( "This service will only work with a live domain, not a localhost.", 'ayecode-connect' ), |
|
42 | + 'error' => __( "Something went wrong, please refresh and try again.", 'ayecode-connect' ), |
|
43 | + ); |
|
44 | + $this->strings = array_merge( $default_strings, $strings ); |
|
45 | + |
|
46 | + // set default pages |
|
47 | + $default_pages = array(); |
|
48 | + $this->pages = array_merge( $default_pages, $pages ); |
|
49 | + |
|
50 | + // maybe show connect site notice |
|
51 | + add_action( 'admin_notices', array( $this, 'ayecode_connect_install_notice' ) ); |
|
52 | + |
|
53 | + // add ajax action if not already added |
|
54 | + if ( ! has_action( 'wp_ajax_ayecode_connect_helper' ) ) { |
|
55 | + add_action( 'wp_ajax_ayecode_connect_helper', array( $this, 'ayecode_connect_install' ) ); |
|
56 | + } |
|
57 | + } |
|
58 | + |
|
59 | + // add ajax action if not already added |
|
60 | + if ( ! has_action( 'wp_ajax_nopriv_ayecode_connect_helper_installed' ) ) { |
|
61 | + add_action( 'wp_ajax_nopriv_ayecode_connect_helper_installed', array( $this, 'ayecode_connect_helper_installed' ) ); |
|
62 | + } |
|
63 | + } |
|
64 | + |
|
65 | + /** |
|
66 | + * Give a way to check we can connect via a external redirect. |
|
67 | + */ |
|
68 | + public function ayecode_connect_helper_installed(){ |
|
69 | + $active = array( |
|
70 | + 'gd' => defined('GEODIRECTORY_VERSION') && version_compare(GEODIRECTORY_VERSION,'2.0.0.79','>') ? 1 : 0, |
|
71 | + 'uwp' => defined('USERSWP_VERSION') && version_compare(USERSWP_VERSION,'1.2.1.5','>') ? 1 : 0, |
|
72 | + 'wpi' => defined('WPINV_VERSION') && version_compare(WPINV_VERSION,'1.0.14','>') ? 1 : 0, |
|
73 | + ); |
|
74 | + wp_send_json_success( $active ); |
|
75 | + wp_die(); |
|
76 | + } |
|
77 | + |
|
78 | + /** |
|
79 | + * Get slug from path |
|
80 | + * |
|
81 | + * @param string $key |
|
82 | + * |
|
83 | + * @return string |
|
84 | + */ |
|
85 | + private function format_plugin_slug( $key ) { |
|
86 | + $slug = explode( '/', $key ); |
|
87 | + $slug = explode( '.', end( $slug ) ); |
|
88 | + |
|
89 | + return $slug[0]; |
|
90 | + } |
|
91 | + |
|
92 | + /** |
|
93 | + * Install and activate the AyeCode Connect Plugin |
|
94 | + */ |
|
95 | + public function ayecode_connect_install() { |
|
96 | + // bail if localhost |
|
97 | + if ( $this->is_localhost() ) { |
|
98 | + wp_send_json_error( $this->strings['error_localhost'] ); |
|
99 | + } |
|
100 | + |
|
101 | + // Explicitly clear the event. |
|
102 | + wp_clear_scheduled_hook( 'geodir_plugin_background_installer', func_get_args() ); |
|
103 | + |
|
104 | + $success = true; |
|
105 | + $plugin_slug = "ayecode-connect"; |
|
106 | + if ( ! empty( $plugin_slug ) ) { |
|
107 | + require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
|
108 | + require_once( ABSPATH . 'wp-admin/includes/plugin-install.php' ); |
|
109 | + require_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' ); |
|
110 | + require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); |
|
111 | + |
|
112 | + WP_Filesystem(); |
|
113 | + |
|
114 | + $skin = new Automatic_Upgrader_Skin; |
|
115 | + $upgrader = new WP_Upgrader( $skin ); |
|
116 | + $installed_plugins = array_map( array( $this, 'format_plugin_slug' ), array_keys( get_plugins() ) ); |
|
117 | + $plugin_slug = $plugin_slug; |
|
118 | + $plugin = $plugin_slug . '/' . $plugin_slug . '.php'; |
|
119 | + $installed = false; |
|
120 | + $activate = false; |
|
121 | + |
|
122 | + // See if the plugin is installed already |
|
123 | + if ( in_array( $plugin_slug, $installed_plugins ) ) { |
|
124 | + $installed = true; |
|
125 | + $activate = ! is_plugin_active( $plugin ); |
|
126 | + } |
|
127 | + |
|
128 | + // Install this thing! |
|
129 | + if ( ! $installed ) { |
|
130 | + |
|
131 | + // Suppress feedback |
|
132 | + ob_start(); |
|
133 | + |
|
134 | + try { |
|
135 | + $plugin_information = plugins_api( 'plugin_information', array( |
|
136 | + 'slug' => $plugin_slug, |
|
137 | + 'fields' => array( |
|
138 | + 'short_description' => false, |
|
139 | + 'sections' => false, |
|
140 | + 'requires' => false, |
|
141 | + 'rating' => false, |
|
142 | + 'ratings' => false, |
|
143 | + 'downloaded' => false, |
|
144 | + 'last_updated' => false, |
|
145 | + 'added' => false, |
|
146 | + 'tags' => false, |
|
147 | + 'homepage' => false, |
|
148 | + 'donate_link' => false, |
|
149 | + 'author_profile' => false, |
|
150 | + 'author' => false, |
|
151 | + ), |
|
152 | + ) ); |
|
153 | + |
|
154 | + if ( is_wp_error( $plugin_information ) ) { |
|
155 | + throw new Exception( $plugin_information->get_error_message() ); |
|
156 | + } |
|
157 | + |
|
158 | + $package = $plugin_information->download_link; |
|
159 | + $download = $upgrader->download_package( $package ); |
|
160 | + |
|
161 | + if ( is_wp_error( $download ) ) { |
|
162 | + throw new Exception( $download->get_error_message() ); |
|
163 | + } |
|
164 | + |
|
165 | + $working_dir = $upgrader->unpack_package( $download, true ); |
|
166 | + |
|
167 | + if ( is_wp_error( $working_dir ) ) { |
|
168 | + throw new Exception( $working_dir->get_error_message() ); |
|
169 | + } |
|
170 | + |
|
171 | + $result = $upgrader->install_package( array( |
|
172 | + 'source' => $working_dir, |
|
173 | + 'destination' => WP_PLUGIN_DIR, |
|
174 | + 'clear_destination' => false, |
|
175 | + 'abort_if_destination_exists' => false, |
|
176 | + 'clear_working' => true, |
|
177 | + 'hook_extra' => array( |
|
178 | + 'type' => 'plugin', |
|
179 | + 'action' => 'install', |
|
180 | + ), |
|
181 | + ) ); |
|
182 | + |
|
183 | + if ( is_wp_error( $result ) ) { |
|
184 | + throw new Exception( $result->get_error_message() ); |
|
185 | + } |
|
186 | + |
|
187 | + $activate = true; |
|
188 | + |
|
189 | + } catch ( Exception $e ) { |
|
190 | + $success = false; |
|
191 | + } |
|
192 | + |
|
193 | + // Discard feedback |
|
194 | + ob_end_clean(); |
|
195 | + } |
|
196 | + |
|
197 | + wp_clean_plugins_cache(); |
|
198 | + |
|
199 | + // Activate this thing |
|
200 | + if ( $activate ) { |
|
201 | + try { |
|
202 | + $result = activate_plugin( $plugin ); |
|
203 | + |
|
204 | + if ( is_wp_error( $result ) ) { |
|
205 | + $success = false; |
|
206 | + } else { |
|
207 | + $success = true; |
|
208 | + } |
|
209 | + } catch ( Exception $e ) { |
|
210 | + $success = false; |
|
211 | + } |
|
212 | + } |
|
213 | + } |
|
214 | + |
|
215 | + if ( $success && function_exists( 'ayecode_connect_args' ) ) { |
|
216 | + ayecode_connect();// init |
|
217 | + $args = ayecode_connect_args(); |
|
218 | + $client = new AyeCode_Connect( $args ); |
|
219 | + $redirect_to = ! empty( $_POST['redirect_to'] ) ? esc_url_raw( $_POST['redirect_to'] ) : ''; |
|
220 | + $redirect = $client->build_connect_url( $redirect_to ); |
|
221 | + wp_send_json_success( array( 'connect_url' => $redirect ) ); |
|
222 | + } else { |
|
223 | + wp_send_json_error( $this->strings['error_localhost'] ); |
|
224 | + } |
|
225 | + wp_die(); |
|
226 | + } |
|
227 | + |
|
228 | + /** |
|
229 | + * Check if maybe localhost. |
|
230 | + * |
|
231 | + * @return bool |
|
232 | + */ |
|
233 | + public function is_localhost() { |
|
234 | + $localhost = false; |
|
235 | + |
|
236 | + $host = isset( $_SERVER['HTTP_HOST'] ) ? $_SERVER['HTTP_HOST'] : ''; |
|
237 | + $localhost_domains = array( |
|
238 | + 'localhost', |
|
239 | + 'localhost.localdomain', |
|
240 | + '127.0.0.1', |
|
241 | + '::1' |
|
242 | + ); |
|
243 | + |
|
244 | + if ( in_array( $host, $localhost_domains ) ) { |
|
245 | + $localhost = true; |
|
246 | + } |
|
247 | + |
|
248 | + return $localhost; |
|
249 | + } |
|
250 | + |
|
251 | + /** |
|
252 | + * Show notice to connect site. |
|
253 | + */ |
|
254 | + public function ayecode_connect_install_notice() { |
|
255 | + if ( $this->maybe_show() ) { |
|
256 | + $connect_title_string = $this->strings['connect_title']; |
|
257 | + $connect_external_string = $this->strings['connect_external']; |
|
258 | + $connect_string = $this->strings['connect']; |
|
259 | + $connect_button_string = $this->strings['connect_button']; |
|
260 | + $connecting_button_string = $this->strings['connecting_button']; |
|
261 | + ?> |
|
262 | 262 | <div class="notice notice-info acch-notice"> |
263 | 263 | <span class="acch-float-left"> |
264 | 264 | <svg width="61px" height="61px" viewBox="0 0 61 61" version="1.1" |
@@ -305,9 +305,9 @@ discard block |
||
305 | 305 | </div> |
306 | 306 | |
307 | 307 | <?php |
308 | - // only include the popup HTML if needed. |
|
309 | - if ( ! empty( $_REQUEST['external-connect-request'] ) ) { |
|
310 | - ?> |
|
308 | + // only include the popup HTML if needed. |
|
309 | + if ( ! empty( $_REQUEST['external-connect-request'] ) ) { |
|
310 | + ?> |
|
311 | 311 | <div id="ayecode-connect-helper-external-confirm" style="display:none;"> |
312 | 312 | <div class="noticex notice-info acch-notice" style="border: none;"> |
313 | 313 | <span class="acch-float-left"> |
@@ -353,23 +353,23 @@ discard block |
||
353 | 353 | </div> |
354 | 354 | </div> |
355 | 355 | <?php |
356 | - } |
|
357 | - |
|
358 | - // add required scripts |
|
359 | - $this->script(); |
|
360 | - } |
|
361 | - } |
|
362 | - |
|
363 | - /** |
|
364 | - * Get the JS Script. |
|
365 | - */ |
|
366 | - public function script() { |
|
367 | - |
|
368 | - // add thickbox if external request is requested |
|
369 | - if ( ! empty( $_REQUEST['external-connect-request'] ) ) { |
|
370 | - add_thickbox(); |
|
371 | - } |
|
372 | - ?> |
|
356 | + } |
|
357 | + |
|
358 | + // add required scripts |
|
359 | + $this->script(); |
|
360 | + } |
|
361 | + } |
|
362 | + |
|
363 | + /** |
|
364 | + * Get the JS Script. |
|
365 | + */ |
|
366 | + public function script() { |
|
367 | + |
|
368 | + // add thickbox if external request is requested |
|
369 | + if ( ! empty( $_REQUEST['external-connect-request'] ) ) { |
|
370 | + add_thickbox(); |
|
371 | + } |
|
372 | + ?> |
|
373 | 373 | <style> |
374 | 374 | .acch-title { |
375 | 375 | margin: 0; |
@@ -435,38 +435,38 @@ discard block |
||
435 | 435 | }); |
436 | 436 | } |
437 | 437 | <?php |
438 | - // add thickbox if external request is requested |
|
439 | - if(! empty( $_REQUEST['external-connect-request'] )) { |
|
440 | - ?> |
|
438 | + // add thickbox if external request is requested |
|
439 | + if(! empty( $_REQUEST['external-connect-request'] )) { |
|
440 | + ?> |
|
441 | 441 | jQuery(function () { |
442 | 442 | setTimeout(function () { |
443 | 443 | tb_show("AyeCode Connect", "?TB_inline?width=300&height=80&inlineId=ayecode-connect-helper-external-confirm"); |
444 | 444 | }, 200); |
445 | 445 | }); |
446 | 446 | <?php |
447 | - } |
|
448 | - ?> |
|
447 | + } |
|
448 | + ?> |
|
449 | 449 | </script> |
450 | 450 | <?php |
451 | - } |
|
452 | - |
|
453 | - /** |
|
454 | - * Decide what pages to show on. |
|
455 | - * |
|
456 | - * @return bool |
|
457 | - */ |
|
458 | - public function maybe_show() { |
|
459 | - $show = false; |
|
460 | - |
|
461 | - // check if on a page set to show |
|
462 | - if ( isset( $_REQUEST['page'] ) && in_array( $_REQUEST['page'], $this->pages ) ) { |
|
463 | - // check if not active and connected |
|
464 | - if ( ! defined( 'AYECODE_CONNECT_VERSION' ) || ! get_option( 'ayecode_connect_blog_token' ) ) { |
|
465 | - $show = true; |
|
466 | - } |
|
467 | - } |
|
468 | - |
|
469 | - return $show; |
|
470 | - } |
|
471 | - } |
|
451 | + } |
|
452 | + |
|
453 | + /** |
|
454 | + * Decide what pages to show on. |
|
455 | + * |
|
456 | + * @return bool |
|
457 | + */ |
|
458 | + public function maybe_show() { |
|
459 | + $show = false; |
|
460 | + |
|
461 | + // check if on a page set to show |
|
462 | + if ( isset( $_REQUEST['page'] ) && in_array( $_REQUEST['page'], $this->pages ) ) { |
|
463 | + // check if not active and connected |
|
464 | + if ( ! defined( 'AYECODE_CONNECT_VERSION' ) || ! get_option( 'ayecode_connect_blog_token' ) ) { |
|
465 | + $show = true; |
|
466 | + } |
|
467 | + } |
|
468 | + |
|
469 | + return $show; |
|
470 | + } |
|
471 | + } |
|
472 | 472 | } |
@@ -1,9 +1,9 @@ discard block |
||
1 | 1 | <?php |
2 | -if ( ! defined( 'ABSPATH' ) ) { |
|
2 | +if (!defined('ABSPATH')) { |
|
3 | 3 | exit; |
4 | 4 | } |
5 | 5 | |
6 | -if ( ! class_exists( "AyeCode_Connect_Helper" ) ) { |
|
6 | +if (!class_exists("AyeCode_Connect_Helper")) { |
|
7 | 7 | /** |
8 | 8 | * Allow the quick setup and connection of our AyeCode Connect plugin. |
9 | 9 | * |
@@ -28,50 +28,50 @@ discard block |
||
28 | 28 | * @param array $strings |
29 | 29 | * @param array $pages |
30 | 30 | */ |
31 | - public function __construct( $strings = array(), $pages = array() ) { |
|
31 | + public function __construct($strings = array(), $pages = array()) { |
|
32 | 32 | // Only fire if not localhost and the current user has the right permissions. |
33 | - if ( ! $this->is_localhost() && current_user_can( 'manage_options' ) ) { |
|
33 | + if (!$this->is_localhost() && current_user_can('manage_options')) { |
|
34 | 34 | // set default strings |
35 | 35 | $default_strings = array( |
36 | - 'connect_title' => __( "Thanks for choosing an AyeCode Product!", 'ayecode-connect' ), |
|
37 | - 'connect_external' => __( "Please confirm you wish to connect your site?", 'ayecode-connect' ), |
|
38 | - 'connect' => wp_sprintf( __( "<strong>Have a license?</strong> Forget about entering license keys or downloading zip files, connect your site for instant access. %slearn more%s", 'ayecode-connect' ), "<a href='https://ayecode.io/introducing-ayecode-connect/' target='_blank'>", "</a>" ), |
|
39 | - 'connect_button' => __( "Connect Site", 'ayecode-connect' ), |
|
40 | - 'connecting_button' => __( "Connecting...", 'ayecode-connect' ), |
|
41 | - 'error_localhost' => __( "This service will only work with a live domain, not a localhost.", 'ayecode-connect' ), |
|
42 | - 'error' => __( "Something went wrong, please refresh and try again.", 'ayecode-connect' ), |
|
36 | + 'connect_title' => __("Thanks for choosing an AyeCode Product!", 'ayecode-connect'), |
|
37 | + 'connect_external' => __("Please confirm you wish to connect your site?", 'ayecode-connect'), |
|
38 | + 'connect' => wp_sprintf(__("<strong>Have a license?</strong> Forget about entering license keys or downloading zip files, connect your site for instant access. %slearn more%s", 'ayecode-connect'), "<a href='https://ayecode.io/introducing-ayecode-connect/' target='_blank'>", "</a>"), |
|
39 | + 'connect_button' => __("Connect Site", 'ayecode-connect'), |
|
40 | + 'connecting_button' => __("Connecting...", 'ayecode-connect'), |
|
41 | + 'error_localhost' => __("This service will only work with a live domain, not a localhost.", 'ayecode-connect'), |
|
42 | + 'error' => __("Something went wrong, please refresh and try again.", 'ayecode-connect'), |
|
43 | 43 | ); |
44 | - $this->strings = array_merge( $default_strings, $strings ); |
|
44 | + $this->strings = array_merge($default_strings, $strings); |
|
45 | 45 | |
46 | 46 | // set default pages |
47 | 47 | $default_pages = array(); |
48 | - $this->pages = array_merge( $default_pages, $pages ); |
|
48 | + $this->pages = array_merge($default_pages, $pages); |
|
49 | 49 | |
50 | 50 | // maybe show connect site notice |
51 | - add_action( 'admin_notices', array( $this, 'ayecode_connect_install_notice' ) ); |
|
51 | + add_action('admin_notices', array($this, 'ayecode_connect_install_notice')); |
|
52 | 52 | |
53 | 53 | // add ajax action if not already added |
54 | - if ( ! has_action( 'wp_ajax_ayecode_connect_helper' ) ) { |
|
55 | - add_action( 'wp_ajax_ayecode_connect_helper', array( $this, 'ayecode_connect_install' ) ); |
|
54 | + if (!has_action('wp_ajax_ayecode_connect_helper')) { |
|
55 | + add_action('wp_ajax_ayecode_connect_helper', array($this, 'ayecode_connect_install')); |
|
56 | 56 | } |
57 | 57 | } |
58 | 58 | |
59 | 59 | // add ajax action if not already added |
60 | - if ( ! has_action( 'wp_ajax_nopriv_ayecode_connect_helper_installed' ) ) { |
|
61 | - add_action( 'wp_ajax_nopriv_ayecode_connect_helper_installed', array( $this, 'ayecode_connect_helper_installed' ) ); |
|
60 | + if (!has_action('wp_ajax_nopriv_ayecode_connect_helper_installed')) { |
|
61 | + add_action('wp_ajax_nopriv_ayecode_connect_helper_installed', array($this, 'ayecode_connect_helper_installed')); |
|
62 | 62 | } |
63 | 63 | } |
64 | 64 | |
65 | 65 | /** |
66 | 66 | * Give a way to check we can connect via a external redirect. |
67 | 67 | */ |
68 | - public function ayecode_connect_helper_installed(){ |
|
68 | + public function ayecode_connect_helper_installed() { |
|
69 | 69 | $active = array( |
70 | - 'gd' => defined('GEODIRECTORY_VERSION') && version_compare(GEODIRECTORY_VERSION,'2.0.0.79','>') ? 1 : 0, |
|
71 | - 'uwp' => defined('USERSWP_VERSION') && version_compare(USERSWP_VERSION,'1.2.1.5','>') ? 1 : 0, |
|
72 | - 'wpi' => defined('WPINV_VERSION') && version_compare(WPINV_VERSION,'1.0.14','>') ? 1 : 0, |
|
70 | + 'gd' => defined('GEODIRECTORY_VERSION') && version_compare(GEODIRECTORY_VERSION, '2.0.0.79', '>') ? 1 : 0, |
|
71 | + 'uwp' => defined('USERSWP_VERSION') && version_compare(USERSWP_VERSION, '1.2.1.5', '>') ? 1 : 0, |
|
72 | + 'wpi' => defined('WPINV_VERSION') && version_compare(WPINV_VERSION, '1.0.14', '>') ? 1 : 0, |
|
73 | 73 | ); |
74 | - wp_send_json_success( $active ); |
|
74 | + wp_send_json_success($active); |
|
75 | 75 | wp_die(); |
76 | 76 | } |
77 | 77 | |
@@ -82,9 +82,9 @@ discard block |
||
82 | 82 | * |
83 | 83 | * @return string |
84 | 84 | */ |
85 | - private function format_plugin_slug( $key ) { |
|
86 | - $slug = explode( '/', $key ); |
|
87 | - $slug = explode( '.', end( $slug ) ); |
|
85 | + private function format_plugin_slug($key) { |
|
86 | + $slug = explode('/', $key); |
|
87 | + $slug = explode('.', end($slug)); |
|
88 | 88 | |
89 | 89 | return $slug[0]; |
90 | 90 | } |
@@ -94,45 +94,45 @@ discard block |
||
94 | 94 | */ |
95 | 95 | public function ayecode_connect_install() { |
96 | 96 | // bail if localhost |
97 | - if ( $this->is_localhost() ) { |
|
98 | - wp_send_json_error( $this->strings['error_localhost'] ); |
|
97 | + if ($this->is_localhost()) { |
|
98 | + wp_send_json_error($this->strings['error_localhost']); |
|
99 | 99 | } |
100 | 100 | |
101 | 101 | // Explicitly clear the event. |
102 | - wp_clear_scheduled_hook( 'geodir_plugin_background_installer', func_get_args() ); |
|
102 | + wp_clear_scheduled_hook('geodir_plugin_background_installer', func_get_args()); |
|
103 | 103 | |
104 | 104 | $success = true; |
105 | 105 | $plugin_slug = "ayecode-connect"; |
106 | - if ( ! empty( $plugin_slug ) ) { |
|
107 | - require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
|
108 | - require_once( ABSPATH . 'wp-admin/includes/plugin-install.php' ); |
|
109 | - require_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' ); |
|
110 | - require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); |
|
106 | + if (!empty($plugin_slug)) { |
|
107 | + require_once(ABSPATH . 'wp-admin/includes/file.php'); |
|
108 | + require_once(ABSPATH . 'wp-admin/includes/plugin-install.php'); |
|
109 | + require_once(ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'); |
|
110 | + require_once(ABSPATH . 'wp-admin/includes/plugin.php'); |
|
111 | 111 | |
112 | 112 | WP_Filesystem(); |
113 | 113 | |
114 | 114 | $skin = new Automatic_Upgrader_Skin; |
115 | - $upgrader = new WP_Upgrader( $skin ); |
|
116 | - $installed_plugins = array_map( array( $this, 'format_plugin_slug' ), array_keys( get_plugins() ) ); |
|
115 | + $upgrader = new WP_Upgrader($skin); |
|
116 | + $installed_plugins = array_map(array($this, 'format_plugin_slug'), array_keys(get_plugins())); |
|
117 | 117 | $plugin_slug = $plugin_slug; |
118 | 118 | $plugin = $plugin_slug . '/' . $plugin_slug . '.php'; |
119 | 119 | $installed = false; |
120 | 120 | $activate = false; |
121 | 121 | |
122 | 122 | // See if the plugin is installed already |
123 | - if ( in_array( $plugin_slug, $installed_plugins ) ) { |
|
123 | + if (in_array($plugin_slug, $installed_plugins)) { |
|
124 | 124 | $installed = true; |
125 | - $activate = ! is_plugin_active( $plugin ); |
|
125 | + $activate = !is_plugin_active($plugin); |
|
126 | 126 | } |
127 | 127 | |
128 | 128 | // Install this thing! |
129 | - if ( ! $installed ) { |
|
129 | + if (!$installed) { |
|
130 | 130 | |
131 | 131 | // Suppress feedback |
132 | 132 | ob_start(); |
133 | 133 | |
134 | 134 | try { |
135 | - $plugin_information = plugins_api( 'plugin_information', array( |
|
135 | + $plugin_information = plugins_api('plugin_information', array( |
|
136 | 136 | 'slug' => $plugin_slug, |
137 | 137 | 'fields' => array( |
138 | 138 | 'short_description' => false, |
@@ -149,26 +149,26 @@ discard block |
||
149 | 149 | 'author_profile' => false, |
150 | 150 | 'author' => false, |
151 | 151 | ), |
152 | - ) ); |
|
152 | + )); |
|
153 | 153 | |
154 | - if ( is_wp_error( $plugin_information ) ) { |
|
155 | - throw new Exception( $plugin_information->get_error_message() ); |
|
154 | + if (is_wp_error($plugin_information)) { |
|
155 | + throw new Exception($plugin_information->get_error_message()); |
|
156 | 156 | } |
157 | 157 | |
158 | 158 | $package = $plugin_information->download_link; |
159 | - $download = $upgrader->download_package( $package ); |
|
159 | + $download = $upgrader->download_package($package); |
|
160 | 160 | |
161 | - if ( is_wp_error( $download ) ) { |
|
162 | - throw new Exception( $download->get_error_message() ); |
|
161 | + if (is_wp_error($download)) { |
|
162 | + throw new Exception($download->get_error_message()); |
|
163 | 163 | } |
164 | 164 | |
165 | - $working_dir = $upgrader->unpack_package( $download, true ); |
|
165 | + $working_dir = $upgrader->unpack_package($download, true); |
|
166 | 166 | |
167 | - if ( is_wp_error( $working_dir ) ) { |
|
168 | - throw new Exception( $working_dir->get_error_message() ); |
|
167 | + if (is_wp_error($working_dir)) { |
|
168 | + throw new Exception($working_dir->get_error_message()); |
|
169 | 169 | } |
170 | 170 | |
171 | - $result = $upgrader->install_package( array( |
|
171 | + $result = $upgrader->install_package(array( |
|
172 | 172 | 'source' => $working_dir, |
173 | 173 | 'destination' => WP_PLUGIN_DIR, |
174 | 174 | 'clear_destination' => false, |
@@ -178,15 +178,15 @@ discard block |
||
178 | 178 | 'type' => 'plugin', |
179 | 179 | 'action' => 'install', |
180 | 180 | ), |
181 | - ) ); |
|
181 | + )); |
|
182 | 182 | |
183 | - if ( is_wp_error( $result ) ) { |
|
184 | - throw new Exception( $result->get_error_message() ); |
|
183 | + if (is_wp_error($result)) { |
|
184 | + throw new Exception($result->get_error_message()); |
|
185 | 185 | } |
186 | 186 | |
187 | 187 | $activate = true; |
188 | 188 | |
189 | - } catch ( Exception $e ) { |
|
189 | + } catch (Exception $e) { |
|
190 | 190 | $success = false; |
191 | 191 | } |
192 | 192 | |
@@ -197,30 +197,30 @@ discard block |
||
197 | 197 | wp_clean_plugins_cache(); |
198 | 198 | |
199 | 199 | // Activate this thing |
200 | - if ( $activate ) { |
|
200 | + if ($activate) { |
|
201 | 201 | try { |
202 | - $result = activate_plugin( $plugin ); |
|
202 | + $result = activate_plugin($plugin); |
|
203 | 203 | |
204 | - if ( is_wp_error( $result ) ) { |
|
204 | + if (is_wp_error($result)) { |
|
205 | 205 | $success = false; |
206 | 206 | } else { |
207 | 207 | $success = true; |
208 | 208 | } |
209 | - } catch ( Exception $e ) { |
|
209 | + } catch (Exception $e) { |
|
210 | 210 | $success = false; |
211 | 211 | } |
212 | 212 | } |
213 | 213 | } |
214 | 214 | |
215 | - if ( $success && function_exists( 'ayecode_connect_args' ) ) { |
|
216 | - ayecode_connect();// init |
|
215 | + if ($success && function_exists('ayecode_connect_args')) { |
|
216 | + ayecode_connect(); // init |
|
217 | 217 | $args = ayecode_connect_args(); |
218 | - $client = new AyeCode_Connect( $args ); |
|
219 | - $redirect_to = ! empty( $_POST['redirect_to'] ) ? esc_url_raw( $_POST['redirect_to'] ) : ''; |
|
220 | - $redirect = $client->build_connect_url( $redirect_to ); |
|
221 | - wp_send_json_success( array( 'connect_url' => $redirect ) ); |
|
218 | + $client = new AyeCode_Connect($args); |
|
219 | + $redirect_to = !empty($_POST['redirect_to']) ? esc_url_raw($_POST['redirect_to']) : ''; |
|
220 | + $redirect = $client->build_connect_url($redirect_to); |
|
221 | + wp_send_json_success(array('connect_url' => $redirect)); |
|
222 | 222 | } else { |
223 | - wp_send_json_error( $this->strings['error_localhost'] ); |
|
223 | + wp_send_json_error($this->strings['error_localhost']); |
|
224 | 224 | } |
225 | 225 | wp_die(); |
226 | 226 | } |
@@ -233,7 +233,7 @@ discard block |
||
233 | 233 | public function is_localhost() { |
234 | 234 | $localhost = false; |
235 | 235 | |
236 | - $host = isset( $_SERVER['HTTP_HOST'] ) ? $_SERVER['HTTP_HOST'] : ''; |
|
236 | + $host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : ''; |
|
237 | 237 | $localhost_domains = array( |
238 | 238 | 'localhost', |
239 | 239 | 'localhost.localdomain', |
@@ -241,7 +241,7 @@ discard block |
||
241 | 241 | '::1' |
242 | 242 | ); |
243 | 243 | |
244 | - if ( in_array( $host, $localhost_domains ) ) { |
|
244 | + if (in_array($host, $localhost_domains)) { |
|
245 | 245 | $localhost = true; |
246 | 246 | } |
247 | 247 | |
@@ -252,7 +252,7 @@ discard block |
||
252 | 252 | * Show notice to connect site. |
253 | 253 | */ |
254 | 254 | public function ayecode_connect_install_notice() { |
255 | - if ( $this->maybe_show() ) { |
|
255 | + if ($this->maybe_show()) { |
|
256 | 256 | $connect_title_string = $this->strings['connect_title']; |
257 | 257 | $connect_external_string = $this->strings['connect_external']; |
258 | 258 | $connect_string = $this->strings['connect']; |
@@ -294,19 +294,19 @@ discard block |
||
294 | 294 | </svg> |
295 | 295 | </span> |
296 | 296 | <span class="acch-float-left acch-text"> |
297 | - <h3 class="acch-title"><?php echo esc_attr( $connect_title_string ); ?></h3> |
|
297 | + <h3 class="acch-title"><?php echo esc_attr($connect_title_string); ?></h3> |
|
298 | 298 | <p><?php echo $connect_string; ?> |
299 | 299 | </p> |
300 | 300 | </span> |
301 | 301 | |
302 | 302 | <span class="acch-float-left acch-button"> |
303 | - <button onclick="ayecode_connect_helper(this);" id="gd-connect-site" class="button button-primary" data-connecting="<?php echo esc_attr( $connecting_button_string ); ?>"><?php echo esc_attr( $connect_button_string ) ?></button> |
|
303 | + <button onclick="ayecode_connect_helper(this);" id="gd-connect-site" class="button button-primary" data-connecting="<?php echo esc_attr($connecting_button_string); ?>"><?php echo esc_attr($connect_button_string) ?></button> |
|
304 | 304 | </span> |
305 | 305 | </div> |
306 | 306 | |
307 | 307 | <?php |
308 | 308 | // only include the popup HTML if needed. |
309 | - if ( ! empty( $_REQUEST['external-connect-request'] ) ) { |
|
309 | + if (!empty($_REQUEST['external-connect-request'])) { |
|
310 | 310 | ?> |
311 | 311 | <div id="ayecode-connect-helper-external-confirm" style="display:none;"> |
312 | 312 | <div class="noticex notice-info acch-notice" style="border: none;"> |
@@ -344,11 +344,11 @@ discard block |
||
344 | 344 | </svg> |
345 | 345 | </span> |
346 | 346 | <span class="acch-float-left acch-text"> |
347 | - <h3 class="acch-title"><?php echo esc_attr( $connect_external_string ); ?></h3> |
|
347 | + <h3 class="acch-title"><?php echo esc_attr($connect_external_string); ?></h3> |
|
348 | 348 | </span> |
349 | 349 | |
350 | 350 | <span class="acch-float-left acch-button"> |
351 | - <button onclick="ayecode_connect_helper(this);" id="gd-connect-site" class="button button-primary" data-connecting="<?php echo esc_attr( $connecting_button_string ); ?>"><?php echo esc_attr( $connect_button_string ) ?></button> |
|
351 | + <button onclick="ayecode_connect_helper(this);" id="gd-connect-site" class="button button-primary" data-connecting="<?php echo esc_attr($connecting_button_string); ?>"><?php echo esc_attr($connect_button_string) ?></button> |
|
352 | 352 | </span> |
353 | 353 | </div> |
354 | 354 | </div> |
@@ -366,7 +366,7 @@ discard block |
||
366 | 366 | public function script() { |
367 | 367 | |
368 | 368 | // add thickbox if external request is requested |
369 | - if ( ! empty( $_REQUEST['external-connect-request'] ) ) { |
|
369 | + if (!empty($_REQUEST['external-connect-request'])) { |
|
370 | 370 | add_thickbox(); |
371 | 371 | } |
372 | 372 | ?> |
@@ -417,7 +417,7 @@ discard block |
||
417 | 417 | url: ajaxurl, |
418 | 418 | data: { |
419 | 419 | action: 'ayecode_connect_helper', |
420 | - security: '<?php echo wp_create_nonce( 'ayecode-connect-helper' );?>', |
|
420 | + security: '<?php echo wp_create_nonce('ayecode-connect-helper'); ?>', |
|
421 | 421 | redirect_to: $current_url |
422 | 422 | }, |
423 | 423 | beforeSend: function () { |
@@ -436,7 +436,7 @@ discard block |
||
436 | 436 | } |
437 | 437 | <?php |
438 | 438 | // add thickbox if external request is requested |
439 | - if(! empty( $_REQUEST['external-connect-request'] )) { |
|
439 | + if (!empty($_REQUEST['external-connect-request'])) { |
|
440 | 440 | ?> |
441 | 441 | jQuery(function () { |
442 | 442 | setTimeout(function () { |
@@ -459,9 +459,9 @@ discard block |
||
459 | 459 | $show = false; |
460 | 460 | |
461 | 461 | // check if on a page set to show |
462 | - if ( isset( $_REQUEST['page'] ) && in_array( $_REQUEST['page'], $this->pages ) ) { |
|
462 | + if (isset($_REQUEST['page']) && in_array($_REQUEST['page'], $this->pages)) { |
|
463 | 463 | // check if not active and connected |
464 | - if ( ! defined( 'AYECODE_CONNECT_VERSION' ) || ! get_option( 'ayecode_connect_blog_token' ) ) { |
|
464 | + if (!defined('AYECODE_CONNECT_VERSION') || !get_option('ayecode_connect_blog_token')) { |
|
465 | 465 | $show = true; |
466 | 466 | } |
467 | 467 | } |
@@ -1,99 +1,99 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | if ( ! defined( 'ABSPATH' ) ) { |
4 | - exit; |
|
4 | + exit; |
|
5 | 5 | } |
6 | 6 | |
7 | 7 | if ( ! class_exists( 'AyeCode_Deactivation_Survey' ) ) { |
8 | 8 | |
9 | - class AyeCode_Deactivation_Survey { |
|
9 | + class AyeCode_Deactivation_Survey { |
|
10 | 10 | |
11 | - /** |
|
12 | - * AyeCode_Deactivation_Survey instance. |
|
13 | - * |
|
14 | - * @access private |
|
15 | - * @since 1.0.0 |
|
16 | - * @var AyeCode_Deactivation_Survey There can be only one! |
|
17 | - */ |
|
18 | - private static $instance = null; |
|
11 | + /** |
|
12 | + * AyeCode_Deactivation_Survey instance. |
|
13 | + * |
|
14 | + * @access private |
|
15 | + * @since 1.0.0 |
|
16 | + * @var AyeCode_Deactivation_Survey There can be only one! |
|
17 | + */ |
|
18 | + private static $instance = null; |
|
19 | 19 | |
20 | - public static $plugins; |
|
20 | + public static $plugins; |
|
21 | 21 | |
22 | - public $version = "1.0.7"; |
|
22 | + public $version = "1.0.7"; |
|
23 | 23 | |
24 | - public static function instance( $plugin = array() ) { |
|
25 | - if ( ! isset( self::$instance ) && ! ( self::$instance instanceof AyeCode_Deactivation_Survey ) ) { |
|
26 | - self::$instance = new AyeCode_Deactivation_Survey; |
|
27 | - self::$plugins = array(); |
|
24 | + public static function instance( $plugin = array() ) { |
|
25 | + if ( ! isset( self::$instance ) && ! ( self::$instance instanceof AyeCode_Deactivation_Survey ) ) { |
|
26 | + self::$instance = new AyeCode_Deactivation_Survey; |
|
27 | + self::$plugins = array(); |
|
28 | 28 | |
29 | - add_action( 'admin_enqueue_scripts', array( self::$instance, 'scripts' ) ); |
|
29 | + add_action( 'admin_enqueue_scripts', array( self::$instance, 'scripts' ) ); |
|
30 | 30 | |
31 | - do_action( 'ayecode_deactivation_survey_loaded' ); |
|
32 | - } |
|
31 | + do_action( 'ayecode_deactivation_survey_loaded' ); |
|
32 | + } |
|
33 | 33 | |
34 | - if(!empty($plugin)){ |
|
35 | - self::$plugins[] = (object)$plugin; |
|
36 | - } |
|
34 | + if(!empty($plugin)){ |
|
35 | + self::$plugins[] = (object)$plugin; |
|
36 | + } |
|
37 | 37 | |
38 | - return self::$instance; |
|
39 | - } |
|
38 | + return self::$instance; |
|
39 | + } |
|
40 | 40 | |
41 | - public function scripts() { |
|
42 | - global $pagenow; |
|
41 | + public function scripts() { |
|
42 | + global $pagenow; |
|
43 | 43 | |
44 | - // Bail if we are not on the plugins page |
|
45 | - if ( $pagenow != "plugins.php" ) { |
|
46 | - return; |
|
47 | - } |
|
44 | + // Bail if we are not on the plugins page |
|
45 | + if ( $pagenow != "plugins.php" ) { |
|
46 | + return; |
|
47 | + } |
|
48 | 48 | |
49 | - // Enqueue scripts |
|
50 | - add_thickbox(); |
|
51 | - wp_enqueue_script('ayecode-deactivation-survey', plugin_dir_url(__FILE__) . 'ayecode-ds.js'); |
|
49 | + // Enqueue scripts |
|
50 | + add_thickbox(); |
|
51 | + wp_enqueue_script('ayecode-deactivation-survey', plugin_dir_url(__FILE__) . 'ayecode-ds.js'); |
|
52 | 52 | |
53 | - /* |
|
53 | + /* |
|
54 | 54 | * Localized strings. Strings can be localised by plugins using this class. |
55 | 55 | * We deliberately don't add textdomains here so that double textdomain warning is not given in theme review. |
56 | 56 | */ |
57 | - wp_localize_script('ayecode-deactivation-survey', 'ayecodeds_deactivate_feedback_form_strings', array( |
|
58 | - 'quick_feedback' => __( 'Quick Feedback', 'ayecode-connect' ), |
|
59 | - 'foreword' => __( 'If you would be kind enough, please tell us why you\'re deactivating?', 'ayecode-connect' ), |
|
60 | - 'better_plugins_name' => __( 'Please tell us which plugin?', 'ayecode-connect' ), |
|
61 | - 'please_tell_us' => __( 'Please tell us the reason so we can improve the plugin', 'ayecode-connect' ), |
|
62 | - 'do_not_attach_email' => __( 'Do not send my e-mail address with this feedback', 'ayecode-connect' ), |
|
63 | - 'brief_description' => __( 'Please give us any feedback that could help us improve', 'ayecode-connect' ), |
|
64 | - 'cancel' => __( 'Cancel', 'ayecode-connect' ), |
|
65 | - 'skip_and_deactivate' => __( 'Skip & Deactivate', 'ayecode-connect' ), |
|
66 | - 'submit_and_deactivate' => __( 'Submit & Deactivate', 'ayecode-connect' ), |
|
67 | - 'please_wait' => __( 'Please wait', 'ayecode-connect' ), |
|
68 | - 'get_support' => __( 'Get Support', 'ayecode-connect' ), |
|
69 | - 'documentation' => __( 'Documentation', 'ayecode-connect' ), |
|
70 | - 'thank_you' => __( 'Thank you!', 'ayecode-connect' ), |
|
71 | - )); |
|
72 | - |
|
73 | - // Plugins |
|
74 | - $plugins = apply_filters('ayecode_deactivation_survey_plugins', self::$plugins); |
|
75 | - |
|
76 | - // Reasons |
|
77 | - $defaultReasons = array( |
|
78 | - 'suddenly-stopped-working' => __( 'The plugin suddenly stopped working', 'ayecode-connect' ), |
|
79 | - 'plugin-broke-site' => __( 'The plugin broke my site', 'ayecode-connect' ), |
|
80 | - 'plugin-setup-difficult' => __( 'Too difficult to setup', 'ayecode-connect' ), |
|
81 | - 'plugin-design-difficult' => __( 'Too difficult to get the design i want', 'ayecode-connect' ), |
|
82 | - 'no-longer-needed' => __( 'I don\'t need this plugin any more', 'ayecode-connect' ), |
|
83 | - 'found-better-plugin' => __( 'I found a better plugin', 'ayecode-connect' ), |
|
84 | - 'temporary-deactivation' => __( 'It\'s a temporary deactivation, I\'m troubleshooting', 'ayecode-connect' ), |
|
85 | - 'other' => __( 'Other', 'ayecode-connect' ), |
|
86 | - ); |
|
87 | - |
|
88 | - foreach( $plugins as $plugin ) { |
|
89 | - $plugin->reasons = apply_filters( 'ayecode_deactivation_survey_reasons', $defaultReasons, $plugin ); |
|
90 | - $plugin->url = home_url(); |
|
91 | - $plugin->activated = 0; |
|
92 | - } |
|
93 | - |
|
94 | - // Send plugin data |
|
95 | - wp_localize_script('ayecode-deactivation-survey', 'ayecodeds_deactivate_feedback_form_plugins', $plugins); |
|
96 | - } |
|
97 | - } |
|
57 | + wp_localize_script('ayecode-deactivation-survey', 'ayecodeds_deactivate_feedback_form_strings', array( |
|
58 | + 'quick_feedback' => __( 'Quick Feedback', 'ayecode-connect' ), |
|
59 | + 'foreword' => __( 'If you would be kind enough, please tell us why you\'re deactivating?', 'ayecode-connect' ), |
|
60 | + 'better_plugins_name' => __( 'Please tell us which plugin?', 'ayecode-connect' ), |
|
61 | + 'please_tell_us' => __( 'Please tell us the reason so we can improve the plugin', 'ayecode-connect' ), |
|
62 | + 'do_not_attach_email' => __( 'Do not send my e-mail address with this feedback', 'ayecode-connect' ), |
|
63 | + 'brief_description' => __( 'Please give us any feedback that could help us improve', 'ayecode-connect' ), |
|
64 | + 'cancel' => __( 'Cancel', 'ayecode-connect' ), |
|
65 | + 'skip_and_deactivate' => __( 'Skip & Deactivate', 'ayecode-connect' ), |
|
66 | + 'submit_and_deactivate' => __( 'Submit & Deactivate', 'ayecode-connect' ), |
|
67 | + 'please_wait' => __( 'Please wait', 'ayecode-connect' ), |
|
68 | + 'get_support' => __( 'Get Support', 'ayecode-connect' ), |
|
69 | + 'documentation' => __( 'Documentation', 'ayecode-connect' ), |
|
70 | + 'thank_you' => __( 'Thank you!', 'ayecode-connect' ), |
|
71 | + )); |
|
72 | + |
|
73 | + // Plugins |
|
74 | + $plugins = apply_filters('ayecode_deactivation_survey_plugins', self::$plugins); |
|
75 | + |
|
76 | + // Reasons |
|
77 | + $defaultReasons = array( |
|
78 | + 'suddenly-stopped-working' => __( 'The plugin suddenly stopped working', 'ayecode-connect' ), |
|
79 | + 'plugin-broke-site' => __( 'The plugin broke my site', 'ayecode-connect' ), |
|
80 | + 'plugin-setup-difficult' => __( 'Too difficult to setup', 'ayecode-connect' ), |
|
81 | + 'plugin-design-difficult' => __( 'Too difficult to get the design i want', 'ayecode-connect' ), |
|
82 | + 'no-longer-needed' => __( 'I don\'t need this plugin any more', 'ayecode-connect' ), |
|
83 | + 'found-better-plugin' => __( 'I found a better plugin', 'ayecode-connect' ), |
|
84 | + 'temporary-deactivation' => __( 'It\'s a temporary deactivation, I\'m troubleshooting', 'ayecode-connect' ), |
|
85 | + 'other' => __( 'Other', 'ayecode-connect' ), |
|
86 | + ); |
|
87 | + |
|
88 | + foreach( $plugins as $plugin ) { |
|
89 | + $plugin->reasons = apply_filters( 'ayecode_deactivation_survey_reasons', $defaultReasons, $plugin ); |
|
90 | + $plugin->url = home_url(); |
|
91 | + $plugin->activated = 0; |
|
92 | + } |
|
93 | + |
|
94 | + // Send plugin data |
|
95 | + wp_localize_script('ayecode-deactivation-survey', 'ayecodeds_deactivate_feedback_form_plugins', $plugins); |
|
96 | + } |
|
97 | + } |
|
98 | 98 | |
99 | 99 | } |
100 | 100 | \ No newline at end of file |
@@ -1,10 +1,10 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -if ( ! defined( 'ABSPATH' ) ) { |
|
3 | +if (!defined('ABSPATH')) { |
|
4 | 4 | exit; |
5 | 5 | } |
6 | 6 | |
7 | -if ( ! class_exists( 'AyeCode_Deactivation_Survey' ) ) { |
|
7 | +if (!class_exists('AyeCode_Deactivation_Survey')) { |
|
8 | 8 | |
9 | 9 | class AyeCode_Deactivation_Survey { |
10 | 10 | |
@@ -21,18 +21,18 @@ discard block |
||
21 | 21 | |
22 | 22 | public $version = "1.0.7"; |
23 | 23 | |
24 | - public static function instance( $plugin = array() ) { |
|
25 | - if ( ! isset( self::$instance ) && ! ( self::$instance instanceof AyeCode_Deactivation_Survey ) ) { |
|
24 | + public static function instance($plugin = array()) { |
|
25 | + if (!isset(self::$instance) && !(self::$instance instanceof AyeCode_Deactivation_Survey)) { |
|
26 | 26 | self::$instance = new AyeCode_Deactivation_Survey; |
27 | 27 | self::$plugins = array(); |
28 | 28 | |
29 | - add_action( 'admin_enqueue_scripts', array( self::$instance, 'scripts' ) ); |
|
29 | + add_action('admin_enqueue_scripts', array(self::$instance, 'scripts')); |
|
30 | 30 | |
31 | - do_action( 'ayecode_deactivation_survey_loaded' ); |
|
31 | + do_action('ayecode_deactivation_survey_loaded'); |
|
32 | 32 | } |
33 | 33 | |
34 | - if(!empty($plugin)){ |
|
35 | - self::$plugins[] = (object)$plugin; |
|
34 | + if (!empty($plugin)) { |
|
35 | + self::$plugins[] = (object) $plugin; |
|
36 | 36 | } |
37 | 37 | |
38 | 38 | return self::$instance; |
@@ -42,7 +42,7 @@ discard block |
||
42 | 42 | global $pagenow; |
43 | 43 | |
44 | 44 | // Bail if we are not on the plugins page |
45 | - if ( $pagenow != "plugins.php" ) { |
|
45 | + if ($pagenow != "plugins.php") { |
|
46 | 46 | return; |
47 | 47 | } |
48 | 48 | |
@@ -55,19 +55,19 @@ discard block |
||
55 | 55 | * We deliberately don't add textdomains here so that double textdomain warning is not given in theme review. |
56 | 56 | */ |
57 | 57 | wp_localize_script('ayecode-deactivation-survey', 'ayecodeds_deactivate_feedback_form_strings', array( |
58 | - 'quick_feedback' => __( 'Quick Feedback', 'ayecode-connect' ), |
|
59 | - 'foreword' => __( 'If you would be kind enough, please tell us why you\'re deactivating?', 'ayecode-connect' ), |
|
60 | - 'better_plugins_name' => __( 'Please tell us which plugin?', 'ayecode-connect' ), |
|
61 | - 'please_tell_us' => __( 'Please tell us the reason so we can improve the plugin', 'ayecode-connect' ), |
|
62 | - 'do_not_attach_email' => __( 'Do not send my e-mail address with this feedback', 'ayecode-connect' ), |
|
63 | - 'brief_description' => __( 'Please give us any feedback that could help us improve', 'ayecode-connect' ), |
|
64 | - 'cancel' => __( 'Cancel', 'ayecode-connect' ), |
|
65 | - 'skip_and_deactivate' => __( 'Skip & Deactivate', 'ayecode-connect' ), |
|
66 | - 'submit_and_deactivate' => __( 'Submit & Deactivate', 'ayecode-connect' ), |
|
67 | - 'please_wait' => __( 'Please wait', 'ayecode-connect' ), |
|
68 | - 'get_support' => __( 'Get Support', 'ayecode-connect' ), |
|
69 | - 'documentation' => __( 'Documentation', 'ayecode-connect' ), |
|
70 | - 'thank_you' => __( 'Thank you!', 'ayecode-connect' ), |
|
58 | + 'quick_feedback' => __('Quick Feedback', 'ayecode-connect'), |
|
59 | + 'foreword' => __('If you would be kind enough, please tell us why you\'re deactivating?', 'ayecode-connect'), |
|
60 | + 'better_plugins_name' => __('Please tell us which plugin?', 'ayecode-connect'), |
|
61 | + 'please_tell_us' => __('Please tell us the reason so we can improve the plugin', 'ayecode-connect'), |
|
62 | + 'do_not_attach_email' => __('Do not send my e-mail address with this feedback', 'ayecode-connect'), |
|
63 | + 'brief_description' => __('Please give us any feedback that could help us improve', 'ayecode-connect'), |
|
64 | + 'cancel' => __('Cancel', 'ayecode-connect'), |
|
65 | + 'skip_and_deactivate' => __('Skip & Deactivate', 'ayecode-connect'), |
|
66 | + 'submit_and_deactivate' => __('Submit & Deactivate', 'ayecode-connect'), |
|
67 | + 'please_wait' => __('Please wait', 'ayecode-connect'), |
|
68 | + 'get_support' => __('Get Support', 'ayecode-connect'), |
|
69 | + 'documentation' => __('Documentation', 'ayecode-connect'), |
|
70 | + 'thank_you' => __('Thank you!', 'ayecode-connect'), |
|
71 | 71 | )); |
72 | 72 | |
73 | 73 | // Plugins |
@@ -75,18 +75,18 @@ discard block |
||
75 | 75 | |
76 | 76 | // Reasons |
77 | 77 | $defaultReasons = array( |
78 | - 'suddenly-stopped-working' => __( 'The plugin suddenly stopped working', 'ayecode-connect' ), |
|
79 | - 'plugin-broke-site' => __( 'The plugin broke my site', 'ayecode-connect' ), |
|
80 | - 'plugin-setup-difficult' => __( 'Too difficult to setup', 'ayecode-connect' ), |
|
81 | - 'plugin-design-difficult' => __( 'Too difficult to get the design i want', 'ayecode-connect' ), |
|
82 | - 'no-longer-needed' => __( 'I don\'t need this plugin any more', 'ayecode-connect' ), |
|
83 | - 'found-better-plugin' => __( 'I found a better plugin', 'ayecode-connect' ), |
|
84 | - 'temporary-deactivation' => __( 'It\'s a temporary deactivation, I\'m troubleshooting', 'ayecode-connect' ), |
|
85 | - 'other' => __( 'Other', 'ayecode-connect' ), |
|
78 | + 'suddenly-stopped-working' => __('The plugin suddenly stopped working', 'ayecode-connect'), |
|
79 | + 'plugin-broke-site' => __('The plugin broke my site', 'ayecode-connect'), |
|
80 | + 'plugin-setup-difficult' => __('Too difficult to setup', 'ayecode-connect'), |
|
81 | + 'plugin-design-difficult' => __('Too difficult to get the design i want', 'ayecode-connect'), |
|
82 | + 'no-longer-needed' => __('I don\'t need this plugin any more', 'ayecode-connect'), |
|
83 | + 'found-better-plugin' => __('I found a better plugin', 'ayecode-connect'), |
|
84 | + 'temporary-deactivation' => __('It\'s a temporary deactivation, I\'m troubleshooting', 'ayecode-connect'), |
|
85 | + 'other' => __('Other', 'ayecode-connect'), |
|
86 | 86 | ); |
87 | 87 | |
88 | - foreach( $plugins as $plugin ) { |
|
89 | - $plugin->reasons = apply_filters( 'ayecode_deactivation_survey_reasons', $defaultReasons, $plugin ); |
|
88 | + foreach ($plugins as $plugin) { |
|
89 | + $plugin->reasons = apply_filters('ayecode_deactivation_survey_reasons', $defaultReasons, $plugin); |
|
90 | 90 | $plugin->url = home_url(); |
91 | 91 | $plugin->activated = 0; |
92 | 92 | } |
@@ -7,7 +7,7 @@ discard block |
||
7 | 7 | * @since 2.0.0 |
8 | 8 | */ |
9 | 9 | |
10 | -defined( 'ABSPATH' ) || exit; |
|
10 | +defined('ABSPATH') || exit; |
|
11 | 11 | |
12 | 12 | /** |
13 | 13 | * GetPaid REST date based controller class. |
@@ -43,26 +43,26 @@ discard block |
||
43 | 43 | * @param WP_REST_Request $request Request object. |
44 | 44 | * @return array The appropriate date range. |
45 | 45 | */ |
46 | - public function get_date_range( $request ) { |
|
46 | + public function get_date_range($request) { |
|
47 | 47 | |
48 | 48 | // Check if the period is x_days. |
49 | - if ( preg_match( '/^(\d+)_days$/', $request['period'], $matches ) ) { |
|
50 | - $date_range = $this->get_x_days_date_range( absint( $matches[1] ) ); |
|
51 | - } elseif ( is_callable( array( $this, 'get_' . $request['period'] . '_date_range' ) ) ) { |
|
52 | - $date_range = call_user_func( array( $this, 'get_' . $request['period'] . '_date_range' ), $request ); |
|
49 | + if (preg_match('/^(\d+)_days$/', $request['period'], $matches)) { |
|
50 | + $date_range = $this->get_x_days_date_range(absint($matches[1])); |
|
51 | + } elseif (is_callable(array($this, 'get_' . $request['period'] . '_date_range'))) { |
|
52 | + $date_range = call_user_func(array($this, 'get_' . $request['period'] . '_date_range'), $request); |
|
53 | 53 | } else { |
54 | 54 | $request['period'] = '7_days'; |
55 | 55 | $date_range = $this->get_x_days_date_range(); |
56 | 56 | } |
57 | 57 | |
58 | 58 | // 3 months max for day view. |
59 | - $before = strtotime( $date_range['before'] ); |
|
60 | - $after = strtotime( $date_range['after'] ); |
|
61 | - if ( floor( ( $before - $after ) / MONTH_IN_SECONDS ) > 2 ) { |
|
59 | + $before = strtotime($date_range['before']); |
|
60 | + $after = strtotime($date_range['after']); |
|
61 | + if (floor(($before - $after) / MONTH_IN_SECONDS) > 2) { |
|
62 | 62 | $this->groupby = 'month'; |
63 | 63 | } |
64 | 64 | |
65 | - $this->prepare_interval( $date_range ); |
|
65 | + $this->prepare_interval($date_range); |
|
66 | 66 | |
67 | 67 | return $date_range; |
68 | 68 | |
@@ -74,25 +74,25 @@ discard block |
||
74 | 74 | * @param array $range Date range. |
75 | 75 | * @return array The appropriate date range. |
76 | 76 | */ |
77 | - public function prepare_interval( $range ) { |
|
77 | + public function prepare_interval($range) { |
|
78 | 78 | |
79 | - $before = strtotime( $range['before'] ); |
|
80 | - $after = strtotime( $range['after'] ); |
|
81 | - if ( 'day' === $this->groupby ) { |
|
82 | - $difference = max( DAY_IN_SECONDS, ( DAY_IN_SECONDS + $before - $after ) ); // Prevent division by 0; |
|
83 | - $this->interval = absint( ceil( max( 1, $difference / DAY_IN_SECONDS ) ) ); |
|
79 | + $before = strtotime($range['before']); |
|
80 | + $after = strtotime($range['after']); |
|
81 | + if ('day' === $this->groupby) { |
|
82 | + $difference = max(DAY_IN_SECONDS, (DAY_IN_SECONDS + $before - $after)); // Prevent division by 0; |
|
83 | + $this->interval = absint(ceil(max(1, $difference / DAY_IN_SECONDS))); |
|
84 | 84 | return; |
85 | 85 | } |
86 | 86 | |
87 | 87 | $this->interval = 0; |
88 | - $min_date = strtotime( gmdate( 'Y-m-01', $after ) ); |
|
88 | + $min_date = strtotime(gmdate('Y-m-01', $after)); |
|
89 | 89 | |
90 | - while ( $min_date <= $before ) { |
|
91 | - $this->interval ++; |
|
92 | - $min_date = strtotime( '+1 MONTH', $min_date ); |
|
90 | + while ($min_date <= $before) { |
|
91 | + $this->interval++; |
|
92 | + $min_date = strtotime('+1 MONTH', $min_date); |
|
93 | 93 | } |
94 | 94 | |
95 | - $this->interval = max( 1, $this->interval ); |
|
95 | + $this->interval = max(1, $this->interval); |
|
96 | 96 | |
97 | 97 | } |
98 | 98 | |
@@ -102,27 +102,27 @@ discard block |
||
102 | 102 | * @param WP_REST_Request $request Request object. |
103 | 103 | * @return array The appropriate date range. |
104 | 104 | */ |
105 | - public function get_custom_date_range( $request ) { |
|
105 | + public function get_custom_date_range($request) { |
|
106 | 106 | |
107 | - $after = max( strtotime( '-20 years' ), strtotime( sanitize_text_field( $request['after'] ) ) ); |
|
108 | - $before = gmdate( 'Y-m-d' ); |
|
107 | + $after = max(strtotime('-20 years'), strtotime(sanitize_text_field($request['after']))); |
|
108 | + $before = gmdate('Y-m-d'); |
|
109 | 109 | |
110 | - if ( ! empty( $request['before'] ) ) { |
|
111 | - $before = min( strtotime( $before ), strtotime( sanitize_text_field( $request['before'] ) ) ); |
|
110 | + if (!empty($request['before'])) { |
|
111 | + $before = min(strtotime($before), strtotime(sanitize_text_field($request['before']))); |
|
112 | 112 | } |
113 | 113 | |
114 | 114 | // Set the previous date range. |
115 | 115 | $difference = $before - $after; |
116 | 116 | $this->previous_range = array( |
117 | 117 | 'period' => 'custom', |
118 | - 'before' => gmdate( 'Y-m-d', $before - $difference - DAY_IN_SECONDS ), |
|
119 | - 'after' => gmdate( 'Y-m-d', $after - $difference - DAY_IN_SECONDS ), |
|
118 | + 'before' => gmdate('Y-m-d', $before - $difference - DAY_IN_SECONDS), |
|
119 | + 'after' => gmdate('Y-m-d', $after - $difference - DAY_IN_SECONDS), |
|
120 | 120 | ); |
121 | 121 | |
122 | 122 | // Generate the report. |
123 | 123 | return array( |
124 | - 'before' => gmdate( 'Y-m-d', $before ), |
|
125 | - 'after' => gmdate( 'Y-m-d', $after ), |
|
124 | + 'before' => gmdate('Y-m-d', $before), |
|
125 | + 'after' => gmdate('Y-m-d', $after), |
|
126 | 126 | ); |
127 | 127 | |
128 | 128 | } |
@@ -141,8 +141,8 @@ discard block |
||
141 | 141 | |
142 | 142 | // Generate the report. |
143 | 143 | return array( |
144 | - 'before' => gmdate( 'Y-m-d' ), |
|
145 | - 'after' => gmdate( 'Y-m-d' ), |
|
144 | + 'before' => gmdate('Y-m-d'), |
|
145 | + 'after' => gmdate('Y-m-d'), |
|
146 | 146 | ); |
147 | 147 | |
148 | 148 | } |
@@ -157,14 +157,14 @@ discard block |
||
157 | 157 | // Set the previous date range. |
158 | 158 | $this->previous_range = array( |
159 | 159 | 'period' => 'custom', |
160 | - 'before' => gmdate( 'Y-m-d', strtotime( '-2 days' ) ), |
|
161 | - 'after' => gmdate( 'Y-m-d', strtotime( '-2 days' ) ), |
|
160 | + 'before' => gmdate('Y-m-d', strtotime('-2 days')), |
|
161 | + 'after' => gmdate('Y-m-d', strtotime('-2 days')), |
|
162 | 162 | ); |
163 | 163 | |
164 | 164 | // Generate the report. |
165 | 165 | return array( |
166 | - 'before' => gmdate( 'Y-m-d', strtotime( '-1 day' ) ), |
|
167 | - 'after' => gmdate( 'Y-m-d', strtotime( '-1 day' ) ), |
|
166 | + 'before' => gmdate('Y-m-d', strtotime('-1 day')), |
|
167 | + 'after' => gmdate('Y-m-d', strtotime('-1 day')), |
|
168 | 168 | ); |
169 | 169 | |
170 | 170 | } |
@@ -182,10 +182,10 @@ discard block |
||
182 | 182 | ); |
183 | 183 | |
184 | 184 | // Generate the report. |
185 | - $week_starts = absint( get_option( 'start_of_week' ) ); |
|
185 | + $week_starts = absint(get_option('start_of_week')); |
|
186 | 186 | return array( |
187 | - 'before' => gmdate( 'Y-m-d' ), |
|
188 | - 'after' => gmdate( 'Y-m-d', strtotime( 'next Sunday -' . ( 7 - $week_starts ) . ' days' ) ), |
|
187 | + 'before' => gmdate('Y-m-d'), |
|
188 | + 'after' => gmdate('Y-m-d', strtotime('next Sunday -' . (7 - $week_starts) . ' days')), |
|
189 | 189 | ); |
190 | 190 | } |
191 | 191 | |
@@ -196,19 +196,19 @@ discard block |
||
196 | 196 | */ |
197 | 197 | public function get_last_week_date_range() { |
198 | 198 | |
199 | - $week_starts = absint( get_option( 'start_of_week' ) ); |
|
200 | - $week_starts = strtotime( 'last Sunday -' . ( 7 - $week_starts ) . ' days' ); |
|
199 | + $week_starts = absint(get_option('start_of_week')); |
|
200 | + $week_starts = strtotime('last Sunday -' . (7 - $week_starts) . ' days'); |
|
201 | 201 | $date_range = array( |
202 | - 'before' => gmdate( 'Y-m-d', $week_starts + 6 * DAY_IN_SECONDS ), |
|
203 | - 'after' => gmdate( 'Y-m-d', $week_starts ), |
|
202 | + 'before' => gmdate('Y-m-d', $week_starts + 6 * DAY_IN_SECONDS), |
|
203 | + 'after' => gmdate('Y-m-d', $week_starts), |
|
204 | 204 | ); |
205 | 205 | |
206 | 206 | // Set the previous date range. |
207 | 207 | $week_starts = $week_starts - 7 * DAY_IN_SECONDS; |
208 | 208 | $this->previous_range = array( |
209 | 209 | 'period' => 'custom', |
210 | - 'before' => gmdate( 'Y-m-d', $week_starts + 6 * DAY_IN_SECONDS ), |
|
211 | - 'after' => gmdate( 'Y-m-d', $week_starts ), |
|
210 | + 'before' => gmdate('Y-m-d', $week_starts + 6 * DAY_IN_SECONDS), |
|
211 | + 'after' => gmdate('Y-m-d', $week_starts), |
|
212 | 212 | ); |
213 | 213 | |
214 | 214 | // Generate the report. |
@@ -220,13 +220,13 @@ discard block |
||
220 | 220 | * |
221 | 221 | * @return array The appropriate date range. |
222 | 222 | */ |
223 | - public function get_x_days_date_range( $days = 7 ) { |
|
223 | + public function get_x_days_date_range($days = 7) { |
|
224 | 224 | |
225 | 225 | $days--; |
226 | 226 | |
227 | - $date_range = array( |
|
228 | - 'before' => gmdate( 'Y-m-d' ), |
|
229 | - 'after' => gmdate( 'Y-m-d', strtotime( "-$days days" ) ), |
|
227 | + $date_range = array( |
|
228 | + 'before' => gmdate('Y-m-d'), |
|
229 | + 'after' => gmdate('Y-m-d', strtotime("-$days days")), |
|
230 | 230 | ); |
231 | 231 | |
232 | 232 | $days++; |
@@ -234,8 +234,8 @@ discard block |
||
234 | 234 | // Set the previous date range. |
235 | 235 | $this->previous_range = array( |
236 | 236 | 'period' => 'custom', |
237 | - 'before' => gmdate( 'Y-m-d', strtotime( $date_range['before'] ) - $days * DAY_IN_SECONDS ), |
|
238 | - 'after' => gmdate( 'Y-m-d', strtotime( $date_range['after'] ) - $days * DAY_IN_SECONDS ), |
|
237 | + 'before' => gmdate('Y-m-d', strtotime($date_range['before']) - $days * DAY_IN_SECONDS), |
|
238 | + 'after' => gmdate('Y-m-d', strtotime($date_range['after']) - $days * DAY_IN_SECONDS), |
|
239 | 239 | ); |
240 | 240 | |
241 | 241 | // Generate the report. |
@@ -256,8 +256,8 @@ discard block |
||
256 | 256 | |
257 | 257 | // Generate the report. |
258 | 258 | return array( |
259 | - 'after' => gmdate( 'Y-m-01' ), |
|
260 | - 'before' => gmdate( 'Y-m-t' ), |
|
259 | + 'after' => gmdate('Y-m-01'), |
|
260 | + 'before' => gmdate('Y-m-t'), |
|
261 | 261 | ); |
262 | 262 | |
263 | 263 | } |
@@ -272,14 +272,14 @@ discard block |
||
272 | 272 | // Set the previous date range. |
273 | 273 | $this->previous_range = array( |
274 | 274 | 'period' => 'custom', |
275 | - 'after' => gmdate( 'Y-m-01', strtotime( '-2 months' ) ), |
|
276 | - 'before' => gmdate( 'Y-m-t', strtotime( '-2 months' ) ), |
|
275 | + 'after' => gmdate('Y-m-01', strtotime('-2 months')), |
|
276 | + 'before' => gmdate('Y-m-t', strtotime('-2 months')), |
|
277 | 277 | ); |
278 | 278 | |
279 | 279 | // Generate the report. |
280 | 280 | return array( |
281 | - 'after' => gmdate( 'Y-m-01', strtotime( 'last month' ) ), |
|
282 | - 'before' => gmdate( 'Y-m-t', strtotime( 'last month' ) ), |
|
281 | + 'after' => gmdate('Y-m-01', strtotime('last month')), |
|
282 | + 'before' => gmdate('Y-m-t', strtotime('last month')), |
|
283 | 283 | ); |
284 | 284 | |
285 | 285 | } |
@@ -291,7 +291,7 @@ discard block |
||
291 | 291 | */ |
292 | 292 | public function get_quarters() { |
293 | 293 | |
294 | - $year = (int) gmdate( 'Y' ); |
|
294 | + $year = (int) gmdate('Y'); |
|
295 | 295 | $last_year = (int) $year - 1; |
296 | 296 | return array( |
297 | 297 | |
@@ -340,9 +340,9 @@ discard block |
||
340 | 340 | */ |
341 | 341 | public function get_quarter() { |
342 | 342 | |
343 | - $month = (int) gmdate( 'n' ); |
|
344 | - $quarters = array( 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4 ); |
|
345 | - return $quarters[ $month - 1 ]; |
|
343 | + $month = (int) gmdate('n'); |
|
344 | + $quarters = array(1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4); |
|
345 | + return $quarters[$month - 1]; |
|
346 | 346 | |
347 | 347 | } |
348 | 348 | |
@@ -361,7 +361,7 @@ discard block |
||
361 | 361 | // Generate the report. |
362 | 362 | $quarter = $this->get_quarter(); |
363 | 363 | $quarters = $this->get_quarters(); |
364 | - return $quarters[ $quarter + 1 ]; |
|
364 | + return $quarters[$quarter + 1]; |
|
365 | 365 | |
366 | 366 | } |
367 | 367 | |
@@ -377,12 +377,12 @@ discard block |
||
377 | 377 | |
378 | 378 | // Set the previous date range. |
379 | 379 | $this->previous_range = array_merge( |
380 | - $quarters[ $quarter - 1 ], |
|
381 | - array( 'period' => 'custom' ) |
|
380 | + $quarters[$quarter - 1], |
|
381 | + array('period' => 'custom') |
|
382 | 382 | ); |
383 | 383 | |
384 | 384 | // Generate the report. |
385 | - return $quarters[ $quarter ]; |
|
385 | + return $quarters[$quarter]; |
|
386 | 386 | |
387 | 387 | } |
388 | 388 | |
@@ -400,8 +400,8 @@ discard block |
||
400 | 400 | |
401 | 401 | // Generate the report. |
402 | 402 | return array( |
403 | - 'after' => gmdate( 'Y-01-01' ), |
|
404 | - 'before' => gmdate( 'Y-12-31' ), |
|
403 | + 'after' => gmdate('Y-01-01'), |
|
404 | + 'before' => gmdate('Y-12-31'), |
|
405 | 405 | ); |
406 | 406 | |
407 | 407 | } |
@@ -416,14 +416,14 @@ discard block |
||
416 | 416 | // Set the previous date range. |
417 | 417 | $this->previous_range = array( |
418 | 418 | 'period' => 'custom', |
419 | - 'after' => gmdate( 'Y-01-01', strtotime( '-2 years' ) ), |
|
420 | - 'before' => gmdate( 'Y-12-31', strtotime( '-2 years' ) ), |
|
419 | + 'after' => gmdate('Y-01-01', strtotime('-2 years')), |
|
420 | + 'before' => gmdate('Y-12-31', strtotime('-2 years')), |
|
421 | 421 | ); |
422 | 422 | |
423 | 423 | // Generate the report. |
424 | 424 | return array( |
425 | - 'after' => gmdate( 'Y-01-01', strtotime( 'last year' ) ), |
|
426 | - 'before' => gmdate( 'Y-12-31', strtotime( 'last year' ) ), |
|
425 | + 'after' => gmdate('Y-01-01', strtotime('last year')), |
|
426 | + 'before' => gmdate('Y-12-31', strtotime('last year')), |
|
427 | 427 | ); |
428 | 428 | |
429 | 429 | } |
@@ -435,20 +435,20 @@ discard block |
||
435 | 435 | * @param string $date_field The date field. |
436 | 436 | * @return string The appropriate SQL. |
437 | 437 | */ |
438 | - public function get_date_range_sql( $request, $date_field ) { |
|
438 | + public function get_date_range_sql($request, $date_field) { |
|
439 | 439 | global $wpdb; |
440 | 440 | |
441 | 441 | $sql = '1=1'; |
442 | - $range = $this->get_date_range( $request ); |
|
442 | + $range = $this->get_date_range($request); |
|
443 | 443 | |
444 | - if ( ! empty( $range['after'] ) ) { |
|
444 | + if (!empty($range['after'])) { |
|
445 | 445 | $sql .= ' AND ' . $wpdb->prepare( |
446 | 446 | "$date_field >= %s", |
447 | 447 | $range['after'] |
448 | 448 | ); |
449 | 449 | } |
450 | 450 | |
451 | - if ( ! empty( $range['before'] ) ) { |
|
451 | + if (!empty($range['before'])) { |
|
452 | 452 | $sql .= ' AND ' . $wpdb->prepare( |
453 | 453 | "$date_field <= %s", |
454 | 454 | $range['before'] |
@@ -465,9 +465,9 @@ discard block |
||
465 | 465 | * @param string $date_field The date field. |
466 | 466 | * @return string The appropriate SQL. |
467 | 467 | */ |
468 | - public function get_group_by_sql( $date_field ) { |
|
468 | + public function get_group_by_sql($date_field) { |
|
469 | 469 | |
470 | - if ( 'day' === $this->groupby ) { |
|
470 | + if ('day' === $this->groupby) { |
|
471 | 471 | return "YEAR($date_field), MONTH($date_field), DAY($date_field)"; |
472 | 472 | } |
473 | 473 | |
@@ -481,30 +481,30 @@ discard block |
||
481 | 481 | */ |
482 | 482 | public function get_collection_params() { |
483 | 483 | return array( |
484 | - 'context' => $this->get_context_param( array( 'default' => 'view' ) ), |
|
484 | + 'context' => $this->get_context_param(array('default' => 'view')), |
|
485 | 485 | 'period' => array( |
486 | - 'description' => __( 'Limit to results of a specific period.', 'invoicing' ), |
|
486 | + 'description' => __('Limit to results of a specific period.', 'invoicing'), |
|
487 | 487 | 'type' => 'string', |
488 | - 'enum' => array( 'custom', 'today', 'yesterday', 'week', 'last_week', '7_days', '30_days', '60_days', '90_days', '180_days', 'month', 'last_month', 'quarter', 'last_quarter', 'year', 'last_year', 'quarter', 'last_quarter' ), |
|
488 | + 'enum' => array('custom', 'today', 'yesterday', 'week', 'last_week', '7_days', '30_days', '60_days', '90_days', '180_days', 'month', 'last_month', 'quarter', 'last_quarter', 'year', 'last_year', 'quarter', 'last_quarter'), |
|
489 | 489 | 'validate_callback' => 'rest_validate_request_arg', |
490 | 490 | 'sanitize_callback' => 'sanitize_text_field', |
491 | 491 | 'default' => '7_days', |
492 | 492 | ), |
493 | 493 | 'after' => array( |
494 | 494 | /* translators: %s: date format */ |
495 | - 'description' => sprintf( __( 'Limit to results after a specific date, the date needs to be in the %s format.', 'invoicing' ), 'YYYY-MM-DD' ), |
|
495 | + 'description' => sprintf(__('Limit to results after a specific date, the date needs to be in the %s format.', 'invoicing'), 'YYYY-MM-DD'), |
|
496 | 496 | 'type' => 'string', |
497 | 497 | 'validate_callback' => 'rest_validate_request_arg', |
498 | 498 | 'sanitize_callback' => 'sanitize_text_field', |
499 | - 'default' => gmdate( 'Y-m-d', strtotime( '-7 days' ) ), |
|
499 | + 'default' => gmdate('Y-m-d', strtotime('-7 days')), |
|
500 | 500 | ), |
501 | 501 | 'before' => array( |
502 | 502 | /* translators: %s: date format */ |
503 | - 'description' => sprintf( __( 'Limit to results before a specific date, the date needs to be in the %s format.', 'invoicing' ), 'YYYY-MM-DD' ), |
|
503 | + 'description' => sprintf(__('Limit to results before a specific date, the date needs to be in the %s format.', 'invoicing'), 'YYYY-MM-DD'), |
|
504 | 504 | 'type' => 'string', |
505 | 505 | 'validate_callback' => 'rest_validate_request_arg', |
506 | 506 | 'sanitize_callback' => 'sanitize_text_field', |
507 | - 'default' => gmdate( 'Y-m-d' ), |
|
507 | + 'default' => gmdate('Y-m-d'), |
|
508 | 508 | ), |
509 | 509 | ); |
510 | 510 | } |
@@ -16,496 +16,496 @@ |
||
16 | 16 | */ |
17 | 17 | class GetPaid_REST_Date_Based_Controller extends GetPaid_REST_Controller { |
18 | 18 | |
19 | - /** |
|
20 | - * Group response items by day or month. |
|
21 | - * |
|
22 | - * @var string |
|
23 | - */ |
|
24 | - public $groupby = 'day'; |
|
25 | - |
|
26 | - /** |
|
27 | - * Returns an array with arguments to request the previous report. |
|
28 | - * |
|
29 | - * @var array |
|
30 | - */ |
|
31 | - public $previous_range = array(); |
|
32 | - |
|
33 | - /** |
|
34 | - * The period interval. |
|
35 | - * |
|
36 | - * @var int |
|
37 | - */ |
|
38 | - public $interval; |
|
39 | - |
|
40 | - /** |
|
41 | - * Retrieves the before and after dates. |
|
42 | - * |
|
43 | - * @param WP_REST_Request $request Request object. |
|
44 | - * @return array The appropriate date range. |
|
45 | - */ |
|
46 | - public function get_date_range( $request ) { |
|
47 | - |
|
48 | - // Check if the period is x_days. |
|
49 | - if ( preg_match( '/^(\d+)_days$/', $request['period'], $matches ) ) { |
|
50 | - $date_range = $this->get_x_days_date_range( absint( $matches[1] ) ); |
|
51 | - } elseif ( is_callable( array( $this, 'get_' . $request['period'] . '_date_range' ) ) ) { |
|
52 | - $date_range = call_user_func( array( $this, 'get_' . $request['period'] . '_date_range' ), $request ); |
|
53 | - } else { |
|
54 | - $request['period'] = '7_days'; |
|
55 | - $date_range = $this->get_x_days_date_range(); |
|
56 | - } |
|
57 | - |
|
58 | - // 3 months max for day view. |
|
59 | - $before = strtotime( $date_range['before'] ); |
|
60 | - $after = strtotime( $date_range['after'] ); |
|
61 | - if ( floor( ( $before - $after ) / MONTH_IN_SECONDS ) > 2 ) { |
|
62 | - $this->groupby = 'month'; |
|
63 | - } |
|
64 | - |
|
65 | - $this->prepare_interval( $date_range ); |
|
66 | - |
|
67 | - return $date_range; |
|
68 | - |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * Groups by month or days. |
|
73 | - * |
|
74 | - * @param array $range Date range. |
|
75 | - * @return array The appropriate date range. |
|
76 | - */ |
|
77 | - public function prepare_interval( $range ) { |
|
78 | - |
|
79 | - $before = strtotime( $range['before'] ); |
|
80 | - $after = strtotime( $range['after'] ); |
|
81 | - if ( 'day' === $this->groupby ) { |
|
82 | - $difference = max( DAY_IN_SECONDS, ( DAY_IN_SECONDS + $before - $after ) ); // Prevent division by 0; |
|
83 | - $this->interval = absint( ceil( max( 1, $difference / DAY_IN_SECONDS ) ) ); |
|
84 | - return; |
|
85 | - } |
|
86 | - |
|
87 | - $this->interval = 0; |
|
88 | - $min_date = strtotime( gmdate( 'Y-m-01', $after ) ); |
|
89 | - |
|
90 | - while ( $min_date <= $before ) { |
|
91 | - $this->interval ++; |
|
92 | - $min_date = strtotime( '+1 MONTH', $min_date ); |
|
93 | - } |
|
94 | - |
|
95 | - $this->interval = max( 1, $this->interval ); |
|
96 | - |
|
97 | - } |
|
98 | - |
|
99 | - /** |
|
100 | - * Retrieves a custom date range. |
|
101 | - * |
|
102 | - * @param WP_REST_Request $request Request object. |
|
103 | - * @return array The appropriate date range. |
|
104 | - */ |
|
105 | - public function get_custom_date_range( $request ) { |
|
106 | - |
|
107 | - $after = max( strtotime( '-20 years' ), strtotime( sanitize_text_field( $request['after'] ) ) ); |
|
108 | - $before = gmdate( 'Y-m-d' ); |
|
19 | + /** |
|
20 | + * Group response items by day or month. |
|
21 | + * |
|
22 | + * @var string |
|
23 | + */ |
|
24 | + public $groupby = 'day'; |
|
25 | + |
|
26 | + /** |
|
27 | + * Returns an array with arguments to request the previous report. |
|
28 | + * |
|
29 | + * @var array |
|
30 | + */ |
|
31 | + public $previous_range = array(); |
|
32 | + |
|
33 | + /** |
|
34 | + * The period interval. |
|
35 | + * |
|
36 | + * @var int |
|
37 | + */ |
|
38 | + public $interval; |
|
39 | + |
|
40 | + /** |
|
41 | + * Retrieves the before and after dates. |
|
42 | + * |
|
43 | + * @param WP_REST_Request $request Request object. |
|
44 | + * @return array The appropriate date range. |
|
45 | + */ |
|
46 | + public function get_date_range( $request ) { |
|
47 | + |
|
48 | + // Check if the period is x_days. |
|
49 | + if ( preg_match( '/^(\d+)_days$/', $request['period'], $matches ) ) { |
|
50 | + $date_range = $this->get_x_days_date_range( absint( $matches[1] ) ); |
|
51 | + } elseif ( is_callable( array( $this, 'get_' . $request['period'] . '_date_range' ) ) ) { |
|
52 | + $date_range = call_user_func( array( $this, 'get_' . $request['period'] . '_date_range' ), $request ); |
|
53 | + } else { |
|
54 | + $request['period'] = '7_days'; |
|
55 | + $date_range = $this->get_x_days_date_range(); |
|
56 | + } |
|
57 | + |
|
58 | + // 3 months max for day view. |
|
59 | + $before = strtotime( $date_range['before'] ); |
|
60 | + $after = strtotime( $date_range['after'] ); |
|
61 | + if ( floor( ( $before - $after ) / MONTH_IN_SECONDS ) > 2 ) { |
|
62 | + $this->groupby = 'month'; |
|
63 | + } |
|
64 | + |
|
65 | + $this->prepare_interval( $date_range ); |
|
66 | + |
|
67 | + return $date_range; |
|
68 | + |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * Groups by month or days. |
|
73 | + * |
|
74 | + * @param array $range Date range. |
|
75 | + * @return array The appropriate date range. |
|
76 | + */ |
|
77 | + public function prepare_interval( $range ) { |
|
78 | + |
|
79 | + $before = strtotime( $range['before'] ); |
|
80 | + $after = strtotime( $range['after'] ); |
|
81 | + if ( 'day' === $this->groupby ) { |
|
82 | + $difference = max( DAY_IN_SECONDS, ( DAY_IN_SECONDS + $before - $after ) ); // Prevent division by 0; |
|
83 | + $this->interval = absint( ceil( max( 1, $difference / DAY_IN_SECONDS ) ) ); |
|
84 | + return; |
|
85 | + } |
|
86 | + |
|
87 | + $this->interval = 0; |
|
88 | + $min_date = strtotime( gmdate( 'Y-m-01', $after ) ); |
|
89 | + |
|
90 | + while ( $min_date <= $before ) { |
|
91 | + $this->interval ++; |
|
92 | + $min_date = strtotime( '+1 MONTH', $min_date ); |
|
93 | + } |
|
94 | + |
|
95 | + $this->interval = max( 1, $this->interval ); |
|
96 | + |
|
97 | + } |
|
98 | + |
|
99 | + /** |
|
100 | + * Retrieves a custom date range. |
|
101 | + * |
|
102 | + * @param WP_REST_Request $request Request object. |
|
103 | + * @return array The appropriate date range. |
|
104 | + */ |
|
105 | + public function get_custom_date_range( $request ) { |
|
106 | + |
|
107 | + $after = max( strtotime( '-20 years' ), strtotime( sanitize_text_field( $request['after'] ) ) ); |
|
108 | + $before = gmdate( 'Y-m-d' ); |
|
109 | 109 | |
110 | - if ( ! empty( $request['before'] ) ) { |
|
111 | - $before = min( strtotime( $before ), strtotime( sanitize_text_field( $request['before'] ) ) ); |
|
112 | - } |
|
113 | - |
|
114 | - // Set the previous date range. |
|
115 | - $difference = $before - $after; |
|
116 | - $this->previous_range = array( |
|
117 | - 'period' => 'custom', |
|
118 | - 'before' => gmdate( 'Y-m-d', $before - $difference - DAY_IN_SECONDS ), |
|
119 | - 'after' => gmdate( 'Y-m-d', $after - $difference - DAY_IN_SECONDS ), |
|
120 | - ); |
|
121 | - |
|
122 | - // Generate the report. |
|
123 | - return array( |
|
124 | - 'before' => gmdate( 'Y-m-d', $before ), |
|
125 | - 'after' => gmdate( 'Y-m-d', $after ), |
|
126 | - ); |
|
127 | - |
|
128 | - } |
|
129 | - |
|
130 | - /** |
|
131 | - * Retrieves todays date range. |
|
132 | - * |
|
133 | - * @return array The appropriate date range. |
|
134 | - */ |
|
135 | - public function get_today_date_range() { |
|
136 | - |
|
137 | - // Set the previous date range. |
|
138 | - $this->previous_range = array( |
|
139 | - 'period' => 'yesterday', |
|
140 | - ); |
|
141 | - |
|
142 | - // Generate the report. |
|
143 | - return array( |
|
144 | - 'before' => gmdate( 'Y-m-d' ), |
|
145 | - 'after' => gmdate( 'Y-m-d' ), |
|
146 | - ); |
|
147 | - |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * Retrieves yesterdays date range. |
|
152 | - * |
|
153 | - * @return array The appropriate date range. |
|
154 | - */ |
|
155 | - public function get_yesterday_date_range() { |
|
156 | - |
|
157 | - // Set the previous date range. |
|
158 | - $this->previous_range = array( |
|
159 | - 'period' => 'custom', |
|
160 | - 'before' => gmdate( 'Y-m-d', strtotime( '-2 days' ) ), |
|
161 | - 'after' => gmdate( 'Y-m-d', strtotime( '-2 days' ) ), |
|
162 | - ); |
|
163 | - |
|
164 | - // Generate the report. |
|
165 | - return array( |
|
166 | - 'before' => gmdate( 'Y-m-d', strtotime( '-1 day' ) ), |
|
167 | - 'after' => gmdate( 'Y-m-d', strtotime( '-1 day' ) ), |
|
168 | - ); |
|
169 | - |
|
170 | - } |
|
171 | - |
|
172 | - /** |
|
173 | - * Retrieves this week's date range. |
|
174 | - * |
|
175 | - * @return array The appropriate date range. |
|
176 | - */ |
|
177 | - public function get_week_date_range() { |
|
178 | - |
|
179 | - // Set the previous date range. |
|
180 | - $this->previous_range = array( |
|
181 | - 'period' => 'last_week', |
|
182 | - ); |
|
183 | - |
|
184 | - // Generate the report. |
|
185 | - $week_starts = absint( get_option( 'start_of_week' ) ); |
|
186 | - return array( |
|
187 | - 'before' => gmdate( 'Y-m-d' ), |
|
188 | - 'after' => gmdate( 'Y-m-d', strtotime( 'next Sunday -' . ( 7 - $week_starts ) . ' days' ) ), |
|
189 | - ); |
|
190 | - } |
|
191 | - |
|
192 | - /** |
|
193 | - * Retrieves last week's date range. |
|
194 | - * |
|
195 | - * @return array The appropriate date range. |
|
196 | - */ |
|
197 | - public function get_last_week_date_range() { |
|
198 | - |
|
199 | - $week_starts = absint( get_option( 'start_of_week' ) ); |
|
200 | - $week_starts = strtotime( 'last Sunday -' . ( 7 - $week_starts ) . ' days' ); |
|
201 | - $date_range = array( |
|
202 | - 'before' => gmdate( 'Y-m-d', $week_starts + 6 * DAY_IN_SECONDS ), |
|
203 | - 'after' => gmdate( 'Y-m-d', $week_starts ), |
|
204 | - ); |
|
205 | - |
|
206 | - // Set the previous date range. |
|
207 | - $week_starts = $week_starts - 7 * DAY_IN_SECONDS; |
|
208 | - $this->previous_range = array( |
|
209 | - 'period' => 'custom', |
|
210 | - 'before' => gmdate( 'Y-m-d', $week_starts + 6 * DAY_IN_SECONDS ), |
|
211 | - 'after' => gmdate( 'Y-m-d', $week_starts ), |
|
212 | - ); |
|
213 | - |
|
214 | - // Generate the report. |
|
215 | - return $date_range; |
|
216 | - } |
|
217 | - |
|
218 | - /** |
|
219 | - * Retrieves last x days date range. |
|
220 | - * |
|
221 | - * @return array The appropriate date range. |
|
222 | - */ |
|
223 | - public function get_x_days_date_range( $days = 7 ) { |
|
224 | - |
|
225 | - $days--; |
|
226 | - |
|
227 | - $date_range = array( |
|
228 | - 'before' => gmdate( 'Y-m-d' ), |
|
229 | - 'after' => gmdate( 'Y-m-d', strtotime( "-$days days" ) ), |
|
230 | - ); |
|
231 | - |
|
232 | - $days++; |
|
233 | - |
|
234 | - // Set the previous date range. |
|
235 | - $this->previous_range = array( |
|
236 | - 'period' => 'custom', |
|
237 | - 'before' => gmdate( 'Y-m-d', strtotime( $date_range['before'] ) - $days * DAY_IN_SECONDS ), |
|
238 | - 'after' => gmdate( 'Y-m-d', strtotime( $date_range['after'] ) - $days * DAY_IN_SECONDS ), |
|
239 | - ); |
|
240 | - |
|
241 | - // Generate the report. |
|
242 | - return $date_range; |
|
243 | - } |
|
244 | - |
|
245 | - /** |
|
246 | - * Retrieves this month date range. |
|
247 | - * |
|
248 | - * @return array The appropriate date range. |
|
249 | - */ |
|
250 | - public function get_month_date_range() { |
|
251 | - |
|
252 | - // Set the previous date range. |
|
253 | - $this->previous_range = array( |
|
254 | - 'period' => 'last_month', |
|
255 | - ); |
|
256 | - |
|
257 | - // Generate the report. |
|
258 | - return array( |
|
259 | - 'after' => gmdate( 'Y-m-01' ), |
|
260 | - 'before' => gmdate( 'Y-m-t' ), |
|
261 | - ); |
|
262 | - |
|
263 | - } |
|
264 | - |
|
265 | - /** |
|
266 | - * Retrieves last month's date range. |
|
267 | - * |
|
268 | - * @return array The appropriate date range. |
|
269 | - */ |
|
270 | - public function get_last_month_date_range() { |
|
271 | - |
|
272 | - // Set the previous date range. |
|
273 | - $this->previous_range = array( |
|
274 | - 'period' => 'custom', |
|
275 | - 'after' => gmdate( 'Y-m-01', strtotime( '-2 months' ) ), |
|
276 | - 'before' => gmdate( 'Y-m-t', strtotime( '-2 months' ) ), |
|
277 | - ); |
|
278 | - |
|
279 | - // Generate the report. |
|
280 | - return array( |
|
281 | - 'after' => gmdate( 'Y-m-01', strtotime( 'last month' ) ), |
|
282 | - 'before' => gmdate( 'Y-m-t', strtotime( 'last month' ) ), |
|
283 | - ); |
|
284 | - |
|
285 | - } |
|
286 | - |
|
287 | - /** |
|
288 | - * Retrieves this quarter date range. |
|
289 | - * |
|
290 | - * @return array The available quarters. |
|
291 | - */ |
|
292 | - public function get_quarters() { |
|
293 | - |
|
294 | - $year = (int) gmdate( 'Y' ); |
|
295 | - $last_year = (int) $year - 1; |
|
296 | - return array( |
|
297 | - |
|
298 | - // Third quarter of previous year: July 1st to September 30th |
|
299 | - array( |
|
300 | - 'before' => "{$last_year}-09-30", |
|
301 | - 'after' => "{$last_year}-07-01", |
|
302 | - ), |
|
303 | - |
|
304 | - // Last quarter of previous year: October 1st to December 31st |
|
305 | - array( |
|
306 | - 'before' => "{$last_year}-12-31", |
|
307 | - 'after' => "{$last_year}-10-01", |
|
308 | - ), |
|
309 | - |
|
310 | - // First quarter: January 1st to March 31st |
|
311 | - array( |
|
312 | - 'before' => "{$year}-03-31", |
|
313 | - 'after' => "{$year}-01-01", |
|
314 | - ), |
|
315 | - |
|
316 | - // Second quarter: April 1st to June 30th |
|
317 | - array( |
|
318 | - 'before' => "{$year}-06-30", |
|
319 | - 'after' => "{$year}-04-01", |
|
320 | - ), |
|
321 | - |
|
322 | - // Third quarter: July 1st to September 30th |
|
323 | - array( |
|
324 | - 'before' => "{$year}-09-30", |
|
325 | - 'after' => "{$year}-07-01", |
|
326 | - ), |
|
327 | - |
|
328 | - // Fourth quarter: October 1st to December 31st |
|
329 | - array( |
|
330 | - 'before' => "{$year}-12-31", |
|
331 | - 'after' => "{$year}-10-01", |
|
332 | - ), |
|
333 | - ); |
|
334 | - } |
|
335 | - |
|
336 | - /** |
|
337 | - * Retrieves the current quater. |
|
338 | - * |
|
339 | - * @return int The current quarter. |
|
340 | - */ |
|
341 | - public function get_quarter() { |
|
342 | - |
|
343 | - $month = (int) gmdate( 'n' ); |
|
344 | - $quarters = array( 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4 ); |
|
345 | - return $quarters[ $month - 1 ]; |
|
346 | - |
|
347 | - } |
|
348 | - |
|
349 | - /** |
|
350 | - * Retrieves this quarter date range. |
|
351 | - * |
|
352 | - * @return array The appropriate date range. |
|
353 | - */ |
|
354 | - public function get_quarter_date_range() { |
|
355 | - |
|
356 | - // Set the previous date range. |
|
357 | - $this->previous_range = array( |
|
358 | - 'period' => 'last_quarter', |
|
359 | - ); |
|
360 | - |
|
361 | - // Generate the report. |
|
362 | - $quarter = $this->get_quarter(); |
|
363 | - $quarters = $this->get_quarters(); |
|
364 | - return $quarters[ $quarter + 1 ]; |
|
365 | - |
|
366 | - } |
|
367 | - |
|
368 | - /** |
|
369 | - * Retrieves last quarter's date range. |
|
370 | - * |
|
371 | - * @return array The appropriate date range. |
|
372 | - */ |
|
373 | - public function get_last_quarter_date_range() { |
|
374 | - |
|
375 | - $quarters = $this->get_quarters(); |
|
376 | - $quarter = $this->get_quarter(); |
|
377 | - |
|
378 | - // Set the previous date range. |
|
379 | - $this->previous_range = array_merge( |
|
380 | - $quarters[ $quarter - 1 ], |
|
381 | - array( 'period' => 'custom' ) |
|
382 | - ); |
|
383 | - |
|
384 | - // Generate the report. |
|
385 | - return $quarters[ $quarter ]; |
|
386 | - |
|
387 | - } |
|
388 | - |
|
389 | - /** |
|
390 | - * Retrieves this year date range. |
|
391 | - * |
|
392 | - * @return array The appropriate date range. |
|
393 | - */ |
|
394 | - public function get_year_date_range() { |
|
395 | - |
|
396 | - // Set the previous date range. |
|
397 | - $this->previous_range = array( |
|
398 | - 'period' => 'last_year', |
|
399 | - ); |
|
400 | - |
|
401 | - // Generate the report. |
|
402 | - return array( |
|
403 | - 'after' => gmdate( 'Y-01-01' ), |
|
404 | - 'before' => gmdate( 'Y-12-31' ), |
|
405 | - ); |
|
406 | - |
|
407 | - } |
|
408 | - |
|
409 | - /** |
|
410 | - * Retrieves last year date range. |
|
411 | - * |
|
412 | - * @return array The appropriate date range. |
|
413 | - */ |
|
414 | - public function get_last_year_date_range() { |
|
415 | - |
|
416 | - // Set the previous date range. |
|
417 | - $this->previous_range = array( |
|
418 | - 'period' => 'custom', |
|
419 | - 'after' => gmdate( 'Y-01-01', strtotime( '-2 years' ) ), |
|
420 | - 'before' => gmdate( 'Y-12-31', strtotime( '-2 years' ) ), |
|
421 | - ); |
|
422 | - |
|
423 | - // Generate the report. |
|
424 | - return array( |
|
425 | - 'after' => gmdate( 'Y-01-01', strtotime( 'last year' ) ), |
|
426 | - 'before' => gmdate( 'Y-12-31', strtotime( 'last year' ) ), |
|
427 | - ); |
|
428 | - |
|
429 | - } |
|
430 | - |
|
431 | - /** |
|
432 | - * Prepare a the request date for SQL usage. |
|
433 | - * |
|
434 | - * @param WP_REST_Request $request Request object. |
|
435 | - * @param string $date_field The date field. |
|
436 | - * @return string The appropriate SQL. |
|
437 | - */ |
|
438 | - public function get_date_range_sql( $request, $date_field ) { |
|
439 | - global $wpdb; |
|
440 | - |
|
441 | - $sql = '1=1'; |
|
442 | - $range = $this->get_date_range( $request ); |
|
443 | - |
|
444 | - if ( ! empty( $range['after'] ) ) { |
|
445 | - $sql .= ' AND ' . $wpdb->prepare( |
|
446 | - "$date_field >= %s", |
|
447 | - $range['after'] |
|
448 | - ); |
|
449 | - } |
|
450 | - |
|
451 | - if ( ! empty( $range['before'] ) ) { |
|
452 | - $sql .= ' AND ' . $wpdb->prepare( |
|
453 | - "$date_field <= %s", |
|
454 | - $range['before'] |
|
455 | - ); |
|
456 | - } |
|
457 | - |
|
458 | - return $sql; |
|
459 | - |
|
460 | - } |
|
461 | - |
|
462 | - /** |
|
463 | - * Prepares a group by query. |
|
464 | - * |
|
465 | - * @param string $date_field The date field. |
|
466 | - * @return string The appropriate SQL. |
|
467 | - */ |
|
468 | - public function get_group_by_sql( $date_field ) { |
|
469 | - |
|
470 | - if ( 'day' === $this->groupby ) { |
|
471 | - return "YEAR($date_field), MONTH($date_field), DAY($date_field)"; |
|
472 | - } |
|
473 | - |
|
474 | - return "YEAR($date_field), MONTH($date_field)"; |
|
475 | - } |
|
476 | - |
|
477 | - /** |
|
478 | - * Get the query params for collections. |
|
479 | - * |
|
480 | - * @return array |
|
481 | - */ |
|
482 | - public function get_collection_params() { |
|
483 | - return array( |
|
484 | - 'context' => $this->get_context_param( array( 'default' => 'view' ) ), |
|
485 | - 'period' => array( |
|
486 | - 'description' => __( 'Limit to results of a specific period.', 'invoicing' ), |
|
487 | - 'type' => 'string', |
|
488 | - 'enum' => array( 'custom', 'today', 'yesterday', 'week', 'last_week', '7_days', '30_days', '60_days', '90_days', '180_days', 'month', 'last_month', 'quarter', 'last_quarter', 'year', 'last_year', 'quarter', 'last_quarter' ), |
|
489 | - 'validate_callback' => 'rest_validate_request_arg', |
|
490 | - 'sanitize_callback' => 'sanitize_text_field', |
|
491 | - 'default' => '7_days', |
|
492 | - ), |
|
493 | - 'after' => array( |
|
494 | - /* translators: %s: date format */ |
|
495 | - 'description' => sprintf( __( 'Limit to results after a specific date, the date needs to be in the %s format.', 'invoicing' ), 'YYYY-MM-DD' ), |
|
496 | - 'type' => 'string', |
|
497 | - 'validate_callback' => 'rest_validate_request_arg', |
|
498 | - 'sanitize_callback' => 'sanitize_text_field', |
|
499 | - 'default' => gmdate( 'Y-m-d', strtotime( '-7 days' ) ), |
|
500 | - ), |
|
501 | - 'before' => array( |
|
502 | - /* translators: %s: date format */ |
|
503 | - 'description' => sprintf( __( 'Limit to results before a specific date, the date needs to be in the %s format.', 'invoicing' ), 'YYYY-MM-DD' ), |
|
504 | - 'type' => 'string', |
|
505 | - 'validate_callback' => 'rest_validate_request_arg', |
|
506 | - 'sanitize_callback' => 'sanitize_text_field', |
|
507 | - 'default' => gmdate( 'Y-m-d' ), |
|
508 | - ), |
|
509 | - ); |
|
510 | - } |
|
110 | + if ( ! empty( $request['before'] ) ) { |
|
111 | + $before = min( strtotime( $before ), strtotime( sanitize_text_field( $request['before'] ) ) ); |
|
112 | + } |
|
113 | + |
|
114 | + // Set the previous date range. |
|
115 | + $difference = $before - $after; |
|
116 | + $this->previous_range = array( |
|
117 | + 'period' => 'custom', |
|
118 | + 'before' => gmdate( 'Y-m-d', $before - $difference - DAY_IN_SECONDS ), |
|
119 | + 'after' => gmdate( 'Y-m-d', $after - $difference - DAY_IN_SECONDS ), |
|
120 | + ); |
|
121 | + |
|
122 | + // Generate the report. |
|
123 | + return array( |
|
124 | + 'before' => gmdate( 'Y-m-d', $before ), |
|
125 | + 'after' => gmdate( 'Y-m-d', $after ), |
|
126 | + ); |
|
127 | + |
|
128 | + } |
|
129 | + |
|
130 | + /** |
|
131 | + * Retrieves todays date range. |
|
132 | + * |
|
133 | + * @return array The appropriate date range. |
|
134 | + */ |
|
135 | + public function get_today_date_range() { |
|
136 | + |
|
137 | + // Set the previous date range. |
|
138 | + $this->previous_range = array( |
|
139 | + 'period' => 'yesterday', |
|
140 | + ); |
|
141 | + |
|
142 | + // Generate the report. |
|
143 | + return array( |
|
144 | + 'before' => gmdate( 'Y-m-d' ), |
|
145 | + 'after' => gmdate( 'Y-m-d' ), |
|
146 | + ); |
|
147 | + |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * Retrieves yesterdays date range. |
|
152 | + * |
|
153 | + * @return array The appropriate date range. |
|
154 | + */ |
|
155 | + public function get_yesterday_date_range() { |
|
156 | + |
|
157 | + // Set the previous date range. |
|
158 | + $this->previous_range = array( |
|
159 | + 'period' => 'custom', |
|
160 | + 'before' => gmdate( 'Y-m-d', strtotime( '-2 days' ) ), |
|
161 | + 'after' => gmdate( 'Y-m-d', strtotime( '-2 days' ) ), |
|
162 | + ); |
|
163 | + |
|
164 | + // Generate the report. |
|
165 | + return array( |
|
166 | + 'before' => gmdate( 'Y-m-d', strtotime( '-1 day' ) ), |
|
167 | + 'after' => gmdate( 'Y-m-d', strtotime( '-1 day' ) ), |
|
168 | + ); |
|
169 | + |
|
170 | + } |
|
171 | + |
|
172 | + /** |
|
173 | + * Retrieves this week's date range. |
|
174 | + * |
|
175 | + * @return array The appropriate date range. |
|
176 | + */ |
|
177 | + public function get_week_date_range() { |
|
178 | + |
|
179 | + // Set the previous date range. |
|
180 | + $this->previous_range = array( |
|
181 | + 'period' => 'last_week', |
|
182 | + ); |
|
183 | + |
|
184 | + // Generate the report. |
|
185 | + $week_starts = absint( get_option( 'start_of_week' ) ); |
|
186 | + return array( |
|
187 | + 'before' => gmdate( 'Y-m-d' ), |
|
188 | + 'after' => gmdate( 'Y-m-d', strtotime( 'next Sunday -' . ( 7 - $week_starts ) . ' days' ) ), |
|
189 | + ); |
|
190 | + } |
|
191 | + |
|
192 | + /** |
|
193 | + * Retrieves last week's date range. |
|
194 | + * |
|
195 | + * @return array The appropriate date range. |
|
196 | + */ |
|
197 | + public function get_last_week_date_range() { |
|
198 | + |
|
199 | + $week_starts = absint( get_option( 'start_of_week' ) ); |
|
200 | + $week_starts = strtotime( 'last Sunday -' . ( 7 - $week_starts ) . ' days' ); |
|
201 | + $date_range = array( |
|
202 | + 'before' => gmdate( 'Y-m-d', $week_starts + 6 * DAY_IN_SECONDS ), |
|
203 | + 'after' => gmdate( 'Y-m-d', $week_starts ), |
|
204 | + ); |
|
205 | + |
|
206 | + // Set the previous date range. |
|
207 | + $week_starts = $week_starts - 7 * DAY_IN_SECONDS; |
|
208 | + $this->previous_range = array( |
|
209 | + 'period' => 'custom', |
|
210 | + 'before' => gmdate( 'Y-m-d', $week_starts + 6 * DAY_IN_SECONDS ), |
|
211 | + 'after' => gmdate( 'Y-m-d', $week_starts ), |
|
212 | + ); |
|
213 | + |
|
214 | + // Generate the report. |
|
215 | + return $date_range; |
|
216 | + } |
|
217 | + |
|
218 | + /** |
|
219 | + * Retrieves last x days date range. |
|
220 | + * |
|
221 | + * @return array The appropriate date range. |
|
222 | + */ |
|
223 | + public function get_x_days_date_range( $days = 7 ) { |
|
224 | + |
|
225 | + $days--; |
|
226 | + |
|
227 | + $date_range = array( |
|
228 | + 'before' => gmdate( 'Y-m-d' ), |
|
229 | + 'after' => gmdate( 'Y-m-d', strtotime( "-$days days" ) ), |
|
230 | + ); |
|
231 | + |
|
232 | + $days++; |
|
233 | + |
|
234 | + // Set the previous date range. |
|
235 | + $this->previous_range = array( |
|
236 | + 'period' => 'custom', |
|
237 | + 'before' => gmdate( 'Y-m-d', strtotime( $date_range['before'] ) - $days * DAY_IN_SECONDS ), |
|
238 | + 'after' => gmdate( 'Y-m-d', strtotime( $date_range['after'] ) - $days * DAY_IN_SECONDS ), |
|
239 | + ); |
|
240 | + |
|
241 | + // Generate the report. |
|
242 | + return $date_range; |
|
243 | + } |
|
244 | + |
|
245 | + /** |
|
246 | + * Retrieves this month date range. |
|
247 | + * |
|
248 | + * @return array The appropriate date range. |
|
249 | + */ |
|
250 | + public function get_month_date_range() { |
|
251 | + |
|
252 | + // Set the previous date range. |
|
253 | + $this->previous_range = array( |
|
254 | + 'period' => 'last_month', |
|
255 | + ); |
|
256 | + |
|
257 | + // Generate the report. |
|
258 | + return array( |
|
259 | + 'after' => gmdate( 'Y-m-01' ), |
|
260 | + 'before' => gmdate( 'Y-m-t' ), |
|
261 | + ); |
|
262 | + |
|
263 | + } |
|
264 | + |
|
265 | + /** |
|
266 | + * Retrieves last month's date range. |
|
267 | + * |
|
268 | + * @return array The appropriate date range. |
|
269 | + */ |
|
270 | + public function get_last_month_date_range() { |
|
271 | + |
|
272 | + // Set the previous date range. |
|
273 | + $this->previous_range = array( |
|
274 | + 'period' => 'custom', |
|
275 | + 'after' => gmdate( 'Y-m-01', strtotime( '-2 months' ) ), |
|
276 | + 'before' => gmdate( 'Y-m-t', strtotime( '-2 months' ) ), |
|
277 | + ); |
|
278 | + |
|
279 | + // Generate the report. |
|
280 | + return array( |
|
281 | + 'after' => gmdate( 'Y-m-01', strtotime( 'last month' ) ), |
|
282 | + 'before' => gmdate( 'Y-m-t', strtotime( 'last month' ) ), |
|
283 | + ); |
|
284 | + |
|
285 | + } |
|
286 | + |
|
287 | + /** |
|
288 | + * Retrieves this quarter date range. |
|
289 | + * |
|
290 | + * @return array The available quarters. |
|
291 | + */ |
|
292 | + public function get_quarters() { |
|
293 | + |
|
294 | + $year = (int) gmdate( 'Y' ); |
|
295 | + $last_year = (int) $year - 1; |
|
296 | + return array( |
|
297 | + |
|
298 | + // Third quarter of previous year: July 1st to September 30th |
|
299 | + array( |
|
300 | + 'before' => "{$last_year}-09-30", |
|
301 | + 'after' => "{$last_year}-07-01", |
|
302 | + ), |
|
303 | + |
|
304 | + // Last quarter of previous year: October 1st to December 31st |
|
305 | + array( |
|
306 | + 'before' => "{$last_year}-12-31", |
|
307 | + 'after' => "{$last_year}-10-01", |
|
308 | + ), |
|
309 | + |
|
310 | + // First quarter: January 1st to March 31st |
|
311 | + array( |
|
312 | + 'before' => "{$year}-03-31", |
|
313 | + 'after' => "{$year}-01-01", |
|
314 | + ), |
|
315 | + |
|
316 | + // Second quarter: April 1st to June 30th |
|
317 | + array( |
|
318 | + 'before' => "{$year}-06-30", |
|
319 | + 'after' => "{$year}-04-01", |
|
320 | + ), |
|
321 | + |
|
322 | + // Third quarter: July 1st to September 30th |
|
323 | + array( |
|
324 | + 'before' => "{$year}-09-30", |
|
325 | + 'after' => "{$year}-07-01", |
|
326 | + ), |
|
327 | + |
|
328 | + // Fourth quarter: October 1st to December 31st |
|
329 | + array( |
|
330 | + 'before' => "{$year}-12-31", |
|
331 | + 'after' => "{$year}-10-01", |
|
332 | + ), |
|
333 | + ); |
|
334 | + } |
|
335 | + |
|
336 | + /** |
|
337 | + * Retrieves the current quater. |
|
338 | + * |
|
339 | + * @return int The current quarter. |
|
340 | + */ |
|
341 | + public function get_quarter() { |
|
342 | + |
|
343 | + $month = (int) gmdate( 'n' ); |
|
344 | + $quarters = array( 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4 ); |
|
345 | + return $quarters[ $month - 1 ]; |
|
346 | + |
|
347 | + } |
|
348 | + |
|
349 | + /** |
|
350 | + * Retrieves this quarter date range. |
|
351 | + * |
|
352 | + * @return array The appropriate date range. |
|
353 | + */ |
|
354 | + public function get_quarter_date_range() { |
|
355 | + |
|
356 | + // Set the previous date range. |
|
357 | + $this->previous_range = array( |
|
358 | + 'period' => 'last_quarter', |
|
359 | + ); |
|
360 | + |
|
361 | + // Generate the report. |
|
362 | + $quarter = $this->get_quarter(); |
|
363 | + $quarters = $this->get_quarters(); |
|
364 | + return $quarters[ $quarter + 1 ]; |
|
365 | + |
|
366 | + } |
|
367 | + |
|
368 | + /** |
|
369 | + * Retrieves last quarter's date range. |
|
370 | + * |
|
371 | + * @return array The appropriate date range. |
|
372 | + */ |
|
373 | + public function get_last_quarter_date_range() { |
|
374 | + |
|
375 | + $quarters = $this->get_quarters(); |
|
376 | + $quarter = $this->get_quarter(); |
|
377 | + |
|
378 | + // Set the previous date range. |
|
379 | + $this->previous_range = array_merge( |
|
380 | + $quarters[ $quarter - 1 ], |
|
381 | + array( 'period' => 'custom' ) |
|
382 | + ); |
|
383 | + |
|
384 | + // Generate the report. |
|
385 | + return $quarters[ $quarter ]; |
|
386 | + |
|
387 | + } |
|
388 | + |
|
389 | + /** |
|
390 | + * Retrieves this year date range. |
|
391 | + * |
|
392 | + * @return array The appropriate date range. |
|
393 | + */ |
|
394 | + public function get_year_date_range() { |
|
395 | + |
|
396 | + // Set the previous date range. |
|
397 | + $this->previous_range = array( |
|
398 | + 'period' => 'last_year', |
|
399 | + ); |
|
400 | + |
|
401 | + // Generate the report. |
|
402 | + return array( |
|
403 | + 'after' => gmdate( 'Y-01-01' ), |
|
404 | + 'before' => gmdate( 'Y-12-31' ), |
|
405 | + ); |
|
406 | + |
|
407 | + } |
|
408 | + |
|
409 | + /** |
|
410 | + * Retrieves last year date range. |
|
411 | + * |
|
412 | + * @return array The appropriate date range. |
|
413 | + */ |
|
414 | + public function get_last_year_date_range() { |
|
415 | + |
|
416 | + // Set the previous date range. |
|
417 | + $this->previous_range = array( |
|
418 | + 'period' => 'custom', |
|
419 | + 'after' => gmdate( 'Y-01-01', strtotime( '-2 years' ) ), |
|
420 | + 'before' => gmdate( 'Y-12-31', strtotime( '-2 years' ) ), |
|
421 | + ); |
|
422 | + |
|
423 | + // Generate the report. |
|
424 | + return array( |
|
425 | + 'after' => gmdate( 'Y-01-01', strtotime( 'last year' ) ), |
|
426 | + 'before' => gmdate( 'Y-12-31', strtotime( 'last year' ) ), |
|
427 | + ); |
|
428 | + |
|
429 | + } |
|
430 | + |
|
431 | + /** |
|
432 | + * Prepare a the request date for SQL usage. |
|
433 | + * |
|
434 | + * @param WP_REST_Request $request Request object. |
|
435 | + * @param string $date_field The date field. |
|
436 | + * @return string The appropriate SQL. |
|
437 | + */ |
|
438 | + public function get_date_range_sql( $request, $date_field ) { |
|
439 | + global $wpdb; |
|
440 | + |
|
441 | + $sql = '1=1'; |
|
442 | + $range = $this->get_date_range( $request ); |
|
443 | + |
|
444 | + if ( ! empty( $range['after'] ) ) { |
|
445 | + $sql .= ' AND ' . $wpdb->prepare( |
|
446 | + "$date_field >= %s", |
|
447 | + $range['after'] |
|
448 | + ); |
|
449 | + } |
|
450 | + |
|
451 | + if ( ! empty( $range['before'] ) ) { |
|
452 | + $sql .= ' AND ' . $wpdb->prepare( |
|
453 | + "$date_field <= %s", |
|
454 | + $range['before'] |
|
455 | + ); |
|
456 | + } |
|
457 | + |
|
458 | + return $sql; |
|
459 | + |
|
460 | + } |
|
461 | + |
|
462 | + /** |
|
463 | + * Prepares a group by query. |
|
464 | + * |
|
465 | + * @param string $date_field The date field. |
|
466 | + * @return string The appropriate SQL. |
|
467 | + */ |
|
468 | + public function get_group_by_sql( $date_field ) { |
|
469 | + |
|
470 | + if ( 'day' === $this->groupby ) { |
|
471 | + return "YEAR($date_field), MONTH($date_field), DAY($date_field)"; |
|
472 | + } |
|
473 | + |
|
474 | + return "YEAR($date_field), MONTH($date_field)"; |
|
475 | + } |
|
476 | + |
|
477 | + /** |
|
478 | + * Get the query params for collections. |
|
479 | + * |
|
480 | + * @return array |
|
481 | + */ |
|
482 | + public function get_collection_params() { |
|
483 | + return array( |
|
484 | + 'context' => $this->get_context_param( array( 'default' => 'view' ) ), |
|
485 | + 'period' => array( |
|
486 | + 'description' => __( 'Limit to results of a specific period.', 'invoicing' ), |
|
487 | + 'type' => 'string', |
|
488 | + 'enum' => array( 'custom', 'today', 'yesterday', 'week', 'last_week', '7_days', '30_days', '60_days', '90_days', '180_days', 'month', 'last_month', 'quarter', 'last_quarter', 'year', 'last_year', 'quarter', 'last_quarter' ), |
|
489 | + 'validate_callback' => 'rest_validate_request_arg', |
|
490 | + 'sanitize_callback' => 'sanitize_text_field', |
|
491 | + 'default' => '7_days', |
|
492 | + ), |
|
493 | + 'after' => array( |
|
494 | + /* translators: %s: date format */ |
|
495 | + 'description' => sprintf( __( 'Limit to results after a specific date, the date needs to be in the %s format.', 'invoicing' ), 'YYYY-MM-DD' ), |
|
496 | + 'type' => 'string', |
|
497 | + 'validate_callback' => 'rest_validate_request_arg', |
|
498 | + 'sanitize_callback' => 'sanitize_text_field', |
|
499 | + 'default' => gmdate( 'Y-m-d', strtotime( '-7 days' ) ), |
|
500 | + ), |
|
501 | + 'before' => array( |
|
502 | + /* translators: %s: date format */ |
|
503 | + 'description' => sprintf( __( 'Limit to results before a specific date, the date needs to be in the %s format.', 'invoicing' ), 'YYYY-MM-DD' ), |
|
504 | + 'type' => 'string', |
|
505 | + 'validate_callback' => 'rest_validate_request_arg', |
|
506 | + 'sanitize_callback' => 'sanitize_text_field', |
|
507 | + 'default' => gmdate( 'Y-m-d' ), |
|
508 | + ), |
|
509 | + ); |
|
510 | + } |
|
511 | 511 | } |
@@ -12,185 +12,185 @@ discard block |
||
12 | 12 | */ |
13 | 13 | class GetPaid_Checkout { |
14 | 14 | |
15 | - /** |
|
16 | - * @var GetPaid_Payment_Form_Submission |
|
17 | - */ |
|
18 | - protected $payment_form_submission; |
|
19 | - |
|
20 | - /** |
|
21 | - * Class constructor. |
|
22 | - * |
|
23 | - * @param GetPaid_Payment_Form_Submission $submission |
|
24 | - */ |
|
25 | - public function __construct( $submission ) { |
|
26 | - $this->payment_form_submission = $submission; |
|
27 | - } |
|
28 | - |
|
29 | - /** |
|
30 | - * Processes the checkout. |
|
31 | - * |
|
32 | - */ |
|
33 | - public function process_checkout() { |
|
34 | - |
|
35 | - // Validate the submission. |
|
36 | - $this->validate_submission(); |
|
37 | - |
|
38 | - // Prepare the invoice. |
|
39 | - $items = $this->get_submission_items(); |
|
40 | - $invoice = $this->get_submission_invoice(); |
|
41 | - $invoice = $this->process_submission_invoice( $invoice, $items ); |
|
42 | - $prepared = $this->prepare_submission_data_for_saving(); |
|
43 | - |
|
44 | - $this->prepare_billing_info( $invoice ); |
|
45 | - |
|
46 | - $shipping = $this->prepare_shipping_info( $invoice ); |
|
47 | - |
|
48 | - // Save the invoice. |
|
49 | - $invoice->set_is_viewed( true ); |
|
50 | - $invoice->recalculate_total(); |
|
15 | + /** |
|
16 | + * @var GetPaid_Payment_Form_Submission |
|
17 | + */ |
|
18 | + protected $payment_form_submission; |
|
19 | + |
|
20 | + /** |
|
21 | + * Class constructor. |
|
22 | + * |
|
23 | + * @param GetPaid_Payment_Form_Submission $submission |
|
24 | + */ |
|
25 | + public function __construct( $submission ) { |
|
26 | + $this->payment_form_submission = $submission; |
|
27 | + } |
|
28 | + |
|
29 | + /** |
|
30 | + * Processes the checkout. |
|
31 | + * |
|
32 | + */ |
|
33 | + public function process_checkout() { |
|
34 | + |
|
35 | + // Validate the submission. |
|
36 | + $this->validate_submission(); |
|
37 | + |
|
38 | + // Prepare the invoice. |
|
39 | + $items = $this->get_submission_items(); |
|
40 | + $invoice = $this->get_submission_invoice(); |
|
41 | + $invoice = $this->process_submission_invoice( $invoice, $items ); |
|
42 | + $prepared = $this->prepare_submission_data_for_saving(); |
|
43 | + |
|
44 | + $this->prepare_billing_info( $invoice ); |
|
45 | + |
|
46 | + $shipping = $this->prepare_shipping_info( $invoice ); |
|
47 | + |
|
48 | + // Save the invoice. |
|
49 | + $invoice->set_is_viewed( true ); |
|
50 | + $invoice->recalculate_total(); |
|
51 | 51 | $invoice->save(); |
52 | 52 | |
53 | - do_action( 'getpaid_checkout_invoice_updated', $invoice ); |
|
53 | + do_action( 'getpaid_checkout_invoice_updated', $invoice ); |
|
54 | 54 | |
55 | - // Send to the gateway. |
|
56 | - $this->post_process_submission( $invoice, $prepared, $shipping ); |
|
57 | - } |
|
55 | + // Send to the gateway. |
|
56 | + $this->post_process_submission( $invoice, $prepared, $shipping ); |
|
57 | + } |
|
58 | 58 | |
59 | - /** |
|
60 | - * Validates the submission. |
|
61 | - * |
|
62 | - */ |
|
63 | - protected function validate_submission() { |
|
59 | + /** |
|
60 | + * Validates the submission. |
|
61 | + * |
|
62 | + */ |
|
63 | + protected function validate_submission() { |
|
64 | 64 | |
65 | - $submission = $this->payment_form_submission; |
|
66 | - $data = $submission->get_data(); |
|
65 | + $submission = $this->payment_form_submission; |
|
66 | + $data = $submission->get_data(); |
|
67 | 67 | |
68 | - // Do we have an error? |
|
68 | + // Do we have an error? |
|
69 | 69 | if ( ! empty( $submission->last_error ) ) { |
70 | - wp_send_json_error( $submission->last_error ); |
|
70 | + wp_send_json_error( $submission->last_error ); |
|
71 | 71 | } |
72 | 72 | |
73 | - // We need a billing email. |
|
73 | + // We need a billing email. |
|
74 | 74 | if ( ! $submission->has_billing_email() ) { |
75 | 75 | wp_send_json_error( __( 'Provide a valid billing email.', 'invoicing' ) ); |
76 | - } |
|
76 | + } |
|
77 | 77 | |
78 | - // Non-recurring gateways should not be allowed to process recurring invoices. |
|
79 | - if ( $submission->should_collect_payment_details() && $submission->has_recurring && ! wpinv_gateway_support_subscription( $data['wpi-gateway'] ) ) { |
|
80 | - wp_send_json_error( __( 'The selected payment gateway does not support subscription payments.', 'invoicing' ) ); |
|
81 | - } |
|
78 | + // Non-recurring gateways should not be allowed to process recurring invoices. |
|
79 | + if ( $submission->should_collect_payment_details() && $submission->has_recurring && ! wpinv_gateway_support_subscription( $data['wpi-gateway'] ) ) { |
|
80 | + wp_send_json_error( __( 'The selected payment gateway does not support subscription payments.', 'invoicing' ) ); |
|
81 | + } |
|
82 | 82 | |
83 | - // Ensure the gateway is active. |
|
84 | - if ( $submission->should_collect_payment_details() && ! wpinv_is_gateway_active( $data['wpi-gateway'] ) ) { |
|
85 | - wp_send_json_error( __( 'The selected payment gateway is not active', 'invoicing' ) ); |
|
86 | - } |
|
83 | + // Ensure the gateway is active. |
|
84 | + if ( $submission->should_collect_payment_details() && ! wpinv_is_gateway_active( $data['wpi-gateway'] ) ) { |
|
85 | + wp_send_json_error( __( 'The selected payment gateway is not active', 'invoicing' ) ); |
|
86 | + } |
|
87 | 87 | |
88 | - // Clear any existing errors. |
|
89 | - wpinv_clear_errors(); |
|
88 | + // Clear any existing errors. |
|
89 | + wpinv_clear_errors(); |
|
90 | 90 | |
91 | - // Allow themes and plugins to hook to errors |
|
92 | - do_action( 'getpaid_checkout_error_checks', $submission ); |
|
91 | + // Allow themes and plugins to hook to errors |
|
92 | + do_action( 'getpaid_checkout_error_checks', $submission ); |
|
93 | 93 | |
94 | - // Do we have any errors? |
|
94 | + // Do we have any errors? |
|
95 | 95 | if ( wpinv_get_errors() ) { |
96 | 96 | wp_send_json_error( getpaid_get_errors_html() ); |
97 | - } |
|
97 | + } |
|
98 | 98 | |
99 | - } |
|
99 | + } |
|
100 | 100 | |
101 | - /** |
|
102 | - * Retrieves submission items. |
|
103 | - * |
|
104 | - * @return GetPaid_Form_Item[] |
|
105 | - */ |
|
106 | - protected function get_submission_items() { |
|
101 | + /** |
|
102 | + * Retrieves submission items. |
|
103 | + * |
|
104 | + * @return GetPaid_Form_Item[] |
|
105 | + */ |
|
106 | + protected function get_submission_items() { |
|
107 | 107 | |
108 | - $items = $this->payment_form_submission->get_items(); |
|
108 | + $items = $this->payment_form_submission->get_items(); |
|
109 | 109 | |
110 | 110 | // Ensure that we have items. |
111 | 111 | if ( empty( $items ) && ! $this->payment_form_submission->has_fees() ) { |
112 | 112 | wp_send_json_error( __( 'Please provide at least one item or amount.', 'invoicing' ) ); |
113 | - } |
|
114 | - |
|
115 | - return $items; |
|
116 | - } |
|
117 | - |
|
118 | - /** |
|
119 | - * Retrieves submission invoice. |
|
120 | - * |
|
121 | - * @return WPInv_Invoice |
|
122 | - */ |
|
123 | - protected function get_submission_invoice() { |
|
124 | - $submission = $this->payment_form_submission; |
|
125 | - |
|
126 | - if ( ! $submission->has_invoice() ) { |
|
127 | - $invoice = new WPInv_Invoice(); |
|
128 | - $invoice->set_created_via( 'payment_form' ); |
|
129 | - return $invoice; |
|
130 | 113 | } |
131 | 114 | |
132 | - $invoice = $submission->get_invoice(); |
|
115 | + return $items; |
|
116 | + } |
|
117 | + |
|
118 | + /** |
|
119 | + * Retrieves submission invoice. |
|
120 | + * |
|
121 | + * @return WPInv_Invoice |
|
122 | + */ |
|
123 | + protected function get_submission_invoice() { |
|
124 | + $submission = $this->payment_form_submission; |
|
133 | 125 | |
134 | - // Make sure that it is neither paid or refunded. |
|
135 | - if ( $invoice->is_paid() || $invoice->is_refunded() ) { |
|
136 | - wp_send_json_error( __( 'This invoice has already been paid for.', 'invoicing' ) ); |
|
137 | - } |
|
126 | + if ( ! $submission->has_invoice() ) { |
|
127 | + $invoice = new WPInv_Invoice(); |
|
128 | + $invoice->set_created_via( 'payment_form' ); |
|
129 | + return $invoice; |
|
130 | + } |
|
138 | 131 | |
139 | - return $invoice; |
|
140 | - } |
|
132 | + $invoice = $submission->get_invoice(); |
|
141 | 133 | |
142 | - /** |
|
143 | - * Processes the submission invoice. |
|
144 | - * |
|
145 | - * @param WPInv_Invoice $invoice |
|
146 | - * @param GetPaid_Form_Item[] $items |
|
147 | - * @return WPInv_Invoice |
|
148 | - */ |
|
149 | - protected function process_submission_invoice( $invoice, $items ) { |
|
134 | + // Make sure that it is neither paid or refunded. |
|
135 | + if ( $invoice->is_paid() || $invoice->is_refunded() ) { |
|
136 | + wp_send_json_error( __( 'This invoice has already been paid for.', 'invoicing' ) ); |
|
137 | + } |
|
150 | 138 | |
151 | - $submission = $this->payment_form_submission; |
|
139 | + return $invoice; |
|
140 | + } |
|
152 | 141 | |
153 | - // Set-up the invoice details. |
|
154 | - $invoice->set_email( sanitize_email( $submission->get_billing_email() ) ); |
|
155 | - $invoice->set_user_id( $this->get_submission_customer() ); |
|
156 | - $invoice->set_submission_id( $submission->id ); |
|
157 | - $invoice->set_payment_form( absint( $submission->get_payment_form()->get_id() ) ); |
|
142 | + /** |
|
143 | + * Processes the submission invoice. |
|
144 | + * |
|
145 | + * @param WPInv_Invoice $invoice |
|
146 | + * @param GetPaid_Form_Item[] $items |
|
147 | + * @return WPInv_Invoice |
|
148 | + */ |
|
149 | + protected function process_submission_invoice( $invoice, $items ) { |
|
150 | + |
|
151 | + $submission = $this->payment_form_submission; |
|
152 | + |
|
153 | + // Set-up the invoice details. |
|
154 | + $invoice->set_email( sanitize_email( $submission->get_billing_email() ) ); |
|
155 | + $invoice->set_user_id( $this->get_submission_customer() ); |
|
156 | + $invoice->set_submission_id( $submission->id ); |
|
157 | + $invoice->set_payment_form( absint( $submission->get_payment_form()->get_id() ) ); |
|
158 | 158 | $invoice->set_items( $items ); |
159 | 159 | $invoice->set_fees( $submission->get_fees() ); |
160 | 160 | $invoice->set_taxes( $submission->get_taxes() ); |
161 | - $invoice->set_discounts( $submission->get_discounts() ); |
|
162 | - $invoice->set_gateway( $submission->get_field( 'wpi-gateway' ) ); |
|
163 | - $invoice->set_currency( $submission->get_currency() ); |
|
161 | + $invoice->set_discounts( $submission->get_discounts() ); |
|
162 | + $invoice->set_gateway( $submission->get_field( 'wpi-gateway' ) ); |
|
163 | + $invoice->set_currency( $submission->get_currency() ); |
|
164 | 164 | |
165 | - if ( $submission->has_shipping() ) { |
|
166 | - $invoice->set_shipping( $submission->get_shipping() ); |
|
167 | - } |
|
165 | + if ( $submission->has_shipping() ) { |
|
166 | + $invoice->set_shipping( $submission->get_shipping() ); |
|
167 | + } |
|
168 | 168 | |
169 | - $address_confirmed = $submission->get_field( 'confirm-address' ); |
|
170 | - $invoice->set_address_confirmed( ! empty( $address_confirmed ) ); |
|
169 | + $address_confirmed = $submission->get_field( 'confirm-address' ); |
|
170 | + $invoice->set_address_confirmed( ! empty( $address_confirmed ) ); |
|
171 | 171 | |
172 | - if ( $submission->has_discount_code() ) { |
|
172 | + if ( $submission->has_discount_code() ) { |
|
173 | 173 | $invoice->set_discount_code( $submission->get_discount_code() ); |
174 | - } |
|
175 | - |
|
176 | - getpaid_maybe_add_default_address( $invoice ); |
|
177 | - return $invoice; |
|
178 | - } |
|
179 | - |
|
180 | - /** |
|
181 | - * Retrieves the submission's customer. |
|
182 | - * |
|
183 | - * @return int The customer id. |
|
184 | - */ |
|
185 | - protected function get_submission_customer() { |
|
186 | - $submission = $this->payment_form_submission; |
|
187 | - |
|
188 | - // If this is an existing invoice... |
|
189 | - if ( $submission->has_invoice() ) { |
|
190 | - return $submission->get_invoice()->get_user_id(); |
|
191 | - } |
|
192 | - |
|
193 | - // (Maybe) create the user. |
|
174 | + } |
|
175 | + |
|
176 | + getpaid_maybe_add_default_address( $invoice ); |
|
177 | + return $invoice; |
|
178 | + } |
|
179 | + |
|
180 | + /** |
|
181 | + * Retrieves the submission's customer. |
|
182 | + * |
|
183 | + * @return int The customer id. |
|
184 | + */ |
|
185 | + protected function get_submission_customer() { |
|
186 | + $submission = $this->payment_form_submission; |
|
187 | + |
|
188 | + // If this is an existing invoice... |
|
189 | + if ( $submission->has_invoice() ) { |
|
190 | + return $submission->get_invoice()->get_user_id(); |
|
191 | + } |
|
192 | + |
|
193 | + // (Maybe) create the user. |
|
194 | 194 | $user = get_current_user_id(); |
195 | 195 | |
196 | 196 | if ( empty( $user ) ) { |
@@ -198,16 +198,16 @@ discard block |
||
198 | 198 | } |
199 | 199 | |
200 | 200 | if ( empty( $user ) ) { |
201 | - $name = array( $submission->get_field( 'wpinv_first_name', 'billing' ), $submission->get_field( 'wpinv_last_name', 'billing' ) ); |
|
202 | - $name = implode( '', array_filter( $name ) ); |
|
201 | + $name = array( $submission->get_field( 'wpinv_first_name', 'billing' ), $submission->get_field( 'wpinv_last_name', 'billing' ) ); |
|
202 | + $name = implode( '', array_filter( $name ) ); |
|
203 | 203 | $user = wpinv_create_user( $submission->get_billing_email(), $name ); |
204 | 204 | |
205 | - // (Maybe) send new user notification. |
|
206 | - $should_send_notification = wpinv_get_option( 'disable_new_user_emails' ); |
|
207 | - if ( ! empty( $user ) && is_numeric( $user ) && apply_filters( 'getpaid_send_new_user_notification', empty( $should_send_notification ), $user ) ) { |
|
208 | - wp_send_new_user_notifications( $user, 'user' ); |
|
209 | - } |
|
210 | - } |
|
205 | + // (Maybe) send new user notification. |
|
206 | + $should_send_notification = wpinv_get_option( 'disable_new_user_emails' ); |
|
207 | + if ( ! empty( $user ) && is_numeric( $user ) && apply_filters( 'getpaid_send_new_user_notification', empty( $should_send_notification ), $user ) ) { |
|
208 | + wp_send_new_user_notifications( $user, 'user' ); |
|
209 | + } |
|
210 | + } |
|
211 | 211 | |
212 | 212 | if ( is_wp_error( $user ) ) { |
213 | 213 | wp_send_json_error( $user->get_error_message() ); |
@@ -215,49 +215,49 @@ discard block |
||
215 | 215 | |
216 | 216 | if ( is_numeric( $user ) ) { |
217 | 217 | return $user; |
218 | - } |
|
218 | + } |
|
219 | 219 | |
220 | - return $user->ID; |
|
220 | + return $user->ID; |
|
221 | 221 | |
222 | - } |
|
222 | + } |
|
223 | 223 | |
224 | - /** |
|
224 | + /** |
|
225 | 225 | * Prepares submission data for saving to the database. |
226 | 226 | * |
227 | - * @return array |
|
227 | + * @return array |
|
228 | 228 | */ |
229 | 229 | public function prepare_submission_data_for_saving() { |
230 | 230 | |
231 | - $submission = $this->payment_form_submission; |
|
231 | + $submission = $this->payment_form_submission; |
|
232 | 232 | |
233 | - // Prepared submission details. |
|
233 | + // Prepared submission details. |
|
234 | 234 | $prepared = array( |
235 | - 'all' => array(), |
|
236 | - 'meta' => array(), |
|
237 | - ); |
|
235 | + 'all' => array(), |
|
236 | + 'meta' => array(), |
|
237 | + ); |
|
238 | 238 | |
239 | 239 | // Raw submission details. |
240 | - $data = $submission->get_data(); |
|
240 | + $data = $submission->get_data(); |
|
241 | 241 | |
242 | - // Loop through the submitted details. |
|
242 | + // Loop through the submitted details. |
|
243 | 243 | foreach ( $submission->get_payment_form()->get_elements() as $field ) { |
244 | 244 | |
245 | - // Skip premade fields. |
|
245 | + // Skip premade fields. |
|
246 | 246 | if ( ! empty( $field['premade'] ) ) { |
247 | 247 | continue; |
248 | 248 | } |
249 | 249 | |
250 | - // Ensure address is provided. |
|
251 | - if ( 'address' === $field['type'] ) { |
|
250 | + // Ensure address is provided. |
|
251 | + if ( 'address' === $field['type'] ) { |
|
252 | 252 | $address_type = isset( $field['address_type'] ) && 'shipping' === $field['address_type'] ? 'shipping' : 'billing'; |
253 | 253 | |
254 | - foreach ( $field['fields'] as $address_field ) { |
|
254 | + foreach ( $field['fields'] as $address_field ) { |
|
255 | 255 | |
256 | - if ( ! empty( $address_field['visible'] ) && ! empty( $address_field['required'] ) && '' === trim( $_POST[ $address_type ][ $address_field['name'] ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
257 | - wp_send_json_error( __( 'Please fill all required fields.', 'invoicing' ) ); |
|
258 | - } |
|
259 | - } |
|
260 | - } |
|
256 | + if ( ! empty( $address_field['visible'] ) && ! empty( $address_field['required'] ) && '' === trim( $_POST[ $address_type ][ $address_field['name'] ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
257 | + wp_send_json_error( __( 'Please fill all required fields.', 'invoicing' ) ); |
|
258 | + } |
|
259 | + } |
|
260 | + } |
|
261 | 261 | |
262 | 262 | // If it is required and not set, abort. |
263 | 263 | if ( ! $submission->is_required_field_set( $field ) ) { |
@@ -267,31 +267,31 @@ discard block |
||
267 | 267 | // Handle misc fields. |
268 | 268 | if ( isset( $data[ $field['id'] ] ) ) { |
269 | 269 | |
270 | - // Uploads. |
|
271 | - if ( 'file_upload' === $field['type'] ) { |
|
272 | - $max_file_num = empty( $field['max_file_num'] ) ? 1 : absint( $field['max_file_num'] ); |
|
270 | + // Uploads. |
|
271 | + if ( 'file_upload' === $field['type'] ) { |
|
272 | + $max_file_num = empty( $field['max_file_num'] ) ? 1 : absint( $field['max_file_num'] ); |
|
273 | 273 | |
274 | - if ( count( $data[ $field['id'] ] ) > $max_file_num ) { |
|
275 | - wp_send_json_error( __( 'Maximum number of allowed files exceeded.', 'invoicing' ) ); |
|
276 | - } |
|
274 | + if ( count( $data[ $field['id'] ] ) > $max_file_num ) { |
|
275 | + wp_send_json_error( __( 'Maximum number of allowed files exceeded.', 'invoicing' ) ); |
|
276 | + } |
|
277 | 277 | |
278 | - $value = array(); |
|
278 | + $value = array(); |
|
279 | 279 | |
280 | - foreach ( $data[ $field['id'] ] as $url => $name ) { |
|
281 | - $value[] = sprintf( |
|
282 | - '<a href="%s" target="_blank">%s</a>', |
|
283 | - esc_url_raw( $url ), |
|
284 | - esc_html( $name ) |
|
285 | - ); |
|
286 | - } |
|
280 | + foreach ( $data[ $field['id'] ] as $url => $name ) { |
|
281 | + $value[] = sprintf( |
|
282 | + '<a href="%s" target="_blank">%s</a>', |
|
283 | + esc_url_raw( $url ), |
|
284 | + esc_html( $name ) |
|
285 | + ); |
|
286 | + } |
|
287 | 287 | |
288 | - $value = implode( ' | ', $value ); |
|
288 | + $value = implode( ' | ', $value ); |
|
289 | 289 | |
290 | - } elseif ( 'checkbox' === $field['type'] ) { |
|
291 | - $value = ! empty( $data[ $field['id'] ] ) ? __( 'Yes', 'invoicing' ) : __( 'No', 'invoicing' ); |
|
292 | - } else { |
|
293 | - $value = wp_kses_post( $data[ $field['id'] ] ); |
|
294 | - } |
|
290 | + } elseif ( 'checkbox' === $field['type'] ) { |
|
291 | + $value = ! empty( $data[ $field['id'] ] ) ? __( 'Yes', 'invoicing' ) : __( 'No', 'invoicing' ); |
|
292 | + } else { |
|
293 | + $value = wp_kses_post( $data[ $field['id'] ] ); |
|
294 | + } |
|
295 | 295 | |
296 | 296 | $label = $field['id']; |
297 | 297 | |
@@ -299,192 +299,192 @@ discard block |
||
299 | 299 | $label = $field['label']; |
300 | 300 | } |
301 | 301 | |
302 | - if ( ! empty( $field['add_meta'] ) ) { |
|
303 | - $prepared['meta'][ wpinv_clean( $label ) ] = wp_kses_post_deep( $value ); |
|
304 | - } |
|
305 | - $prepared['all'][ wpinv_clean( $label ) ] = wp_kses_post_deep( $value ); |
|
302 | + if ( ! empty( $field['add_meta'] ) ) { |
|
303 | + $prepared['meta'][ wpinv_clean( $label ) ] = wp_kses_post_deep( $value ); |
|
304 | + } |
|
305 | + $prepared['all'][ wpinv_clean( $label ) ] = wp_kses_post_deep( $value ); |
|
306 | 306 | |
307 | 307 | } |
308 | - } |
|
308 | + } |
|
309 | 309 | |
310 | - return $prepared; |
|
310 | + return $prepared; |
|
311 | 311 | |
312 | - } |
|
312 | + } |
|
313 | 313 | |
314 | - /** |
|
314 | + /** |
|
315 | 315 | * Retrieves address details. |
316 | 316 | * |
317 | - * @return array |
|
318 | - * @param WPInv_Invoice $invoice |
|
319 | - * @param string $type |
|
317 | + * @return array |
|
318 | + * @param WPInv_Invoice $invoice |
|
319 | + * @param string $type |
|
320 | 320 | */ |
321 | 321 | public function prepare_address_details( $invoice, $type = 'billing' ) { |
322 | 322 | |
323 | - $data = $this->payment_form_submission->get_data(); |
|
324 | - $type = sanitize_key( $type ); |
|
325 | - $address = array(); |
|
326 | - $prepared = array(); |
|
323 | + $data = $this->payment_form_submission->get_data(); |
|
324 | + $type = sanitize_key( $type ); |
|
325 | + $address = array(); |
|
326 | + $prepared = array(); |
|
327 | 327 | |
328 | - if ( ! empty( $data[ $type ] ) ) { |
|
329 | - $address = $data[ $type ]; |
|
330 | - } |
|
328 | + if ( ! empty( $data[ $type ] ) ) { |
|
329 | + $address = $data[ $type ]; |
|
330 | + } |
|
331 | 331 | |
332 | - // Clean address details. |
|
333 | - foreach ( $address as $key => $value ) { |
|
334 | - $key = sanitize_key( $key ); |
|
335 | - $key = str_replace( 'wpinv_', '', $key ); |
|
336 | - $value = wpinv_clean( $value ); |
|
337 | - $prepared[ $key ] = apply_filters( "getpaid_checkout_{$type}_address_$key", $value, $this->payment_form_submission, $invoice ); |
|
338 | - } |
|
332 | + // Clean address details. |
|
333 | + foreach ( $address as $key => $value ) { |
|
334 | + $key = sanitize_key( $key ); |
|
335 | + $key = str_replace( 'wpinv_', '', $key ); |
|
336 | + $value = wpinv_clean( $value ); |
|
337 | + $prepared[ $key ] = apply_filters( "getpaid_checkout_{$type}_address_$key", $value, $this->payment_form_submission, $invoice ); |
|
338 | + } |
|
339 | 339 | |
340 | - // Filter address details. |
|
341 | - $prepared = apply_filters( "getpaid_checkout_{$type}_address", $prepared, $this->payment_form_submission, $invoice ); |
|
340 | + // Filter address details. |
|
341 | + $prepared = apply_filters( "getpaid_checkout_{$type}_address", $prepared, $this->payment_form_submission, $invoice ); |
|
342 | 342 | |
343 | - // Remove non-whitelisted values. |
|
344 | - return array_filter( $prepared, 'getpaid_is_address_field_whitelisted', ARRAY_FILTER_USE_KEY ); |
|
343 | + // Remove non-whitelisted values. |
|
344 | + return array_filter( $prepared, 'getpaid_is_address_field_whitelisted', ARRAY_FILTER_USE_KEY ); |
|
345 | 345 | |
346 | - } |
|
346 | + } |
|
347 | 347 | |
348 | - /** |
|
348 | + /** |
|
349 | 349 | * Prepares the billing details. |
350 | 350 | * |
351 | - * @return array |
|
352 | - * @param WPInv_Invoice $invoice |
|
351 | + * @return array |
|
352 | + * @param WPInv_Invoice $invoice |
|
353 | 353 | */ |
354 | 354 | protected function prepare_billing_info( &$invoice ) { |
355 | 355 | |
356 | - $billing_address = $this->prepare_address_details( $invoice, 'billing' ); |
|
356 | + $billing_address = $this->prepare_address_details( $invoice, 'billing' ); |
|
357 | 357 | |
358 | - // Update the invoice with the billing details. |
|
359 | - $invoice->set_props( $billing_address ); |
|
358 | + // Update the invoice with the billing details. |
|
359 | + $invoice->set_props( $billing_address ); |
|
360 | 360 | |
361 | - } |
|
361 | + } |
|
362 | 362 | |
363 | - /** |
|
363 | + /** |
|
364 | 364 | * Prepares the shipping details. |
365 | 365 | * |
366 | - * @return array |
|
367 | - * @param WPInv_Invoice $invoice |
|
366 | + * @return array |
|
367 | + * @param WPInv_Invoice $invoice |
|
368 | 368 | */ |
369 | 369 | protected function prepare_shipping_info( $invoice ) { |
370 | 370 | |
371 | - $data = $this->payment_form_submission->get_data(); |
|
371 | + $data = $this->payment_form_submission->get_data(); |
|
372 | 372 | |
373 | - if ( empty( $data['same-shipping-address'] ) ) { |
|
374 | - return $this->prepare_address_details( $invoice, 'shipping' ); |
|
375 | - } |
|
373 | + if ( empty( $data['same-shipping-address'] ) ) { |
|
374 | + return $this->prepare_address_details( $invoice, 'shipping' ); |
|
375 | + } |
|
376 | 376 | |
377 | - return $this->prepare_address_details( $invoice, 'billing' ); |
|
377 | + return $this->prepare_address_details( $invoice, 'billing' ); |
|
378 | 378 | |
379 | - } |
|
379 | + } |
|
380 | 380 | |
381 | - /** |
|
382 | - * Confirms the submission is valid and send users to the gateway. |
|
383 | - * |
|
384 | - * @param WPInv_Invoice $invoice |
|
385 | - * @param array $prepared_payment_form_data |
|
386 | - * @param array $shipping |
|
387 | - */ |
|
388 | - protected function post_process_submission( $invoice, $prepared_payment_form_data, $shipping ) { |
|
381 | + /** |
|
382 | + * Confirms the submission is valid and send users to the gateway. |
|
383 | + * |
|
384 | + * @param WPInv_Invoice $invoice |
|
385 | + * @param array $prepared_payment_form_data |
|
386 | + * @param array $shipping |
|
387 | + */ |
|
388 | + protected function post_process_submission( $invoice, $prepared_payment_form_data, $shipping ) { |
|
389 | 389 | |
390 | - // Ensure the invoice exists. |
|
390 | + // Ensure the invoice exists. |
|
391 | 391 | if ( ! $invoice->exists() ) { |
392 | 392 | wp_send_json_error( __( 'An error occured while saving your invoice. Please try again.', 'invoicing' ) ); |
393 | 393 | } |
394 | 394 | |
395 | - // Save payment form data. |
|
396 | - $prepared_payment_form_data = apply_filters( 'getpaid_prepared_payment_form_data', $prepared_payment_form_data, $invoice ); |
|
395 | + // Save payment form data. |
|
396 | + $prepared_payment_form_data = apply_filters( 'getpaid_prepared_payment_form_data', $prepared_payment_form_data, $invoice ); |
|
397 | 397 | delete_post_meta( $invoice->get_id(), 'payment_form_data' ); |
398 | - delete_post_meta( $invoice->get_id(), 'additional_meta_data' ); |
|
399 | - if ( ! empty( $prepared_payment_form_data ) ) { |
|
398 | + delete_post_meta( $invoice->get_id(), 'additional_meta_data' ); |
|
399 | + if ( ! empty( $prepared_payment_form_data ) ) { |
|
400 | 400 | |
401 | - if ( ! empty( $prepared_payment_form_data['all'] ) ) { |
|
402 | - update_post_meta( $invoice->get_id(), 'payment_form_data', $prepared_payment_form_data['all'] ); |
|
403 | - } |
|
401 | + if ( ! empty( $prepared_payment_form_data['all'] ) ) { |
|
402 | + update_post_meta( $invoice->get_id(), 'payment_form_data', $prepared_payment_form_data['all'] ); |
|
403 | + } |
|
404 | 404 | |
405 | - if ( ! empty( $prepared_payment_form_data['meta'] ) ) { |
|
406 | - update_post_meta( $invoice->get_id(), 'additional_meta_data', $prepared_payment_form_data['meta'] ); |
|
407 | - } |
|
408 | - } |
|
405 | + if ( ! empty( $prepared_payment_form_data['meta'] ) ) { |
|
406 | + update_post_meta( $invoice->get_id(), 'additional_meta_data', $prepared_payment_form_data['meta'] ); |
|
407 | + } |
|
408 | + } |
|
409 | 409 | |
410 | - // Save payment form data. |
|
411 | - $shipping = apply_filters( 'getpaid_checkout_shipping_details', $shipping, $this->payment_form_submission ); |
|
410 | + // Save payment form data. |
|
411 | + $shipping = apply_filters( 'getpaid_checkout_shipping_details', $shipping, $this->payment_form_submission ); |
|
412 | 412 | if ( ! empty( $shipping ) ) { |
413 | 413 | update_post_meta( $invoice->get_id(), 'shipping_address', $shipping ); |
414 | - } |
|
414 | + } |
|
415 | 415 | |
416 | - // Backwards compatibility. |
|
416 | + // Backwards compatibility. |
|
417 | 417 | add_filter( 'wp_redirect', array( $this, 'send_redirect_response' ) ); |
418 | 418 | |
419 | - try { |
|
420 | - $this->process_payment( $invoice ); |
|
421 | - } catch ( Exception $e ) { |
|
422 | - wpinv_set_error( 'payment_error', $e->getMessage() ); |
|
423 | - } |
|
419 | + try { |
|
420 | + $this->process_payment( $invoice ); |
|
421 | + } catch ( Exception $e ) { |
|
422 | + wpinv_set_error( 'payment_error', $e->getMessage() ); |
|
423 | + } |
|
424 | 424 | |
425 | 425 | // If we are here, there was an error. |
426 | - wpinv_send_back_to_checkout( $invoice ); |
|
426 | + wpinv_send_back_to_checkout( $invoice ); |
|
427 | 427 | |
428 | - } |
|
428 | + } |
|
429 | 429 | |
430 | - /** |
|
431 | - * Processes the actual payment. |
|
432 | - * |
|
433 | - * @param WPInv_Invoice $invoice |
|
434 | - */ |
|
435 | - protected function process_payment( $invoice ) { |
|
430 | + /** |
|
431 | + * Processes the actual payment. |
|
432 | + * |
|
433 | + * @param WPInv_Invoice $invoice |
|
434 | + */ |
|
435 | + protected function process_payment( $invoice ) { |
|
436 | 436 | |
437 | - // Clear any checkout errors. |
|
438 | - wpinv_clear_errors(); |
|
437 | + // Clear any checkout errors. |
|
438 | + wpinv_clear_errors(); |
|
439 | 439 | |
440 | - // No need to send free invoices to the gateway. |
|
441 | - if ( $invoice->is_free() ) { |
|
442 | - $this->process_free_payment( $invoice ); |
|
443 | - } |
|
440 | + // No need to send free invoices to the gateway. |
|
441 | + if ( $invoice->is_free() ) { |
|
442 | + $this->process_free_payment( $invoice ); |
|
443 | + } |
|
444 | 444 | |
445 | - $submission = $this->payment_form_submission; |
|
445 | + $submission = $this->payment_form_submission; |
|
446 | 446 | |
447 | - // Fires before sending to the gateway. |
|
448 | - do_action( 'getpaid_checkout_before_gateway', $invoice, $submission ); |
|
447 | + // Fires before sending to the gateway. |
|
448 | + do_action( 'getpaid_checkout_before_gateway', $invoice, $submission ); |
|
449 | 449 | |
450 | - // Allow the sumission data to be modified before it is sent to the gateway. |
|
451 | - $submission_data = $submission->get_data(); |
|
452 | - $submission_gateway = apply_filters( 'getpaid_gateway_submission_gateway', $invoice->get_gateway(), $submission, $invoice ); |
|
453 | - $submission_data = apply_filters( 'getpaid_gateway_submission_data', $submission_data, $submission, $invoice ); |
|
450 | + // Allow the sumission data to be modified before it is sent to the gateway. |
|
451 | + $submission_data = $submission->get_data(); |
|
452 | + $submission_gateway = apply_filters( 'getpaid_gateway_submission_gateway', $invoice->get_gateway(), $submission, $invoice ); |
|
453 | + $submission_data = apply_filters( 'getpaid_gateway_submission_data', $submission_data, $submission, $invoice ); |
|
454 | 454 | |
455 | - // Validate the currency. |
|
456 | - if ( ! apply_filters( "getpaid_gateway_{$submission_gateway}_is_valid_for_currency", true, $invoice->get_currency() ) ) { |
|
457 | - wpinv_set_error( 'invalid_currency' ); |
|
458 | - } |
|
455 | + // Validate the currency. |
|
456 | + if ( ! apply_filters( "getpaid_gateway_{$submission_gateway}_is_valid_for_currency", true, $invoice->get_currency() ) ) { |
|
457 | + wpinv_set_error( 'invalid_currency' ); |
|
458 | + } |
|
459 | 459 | |
460 | - // Check to see if we have any errors. |
|
461 | - if ( wpinv_get_errors() ) { |
|
462 | - wpinv_send_back_to_checkout( $invoice ); |
|
463 | - } |
|
460 | + // Check to see if we have any errors. |
|
461 | + if ( wpinv_get_errors() ) { |
|
462 | + wpinv_send_back_to_checkout( $invoice ); |
|
463 | + } |
|
464 | 464 | |
465 | - // Send info to the gateway for payment processing |
|
466 | - do_action( "getpaid_gateway_$submission_gateway", $invoice, $submission_data, $submission ); |
|
465 | + // Send info to the gateway for payment processing |
|
466 | + do_action( "getpaid_gateway_$submission_gateway", $invoice, $submission_data, $submission ); |
|
467 | 467 | |
468 | - // Backwards compatibility. |
|
469 | - wpinv_send_to_gateway( $submission_gateway, $invoice ); |
|
468 | + // Backwards compatibility. |
|
469 | + wpinv_send_to_gateway( $submission_gateway, $invoice ); |
|
470 | 470 | |
471 | - } |
|
471 | + } |
|
472 | 472 | |
473 | - /** |
|
474 | - * Marks the invoice as paid in case the checkout is free. |
|
475 | - * |
|
476 | - * @param WPInv_Invoice $invoice |
|
477 | - */ |
|
478 | - protected function process_free_payment( $invoice ) { |
|
473 | + /** |
|
474 | + * Marks the invoice as paid in case the checkout is free. |
|
475 | + * |
|
476 | + * @param WPInv_Invoice $invoice |
|
477 | + */ |
|
478 | + protected function process_free_payment( $invoice ) { |
|
479 | 479 | |
480 | - $invoice->set_gateway( 'none' ); |
|
481 | - $invoice->add_note( __( "This is a free invoice and won't be sent to the payment gateway", 'invoicing' ), false, false, true ); |
|
482 | - $invoice->mark_paid(); |
|
483 | - wpinv_send_to_success_page( array( 'invoice_key' => $invoice->get_key() ) ); |
|
480 | + $invoice->set_gateway( 'none' ); |
|
481 | + $invoice->add_note( __( "This is a free invoice and won't be sent to the payment gateway", 'invoicing' ), false, false, true ); |
|
482 | + $invoice->mark_paid(); |
|
483 | + wpinv_send_to_success_page( array( 'invoice_key' => $invoice->get_key() ) ); |
|
484 | 484 | |
485 | - } |
|
485 | + } |
|
486 | 486 | |
487 | - /** |
|
487 | + /** |
|
488 | 488 | * Sends a redrect response to payment details. |
489 | 489 | * |
490 | 490 | */ |
@@ -4,7 +4,7 @@ discard block |
||
4 | 4 | * |
5 | 5 | */ |
6 | 6 | |
7 | -defined( 'ABSPATH' ) || exit; |
|
7 | +defined('ABSPATH') || exit; |
|
8 | 8 | |
9 | 9 | /** |
10 | 10 | * Main Checkout Class. |
@@ -22,7 +22,7 @@ discard block |
||
22 | 22 | * |
23 | 23 | * @param GetPaid_Payment_Form_Submission $submission |
24 | 24 | */ |
25 | - public function __construct( $submission ) { |
|
25 | + public function __construct($submission) { |
|
26 | 26 | $this->payment_form_submission = $submission; |
27 | 27 | } |
28 | 28 | |
@@ -38,22 +38,22 @@ discard block |
||
38 | 38 | // Prepare the invoice. |
39 | 39 | $items = $this->get_submission_items(); |
40 | 40 | $invoice = $this->get_submission_invoice(); |
41 | - $invoice = $this->process_submission_invoice( $invoice, $items ); |
|
41 | + $invoice = $this->process_submission_invoice($invoice, $items); |
|
42 | 42 | $prepared = $this->prepare_submission_data_for_saving(); |
43 | 43 | |
44 | - $this->prepare_billing_info( $invoice ); |
|
44 | + $this->prepare_billing_info($invoice); |
|
45 | 45 | |
46 | - $shipping = $this->prepare_shipping_info( $invoice ); |
|
46 | + $shipping = $this->prepare_shipping_info($invoice); |
|
47 | 47 | |
48 | 48 | // Save the invoice. |
49 | - $invoice->set_is_viewed( true ); |
|
49 | + $invoice->set_is_viewed(true); |
|
50 | 50 | $invoice->recalculate_total(); |
51 | 51 | $invoice->save(); |
52 | 52 | |
53 | - do_action( 'getpaid_checkout_invoice_updated', $invoice ); |
|
53 | + do_action('getpaid_checkout_invoice_updated', $invoice); |
|
54 | 54 | |
55 | 55 | // Send to the gateway. |
56 | - $this->post_process_submission( $invoice, $prepared, $shipping ); |
|
56 | + $this->post_process_submission($invoice, $prepared, $shipping); |
|
57 | 57 | } |
58 | 58 | |
59 | 59 | /** |
@@ -66,34 +66,34 @@ discard block |
||
66 | 66 | $data = $submission->get_data(); |
67 | 67 | |
68 | 68 | // Do we have an error? |
69 | - if ( ! empty( $submission->last_error ) ) { |
|
70 | - wp_send_json_error( $submission->last_error ); |
|
69 | + if (!empty($submission->last_error)) { |
|
70 | + wp_send_json_error($submission->last_error); |
|
71 | 71 | } |
72 | 72 | |
73 | 73 | // We need a billing email. |
74 | - if ( ! $submission->has_billing_email() ) { |
|
75 | - wp_send_json_error( __( 'Provide a valid billing email.', 'invoicing' ) ); |
|
74 | + if (!$submission->has_billing_email()) { |
|
75 | + wp_send_json_error(__('Provide a valid billing email.', 'invoicing')); |
|
76 | 76 | } |
77 | 77 | |
78 | 78 | // Non-recurring gateways should not be allowed to process recurring invoices. |
79 | - if ( $submission->should_collect_payment_details() && $submission->has_recurring && ! wpinv_gateway_support_subscription( $data['wpi-gateway'] ) ) { |
|
80 | - wp_send_json_error( __( 'The selected payment gateway does not support subscription payments.', 'invoicing' ) ); |
|
79 | + if ($submission->should_collect_payment_details() && $submission->has_recurring && !wpinv_gateway_support_subscription($data['wpi-gateway'])) { |
|
80 | + wp_send_json_error(__('The selected payment gateway does not support subscription payments.', 'invoicing')); |
|
81 | 81 | } |
82 | 82 | |
83 | 83 | // Ensure the gateway is active. |
84 | - if ( $submission->should_collect_payment_details() && ! wpinv_is_gateway_active( $data['wpi-gateway'] ) ) { |
|
85 | - wp_send_json_error( __( 'The selected payment gateway is not active', 'invoicing' ) ); |
|
84 | + if ($submission->should_collect_payment_details() && !wpinv_is_gateway_active($data['wpi-gateway'])) { |
|
85 | + wp_send_json_error(__('The selected payment gateway is not active', 'invoicing')); |
|
86 | 86 | } |
87 | 87 | |
88 | 88 | // Clear any existing errors. |
89 | 89 | wpinv_clear_errors(); |
90 | 90 | |
91 | 91 | // Allow themes and plugins to hook to errors |
92 | - do_action( 'getpaid_checkout_error_checks', $submission ); |
|
92 | + do_action('getpaid_checkout_error_checks', $submission); |
|
93 | 93 | |
94 | 94 | // Do we have any errors? |
95 | - if ( wpinv_get_errors() ) { |
|
96 | - wp_send_json_error( getpaid_get_errors_html() ); |
|
95 | + if (wpinv_get_errors()) { |
|
96 | + wp_send_json_error(getpaid_get_errors_html()); |
|
97 | 97 | } |
98 | 98 | |
99 | 99 | } |
@@ -108,8 +108,8 @@ discard block |
||
108 | 108 | $items = $this->payment_form_submission->get_items(); |
109 | 109 | |
110 | 110 | // Ensure that we have items. |
111 | - if ( empty( $items ) && ! $this->payment_form_submission->has_fees() ) { |
|
112 | - wp_send_json_error( __( 'Please provide at least one item or amount.', 'invoicing' ) ); |
|
111 | + if (empty($items) && !$this->payment_form_submission->has_fees()) { |
|
112 | + wp_send_json_error(__('Please provide at least one item or amount.', 'invoicing')); |
|
113 | 113 | } |
114 | 114 | |
115 | 115 | return $items; |
@@ -123,17 +123,17 @@ discard block |
||
123 | 123 | protected function get_submission_invoice() { |
124 | 124 | $submission = $this->payment_form_submission; |
125 | 125 | |
126 | - if ( ! $submission->has_invoice() ) { |
|
126 | + if (!$submission->has_invoice()) { |
|
127 | 127 | $invoice = new WPInv_Invoice(); |
128 | - $invoice->set_created_via( 'payment_form' ); |
|
128 | + $invoice->set_created_via('payment_form'); |
|
129 | 129 | return $invoice; |
130 | 130 | } |
131 | 131 | |
132 | 132 | $invoice = $submission->get_invoice(); |
133 | 133 | |
134 | 134 | // Make sure that it is neither paid or refunded. |
135 | - if ( $invoice->is_paid() || $invoice->is_refunded() ) { |
|
136 | - wp_send_json_error( __( 'This invoice has already been paid for.', 'invoicing' ) ); |
|
135 | + if ($invoice->is_paid() || $invoice->is_refunded()) { |
|
136 | + wp_send_json_error(__('This invoice has already been paid for.', 'invoicing')); |
|
137 | 137 | } |
138 | 138 | |
139 | 139 | return $invoice; |
@@ -146,34 +146,34 @@ discard block |
||
146 | 146 | * @param GetPaid_Form_Item[] $items |
147 | 147 | * @return WPInv_Invoice |
148 | 148 | */ |
149 | - protected function process_submission_invoice( $invoice, $items ) { |
|
149 | + protected function process_submission_invoice($invoice, $items) { |
|
150 | 150 | |
151 | 151 | $submission = $this->payment_form_submission; |
152 | 152 | |
153 | 153 | // Set-up the invoice details. |
154 | - $invoice->set_email( sanitize_email( $submission->get_billing_email() ) ); |
|
155 | - $invoice->set_user_id( $this->get_submission_customer() ); |
|
156 | - $invoice->set_submission_id( $submission->id ); |
|
157 | - $invoice->set_payment_form( absint( $submission->get_payment_form()->get_id() ) ); |
|
158 | - $invoice->set_items( $items ); |
|
159 | - $invoice->set_fees( $submission->get_fees() ); |
|
160 | - $invoice->set_taxes( $submission->get_taxes() ); |
|
161 | - $invoice->set_discounts( $submission->get_discounts() ); |
|
162 | - $invoice->set_gateway( $submission->get_field( 'wpi-gateway' ) ); |
|
163 | - $invoice->set_currency( $submission->get_currency() ); |
|
164 | - |
|
165 | - if ( $submission->has_shipping() ) { |
|
166 | - $invoice->set_shipping( $submission->get_shipping() ); |
|
154 | + $invoice->set_email(sanitize_email($submission->get_billing_email())); |
|
155 | + $invoice->set_user_id($this->get_submission_customer()); |
|
156 | + $invoice->set_submission_id($submission->id); |
|
157 | + $invoice->set_payment_form(absint($submission->get_payment_form()->get_id())); |
|
158 | + $invoice->set_items($items); |
|
159 | + $invoice->set_fees($submission->get_fees()); |
|
160 | + $invoice->set_taxes($submission->get_taxes()); |
|
161 | + $invoice->set_discounts($submission->get_discounts()); |
|
162 | + $invoice->set_gateway($submission->get_field('wpi-gateway')); |
|
163 | + $invoice->set_currency($submission->get_currency()); |
|
164 | + |
|
165 | + if ($submission->has_shipping()) { |
|
166 | + $invoice->set_shipping($submission->get_shipping()); |
|
167 | 167 | } |
168 | 168 | |
169 | - $address_confirmed = $submission->get_field( 'confirm-address' ); |
|
170 | - $invoice->set_address_confirmed( ! empty( $address_confirmed ) ); |
|
169 | + $address_confirmed = $submission->get_field('confirm-address'); |
|
170 | + $invoice->set_address_confirmed(!empty($address_confirmed)); |
|
171 | 171 | |
172 | - if ( $submission->has_discount_code() ) { |
|
173 | - $invoice->set_discount_code( $submission->get_discount_code() ); |
|
172 | + if ($submission->has_discount_code()) { |
|
173 | + $invoice->set_discount_code($submission->get_discount_code()); |
|
174 | 174 | } |
175 | 175 | |
176 | - getpaid_maybe_add_default_address( $invoice ); |
|
176 | + getpaid_maybe_add_default_address($invoice); |
|
177 | 177 | return $invoice; |
178 | 178 | } |
179 | 179 | |
@@ -186,34 +186,34 @@ discard block |
||
186 | 186 | $submission = $this->payment_form_submission; |
187 | 187 | |
188 | 188 | // If this is an existing invoice... |
189 | - if ( $submission->has_invoice() ) { |
|
189 | + if ($submission->has_invoice()) { |
|
190 | 190 | return $submission->get_invoice()->get_user_id(); |
191 | 191 | } |
192 | 192 | |
193 | 193 | // (Maybe) create the user. |
194 | 194 | $user = get_current_user_id(); |
195 | 195 | |
196 | - if ( empty( $user ) ) { |
|
197 | - $user = get_user_by( 'email', $submission->get_billing_email() ); |
|
196 | + if (empty($user)) { |
|
197 | + $user = get_user_by('email', $submission->get_billing_email()); |
|
198 | 198 | } |
199 | 199 | |
200 | - if ( empty( $user ) ) { |
|
201 | - $name = array( $submission->get_field( 'wpinv_first_name', 'billing' ), $submission->get_field( 'wpinv_last_name', 'billing' ) ); |
|
202 | - $name = implode( '', array_filter( $name ) ); |
|
203 | - $user = wpinv_create_user( $submission->get_billing_email(), $name ); |
|
200 | + if (empty($user)) { |
|
201 | + $name = array($submission->get_field('wpinv_first_name', 'billing'), $submission->get_field('wpinv_last_name', 'billing')); |
|
202 | + $name = implode('', array_filter($name)); |
|
203 | + $user = wpinv_create_user($submission->get_billing_email(), $name); |
|
204 | 204 | |
205 | 205 | // (Maybe) send new user notification. |
206 | - $should_send_notification = wpinv_get_option( 'disable_new_user_emails' ); |
|
207 | - if ( ! empty( $user ) && is_numeric( $user ) && apply_filters( 'getpaid_send_new_user_notification', empty( $should_send_notification ), $user ) ) { |
|
208 | - wp_send_new_user_notifications( $user, 'user' ); |
|
206 | + $should_send_notification = wpinv_get_option('disable_new_user_emails'); |
|
207 | + if (!empty($user) && is_numeric($user) && apply_filters('getpaid_send_new_user_notification', empty($should_send_notification), $user)) { |
|
208 | + wp_send_new_user_notifications($user, 'user'); |
|
209 | 209 | } |
210 | 210 | } |
211 | 211 | |
212 | - if ( is_wp_error( $user ) ) { |
|
213 | - wp_send_json_error( $user->get_error_message() ); |
|
212 | + if (is_wp_error($user)) { |
|
213 | + wp_send_json_error($user->get_error_message()); |
|
214 | 214 | } |
215 | 215 | |
216 | - if ( is_numeric( $user ) ) { |
|
216 | + if (is_numeric($user)) { |
|
217 | 217 | return $user; |
218 | 218 | } |
219 | 219 | |
@@ -237,72 +237,72 @@ discard block |
||
237 | 237 | ); |
238 | 238 | |
239 | 239 | // Raw submission details. |
240 | - $data = $submission->get_data(); |
|
240 | + $data = $submission->get_data(); |
|
241 | 241 | |
242 | 242 | // Loop through the submitted details. |
243 | - foreach ( $submission->get_payment_form()->get_elements() as $field ) { |
|
243 | + foreach ($submission->get_payment_form()->get_elements() as $field) { |
|
244 | 244 | |
245 | 245 | // Skip premade fields. |
246 | - if ( ! empty( $field['premade'] ) ) { |
|
246 | + if (!empty($field['premade'])) { |
|
247 | 247 | continue; |
248 | 248 | } |
249 | 249 | |
250 | 250 | // Ensure address is provided. |
251 | - if ( 'address' === $field['type'] ) { |
|
252 | - $address_type = isset( $field['address_type'] ) && 'shipping' === $field['address_type'] ? 'shipping' : 'billing'; |
|
251 | + if ('address' === $field['type']) { |
|
252 | + $address_type = isset($field['address_type']) && 'shipping' === $field['address_type'] ? 'shipping' : 'billing'; |
|
253 | 253 | |
254 | - foreach ( $field['fields'] as $address_field ) { |
|
254 | + foreach ($field['fields'] as $address_field) { |
|
255 | 255 | |
256 | - if ( ! empty( $address_field['visible'] ) && ! empty( $address_field['required'] ) && '' === trim( $_POST[ $address_type ][ $address_field['name'] ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
257 | - wp_send_json_error( __( 'Please fill all required fields.', 'invoicing' ) ); |
|
256 | + if (!empty($address_field['visible']) && !empty($address_field['required']) && '' === trim($_POST[$address_type][$address_field['name']])) { // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
257 | + wp_send_json_error(__('Please fill all required fields.', 'invoicing')); |
|
258 | 258 | } |
259 | 259 | } |
260 | 260 | } |
261 | 261 | |
262 | 262 | // If it is required and not set, abort. |
263 | - if ( ! $submission->is_required_field_set( $field ) ) { |
|
264 | - wp_send_json_error( __( 'Please fill all required fields.', 'invoicing' ) ); |
|
263 | + if (!$submission->is_required_field_set($field)) { |
|
264 | + wp_send_json_error(__('Please fill all required fields.', 'invoicing')); |
|
265 | 265 | } |
266 | 266 | |
267 | 267 | // Handle misc fields. |
268 | - if ( isset( $data[ $field['id'] ] ) ) { |
|
268 | + if (isset($data[$field['id']])) { |
|
269 | 269 | |
270 | 270 | // Uploads. |
271 | - if ( 'file_upload' === $field['type'] ) { |
|
272 | - $max_file_num = empty( $field['max_file_num'] ) ? 1 : absint( $field['max_file_num'] ); |
|
271 | + if ('file_upload' === $field['type']) { |
|
272 | + $max_file_num = empty($field['max_file_num']) ? 1 : absint($field['max_file_num']); |
|
273 | 273 | |
274 | - if ( count( $data[ $field['id'] ] ) > $max_file_num ) { |
|
275 | - wp_send_json_error( __( 'Maximum number of allowed files exceeded.', 'invoicing' ) ); |
|
274 | + if (count($data[$field['id']]) > $max_file_num) { |
|
275 | + wp_send_json_error(__('Maximum number of allowed files exceeded.', 'invoicing')); |
|
276 | 276 | } |
277 | 277 | |
278 | 278 | $value = array(); |
279 | 279 | |
280 | - foreach ( $data[ $field['id'] ] as $url => $name ) { |
|
280 | + foreach ($data[$field['id']] as $url => $name) { |
|
281 | 281 | $value[] = sprintf( |
282 | 282 | '<a href="%s" target="_blank">%s</a>', |
283 | - esc_url_raw( $url ), |
|
284 | - esc_html( $name ) |
|
283 | + esc_url_raw($url), |
|
284 | + esc_html($name) |
|
285 | 285 | ); |
286 | 286 | } |
287 | 287 | |
288 | - $value = implode( ' | ', $value ); |
|
288 | + $value = implode(' | ', $value); |
|
289 | 289 | |
290 | - } elseif ( 'checkbox' === $field['type'] ) { |
|
291 | - $value = ! empty( $data[ $field['id'] ] ) ? __( 'Yes', 'invoicing' ) : __( 'No', 'invoicing' ); |
|
290 | + } elseif ('checkbox' === $field['type']) { |
|
291 | + $value = !empty($data[$field['id']]) ? __('Yes', 'invoicing') : __('No', 'invoicing'); |
|
292 | 292 | } else { |
293 | - $value = wp_kses_post( $data[ $field['id'] ] ); |
|
293 | + $value = wp_kses_post($data[$field['id']]); |
|
294 | 294 | } |
295 | 295 | |
296 | 296 | $label = $field['id']; |
297 | 297 | |
298 | - if ( isset( $field['label'] ) ) { |
|
298 | + if (isset($field['label'])) { |
|
299 | 299 | $label = $field['label']; |
300 | 300 | } |
301 | 301 | |
302 | - if ( ! empty( $field['add_meta'] ) ) { |
|
303 | - $prepared['meta'][ wpinv_clean( $label ) ] = wp_kses_post_deep( $value ); |
|
302 | + if (!empty($field['add_meta'])) { |
|
303 | + $prepared['meta'][wpinv_clean($label)] = wp_kses_post_deep($value); |
|
304 | 304 | } |
305 | - $prepared['all'][ wpinv_clean( $label ) ] = wp_kses_post_deep( $value ); |
|
305 | + $prepared['all'][wpinv_clean($label)] = wp_kses_post_deep($value); |
|
306 | 306 | |
307 | 307 | } |
308 | 308 | } |
@@ -318,30 +318,30 @@ discard block |
||
318 | 318 | * @param WPInv_Invoice $invoice |
319 | 319 | * @param string $type |
320 | 320 | */ |
321 | - public function prepare_address_details( $invoice, $type = 'billing' ) { |
|
321 | + public function prepare_address_details($invoice, $type = 'billing') { |
|
322 | 322 | |
323 | 323 | $data = $this->payment_form_submission->get_data(); |
324 | - $type = sanitize_key( $type ); |
|
324 | + $type = sanitize_key($type); |
|
325 | 325 | $address = array(); |
326 | 326 | $prepared = array(); |
327 | 327 | |
328 | - if ( ! empty( $data[ $type ] ) ) { |
|
329 | - $address = $data[ $type ]; |
|
328 | + if (!empty($data[$type])) { |
|
329 | + $address = $data[$type]; |
|
330 | 330 | } |
331 | 331 | |
332 | 332 | // Clean address details. |
333 | - foreach ( $address as $key => $value ) { |
|
334 | - $key = sanitize_key( $key ); |
|
335 | - $key = str_replace( 'wpinv_', '', $key ); |
|
336 | - $value = wpinv_clean( $value ); |
|
337 | - $prepared[ $key ] = apply_filters( "getpaid_checkout_{$type}_address_$key", $value, $this->payment_form_submission, $invoice ); |
|
333 | + foreach ($address as $key => $value) { |
|
334 | + $key = sanitize_key($key); |
|
335 | + $key = str_replace('wpinv_', '', $key); |
|
336 | + $value = wpinv_clean($value); |
|
337 | + $prepared[$key] = apply_filters("getpaid_checkout_{$type}_address_$key", $value, $this->payment_form_submission, $invoice); |
|
338 | 338 | } |
339 | 339 | |
340 | 340 | // Filter address details. |
341 | - $prepared = apply_filters( "getpaid_checkout_{$type}_address", $prepared, $this->payment_form_submission, $invoice ); |
|
341 | + $prepared = apply_filters("getpaid_checkout_{$type}_address", $prepared, $this->payment_form_submission, $invoice); |
|
342 | 342 | |
343 | 343 | // Remove non-whitelisted values. |
344 | - return array_filter( $prepared, 'getpaid_is_address_field_whitelisted', ARRAY_FILTER_USE_KEY ); |
|
344 | + return array_filter($prepared, 'getpaid_is_address_field_whitelisted', ARRAY_FILTER_USE_KEY); |
|
345 | 345 | |
346 | 346 | } |
347 | 347 | |
@@ -351,12 +351,12 @@ discard block |
||
351 | 351 | * @return array |
352 | 352 | * @param WPInv_Invoice $invoice |
353 | 353 | */ |
354 | - protected function prepare_billing_info( &$invoice ) { |
|
354 | + protected function prepare_billing_info(&$invoice) { |
|
355 | 355 | |
356 | - $billing_address = $this->prepare_address_details( $invoice, 'billing' ); |
|
356 | + $billing_address = $this->prepare_address_details($invoice, 'billing'); |
|
357 | 357 | |
358 | 358 | // Update the invoice with the billing details. |
359 | - $invoice->set_props( $billing_address ); |
|
359 | + $invoice->set_props($billing_address); |
|
360 | 360 | |
361 | 361 | } |
362 | 362 | |
@@ -366,15 +366,15 @@ discard block |
||
366 | 366 | * @return array |
367 | 367 | * @param WPInv_Invoice $invoice |
368 | 368 | */ |
369 | - protected function prepare_shipping_info( $invoice ) { |
|
369 | + protected function prepare_shipping_info($invoice) { |
|
370 | 370 | |
371 | 371 | $data = $this->payment_form_submission->get_data(); |
372 | 372 | |
373 | - if ( empty( $data['same-shipping-address'] ) ) { |
|
374 | - return $this->prepare_address_details( $invoice, 'shipping' ); |
|
373 | + if (empty($data['same-shipping-address'])) { |
|
374 | + return $this->prepare_address_details($invoice, 'shipping'); |
|
375 | 375 | } |
376 | 376 | |
377 | - return $this->prepare_address_details( $invoice, 'billing' ); |
|
377 | + return $this->prepare_address_details($invoice, 'billing'); |
|
378 | 378 | |
379 | 379 | } |
380 | 380 | |
@@ -385,45 +385,45 @@ discard block |
||
385 | 385 | * @param array $prepared_payment_form_data |
386 | 386 | * @param array $shipping |
387 | 387 | */ |
388 | - protected function post_process_submission( $invoice, $prepared_payment_form_data, $shipping ) { |
|
388 | + protected function post_process_submission($invoice, $prepared_payment_form_data, $shipping) { |
|
389 | 389 | |
390 | 390 | // Ensure the invoice exists. |
391 | - if ( ! $invoice->exists() ) { |
|
392 | - wp_send_json_error( __( 'An error occured while saving your invoice. Please try again.', 'invoicing' ) ); |
|
391 | + if (!$invoice->exists()) { |
|
392 | + wp_send_json_error(__('An error occured while saving your invoice. Please try again.', 'invoicing')); |
|
393 | 393 | } |
394 | 394 | |
395 | 395 | // Save payment form data. |
396 | - $prepared_payment_form_data = apply_filters( 'getpaid_prepared_payment_form_data', $prepared_payment_form_data, $invoice ); |
|
397 | - delete_post_meta( $invoice->get_id(), 'payment_form_data' ); |
|
398 | - delete_post_meta( $invoice->get_id(), 'additional_meta_data' ); |
|
399 | - if ( ! empty( $prepared_payment_form_data ) ) { |
|
396 | + $prepared_payment_form_data = apply_filters('getpaid_prepared_payment_form_data', $prepared_payment_form_data, $invoice); |
|
397 | + delete_post_meta($invoice->get_id(), 'payment_form_data'); |
|
398 | + delete_post_meta($invoice->get_id(), 'additional_meta_data'); |
|
399 | + if (!empty($prepared_payment_form_data)) { |
|
400 | 400 | |
401 | - if ( ! empty( $prepared_payment_form_data['all'] ) ) { |
|
402 | - update_post_meta( $invoice->get_id(), 'payment_form_data', $prepared_payment_form_data['all'] ); |
|
401 | + if (!empty($prepared_payment_form_data['all'])) { |
|
402 | + update_post_meta($invoice->get_id(), 'payment_form_data', $prepared_payment_form_data['all']); |
|
403 | 403 | } |
404 | 404 | |
405 | - if ( ! empty( $prepared_payment_form_data['meta'] ) ) { |
|
406 | - update_post_meta( $invoice->get_id(), 'additional_meta_data', $prepared_payment_form_data['meta'] ); |
|
405 | + if (!empty($prepared_payment_form_data['meta'])) { |
|
406 | + update_post_meta($invoice->get_id(), 'additional_meta_data', $prepared_payment_form_data['meta']); |
|
407 | 407 | } |
408 | 408 | } |
409 | 409 | |
410 | 410 | // Save payment form data. |
411 | - $shipping = apply_filters( 'getpaid_checkout_shipping_details', $shipping, $this->payment_form_submission ); |
|
412 | - if ( ! empty( $shipping ) ) { |
|
413 | - update_post_meta( $invoice->get_id(), 'shipping_address', $shipping ); |
|
411 | + $shipping = apply_filters('getpaid_checkout_shipping_details', $shipping, $this->payment_form_submission); |
|
412 | + if (!empty($shipping)) { |
|
413 | + update_post_meta($invoice->get_id(), 'shipping_address', $shipping); |
|
414 | 414 | } |
415 | 415 | |
416 | 416 | // Backwards compatibility. |
417 | - add_filter( 'wp_redirect', array( $this, 'send_redirect_response' ) ); |
|
417 | + add_filter('wp_redirect', array($this, 'send_redirect_response')); |
|
418 | 418 | |
419 | 419 | try { |
420 | - $this->process_payment( $invoice ); |
|
421 | - } catch ( Exception $e ) { |
|
422 | - wpinv_set_error( 'payment_error', $e->getMessage() ); |
|
420 | + $this->process_payment($invoice); |
|
421 | + } catch (Exception $e) { |
|
422 | + wpinv_set_error('payment_error', $e->getMessage()); |
|
423 | 423 | } |
424 | 424 | |
425 | 425 | // If we are here, there was an error. |
426 | - wpinv_send_back_to_checkout( $invoice ); |
|
426 | + wpinv_send_back_to_checkout($invoice); |
|
427 | 427 | |
428 | 428 | } |
429 | 429 | |
@@ -432,41 +432,41 @@ discard block |
||
432 | 432 | * |
433 | 433 | * @param WPInv_Invoice $invoice |
434 | 434 | */ |
435 | - protected function process_payment( $invoice ) { |
|
435 | + protected function process_payment($invoice) { |
|
436 | 436 | |
437 | 437 | // Clear any checkout errors. |
438 | 438 | wpinv_clear_errors(); |
439 | 439 | |
440 | 440 | // No need to send free invoices to the gateway. |
441 | - if ( $invoice->is_free() ) { |
|
442 | - $this->process_free_payment( $invoice ); |
|
441 | + if ($invoice->is_free()) { |
|
442 | + $this->process_free_payment($invoice); |
|
443 | 443 | } |
444 | 444 | |
445 | 445 | $submission = $this->payment_form_submission; |
446 | 446 | |
447 | 447 | // Fires before sending to the gateway. |
448 | - do_action( 'getpaid_checkout_before_gateway', $invoice, $submission ); |
|
448 | + do_action('getpaid_checkout_before_gateway', $invoice, $submission); |
|
449 | 449 | |
450 | 450 | // Allow the sumission data to be modified before it is sent to the gateway. |
451 | 451 | $submission_data = $submission->get_data(); |
452 | - $submission_gateway = apply_filters( 'getpaid_gateway_submission_gateway', $invoice->get_gateway(), $submission, $invoice ); |
|
453 | - $submission_data = apply_filters( 'getpaid_gateway_submission_data', $submission_data, $submission, $invoice ); |
|
452 | + $submission_gateway = apply_filters('getpaid_gateway_submission_gateway', $invoice->get_gateway(), $submission, $invoice); |
|
453 | + $submission_data = apply_filters('getpaid_gateway_submission_data', $submission_data, $submission, $invoice); |
|
454 | 454 | |
455 | 455 | // Validate the currency. |
456 | - if ( ! apply_filters( "getpaid_gateway_{$submission_gateway}_is_valid_for_currency", true, $invoice->get_currency() ) ) { |
|
457 | - wpinv_set_error( 'invalid_currency' ); |
|
456 | + if (!apply_filters("getpaid_gateway_{$submission_gateway}_is_valid_for_currency", true, $invoice->get_currency())) { |
|
457 | + wpinv_set_error('invalid_currency'); |
|
458 | 458 | } |
459 | 459 | |
460 | 460 | // Check to see if we have any errors. |
461 | - if ( wpinv_get_errors() ) { |
|
462 | - wpinv_send_back_to_checkout( $invoice ); |
|
461 | + if (wpinv_get_errors()) { |
|
462 | + wpinv_send_back_to_checkout($invoice); |
|
463 | 463 | } |
464 | 464 | |
465 | 465 | // Send info to the gateway for payment processing |
466 | - do_action( "getpaid_gateway_$submission_gateway", $invoice, $submission_data, $submission ); |
|
466 | + do_action("getpaid_gateway_$submission_gateway", $invoice, $submission_data, $submission); |
|
467 | 467 | |
468 | 468 | // Backwards compatibility. |
469 | - wpinv_send_to_gateway( $submission_gateway, $invoice ); |
|
469 | + wpinv_send_to_gateway($submission_gateway, $invoice); |
|
470 | 470 | |
471 | 471 | } |
472 | 472 | |
@@ -475,12 +475,12 @@ discard block |
||
475 | 475 | * |
476 | 476 | * @param WPInv_Invoice $invoice |
477 | 477 | */ |
478 | - protected function process_free_payment( $invoice ) { |
|
478 | + protected function process_free_payment($invoice) { |
|
479 | 479 | |
480 | - $invoice->set_gateway( 'none' ); |
|
481 | - $invoice->add_note( __( "This is a free invoice and won't be sent to the payment gateway", 'invoicing' ), false, false, true ); |
|
480 | + $invoice->set_gateway('none'); |
|
481 | + $invoice->add_note(__("This is a free invoice and won't be sent to the payment gateway", 'invoicing'), false, false, true); |
|
482 | 482 | $invoice->mark_paid(); |
483 | - wpinv_send_to_success_page( array( 'invoice_key' => $invoice->get_key() ) ); |
|
483 | + wpinv_send_to_success_page(array('invoice_key' => $invoice->get_key())); |
|
484 | 484 | |
485 | 485 | } |
486 | 486 | |
@@ -488,9 +488,9 @@ discard block |
||
488 | 488 | * Sends a redrect response to payment details. |
489 | 489 | * |
490 | 490 | */ |
491 | - public function send_redirect_response( $url ) { |
|
492 | - $url = rawurlencode( $url ); |
|
493 | - wp_send_json_success( $url ); |
|
491 | + public function send_redirect_response($url) { |
|
492 | + $url = rawurlencode($url); |
|
493 | + wp_send_json_success($url); |
|
494 | 494 | } |
495 | 495 | |
496 | 496 | } |