@@ -12,455 +12,455 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class GetPaid_Invoice_Notification_Emails { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * The array of invoice email actions. |
|
| 17 | - * |
|
| 18 | - * @param array |
|
| 19 | - */ |
|
| 20 | - public $invoice_actions; |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * Class constructor |
|
| 24 | - * |
|
| 25 | - */ |
|
| 26 | - public function __construct() { |
|
| 27 | - |
|
| 28 | - $this->invoice_actions = apply_filters( |
|
| 29 | - 'getpaid_notification_email_invoice_triggers', |
|
| 30 | - array( |
|
| 31 | - 'getpaid_new_invoice' => array( 'new_invoice', 'user_invoice' ), |
|
| 32 | - 'getpaid_invoice_status_wpi-cancelled' => 'cancelled_invoice', |
|
| 33 | - 'getpaid_invoice_status_wpi-failed' => 'failed_invoice', |
|
| 34 | - 'getpaid_invoice_status_wpi-onhold' => 'onhold_invoice', |
|
| 35 | - 'getpaid_invoice_status_wpi-processing' => 'processing_invoice', |
|
| 36 | - 'getpaid_invoice_status_publish' => 'completed_invoice', |
|
| 37 | - 'getpaid_invoice_status_wpi-renewal' => 'completed_invoice', |
|
| 38 | - 'getpaid_invoice_status_wpi-refunded' => 'refunded_invoice', |
|
| 39 | - 'getpaid_new_customer_note' => 'user_note', |
|
| 40 | - 'getpaid_daily_maintenance' => 'overdue', |
|
| 41 | - ) |
|
| 42 | - ); |
|
| 43 | - |
|
| 44 | - $this->init_hooks(); |
|
| 45 | - |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * Registers email hooks. |
|
| 50 | - */ |
|
| 51 | - public function init_hooks() { |
|
| 52 | - |
|
| 53 | - add_filter( 'getpaid_get_email_merge_tags', array( $this, 'invoice_merge_tags' ), 10, 2 ); |
|
| 54 | - add_filter( 'getpaid_invoice_email_recipients', array( $this, 'filter_email_recipients' ), 10, 2 ); |
|
| 55 | - |
|
| 56 | - foreach ( $this->invoice_actions as $hook => $email_type ) { |
|
| 57 | - $this->init_email_type_hook( $hook, $email_type ); |
|
| 58 | - } |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * Registers an email hook for an invoice action. |
|
| 63 | - * |
|
| 64 | - * @param string $hook |
|
| 65 | - * @param string|array $email_type |
|
| 66 | - */ |
|
| 67 | - public function init_email_type_hook( $hook, $email_type ) { |
|
| 68 | - |
|
| 69 | - $email_type = wpinv_parse_list( $email_type ); |
|
| 70 | - |
|
| 71 | - foreach ( $email_type as $type ) { |
|
| 72 | - |
|
| 73 | - $email = new GetPaid_Notification_Email( $type ); |
|
| 74 | - |
|
| 75 | - // Abort if it is not active. |
|
| 76 | - if ( ! $email->is_active() ) { |
|
| 77 | - continue; |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - if ( method_exists( $this, $type ) ) { |
|
| 81 | - add_action( $hook, array( $this, $type ), 100, 2 ); |
|
| 82 | - continue; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - do_action( 'getpaid_invoice_init_email_type_hook', $type, $hook ); |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Filters invoice merge tags. |
|
| 92 | - * |
|
| 93 | - * @param array $merge_tags |
|
| 94 | - * @param mixed|WPInv_Invoice|WPInv_Subscription $object |
|
| 95 | - */ |
|
| 96 | - public function invoice_merge_tags( $merge_tags, $object ) { |
|
| 97 | - |
|
| 98 | - if ( is_a( $object, 'WPInv_Invoice' ) ) { |
|
| 99 | - return array_merge( |
|
| 100 | - $merge_tags, |
|
| 101 | - $this->get_invoice_merge_tags( $object ) |
|
| 102 | - ); |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - if ( is_a( $object, 'WPInv_Subscription' ) ) { |
|
| 106 | - return array_merge( |
|
| 107 | - $merge_tags, |
|
| 108 | - $this->get_invoice_merge_tags( $object->get_parent_payment() ) |
|
| 109 | - ); |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - return $merge_tags; |
|
| 113 | - |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * Generates invoice merge tags. |
|
| 118 | - * |
|
| 119 | - * @param WPInv_Invoice $invoice |
|
| 120 | - * @return array |
|
| 121 | - */ |
|
| 122 | - public function get_invoice_merge_tags( $invoice ) { |
|
| 123 | - |
|
| 124 | - // Abort if it does not exist. |
|
| 125 | - if ( ! $invoice->get_id() ) { |
|
| 126 | - return array(); |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - $merge_tags = array( |
|
| 130 | - '{name}' => sanitize_text_field( $invoice->get_user_full_name() ), |
|
| 131 | - '{full_name}' => sanitize_text_field( $invoice->get_user_full_name() ), |
|
| 132 | - '{first_name}' => sanitize_text_field( $invoice->get_first_name() ), |
|
| 133 | - '{last_name}' => sanitize_text_field( $invoice->get_last_name() ), |
|
| 134 | - '{email}' => sanitize_email( $invoice->get_email() ), |
|
| 135 | - '{invoice_number}' => sanitize_text_field( $invoice->get_number() ), |
|
| 136 | - '{invoice_currency}' => sanitize_text_field( $invoice->get_currency() ), |
|
| 137 | - '{invoice_total}' => sanitize_text_field( wpinv_price( $invoice->get_total(), $invoice->get_currency() ) ), |
|
| 138 | - '{invoice_link}' => esc_url( $invoice->get_view_url() ), |
|
| 139 | - '{invoice_pay_link}' => esc_url( $invoice->get_checkout_payment_url() ), |
|
| 140 | - '{invoice_receipt_link}'=> esc_url( $invoice->get_receipt_url() ), |
|
| 141 | - '{invoice_date}' => getpaid_format_date_value( $invoice->get_date_created() ), |
|
| 142 | - '{invoice_due_date}' => getpaid_format_date_value( $invoice->get_due_date(), __( 'on receipt', 'invoicing' ) ), |
|
| 143 | - '{invoice_quote}' => sanitize_text_field( strtolower( $invoice->get_label() ) ), |
|
| 144 | - '{invoice_label}' => sanitize_text_field( ucfirst( $invoice->get_label() ) ), |
|
| 145 | - '{invoice_description}' => wp_kses_post( $invoice->get_description() ), |
|
| 146 | - '{subscription_name}' => wp_kses_post( $invoice->get_subscription_name() ), |
|
| 147 | - '{is_was}' => strtotime( $invoice->get_due_date() ) < current_time( 'timestamp' ) ? __( 'was', 'invoicing' ) : __( 'is', 'invoicing' ), |
|
| 148 | - ); |
|
| 149 | - |
|
| 150 | - return apply_filters( 'getpaid_invoice_email_merge_tags', $merge_tags, $invoice ); |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * Helper function to send an email. |
|
| 155 | - * |
|
| 156 | - * @param WPInv_Invoice $invoice |
|
| 157 | - * @param GetPaid_Notification_Email $email |
|
| 158 | - * @param string $type |
|
| 159 | - * @param string|array $recipients |
|
| 160 | - * @param array $extra_args Extra template args. |
|
| 161 | - */ |
|
| 162 | - public function send_email( $invoice, $email, $type, $recipients, $extra_args = array() ) { |
|
| 163 | - |
|
| 164 | - do_action( 'getpaid_before_send_invoice_notification', $type, $invoice, $email ); |
|
| 165 | - |
|
| 166 | - if ( apply_filters( 'getpaid_skip_invoice_email', false, $type, $invoice ) ) { |
|
| 167 | - return; |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - $mailer = new GetPaid_Notification_Email_Sender(); |
|
| 171 | - $merge_tags = $email->get_merge_tags(); |
|
| 172 | - |
|
| 173 | - $result = $mailer->send( |
|
| 174 | - apply_filters( 'getpaid_invoice_email_recipients', wpinv_parse_list( $recipients ), $email ), |
|
| 175 | - $email->add_merge_tags( $email->get_subject(), $merge_tags ), |
|
| 176 | - $email->get_content( $merge_tags, $extra_args ), |
|
| 177 | - $email->get_attachments() |
|
| 178 | - ); |
|
| 179 | - |
|
| 180 | - // Maybe send a copy to the admin. |
|
| 181 | - if ( $email->include_admin_bcc() ) { |
|
| 182 | - $mailer->send( |
|
| 183 | - wpinv_get_admin_email(), |
|
| 184 | - $email->add_merge_tags( $email->get_subject() . __( ' - ADMIN BCC COPY', 'invoicing' ), $merge_tags ), |
|
| 185 | - $email->get_content( $merge_tags ), |
|
| 186 | - $email->get_attachments() |
|
| 187 | - ); |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - if ( $result ) { |
|
| 191 | - $invoice->add_note( sprintf( __( 'Successfully sent %s notification email.', 'invoicing' ), sanitize_key( $type ) ), false, false, true ); |
|
| 192 | - } else { |
|
| 193 | - $invoice->add_note( sprintf( __( 'Failed sending %s notification email.', 'invoicing' ), sanitize_key( $type ) ), false, false, true ); |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - do_action( 'getpaid_after_send_invoice_notification', $type, $invoice, $email ); |
|
| 197 | - |
|
| 198 | - return $result; |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - /** |
|
| 202 | - * Also send emails to any cc users. |
|
| 203 | - * |
|
| 204 | - * @param array $recipients |
|
| 205 | - * @param GetPaid_Notification_Email $email |
|
| 206 | - */ |
|
| 207 | - public function filter_email_recipients( $recipients, $email ) { |
|
| 208 | - |
|
| 209 | - if ( ! $email->is_admin_email() ) { |
|
| 210 | - $cc = $email->object->get_email_cc(); |
|
| 211 | - |
|
| 212 | - if ( ! empty( $cc ) ) { |
|
| 213 | - $cc = array_map( 'sanitize_email', wpinv_parse_list( $cc ) ); |
|
| 214 | - $recipients = array_filter( array_unique( array_merge( $recipients, $cc ) ) ); |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - return $recipients; |
|
| 220 | - |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - /** |
|
| 224 | - * Sends a new invoice notification. |
|
| 225 | - * |
|
| 226 | - * @param WPInv_Invoice $invoice |
|
| 227 | - */ |
|
| 228 | - public function new_invoice( $invoice ) { |
|
| 229 | - |
|
| 230 | - // Only send this email for invoices created via the admin page. |
|
| 231 | - if ( ! $invoice->is_type( 'invoice' ) || $this->is_payment_form_invoice( $invoice->get_id() ) ) { |
|
| 232 | - return; |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 236 | - $recipient = wpinv_get_admin_email(); |
|
| 237 | - |
|
| 238 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 239 | - |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - /** |
|
| 243 | - * Sends a cancelled invoice notification. |
|
| 244 | - * |
|
| 245 | - * @param WPInv_Invoice $invoice |
|
| 246 | - */ |
|
| 247 | - public function cancelled_invoice( $invoice ) { |
|
| 15 | + /** |
|
| 16 | + * The array of invoice email actions. |
|
| 17 | + * |
|
| 18 | + * @param array |
|
| 19 | + */ |
|
| 20 | + public $invoice_actions; |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * Class constructor |
|
| 24 | + * |
|
| 25 | + */ |
|
| 26 | + public function __construct() { |
|
| 27 | + |
|
| 28 | + $this->invoice_actions = apply_filters( |
|
| 29 | + 'getpaid_notification_email_invoice_triggers', |
|
| 30 | + array( |
|
| 31 | + 'getpaid_new_invoice' => array( 'new_invoice', 'user_invoice' ), |
|
| 32 | + 'getpaid_invoice_status_wpi-cancelled' => 'cancelled_invoice', |
|
| 33 | + 'getpaid_invoice_status_wpi-failed' => 'failed_invoice', |
|
| 34 | + 'getpaid_invoice_status_wpi-onhold' => 'onhold_invoice', |
|
| 35 | + 'getpaid_invoice_status_wpi-processing' => 'processing_invoice', |
|
| 36 | + 'getpaid_invoice_status_publish' => 'completed_invoice', |
|
| 37 | + 'getpaid_invoice_status_wpi-renewal' => 'completed_invoice', |
|
| 38 | + 'getpaid_invoice_status_wpi-refunded' => 'refunded_invoice', |
|
| 39 | + 'getpaid_new_customer_note' => 'user_note', |
|
| 40 | + 'getpaid_daily_maintenance' => 'overdue', |
|
| 41 | + ) |
|
| 42 | + ); |
|
| 43 | + |
|
| 44 | + $this->init_hooks(); |
|
| 45 | + |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * Registers email hooks. |
|
| 50 | + */ |
|
| 51 | + public function init_hooks() { |
|
| 52 | + |
|
| 53 | + add_filter( 'getpaid_get_email_merge_tags', array( $this, 'invoice_merge_tags' ), 10, 2 ); |
|
| 54 | + add_filter( 'getpaid_invoice_email_recipients', array( $this, 'filter_email_recipients' ), 10, 2 ); |
|
| 55 | + |
|
| 56 | + foreach ( $this->invoice_actions as $hook => $email_type ) { |
|
| 57 | + $this->init_email_type_hook( $hook, $email_type ); |
|
| 58 | + } |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * Registers an email hook for an invoice action. |
|
| 63 | + * |
|
| 64 | + * @param string $hook |
|
| 65 | + * @param string|array $email_type |
|
| 66 | + */ |
|
| 67 | + public function init_email_type_hook( $hook, $email_type ) { |
|
| 68 | + |
|
| 69 | + $email_type = wpinv_parse_list( $email_type ); |
|
| 70 | + |
|
| 71 | + foreach ( $email_type as $type ) { |
|
| 72 | + |
|
| 73 | + $email = new GetPaid_Notification_Email( $type ); |
|
| 74 | + |
|
| 75 | + // Abort if it is not active. |
|
| 76 | + if ( ! $email->is_active() ) { |
|
| 77 | + continue; |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + if ( method_exists( $this, $type ) ) { |
|
| 81 | + add_action( $hook, array( $this, $type ), 100, 2 ); |
|
| 82 | + continue; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + do_action( 'getpaid_invoice_init_email_type_hook', $type, $hook ); |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Filters invoice merge tags. |
|
| 92 | + * |
|
| 93 | + * @param array $merge_tags |
|
| 94 | + * @param mixed|WPInv_Invoice|WPInv_Subscription $object |
|
| 95 | + */ |
|
| 96 | + public function invoice_merge_tags( $merge_tags, $object ) { |
|
| 97 | + |
|
| 98 | + if ( is_a( $object, 'WPInv_Invoice' ) ) { |
|
| 99 | + return array_merge( |
|
| 100 | + $merge_tags, |
|
| 101 | + $this->get_invoice_merge_tags( $object ) |
|
| 102 | + ); |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + if ( is_a( $object, 'WPInv_Subscription' ) ) { |
|
| 106 | + return array_merge( |
|
| 107 | + $merge_tags, |
|
| 108 | + $this->get_invoice_merge_tags( $object->get_parent_payment() ) |
|
| 109 | + ); |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + return $merge_tags; |
|
| 113 | + |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * Generates invoice merge tags. |
|
| 118 | + * |
|
| 119 | + * @param WPInv_Invoice $invoice |
|
| 120 | + * @return array |
|
| 121 | + */ |
|
| 122 | + public function get_invoice_merge_tags( $invoice ) { |
|
| 123 | + |
|
| 124 | + // Abort if it does not exist. |
|
| 125 | + if ( ! $invoice->get_id() ) { |
|
| 126 | + return array(); |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + $merge_tags = array( |
|
| 130 | + '{name}' => sanitize_text_field( $invoice->get_user_full_name() ), |
|
| 131 | + '{full_name}' => sanitize_text_field( $invoice->get_user_full_name() ), |
|
| 132 | + '{first_name}' => sanitize_text_field( $invoice->get_first_name() ), |
|
| 133 | + '{last_name}' => sanitize_text_field( $invoice->get_last_name() ), |
|
| 134 | + '{email}' => sanitize_email( $invoice->get_email() ), |
|
| 135 | + '{invoice_number}' => sanitize_text_field( $invoice->get_number() ), |
|
| 136 | + '{invoice_currency}' => sanitize_text_field( $invoice->get_currency() ), |
|
| 137 | + '{invoice_total}' => sanitize_text_field( wpinv_price( $invoice->get_total(), $invoice->get_currency() ) ), |
|
| 138 | + '{invoice_link}' => esc_url( $invoice->get_view_url() ), |
|
| 139 | + '{invoice_pay_link}' => esc_url( $invoice->get_checkout_payment_url() ), |
|
| 140 | + '{invoice_receipt_link}'=> esc_url( $invoice->get_receipt_url() ), |
|
| 141 | + '{invoice_date}' => getpaid_format_date_value( $invoice->get_date_created() ), |
|
| 142 | + '{invoice_due_date}' => getpaid_format_date_value( $invoice->get_due_date(), __( 'on receipt', 'invoicing' ) ), |
|
| 143 | + '{invoice_quote}' => sanitize_text_field( strtolower( $invoice->get_label() ) ), |
|
| 144 | + '{invoice_label}' => sanitize_text_field( ucfirst( $invoice->get_label() ) ), |
|
| 145 | + '{invoice_description}' => wp_kses_post( $invoice->get_description() ), |
|
| 146 | + '{subscription_name}' => wp_kses_post( $invoice->get_subscription_name() ), |
|
| 147 | + '{is_was}' => strtotime( $invoice->get_due_date() ) < current_time( 'timestamp' ) ? __( 'was', 'invoicing' ) : __( 'is', 'invoicing' ), |
|
| 148 | + ); |
|
| 149 | + |
|
| 150 | + return apply_filters( 'getpaid_invoice_email_merge_tags', $merge_tags, $invoice ); |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * Helper function to send an email. |
|
| 155 | + * |
|
| 156 | + * @param WPInv_Invoice $invoice |
|
| 157 | + * @param GetPaid_Notification_Email $email |
|
| 158 | + * @param string $type |
|
| 159 | + * @param string|array $recipients |
|
| 160 | + * @param array $extra_args Extra template args. |
|
| 161 | + */ |
|
| 162 | + public function send_email( $invoice, $email, $type, $recipients, $extra_args = array() ) { |
|
| 163 | + |
|
| 164 | + do_action( 'getpaid_before_send_invoice_notification', $type, $invoice, $email ); |
|
| 165 | + |
|
| 166 | + if ( apply_filters( 'getpaid_skip_invoice_email', false, $type, $invoice ) ) { |
|
| 167 | + return; |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + $mailer = new GetPaid_Notification_Email_Sender(); |
|
| 171 | + $merge_tags = $email->get_merge_tags(); |
|
| 172 | + |
|
| 173 | + $result = $mailer->send( |
|
| 174 | + apply_filters( 'getpaid_invoice_email_recipients', wpinv_parse_list( $recipients ), $email ), |
|
| 175 | + $email->add_merge_tags( $email->get_subject(), $merge_tags ), |
|
| 176 | + $email->get_content( $merge_tags, $extra_args ), |
|
| 177 | + $email->get_attachments() |
|
| 178 | + ); |
|
| 179 | + |
|
| 180 | + // Maybe send a copy to the admin. |
|
| 181 | + if ( $email->include_admin_bcc() ) { |
|
| 182 | + $mailer->send( |
|
| 183 | + wpinv_get_admin_email(), |
|
| 184 | + $email->add_merge_tags( $email->get_subject() . __( ' - ADMIN BCC COPY', 'invoicing' ), $merge_tags ), |
|
| 185 | + $email->get_content( $merge_tags ), |
|
| 186 | + $email->get_attachments() |
|
| 187 | + ); |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + if ( $result ) { |
|
| 191 | + $invoice->add_note( sprintf( __( 'Successfully sent %s notification email.', 'invoicing' ), sanitize_key( $type ) ), false, false, true ); |
|
| 192 | + } else { |
|
| 193 | + $invoice->add_note( sprintf( __( 'Failed sending %s notification email.', 'invoicing' ), sanitize_key( $type ) ), false, false, true ); |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + do_action( 'getpaid_after_send_invoice_notification', $type, $invoice, $email ); |
|
| 197 | + |
|
| 198 | + return $result; |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + /** |
|
| 202 | + * Also send emails to any cc users. |
|
| 203 | + * |
|
| 204 | + * @param array $recipients |
|
| 205 | + * @param GetPaid_Notification_Email $email |
|
| 206 | + */ |
|
| 207 | + public function filter_email_recipients( $recipients, $email ) { |
|
| 208 | + |
|
| 209 | + if ( ! $email->is_admin_email() ) { |
|
| 210 | + $cc = $email->object->get_email_cc(); |
|
| 211 | + |
|
| 212 | + if ( ! empty( $cc ) ) { |
|
| 213 | + $cc = array_map( 'sanitize_email', wpinv_parse_list( $cc ) ); |
|
| 214 | + $recipients = array_filter( array_unique( array_merge( $recipients, $cc ) ) ); |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + return $recipients; |
|
| 220 | + |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + /** |
|
| 224 | + * Sends a new invoice notification. |
|
| 225 | + * |
|
| 226 | + * @param WPInv_Invoice $invoice |
|
| 227 | + */ |
|
| 228 | + public function new_invoice( $invoice ) { |
|
| 229 | + |
|
| 230 | + // Only send this email for invoices created via the admin page. |
|
| 231 | + if ( ! $invoice->is_type( 'invoice' ) || $this->is_payment_form_invoice( $invoice->get_id() ) ) { |
|
| 232 | + return; |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 236 | + $recipient = wpinv_get_admin_email(); |
|
| 237 | + |
|
| 238 | + return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 239 | + |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + /** |
|
| 243 | + * Sends a cancelled invoice notification. |
|
| 244 | + * |
|
| 245 | + * @param WPInv_Invoice $invoice |
|
| 246 | + */ |
|
| 247 | + public function cancelled_invoice( $invoice ) { |
|
| 248 | 248 | |
| 249 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 250 | - $recipient = wpinv_get_admin_email(); |
|
| 249 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 250 | + $recipient = wpinv_get_admin_email(); |
|
| 251 | 251 | |
| 252 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 252 | + return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 253 | 253 | |
| 254 | - } |
|
| 254 | + } |
|
| 255 | 255 | |
| 256 | - /** |
|
| 257 | - * Sends a failed invoice notification. |
|
| 258 | - * |
|
| 259 | - * @param WPInv_Invoice $invoice |
|
| 260 | - */ |
|
| 261 | - public function failed_invoice( $invoice ) { |
|
| 256 | + /** |
|
| 257 | + * Sends a failed invoice notification. |
|
| 258 | + * |
|
| 259 | + * @param WPInv_Invoice $invoice |
|
| 260 | + */ |
|
| 261 | + public function failed_invoice( $invoice ) { |
|
| 262 | 262 | |
| 263 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 264 | - $recipient = wpinv_get_admin_email(); |
|
| 263 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 264 | + $recipient = wpinv_get_admin_email(); |
|
| 265 | 265 | |
| 266 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 266 | + return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 267 | 267 | |
| 268 | - } |
|
| 268 | + } |
|
| 269 | 269 | |
| 270 | - /** |
|
| 271 | - * Sends a notification whenever an invoice is put on hold. |
|
| 272 | - * |
|
| 273 | - * @param WPInv_Invoice $invoice |
|
| 274 | - */ |
|
| 275 | - public function onhold_invoice( $invoice ) { |
|
| 270 | + /** |
|
| 271 | + * Sends a notification whenever an invoice is put on hold. |
|
| 272 | + * |
|
| 273 | + * @param WPInv_Invoice $invoice |
|
| 274 | + */ |
|
| 275 | + public function onhold_invoice( $invoice ) { |
|
| 276 | 276 | |
| 277 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 278 | - $recipient = $invoice->get_email(); |
|
| 277 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 278 | + $recipient = $invoice->get_email(); |
|
| 279 | 279 | |
| 280 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 280 | + return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 281 | 281 | |
| 282 | - } |
|
| 282 | + } |
|
| 283 | 283 | |
| 284 | - /** |
|
| 285 | - * Sends a notification whenever an invoice is marked as processing payment. |
|
| 286 | - * |
|
| 287 | - * @param WPInv_Invoice $invoice |
|
| 288 | - */ |
|
| 289 | - public function processing_invoice( $invoice ) { |
|
| 284 | + /** |
|
| 285 | + * Sends a notification whenever an invoice is marked as processing payment. |
|
| 286 | + * |
|
| 287 | + * @param WPInv_Invoice $invoice |
|
| 288 | + */ |
|
| 289 | + public function processing_invoice( $invoice ) { |
|
| 290 | 290 | |
| 291 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 292 | - $recipient = $invoice->get_email(); |
|
| 291 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 292 | + $recipient = $invoice->get_email(); |
|
| 293 | 293 | |
| 294 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 294 | + return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 295 | 295 | |
| 296 | - } |
|
| 296 | + } |
|
| 297 | 297 | |
| 298 | - /** |
|
| 299 | - * Sends a notification whenever an invoice is paid. |
|
| 300 | - * |
|
| 301 | - * @param WPInv_Invoice $invoice |
|
| 302 | - */ |
|
| 303 | - public function completed_invoice( $invoice ) { |
|
| 298 | + /** |
|
| 299 | + * Sends a notification whenever an invoice is paid. |
|
| 300 | + * |
|
| 301 | + * @param WPInv_Invoice $invoice |
|
| 302 | + */ |
|
| 303 | + public function completed_invoice( $invoice ) { |
|
| 304 | 304 | |
| 305 | - // (Maybe) abort if it is a renewal invoice. |
|
| 306 | - if ( $invoice->is_renewal() && ! wpinv_get_option( 'email_completed_invoice_renewal_active', false ) ) { |
|
| 307 | - return; |
|
| 308 | - } |
|
| 305 | + // (Maybe) abort if it is a renewal invoice. |
|
| 306 | + if ( $invoice->is_renewal() && ! wpinv_get_option( 'email_completed_invoice_renewal_active', false ) ) { |
|
| 307 | + return; |
|
| 308 | + } |
|
| 309 | 309 | |
| 310 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 311 | - $recipient = $invoice->get_email(); |
|
| 310 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 311 | + $recipient = $invoice->get_email(); |
|
| 312 | 312 | |
| 313 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 313 | + return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 314 | 314 | |
| 315 | - } |
|
| 315 | + } |
|
| 316 | 316 | |
| 317 | - /** |
|
| 318 | - * Sends a notification whenever an invoice is refunded. |
|
| 319 | - * |
|
| 320 | - * @param WPInv_Invoice $invoice |
|
| 321 | - */ |
|
| 322 | - public function refunded_invoice( $invoice ) { |
|
| 317 | + /** |
|
| 318 | + * Sends a notification whenever an invoice is refunded. |
|
| 319 | + * |
|
| 320 | + * @param WPInv_Invoice $invoice |
|
| 321 | + */ |
|
| 322 | + public function refunded_invoice( $invoice ) { |
|
| 323 | 323 | |
| 324 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 325 | - $recipient = $invoice->get_email(); |
|
| 324 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 325 | + $recipient = $invoice->get_email(); |
|
| 326 | 326 | |
| 327 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 327 | + return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 328 | 328 | |
| 329 | - } |
|
| 329 | + } |
|
| 330 | 330 | |
| 331 | - /** |
|
| 332 | - * Notifies a user about new invoices |
|
| 333 | - * |
|
| 334 | - * @param WPInv_Invoice $invoice |
|
| 335 | - * @param bool $force |
|
| 336 | - */ |
|
| 337 | - public function user_invoice( $invoice, $force = false ) { |
|
| 331 | + /** |
|
| 332 | + * Notifies a user about new invoices |
|
| 333 | + * |
|
| 334 | + * @param WPInv_Invoice $invoice |
|
| 335 | + * @param bool $force |
|
| 336 | + */ |
|
| 337 | + public function user_invoice( $invoice, $force = false ) { |
|
| 338 | 338 | |
| 339 | - if ( ! empty( $GLOBALS['wpinv_skip_invoice_notification'] ) ) { |
|
| 340 | - return; |
|
| 341 | - } |
|
| 339 | + if ( ! empty( $GLOBALS['wpinv_skip_invoice_notification'] ) ) { |
|
| 340 | + return; |
|
| 341 | + } |
|
| 342 | 342 | |
| 343 | - // Only send this email for invoices created via the admin page. |
|
| 344 | - if ( ! $invoice->is_type( 'invoice' ) || ( empty( $force ) && $this->is_payment_form_invoice( $invoice->get_id() ) ) ) { |
|
| 345 | - return; |
|
| 346 | - } |
|
| 343 | + // Only send this email for invoices created via the admin page. |
|
| 344 | + if ( ! $invoice->is_type( 'invoice' ) || ( empty( $force ) && $this->is_payment_form_invoice( $invoice->get_id() ) ) ) { |
|
| 345 | + return; |
|
| 346 | + } |
|
| 347 | 347 | |
| 348 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 349 | - $recipient = $invoice->get_email(); |
|
| 348 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 349 | + $recipient = $invoice->get_email(); |
|
| 350 | 350 | |
| 351 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 351 | + return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 352 | 352 | |
| 353 | - } |
|
| 353 | + } |
|
| 354 | 354 | |
| 355 | - /** |
|
| 356 | - * Checks if an invoice is a payment form invoice. |
|
| 357 | - * |
|
| 358 | - * @param int $invoice |
|
| 359 | - * @return bool |
|
| 360 | - */ |
|
| 361 | - public function is_payment_form_invoice( $invoice ) { |
|
| 362 | - return empty( $_GET['getpaid-admin-action'] ) && ( 'payment_form' == get_post_meta( $invoice, 'wpinv_created_via', true ) || 'geodirectory' == get_post_meta( $invoice, 'wpinv_created_via', true ) ); |
|
| 363 | - } |
|
| 355 | + /** |
|
| 356 | + * Checks if an invoice is a payment form invoice. |
|
| 357 | + * |
|
| 358 | + * @param int $invoice |
|
| 359 | + * @return bool |
|
| 360 | + */ |
|
| 361 | + public function is_payment_form_invoice( $invoice ) { |
|
| 362 | + return empty( $_GET['getpaid-admin-action'] ) && ( 'payment_form' == get_post_meta( $invoice, 'wpinv_created_via', true ) || 'geodirectory' == get_post_meta( $invoice, 'wpinv_created_via', true ) ); |
|
| 363 | + } |
|
| 364 | 364 | |
| 365 | - /** |
|
| 366 | - * Notifies admin about new invoice notes |
|
| 367 | - * |
|
| 368 | - * @param WPInv_Invoice $invoice |
|
| 369 | - * @param string $note |
|
| 370 | - */ |
|
| 371 | - public function user_note( $invoice, $note ) { |
|
| 372 | - |
|
| 373 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 374 | - $recipient = $invoice->get_email(); |
|
| 365 | + /** |
|
| 366 | + * Notifies admin about new invoice notes |
|
| 367 | + * |
|
| 368 | + * @param WPInv_Invoice $invoice |
|
| 369 | + * @param string $note |
|
| 370 | + */ |
|
| 371 | + public function user_note( $invoice, $note ) { |
|
| 372 | + |
|
| 373 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 374 | + $recipient = $invoice->get_email(); |
|
| 375 | 375 | |
| 376 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient, array( 'customer_note' => $note ) ); |
|
| 377 | - |
|
| 378 | - } |
|
| 376 | + return $this->send_email( $invoice, $email, __FUNCTION__, $recipient, array( 'customer_note' => $note ) ); |
|
| 377 | + |
|
| 378 | + } |
|
| 379 | 379 | |
| 380 | - /** |
|
| 381 | - * (Force) Sends overdue notices. |
|
| 382 | - * |
|
| 383 | - * @param WPInv_Invoice $invoice |
|
| 384 | - */ |
|
| 385 | - public function force_send_overdue_notice( $invoice ) { |
|
| 386 | - $email = new GetPaid_Notification_Email( 'overdue', $invoice ); |
|
| 387 | - return $this->send_email( $invoice, $email, 'overdue', $invoice->get_email() ); |
|
| 388 | - } |
|
| 389 | - |
|
| 390 | - /** |
|
| 391 | - * Sends overdue notices. |
|
| 392 | - * |
|
| 393 | - * @TODO: Create an invoices query class. |
|
| 394 | - */ |
|
| 395 | - public function overdue() { |
|
| 396 | - global $wpdb; |
|
| 397 | - |
|
| 398 | - $email = new GetPaid_Notification_Email( __FUNCTION__ ); |
|
| 399 | - |
|
| 400 | - // Fetch reminder days. |
|
| 401 | - $reminder_days = array_unique( wp_parse_id_list( $email->get_option( 'days' ) ) ); |
|
| 402 | - |
|
| 403 | - // Abort if non is set. |
|
| 404 | - if ( empty( $reminder_days ) ) { |
|
| 405 | - return; |
|
| 406 | - } |
|
| 407 | - |
|
| 408 | - // Retrieve date query. |
|
| 409 | - $date_query = $this->get_date_query( $reminder_days ); |
|
| 410 | - |
|
| 411 | - // Invoices table. |
|
| 412 | - $table = $wpdb->prefix . 'getpaid_invoices'; |
|
| 413 | - |
|
| 414 | - // Fetch invoices. |
|
| 415 | - $invoices = $wpdb->get_col( |
|
| 416 | - "SELECT posts.ID FROM $wpdb->posts as posts |
|
| 380 | + /** |
|
| 381 | + * (Force) Sends overdue notices. |
|
| 382 | + * |
|
| 383 | + * @param WPInv_Invoice $invoice |
|
| 384 | + */ |
|
| 385 | + public function force_send_overdue_notice( $invoice ) { |
|
| 386 | + $email = new GetPaid_Notification_Email( 'overdue', $invoice ); |
|
| 387 | + return $this->send_email( $invoice, $email, 'overdue', $invoice->get_email() ); |
|
| 388 | + } |
|
| 389 | + |
|
| 390 | + /** |
|
| 391 | + * Sends overdue notices. |
|
| 392 | + * |
|
| 393 | + * @TODO: Create an invoices query class. |
|
| 394 | + */ |
|
| 395 | + public function overdue() { |
|
| 396 | + global $wpdb; |
|
| 397 | + |
|
| 398 | + $email = new GetPaid_Notification_Email( __FUNCTION__ ); |
|
| 399 | + |
|
| 400 | + // Fetch reminder days. |
|
| 401 | + $reminder_days = array_unique( wp_parse_id_list( $email->get_option( 'days' ) ) ); |
|
| 402 | + |
|
| 403 | + // Abort if non is set. |
|
| 404 | + if ( empty( $reminder_days ) ) { |
|
| 405 | + return; |
|
| 406 | + } |
|
| 407 | + |
|
| 408 | + // Retrieve date query. |
|
| 409 | + $date_query = $this->get_date_query( $reminder_days ); |
|
| 410 | + |
|
| 411 | + // Invoices table. |
|
| 412 | + $table = $wpdb->prefix . 'getpaid_invoices'; |
|
| 413 | + |
|
| 414 | + // Fetch invoices. |
|
| 415 | + $invoices = $wpdb->get_col( |
|
| 416 | + "SELECT posts.ID FROM $wpdb->posts as posts |
|
| 417 | 417 | LEFT JOIN $table as invoices ON invoices.post_id = posts.ID |
| 418 | 418 | WHERE posts.post_type = 'wpi_invoice' AND posts.post_status = 'wpi-pending' $date_query"); |
| 419 | 419 | |
| 420 | - foreach ( $invoices as $invoice ) { |
|
| 420 | + foreach ( $invoices as $invoice ) { |
|
| 421 | 421 | |
| 422 | - // Only send this email for invoices created via the admin page. |
|
| 423 | - if ( ! $this->is_payment_form_invoice( $invoice ) ) { |
|
| 424 | - $invoice = new WPInv_Invoice( $invoice ); |
|
| 425 | - $email->object = $invoice; |
|
| 422 | + // Only send this email for invoices created via the admin page. |
|
| 423 | + if ( ! $this->is_payment_form_invoice( $invoice ) ) { |
|
| 424 | + $invoice = new WPInv_Invoice( $invoice ); |
|
| 425 | + $email->object = $invoice; |
|
| 426 | 426 | |
| 427 | - if ( $invoice->needs_payment() ) { |
|
| 428 | - $this->send_email( $invoice, $email, __FUNCTION__, $invoice->get_email() ); |
|
| 429 | - } |
|
| 427 | + if ( $invoice->needs_payment() ) { |
|
| 428 | + $this->send_email( $invoice, $email, __FUNCTION__, $invoice->get_email() ); |
|
| 429 | + } |
|
| 430 | 430 | |
| 431 | - } |
|
| 431 | + } |
|
| 432 | 432 | |
| 433 | - } |
|
| 433 | + } |
|
| 434 | 434 | |
| 435 | - } |
|
| 435 | + } |
|
| 436 | 436 | |
| 437 | - /** |
|
| 438 | - * Calculates the date query for an invoices query |
|
| 439 | - * |
|
| 440 | - * @param array $reminder_days |
|
| 441 | - * @return string |
|
| 442 | - */ |
|
| 443 | - public function get_date_query( $reminder_days ) { |
|
| 437 | + /** |
|
| 438 | + * Calculates the date query for an invoices query |
|
| 439 | + * |
|
| 440 | + * @param array $reminder_days |
|
| 441 | + * @return string |
|
| 442 | + */ |
|
| 443 | + public function get_date_query( $reminder_days ) { |
|
| 444 | 444 | |
| 445 | - $date_query = array( |
|
| 446 | - 'relation' => 'OR' |
|
| 447 | - ); |
|
| 445 | + $date_query = array( |
|
| 446 | + 'relation' => 'OR' |
|
| 447 | + ); |
|
| 448 | 448 | |
| 449 | - foreach ( $reminder_days as $days ) { |
|
| 450 | - $date = date_parse( date( 'Y-m-d', strtotime( "-$days days", current_time( 'timestamp' ) ) ) ); |
|
| 449 | + foreach ( $reminder_days as $days ) { |
|
| 450 | + $date = date_parse( date( 'Y-m-d', strtotime( "-$days days", current_time( 'timestamp' ) ) ) ); |
|
| 451 | 451 | |
| 452 | - $date_query[] = array( |
|
| 453 | - 'year' => $date['year'], |
|
| 454 | - 'month' => $date['month'], |
|
| 455 | - 'day' => $date['day'], |
|
| 456 | - ); |
|
| 452 | + $date_query[] = array( |
|
| 453 | + 'year' => $date['year'], |
|
| 454 | + 'month' => $date['month'], |
|
| 455 | + 'day' => $date['day'], |
|
| 456 | + ); |
|
| 457 | 457 | |
| 458 | - } |
|
| 458 | + } |
|
| 459 | 459 | |
| 460 | - $date_query = new WP_Date_Query( $date_query, 'invoices.due_date' ); |
|
| 460 | + $date_query = new WP_Date_Query( $date_query, 'invoices.due_date' ); |
|
| 461 | 461 | |
| 462 | - return $date_query->get_sql(); |
|
| 462 | + return $date_query->get_sql(); |
|
| 463 | 463 | |
| 464 | - } |
|
| 464 | + } |
|
| 465 | 465 | |
| 466 | 466 | } |
@@ -4,7 +4,7 @@ discard block |
||
| 4 | 4 | * |
| 5 | 5 | */ |
| 6 | 6 | |
| 7 | -defined( 'ABSPATH' ) || exit; |
|
| 7 | +defined('ABSPATH') || exit; |
|
| 8 | 8 | |
| 9 | 9 | /** |
| 10 | 10 | * This class handles invoice notificaiton emails. |
@@ -28,7 +28,7 @@ discard block |
||
| 28 | 28 | $this->invoice_actions = apply_filters( |
| 29 | 29 | 'getpaid_notification_email_invoice_triggers', |
| 30 | 30 | array( |
| 31 | - 'getpaid_new_invoice' => array( 'new_invoice', 'user_invoice' ), |
|
| 31 | + 'getpaid_new_invoice' => array('new_invoice', 'user_invoice'), |
|
| 32 | 32 | 'getpaid_invoice_status_wpi-cancelled' => 'cancelled_invoice', |
| 33 | 33 | 'getpaid_invoice_status_wpi-failed' => 'failed_invoice', |
| 34 | 34 | 'getpaid_invoice_status_wpi-onhold' => 'onhold_invoice', |
@@ -50,11 +50,11 @@ discard block |
||
| 50 | 50 | */ |
| 51 | 51 | public function init_hooks() { |
| 52 | 52 | |
| 53 | - add_filter( 'getpaid_get_email_merge_tags', array( $this, 'invoice_merge_tags' ), 10, 2 ); |
|
| 54 | - add_filter( 'getpaid_invoice_email_recipients', array( $this, 'filter_email_recipients' ), 10, 2 ); |
|
| 53 | + add_filter('getpaid_get_email_merge_tags', array($this, 'invoice_merge_tags'), 10, 2); |
|
| 54 | + add_filter('getpaid_invoice_email_recipients', array($this, 'filter_email_recipients'), 10, 2); |
|
| 55 | 55 | |
| 56 | - foreach ( $this->invoice_actions as $hook => $email_type ) { |
|
| 57 | - $this->init_email_type_hook( $hook, $email_type ); |
|
| 56 | + foreach ($this->invoice_actions as $hook => $email_type) { |
|
| 57 | + $this->init_email_type_hook($hook, $email_type); |
|
| 58 | 58 | } |
| 59 | 59 | } |
| 60 | 60 | |
@@ -64,25 +64,25 @@ discard block |
||
| 64 | 64 | * @param string $hook |
| 65 | 65 | * @param string|array $email_type |
| 66 | 66 | */ |
| 67 | - public function init_email_type_hook( $hook, $email_type ) { |
|
| 67 | + public function init_email_type_hook($hook, $email_type) { |
|
| 68 | 68 | |
| 69 | - $email_type = wpinv_parse_list( $email_type ); |
|
| 69 | + $email_type = wpinv_parse_list($email_type); |
|
| 70 | 70 | |
| 71 | - foreach ( $email_type as $type ) { |
|
| 71 | + foreach ($email_type as $type) { |
|
| 72 | 72 | |
| 73 | - $email = new GetPaid_Notification_Email( $type ); |
|
| 73 | + $email = new GetPaid_Notification_Email($type); |
|
| 74 | 74 | |
| 75 | 75 | // Abort if it is not active. |
| 76 | - if ( ! $email->is_active() ) { |
|
| 76 | + if (!$email->is_active()) { |
|
| 77 | 77 | continue; |
| 78 | 78 | } |
| 79 | 79 | |
| 80 | - if ( method_exists( $this, $type ) ) { |
|
| 81 | - add_action( $hook, array( $this, $type ), 100, 2 ); |
|
| 80 | + if (method_exists($this, $type)) { |
|
| 81 | + add_action($hook, array($this, $type), 100, 2); |
|
| 82 | 82 | continue; |
| 83 | 83 | } |
| 84 | 84 | |
| 85 | - do_action( 'getpaid_invoice_init_email_type_hook', $type, $hook ); |
|
| 85 | + do_action('getpaid_invoice_init_email_type_hook', $type, $hook); |
|
| 86 | 86 | } |
| 87 | 87 | |
| 88 | 88 | } |
@@ -93,19 +93,19 @@ discard block |
||
| 93 | 93 | * @param array $merge_tags |
| 94 | 94 | * @param mixed|WPInv_Invoice|WPInv_Subscription $object |
| 95 | 95 | */ |
| 96 | - public function invoice_merge_tags( $merge_tags, $object ) { |
|
| 96 | + public function invoice_merge_tags($merge_tags, $object) { |
|
| 97 | 97 | |
| 98 | - if ( is_a( $object, 'WPInv_Invoice' ) ) { |
|
| 98 | + if (is_a($object, 'WPInv_Invoice')) { |
|
| 99 | 99 | return array_merge( |
| 100 | 100 | $merge_tags, |
| 101 | - $this->get_invoice_merge_tags( $object ) |
|
| 101 | + $this->get_invoice_merge_tags($object) |
|
| 102 | 102 | ); |
| 103 | 103 | } |
| 104 | 104 | |
| 105 | - if ( is_a( $object, 'WPInv_Subscription' ) ) { |
|
| 105 | + if (is_a($object, 'WPInv_Subscription')) { |
|
| 106 | 106 | return array_merge( |
| 107 | 107 | $merge_tags, |
| 108 | - $this->get_invoice_merge_tags( $object->get_parent_payment() ) |
|
| 108 | + $this->get_invoice_merge_tags($object->get_parent_payment()) |
|
| 109 | 109 | ); |
| 110 | 110 | } |
| 111 | 111 | |
@@ -119,35 +119,35 @@ discard block |
||
| 119 | 119 | * @param WPInv_Invoice $invoice |
| 120 | 120 | * @return array |
| 121 | 121 | */ |
| 122 | - public function get_invoice_merge_tags( $invoice ) { |
|
| 122 | + public function get_invoice_merge_tags($invoice) { |
|
| 123 | 123 | |
| 124 | 124 | // Abort if it does not exist. |
| 125 | - if ( ! $invoice->get_id() ) { |
|
| 125 | + if (!$invoice->get_id()) { |
|
| 126 | 126 | return array(); |
| 127 | 127 | } |
| 128 | 128 | |
| 129 | 129 | $merge_tags = array( |
| 130 | - '{name}' => sanitize_text_field( $invoice->get_user_full_name() ), |
|
| 131 | - '{full_name}' => sanitize_text_field( $invoice->get_user_full_name() ), |
|
| 132 | - '{first_name}' => sanitize_text_field( $invoice->get_first_name() ), |
|
| 133 | - '{last_name}' => sanitize_text_field( $invoice->get_last_name() ), |
|
| 134 | - '{email}' => sanitize_email( $invoice->get_email() ), |
|
| 135 | - '{invoice_number}' => sanitize_text_field( $invoice->get_number() ), |
|
| 136 | - '{invoice_currency}' => sanitize_text_field( $invoice->get_currency() ), |
|
| 137 | - '{invoice_total}' => sanitize_text_field( wpinv_price( $invoice->get_total(), $invoice->get_currency() ) ), |
|
| 138 | - '{invoice_link}' => esc_url( $invoice->get_view_url() ), |
|
| 139 | - '{invoice_pay_link}' => esc_url( $invoice->get_checkout_payment_url() ), |
|
| 140 | - '{invoice_receipt_link}'=> esc_url( $invoice->get_receipt_url() ), |
|
| 141 | - '{invoice_date}' => getpaid_format_date_value( $invoice->get_date_created() ), |
|
| 142 | - '{invoice_due_date}' => getpaid_format_date_value( $invoice->get_due_date(), __( 'on receipt', 'invoicing' ) ), |
|
| 143 | - '{invoice_quote}' => sanitize_text_field( strtolower( $invoice->get_label() ) ), |
|
| 144 | - '{invoice_label}' => sanitize_text_field( ucfirst( $invoice->get_label() ) ), |
|
| 145 | - '{invoice_description}' => wp_kses_post( $invoice->get_description() ), |
|
| 146 | - '{subscription_name}' => wp_kses_post( $invoice->get_subscription_name() ), |
|
| 147 | - '{is_was}' => strtotime( $invoice->get_due_date() ) < current_time( 'timestamp' ) ? __( 'was', 'invoicing' ) : __( 'is', 'invoicing' ), |
|
| 130 | + '{name}' => sanitize_text_field($invoice->get_user_full_name()), |
|
| 131 | + '{full_name}' => sanitize_text_field($invoice->get_user_full_name()), |
|
| 132 | + '{first_name}' => sanitize_text_field($invoice->get_first_name()), |
|
| 133 | + '{last_name}' => sanitize_text_field($invoice->get_last_name()), |
|
| 134 | + '{email}' => sanitize_email($invoice->get_email()), |
|
| 135 | + '{invoice_number}' => sanitize_text_field($invoice->get_number()), |
|
| 136 | + '{invoice_currency}' => sanitize_text_field($invoice->get_currency()), |
|
| 137 | + '{invoice_total}' => sanitize_text_field(wpinv_price($invoice->get_total(), $invoice->get_currency())), |
|
| 138 | + '{invoice_link}' => esc_url($invoice->get_view_url()), |
|
| 139 | + '{invoice_pay_link}' => esc_url($invoice->get_checkout_payment_url()), |
|
| 140 | + '{invoice_receipt_link}'=> esc_url($invoice->get_receipt_url()), |
|
| 141 | + '{invoice_date}' => getpaid_format_date_value($invoice->get_date_created()), |
|
| 142 | + '{invoice_due_date}' => getpaid_format_date_value($invoice->get_due_date(), __('on receipt', 'invoicing')), |
|
| 143 | + '{invoice_quote}' => sanitize_text_field(strtolower($invoice->get_label())), |
|
| 144 | + '{invoice_label}' => sanitize_text_field(ucfirst($invoice->get_label())), |
|
| 145 | + '{invoice_description}' => wp_kses_post($invoice->get_description()), |
|
| 146 | + '{subscription_name}' => wp_kses_post($invoice->get_subscription_name()), |
|
| 147 | + '{is_was}' => strtotime($invoice->get_due_date()) < current_time('timestamp') ? __('was', 'invoicing') : __('is', 'invoicing'), |
|
| 148 | 148 | ); |
| 149 | 149 | |
| 150 | - return apply_filters( 'getpaid_invoice_email_merge_tags', $merge_tags, $invoice ); |
|
| 150 | + return apply_filters('getpaid_invoice_email_merge_tags', $merge_tags, $invoice); |
|
| 151 | 151 | } |
| 152 | 152 | |
| 153 | 153 | /** |
@@ -159,11 +159,11 @@ discard block |
||
| 159 | 159 | * @param string|array $recipients |
| 160 | 160 | * @param array $extra_args Extra template args. |
| 161 | 161 | */ |
| 162 | - public function send_email( $invoice, $email, $type, $recipients, $extra_args = array() ) { |
|
| 162 | + public function send_email($invoice, $email, $type, $recipients, $extra_args = array()) { |
|
| 163 | 163 | |
| 164 | - do_action( 'getpaid_before_send_invoice_notification', $type, $invoice, $email ); |
|
| 164 | + do_action('getpaid_before_send_invoice_notification', $type, $invoice, $email); |
|
| 165 | 165 | |
| 166 | - if ( apply_filters( 'getpaid_skip_invoice_email', false, $type, $invoice ) ) { |
|
| 166 | + if (apply_filters('getpaid_skip_invoice_email', false, $type, $invoice)) { |
|
| 167 | 167 | return; |
| 168 | 168 | } |
| 169 | 169 | |
@@ -171,29 +171,29 @@ discard block |
||
| 171 | 171 | $merge_tags = $email->get_merge_tags(); |
| 172 | 172 | |
| 173 | 173 | $result = $mailer->send( |
| 174 | - apply_filters( 'getpaid_invoice_email_recipients', wpinv_parse_list( $recipients ), $email ), |
|
| 175 | - $email->add_merge_tags( $email->get_subject(), $merge_tags ), |
|
| 176 | - $email->get_content( $merge_tags, $extra_args ), |
|
| 174 | + apply_filters('getpaid_invoice_email_recipients', wpinv_parse_list($recipients), $email), |
|
| 175 | + $email->add_merge_tags($email->get_subject(), $merge_tags), |
|
| 176 | + $email->get_content($merge_tags, $extra_args), |
|
| 177 | 177 | $email->get_attachments() |
| 178 | 178 | ); |
| 179 | 179 | |
| 180 | 180 | // Maybe send a copy to the admin. |
| 181 | - if ( $email->include_admin_bcc() ) { |
|
| 181 | + if ($email->include_admin_bcc()) { |
|
| 182 | 182 | $mailer->send( |
| 183 | 183 | wpinv_get_admin_email(), |
| 184 | - $email->add_merge_tags( $email->get_subject() . __( ' - ADMIN BCC COPY', 'invoicing' ), $merge_tags ), |
|
| 185 | - $email->get_content( $merge_tags ), |
|
| 184 | + $email->add_merge_tags($email->get_subject() . __(' - ADMIN BCC COPY', 'invoicing'), $merge_tags), |
|
| 185 | + $email->get_content($merge_tags), |
|
| 186 | 186 | $email->get_attachments() |
| 187 | 187 | ); |
| 188 | 188 | } |
| 189 | 189 | |
| 190 | - if ( $result ) { |
|
| 191 | - $invoice->add_note( sprintf( __( 'Successfully sent %s notification email.', 'invoicing' ), sanitize_key( $type ) ), false, false, true ); |
|
| 190 | + if ($result) { |
|
| 191 | + $invoice->add_note(sprintf(__('Successfully sent %s notification email.', 'invoicing'), sanitize_key($type)), false, false, true); |
|
| 192 | 192 | } else { |
| 193 | - $invoice->add_note( sprintf( __( 'Failed sending %s notification email.', 'invoicing' ), sanitize_key( $type ) ), false, false, true ); |
|
| 193 | + $invoice->add_note(sprintf(__('Failed sending %s notification email.', 'invoicing'), sanitize_key($type)), false, false, true); |
|
| 194 | 194 | } |
| 195 | 195 | |
| 196 | - do_action( 'getpaid_after_send_invoice_notification', $type, $invoice, $email ); |
|
| 196 | + do_action('getpaid_after_send_invoice_notification', $type, $invoice, $email); |
|
| 197 | 197 | |
| 198 | 198 | return $result; |
| 199 | 199 | } |
@@ -204,14 +204,14 @@ discard block |
||
| 204 | 204 | * @param array $recipients |
| 205 | 205 | * @param GetPaid_Notification_Email $email |
| 206 | 206 | */ |
| 207 | - public function filter_email_recipients( $recipients, $email ) { |
|
| 207 | + public function filter_email_recipients($recipients, $email) { |
|
| 208 | 208 | |
| 209 | - if ( ! $email->is_admin_email() ) { |
|
| 209 | + if (!$email->is_admin_email()) { |
|
| 210 | 210 | $cc = $email->object->get_email_cc(); |
| 211 | 211 | |
| 212 | - if ( ! empty( $cc ) ) { |
|
| 213 | - $cc = array_map( 'sanitize_email', wpinv_parse_list( $cc ) ); |
|
| 214 | - $recipients = array_filter( array_unique( array_merge( $recipients, $cc ) ) ); |
|
| 212 | + if (!empty($cc)) { |
|
| 213 | + $cc = array_map('sanitize_email', wpinv_parse_list($cc)); |
|
| 214 | + $recipients = array_filter(array_unique(array_merge($recipients, $cc))); |
|
| 215 | 215 | } |
| 216 | 216 | |
| 217 | 217 | } |
@@ -225,17 +225,17 @@ discard block |
||
| 225 | 225 | * |
| 226 | 226 | * @param WPInv_Invoice $invoice |
| 227 | 227 | */ |
| 228 | - public function new_invoice( $invoice ) { |
|
| 228 | + public function new_invoice($invoice) { |
|
| 229 | 229 | |
| 230 | 230 | // Only send this email for invoices created via the admin page. |
| 231 | - if ( ! $invoice->is_type( 'invoice' ) || $this->is_payment_form_invoice( $invoice->get_id() ) ) { |
|
| 231 | + if (!$invoice->is_type('invoice') || $this->is_payment_form_invoice($invoice->get_id())) { |
|
| 232 | 232 | return; |
| 233 | 233 | } |
| 234 | 234 | |
| 235 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 235 | + $email = new GetPaid_Notification_Email(__FUNCTION__, $invoice); |
|
| 236 | 236 | $recipient = wpinv_get_admin_email(); |
| 237 | 237 | |
| 238 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 238 | + return $this->send_email($invoice, $email, __FUNCTION__, $recipient); |
|
| 239 | 239 | |
| 240 | 240 | } |
| 241 | 241 | |
@@ -244,12 +244,12 @@ discard block |
||
| 244 | 244 | * |
| 245 | 245 | * @param WPInv_Invoice $invoice |
| 246 | 246 | */ |
| 247 | - public function cancelled_invoice( $invoice ) { |
|
| 247 | + public function cancelled_invoice($invoice) { |
|
| 248 | 248 | |
| 249 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 249 | + $email = new GetPaid_Notification_Email(__FUNCTION__, $invoice); |
|
| 250 | 250 | $recipient = wpinv_get_admin_email(); |
| 251 | 251 | |
| 252 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 252 | + return $this->send_email($invoice, $email, __FUNCTION__, $recipient); |
|
| 253 | 253 | |
| 254 | 254 | } |
| 255 | 255 | |
@@ -258,12 +258,12 @@ discard block |
||
| 258 | 258 | * |
| 259 | 259 | * @param WPInv_Invoice $invoice |
| 260 | 260 | */ |
| 261 | - public function failed_invoice( $invoice ) { |
|
| 261 | + public function failed_invoice($invoice) { |
|
| 262 | 262 | |
| 263 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 263 | + $email = new GetPaid_Notification_Email(__FUNCTION__, $invoice); |
|
| 264 | 264 | $recipient = wpinv_get_admin_email(); |
| 265 | 265 | |
| 266 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 266 | + return $this->send_email($invoice, $email, __FUNCTION__, $recipient); |
|
| 267 | 267 | |
| 268 | 268 | } |
| 269 | 269 | |
@@ -272,12 +272,12 @@ discard block |
||
| 272 | 272 | * |
| 273 | 273 | * @param WPInv_Invoice $invoice |
| 274 | 274 | */ |
| 275 | - public function onhold_invoice( $invoice ) { |
|
| 275 | + public function onhold_invoice($invoice) { |
|
| 276 | 276 | |
| 277 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 277 | + $email = new GetPaid_Notification_Email(__FUNCTION__, $invoice); |
|
| 278 | 278 | $recipient = $invoice->get_email(); |
| 279 | 279 | |
| 280 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 280 | + return $this->send_email($invoice, $email, __FUNCTION__, $recipient); |
|
| 281 | 281 | |
| 282 | 282 | } |
| 283 | 283 | |
@@ -286,12 +286,12 @@ discard block |
||
| 286 | 286 | * |
| 287 | 287 | * @param WPInv_Invoice $invoice |
| 288 | 288 | */ |
| 289 | - public function processing_invoice( $invoice ) { |
|
| 289 | + public function processing_invoice($invoice) { |
|
| 290 | 290 | |
| 291 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 291 | + $email = new GetPaid_Notification_Email(__FUNCTION__, $invoice); |
|
| 292 | 292 | $recipient = $invoice->get_email(); |
| 293 | 293 | |
| 294 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 294 | + return $this->send_email($invoice, $email, __FUNCTION__, $recipient); |
|
| 295 | 295 | |
| 296 | 296 | } |
| 297 | 297 | |
@@ -300,17 +300,17 @@ discard block |
||
| 300 | 300 | * |
| 301 | 301 | * @param WPInv_Invoice $invoice |
| 302 | 302 | */ |
| 303 | - public function completed_invoice( $invoice ) { |
|
| 303 | + public function completed_invoice($invoice) { |
|
| 304 | 304 | |
| 305 | 305 | // (Maybe) abort if it is a renewal invoice. |
| 306 | - if ( $invoice->is_renewal() && ! wpinv_get_option( 'email_completed_invoice_renewal_active', false ) ) { |
|
| 306 | + if ($invoice->is_renewal() && !wpinv_get_option('email_completed_invoice_renewal_active', false)) { |
|
| 307 | 307 | return; |
| 308 | 308 | } |
| 309 | 309 | |
| 310 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 310 | + $email = new GetPaid_Notification_Email(__FUNCTION__, $invoice); |
|
| 311 | 311 | $recipient = $invoice->get_email(); |
| 312 | 312 | |
| 313 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 313 | + return $this->send_email($invoice, $email, __FUNCTION__, $recipient); |
|
| 314 | 314 | |
| 315 | 315 | } |
| 316 | 316 | |
@@ -319,12 +319,12 @@ discard block |
||
| 319 | 319 | * |
| 320 | 320 | * @param WPInv_Invoice $invoice |
| 321 | 321 | */ |
| 322 | - public function refunded_invoice( $invoice ) { |
|
| 322 | + public function refunded_invoice($invoice) { |
|
| 323 | 323 | |
| 324 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 324 | + $email = new GetPaid_Notification_Email(__FUNCTION__, $invoice); |
|
| 325 | 325 | $recipient = $invoice->get_email(); |
| 326 | 326 | |
| 327 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 327 | + return $this->send_email($invoice, $email, __FUNCTION__, $recipient); |
|
| 328 | 328 | |
| 329 | 329 | } |
| 330 | 330 | |
@@ -334,21 +334,21 @@ discard block |
||
| 334 | 334 | * @param WPInv_Invoice $invoice |
| 335 | 335 | * @param bool $force |
| 336 | 336 | */ |
| 337 | - public function user_invoice( $invoice, $force = false ) { |
|
| 337 | + public function user_invoice($invoice, $force = false) { |
|
| 338 | 338 | |
| 339 | - if ( ! empty( $GLOBALS['wpinv_skip_invoice_notification'] ) ) { |
|
| 339 | + if (!empty($GLOBALS['wpinv_skip_invoice_notification'])) { |
|
| 340 | 340 | return; |
| 341 | 341 | } |
| 342 | 342 | |
| 343 | 343 | // Only send this email for invoices created via the admin page. |
| 344 | - if ( ! $invoice->is_type( 'invoice' ) || ( empty( $force ) && $this->is_payment_form_invoice( $invoice->get_id() ) ) ) { |
|
| 344 | + if (!$invoice->is_type('invoice') || (empty($force) && $this->is_payment_form_invoice($invoice->get_id()))) { |
|
| 345 | 345 | return; |
| 346 | 346 | } |
| 347 | 347 | |
| 348 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 348 | + $email = new GetPaid_Notification_Email(__FUNCTION__, $invoice); |
|
| 349 | 349 | $recipient = $invoice->get_email(); |
| 350 | 350 | |
| 351 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 351 | + return $this->send_email($invoice, $email, __FUNCTION__, $recipient); |
|
| 352 | 352 | |
| 353 | 353 | } |
| 354 | 354 | |
@@ -358,8 +358,8 @@ discard block |
||
| 358 | 358 | * @param int $invoice |
| 359 | 359 | * @return bool |
| 360 | 360 | */ |
| 361 | - public function is_payment_form_invoice( $invoice ) { |
|
| 362 | - return empty( $_GET['getpaid-admin-action'] ) && ( 'payment_form' == get_post_meta( $invoice, 'wpinv_created_via', true ) || 'geodirectory' == get_post_meta( $invoice, 'wpinv_created_via', true ) ); |
|
| 361 | + public function is_payment_form_invoice($invoice) { |
|
| 362 | + return empty($_GET['getpaid-admin-action']) && ('payment_form' == get_post_meta($invoice, 'wpinv_created_via', true) || 'geodirectory' == get_post_meta($invoice, 'wpinv_created_via', true)); |
|
| 363 | 363 | } |
| 364 | 364 | |
| 365 | 365 | /** |
@@ -368,12 +368,12 @@ discard block |
||
| 368 | 368 | * @param WPInv_Invoice $invoice |
| 369 | 369 | * @param string $note |
| 370 | 370 | */ |
| 371 | - public function user_note( $invoice, $note ) { |
|
| 371 | + public function user_note($invoice, $note) { |
|
| 372 | 372 | |
| 373 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 373 | + $email = new GetPaid_Notification_Email(__FUNCTION__, $invoice); |
|
| 374 | 374 | $recipient = $invoice->get_email(); |
| 375 | 375 | |
| 376 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient, array( 'customer_note' => $note ) ); |
|
| 376 | + return $this->send_email($invoice, $email, __FUNCTION__, $recipient, array('customer_note' => $note)); |
|
| 377 | 377 | |
| 378 | 378 | } |
| 379 | 379 | |
@@ -382,9 +382,9 @@ discard block |
||
| 382 | 382 | * |
| 383 | 383 | * @param WPInv_Invoice $invoice |
| 384 | 384 | */ |
| 385 | - public function force_send_overdue_notice( $invoice ) { |
|
| 386 | - $email = new GetPaid_Notification_Email( 'overdue', $invoice ); |
|
| 387 | - return $this->send_email( $invoice, $email, 'overdue', $invoice->get_email() ); |
|
| 385 | + public function force_send_overdue_notice($invoice) { |
|
| 386 | + $email = new GetPaid_Notification_Email('overdue', $invoice); |
|
| 387 | + return $this->send_email($invoice, $email, 'overdue', $invoice->get_email()); |
|
| 388 | 388 | } |
| 389 | 389 | |
| 390 | 390 | /** |
@@ -395,37 +395,37 @@ discard block |
||
| 395 | 395 | public function overdue() { |
| 396 | 396 | global $wpdb; |
| 397 | 397 | |
| 398 | - $email = new GetPaid_Notification_Email( __FUNCTION__ ); |
|
| 398 | + $email = new GetPaid_Notification_Email(__FUNCTION__); |
|
| 399 | 399 | |
| 400 | 400 | // Fetch reminder days. |
| 401 | - $reminder_days = array_unique( wp_parse_id_list( $email->get_option( 'days' ) ) ); |
|
| 401 | + $reminder_days = array_unique(wp_parse_id_list($email->get_option('days'))); |
|
| 402 | 402 | |
| 403 | 403 | // Abort if non is set. |
| 404 | - if ( empty( $reminder_days ) ) { |
|
| 404 | + if (empty($reminder_days)) { |
|
| 405 | 405 | return; |
| 406 | 406 | } |
| 407 | 407 | |
| 408 | 408 | // Retrieve date query. |
| 409 | - $date_query = $this->get_date_query( $reminder_days ); |
|
| 409 | + $date_query = $this->get_date_query($reminder_days); |
|
| 410 | 410 | |
| 411 | 411 | // Invoices table. |
| 412 | 412 | $table = $wpdb->prefix . 'getpaid_invoices'; |
| 413 | 413 | |
| 414 | 414 | // Fetch invoices. |
| 415 | - $invoices = $wpdb->get_col( |
|
| 415 | + $invoices = $wpdb->get_col( |
|
| 416 | 416 | "SELECT posts.ID FROM $wpdb->posts as posts |
| 417 | 417 | LEFT JOIN $table as invoices ON invoices.post_id = posts.ID |
| 418 | 418 | WHERE posts.post_type = 'wpi_invoice' AND posts.post_status = 'wpi-pending' $date_query"); |
| 419 | 419 | |
| 420 | - foreach ( $invoices as $invoice ) { |
|
| 420 | + foreach ($invoices as $invoice) { |
|
| 421 | 421 | |
| 422 | 422 | // Only send this email for invoices created via the admin page. |
| 423 | - if ( ! $this->is_payment_form_invoice( $invoice ) ) { |
|
| 424 | - $invoice = new WPInv_Invoice( $invoice ); |
|
| 423 | + if (!$this->is_payment_form_invoice($invoice)) { |
|
| 424 | + $invoice = new WPInv_Invoice($invoice); |
|
| 425 | 425 | $email->object = $invoice; |
| 426 | 426 | |
| 427 | - if ( $invoice->needs_payment() ) { |
|
| 428 | - $this->send_email( $invoice, $email, __FUNCTION__, $invoice->get_email() ); |
|
| 427 | + if ($invoice->needs_payment()) { |
|
| 428 | + $this->send_email($invoice, $email, __FUNCTION__, $invoice->get_email()); |
|
| 429 | 429 | } |
| 430 | 430 | |
| 431 | 431 | } |
@@ -440,14 +440,14 @@ discard block |
||
| 440 | 440 | * @param array $reminder_days |
| 441 | 441 | * @return string |
| 442 | 442 | */ |
| 443 | - public function get_date_query( $reminder_days ) { |
|
| 443 | + public function get_date_query($reminder_days) { |
|
| 444 | 444 | |
| 445 | 445 | $date_query = array( |
| 446 | 446 | 'relation' => 'OR' |
| 447 | 447 | ); |
| 448 | 448 | |
| 449 | - foreach ( $reminder_days as $days ) { |
|
| 450 | - $date = date_parse( date( 'Y-m-d', strtotime( "-$days days", current_time( 'timestamp' ) ) ) ); |
|
| 449 | + foreach ($reminder_days as $days) { |
|
| 450 | + $date = date_parse(date('Y-m-d', strtotime("-$days days", current_time('timestamp')))); |
|
| 451 | 451 | |
| 452 | 452 | $date_query[] = array( |
| 453 | 453 | 'year' => $date['year'], |
@@ -457,7 +457,7 @@ discard block |
||
| 457 | 457 | |
| 458 | 458 | } |
| 459 | 459 | |
| 460 | - $date_query = new WP_Date_Query( $date_query, 'invoices.due_date' ); |
|
| 460 | + $date_query = new WP_Date_Query($date_query, 'invoices.due_date'); |
|
| 461 | 461 | |
| 462 | 462 | return $date_query->get_sql(); |
| 463 | 463 | |
@@ -12,456 +12,456 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class GetPaid_Paypal_Gateway_IPN_Handler { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * Payment method id. |
|
| 17 | - * |
|
| 18 | - * @var string |
|
| 19 | - */ |
|
| 20 | - protected $id = 'paypal'; |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * Payment method object. |
|
| 24 | - * |
|
| 25 | - * @var GetPaid_Paypal_Gateway |
|
| 26 | - */ |
|
| 27 | - protected $gateway; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * Class constructor. |
|
| 31 | - * |
|
| 32 | - * @param GetPaid_Paypal_Gateway $gateway |
|
| 33 | - */ |
|
| 34 | - public function __construct( $gateway ) { |
|
| 35 | - $this->gateway = $gateway; |
|
| 36 | - $this->verify_ipn(); |
|
| 37 | - } |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * Processes ipns and marks payments as complete. |
|
| 41 | - * |
|
| 42 | - * @return void |
|
| 43 | - */ |
|
| 44 | - public function verify_ipn() { |
|
| 45 | - |
|
| 46 | - wpinv_error_log( 'GetPaid PayPal IPN Handler', false ); |
|
| 47 | - |
|
| 48 | - // Validate the IPN. |
|
| 49 | - if ( empty( $_POST ) || ! $this->validate_ipn() ) { |
|
| 50 | - wp_die( 'PayPal IPN Request Failure', 500 ); |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - // Process the IPN. |
|
| 54 | - $posted = wp_unslash( $_POST ); |
|
| 55 | - $invoice = $this->get_ipn_invoice( $posted ); |
|
| 56 | - |
|
| 57 | - // Abort if it was not paid by our gateway. |
|
| 58 | - if ( $this->id != $invoice->get_gateway() ) { |
|
| 59 | - wpinv_error_log( 'Aborting, Invoice was not paid via PayPal', false ); |
|
| 60 | - wp_die( 'Invoice not paid via PayPal', 500 ); |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - $posted['payment_status'] = isset( $posted['payment_status'] ) ? sanitize_key( strtolower( $posted['payment_status'] ) ) : ''; |
|
| 64 | - $posted['txn_type'] = sanitize_key( strtolower( $posted['txn_type'] ) ); |
|
| 65 | - |
|
| 66 | - wpinv_error_log( 'Payment status:' . $posted['payment_status'], false ); |
|
| 67 | - wpinv_error_log( 'IPN Type:' . $posted['txn_type'], false ); |
|
| 68 | - |
|
| 69 | - if ( method_exists( $this, 'ipn_txn_' . $posted['txn_type'] ) ) { |
|
| 70 | - call_user_func( array( $this, 'ipn_txn_' . $posted['txn_type'] ), $invoice, $posted ); |
|
| 71 | - wpinv_error_log( 'Done processing IPN', false ); |
|
| 72 | - wp_die( 'Processed', 200 ); |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - wpinv_error_log( 'Aborting, Unsupported IPN type:' . $posted['txn_type'], false ); |
|
| 76 | - wp_die( 'Unsupported IPN type', 200 ); |
|
| 77 | - |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * Retrieves IPN Invoice. |
|
| 82 | - * |
|
| 83 | - * @param array $posted |
|
| 84 | - * @return WPInv_Invoice |
|
| 85 | - */ |
|
| 86 | - protected function get_ipn_invoice( $posted ) { |
|
| 87 | - |
|
| 88 | - wpinv_error_log( 'Retrieving PayPal IPN Response Invoice', false ); |
|
| 89 | - |
|
| 90 | - if ( ! empty( $posted['custom'] ) ) { |
|
| 91 | - $invoice = new WPInv_Invoice( $posted['custom'] ); |
|
| 92 | - |
|
| 93 | - if ( $invoice->exists() ) { |
|
| 94 | - wpinv_error_log( 'Found invoice #' . $invoice->get_number(), false ); |
|
| 95 | - return $invoice; |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - wpinv_error_log( 'Could not retrieve the associated invoice.', false ); |
|
| 101 | - wp_die( 'Could not retrieve the associated invoice.', 200 ); |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * Check PayPal IPN validity. |
|
| 106 | - */ |
|
| 107 | - protected function validate_ipn() { |
|
| 108 | - |
|
| 109 | - wpinv_error_log( 'Validating PayPal IPN response', false ); |
|
| 110 | - |
|
| 111 | - // Retrieve the associated invoice. |
|
| 112 | - $posted = wp_unslash( $_POST ); |
|
| 113 | - $invoice = $this->get_ipn_invoice( $posted ); |
|
| 114 | - |
|
| 115 | - if ( $this->gateway->is_sandbox( $invoice ) ) { |
|
| 116 | - wpinv_error_log( $posted, 'Invoice was processed in sandbox hence logging the posted data' ); |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - // Validate the IPN. |
|
| 120 | - $posted['cmd'] = '_notify-validate'; |
|
| 121 | - |
|
| 122 | - // Send back post vars to paypal. |
|
| 123 | - $params = array( |
|
| 124 | - 'body' => $posted, |
|
| 125 | - 'timeout' => 60, |
|
| 126 | - 'httpversion' => '1.1', |
|
| 127 | - 'compress' => false, |
|
| 128 | - 'decompress' => false, |
|
| 129 | - 'user-agent' => 'GetPaid/' . WPINV_VERSION, |
|
| 130 | - ); |
|
| 131 | - |
|
| 132 | - // Post back to get a response. |
|
| 133 | - $response = wp_safe_remote_post( $this->gateway->is_sandbox( $invoice ) ? 'https://www.sandbox.paypal.com/cgi-bin/webscr' : 'https://www.paypal.com/cgi-bin/webscr', $params ); |
|
| 134 | - |
|
| 135 | - // Check to see if the request was valid. |
|
| 136 | - if ( ! is_wp_error( $response ) && $response['response']['code'] < 300 && strstr( $response['body'], 'VERIFIED' ) ) { |
|
| 137 | - wpinv_error_log( $response['body'], 'Received valid response from PayPal IPN' ); |
|
| 138 | - return true; |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - if ( is_wp_error( $response ) ) { |
|
| 142 | - wpinv_error_log( $response->get_error_message(), 'Received invalid response from PayPal IPN' ); |
|
| 143 | - return false; |
|
| 144 | - } |
|
| 15 | + /** |
|
| 16 | + * Payment method id. |
|
| 17 | + * |
|
| 18 | + * @var string |
|
| 19 | + */ |
|
| 20 | + protected $id = 'paypal'; |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * Payment method object. |
|
| 24 | + * |
|
| 25 | + * @var GetPaid_Paypal_Gateway |
|
| 26 | + */ |
|
| 27 | + protected $gateway; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * Class constructor. |
|
| 31 | + * |
|
| 32 | + * @param GetPaid_Paypal_Gateway $gateway |
|
| 33 | + */ |
|
| 34 | + public function __construct( $gateway ) { |
|
| 35 | + $this->gateway = $gateway; |
|
| 36 | + $this->verify_ipn(); |
|
| 37 | + } |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * Processes ipns and marks payments as complete. |
|
| 41 | + * |
|
| 42 | + * @return void |
|
| 43 | + */ |
|
| 44 | + public function verify_ipn() { |
|
| 45 | + |
|
| 46 | + wpinv_error_log( 'GetPaid PayPal IPN Handler', false ); |
|
| 47 | + |
|
| 48 | + // Validate the IPN. |
|
| 49 | + if ( empty( $_POST ) || ! $this->validate_ipn() ) { |
|
| 50 | + wp_die( 'PayPal IPN Request Failure', 500 ); |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + // Process the IPN. |
|
| 54 | + $posted = wp_unslash( $_POST ); |
|
| 55 | + $invoice = $this->get_ipn_invoice( $posted ); |
|
| 56 | + |
|
| 57 | + // Abort if it was not paid by our gateway. |
|
| 58 | + if ( $this->id != $invoice->get_gateway() ) { |
|
| 59 | + wpinv_error_log( 'Aborting, Invoice was not paid via PayPal', false ); |
|
| 60 | + wp_die( 'Invoice not paid via PayPal', 500 ); |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + $posted['payment_status'] = isset( $posted['payment_status'] ) ? sanitize_key( strtolower( $posted['payment_status'] ) ) : ''; |
|
| 64 | + $posted['txn_type'] = sanitize_key( strtolower( $posted['txn_type'] ) ); |
|
| 65 | + |
|
| 66 | + wpinv_error_log( 'Payment status:' . $posted['payment_status'], false ); |
|
| 67 | + wpinv_error_log( 'IPN Type:' . $posted['txn_type'], false ); |
|
| 68 | + |
|
| 69 | + if ( method_exists( $this, 'ipn_txn_' . $posted['txn_type'] ) ) { |
|
| 70 | + call_user_func( array( $this, 'ipn_txn_' . $posted['txn_type'] ), $invoice, $posted ); |
|
| 71 | + wpinv_error_log( 'Done processing IPN', false ); |
|
| 72 | + wp_die( 'Processed', 200 ); |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + wpinv_error_log( 'Aborting, Unsupported IPN type:' . $posted['txn_type'], false ); |
|
| 76 | + wp_die( 'Unsupported IPN type', 200 ); |
|
| 77 | + |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * Retrieves IPN Invoice. |
|
| 82 | + * |
|
| 83 | + * @param array $posted |
|
| 84 | + * @return WPInv_Invoice |
|
| 85 | + */ |
|
| 86 | + protected function get_ipn_invoice( $posted ) { |
|
| 87 | + |
|
| 88 | + wpinv_error_log( 'Retrieving PayPal IPN Response Invoice', false ); |
|
| 89 | + |
|
| 90 | + if ( ! empty( $posted['custom'] ) ) { |
|
| 91 | + $invoice = new WPInv_Invoice( $posted['custom'] ); |
|
| 92 | + |
|
| 93 | + if ( $invoice->exists() ) { |
|
| 94 | + wpinv_error_log( 'Found invoice #' . $invoice->get_number(), false ); |
|
| 95 | + return $invoice; |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + wpinv_error_log( 'Could not retrieve the associated invoice.', false ); |
|
| 101 | + wp_die( 'Could not retrieve the associated invoice.', 200 ); |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * Check PayPal IPN validity. |
|
| 106 | + */ |
|
| 107 | + protected function validate_ipn() { |
|
| 108 | + |
|
| 109 | + wpinv_error_log( 'Validating PayPal IPN response', false ); |
|
| 110 | + |
|
| 111 | + // Retrieve the associated invoice. |
|
| 112 | + $posted = wp_unslash( $_POST ); |
|
| 113 | + $invoice = $this->get_ipn_invoice( $posted ); |
|
| 114 | + |
|
| 115 | + if ( $this->gateway->is_sandbox( $invoice ) ) { |
|
| 116 | + wpinv_error_log( $posted, 'Invoice was processed in sandbox hence logging the posted data' ); |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + // Validate the IPN. |
|
| 120 | + $posted['cmd'] = '_notify-validate'; |
|
| 121 | + |
|
| 122 | + // Send back post vars to paypal. |
|
| 123 | + $params = array( |
|
| 124 | + 'body' => $posted, |
|
| 125 | + 'timeout' => 60, |
|
| 126 | + 'httpversion' => '1.1', |
|
| 127 | + 'compress' => false, |
|
| 128 | + 'decompress' => false, |
|
| 129 | + 'user-agent' => 'GetPaid/' . WPINV_VERSION, |
|
| 130 | + ); |
|
| 131 | + |
|
| 132 | + // Post back to get a response. |
|
| 133 | + $response = wp_safe_remote_post( $this->gateway->is_sandbox( $invoice ) ? 'https://www.sandbox.paypal.com/cgi-bin/webscr' : 'https://www.paypal.com/cgi-bin/webscr', $params ); |
|
| 134 | + |
|
| 135 | + // Check to see if the request was valid. |
|
| 136 | + if ( ! is_wp_error( $response ) && $response['response']['code'] < 300 && strstr( $response['body'], 'VERIFIED' ) ) { |
|
| 137 | + wpinv_error_log( $response['body'], 'Received valid response from PayPal IPN' ); |
|
| 138 | + return true; |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + if ( is_wp_error( $response ) ) { |
|
| 142 | + wpinv_error_log( $response->get_error_message(), 'Received invalid response from PayPal IPN' ); |
|
| 143 | + return false; |
|
| 144 | + } |
|
| 145 | 145 | |
| 146 | - wpinv_error_log( $response['body'], 'Received invalid response from PayPal IPN' ); |
|
| 147 | - return false; |
|
| 148 | - |
|
| 149 | - } |
|
| 146 | + wpinv_error_log( $response['body'], 'Received invalid response from PayPal IPN' ); |
|
| 147 | + return false; |
|
| 148 | + |
|
| 149 | + } |
|
| 150 | 150 | |
| 151 | - /** |
|
| 152 | - * Check currency from IPN matches the invoice. |
|
| 153 | - * |
|
| 154 | - * @param WPInv_Invoice $invoice Invoice object. |
|
| 155 | - * @param string $currency currency to validate. |
|
| 156 | - */ |
|
| 157 | - protected function validate_ipn_currency( $invoice, $currency ) { |
|
| 151 | + /** |
|
| 152 | + * Check currency from IPN matches the invoice. |
|
| 153 | + * |
|
| 154 | + * @param WPInv_Invoice $invoice Invoice object. |
|
| 155 | + * @param string $currency currency to validate. |
|
| 156 | + */ |
|
| 157 | + protected function validate_ipn_currency( $invoice, $currency ) { |
|
| 158 | 158 | |
| 159 | - if ( strtolower( $invoice->get_currency() ) !== strtolower( $currency ) ) { |
|
| 159 | + if ( strtolower( $invoice->get_currency() ) !== strtolower( $currency ) ) { |
|
| 160 | 160 | |
| 161 | - /* translators: %s: currency code. */ |
|
| 162 | - $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal currencies do not match (code %s).', 'invoicing' ), $currency ) ); |
|
| 161 | + /* translators: %s: currency code. */ |
|
| 162 | + $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal currencies do not match (code %s).', 'invoicing' ), $currency ) ); |
|
| 163 | 163 | |
| 164 | - wpinv_error_log( "Currencies do not match: {$currency} instead of {$invoice->get_currency()}", 'IPN Error', __FILE__, __LINE__, true ); |
|
| 165 | - } |
|
| 164 | + wpinv_error_log( "Currencies do not match: {$currency} instead of {$invoice->get_currency()}", 'IPN Error', __FILE__, __LINE__, true ); |
|
| 165 | + } |
|
| 166 | 166 | |
| 167 | - wpinv_error_log( $currency, 'Validated IPN Currency' ); |
|
| 168 | - } |
|
| 167 | + wpinv_error_log( $currency, 'Validated IPN Currency' ); |
|
| 168 | + } |
|
| 169 | 169 | |
| 170 | - /** |
|
| 171 | - * Check payment amount from IPN matches the invoice. |
|
| 172 | - * |
|
| 173 | - * @param WPInv_Invoice $invoice Invoice object. |
|
| 174 | - * @param float $amount amount to validate. |
|
| 175 | - */ |
|
| 176 | - protected function validate_ipn_amount( $invoice, $amount ) { |
|
| 177 | - if ( number_format( $invoice->get_total(), 2, '.', '' ) !== number_format( $amount, 2, '.', '' ) ) { |
|
| 170 | + /** |
|
| 171 | + * Check payment amount from IPN matches the invoice. |
|
| 172 | + * |
|
| 173 | + * @param WPInv_Invoice $invoice Invoice object. |
|
| 174 | + * @param float $amount amount to validate. |
|
| 175 | + */ |
|
| 176 | + protected function validate_ipn_amount( $invoice, $amount ) { |
|
| 177 | + if ( number_format( $invoice->get_total(), 2, '.', '' ) !== number_format( $amount, 2, '.', '' ) ) { |
|
| 178 | 178 | |
| 179 | - /* translators: %s: Amount. */ |
|
| 180 | - $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal amounts do not match (gross %s).', 'invoicing' ), $amount ) ); |
|
| 179 | + /* translators: %s: Amount. */ |
|
| 180 | + $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal amounts do not match (gross %s).', 'invoicing' ), $amount ) ); |
|
| 181 | 181 | |
| 182 | - wpinv_error_log( "Amounts do not match: {$amount} instead of {$invoice->get_total()}", 'IPN Error', __FILE__, __LINE__, true ); |
|
| 183 | - } |
|
| 182 | + wpinv_error_log( "Amounts do not match: {$amount} instead of {$invoice->get_total()}", 'IPN Error', __FILE__, __LINE__, true ); |
|
| 183 | + } |
|
| 184 | 184 | |
| 185 | - wpinv_error_log( $amount, 'Validated IPN Amount' ); |
|
| 186 | - } |
|
| 185 | + wpinv_error_log( $amount, 'Validated IPN Amount' ); |
|
| 186 | + } |
|
| 187 | 187 | |
| 188 | - /** |
|
| 189 | - * Verify receiver email from PayPal. |
|
| 190 | - * |
|
| 191 | - * @param WPInv_Invoice $invoice Invoice object. |
|
| 192 | - * @param string $receiver_email Email to validate. |
|
| 193 | - */ |
|
| 194 | - protected function validate_ipn_receiver_email( $invoice, $receiver_email ) { |
|
| 195 | - $paypal_email = wpinv_get_option( 'paypal_email' ); |
|
| 188 | + /** |
|
| 189 | + * Verify receiver email from PayPal. |
|
| 190 | + * |
|
| 191 | + * @param WPInv_Invoice $invoice Invoice object. |
|
| 192 | + * @param string $receiver_email Email to validate. |
|
| 193 | + */ |
|
| 194 | + protected function validate_ipn_receiver_email( $invoice, $receiver_email ) { |
|
| 195 | + $paypal_email = wpinv_get_option( 'paypal_email' ); |
|
| 196 | 196 | |
| 197 | - if ( strcasecmp( trim( $receiver_email ), trim( $paypal_email ) ) !== 0 ) { |
|
| 198 | - wpinv_record_gateway_error( 'IPN Error', "IPN Response is for another account: {$receiver_email}. Your email is {$paypal_email}" ); |
|
| 197 | + if ( strcasecmp( trim( $receiver_email ), trim( $paypal_email ) ) !== 0 ) { |
|
| 198 | + wpinv_record_gateway_error( 'IPN Error', "IPN Response is for another account: {$receiver_email}. Your email is {$paypal_email}" ); |
|
| 199 | 199 | |
| 200 | - /* translators: %s: email address . */ |
|
| 201 | - $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal IPN response from a different email address (%s).', 'invoicing' ), $receiver_email ) ); |
|
| 200 | + /* translators: %s: email address . */ |
|
| 201 | + $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal IPN response from a different email address (%s).', 'invoicing' ), $receiver_email ) ); |
|
| 202 | 202 | |
| 203 | - return wpinv_error_log( "IPN Response is for another account: {$receiver_email}. Your email is {$paypal_email}", 'IPN Error', __FILE__, __LINE__, true ); |
|
| 204 | - } |
|
| 203 | + return wpinv_error_log( "IPN Response is for another account: {$receiver_email}. Your email is {$paypal_email}", 'IPN Error', __FILE__, __LINE__, true ); |
|
| 204 | + } |
|
| 205 | 205 | |
| 206 | - wpinv_error_log( 'Validated PayPal Email', false ); |
|
| 207 | - } |
|
| 206 | + wpinv_error_log( 'Validated PayPal Email', false ); |
|
| 207 | + } |
|
| 208 | 208 | |
| 209 | - /** |
|
| 210 | - * Handles one time payments. |
|
| 211 | - * |
|
| 212 | - * @param WPInv_Invoice $invoice Invoice object. |
|
| 213 | - * @param array $posted Posted data. |
|
| 214 | - */ |
|
| 215 | - protected function ipn_txn_web_accept( $invoice, $posted ) { |
|
| 216 | - |
|
| 217 | - // Collect payment details |
|
| 218 | - $payment_status = strtolower( $posted['payment_status'] ); |
|
| 219 | - $business_email = isset( $posted['business'] ) && is_email( $posted['business'] ) ? trim( $posted['business'] ) : trim( $posted['receiver_email'] ); |
|
| 220 | - |
|
| 221 | - $this->validate_ipn_receiver_email( $invoice, $business_email ); |
|
| 222 | - $this->validate_ipn_currency( $invoice, $posted['mc_currency'] ); |
|
| 223 | - |
|
| 224 | - // Update the transaction id. |
|
| 225 | - if ( ! empty( $posted['txn_id'] ) ) { |
|
| 226 | - $invoice->set_transaction_id( wpinv_clean( $posted['txn_id'] ) ); |
|
| 227 | - $invoice->save(); |
|
| 228 | - } |
|
| 209 | + /** |
|
| 210 | + * Handles one time payments. |
|
| 211 | + * |
|
| 212 | + * @param WPInv_Invoice $invoice Invoice object. |
|
| 213 | + * @param array $posted Posted data. |
|
| 214 | + */ |
|
| 215 | + protected function ipn_txn_web_accept( $invoice, $posted ) { |
|
| 216 | + |
|
| 217 | + // Collect payment details |
|
| 218 | + $payment_status = strtolower( $posted['payment_status'] ); |
|
| 219 | + $business_email = isset( $posted['business'] ) && is_email( $posted['business'] ) ? trim( $posted['business'] ) : trim( $posted['receiver_email'] ); |
|
| 220 | + |
|
| 221 | + $this->validate_ipn_receiver_email( $invoice, $business_email ); |
|
| 222 | + $this->validate_ipn_currency( $invoice, $posted['mc_currency'] ); |
|
| 223 | + |
|
| 224 | + // Update the transaction id. |
|
| 225 | + if ( ! empty( $posted['txn_id'] ) ) { |
|
| 226 | + $invoice->set_transaction_id( wpinv_clean( $posted['txn_id'] ) ); |
|
| 227 | + $invoice->save(); |
|
| 228 | + } |
|
| 229 | 229 | |
| 230 | - // Process a refund. |
|
| 231 | - if ( $payment_status == 'refunded' || $payment_status == 'reversed' ) { |
|
| 230 | + // Process a refund. |
|
| 231 | + if ( $payment_status == 'refunded' || $payment_status == 'reversed' ) { |
|
| 232 | 232 | |
| 233 | - update_post_meta( $invoice->get_id(), 'refunded_remotely', 1 ); |
|
| 233 | + update_post_meta( $invoice->get_id(), 'refunded_remotely', 1 ); |
|
| 234 | 234 | |
| 235 | - if ( ! $invoice->is_refunded() ) { |
|
| 236 | - $invoice->update_status( 'wpi-refunded', $posted['reason_code'] ); |
|
| 237 | - } |
|
| 235 | + if ( ! $invoice->is_refunded() ) { |
|
| 236 | + $invoice->update_status( 'wpi-refunded', $posted['reason_code'] ); |
|
| 237 | + } |
|
| 238 | 238 | |
| 239 | - return wpinv_error_log( $posted['reason_code'], false ); |
|
| 240 | - } |
|
| 239 | + return wpinv_error_log( $posted['reason_code'], false ); |
|
| 240 | + } |
|
| 241 | 241 | |
| 242 | - // Process payments. |
|
| 243 | - if ( $payment_status == 'completed' ) { |
|
| 242 | + // Process payments. |
|
| 243 | + if ( $payment_status == 'completed' ) { |
|
| 244 | 244 | |
| 245 | - if ( $invoice->is_paid() && 'wpi_processing' != $invoice->get_status() ) { |
|
| 246 | - return wpinv_error_log( 'Aborting, Invoice #' . $invoice->get_number() . ' is already paid.', false ); |
|
| 247 | - } |
|
| 245 | + if ( $invoice->is_paid() && 'wpi_processing' != $invoice->get_status() ) { |
|
| 246 | + return wpinv_error_log( 'Aborting, Invoice #' . $invoice->get_number() . ' is already paid.', false ); |
|
| 247 | + } |
|
| 248 | 248 | |
| 249 | - $this->validate_ipn_amount( $invoice, $posted['mc_gross'] ); |
|
| 249 | + $this->validate_ipn_amount( $invoice, $posted['mc_gross'] ); |
|
| 250 | 250 | |
| 251 | - $note = ''; |
|
| 251 | + $note = ''; |
|
| 252 | 252 | |
| 253 | - if ( ! empty( $posted['mc_fee'] ) ) { |
|
| 254 | - $note = sprintf( __( 'PayPal Transaction Fee %.', 'invoicing' ), sanitize_text_field( $posted['mc_fee'] ) ); |
|
| 255 | - } |
|
| 253 | + if ( ! empty( $posted['mc_fee'] ) ) { |
|
| 254 | + $note = sprintf( __( 'PayPal Transaction Fee %.', 'invoicing' ), sanitize_text_field( $posted['mc_fee'] ) ); |
|
| 255 | + } |
|
| 256 | 256 | |
| 257 | - if ( ! empty( $posted['payer_status'] ) ) { |
|
| 258 | - $note = ' ' . sprintf( __( 'Buyer status %.', 'invoicing' ), sanitize_text_field( $posted['payer_status'] ) ); |
|
| 259 | - } |
|
| 257 | + if ( ! empty( $posted['payer_status'] ) ) { |
|
| 258 | + $note = ' ' . sprintf( __( 'Buyer status %.', 'invoicing' ), sanitize_text_field( $posted['payer_status'] ) ); |
|
| 259 | + } |
|
| 260 | 260 | |
| 261 | - $invoice->mark_paid( ( ! empty( $posted['txn_id'] ) ? sanitize_text_field( $posted['txn_id'] ) : '' ), trim( $note ) ); |
|
| 262 | - return wpinv_error_log( 'Invoice marked as paid.', false ); |
|
| 261 | + $invoice->mark_paid( ( ! empty( $posted['txn_id'] ) ? sanitize_text_field( $posted['txn_id'] ) : '' ), trim( $note ) ); |
|
| 262 | + return wpinv_error_log( 'Invoice marked as paid.', false ); |
|
| 263 | 263 | |
| 264 | - } |
|
| 264 | + } |
|
| 265 | 265 | |
| 266 | - // Pending payments. |
|
| 267 | - if ( $payment_status == 'pending' ) { |
|
| 266 | + // Pending payments. |
|
| 267 | + if ( $payment_status == 'pending' ) { |
|
| 268 | 268 | |
| 269 | - /* translators: %s: pending reason. */ |
|
| 270 | - $invoice->update_status( 'wpi-onhold', sprintf( __( 'Payment pending (%s).', 'invoicing' ), $posted['pending_reason'] ) ); |
|
| 269 | + /* translators: %s: pending reason. */ |
|
| 270 | + $invoice->update_status( 'wpi-onhold', sprintf( __( 'Payment pending (%s).', 'invoicing' ), $posted['pending_reason'] ) ); |
|
| 271 | 271 | |
| 272 | - return wpinv_error_log( 'Invoice marked as "payment held".', false ); |
|
| 273 | - } |
|
| 272 | + return wpinv_error_log( 'Invoice marked as "payment held".', false ); |
|
| 273 | + } |
|
| 274 | 274 | |
| 275 | - /* translators: %s: payment status. */ |
|
| 276 | - $invoice->update_status( 'wpi-failed', sprintf( __( 'Payment %s via IPN.', 'invoicing' ), sanitize_text_field( $posted['payment_status'] ) ) ); |
|
| 275 | + /* translators: %s: payment status. */ |
|
| 276 | + $invoice->update_status( 'wpi-failed', sprintf( __( 'Payment %s via IPN.', 'invoicing' ), sanitize_text_field( $posted['payment_status'] ) ) ); |
|
| 277 | 277 | |
| 278 | - } |
|
| 278 | + } |
|
| 279 | 279 | |
| 280 | - /** |
|
| 281 | - * Handles one time payments. |
|
| 282 | - * |
|
| 283 | - * @param WPInv_Invoice $invoice Invoice object. |
|
| 284 | - * @param array $posted Posted data. |
|
| 285 | - */ |
|
| 286 | - protected function ipn_txn_cart( $invoice, $posted ) { |
|
| 287 | - $this->ipn_txn_web_accept( $invoice, $posted ); |
|
| 288 | - } |
|
| 280 | + /** |
|
| 281 | + * Handles one time payments. |
|
| 282 | + * |
|
| 283 | + * @param WPInv_Invoice $invoice Invoice object. |
|
| 284 | + * @param array $posted Posted data. |
|
| 285 | + */ |
|
| 286 | + protected function ipn_txn_cart( $invoice, $posted ) { |
|
| 287 | + $this->ipn_txn_web_accept( $invoice, $posted ); |
|
| 288 | + } |
|
| 289 | 289 | |
| 290 | - /** |
|
| 291 | - * Handles subscription sign ups. |
|
| 292 | - * |
|
| 293 | - * @param WPInv_Invoice $invoice Invoice object. |
|
| 294 | - * @param array $posted Posted data. |
|
| 295 | - */ |
|
| 296 | - protected function ipn_txn_subscr_signup( $invoice, $posted ) { |
|
| 290 | + /** |
|
| 291 | + * Handles subscription sign ups. |
|
| 292 | + * |
|
| 293 | + * @param WPInv_Invoice $invoice Invoice object. |
|
| 294 | + * @param array $posted Posted data. |
|
| 295 | + */ |
|
| 296 | + protected function ipn_txn_subscr_signup( $invoice, $posted ) { |
|
| 297 | 297 | |
| 298 | - wpinv_error_log( 'Processing subscription signup', false ); |
|
| 298 | + wpinv_error_log( 'Processing subscription signup', false ); |
|
| 299 | 299 | |
| 300 | - // Make sure the invoice has a subscription. |
|
| 301 | - $subscription = getpaid_get_invoice_subscription( $invoice ); |
|
| 300 | + // Make sure the invoice has a subscription. |
|
| 301 | + $subscription = getpaid_get_invoice_subscription( $invoice ); |
|
| 302 | 302 | |
| 303 | - if ( empty( $subscription ) ) { |
|
| 304 | - return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
| 305 | - } |
|
| 303 | + if ( empty( $subscription ) ) { |
|
| 304 | + return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
| 305 | + } |
|
| 306 | 306 | |
| 307 | - // Validate the IPN. |
|
| 308 | - $business_email = isset( $posted['business'] ) && is_email( $posted['business'] ) ? trim( $posted['business'] ) : trim( $posted['receiver_email'] ); |
|
| 309 | - $this->validate_ipn_receiver_email( $invoice, $business_email ); |
|
| 310 | - $this->validate_ipn_currency( $invoice, $posted['mc_currency'] ); |
|
| 307 | + // Validate the IPN. |
|
| 308 | + $business_email = isset( $posted['business'] ) && is_email( $posted['business'] ) ? trim( $posted['business'] ) : trim( $posted['receiver_email'] ); |
|
| 309 | + $this->validate_ipn_receiver_email( $invoice, $business_email ); |
|
| 310 | + $this->validate_ipn_currency( $invoice, $posted['mc_currency'] ); |
|
| 311 | 311 | |
| 312 | - // Activate the subscription. |
|
| 313 | - $duration = strtotime( $subscription->get_expiration() ) - strtotime( $subscription->get_date_created() ); |
|
| 314 | - $subscription->set_date_created( current_time( 'mysql' ) ); |
|
| 315 | - $subscription->set_expiration( date( 'Y-m-d H:i:s', ( current_time( 'timestamp' ) + $duration ) ) ); |
|
| 316 | - $subscription->set_profile_id( sanitize_text_field( $posted['subscr_id'] ) ); |
|
| 317 | - $subscription->activate(); |
|
| 312 | + // Activate the subscription. |
|
| 313 | + $duration = strtotime( $subscription->get_expiration() ) - strtotime( $subscription->get_date_created() ); |
|
| 314 | + $subscription->set_date_created( current_time( 'mysql' ) ); |
|
| 315 | + $subscription->set_expiration( date( 'Y-m-d H:i:s', ( current_time( 'timestamp' ) + $duration ) ) ); |
|
| 316 | + $subscription->set_profile_id( sanitize_text_field( $posted['subscr_id'] ) ); |
|
| 317 | + $subscription->activate(); |
|
| 318 | 318 | |
| 319 | - // Set the transaction id. |
|
| 320 | - if ( ! empty( $posted['txn_id'] ) ) { |
|
| 321 | - $invoice->set_transaction_id( $posted['txn_id'] ); |
|
| 322 | - } |
|
| 319 | + // Set the transaction id. |
|
| 320 | + if ( ! empty( $posted['txn_id'] ) ) { |
|
| 321 | + $invoice->set_transaction_id( $posted['txn_id'] ); |
|
| 322 | + } |
|
| 323 | 323 | |
| 324 | - // Update the payment status. |
|
| 325 | - $invoice->mark_paid(); |
|
| 324 | + // Update the payment status. |
|
| 325 | + $invoice->mark_paid(); |
|
| 326 | 326 | |
| 327 | - $invoice->add_note( sprintf( __( 'PayPal Subscription ID: %s', 'invoicing' ) , $posted['subscr_id'] ), false, false, true ); |
|
| 327 | + $invoice->add_note( sprintf( __( 'PayPal Subscription ID: %s', 'invoicing' ) , $posted['subscr_id'] ), false, false, true ); |
|
| 328 | 328 | |
| 329 | - wpinv_error_log( 'Subscription started.', false ); |
|
| 330 | - } |
|
| 329 | + wpinv_error_log( 'Subscription started.', false ); |
|
| 330 | + } |
|
| 331 | 331 | |
| 332 | - /** |
|
| 333 | - * Handles subscription renewals. |
|
| 334 | - * |
|
| 335 | - * @param WPInv_Invoice $invoice Invoice object. |
|
| 336 | - * @param array $posted Posted data. |
|
| 337 | - */ |
|
| 338 | - protected function ipn_txn_subscr_payment( $invoice, $posted ) { |
|
| 332 | + /** |
|
| 333 | + * Handles subscription renewals. |
|
| 334 | + * |
|
| 335 | + * @param WPInv_Invoice $invoice Invoice object. |
|
| 336 | + * @param array $posted Posted data. |
|
| 337 | + */ |
|
| 338 | + protected function ipn_txn_subscr_payment( $invoice, $posted ) { |
|
| 339 | 339 | |
| 340 | - // Make sure the invoice has a subscription. |
|
| 341 | - $subscription = wpinv_get_subscription( $invoice ); |
|
| 340 | + // Make sure the invoice has a subscription. |
|
| 341 | + $subscription = wpinv_get_subscription( $invoice ); |
|
| 342 | 342 | |
| 343 | - if ( empty( $subscription ) ) { |
|
| 344 | - return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
| 345 | - } |
|
| 343 | + if ( empty( $subscription ) ) { |
|
| 344 | + return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
| 345 | + } |
|
| 346 | 346 | |
| 347 | - // Abort if this is the first payment. |
|
| 348 | - if ( ( $invoice->is_paid() && date( 'Ynd', strtotime( $invoice->get_date_completed() ) ) == date( 'Ynd', strtotime( $posted['payment_date'] ) ) ) || date( 'Ynd', $subscription->get_time_created() ) == date( 'Ynd', strtotime( $posted['payment_date'] ) ) ) { |
|
| 347 | + // Abort if this is the first payment. |
|
| 348 | + if ( ( $invoice->is_paid() && date( 'Ynd', strtotime( $invoice->get_date_completed() ) ) == date( 'Ynd', strtotime( $posted['payment_date'] ) ) ) || date( 'Ynd', $subscription->get_time_created() ) == date( 'Ynd', strtotime( $posted['payment_date'] ) ) ) { |
|
| 349 | 349 | |
| 350 | - if ( ! empty( $posted['txn_id'] ) ) { |
|
| 351 | - $invoice->set_transaction_id( sanitize_text_field( $posted['txn_id'] ) ); |
|
| 352 | - $invoice->mark_paid(); |
|
| 353 | - $invoice->save(); |
|
| 354 | - } |
|
| 350 | + if ( ! empty( $posted['txn_id'] ) ) { |
|
| 351 | + $invoice->set_transaction_id( sanitize_text_field( $posted['txn_id'] ) ); |
|
| 352 | + $invoice->mark_paid(); |
|
| 353 | + $invoice->save(); |
|
| 354 | + } |
|
| 355 | 355 | |
| 356 | - return; |
|
| 357 | - } |
|
| 356 | + return; |
|
| 357 | + } |
|
| 358 | 358 | |
| 359 | - wpinv_error_log( 'Processing subscription renewal payment for the invoice ' . $invoice->get_id(), false ); |
|
| 359 | + wpinv_error_log( 'Processing subscription renewal payment for the invoice ' . $invoice->get_id(), false ); |
|
| 360 | 360 | |
| 361 | - // Abort if the payment is already recorded. |
|
| 362 | - if ( wpinv_get_id_by_transaction_id( $posted['txn_id'] ) ) { |
|
| 363 | - return wpinv_error_log( 'Aborting, Transaction ' . $posted['txn_id'] .' has already been processed', false ); |
|
| 364 | - } |
|
| 365 | - |
|
| 366 | - $args = array( |
|
| 367 | - 'transaction_id' => $posted['txn_id'], |
|
| 368 | - 'gateway' => $this->id, |
|
| 369 | - ); |
|
| 370 | - |
|
| 371 | - $invoice = wpinv_get_invoice( $subscription->add_payment( $args ) ); |
|
| 372 | - |
|
| 373 | - if ( empty( $invoice ) ) { |
|
| 374 | - return; |
|
| 375 | - } |
|
| 376 | - |
|
| 377 | - $invoice->add_note( wp_sprintf( __( 'PayPal Transaction ID: %s', 'invoicing' ) , $posted['txn_id'] ), false, false, true ); |
|
| 378 | - $invoice->add_note( wp_sprintf( __( 'PayPal Subscription ID: %s', 'invoicing' ) , $posted['subscr_id'] ), false, false, true ); |
|
| 379 | - |
|
| 380 | - $subscription->renew(); |
|
| 381 | - wpinv_error_log( 'Subscription renewed.', false ); |
|
| 382 | - |
|
| 383 | - } |
|
| 384 | - |
|
| 385 | - /** |
|
| 386 | - * Handles subscription cancelations. |
|
| 387 | - * |
|
| 388 | - * @param WPInv_Invoice $invoice Invoice object. |
|
| 389 | - */ |
|
| 390 | - protected function ipn_txn_subscr_cancel( $invoice ) { |
|
| 391 | - |
|
| 392 | - // Make sure the invoice has a subscription. |
|
| 393 | - $subscription = wpinv_get_subscription( $invoice ); |
|
| 394 | - |
|
| 395 | - if ( empty( $subscription ) ) { |
|
| 396 | - return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false); |
|
| 397 | - } |
|
| 398 | - |
|
| 399 | - wpinv_error_log( 'Processing subscription cancellation for the invoice ' . $invoice->get_id(), false ); |
|
| 400 | - $subscription->cancel(); |
|
| 401 | - wpinv_error_log( 'Subscription cancelled.', false ); |
|
| 402 | - |
|
| 403 | - } |
|
| 404 | - |
|
| 405 | - /** |
|
| 406 | - * Handles subscription completions. |
|
| 407 | - * |
|
| 408 | - * @param WPInv_Invoice $invoice Invoice object. |
|
| 409 | - * @param array $posted Posted data. |
|
| 410 | - */ |
|
| 411 | - protected function ipn_txn_subscr_eot( $invoice ) { |
|
| 412 | - |
|
| 413 | - // Make sure the invoice has a subscription. |
|
| 414 | - $subscription = wpinv_get_subscription( $invoice ); |
|
| 361 | + // Abort if the payment is already recorded. |
|
| 362 | + if ( wpinv_get_id_by_transaction_id( $posted['txn_id'] ) ) { |
|
| 363 | + return wpinv_error_log( 'Aborting, Transaction ' . $posted['txn_id'] .' has already been processed', false ); |
|
| 364 | + } |
|
| 365 | + |
|
| 366 | + $args = array( |
|
| 367 | + 'transaction_id' => $posted['txn_id'], |
|
| 368 | + 'gateway' => $this->id, |
|
| 369 | + ); |
|
| 370 | + |
|
| 371 | + $invoice = wpinv_get_invoice( $subscription->add_payment( $args ) ); |
|
| 372 | + |
|
| 373 | + if ( empty( $invoice ) ) { |
|
| 374 | + return; |
|
| 375 | + } |
|
| 376 | + |
|
| 377 | + $invoice->add_note( wp_sprintf( __( 'PayPal Transaction ID: %s', 'invoicing' ) , $posted['txn_id'] ), false, false, true ); |
|
| 378 | + $invoice->add_note( wp_sprintf( __( 'PayPal Subscription ID: %s', 'invoicing' ) , $posted['subscr_id'] ), false, false, true ); |
|
| 379 | + |
|
| 380 | + $subscription->renew(); |
|
| 381 | + wpinv_error_log( 'Subscription renewed.', false ); |
|
| 382 | + |
|
| 383 | + } |
|
| 384 | + |
|
| 385 | + /** |
|
| 386 | + * Handles subscription cancelations. |
|
| 387 | + * |
|
| 388 | + * @param WPInv_Invoice $invoice Invoice object. |
|
| 389 | + */ |
|
| 390 | + protected function ipn_txn_subscr_cancel( $invoice ) { |
|
| 391 | + |
|
| 392 | + // Make sure the invoice has a subscription. |
|
| 393 | + $subscription = wpinv_get_subscription( $invoice ); |
|
| 394 | + |
|
| 395 | + if ( empty( $subscription ) ) { |
|
| 396 | + return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false); |
|
| 397 | + } |
|
| 398 | + |
|
| 399 | + wpinv_error_log( 'Processing subscription cancellation for the invoice ' . $invoice->get_id(), false ); |
|
| 400 | + $subscription->cancel(); |
|
| 401 | + wpinv_error_log( 'Subscription cancelled.', false ); |
|
| 402 | + |
|
| 403 | + } |
|
| 404 | + |
|
| 405 | + /** |
|
| 406 | + * Handles subscription completions. |
|
| 407 | + * |
|
| 408 | + * @param WPInv_Invoice $invoice Invoice object. |
|
| 409 | + * @param array $posted Posted data. |
|
| 410 | + */ |
|
| 411 | + protected function ipn_txn_subscr_eot( $invoice ) { |
|
| 412 | + |
|
| 413 | + // Make sure the invoice has a subscription. |
|
| 414 | + $subscription = wpinv_get_subscription( $invoice ); |
|
| 415 | 415 | |
| 416 | - if ( empty( $subscription ) ) { |
|
| 417 | - return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
| 418 | - } |
|
| 416 | + if ( empty( $subscription ) ) { |
|
| 417 | + return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
| 418 | + } |
|
| 419 | 419 | |
| 420 | - wpinv_error_log( 'Processing subscription end of life for the invoice ' . $invoice->get_id(), false ); |
|
| 421 | - $subscription->complete(); |
|
| 422 | - wpinv_error_log( 'Subscription completed.', false ); |
|
| 420 | + wpinv_error_log( 'Processing subscription end of life for the invoice ' . $invoice->get_id(), false ); |
|
| 421 | + $subscription->complete(); |
|
| 422 | + wpinv_error_log( 'Subscription completed.', false ); |
|
| 423 | 423 | |
| 424 | - } |
|
| 424 | + } |
|
| 425 | 425 | |
| 426 | - /** |
|
| 427 | - * Handles subscription fails. |
|
| 428 | - * |
|
| 429 | - * @param WPInv_Invoice $invoice Invoice object. |
|
| 430 | - * @param array $posted Posted data. |
|
| 431 | - */ |
|
| 432 | - protected function ipn_txn_subscr_failed( $invoice ) { |
|
| 433 | - |
|
| 434 | - // Make sure the invoice has a subscription. |
|
| 435 | - $subscription = wpinv_get_subscription( $invoice ); |
|
| 426 | + /** |
|
| 427 | + * Handles subscription fails. |
|
| 428 | + * |
|
| 429 | + * @param WPInv_Invoice $invoice Invoice object. |
|
| 430 | + * @param array $posted Posted data. |
|
| 431 | + */ |
|
| 432 | + protected function ipn_txn_subscr_failed( $invoice ) { |
|
| 433 | + |
|
| 434 | + // Make sure the invoice has a subscription. |
|
| 435 | + $subscription = wpinv_get_subscription( $invoice ); |
|
| 436 | 436 | |
| 437 | - if ( empty( $subscription ) ) { |
|
| 438 | - return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
| 439 | - } |
|
| 437 | + if ( empty( $subscription ) ) { |
|
| 438 | + return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
| 439 | + } |
|
| 440 | 440 | |
| 441 | - wpinv_error_log( 'Processing subscription payment failure for the invoice ' . $invoice->get_id(), false ); |
|
| 442 | - $subscription->failing(); |
|
| 443 | - wpinv_error_log( 'Subscription marked as failing.', false ); |
|
| 441 | + wpinv_error_log( 'Processing subscription payment failure for the invoice ' . $invoice->get_id(), false ); |
|
| 442 | + $subscription->failing(); |
|
| 443 | + wpinv_error_log( 'Subscription marked as failing.', false ); |
|
| 444 | 444 | |
| 445 | - } |
|
| 445 | + } |
|
| 446 | 446 | |
| 447 | - /** |
|
| 448 | - * Handles subscription suspensions. |
|
| 449 | - * |
|
| 450 | - * @param WPInv_Invoice $invoice Invoice object. |
|
| 451 | - * @param array $posted Posted data. |
|
| 452 | - */ |
|
| 453 | - protected function ipn_txn_recurring_payment_suspended_due_to_max_failed_payment( $invoice ) { |
|
| 447 | + /** |
|
| 448 | + * Handles subscription suspensions. |
|
| 449 | + * |
|
| 450 | + * @param WPInv_Invoice $invoice Invoice object. |
|
| 451 | + * @param array $posted Posted data. |
|
| 452 | + */ |
|
| 453 | + protected function ipn_txn_recurring_payment_suspended_due_to_max_failed_payment( $invoice ) { |
|
| 454 | 454 | |
| 455 | - // Make sure the invoice has a subscription. |
|
| 456 | - $subscription = wpinv_get_subscription( $invoice ); |
|
| 455 | + // Make sure the invoice has a subscription. |
|
| 456 | + $subscription = wpinv_get_subscription( $invoice ); |
|
| 457 | 457 | |
| 458 | - if ( empty( $subscription ) ) { |
|
| 459 | - return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
| 460 | - } |
|
| 461 | - |
|
| 462 | - wpinv_error_log( 'Processing subscription cancellation due to max failed payment for the invoice ' . $invoice->get_id(), false ); |
|
| 463 | - $subscription->cancel(); |
|
| 464 | - wpinv_error_log( 'Subscription cancelled.', false ); |
|
| 465 | - } |
|
| 458 | + if ( empty( $subscription ) ) { |
|
| 459 | + return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
| 460 | + } |
|
| 461 | + |
|
| 462 | + wpinv_error_log( 'Processing subscription cancellation due to max failed payment for the invoice ' . $invoice->get_id(), false ); |
|
| 463 | + $subscription->cancel(); |
|
| 464 | + wpinv_error_log( 'Subscription cancelled.', false ); |
|
| 465 | + } |
|
| 466 | 466 | |
| 467 | 467 | } |
@@ -4,7 +4,7 @@ discard block |
||
| 4 | 4 | * |
| 5 | 5 | */ |
| 6 | 6 | |
| 7 | -defined( 'ABSPATH' ) || exit; |
|
| 7 | +defined('ABSPATH') || exit; |
|
| 8 | 8 | |
| 9 | 9 | /** |
| 10 | 10 | * Paypal Payment Gateway IPN handler class. |
@@ -31,7 +31,7 @@ discard block |
||
| 31 | 31 | * |
| 32 | 32 | * @param GetPaid_Paypal_Gateway $gateway |
| 33 | 33 | */ |
| 34 | - public function __construct( $gateway ) { |
|
| 34 | + public function __construct($gateway) { |
|
| 35 | 35 | $this->gateway = $gateway; |
| 36 | 36 | $this->verify_ipn(); |
| 37 | 37 | } |
@@ -43,37 +43,37 @@ discard block |
||
| 43 | 43 | */ |
| 44 | 44 | public function verify_ipn() { |
| 45 | 45 | |
| 46 | - wpinv_error_log( 'GetPaid PayPal IPN Handler', false ); |
|
| 46 | + wpinv_error_log('GetPaid PayPal IPN Handler', false); |
|
| 47 | 47 | |
| 48 | 48 | // Validate the IPN. |
| 49 | - if ( empty( $_POST ) || ! $this->validate_ipn() ) { |
|
| 50 | - wp_die( 'PayPal IPN Request Failure', 500 ); |
|
| 49 | + if (empty($_POST) || !$this->validate_ipn()) { |
|
| 50 | + wp_die('PayPal IPN Request Failure', 500); |
|
| 51 | 51 | } |
| 52 | 52 | |
| 53 | 53 | // Process the IPN. |
| 54 | - $posted = wp_unslash( $_POST ); |
|
| 55 | - $invoice = $this->get_ipn_invoice( $posted ); |
|
| 54 | + $posted = wp_unslash($_POST); |
|
| 55 | + $invoice = $this->get_ipn_invoice($posted); |
|
| 56 | 56 | |
| 57 | 57 | // Abort if it was not paid by our gateway. |
| 58 | - if ( $this->id != $invoice->get_gateway() ) { |
|
| 59 | - wpinv_error_log( 'Aborting, Invoice was not paid via PayPal', false ); |
|
| 60 | - wp_die( 'Invoice not paid via PayPal', 500 ); |
|
| 58 | + if ($this->id != $invoice->get_gateway()) { |
|
| 59 | + wpinv_error_log('Aborting, Invoice was not paid via PayPal', false); |
|
| 60 | + wp_die('Invoice not paid via PayPal', 500); |
|
| 61 | 61 | } |
| 62 | 62 | |
| 63 | - $posted['payment_status'] = isset( $posted['payment_status'] ) ? sanitize_key( strtolower( $posted['payment_status'] ) ) : ''; |
|
| 64 | - $posted['txn_type'] = sanitize_key( strtolower( $posted['txn_type'] ) ); |
|
| 63 | + $posted['payment_status'] = isset($posted['payment_status']) ? sanitize_key(strtolower($posted['payment_status'])) : ''; |
|
| 64 | + $posted['txn_type'] = sanitize_key(strtolower($posted['txn_type'])); |
|
| 65 | 65 | |
| 66 | - wpinv_error_log( 'Payment status:' . $posted['payment_status'], false ); |
|
| 67 | - wpinv_error_log( 'IPN Type:' . $posted['txn_type'], false ); |
|
| 66 | + wpinv_error_log('Payment status:' . $posted['payment_status'], false); |
|
| 67 | + wpinv_error_log('IPN Type:' . $posted['txn_type'], false); |
|
| 68 | 68 | |
| 69 | - if ( method_exists( $this, 'ipn_txn_' . $posted['txn_type'] ) ) { |
|
| 70 | - call_user_func( array( $this, 'ipn_txn_' . $posted['txn_type'] ), $invoice, $posted ); |
|
| 71 | - wpinv_error_log( 'Done processing IPN', false ); |
|
| 72 | - wp_die( 'Processed', 200 ); |
|
| 69 | + if (method_exists($this, 'ipn_txn_' . $posted['txn_type'])) { |
|
| 70 | + call_user_func(array($this, 'ipn_txn_' . $posted['txn_type']), $invoice, $posted); |
|
| 71 | + wpinv_error_log('Done processing IPN', false); |
|
| 72 | + wp_die('Processed', 200); |
|
| 73 | 73 | } |
| 74 | 74 | |
| 75 | - wpinv_error_log( 'Aborting, Unsupported IPN type:' . $posted['txn_type'], false ); |
|
| 76 | - wp_die( 'Unsupported IPN type', 200 ); |
|
| 75 | + wpinv_error_log('Aborting, Unsupported IPN type:' . $posted['txn_type'], false); |
|
| 76 | + wp_die('Unsupported IPN type', 200); |
|
| 77 | 77 | |
| 78 | 78 | } |
| 79 | 79 | |
@@ -83,22 +83,22 @@ discard block |
||
| 83 | 83 | * @param array $posted |
| 84 | 84 | * @return WPInv_Invoice |
| 85 | 85 | */ |
| 86 | - protected function get_ipn_invoice( $posted ) { |
|
| 86 | + protected function get_ipn_invoice($posted) { |
|
| 87 | 87 | |
| 88 | - wpinv_error_log( 'Retrieving PayPal IPN Response Invoice', false ); |
|
| 88 | + wpinv_error_log('Retrieving PayPal IPN Response Invoice', false); |
|
| 89 | 89 | |
| 90 | - if ( ! empty( $posted['custom'] ) ) { |
|
| 91 | - $invoice = new WPInv_Invoice( $posted['custom'] ); |
|
| 90 | + if (!empty($posted['custom'])) { |
|
| 91 | + $invoice = new WPInv_Invoice($posted['custom']); |
|
| 92 | 92 | |
| 93 | - if ( $invoice->exists() ) { |
|
| 94 | - wpinv_error_log( 'Found invoice #' . $invoice->get_number(), false ); |
|
| 93 | + if ($invoice->exists()) { |
|
| 94 | + wpinv_error_log('Found invoice #' . $invoice->get_number(), false); |
|
| 95 | 95 | return $invoice; |
| 96 | 96 | } |
| 97 | 97 | |
| 98 | 98 | } |
| 99 | 99 | |
| 100 | - wpinv_error_log( 'Could not retrieve the associated invoice.', false ); |
|
| 101 | - wp_die( 'Could not retrieve the associated invoice.', 200 ); |
|
| 100 | + wpinv_error_log('Could not retrieve the associated invoice.', false); |
|
| 101 | + wp_die('Could not retrieve the associated invoice.', 200); |
|
| 102 | 102 | } |
| 103 | 103 | |
| 104 | 104 | /** |
@@ -106,14 +106,14 @@ discard block |
||
| 106 | 106 | */ |
| 107 | 107 | protected function validate_ipn() { |
| 108 | 108 | |
| 109 | - wpinv_error_log( 'Validating PayPal IPN response', false ); |
|
| 109 | + wpinv_error_log('Validating PayPal IPN response', false); |
|
| 110 | 110 | |
| 111 | 111 | // Retrieve the associated invoice. |
| 112 | - $posted = wp_unslash( $_POST ); |
|
| 113 | - $invoice = $this->get_ipn_invoice( $posted ); |
|
| 112 | + $posted = wp_unslash($_POST); |
|
| 113 | + $invoice = $this->get_ipn_invoice($posted); |
|
| 114 | 114 | |
| 115 | - if ( $this->gateway->is_sandbox( $invoice ) ) { |
|
| 116 | - wpinv_error_log( $posted, 'Invoice was processed in sandbox hence logging the posted data' ); |
|
| 115 | + if ($this->gateway->is_sandbox($invoice)) { |
|
| 116 | + wpinv_error_log($posted, 'Invoice was processed in sandbox hence logging the posted data'); |
|
| 117 | 117 | } |
| 118 | 118 | |
| 119 | 119 | // Validate the IPN. |
@@ -130,20 +130,20 @@ discard block |
||
| 130 | 130 | ); |
| 131 | 131 | |
| 132 | 132 | // Post back to get a response. |
| 133 | - $response = wp_safe_remote_post( $this->gateway->is_sandbox( $invoice ) ? 'https://www.sandbox.paypal.com/cgi-bin/webscr' : 'https://www.paypal.com/cgi-bin/webscr', $params ); |
|
| 133 | + $response = wp_safe_remote_post($this->gateway->is_sandbox($invoice) ? 'https://www.sandbox.paypal.com/cgi-bin/webscr' : 'https://www.paypal.com/cgi-bin/webscr', $params); |
|
| 134 | 134 | |
| 135 | 135 | // Check to see if the request was valid. |
| 136 | - if ( ! is_wp_error( $response ) && $response['response']['code'] < 300 && strstr( $response['body'], 'VERIFIED' ) ) { |
|
| 137 | - wpinv_error_log( $response['body'], 'Received valid response from PayPal IPN' ); |
|
| 136 | + if (!is_wp_error($response) && $response['response']['code'] < 300 && strstr($response['body'], 'VERIFIED')) { |
|
| 137 | + wpinv_error_log($response['body'], 'Received valid response from PayPal IPN'); |
|
| 138 | 138 | return true; |
| 139 | 139 | } |
| 140 | 140 | |
| 141 | - if ( is_wp_error( $response ) ) { |
|
| 142 | - wpinv_error_log( $response->get_error_message(), 'Received invalid response from PayPal IPN' ); |
|
| 141 | + if (is_wp_error($response)) { |
|
| 142 | + wpinv_error_log($response->get_error_message(), 'Received invalid response from PayPal IPN'); |
|
| 143 | 143 | return false; |
| 144 | 144 | } |
| 145 | 145 | |
| 146 | - wpinv_error_log( $response['body'], 'Received invalid response from PayPal IPN' ); |
|
| 146 | + wpinv_error_log($response['body'], 'Received invalid response from PayPal IPN'); |
|
| 147 | 147 | return false; |
| 148 | 148 | |
| 149 | 149 | } |
@@ -154,17 +154,17 @@ discard block |
||
| 154 | 154 | * @param WPInv_Invoice $invoice Invoice object. |
| 155 | 155 | * @param string $currency currency to validate. |
| 156 | 156 | */ |
| 157 | - protected function validate_ipn_currency( $invoice, $currency ) { |
|
| 157 | + protected function validate_ipn_currency($invoice, $currency) { |
|
| 158 | 158 | |
| 159 | - if ( strtolower( $invoice->get_currency() ) !== strtolower( $currency ) ) { |
|
| 159 | + if (strtolower($invoice->get_currency()) !== strtolower($currency)) { |
|
| 160 | 160 | |
| 161 | 161 | /* translators: %s: currency code. */ |
| 162 | - $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal currencies do not match (code %s).', 'invoicing' ), $currency ) ); |
|
| 162 | + $invoice->update_status('wpi-processing', sprintf(__('Validation error: PayPal currencies do not match (code %s).', 'invoicing'), $currency)); |
|
| 163 | 163 | |
| 164 | - wpinv_error_log( "Currencies do not match: {$currency} instead of {$invoice->get_currency()}", 'IPN Error', __FILE__, __LINE__, true ); |
|
| 164 | + wpinv_error_log("Currencies do not match: {$currency} instead of {$invoice->get_currency()}", 'IPN Error', __FILE__, __LINE__, true); |
|
| 165 | 165 | } |
| 166 | 166 | |
| 167 | - wpinv_error_log( $currency, 'Validated IPN Currency' ); |
|
| 167 | + wpinv_error_log($currency, 'Validated IPN Currency'); |
|
| 168 | 168 | } |
| 169 | 169 | |
| 170 | 170 | /** |
@@ -173,16 +173,16 @@ discard block |
||
| 173 | 173 | * @param WPInv_Invoice $invoice Invoice object. |
| 174 | 174 | * @param float $amount amount to validate. |
| 175 | 175 | */ |
| 176 | - protected function validate_ipn_amount( $invoice, $amount ) { |
|
| 177 | - if ( number_format( $invoice->get_total(), 2, '.', '' ) !== number_format( $amount, 2, '.', '' ) ) { |
|
| 176 | + protected function validate_ipn_amount($invoice, $amount) { |
|
| 177 | + if (number_format($invoice->get_total(), 2, '.', '') !== number_format($amount, 2, '.', '')) { |
|
| 178 | 178 | |
| 179 | 179 | /* translators: %s: Amount. */ |
| 180 | - $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal amounts do not match (gross %s).', 'invoicing' ), $amount ) ); |
|
| 180 | + $invoice->update_status('wpi-processing', sprintf(__('Validation error: PayPal amounts do not match (gross %s).', 'invoicing'), $amount)); |
|
| 181 | 181 | |
| 182 | - wpinv_error_log( "Amounts do not match: {$amount} instead of {$invoice->get_total()}", 'IPN Error', __FILE__, __LINE__, true ); |
|
| 182 | + wpinv_error_log("Amounts do not match: {$amount} instead of {$invoice->get_total()}", 'IPN Error', __FILE__, __LINE__, true); |
|
| 183 | 183 | } |
| 184 | 184 | |
| 185 | - wpinv_error_log( $amount, 'Validated IPN Amount' ); |
|
| 185 | + wpinv_error_log($amount, 'Validated IPN Amount'); |
|
| 186 | 186 | } |
| 187 | 187 | |
| 188 | 188 | /** |
@@ -191,19 +191,19 @@ discard block |
||
| 191 | 191 | * @param WPInv_Invoice $invoice Invoice object. |
| 192 | 192 | * @param string $receiver_email Email to validate. |
| 193 | 193 | */ |
| 194 | - protected function validate_ipn_receiver_email( $invoice, $receiver_email ) { |
|
| 195 | - $paypal_email = wpinv_get_option( 'paypal_email' ); |
|
| 194 | + protected function validate_ipn_receiver_email($invoice, $receiver_email) { |
|
| 195 | + $paypal_email = wpinv_get_option('paypal_email'); |
|
| 196 | 196 | |
| 197 | - if ( strcasecmp( trim( $receiver_email ), trim( $paypal_email ) ) !== 0 ) { |
|
| 198 | - wpinv_record_gateway_error( 'IPN Error', "IPN Response is for another account: {$receiver_email}. Your email is {$paypal_email}" ); |
|
| 197 | + if (strcasecmp(trim($receiver_email), trim($paypal_email)) !== 0) { |
|
| 198 | + wpinv_record_gateway_error('IPN Error', "IPN Response is for another account: {$receiver_email}. Your email is {$paypal_email}"); |
|
| 199 | 199 | |
| 200 | 200 | /* translators: %s: email address . */ |
| 201 | - $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal IPN response from a different email address (%s).', 'invoicing' ), $receiver_email ) ); |
|
| 201 | + $invoice->update_status('wpi-processing', sprintf(__('Validation error: PayPal IPN response from a different email address (%s).', 'invoicing'), $receiver_email)); |
|
| 202 | 202 | |
| 203 | - return wpinv_error_log( "IPN Response is for another account: {$receiver_email}. Your email is {$paypal_email}", 'IPN Error', __FILE__, __LINE__, true ); |
|
| 203 | + return wpinv_error_log("IPN Response is for another account: {$receiver_email}. Your email is {$paypal_email}", 'IPN Error', __FILE__, __LINE__, true); |
|
| 204 | 204 | } |
| 205 | 205 | |
| 206 | - wpinv_error_log( 'Validated PayPal Email', false ); |
|
| 206 | + wpinv_error_log('Validated PayPal Email', false); |
|
| 207 | 207 | } |
| 208 | 208 | |
| 209 | 209 | /** |
@@ -212,68 +212,68 @@ discard block |
||
| 212 | 212 | * @param WPInv_Invoice $invoice Invoice object. |
| 213 | 213 | * @param array $posted Posted data. |
| 214 | 214 | */ |
| 215 | - protected function ipn_txn_web_accept( $invoice, $posted ) { |
|
| 215 | + protected function ipn_txn_web_accept($invoice, $posted) { |
|
| 216 | 216 | |
| 217 | 217 | // Collect payment details |
| 218 | - $payment_status = strtolower( $posted['payment_status'] ); |
|
| 219 | - $business_email = isset( $posted['business'] ) && is_email( $posted['business'] ) ? trim( $posted['business'] ) : trim( $posted['receiver_email'] ); |
|
| 218 | + $payment_status = strtolower($posted['payment_status']); |
|
| 219 | + $business_email = isset($posted['business']) && is_email($posted['business']) ? trim($posted['business']) : trim($posted['receiver_email']); |
|
| 220 | 220 | |
| 221 | - $this->validate_ipn_receiver_email( $invoice, $business_email ); |
|
| 222 | - $this->validate_ipn_currency( $invoice, $posted['mc_currency'] ); |
|
| 221 | + $this->validate_ipn_receiver_email($invoice, $business_email); |
|
| 222 | + $this->validate_ipn_currency($invoice, $posted['mc_currency']); |
|
| 223 | 223 | |
| 224 | 224 | // Update the transaction id. |
| 225 | - if ( ! empty( $posted['txn_id'] ) ) { |
|
| 226 | - $invoice->set_transaction_id( wpinv_clean( $posted['txn_id'] ) ); |
|
| 225 | + if (!empty($posted['txn_id'])) { |
|
| 226 | + $invoice->set_transaction_id(wpinv_clean($posted['txn_id'])); |
|
| 227 | 227 | $invoice->save(); |
| 228 | 228 | } |
| 229 | 229 | |
| 230 | 230 | // Process a refund. |
| 231 | - if ( $payment_status == 'refunded' || $payment_status == 'reversed' ) { |
|
| 231 | + if ($payment_status == 'refunded' || $payment_status == 'reversed') { |
|
| 232 | 232 | |
| 233 | - update_post_meta( $invoice->get_id(), 'refunded_remotely', 1 ); |
|
| 233 | + update_post_meta($invoice->get_id(), 'refunded_remotely', 1); |
|
| 234 | 234 | |
| 235 | - if ( ! $invoice->is_refunded() ) { |
|
| 236 | - $invoice->update_status( 'wpi-refunded', $posted['reason_code'] ); |
|
| 235 | + if (!$invoice->is_refunded()) { |
|
| 236 | + $invoice->update_status('wpi-refunded', $posted['reason_code']); |
|
| 237 | 237 | } |
| 238 | 238 | |
| 239 | - return wpinv_error_log( $posted['reason_code'], false ); |
|
| 239 | + return wpinv_error_log($posted['reason_code'], false); |
|
| 240 | 240 | } |
| 241 | 241 | |
| 242 | 242 | // Process payments. |
| 243 | - if ( $payment_status == 'completed' ) { |
|
| 243 | + if ($payment_status == 'completed') { |
|
| 244 | 244 | |
| 245 | - if ( $invoice->is_paid() && 'wpi_processing' != $invoice->get_status() ) { |
|
| 246 | - return wpinv_error_log( 'Aborting, Invoice #' . $invoice->get_number() . ' is already paid.', false ); |
|
| 245 | + if ($invoice->is_paid() && 'wpi_processing' != $invoice->get_status()) { |
|
| 246 | + return wpinv_error_log('Aborting, Invoice #' . $invoice->get_number() . ' is already paid.', false); |
|
| 247 | 247 | } |
| 248 | 248 | |
| 249 | - $this->validate_ipn_amount( $invoice, $posted['mc_gross'] ); |
|
| 249 | + $this->validate_ipn_amount($invoice, $posted['mc_gross']); |
|
| 250 | 250 | |
| 251 | 251 | $note = ''; |
| 252 | 252 | |
| 253 | - if ( ! empty( $posted['mc_fee'] ) ) { |
|
| 254 | - $note = sprintf( __( 'PayPal Transaction Fee %.', 'invoicing' ), sanitize_text_field( $posted['mc_fee'] ) ); |
|
| 253 | + if (!empty($posted['mc_fee'])) { |
|
| 254 | + $note = sprintf(__('PayPal Transaction Fee %.', 'invoicing'), sanitize_text_field($posted['mc_fee'])); |
|
| 255 | 255 | } |
| 256 | 256 | |
| 257 | - if ( ! empty( $posted['payer_status'] ) ) { |
|
| 258 | - $note = ' ' . sprintf( __( 'Buyer status %.', 'invoicing' ), sanitize_text_field( $posted['payer_status'] ) ); |
|
| 257 | + if (!empty($posted['payer_status'])) { |
|
| 258 | + $note = ' ' . sprintf(__('Buyer status %.', 'invoicing'), sanitize_text_field($posted['payer_status'])); |
|
| 259 | 259 | } |
| 260 | 260 | |
| 261 | - $invoice->mark_paid( ( ! empty( $posted['txn_id'] ) ? sanitize_text_field( $posted['txn_id'] ) : '' ), trim( $note ) ); |
|
| 262 | - return wpinv_error_log( 'Invoice marked as paid.', false ); |
|
| 261 | + $invoice->mark_paid((!empty($posted['txn_id']) ? sanitize_text_field($posted['txn_id']) : ''), trim($note)); |
|
| 262 | + return wpinv_error_log('Invoice marked as paid.', false); |
|
| 263 | 263 | |
| 264 | 264 | } |
| 265 | 265 | |
| 266 | 266 | // Pending payments. |
| 267 | - if ( $payment_status == 'pending' ) { |
|
| 267 | + if ($payment_status == 'pending') { |
|
| 268 | 268 | |
| 269 | 269 | /* translators: %s: pending reason. */ |
| 270 | - $invoice->update_status( 'wpi-onhold', sprintf( __( 'Payment pending (%s).', 'invoicing' ), $posted['pending_reason'] ) ); |
|
| 270 | + $invoice->update_status('wpi-onhold', sprintf(__('Payment pending (%s).', 'invoicing'), $posted['pending_reason'])); |
|
| 271 | 271 | |
| 272 | - return wpinv_error_log( 'Invoice marked as "payment held".', false ); |
|
| 272 | + return wpinv_error_log('Invoice marked as "payment held".', false); |
|
| 273 | 273 | } |
| 274 | 274 | |
| 275 | 275 | /* translators: %s: payment status. */ |
| 276 | - $invoice->update_status( 'wpi-failed', sprintf( __( 'Payment %s via IPN.', 'invoicing' ), sanitize_text_field( $posted['payment_status'] ) ) ); |
|
| 276 | + $invoice->update_status('wpi-failed', sprintf(__('Payment %s via IPN.', 'invoicing'), sanitize_text_field($posted['payment_status']))); |
|
| 277 | 277 | |
| 278 | 278 | } |
| 279 | 279 | |
@@ -283,8 +283,8 @@ discard block |
||
| 283 | 283 | * @param WPInv_Invoice $invoice Invoice object. |
| 284 | 284 | * @param array $posted Posted data. |
| 285 | 285 | */ |
| 286 | - protected function ipn_txn_cart( $invoice, $posted ) { |
|
| 287 | - $this->ipn_txn_web_accept( $invoice, $posted ); |
|
| 286 | + protected function ipn_txn_cart($invoice, $posted) { |
|
| 287 | + $this->ipn_txn_web_accept($invoice, $posted); |
|
| 288 | 288 | } |
| 289 | 289 | |
| 290 | 290 | /** |
@@ -293,40 +293,40 @@ discard block |
||
| 293 | 293 | * @param WPInv_Invoice $invoice Invoice object. |
| 294 | 294 | * @param array $posted Posted data. |
| 295 | 295 | */ |
| 296 | - protected function ipn_txn_subscr_signup( $invoice, $posted ) { |
|
| 296 | + protected function ipn_txn_subscr_signup($invoice, $posted) { |
|
| 297 | 297 | |
| 298 | - wpinv_error_log( 'Processing subscription signup', false ); |
|
| 298 | + wpinv_error_log('Processing subscription signup', false); |
|
| 299 | 299 | |
| 300 | 300 | // Make sure the invoice has a subscription. |
| 301 | - $subscription = getpaid_get_invoice_subscription( $invoice ); |
|
| 301 | + $subscription = getpaid_get_invoice_subscription($invoice); |
|
| 302 | 302 | |
| 303 | - if ( empty( $subscription ) ) { |
|
| 304 | - return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
| 303 | + if (empty($subscription)) { |
|
| 304 | + return wpinv_error_log('Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false); |
|
| 305 | 305 | } |
| 306 | 306 | |
| 307 | 307 | // Validate the IPN. |
| 308 | - $business_email = isset( $posted['business'] ) && is_email( $posted['business'] ) ? trim( $posted['business'] ) : trim( $posted['receiver_email'] ); |
|
| 309 | - $this->validate_ipn_receiver_email( $invoice, $business_email ); |
|
| 310 | - $this->validate_ipn_currency( $invoice, $posted['mc_currency'] ); |
|
| 308 | + $business_email = isset($posted['business']) && is_email($posted['business']) ? trim($posted['business']) : trim($posted['receiver_email']); |
|
| 309 | + $this->validate_ipn_receiver_email($invoice, $business_email); |
|
| 310 | + $this->validate_ipn_currency($invoice, $posted['mc_currency']); |
|
| 311 | 311 | |
| 312 | 312 | // Activate the subscription. |
| 313 | - $duration = strtotime( $subscription->get_expiration() ) - strtotime( $subscription->get_date_created() ); |
|
| 314 | - $subscription->set_date_created( current_time( 'mysql' ) ); |
|
| 315 | - $subscription->set_expiration( date( 'Y-m-d H:i:s', ( current_time( 'timestamp' ) + $duration ) ) ); |
|
| 316 | - $subscription->set_profile_id( sanitize_text_field( $posted['subscr_id'] ) ); |
|
| 313 | + $duration = strtotime($subscription->get_expiration()) - strtotime($subscription->get_date_created()); |
|
| 314 | + $subscription->set_date_created(current_time('mysql')); |
|
| 315 | + $subscription->set_expiration(date('Y-m-d H:i:s', (current_time('timestamp') + $duration))); |
|
| 316 | + $subscription->set_profile_id(sanitize_text_field($posted['subscr_id'])); |
|
| 317 | 317 | $subscription->activate(); |
| 318 | 318 | |
| 319 | 319 | // Set the transaction id. |
| 320 | - if ( ! empty( $posted['txn_id'] ) ) { |
|
| 321 | - $invoice->set_transaction_id( $posted['txn_id'] ); |
|
| 320 | + if (!empty($posted['txn_id'])) { |
|
| 321 | + $invoice->set_transaction_id($posted['txn_id']); |
|
| 322 | 322 | } |
| 323 | 323 | |
| 324 | 324 | // Update the payment status. |
| 325 | 325 | $invoice->mark_paid(); |
| 326 | 326 | |
| 327 | - $invoice->add_note( sprintf( __( 'PayPal Subscription ID: %s', 'invoicing' ) , $posted['subscr_id'] ), false, false, true ); |
|
| 327 | + $invoice->add_note(sprintf(__('PayPal Subscription ID: %s', 'invoicing'), $posted['subscr_id']), false, false, true); |
|
| 328 | 328 | |
| 329 | - wpinv_error_log( 'Subscription started.', false ); |
|
| 329 | + wpinv_error_log('Subscription started.', false); |
|
| 330 | 330 | } |
| 331 | 331 | |
| 332 | 332 | /** |
@@ -335,20 +335,20 @@ discard block |
||
| 335 | 335 | * @param WPInv_Invoice $invoice Invoice object. |
| 336 | 336 | * @param array $posted Posted data. |
| 337 | 337 | */ |
| 338 | - protected function ipn_txn_subscr_payment( $invoice, $posted ) { |
|
| 338 | + protected function ipn_txn_subscr_payment($invoice, $posted) { |
|
| 339 | 339 | |
| 340 | 340 | // Make sure the invoice has a subscription. |
| 341 | - $subscription = wpinv_get_subscription( $invoice ); |
|
| 341 | + $subscription = wpinv_get_subscription($invoice); |
|
| 342 | 342 | |
| 343 | - if ( empty( $subscription ) ) { |
|
| 344 | - return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
| 343 | + if (empty($subscription)) { |
|
| 344 | + return wpinv_error_log('Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false); |
|
| 345 | 345 | } |
| 346 | 346 | |
| 347 | 347 | // Abort if this is the first payment. |
| 348 | - if ( ( $invoice->is_paid() && date( 'Ynd', strtotime( $invoice->get_date_completed() ) ) == date( 'Ynd', strtotime( $posted['payment_date'] ) ) ) || date( 'Ynd', $subscription->get_time_created() ) == date( 'Ynd', strtotime( $posted['payment_date'] ) ) ) { |
|
| 348 | + if (($invoice->is_paid() && date('Ynd', strtotime($invoice->get_date_completed())) == date('Ynd', strtotime($posted['payment_date']))) || date('Ynd', $subscription->get_time_created()) == date('Ynd', strtotime($posted['payment_date']))) { |
|
| 349 | 349 | |
| 350 | - if ( ! empty( $posted['txn_id'] ) ) { |
|
| 351 | - $invoice->set_transaction_id( sanitize_text_field( $posted['txn_id'] ) ); |
|
| 350 | + if (!empty($posted['txn_id'])) { |
|
| 351 | + $invoice->set_transaction_id(sanitize_text_field($posted['txn_id'])); |
|
| 352 | 352 | $invoice->mark_paid(); |
| 353 | 353 | $invoice->save(); |
| 354 | 354 | } |
@@ -356,11 +356,11 @@ discard block |
||
| 356 | 356 | return; |
| 357 | 357 | } |
| 358 | 358 | |
| 359 | - wpinv_error_log( 'Processing subscription renewal payment for the invoice ' . $invoice->get_id(), false ); |
|
| 359 | + wpinv_error_log('Processing subscription renewal payment for the invoice ' . $invoice->get_id(), false); |
|
| 360 | 360 | |
| 361 | 361 | // Abort if the payment is already recorded. |
| 362 | - if ( wpinv_get_id_by_transaction_id( $posted['txn_id'] ) ) { |
|
| 363 | - return wpinv_error_log( 'Aborting, Transaction ' . $posted['txn_id'] .' has already been processed', false ); |
|
| 362 | + if (wpinv_get_id_by_transaction_id($posted['txn_id'])) { |
|
| 363 | + return wpinv_error_log('Aborting, Transaction ' . $posted['txn_id'] . ' has already been processed', false); |
|
| 364 | 364 | } |
| 365 | 365 | |
| 366 | 366 | $args = array( |
@@ -368,17 +368,17 @@ discard block |
||
| 368 | 368 | 'gateway' => $this->id, |
| 369 | 369 | ); |
| 370 | 370 | |
| 371 | - $invoice = wpinv_get_invoice( $subscription->add_payment( $args ) ); |
|
| 371 | + $invoice = wpinv_get_invoice($subscription->add_payment($args)); |
|
| 372 | 372 | |
| 373 | - if ( empty( $invoice ) ) { |
|
| 373 | + if (empty($invoice)) { |
|
| 374 | 374 | return; |
| 375 | 375 | } |
| 376 | 376 | |
| 377 | - $invoice->add_note( wp_sprintf( __( 'PayPal Transaction ID: %s', 'invoicing' ) , $posted['txn_id'] ), false, false, true ); |
|
| 378 | - $invoice->add_note( wp_sprintf( __( 'PayPal Subscription ID: %s', 'invoicing' ) , $posted['subscr_id'] ), false, false, true ); |
|
| 377 | + $invoice->add_note(wp_sprintf(__('PayPal Transaction ID: %s', 'invoicing'), $posted['txn_id']), false, false, true); |
|
| 378 | + $invoice->add_note(wp_sprintf(__('PayPal Subscription ID: %s', 'invoicing'), $posted['subscr_id']), false, false, true); |
|
| 379 | 379 | |
| 380 | 380 | $subscription->renew(); |
| 381 | - wpinv_error_log( 'Subscription renewed.', false ); |
|
| 381 | + wpinv_error_log('Subscription renewed.', false); |
|
| 382 | 382 | |
| 383 | 383 | } |
| 384 | 384 | |
@@ -387,18 +387,18 @@ discard block |
||
| 387 | 387 | * |
| 388 | 388 | * @param WPInv_Invoice $invoice Invoice object. |
| 389 | 389 | */ |
| 390 | - protected function ipn_txn_subscr_cancel( $invoice ) { |
|
| 390 | + protected function ipn_txn_subscr_cancel($invoice) { |
|
| 391 | 391 | |
| 392 | 392 | // Make sure the invoice has a subscription. |
| 393 | - $subscription = wpinv_get_subscription( $invoice ); |
|
| 393 | + $subscription = wpinv_get_subscription($invoice); |
|
| 394 | 394 | |
| 395 | - if ( empty( $subscription ) ) { |
|
| 396 | - return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false); |
|
| 395 | + if (empty($subscription)) { |
|
| 396 | + return wpinv_error_log('Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false); |
|
| 397 | 397 | } |
| 398 | 398 | |
| 399 | - wpinv_error_log( 'Processing subscription cancellation for the invoice ' . $invoice->get_id(), false ); |
|
| 399 | + wpinv_error_log('Processing subscription cancellation for the invoice ' . $invoice->get_id(), false); |
|
| 400 | 400 | $subscription->cancel(); |
| 401 | - wpinv_error_log( 'Subscription cancelled.', false ); |
|
| 401 | + wpinv_error_log('Subscription cancelled.', false); |
|
| 402 | 402 | |
| 403 | 403 | } |
| 404 | 404 | |
@@ -408,18 +408,18 @@ discard block |
||
| 408 | 408 | * @param WPInv_Invoice $invoice Invoice object. |
| 409 | 409 | * @param array $posted Posted data. |
| 410 | 410 | */ |
| 411 | - protected function ipn_txn_subscr_eot( $invoice ) { |
|
| 411 | + protected function ipn_txn_subscr_eot($invoice) { |
|
| 412 | 412 | |
| 413 | 413 | // Make sure the invoice has a subscription. |
| 414 | - $subscription = wpinv_get_subscription( $invoice ); |
|
| 414 | + $subscription = wpinv_get_subscription($invoice); |
|
| 415 | 415 | |
| 416 | - if ( empty( $subscription ) ) { |
|
| 417 | - return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
| 416 | + if (empty($subscription)) { |
|
| 417 | + return wpinv_error_log('Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false); |
|
| 418 | 418 | } |
| 419 | 419 | |
| 420 | - wpinv_error_log( 'Processing subscription end of life for the invoice ' . $invoice->get_id(), false ); |
|
| 420 | + wpinv_error_log('Processing subscription end of life for the invoice ' . $invoice->get_id(), false); |
|
| 421 | 421 | $subscription->complete(); |
| 422 | - wpinv_error_log( 'Subscription completed.', false ); |
|
| 422 | + wpinv_error_log('Subscription completed.', false); |
|
| 423 | 423 | |
| 424 | 424 | } |
| 425 | 425 | |
@@ -429,18 +429,18 @@ discard block |
||
| 429 | 429 | * @param WPInv_Invoice $invoice Invoice object. |
| 430 | 430 | * @param array $posted Posted data. |
| 431 | 431 | */ |
| 432 | - protected function ipn_txn_subscr_failed( $invoice ) { |
|
| 432 | + protected function ipn_txn_subscr_failed($invoice) { |
|
| 433 | 433 | |
| 434 | 434 | // Make sure the invoice has a subscription. |
| 435 | - $subscription = wpinv_get_subscription( $invoice ); |
|
| 435 | + $subscription = wpinv_get_subscription($invoice); |
|
| 436 | 436 | |
| 437 | - if ( empty( $subscription ) ) { |
|
| 438 | - return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
| 437 | + if (empty($subscription)) { |
|
| 438 | + return wpinv_error_log('Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false); |
|
| 439 | 439 | } |
| 440 | 440 | |
| 441 | - wpinv_error_log( 'Processing subscription payment failure for the invoice ' . $invoice->get_id(), false ); |
|
| 441 | + wpinv_error_log('Processing subscription payment failure for the invoice ' . $invoice->get_id(), false); |
|
| 442 | 442 | $subscription->failing(); |
| 443 | - wpinv_error_log( 'Subscription marked as failing.', false ); |
|
| 443 | + wpinv_error_log('Subscription marked as failing.', false); |
|
| 444 | 444 | |
| 445 | 445 | } |
| 446 | 446 | |
@@ -450,18 +450,18 @@ discard block |
||
| 450 | 450 | * @param WPInv_Invoice $invoice Invoice object. |
| 451 | 451 | * @param array $posted Posted data. |
| 452 | 452 | */ |
| 453 | - protected function ipn_txn_recurring_payment_suspended_due_to_max_failed_payment( $invoice ) { |
|
| 453 | + protected function ipn_txn_recurring_payment_suspended_due_to_max_failed_payment($invoice) { |
|
| 454 | 454 | |
| 455 | 455 | // Make sure the invoice has a subscription. |
| 456 | - $subscription = wpinv_get_subscription( $invoice ); |
|
| 456 | + $subscription = wpinv_get_subscription($invoice); |
|
| 457 | 457 | |
| 458 | - if ( empty( $subscription ) ) { |
|
| 459 | - return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
| 458 | + if (empty($subscription)) { |
|
| 459 | + return wpinv_error_log('Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false); |
|
| 460 | 460 | } |
| 461 | 461 | |
| 462 | - wpinv_error_log( 'Processing subscription cancellation due to max failed payment for the invoice ' . $invoice->get_id(), false ); |
|
| 462 | + wpinv_error_log('Processing subscription cancellation due to max failed payment for the invoice ' . $invoice->get_id(), false); |
|
| 463 | 463 | $subscription->cancel(); |
| 464 | - wpinv_error_log( 'Subscription cancelled.', false ); |
|
| 464 | + wpinv_error_log('Subscription cancelled.', false); |
|
| 465 | 465 | } |
| 466 | 466 | |
| 467 | 467 | } |
@@ -13,288 +13,288 @@ |
||
| 13 | 13 | class GetPaid_Subscription_Notification_Emails { |
| 14 | 14 | |
| 15 | 15 | /** |
| 16 | - * The array of subscription email actions. |
|
| 17 | - * |
|
| 18 | - * @param array |
|
| 19 | - */ |
|
| 20 | - public $subscription_actions; |
|
| 16 | + * The array of subscription email actions. |
|
| 17 | + * |
|
| 18 | + * @param array |
|
| 19 | + */ |
|
| 20 | + public $subscription_actions; |
|
| 21 | 21 | |
| 22 | 22 | /** |
| 23 | - * Class constructor |
|
| 23 | + * Class constructor |
|
| 24 | 24 | * |
| 25 | - */ |
|
| 26 | - public function __construct() { |
|
| 27 | - |
|
| 28 | - $this->subscription_actions = apply_filters( |
|
| 29 | - 'getpaid_notification_email_subscription_triggers', |
|
| 30 | - array( |
|
| 31 | - 'getpaid_subscription_trialling' => 'subscription_trial', |
|
| 32 | - 'getpaid_subscription_cancelled' => 'subscription_cancelled', |
|
| 33 | - 'getpaid_subscription_expired' => 'subscription_expired', |
|
| 34 | - 'getpaid_subscription_completed' => 'subscription_complete', |
|
| 35 | - 'getpaid_daily_maintenance' => 'renewal_reminder', |
|
| 36 | - ) |
|
| 37 | - ); |
|
| 38 | - |
|
| 39 | - $this->init_hooks(); |
|
| 25 | + */ |
|
| 26 | + public function __construct() { |
|
| 27 | + |
|
| 28 | + $this->subscription_actions = apply_filters( |
|
| 29 | + 'getpaid_notification_email_subscription_triggers', |
|
| 30 | + array( |
|
| 31 | + 'getpaid_subscription_trialling' => 'subscription_trial', |
|
| 32 | + 'getpaid_subscription_cancelled' => 'subscription_cancelled', |
|
| 33 | + 'getpaid_subscription_expired' => 'subscription_expired', |
|
| 34 | + 'getpaid_subscription_completed' => 'subscription_complete', |
|
| 35 | + 'getpaid_daily_maintenance' => 'renewal_reminder', |
|
| 36 | + ) |
|
| 37 | + ); |
|
| 38 | + |
|
| 39 | + $this->init_hooks(); |
|
| 40 | 40 | |
| 41 | 41 | } |
| 42 | 42 | |
| 43 | 43 | /** |
| 44 | - * Registers email hooks. |
|
| 45 | - */ |
|
| 46 | - public function init_hooks() { |
|
| 47 | - |
|
| 48 | - add_filter( 'getpaid_get_email_merge_tags', array( $this, 'subscription_merge_tags' ), 10, 2 ); |
|
| 49 | - foreach ( $this->subscription_actions as $hook => $email_type ) { |
|
| 50 | - |
|
| 51 | - $email = new GetPaid_Notification_Email( $email_type ); |
|
| 52 | - |
|
| 53 | - if ( ! $email->is_active() ) { |
|
| 54 | - continue; |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - if ( method_exists( $this, $email_type ) ) { |
|
| 58 | - add_action( $hook, array( $this, $email_type ), 100, 2 ); |
|
| 59 | - continue; |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - do_action( 'getpaid_subscription_notification_email_register_hook', $email_type, $hook ); |
|
| 63 | - |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * Filters subscription merge tags. |
|
| 70 | - * |
|
| 71 | - * @param array $merge_tags |
|
| 72 | - * @param mixed|WPInv_Invoice|WPInv_Subscription $object |
|
| 73 | - */ |
|
| 74 | - public function subscription_merge_tags( $merge_tags, $object ) { |
|
| 75 | - |
|
| 76 | - if ( is_a( $object, 'WPInv_Subscription' ) ) { |
|
| 77 | - $merge_tags = array_merge( |
|
| 78 | - $merge_tags, |
|
| 79 | - $this->get_subscription_merge_tags( $object ) |
|
| 80 | - ); |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - return $merge_tags; |
|
| 84 | - |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * Generates subscription merge tags. |
|
| 89 | - * |
|
| 90 | - * @param WPInv_Subscription $subscription |
|
| 91 | - * @return array |
|
| 92 | - */ |
|
| 93 | - public function get_subscription_merge_tags( $subscription ) { |
|
| 94 | - |
|
| 95 | - // Abort if it does not exist. |
|
| 96 | - if ( ! $subscription->get_id() ) { |
|
| 97 | - return array(); |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - $invoice = $subscription->get_parent_invoice(); |
|
| 101 | - return array( |
|
| 102 | - '{subscription_renewal_date}' => getpaid_format_date_value( $subscription->get_next_renewal_date(), __( 'Never', 'invoicing' ) ), |
|
| 103 | - '{subscription_created}' => getpaid_format_date_value( $subscription->get_date_created() ), |
|
| 104 | - '{subscription_status}' => sanitize_text_field( $subscription->get_status_label() ), |
|
| 105 | - '{subscription_profile_id}' => sanitize_text_field( $subscription->get_profile_id() ), |
|
| 106 | - '{subscription_id}' => absint( $subscription->get_id() ), |
|
| 107 | - '{subscription_recurring_amount}' => sanitize_text_field( wpinv_price( $subscription->get_recurring_amount(), $invoice->get_currency() ) ), |
|
| 108 | - '{subscription_initial_amount}' => sanitize_text_field( wpinv_price( $subscription->get_initial_amount(), $invoice->get_currency() ) ), |
|
| 109 | - '{subscription_recurring_period}' => getpaid_get_subscription_period_label( $subscription->get_period(), $subscription->get_frequency(), '' ), |
|
| 110 | - '{subscription_bill_times}' => $subscription->get_bill_times(), |
|
| 111 | - '{subscription_url}' => esc_url( $subscription->get_view_url() ), |
|
| 112 | - ); |
|
| 113 | - |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * Checks if we should send a notification for a subscription. |
|
| 118 | - * |
|
| 119 | - * @param WPInv_Invoice $invoice |
|
| 120 | - * @return bool |
|
| 121 | - */ |
|
| 122 | - public function should_send_notification( $invoice ) { |
|
| 123 | - return 0 != $invoice->get_id(); |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * Returns notification recipients. |
|
| 128 | - * |
|
| 129 | - * @param WPInv_Invoice $invoice |
|
| 130 | - * @return array |
|
| 131 | - */ |
|
| 132 | - public function get_recipients( $invoice ) { |
|
| 133 | - $recipients = array( $invoice->get_email() ); |
|
| 134 | - |
|
| 135 | - $cc = $invoice->get_email_cc(); |
|
| 136 | - |
|
| 137 | - if ( ! empty( $cc ) ) { |
|
| 138 | - $cc = array_map( 'sanitize_email', wpinv_parse_list( $cc ) ); |
|
| 139 | - $recipients = array_filter( array_unique( array_merge( $recipients, $cc ) ) ); |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - return $recipients; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * Helper function to send an email. |
|
| 147 | - * |
|
| 148 | - * @param WPInv_Subscription $subscription |
|
| 149 | - * @param GetPaid_Notification_Email $email |
|
| 150 | - * @param string $type |
|
| 151 | - * @param array $extra_args Extra template args. |
|
| 152 | - */ |
|
| 153 | - public function send_email( $subscription, $email, $type, $extra_args = array() ) { |
|
| 154 | - |
|
| 155 | - // Abort in case the parent invoice does not exist. |
|
| 156 | - $invoice = $subscription->get_parent_invoice(); |
|
| 157 | - if ( ! $this->should_send_notification( $invoice ) ) { |
|
| 158 | - return; |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - if ( apply_filters( 'getpaid_skip_subscription_email', false, $type, $subscription ) ) { |
|
| 162 | - return; |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - do_action( 'getpaid_before_send_subscription_notification', $type, $subscription, $email ); |
|
| 166 | - |
|
| 167 | - $recipients = $this->get_recipients( $invoice ); |
|
| 168 | - $mailer = new GetPaid_Notification_Email_Sender(); |
|
| 169 | - $merge_tags = $email->get_merge_tags(); |
|
| 170 | - $content = $email->get_content( $merge_tags, $extra_args ); |
|
| 171 | - $subject = $email->add_merge_tags( $email->get_subject(), $merge_tags ); |
|
| 172 | - $attachments = $email->get_attachments(); |
|
| 173 | - |
|
| 174 | - $result = $mailer->send( |
|
| 175 | - apply_filters( 'getpaid_subscription_email_recipients', wpinv_parse_list( $recipients ), $email ), |
|
| 176 | - $subject, |
|
| 177 | - $content, |
|
| 178 | - $attachments |
|
| 179 | - ); |
|
| 180 | - |
|
| 181 | - // Maybe send a copy to the admin. |
|
| 182 | - if ( $email->include_admin_bcc() ) { |
|
| 183 | - $mailer->send( |
|
| 184 | - wpinv_get_admin_email(), |
|
| 185 | - $subject . __( ' - ADMIN BCC COPY', 'invoicing' ), |
|
| 186 | - $content, |
|
| 187 | - $attachments |
|
| 188 | - ); |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - if ( $result ) { |
|
| 192 | - $subscription->get_parent_invoice()->add_note( sprintf( __( 'Successfully sent %s notification email.', 'invoicing' ), sanitize_key( $type ) ), false, false, true ); |
|
| 193 | - } else { |
|
| 194 | - $subscription->get_parent_invoice()->add_note( sprintf( __( 'Failed sending %s notification email.', 'invoicing' ), sanitize_key( $type ) ), false, false, true ); |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - do_action( 'getpaid_after_send_subscription_notification', $type, $subscription, $email ); |
|
| 198 | - |
|
| 199 | - } |
|
| 44 | + * Registers email hooks. |
|
| 45 | + */ |
|
| 46 | + public function init_hooks() { |
|
| 47 | + |
|
| 48 | + add_filter( 'getpaid_get_email_merge_tags', array( $this, 'subscription_merge_tags' ), 10, 2 ); |
|
| 49 | + foreach ( $this->subscription_actions as $hook => $email_type ) { |
|
| 50 | + |
|
| 51 | + $email = new GetPaid_Notification_Email( $email_type ); |
|
| 52 | + |
|
| 53 | + if ( ! $email->is_active() ) { |
|
| 54 | + continue; |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + if ( method_exists( $this, $email_type ) ) { |
|
| 58 | + add_action( $hook, array( $this, $email_type ), 100, 2 ); |
|
| 59 | + continue; |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + do_action( 'getpaid_subscription_notification_email_register_hook', $email_type, $hook ); |
|
| 63 | + |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + } |
|
| 200 | 67 | |
| 201 | 68 | /** |
| 202 | - * Sends a new trial notification. |
|
| 203 | - * |
|
| 204 | - * @param WPInv_Subscription $subscription |
|
| 205 | - */ |
|
| 206 | - public function subscription_trial( $subscription ) { |
|
| 69 | + * Filters subscription merge tags. |
|
| 70 | + * |
|
| 71 | + * @param array $merge_tags |
|
| 72 | + * @param mixed|WPInv_Invoice|WPInv_Subscription $object |
|
| 73 | + */ |
|
| 74 | + public function subscription_merge_tags( $merge_tags, $object ) { |
|
| 207 | 75 | |
| 208 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 209 | - $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 76 | + if ( is_a( $object, 'WPInv_Subscription' ) ) { |
|
| 77 | + $merge_tags = array_merge( |
|
| 78 | + $merge_tags, |
|
| 79 | + $this->get_subscription_merge_tags( $object ) |
|
| 80 | + ); |
|
| 81 | + } |
|
| 210 | 82 | |
| 211 | - } |
|
| 83 | + return $merge_tags; |
|
| 212 | 84 | |
| 213 | - /** |
|
| 214 | - * Sends a cancelled subscription notification. |
|
| 215 | - * |
|
| 216 | - * @param WPInv_Subscription $subscription |
|
| 217 | - */ |
|
| 218 | - public function subscription_cancelled( $subscription ) { |
|
| 85 | + } |
|
| 219 | 86 | |
| 220 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 221 | - $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 87 | + /** |
|
| 88 | + * Generates subscription merge tags. |
|
| 89 | + * |
|
| 90 | + * @param WPInv_Subscription $subscription |
|
| 91 | + * @return array |
|
| 92 | + */ |
|
| 93 | + public function get_subscription_merge_tags( $subscription ) { |
|
| 94 | + |
|
| 95 | + // Abort if it does not exist. |
|
| 96 | + if ( ! $subscription->get_id() ) { |
|
| 97 | + return array(); |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + $invoice = $subscription->get_parent_invoice(); |
|
| 101 | + return array( |
|
| 102 | + '{subscription_renewal_date}' => getpaid_format_date_value( $subscription->get_next_renewal_date(), __( 'Never', 'invoicing' ) ), |
|
| 103 | + '{subscription_created}' => getpaid_format_date_value( $subscription->get_date_created() ), |
|
| 104 | + '{subscription_status}' => sanitize_text_field( $subscription->get_status_label() ), |
|
| 105 | + '{subscription_profile_id}' => sanitize_text_field( $subscription->get_profile_id() ), |
|
| 106 | + '{subscription_id}' => absint( $subscription->get_id() ), |
|
| 107 | + '{subscription_recurring_amount}' => sanitize_text_field( wpinv_price( $subscription->get_recurring_amount(), $invoice->get_currency() ) ), |
|
| 108 | + '{subscription_initial_amount}' => sanitize_text_field( wpinv_price( $subscription->get_initial_amount(), $invoice->get_currency() ) ), |
|
| 109 | + '{subscription_recurring_period}' => getpaid_get_subscription_period_label( $subscription->get_period(), $subscription->get_frequency(), '' ), |
|
| 110 | + '{subscription_bill_times}' => $subscription->get_bill_times(), |
|
| 111 | + '{subscription_url}' => esc_url( $subscription->get_view_url() ), |
|
| 112 | + ); |
|
| 222 | 113 | |
| 223 | - } |
|
| 114 | + } |
|
| 224 | 115 | |
| 225 | - /** |
|
| 226 | - * Sends a subscription expired notification. |
|
| 227 | - * |
|
| 228 | - * @param WPInv_Subscription $subscription |
|
| 229 | - */ |
|
| 230 | - public function subscription_expired( $subscription ) { |
|
| 116 | + /** |
|
| 117 | + * Checks if we should send a notification for a subscription. |
|
| 118 | + * |
|
| 119 | + * @param WPInv_Invoice $invoice |
|
| 120 | + * @return bool |
|
| 121 | + */ |
|
| 122 | + public function should_send_notification( $invoice ) { |
|
| 123 | + return 0 != $invoice->get_id(); |
|
| 124 | + } |
|
| 231 | 125 | |
| 232 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 233 | - $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 126 | + /** |
|
| 127 | + * Returns notification recipients. |
|
| 128 | + * |
|
| 129 | + * @param WPInv_Invoice $invoice |
|
| 130 | + * @return array |
|
| 131 | + */ |
|
| 132 | + public function get_recipients( $invoice ) { |
|
| 133 | + $recipients = array( $invoice->get_email() ); |
|
| 234 | 134 | |
| 235 | - } |
|
| 135 | + $cc = $invoice->get_email_cc(); |
|
| 236 | 136 | |
| 237 | - /** |
|
| 238 | - * Sends a completed subscription notification. |
|
| 239 | - * |
|
| 240 | - * @param WPInv_Subscription $subscription |
|
| 241 | - */ |
|
| 242 | - public function subscription_complete( $subscription ) { |
|
| 137 | + if ( ! empty( $cc ) ) { |
|
| 138 | + $cc = array_map( 'sanitize_email', wpinv_parse_list( $cc ) ); |
|
| 139 | + $recipients = array_filter( array_unique( array_merge( $recipients, $cc ) ) ); |
|
| 140 | + } |
|
| 243 | 141 | |
| 244 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 245 | - $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 142 | + return $recipients; |
|
| 143 | + } |
|
| 246 | 144 | |
| 247 | - } |
|
| 145 | + /** |
|
| 146 | + * Helper function to send an email. |
|
| 147 | + * |
|
| 148 | + * @param WPInv_Subscription $subscription |
|
| 149 | + * @param GetPaid_Notification_Email $email |
|
| 150 | + * @param string $type |
|
| 151 | + * @param array $extra_args Extra template args. |
|
| 152 | + */ |
|
| 153 | + public function send_email( $subscription, $email, $type, $extra_args = array() ) { |
|
| 154 | + |
|
| 155 | + // Abort in case the parent invoice does not exist. |
|
| 156 | + $invoice = $subscription->get_parent_invoice(); |
|
| 157 | + if ( ! $this->should_send_notification( $invoice ) ) { |
|
| 158 | + return; |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + if ( apply_filters( 'getpaid_skip_subscription_email', false, $type, $subscription ) ) { |
|
| 162 | + return; |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + do_action( 'getpaid_before_send_subscription_notification', $type, $subscription, $email ); |
|
| 166 | + |
|
| 167 | + $recipients = $this->get_recipients( $invoice ); |
|
| 168 | + $mailer = new GetPaid_Notification_Email_Sender(); |
|
| 169 | + $merge_tags = $email->get_merge_tags(); |
|
| 170 | + $content = $email->get_content( $merge_tags, $extra_args ); |
|
| 171 | + $subject = $email->add_merge_tags( $email->get_subject(), $merge_tags ); |
|
| 172 | + $attachments = $email->get_attachments(); |
|
| 173 | + |
|
| 174 | + $result = $mailer->send( |
|
| 175 | + apply_filters( 'getpaid_subscription_email_recipients', wpinv_parse_list( $recipients ), $email ), |
|
| 176 | + $subject, |
|
| 177 | + $content, |
|
| 178 | + $attachments |
|
| 179 | + ); |
|
| 180 | + |
|
| 181 | + // Maybe send a copy to the admin. |
|
| 182 | + if ( $email->include_admin_bcc() ) { |
|
| 183 | + $mailer->send( |
|
| 184 | + wpinv_get_admin_email(), |
|
| 185 | + $subject . __( ' - ADMIN BCC COPY', 'invoicing' ), |
|
| 186 | + $content, |
|
| 187 | + $attachments |
|
| 188 | + ); |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + if ( $result ) { |
|
| 192 | + $subscription->get_parent_invoice()->add_note( sprintf( __( 'Successfully sent %s notification email.', 'invoicing' ), sanitize_key( $type ) ), false, false, true ); |
|
| 193 | + } else { |
|
| 194 | + $subscription->get_parent_invoice()->add_note( sprintf( __( 'Failed sending %s notification email.', 'invoicing' ), sanitize_key( $type ) ), false, false, true ); |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + do_action( 'getpaid_after_send_subscription_notification', $type, $subscription, $email ); |
|
| 248 | 198 | |
| 249 | - /** |
|
| 250 | - * Sends a subscription renewal reminder notification. |
|
| 251 | - * |
|
| 252 | - */ |
|
| 253 | - public function renewal_reminder() { |
|
| 199 | + } |
|
| 254 | 200 | |
| 255 | - $email = new GetPaid_Notification_Email( __FUNCTION__ ); |
|
| 201 | + /** |
|
| 202 | + * Sends a new trial notification. |
|
| 203 | + * |
|
| 204 | + * @param WPInv_Subscription $subscription |
|
| 205 | + */ |
|
| 206 | + public function subscription_trial( $subscription ) { |
|
| 256 | 207 | |
| 257 | - // Fetch reminder days. |
|
| 258 | - $reminder_days = array_unique( wp_parse_id_list( $email->get_option( 'days' ) ) ); |
|
| 208 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 209 | + $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 259 | 210 | |
| 260 | - // Abort if non is set. |
|
| 261 | - if ( empty( $reminder_days ) ) { |
|
| 262 | - return; |
|
| 263 | - } |
|
| 211 | + } |
|
| 264 | 212 | |
| 265 | - // Fetch matching subscriptions. |
|
| 213 | + /** |
|
| 214 | + * Sends a cancelled subscription notification. |
|
| 215 | + * |
|
| 216 | + * @param WPInv_Subscription $subscription |
|
| 217 | + */ |
|
| 218 | + public function subscription_cancelled( $subscription ) { |
|
| 219 | + |
|
| 220 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 221 | + $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 222 | + |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + /** |
|
| 226 | + * Sends a subscription expired notification. |
|
| 227 | + * |
|
| 228 | + * @param WPInv_Subscription $subscription |
|
| 229 | + */ |
|
| 230 | + public function subscription_expired( $subscription ) { |
|
| 231 | + |
|
| 232 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 233 | + $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 234 | + |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + /** |
|
| 238 | + * Sends a completed subscription notification. |
|
| 239 | + * |
|
| 240 | + * @param WPInv_Subscription $subscription |
|
| 241 | + */ |
|
| 242 | + public function subscription_complete( $subscription ) { |
|
| 243 | + |
|
| 244 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 245 | + $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 246 | + |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + /** |
|
| 250 | + * Sends a subscription renewal reminder notification. |
|
| 251 | + * |
|
| 252 | + */ |
|
| 253 | + public function renewal_reminder() { |
|
| 254 | + |
|
| 255 | + $email = new GetPaid_Notification_Email( __FUNCTION__ ); |
|
| 256 | + |
|
| 257 | + // Fetch reminder days. |
|
| 258 | + $reminder_days = array_unique( wp_parse_id_list( $email->get_option( 'days' ) ) ); |
|
| 259 | + |
|
| 260 | + // Abort if non is set. |
|
| 261 | + if ( empty( $reminder_days ) ) { |
|
| 262 | + return; |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + // Fetch matching subscriptions. |
|
| 266 | 266 | $args = array( |
| 267 | 267 | 'number' => -1, |
| 268 | - 'count_total' => false, |
|
| 269 | - 'status' => 'trialling active', |
|
| 268 | + 'count_total' => false, |
|
| 269 | + 'status' => 'trialling active', |
|
| 270 | 270 | 'date_expires_query' => array( |
| 271 | - 'relation' => 'OR' |
|
| 271 | + 'relation' => 'OR' |
|
| 272 | 272 | ), |
| 273 | - ); |
|
| 273 | + ); |
|
| 274 | 274 | |
| 275 | - foreach ( $reminder_days as $days ) { |
|
| 276 | - $date = date_parse( date( 'Y-m-d', strtotime( "+$days days", current_time( 'timestamp' ) ) ) ); |
|
| 275 | + foreach ( $reminder_days as $days ) { |
|
| 276 | + $date = date_parse( date( 'Y-m-d', strtotime( "+$days days", current_time( 'timestamp' ) ) ) ); |
|
| 277 | 277 | |
| 278 | - $args['date_expires_query'][] = array( |
|
| 279 | - 'year' => $date['year'], |
|
| 280 | - 'month' => $date['month'], |
|
| 281 | - 'day' => $date['day'], |
|
| 282 | - ); |
|
| 278 | + $args['date_expires_query'][] = array( |
|
| 279 | + 'year' => $date['year'], |
|
| 280 | + 'month' => $date['month'], |
|
| 281 | + 'day' => $date['day'], |
|
| 282 | + ); |
|
| 283 | 283 | |
| 284 | - } |
|
| 284 | + } |
|
| 285 | 285 | |
| 286 | - $subscriptions = new GetPaid_Subscriptions_Query( $args ); |
|
| 286 | + $subscriptions = new GetPaid_Subscriptions_Query( $args ); |
|
| 287 | 287 | |
| 288 | 288 | foreach ( $subscriptions as $subscription ) { |
| 289 | 289 | |
| 290 | - // Skip packages. |
|
| 291 | - if ( get_post_meta( $subscription->get_product_id(), '_wpinv_type', true ) != 'package' ) { |
|
| 292 | - $email->object = $subscription; |
|
| 293 | - $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 294 | - } |
|
| 290 | + // Skip packages. |
|
| 291 | + if ( get_post_meta( $subscription->get_product_id(), '_wpinv_type', true ) != 'package' ) { |
|
| 292 | + $email->object = $subscription; |
|
| 293 | + $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 294 | + } |
|
| 295 | 295 | |
| 296 | - } |
|
| 296 | + } |
|
| 297 | 297 | |
| 298 | - } |
|
| 298 | + } |
|
| 299 | 299 | |
| 300 | 300 | } |
@@ -4,7 +4,7 @@ discard block |
||
| 4 | 4 | * |
| 5 | 5 | */ |
| 6 | 6 | |
| 7 | -defined( 'ABSPATH' ) || exit; |
|
| 7 | +defined('ABSPATH') || exit; |
|
| 8 | 8 | |
| 9 | 9 | /** |
| 10 | 10 | * This class handles subscription notificaiton emails. |
@@ -45,21 +45,21 @@ discard block |
||
| 45 | 45 | */ |
| 46 | 46 | public function init_hooks() { |
| 47 | 47 | |
| 48 | - add_filter( 'getpaid_get_email_merge_tags', array( $this, 'subscription_merge_tags' ), 10, 2 ); |
|
| 49 | - foreach ( $this->subscription_actions as $hook => $email_type ) { |
|
| 48 | + add_filter('getpaid_get_email_merge_tags', array($this, 'subscription_merge_tags'), 10, 2); |
|
| 49 | + foreach ($this->subscription_actions as $hook => $email_type) { |
|
| 50 | 50 | |
| 51 | - $email = new GetPaid_Notification_Email( $email_type ); |
|
| 51 | + $email = new GetPaid_Notification_Email($email_type); |
|
| 52 | 52 | |
| 53 | - if ( ! $email->is_active() ) { |
|
| 53 | + if (!$email->is_active()) { |
|
| 54 | 54 | continue; |
| 55 | 55 | } |
| 56 | 56 | |
| 57 | - if ( method_exists( $this, $email_type ) ) { |
|
| 58 | - add_action( $hook, array( $this, $email_type ), 100, 2 ); |
|
| 57 | + if (method_exists($this, $email_type)) { |
|
| 58 | + add_action($hook, array($this, $email_type), 100, 2); |
|
| 59 | 59 | continue; |
| 60 | 60 | } |
| 61 | 61 | |
| 62 | - do_action( 'getpaid_subscription_notification_email_register_hook', $email_type, $hook ); |
|
| 62 | + do_action('getpaid_subscription_notification_email_register_hook', $email_type, $hook); |
|
| 63 | 63 | |
| 64 | 64 | } |
| 65 | 65 | |
@@ -71,12 +71,12 @@ discard block |
||
| 71 | 71 | * @param array $merge_tags |
| 72 | 72 | * @param mixed|WPInv_Invoice|WPInv_Subscription $object |
| 73 | 73 | */ |
| 74 | - public function subscription_merge_tags( $merge_tags, $object ) { |
|
| 74 | + public function subscription_merge_tags($merge_tags, $object) { |
|
| 75 | 75 | |
| 76 | - if ( is_a( $object, 'WPInv_Subscription' ) ) { |
|
| 76 | + if (is_a($object, 'WPInv_Subscription')) { |
|
| 77 | 77 | $merge_tags = array_merge( |
| 78 | 78 | $merge_tags, |
| 79 | - $this->get_subscription_merge_tags( $object ) |
|
| 79 | + $this->get_subscription_merge_tags($object) |
|
| 80 | 80 | ); |
| 81 | 81 | } |
| 82 | 82 | |
@@ -90,25 +90,25 @@ discard block |
||
| 90 | 90 | * @param WPInv_Subscription $subscription |
| 91 | 91 | * @return array |
| 92 | 92 | */ |
| 93 | - public function get_subscription_merge_tags( $subscription ) { |
|
| 93 | + public function get_subscription_merge_tags($subscription) { |
|
| 94 | 94 | |
| 95 | 95 | // Abort if it does not exist. |
| 96 | - if ( ! $subscription->get_id() ) { |
|
| 96 | + if (!$subscription->get_id()) { |
|
| 97 | 97 | return array(); |
| 98 | 98 | } |
| 99 | 99 | |
| 100 | - $invoice = $subscription->get_parent_invoice(); |
|
| 100 | + $invoice = $subscription->get_parent_invoice(); |
|
| 101 | 101 | return array( |
| 102 | - '{subscription_renewal_date}' => getpaid_format_date_value( $subscription->get_next_renewal_date(), __( 'Never', 'invoicing' ) ), |
|
| 103 | - '{subscription_created}' => getpaid_format_date_value( $subscription->get_date_created() ), |
|
| 104 | - '{subscription_status}' => sanitize_text_field( $subscription->get_status_label() ), |
|
| 105 | - '{subscription_profile_id}' => sanitize_text_field( $subscription->get_profile_id() ), |
|
| 106 | - '{subscription_id}' => absint( $subscription->get_id() ), |
|
| 107 | - '{subscription_recurring_amount}' => sanitize_text_field( wpinv_price( $subscription->get_recurring_amount(), $invoice->get_currency() ) ), |
|
| 108 | - '{subscription_initial_amount}' => sanitize_text_field( wpinv_price( $subscription->get_initial_amount(), $invoice->get_currency() ) ), |
|
| 109 | - '{subscription_recurring_period}' => getpaid_get_subscription_period_label( $subscription->get_period(), $subscription->get_frequency(), '' ), |
|
| 102 | + '{subscription_renewal_date}' => getpaid_format_date_value($subscription->get_next_renewal_date(), __('Never', 'invoicing')), |
|
| 103 | + '{subscription_created}' => getpaid_format_date_value($subscription->get_date_created()), |
|
| 104 | + '{subscription_status}' => sanitize_text_field($subscription->get_status_label()), |
|
| 105 | + '{subscription_profile_id}' => sanitize_text_field($subscription->get_profile_id()), |
|
| 106 | + '{subscription_id}' => absint($subscription->get_id()), |
|
| 107 | + '{subscription_recurring_amount}' => sanitize_text_field(wpinv_price($subscription->get_recurring_amount(), $invoice->get_currency())), |
|
| 108 | + '{subscription_initial_amount}' => sanitize_text_field(wpinv_price($subscription->get_initial_amount(), $invoice->get_currency())), |
|
| 109 | + '{subscription_recurring_period}' => getpaid_get_subscription_period_label($subscription->get_period(), $subscription->get_frequency(), ''), |
|
| 110 | 110 | '{subscription_bill_times}' => $subscription->get_bill_times(), |
| 111 | - '{subscription_url}' => esc_url( $subscription->get_view_url() ), |
|
| 111 | + '{subscription_url}' => esc_url($subscription->get_view_url()), |
|
| 112 | 112 | ); |
| 113 | 113 | |
| 114 | 114 | } |
@@ -119,7 +119,7 @@ discard block |
||
| 119 | 119 | * @param WPInv_Invoice $invoice |
| 120 | 120 | * @return bool |
| 121 | 121 | */ |
| 122 | - public function should_send_notification( $invoice ) { |
|
| 122 | + public function should_send_notification($invoice) { |
|
| 123 | 123 | return 0 != $invoice->get_id(); |
| 124 | 124 | } |
| 125 | 125 | |
@@ -129,14 +129,14 @@ discard block |
||
| 129 | 129 | * @param WPInv_Invoice $invoice |
| 130 | 130 | * @return array |
| 131 | 131 | */ |
| 132 | - public function get_recipients( $invoice ) { |
|
| 133 | - $recipients = array( $invoice->get_email() ); |
|
| 132 | + public function get_recipients($invoice) { |
|
| 133 | + $recipients = array($invoice->get_email()); |
|
| 134 | 134 | |
| 135 | 135 | $cc = $invoice->get_email_cc(); |
| 136 | 136 | |
| 137 | - if ( ! empty( $cc ) ) { |
|
| 138 | - $cc = array_map( 'sanitize_email', wpinv_parse_list( $cc ) ); |
|
| 139 | - $recipients = array_filter( array_unique( array_merge( $recipients, $cc ) ) ); |
|
| 137 | + if (!empty($cc)) { |
|
| 138 | + $cc = array_map('sanitize_email', wpinv_parse_list($cc)); |
|
| 139 | + $recipients = array_filter(array_unique(array_merge($recipients, $cc))); |
|
| 140 | 140 | } |
| 141 | 141 | |
| 142 | 142 | return $recipients; |
@@ -150,51 +150,51 @@ discard block |
||
| 150 | 150 | * @param string $type |
| 151 | 151 | * @param array $extra_args Extra template args. |
| 152 | 152 | */ |
| 153 | - public function send_email( $subscription, $email, $type, $extra_args = array() ) { |
|
| 153 | + public function send_email($subscription, $email, $type, $extra_args = array()) { |
|
| 154 | 154 | |
| 155 | 155 | // Abort in case the parent invoice does not exist. |
| 156 | 156 | $invoice = $subscription->get_parent_invoice(); |
| 157 | - if ( ! $this->should_send_notification( $invoice ) ) { |
|
| 157 | + if (!$this->should_send_notification($invoice)) { |
|
| 158 | 158 | return; |
| 159 | 159 | } |
| 160 | 160 | |
| 161 | - if ( apply_filters( 'getpaid_skip_subscription_email', false, $type, $subscription ) ) { |
|
| 161 | + if (apply_filters('getpaid_skip_subscription_email', false, $type, $subscription)) { |
|
| 162 | 162 | return; |
| 163 | 163 | } |
| 164 | 164 | |
| 165 | - do_action( 'getpaid_before_send_subscription_notification', $type, $subscription, $email ); |
|
| 165 | + do_action('getpaid_before_send_subscription_notification', $type, $subscription, $email); |
|
| 166 | 166 | |
| 167 | - $recipients = $this->get_recipients( $invoice ); |
|
| 167 | + $recipients = $this->get_recipients($invoice); |
|
| 168 | 168 | $mailer = new GetPaid_Notification_Email_Sender(); |
| 169 | 169 | $merge_tags = $email->get_merge_tags(); |
| 170 | - $content = $email->get_content( $merge_tags, $extra_args ); |
|
| 171 | - $subject = $email->add_merge_tags( $email->get_subject(), $merge_tags ); |
|
| 170 | + $content = $email->get_content($merge_tags, $extra_args); |
|
| 171 | + $subject = $email->add_merge_tags($email->get_subject(), $merge_tags); |
|
| 172 | 172 | $attachments = $email->get_attachments(); |
| 173 | 173 | |
| 174 | 174 | $result = $mailer->send( |
| 175 | - apply_filters( 'getpaid_subscription_email_recipients', wpinv_parse_list( $recipients ), $email ), |
|
| 175 | + apply_filters('getpaid_subscription_email_recipients', wpinv_parse_list($recipients), $email), |
|
| 176 | 176 | $subject, |
| 177 | 177 | $content, |
| 178 | 178 | $attachments |
| 179 | 179 | ); |
| 180 | 180 | |
| 181 | 181 | // Maybe send a copy to the admin. |
| 182 | - if ( $email->include_admin_bcc() ) { |
|
| 182 | + if ($email->include_admin_bcc()) { |
|
| 183 | 183 | $mailer->send( |
| 184 | 184 | wpinv_get_admin_email(), |
| 185 | - $subject . __( ' - ADMIN BCC COPY', 'invoicing' ), |
|
| 185 | + $subject . __(' - ADMIN BCC COPY', 'invoicing'), |
|
| 186 | 186 | $content, |
| 187 | 187 | $attachments |
| 188 | 188 | ); |
| 189 | 189 | } |
| 190 | 190 | |
| 191 | - if ( $result ) { |
|
| 192 | - $subscription->get_parent_invoice()->add_note( sprintf( __( 'Successfully sent %s notification email.', 'invoicing' ), sanitize_key( $type ) ), false, false, true ); |
|
| 191 | + if ($result) { |
|
| 192 | + $subscription->get_parent_invoice()->add_note(sprintf(__('Successfully sent %s notification email.', 'invoicing'), sanitize_key($type)), false, false, true); |
|
| 193 | 193 | } else { |
| 194 | - $subscription->get_parent_invoice()->add_note( sprintf( __( 'Failed sending %s notification email.', 'invoicing' ), sanitize_key( $type ) ), false, false, true ); |
|
| 194 | + $subscription->get_parent_invoice()->add_note(sprintf(__('Failed sending %s notification email.', 'invoicing'), sanitize_key($type)), false, false, true); |
|
| 195 | 195 | } |
| 196 | 196 | |
| 197 | - do_action( 'getpaid_after_send_subscription_notification', $type, $subscription, $email ); |
|
| 197 | + do_action('getpaid_after_send_subscription_notification', $type, $subscription, $email); |
|
| 198 | 198 | |
| 199 | 199 | } |
| 200 | 200 | |
@@ -203,10 +203,10 @@ discard block |
||
| 203 | 203 | * |
| 204 | 204 | * @param WPInv_Subscription $subscription |
| 205 | 205 | */ |
| 206 | - public function subscription_trial( $subscription ) { |
|
| 206 | + public function subscription_trial($subscription) { |
|
| 207 | 207 | |
| 208 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 209 | - $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 208 | + $email = new GetPaid_Notification_Email(__FUNCTION__, $subscription); |
|
| 209 | + $this->send_email($subscription, $email, __FUNCTION__); |
|
| 210 | 210 | |
| 211 | 211 | } |
| 212 | 212 | |
@@ -215,10 +215,10 @@ discard block |
||
| 215 | 215 | * |
| 216 | 216 | * @param WPInv_Subscription $subscription |
| 217 | 217 | */ |
| 218 | - public function subscription_cancelled( $subscription ) { |
|
| 218 | + public function subscription_cancelled($subscription) { |
|
| 219 | 219 | |
| 220 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 221 | - $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 220 | + $email = new GetPaid_Notification_Email(__FUNCTION__, $subscription); |
|
| 221 | + $this->send_email($subscription, $email, __FUNCTION__); |
|
| 222 | 222 | |
| 223 | 223 | } |
| 224 | 224 | |
@@ -227,10 +227,10 @@ discard block |
||
| 227 | 227 | * |
| 228 | 228 | * @param WPInv_Subscription $subscription |
| 229 | 229 | */ |
| 230 | - public function subscription_expired( $subscription ) { |
|
| 230 | + public function subscription_expired($subscription) { |
|
| 231 | 231 | |
| 232 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 233 | - $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 232 | + $email = new GetPaid_Notification_Email(__FUNCTION__, $subscription); |
|
| 233 | + $this->send_email($subscription, $email, __FUNCTION__); |
|
| 234 | 234 | |
| 235 | 235 | } |
| 236 | 236 | |
@@ -239,10 +239,10 @@ discard block |
||
| 239 | 239 | * |
| 240 | 240 | * @param WPInv_Subscription $subscription |
| 241 | 241 | */ |
| 242 | - public function subscription_complete( $subscription ) { |
|
| 242 | + public function subscription_complete($subscription) { |
|
| 243 | 243 | |
| 244 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 245 | - $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 244 | + $email = new GetPaid_Notification_Email(__FUNCTION__, $subscription); |
|
| 245 | + $this->send_email($subscription, $email, __FUNCTION__); |
|
| 246 | 246 | |
| 247 | 247 | } |
| 248 | 248 | |
@@ -252,18 +252,18 @@ discard block |
||
| 252 | 252 | */ |
| 253 | 253 | public function renewal_reminder() { |
| 254 | 254 | |
| 255 | - $email = new GetPaid_Notification_Email( __FUNCTION__ ); |
|
| 255 | + $email = new GetPaid_Notification_Email(__FUNCTION__); |
|
| 256 | 256 | |
| 257 | 257 | // Fetch reminder days. |
| 258 | - $reminder_days = array_unique( wp_parse_id_list( $email->get_option( 'days' ) ) ); |
|
| 258 | + $reminder_days = array_unique(wp_parse_id_list($email->get_option('days'))); |
|
| 259 | 259 | |
| 260 | 260 | // Abort if non is set. |
| 261 | - if ( empty( $reminder_days ) ) { |
|
| 261 | + if (empty($reminder_days)) { |
|
| 262 | 262 | return; |
| 263 | 263 | } |
| 264 | 264 | |
| 265 | 265 | // Fetch matching subscriptions. |
| 266 | - $args = array( |
|
| 266 | + $args = array( |
|
| 267 | 267 | 'number' => -1, |
| 268 | 268 | 'count_total' => false, |
| 269 | 269 | 'status' => 'trialling active', |
@@ -272,8 +272,8 @@ discard block |
||
| 272 | 272 | ), |
| 273 | 273 | ); |
| 274 | 274 | |
| 275 | - foreach ( $reminder_days as $days ) { |
|
| 276 | - $date = date_parse( date( 'Y-m-d', strtotime( "+$days days", current_time( 'timestamp' ) ) ) ); |
|
| 275 | + foreach ($reminder_days as $days) { |
|
| 276 | + $date = date_parse(date('Y-m-d', strtotime("+$days days", current_time('timestamp')))); |
|
| 277 | 277 | |
| 278 | 278 | $args['date_expires_query'][] = array( |
| 279 | 279 | 'year' => $date['year'], |
@@ -283,14 +283,14 @@ discard block |
||
| 283 | 283 | |
| 284 | 284 | } |
| 285 | 285 | |
| 286 | - $subscriptions = new GetPaid_Subscriptions_Query( $args ); |
|
| 286 | + $subscriptions = new GetPaid_Subscriptions_Query($args); |
|
| 287 | 287 | |
| 288 | - foreach ( $subscriptions as $subscription ) { |
|
| 288 | + foreach ($subscriptions as $subscription) { |
|
| 289 | 289 | |
| 290 | 290 | // Skip packages. |
| 291 | - if ( get_post_meta( $subscription->get_product_id(), '_wpinv_type', true ) != 'package' ) { |
|
| 291 | + if (get_post_meta($subscription->get_product_id(), '_wpinv_type', true) != 'package') { |
|
| 292 | 292 | $email->object = $subscription; |
| 293 | - $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 293 | + $this->send_email($subscription, $email, __FUNCTION__); |
|
| 294 | 294 | } |
| 295 | 295 | |
| 296 | 296 | } |