@@ -12,119 +12,119 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class WPInv_Notes { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * Class constructor. |
|
| 17 | - */ |
|
| 18 | - public function __construct() { |
|
| 19 | - |
|
| 20 | - // Filter inovice notes. |
|
| 21 | - add_action( 'pre_get_comments', array( $this, 'set_invoice_note_type' ), 11, 1 ); |
|
| 22 | - add_action( 'comment_feed_where', array( $this, 'wpinv_comment_feed_where' ), 10, 1 ); |
|
| 23 | - |
|
| 24 | - // Fires after notes are loaded. |
|
| 25 | - do_action( 'wpinv_notes_init', $this ); |
|
| 26 | - } |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * Filters invoice notes query to only include our notes. |
|
| 30 | - * |
|
| 31 | - * @param WP_Comment_Query $query |
|
| 32 | - */ |
|
| 33 | - public function set_invoice_note_type( $query ) { |
|
| 34 | - $post_id = ! empty( $query->query_vars['post_ID'] ) ? $query->query_vars['post_ID'] : $query->query_vars['post_id']; |
|
| 35 | - |
|
| 36 | - if ( $post_id && getpaid_is_invoice_post_type( get_post_type( $post_id ) ) ) { |
|
| 37 | - $query->query_vars['type'] = 'wpinv_note'; |
|
| 38 | - } else { |
|
| 39 | - |
|
| 40 | - if ( empty( $query->query_vars['type__not_in'] ) ) { |
|
| 41 | - $query->query_vars['type__not_in'] = array(); |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - $query->query_vars['type__not_in'] = wpinv_parse_list( $query->query_vars['type__not_in'] ); |
|
| 45 | - $query->query_vars['type__not_in'] = array_merge( array( 'wpinv_note' ), $query->query_vars['type__not_in'] ); |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - return $query; |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * Exclude notes from the comments feed. |
|
| 53 | - */ |
|
| 54 | - function wpinv_comment_feed_where( $where ){ |
|
| 55 | - return $where . ( $where ? ' AND ' : '' ) . " comment_type != 'wpinv_note' "; |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * Returns an array of invoice notes. |
|
| 60 | - * |
|
| 61 | - * @param int $invoice_id The invoice ID whose notes to retrieve. |
|
| 62 | - * @param string $type Optional. Pass in customer to only return customer notes. |
|
| 63 | - * @return WP_Comment[] |
|
| 64 | - */ |
|
| 65 | - public function get_invoice_notes( $invoice_id = 0, $type = 'all' ) { |
|
| 66 | - |
|
| 67 | - // Default comment args. |
|
| 68 | - $args = array( |
|
| 69 | - 'post_id' => $invoice_id, |
|
| 70 | - 'orderby' => 'comment_ID', |
|
| 71 | - 'order' => 'ASC', |
|
| 72 | - ); |
|
| 73 | - |
|
| 74 | - // Maybe only show customer comments. |
|
| 75 | - if ( $type == 'customer' ) { |
|
| 76 | - $args['meta_key'] = '_wpi_customer_note'; |
|
| 77 | - $args['meta_value'] = 1; |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - $args = apply_filters( 'wpinv_invoice_notes_args', $args, $this, $invoice_id, $type ); |
|
| 81 | - |
|
| 82 | - return get_comments( $args ); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * Saves an invoice comment. |
|
| 87 | - * |
|
| 88 | - * @param WPInv_Invoice $invoice The invoice to add the comment to. |
|
| 89 | - * @param string $note The note content. |
|
| 90 | - * @param string $note_author The name of the author of the note. |
|
| 91 | - * @param bool $for_customer Whether or not this comment is meant to be sent to the customer. |
|
| 92 | - * @return int|false The new note's ID on success, false on failure. |
|
| 93 | - */ |
|
| 94 | - function add_invoice_note( $invoice, $note, $note_author, $author_email, $for_customer = false ){ |
|
| 95 | - |
|
| 96 | - do_action( 'wpinv_pre_insert_invoice_note', $invoice->get_id(), $note, $for_customer ); |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * Insert the comment. |
|
| 100 | - */ |
|
| 101 | - $note_id = wp_insert_comment( |
|
| 102 | - wp_filter_comment( |
|
| 103 | - array( |
|
| 104 | - 'comment_post_ID' => $invoice->get_id(), |
|
| 105 | - 'comment_content' => $note, |
|
| 106 | - 'comment_agent' => 'Invoicing', |
|
| 107 | - 'user_id' => get_current_user_id(), |
|
| 108 | - 'comment_author' => $note_author, |
|
| 109 | - 'comment_author_IP' => wpinv_get_ip(), |
|
| 110 | - 'comment_author_email' => $author_email, |
|
| 111 | - 'comment_author_url' => $invoice->get_view_url(), |
|
| 112 | - 'comment_type' => 'wpinv_note', |
|
| 113 | - ) |
|
| 114 | - ) |
|
| 115 | - ); |
|
| 116 | - |
|
| 117 | - do_action( 'wpinv_insert_payment_note', $note_id, $invoice->get_id(), $note, $for_customer ); |
|
| 118 | - |
|
| 119 | - // Are we notifying the customer? |
|
| 120 | - if ( empty( $note_id ) || empty( $for_customer ) ) { |
|
| 121 | - return $note_id; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - add_comment_meta( $note_id, '_wpi_customer_note', 1 ); |
|
| 125 | - do_action( 'wpinv_new_customer_note', array( 'invoice_id' => $invoice->get_id(), 'user_note' => $note ) ); |
|
| 126 | - do_action( 'getpaid_new_customer_note', $invoice, $note ); |
|
| 127 | - return $note_id; |
|
| 128 | - } |
|
| 15 | + /** |
|
| 16 | + * Class constructor. |
|
| 17 | + */ |
|
| 18 | + public function __construct() { |
|
| 19 | + |
|
| 20 | + // Filter inovice notes. |
|
| 21 | + add_action( 'pre_get_comments', array( $this, 'set_invoice_note_type' ), 11, 1 ); |
|
| 22 | + add_action( 'comment_feed_where', array( $this, 'wpinv_comment_feed_where' ), 10, 1 ); |
|
| 23 | + |
|
| 24 | + // Fires after notes are loaded. |
|
| 25 | + do_action( 'wpinv_notes_init', $this ); |
|
| 26 | + } |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * Filters invoice notes query to only include our notes. |
|
| 30 | + * |
|
| 31 | + * @param WP_Comment_Query $query |
|
| 32 | + */ |
|
| 33 | + public function set_invoice_note_type( $query ) { |
|
| 34 | + $post_id = ! empty( $query->query_vars['post_ID'] ) ? $query->query_vars['post_ID'] : $query->query_vars['post_id']; |
|
| 35 | + |
|
| 36 | + if ( $post_id && getpaid_is_invoice_post_type( get_post_type( $post_id ) ) ) { |
|
| 37 | + $query->query_vars['type'] = 'wpinv_note'; |
|
| 38 | + } else { |
|
| 39 | + |
|
| 40 | + if ( empty( $query->query_vars['type__not_in'] ) ) { |
|
| 41 | + $query->query_vars['type__not_in'] = array(); |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + $query->query_vars['type__not_in'] = wpinv_parse_list( $query->query_vars['type__not_in'] ); |
|
| 45 | + $query->query_vars['type__not_in'] = array_merge( array( 'wpinv_note' ), $query->query_vars['type__not_in'] ); |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + return $query; |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * Exclude notes from the comments feed. |
|
| 53 | + */ |
|
| 54 | + function wpinv_comment_feed_where( $where ){ |
|
| 55 | + return $where . ( $where ? ' AND ' : '' ) . " comment_type != 'wpinv_note' "; |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * Returns an array of invoice notes. |
|
| 60 | + * |
|
| 61 | + * @param int $invoice_id The invoice ID whose notes to retrieve. |
|
| 62 | + * @param string $type Optional. Pass in customer to only return customer notes. |
|
| 63 | + * @return WP_Comment[] |
|
| 64 | + */ |
|
| 65 | + public function get_invoice_notes( $invoice_id = 0, $type = 'all' ) { |
|
| 66 | + |
|
| 67 | + // Default comment args. |
|
| 68 | + $args = array( |
|
| 69 | + 'post_id' => $invoice_id, |
|
| 70 | + 'orderby' => 'comment_ID', |
|
| 71 | + 'order' => 'ASC', |
|
| 72 | + ); |
|
| 73 | + |
|
| 74 | + // Maybe only show customer comments. |
|
| 75 | + if ( $type == 'customer' ) { |
|
| 76 | + $args['meta_key'] = '_wpi_customer_note'; |
|
| 77 | + $args['meta_value'] = 1; |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + $args = apply_filters( 'wpinv_invoice_notes_args', $args, $this, $invoice_id, $type ); |
|
| 81 | + |
|
| 82 | + return get_comments( $args ); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * Saves an invoice comment. |
|
| 87 | + * |
|
| 88 | + * @param WPInv_Invoice $invoice The invoice to add the comment to. |
|
| 89 | + * @param string $note The note content. |
|
| 90 | + * @param string $note_author The name of the author of the note. |
|
| 91 | + * @param bool $for_customer Whether or not this comment is meant to be sent to the customer. |
|
| 92 | + * @return int|false The new note's ID on success, false on failure. |
|
| 93 | + */ |
|
| 94 | + function add_invoice_note( $invoice, $note, $note_author, $author_email, $for_customer = false ){ |
|
| 95 | + |
|
| 96 | + do_action( 'wpinv_pre_insert_invoice_note', $invoice->get_id(), $note, $for_customer ); |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * Insert the comment. |
|
| 100 | + */ |
|
| 101 | + $note_id = wp_insert_comment( |
|
| 102 | + wp_filter_comment( |
|
| 103 | + array( |
|
| 104 | + 'comment_post_ID' => $invoice->get_id(), |
|
| 105 | + 'comment_content' => $note, |
|
| 106 | + 'comment_agent' => 'Invoicing', |
|
| 107 | + 'user_id' => get_current_user_id(), |
|
| 108 | + 'comment_author' => $note_author, |
|
| 109 | + 'comment_author_IP' => wpinv_get_ip(), |
|
| 110 | + 'comment_author_email' => $author_email, |
|
| 111 | + 'comment_author_url' => $invoice->get_view_url(), |
|
| 112 | + 'comment_type' => 'wpinv_note', |
|
| 113 | + ) |
|
| 114 | + ) |
|
| 115 | + ); |
|
| 116 | + |
|
| 117 | + do_action( 'wpinv_insert_payment_note', $note_id, $invoice->get_id(), $note, $for_customer ); |
|
| 118 | + |
|
| 119 | + // Are we notifying the customer? |
|
| 120 | + if ( empty( $note_id ) || empty( $for_customer ) ) { |
|
| 121 | + return $note_id; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + add_comment_meta( $note_id, '_wpi_customer_note', 1 ); |
|
| 125 | + do_action( 'wpinv_new_customer_note', array( 'invoice_id' => $invoice->get_id(), 'user_note' => $note ) ); |
|
| 126 | + do_action( 'getpaid_new_customer_note', $invoice, $note ); |
|
| 127 | + return $note_id; |
|
| 128 | + } |
|
| 129 | 129 | |
| 130 | 130 | } |
@@ -196,11 +196,11 @@ discard block |
||
| 196 | 196 | $cb = "wpinv_{$option['type']}_callback"; |
| 197 | 197 | $section = "wpinv_settings_{$tab}_$section"; |
| 198 | 198 | |
| 199 | - if ( isset( $option['desc'] ) && ! empty( $option['help-tip'] ) ) { |
|
| 200 | - $tip = esc_attr( $option['desc'] ); |
|
| 201 | - $name .= "<span class='dashicons dashicons-editor-help wpi-help-tip' title='$tip'></span>"; |
|
| 202 | - unset( $option['desc'] ); |
|
| 203 | - } |
|
| 199 | + if ( isset( $option['desc'] ) && ! empty( $option['help-tip'] ) ) { |
|
| 200 | + $tip = esc_attr( $option['desc'] ); |
|
| 201 | + $name .= "<span class='dashicons dashicons-editor-help wpi-help-tip' title='$tip'></span>"; |
|
| 202 | + unset( $option['desc'] ); |
|
| 203 | + } |
|
| 204 | 204 | |
| 205 | 205 | // Loop through all tabs. |
| 206 | 206 | add_settings_field( |
@@ -425,333 +425,333 @@ discard block |
||
| 425 | 425 | } |
| 426 | 426 | |
| 427 | 427 | function wpinv_get_pages( $with_slug = false, $default_label = NULL ) { |
| 428 | - $pages_options = array(); |
|
| 428 | + $pages_options = array(); |
|
| 429 | 429 | |
| 430 | - if( $default_label !== NULL && $default_label !== false ) { |
|
| 431 | - $pages_options = array( '' => $default_label ); // Blank option |
|
| 432 | - } |
|
| 430 | + if( $default_label !== NULL && $default_label !== false ) { |
|
| 431 | + $pages_options = array( '' => $default_label ); // Blank option |
|
| 432 | + } |
|
| 433 | 433 | |
| 434 | - $pages = get_pages(); |
|
| 435 | - if ( $pages ) { |
|
| 436 | - foreach ( $pages as $page ) { |
|
| 437 | - $title = $with_slug ? $page->post_title . ' (' . $page->post_name . ')' : $page->post_title; |
|
| 434 | + $pages = get_pages(); |
|
| 435 | + if ( $pages ) { |
|
| 436 | + foreach ( $pages as $page ) { |
|
| 437 | + $title = $with_slug ? $page->post_title . ' (' . $page->post_name . ')' : $page->post_title; |
|
| 438 | 438 | $pages_options[ $page->ID ] = $title; |
| 439 | - } |
|
| 440 | - } |
|
| 439 | + } |
|
| 440 | + } |
|
| 441 | 441 | |
| 442 | - return $pages_options; |
|
| 442 | + return $pages_options; |
|
| 443 | 443 | } |
| 444 | 444 | |
| 445 | 445 | function wpinv_header_callback( $args ) { |
| 446 | - if ( !empty( $args['desc'] ) ) { |
|
| 446 | + if ( !empty( $args['desc'] ) ) { |
|
| 447 | 447 | echo $args['desc']; |
| 448 | 448 | } |
| 449 | 449 | } |
| 450 | 450 | |
| 451 | 451 | function wpinv_hidden_callback( $args ) { |
| 452 | - global $wpinv_options; |
|
| 453 | - |
|
| 454 | - if ( isset( $args['set_value'] ) ) { |
|
| 455 | - $value = $args['set_value']; |
|
| 456 | - } elseif ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
| 457 | - $value = $wpinv_options[ $args['id'] ]; |
|
| 458 | - } else { |
|
| 459 | - $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 460 | - } |
|
| 461 | - |
|
| 462 | - if ( isset( $args['faux'] ) && true === $args['faux'] ) { |
|
| 463 | - $args['readonly'] = true; |
|
| 464 | - $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 465 | - $name = ''; |
|
| 466 | - } else { |
|
| 467 | - $name = 'name="wpinv_settings[' . esc_attr( $args['id'] ) . ']"'; |
|
| 468 | - } |
|
| 469 | - |
|
| 470 | - $html = '<input type="hidden" id="wpinv_settings[' . wpinv_sanitize_key( $args['id'] ) . ']" ' . $name . ' value="' . esc_attr( stripslashes( $value ) ) . '" />'; |
|
| 452 | + global $wpinv_options; |
|
| 453 | + |
|
| 454 | + if ( isset( $args['set_value'] ) ) { |
|
| 455 | + $value = $args['set_value']; |
|
| 456 | + } elseif ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
| 457 | + $value = $wpinv_options[ $args['id'] ]; |
|
| 458 | + } else { |
|
| 459 | + $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 460 | + } |
|
| 461 | + |
|
| 462 | + if ( isset( $args['faux'] ) && true === $args['faux'] ) { |
|
| 463 | + $args['readonly'] = true; |
|
| 464 | + $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 465 | + $name = ''; |
|
| 466 | + } else { |
|
| 467 | + $name = 'name="wpinv_settings[' . esc_attr( $args['id'] ) . ']"'; |
|
| 468 | + } |
|
| 469 | + |
|
| 470 | + $html = '<input type="hidden" id="wpinv_settings[' . wpinv_sanitize_key( $args['id'] ) . ']" ' . $name . ' value="' . esc_attr( stripslashes( $value ) ) . '" />'; |
|
| 471 | 471 | |
| 472 | - echo $html; |
|
| 472 | + echo $html; |
|
| 473 | 473 | } |
| 474 | 474 | |
| 475 | 475 | function wpinv_checkbox_callback( $args ) { |
| 476 | - global $wpinv_options; |
|
| 476 | + global $wpinv_options; |
|
| 477 | 477 | |
| 478 | 478 | $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
| 479 | 479 | |
| 480 | - if ( isset( $args['faux'] ) && true === $args['faux'] ) { |
|
| 481 | - $name = ''; |
|
| 482 | - } else { |
|
| 483 | - $name = 'name="wpinv_settings[' . $sanitize_id . ']"'; |
|
| 484 | - } |
|
| 480 | + if ( isset( $args['faux'] ) && true === $args['faux'] ) { |
|
| 481 | + $name = ''; |
|
| 482 | + } else { |
|
| 483 | + $name = 'name="wpinv_settings[' . $sanitize_id . ']"'; |
|
| 484 | + } |
|
| 485 | 485 | |
| 486 | - $std = isset( $args['std'] ) ? $args['std'] : 0; |
|
| 487 | - $value = isset( $wpinv_options[ $args['id'] ] ) ? $wpinv_options[ $args['id'] ] : $std; |
|
| 488 | - $checked = checked( empty( $value ), false, false ); |
|
| 486 | + $std = isset( $args['std'] ) ? $args['std'] : 0; |
|
| 487 | + $value = isset( $wpinv_options[ $args['id'] ] ) ? $wpinv_options[ $args['id'] ] : $std; |
|
| 488 | + $checked = checked( empty( $value ), false, false ); |
|
| 489 | 489 | |
| 490 | - $html = '<input type="checkbox" id="wpinv_settings[' . $sanitize_id . ']"' . $name . ' value="1" ' . $checked . '/>'; |
|
| 491 | - $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
| 490 | + $html = '<input type="checkbox" id="wpinv_settings[' . $sanitize_id . ']"' . $name . ' value="1" ' . $checked . '/>'; |
|
| 491 | + $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
| 492 | 492 | |
| 493 | - echo $html; |
|
| 493 | + echo $html; |
|
| 494 | 494 | } |
| 495 | 495 | |
| 496 | 496 | function wpinv_multicheck_callback( $args ) { |
| 497 | - global $wpinv_options; |
|
| 497 | + global $wpinv_options; |
|
| 498 | 498 | |
| 499 | - $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
|
| 500 | - $class = !empty( $args['class'] ) ? ' ' . esc_attr( $args['class'] ) : ''; |
|
| 499 | + $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
|
| 500 | + $class = !empty( $args['class'] ) ? ' ' . esc_attr( $args['class'] ) : ''; |
|
| 501 | 501 | |
| 502 | - if ( ! empty( $args['options'] ) ) { |
|
| 502 | + if ( ! empty( $args['options'] ) ) { |
|
| 503 | 503 | |
| 504 | - $std = isset( $args['std'] ) ? $args['std'] : array(); |
|
| 505 | - $value = isset( $wpinv_options[ $args['id'] ] ) ? $wpinv_options[ $args['id'] ] : $std; |
|
| 504 | + $std = isset( $args['std'] ) ? $args['std'] : array(); |
|
| 505 | + $value = isset( $wpinv_options[ $args['id'] ] ) ? $wpinv_options[ $args['id'] ] : $std; |
|
| 506 | 506 | |
| 507 | - echo '<div class="wpi-mcheck-rows wpi-mcheck-' . $sanitize_id . $class . '">'; |
|
| 507 | + echo '<div class="wpi-mcheck-rows wpi-mcheck-' . $sanitize_id . $class . '">'; |
|
| 508 | 508 | foreach( $args['options'] as $key => $option ): |
| 509 | - $sanitize_key = wpinv_sanitize_key( $key ); |
|
| 510 | - if ( in_array( $sanitize_key, $value ) ) { |
|
| 511 | - $enabled = $sanitize_key; |
|
| 512 | - } else { |
|
| 513 | - $enabled = NULL; |
|
| 514 | - } |
|
| 515 | - echo '<div class="wpi-mcheck-row"><input name="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" id="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" type="checkbox" value="' . esc_attr( $sanitize_key ) . '" ' . checked( $sanitize_key, $enabled, false ) . '/> '; |
|
| 516 | - echo '<label for="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']">' . wp_kses_post( $option ) . '</label></div>'; |
|
| 517 | - endforeach; |
|
| 518 | - echo '</div>'; |
|
| 519 | - echo '<p class="description">' . $args['desc'] . '</p>'; |
|
| 520 | - } |
|
| 509 | + $sanitize_key = wpinv_sanitize_key( $key ); |
|
| 510 | + if ( in_array( $sanitize_key, $value ) ) { |
|
| 511 | + $enabled = $sanitize_key; |
|
| 512 | + } else { |
|
| 513 | + $enabled = NULL; |
|
| 514 | + } |
|
| 515 | + echo '<div class="wpi-mcheck-row"><input name="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" id="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" type="checkbox" value="' . esc_attr( $sanitize_key ) . '" ' . checked( $sanitize_key, $enabled, false ) . '/> '; |
|
| 516 | + echo '<label for="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']">' . wp_kses_post( $option ) . '</label></div>'; |
|
| 517 | + endforeach; |
|
| 518 | + echo '</div>'; |
|
| 519 | + echo '<p class="description">' . $args['desc'] . '</p>'; |
|
| 520 | + } |
|
| 521 | 521 | } |
| 522 | 522 | |
| 523 | 523 | function wpinv_payment_icons_callback( $args ) { |
| 524 | - global $wpinv_options; |
|
| 524 | + global $wpinv_options; |
|
| 525 | 525 | |
| 526 | 526 | $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
| 527 | 527 | |
| 528 | - if ( ! empty( $args['options'] ) ) { |
|
| 529 | - foreach( $args['options'] as $key => $option ) { |
|
| 528 | + if ( ! empty( $args['options'] ) ) { |
|
| 529 | + foreach( $args['options'] as $key => $option ) { |
|
| 530 | 530 | $sanitize_key = wpinv_sanitize_key( $key ); |
| 531 | 531 | |
| 532 | - if( isset( $wpinv_options[$args['id']][$key] ) ) { |
|
| 533 | - $enabled = $option; |
|
| 534 | - } else { |
|
| 535 | - $enabled = NULL; |
|
| 536 | - } |
|
| 537 | - |
|
| 538 | - echo '<label for="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" style="margin-right:10px;line-height:16px;height:16px;display:inline-block;">'; |
|
| 539 | - |
|
| 540 | - echo '<input name="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" id="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" type="checkbox" value="' . esc_attr( $option ) . '" ' . checked( $option, $enabled, false ) . '/> '; |
|
| 541 | - |
|
| 542 | - if ( wpinv_string_is_image_url( $key ) ) { |
|
| 543 | - echo '<img class="payment-icon" src="' . esc_url( $key ) . '" style="width:32px;height:24px;position:relative;top:6px;margin-right:5px;"/>'; |
|
| 544 | - } else { |
|
| 545 | - $card = strtolower( str_replace( ' ', '', $option ) ); |
|
| 546 | - |
|
| 547 | - if ( has_filter( 'wpinv_accepted_payment_' . $card . '_image' ) ) { |
|
| 548 | - $image = apply_filters( 'wpinv_accepted_payment_' . $card . '_image', '' ); |
|
| 549 | - } else { |
|
| 550 | - $image = wpinv_locate_template( 'images' . DIRECTORY_SEPARATOR . 'icons' . DIRECTORY_SEPARATOR . $card . '.gif', false ); |
|
| 551 | - $content_dir = WP_CONTENT_DIR; |
|
| 552 | - |
|
| 553 | - if ( function_exists( 'wp_normalize_path' ) ) { |
|
| 554 | - // Replaces backslashes with forward slashes for Windows systems |
|
| 555 | - $image = wp_normalize_path( $image ); |
|
| 556 | - $content_dir = wp_normalize_path( $content_dir ); |
|
| 557 | - } |
|
| 558 | - |
|
| 559 | - $image = str_replace( $content_dir, content_url(), $image ); |
|
| 560 | - } |
|
| 561 | - |
|
| 562 | - echo '<img class="payment-icon" src="' . esc_url( $image ) . '" style="width:32px;height:24px;position:relative;top:6px;margin-right:5px;"/>'; |
|
| 563 | - } |
|
| 564 | - echo $option . '</label>'; |
|
| 565 | - } |
|
| 566 | - echo '<p class="description" style="margin-top:16px;">' . wp_kses_post( $args['desc'] ) . '</p>'; |
|
| 567 | - } |
|
| 532 | + if( isset( $wpinv_options[$args['id']][$key] ) ) { |
|
| 533 | + $enabled = $option; |
|
| 534 | + } else { |
|
| 535 | + $enabled = NULL; |
|
| 536 | + } |
|
| 537 | + |
|
| 538 | + echo '<label for="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" style="margin-right:10px;line-height:16px;height:16px;display:inline-block;">'; |
|
| 539 | + |
|
| 540 | + echo '<input name="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" id="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" type="checkbox" value="' . esc_attr( $option ) . '" ' . checked( $option, $enabled, false ) . '/> '; |
|
| 541 | + |
|
| 542 | + if ( wpinv_string_is_image_url( $key ) ) { |
|
| 543 | + echo '<img class="payment-icon" src="' . esc_url( $key ) . '" style="width:32px;height:24px;position:relative;top:6px;margin-right:5px;"/>'; |
|
| 544 | + } else { |
|
| 545 | + $card = strtolower( str_replace( ' ', '', $option ) ); |
|
| 546 | + |
|
| 547 | + if ( has_filter( 'wpinv_accepted_payment_' . $card . '_image' ) ) { |
|
| 548 | + $image = apply_filters( 'wpinv_accepted_payment_' . $card . '_image', '' ); |
|
| 549 | + } else { |
|
| 550 | + $image = wpinv_locate_template( 'images' . DIRECTORY_SEPARATOR . 'icons' . DIRECTORY_SEPARATOR . $card . '.gif', false ); |
|
| 551 | + $content_dir = WP_CONTENT_DIR; |
|
| 552 | + |
|
| 553 | + if ( function_exists( 'wp_normalize_path' ) ) { |
|
| 554 | + // Replaces backslashes with forward slashes for Windows systems |
|
| 555 | + $image = wp_normalize_path( $image ); |
|
| 556 | + $content_dir = wp_normalize_path( $content_dir ); |
|
| 557 | + } |
|
| 558 | + |
|
| 559 | + $image = str_replace( $content_dir, content_url(), $image ); |
|
| 560 | + } |
|
| 561 | + |
|
| 562 | + echo '<img class="payment-icon" src="' . esc_url( $image ) . '" style="width:32px;height:24px;position:relative;top:6px;margin-right:5px;"/>'; |
|
| 563 | + } |
|
| 564 | + echo $option . '</label>'; |
|
| 565 | + } |
|
| 566 | + echo '<p class="description" style="margin-top:16px;">' . wp_kses_post( $args['desc'] ) . '</p>'; |
|
| 567 | + } |
|
| 568 | 568 | } |
| 569 | 569 | |
| 570 | 570 | function wpinv_radio_callback( $args ) { |
| 571 | - global $wpinv_options; |
|
| 571 | + global $wpinv_options; |
|
| 572 | 572 | |
| 573 | 573 | $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
| 574 | 574 | |
| 575 | 575 | foreach ( $args['options'] as $key => $option ) : |
| 576 | - $sanitize_key = wpinv_sanitize_key( $key ); |
|
| 576 | + $sanitize_key = wpinv_sanitize_key( $key ); |
|
| 577 | 577 | |
| 578 | 578 | $checked = false; |
| 579 | 579 | |
| 580 | - if ( isset( $wpinv_options[ $args['id'] ] ) && $wpinv_options[ $args['id'] ] == $key ) |
|
| 581 | - $checked = true; |
|
| 582 | - elseif( isset( $args['std'] ) && $args['std'] == $key && ! isset( $wpinv_options[ $args['id'] ] ) ) |
|
| 583 | - $checked = true; |
|
| 580 | + if ( isset( $wpinv_options[ $args['id'] ] ) && $wpinv_options[ $args['id'] ] == $key ) |
|
| 581 | + $checked = true; |
|
| 582 | + elseif( isset( $args['std'] ) && $args['std'] == $key && ! isset( $wpinv_options[ $args['id'] ] ) ) |
|
| 583 | + $checked = true; |
|
| 584 | 584 | |
| 585 | - echo '<input name="wpinv_settings[' . $sanitize_id . ']" id="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" type="radio" value="' . $sanitize_key . '" ' . checked(true, $checked, false) . '/> '; |
|
| 586 | - echo '<label for="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']">' . esc_html( $option ) . '</label><br/>'; |
|
| 587 | - endforeach; |
|
| 585 | + echo '<input name="wpinv_settings[' . $sanitize_id . ']" id="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" type="radio" value="' . $sanitize_key . '" ' . checked(true, $checked, false) . '/> '; |
|
| 586 | + echo '<label for="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']">' . esc_html( $option ) . '</label><br/>'; |
|
| 587 | + endforeach; |
|
| 588 | 588 | |
| 589 | - echo '<p class="description">' . wp_kses_post( $args['desc'] ) . '</p>'; |
|
| 589 | + echo '<p class="description">' . wp_kses_post( $args['desc'] ) . '</p>'; |
|
| 590 | 590 | } |
| 591 | 591 | |
| 592 | 592 | function wpinv_gateways_callback( $args ) { |
| 593 | - global $wpinv_options; |
|
| 593 | + global $wpinv_options; |
|
| 594 | 594 | |
| 595 | 595 | $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
| 596 | 596 | |
| 597 | - foreach ( $args['options'] as $key => $option ) : |
|
| 598 | - $sanitize_key = wpinv_sanitize_key( $key ); |
|
| 597 | + foreach ( $args['options'] as $key => $option ) : |
|
| 598 | + $sanitize_key = wpinv_sanitize_key( $key ); |
|
| 599 | 599 | |
| 600 | 600 | if ( isset( $wpinv_options['gateways'][ $key ] ) ) |
| 601 | - $enabled = '1'; |
|
| 602 | - else |
|
| 603 | - $enabled = null; |
|
| 601 | + $enabled = '1'; |
|
| 602 | + else |
|
| 603 | + $enabled = null; |
|
| 604 | 604 | |
| 605 | - echo '<input name="wpinv_settings[' . esc_attr( $args['id'] ) . '][' . $sanitize_key . ']" id="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" type="checkbox" value="1" ' . checked('1', $enabled, false) . '/> '; |
|
| 606 | - echo '<label for="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']">' . esc_html( $option['admin_label'] ) . '</label><br/>'; |
|
| 607 | - endforeach; |
|
| 605 | + echo '<input name="wpinv_settings[' . esc_attr( $args['id'] ) . '][' . $sanitize_key . ']" id="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" type="checkbox" value="1" ' . checked('1', $enabled, false) . '/> '; |
|
| 606 | + echo '<label for="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']">' . esc_html( $option['admin_label'] ) . '</label><br/>'; |
|
| 607 | + endforeach; |
|
| 608 | 608 | } |
| 609 | 609 | |
| 610 | 610 | function wpinv_gateway_select_callback($args) { |
| 611 | - global $wpinv_options; |
|
| 611 | + global $wpinv_options; |
|
| 612 | 612 | |
| 613 | 613 | $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
| 614 | 614 | $class = !empty( $args['class'] ) ? ' ' . esc_attr( $args['class'] ) : ''; |
| 615 | 615 | |
| 616 | - echo '<select name="wpinv_settings[' . $sanitize_id . ']"" id="wpinv_settings[' . $sanitize_id . ']" class="'.$class.'" >'; |
|
| 616 | + echo '<select name="wpinv_settings[' . $sanitize_id . ']"" id="wpinv_settings[' . $sanitize_id . ']" class="'.$class.'" >'; |
|
| 617 | 617 | |
| 618 | - foreach ( $args['options'] as $key => $option ) : |
|
| 619 | - if ( isset( $args['selected'] ) && $args['selected'] !== null && $args['selected'] !== false ) { |
|
| 618 | + foreach ( $args['options'] as $key => $option ) : |
|
| 619 | + if ( isset( $args['selected'] ) && $args['selected'] !== null && $args['selected'] !== false ) { |
|
| 620 | 620 | $selected = selected( $key, $args['selected'], false ); |
| 621 | 621 | } else { |
| 622 | 622 | $selected = isset( $wpinv_options[ $args['id'] ] ) ? selected( $key, $wpinv_options[$args['id']], false ) : ''; |
| 623 | 623 | } |
| 624 | - echo '<option value="' . wpinv_sanitize_key( $key ) . '"' . $selected . '>' . esc_html( $option['admin_label'] ) . '</option>'; |
|
| 625 | - endforeach; |
|
| 624 | + echo '<option value="' . wpinv_sanitize_key( $key ) . '"' . $selected . '>' . esc_html( $option['admin_label'] ) . '</option>'; |
|
| 625 | + endforeach; |
|
| 626 | 626 | |
| 627 | - echo '</select>'; |
|
| 628 | - echo '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
| 627 | + echo '</select>'; |
|
| 628 | + echo '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
| 629 | 629 | } |
| 630 | 630 | |
| 631 | 631 | function wpinv_text_callback( $args ) { |
| 632 | - global $wpinv_options; |
|
| 632 | + global $wpinv_options; |
|
| 633 | 633 | |
| 634 | 634 | $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
| 635 | 635 | |
| 636 | - if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
| 637 | - $value = $wpinv_options[ $args['id'] ]; |
|
| 638 | - } else { |
|
| 639 | - $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 640 | - } |
|
| 641 | - |
|
| 642 | - if ( isset( $args['faux'] ) && true === $args['faux'] ) { |
|
| 643 | - $args['readonly'] = true; |
|
| 644 | - $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 645 | - $name = ''; |
|
| 646 | - } else { |
|
| 647 | - $name = 'name="wpinv_settings[' . esc_attr( $args['id'] ) . ']"'; |
|
| 648 | - } |
|
| 649 | - $class = !empty( $args['class'] ) ? sanitize_html_class( $args['class'] ) : ''; |
|
| 650 | - |
|
| 651 | - $readonly = $args['readonly'] === true ? ' readonly="readonly"' : ''; |
|
| 652 | - $size = ( isset( $args['size'] ) && ! is_null( $args['size'] ) ) ? $args['size'] : 'regular'; |
|
| 653 | - $html = '<input type="text" class="' . sanitize_html_class( $size ) . '-text ' . $class . '" id="wpinv_settings[' . $sanitize_id . ']" ' . $name . ' value="' . esc_attr( stripslashes( $value ) ) . '"' . $readonly . '/>'; |
|
| 654 | - $html .= '<br /><label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
| 655 | - |
|
| 656 | - echo $html; |
|
| 636 | + if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
| 637 | + $value = $wpinv_options[ $args['id'] ]; |
|
| 638 | + } else { |
|
| 639 | + $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 640 | + } |
|
| 641 | + |
|
| 642 | + if ( isset( $args['faux'] ) && true === $args['faux'] ) { |
|
| 643 | + $args['readonly'] = true; |
|
| 644 | + $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 645 | + $name = ''; |
|
| 646 | + } else { |
|
| 647 | + $name = 'name="wpinv_settings[' . esc_attr( $args['id'] ) . ']"'; |
|
| 648 | + } |
|
| 649 | + $class = !empty( $args['class'] ) ? sanitize_html_class( $args['class'] ) : ''; |
|
| 650 | + |
|
| 651 | + $readonly = $args['readonly'] === true ? ' readonly="readonly"' : ''; |
|
| 652 | + $size = ( isset( $args['size'] ) && ! is_null( $args['size'] ) ) ? $args['size'] : 'regular'; |
|
| 653 | + $html = '<input type="text" class="' . sanitize_html_class( $size ) . '-text ' . $class . '" id="wpinv_settings[' . $sanitize_id . ']" ' . $name . ' value="' . esc_attr( stripslashes( $value ) ) . '"' . $readonly . '/>'; |
|
| 654 | + $html .= '<br /><label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
| 655 | + |
|
| 656 | + echo $html; |
|
| 657 | 657 | } |
| 658 | 658 | |
| 659 | 659 | function wpinv_number_callback( $args ) { |
| 660 | - global $wpinv_options; |
|
| 660 | + global $wpinv_options; |
|
| 661 | 661 | |
| 662 | 662 | $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
| 663 | 663 | |
| 664 | - if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
| 665 | - $value = $wpinv_options[ $args['id'] ]; |
|
| 666 | - } else { |
|
| 667 | - $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 668 | - } |
|
| 669 | - |
|
| 670 | - if ( isset( $args['faux'] ) && true === $args['faux'] ) { |
|
| 671 | - $args['readonly'] = true; |
|
| 672 | - $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 673 | - $name = ''; |
|
| 674 | - } else { |
|
| 675 | - $name = 'name="wpinv_settings[' . esc_attr( $args['id'] ) . ']"'; |
|
| 676 | - } |
|
| 677 | - |
|
| 678 | - $max = isset( $args['max'] ) ? $args['max'] : 999999; |
|
| 679 | - $min = isset( $args['min'] ) ? $args['min'] : 0; |
|
| 680 | - $step = isset( $args['step'] ) ? $args['step'] : 1; |
|
| 681 | - $class = !empty( $args['class'] ) ? sanitize_html_class( $args['class'] ) : ''; |
|
| 682 | - |
|
| 683 | - $size = ( isset( $args['size'] ) && ! is_null( $args['size'] ) ) ? $args['size'] : 'regular'; |
|
| 684 | - $html = '<input type="number" step="' . esc_attr( $step ) . '" max="' . esc_attr( $max ) . '" min="' . esc_attr( $min ) . '" class="' . sanitize_html_class( $size ) . '-text ' . $class . '" id="wpinv_settings[' . $sanitize_id . ']" ' . $name . ' value="' . esc_attr( stripslashes( $value ) ) . '"/>'; |
|
| 685 | - $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
| 686 | - |
|
| 687 | - echo $html; |
|
| 664 | + if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
| 665 | + $value = $wpinv_options[ $args['id'] ]; |
|
| 666 | + } else { |
|
| 667 | + $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 668 | + } |
|
| 669 | + |
|
| 670 | + if ( isset( $args['faux'] ) && true === $args['faux'] ) { |
|
| 671 | + $args['readonly'] = true; |
|
| 672 | + $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 673 | + $name = ''; |
|
| 674 | + } else { |
|
| 675 | + $name = 'name="wpinv_settings[' . esc_attr( $args['id'] ) . ']"'; |
|
| 676 | + } |
|
| 677 | + |
|
| 678 | + $max = isset( $args['max'] ) ? $args['max'] : 999999; |
|
| 679 | + $min = isset( $args['min'] ) ? $args['min'] : 0; |
|
| 680 | + $step = isset( $args['step'] ) ? $args['step'] : 1; |
|
| 681 | + $class = !empty( $args['class'] ) ? sanitize_html_class( $args['class'] ) : ''; |
|
| 682 | + |
|
| 683 | + $size = ( isset( $args['size'] ) && ! is_null( $args['size'] ) ) ? $args['size'] : 'regular'; |
|
| 684 | + $html = '<input type="number" step="' . esc_attr( $step ) . '" max="' . esc_attr( $max ) . '" min="' . esc_attr( $min ) . '" class="' . sanitize_html_class( $size ) . '-text ' . $class . '" id="wpinv_settings[' . $sanitize_id . ']" ' . $name . ' value="' . esc_attr( stripslashes( $value ) ) . '"/>'; |
|
| 685 | + $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
| 686 | + |
|
| 687 | + echo $html; |
|
| 688 | 688 | } |
| 689 | 689 | |
| 690 | 690 | function wpinv_textarea_callback( $args ) { |
| 691 | - global $wpinv_options; |
|
| 691 | + global $wpinv_options; |
|
| 692 | 692 | |
| 693 | 693 | $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
| 694 | 694 | |
| 695 | - if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
| 696 | - $value = $wpinv_options[ $args['id'] ]; |
|
| 697 | - } else { |
|
| 698 | - $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 699 | - } |
|
| 695 | + if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
| 696 | + $value = $wpinv_options[ $args['id'] ]; |
|
| 697 | + } else { |
|
| 698 | + $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 699 | + } |
|
| 700 | 700 | |
| 701 | 701 | $size = ( isset( $args['size'] ) && ! is_null( $args['size'] ) ) ? $args['size'] : 'regular'; |
| 702 | 702 | $class = ( isset( $args['class'] ) && ! is_null( $args['class'] ) ) ? $args['class'] : 'large-text'; |
| 703 | 703 | |
| 704 | - $html = '<textarea class="' . sanitize_html_class( $class ) . ' txtarea-' . sanitize_html_class( $size ) . ' wpi-' . esc_attr( sanitize_html_class( $sanitize_id ) ) . ' " cols="' . $args['cols'] . '" rows="' . $args['rows'] . '" id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']">' . esc_textarea( stripslashes( $value ) ) . '</textarea>'; |
|
| 705 | - $html .= '<br /><label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
| 704 | + $html = '<textarea class="' . sanitize_html_class( $class ) . ' txtarea-' . sanitize_html_class( $size ) . ' wpi-' . esc_attr( sanitize_html_class( $sanitize_id ) ) . ' " cols="' . $args['cols'] . '" rows="' . $args['rows'] . '" id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']">' . esc_textarea( stripslashes( $value ) ) . '</textarea>'; |
|
| 705 | + $html .= '<br /><label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
| 706 | 706 | |
| 707 | - echo $html; |
|
| 707 | + echo $html; |
|
| 708 | 708 | } |
| 709 | 709 | |
| 710 | 710 | function wpinv_password_callback( $args ) { |
| 711 | - global $wpinv_options; |
|
| 711 | + global $wpinv_options; |
|
| 712 | 712 | |
| 713 | 713 | $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
| 714 | 714 | |
| 715 | - if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
| 716 | - $value = $wpinv_options[ $args['id'] ]; |
|
| 717 | - } else { |
|
| 718 | - $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 719 | - } |
|
| 715 | + if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
| 716 | + $value = $wpinv_options[ $args['id'] ]; |
|
| 717 | + } else { |
|
| 718 | + $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 719 | + } |
|
| 720 | 720 | |
| 721 | - $size = ( isset( $args['size'] ) && ! is_null( $args['size'] ) ) ? $args['size'] : 'regular'; |
|
| 722 | - $html = '<input type="password" class="' . sanitize_html_class( $size ) . '-text" id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']" value="' . esc_attr( $value ) . '"/>'; |
|
| 723 | - $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
| 721 | + $size = ( isset( $args['size'] ) && ! is_null( $args['size'] ) ) ? $args['size'] : 'regular'; |
|
| 722 | + $html = '<input type="password" class="' . sanitize_html_class( $size ) . '-text" id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']" value="' . esc_attr( $value ) . '"/>'; |
|
| 723 | + $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
| 724 | 724 | |
| 725 | - echo $html; |
|
| 725 | + echo $html; |
|
| 726 | 726 | } |
| 727 | 727 | |
| 728 | 728 | function wpinv_missing_callback($args) { |
| 729 | - printf( |
|
| 730 | - __( 'The callback function used for the %s setting is missing.', 'invoicing' ), |
|
| 731 | - '<strong>' . $args['id'] . '</strong>' |
|
| 732 | - ); |
|
| 729 | + printf( |
|
| 730 | + __( 'The callback function used for the %s setting is missing.', 'invoicing' ), |
|
| 731 | + '<strong>' . $args['id'] . '</strong>' |
|
| 732 | + ); |
|
| 733 | 733 | } |
| 734 | 734 | |
| 735 | 735 | function wpinv_select_callback($args) { |
| 736 | - global $wpinv_options; |
|
| 736 | + global $wpinv_options; |
|
| 737 | 737 | |
| 738 | 738 | $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
| 739 | 739 | |
| 740 | - if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
| 741 | - $value = $wpinv_options[ $args['id'] ]; |
|
| 742 | - } else { |
|
| 743 | - $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 744 | - } |
|
| 740 | + if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
| 741 | + $value = $wpinv_options[ $args['id'] ]; |
|
| 742 | + } else { |
|
| 743 | + $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 744 | + } |
|
| 745 | 745 | |
| 746 | 746 | if ( isset( $args['selected'] ) && $args['selected'] !== null && $args['selected'] !== false ) { |
| 747 | 747 | $value = $args['selected']; |
| 748 | 748 | } |
| 749 | 749 | |
| 750 | - if ( isset( $args['placeholder'] ) ) { |
|
| 751 | - $placeholder = $args['placeholder']; |
|
| 752 | - } else { |
|
| 753 | - $placeholder = ''; |
|
| 754 | - } |
|
| 750 | + if ( isset( $args['placeholder'] ) ) { |
|
| 751 | + $placeholder = $args['placeholder']; |
|
| 752 | + } else { |
|
| 753 | + $placeholder = ''; |
|
| 754 | + } |
|
| 755 | 755 | |
| 756 | 756 | if( !empty( $args['onchange'] ) ) { |
| 757 | 757 | $onchange = ' onchange="' . esc_attr( $args['onchange'] ) . '"'; |
@@ -761,143 +761,143 @@ discard block |
||
| 761 | 761 | |
| 762 | 762 | $class = !empty( $args['class'] ) ? ' ' . esc_attr( $args['class'] ) : ''; |
| 763 | 763 | |
| 764 | - $html = '<select id="wpinv_settings[' . $sanitize_id . ']" class="'.$class.'" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']" data-placeholder="' . esc_html( $placeholder ) . '"' . $onchange . ' />'; |
|
| 764 | + $html = '<select id="wpinv_settings[' . $sanitize_id . ']" class="'.$class.'" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']" data-placeholder="' . esc_html( $placeholder ) . '"' . $onchange . ' />'; |
|
| 765 | 765 | |
| 766 | - foreach ( $args['options'] as $option => $name ) { |
|
| 767 | - $selected = selected( $option, $value, false ); |
|
| 768 | - $html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( $name ) . '</option>'; |
|
| 769 | - } |
|
| 766 | + foreach ( $args['options'] as $option => $name ) { |
|
| 767 | + $selected = selected( $option, $value, false ); |
|
| 768 | + $html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( $name ) . '</option>'; |
|
| 769 | + } |
|
| 770 | 770 | |
| 771 | - $html .= '</select>'; |
|
| 772 | - $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
| 771 | + $html .= '</select>'; |
|
| 772 | + $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
| 773 | 773 | |
| 774 | - echo $html; |
|
| 774 | + echo $html; |
|
| 775 | 775 | } |
| 776 | 776 | |
| 777 | 777 | function wpinv_color_select_callback( $args ) { |
| 778 | - global $wpinv_options; |
|
| 778 | + global $wpinv_options; |
|
| 779 | 779 | |
| 780 | 780 | $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
| 781 | 781 | |
| 782 | - if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
| 783 | - $value = $wpinv_options[ $args['id'] ]; |
|
| 784 | - } else { |
|
| 785 | - $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 786 | - } |
|
| 782 | + if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
| 783 | + $value = $wpinv_options[ $args['id'] ]; |
|
| 784 | + } else { |
|
| 785 | + $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 786 | + } |
|
| 787 | 787 | |
| 788 | - $html = '<select id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']"/>'; |
|
| 788 | + $html = '<select id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']"/>'; |
|
| 789 | 789 | |
| 790 | - foreach ( $args['options'] as $option => $color ) { |
|
| 791 | - $selected = selected( $option, $value, false ); |
|
| 792 | - $html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( $color['label'] ) . '</option>'; |
|
| 793 | - } |
|
| 790 | + foreach ( $args['options'] as $option => $color ) { |
|
| 791 | + $selected = selected( $option, $value, false ); |
|
| 792 | + $html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( $color['label'] ) . '</option>'; |
|
| 793 | + } |
|
| 794 | 794 | |
| 795 | - $html .= '</select>'; |
|
| 796 | - $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
| 795 | + $html .= '</select>'; |
|
| 796 | + $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
| 797 | 797 | |
| 798 | - echo $html; |
|
| 798 | + echo $html; |
|
| 799 | 799 | } |
| 800 | 800 | |
| 801 | 801 | function wpinv_rich_editor_callback( $args ) { |
| 802 | - global $wpinv_options, $wp_version; |
|
| 802 | + global $wpinv_options, $wp_version; |
|
| 803 | 803 | |
| 804 | 804 | $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
| 805 | 805 | |
| 806 | - if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
| 807 | - $value = $wpinv_options[ $args['id'] ]; |
|
| 806 | + if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
| 807 | + $value = $wpinv_options[ $args['id'] ]; |
|
| 808 | 808 | |
| 809 | - if( empty( $args['allow_blank'] ) && empty( $value ) ) { |
|
| 810 | - $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 811 | - } |
|
| 812 | - } else { |
|
| 813 | - $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 814 | - } |
|
| 809 | + if( empty( $args['allow_blank'] ) && empty( $value ) ) { |
|
| 810 | + $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 811 | + } |
|
| 812 | + } else { |
|
| 813 | + $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 814 | + } |
|
| 815 | 815 | |
| 816 | - $rows = isset( $args['size'] ) ? $args['size'] : 20; |
|
| 816 | + $rows = isset( $args['size'] ) ? $args['size'] : 20; |
|
| 817 | 817 | |
| 818 | - $html = '<div class="getpaid-settings-editor-input">'; |
|
| 819 | - if ( $wp_version >= 3.3 && function_exists( 'wp_editor' ) ) { |
|
| 820 | - ob_start(); |
|
| 821 | - wp_editor( stripslashes( $value ), 'wpinv_settings_' . esc_attr( $args['id'] ), array( 'textarea_name' => 'wpinv_settings[' . esc_attr( $args['id'] ) . ']', 'textarea_rows' => absint( $rows ), 'media_buttons' => false ) ); |
|
| 822 | - $html .= ob_get_clean(); |
|
| 823 | - } else { |
|
| 824 | - $html .= '<textarea class="large-text" rows="10" id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']" class="wpi-' . esc_attr( sanitize_html_class( $args['id'] ) ) . '">' . esc_textarea( stripslashes( $value ) ) . '</textarea>'; |
|
| 825 | - } |
|
| 818 | + $html = '<div class="getpaid-settings-editor-input">'; |
|
| 819 | + if ( $wp_version >= 3.3 && function_exists( 'wp_editor' ) ) { |
|
| 820 | + ob_start(); |
|
| 821 | + wp_editor( stripslashes( $value ), 'wpinv_settings_' . esc_attr( $args['id'] ), array( 'textarea_name' => 'wpinv_settings[' . esc_attr( $args['id'] ) . ']', 'textarea_rows' => absint( $rows ), 'media_buttons' => false ) ); |
|
| 822 | + $html .= ob_get_clean(); |
|
| 823 | + } else { |
|
| 824 | + $html .= '<textarea class="large-text" rows="10" id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']" class="wpi-' . esc_attr( sanitize_html_class( $args['id'] ) ) . '">' . esc_textarea( stripslashes( $value ) ) . '</textarea>'; |
|
| 825 | + } |
|
| 826 | 826 | |
| 827 | - $html .= '</div><br/><label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
| 827 | + $html .= '</div><br/><label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
| 828 | 828 | |
| 829 | - echo $html; |
|
| 829 | + echo $html; |
|
| 830 | 830 | } |
| 831 | 831 | |
| 832 | 832 | function wpinv_upload_callback( $args ) { |
| 833 | - global $wpinv_options; |
|
| 833 | + global $wpinv_options; |
|
| 834 | 834 | |
| 835 | 835 | $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
| 836 | 836 | |
| 837 | - if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
| 838 | - $value = $wpinv_options[$args['id']]; |
|
| 839 | - } else { |
|
| 840 | - $value = isset($args['std']) ? $args['std'] : ''; |
|
| 841 | - } |
|
| 837 | + if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
| 838 | + $value = $wpinv_options[$args['id']]; |
|
| 839 | + } else { |
|
| 840 | + $value = isset($args['std']) ? $args['std'] : ''; |
|
| 841 | + } |
|
| 842 | 842 | |
| 843 | - $size = ( isset( $args['size'] ) && ! is_null( $args['size'] ) ) ? $args['size'] : 'regular'; |
|
| 844 | - $html = '<input type="text" class="' . sanitize_html_class( $size ) . '-text" id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']" value="' . esc_attr( stripslashes( $value ) ) . '"/>'; |
|
| 845 | - $html .= '<span> <input type="button" class="wpinv_settings_upload_button button-secondary" value="' . __( 'Upload File', 'invoicing' ) . '"/></span>'; |
|
| 846 | - $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
| 843 | + $size = ( isset( $args['size'] ) && ! is_null( $args['size'] ) ) ? $args['size'] : 'regular'; |
|
| 844 | + $html = '<input type="text" class="' . sanitize_html_class( $size ) . '-text" id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']" value="' . esc_attr( stripslashes( $value ) ) . '"/>'; |
|
| 845 | + $html .= '<span> <input type="button" class="wpinv_settings_upload_button button-secondary" value="' . __( 'Upload File', 'invoicing' ) . '"/></span>'; |
|
| 846 | + $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
| 847 | 847 | |
| 848 | - echo $html; |
|
| 848 | + echo $html; |
|
| 849 | 849 | } |
| 850 | 850 | |
| 851 | 851 | function wpinv_color_callback( $args ) { |
| 852 | - global $wpinv_options; |
|
| 852 | + global $wpinv_options; |
|
| 853 | 853 | |
| 854 | 854 | $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
| 855 | 855 | |
| 856 | - if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
| 857 | - $value = $wpinv_options[ $args['id'] ]; |
|
| 858 | - } else { |
|
| 859 | - $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 860 | - } |
|
| 856 | + if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
| 857 | + $value = $wpinv_options[ $args['id'] ]; |
|
| 858 | + } else { |
|
| 859 | + $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 860 | + } |
|
| 861 | 861 | |
| 862 | - $default = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 862 | + $default = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 863 | 863 | |
| 864 | - $html = '<input type="text" class="wpinv-color-picker" id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']" value="' . esc_attr( $value ) . '" data-default-color="' . esc_attr( $default ) . '" />'; |
|
| 865 | - $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
| 864 | + $html = '<input type="text" class="wpinv-color-picker" id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']" value="' . esc_attr( $value ) . '" data-default-color="' . esc_attr( $default ) . '" />'; |
|
| 865 | + $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
| 866 | 866 | |
| 867 | - echo $html; |
|
| 867 | + echo $html; |
|
| 868 | 868 | } |
| 869 | 869 | |
| 870 | 870 | function wpinv_country_states_callback($args) { |
| 871 | - global $wpinv_options; |
|
| 871 | + global $wpinv_options; |
|
| 872 | 872 | |
| 873 | 873 | $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
| 874 | 874 | |
| 875 | - if ( isset( $args['placeholder'] ) ) { |
|
| 876 | - $placeholder = $args['placeholder']; |
|
| 877 | - } else { |
|
| 878 | - $placeholder = ''; |
|
| 879 | - } |
|
| 875 | + if ( isset( $args['placeholder'] ) ) { |
|
| 876 | + $placeholder = $args['placeholder']; |
|
| 877 | + } else { |
|
| 878 | + $placeholder = ''; |
|
| 879 | + } |
|
| 880 | 880 | |
| 881 | - $states = wpinv_get_country_states(); |
|
| 881 | + $states = wpinv_get_country_states(); |
|
| 882 | 882 | |
| 883 | - $class = empty( $states ) ? ' class="wpinv-no-states"' : ' class="wpi_select2"'; |
|
| 884 | - $html = '<select id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']"' . $class . 'data-placeholder="' . esc_html( $placeholder ) . '"/>'; |
|
| 883 | + $class = empty( $states ) ? ' class="wpinv-no-states"' : ' class="wpi_select2"'; |
|
| 884 | + $html = '<select id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']"' . $class . 'data-placeholder="' . esc_html( $placeholder ) . '"/>'; |
|
| 885 | 885 | |
| 886 | - foreach ( $states as $option => $name ) { |
|
| 887 | - $selected = isset( $wpinv_options[ $args['id'] ] ) ? selected( $option, $wpinv_options[$args['id']], false ) : ''; |
|
| 888 | - $html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( $name ) . '</option>'; |
|
| 889 | - } |
|
| 886 | + foreach ( $states as $option => $name ) { |
|
| 887 | + $selected = isset( $wpinv_options[ $args['id'] ] ) ? selected( $option, $wpinv_options[$args['id']], false ) : ''; |
|
| 888 | + $html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( $name ) . '</option>'; |
|
| 889 | + } |
|
| 890 | 890 | |
| 891 | - $html .= '</select>'; |
|
| 892 | - $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
| 891 | + $html .= '</select>'; |
|
| 892 | + $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
| 893 | 893 | |
| 894 | - echo $html; |
|
| 894 | + echo $html; |
|
| 895 | 895 | } |
| 896 | 896 | |
| 897 | 897 | function wpinv_tax_rates_callback($args) { |
| 898 | - global $wpinv_options; |
|
| 899 | - $rates = wpinv_get_tax_rates(); |
|
| 900 | - ob_start(); ?> |
|
| 898 | + global $wpinv_options; |
|
| 899 | + $rates = wpinv_get_tax_rates(); |
|
| 900 | + ob_start(); ?> |
|
| 901 | 901 | </td><tr> |
| 902 | 902 | <td colspan="2" class="wpinv_tax_tdbox"> |
| 903 | 903 | <p><?php echo $args['desc']; ?></p> |
@@ -921,40 +921,40 @@ discard block |
||
| 921 | 921 | <tr> |
| 922 | 922 | <td class="wpinv_tax_country"> |
| 923 | 923 | <?php |
| 924 | - echo wpinv_html_select( array( |
|
| 925 | - 'options' => wpinv_get_country_list( true ), |
|
| 926 | - 'name' => 'tax_rates[' . $sanitized_key . '][country]', |
|
| 924 | + echo wpinv_html_select( array( |
|
| 925 | + 'options' => wpinv_get_country_list( true ), |
|
| 926 | + 'name' => 'tax_rates[' . $sanitized_key . '][country]', |
|
| 927 | 927 | 'id' => 'tax_rates[' . $sanitized_key . '][country]', |
| 928 | - 'selected' => $rate['country'], |
|
| 929 | - 'show_option_all' => false, |
|
| 930 | - 'show_option_none' => false, |
|
| 931 | - 'class' => 'wpinv-tax-country wpi_select2', |
|
| 932 | - 'placeholder' => __( 'Choose a country', 'invoicing' ) |
|
| 933 | - ) ); |
|
| 934 | - ?> |
|
| 928 | + 'selected' => $rate['country'], |
|
| 929 | + 'show_option_all' => false, |
|
| 930 | + 'show_option_none' => false, |
|
| 931 | + 'class' => 'wpinv-tax-country wpi_select2', |
|
| 932 | + 'placeholder' => __( 'Choose a country', 'invoicing' ) |
|
| 933 | + ) ); |
|
| 934 | + ?> |
|
| 935 | 935 | </td> |
| 936 | 936 | <td class="wpinv_tax_state"> |
| 937 | 937 | <?php |
| 938 | - $states = wpinv_get_country_states( $rate['country'] ); |
|
| 939 | - if( !empty( $states ) ) { |
|
| 940 | - echo wpinv_html_select( array( |
|
| 941 | - 'options' => array_merge( array( '' => '' ), $states ), |
|
| 942 | - 'name' => 'tax_rates[' . $sanitized_key . '][state]', |
|
| 938 | + $states = wpinv_get_country_states( $rate['country'] ); |
|
| 939 | + if( !empty( $states ) ) { |
|
| 940 | + echo wpinv_html_select( array( |
|
| 941 | + 'options' => array_merge( array( '' => '' ), $states ), |
|
| 942 | + 'name' => 'tax_rates[' . $sanitized_key . '][state]', |
|
| 943 | 943 | 'id' => 'tax_rates[' . $sanitized_key . '][state]', |
| 944 | - 'selected' => $rate['state'], |
|
| 945 | - 'show_option_all' => false, |
|
| 946 | - 'show_option_none' => false, |
|
| 944 | + 'selected' => $rate['state'], |
|
| 945 | + 'show_option_all' => false, |
|
| 946 | + 'show_option_none' => false, |
|
| 947 | 947 | 'class' => 'wpi_select2', |
| 948 | - 'placeholder' => __( 'Choose a state', 'invoicing' ) |
|
| 949 | - ) ); |
|
| 950 | - } else { |
|
| 951 | - echo wpinv_html_text( array( |
|
| 952 | - 'name' => 'tax_rates[' . $sanitized_key . '][state]', $rate['state'], |
|
| 953 | - 'value' => ! empty( $rate['state'] ) ? $rate['state'] : '', |
|
| 948 | + 'placeholder' => __( 'Choose a state', 'invoicing' ) |
|
| 949 | + ) ); |
|
| 950 | + } else { |
|
| 951 | + echo wpinv_html_text( array( |
|
| 952 | + 'name' => 'tax_rates[' . $sanitized_key . '][state]', $rate['state'], |
|
| 953 | + 'value' => ! empty( $rate['state'] ) ? $rate['state'] : '', |
|
| 954 | 954 | 'id' => 'tax_rates[' . $sanitized_key . '][state]', |
| 955 | - ) ); |
|
| 956 | - } |
|
| 957 | - ?> |
|
| 955 | + ) ); |
|
| 956 | + } |
|
| 957 | + ?> |
|
| 958 | 958 | </td> |
| 959 | 959 | <td class="wpinv_tax_global"> |
| 960 | 960 | <input type="checkbox" name="tax_rates[<?php echo $sanitized_key; ?>][global]" id="tax_rates[<?php echo $sanitized_key; ?>][global]" value="1"<?php checked( true, ! empty( $rate['global'] ) ); ?>/> |
@@ -969,19 +969,19 @@ discard block |
||
| 969 | 969 | <tr> |
| 970 | 970 | <td class="wpinv_tax_country"> |
| 971 | 971 | <?php |
| 972 | - echo wpinv_html_select( array( |
|
| 973 | - 'options' => wpinv_get_country_list( true ), |
|
| 974 | - 'name' => 'tax_rates[0][country]', |
|
| 975 | - 'show_option_all' => false, |
|
| 976 | - 'show_option_none' => false, |
|
| 977 | - 'class' => 'wpinv-tax-country wpi_select2', |
|
| 978 | - 'placeholder' => __( 'Choose a country', 'invoicing' ) |
|
| 979 | - ) ); ?> |
|
| 972 | + echo wpinv_html_select( array( |
|
| 973 | + 'options' => wpinv_get_country_list( true ), |
|
| 974 | + 'name' => 'tax_rates[0][country]', |
|
| 975 | + 'show_option_all' => false, |
|
| 976 | + 'show_option_none' => false, |
|
| 977 | + 'class' => 'wpinv-tax-country wpi_select2', |
|
| 978 | + 'placeholder' => __( 'Choose a country', 'invoicing' ) |
|
| 979 | + ) ); ?> |
|
| 980 | 980 | </td> |
| 981 | 981 | <td class="wpinv_tax_state"> |
| 982 | 982 | <?php echo wpinv_html_text( array( |
| 983 | - 'name' => 'tax_rates[0][state]' |
|
| 984 | - ) ); ?> |
|
| 983 | + 'name' => 'tax_rates[0][state]' |
|
| 984 | + ) ); ?> |
|
| 985 | 985 | </td> |
| 986 | 986 | <td class="wpinv_tax_global"> |
| 987 | 987 | <input type="checkbox" name="tax_rates[0][global]" id="tax_rates[0][global]" value="1"/> |
@@ -996,7 +996,7 @@ discard block |
||
| 996 | 996 | <tfoot><tr><td colspan="5"></td><td class="wpinv_tax_action"><span class="button-secondary" id="wpinv_add_tax_rate"><?php _e( 'Add Tax Rate', 'invoicing' ); ?></span></td></tr></tfoot> |
| 997 | 997 | </table> |
| 998 | 998 | <?php |
| 999 | - echo ob_get_clean(); |
|
| 999 | + echo ob_get_clean(); |
|
| 1000 | 1000 | } |
| 1001 | 1001 | |
| 1002 | 1002 | function wpinv_tools_callback($args) { |
@@ -1024,15 +1024,15 @@ discard block |
||
| 1024 | 1024 | } |
| 1025 | 1025 | |
| 1026 | 1026 | function wpinv_descriptive_text_callback( $args ) { |
| 1027 | - echo wp_kses_post( $args['desc'] ); |
|
| 1027 | + echo wp_kses_post( $args['desc'] ); |
|
| 1028 | 1028 | } |
| 1029 | 1029 | |
| 1030 | 1030 | function wpinv_hook_callback( $args ) { |
| 1031 | - do_action( 'wpinv_' . $args['id'], $args ); |
|
| 1031 | + do_action( 'wpinv_' . $args['id'], $args ); |
|
| 1032 | 1032 | } |
| 1033 | 1033 | |
| 1034 | 1034 | function wpinv_set_settings_cap() { |
| 1035 | - return wpinv_get_capability(); |
|
| 1035 | + return wpinv_get_capability(); |
|
| 1036 | 1036 | } |
| 1037 | 1037 | add_filter( 'option_page_capability_wpinv_settings', 'wpinv_set_settings_cap' ); |
| 1038 | 1038 | |
@@ -9,7 +9,7 @@ discard block |
||
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | 11 | if ( ! defined( 'ABSPATH' ) ) { |
| 12 | - exit; |
|
| 12 | + exit; |
|
| 13 | 13 | } |
| 14 | 14 | |
| 15 | 15 | /** |
@@ -21,356 +21,356 @@ discard block |
||
| 21 | 21 | */ |
| 22 | 22 | abstract class GetPaid_Data { |
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * ID for this object. |
|
| 26 | - * |
|
| 27 | - * @since 1.0.19 |
|
| 28 | - * @var int |
|
| 29 | - */ |
|
| 30 | - protected $id = 0; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * Core data for this object. Name value pairs (name + default value). |
|
| 34 | - * |
|
| 35 | - * @since 1.0.19 |
|
| 36 | - * @var array |
|
| 37 | - */ |
|
| 38 | - protected $data = array(); |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * Core data changes for this object. |
|
| 42 | - * |
|
| 43 | - * @since 1.0.19 |
|
| 44 | - * @var array |
|
| 45 | - */ |
|
| 46 | - protected $changes = array(); |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * This is false until the object is read from the DB. |
|
| 50 | - * |
|
| 51 | - * @since 1.0.19 |
|
| 52 | - * @var bool |
|
| 53 | - */ |
|
| 54 | - protected $object_read = false; |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * This is the name of this object type. |
|
| 58 | - * |
|
| 59 | - * @since 1.0.19 |
|
| 60 | - * @var string |
|
| 61 | - */ |
|
| 62 | - protected $object_type = 'data'; |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Extra data for this object. Name value pairs (name + default value). |
|
| 66 | - * Used as a standard way for sub classes (like item types) to add |
|
| 67 | - * additional information to an inherited class. |
|
| 68 | - * |
|
| 69 | - * @since 1.0.19 |
|
| 70 | - * @var array |
|
| 71 | - */ |
|
| 72 | - protected $extra_data = array(); |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * Set to _data on construct so we can track and reset data if needed. |
|
| 76 | - * |
|
| 77 | - * @since 1.0.19 |
|
| 78 | - * @var array |
|
| 79 | - */ |
|
| 80 | - protected $default_data = array(); |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * Contains a reference to the data store for this class. |
|
| 84 | - * |
|
| 85 | - * @since 1.0.19 |
|
| 86 | - * @var GetPaid_Data_Store |
|
| 87 | - */ |
|
| 88 | - protected $data_store; |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Stores meta in cache for future reads. |
|
| 92 | - * A group must be set to to enable caching. |
|
| 93 | - * |
|
| 94 | - * @since 1.0.19 |
|
| 95 | - * @var string |
|
| 96 | - */ |
|
| 97 | - protected $cache_group = ''; |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * Stores the last error. |
|
| 101 | - * |
|
| 102 | - * @since 1.0.19 |
|
| 103 | - * @var string |
|
| 104 | - */ |
|
| 105 | - public $last_error = ''; |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * Stores additional meta data. |
|
| 109 | - * |
|
| 110 | - * @since 1.0.19 |
|
| 111 | - * @var array |
|
| 112 | - */ |
|
| 113 | - protected $meta_data = null; |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * Default constructor. |
|
| 117 | - * |
|
| 118 | - * @param int|object|array $read ID to load from the DB (optional) or already queried data. |
|
| 119 | - */ |
|
| 120 | - public function __construct( $read = 0 ) { |
|
| 121 | - $this->data = array_merge( $this->data, $this->extra_data ); |
|
| 122 | - $this->default_data = $this->data; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * Only store the object ID to avoid serializing the data object instance. |
|
| 127 | - * |
|
| 128 | - * @return array |
|
| 129 | - */ |
|
| 130 | - public function __sleep() { |
|
| 131 | - return array( 'id' ); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * Re-run the constructor with the object ID. |
|
| 136 | - * |
|
| 137 | - * If the object no longer exists, remove the ID. |
|
| 138 | - */ |
|
| 139 | - public function __wakeup() { |
|
| 140 | - $this->__construct( absint( $this->id ) ); |
|
| 141 | - |
|
| 142 | - if ( ! empty( $this->last_error ) ) { |
|
| 143 | - $this->set_id( 0 ); |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - /** |
|
| 149 | - * When the object is cloned, make sure meta is duplicated correctly. |
|
| 150 | - * |
|
| 151 | - * @since 1.0.19 |
|
| 152 | - */ |
|
| 153 | - public function __clone() { |
|
| 154 | - $this->maybe_read_meta_data(); |
|
| 155 | - if ( ! empty( $this->meta_data ) ) { |
|
| 156 | - foreach ( $this->meta_data as $array_key => $meta ) { |
|
| 157 | - $this->meta_data[ $array_key ] = clone $meta; |
|
| 158 | - if ( ! empty( $meta->id ) ) { |
|
| 159 | - $this->meta_data[ $array_key ]->id = null; |
|
| 160 | - } |
|
| 161 | - } |
|
| 162 | - } |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - /** |
|
| 166 | - * Get the data store. |
|
| 167 | - * |
|
| 168 | - * @since 1.0.19 |
|
| 169 | - * @return object |
|
| 170 | - */ |
|
| 171 | - public function get_data_store() { |
|
| 172 | - return $this->data_store; |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - /** |
|
| 176 | - * Get the object type. |
|
| 177 | - * |
|
| 178 | - * @since 1.0.19 |
|
| 179 | - * @return string |
|
| 180 | - */ |
|
| 181 | - public function get_object_type() { |
|
| 182 | - return $this->object_type; |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - /** |
|
| 186 | - * Returns the unique ID for this object. |
|
| 187 | - * |
|
| 188 | - * @since 1.0.19 |
|
| 189 | - * @return int |
|
| 190 | - */ |
|
| 191 | - public function get_id() { |
|
| 192 | - return $this->id; |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - /** |
|
| 196 | - * Get form status. |
|
| 197 | - * |
|
| 198 | - * @since 1.0.19 |
|
| 199 | - * @param string $context View or edit context. |
|
| 200 | - * @return string |
|
| 201 | - */ |
|
| 202 | - public function get_status( $context = 'view' ) { |
|
| 203 | - return $this->get_prop( 'status', $context ); |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - /** |
|
| 207 | - * Delete an object, set the ID to 0, and return result. |
|
| 208 | - * |
|
| 209 | - * @since 1.0.19 |
|
| 210 | - * @param bool $force_delete Should the data be deleted permanently. |
|
| 211 | - * @return bool result |
|
| 212 | - */ |
|
| 213 | - public function delete( $force_delete = false ) { |
|
| 214 | - if ( $this->data_store && $this->get_id() ) { |
|
| 215 | - $this->data_store->delete( $this, array( 'force_delete' => $force_delete ) ); |
|
| 216 | - $this->set_id( 0 ); |
|
| 217 | - return true; |
|
| 218 | - } |
|
| 219 | - return false; |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - /** |
|
| 223 | - * Save should create or update based on object existence. |
|
| 224 | - * |
|
| 225 | - * @since 1.0.19 |
|
| 226 | - * @return int |
|
| 227 | - */ |
|
| 228 | - public function save() { |
|
| 229 | - if ( ! $this->data_store ) { |
|
| 230 | - return $this->get_id(); |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - /** |
|
| 234 | - * Trigger action before saving to the DB. Allows you to adjust object props before save. |
|
| 235 | - * |
|
| 236 | - * @param GetPaid_Data $this The object being saved. |
|
| 237 | - * @param GetPaid_Data_Store_WP $data_store The data store persisting the data. |
|
| 238 | - */ |
|
| 239 | - do_action( 'getpaid_before_' . $this->object_type . '_object_save', $this, $this->data_store ); |
|
| 240 | - |
|
| 241 | - if ( $this->get_id() ) { |
|
| 242 | - $this->data_store->update( $this ); |
|
| 243 | - } else { |
|
| 244 | - $this->data_store->create( $this ); |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - /** |
|
| 248 | - * Trigger action after saving to the DB. |
|
| 249 | - * |
|
| 250 | - * @param GetPaid_Data $this The object being saved. |
|
| 251 | - * @param GetPaid_Data_Store_WP $data_store The data store persisting the data. |
|
| 252 | - */ |
|
| 253 | - do_action( 'getpaid_after_' . $this->object_type . '_object_save', $this, $this->data_store ); |
|
| 254 | - |
|
| 255 | - return $this->get_id(); |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - /** |
|
| 259 | - * Change data to JSON format. |
|
| 260 | - * |
|
| 261 | - * @since 1.0.19 |
|
| 262 | - * @return string Data in JSON format. |
|
| 263 | - */ |
|
| 264 | - public function __toString() { |
|
| 265 | - return wp_json_encode( $this->get_data() ); |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - /** |
|
| 269 | - * Returns all data for this object. |
|
| 270 | - * |
|
| 271 | - * @since 1.0.19 |
|
| 272 | - * @return array |
|
| 273 | - */ |
|
| 274 | - public function get_data() { |
|
| 275 | - return array_merge( array( 'id' => $this->get_id() ), $this->data, array( 'meta_data' => $this->get_meta_data() ) ); |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - /** |
|
| 279 | - * Returns array of expected data keys for this object. |
|
| 280 | - * |
|
| 281 | - * @since 1.0.19 |
|
| 282 | - * @return array |
|
| 283 | - */ |
|
| 284 | - public function get_data_keys() { |
|
| 285 | - return array_keys( $this->data ); |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - /** |
|
| 289 | - * Returns all "extra" data keys for an object (for sub objects like item types). |
|
| 290 | - * |
|
| 291 | - * @since 1.0.19 |
|
| 292 | - * @return array |
|
| 293 | - */ |
|
| 294 | - public function get_extra_data_keys() { |
|
| 295 | - return array_keys( $this->extra_data ); |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - /** |
|
| 299 | - * Filter null meta values from array. |
|
| 300 | - * |
|
| 301 | - * @since 1.0.19 |
|
| 302 | - * @param mixed $meta Meta value to check. |
|
| 303 | - * @return bool |
|
| 304 | - */ |
|
| 305 | - protected function filter_null_meta( $meta ) { |
|
| 306 | - return ! is_null( $meta->value ); |
|
| 307 | - } |
|
| 308 | - |
|
| 309 | - /** |
|
| 310 | - * Get All Meta Data. |
|
| 311 | - * |
|
| 312 | - * @since 1.0.19 |
|
| 313 | - * @return array of objects. |
|
| 314 | - */ |
|
| 315 | - public function get_meta_data() { |
|
| 316 | - $this->maybe_read_meta_data(); |
|
| 317 | - return array_values( array_filter( $this->meta_data, array( $this, 'filter_null_meta' ) ) ); |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - /** |
|
| 321 | - * Check if the key is an internal one. |
|
| 322 | - * |
|
| 323 | - * @since 1.0.19 |
|
| 324 | - * @param string $key Key to check. |
|
| 325 | - * @return bool true if it's an internal key, false otherwise |
|
| 326 | - */ |
|
| 327 | - protected function is_internal_meta_key( $key ) { |
|
| 328 | - $internal_meta_key = ! empty( $key ) && $this->data_store && in_array( $key, $this->data_store->get_internal_meta_keys(), true ); |
|
| 329 | - |
|
| 330 | - if ( ! $internal_meta_key ) { |
|
| 331 | - return false; |
|
| 332 | - } |
|
| 333 | - |
|
| 334 | - $has_setter_or_getter = is_callable( array( $this, 'set_' . $key ) ) || is_callable( array( $this, 'get_' . $key ) ); |
|
| 335 | - |
|
| 336 | - if ( ! $has_setter_or_getter ) { |
|
| 337 | - return false; |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - /* translators: %s: $key Key to check */ |
|
| 341 | - getpaid_doing_it_wrong( __FUNCTION__, sprintf( __( 'Generic add/update/get meta methods should not be used for internal meta data, including "%s". Use getters and setters.', 'getpaid' ), $key ), '1.0.19' ); |
|
| 342 | - |
|
| 343 | - return true; |
|
| 344 | - } |
|
| 345 | - |
|
| 346 | - /** |
|
| 347 | - * Magic method for setting data fields. |
|
| 348 | - * |
|
| 349 | - * This method does not update custom fields in the database. |
|
| 350 | - * |
|
| 351 | - * @since 1.0.19 |
|
| 352 | - * @access public |
|
| 353 | - * |
|
| 354 | - */ |
|
| 355 | - public function __set( $key, $value ) { |
|
| 356 | - |
|
| 357 | - if ( 'id' == strtolower( $key ) ) { |
|
| 358 | - return $this->set_id( $value ); |
|
| 359 | - } |
|
| 360 | - |
|
| 361 | - if ( method_exists( $this, "set_$key") ) { |
|
| 362 | - |
|
| 363 | - /* translators: %s: $key Key to set */ |
|
| 364 | - getpaid_doing_it_wrong( __FUNCTION__, sprintf( __( 'Object data such as "%s" should not be accessed directly. Use getters and setters.', 'getpaid' ), $key ), '1.0.19' ); |
|
| 365 | - |
|
| 366 | - call_user_func( array( $this, "set_$key" ), $value ); |
|
| 367 | - } else { |
|
| 368 | - $this->set_prop( $key, $value ); |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - } |
|
| 372 | - |
|
| 373 | - /** |
|
| 24 | + /** |
|
| 25 | + * ID for this object. |
|
| 26 | + * |
|
| 27 | + * @since 1.0.19 |
|
| 28 | + * @var int |
|
| 29 | + */ |
|
| 30 | + protected $id = 0; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * Core data for this object. Name value pairs (name + default value). |
|
| 34 | + * |
|
| 35 | + * @since 1.0.19 |
|
| 36 | + * @var array |
|
| 37 | + */ |
|
| 38 | + protected $data = array(); |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * Core data changes for this object. |
|
| 42 | + * |
|
| 43 | + * @since 1.0.19 |
|
| 44 | + * @var array |
|
| 45 | + */ |
|
| 46 | + protected $changes = array(); |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * This is false until the object is read from the DB. |
|
| 50 | + * |
|
| 51 | + * @since 1.0.19 |
|
| 52 | + * @var bool |
|
| 53 | + */ |
|
| 54 | + protected $object_read = false; |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * This is the name of this object type. |
|
| 58 | + * |
|
| 59 | + * @since 1.0.19 |
|
| 60 | + * @var string |
|
| 61 | + */ |
|
| 62 | + protected $object_type = 'data'; |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Extra data for this object. Name value pairs (name + default value). |
|
| 66 | + * Used as a standard way for sub classes (like item types) to add |
|
| 67 | + * additional information to an inherited class. |
|
| 68 | + * |
|
| 69 | + * @since 1.0.19 |
|
| 70 | + * @var array |
|
| 71 | + */ |
|
| 72 | + protected $extra_data = array(); |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * Set to _data on construct so we can track and reset data if needed. |
|
| 76 | + * |
|
| 77 | + * @since 1.0.19 |
|
| 78 | + * @var array |
|
| 79 | + */ |
|
| 80 | + protected $default_data = array(); |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * Contains a reference to the data store for this class. |
|
| 84 | + * |
|
| 85 | + * @since 1.0.19 |
|
| 86 | + * @var GetPaid_Data_Store |
|
| 87 | + */ |
|
| 88 | + protected $data_store; |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Stores meta in cache for future reads. |
|
| 92 | + * A group must be set to to enable caching. |
|
| 93 | + * |
|
| 94 | + * @since 1.0.19 |
|
| 95 | + * @var string |
|
| 96 | + */ |
|
| 97 | + protected $cache_group = ''; |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * Stores the last error. |
|
| 101 | + * |
|
| 102 | + * @since 1.0.19 |
|
| 103 | + * @var string |
|
| 104 | + */ |
|
| 105 | + public $last_error = ''; |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * Stores additional meta data. |
|
| 109 | + * |
|
| 110 | + * @since 1.0.19 |
|
| 111 | + * @var array |
|
| 112 | + */ |
|
| 113 | + protected $meta_data = null; |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * Default constructor. |
|
| 117 | + * |
|
| 118 | + * @param int|object|array $read ID to load from the DB (optional) or already queried data. |
|
| 119 | + */ |
|
| 120 | + public function __construct( $read = 0 ) { |
|
| 121 | + $this->data = array_merge( $this->data, $this->extra_data ); |
|
| 122 | + $this->default_data = $this->data; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * Only store the object ID to avoid serializing the data object instance. |
|
| 127 | + * |
|
| 128 | + * @return array |
|
| 129 | + */ |
|
| 130 | + public function __sleep() { |
|
| 131 | + return array( 'id' ); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * Re-run the constructor with the object ID. |
|
| 136 | + * |
|
| 137 | + * If the object no longer exists, remove the ID. |
|
| 138 | + */ |
|
| 139 | + public function __wakeup() { |
|
| 140 | + $this->__construct( absint( $this->id ) ); |
|
| 141 | + |
|
| 142 | + if ( ! empty( $this->last_error ) ) { |
|
| 143 | + $this->set_id( 0 ); |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + /** |
|
| 149 | + * When the object is cloned, make sure meta is duplicated correctly. |
|
| 150 | + * |
|
| 151 | + * @since 1.0.19 |
|
| 152 | + */ |
|
| 153 | + public function __clone() { |
|
| 154 | + $this->maybe_read_meta_data(); |
|
| 155 | + if ( ! empty( $this->meta_data ) ) { |
|
| 156 | + foreach ( $this->meta_data as $array_key => $meta ) { |
|
| 157 | + $this->meta_data[ $array_key ] = clone $meta; |
|
| 158 | + if ( ! empty( $meta->id ) ) { |
|
| 159 | + $this->meta_data[ $array_key ]->id = null; |
|
| 160 | + } |
|
| 161 | + } |
|
| 162 | + } |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + /** |
|
| 166 | + * Get the data store. |
|
| 167 | + * |
|
| 168 | + * @since 1.0.19 |
|
| 169 | + * @return object |
|
| 170 | + */ |
|
| 171 | + public function get_data_store() { |
|
| 172 | + return $this->data_store; |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + /** |
|
| 176 | + * Get the object type. |
|
| 177 | + * |
|
| 178 | + * @since 1.0.19 |
|
| 179 | + * @return string |
|
| 180 | + */ |
|
| 181 | + public function get_object_type() { |
|
| 182 | + return $this->object_type; |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + /** |
|
| 186 | + * Returns the unique ID for this object. |
|
| 187 | + * |
|
| 188 | + * @since 1.0.19 |
|
| 189 | + * @return int |
|
| 190 | + */ |
|
| 191 | + public function get_id() { |
|
| 192 | + return $this->id; |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + /** |
|
| 196 | + * Get form status. |
|
| 197 | + * |
|
| 198 | + * @since 1.0.19 |
|
| 199 | + * @param string $context View or edit context. |
|
| 200 | + * @return string |
|
| 201 | + */ |
|
| 202 | + public function get_status( $context = 'view' ) { |
|
| 203 | + return $this->get_prop( 'status', $context ); |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + /** |
|
| 207 | + * Delete an object, set the ID to 0, and return result. |
|
| 208 | + * |
|
| 209 | + * @since 1.0.19 |
|
| 210 | + * @param bool $force_delete Should the data be deleted permanently. |
|
| 211 | + * @return bool result |
|
| 212 | + */ |
|
| 213 | + public function delete( $force_delete = false ) { |
|
| 214 | + if ( $this->data_store && $this->get_id() ) { |
|
| 215 | + $this->data_store->delete( $this, array( 'force_delete' => $force_delete ) ); |
|
| 216 | + $this->set_id( 0 ); |
|
| 217 | + return true; |
|
| 218 | + } |
|
| 219 | + return false; |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + /** |
|
| 223 | + * Save should create or update based on object existence. |
|
| 224 | + * |
|
| 225 | + * @since 1.0.19 |
|
| 226 | + * @return int |
|
| 227 | + */ |
|
| 228 | + public function save() { |
|
| 229 | + if ( ! $this->data_store ) { |
|
| 230 | + return $this->get_id(); |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + /** |
|
| 234 | + * Trigger action before saving to the DB. Allows you to adjust object props before save. |
|
| 235 | + * |
|
| 236 | + * @param GetPaid_Data $this The object being saved. |
|
| 237 | + * @param GetPaid_Data_Store_WP $data_store The data store persisting the data. |
|
| 238 | + */ |
|
| 239 | + do_action( 'getpaid_before_' . $this->object_type . '_object_save', $this, $this->data_store ); |
|
| 240 | + |
|
| 241 | + if ( $this->get_id() ) { |
|
| 242 | + $this->data_store->update( $this ); |
|
| 243 | + } else { |
|
| 244 | + $this->data_store->create( $this ); |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + /** |
|
| 248 | + * Trigger action after saving to the DB. |
|
| 249 | + * |
|
| 250 | + * @param GetPaid_Data $this The object being saved. |
|
| 251 | + * @param GetPaid_Data_Store_WP $data_store The data store persisting the data. |
|
| 252 | + */ |
|
| 253 | + do_action( 'getpaid_after_' . $this->object_type . '_object_save', $this, $this->data_store ); |
|
| 254 | + |
|
| 255 | + return $this->get_id(); |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + /** |
|
| 259 | + * Change data to JSON format. |
|
| 260 | + * |
|
| 261 | + * @since 1.0.19 |
|
| 262 | + * @return string Data in JSON format. |
|
| 263 | + */ |
|
| 264 | + public function __toString() { |
|
| 265 | + return wp_json_encode( $this->get_data() ); |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + /** |
|
| 269 | + * Returns all data for this object. |
|
| 270 | + * |
|
| 271 | + * @since 1.0.19 |
|
| 272 | + * @return array |
|
| 273 | + */ |
|
| 274 | + public function get_data() { |
|
| 275 | + return array_merge( array( 'id' => $this->get_id() ), $this->data, array( 'meta_data' => $this->get_meta_data() ) ); |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + /** |
|
| 279 | + * Returns array of expected data keys for this object. |
|
| 280 | + * |
|
| 281 | + * @since 1.0.19 |
|
| 282 | + * @return array |
|
| 283 | + */ |
|
| 284 | + public function get_data_keys() { |
|
| 285 | + return array_keys( $this->data ); |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + /** |
|
| 289 | + * Returns all "extra" data keys for an object (for sub objects like item types). |
|
| 290 | + * |
|
| 291 | + * @since 1.0.19 |
|
| 292 | + * @return array |
|
| 293 | + */ |
|
| 294 | + public function get_extra_data_keys() { |
|
| 295 | + return array_keys( $this->extra_data ); |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + /** |
|
| 299 | + * Filter null meta values from array. |
|
| 300 | + * |
|
| 301 | + * @since 1.0.19 |
|
| 302 | + * @param mixed $meta Meta value to check. |
|
| 303 | + * @return bool |
|
| 304 | + */ |
|
| 305 | + protected function filter_null_meta( $meta ) { |
|
| 306 | + return ! is_null( $meta->value ); |
|
| 307 | + } |
|
| 308 | + |
|
| 309 | + /** |
|
| 310 | + * Get All Meta Data. |
|
| 311 | + * |
|
| 312 | + * @since 1.0.19 |
|
| 313 | + * @return array of objects. |
|
| 314 | + */ |
|
| 315 | + public function get_meta_data() { |
|
| 316 | + $this->maybe_read_meta_data(); |
|
| 317 | + return array_values( array_filter( $this->meta_data, array( $this, 'filter_null_meta' ) ) ); |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + /** |
|
| 321 | + * Check if the key is an internal one. |
|
| 322 | + * |
|
| 323 | + * @since 1.0.19 |
|
| 324 | + * @param string $key Key to check. |
|
| 325 | + * @return bool true if it's an internal key, false otherwise |
|
| 326 | + */ |
|
| 327 | + protected function is_internal_meta_key( $key ) { |
|
| 328 | + $internal_meta_key = ! empty( $key ) && $this->data_store && in_array( $key, $this->data_store->get_internal_meta_keys(), true ); |
|
| 329 | + |
|
| 330 | + if ( ! $internal_meta_key ) { |
|
| 331 | + return false; |
|
| 332 | + } |
|
| 333 | + |
|
| 334 | + $has_setter_or_getter = is_callable( array( $this, 'set_' . $key ) ) || is_callable( array( $this, 'get_' . $key ) ); |
|
| 335 | + |
|
| 336 | + if ( ! $has_setter_or_getter ) { |
|
| 337 | + return false; |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + /* translators: %s: $key Key to check */ |
|
| 341 | + getpaid_doing_it_wrong( __FUNCTION__, sprintf( __( 'Generic add/update/get meta methods should not be used for internal meta data, including "%s". Use getters and setters.', 'getpaid' ), $key ), '1.0.19' ); |
|
| 342 | + |
|
| 343 | + return true; |
|
| 344 | + } |
|
| 345 | + |
|
| 346 | + /** |
|
| 347 | + * Magic method for setting data fields. |
|
| 348 | + * |
|
| 349 | + * This method does not update custom fields in the database. |
|
| 350 | + * |
|
| 351 | + * @since 1.0.19 |
|
| 352 | + * @access public |
|
| 353 | + * |
|
| 354 | + */ |
|
| 355 | + public function __set( $key, $value ) { |
|
| 356 | + |
|
| 357 | + if ( 'id' == strtolower( $key ) ) { |
|
| 358 | + return $this->set_id( $value ); |
|
| 359 | + } |
|
| 360 | + |
|
| 361 | + if ( method_exists( $this, "set_$key") ) { |
|
| 362 | + |
|
| 363 | + /* translators: %s: $key Key to set */ |
|
| 364 | + getpaid_doing_it_wrong( __FUNCTION__, sprintf( __( 'Object data such as "%s" should not be accessed directly. Use getters and setters.', 'getpaid' ), $key ), '1.0.19' ); |
|
| 365 | + |
|
| 366 | + call_user_func( array( $this, "set_$key" ), $value ); |
|
| 367 | + } else { |
|
| 368 | + $this->set_prop( $key, $value ); |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + } |
|
| 372 | + |
|
| 373 | + /** |
|
| 374 | 374 | * Margic method for retrieving a property. |
| 375 | 375 | */ |
| 376 | 376 | public function __get( $key ) { |
@@ -378,10 +378,10 @@ discard block |
||
| 378 | 378 | // Check if we have a helper method for that. |
| 379 | 379 | if ( method_exists( $this, 'get_' . $key ) ) { |
| 380 | 380 | |
| 381 | - if ( 'post_type' != $key ) { |
|
| 382 | - /* translators: %s: $key Key to set */ |
|
| 383 | - getpaid_doing_it_wrong( __FUNCTION__, sprintf( __( 'Object data such as "%s" should not be accessed directly. Use getters and setters.', 'getpaid' ), $key ), '1.0.19' ); |
|
| 384 | - } |
|
| 381 | + if ( 'post_type' != $key ) { |
|
| 382 | + /* translators: %s: $key Key to set */ |
|
| 383 | + getpaid_doing_it_wrong( __FUNCTION__, sprintf( __( 'Object data such as "%s" should not be accessed directly. Use getters and setters.', 'getpaid' ), $key ), '1.0.19' ); |
|
| 384 | + } |
|
| 385 | 385 | |
| 386 | 386 | return call_user_func( array( $this, 'get_' . $key ) ); |
| 387 | 387 | } |
@@ -391,512 +391,512 @@ discard block |
||
| 391 | 391 | return $this->post->$key; |
| 392 | 392 | } |
| 393 | 393 | |
| 394 | - return $this->get_prop( $key ); |
|
| 395 | - |
|
| 396 | - } |
|
| 397 | - |
|
| 398 | - /** |
|
| 399 | - * Get Meta Data by Key. |
|
| 400 | - * |
|
| 401 | - * @since 1.0.19 |
|
| 402 | - * @param string $key Meta Key. |
|
| 403 | - * @param bool $single return first found meta with key, or all with $key. |
|
| 404 | - * @param string $context What the value is for. Valid values are view and edit. |
|
| 405 | - * @return mixed |
|
| 406 | - */ |
|
| 407 | - public function get_meta( $key = '', $single = true, $context = 'view' ) { |
|
| 408 | - |
|
| 409 | - // Check if this is an internal meta key. |
|
| 410 | - $_key = str_replace( '_wpinv', '', $key ); |
|
| 411 | - $_key = str_replace( 'wpinv', '', $_key ); |
|
| 412 | - if ( $this->is_internal_meta_key( $_key ) ) { |
|
| 413 | - $function = 'get_' . $_key; |
|
| 414 | - |
|
| 415 | - if ( is_callable( array( $this, $function ) ) ) { |
|
| 416 | - return $this->{$function}(); |
|
| 417 | - } |
|
| 418 | - } |
|
| 419 | - |
|
| 420 | - // Read the meta data if not yet read. |
|
| 421 | - $this->maybe_read_meta_data(); |
|
| 422 | - $meta_data = $this->get_meta_data(); |
|
| 423 | - $array_keys = array_keys( wp_list_pluck( $meta_data, 'key' ), $key, true ); |
|
| 424 | - $value = $single ? '' : array(); |
|
| 425 | - |
|
| 426 | - if ( ! empty( $array_keys ) ) { |
|
| 427 | - // We don't use the $this->meta_data property directly here because we don't want meta with a null value (i.e. meta which has been deleted via $this->delete_meta_data()). |
|
| 428 | - if ( $single ) { |
|
| 429 | - $value = $meta_data[ current( $array_keys ) ]->value; |
|
| 430 | - } else { |
|
| 431 | - $value = array_intersect_key( $meta_data, array_flip( $array_keys ) ); |
|
| 432 | - } |
|
| 433 | - } |
|
| 434 | - |
|
| 435 | - if ( 'view' === $context ) { |
|
| 436 | - $value = apply_filters( $this->get_hook_prefix() . $key, $value, $this ); |
|
| 437 | - } |
|
| 438 | - |
|
| 439 | - return $value; |
|
| 440 | - } |
|
| 441 | - |
|
| 442 | - /** |
|
| 443 | - * See if meta data exists, since get_meta always returns a '' or array(). |
|
| 444 | - * |
|
| 445 | - * @since 1.0.19 |
|
| 446 | - * @param string $key Meta Key. |
|
| 447 | - * @return boolean |
|
| 448 | - */ |
|
| 449 | - public function meta_exists( $key = '' ) { |
|
| 450 | - $this->maybe_read_meta_data(); |
|
| 451 | - $array_keys = wp_list_pluck( $this->get_meta_data(), 'key' ); |
|
| 452 | - return in_array( $key, $array_keys, true ); |
|
| 453 | - } |
|
| 454 | - |
|
| 455 | - /** |
|
| 456 | - * Set all meta data from array. |
|
| 457 | - * |
|
| 458 | - * @since 1.0.19 |
|
| 459 | - * @param array $data Key/Value pairs. |
|
| 460 | - */ |
|
| 461 | - public function set_meta_data( $data ) { |
|
| 462 | - if ( ! empty( $data ) && is_array( $data ) ) { |
|
| 463 | - $this->maybe_read_meta_data(); |
|
| 464 | - foreach ( $data as $meta ) { |
|
| 465 | - $meta = (array) $meta; |
|
| 466 | - if ( isset( $meta['key'], $meta['value'], $meta['id'] ) ) { |
|
| 467 | - $this->meta_data[] = new GetPaid_Meta_Data( |
|
| 468 | - array( |
|
| 469 | - 'id' => $meta['id'], |
|
| 470 | - 'key' => $meta['key'], |
|
| 471 | - 'value' => $meta['value'], |
|
| 472 | - ) |
|
| 473 | - ); |
|
| 474 | - } |
|
| 475 | - } |
|
| 476 | - } |
|
| 477 | - } |
|
| 478 | - |
|
| 479 | - /** |
|
| 480 | - * Add meta data. |
|
| 481 | - * |
|
| 482 | - * @since 1.0.19 |
|
| 483 | - * |
|
| 484 | - * @param string $key Meta key. |
|
| 485 | - * @param string|array $value Meta value. |
|
| 486 | - * @param bool $unique Should this be a unique key?. |
|
| 487 | - */ |
|
| 488 | - public function add_meta_data( $key, $value, $unique = false ) { |
|
| 489 | - if ( $this->is_internal_meta_key( $key ) ) { |
|
| 490 | - $function = 'set_' . $key; |
|
| 491 | - |
|
| 492 | - if ( is_callable( array( $this, $function ) ) ) { |
|
| 493 | - return $this->{$function}( $value ); |
|
| 494 | - } |
|
| 495 | - } |
|
| 496 | - |
|
| 497 | - $this->maybe_read_meta_data(); |
|
| 498 | - if ( $unique ) { |
|
| 499 | - $this->delete_meta_data( $key ); |
|
| 500 | - } |
|
| 501 | - $this->meta_data[] = new GetPaid_Meta_Data( |
|
| 502 | - array( |
|
| 503 | - 'key' => $key, |
|
| 504 | - 'value' => $value, |
|
| 505 | - ) |
|
| 506 | - ); |
|
| 507 | - } |
|
| 508 | - |
|
| 509 | - /** |
|
| 510 | - * Update meta data by key or ID, if provided. |
|
| 511 | - * |
|
| 512 | - * @since 1.0.19 |
|
| 513 | - * |
|
| 514 | - * @param string $key Meta key. |
|
| 515 | - * @param string|array $value Meta value. |
|
| 516 | - * @param int $meta_id Meta ID. |
|
| 517 | - */ |
|
| 518 | - public function update_meta_data( $key, $value, $meta_id = 0 ) { |
|
| 519 | - if ( $this->is_internal_meta_key( $key ) ) { |
|
| 520 | - $function = 'set_' . $key; |
|
| 521 | - |
|
| 522 | - if ( is_callable( array( $this, $function ) ) ) { |
|
| 523 | - return $this->{$function}( $value ); |
|
| 524 | - } |
|
| 525 | - } |
|
| 526 | - |
|
| 527 | - $this->maybe_read_meta_data(); |
|
| 528 | - |
|
| 529 | - $array_key = false; |
|
| 530 | - |
|
| 531 | - if ( $meta_id ) { |
|
| 532 | - $array_keys = array_keys( wp_list_pluck( $this->meta_data, 'id' ), $meta_id, true ); |
|
| 533 | - $array_key = $array_keys ? current( $array_keys ) : false; |
|
| 534 | - } else { |
|
| 535 | - // Find matches by key. |
|
| 536 | - $matches = array(); |
|
| 537 | - foreach ( $this->meta_data as $meta_data_array_key => $meta ) { |
|
| 538 | - if ( $meta->key === $key ) { |
|
| 539 | - $matches[] = $meta_data_array_key; |
|
| 540 | - } |
|
| 541 | - } |
|
| 542 | - |
|
| 543 | - if ( ! empty( $matches ) ) { |
|
| 544 | - // Set matches to null so only one key gets the new value. |
|
| 545 | - foreach ( $matches as $meta_data_array_key ) { |
|
| 546 | - $this->meta_data[ $meta_data_array_key ]->value = null; |
|
| 547 | - } |
|
| 548 | - $array_key = current( $matches ); |
|
| 549 | - } |
|
| 550 | - } |
|
| 551 | - |
|
| 552 | - if ( false !== $array_key ) { |
|
| 553 | - $meta = $this->meta_data[ $array_key ]; |
|
| 554 | - $meta->key = $key; |
|
| 555 | - $meta->value = $value; |
|
| 556 | - } else { |
|
| 557 | - $this->add_meta_data( $key, $value, true ); |
|
| 558 | - } |
|
| 559 | - } |
|
| 560 | - |
|
| 561 | - /** |
|
| 562 | - * Delete meta data. |
|
| 563 | - * |
|
| 564 | - * @since 1.0.19 |
|
| 565 | - * @param string $key Meta key. |
|
| 566 | - */ |
|
| 567 | - public function delete_meta_data( $key ) { |
|
| 568 | - $this->maybe_read_meta_data(); |
|
| 569 | - $array_keys = array_keys( wp_list_pluck( $this->meta_data, 'key' ), $key, true ); |
|
| 570 | - |
|
| 571 | - if ( $array_keys ) { |
|
| 572 | - foreach ( $array_keys as $array_key ) { |
|
| 573 | - $this->meta_data[ $array_key ]->value = null; |
|
| 574 | - } |
|
| 575 | - } |
|
| 576 | - } |
|
| 577 | - |
|
| 578 | - /** |
|
| 579 | - * Delete meta data. |
|
| 580 | - * |
|
| 581 | - * @since 1.0.19 |
|
| 582 | - * @param int $mid Meta ID. |
|
| 583 | - */ |
|
| 584 | - public function delete_meta_data_by_mid( $mid ) { |
|
| 585 | - $this->maybe_read_meta_data(); |
|
| 586 | - $array_keys = array_keys( wp_list_pluck( $this->meta_data, 'id' ), (int) $mid, true ); |
|
| 587 | - |
|
| 588 | - if ( $array_keys ) { |
|
| 589 | - foreach ( $array_keys as $array_key ) { |
|
| 590 | - $this->meta_data[ $array_key ]->value = null; |
|
| 591 | - } |
|
| 592 | - } |
|
| 593 | - } |
|
| 594 | - |
|
| 595 | - /** |
|
| 596 | - * Read meta data if null. |
|
| 597 | - * |
|
| 598 | - * @since 1.0.19 |
|
| 599 | - */ |
|
| 600 | - protected function maybe_read_meta_data() { |
|
| 601 | - if ( is_null( $this->meta_data ) ) { |
|
| 602 | - $this->read_meta_data(); |
|
| 603 | - } |
|
| 604 | - } |
|
| 605 | - |
|
| 606 | - /** |
|
| 607 | - * Read Meta Data from the database. Ignore any internal properties. |
|
| 608 | - * Uses it's own caches because get_metadata does not provide meta_ids. |
|
| 609 | - * |
|
| 610 | - * @since 1.0.19 |
|
| 611 | - * @param bool $force_read True to force a new DB read (and update cache). |
|
| 612 | - */ |
|
| 613 | - public function read_meta_data( $force_read = false ) { |
|
| 614 | - |
|
| 615 | - // Reset meta data. |
|
| 616 | - $this->meta_data = array(); |
|
| 617 | - |
|
| 618 | - // Maybe abort early. |
|
| 619 | - if ( ! $this->get_id() || ! $this->data_store ) { |
|
| 620 | - return; |
|
| 621 | - } |
|
| 622 | - |
|
| 623 | - // Only read from cache if the cache key is set. |
|
| 624 | - $cache_key = null; |
|
| 625 | - if ( ! $force_read && ! empty( $this->cache_group ) ) { |
|
| 626 | - $cache_key = GetPaid_Cache_Helper::get_cache_prefix( $this->cache_group ) . GetPaid_Cache_Helper::get_cache_prefix( 'object_' . $this->get_id() ) . 'object_meta_' . $this->get_id(); |
|
| 627 | - $raw_meta_data = wp_cache_get( $cache_key, $this->cache_group ); |
|
| 628 | - } |
|
| 629 | - |
|
| 630 | - // Should we force read? |
|
| 631 | - if ( empty( $raw_meta_data ) ) { |
|
| 632 | - $raw_meta_data = $this->data_store->read_meta( $this ); |
|
| 633 | - |
|
| 634 | - if ( ! empty( $cache_key ) ) { |
|
| 635 | - wp_cache_set( $cache_key, $raw_meta_data, $this->cache_group ); |
|
| 636 | - } |
|
| 637 | - |
|
| 638 | - } |
|
| 639 | - |
|
| 640 | - // Set meta data. |
|
| 641 | - if ( is_array( $raw_meta_data ) ) { |
|
| 642 | - |
|
| 643 | - foreach ( $raw_meta_data as $meta ) { |
|
| 644 | - $this->meta_data[] = new GetPaid_Meta_Data( |
|
| 645 | - array( |
|
| 646 | - 'id' => (int) $meta->meta_id, |
|
| 647 | - 'key' => $meta->meta_key, |
|
| 648 | - 'value' => maybe_unserialize( $meta->meta_value ), |
|
| 649 | - ) |
|
| 650 | - ); |
|
| 651 | - } |
|
| 652 | - |
|
| 653 | - } |
|
| 654 | - |
|
| 655 | - } |
|
| 656 | - |
|
| 657 | - /** |
|
| 658 | - * Update Meta Data in the database. |
|
| 659 | - * |
|
| 660 | - * @since 1.0.19 |
|
| 661 | - */ |
|
| 662 | - public function save_meta_data() { |
|
| 663 | - if ( ! $this->data_store || is_null( $this->meta_data ) ) { |
|
| 664 | - return; |
|
| 665 | - } |
|
| 666 | - foreach ( $this->meta_data as $array_key => $meta ) { |
|
| 667 | - if ( is_null( $meta->value ) ) { |
|
| 668 | - if ( ! empty( $meta->id ) ) { |
|
| 669 | - $this->data_store->delete_meta( $this, $meta ); |
|
| 670 | - unset( $this->meta_data[ $array_key ] ); |
|
| 671 | - } |
|
| 672 | - } elseif ( empty( $meta->id ) ) { |
|
| 673 | - $meta->id = $this->data_store->add_meta( $this, $meta ); |
|
| 674 | - $meta->apply_changes(); |
|
| 675 | - } else { |
|
| 676 | - if ( $meta->get_changes() ) { |
|
| 677 | - $this->data_store->update_meta( $this, $meta ); |
|
| 678 | - $meta->apply_changes(); |
|
| 679 | - } |
|
| 680 | - } |
|
| 681 | - } |
|
| 682 | - if ( ! empty( $this->cache_group ) ) { |
|
| 683 | - $cache_key = GetPaid_Cache_Helper::get_cache_prefix( $this->cache_group ) . GetPaid_Cache_Helper::get_cache_prefix( 'object_' . $this->get_id() ) . 'object_meta_' . $this->get_id(); |
|
| 684 | - wp_cache_delete( $cache_key, $this->cache_group ); |
|
| 685 | - } |
|
| 686 | - } |
|
| 687 | - |
|
| 688 | - /** |
|
| 689 | - * Set ID. |
|
| 690 | - * |
|
| 691 | - * @since 1.0.19 |
|
| 692 | - * @param int $id ID. |
|
| 693 | - */ |
|
| 694 | - public function set_id( $id ) { |
|
| 695 | - $this->id = absint( $id ); |
|
| 696 | - } |
|
| 697 | - |
|
| 698 | - /** |
|
| 699 | - * Sets item status. |
|
| 700 | - * |
|
| 701 | - * @since 1.0.19 |
|
| 702 | - * @param string $status New status. |
|
| 703 | - * @return array details of change. |
|
| 704 | - */ |
|
| 705 | - public function set_status( $status ) { |
|
| 394 | + return $this->get_prop( $key ); |
|
| 395 | + |
|
| 396 | + } |
|
| 397 | + |
|
| 398 | + /** |
|
| 399 | + * Get Meta Data by Key. |
|
| 400 | + * |
|
| 401 | + * @since 1.0.19 |
|
| 402 | + * @param string $key Meta Key. |
|
| 403 | + * @param bool $single return first found meta with key, or all with $key. |
|
| 404 | + * @param string $context What the value is for. Valid values are view and edit. |
|
| 405 | + * @return mixed |
|
| 406 | + */ |
|
| 407 | + public function get_meta( $key = '', $single = true, $context = 'view' ) { |
|
| 408 | + |
|
| 409 | + // Check if this is an internal meta key. |
|
| 410 | + $_key = str_replace( '_wpinv', '', $key ); |
|
| 411 | + $_key = str_replace( 'wpinv', '', $_key ); |
|
| 412 | + if ( $this->is_internal_meta_key( $_key ) ) { |
|
| 413 | + $function = 'get_' . $_key; |
|
| 414 | + |
|
| 415 | + if ( is_callable( array( $this, $function ) ) ) { |
|
| 416 | + return $this->{$function}(); |
|
| 417 | + } |
|
| 418 | + } |
|
| 419 | + |
|
| 420 | + // Read the meta data if not yet read. |
|
| 421 | + $this->maybe_read_meta_data(); |
|
| 422 | + $meta_data = $this->get_meta_data(); |
|
| 423 | + $array_keys = array_keys( wp_list_pluck( $meta_data, 'key' ), $key, true ); |
|
| 424 | + $value = $single ? '' : array(); |
|
| 425 | + |
|
| 426 | + if ( ! empty( $array_keys ) ) { |
|
| 427 | + // We don't use the $this->meta_data property directly here because we don't want meta with a null value (i.e. meta which has been deleted via $this->delete_meta_data()). |
|
| 428 | + if ( $single ) { |
|
| 429 | + $value = $meta_data[ current( $array_keys ) ]->value; |
|
| 430 | + } else { |
|
| 431 | + $value = array_intersect_key( $meta_data, array_flip( $array_keys ) ); |
|
| 432 | + } |
|
| 433 | + } |
|
| 434 | + |
|
| 435 | + if ( 'view' === $context ) { |
|
| 436 | + $value = apply_filters( $this->get_hook_prefix() . $key, $value, $this ); |
|
| 437 | + } |
|
| 438 | + |
|
| 439 | + return $value; |
|
| 440 | + } |
|
| 441 | + |
|
| 442 | + /** |
|
| 443 | + * See if meta data exists, since get_meta always returns a '' or array(). |
|
| 444 | + * |
|
| 445 | + * @since 1.0.19 |
|
| 446 | + * @param string $key Meta Key. |
|
| 447 | + * @return boolean |
|
| 448 | + */ |
|
| 449 | + public function meta_exists( $key = '' ) { |
|
| 450 | + $this->maybe_read_meta_data(); |
|
| 451 | + $array_keys = wp_list_pluck( $this->get_meta_data(), 'key' ); |
|
| 452 | + return in_array( $key, $array_keys, true ); |
|
| 453 | + } |
|
| 454 | + |
|
| 455 | + /** |
|
| 456 | + * Set all meta data from array. |
|
| 457 | + * |
|
| 458 | + * @since 1.0.19 |
|
| 459 | + * @param array $data Key/Value pairs. |
|
| 460 | + */ |
|
| 461 | + public function set_meta_data( $data ) { |
|
| 462 | + if ( ! empty( $data ) && is_array( $data ) ) { |
|
| 463 | + $this->maybe_read_meta_data(); |
|
| 464 | + foreach ( $data as $meta ) { |
|
| 465 | + $meta = (array) $meta; |
|
| 466 | + if ( isset( $meta['key'], $meta['value'], $meta['id'] ) ) { |
|
| 467 | + $this->meta_data[] = new GetPaid_Meta_Data( |
|
| 468 | + array( |
|
| 469 | + 'id' => $meta['id'], |
|
| 470 | + 'key' => $meta['key'], |
|
| 471 | + 'value' => $meta['value'], |
|
| 472 | + ) |
|
| 473 | + ); |
|
| 474 | + } |
|
| 475 | + } |
|
| 476 | + } |
|
| 477 | + } |
|
| 478 | + |
|
| 479 | + /** |
|
| 480 | + * Add meta data. |
|
| 481 | + * |
|
| 482 | + * @since 1.0.19 |
|
| 483 | + * |
|
| 484 | + * @param string $key Meta key. |
|
| 485 | + * @param string|array $value Meta value. |
|
| 486 | + * @param bool $unique Should this be a unique key?. |
|
| 487 | + */ |
|
| 488 | + public function add_meta_data( $key, $value, $unique = false ) { |
|
| 489 | + if ( $this->is_internal_meta_key( $key ) ) { |
|
| 490 | + $function = 'set_' . $key; |
|
| 491 | + |
|
| 492 | + if ( is_callable( array( $this, $function ) ) ) { |
|
| 493 | + return $this->{$function}( $value ); |
|
| 494 | + } |
|
| 495 | + } |
|
| 496 | + |
|
| 497 | + $this->maybe_read_meta_data(); |
|
| 498 | + if ( $unique ) { |
|
| 499 | + $this->delete_meta_data( $key ); |
|
| 500 | + } |
|
| 501 | + $this->meta_data[] = new GetPaid_Meta_Data( |
|
| 502 | + array( |
|
| 503 | + 'key' => $key, |
|
| 504 | + 'value' => $value, |
|
| 505 | + ) |
|
| 506 | + ); |
|
| 507 | + } |
|
| 508 | + |
|
| 509 | + /** |
|
| 510 | + * Update meta data by key or ID, if provided. |
|
| 511 | + * |
|
| 512 | + * @since 1.0.19 |
|
| 513 | + * |
|
| 514 | + * @param string $key Meta key. |
|
| 515 | + * @param string|array $value Meta value. |
|
| 516 | + * @param int $meta_id Meta ID. |
|
| 517 | + */ |
|
| 518 | + public function update_meta_data( $key, $value, $meta_id = 0 ) { |
|
| 519 | + if ( $this->is_internal_meta_key( $key ) ) { |
|
| 520 | + $function = 'set_' . $key; |
|
| 521 | + |
|
| 522 | + if ( is_callable( array( $this, $function ) ) ) { |
|
| 523 | + return $this->{$function}( $value ); |
|
| 524 | + } |
|
| 525 | + } |
|
| 526 | + |
|
| 527 | + $this->maybe_read_meta_data(); |
|
| 528 | + |
|
| 529 | + $array_key = false; |
|
| 530 | + |
|
| 531 | + if ( $meta_id ) { |
|
| 532 | + $array_keys = array_keys( wp_list_pluck( $this->meta_data, 'id' ), $meta_id, true ); |
|
| 533 | + $array_key = $array_keys ? current( $array_keys ) : false; |
|
| 534 | + } else { |
|
| 535 | + // Find matches by key. |
|
| 536 | + $matches = array(); |
|
| 537 | + foreach ( $this->meta_data as $meta_data_array_key => $meta ) { |
|
| 538 | + if ( $meta->key === $key ) { |
|
| 539 | + $matches[] = $meta_data_array_key; |
|
| 540 | + } |
|
| 541 | + } |
|
| 542 | + |
|
| 543 | + if ( ! empty( $matches ) ) { |
|
| 544 | + // Set matches to null so only one key gets the new value. |
|
| 545 | + foreach ( $matches as $meta_data_array_key ) { |
|
| 546 | + $this->meta_data[ $meta_data_array_key ]->value = null; |
|
| 547 | + } |
|
| 548 | + $array_key = current( $matches ); |
|
| 549 | + } |
|
| 550 | + } |
|
| 551 | + |
|
| 552 | + if ( false !== $array_key ) { |
|
| 553 | + $meta = $this->meta_data[ $array_key ]; |
|
| 554 | + $meta->key = $key; |
|
| 555 | + $meta->value = $value; |
|
| 556 | + } else { |
|
| 557 | + $this->add_meta_data( $key, $value, true ); |
|
| 558 | + } |
|
| 559 | + } |
|
| 560 | + |
|
| 561 | + /** |
|
| 562 | + * Delete meta data. |
|
| 563 | + * |
|
| 564 | + * @since 1.0.19 |
|
| 565 | + * @param string $key Meta key. |
|
| 566 | + */ |
|
| 567 | + public function delete_meta_data( $key ) { |
|
| 568 | + $this->maybe_read_meta_data(); |
|
| 569 | + $array_keys = array_keys( wp_list_pluck( $this->meta_data, 'key' ), $key, true ); |
|
| 570 | + |
|
| 571 | + if ( $array_keys ) { |
|
| 572 | + foreach ( $array_keys as $array_key ) { |
|
| 573 | + $this->meta_data[ $array_key ]->value = null; |
|
| 574 | + } |
|
| 575 | + } |
|
| 576 | + } |
|
| 577 | + |
|
| 578 | + /** |
|
| 579 | + * Delete meta data. |
|
| 580 | + * |
|
| 581 | + * @since 1.0.19 |
|
| 582 | + * @param int $mid Meta ID. |
|
| 583 | + */ |
|
| 584 | + public function delete_meta_data_by_mid( $mid ) { |
|
| 585 | + $this->maybe_read_meta_data(); |
|
| 586 | + $array_keys = array_keys( wp_list_pluck( $this->meta_data, 'id' ), (int) $mid, true ); |
|
| 587 | + |
|
| 588 | + if ( $array_keys ) { |
|
| 589 | + foreach ( $array_keys as $array_key ) { |
|
| 590 | + $this->meta_data[ $array_key ]->value = null; |
|
| 591 | + } |
|
| 592 | + } |
|
| 593 | + } |
|
| 594 | + |
|
| 595 | + /** |
|
| 596 | + * Read meta data if null. |
|
| 597 | + * |
|
| 598 | + * @since 1.0.19 |
|
| 599 | + */ |
|
| 600 | + protected function maybe_read_meta_data() { |
|
| 601 | + if ( is_null( $this->meta_data ) ) { |
|
| 602 | + $this->read_meta_data(); |
|
| 603 | + } |
|
| 604 | + } |
|
| 605 | + |
|
| 606 | + /** |
|
| 607 | + * Read Meta Data from the database. Ignore any internal properties. |
|
| 608 | + * Uses it's own caches because get_metadata does not provide meta_ids. |
|
| 609 | + * |
|
| 610 | + * @since 1.0.19 |
|
| 611 | + * @param bool $force_read True to force a new DB read (and update cache). |
|
| 612 | + */ |
|
| 613 | + public function read_meta_data( $force_read = false ) { |
|
| 614 | + |
|
| 615 | + // Reset meta data. |
|
| 616 | + $this->meta_data = array(); |
|
| 617 | + |
|
| 618 | + // Maybe abort early. |
|
| 619 | + if ( ! $this->get_id() || ! $this->data_store ) { |
|
| 620 | + return; |
|
| 621 | + } |
|
| 622 | + |
|
| 623 | + // Only read from cache if the cache key is set. |
|
| 624 | + $cache_key = null; |
|
| 625 | + if ( ! $force_read && ! empty( $this->cache_group ) ) { |
|
| 626 | + $cache_key = GetPaid_Cache_Helper::get_cache_prefix( $this->cache_group ) . GetPaid_Cache_Helper::get_cache_prefix( 'object_' . $this->get_id() ) . 'object_meta_' . $this->get_id(); |
|
| 627 | + $raw_meta_data = wp_cache_get( $cache_key, $this->cache_group ); |
|
| 628 | + } |
|
| 629 | + |
|
| 630 | + // Should we force read? |
|
| 631 | + if ( empty( $raw_meta_data ) ) { |
|
| 632 | + $raw_meta_data = $this->data_store->read_meta( $this ); |
|
| 633 | + |
|
| 634 | + if ( ! empty( $cache_key ) ) { |
|
| 635 | + wp_cache_set( $cache_key, $raw_meta_data, $this->cache_group ); |
|
| 636 | + } |
|
| 637 | + |
|
| 638 | + } |
|
| 639 | + |
|
| 640 | + // Set meta data. |
|
| 641 | + if ( is_array( $raw_meta_data ) ) { |
|
| 642 | + |
|
| 643 | + foreach ( $raw_meta_data as $meta ) { |
|
| 644 | + $this->meta_data[] = new GetPaid_Meta_Data( |
|
| 645 | + array( |
|
| 646 | + 'id' => (int) $meta->meta_id, |
|
| 647 | + 'key' => $meta->meta_key, |
|
| 648 | + 'value' => maybe_unserialize( $meta->meta_value ), |
|
| 649 | + ) |
|
| 650 | + ); |
|
| 651 | + } |
|
| 652 | + |
|
| 653 | + } |
|
| 654 | + |
|
| 655 | + } |
|
| 656 | + |
|
| 657 | + /** |
|
| 658 | + * Update Meta Data in the database. |
|
| 659 | + * |
|
| 660 | + * @since 1.0.19 |
|
| 661 | + */ |
|
| 662 | + public function save_meta_data() { |
|
| 663 | + if ( ! $this->data_store || is_null( $this->meta_data ) ) { |
|
| 664 | + return; |
|
| 665 | + } |
|
| 666 | + foreach ( $this->meta_data as $array_key => $meta ) { |
|
| 667 | + if ( is_null( $meta->value ) ) { |
|
| 668 | + if ( ! empty( $meta->id ) ) { |
|
| 669 | + $this->data_store->delete_meta( $this, $meta ); |
|
| 670 | + unset( $this->meta_data[ $array_key ] ); |
|
| 671 | + } |
|
| 672 | + } elseif ( empty( $meta->id ) ) { |
|
| 673 | + $meta->id = $this->data_store->add_meta( $this, $meta ); |
|
| 674 | + $meta->apply_changes(); |
|
| 675 | + } else { |
|
| 676 | + if ( $meta->get_changes() ) { |
|
| 677 | + $this->data_store->update_meta( $this, $meta ); |
|
| 678 | + $meta->apply_changes(); |
|
| 679 | + } |
|
| 680 | + } |
|
| 681 | + } |
|
| 682 | + if ( ! empty( $this->cache_group ) ) { |
|
| 683 | + $cache_key = GetPaid_Cache_Helper::get_cache_prefix( $this->cache_group ) . GetPaid_Cache_Helper::get_cache_prefix( 'object_' . $this->get_id() ) . 'object_meta_' . $this->get_id(); |
|
| 684 | + wp_cache_delete( $cache_key, $this->cache_group ); |
|
| 685 | + } |
|
| 686 | + } |
|
| 687 | + |
|
| 688 | + /** |
|
| 689 | + * Set ID. |
|
| 690 | + * |
|
| 691 | + * @since 1.0.19 |
|
| 692 | + * @param int $id ID. |
|
| 693 | + */ |
|
| 694 | + public function set_id( $id ) { |
|
| 695 | + $this->id = absint( $id ); |
|
| 696 | + } |
|
| 697 | + |
|
| 698 | + /** |
|
| 699 | + * Sets item status. |
|
| 700 | + * |
|
| 701 | + * @since 1.0.19 |
|
| 702 | + * @param string $status New status. |
|
| 703 | + * @return array details of change. |
|
| 704 | + */ |
|
| 705 | + public function set_status( $status ) { |
|
| 706 | 706 | $old_status = $this->get_status(); |
| 707 | 707 | |
| 708 | - $this->set_prop( 'status', $status ); |
|
| 709 | - |
|
| 710 | - return array( |
|
| 711 | - 'from' => $old_status, |
|
| 712 | - 'to' => $status, |
|
| 713 | - ); |
|
| 714 | - } |
|
| 715 | - |
|
| 716 | - /** |
|
| 717 | - * Set all props to default values. |
|
| 718 | - * |
|
| 719 | - * @since 1.0.19 |
|
| 720 | - */ |
|
| 721 | - public function set_defaults() { |
|
| 722 | - $this->data = $this->default_data; |
|
| 723 | - $this->changes = array(); |
|
| 724 | - $this->set_object_read( false ); |
|
| 725 | - } |
|
| 726 | - |
|
| 727 | - /** |
|
| 728 | - * Set object read property. |
|
| 729 | - * |
|
| 730 | - * @since 1.0.19 |
|
| 731 | - * @param boolean $read Should read?. |
|
| 732 | - */ |
|
| 733 | - public function set_object_read( $read = true ) { |
|
| 734 | - $this->object_read = (bool) $read; |
|
| 735 | - } |
|
| 736 | - |
|
| 737 | - /** |
|
| 738 | - * Get object read property. |
|
| 739 | - * |
|
| 740 | - * @since 1.0.19 |
|
| 741 | - * @return boolean |
|
| 742 | - */ |
|
| 743 | - public function get_object_read() { |
|
| 744 | - return (bool) $this->object_read; |
|
| 745 | - } |
|
| 746 | - |
|
| 747 | - /** |
|
| 748 | - * Set a collection of props in one go, collect any errors, and return the result. |
|
| 749 | - * Only sets using public methods. |
|
| 750 | - * |
|
| 751 | - * @since 1.0.19 |
|
| 752 | - * |
|
| 753 | - * @param array $props Key value pairs to set. Key is the prop and should map to a setter function name. |
|
| 754 | - * @param string $context In what context to run this. |
|
| 755 | - * |
|
| 756 | - * @return bool|WP_Error |
|
| 757 | - */ |
|
| 758 | - public function set_props( $props, $context = 'set' ) { |
|
| 759 | - $errors = false; |
|
| 760 | - |
|
| 761 | - foreach ( $props as $prop => $value ) { |
|
| 762 | - try { |
|
| 763 | - /** |
|
| 764 | - * Checks if the prop being set is allowed, and the value is not null. |
|
| 765 | - */ |
|
| 766 | - if ( is_null( $value ) || in_array( $prop, array( 'prop', 'date_prop', 'meta_data' ), true ) ) { |
|
| 767 | - continue; |
|
| 768 | - } |
|
| 769 | - $setter = "set_$prop"; |
|
| 770 | - |
|
| 771 | - if ( is_callable( array( $this, $setter ) ) ) { |
|
| 772 | - $this->{$setter}( $value ); |
|
| 773 | - } |
|
| 774 | - } catch ( Exception $e ) { |
|
| 775 | - if ( ! $errors ) { |
|
| 776 | - $errors = new WP_Error(); |
|
| 777 | - } |
|
| 778 | - $errors->add( $e->getCode(), $e->getMessage() ); |
|
| 779 | - $this->last_error = $e->getMessage(); |
|
| 780 | - } |
|
| 781 | - } |
|
| 782 | - |
|
| 783 | - return $errors && count( $errors->get_error_codes() ) ? $errors : true; |
|
| 784 | - } |
|
| 785 | - |
|
| 786 | - /** |
|
| 787 | - * Sets a prop for a setter method. |
|
| 788 | - * |
|
| 789 | - * This stores changes in a special array so we can track what needs saving |
|
| 790 | - * the the DB later. |
|
| 791 | - * |
|
| 792 | - * @since 1.0.19 |
|
| 793 | - * @param string $prop Name of prop to set. |
|
| 794 | - * @param mixed $value Value of the prop. |
|
| 795 | - */ |
|
| 796 | - protected function set_prop( $prop, $value ) { |
|
| 797 | - if ( array_key_exists( $prop, $this->data ) ) { |
|
| 798 | - if ( true === $this->object_read ) { |
|
| 799 | - if ( $value !== $this->data[ $prop ] || array_key_exists( $prop, $this->changes ) ) { |
|
| 800 | - $this->changes[ $prop ] = $value; |
|
| 801 | - } |
|
| 802 | - } else { |
|
| 803 | - $this->data[ $prop ] = $value; |
|
| 804 | - } |
|
| 805 | - } |
|
| 806 | - } |
|
| 807 | - |
|
| 808 | - /** |
|
| 809 | - * Return data changes only. |
|
| 810 | - * |
|
| 811 | - * @since 1.0.19 |
|
| 812 | - * @return array |
|
| 813 | - */ |
|
| 814 | - public function get_changes() { |
|
| 815 | - return $this->changes; |
|
| 816 | - } |
|
| 817 | - |
|
| 818 | - /** |
|
| 819 | - * Merge changes with data and clear. |
|
| 820 | - * |
|
| 821 | - * @since 1.0.19 |
|
| 822 | - */ |
|
| 823 | - public function apply_changes() { |
|
| 824 | - $this->data = array_replace_recursive( $this->data, $this->changes ); |
|
| 825 | - $this->changes = array(); |
|
| 826 | - } |
|
| 827 | - |
|
| 828 | - /** |
|
| 829 | - * Prefix for action and filter hooks on data. |
|
| 830 | - * |
|
| 831 | - * @since 1.0.19 |
|
| 832 | - * @return string |
|
| 833 | - */ |
|
| 834 | - protected function get_hook_prefix() { |
|
| 835 | - return 'wpinv_get_' . $this->object_type . '_'; |
|
| 836 | - } |
|
| 837 | - |
|
| 838 | - /** |
|
| 839 | - * Gets a prop for a getter method. |
|
| 840 | - * |
|
| 841 | - * Gets the value from either current pending changes, or the data itself. |
|
| 842 | - * Context controls what happens to the value before it's returned. |
|
| 843 | - * |
|
| 844 | - * @since 1.0.19 |
|
| 845 | - * @param string $prop Name of prop to get. |
|
| 846 | - * @param string $context What the value is for. Valid values are view and edit. |
|
| 847 | - * @return mixed |
|
| 848 | - */ |
|
| 849 | - protected function get_prop( $prop, $context = 'view' ) { |
|
| 850 | - $value = null; |
|
| 851 | - |
|
| 852 | - if ( array_key_exists( $prop, $this->data ) ) { |
|
| 853 | - $value = array_key_exists( $prop, $this->changes ) ? $this->changes[ $prop ] : $this->data[ $prop ]; |
|
| 854 | - |
|
| 855 | - if ( 'view' === $context ) { |
|
| 856 | - $value = apply_filters( $this->get_hook_prefix() . $prop, $value, $this ); |
|
| 857 | - } |
|
| 858 | - } |
|
| 859 | - |
|
| 860 | - return $value; |
|
| 861 | - } |
|
| 862 | - |
|
| 863 | - /** |
|
| 864 | - * Sets a date prop whilst handling formatting and datetime objects. |
|
| 865 | - * |
|
| 866 | - * @since 1.0.19 |
|
| 867 | - * @param string $prop Name of prop to set. |
|
| 868 | - * @param string|integer $value Value of the prop. |
|
| 869 | - */ |
|
| 870 | - protected function set_date_prop( $prop, $value ) { |
|
| 871 | - |
|
| 872 | - if ( empty( $value ) ) { |
|
| 873 | - $this->set_prop( $prop, null ); |
|
| 874 | - return; |
|
| 875 | - } |
|
| 876 | - $this->set_prop( $prop, $value ); |
|
| 877 | - |
|
| 878 | - } |
|
| 879 | - |
|
| 880 | - /** |
|
| 881 | - * When invalid data is found, throw an exception unless reading from the DB. |
|
| 882 | - * |
|
| 883 | - * @since 1.0.19 |
|
| 884 | - * @param string $code Error code. |
|
| 885 | - * @param string $message Error message. |
|
| 886 | - */ |
|
| 887 | - protected function error( $code, $message ) { |
|
| 888 | - $this->last_error = $message; |
|
| 889 | - } |
|
| 890 | - |
|
| 891 | - /** |
|
| 892 | - * Checks if the object is saved in the database |
|
| 893 | - * |
|
| 894 | - * @since 1.0.19 |
|
| 895 | - * @return bool |
|
| 896 | - */ |
|
| 897 | - public function exists() { |
|
| 898 | - $id = $this->get_id(); |
|
| 899 | - return ! empty( $id ); |
|
| 900 | - } |
|
| 708 | + $this->set_prop( 'status', $status ); |
|
| 709 | + |
|
| 710 | + return array( |
|
| 711 | + 'from' => $old_status, |
|
| 712 | + 'to' => $status, |
|
| 713 | + ); |
|
| 714 | + } |
|
| 715 | + |
|
| 716 | + /** |
|
| 717 | + * Set all props to default values. |
|
| 718 | + * |
|
| 719 | + * @since 1.0.19 |
|
| 720 | + */ |
|
| 721 | + public function set_defaults() { |
|
| 722 | + $this->data = $this->default_data; |
|
| 723 | + $this->changes = array(); |
|
| 724 | + $this->set_object_read( false ); |
|
| 725 | + } |
|
| 726 | + |
|
| 727 | + /** |
|
| 728 | + * Set object read property. |
|
| 729 | + * |
|
| 730 | + * @since 1.0.19 |
|
| 731 | + * @param boolean $read Should read?. |
|
| 732 | + */ |
|
| 733 | + public function set_object_read( $read = true ) { |
|
| 734 | + $this->object_read = (bool) $read; |
|
| 735 | + } |
|
| 736 | + |
|
| 737 | + /** |
|
| 738 | + * Get object read property. |
|
| 739 | + * |
|
| 740 | + * @since 1.0.19 |
|
| 741 | + * @return boolean |
|
| 742 | + */ |
|
| 743 | + public function get_object_read() { |
|
| 744 | + return (bool) $this->object_read; |
|
| 745 | + } |
|
| 746 | + |
|
| 747 | + /** |
|
| 748 | + * Set a collection of props in one go, collect any errors, and return the result. |
|
| 749 | + * Only sets using public methods. |
|
| 750 | + * |
|
| 751 | + * @since 1.0.19 |
|
| 752 | + * |
|
| 753 | + * @param array $props Key value pairs to set. Key is the prop and should map to a setter function name. |
|
| 754 | + * @param string $context In what context to run this. |
|
| 755 | + * |
|
| 756 | + * @return bool|WP_Error |
|
| 757 | + */ |
|
| 758 | + public function set_props( $props, $context = 'set' ) { |
|
| 759 | + $errors = false; |
|
| 760 | + |
|
| 761 | + foreach ( $props as $prop => $value ) { |
|
| 762 | + try { |
|
| 763 | + /** |
|
| 764 | + * Checks if the prop being set is allowed, and the value is not null. |
|
| 765 | + */ |
|
| 766 | + if ( is_null( $value ) || in_array( $prop, array( 'prop', 'date_prop', 'meta_data' ), true ) ) { |
|
| 767 | + continue; |
|
| 768 | + } |
|
| 769 | + $setter = "set_$prop"; |
|
| 770 | + |
|
| 771 | + if ( is_callable( array( $this, $setter ) ) ) { |
|
| 772 | + $this->{$setter}( $value ); |
|
| 773 | + } |
|
| 774 | + } catch ( Exception $e ) { |
|
| 775 | + if ( ! $errors ) { |
|
| 776 | + $errors = new WP_Error(); |
|
| 777 | + } |
|
| 778 | + $errors->add( $e->getCode(), $e->getMessage() ); |
|
| 779 | + $this->last_error = $e->getMessage(); |
|
| 780 | + } |
|
| 781 | + } |
|
| 782 | + |
|
| 783 | + return $errors && count( $errors->get_error_codes() ) ? $errors : true; |
|
| 784 | + } |
|
| 785 | + |
|
| 786 | + /** |
|
| 787 | + * Sets a prop for a setter method. |
|
| 788 | + * |
|
| 789 | + * This stores changes in a special array so we can track what needs saving |
|
| 790 | + * the the DB later. |
|
| 791 | + * |
|
| 792 | + * @since 1.0.19 |
|
| 793 | + * @param string $prop Name of prop to set. |
|
| 794 | + * @param mixed $value Value of the prop. |
|
| 795 | + */ |
|
| 796 | + protected function set_prop( $prop, $value ) { |
|
| 797 | + if ( array_key_exists( $prop, $this->data ) ) { |
|
| 798 | + if ( true === $this->object_read ) { |
|
| 799 | + if ( $value !== $this->data[ $prop ] || array_key_exists( $prop, $this->changes ) ) { |
|
| 800 | + $this->changes[ $prop ] = $value; |
|
| 801 | + } |
|
| 802 | + } else { |
|
| 803 | + $this->data[ $prop ] = $value; |
|
| 804 | + } |
|
| 805 | + } |
|
| 806 | + } |
|
| 807 | + |
|
| 808 | + /** |
|
| 809 | + * Return data changes only. |
|
| 810 | + * |
|
| 811 | + * @since 1.0.19 |
|
| 812 | + * @return array |
|
| 813 | + */ |
|
| 814 | + public function get_changes() { |
|
| 815 | + return $this->changes; |
|
| 816 | + } |
|
| 817 | + |
|
| 818 | + /** |
|
| 819 | + * Merge changes with data and clear. |
|
| 820 | + * |
|
| 821 | + * @since 1.0.19 |
|
| 822 | + */ |
|
| 823 | + public function apply_changes() { |
|
| 824 | + $this->data = array_replace_recursive( $this->data, $this->changes ); |
|
| 825 | + $this->changes = array(); |
|
| 826 | + } |
|
| 827 | + |
|
| 828 | + /** |
|
| 829 | + * Prefix for action and filter hooks on data. |
|
| 830 | + * |
|
| 831 | + * @since 1.0.19 |
|
| 832 | + * @return string |
|
| 833 | + */ |
|
| 834 | + protected function get_hook_prefix() { |
|
| 835 | + return 'wpinv_get_' . $this->object_type . '_'; |
|
| 836 | + } |
|
| 837 | + |
|
| 838 | + /** |
|
| 839 | + * Gets a prop for a getter method. |
|
| 840 | + * |
|
| 841 | + * Gets the value from either current pending changes, or the data itself. |
|
| 842 | + * Context controls what happens to the value before it's returned. |
|
| 843 | + * |
|
| 844 | + * @since 1.0.19 |
|
| 845 | + * @param string $prop Name of prop to get. |
|
| 846 | + * @param string $context What the value is for. Valid values are view and edit. |
|
| 847 | + * @return mixed |
|
| 848 | + */ |
|
| 849 | + protected function get_prop( $prop, $context = 'view' ) { |
|
| 850 | + $value = null; |
|
| 851 | + |
|
| 852 | + if ( array_key_exists( $prop, $this->data ) ) { |
|
| 853 | + $value = array_key_exists( $prop, $this->changes ) ? $this->changes[ $prop ] : $this->data[ $prop ]; |
|
| 854 | + |
|
| 855 | + if ( 'view' === $context ) { |
|
| 856 | + $value = apply_filters( $this->get_hook_prefix() . $prop, $value, $this ); |
|
| 857 | + } |
|
| 858 | + } |
|
| 859 | + |
|
| 860 | + return $value; |
|
| 861 | + } |
|
| 862 | + |
|
| 863 | + /** |
|
| 864 | + * Sets a date prop whilst handling formatting and datetime objects. |
|
| 865 | + * |
|
| 866 | + * @since 1.0.19 |
|
| 867 | + * @param string $prop Name of prop to set. |
|
| 868 | + * @param string|integer $value Value of the prop. |
|
| 869 | + */ |
|
| 870 | + protected function set_date_prop( $prop, $value ) { |
|
| 871 | + |
|
| 872 | + if ( empty( $value ) ) { |
|
| 873 | + $this->set_prop( $prop, null ); |
|
| 874 | + return; |
|
| 875 | + } |
|
| 876 | + $this->set_prop( $prop, $value ); |
|
| 877 | + |
|
| 878 | + } |
|
| 879 | + |
|
| 880 | + /** |
|
| 881 | + * When invalid data is found, throw an exception unless reading from the DB. |
|
| 882 | + * |
|
| 883 | + * @since 1.0.19 |
|
| 884 | + * @param string $code Error code. |
|
| 885 | + * @param string $message Error message. |
|
| 886 | + */ |
|
| 887 | + protected function error( $code, $message ) { |
|
| 888 | + $this->last_error = $message; |
|
| 889 | + } |
|
| 890 | + |
|
| 891 | + /** |
|
| 892 | + * Checks if the object is saved in the database |
|
| 893 | + * |
|
| 894 | + * @since 1.0.19 |
|
| 895 | + * @return bool |
|
| 896 | + */ |
|
| 897 | + public function exists() { |
|
| 898 | + $id = $this->get_id(); |
|
| 899 | + return ! empty( $id ); |
|
| 900 | + } |
|
| 901 | 901 | |
| 902 | 902 | } |
@@ -5,7 +5,7 @@ discard block |
||
| 5 | 5 | * |
| 6 | 6 | */ |
| 7 | 7 | if ( ! defined( 'ABSPATH' ) ) { |
| 8 | - exit; |
|
| 8 | + exit; |
|
| 9 | 9 | } |
| 10 | 10 | |
| 11 | 11 | /** |
@@ -15,196 +15,196 @@ discard block |
||
| 15 | 15 | */ |
| 16 | 16 | class GetPaid_Subscription_Data_Store { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * A map of database fields to data types. |
|
| 20 | - * |
|
| 21 | - * @since 1.0.19 |
|
| 22 | - * @var array |
|
| 23 | - */ |
|
| 24 | - protected $database_fields_to_data_type = array( |
|
| 25 | - 'id' => '%d', |
|
| 26 | - 'customer_id' => '%d', |
|
| 27 | - 'frequency' => '%d', |
|
| 28 | - 'period' => '%s', |
|
| 29 | - 'initial_amount' => '%s', |
|
| 30 | - 'recurring_amount' => '%s', |
|
| 31 | - 'bill_times' => '%d', |
|
| 32 | - 'transaction_id' => '%s', |
|
| 33 | - 'parent_payment_id' => '%d', |
|
| 34 | - 'product_id' => '%d', |
|
| 35 | - 'created' => '%s', |
|
| 36 | - 'expiration' => '%s', |
|
| 37 | - 'trial_period' => '%s', |
|
| 38 | - 'status' => '%s', |
|
| 39 | - 'profile_id' => '%s', |
|
| 40 | - ); |
|
| 41 | - |
|
| 42 | - /* |
|
| 18 | + /** |
|
| 19 | + * A map of database fields to data types. |
|
| 20 | + * |
|
| 21 | + * @since 1.0.19 |
|
| 22 | + * @var array |
|
| 23 | + */ |
|
| 24 | + protected $database_fields_to_data_type = array( |
|
| 25 | + 'id' => '%d', |
|
| 26 | + 'customer_id' => '%d', |
|
| 27 | + 'frequency' => '%d', |
|
| 28 | + 'period' => '%s', |
|
| 29 | + 'initial_amount' => '%s', |
|
| 30 | + 'recurring_amount' => '%s', |
|
| 31 | + 'bill_times' => '%d', |
|
| 32 | + 'transaction_id' => '%s', |
|
| 33 | + 'parent_payment_id' => '%d', |
|
| 34 | + 'product_id' => '%d', |
|
| 35 | + 'created' => '%s', |
|
| 36 | + 'expiration' => '%s', |
|
| 37 | + 'trial_period' => '%s', |
|
| 38 | + 'status' => '%s', |
|
| 39 | + 'profile_id' => '%s', |
|
| 40 | + ); |
|
| 41 | + |
|
| 42 | + /* |
|
| 43 | 43 | |-------------------------------------------------------------------------- |
| 44 | 44 | | CRUD Methods |
| 45 | 45 | |-------------------------------------------------------------------------- |
| 46 | 46 | */ |
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Method to create a new subscription in the database. |
|
| 50 | - * |
|
| 51 | - * @param WPInv_Subscription $subscription Subscription object. |
|
| 52 | - */ |
|
| 53 | - public function create( &$subscription ) { |
|
| 54 | - global $wpdb; |
|
| 55 | - |
|
| 56 | - $values = array(); |
|
| 57 | - $formats = array(); |
|
| 58 | - |
|
| 59 | - $fields = $this->database_fields_to_data_type; |
|
| 60 | - unset( $fields['id'] ); |
|
| 61 | - |
|
| 62 | - foreach ( $fields as $key => $format ) { |
|
| 63 | - $method = "get_$key"; |
|
| 64 | - $values[$key] = $subscription->$method( 'edit' ); |
|
| 65 | - $formats[] = $format; |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - $result = $wpdb->insert( $wpdb->prefix . 'wpinv_subscriptions', $values, $formats ); |
|
| 69 | - |
|
| 70 | - if ( $result ) { |
|
| 71 | - $subscription->set_id( $wpdb->insert_id ); |
|
| 72 | - $subscription->apply_changes(); |
|
| 73 | - $subscription->clear_cache(); |
|
| 74 | - update_post_meta( $subscription->get_parent_invoice_id(), '_wpinv_subscription_id', $subscription->get_id() ); |
|
| 75 | - do_action( 'getpaid_new_subscription', $subscription ); |
|
| 76 | - return true; |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - return false; |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * Method to read a subscription from the database. |
|
| 84 | - * |
|
| 85 | - * @param WPInv_Subscription $subscription Subscription object. |
|
| 86 | - * |
|
| 87 | - */ |
|
| 88 | - public function read( &$subscription ) { |
|
| 89 | - global $wpdb; |
|
| 90 | - |
|
| 91 | - $subscription->set_defaults(); |
|
| 92 | - |
|
| 93 | - if ( ! $subscription->get_id() ) { |
|
| 94 | - $subscription->last_error = __( 'Invalid subscription ID.', 'invoicing' ); |
|
| 95 | - $subscription->set_id( 0 ); |
|
| 96 | - return false; |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - // Maybe retrieve from the cache. |
|
| 100 | - $raw_subscription = wp_cache_get( $subscription->get_id(), 'getpaid_subscriptions' ); |
|
| 101 | - |
|
| 102 | - // If not found, retrieve from the db. |
|
| 103 | - if ( false === $raw_subscription ) { |
|
| 104 | - |
|
| 105 | - $raw_subscription = $wpdb->get_row( |
|
| 106 | - $wpdb->prepare( |
|
| 107 | - "SELECT * FROM {$wpdb->prefix}wpinv_subscriptions WHERE id = %d", |
|
| 108 | - $subscription->get_id() |
|
| 109 | - ) |
|
| 110 | - ); |
|
| 111 | - |
|
| 112 | - // Update the cache with our data |
|
| 113 | - wp_cache_set( $subscription->get_id(), $raw_subscription, 'getpaid_subscriptions' ); |
|
| 114 | - |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - if ( ! $raw_subscription ) { |
|
| 118 | - $subscription->last_error = __( 'Invalid subscription ID.', 'invoicing' ); |
|
| 119 | - return false; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - foreach ( array_keys( $this->database_fields_to_data_type ) as $key ) { |
|
| 123 | - $method = "set_$key"; |
|
| 124 | - $subscription->$method( $raw_subscription->$key ); |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - $subscription->set_object_read( true ); |
|
| 128 | - do_action( 'getpaid_read_subscription', $subscription ); |
|
| 129 | - |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * Method to update a subscription in the database. |
|
| 134 | - * |
|
| 135 | - * @param WPInv_Subscription $subscription Subscription object. |
|
| 136 | - */ |
|
| 137 | - public function update( &$subscription ) { |
|
| 138 | - global $wpdb; |
|
| 139 | - |
|
| 140 | - $changes = $subscription->get_changes(); |
|
| 141 | - $values = array(); |
|
| 142 | - $format = array(); |
|
| 143 | - |
|
| 144 | - foreach ( $this->database_fields_to_data_type as $key => $format ) { |
|
| 145 | - if ( array_key_exists( $key, $changes ) ) { |
|
| 146 | - $method = "get_$key"; |
|
| 147 | - $values[$key] = $subscription->$method( 'edit' ); |
|
| 148 | - $formats[] = $format; |
|
| 149 | - } |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - if ( empty( $values ) ) { |
|
| 153 | - return; |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - $wpdb->update( |
|
| 157 | - $wpdb->prefix . 'wpinv_subscriptions', |
|
| 158 | - $values, |
|
| 159 | - array( |
|
| 160 | - 'id' => $subscription->get_id(), |
|
| 161 | - ), |
|
| 162 | - $formats, |
|
| 163 | - '%d' |
|
| 164 | - ); |
|
| 165 | - |
|
| 166 | - // Apply the changes. |
|
| 167 | - $subscription->apply_changes(); |
|
| 168 | - |
|
| 169 | - // Delete cache. |
|
| 170 | - $subscription->clear_cache(); |
|
| 171 | - |
|
| 172 | - update_post_meta( $subscription->get_parent_invoice_id(), '_wpinv_subscr_profile_id', $subscription->get_profile_id() ); |
|
| 173 | - |
|
| 174 | - // Fire a hook. |
|
| 175 | - do_action( 'getpaid_update_subscription', $subscription ); |
|
| 176 | - |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - /** |
|
| 180 | - * Method to delete a subscription from the database. |
|
| 181 | - * |
|
| 182 | - * @param WPInv_Subscription $subscription |
|
| 183 | - */ |
|
| 184 | - public function delete( &$subscription ) { |
|
| 185 | - global $wpdb; |
|
| 186 | - |
|
| 187 | - $wpdb->query( |
|
| 188 | - $wpdb->prepare( |
|
| 189 | - "DELETE FROM {$wpdb->prefix}getpaid_subscriptions |
|
| 48 | + /** |
|
| 49 | + * Method to create a new subscription in the database. |
|
| 50 | + * |
|
| 51 | + * @param WPInv_Subscription $subscription Subscription object. |
|
| 52 | + */ |
|
| 53 | + public function create( &$subscription ) { |
|
| 54 | + global $wpdb; |
|
| 55 | + |
|
| 56 | + $values = array(); |
|
| 57 | + $formats = array(); |
|
| 58 | + |
|
| 59 | + $fields = $this->database_fields_to_data_type; |
|
| 60 | + unset( $fields['id'] ); |
|
| 61 | + |
|
| 62 | + foreach ( $fields as $key => $format ) { |
|
| 63 | + $method = "get_$key"; |
|
| 64 | + $values[$key] = $subscription->$method( 'edit' ); |
|
| 65 | + $formats[] = $format; |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + $result = $wpdb->insert( $wpdb->prefix . 'wpinv_subscriptions', $values, $formats ); |
|
| 69 | + |
|
| 70 | + if ( $result ) { |
|
| 71 | + $subscription->set_id( $wpdb->insert_id ); |
|
| 72 | + $subscription->apply_changes(); |
|
| 73 | + $subscription->clear_cache(); |
|
| 74 | + update_post_meta( $subscription->get_parent_invoice_id(), '_wpinv_subscription_id', $subscription->get_id() ); |
|
| 75 | + do_action( 'getpaid_new_subscription', $subscription ); |
|
| 76 | + return true; |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + return false; |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * Method to read a subscription from the database. |
|
| 84 | + * |
|
| 85 | + * @param WPInv_Subscription $subscription Subscription object. |
|
| 86 | + * |
|
| 87 | + */ |
|
| 88 | + public function read( &$subscription ) { |
|
| 89 | + global $wpdb; |
|
| 90 | + |
|
| 91 | + $subscription->set_defaults(); |
|
| 92 | + |
|
| 93 | + if ( ! $subscription->get_id() ) { |
|
| 94 | + $subscription->last_error = __( 'Invalid subscription ID.', 'invoicing' ); |
|
| 95 | + $subscription->set_id( 0 ); |
|
| 96 | + return false; |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + // Maybe retrieve from the cache. |
|
| 100 | + $raw_subscription = wp_cache_get( $subscription->get_id(), 'getpaid_subscriptions' ); |
|
| 101 | + |
|
| 102 | + // If not found, retrieve from the db. |
|
| 103 | + if ( false === $raw_subscription ) { |
|
| 104 | + |
|
| 105 | + $raw_subscription = $wpdb->get_row( |
|
| 106 | + $wpdb->prepare( |
|
| 107 | + "SELECT * FROM {$wpdb->prefix}wpinv_subscriptions WHERE id = %d", |
|
| 108 | + $subscription->get_id() |
|
| 109 | + ) |
|
| 110 | + ); |
|
| 111 | + |
|
| 112 | + // Update the cache with our data |
|
| 113 | + wp_cache_set( $subscription->get_id(), $raw_subscription, 'getpaid_subscriptions' ); |
|
| 114 | + |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + if ( ! $raw_subscription ) { |
|
| 118 | + $subscription->last_error = __( 'Invalid subscription ID.', 'invoicing' ); |
|
| 119 | + return false; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + foreach ( array_keys( $this->database_fields_to_data_type ) as $key ) { |
|
| 123 | + $method = "set_$key"; |
|
| 124 | + $subscription->$method( $raw_subscription->$key ); |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + $subscription->set_object_read( true ); |
|
| 128 | + do_action( 'getpaid_read_subscription', $subscription ); |
|
| 129 | + |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * Method to update a subscription in the database. |
|
| 134 | + * |
|
| 135 | + * @param WPInv_Subscription $subscription Subscription object. |
|
| 136 | + */ |
|
| 137 | + public function update( &$subscription ) { |
|
| 138 | + global $wpdb; |
|
| 139 | + |
|
| 140 | + $changes = $subscription->get_changes(); |
|
| 141 | + $values = array(); |
|
| 142 | + $format = array(); |
|
| 143 | + |
|
| 144 | + foreach ( $this->database_fields_to_data_type as $key => $format ) { |
|
| 145 | + if ( array_key_exists( $key, $changes ) ) { |
|
| 146 | + $method = "get_$key"; |
|
| 147 | + $values[$key] = $subscription->$method( 'edit' ); |
|
| 148 | + $formats[] = $format; |
|
| 149 | + } |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + if ( empty( $values ) ) { |
|
| 153 | + return; |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + $wpdb->update( |
|
| 157 | + $wpdb->prefix . 'wpinv_subscriptions', |
|
| 158 | + $values, |
|
| 159 | + array( |
|
| 160 | + 'id' => $subscription->get_id(), |
|
| 161 | + ), |
|
| 162 | + $formats, |
|
| 163 | + '%d' |
|
| 164 | + ); |
|
| 165 | + |
|
| 166 | + // Apply the changes. |
|
| 167 | + $subscription->apply_changes(); |
|
| 168 | + |
|
| 169 | + // Delete cache. |
|
| 170 | + $subscription->clear_cache(); |
|
| 171 | + |
|
| 172 | + update_post_meta( $subscription->get_parent_invoice_id(), '_wpinv_subscr_profile_id', $subscription->get_profile_id() ); |
|
| 173 | + |
|
| 174 | + // Fire a hook. |
|
| 175 | + do_action( 'getpaid_update_subscription', $subscription ); |
|
| 176 | + |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + /** |
|
| 180 | + * Method to delete a subscription from the database. |
|
| 181 | + * |
|
| 182 | + * @param WPInv_Subscription $subscription |
|
| 183 | + */ |
|
| 184 | + public function delete( &$subscription ) { |
|
| 185 | + global $wpdb; |
|
| 186 | + |
|
| 187 | + $wpdb->query( |
|
| 188 | + $wpdb->prepare( |
|
| 189 | + "DELETE FROM {$wpdb->prefix}getpaid_subscriptions |
|
| 190 | 190 | WHERE id = %d", |
| 191 | - $subscription->get_id() |
|
| 192 | - ) |
|
| 193 | - ); |
|
| 191 | + $subscription->get_id() |
|
| 192 | + ) |
|
| 193 | + ); |
|
| 194 | 194 | |
| 195 | - delete_post_meta( $subscription->get_parent_invoice_id(), '_wpinv_subscr_profile_id' ); |
|
| 196 | - delete_post_meta( $subscription->get_parent_invoice_id(), '_wpinv_subscription_id' ); |
|
| 195 | + delete_post_meta( $subscription->get_parent_invoice_id(), '_wpinv_subscr_profile_id' ); |
|
| 196 | + delete_post_meta( $subscription->get_parent_invoice_id(), '_wpinv_subscription_id' ); |
|
| 197 | 197 | |
| 198 | - // Delete cache. |
|
| 199 | - $subscription->clear_cache(); |
|
| 198 | + // Delete cache. |
|
| 199 | + $subscription->clear_cache(); |
|
| 200 | 200 | |
| 201 | - // Fire a hook. |
|
| 202 | - do_action( 'getpaid_delete_subscription', $subscription ); |
|
| 201 | + // Fire a hook. |
|
| 202 | + do_action( 'getpaid_delete_subscription', $subscription ); |
|
| 203 | 203 | |
| 204 | - $subscription->set_id( 0 ); |
|
| 205 | - } |
|
| 204 | + $subscription->set_id( 0 ); |
|
| 205 | + } |
|
| 206 | 206 | |
| 207 | - /* |
|
| 207 | + /* |
|
| 208 | 208 | |-------------------------------------------------------------------------- |
| 209 | 209 | | Additional Methods |
| 210 | 210 | |-------------------------------------------------------------------------- |
@@ -17,476 +17,476 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | class GetPaid_REST_CRUD_Controller extends GetPaid_REST_Controller { |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * Contains this controller's class name. |
|
| 22 | - * |
|
| 23 | - * @var string |
|
| 24 | - */ |
|
| 25 | - public $crud_class; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * Contains the current CRUD object. |
|
| 29 | - * |
|
| 30 | - * @var GetPaid_Data |
|
| 31 | - */ |
|
| 32 | - protected $data_object; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * Registers the routes for the objects of the controller. |
|
| 36 | - * |
|
| 37 | - * @since 1.0.19 |
|
| 38 | - * |
|
| 39 | - * @see register_rest_route() |
|
| 40 | - */ |
|
| 41 | - public function register_namespace_routes( $namespace ) { |
|
| 42 | - |
|
| 43 | - register_rest_route( |
|
| 44 | - $namespace, |
|
| 45 | - '/' . $this->rest_base, |
|
| 46 | - array( |
|
| 47 | - array( |
|
| 48 | - 'methods' => WP_REST_Server::READABLE, |
|
| 49 | - 'callback' => array( $this, 'get_items' ), |
|
| 50 | - 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
|
| 51 | - 'args' => $this->get_collection_params(), |
|
| 52 | - ), |
|
| 53 | - array( |
|
| 54 | - 'methods' => WP_REST_Server::CREATABLE, |
|
| 55 | - 'callback' => array( $this, 'create_item' ), |
|
| 56 | - 'permission_callback' => array( $this, 'create_item_permissions_check' ), |
|
| 57 | - 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), |
|
| 58 | - ), |
|
| 59 | - 'schema' => array( $this, 'get_public_item_schema' ), |
|
| 60 | - ) |
|
| 61 | - ); |
|
| 62 | - |
|
| 63 | - $get_item_args = array( |
|
| 64 | - 'context' => $this->get_context_param( array( 'default' => 'view' ) ), |
|
| 65 | - ); |
|
| 66 | - |
|
| 67 | - register_rest_route( |
|
| 68 | - $namespace, |
|
| 69 | - '/' . $this->rest_base . '/(?P<id>[\d]+)', |
|
| 70 | - array( |
|
| 71 | - 'args' => array( |
|
| 72 | - 'id' => array( |
|
| 73 | - 'description' => __( 'Unique identifier for the object.', 'invoicing' ), |
|
| 74 | - 'type' => 'integer', |
|
| 75 | - ), |
|
| 76 | - ), |
|
| 77 | - array( |
|
| 78 | - 'methods' => WP_REST_Server::READABLE, |
|
| 79 | - 'callback' => array( $this, 'get_item' ), |
|
| 80 | - 'permission_callback' => array( $this, 'get_item_permissions_check' ), |
|
| 81 | - 'args' => $get_item_args, |
|
| 82 | - ), |
|
| 83 | - array( |
|
| 84 | - 'methods' => WP_REST_Server::EDITABLE, |
|
| 85 | - 'callback' => array( $this, 'update_item' ), |
|
| 86 | - 'permission_callback' => array( $this, 'update_item_permissions_check' ), |
|
| 87 | - 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), |
|
| 88 | - ), |
|
| 89 | - array( |
|
| 90 | - 'methods' => WP_REST_Server::DELETABLE, |
|
| 91 | - 'callback' => array( $this, 'delete_item' ), |
|
| 92 | - 'permission_callback' => array( $this, 'delete_item_permissions_check' ), |
|
| 93 | - 'args' => array( |
|
| 94 | - 'force' => array( |
|
| 95 | - 'type' => 'boolean', |
|
| 96 | - 'default' => false, |
|
| 97 | - 'description' => __( 'Whether to bypass Trash and force deletion.', 'invoicing' ), |
|
| 98 | - ), |
|
| 99 | - ), |
|
| 100 | - ), |
|
| 101 | - 'schema' => array( $this, 'get_public_item_schema' ), |
|
| 102 | - ) |
|
| 103 | - ); |
|
| 104 | - |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * Saves a single object. |
|
| 109 | - * |
|
| 110 | - * @param GetPaid_Data $object Object to save. |
|
| 111 | - * @return WP_Error|GetPaid_Data |
|
| 112 | - */ |
|
| 113 | - protected function save_object( $object ) { |
|
| 114 | - $object->save(); |
|
| 115 | - |
|
| 116 | - if ( ! empty( $object->last_error ) ) { |
|
| 117 | - return new WP_Error( 'rest_cannot_save', $object->last_error, array( 'status' => 400 ) ); |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - return new $this->crud_class( $object->get_id() ); |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Retrieves a single object. |
|
| 125 | - * |
|
| 126 | - * @since 1.0.13 |
|
| 127 | - * |
|
| 128 | - * @param int|WP_Post $object_id Supplied ID. |
|
| 129 | - * @return GetPaid_Data|WP_Error GetPaid_Data object if ID is valid, WP_Error otherwise. |
|
| 130 | - */ |
|
| 131 | - protected function get_object( $object_id ) { |
|
| 132 | - |
|
| 133 | - // Do we have an object? |
|
| 134 | - if ( empty( $this->crud_class ) || ! class_exists( $this->crud_class ) ) { |
|
| 135 | - return new WP_Error( 'no_crud_class', __( 'You need to specify a CRUD class for this controller', 'invoicing' ) ); |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - // Fetch the object. |
|
| 139 | - $object = new $this->crud_class( $object_id ); |
|
| 140 | - if ( ! empty( $object->last_error ) ) { |
|
| 141 | - return new WP_Error( 'rest_object_invalid_id', $object->last_error, array( 'status' => 404 ) ); |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - $this->data_object = $object; |
|
| 145 | - return $object->get_id() ? $object : new WP_Error( 'rest_object_invalid_id', __( 'Invalid ID.', 'invoicing' ), array( 'status' => 404 ) ); |
|
| 146 | - |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * Get a single object. |
|
| 151 | - * |
|
| 152 | - * @param WP_REST_Request $request Full details about the request. |
|
| 153 | - * @return WP_Error|WP_REST_Response |
|
| 154 | - */ |
|
| 155 | - public function get_item( $request ) { |
|
| 156 | - |
|
| 157 | - // Fetch the item. |
|
| 158 | - $object = $this->get_object( $request['id'] ); |
|
| 159 | - |
|
| 160 | - if ( is_wp_error( $object ) ) { |
|
| 161 | - return $object; |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - // Generate a response. |
|
| 165 | - return rest_ensure_response( $this->prepare_item_for_response( $object, $request ) ); |
|
| 166 | - |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * Create a single object. |
|
| 171 | - * |
|
| 172 | - * @param WP_REST_Request $request Full details about the request. |
|
| 173 | - * @return WP_Error|WP_REST_Response |
|
| 174 | - */ |
|
| 175 | - public function create_item( $request ) { |
|
| 176 | - |
|
| 177 | - // Can not create an existing item. |
|
| 178 | - if ( ! empty( $request['id'] ) ) { |
|
| 179 | - /* translators: %s: post type */ |
|
| 180 | - return new WP_Error( "getpaid_rest_{$this->rest_base}_exists", __( 'Cannot create existing resource.', 'invoicing' ), array( 'status' => 400 ) ); |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - // Generate a GetPaid_Data object from the request. |
|
| 184 | - $object = $this->prepare_item_for_database( $request ); |
|
| 185 | - if ( is_wp_error( $object ) ) { |
|
| 186 | - return $object; |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - // Save the object. |
|
| 190 | - $object = $this->save_object( $object ); |
|
| 191 | - if ( is_wp_error( $object ) ) { |
|
| 192 | - return $object; |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - // Save special fields. |
|
| 196 | - $save_special = $this->update_additional_fields_for_object( $object, $request ); |
|
| 197 | - if ( is_wp_error( $save_special ) ) { |
|
| 198 | - $object->delete( true ); |
|
| 199 | - return $save_special; |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - $request->set_param( 'context', 'edit' ); |
|
| 203 | - $response = $this->prepare_item_for_response( $object, $request ); |
|
| 204 | - $response = rest_ensure_response( $response ); |
|
| 205 | - $response->set_status( 201 ); |
|
| 206 | - $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $object->get_id() ) ) ); |
|
| 207 | - |
|
| 208 | - return $response; |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - /** |
|
| 212 | - * Update a single object. |
|
| 213 | - * |
|
| 214 | - * @param WP_REST_Request $request Full details about the request. |
|
| 215 | - * @return WP_Error|WP_REST_Response |
|
| 216 | - */ |
|
| 217 | - public function update_item( $request ) { |
|
| 218 | - |
|
| 219 | - // Fetch the item. |
|
| 220 | - $object = $this->get_object( $request['id'] ); |
|
| 221 | - if ( is_wp_error( $object ) ) { |
|
| 222 | - return $object; |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - // Prepare the item for saving. |
|
| 226 | - $object = $this->prepare_item_for_database( $request ); |
|
| 227 | - if ( is_wp_error( $object ) ) { |
|
| 228 | - return $object; |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - // Save the item. |
|
| 232 | - $object = $this->save_object( $object ); |
|
| 233 | - if ( is_wp_error( $object ) ) { |
|
| 234 | - return $object; |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - // Save special fields (those added via hooks). |
|
| 238 | - $save_special = $this->update_additional_fields_for_object( $object, $request ); |
|
| 239 | - if ( is_wp_error( $save_special ) ) { |
|
| 240 | - return $save_special; |
|
| 241 | - } |
|
| 242 | - |
|
| 243 | - $request->set_param( 'context', 'edit' ); |
|
| 244 | - $response = $this->prepare_item_for_response( $object, $request ); |
|
| 245 | - return rest_ensure_response( $response ); |
|
| 246 | - } |
|
| 247 | - |
|
| 248 | - /** |
|
| 249 | - * Prepare links for the request. |
|
| 250 | - * |
|
| 251 | - * @param GetPaid_Data $object GetPaid_Data object. |
|
| 252 | - * @return array Links for the given object. |
|
| 253 | - */ |
|
| 254 | - protected function prepare_links( $object ) { |
|
| 255 | - |
|
| 256 | - $links = array( |
|
| 257 | - 'self' => array( |
|
| 258 | - 'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $object->get_id() ) ), |
|
| 259 | - ), |
|
| 260 | - 'collection' => array( |
|
| 261 | - 'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ), |
|
| 262 | - ), |
|
| 263 | - ); |
|
| 264 | - |
|
| 265 | - return $links; |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - /** |
|
| 269 | - * Get the query params for collections of attachments. |
|
| 270 | - * |
|
| 271 | - * @return array |
|
| 272 | - */ |
|
| 273 | - public function get_collection_params() { |
|
| 274 | - $params = parent::get_collection_params(); |
|
| 275 | - $params['context']['default'] = 'view'; |
|
| 276 | - return $params; |
|
| 277 | - } |
|
| 278 | - |
|
| 279 | - /** |
|
| 280 | - * Only return writable props from schema. |
|
| 281 | - * |
|
| 282 | - * @param array $schema Schema. |
|
| 283 | - * @return bool |
|
| 284 | - */ |
|
| 285 | - public function filter_writable_props( $schema ) { |
|
| 286 | - return empty( $schema['readonly'] ); |
|
| 287 | - } |
|
| 288 | - |
|
| 289 | - /** |
|
| 290 | - * Prepare a single object for create or update. |
|
| 291 | - * |
|
| 292 | - * @since 1.0.19 |
|
| 293 | - * @param WP_REST_Request $request Request object. |
|
| 294 | - * @return GetPaid_Data|WP_Error Data object or WP_Error. |
|
| 295 | - */ |
|
| 296 | - protected function prepare_item_for_database( $request ) { |
|
| 297 | - |
|
| 298 | - // Do we have an object? |
|
| 299 | - if ( empty( $this->crud_class ) || ! class_exists( $this->crud_class ) ) { |
|
| 300 | - return new WP_Error( 'no_crud_class', __( 'You need to specify a CRUD class for this controller', 'invoicing' ) ); |
|
| 301 | - } |
|
| 302 | - |
|
| 303 | - // Prepare the object. |
|
| 304 | - $id = isset( $request['id'] ) ? absint( $request['id'] ) : 0; |
|
| 305 | - $object = new $this->crud_class( $id ); |
|
| 306 | - |
|
| 307 | - // Abort if an error exists. |
|
| 308 | - if ( ! empty( $object->last_error ) ) { |
|
| 309 | - return new WP_Error( 'invalid_item', $object->last_error ); |
|
| 310 | - } |
|
| 311 | - |
|
| 312 | - $schema = $this->get_item_schema(); |
|
| 313 | - $data_keys = array_keys( array_filter( $schema['properties'], array( $this, 'filter_writable_props' ) ) ); |
|
| 314 | - |
|
| 315 | - // Handle all writable props. |
|
| 316 | - foreach ( $data_keys as $key ) { |
|
| 317 | - $value = $request[ $key ]; |
|
| 318 | - |
|
| 319 | - if ( ! is_null( $value ) ) { |
|
| 320 | - switch ( $key ) { |
|
| 321 | - |
|
| 322 | - case 'meta_data': |
|
| 323 | - if ( is_array( $value ) ) { |
|
| 324 | - foreach ( $value as $meta ) { |
|
| 325 | - $object->update_meta_data( $meta['key'], $meta['value'], isset( $meta['id'] ) ? $meta['id'] : '' ); |
|
| 326 | - } |
|
| 327 | - } |
|
| 328 | - break; |
|
| 329 | - |
|
| 330 | - default: |
|
| 331 | - if ( is_callable( array( $object, "set_{$key}" ) ) ) { |
|
| 332 | - $object->{"set_{$key}"}( $value ); |
|
| 333 | - } |
|
| 334 | - break; |
|
| 335 | - } |
|
| 336 | - } |
|
| 337 | - |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - // Filters an object before it is inserted via the REST API.. |
|
| 341 | - return apply_filters( "getpaid_rest_pre_insert_{$this->rest_base}_object", $object, $request ); |
|
| 342 | - } |
|
| 343 | - |
|
| 344 | - /** |
|
| 345 | - * Retrieves data from a GetPaid class. |
|
| 346 | - * |
|
| 347 | - * @since 1.0.19 |
|
| 348 | - * @param GetPaid_Meta_Data[] $meta_data meta data objects. |
|
| 349 | - * @return array |
|
| 350 | - */ |
|
| 351 | - protected function prepare_object_meta_data( $meta_data ) { |
|
| 352 | - $meta = array(); |
|
| 353 | - |
|
| 354 | - foreach( $meta_data as $object ) { |
|
| 355 | - $meta[] = $object->get_data(); |
|
| 356 | - } |
|
| 357 | - |
|
| 358 | - return $meta; |
|
| 359 | - } |
|
| 360 | - |
|
| 361 | - /** |
|
| 362 | - * Retrieves invoice items. |
|
| 363 | - * |
|
| 364 | - * @since 1.0.19 |
|
| 365 | - * @param WPInv_Invoice $invoice Invoice items. |
|
| 366 | - * @param array $fields Fields to include. |
|
| 367 | - * @return array |
|
| 368 | - */ |
|
| 369 | - protected function prepare_invoice_items( $invoice ) { |
|
| 370 | - $items = array(); |
|
| 371 | - |
|
| 372 | - foreach( $invoice->get_items() as $item ) { |
|
| 373 | - |
|
| 374 | - $item_data = $item->prepare_data_for_saving(); |
|
| 375 | - |
|
| 376 | - if ( 'amount' == $invoice->get_template() ) { |
|
| 377 | - $item_data['quantity'] = 1; |
|
| 378 | - } |
|
| 379 | - |
|
| 380 | - $items[] = $item_data; |
|
| 381 | - } |
|
| 382 | - |
|
| 383 | - return $items; |
|
| 384 | - } |
|
| 385 | - |
|
| 386 | - /** |
|
| 387 | - * Retrieves data from a GetPaid class. |
|
| 388 | - * |
|
| 389 | - * @since 1.0.19 |
|
| 390 | - * @param GetPaid_Data $object Data object. |
|
| 391 | - * @param array $fields Fields to include. |
|
| 392 | - * @param string $context either view or edit. |
|
| 393 | - * @return array |
|
| 394 | - */ |
|
| 395 | - protected function prepare_object_data( $object, $fields, $context = 'view' ) { |
|
| 396 | - |
|
| 397 | - $data = array(); |
|
| 398 | - |
|
| 399 | - // Handle all writable props. |
|
| 400 | - foreach ( array_keys( $this->get_schema_properties() ) as $key ) { |
|
| 401 | - |
|
| 402 | - // Abort if it is not included. |
|
| 403 | - if ( ! empty( $fields ) && ! $this->is_field_included( $key, $fields ) ) { |
|
| 404 | - continue; |
|
| 405 | - } |
|
| 406 | - |
|
| 407 | - // Or this current object does not support the field. |
|
| 408 | - if ( ! $this->object_supports_field( $object, $key ) ) { |
|
| 409 | - continue; |
|
| 410 | - } |
|
| 411 | - |
|
| 412 | - // Handle meta data. |
|
| 413 | - if ( $key == 'meta_data' ) { |
|
| 414 | - $data['meta_data'] = $this->prepare_object_meta_data( $object->get_meta_data() ); |
|
| 415 | - continue; |
|
| 416 | - } |
|
| 417 | - |
|
| 418 | - // Handle items. |
|
| 419 | - if ( $key == 'items' && is_a( $object, 'WPInv_Invoice' ) ) { |
|
| 420 | - $data['items'] = $this->prepare_invoice_items( $object ); |
|
| 421 | - continue; |
|
| 422 | - } |
|
| 423 | - |
|
| 424 | - // Booleans. |
|
| 425 | - if ( is_callable( array( $object, $key ) ) ) { |
|
| 426 | - $data[ $key ] = $object->$key( $context ); |
|
| 427 | - continue; |
|
| 428 | - } |
|
| 429 | - |
|
| 430 | - // Get object value. |
|
| 431 | - if ( is_callable( array( $object, "get_{$key}" ) ) ) { |
|
| 432 | - $value = $object->{"get_{$key}"}( $context ); |
|
| 433 | - |
|
| 434 | - // If the value is an instance of GetPaid_Data... |
|
| 435 | - if ( is_a( $value, 'GetPaid_Data' ) ) { |
|
| 436 | - $value = $value->get_data( $context ); |
|
| 437 | - } |
|
| 438 | - |
|
| 439 | - // For objects, retrieves it's properties. |
|
| 440 | - $data[ $key ] = is_object( $value ) ? get_object_vars( $value ) : $value ; |
|
| 441 | - continue; |
|
| 442 | - } |
|
| 443 | - |
|
| 444 | - } |
|
| 445 | - |
|
| 446 | - return $data; |
|
| 447 | - } |
|
| 448 | - |
|
| 449 | - /** |
|
| 450 | - * Checks if a key should be included in a response. |
|
| 451 | - * |
|
| 452 | - * @since 1.0.19 |
|
| 453 | - * @param GetPaid_Data $object Data object. |
|
| 454 | - * @param string $field_key The key to check for. |
|
| 455 | - * @return bool |
|
| 456 | - */ |
|
| 457 | - public function object_supports_field( $object, $field_key ) { |
|
| 458 | - return apply_filters( 'getpaid_rest_object_supports_key', true, $object, $field_key ); |
|
| 459 | - } |
|
| 460 | - |
|
| 461 | - /** |
|
| 462 | - * Prepare a single object output for response. |
|
| 463 | - * |
|
| 464 | - * @since 1.0.19 |
|
| 465 | - * @param GetPaid_Data $object Data object. |
|
| 466 | - * @param WP_REST_Request $request Request object. |
|
| 467 | - * @return WP_REST_Response |
|
| 468 | - */ |
|
| 469 | - public function prepare_item_for_response( $object, $request ) { |
|
| 470 | - remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 ); |
|
| 471 | - |
|
| 472 | - $this->data_object = $object; |
|
| 473 | - |
|
| 474 | - // Fetch the fields to include in this response. |
|
| 475 | - $fields = $this->get_fields_for_response( $request ); |
|
| 476 | - |
|
| 477 | - // Prepare object data. |
|
| 478 | - $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
|
| 479 | - $data = $this->prepare_object_data( $object, $fields, $context ); |
|
| 480 | - $data = $this->add_additional_fields_to_object( $data, $request ); |
|
| 481 | - $data = $this->limit_object_to_requested_fields( $data, $fields ); |
|
| 482 | - $data = $this->filter_response_by_context( $data, $context ); |
|
| 483 | - |
|
| 484 | - // Prepare the response. |
|
| 485 | - $response = rest_ensure_response( $data ); |
|
| 486 | - $response->add_links( $this->prepare_links( $object, $request ) ); |
|
| 487 | - |
|
| 488 | - // Filter item response. |
|
| 489 | - return apply_filters( "getpaid_rest_prepare_{$this->rest_base}_object", $response, $object, $request ); |
|
| 490 | - } |
|
| 20 | + /** |
|
| 21 | + * Contains this controller's class name. |
|
| 22 | + * |
|
| 23 | + * @var string |
|
| 24 | + */ |
|
| 25 | + public $crud_class; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * Contains the current CRUD object. |
|
| 29 | + * |
|
| 30 | + * @var GetPaid_Data |
|
| 31 | + */ |
|
| 32 | + protected $data_object; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * Registers the routes for the objects of the controller. |
|
| 36 | + * |
|
| 37 | + * @since 1.0.19 |
|
| 38 | + * |
|
| 39 | + * @see register_rest_route() |
|
| 40 | + */ |
|
| 41 | + public function register_namespace_routes( $namespace ) { |
|
| 42 | + |
|
| 43 | + register_rest_route( |
|
| 44 | + $namespace, |
|
| 45 | + '/' . $this->rest_base, |
|
| 46 | + array( |
|
| 47 | + array( |
|
| 48 | + 'methods' => WP_REST_Server::READABLE, |
|
| 49 | + 'callback' => array( $this, 'get_items' ), |
|
| 50 | + 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
|
| 51 | + 'args' => $this->get_collection_params(), |
|
| 52 | + ), |
|
| 53 | + array( |
|
| 54 | + 'methods' => WP_REST_Server::CREATABLE, |
|
| 55 | + 'callback' => array( $this, 'create_item' ), |
|
| 56 | + 'permission_callback' => array( $this, 'create_item_permissions_check' ), |
|
| 57 | + 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), |
|
| 58 | + ), |
|
| 59 | + 'schema' => array( $this, 'get_public_item_schema' ), |
|
| 60 | + ) |
|
| 61 | + ); |
|
| 62 | + |
|
| 63 | + $get_item_args = array( |
|
| 64 | + 'context' => $this->get_context_param( array( 'default' => 'view' ) ), |
|
| 65 | + ); |
|
| 66 | + |
|
| 67 | + register_rest_route( |
|
| 68 | + $namespace, |
|
| 69 | + '/' . $this->rest_base . '/(?P<id>[\d]+)', |
|
| 70 | + array( |
|
| 71 | + 'args' => array( |
|
| 72 | + 'id' => array( |
|
| 73 | + 'description' => __( 'Unique identifier for the object.', 'invoicing' ), |
|
| 74 | + 'type' => 'integer', |
|
| 75 | + ), |
|
| 76 | + ), |
|
| 77 | + array( |
|
| 78 | + 'methods' => WP_REST_Server::READABLE, |
|
| 79 | + 'callback' => array( $this, 'get_item' ), |
|
| 80 | + 'permission_callback' => array( $this, 'get_item_permissions_check' ), |
|
| 81 | + 'args' => $get_item_args, |
|
| 82 | + ), |
|
| 83 | + array( |
|
| 84 | + 'methods' => WP_REST_Server::EDITABLE, |
|
| 85 | + 'callback' => array( $this, 'update_item' ), |
|
| 86 | + 'permission_callback' => array( $this, 'update_item_permissions_check' ), |
|
| 87 | + 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), |
|
| 88 | + ), |
|
| 89 | + array( |
|
| 90 | + 'methods' => WP_REST_Server::DELETABLE, |
|
| 91 | + 'callback' => array( $this, 'delete_item' ), |
|
| 92 | + 'permission_callback' => array( $this, 'delete_item_permissions_check' ), |
|
| 93 | + 'args' => array( |
|
| 94 | + 'force' => array( |
|
| 95 | + 'type' => 'boolean', |
|
| 96 | + 'default' => false, |
|
| 97 | + 'description' => __( 'Whether to bypass Trash and force deletion.', 'invoicing' ), |
|
| 98 | + ), |
|
| 99 | + ), |
|
| 100 | + ), |
|
| 101 | + 'schema' => array( $this, 'get_public_item_schema' ), |
|
| 102 | + ) |
|
| 103 | + ); |
|
| 104 | + |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * Saves a single object. |
|
| 109 | + * |
|
| 110 | + * @param GetPaid_Data $object Object to save. |
|
| 111 | + * @return WP_Error|GetPaid_Data |
|
| 112 | + */ |
|
| 113 | + protected function save_object( $object ) { |
|
| 114 | + $object->save(); |
|
| 115 | + |
|
| 116 | + if ( ! empty( $object->last_error ) ) { |
|
| 117 | + return new WP_Error( 'rest_cannot_save', $object->last_error, array( 'status' => 400 ) ); |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + return new $this->crud_class( $object->get_id() ); |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Retrieves a single object. |
|
| 125 | + * |
|
| 126 | + * @since 1.0.13 |
|
| 127 | + * |
|
| 128 | + * @param int|WP_Post $object_id Supplied ID. |
|
| 129 | + * @return GetPaid_Data|WP_Error GetPaid_Data object if ID is valid, WP_Error otherwise. |
|
| 130 | + */ |
|
| 131 | + protected function get_object( $object_id ) { |
|
| 132 | + |
|
| 133 | + // Do we have an object? |
|
| 134 | + if ( empty( $this->crud_class ) || ! class_exists( $this->crud_class ) ) { |
|
| 135 | + return new WP_Error( 'no_crud_class', __( 'You need to specify a CRUD class for this controller', 'invoicing' ) ); |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + // Fetch the object. |
|
| 139 | + $object = new $this->crud_class( $object_id ); |
|
| 140 | + if ( ! empty( $object->last_error ) ) { |
|
| 141 | + return new WP_Error( 'rest_object_invalid_id', $object->last_error, array( 'status' => 404 ) ); |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + $this->data_object = $object; |
|
| 145 | + return $object->get_id() ? $object : new WP_Error( 'rest_object_invalid_id', __( 'Invalid ID.', 'invoicing' ), array( 'status' => 404 ) ); |
|
| 146 | + |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * Get a single object. |
|
| 151 | + * |
|
| 152 | + * @param WP_REST_Request $request Full details about the request. |
|
| 153 | + * @return WP_Error|WP_REST_Response |
|
| 154 | + */ |
|
| 155 | + public function get_item( $request ) { |
|
| 156 | + |
|
| 157 | + // Fetch the item. |
|
| 158 | + $object = $this->get_object( $request['id'] ); |
|
| 159 | + |
|
| 160 | + if ( is_wp_error( $object ) ) { |
|
| 161 | + return $object; |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + // Generate a response. |
|
| 165 | + return rest_ensure_response( $this->prepare_item_for_response( $object, $request ) ); |
|
| 166 | + |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * Create a single object. |
|
| 171 | + * |
|
| 172 | + * @param WP_REST_Request $request Full details about the request. |
|
| 173 | + * @return WP_Error|WP_REST_Response |
|
| 174 | + */ |
|
| 175 | + public function create_item( $request ) { |
|
| 176 | + |
|
| 177 | + // Can not create an existing item. |
|
| 178 | + if ( ! empty( $request['id'] ) ) { |
|
| 179 | + /* translators: %s: post type */ |
|
| 180 | + return new WP_Error( "getpaid_rest_{$this->rest_base}_exists", __( 'Cannot create existing resource.', 'invoicing' ), array( 'status' => 400 ) ); |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + // Generate a GetPaid_Data object from the request. |
|
| 184 | + $object = $this->prepare_item_for_database( $request ); |
|
| 185 | + if ( is_wp_error( $object ) ) { |
|
| 186 | + return $object; |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + // Save the object. |
|
| 190 | + $object = $this->save_object( $object ); |
|
| 191 | + if ( is_wp_error( $object ) ) { |
|
| 192 | + return $object; |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + // Save special fields. |
|
| 196 | + $save_special = $this->update_additional_fields_for_object( $object, $request ); |
|
| 197 | + if ( is_wp_error( $save_special ) ) { |
|
| 198 | + $object->delete( true ); |
|
| 199 | + return $save_special; |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + $request->set_param( 'context', 'edit' ); |
|
| 203 | + $response = $this->prepare_item_for_response( $object, $request ); |
|
| 204 | + $response = rest_ensure_response( $response ); |
|
| 205 | + $response->set_status( 201 ); |
|
| 206 | + $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $object->get_id() ) ) ); |
|
| 207 | + |
|
| 208 | + return $response; |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + /** |
|
| 212 | + * Update a single object. |
|
| 213 | + * |
|
| 214 | + * @param WP_REST_Request $request Full details about the request. |
|
| 215 | + * @return WP_Error|WP_REST_Response |
|
| 216 | + */ |
|
| 217 | + public function update_item( $request ) { |
|
| 218 | + |
|
| 219 | + // Fetch the item. |
|
| 220 | + $object = $this->get_object( $request['id'] ); |
|
| 221 | + if ( is_wp_error( $object ) ) { |
|
| 222 | + return $object; |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + // Prepare the item for saving. |
|
| 226 | + $object = $this->prepare_item_for_database( $request ); |
|
| 227 | + if ( is_wp_error( $object ) ) { |
|
| 228 | + return $object; |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + // Save the item. |
|
| 232 | + $object = $this->save_object( $object ); |
|
| 233 | + if ( is_wp_error( $object ) ) { |
|
| 234 | + return $object; |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + // Save special fields (those added via hooks). |
|
| 238 | + $save_special = $this->update_additional_fields_for_object( $object, $request ); |
|
| 239 | + if ( is_wp_error( $save_special ) ) { |
|
| 240 | + return $save_special; |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + $request->set_param( 'context', 'edit' ); |
|
| 244 | + $response = $this->prepare_item_for_response( $object, $request ); |
|
| 245 | + return rest_ensure_response( $response ); |
|
| 246 | + } |
|
| 247 | + |
|
| 248 | + /** |
|
| 249 | + * Prepare links for the request. |
|
| 250 | + * |
|
| 251 | + * @param GetPaid_Data $object GetPaid_Data object. |
|
| 252 | + * @return array Links for the given object. |
|
| 253 | + */ |
|
| 254 | + protected function prepare_links( $object ) { |
|
| 255 | + |
|
| 256 | + $links = array( |
|
| 257 | + 'self' => array( |
|
| 258 | + 'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $object->get_id() ) ), |
|
| 259 | + ), |
|
| 260 | + 'collection' => array( |
|
| 261 | + 'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ), |
|
| 262 | + ), |
|
| 263 | + ); |
|
| 264 | + |
|
| 265 | + return $links; |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + /** |
|
| 269 | + * Get the query params for collections of attachments. |
|
| 270 | + * |
|
| 271 | + * @return array |
|
| 272 | + */ |
|
| 273 | + public function get_collection_params() { |
|
| 274 | + $params = parent::get_collection_params(); |
|
| 275 | + $params['context']['default'] = 'view'; |
|
| 276 | + return $params; |
|
| 277 | + } |
|
| 278 | + |
|
| 279 | + /** |
|
| 280 | + * Only return writable props from schema. |
|
| 281 | + * |
|
| 282 | + * @param array $schema Schema. |
|
| 283 | + * @return bool |
|
| 284 | + */ |
|
| 285 | + public function filter_writable_props( $schema ) { |
|
| 286 | + return empty( $schema['readonly'] ); |
|
| 287 | + } |
|
| 288 | + |
|
| 289 | + /** |
|
| 290 | + * Prepare a single object for create or update. |
|
| 291 | + * |
|
| 292 | + * @since 1.0.19 |
|
| 293 | + * @param WP_REST_Request $request Request object. |
|
| 294 | + * @return GetPaid_Data|WP_Error Data object or WP_Error. |
|
| 295 | + */ |
|
| 296 | + protected function prepare_item_for_database( $request ) { |
|
| 297 | + |
|
| 298 | + // Do we have an object? |
|
| 299 | + if ( empty( $this->crud_class ) || ! class_exists( $this->crud_class ) ) { |
|
| 300 | + return new WP_Error( 'no_crud_class', __( 'You need to specify a CRUD class for this controller', 'invoicing' ) ); |
|
| 301 | + } |
|
| 302 | + |
|
| 303 | + // Prepare the object. |
|
| 304 | + $id = isset( $request['id'] ) ? absint( $request['id'] ) : 0; |
|
| 305 | + $object = new $this->crud_class( $id ); |
|
| 306 | + |
|
| 307 | + // Abort if an error exists. |
|
| 308 | + if ( ! empty( $object->last_error ) ) { |
|
| 309 | + return new WP_Error( 'invalid_item', $object->last_error ); |
|
| 310 | + } |
|
| 311 | + |
|
| 312 | + $schema = $this->get_item_schema(); |
|
| 313 | + $data_keys = array_keys( array_filter( $schema['properties'], array( $this, 'filter_writable_props' ) ) ); |
|
| 314 | + |
|
| 315 | + // Handle all writable props. |
|
| 316 | + foreach ( $data_keys as $key ) { |
|
| 317 | + $value = $request[ $key ]; |
|
| 318 | + |
|
| 319 | + if ( ! is_null( $value ) ) { |
|
| 320 | + switch ( $key ) { |
|
| 321 | + |
|
| 322 | + case 'meta_data': |
|
| 323 | + if ( is_array( $value ) ) { |
|
| 324 | + foreach ( $value as $meta ) { |
|
| 325 | + $object->update_meta_data( $meta['key'], $meta['value'], isset( $meta['id'] ) ? $meta['id'] : '' ); |
|
| 326 | + } |
|
| 327 | + } |
|
| 328 | + break; |
|
| 329 | + |
|
| 330 | + default: |
|
| 331 | + if ( is_callable( array( $object, "set_{$key}" ) ) ) { |
|
| 332 | + $object->{"set_{$key}"}( $value ); |
|
| 333 | + } |
|
| 334 | + break; |
|
| 335 | + } |
|
| 336 | + } |
|
| 337 | + |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + // Filters an object before it is inserted via the REST API.. |
|
| 341 | + return apply_filters( "getpaid_rest_pre_insert_{$this->rest_base}_object", $object, $request ); |
|
| 342 | + } |
|
| 343 | + |
|
| 344 | + /** |
|
| 345 | + * Retrieves data from a GetPaid class. |
|
| 346 | + * |
|
| 347 | + * @since 1.0.19 |
|
| 348 | + * @param GetPaid_Meta_Data[] $meta_data meta data objects. |
|
| 349 | + * @return array |
|
| 350 | + */ |
|
| 351 | + protected function prepare_object_meta_data( $meta_data ) { |
|
| 352 | + $meta = array(); |
|
| 353 | + |
|
| 354 | + foreach( $meta_data as $object ) { |
|
| 355 | + $meta[] = $object->get_data(); |
|
| 356 | + } |
|
| 357 | + |
|
| 358 | + return $meta; |
|
| 359 | + } |
|
| 360 | + |
|
| 361 | + /** |
|
| 362 | + * Retrieves invoice items. |
|
| 363 | + * |
|
| 364 | + * @since 1.0.19 |
|
| 365 | + * @param WPInv_Invoice $invoice Invoice items. |
|
| 366 | + * @param array $fields Fields to include. |
|
| 367 | + * @return array |
|
| 368 | + */ |
|
| 369 | + protected function prepare_invoice_items( $invoice ) { |
|
| 370 | + $items = array(); |
|
| 371 | + |
|
| 372 | + foreach( $invoice->get_items() as $item ) { |
|
| 373 | + |
|
| 374 | + $item_data = $item->prepare_data_for_saving(); |
|
| 375 | + |
|
| 376 | + if ( 'amount' == $invoice->get_template() ) { |
|
| 377 | + $item_data['quantity'] = 1; |
|
| 378 | + } |
|
| 379 | + |
|
| 380 | + $items[] = $item_data; |
|
| 381 | + } |
|
| 382 | + |
|
| 383 | + return $items; |
|
| 384 | + } |
|
| 385 | + |
|
| 386 | + /** |
|
| 387 | + * Retrieves data from a GetPaid class. |
|
| 388 | + * |
|
| 389 | + * @since 1.0.19 |
|
| 390 | + * @param GetPaid_Data $object Data object. |
|
| 391 | + * @param array $fields Fields to include. |
|
| 392 | + * @param string $context either view or edit. |
|
| 393 | + * @return array |
|
| 394 | + */ |
|
| 395 | + protected function prepare_object_data( $object, $fields, $context = 'view' ) { |
|
| 396 | + |
|
| 397 | + $data = array(); |
|
| 398 | + |
|
| 399 | + // Handle all writable props. |
|
| 400 | + foreach ( array_keys( $this->get_schema_properties() ) as $key ) { |
|
| 401 | + |
|
| 402 | + // Abort if it is not included. |
|
| 403 | + if ( ! empty( $fields ) && ! $this->is_field_included( $key, $fields ) ) { |
|
| 404 | + continue; |
|
| 405 | + } |
|
| 406 | + |
|
| 407 | + // Or this current object does not support the field. |
|
| 408 | + if ( ! $this->object_supports_field( $object, $key ) ) { |
|
| 409 | + continue; |
|
| 410 | + } |
|
| 411 | + |
|
| 412 | + // Handle meta data. |
|
| 413 | + if ( $key == 'meta_data' ) { |
|
| 414 | + $data['meta_data'] = $this->prepare_object_meta_data( $object->get_meta_data() ); |
|
| 415 | + continue; |
|
| 416 | + } |
|
| 417 | + |
|
| 418 | + // Handle items. |
|
| 419 | + if ( $key == 'items' && is_a( $object, 'WPInv_Invoice' ) ) { |
|
| 420 | + $data['items'] = $this->prepare_invoice_items( $object ); |
|
| 421 | + continue; |
|
| 422 | + } |
|
| 423 | + |
|
| 424 | + // Booleans. |
|
| 425 | + if ( is_callable( array( $object, $key ) ) ) { |
|
| 426 | + $data[ $key ] = $object->$key( $context ); |
|
| 427 | + continue; |
|
| 428 | + } |
|
| 429 | + |
|
| 430 | + // Get object value. |
|
| 431 | + if ( is_callable( array( $object, "get_{$key}" ) ) ) { |
|
| 432 | + $value = $object->{"get_{$key}"}( $context ); |
|
| 433 | + |
|
| 434 | + // If the value is an instance of GetPaid_Data... |
|
| 435 | + if ( is_a( $value, 'GetPaid_Data' ) ) { |
|
| 436 | + $value = $value->get_data( $context ); |
|
| 437 | + } |
|
| 438 | + |
|
| 439 | + // For objects, retrieves it's properties. |
|
| 440 | + $data[ $key ] = is_object( $value ) ? get_object_vars( $value ) : $value ; |
|
| 441 | + continue; |
|
| 442 | + } |
|
| 443 | + |
|
| 444 | + } |
|
| 445 | + |
|
| 446 | + return $data; |
|
| 447 | + } |
|
| 448 | + |
|
| 449 | + /** |
|
| 450 | + * Checks if a key should be included in a response. |
|
| 451 | + * |
|
| 452 | + * @since 1.0.19 |
|
| 453 | + * @param GetPaid_Data $object Data object. |
|
| 454 | + * @param string $field_key The key to check for. |
|
| 455 | + * @return bool |
|
| 456 | + */ |
|
| 457 | + public function object_supports_field( $object, $field_key ) { |
|
| 458 | + return apply_filters( 'getpaid_rest_object_supports_key', true, $object, $field_key ); |
|
| 459 | + } |
|
| 460 | + |
|
| 461 | + /** |
|
| 462 | + * Prepare a single object output for response. |
|
| 463 | + * |
|
| 464 | + * @since 1.0.19 |
|
| 465 | + * @param GetPaid_Data $object Data object. |
|
| 466 | + * @param WP_REST_Request $request Request object. |
|
| 467 | + * @return WP_REST_Response |
|
| 468 | + */ |
|
| 469 | + public function prepare_item_for_response( $object, $request ) { |
|
| 470 | + remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 ); |
|
| 471 | + |
|
| 472 | + $this->data_object = $object; |
|
| 473 | + |
|
| 474 | + // Fetch the fields to include in this response. |
|
| 475 | + $fields = $this->get_fields_for_response( $request ); |
|
| 476 | + |
|
| 477 | + // Prepare object data. |
|
| 478 | + $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
|
| 479 | + $data = $this->prepare_object_data( $object, $fields, $context ); |
|
| 480 | + $data = $this->add_additional_fields_to_object( $data, $request ); |
|
| 481 | + $data = $this->limit_object_to_requested_fields( $data, $fields ); |
|
| 482 | + $data = $this->filter_response_by_context( $data, $context ); |
|
| 483 | + |
|
| 484 | + // Prepare the response. |
|
| 485 | + $response = rest_ensure_response( $data ); |
|
| 486 | + $response->add_links( $this->prepare_links( $object, $request ) ); |
|
| 487 | + |
|
| 488 | + // Filter item response. |
|
| 489 | + return apply_filters( "getpaid_rest_prepare_{$this->rest_base}_object", $response, $object, $request ); |
|
| 490 | + } |
|
| 491 | 491 | |
| 492 | 492 | } |
@@ -16,495 +16,495 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class GetPaid_Subscriptions_Query { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * Query vars, after parsing |
|
| 21 | - * |
|
| 22 | - * @since 1.0.19 |
|
| 23 | - * @var array |
|
| 24 | - */ |
|
| 25 | - public $query_vars = array(); |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * List of found subscriptions. |
|
| 29 | - * |
|
| 30 | - * @since 1.0.19 |
|
| 31 | - * @var array |
|
| 32 | - */ |
|
| 33 | - private $results; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * Total number of found subscriptions for the current query |
|
| 37 | - * |
|
| 38 | - * @since 1.0.19 |
|
| 39 | - * @var int |
|
| 40 | - */ |
|
| 41 | - private $total_subscriptions = 0; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * The SQL query used to fetch matching subscriptions. |
|
| 45 | - * |
|
| 46 | - * @since 1.0.19 |
|
| 47 | - * @var string |
|
| 48 | - */ |
|
| 49 | - public $request; |
|
| 50 | - |
|
| 51 | - // SQL clauses |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * Contains the 'FIELDS' sql clause |
|
| 55 | - * |
|
| 56 | - * @since 1.0.19 |
|
| 57 | - * @var string |
|
| 58 | - */ |
|
| 59 | - public $query_fields; |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * Contains the 'FROM' sql clause |
|
| 63 | - * |
|
| 64 | - * @since 1.0.19 |
|
| 65 | - * @var string |
|
| 66 | - */ |
|
| 67 | - public $query_from; |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * Contains the 'WHERE' sql clause |
|
| 71 | - * |
|
| 72 | - * @since 1.0.19 |
|
| 73 | - * @var string |
|
| 74 | - */ |
|
| 75 | - public $query_where; |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * Contains the 'ORDER BY' sql clause |
|
| 79 | - * |
|
| 80 | - * @since 1.0.19 |
|
| 81 | - * @var string |
|
| 82 | - */ |
|
| 83 | - public $query_orderby; |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * Contains the 'LIMIT' sql clause |
|
| 87 | - * |
|
| 88 | - * @since 1.0.19 |
|
| 89 | - * @var string |
|
| 90 | - */ |
|
| 91 | - public $query_limit; |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * Class constructor. |
|
| 95 | - * |
|
| 96 | - * @since 1.0.19 |
|
| 97 | - * |
|
| 98 | - * @param null|string|array $query Optional. The query variables. |
|
| 99 | - */ |
|
| 100 | - public function __construct( $query = null ) { |
|
| 101 | - if ( ! is_null( $query ) ) { |
|
| 102 | - $this->prepare_query( $query ); |
|
| 103 | - $this->query(); |
|
| 104 | - } |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * Fills in missing query variables with default values. |
|
| 109 | - * |
|
| 110 | - * @since 1.0.19 |
|
| 111 | - * |
|
| 112 | - * @param string|array $args Query vars, as passed to `GetPaid_Subscriptions_Query`. |
|
| 113 | - * @return array Complete query variables with undefined ones filled in with defaults. |
|
| 114 | - */ |
|
| 115 | - public static function fill_query_vars( $args ) { |
|
| 116 | - $defaults = array( |
|
| 117 | - 'status' => 'all', |
|
| 118 | - 'customer_in' => array(), |
|
| 119 | - 'customer_not_in' => array(), |
|
| 120 | - 'product_in' => array(), |
|
| 121 | - 'product_not_in' => array(), |
|
| 122 | - 'include' => array(), |
|
| 123 | - 'exclude' => array(), |
|
| 124 | - 'orderby' => 'id', |
|
| 125 | - 'order' => 'DESC', |
|
| 126 | - 'offset' => '', |
|
| 127 | - 'number' => 10, |
|
| 128 | - 'paged' => 1, |
|
| 129 | - 'count_total' => true, |
|
| 130 | - 'fields' => 'all', |
|
| 131 | - ); |
|
| 132 | - |
|
| 133 | - return wp_parse_args( $args, $defaults ); |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Prepare the query variables. |
|
| 138 | - * |
|
| 139 | - * @since 1.0.19 |
|
| 140 | - * |
|
| 141 | - * @global wpdb $wpdb WordPress database abstraction object. |
|
| 142 | - * |
|
| 143 | - * @param string|array $query { |
|
| 144 | - * Optional. Array or string of Query parameters. |
|
| 145 | - * |
|
| 146 | - * @type string|array $status The subscription status to filter by. Can either be a single status or an array of statuses. |
|
| 147 | - * Default is all. |
|
| 148 | - * @type int[] $customer_in An array of customer ids to filter by. |
|
| 149 | - * @type int[] $customer_not_in An array of customer ids whose subscriptions should be excluded. |
|
| 150 | - * @type int[] $invoice_in An array of invoice ids to filter by. |
|
| 151 | - * @type int[] $invoice_not_in An array of invoice ids whose subscriptions should be excluded. |
|
| 152 | - * @type int[] $product_in An array of product ids to filter by. |
|
| 153 | - * @type int[] $product_not_in An array of product ids whose subscriptions should be excluded. |
|
| 154 | - * @type array $date_created_query A WP_Date_Query compatible array use to filter subscriptions by their date of creation. |
|
| 155 | - * @type array $date_expires_query A WP_Date_Query compatible array use to filter subscriptions by their expiration date. |
|
| 156 | - * @type array $include An array of subscription IDs to include. Default empty array. |
|
| 157 | - * @type array $exclude An array of subscription IDs to exclude. Default empty array. |
|
| 158 | - * @type string|array $orderby Field(s) to sort the retrieved subscription by. May be a single value, |
|
| 159 | - * an array of values, or a multi-dimensional array with fields as |
|
| 160 | - * keys and orders ('ASC' or 'DESC') as values. Accepted values are |
|
| 161 | - * 'id', 'customer_id', 'frequency', 'period', 'initial_amount, |
|
| 162 | - * 'recurring_amount', 'bill_times', 'parent_payment_id', 'created', 'expiration' |
|
| 163 | - * 'transaction_id', 'product_id', 'trial_period', 'include', 'status', 'profile_id'. Default array( 'id' ). |
|
| 164 | - * @type string $order Designates ascending or descending order of subscriptions. Order values |
|
| 165 | - * passed as part of an `$orderby` array take precedence over this |
|
| 166 | - * parameter. Accepts 'ASC', 'DESC'. Default 'DESC'. |
|
| 167 | - * @type int $offset Number of subscriptions to offset in retrieved results. Can be used in |
|
| 168 | - * conjunction with pagination. Default 0. |
|
| 169 | - * @type int $number Number of subscriptions to limit the query for. Can be used in |
|
| 170 | - * conjunction with pagination. Value -1 (all) is supported, but |
|
| 171 | - * should be used with caution on larger sites. |
|
| 172 | - * Default 10. |
|
| 173 | - * @type int $paged When used with number, defines the page of results to return. |
|
| 174 | - * Default 1. |
|
| 175 | - * @type bool $count_total Whether to count the total number of subscriptions found. If pagination |
|
| 176 | - * is not needed, setting this to false can improve performance. |
|
| 177 | - * Default true. |
|
| 178 | - * @type string|array $fields Which fields to return. Single or all fields (string), or array |
|
| 179 | - * of fields. Accepts 'id', 'customer_id', 'frequency', 'period', 'initial_amount, |
|
| 180 | - * 'recurring_amount', 'bill_times', 'parent_payment_id', 'created', 'expiration' |
|
| 181 | - * 'transaction_id', 'product_id', 'trial_period', 'status', 'profile_id'. |
|
| 182 | - * Use 'all' for all fields. Default 'all'. |
|
| 183 | - * } |
|
| 184 | - */ |
|
| 185 | - public function prepare_query( $query = array() ) { |
|
| 186 | - global $wpdb; |
|
| 187 | - |
|
| 188 | - if ( empty( $this->query_vars ) || ! empty( $query ) ) { |
|
| 189 | - $this->query_limit = null; |
|
| 190 | - $this->query_vars = $this->fill_query_vars( $query ); |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - if ( ! empty( $this->query_vars['fields'] ) && 'all' !== $this->query_vars['fields'] ) { |
|
| 194 | - $this->query_vars['fields'] = wpinv_parse_list( $this->query_vars['fields'] ); |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - do_action( 'getpaid_pre_get_subscriptions', array( &$this ) ); |
|
| 198 | - |
|
| 199 | - // Ensure that query vars are filled after 'getpaid_pre_get_subscriptions'. |
|
| 200 | - $qv =& $this->query_vars; |
|
| 201 | - $qv = $this->fill_query_vars( $qv ); |
|
| 202 | - $table = $wpdb->prefix . 'wpinv_subscriptions'; |
|
| 203 | - $this->query_from = "FROM $table"; |
|
| 204 | - |
|
| 205 | - // Prepare query fields. |
|
| 206 | - $this->prepare_query_fields( $qv, $table ); |
|
| 207 | - |
|
| 208 | - // Prepare query where. |
|
| 209 | - $this->prepare_query_where( $qv, $table ); |
|
| 210 | - |
|
| 211 | - // Prepare query order. |
|
| 212 | - $this->prepare_query_order( $qv, $table ); |
|
| 213 | - |
|
| 214 | - // limit |
|
| 215 | - if ( isset( $qv['number'] ) && $qv['number'] > 0 ) { |
|
| 216 | - if ( $qv['offset'] ) { |
|
| 217 | - $this->query_limit = $wpdb->prepare( 'LIMIT %d, %d', $qv['offset'], $qv['number'] ); |
|
| 218 | - } else { |
|
| 219 | - $this->query_limit = $wpdb->prepare( 'LIMIT %d, %d', $qv['number'] * ( $qv['paged'] - 1 ), $qv['number'] ); |
|
| 220 | - } |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - do_action_ref_array( 'getpaid_after_subscriptions_query', array( &$this ) ); |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - /** |
|
| 227 | - * Prepares the query fields. |
|
| 228 | - * |
|
| 229 | - * @since 1.0.19 |
|
| 230 | - * |
|
| 231 | - * @param array $qv Query vars. |
|
| 232 | - * @param string $table Table name. |
|
| 233 | - */ |
|
| 234 | - protected function prepare_query_fields( &$qv, $table ) { |
|
| 235 | - |
|
| 236 | - if ( is_array( $qv['fields'] ) ) { |
|
| 237 | - $qv['fields'] = array_unique( $qv['fields'] ); |
|
| 238 | - |
|
| 239 | - $query_fields = array(); |
|
| 240 | - foreach ( $qv['fields'] as $field ) { |
|
| 241 | - $field = sanitize_key( $field ); |
|
| 242 | - $query_fields[] = "$table.`$field`"; |
|
| 243 | - } |
|
| 244 | - $this->query_fields = implode( ',', $query_fields ); |
|
| 245 | - } else { |
|
| 246 | - $this->query_fields = "$table.*"; |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - if ( isset( $qv['count_total'] ) && $qv['count_total'] ) { |
|
| 250 | - $this->query_fields = 'SQL_CALC_FOUND_ROWS ' . $this->query_fields; |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - /** |
|
| 256 | - * Prepares the query where. |
|
| 257 | - * |
|
| 258 | - * @since 1.0.19 |
|
| 259 | - * |
|
| 260 | - * @param array $qv Query vars. |
|
| 261 | - * @param string $table Table name. |
|
| 262 | - */ |
|
| 263 | - protected function prepare_query_where( &$qv, $table ) { |
|
| 264 | - global $wpdb; |
|
| 265 | - $this->query_where = 'WHERE 1=1'; |
|
| 266 | - |
|
| 267 | - // Status. |
|
| 268 | - if ( 'all' !== $qv['status'] ) { |
|
| 269 | - $statuses = wpinv_clean( wpinv_parse_list( $qv['status'] ) ); |
|
| 270 | - $prepared_statuses = join( ',', array_fill( 0, count( $statuses ), '%s' ) ); |
|
| 271 | - $this->query_where .= $wpdb->prepare( " AND $table.`status` IN ( $prepared_statuses )", $statuses ); |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - if ( ! empty( $qv['customer_in'] ) ) { |
|
| 275 | - $customer_in = implode( ',', wp_parse_id_list( $qv['customer_in'] ) ); |
|
| 276 | - $this->query_where .= " AND $table.`customer_id` IN ($customer_in)"; |
|
| 277 | - } elseif ( ! empty( $qv['customer_not_in'] ) ) { |
|
| 278 | - $customer_not_in = implode( ',', wp_parse_id_list( $qv['customer_not_in'] ) ); |
|
| 279 | - $this->query_where .= " AND $table.`customer_id` NOT IN ($customer_not_in)"; |
|
| 280 | - } |
|
| 281 | - |
|
| 282 | - if ( ! empty( $qv['product_in'] ) ) { |
|
| 283 | - $product_in = implode( ',', wp_parse_id_list( $qv['product_in'] ) ); |
|
| 284 | - $this->query_where .= " AND $table.`product_id` IN ($product_in)"; |
|
| 285 | - } elseif ( ! empty( $qv['product_not_in'] ) ) { |
|
| 286 | - $product_not_in = implode( ',', wp_parse_id_list( $qv['product_not_in'] ) ); |
|
| 287 | - $this->query_where .= " AND $table.`product_id` NOT IN ($product_not_in)"; |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - if ( ! empty( $qv['invoice_in'] ) ) { |
|
| 291 | - $invoice_in = implode( ',', wp_parse_id_list( $qv['invoice_in'] ) ); |
|
| 292 | - $this->query_where .= " AND $table.`parent_payment_id` IN ($invoice_in)"; |
|
| 293 | - } elseif ( ! empty( $qv['invoice_not_in'] ) ) { |
|
| 294 | - $invoice_not_in = implode( ',', wp_parse_id_list( $qv['invoice_not_in'] ) ); |
|
| 295 | - $this->query_where .= " AND $table.`parent_payment_id` NOT IN ($invoice_not_in)"; |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - if ( ! empty( $qv['include'] ) ) { |
|
| 299 | - $include = implode( ',', wp_parse_id_list( $qv['include'] ) ); |
|
| 300 | - $this->query_where .= " AND $table.`id` IN ($include)"; |
|
| 301 | - } elseif ( ! empty( $qv['exclude'] ) ) { |
|
| 302 | - $exclude = implode( ',', wp_parse_id_list( $qv['exclude'] ) ); |
|
| 303 | - $this->query_where .= " AND $table.`id` NOT IN ($exclude)"; |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - // Date queries are allowed for the subscription creation date. |
|
| 307 | - if ( ! empty( $qv['date_created_query'] ) && is_array( $qv['date_created_query'] ) ) { |
|
| 308 | - $date_created_query = new WP_Date_Query( $qv['date_created_query'], "$table.created" ); |
|
| 309 | - $this->query_where .= $date_created_query->get_sql(); |
|
| 310 | - } |
|
| 311 | - |
|
| 312 | - // Date queries are also allowed for the subscription expiration date. |
|
| 313 | - if ( ! empty( $qv['date_expires_query'] ) && is_array( $qv['date_expires_query'] ) ) { |
|
| 314 | - $date_expires_query = new WP_Date_Query( $qv['date_expires_query'], "$table.expiration" ); |
|
| 315 | - $this->query_where .= $date_expires_query->get_sql(); |
|
| 316 | - } |
|
| 317 | - |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - /** |
|
| 321 | - * Prepares the query order. |
|
| 322 | - * |
|
| 323 | - * @since 1.0.19 |
|
| 324 | - * |
|
| 325 | - * @param array $qv Query vars. |
|
| 326 | - * @param string $table Table name. |
|
| 327 | - */ |
|
| 328 | - protected function prepare_query_order( &$qv, $table ) { |
|
| 329 | - |
|
| 330 | - // sorting. |
|
| 331 | - $qv['order'] = isset( $qv['order'] ) ? strtoupper( $qv['order'] ) : ''; |
|
| 332 | - $order = $this->parse_order( $qv['order'] ); |
|
| 333 | - |
|
| 334 | - // Default order is by 'id' (latest subscriptions). |
|
| 335 | - if ( empty( $qv['orderby'] ) ) { |
|
| 336 | - $qv['orderby'] = array( 'id' ); |
|
| 337 | - } |
|
| 338 | - |
|
| 339 | - // 'orderby' values may be an array, comma- or space-separated list. |
|
| 340 | - $ordersby = array_filter( wpinv_parse_list( $qv['orderby'] ) ); |
|
| 341 | - |
|
| 342 | - $orderby_array = array(); |
|
| 343 | - foreach ( $ordersby as $_key => $_value ) { |
|
| 344 | - |
|
| 345 | - if ( is_int( $_key ) ) { |
|
| 346 | - // Integer key means this is a flat array of 'orderby' fields. |
|
| 347 | - $_orderby = $_value; |
|
| 348 | - $_order = $order; |
|
| 349 | - } else { |
|
| 350 | - // Non-integer key means that the key is the field and the value is ASC/DESC. |
|
| 351 | - $_orderby = $_key; |
|
| 352 | - $_order = $_value; |
|
| 353 | - } |
|
| 354 | - |
|
| 355 | - $parsed = $this->parse_orderby( $_orderby, $table ); |
|
| 356 | - |
|
| 357 | - if ( $parsed ) { |
|
| 358 | - $orderby_array[] = $parsed . ' ' . $this->parse_order( $_order ); |
|
| 359 | - } |
|
| 360 | - |
|
| 361 | - } |
|
| 362 | - |
|
| 363 | - // If no valid clauses were found, order by id. |
|
| 364 | - if ( empty( $orderby_array ) ) { |
|
| 365 | - $orderby_array[] = "id $order"; |
|
| 366 | - } |
|
| 367 | - |
|
| 368 | - $this->query_orderby = 'ORDER BY ' . implode( ', ', $orderby_array ); |
|
| 369 | - |
|
| 370 | - } |
|
| 371 | - |
|
| 372 | - /** |
|
| 373 | - * Execute the query, with the current variables. |
|
| 374 | - * |
|
| 375 | - * @since 1.0.19 |
|
| 376 | - * |
|
| 377 | - * @global wpdb $wpdb WordPress database abstraction object. |
|
| 378 | - */ |
|
| 379 | - public function query() { |
|
| 380 | - global $wpdb; |
|
| 381 | - |
|
| 382 | - $qv =& $this->query_vars; |
|
| 383 | - |
|
| 384 | - // Return a non-null value to bypass the default GetPaid subscriptions query and remember to set the |
|
| 385 | - // total_subscriptions property. |
|
| 386 | - $this->results = apply_filters_ref_array( 'getpaid_subscriptions_pre_query', array( null, &$this ) ); |
|
| 387 | - |
|
| 388 | - if ( null === $this->results ) { |
|
| 389 | - $this->request = "SELECT $this->query_fields $this->query_from $this->query_where $this->query_orderby $this->query_limit"; |
|
| 390 | - |
|
| 391 | - if ( ( is_array( $qv['fields'] ) && 1 != count( $qv['fields'] ) ) || 'all' == $qv['fields'] ) { |
|
| 392 | - $this->results = $wpdb->get_results( $this->request ); |
|
| 393 | - } else { |
|
| 394 | - $this->results = $wpdb->get_col( $this->request ); |
|
| 395 | - } |
|
| 396 | - |
|
| 397 | - if ( isset( $qv['count_total'] ) && $qv['count_total'] ) { |
|
| 398 | - $found_subscriptions_query = apply_filters( 'getpaid_found_subscriptions_query', 'SELECT FOUND_ROWS()', $this ); |
|
| 399 | - $this->total_subscriptions = (int) $wpdb->get_var( $found_subscriptions_query ); |
|
| 400 | - } |
|
| 401 | - } |
|
| 402 | - |
|
| 403 | - if ( 'all' == $qv['fields'] ) { |
|
| 404 | - foreach ( $this->results as $key => $subscription ) { |
|
| 405 | - wp_cache_set( $subscription->id, $subscription, 'getpaid_subscriptions' ); |
|
| 406 | - wp_cache_set( $subscription->profile_id, $subscription->id, 'getpaid_subscription_profile_ids_to_subscription_ids' ); |
|
| 407 | - wp_cache_set( $subscription->transaction_id, $subscription->id, 'getpaid_subscription_transaction_ids_to_subscription_ids' ); |
|
| 408 | - wp_cache_set( $subscription->transaction_id, $subscription->id, 'getpaid_subscription_transaction_ids_to_subscription_ids' ); |
|
| 409 | - $this->results[ $key ] = new WPInv_Subscription( $subscription ); |
|
| 410 | - } |
|
| 411 | - } |
|
| 412 | - |
|
| 413 | - } |
|
| 414 | - |
|
| 415 | - /** |
|
| 416 | - * Retrieve query variable. |
|
| 417 | - * |
|
| 418 | - * @since 1.0.19 |
|
| 419 | - * |
|
| 420 | - * @param string $query_var Query variable key. |
|
| 421 | - * @return mixed |
|
| 422 | - */ |
|
| 423 | - public function get( $query_var ) { |
|
| 424 | - if ( isset( $this->query_vars[ $query_var ] ) ) { |
|
| 425 | - return $this->query_vars[ $query_var ]; |
|
| 426 | - } |
|
| 427 | - |
|
| 428 | - return null; |
|
| 429 | - } |
|
| 430 | - |
|
| 431 | - /** |
|
| 432 | - * Set query variable. |
|
| 433 | - * |
|
| 434 | - * @since 1.0.19 |
|
| 435 | - * |
|
| 436 | - * @param string $query_var Query variable key. |
|
| 437 | - * @param mixed $value Query variable value. |
|
| 438 | - */ |
|
| 439 | - public function set( $query_var, $value ) { |
|
| 440 | - $this->query_vars[ $query_var ] = $value; |
|
| 441 | - } |
|
| 442 | - |
|
| 443 | - /** |
|
| 444 | - * Return the list of subscriptions. |
|
| 445 | - * |
|
| 446 | - * @since 1.0.19 |
|
| 447 | - * |
|
| 448 | - * @return WPInv_Subscription[]|array Found subscriptions. |
|
| 449 | - */ |
|
| 450 | - public function get_results() { |
|
| 451 | - return $this->results; |
|
| 452 | - } |
|
| 453 | - |
|
| 454 | - /** |
|
| 455 | - * Return the total number of subscriptions for the current query. |
|
| 456 | - * |
|
| 457 | - * @since 1.0.19 |
|
| 458 | - * |
|
| 459 | - * @return int Number of total subscriptions. |
|
| 460 | - */ |
|
| 461 | - public function get_total() { |
|
| 462 | - return $this->total_subscriptions; |
|
| 463 | - } |
|
| 464 | - |
|
| 465 | - /** |
|
| 466 | - * Parse and sanitize 'orderby' keys passed to the subscriptions query. |
|
| 467 | - * |
|
| 468 | - * @since 1.0.19 |
|
| 469 | - * |
|
| 470 | - * @param string $orderby Alias for the field to order by. |
|
| 471 | - * @param string $table The current table. |
|
| 472 | - * @return string Value to use in the ORDER clause, if `$orderby` is valid. |
|
| 473 | - */ |
|
| 474 | - protected function parse_orderby( $orderby, $table ) { |
|
| 475 | - |
|
| 476 | - $_orderby = ''; |
|
| 477 | - if ( in_array( $orderby, array( 'customer_id', 'frequency', 'period', 'initial_amount', 'recurring_amount', 'bill_times', 'transaction_id', 'parent_payment_id', 'product_id', 'created', 'expiration', 'trial_period', 'status', 'profile_id' ) ) ) { |
|
| 478 | - $_orderby = "$table.`$orderby`"; |
|
| 479 | - } elseif ( 'id' === strtolower( $orderby ) ) { |
|
| 480 | - $_orderby = "$table.id"; |
|
| 481 | - } elseif ( 'include' === $orderby && ! empty( $this->query_vars['include'] ) ) { |
|
| 482 | - $include = wp_parse_id_list( $this->query_vars['include'] ); |
|
| 483 | - $include_sql = implode( ',', $include ); |
|
| 484 | - $_orderby = "FIELD( $table.id, $include_sql )"; |
|
| 485 | - } |
|
| 486 | - |
|
| 487 | - return $_orderby; |
|
| 488 | - } |
|
| 489 | - |
|
| 490 | - /** |
|
| 491 | - * Parse an 'order' query variable and cast it to ASC or DESC as necessary. |
|
| 492 | - * |
|
| 493 | - * @since 1.0.19 |
|
| 494 | - * |
|
| 495 | - * @param string $order The 'order' query variable. |
|
| 496 | - * @return string The sanitized 'order' query variable. |
|
| 497 | - */ |
|
| 498 | - protected function parse_order( $order ) { |
|
| 499 | - if ( ! is_string( $order ) || empty( $order ) ) { |
|
| 500 | - return 'DESC'; |
|
| 501 | - } |
|
| 502 | - |
|
| 503 | - if ( 'ASC' === strtoupper( $order ) ) { |
|
| 504 | - return 'ASC'; |
|
| 505 | - } else { |
|
| 506 | - return 'DESC'; |
|
| 507 | - } |
|
| 508 | - } |
|
| 19 | + /** |
|
| 20 | + * Query vars, after parsing |
|
| 21 | + * |
|
| 22 | + * @since 1.0.19 |
|
| 23 | + * @var array |
|
| 24 | + */ |
|
| 25 | + public $query_vars = array(); |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * List of found subscriptions. |
|
| 29 | + * |
|
| 30 | + * @since 1.0.19 |
|
| 31 | + * @var array |
|
| 32 | + */ |
|
| 33 | + private $results; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * Total number of found subscriptions for the current query |
|
| 37 | + * |
|
| 38 | + * @since 1.0.19 |
|
| 39 | + * @var int |
|
| 40 | + */ |
|
| 41 | + private $total_subscriptions = 0; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * The SQL query used to fetch matching subscriptions. |
|
| 45 | + * |
|
| 46 | + * @since 1.0.19 |
|
| 47 | + * @var string |
|
| 48 | + */ |
|
| 49 | + public $request; |
|
| 50 | + |
|
| 51 | + // SQL clauses |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * Contains the 'FIELDS' sql clause |
|
| 55 | + * |
|
| 56 | + * @since 1.0.19 |
|
| 57 | + * @var string |
|
| 58 | + */ |
|
| 59 | + public $query_fields; |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * Contains the 'FROM' sql clause |
|
| 63 | + * |
|
| 64 | + * @since 1.0.19 |
|
| 65 | + * @var string |
|
| 66 | + */ |
|
| 67 | + public $query_from; |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * Contains the 'WHERE' sql clause |
|
| 71 | + * |
|
| 72 | + * @since 1.0.19 |
|
| 73 | + * @var string |
|
| 74 | + */ |
|
| 75 | + public $query_where; |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * Contains the 'ORDER BY' sql clause |
|
| 79 | + * |
|
| 80 | + * @since 1.0.19 |
|
| 81 | + * @var string |
|
| 82 | + */ |
|
| 83 | + public $query_orderby; |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * Contains the 'LIMIT' sql clause |
|
| 87 | + * |
|
| 88 | + * @since 1.0.19 |
|
| 89 | + * @var string |
|
| 90 | + */ |
|
| 91 | + public $query_limit; |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Class constructor. |
|
| 95 | + * |
|
| 96 | + * @since 1.0.19 |
|
| 97 | + * |
|
| 98 | + * @param null|string|array $query Optional. The query variables. |
|
| 99 | + */ |
|
| 100 | + public function __construct( $query = null ) { |
|
| 101 | + if ( ! is_null( $query ) ) { |
|
| 102 | + $this->prepare_query( $query ); |
|
| 103 | + $this->query(); |
|
| 104 | + } |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * Fills in missing query variables with default values. |
|
| 109 | + * |
|
| 110 | + * @since 1.0.19 |
|
| 111 | + * |
|
| 112 | + * @param string|array $args Query vars, as passed to `GetPaid_Subscriptions_Query`. |
|
| 113 | + * @return array Complete query variables with undefined ones filled in with defaults. |
|
| 114 | + */ |
|
| 115 | + public static function fill_query_vars( $args ) { |
|
| 116 | + $defaults = array( |
|
| 117 | + 'status' => 'all', |
|
| 118 | + 'customer_in' => array(), |
|
| 119 | + 'customer_not_in' => array(), |
|
| 120 | + 'product_in' => array(), |
|
| 121 | + 'product_not_in' => array(), |
|
| 122 | + 'include' => array(), |
|
| 123 | + 'exclude' => array(), |
|
| 124 | + 'orderby' => 'id', |
|
| 125 | + 'order' => 'DESC', |
|
| 126 | + 'offset' => '', |
|
| 127 | + 'number' => 10, |
|
| 128 | + 'paged' => 1, |
|
| 129 | + 'count_total' => true, |
|
| 130 | + 'fields' => 'all', |
|
| 131 | + ); |
|
| 132 | + |
|
| 133 | + return wp_parse_args( $args, $defaults ); |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Prepare the query variables. |
|
| 138 | + * |
|
| 139 | + * @since 1.0.19 |
|
| 140 | + * |
|
| 141 | + * @global wpdb $wpdb WordPress database abstraction object. |
|
| 142 | + * |
|
| 143 | + * @param string|array $query { |
|
| 144 | + * Optional. Array or string of Query parameters. |
|
| 145 | + * |
|
| 146 | + * @type string|array $status The subscription status to filter by. Can either be a single status or an array of statuses. |
|
| 147 | + * Default is all. |
|
| 148 | + * @type int[] $customer_in An array of customer ids to filter by. |
|
| 149 | + * @type int[] $customer_not_in An array of customer ids whose subscriptions should be excluded. |
|
| 150 | + * @type int[] $invoice_in An array of invoice ids to filter by. |
|
| 151 | + * @type int[] $invoice_not_in An array of invoice ids whose subscriptions should be excluded. |
|
| 152 | + * @type int[] $product_in An array of product ids to filter by. |
|
| 153 | + * @type int[] $product_not_in An array of product ids whose subscriptions should be excluded. |
|
| 154 | + * @type array $date_created_query A WP_Date_Query compatible array use to filter subscriptions by their date of creation. |
|
| 155 | + * @type array $date_expires_query A WP_Date_Query compatible array use to filter subscriptions by their expiration date. |
|
| 156 | + * @type array $include An array of subscription IDs to include. Default empty array. |
|
| 157 | + * @type array $exclude An array of subscription IDs to exclude. Default empty array. |
|
| 158 | + * @type string|array $orderby Field(s) to sort the retrieved subscription by. May be a single value, |
|
| 159 | + * an array of values, or a multi-dimensional array with fields as |
|
| 160 | + * keys and orders ('ASC' or 'DESC') as values. Accepted values are |
|
| 161 | + * 'id', 'customer_id', 'frequency', 'period', 'initial_amount, |
|
| 162 | + * 'recurring_amount', 'bill_times', 'parent_payment_id', 'created', 'expiration' |
|
| 163 | + * 'transaction_id', 'product_id', 'trial_period', 'include', 'status', 'profile_id'. Default array( 'id' ). |
|
| 164 | + * @type string $order Designates ascending or descending order of subscriptions. Order values |
|
| 165 | + * passed as part of an `$orderby` array take precedence over this |
|
| 166 | + * parameter. Accepts 'ASC', 'DESC'. Default 'DESC'. |
|
| 167 | + * @type int $offset Number of subscriptions to offset in retrieved results. Can be used in |
|
| 168 | + * conjunction with pagination. Default 0. |
|
| 169 | + * @type int $number Number of subscriptions to limit the query for. Can be used in |
|
| 170 | + * conjunction with pagination. Value -1 (all) is supported, but |
|
| 171 | + * should be used with caution on larger sites. |
|
| 172 | + * Default 10. |
|
| 173 | + * @type int $paged When used with number, defines the page of results to return. |
|
| 174 | + * Default 1. |
|
| 175 | + * @type bool $count_total Whether to count the total number of subscriptions found. If pagination |
|
| 176 | + * is not needed, setting this to false can improve performance. |
|
| 177 | + * Default true. |
|
| 178 | + * @type string|array $fields Which fields to return. Single or all fields (string), or array |
|
| 179 | + * of fields. Accepts 'id', 'customer_id', 'frequency', 'period', 'initial_amount, |
|
| 180 | + * 'recurring_amount', 'bill_times', 'parent_payment_id', 'created', 'expiration' |
|
| 181 | + * 'transaction_id', 'product_id', 'trial_period', 'status', 'profile_id'. |
|
| 182 | + * Use 'all' for all fields. Default 'all'. |
|
| 183 | + * } |
|
| 184 | + */ |
|
| 185 | + public function prepare_query( $query = array() ) { |
|
| 186 | + global $wpdb; |
|
| 187 | + |
|
| 188 | + if ( empty( $this->query_vars ) || ! empty( $query ) ) { |
|
| 189 | + $this->query_limit = null; |
|
| 190 | + $this->query_vars = $this->fill_query_vars( $query ); |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + if ( ! empty( $this->query_vars['fields'] ) && 'all' !== $this->query_vars['fields'] ) { |
|
| 194 | + $this->query_vars['fields'] = wpinv_parse_list( $this->query_vars['fields'] ); |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + do_action( 'getpaid_pre_get_subscriptions', array( &$this ) ); |
|
| 198 | + |
|
| 199 | + // Ensure that query vars are filled after 'getpaid_pre_get_subscriptions'. |
|
| 200 | + $qv =& $this->query_vars; |
|
| 201 | + $qv = $this->fill_query_vars( $qv ); |
|
| 202 | + $table = $wpdb->prefix . 'wpinv_subscriptions'; |
|
| 203 | + $this->query_from = "FROM $table"; |
|
| 204 | + |
|
| 205 | + // Prepare query fields. |
|
| 206 | + $this->prepare_query_fields( $qv, $table ); |
|
| 207 | + |
|
| 208 | + // Prepare query where. |
|
| 209 | + $this->prepare_query_where( $qv, $table ); |
|
| 210 | + |
|
| 211 | + // Prepare query order. |
|
| 212 | + $this->prepare_query_order( $qv, $table ); |
|
| 213 | + |
|
| 214 | + // limit |
|
| 215 | + if ( isset( $qv['number'] ) && $qv['number'] > 0 ) { |
|
| 216 | + if ( $qv['offset'] ) { |
|
| 217 | + $this->query_limit = $wpdb->prepare( 'LIMIT %d, %d', $qv['offset'], $qv['number'] ); |
|
| 218 | + } else { |
|
| 219 | + $this->query_limit = $wpdb->prepare( 'LIMIT %d, %d', $qv['number'] * ( $qv['paged'] - 1 ), $qv['number'] ); |
|
| 220 | + } |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + do_action_ref_array( 'getpaid_after_subscriptions_query', array( &$this ) ); |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + /** |
|
| 227 | + * Prepares the query fields. |
|
| 228 | + * |
|
| 229 | + * @since 1.0.19 |
|
| 230 | + * |
|
| 231 | + * @param array $qv Query vars. |
|
| 232 | + * @param string $table Table name. |
|
| 233 | + */ |
|
| 234 | + protected function prepare_query_fields( &$qv, $table ) { |
|
| 235 | + |
|
| 236 | + if ( is_array( $qv['fields'] ) ) { |
|
| 237 | + $qv['fields'] = array_unique( $qv['fields'] ); |
|
| 238 | + |
|
| 239 | + $query_fields = array(); |
|
| 240 | + foreach ( $qv['fields'] as $field ) { |
|
| 241 | + $field = sanitize_key( $field ); |
|
| 242 | + $query_fields[] = "$table.`$field`"; |
|
| 243 | + } |
|
| 244 | + $this->query_fields = implode( ',', $query_fields ); |
|
| 245 | + } else { |
|
| 246 | + $this->query_fields = "$table.*"; |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + if ( isset( $qv['count_total'] ) && $qv['count_total'] ) { |
|
| 250 | + $this->query_fields = 'SQL_CALC_FOUND_ROWS ' . $this->query_fields; |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + /** |
|
| 256 | + * Prepares the query where. |
|
| 257 | + * |
|
| 258 | + * @since 1.0.19 |
|
| 259 | + * |
|
| 260 | + * @param array $qv Query vars. |
|
| 261 | + * @param string $table Table name. |
|
| 262 | + */ |
|
| 263 | + protected function prepare_query_where( &$qv, $table ) { |
|
| 264 | + global $wpdb; |
|
| 265 | + $this->query_where = 'WHERE 1=1'; |
|
| 266 | + |
|
| 267 | + // Status. |
|
| 268 | + if ( 'all' !== $qv['status'] ) { |
|
| 269 | + $statuses = wpinv_clean( wpinv_parse_list( $qv['status'] ) ); |
|
| 270 | + $prepared_statuses = join( ',', array_fill( 0, count( $statuses ), '%s' ) ); |
|
| 271 | + $this->query_where .= $wpdb->prepare( " AND $table.`status` IN ( $prepared_statuses )", $statuses ); |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + if ( ! empty( $qv['customer_in'] ) ) { |
|
| 275 | + $customer_in = implode( ',', wp_parse_id_list( $qv['customer_in'] ) ); |
|
| 276 | + $this->query_where .= " AND $table.`customer_id` IN ($customer_in)"; |
|
| 277 | + } elseif ( ! empty( $qv['customer_not_in'] ) ) { |
|
| 278 | + $customer_not_in = implode( ',', wp_parse_id_list( $qv['customer_not_in'] ) ); |
|
| 279 | + $this->query_where .= " AND $table.`customer_id` NOT IN ($customer_not_in)"; |
|
| 280 | + } |
|
| 281 | + |
|
| 282 | + if ( ! empty( $qv['product_in'] ) ) { |
|
| 283 | + $product_in = implode( ',', wp_parse_id_list( $qv['product_in'] ) ); |
|
| 284 | + $this->query_where .= " AND $table.`product_id` IN ($product_in)"; |
|
| 285 | + } elseif ( ! empty( $qv['product_not_in'] ) ) { |
|
| 286 | + $product_not_in = implode( ',', wp_parse_id_list( $qv['product_not_in'] ) ); |
|
| 287 | + $this->query_where .= " AND $table.`product_id` NOT IN ($product_not_in)"; |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + if ( ! empty( $qv['invoice_in'] ) ) { |
|
| 291 | + $invoice_in = implode( ',', wp_parse_id_list( $qv['invoice_in'] ) ); |
|
| 292 | + $this->query_where .= " AND $table.`parent_payment_id` IN ($invoice_in)"; |
|
| 293 | + } elseif ( ! empty( $qv['invoice_not_in'] ) ) { |
|
| 294 | + $invoice_not_in = implode( ',', wp_parse_id_list( $qv['invoice_not_in'] ) ); |
|
| 295 | + $this->query_where .= " AND $table.`parent_payment_id` NOT IN ($invoice_not_in)"; |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + if ( ! empty( $qv['include'] ) ) { |
|
| 299 | + $include = implode( ',', wp_parse_id_list( $qv['include'] ) ); |
|
| 300 | + $this->query_where .= " AND $table.`id` IN ($include)"; |
|
| 301 | + } elseif ( ! empty( $qv['exclude'] ) ) { |
|
| 302 | + $exclude = implode( ',', wp_parse_id_list( $qv['exclude'] ) ); |
|
| 303 | + $this->query_where .= " AND $table.`id` NOT IN ($exclude)"; |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + // Date queries are allowed for the subscription creation date. |
|
| 307 | + if ( ! empty( $qv['date_created_query'] ) && is_array( $qv['date_created_query'] ) ) { |
|
| 308 | + $date_created_query = new WP_Date_Query( $qv['date_created_query'], "$table.created" ); |
|
| 309 | + $this->query_where .= $date_created_query->get_sql(); |
|
| 310 | + } |
|
| 311 | + |
|
| 312 | + // Date queries are also allowed for the subscription expiration date. |
|
| 313 | + if ( ! empty( $qv['date_expires_query'] ) && is_array( $qv['date_expires_query'] ) ) { |
|
| 314 | + $date_expires_query = new WP_Date_Query( $qv['date_expires_query'], "$table.expiration" ); |
|
| 315 | + $this->query_where .= $date_expires_query->get_sql(); |
|
| 316 | + } |
|
| 317 | + |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + /** |
|
| 321 | + * Prepares the query order. |
|
| 322 | + * |
|
| 323 | + * @since 1.0.19 |
|
| 324 | + * |
|
| 325 | + * @param array $qv Query vars. |
|
| 326 | + * @param string $table Table name. |
|
| 327 | + */ |
|
| 328 | + protected function prepare_query_order( &$qv, $table ) { |
|
| 329 | + |
|
| 330 | + // sorting. |
|
| 331 | + $qv['order'] = isset( $qv['order'] ) ? strtoupper( $qv['order'] ) : ''; |
|
| 332 | + $order = $this->parse_order( $qv['order'] ); |
|
| 333 | + |
|
| 334 | + // Default order is by 'id' (latest subscriptions). |
|
| 335 | + if ( empty( $qv['orderby'] ) ) { |
|
| 336 | + $qv['orderby'] = array( 'id' ); |
|
| 337 | + } |
|
| 338 | + |
|
| 339 | + // 'orderby' values may be an array, comma- or space-separated list. |
|
| 340 | + $ordersby = array_filter( wpinv_parse_list( $qv['orderby'] ) ); |
|
| 341 | + |
|
| 342 | + $orderby_array = array(); |
|
| 343 | + foreach ( $ordersby as $_key => $_value ) { |
|
| 344 | + |
|
| 345 | + if ( is_int( $_key ) ) { |
|
| 346 | + // Integer key means this is a flat array of 'orderby' fields. |
|
| 347 | + $_orderby = $_value; |
|
| 348 | + $_order = $order; |
|
| 349 | + } else { |
|
| 350 | + // Non-integer key means that the key is the field and the value is ASC/DESC. |
|
| 351 | + $_orderby = $_key; |
|
| 352 | + $_order = $_value; |
|
| 353 | + } |
|
| 354 | + |
|
| 355 | + $parsed = $this->parse_orderby( $_orderby, $table ); |
|
| 356 | + |
|
| 357 | + if ( $parsed ) { |
|
| 358 | + $orderby_array[] = $parsed . ' ' . $this->parse_order( $_order ); |
|
| 359 | + } |
|
| 360 | + |
|
| 361 | + } |
|
| 362 | + |
|
| 363 | + // If no valid clauses were found, order by id. |
|
| 364 | + if ( empty( $orderby_array ) ) { |
|
| 365 | + $orderby_array[] = "id $order"; |
|
| 366 | + } |
|
| 367 | + |
|
| 368 | + $this->query_orderby = 'ORDER BY ' . implode( ', ', $orderby_array ); |
|
| 369 | + |
|
| 370 | + } |
|
| 371 | + |
|
| 372 | + /** |
|
| 373 | + * Execute the query, with the current variables. |
|
| 374 | + * |
|
| 375 | + * @since 1.0.19 |
|
| 376 | + * |
|
| 377 | + * @global wpdb $wpdb WordPress database abstraction object. |
|
| 378 | + */ |
|
| 379 | + public function query() { |
|
| 380 | + global $wpdb; |
|
| 381 | + |
|
| 382 | + $qv =& $this->query_vars; |
|
| 383 | + |
|
| 384 | + // Return a non-null value to bypass the default GetPaid subscriptions query and remember to set the |
|
| 385 | + // total_subscriptions property. |
|
| 386 | + $this->results = apply_filters_ref_array( 'getpaid_subscriptions_pre_query', array( null, &$this ) ); |
|
| 387 | + |
|
| 388 | + if ( null === $this->results ) { |
|
| 389 | + $this->request = "SELECT $this->query_fields $this->query_from $this->query_where $this->query_orderby $this->query_limit"; |
|
| 390 | + |
|
| 391 | + if ( ( is_array( $qv['fields'] ) && 1 != count( $qv['fields'] ) ) || 'all' == $qv['fields'] ) { |
|
| 392 | + $this->results = $wpdb->get_results( $this->request ); |
|
| 393 | + } else { |
|
| 394 | + $this->results = $wpdb->get_col( $this->request ); |
|
| 395 | + } |
|
| 396 | + |
|
| 397 | + if ( isset( $qv['count_total'] ) && $qv['count_total'] ) { |
|
| 398 | + $found_subscriptions_query = apply_filters( 'getpaid_found_subscriptions_query', 'SELECT FOUND_ROWS()', $this ); |
|
| 399 | + $this->total_subscriptions = (int) $wpdb->get_var( $found_subscriptions_query ); |
|
| 400 | + } |
|
| 401 | + } |
|
| 402 | + |
|
| 403 | + if ( 'all' == $qv['fields'] ) { |
|
| 404 | + foreach ( $this->results as $key => $subscription ) { |
|
| 405 | + wp_cache_set( $subscription->id, $subscription, 'getpaid_subscriptions' ); |
|
| 406 | + wp_cache_set( $subscription->profile_id, $subscription->id, 'getpaid_subscription_profile_ids_to_subscription_ids' ); |
|
| 407 | + wp_cache_set( $subscription->transaction_id, $subscription->id, 'getpaid_subscription_transaction_ids_to_subscription_ids' ); |
|
| 408 | + wp_cache_set( $subscription->transaction_id, $subscription->id, 'getpaid_subscription_transaction_ids_to_subscription_ids' ); |
|
| 409 | + $this->results[ $key ] = new WPInv_Subscription( $subscription ); |
|
| 410 | + } |
|
| 411 | + } |
|
| 412 | + |
|
| 413 | + } |
|
| 414 | + |
|
| 415 | + /** |
|
| 416 | + * Retrieve query variable. |
|
| 417 | + * |
|
| 418 | + * @since 1.0.19 |
|
| 419 | + * |
|
| 420 | + * @param string $query_var Query variable key. |
|
| 421 | + * @return mixed |
|
| 422 | + */ |
|
| 423 | + public function get( $query_var ) { |
|
| 424 | + if ( isset( $this->query_vars[ $query_var ] ) ) { |
|
| 425 | + return $this->query_vars[ $query_var ]; |
|
| 426 | + } |
|
| 427 | + |
|
| 428 | + return null; |
|
| 429 | + } |
|
| 430 | + |
|
| 431 | + /** |
|
| 432 | + * Set query variable. |
|
| 433 | + * |
|
| 434 | + * @since 1.0.19 |
|
| 435 | + * |
|
| 436 | + * @param string $query_var Query variable key. |
|
| 437 | + * @param mixed $value Query variable value. |
|
| 438 | + */ |
|
| 439 | + public function set( $query_var, $value ) { |
|
| 440 | + $this->query_vars[ $query_var ] = $value; |
|
| 441 | + } |
|
| 442 | + |
|
| 443 | + /** |
|
| 444 | + * Return the list of subscriptions. |
|
| 445 | + * |
|
| 446 | + * @since 1.0.19 |
|
| 447 | + * |
|
| 448 | + * @return WPInv_Subscription[]|array Found subscriptions. |
|
| 449 | + */ |
|
| 450 | + public function get_results() { |
|
| 451 | + return $this->results; |
|
| 452 | + } |
|
| 453 | + |
|
| 454 | + /** |
|
| 455 | + * Return the total number of subscriptions for the current query. |
|
| 456 | + * |
|
| 457 | + * @since 1.0.19 |
|
| 458 | + * |
|
| 459 | + * @return int Number of total subscriptions. |
|
| 460 | + */ |
|
| 461 | + public function get_total() { |
|
| 462 | + return $this->total_subscriptions; |
|
| 463 | + } |
|
| 464 | + |
|
| 465 | + /** |
|
| 466 | + * Parse and sanitize 'orderby' keys passed to the subscriptions query. |
|
| 467 | + * |
|
| 468 | + * @since 1.0.19 |
|
| 469 | + * |
|
| 470 | + * @param string $orderby Alias for the field to order by. |
|
| 471 | + * @param string $table The current table. |
|
| 472 | + * @return string Value to use in the ORDER clause, if `$orderby` is valid. |
|
| 473 | + */ |
|
| 474 | + protected function parse_orderby( $orderby, $table ) { |
|
| 475 | + |
|
| 476 | + $_orderby = ''; |
|
| 477 | + if ( in_array( $orderby, array( 'customer_id', 'frequency', 'period', 'initial_amount', 'recurring_amount', 'bill_times', 'transaction_id', 'parent_payment_id', 'product_id', 'created', 'expiration', 'trial_period', 'status', 'profile_id' ) ) ) { |
|
| 478 | + $_orderby = "$table.`$orderby`"; |
|
| 479 | + } elseif ( 'id' === strtolower( $orderby ) ) { |
|
| 480 | + $_orderby = "$table.id"; |
|
| 481 | + } elseif ( 'include' === $orderby && ! empty( $this->query_vars['include'] ) ) { |
|
| 482 | + $include = wp_parse_id_list( $this->query_vars['include'] ); |
|
| 483 | + $include_sql = implode( ',', $include ); |
|
| 484 | + $_orderby = "FIELD( $table.id, $include_sql )"; |
|
| 485 | + } |
|
| 486 | + |
|
| 487 | + return $_orderby; |
|
| 488 | + } |
|
| 489 | + |
|
| 490 | + /** |
|
| 491 | + * Parse an 'order' query variable and cast it to ASC or DESC as necessary. |
|
| 492 | + * |
|
| 493 | + * @since 1.0.19 |
|
| 494 | + * |
|
| 495 | + * @param string $order The 'order' query variable. |
|
| 496 | + * @return string The sanitized 'order' query variable. |
|
| 497 | + */ |
|
| 498 | + protected function parse_order( $order ) { |
|
| 499 | + if ( ! is_string( $order ) || empty( $order ) ) { |
|
| 500 | + return 'DESC'; |
|
| 501 | + } |
|
| 502 | + |
|
| 503 | + if ( 'ASC' === strtoupper( $order ) ) { |
|
| 504 | + return 'ASC'; |
|
| 505 | + } else { |
|
| 506 | + return 'DESC'; |
|
| 507 | + } |
|
| 508 | + } |
|
| 509 | 509 | |
| 510 | 510 | } |
@@ -14,144 +14,144 @@ discard block |
||
| 14 | 14 | */ |
| 15 | 15 | class WPInv_Subscriptions_Widget extends WP_Super_Duper { |
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * Register the widget with WordPress. |
|
| 19 | - * |
|
| 20 | - */ |
|
| 21 | - public function __construct() { |
|
| 22 | - |
|
| 23 | - $options = array( |
|
| 24 | - 'textdomain' => 'invoicing', |
|
| 25 | - 'block-icon' => 'controls-repeat', |
|
| 26 | - 'block-category'=> 'widgets', |
|
| 27 | - 'block-keywords'=> "['invoicing','subscriptions', 'getpaid']", |
|
| 28 | - 'class_name' => __CLASS__, |
|
| 29 | - 'base_id' => 'wpinv_subscriptions', |
|
| 30 | - 'name' => __( 'GetPaid > Subscriptions', 'invoicing' ), |
|
| 31 | - 'widget_ops' => array( |
|
| 32 | - 'classname' => 'getpaid-subscriptions bsui', |
|
| 33 | - 'description' => esc_html__( "Displays the current user's subscriptions.", 'invoicing' ), |
|
| 34 | - ), |
|
| 35 | - 'arguments' => array( |
|
| 36 | - 'title' => array( |
|
| 37 | - 'title' => __( 'Widget title', 'invoicing' ), |
|
| 38 | - 'desc' => __( 'Enter widget title.', 'invoicing' ), |
|
| 39 | - 'type' => 'text', |
|
| 40 | - 'desc_tip' => true, |
|
| 41 | - 'default' => '', |
|
| 42 | - 'advanced' => false |
|
| 43 | - ), |
|
| 44 | - ) |
|
| 45 | - |
|
| 46 | - ); |
|
| 47 | - |
|
| 48 | - |
|
| 49 | - parent::__construct( $options ); |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * Retrieves current user's subscriptions. |
|
| 54 | - * |
|
| 55 | - * @return GetPaid_Subscriptions_Query |
|
| 56 | - */ |
|
| 57 | - public function get_subscriptions() { |
|
| 58 | - |
|
| 59 | - // Prepare license args. |
|
| 60 | - $args = array( |
|
| 61 | - 'customer_in' => get_current_user_id(), |
|
| 62 | - 'paged' => ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1, |
|
| 63 | - ); |
|
| 64 | - |
|
| 65 | - return new GetPaid_Subscriptions_Query( $args ); |
|
| 66 | - |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * The Super block output function. |
|
| 71 | - * |
|
| 72 | - * @param array $args |
|
| 73 | - * @param array $widget_args |
|
| 74 | - * @param string $content |
|
| 75 | - * |
|
| 76 | - * @return mixed|string|bool |
|
| 77 | - */ |
|
| 78 | - public function output( $args = array(), $widget_args = array(), $content = '' ) { |
|
| 79 | - |
|
| 80 | - // Ensure that the user is logged in. |
|
| 81 | - if ( ! is_user_logged_in() ) { |
|
| 82 | - |
|
| 83 | - return aui()->alert( |
|
| 84 | - array( |
|
| 85 | - 'content' => wp_kses_post( __( 'You need to log-in or create an account to view this section.', 'invoicing' ) ), |
|
| 86 | - 'type' => 'error', |
|
| 87 | - ) |
|
| 88 | - ); |
|
| 89 | - |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - // Are we displaying a single subscription? |
|
| 93 | - if ( isset( $_GET['subscription'] ) ) { |
|
| 94 | - return $this->display_single_subscription( trim( $_GET['subscription'] ) ); |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - // Retrieve the user's subscriptions. |
|
| 98 | - $subscriptions = $this->get_subscriptions(); |
|
| 99 | - |
|
| 100 | - // Start the output buffer. |
|
| 101 | - ob_start(); |
|
| 102 | - |
|
| 103 | - // Backwards compatibility. |
|
| 104 | - do_action( 'wpinv_before_user_subscriptions' ); |
|
| 105 | - |
|
| 106 | - // Display errors and notices. |
|
| 107 | - wpinv_print_errors(); |
|
| 108 | - |
|
| 109 | - do_action( 'getpaid_license_manager_before_subscriptions', $subscriptions ); |
|
| 110 | - |
|
| 111 | - // Print the table header. |
|
| 112 | - $this->print_table_header(); |
|
| 113 | - |
|
| 114 | - // Print table body. |
|
| 115 | - $this->print_table_body( $subscriptions->get_results() ); |
|
| 116 | - |
|
| 117 | - // Print table footer. |
|
| 118 | - $this->print_table_footer(); |
|
| 119 | - |
|
| 120 | - // Print the navigation. |
|
| 121 | - $this->print_navigation( $subscriptions->get_total() ); |
|
| 122 | - |
|
| 123 | - // Backwards compatibility. |
|
| 124 | - do_action( 'wpinv_after_user_subscriptions' ); |
|
| 125 | - |
|
| 126 | - // Return the output. |
|
| 127 | - return ob_get_clean(); |
|
| 128 | - |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * Retrieves the subscription columns. |
|
| 133 | - * |
|
| 134 | - * @return array |
|
| 135 | - */ |
|
| 136 | - public function get_subscriptions_table_columns() { |
|
| 17 | + /** |
|
| 18 | + * Register the widget with WordPress. |
|
| 19 | + * |
|
| 20 | + */ |
|
| 21 | + public function __construct() { |
|
| 22 | + |
|
| 23 | + $options = array( |
|
| 24 | + 'textdomain' => 'invoicing', |
|
| 25 | + 'block-icon' => 'controls-repeat', |
|
| 26 | + 'block-category'=> 'widgets', |
|
| 27 | + 'block-keywords'=> "['invoicing','subscriptions', 'getpaid']", |
|
| 28 | + 'class_name' => __CLASS__, |
|
| 29 | + 'base_id' => 'wpinv_subscriptions', |
|
| 30 | + 'name' => __( 'GetPaid > Subscriptions', 'invoicing' ), |
|
| 31 | + 'widget_ops' => array( |
|
| 32 | + 'classname' => 'getpaid-subscriptions bsui', |
|
| 33 | + 'description' => esc_html__( "Displays the current user's subscriptions.", 'invoicing' ), |
|
| 34 | + ), |
|
| 35 | + 'arguments' => array( |
|
| 36 | + 'title' => array( |
|
| 37 | + 'title' => __( 'Widget title', 'invoicing' ), |
|
| 38 | + 'desc' => __( 'Enter widget title.', 'invoicing' ), |
|
| 39 | + 'type' => 'text', |
|
| 40 | + 'desc_tip' => true, |
|
| 41 | + 'default' => '', |
|
| 42 | + 'advanced' => false |
|
| 43 | + ), |
|
| 44 | + ) |
|
| 45 | + |
|
| 46 | + ); |
|
| 47 | + |
|
| 48 | + |
|
| 49 | + parent::__construct( $options ); |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * Retrieves current user's subscriptions. |
|
| 54 | + * |
|
| 55 | + * @return GetPaid_Subscriptions_Query |
|
| 56 | + */ |
|
| 57 | + public function get_subscriptions() { |
|
| 58 | + |
|
| 59 | + // Prepare license args. |
|
| 60 | + $args = array( |
|
| 61 | + 'customer_in' => get_current_user_id(), |
|
| 62 | + 'paged' => ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1, |
|
| 63 | + ); |
|
| 64 | + |
|
| 65 | + return new GetPaid_Subscriptions_Query( $args ); |
|
| 66 | + |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * The Super block output function. |
|
| 71 | + * |
|
| 72 | + * @param array $args |
|
| 73 | + * @param array $widget_args |
|
| 74 | + * @param string $content |
|
| 75 | + * |
|
| 76 | + * @return mixed|string|bool |
|
| 77 | + */ |
|
| 78 | + public function output( $args = array(), $widget_args = array(), $content = '' ) { |
|
| 79 | + |
|
| 80 | + // Ensure that the user is logged in. |
|
| 81 | + if ( ! is_user_logged_in() ) { |
|
| 82 | + |
|
| 83 | + return aui()->alert( |
|
| 84 | + array( |
|
| 85 | + 'content' => wp_kses_post( __( 'You need to log-in or create an account to view this section.', 'invoicing' ) ), |
|
| 86 | + 'type' => 'error', |
|
| 87 | + ) |
|
| 88 | + ); |
|
| 89 | + |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + // Are we displaying a single subscription? |
|
| 93 | + if ( isset( $_GET['subscription'] ) ) { |
|
| 94 | + return $this->display_single_subscription( trim( $_GET['subscription'] ) ); |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + // Retrieve the user's subscriptions. |
|
| 98 | + $subscriptions = $this->get_subscriptions(); |
|
| 99 | + |
|
| 100 | + // Start the output buffer. |
|
| 101 | + ob_start(); |
|
| 102 | + |
|
| 103 | + // Backwards compatibility. |
|
| 104 | + do_action( 'wpinv_before_user_subscriptions' ); |
|
| 105 | + |
|
| 106 | + // Display errors and notices. |
|
| 107 | + wpinv_print_errors(); |
|
| 108 | + |
|
| 109 | + do_action( 'getpaid_license_manager_before_subscriptions', $subscriptions ); |
|
| 110 | + |
|
| 111 | + // Print the table header. |
|
| 112 | + $this->print_table_header(); |
|
| 113 | + |
|
| 114 | + // Print table body. |
|
| 115 | + $this->print_table_body( $subscriptions->get_results() ); |
|
| 116 | + |
|
| 117 | + // Print table footer. |
|
| 118 | + $this->print_table_footer(); |
|
| 119 | + |
|
| 120 | + // Print the navigation. |
|
| 121 | + $this->print_navigation( $subscriptions->get_total() ); |
|
| 122 | + |
|
| 123 | + // Backwards compatibility. |
|
| 124 | + do_action( 'wpinv_after_user_subscriptions' ); |
|
| 125 | + |
|
| 126 | + // Return the output. |
|
| 127 | + return ob_get_clean(); |
|
| 128 | + |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * Retrieves the subscription columns. |
|
| 133 | + * |
|
| 134 | + * @return array |
|
| 135 | + */ |
|
| 136 | + public function get_subscriptions_table_columns() { |
|
| 137 | 137 | |
| 138 | - $columns = array( |
|
| 139 | - 'subscription' => __( 'Subscription', 'invoicing' ), |
|
| 140 | - 'amount' => __( 'Amount', 'invoicing' ), |
|
| 141 | - 'renewal-date' => __( 'Next payment', 'invoicing' ), |
|
| 142 | - 'status' => __( 'Status', 'invoicing' ), |
|
| 143 | - ); |
|
| 138 | + $columns = array( |
|
| 139 | + 'subscription' => __( 'Subscription', 'invoicing' ), |
|
| 140 | + 'amount' => __( 'Amount', 'invoicing' ), |
|
| 141 | + 'renewal-date' => __( 'Next payment', 'invoicing' ), |
|
| 142 | + 'status' => __( 'Status', 'invoicing' ), |
|
| 143 | + ); |
|
| 144 | 144 | |
| 145 | - return apply_filters( 'getpaid_frontend_subscriptions_table_columns', $columns ); |
|
| 146 | - } |
|
| 145 | + return apply_filters( 'getpaid_frontend_subscriptions_table_columns', $columns ); |
|
| 146 | + } |
|
| 147 | 147 | |
| 148 | - /** |
|
| 149 | - * Displays the table header. |
|
| 150 | - * |
|
| 151 | - */ |
|
| 152 | - public function print_table_header() { |
|
| 148 | + /** |
|
| 149 | + * Displays the table header. |
|
| 150 | + * |
|
| 151 | + */ |
|
| 152 | + public function print_table_header() { |
|
| 153 | 153 | |
| 154 | - ?> |
|
| 154 | + ?> |
|
| 155 | 155 | |
| 156 | 156 | <table class="table table-bordered table-striped"> |
| 157 | 157 | |
@@ -167,120 +167,120 @@ discard block |
||
| 167 | 167 | |
| 168 | 168 | <?php |
| 169 | 169 | |
| 170 | - } |
|
| 170 | + } |
|
| 171 | 171 | |
| 172 | - /** |
|
| 173 | - * Displays the table body. |
|
| 174 | - * |
|
| 175 | - * @param WPInv_Subscription[] $subscriptions |
|
| 176 | - */ |
|
| 177 | - public function print_table_body( $subscriptions ) { |
|
| 172 | + /** |
|
| 173 | + * Displays the table body. |
|
| 174 | + * |
|
| 175 | + * @param WPInv_Subscription[] $subscriptions |
|
| 176 | + */ |
|
| 177 | + public function print_table_body( $subscriptions ) { |
|
| 178 | 178 | |
| 179 | - if ( empty( $subscriptions ) ) { |
|
| 180 | - $this->print_table_body_no_subscriptions(); |
|
| 181 | - } else { |
|
| 182 | - $this->print_table_body_subscriptions( $subscriptions ); |
|
| 183 | - } |
|
| 179 | + if ( empty( $subscriptions ) ) { |
|
| 180 | + $this->print_table_body_no_subscriptions(); |
|
| 181 | + } else { |
|
| 182 | + $this->print_table_body_subscriptions( $subscriptions ); |
|
| 183 | + } |
|
| 184 | 184 | |
| 185 | - } |
|
| 185 | + } |
|
| 186 | 186 | |
| 187 | - /** |
|
| 188 | - * Displays the table body if no subscriptions were found. |
|
| 189 | - * |
|
| 190 | - */ |
|
| 191 | - public function print_table_body_no_subscriptions() { |
|
| 187 | + /** |
|
| 188 | + * Displays the table body if no subscriptions were found. |
|
| 189 | + * |
|
| 190 | + */ |
|
| 191 | + public function print_table_body_no_subscriptions() { |
|
| 192 | 192 | |
| 193 | - ?> |
|
| 193 | + ?> |
|
| 194 | 194 | <tbody> |
| 195 | 195 | |
| 196 | 196 | <tr> |
| 197 | 197 | <td colspan="<?php echo count( $this->get_subscriptions_table_columns() ); ?>"> |
| 198 | 198 | |
| 199 | 199 | <?php |
| 200 | - echo aui()->alert( |
|
| 201 | - array( |
|
| 202 | - 'content' => wp_kses_post( __( 'No subscriptions found.', 'invoicing' ) ), |
|
| 203 | - 'type' => 'warning', |
|
| 204 | - ) |
|
| 205 | - ); |
|
| 206 | - ?> |
|
| 200 | + echo aui()->alert( |
|
| 201 | + array( |
|
| 202 | + 'content' => wp_kses_post( __( 'No subscriptions found.', 'invoicing' ) ), |
|
| 203 | + 'type' => 'warning', |
|
| 204 | + ) |
|
| 205 | + ); |
|
| 206 | + ?> |
|
| 207 | 207 | |
| 208 | 208 | </td> |
| 209 | 209 | </tr> |
| 210 | 210 | |
| 211 | 211 | </tbody> |
| 212 | 212 | <?php |
| 213 | - } |
|
| 213 | + } |
|
| 214 | 214 | |
| 215 | - /** |
|
| 216 | - * Displays the table body if subscriptions were found. |
|
| 217 | - * |
|
| 218 | - * @param WPInv_Subscription[] $subscriptions |
|
| 219 | - */ |
|
| 220 | - public function print_table_body_subscriptions( $subscriptions ) { |
|
| 215 | + /** |
|
| 216 | + * Displays the table body if subscriptions were found. |
|
| 217 | + * |
|
| 218 | + * @param WPInv_Subscription[] $subscriptions |
|
| 219 | + */ |
|
| 220 | + public function print_table_body_subscriptions( $subscriptions ) { |
|
| 221 | 221 | |
| 222 | - ?> |
|
| 222 | + ?> |
|
| 223 | 223 | <tbody> |
| 224 | 224 | |
| 225 | 225 | <?php foreach ( $subscriptions as $subscription ) : ?> |
| 226 | 226 | <tr class="getpaid-subscriptions-table-row subscription-<?php echo (int) $subscription->get_id(); ?>"> |
| 227 | 227 | <?php |
| 228 | - wpinv_get_template( |
|
| 229 | - 'subscriptions/subscriptions-table-row.php', |
|
| 230 | - array( |
|
| 231 | - 'subscription' => $subscription, |
|
| 232 | - 'widget' => $this |
|
| 233 | - ) |
|
| 234 | - ); |
|
| 235 | - ?> |
|
| 228 | + wpinv_get_template( |
|
| 229 | + 'subscriptions/subscriptions-table-row.php', |
|
| 230 | + array( |
|
| 231 | + 'subscription' => $subscription, |
|
| 232 | + 'widget' => $this |
|
| 233 | + ) |
|
| 234 | + ); |
|
| 235 | + ?> |
|
| 236 | 236 | </tr> |
| 237 | 237 | <?php endforeach; ?> |
| 238 | 238 | |
| 239 | 239 | </tbody> |
| 240 | 240 | <?php |
| 241 | - } |
|
| 242 | - |
|
| 243 | - /** |
|
| 244 | - * Adds row actions to a column |
|
| 245 | - * |
|
| 246 | - * @param string $content column content |
|
| 247 | - * @param WPInv_Subscription $subscription |
|
| 248 | - * @since 1.0.0 |
|
| 249 | - * @return string |
|
| 250 | - */ |
|
| 251 | - public function add_row_actions( $content, $subscription ) { |
|
| 252 | - |
|
| 253 | - // Prepare row actions. |
|
| 254 | - $actions = array(); |
|
| 255 | - |
|
| 256 | - // View subscription action. |
|
| 257 | - $view_url = esc_url( add_query_arg( 'subscription', (int) $subscription->get_id(), get_permalink( (int) wpinv_get_option( 'invoice_subscription_page' ) ) ) ); |
|
| 258 | - $actions['view'] = "<a href='$view_url' class='text-decoration-none'>" . __( 'Manage Subscription', 'invoicing' ) . '</a>'; |
|
| 259 | - |
|
| 260 | - // Filter the actions. |
|
| 261 | - $actions = apply_filters( 'getpaid_subscriptions_table_subscription_actions', $actions, $subscription ); |
|
| 262 | - |
|
| 263 | - $sanitized = array(); |
|
| 264 | - foreach ( $actions as $key => $action ) { |
|
| 265 | - $key = sanitize_html_class( $key ); |
|
| 266 | - $action = wp_kses_post( $action ); |
|
| 267 | - $sanitized[] = "<span class='$key'>$action</span>"; |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - $row_actions = "<small class='form-text getpaid-subscription-item-actions'>"; |
|
| 271 | - $row_actions .= implode( ' | ', $sanitized ); |
|
| 272 | - $row_actions .= '</small>'; |
|
| 273 | - |
|
| 274 | - return $content . $row_actions; |
|
| 275 | - } |
|
| 276 | - |
|
| 277 | - /** |
|
| 278 | - * Displays the table footer. |
|
| 279 | - * |
|
| 280 | - */ |
|
| 281 | - public function print_table_footer() { |
|
| 282 | - |
|
| 283 | - ?> |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + /** |
|
| 244 | + * Adds row actions to a column |
|
| 245 | + * |
|
| 246 | + * @param string $content column content |
|
| 247 | + * @param WPInv_Subscription $subscription |
|
| 248 | + * @since 1.0.0 |
|
| 249 | + * @return string |
|
| 250 | + */ |
|
| 251 | + public function add_row_actions( $content, $subscription ) { |
|
| 252 | + |
|
| 253 | + // Prepare row actions. |
|
| 254 | + $actions = array(); |
|
| 255 | + |
|
| 256 | + // View subscription action. |
|
| 257 | + $view_url = esc_url( add_query_arg( 'subscription', (int) $subscription->get_id(), get_permalink( (int) wpinv_get_option( 'invoice_subscription_page' ) ) ) ); |
|
| 258 | + $actions['view'] = "<a href='$view_url' class='text-decoration-none'>" . __( 'Manage Subscription', 'invoicing' ) . '</a>'; |
|
| 259 | + |
|
| 260 | + // Filter the actions. |
|
| 261 | + $actions = apply_filters( 'getpaid_subscriptions_table_subscription_actions', $actions, $subscription ); |
|
| 262 | + |
|
| 263 | + $sanitized = array(); |
|
| 264 | + foreach ( $actions as $key => $action ) { |
|
| 265 | + $key = sanitize_html_class( $key ); |
|
| 266 | + $action = wp_kses_post( $action ); |
|
| 267 | + $sanitized[] = "<span class='$key'>$action</span>"; |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + $row_actions = "<small class='form-text getpaid-subscription-item-actions'>"; |
|
| 271 | + $row_actions .= implode( ' | ', $sanitized ); |
|
| 272 | + $row_actions .= '</small>'; |
|
| 273 | + |
|
| 274 | + return $content . $row_actions; |
|
| 275 | + } |
|
| 276 | + |
|
| 277 | + /** |
|
| 278 | + * Displays the table footer. |
|
| 279 | + * |
|
| 280 | + */ |
|
| 281 | + public function print_table_footer() { |
|
| 282 | + |
|
| 283 | + ?> |
|
| 284 | 284 | |
| 285 | 285 | <tfoot> |
| 286 | 286 | <tr> |
@@ -295,129 +295,129 @@ discard block |
||
| 295 | 295 | </table> |
| 296 | 296 | <?php |
| 297 | 297 | |
| 298 | - } |
|
| 298 | + } |
|
| 299 | 299 | |
| 300 | - /** |
|
| 301 | - * Displays the navigation. |
|
| 302 | - * |
|
| 303 | - * @param int $total |
|
| 304 | - */ |
|
| 305 | - public function print_navigation( $total ) { |
|
| 300 | + /** |
|
| 301 | + * Displays the navigation. |
|
| 302 | + * |
|
| 303 | + * @param int $total |
|
| 304 | + */ |
|
| 305 | + public function print_navigation( $total ) { |
|
| 306 | 306 | |
| 307 | - if ( $total < 1 ) { |
|
| 307 | + if ( $total < 1 ) { |
|
| 308 | 308 | |
| 309 | - // Out-of-bounds, run the query again without LIMIT for total count. |
|
| 310 | - $args = array( |
|
| 311 | - 'customer_in' => get_current_user_id(), |
|
| 312 | - 'fields' => 'id', |
|
| 313 | - ); |
|
| 309 | + // Out-of-bounds, run the query again without LIMIT for total count. |
|
| 310 | + $args = array( |
|
| 311 | + 'customer_in' => get_current_user_id(), |
|
| 312 | + 'fields' => 'id', |
|
| 313 | + ); |
|
| 314 | 314 | |
| 315 | - $count_query = new GetPaid_Subscriptions_Query( $args ); |
|
| 316 | - $total = $count_query->get_total(); |
|
| 317 | - } |
|
| 315 | + $count_query = new GetPaid_Subscriptions_Query( $args ); |
|
| 316 | + $total = $count_query->get_total(); |
|
| 317 | + } |
|
| 318 | 318 | |
| 319 | - // Abort if we do not have pages. |
|
| 320 | - if ( 2 > $total ) { |
|
| 321 | - return; |
|
| 322 | - } |
|
| 319 | + // Abort if we do not have pages. |
|
| 320 | + if ( 2 > $total ) { |
|
| 321 | + return; |
|
| 322 | + } |
|
| 323 | 323 | |
| 324 | - ?> |
|
| 324 | + ?> |
|
| 325 | 325 | |
| 326 | 326 | <div class="getpaid-subscriptions-pagination"> |
| 327 | 327 | <?php |
| 328 | - $big = 999999; |
|
| 329 | - |
|
| 330 | - echo getpaid_paginate_links( |
|
| 331 | - array( |
|
| 332 | - 'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ), |
|
| 333 | - 'format' => '?paged=%#%', |
|
| 334 | - 'total' => (int) ceil( $total / 10 ), |
|
| 335 | - ) |
|
| 336 | - ); |
|
| 337 | - ?> |
|
| 328 | + $big = 999999; |
|
| 329 | + |
|
| 330 | + echo getpaid_paginate_links( |
|
| 331 | + array( |
|
| 332 | + 'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ), |
|
| 333 | + 'format' => '?paged=%#%', |
|
| 334 | + 'total' => (int) ceil( $total / 10 ), |
|
| 335 | + ) |
|
| 336 | + ); |
|
| 337 | + ?> |
|
| 338 | 338 | </div> |
| 339 | 339 | |
| 340 | 340 | <?php |
| 341 | - } |
|
| 342 | - |
|
| 343 | - /** |
|
| 344 | - * Returns a single subscription's columns. |
|
| 345 | - * |
|
| 346 | - * @param WPInv_Subscription $subscription |
|
| 347 | - * |
|
| 348 | - * @return array |
|
| 349 | - */ |
|
| 350 | - public function get_single_subscription_columns( $subscription ) { |
|
| 351 | - |
|
| 352 | - // Prepare subscription detail columns. |
|
| 353 | - $fields = apply_filters( |
|
| 354 | - 'getpaid_single_subscription_details_fields', |
|
| 355 | - array( |
|
| 356 | - 'status' => __( 'Status', 'invoicing' ), |
|
| 357 | - 'initial_amount' => __( 'Initial amount', 'invoicing' ), |
|
| 358 | - 'recurring_amount' => __( 'Recurring amount', 'invoicing' ), |
|
| 359 | - 'start_date' => __( 'Start date', 'invoicing' ), |
|
| 360 | - 'expiry_date' => __( 'Next payment', 'invoicing' ), |
|
| 361 | - 'payments' => __( 'Payments', 'invoicing' ), |
|
| 362 | - 'item' => __( 'Item', 'invoicing' ), |
|
| 363 | - ), |
|
| 364 | - $subscription |
|
| 365 | - ); |
|
| 366 | - |
|
| 367 | - if ( ! $subscription->is_active() || $subscription->is_last_renewal() ) { |
|
| 368 | - $fields['expiry_date'] = __( 'End date', 'invoicing' ); |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - if ( $subscription->get_initial_amount() == $subscription->get_recurring_amount() ) { |
|
| 372 | - unset( $fields['initial_amount'] ); |
|
| 373 | - } |
|
| 374 | - |
|
| 375 | - return $fields; |
|
| 376 | - } |
|
| 377 | - |
|
| 378 | - /** |
|
| 379 | - * Displays a single subscription. |
|
| 380 | - * |
|
| 381 | - * @param string $subscription |
|
| 382 | - * |
|
| 383 | - * @return string |
|
| 384 | - */ |
|
| 385 | - public function display_single_subscription( $subscription ) { |
|
| 386 | - |
|
| 387 | - // Fetch the subscription. |
|
| 388 | - $subscription = new WPInv_Subscription( (int) $subscription ); |
|
| 389 | - |
|
| 390 | - if ( ! $subscription->get_id() ) { |
|
| 391 | - |
|
| 392 | - return aui()->alert( |
|
| 393 | - array( |
|
| 394 | - 'content' => wp_kses_post( __( 'Subscription not found.', 'invoicing' ) ), |
|
| 395 | - 'type' => 'error', |
|
| 396 | - ) |
|
| 397 | - ); |
|
| 398 | - |
|
| 399 | - } |
|
| 400 | - |
|
| 401 | - // Ensure that the user owns this subscription key. |
|
| 402 | - if ( get_current_user_id() != $subscription->get_customer_id() ) { |
|
| 403 | - |
|
| 404 | - return aui()->alert( |
|
| 405 | - array( |
|
| 406 | - 'content' => wp_kses_post( __( 'You do not have permission to view this subscription. Ensure that you are logged in to the account that owns the subscription.', 'invoicing' ) ), |
|
| 407 | - 'type' => 'error', |
|
| 408 | - ) |
|
| 409 | - ); |
|
| 410 | - |
|
| 411 | - } |
|
| 412 | - |
|
| 413 | - return wpinv_get_template_html( |
|
| 414 | - 'subscriptions/subscription-details.php', |
|
| 415 | - array( |
|
| 416 | - 'subscription' => $subscription, |
|
| 417 | - 'widget' => $this |
|
| 418 | - ) |
|
| 419 | - ); |
|
| 420 | - |
|
| 421 | - } |
|
| 341 | + } |
|
| 342 | + |
|
| 343 | + /** |
|
| 344 | + * Returns a single subscription's columns. |
|
| 345 | + * |
|
| 346 | + * @param WPInv_Subscription $subscription |
|
| 347 | + * |
|
| 348 | + * @return array |
|
| 349 | + */ |
|
| 350 | + public function get_single_subscription_columns( $subscription ) { |
|
| 351 | + |
|
| 352 | + // Prepare subscription detail columns. |
|
| 353 | + $fields = apply_filters( |
|
| 354 | + 'getpaid_single_subscription_details_fields', |
|
| 355 | + array( |
|
| 356 | + 'status' => __( 'Status', 'invoicing' ), |
|
| 357 | + 'initial_amount' => __( 'Initial amount', 'invoicing' ), |
|
| 358 | + 'recurring_amount' => __( 'Recurring amount', 'invoicing' ), |
|
| 359 | + 'start_date' => __( 'Start date', 'invoicing' ), |
|
| 360 | + 'expiry_date' => __( 'Next payment', 'invoicing' ), |
|
| 361 | + 'payments' => __( 'Payments', 'invoicing' ), |
|
| 362 | + 'item' => __( 'Item', 'invoicing' ), |
|
| 363 | + ), |
|
| 364 | + $subscription |
|
| 365 | + ); |
|
| 366 | + |
|
| 367 | + if ( ! $subscription->is_active() || $subscription->is_last_renewal() ) { |
|
| 368 | + $fields['expiry_date'] = __( 'End date', 'invoicing' ); |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + if ( $subscription->get_initial_amount() == $subscription->get_recurring_amount() ) { |
|
| 372 | + unset( $fields['initial_amount'] ); |
|
| 373 | + } |
|
| 374 | + |
|
| 375 | + return $fields; |
|
| 376 | + } |
|
| 377 | + |
|
| 378 | + /** |
|
| 379 | + * Displays a single subscription. |
|
| 380 | + * |
|
| 381 | + * @param string $subscription |
|
| 382 | + * |
|
| 383 | + * @return string |
|
| 384 | + */ |
|
| 385 | + public function display_single_subscription( $subscription ) { |
|
| 386 | + |
|
| 387 | + // Fetch the subscription. |
|
| 388 | + $subscription = new WPInv_Subscription( (int) $subscription ); |
|
| 389 | + |
|
| 390 | + if ( ! $subscription->get_id() ) { |
|
| 391 | + |
|
| 392 | + return aui()->alert( |
|
| 393 | + array( |
|
| 394 | + 'content' => wp_kses_post( __( 'Subscription not found.', 'invoicing' ) ), |
|
| 395 | + 'type' => 'error', |
|
| 396 | + ) |
|
| 397 | + ); |
|
| 398 | + |
|
| 399 | + } |
|
| 400 | + |
|
| 401 | + // Ensure that the user owns this subscription key. |
|
| 402 | + if ( get_current_user_id() != $subscription->get_customer_id() ) { |
|
| 403 | + |
|
| 404 | + return aui()->alert( |
|
| 405 | + array( |
|
| 406 | + 'content' => wp_kses_post( __( 'You do not have permission to view this subscription. Ensure that you are logged in to the account that owns the subscription.', 'invoicing' ) ), |
|
| 407 | + 'type' => 'error', |
|
| 408 | + ) |
|
| 409 | + ); |
|
| 410 | + |
|
| 411 | + } |
|
| 412 | + |
|
| 413 | + return wpinv_get_template_html( |
|
| 414 | + 'subscriptions/subscription-details.php', |
|
| 415 | + array( |
|
| 416 | + 'subscription' => $subscription, |
|
| 417 | + 'widget' => $this |
|
| 418 | + ) |
|
| 419 | + ); |
|
| 420 | + |
|
| 421 | + } |
|
| 422 | 422 | |
| 423 | 423 | } |
@@ -6,7 +6,7 @@ discard block |
||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | - exit; // Exit if accessed directly |
|
| 9 | + exit; // Exit if accessed directly |
|
| 10 | 10 | } |
| 11 | 11 | |
| 12 | 12 | /** |
@@ -15,10 +15,10 @@ discard block |
||
| 15 | 15 | class GetPaid_Meta_Box_Invoice_Subscription { |
| 16 | 16 | |
| 17 | 17 | /** |
| 18 | - * Output the subscription metabox. |
|
| 19 | - * |
|
| 20 | - * @param WP_Post $post |
|
| 21 | - */ |
|
| 18 | + * Output the subscription metabox. |
|
| 19 | + * |
|
| 20 | + * @param WP_Post $post |
|
| 21 | + */ |
|
| 22 | 22 | public static function output( $post ) { |
| 23 | 23 | |
| 24 | 24 | // Fetch the invoice. |
@@ -34,10 +34,10 @@ discard block |
||
| 34 | 34 | } |
| 35 | 35 | |
| 36 | 36 | /** |
| 37 | - * Output the subscription invoices. |
|
| 38 | - * |
|
| 39 | - * @param WP_Post $post |
|
| 40 | - */ |
|
| 37 | + * Output the subscription invoices. |
|
| 38 | + * |
|
| 39 | + * @param WP_Post $post |
|
| 40 | + */ |
|
| 41 | 41 | public static function output_invoices( $post ) { |
| 42 | 42 | |
| 43 | 43 | // Fetch the invoice. |
@@ -40,86 +40,86 @@ discard block |
||
| 40 | 40 | <tr class="wpinv-item wpinv-item-<?php echo $invoice_status = $invoice->get_status(); ?>"> |
| 41 | 41 | <?php |
| 42 | 42 | |
| 43 | - foreach ( wpinv_get_user_invoices_columns() as $column_id => $column_name ) : |
|
| 43 | + foreach ( wpinv_get_user_invoices_columns() as $column_id => $column_name ) : |
|
| 44 | 44 | |
| 45 | - $column_id = sanitize_html_class( $column_id ); |
|
| 46 | - $class = empty( $column_name['class'] ) ? '' : sanitize_html_class( $column_name['class'] ); |
|
| 45 | + $column_id = sanitize_html_class( $column_id ); |
|
| 46 | + $class = empty( $column_name['class'] ) ? '' : sanitize_html_class( $column_name['class'] ); |
|
| 47 | 47 | |
| 48 | - echo "<td class='$column_id $class'>"; |
|
| 49 | - switch ( $column_id ) { |
|
| 48 | + echo "<td class='$column_id $class'>"; |
|
| 49 | + switch ( $column_id ) { |
|
| 50 | 50 | |
| 51 | - case 'invoice-number': |
|
| 52 | - echo wpinv_invoice_link( $invoice ); |
|
| 53 | - break; |
|
| 51 | + case 'invoice-number': |
|
| 52 | + echo wpinv_invoice_link( $invoice ); |
|
| 53 | + break; |
|
| 54 | 54 | |
| 55 | - case 'created-date': |
|
| 56 | - echo getpaid_format_date_value( $invoice->get_date_created() ); |
|
| 57 | - break; |
|
| 55 | + case 'created-date': |
|
| 56 | + echo getpaid_format_date_value( $invoice->get_date_created() ); |
|
| 57 | + break; |
|
| 58 | 58 | |
| 59 | - case 'payment-date': |
|
| 59 | + case 'payment-date': |
|
| 60 | 60 | |
| 61 | - if ( $invoice->needs_payment() ) { |
|
| 62 | - echo "—"; |
|
| 63 | - } else { |
|
| 64 | - echo getpaid_format_date_value( $invoice->get_date_completed() ); |
|
| 65 | - } |
|
| 61 | + if ( $invoice->needs_payment() ) { |
|
| 62 | + echo "—"; |
|
| 63 | + } else { |
|
| 64 | + echo getpaid_format_date_value( $invoice->get_date_completed() ); |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - break; |
|
| 67 | + break; |
|
| 68 | 68 | |
| 69 | - case 'invoice-status': |
|
| 70 | - echo $invoice->get_status_label_html(); |
|
| 69 | + case 'invoice-status': |
|
| 70 | + echo $invoice->get_status_label_html(); |
|
| 71 | 71 | |
| 72 | - break; |
|
| 72 | + break; |
|
| 73 | 73 | |
| 74 | - case 'invoice-total': |
|
| 75 | - echo wpinv_price( wpinv_format_amount( $invoice->get_total() ) ); |
|
| 74 | + case 'invoice-total': |
|
| 75 | + echo wpinv_price( wpinv_format_amount( $invoice->get_total() ) ); |
|
| 76 | 76 | |
| 77 | - break; |
|
| 77 | + break; |
|
| 78 | 78 | |
| 79 | - case 'invoice-actions': |
|
| 79 | + case 'invoice-actions': |
|
| 80 | 80 | |
| 81 | - $actions = array( |
|
| 81 | + $actions = array( |
|
| 82 | 82 | |
| 83 | - 'pay' => array( |
|
| 84 | - 'url' => $invoice->get_checkout_payment_url(), |
|
| 85 | - 'name' => __( 'Pay Now', 'invoicing' ), |
|
| 83 | + 'pay' => array( |
|
| 84 | + 'url' => $invoice->get_checkout_payment_url(), |
|
| 85 | + 'name' => __( 'Pay Now', 'invoicing' ), |
|
| 86 | 86 | 'class' => 'btn-success' |
| 87 | - ), |
|
| 87 | + ), |
|
| 88 | 88 | |
| 89 | 89 | 'print' => array( |
| 90 | - 'url' => $invoice->get_view_url(), |
|
| 91 | - 'name' => __( 'View Invoice', 'invoicing' ), |
|
| 90 | + 'url' => $invoice->get_view_url(), |
|
| 91 | + 'name' => __( 'View Invoice', 'invoicing' ), |
|
| 92 | 92 | 'class' => 'btn-secondary', |
| 93 | 93 | 'attrs' => 'target="_blank"' |
| 94 | - ) |
|
| 95 | - ); |
|
| 94 | + ) |
|
| 95 | + ); |
|
| 96 | 96 | |
| 97 | - if ( ! $invoice->needs_payment() ) { |
|
| 98 | - unset( $actions['pay'] ); |
|
| 99 | - } |
|
| 97 | + if ( ! $invoice->needs_payment() ) { |
|
| 98 | + unset( $actions['pay'] ); |
|
| 99 | + } |
|
| 100 | 100 | |
| 101 | - $actions = apply_filters( 'wpinv_user_invoices_actions', $actions, $invoice ); |
|
| 101 | + $actions = apply_filters( 'wpinv_user_invoices_actions', $actions, $invoice ); |
|
| 102 | 102 | |
| 103 | - foreach ( $actions as $key => $action ) { |
|
| 104 | - $class = !empty($action['class']) ? sanitize_html_class($action['class']) : ''; |
|
| 105 | - echo '<a href="' . esc_url( $action['url'] ) . '" class="btn btn-sm btn-block ' . $class . ' ' . sanitize_html_class( $key ) . '" ' . ( !empty($action['attrs']) ? $action['attrs'] : '' ) . '>' . $action['name'] . '</a>'; |
|
| 106 | - } |
|
| 103 | + foreach ( $actions as $key => $action ) { |
|
| 104 | + $class = !empty($action['class']) ? sanitize_html_class($action['class']) : ''; |
|
| 105 | + echo '<a href="' . esc_url( $action['url'] ) . '" class="btn btn-sm btn-block ' . $class . ' ' . sanitize_html_class( $key ) . '" ' . ( !empty($action['attrs']) ? $action['attrs'] : '' ) . '>' . $action['name'] . '</a>'; |
|
| 106 | + } |
|
| 107 | 107 | |
| 108 | - break; |
|
| 108 | + break; |
|
| 109 | 109 | |
| 110 | - default: |
|
| 111 | - do_action( "wpinv_user_invoices_column_$column_id", $invoice ); |
|
| 112 | - break; |
|
| 110 | + default: |
|
| 111 | + do_action( "wpinv_user_invoices_column_$column_id", $invoice ); |
|
| 112 | + break; |
|
| 113 | 113 | |
| 114 | 114 | |
| 115 | - } |
|
| 115 | + } |
|
| 116 | 116 | |
| 117 | - do_action( "wpinv_user_invoices_column_after_$column_id", $invoice ); |
|
| 117 | + do_action( "wpinv_user_invoices_column_after_$column_id", $invoice ); |
|
| 118 | 118 | |
| 119 | - echo '</td>'; |
|
| 119 | + echo '</td>'; |
|
| 120 | 120 | |
| 121 | - endforeach; |
|
| 122 | - ?> |
|
| 121 | + endforeach; |
|
| 122 | + ?> |
|
| 123 | 123 | </tr> |
| 124 | 124 | |
| 125 | 125 | <?php endforeach; ?> |
@@ -132,14 +132,14 @@ discard block |
||
| 132 | 132 | <?php if ( 1 < $invoices->max_num_pages ) : ?> |
| 133 | 133 | <div class="invoicing-Pagination"> |
| 134 | 134 | <?php |
| 135 | - $big = 999999; |
|
| 136 | - |
|
| 137 | - echo paginate_links( array( |
|
| 138 | - 'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ), |
|
| 139 | - 'format' => '?paged=%#%', |
|
| 140 | - 'total' => $invoices->max_num_pages, |
|
| 141 | - ) ); |
|
| 142 | - ?> |
|
| 135 | + $big = 999999; |
|
| 136 | + |
|
| 137 | + echo paginate_links( array( |
|
| 138 | + 'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ), |
|
| 139 | + 'format' => '?paged=%#%', |
|
| 140 | + 'total' => $invoices->max_num_pages, |
|
| 141 | + ) ); |
|
| 142 | + ?> |
|
| 143 | 143 | </div> |
| 144 | 144 | <?php endif; ?> |
| 145 | 145 | |