@@ -17,28 +17,28 @@ discard block |
||
| 17 | 17 | */ |
| 18 | 18 | function getpaid_get_subscriptions( $args = array(), $return = 'results' ) { |
| 19 | 19 | |
| 20 | - // Do not retrieve all fields if we just want the count. |
|
| 21 | - if ( 'count' == $return ) { |
|
| 22 | - $args['fields'] = 'id'; |
|
| 23 | - $args['number'] = 1; |
|
| 24 | - } |
|
| 20 | + // Do not retrieve all fields if we just want the count. |
|
| 21 | + if ( 'count' == $return ) { |
|
| 22 | + $args['fields'] = 'id'; |
|
| 23 | + $args['number'] = 1; |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | - // Do not count all matches if we just want the results. |
|
| 27 | - if ( 'results' == $return ) { |
|
| 28 | - $args['count_total'] = false; |
|
| 29 | - } |
|
| 26 | + // Do not count all matches if we just want the results. |
|
| 27 | + if ( 'results' == $return ) { |
|
| 28 | + $args['count_total'] = false; |
|
| 29 | + } |
|
| 30 | 30 | |
| 31 | - $query = new GetPaid_Subscriptions_Query( $args ); |
|
| 31 | + $query = new GetPaid_Subscriptions_Query( $args ); |
|
| 32 | 32 | |
| 33 | - if ( 'results' == $return ) { |
|
| 34 | - return $query->get_results(); |
|
| 35 | - } |
|
| 33 | + if ( 'results' == $return ) { |
|
| 34 | + return $query->get_results(); |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | - if ( 'count' == $return ) { |
|
| 38 | - return $query->get_total(); |
|
| 39 | - } |
|
| 37 | + if ( 'count' == $return ) { |
|
| 38 | + return $query->get_total(); |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - return $query; |
|
| 41 | + return $query; |
|
| 42 | 42 | } |
| 43 | 43 | |
| 44 | 44 | /** |
@@ -48,18 +48,18 @@ discard block |
||
| 48 | 48 | */ |
| 49 | 49 | function getpaid_get_subscription_statuses() { |
| 50 | 50 | |
| 51 | - return apply_filters( |
|
| 52 | - 'getpaid_get_subscription_statuses', |
|
| 53 | - array( |
|
| 54 | - 'pending' => __( 'Pending', 'invoicing' ), |
|
| 55 | - 'trialling' => __( 'Trialing', 'invoicing' ), |
|
| 56 | - 'active' => __( 'Active', 'invoicing' ), |
|
| 57 | - 'failing' => __( 'Failing', 'invoicing' ), |
|
| 58 | - 'expired' => __( 'Expired', 'invoicing' ), |
|
| 59 | - 'completed' => __( 'Complete', 'invoicing' ), |
|
| 60 | - 'cancelled' => __( 'Cancelled', 'invoicing' ), |
|
| 61 | - ) |
|
| 62 | - ); |
|
| 51 | + return apply_filters( |
|
| 52 | + 'getpaid_get_subscription_statuses', |
|
| 53 | + array( |
|
| 54 | + 'pending' => __( 'Pending', 'invoicing' ), |
|
| 55 | + 'trialling' => __( 'Trialing', 'invoicing' ), |
|
| 56 | + 'active' => __( 'Active', 'invoicing' ), |
|
| 57 | + 'failing' => __( 'Failing', 'invoicing' ), |
|
| 58 | + 'expired' => __( 'Expired', 'invoicing' ), |
|
| 59 | + 'completed' => __( 'Complete', 'invoicing' ), |
|
| 60 | + 'cancelled' => __( 'Cancelled', 'invoicing' ), |
|
| 61 | + ) |
|
| 62 | + ); |
|
| 63 | 63 | |
| 64 | 64 | } |
| 65 | 65 | |
@@ -69,8 +69,8 @@ discard block |
||
| 69 | 69 | * @return string |
| 70 | 70 | */ |
| 71 | 71 | function getpaid_get_subscription_status_label( $status ) { |
| 72 | - $statuses = getpaid_get_subscription_statuses(); |
|
| 73 | - return isset( $statuses[ $status ] ) ? $statuses[ $status ] : ucfirst( sanitize_text_field( $status ) ); |
|
| 72 | + $statuses = getpaid_get_subscription_statuses(); |
|
| 73 | + return isset( $statuses[ $status ] ) ? $statuses[ $status ] : ucfirst( sanitize_text_field( $status ) ); |
|
| 74 | 74 | } |
| 75 | 75 | |
| 76 | 76 | /** |
@@ -80,18 +80,18 @@ discard block |
||
| 80 | 80 | */ |
| 81 | 81 | function getpaid_get_subscription_status_classes() { |
| 82 | 82 | |
| 83 | - return apply_filters( |
|
| 84 | - 'getpaid_get_subscription_status_classes', |
|
| 85 | - array( |
|
| 86 | - 'pending' => 'badge-dark', |
|
| 87 | - 'trialling' => 'badge-info', |
|
| 88 | - 'active' => 'badge-success', |
|
| 89 | - 'failing' => 'badge-warning', |
|
| 90 | - 'expired' => 'badge-danger', |
|
| 91 | - 'completed' => 'badge-primary', |
|
| 92 | - 'cancelled' => 'badge-secondary', |
|
| 93 | - ) |
|
| 94 | - ); |
|
| 83 | + return apply_filters( |
|
| 84 | + 'getpaid_get_subscription_status_classes', |
|
| 85 | + array( |
|
| 86 | + 'pending' => 'badge-dark', |
|
| 87 | + 'trialling' => 'badge-info', |
|
| 88 | + 'active' => 'badge-success', |
|
| 89 | + 'failing' => 'badge-warning', |
|
| 90 | + 'expired' => 'badge-danger', |
|
| 91 | + 'completed' => 'badge-primary', |
|
| 92 | + 'cancelled' => 'badge-secondary', |
|
| 93 | + ) |
|
| 94 | + ); |
|
| 95 | 95 | |
| 96 | 96 | } |
| 97 | 97 | |
@@ -102,15 +102,15 @@ discard block |
||
| 102 | 102 | */ |
| 103 | 103 | function getpaid_get_subscription_status_counts( $args = array() ) { |
| 104 | 104 | |
| 105 | - $statuses = array_keys( getpaid_get_subscription_statuses() ); |
|
| 106 | - $counts = array(); |
|
| 105 | + $statuses = array_keys( getpaid_get_subscription_statuses() ); |
|
| 106 | + $counts = array(); |
|
| 107 | 107 | |
| 108 | - foreach ( $statuses as $status ) { |
|
| 109 | - $_args = wp_parse_args( "status=$status", $args ); |
|
| 110 | - $counts[ $status ] = getpaid_get_subscriptions( $_args, 'count' ); |
|
| 111 | - } |
|
| 108 | + foreach ( $statuses as $status ) { |
|
| 109 | + $_args = wp_parse_args( "status=$status", $args ); |
|
| 110 | + $counts[ $status ] = getpaid_get_subscriptions( $_args, 'count' ); |
|
| 111 | + } |
|
| 112 | 112 | |
| 113 | - return $counts; |
|
| 113 | + return $counts; |
|
| 114 | 114 | |
| 115 | 115 | } |
| 116 | 116 | |
@@ -121,32 +121,32 @@ discard block |
||
| 121 | 121 | */ |
| 122 | 122 | function getpaid_get_subscription_periods() { |
| 123 | 123 | |
| 124 | - return apply_filters( |
|
| 125 | - 'getpaid_get_subscription_periods', |
|
| 126 | - array( |
|
| 124 | + return apply_filters( |
|
| 125 | + 'getpaid_get_subscription_periods', |
|
| 126 | + array( |
|
| 127 | 127 | |
| 128 | - 'day' => array( |
|
| 129 | - 'singular' => __( '%s day', 'invoicing' ), |
|
| 130 | - 'plural' => __( '%d days', 'invoicing' ), |
|
| 131 | - ), |
|
| 128 | + 'day' => array( |
|
| 129 | + 'singular' => __( '%s day', 'invoicing' ), |
|
| 130 | + 'plural' => __( '%d days', 'invoicing' ), |
|
| 131 | + ), |
|
| 132 | 132 | |
| 133 | - 'week' => array( |
|
| 134 | - 'singular' => __( '%s week', 'invoicing' ), |
|
| 135 | - 'plural' => __( '%d weeks', 'invoicing' ), |
|
| 136 | - ), |
|
| 133 | + 'week' => array( |
|
| 134 | + 'singular' => __( '%s week', 'invoicing' ), |
|
| 135 | + 'plural' => __( '%d weeks', 'invoicing' ), |
|
| 136 | + ), |
|
| 137 | 137 | |
| 138 | - 'month' => array( |
|
| 139 | - 'singular' => __( '%s month', 'invoicing' ), |
|
| 140 | - 'plural' => __( '%d months', 'invoicing' ), |
|
| 141 | - ), |
|
| 138 | + 'month' => array( |
|
| 139 | + 'singular' => __( '%s month', 'invoicing' ), |
|
| 140 | + 'plural' => __( '%d months', 'invoicing' ), |
|
| 141 | + ), |
|
| 142 | 142 | |
| 143 | - 'year' => array( |
|
| 144 | - 'singular' => __( '%s year', 'invoicing' ), |
|
| 145 | - 'plural' => __( '%d years', 'invoicing' ), |
|
| 146 | - ), |
|
| 143 | + 'year' => array( |
|
| 144 | + 'singular' => __( '%s year', 'invoicing' ), |
|
| 145 | + 'plural' => __( '%d years', 'invoicing' ), |
|
| 146 | + ), |
|
| 147 | 147 | |
| 148 | - ) |
|
| 149 | - ); |
|
| 148 | + ) |
|
| 149 | + ); |
|
| 150 | 150 | |
| 151 | 151 | } |
| 152 | 152 | |
@@ -157,7 +157,7 @@ discard block |
||
| 157 | 157 | * @return int |
| 158 | 158 | */ |
| 159 | 159 | function getpaid_get_subscription_trial_period_interval( $trial_period ) { |
| 160 | - return (int) preg_replace( '/[^0-9]/', '', $trial_period ); |
|
| 160 | + return (int) preg_replace( '/[^0-9]/', '', $trial_period ); |
|
| 161 | 161 | } |
| 162 | 162 | |
| 163 | 163 | /** |
@@ -167,7 +167,7 @@ discard block |
||
| 167 | 167 | * @return string |
| 168 | 168 | */ |
| 169 | 169 | function getpaid_get_subscription_trial_period_period( $trial_period ) { |
| 170 | - return preg_replace( '/[^a-z]/', '', strtolower( $trial_period ) ); |
|
| 170 | + return preg_replace( '/[^a-z]/', '', strtolower( $trial_period ) ); |
|
| 171 | 171 | } |
| 172 | 172 | |
| 173 | 173 | /** |
@@ -178,8 +178,8 @@ discard block |
||
| 178 | 178 | * @return string |
| 179 | 179 | */ |
| 180 | 180 | function getpaid_get_subscription_period_label( $period, $interval = 1, $singular_prefix = '1' ) { |
| 181 | - $label = (int) $interval > 1 ? getpaid_get_plural_subscription_period_label( $period, $interval ) : getpaid_get_singular_subscription_period_label( $period, $singular_prefix ); |
|
| 182 | - return strtolower( sanitize_text_field( $label ) ); |
|
| 181 | + $label = (int) $interval > 1 ? getpaid_get_plural_subscription_period_label( $period, $interval ) : getpaid_get_singular_subscription_period_label( $period, $singular_prefix ); |
|
| 182 | + return strtolower( sanitize_text_field( $label ) ); |
|
| 183 | 183 | } |
| 184 | 184 | |
| 185 | 185 | /** |
@@ -190,22 +190,22 @@ discard block |
||
| 190 | 190 | */ |
| 191 | 191 | function getpaid_get_singular_subscription_period_label( $period, $singular_prefix = '1' ) { |
| 192 | 192 | |
| 193 | - $periods = getpaid_get_subscription_periods(); |
|
| 194 | - $period = strtolower( $period ); |
|
| 193 | + $periods = getpaid_get_subscription_periods(); |
|
| 194 | + $period = strtolower( $period ); |
|
| 195 | 195 | |
| 196 | - if ( isset( $periods[ $period ] ) ) { |
|
| 197 | - return sprintf( $periods[ $period ]['singular'], $singular_prefix ); |
|
| 198 | - } |
|
| 196 | + if ( isset( $periods[ $period ] ) ) { |
|
| 197 | + return sprintf( $periods[ $period ]['singular'], $singular_prefix ); |
|
| 198 | + } |
|
| 199 | 199 | |
| 200 | - // Backwards compatibility. |
|
| 201 | - foreach ( $periods as $key => $data ) { |
|
| 202 | - if ( strpos( $key, $period ) === 0 ) { |
|
| 203 | - return sprintf( $data['singular'], $singular_prefix ); |
|
| 204 | - } |
|
| 205 | - } |
|
| 200 | + // Backwards compatibility. |
|
| 201 | + foreach ( $periods as $key => $data ) { |
|
| 202 | + if ( strpos( $key, $period ) === 0 ) { |
|
| 203 | + return sprintf( $data['singular'], $singular_prefix ); |
|
| 204 | + } |
|
| 205 | + } |
|
| 206 | 206 | |
| 207 | - // Invalid string. |
|
| 208 | - return ''; |
|
| 207 | + // Invalid string. |
|
| 208 | + return ''; |
|
| 209 | 209 | } |
| 210 | 210 | |
| 211 | 211 | /** |
@@ -217,22 +217,22 @@ discard block |
||
| 217 | 217 | */ |
| 218 | 218 | function getpaid_get_plural_subscription_period_label( $period, $interval ) { |
| 219 | 219 | |
| 220 | - $periods = getpaid_get_subscription_periods(); |
|
| 221 | - $period = strtolower( $period ); |
|
| 220 | + $periods = getpaid_get_subscription_periods(); |
|
| 221 | + $period = strtolower( $period ); |
|
| 222 | 222 | |
| 223 | - if ( isset( $periods[ $period ] ) ) { |
|
| 224 | - return sprintf( $periods[ $period ]['plural'], $interval ); |
|
| 225 | - } |
|
| 223 | + if ( isset( $periods[ $period ] ) ) { |
|
| 224 | + return sprintf( $periods[ $period ]['plural'], $interval ); |
|
| 225 | + } |
|
| 226 | 226 | |
| 227 | - // Backwards compatibility. |
|
| 228 | - foreach ( $periods as $key => $data ) { |
|
| 229 | - if ( strpos( $key, $period ) === 0 ) { |
|
| 230 | - return sprintf( $data['plural'], $interval ); |
|
| 231 | - } |
|
| 232 | - } |
|
| 227 | + // Backwards compatibility. |
|
| 228 | + foreach ( $periods as $key => $data ) { |
|
| 229 | + if ( strpos( $key, $period ) === 0 ) { |
|
| 230 | + return sprintf( $data['plural'], $interval ); |
|
| 231 | + } |
|
| 232 | + } |
|
| 233 | 233 | |
| 234 | - // Invalid string. |
|
| 235 | - return ''; |
|
| 234 | + // Invalid string. |
|
| 235 | + return ''; |
|
| 236 | 236 | } |
| 237 | 237 | |
| 238 | 238 | /** |
@@ -243,50 +243,50 @@ discard block |
||
| 243 | 243 | */ |
| 244 | 244 | function getpaid_get_formatted_subscription_amount( $subscription ) { |
| 245 | 245 | |
| 246 | - $initial = wpinv_price( $subscription->get_initial_amount(), $subscription->get_parent_payment()->get_currency() ); |
|
| 247 | - $recurring = wpinv_price( $subscription->get_recurring_amount(), $subscription->get_parent_payment()->get_currency() ); |
|
| 248 | - $period = getpaid_get_subscription_period_label( $subscription->get_period(), $subscription->get_frequency(), '' ); |
|
| 246 | + $initial = wpinv_price( $subscription->get_initial_amount(), $subscription->get_parent_payment()->get_currency() ); |
|
| 247 | + $recurring = wpinv_price( $subscription->get_recurring_amount(), $subscription->get_parent_payment()->get_currency() ); |
|
| 248 | + $period = getpaid_get_subscription_period_label( $subscription->get_period(), $subscription->get_frequency(), '' ); |
|
| 249 | 249 | |
| 250 | - // Trial periods. |
|
| 251 | - if ( $subscription->has_trial_period() ) { |
|
| 250 | + // Trial periods. |
|
| 251 | + if ( $subscription->has_trial_period() ) { |
|
| 252 | 252 | |
| 253 | - $trial_period = getpaid_get_subscription_trial_period_period( $subscription->get_trial_period() ); |
|
| 254 | - $trial_interval = getpaid_get_subscription_trial_period_interval( $subscription->get_trial_period() ); |
|
| 255 | - return sprintf( |
|
| 253 | + $trial_period = getpaid_get_subscription_trial_period_period( $subscription->get_trial_period() ); |
|
| 254 | + $trial_interval = getpaid_get_subscription_trial_period_interval( $subscription->get_trial_period() ); |
|
| 255 | + return sprintf( |
|
| 256 | 256 | |
| 257 | - // translators: $1: is the initial amount, $2: is the trial period, $3: is the recurring amount, $4: is the recurring period |
|
| 258 | - _x( '%1$s trial for %2$s then %3$s / %4$s', 'Subscription amount. (e.g.: $10 trial for 1 month then $120 / year)', 'invoicing' ), |
|
| 259 | - $initial, |
|
| 260 | - getpaid_get_subscription_period_label( $trial_period, $trial_interval ), |
|
| 261 | - $recurring, |
|
| 262 | - $period |
|
| 257 | + // translators: $1: is the initial amount, $2: is the trial period, $3: is the recurring amount, $4: is the recurring period |
|
| 258 | + _x( '%1$s trial for %2$s then %3$s / %4$s', 'Subscription amount. (e.g.: $10 trial for 1 month then $120 / year)', 'invoicing' ), |
|
| 259 | + $initial, |
|
| 260 | + getpaid_get_subscription_period_label( $trial_period, $trial_interval ), |
|
| 261 | + $recurring, |
|
| 262 | + $period |
|
| 263 | 263 | |
| 264 | - ); |
|
| 264 | + ); |
|
| 265 | 265 | |
| 266 | - } |
|
| 266 | + } |
|
| 267 | 267 | |
| 268 | - if ( $initial != $recurring ) { |
|
| 268 | + if ( $initial != $recurring ) { |
|
| 269 | 269 | |
| 270 | - return sprintf( |
|
| 270 | + return sprintf( |
|
| 271 | 271 | |
| 272 | - // translators: $1: is the initial amount, $2: is the recurring amount, $3: is the recurring period |
|
| 273 | - _x( 'Initial payment of %1$s which renews at %2$s / %3$s', 'Subscription amount. (e.g.:Initial payment of $100 which renews at $120 / year)', 'invoicing' ), |
|
| 274 | - $initial, |
|
| 275 | - $recurring, |
|
| 276 | - $period |
|
| 272 | + // translators: $1: is the initial amount, $2: is the recurring amount, $3: is the recurring period |
|
| 273 | + _x( 'Initial payment of %1$s which renews at %2$s / %3$s', 'Subscription amount. (e.g.:Initial payment of $100 which renews at $120 / year)', 'invoicing' ), |
|
| 274 | + $initial, |
|
| 275 | + $recurring, |
|
| 276 | + $period |
|
| 277 | 277 | |
| 278 | - ); |
|
| 278 | + ); |
|
| 279 | 279 | |
| 280 | - } |
|
| 280 | + } |
|
| 281 | 281 | |
| 282 | - return sprintf( |
|
| 282 | + return sprintf( |
|
| 283 | 283 | |
| 284 | - // translators: $1: is the recurring amount, $2: is the recurring period |
|
| 285 | - _x( '%1$s / %2$s', 'Subscription amount. (e.g.: $120 / year)', 'invoicing' ), |
|
| 286 | - $initial, |
|
| 287 | - $period |
|
| 284 | + // translators: $1: is the recurring amount, $2: is the recurring period |
|
| 285 | + _x( '%1$s / %2$s', 'Subscription amount. (e.g.: $120 / year)', 'invoicing' ), |
|
| 286 | + $initial, |
|
| 287 | + $period |
|
| 288 | 288 | |
| 289 | - ); |
|
| 289 | + ); |
|
| 290 | 290 | |
| 291 | 291 | } |
| 292 | 292 | |
@@ -297,7 +297,7 @@ discard block |
||
| 297 | 297 | * @return WPInv_Subscription|bool |
| 298 | 298 | */ |
| 299 | 299 | function getpaid_get_invoice_subscription( $invoice ) { |
| 300 | - return getpaid_subscriptions()->get_invoice_subscription( $invoice ); |
|
| 300 | + return getpaid_subscriptions()->get_invoice_subscription( $invoice ); |
|
| 301 | 301 | } |
| 302 | 302 | |
| 303 | 303 | /** |
@@ -306,10 +306,10 @@ discard block |
||
| 306 | 306 | * @param WPInv_Invoice $invoice |
| 307 | 307 | */ |
| 308 | 308 | function getpaid_activate_invoice_subscription( $invoice ) { |
| 309 | - $subscription = getpaid_get_invoice_subscription( $invoice ); |
|
| 310 | - if ( is_a( $subscription, 'WPInv_Subscription' ) ) { |
|
| 311 | - $subscription->activate(); |
|
| 312 | - } |
|
| 309 | + $subscription = getpaid_get_invoice_subscription( $invoice ); |
|
| 310 | + if ( is_a( $subscription, 'WPInv_Subscription' ) ) { |
|
| 311 | + $subscription->activate(); |
|
| 312 | + } |
|
| 313 | 313 | } |
| 314 | 314 | |
| 315 | 315 | /** |
@@ -318,7 +318,7 @@ discard block |
||
| 318 | 318 | * @return WPInv_Subscriptions |
| 319 | 319 | */ |
| 320 | 320 | function getpaid_subscriptions() { |
| 321 | - return getpaid()->get( 'subscriptions' ); |
|
| 321 | + return getpaid()->get( 'subscriptions' ); |
|
| 322 | 322 | } |
| 323 | 323 | |
| 324 | 324 | /** |
@@ -336,14 +336,14 @@ discard block |
||
| 336 | 336 | return false; |
| 337 | 337 | } |
| 338 | 338 | |
| 339 | - // Fetch the invoiec subscription. |
|
| 340 | - $subscription = getpaid_get_subscriptions( |
|
| 341 | - array( |
|
| 342 | - 'invoice_in' => $invoice->is_renewal() ? $invoice->get_parent_id() : $invoice->get_id(), |
|
| 343 | - 'number' => 1, |
|
| 344 | - ) |
|
| 345 | - ); |
|
| 339 | + // Fetch the invoiec subscription. |
|
| 340 | + $subscription = getpaid_get_subscriptions( |
|
| 341 | + array( |
|
| 342 | + 'invoice_in' => $invoice->is_renewal() ? $invoice->get_parent_id() : $invoice->get_id(), |
|
| 343 | + 'number' => 1, |
|
| 344 | + ) |
|
| 345 | + ); |
|
| 346 | 346 | |
| 347 | - return empty( $subscription ) ? false : $subscription[0]; |
|
| 347 | + return empty( $subscription ) ? false : $subscription[0]; |
|
| 348 | 348 | |
| 349 | 349 | } |
@@ -13,282 +13,282 @@ |
||
| 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 | - do_action( 'getpaid_before_send_subscription_notification', $type, $subscription, $email ); |
|
| 162 | - |
|
| 163 | - $recipients = $this->get_recipients( $invoice ); |
|
| 164 | - $mailer = new GetPaid_Notification_Email_Sender(); |
|
| 165 | - $merge_tags = $email->get_merge_tags(); |
|
| 166 | - $content = $email->get_content( $merge_tags, $extra_args ); |
|
| 167 | - $subject = $email->add_merge_tags( $email->get_subject(), $merge_tags ); |
|
| 168 | - $attachments = $email->get_attachments(); |
|
| 169 | - |
|
| 170 | - $result = $mailer->send( |
|
| 171 | - apply_filters( 'getpaid_subscription_email_recipients', wpinv_parse_list( $recipients ), $email ), |
|
| 172 | - $subject, |
|
| 173 | - $content, |
|
| 174 | - $attachments |
|
| 175 | - ); |
|
| 176 | - |
|
| 177 | - // Maybe send a copy to the admin. |
|
| 178 | - if ( $email->include_admin_bcc() ) { |
|
| 179 | - $mailer->send( |
|
| 180 | - wpinv_get_admin_email(), |
|
| 181 | - $subject . __( ' - ADMIN BCC COPY', 'invoicing' ), |
|
| 182 | - $content, |
|
| 183 | - $attachments |
|
| 184 | - ); |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - if ( ! $result ) { |
|
| 188 | - $subscription->get_parent_invoice()->add_note( sprintf( __( 'Failed sending %s notification email.', 'invoicing' ), sanitize_key( $type ) ), false, false, true ); |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - do_action( 'getpaid_after_send_subscription_notification', $type, $subscription, $email ); |
|
| 192 | - |
|
| 193 | - } |
|
| 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 | + } |
|
| 194 | 67 | |
| 195 | 68 | /** |
| 196 | - * Sends a new trial notification. |
|
| 197 | - * |
|
| 198 | - * @param WPInv_Subscription $subscription |
|
| 199 | - */ |
|
| 200 | - 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 ) { |
|
| 201 | 75 | |
| 202 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 203 | - $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 | + } |
|
| 204 | 82 | |
| 205 | - } |
|
| 83 | + return $merge_tags; |
|
| 206 | 84 | |
| 207 | - /** |
|
| 208 | - * Sends a cancelled subscription notification. |
|
| 209 | - * |
|
| 210 | - * @param WPInv_Subscription $subscription |
|
| 211 | - */ |
|
| 212 | - public function subscription_cancelled( $subscription ) { |
|
| 85 | + } |
|
| 213 | 86 | |
| 214 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 215 | - $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 | + ); |
|
| 216 | 113 | |
| 217 | - } |
|
| 114 | + } |
|
| 218 | 115 | |
| 219 | - /** |
|
| 220 | - * Sends a subscription expired notification. |
|
| 221 | - * |
|
| 222 | - * @param WPInv_Subscription $subscription |
|
| 223 | - */ |
|
| 224 | - 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 | + } |
|
| 225 | 125 | |
| 226 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 227 | - $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() ); |
|
| 228 | 134 | |
| 229 | - } |
|
| 135 | + $cc = $invoice->get_email_cc(); |
|
| 230 | 136 | |
| 231 | - /** |
|
| 232 | - * Sends a completed subscription notification. |
|
| 233 | - * |
|
| 234 | - * @param WPInv_Subscription $subscription |
|
| 235 | - */ |
|
| 236 | - 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 | + } |
|
| 237 | 141 | |
| 238 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 239 | - $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 142 | + return $recipients; |
|
| 143 | + } |
|
| 240 | 144 | |
| 241 | - } |
|
| 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 | + do_action( 'getpaid_before_send_subscription_notification', $type, $subscription, $email ); |
|
| 162 | + |
|
| 163 | + $recipients = $this->get_recipients( $invoice ); |
|
| 164 | + $mailer = new GetPaid_Notification_Email_Sender(); |
|
| 165 | + $merge_tags = $email->get_merge_tags(); |
|
| 166 | + $content = $email->get_content( $merge_tags, $extra_args ); |
|
| 167 | + $subject = $email->add_merge_tags( $email->get_subject(), $merge_tags ); |
|
| 168 | + $attachments = $email->get_attachments(); |
|
| 169 | + |
|
| 170 | + $result = $mailer->send( |
|
| 171 | + apply_filters( 'getpaid_subscription_email_recipients', wpinv_parse_list( $recipients ), $email ), |
|
| 172 | + $subject, |
|
| 173 | + $content, |
|
| 174 | + $attachments |
|
| 175 | + ); |
|
| 176 | + |
|
| 177 | + // Maybe send a copy to the admin. |
|
| 178 | + if ( $email->include_admin_bcc() ) { |
|
| 179 | + $mailer->send( |
|
| 180 | + wpinv_get_admin_email(), |
|
| 181 | + $subject . __( ' - ADMIN BCC COPY', 'invoicing' ), |
|
| 182 | + $content, |
|
| 183 | + $attachments |
|
| 184 | + ); |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + if ( ! $result ) { |
|
| 188 | + $subscription->get_parent_invoice()->add_note( sprintf( __( 'Failed sending %s notification email.', 'invoicing' ), sanitize_key( $type ) ), false, false, true ); |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + do_action( 'getpaid_after_send_subscription_notification', $type, $subscription, $email ); |
|
| 242 | 192 | |
| 243 | - /** |
|
| 244 | - * Sends a subscription renewal reminder notification. |
|
| 245 | - * |
|
| 246 | - */ |
|
| 247 | - public function renewal_reminder() { |
|
| 193 | + } |
|
| 248 | 194 | |
| 249 | - $email = new GetPaid_Notification_Email( __FUNCTION__ ); |
|
| 195 | + /** |
|
| 196 | + * Sends a new trial notification. |
|
| 197 | + * |
|
| 198 | + * @param WPInv_Subscription $subscription |
|
| 199 | + */ |
|
| 200 | + public function subscription_trial( $subscription ) { |
|
| 250 | 201 | |
| 251 | - // Fetch reminder days. |
|
| 252 | - $reminder_days = array_unique( wp_parse_id_list( $email->get_option( 'days' ) ) ); |
|
| 202 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 203 | + $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 253 | 204 | |
| 254 | - // Abort if non is set. |
|
| 255 | - if ( empty( $reminder_days ) ) { |
|
| 256 | - return; |
|
| 257 | - } |
|
| 205 | + } |
|
| 258 | 206 | |
| 259 | - // Fetch matching subscriptions. |
|
| 207 | + /** |
|
| 208 | + * Sends a cancelled subscription notification. |
|
| 209 | + * |
|
| 210 | + * @param WPInv_Subscription $subscription |
|
| 211 | + */ |
|
| 212 | + public function subscription_cancelled( $subscription ) { |
|
| 213 | + |
|
| 214 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 215 | + $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 216 | + |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + /** |
|
| 220 | + * Sends a subscription expired notification. |
|
| 221 | + * |
|
| 222 | + * @param WPInv_Subscription $subscription |
|
| 223 | + */ |
|
| 224 | + public function subscription_expired( $subscription ) { |
|
| 225 | + |
|
| 226 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 227 | + $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 228 | + |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + /** |
|
| 232 | + * Sends a completed subscription notification. |
|
| 233 | + * |
|
| 234 | + * @param WPInv_Subscription $subscription |
|
| 235 | + */ |
|
| 236 | + public function subscription_complete( $subscription ) { |
|
| 237 | + |
|
| 238 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 239 | + $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 240 | + |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + /** |
|
| 244 | + * Sends a subscription renewal reminder notification. |
|
| 245 | + * |
|
| 246 | + */ |
|
| 247 | + public function renewal_reminder() { |
|
| 248 | + |
|
| 249 | + $email = new GetPaid_Notification_Email( __FUNCTION__ ); |
|
| 250 | + |
|
| 251 | + // Fetch reminder days. |
|
| 252 | + $reminder_days = array_unique( wp_parse_id_list( $email->get_option( 'days' ) ) ); |
|
| 253 | + |
|
| 254 | + // Abort if non is set. |
|
| 255 | + if ( empty( $reminder_days ) ) { |
|
| 256 | + return; |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + // Fetch matching subscriptions. |
|
| 260 | 260 | $args = array( |
| 261 | 261 | 'number' => -1, |
| 262 | - 'count_total' => false, |
|
| 263 | - 'status' => 'trialling active', |
|
| 262 | + 'count_total' => false, |
|
| 263 | + 'status' => 'trialling active', |
|
| 264 | 264 | 'date_expires_query' => array( |
| 265 | - 'relation' => 'OR' |
|
| 265 | + 'relation' => 'OR' |
|
| 266 | 266 | ), |
| 267 | - ); |
|
| 267 | + ); |
|
| 268 | 268 | |
| 269 | - foreach ( $reminder_days as $days ) { |
|
| 270 | - $date = date_parse( date( 'Y-m-d', strtotime( "+$days days", current_time( 'timestamp' ) ) ) ); |
|
| 269 | + foreach ( $reminder_days as $days ) { |
|
| 270 | + $date = date_parse( date( 'Y-m-d', strtotime( "+$days days", current_time( 'timestamp' ) ) ) ); |
|
| 271 | 271 | |
| 272 | - $args['date_expires_query'][] = array( |
|
| 273 | - 'year' => $date['year'], |
|
| 274 | - 'month' => $date['month'], |
|
| 275 | - 'day' => $date['day'], |
|
| 276 | - ); |
|
| 272 | + $args['date_expires_query'][] = array( |
|
| 273 | + 'year' => $date['year'], |
|
| 274 | + 'month' => $date['month'], |
|
| 275 | + 'day' => $date['day'], |
|
| 276 | + ); |
|
| 277 | 277 | |
| 278 | - } |
|
| 278 | + } |
|
| 279 | 279 | |
| 280 | - $subscriptions = new GetPaid_Subscriptions_Query( $args ); |
|
| 280 | + $subscriptions = new GetPaid_Subscriptions_Query( $args ); |
|
| 281 | 281 | |
| 282 | 282 | foreach ( $subscriptions as $subscription ) { |
| 283 | 283 | |
| 284 | - // Skip packages. |
|
| 285 | - if ( get_post_meta( $subscription->get_product_id(), '_wpinv_type', true ) != 'package' ) { |
|
| 286 | - $email->object = $subscription; |
|
| 287 | - $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 288 | - } |
|
| 284 | + // Skip packages. |
|
| 285 | + if ( get_post_meta( $subscription->get_product_id(), '_wpinv_type', true ) != 'package' ) { |
|
| 286 | + $email->object = $subscription; |
|
| 287 | + $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 288 | + } |
|
| 289 | 289 | |
| 290 | - } |
|
| 290 | + } |
|
| 291 | 291 | |
| 292 | - } |
|
| 292 | + } |
|
| 293 | 293 | |
| 294 | 294 | } |
@@ -12,444 +12,444 @@ |
||
| 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 | - $mailer = new GetPaid_Notification_Email_Sender(); |
|
| 167 | - $merge_tags = $email->get_merge_tags(); |
|
| 168 | - |
|
| 169 | - $result = $mailer->send( |
|
| 170 | - apply_filters( 'getpaid_invoice_email_recipients', wpinv_parse_list( $recipients ), $email ), |
|
| 171 | - $email->add_merge_tags( $email->get_subject(), $merge_tags ), |
|
| 172 | - $email->get_content( $merge_tags, $extra_args ), |
|
| 173 | - $email->get_attachments() |
|
| 174 | - ); |
|
| 175 | - |
|
| 176 | - // Maybe send a copy to the admin. |
|
| 177 | - if ( $email->include_admin_bcc() ) { |
|
| 178 | - $mailer->send( |
|
| 179 | - wpinv_get_admin_email(), |
|
| 180 | - $email->add_merge_tags( $email->get_subject() . __( ' - ADMIN BCC COPY', 'invoicing' ), $merge_tags ), |
|
| 181 | - $email->get_content( $merge_tags ), |
|
| 182 | - $email->get_attachments() |
|
| 183 | - ); |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - if ( ! $result ) { |
|
| 187 | - $invoice->add_note( sprintf( __( 'Failed sending %s notification email.', 'invoicing' ), sanitize_key( $type ) ), false, false, true ); |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - do_action( 'getpaid_after_send_invoice_notification', $type, $invoice, $email ); |
|
| 191 | - |
|
| 192 | - return $result; |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - /** |
|
| 196 | - * Also send emails to any cc users. |
|
| 197 | - * |
|
| 198 | - * @param array $recipients |
|
| 199 | - * @param GetPaid_Notification_Email $email |
|
| 200 | - */ |
|
| 201 | - public function filter_email_recipients( $recipients, $email ) { |
|
| 202 | - |
|
| 203 | - if ( ! $email->is_admin_email() ) { |
|
| 204 | - $cc = $email->object->get_email_cc(); |
|
| 205 | - |
|
| 206 | - if ( ! empty( $cc ) ) { |
|
| 207 | - $cc = array_map( 'sanitize_email', wpinv_parse_list( $cc ) ); |
|
| 208 | - $recipients = array_filter( array_unique( array_merge( $recipients, $cc ) ) ); |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - return $recipients; |
|
| 214 | - |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - /** |
|
| 218 | - * Sends a new invoice notification. |
|
| 219 | - * |
|
| 220 | - * @param WPInv_Invoice $invoice |
|
| 221 | - */ |
|
| 222 | - public function new_invoice( $invoice ) { |
|
| 223 | - |
|
| 224 | - // Only send this email for invoices created via the admin page. |
|
| 225 | - if ( ! $invoice->is_type( 'invoice' ) || $this->is_payment_form_invoice( $invoice->get_id() ) ) { |
|
| 226 | - return; |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 230 | - $recipient = wpinv_get_admin_email(); |
|
| 231 | - |
|
| 232 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 233 | - |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - /** |
|
| 237 | - * Sends a cancelled invoice notification. |
|
| 238 | - * |
|
| 239 | - * @param WPInv_Invoice $invoice |
|
| 240 | - */ |
|
| 241 | - 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 | + $mailer = new GetPaid_Notification_Email_Sender(); |
|
| 167 | + $merge_tags = $email->get_merge_tags(); |
|
| 168 | + |
|
| 169 | + $result = $mailer->send( |
|
| 170 | + apply_filters( 'getpaid_invoice_email_recipients', wpinv_parse_list( $recipients ), $email ), |
|
| 171 | + $email->add_merge_tags( $email->get_subject(), $merge_tags ), |
|
| 172 | + $email->get_content( $merge_tags, $extra_args ), |
|
| 173 | + $email->get_attachments() |
|
| 174 | + ); |
|
| 175 | + |
|
| 176 | + // Maybe send a copy to the admin. |
|
| 177 | + if ( $email->include_admin_bcc() ) { |
|
| 178 | + $mailer->send( |
|
| 179 | + wpinv_get_admin_email(), |
|
| 180 | + $email->add_merge_tags( $email->get_subject() . __( ' - ADMIN BCC COPY', 'invoicing' ), $merge_tags ), |
|
| 181 | + $email->get_content( $merge_tags ), |
|
| 182 | + $email->get_attachments() |
|
| 183 | + ); |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + if ( ! $result ) { |
|
| 187 | + $invoice->add_note( sprintf( __( 'Failed sending %s notification email.', 'invoicing' ), sanitize_key( $type ) ), false, false, true ); |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + do_action( 'getpaid_after_send_invoice_notification', $type, $invoice, $email ); |
|
| 191 | + |
|
| 192 | + return $result; |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + /** |
|
| 196 | + * Also send emails to any cc users. |
|
| 197 | + * |
|
| 198 | + * @param array $recipients |
|
| 199 | + * @param GetPaid_Notification_Email $email |
|
| 200 | + */ |
|
| 201 | + public function filter_email_recipients( $recipients, $email ) { |
|
| 202 | + |
|
| 203 | + if ( ! $email->is_admin_email() ) { |
|
| 204 | + $cc = $email->object->get_email_cc(); |
|
| 205 | + |
|
| 206 | + if ( ! empty( $cc ) ) { |
|
| 207 | + $cc = array_map( 'sanitize_email', wpinv_parse_list( $cc ) ); |
|
| 208 | + $recipients = array_filter( array_unique( array_merge( $recipients, $cc ) ) ); |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + return $recipients; |
|
| 214 | + |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + /** |
|
| 218 | + * Sends a new invoice notification. |
|
| 219 | + * |
|
| 220 | + * @param WPInv_Invoice $invoice |
|
| 221 | + */ |
|
| 222 | + public function new_invoice( $invoice ) { |
|
| 223 | + |
|
| 224 | + // Only send this email for invoices created via the admin page. |
|
| 225 | + if ( ! $invoice->is_type( 'invoice' ) || $this->is_payment_form_invoice( $invoice->get_id() ) ) { |
|
| 226 | + return; |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 230 | + $recipient = wpinv_get_admin_email(); |
|
| 231 | + |
|
| 232 | + return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 233 | + |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + /** |
|
| 237 | + * Sends a cancelled invoice notification. |
|
| 238 | + * |
|
| 239 | + * @param WPInv_Invoice $invoice |
|
| 240 | + */ |
|
| 241 | + public function cancelled_invoice( $invoice ) { |
|
| 242 | 242 | |
| 243 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 244 | - $recipient = wpinv_get_admin_email(); |
|
| 243 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 244 | + $recipient = wpinv_get_admin_email(); |
|
| 245 | 245 | |
| 246 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 246 | + return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 247 | 247 | |
| 248 | - } |
|
| 248 | + } |
|
| 249 | 249 | |
| 250 | - /** |
|
| 251 | - * Sends a failed invoice notification. |
|
| 252 | - * |
|
| 253 | - * @param WPInv_Invoice $invoice |
|
| 254 | - */ |
|
| 255 | - public function failed_invoice( $invoice ) { |
|
| 250 | + /** |
|
| 251 | + * Sends a failed invoice notification. |
|
| 252 | + * |
|
| 253 | + * @param WPInv_Invoice $invoice |
|
| 254 | + */ |
|
| 255 | + public function failed_invoice( $invoice ) { |
|
| 256 | 256 | |
| 257 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 258 | - $recipient = wpinv_get_admin_email(); |
|
| 257 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 258 | + $recipient = wpinv_get_admin_email(); |
|
| 259 | 259 | |
| 260 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 260 | + return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 261 | 261 | |
| 262 | - } |
|
| 262 | + } |
|
| 263 | 263 | |
| 264 | - /** |
|
| 265 | - * Sends a notification whenever an invoice is put on hold. |
|
| 266 | - * |
|
| 267 | - * @param WPInv_Invoice $invoice |
|
| 268 | - */ |
|
| 269 | - public function onhold_invoice( $invoice ) { |
|
| 264 | + /** |
|
| 265 | + * Sends a notification whenever an invoice is put on hold. |
|
| 266 | + * |
|
| 267 | + * @param WPInv_Invoice $invoice |
|
| 268 | + */ |
|
| 269 | + public function onhold_invoice( $invoice ) { |
|
| 270 | 270 | |
| 271 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 272 | - $recipient = $invoice->get_email(); |
|
| 271 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 272 | + $recipient = $invoice->get_email(); |
|
| 273 | 273 | |
| 274 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 274 | + return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 275 | 275 | |
| 276 | - } |
|
| 276 | + } |
|
| 277 | 277 | |
| 278 | - /** |
|
| 279 | - * Sends a notification whenever an invoice is marked as processing payment. |
|
| 280 | - * |
|
| 281 | - * @param WPInv_Invoice $invoice |
|
| 282 | - */ |
|
| 283 | - public function processing_invoice( $invoice ) { |
|
| 278 | + /** |
|
| 279 | + * Sends a notification whenever an invoice is marked as processing payment. |
|
| 280 | + * |
|
| 281 | + * @param WPInv_Invoice $invoice |
|
| 282 | + */ |
|
| 283 | + public function processing_invoice( $invoice ) { |
|
| 284 | 284 | |
| 285 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 286 | - $recipient = $invoice->get_email(); |
|
| 285 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 286 | + $recipient = $invoice->get_email(); |
|
| 287 | 287 | |
| 288 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 288 | + return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 289 | 289 | |
| 290 | - } |
|
| 290 | + } |
|
| 291 | 291 | |
| 292 | - /** |
|
| 293 | - * Sends a notification whenever an invoice is paid. |
|
| 294 | - * |
|
| 295 | - * @param WPInv_Invoice $invoice |
|
| 296 | - */ |
|
| 297 | - public function completed_invoice( $invoice ) { |
|
| 292 | + /** |
|
| 293 | + * Sends a notification whenever an invoice is paid. |
|
| 294 | + * |
|
| 295 | + * @param WPInv_Invoice $invoice |
|
| 296 | + */ |
|
| 297 | + public function completed_invoice( $invoice ) { |
|
| 298 | 298 | |
| 299 | - // (Maybe) abort if it is a renewal invoice. |
|
| 300 | - if ( $invoice->is_renewal() && ! wpinv_get_option( 'email_completed_invoice_renewal_active', false ) ) { |
|
| 301 | - return; |
|
| 302 | - } |
|
| 299 | + // (Maybe) abort if it is a renewal invoice. |
|
| 300 | + if ( $invoice->is_renewal() && ! wpinv_get_option( 'email_completed_invoice_renewal_active', false ) ) { |
|
| 301 | + return; |
|
| 302 | + } |
|
| 303 | 303 | |
| 304 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 305 | - $recipient = $invoice->get_email(); |
|
| 304 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 305 | + $recipient = $invoice->get_email(); |
|
| 306 | 306 | |
| 307 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 307 | + return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 308 | 308 | |
| 309 | - } |
|
| 309 | + } |
|
| 310 | 310 | |
| 311 | - /** |
|
| 312 | - * Sends a notification whenever an invoice is refunded. |
|
| 313 | - * |
|
| 314 | - * @param WPInv_Invoice $invoice |
|
| 315 | - */ |
|
| 316 | - public function refunded_invoice( $invoice ) { |
|
| 311 | + /** |
|
| 312 | + * Sends a notification whenever an invoice is refunded. |
|
| 313 | + * |
|
| 314 | + * @param WPInv_Invoice $invoice |
|
| 315 | + */ |
|
| 316 | + public function refunded_invoice( $invoice ) { |
|
| 317 | 317 | |
| 318 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 319 | - $recipient = $invoice->get_email(); |
|
| 318 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 319 | + $recipient = $invoice->get_email(); |
|
| 320 | 320 | |
| 321 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 321 | + return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 322 | 322 | |
| 323 | - } |
|
| 323 | + } |
|
| 324 | 324 | |
| 325 | - /** |
|
| 326 | - * Notifies a user about new invoices |
|
| 327 | - * |
|
| 328 | - * @param WPInv_Invoice $invoice |
|
| 329 | - */ |
|
| 330 | - public function user_invoice( $invoice ) { |
|
| 325 | + /** |
|
| 326 | + * Notifies a user about new invoices |
|
| 327 | + * |
|
| 328 | + * @param WPInv_Invoice $invoice |
|
| 329 | + */ |
|
| 330 | + public function user_invoice( $invoice ) { |
|
| 331 | 331 | |
| 332 | - // Only send this email for invoices created via the admin page. |
|
| 333 | - if ( ! $invoice->is_type( 'invoice' ) || $this->is_payment_form_invoice( $invoice->get_id() ) ) { |
|
| 334 | - return; |
|
| 335 | - } |
|
| 332 | + // Only send this email for invoices created via the admin page. |
|
| 333 | + if ( ! $invoice->is_type( 'invoice' ) || $this->is_payment_form_invoice( $invoice->get_id() ) ) { |
|
| 334 | + return; |
|
| 335 | + } |
|
| 336 | 336 | |
| 337 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 338 | - $recipient = $invoice->get_email(); |
|
| 337 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 338 | + $recipient = $invoice->get_email(); |
|
| 339 | 339 | |
| 340 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 340 | + return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 341 | 341 | |
| 342 | - } |
|
| 342 | + } |
|
| 343 | 343 | |
| 344 | - /** |
|
| 345 | - * Checks if an invoice is a payment form invoice. |
|
| 346 | - * |
|
| 347 | - * @param int $invoice |
|
| 348 | - * @return bool |
|
| 349 | - */ |
|
| 350 | - public function is_payment_form_invoice( $invoice ) { |
|
| 351 | - return empty( $_GET['getpaid-admin-action'] ) && 'payment_form' == get_post_meta( $invoice, 'wpinv_created_via', true ); |
|
| 352 | - } |
|
| 344 | + /** |
|
| 345 | + * Checks if an invoice is a payment form invoice. |
|
| 346 | + * |
|
| 347 | + * @param int $invoice |
|
| 348 | + * @return bool |
|
| 349 | + */ |
|
| 350 | + public function is_payment_form_invoice( $invoice ) { |
|
| 351 | + return empty( $_GET['getpaid-admin-action'] ) && 'payment_form' == get_post_meta( $invoice, 'wpinv_created_via', true ); |
|
| 352 | + } |
|
| 353 | 353 | |
| 354 | - /** |
|
| 355 | - * Notifies admin about new invoice notes |
|
| 356 | - * |
|
| 357 | - * @param WPInv_Invoice $invoice |
|
| 358 | - * @param string $note |
|
| 359 | - */ |
|
| 360 | - public function user_note( $invoice, $note ) { |
|
| 354 | + /** |
|
| 355 | + * Notifies admin about new invoice notes |
|
| 356 | + * |
|
| 357 | + * @param WPInv_Invoice $invoice |
|
| 358 | + * @param string $note |
|
| 359 | + */ |
|
| 360 | + public function user_note( $invoice, $note ) { |
|
| 361 | 361 | |
| 362 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 363 | - $recipient = $invoice->get_email(); |
|
| 364 | - |
|
| 365 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient, array( 'customer_note' => $note ) ); |
|
| 366 | - |
|
| 367 | - } |
|
| 362 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 363 | + $recipient = $invoice->get_email(); |
|
| 364 | + |
|
| 365 | + return $this->send_email( $invoice, $email, __FUNCTION__, $recipient, array( 'customer_note' => $note ) ); |
|
| 366 | + |
|
| 367 | + } |
|
| 368 | 368 | |
| 369 | - /** |
|
| 370 | - * (Force) Sends overdue notices. |
|
| 371 | - * |
|
| 372 | - * @param WPInv_Invoice $invoice |
|
| 373 | - */ |
|
| 374 | - public function force_send_overdue_notice( $invoice ) { |
|
| 375 | - $email = new GetPaid_Notification_Email( 'overdue', $invoice ); |
|
| 376 | - return $this->send_email( $invoice, $email, 'overdue', $invoice->get_email() ); |
|
| 377 | - } |
|
| 378 | - |
|
| 379 | - /** |
|
| 380 | - * Sends overdue notices. |
|
| 381 | - * |
|
| 382 | - * @TODO: Create an invoices query class. |
|
| 383 | - */ |
|
| 384 | - public function overdue() { |
|
| 385 | - global $wpdb; |
|
| 386 | - |
|
| 387 | - $email = new GetPaid_Notification_Email( __FUNCTION__ ); |
|
| 388 | - |
|
| 389 | - // Fetch reminder days. |
|
| 390 | - $reminder_days = array_unique( wp_parse_id_list( $email->get_option( 'days' ) ) ); |
|
| 391 | - |
|
| 392 | - // Abort if non is set. |
|
| 393 | - if ( empty( $reminder_days ) ) { |
|
| 394 | - return; |
|
| 395 | - } |
|
| 396 | - |
|
| 397 | - // Retrieve date query. |
|
| 398 | - $date_query = $this->get_date_query( $reminder_days ); |
|
| 399 | - |
|
| 400 | - // Invoices table. |
|
| 401 | - $table = $wpdb->prefix . 'getpaid_invoices'; |
|
| 402 | - |
|
| 403 | - // Fetch invoices. |
|
| 404 | - $invoices = $wpdb->get_col( |
|
| 405 | - "SELECT posts.ID FROM $wpdb->posts as posts |
|
| 369 | + /** |
|
| 370 | + * (Force) Sends overdue notices. |
|
| 371 | + * |
|
| 372 | + * @param WPInv_Invoice $invoice |
|
| 373 | + */ |
|
| 374 | + public function force_send_overdue_notice( $invoice ) { |
|
| 375 | + $email = new GetPaid_Notification_Email( 'overdue', $invoice ); |
|
| 376 | + return $this->send_email( $invoice, $email, 'overdue', $invoice->get_email() ); |
|
| 377 | + } |
|
| 378 | + |
|
| 379 | + /** |
|
| 380 | + * Sends overdue notices. |
|
| 381 | + * |
|
| 382 | + * @TODO: Create an invoices query class. |
|
| 383 | + */ |
|
| 384 | + public function overdue() { |
|
| 385 | + global $wpdb; |
|
| 386 | + |
|
| 387 | + $email = new GetPaid_Notification_Email( __FUNCTION__ ); |
|
| 388 | + |
|
| 389 | + // Fetch reminder days. |
|
| 390 | + $reminder_days = array_unique( wp_parse_id_list( $email->get_option( 'days' ) ) ); |
|
| 391 | + |
|
| 392 | + // Abort if non is set. |
|
| 393 | + if ( empty( $reminder_days ) ) { |
|
| 394 | + return; |
|
| 395 | + } |
|
| 396 | + |
|
| 397 | + // Retrieve date query. |
|
| 398 | + $date_query = $this->get_date_query( $reminder_days ); |
|
| 399 | + |
|
| 400 | + // Invoices table. |
|
| 401 | + $table = $wpdb->prefix . 'getpaid_invoices'; |
|
| 402 | + |
|
| 403 | + // Fetch invoices. |
|
| 404 | + $invoices = $wpdb->get_col( |
|
| 405 | + "SELECT posts.ID FROM $wpdb->posts as posts |
|
| 406 | 406 | LEFT JOIN $table as invoices ON invoices.post_id = posts.ID |
| 407 | 407 | WHERE posts.post_type = 'wpi_invoice' AND posts.post_status = 'wpi-pending' $date_query"); |
| 408 | 408 | |
| 409 | - foreach ( $invoices as $invoice ) { |
|
| 409 | + foreach ( $invoices as $invoice ) { |
|
| 410 | 410 | |
| 411 | - // Only send this email for invoices created via the admin page. |
|
| 412 | - if ( ! $this->is_payment_form_invoice( $invoice ) ) { |
|
| 413 | - $invoice = new WPInv_Invoice( $invoice ); |
|
| 414 | - $email->object = $invoice; |
|
| 411 | + // Only send this email for invoices created via the admin page. |
|
| 412 | + if ( ! $this->is_payment_form_invoice( $invoice ) ) { |
|
| 413 | + $invoice = new WPInv_Invoice( $invoice ); |
|
| 414 | + $email->object = $invoice; |
|
| 415 | 415 | |
| 416 | - if ( $invoice->needs_payment() ) { |
|
| 417 | - $this->send_email( $invoice, $email, __FUNCTION__, $invoice->get_email() ); |
|
| 418 | - } |
|
| 416 | + if ( $invoice->needs_payment() ) { |
|
| 417 | + $this->send_email( $invoice, $email, __FUNCTION__, $invoice->get_email() ); |
|
| 418 | + } |
|
| 419 | 419 | |
| 420 | - } |
|
| 420 | + } |
|
| 421 | 421 | |
| 422 | - } |
|
| 422 | + } |
|
| 423 | 423 | |
| 424 | - } |
|
| 424 | + } |
|
| 425 | 425 | |
| 426 | - /** |
|
| 427 | - * Calculates the date query for an invoices query |
|
| 428 | - * |
|
| 429 | - * @param array $reminder_days |
|
| 430 | - * @return string |
|
| 431 | - */ |
|
| 432 | - public function get_date_query( $reminder_days ) { |
|
| 426 | + /** |
|
| 427 | + * Calculates the date query for an invoices query |
|
| 428 | + * |
|
| 429 | + * @param array $reminder_days |
|
| 430 | + * @return string |
|
| 431 | + */ |
|
| 432 | + public function get_date_query( $reminder_days ) { |
|
| 433 | 433 | |
| 434 | - $date_query = array( |
|
| 435 | - 'relation' => 'OR' |
|
| 436 | - ); |
|
| 434 | + $date_query = array( |
|
| 435 | + 'relation' => 'OR' |
|
| 436 | + ); |
|
| 437 | 437 | |
| 438 | - foreach ( $reminder_days as $days ) { |
|
| 439 | - $date = date_parse( date( 'Y-m-d', strtotime( "-$days days", current_time( 'timestamp' ) ) ) ); |
|
| 438 | + foreach ( $reminder_days as $days ) { |
|
| 439 | + $date = date_parse( date( 'Y-m-d', strtotime( "-$days days", current_time( 'timestamp' ) ) ) ); |
|
| 440 | 440 | |
| 441 | - $date_query[] = array( |
|
| 442 | - 'year' => $date['year'], |
|
| 443 | - 'month' => $date['month'], |
|
| 444 | - 'day' => $date['day'], |
|
| 445 | - ); |
|
| 441 | + $date_query[] = array( |
|
| 442 | + 'year' => $date['year'], |
|
| 443 | + 'month' => $date['month'], |
|
| 444 | + 'day' => $date['day'], |
|
| 445 | + ); |
|
| 446 | 446 | |
| 447 | - } |
|
| 447 | + } |
|
| 448 | 448 | |
| 449 | - $date_query = new WP_Date_Query( $date_query, 'invoices.due_date' ); |
|
| 449 | + $date_query = new WP_Date_Query( $date_query, 'invoices.due_date' ); |
|
| 450 | 450 | |
| 451 | - return $date_query->get_sql(); |
|
| 451 | + return $date_query->get_sql(); |
|
| 452 | 452 | |
| 453 | - } |
|
| 453 | + } |
|
| 454 | 454 | |
| 455 | 455 | } |
@@ -13,467 +13,467 @@ discard block |
||
| 13 | 13 | */ |
| 14 | 14 | abstract class GetPaid_Payment_Gateway { |
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Set if the place checkout button should be renamed on selection. |
|
| 18 | - * |
|
| 19 | - * @var string |
|
| 20 | - */ |
|
| 21 | - public $checkout_button_text; |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * Boolean whether the method is enabled. |
|
| 25 | - * |
|
| 26 | - * @var bool |
|
| 27 | - */ |
|
| 28 | - public $enabled = true; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * Payment method id. |
|
| 32 | - * |
|
| 33 | - * @var string |
|
| 34 | - */ |
|
| 35 | - public $id; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * Payment method order. |
|
| 39 | - * |
|
| 40 | - * @var int |
|
| 41 | - */ |
|
| 42 | - public $order = 10; |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * Payment method title for the frontend. |
|
| 46 | - * |
|
| 47 | - * @var string |
|
| 48 | - */ |
|
| 49 | - public $title; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * Payment method description for the frontend. |
|
| 53 | - * |
|
| 54 | - * @var string |
|
| 55 | - */ |
|
| 56 | - public $description; |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * Gateway title. |
|
| 60 | - * |
|
| 61 | - * @var string |
|
| 62 | - */ |
|
| 63 | - public $method_title = ''; |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * Gateway description. |
|
| 67 | - * |
|
| 68 | - * @var string |
|
| 69 | - */ |
|
| 70 | - public $method_description = ''; |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * Countries this gateway is allowed for. |
|
| 74 | - * |
|
| 75 | - * @var array |
|
| 76 | - */ |
|
| 77 | - public $countries; |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * Currencies this gateway is allowed for. |
|
| 81 | - * |
|
| 82 | - * @var array |
|
| 83 | - */ |
|
| 84 | - public $currencies; |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * Currencies this gateway is not allowed for. |
|
| 88 | - * |
|
| 89 | - * @var array |
|
| 90 | - */ |
|
| 91 | - public $exclude_currencies; |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * Maximum transaction amount, zero does not define a maximum. |
|
| 95 | - * |
|
| 96 | - * @var int |
|
| 97 | - */ |
|
| 98 | - public $max_amount = 0; |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * Optional URL to view a transaction. |
|
| 102 | - * |
|
| 103 | - * @var string |
|
| 104 | - */ |
|
| 105 | - public $view_transaction_url = ''; |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * Optional URL to view a subscription. |
|
| 109 | - * |
|
| 110 | - * @var string |
|
| 111 | - */ |
|
| 112 | - public $view_subscription_url = ''; |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * Optional label to show for "new payment method" in the payment |
|
| 116 | - * method/token selection radio selection. |
|
| 117 | - * |
|
| 118 | - * @var string |
|
| 119 | - */ |
|
| 120 | - public $new_method_label = ''; |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * Contains a user's saved tokens for this gateway. |
|
| 124 | - * |
|
| 125 | - * @var array |
|
| 126 | - */ |
|
| 127 | - protected $tokens = array(); |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * An array of features that this gateway supports. |
|
| 131 | - * |
|
| 132 | - * @var array |
|
| 133 | - */ |
|
| 134 | - protected $supports = array(); |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Class constructor. |
|
| 138 | - */ |
|
| 139 | - public function __construct() { |
|
| 140 | - |
|
| 141 | - // Register gateway. |
|
| 142 | - add_filter( 'wpinv_payment_gateways', array( $this, 'register_gateway' ) ); |
|
| 143 | - |
|
| 144 | - $this->enabled = wpinv_is_gateway_active( $this->id ); |
|
| 145 | - |
|
| 146 | - // Enable Subscriptions. |
|
| 147 | - if ( $this->supports( 'subscription' ) ) { |
|
| 148 | - add_filter( "wpinv_{$this->id}_support_subscription", '__return_true' ); |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - // Enable sandbox. |
|
| 152 | - if ( $this->supports( 'sandbox' ) ) { |
|
| 153 | - add_filter( "wpinv_{$this->id}_supports_sandbox", '__return_true' ); |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - // Invoice addons. |
|
| 157 | - if ( $this->supports( 'addons' ) ) { |
|
| 158 | - add_filter( "getpaid_{$this->id}_supports_addons", '__return_true' ); |
|
| 159 | - add_action( "getpaid_process_{$this->id}_invoice_addons", array( $this, 'process_addons' ), 10, 2 ); |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - // Gateway settings. |
|
| 163 | - add_filter( "wpinv_gateway_settings_{$this->id}", array( $this, 'admin_settings' ) ); |
|
| 16 | + /** |
|
| 17 | + * Set if the place checkout button should be renamed on selection. |
|
| 18 | + * |
|
| 19 | + * @var string |
|
| 20 | + */ |
|
| 21 | + public $checkout_button_text; |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * Boolean whether the method is enabled. |
|
| 25 | + * |
|
| 26 | + * @var bool |
|
| 27 | + */ |
|
| 28 | + public $enabled = true; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * Payment method id. |
|
| 32 | + * |
|
| 33 | + * @var string |
|
| 34 | + */ |
|
| 35 | + public $id; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * Payment method order. |
|
| 39 | + * |
|
| 40 | + * @var int |
|
| 41 | + */ |
|
| 42 | + public $order = 10; |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * Payment method title for the frontend. |
|
| 46 | + * |
|
| 47 | + * @var string |
|
| 48 | + */ |
|
| 49 | + public $title; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * Payment method description for the frontend. |
|
| 53 | + * |
|
| 54 | + * @var string |
|
| 55 | + */ |
|
| 56 | + public $description; |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * Gateway title. |
|
| 60 | + * |
|
| 61 | + * @var string |
|
| 62 | + */ |
|
| 63 | + public $method_title = ''; |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Gateway description. |
|
| 67 | + * |
|
| 68 | + * @var string |
|
| 69 | + */ |
|
| 70 | + public $method_description = ''; |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * Countries this gateway is allowed for. |
|
| 74 | + * |
|
| 75 | + * @var array |
|
| 76 | + */ |
|
| 77 | + public $countries; |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * Currencies this gateway is allowed for. |
|
| 81 | + * |
|
| 82 | + * @var array |
|
| 83 | + */ |
|
| 84 | + public $currencies; |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * Currencies this gateway is not allowed for. |
|
| 88 | + * |
|
| 89 | + * @var array |
|
| 90 | + */ |
|
| 91 | + public $exclude_currencies; |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Maximum transaction amount, zero does not define a maximum. |
|
| 95 | + * |
|
| 96 | + * @var int |
|
| 97 | + */ |
|
| 98 | + public $max_amount = 0; |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * Optional URL to view a transaction. |
|
| 102 | + * |
|
| 103 | + * @var string |
|
| 104 | + */ |
|
| 105 | + public $view_transaction_url = ''; |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * Optional URL to view a subscription. |
|
| 109 | + * |
|
| 110 | + * @var string |
|
| 111 | + */ |
|
| 112 | + public $view_subscription_url = ''; |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * Optional label to show for "new payment method" in the payment |
|
| 116 | + * method/token selection radio selection. |
|
| 117 | + * |
|
| 118 | + * @var string |
|
| 119 | + */ |
|
| 120 | + public $new_method_label = ''; |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * Contains a user's saved tokens for this gateway. |
|
| 124 | + * |
|
| 125 | + * @var array |
|
| 126 | + */ |
|
| 127 | + protected $tokens = array(); |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * An array of features that this gateway supports. |
|
| 131 | + * |
|
| 132 | + * @var array |
|
| 133 | + */ |
|
| 134 | + protected $supports = array(); |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Class constructor. |
|
| 138 | + */ |
|
| 139 | + public function __construct() { |
|
| 140 | + |
|
| 141 | + // Register gateway. |
|
| 142 | + add_filter( 'wpinv_payment_gateways', array( $this, 'register_gateway' ) ); |
|
| 143 | + |
|
| 144 | + $this->enabled = wpinv_is_gateway_active( $this->id ); |
|
| 145 | + |
|
| 146 | + // Enable Subscriptions. |
|
| 147 | + if ( $this->supports( 'subscription' ) ) { |
|
| 148 | + add_filter( "wpinv_{$this->id}_support_subscription", '__return_true' ); |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + // Enable sandbox. |
|
| 152 | + if ( $this->supports( 'sandbox' ) ) { |
|
| 153 | + add_filter( "wpinv_{$this->id}_supports_sandbox", '__return_true' ); |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + // Invoice addons. |
|
| 157 | + if ( $this->supports( 'addons' ) ) { |
|
| 158 | + add_filter( "getpaid_{$this->id}_supports_addons", '__return_true' ); |
|
| 159 | + add_action( "getpaid_process_{$this->id}_invoice_addons", array( $this, 'process_addons' ), 10, 2 ); |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + // Gateway settings. |
|
| 163 | + add_filter( "wpinv_gateway_settings_{$this->id}", array( $this, 'admin_settings' ) ); |
|
| 164 | 164 | |
| 165 | 165 | |
| 166 | - // Gateway checkout fiellds. |
|
| 167 | - add_action( "wpinv_{$this->id}_cc_form", array( $this, 'payment_fields' ), 10, 2 ); |
|
| 168 | - |
|
| 169 | - // Process payment. |
|
| 170 | - add_action( "getpaid_gateway_{$this->id}", array( $this, 'process_payment' ), 10, 3 ); |
|
| 166 | + // Gateway checkout fiellds. |
|
| 167 | + add_action( "wpinv_{$this->id}_cc_form", array( $this, 'payment_fields' ), 10, 2 ); |
|
| 168 | + |
|
| 169 | + // Process payment. |
|
| 170 | + add_action( "getpaid_gateway_{$this->id}", array( $this, 'process_payment' ), 10, 3 ); |
|
| 171 | + |
|
| 172 | + // Change the checkout button text. |
|
| 173 | + if ( ! empty( $this->checkout_button_text ) ) { |
|
| 174 | + add_filter( "getpaid_gateway_{$this->id}_checkout_button_label", array( $this, 'rename_checkout_button' ) ); |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + // Check if a gateway is valid for a given currency. |
|
| 178 | + add_filter( "getpaid_gateway_{$this->id}_is_valid_for_currency", array( $this, 'validate_currency' ), 10, 2 ); |
|
| 179 | + |
|
| 180 | + // Generate the transaction url. |
|
| 181 | + add_filter( "getpaid_gateway_{$this->id}_transaction_url", array( $this, 'filter_transaction_url' ), 10, 2 ); |
|
| 182 | + |
|
| 183 | + // Generate the subscription url. |
|
| 184 | + add_filter( "getpaid_gateway_{$this->id}_subscription_url", array( $this, 'filter_subscription_url' ), 10, 2 ); |
|
| 185 | + |
|
| 186 | + // Confirm payments. |
|
| 187 | + add_filter( "wpinv_payment_confirm_{$this->id}", array( $this, 'confirm_payment' ), 10, 2 ); |
|
| 188 | + |
|
| 189 | + // Verify IPNs. |
|
| 190 | + add_action( "wpinv_verify_{$this->id}_ipn", array( $this, 'verify_ipn' ) ); |
|
| 191 | + |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + /** |
|
| 195 | + * Checks if this gateway is a given gateway. |
|
| 196 | + * |
|
| 197 | + * @since 1.0.19 |
|
| 198 | + * @return bool |
|
| 199 | + */ |
|
| 200 | + public function is( $gateway ) { |
|
| 201 | + return $gateway == $this->id; |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + /** |
|
| 205 | + * Returns a users saved tokens for this gateway. |
|
| 206 | + * |
|
| 207 | + * @since 1.0.19 |
|
| 208 | + * @return array |
|
| 209 | + */ |
|
| 210 | + public function get_tokens( $sandbox = null ) { |
|
| 211 | + |
|
| 212 | + if ( is_user_logged_in() && $this->supports( 'tokens' ) && 0 == count( $this->tokens ) ) { |
|
| 213 | + $tokens = get_user_meta( get_current_user_id(), "getpaid_{$this->id}_tokens", true ); |
|
| 214 | + |
|
| 215 | + if ( is_array( $tokens ) ) { |
|
| 216 | + $this->tokens = $tokens; |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + if ( ! is_bool( $sandbox ) ) { |
|
| 222 | + return $this->tokens; |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + $args = array( 'type' => $sandbox ? 'sandbox' : 'live' ); |
|
| 226 | + return wp_list_filter( $this->tokens, $args ); |
|
| 227 | + |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + /** |
|
| 231 | + * Saves a token for this gateway. |
|
| 232 | + * |
|
| 233 | + * @since 1.0.19 |
|
| 234 | + */ |
|
| 235 | + public function save_token( $token ) { |
|
| 236 | + |
|
| 237 | + $tokens = $this->get_tokens(); |
|
| 238 | + $tokens[] = $token; |
|
| 239 | + |
|
| 240 | + update_user_meta( get_current_user_id(), "getpaid_{$this->id}_tokens", $tokens ); |
|
| 241 | + |
|
| 242 | + $this->tokens = $tokens; |
|
| 243 | + |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + /** |
|
| 247 | + * Return the title for admin screens. |
|
| 248 | + * |
|
| 249 | + * @return string |
|
| 250 | + */ |
|
| 251 | + public function get_method_title() { |
|
| 252 | + return apply_filters( 'getpaid_gateway_method_title', $this->method_title, $this ); |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + /** |
|
| 256 | + * Return the description for admin screens. |
|
| 257 | + * |
|
| 258 | + * @return string |
|
| 259 | + */ |
|
| 260 | + public function get_method_description() { |
|
| 261 | + return apply_filters( 'getpaid_gateway_method_description', $this->method_description, $this ); |
|
| 262 | + } |
|
| 263 | + |
|
| 264 | + /** |
|
| 265 | + * Get the success url. |
|
| 266 | + * |
|
| 267 | + * @param WPInv_Invoice $invoice Invoice object. |
|
| 268 | + * @return string |
|
| 269 | + */ |
|
| 270 | + public function get_return_url( $invoice ) { |
|
| 271 | + |
|
| 272 | + // Payment success url |
|
| 273 | + $return_url = add_query_arg( |
|
| 274 | + array( |
|
| 275 | + 'payment-confirm' => $this->id, |
|
| 276 | + 'invoice_key' => $invoice->get_key(), |
|
| 277 | + 'utm_nooverride' => 1 |
|
| 278 | + ), |
|
| 279 | + wpinv_get_success_page_uri() |
|
| 280 | + ); |
|
| 281 | + |
|
| 282 | + return apply_filters( 'getpaid_gateway_success_url', $return_url, $invoice, $this ); |
|
| 283 | + } |
|
| 284 | + |
|
| 285 | + /** |
|
| 286 | + * Confirms payments when rendering the success page. |
|
| 287 | + * |
|
| 288 | + * @param string $content Success page content. |
|
| 289 | + * @return string |
|
| 290 | + */ |
|
| 291 | + public function confirm_payment( $content ) { |
|
| 292 | + |
|
| 293 | + // Retrieve the invoice. |
|
| 294 | + $invoice_id = getpaid_get_current_invoice_id(); |
|
| 295 | + $invoice = wpinv_get_invoice( $invoice_id ); |
|
| 296 | + |
|
| 297 | + // Ensure that it exists and that it is pending payment. |
|
| 298 | + if ( empty( $invoice_id ) || ! $invoice->needs_payment() ) { |
|
| 299 | + return $content; |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + // Can the user view this invoice?? |
|
| 303 | + if ( ! wpinv_user_can_view_invoice( $invoice ) ) { |
|
| 304 | + return $content; |
|
| 305 | + } |
|
| 306 | + |
|
| 307 | + // Show payment processing indicator. |
|
| 308 | + return wpinv_get_template_html( 'wpinv-payment-processing.php', compact( 'invoice' ) ); |
|
| 309 | + } |
|
| 310 | + |
|
| 311 | + /** |
|
| 312 | + * Processes ipns and marks payments as complete. |
|
| 313 | + * |
|
| 314 | + * @return void |
|
| 315 | + */ |
|
| 316 | + public function verify_ipn() {} |
|
| 317 | + |
|
| 318 | + /** |
|
| 319 | + * Processes invoice addons. |
|
| 320 | + * |
|
| 321 | + * @param WPInv_Invoice $invoice |
|
| 322 | + * @param GetPaid_Form_Item[] $items |
|
| 323 | + * @return WPInv_Invoice |
|
| 324 | + */ |
|
| 325 | + public function process_addons( $invoice, $items ) { |
|
| 326 | + |
|
| 327 | + } |
|
| 328 | + |
|
| 329 | + /** |
|
| 330 | + * Get a link to the transaction on the 3rd party gateway site (if applicable). |
|
| 331 | + * |
|
| 332 | + * @param string $transaction_url transaction url. |
|
| 333 | + * @param WPInv_Invoice $invoice Invoice object. |
|
| 334 | + * @return string transaction URL, or empty string. |
|
| 335 | + */ |
|
| 336 | + public function filter_transaction_url( $transaction_url, $invoice ) { |
|
| 337 | + |
|
| 338 | + $transaction_id = $invoice->get_transaction_id(); |
|
| 339 | + |
|
| 340 | + if ( ! empty( $this->view_transaction_url ) && ! empty( $transaction_id ) ) { |
|
| 341 | + $transaction_url = sprintf( $this->view_transaction_url, $transaction_id ); |
|
| 342 | + $replace = $this->is_sandbox( $invoice ) ? 'sandbox' : ''; |
|
| 343 | + $transaction_url = str_replace( '{sandbox}', $replace, $transaction_url ); |
|
| 344 | + } |
|
| 171 | 345 | |
| 172 | - // Change the checkout button text. |
|
| 173 | - if ( ! empty( $this->checkout_button_text ) ) { |
|
| 174 | - add_filter( "getpaid_gateway_{$this->id}_checkout_button_label", array( $this, 'rename_checkout_button' ) ); |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - // Check if a gateway is valid for a given currency. |
|
| 178 | - add_filter( "getpaid_gateway_{$this->id}_is_valid_for_currency", array( $this, 'validate_currency' ), 10, 2 ); |
|
| 179 | - |
|
| 180 | - // Generate the transaction url. |
|
| 181 | - add_filter( "getpaid_gateway_{$this->id}_transaction_url", array( $this, 'filter_transaction_url' ), 10, 2 ); |
|
| 182 | - |
|
| 183 | - // Generate the subscription url. |
|
| 184 | - add_filter( "getpaid_gateway_{$this->id}_subscription_url", array( $this, 'filter_subscription_url' ), 10, 2 ); |
|
| 185 | - |
|
| 186 | - // Confirm payments. |
|
| 187 | - add_filter( "wpinv_payment_confirm_{$this->id}", array( $this, 'confirm_payment' ), 10, 2 ); |
|
| 188 | - |
|
| 189 | - // Verify IPNs. |
|
| 190 | - add_action( "wpinv_verify_{$this->id}_ipn", array( $this, 'verify_ipn' ) ); |
|
| 191 | - |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - /** |
|
| 195 | - * Checks if this gateway is a given gateway. |
|
| 196 | - * |
|
| 197 | - * @since 1.0.19 |
|
| 198 | - * @return bool |
|
| 199 | - */ |
|
| 200 | - public function is( $gateway ) { |
|
| 201 | - return $gateway == $this->id; |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - /** |
|
| 205 | - * Returns a users saved tokens for this gateway. |
|
| 206 | - * |
|
| 207 | - * @since 1.0.19 |
|
| 208 | - * @return array |
|
| 209 | - */ |
|
| 210 | - public function get_tokens( $sandbox = null ) { |
|
| 211 | - |
|
| 212 | - if ( is_user_logged_in() && $this->supports( 'tokens' ) && 0 == count( $this->tokens ) ) { |
|
| 213 | - $tokens = get_user_meta( get_current_user_id(), "getpaid_{$this->id}_tokens", true ); |
|
| 214 | - |
|
| 215 | - if ( is_array( $tokens ) ) { |
|
| 216 | - $this->tokens = $tokens; |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - if ( ! is_bool( $sandbox ) ) { |
|
| 222 | - return $this->tokens; |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - $args = array( 'type' => $sandbox ? 'sandbox' : 'live' ); |
|
| 226 | - return wp_list_filter( $this->tokens, $args ); |
|
| 227 | - |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - /** |
|
| 231 | - * Saves a token for this gateway. |
|
| 232 | - * |
|
| 233 | - * @since 1.0.19 |
|
| 234 | - */ |
|
| 235 | - public function save_token( $token ) { |
|
| 236 | - |
|
| 237 | - $tokens = $this->get_tokens(); |
|
| 238 | - $tokens[] = $token; |
|
| 239 | - |
|
| 240 | - update_user_meta( get_current_user_id(), "getpaid_{$this->id}_tokens", $tokens ); |
|
| 241 | - |
|
| 242 | - $this->tokens = $tokens; |
|
| 243 | - |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - /** |
|
| 247 | - * Return the title for admin screens. |
|
| 248 | - * |
|
| 249 | - * @return string |
|
| 250 | - */ |
|
| 251 | - public function get_method_title() { |
|
| 252 | - return apply_filters( 'getpaid_gateway_method_title', $this->method_title, $this ); |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - /** |
|
| 256 | - * Return the description for admin screens. |
|
| 257 | - * |
|
| 258 | - * @return string |
|
| 259 | - */ |
|
| 260 | - public function get_method_description() { |
|
| 261 | - return apply_filters( 'getpaid_gateway_method_description', $this->method_description, $this ); |
|
| 262 | - } |
|
| 263 | - |
|
| 264 | - /** |
|
| 265 | - * Get the success url. |
|
| 266 | - * |
|
| 267 | - * @param WPInv_Invoice $invoice Invoice object. |
|
| 268 | - * @return string |
|
| 269 | - */ |
|
| 270 | - public function get_return_url( $invoice ) { |
|
| 271 | - |
|
| 272 | - // Payment success url |
|
| 273 | - $return_url = add_query_arg( |
|
| 274 | - array( |
|
| 275 | - 'payment-confirm' => $this->id, |
|
| 276 | - 'invoice_key' => $invoice->get_key(), |
|
| 277 | - 'utm_nooverride' => 1 |
|
| 278 | - ), |
|
| 279 | - wpinv_get_success_page_uri() |
|
| 280 | - ); |
|
| 281 | - |
|
| 282 | - return apply_filters( 'getpaid_gateway_success_url', $return_url, $invoice, $this ); |
|
| 283 | - } |
|
| 284 | - |
|
| 285 | - /** |
|
| 286 | - * Confirms payments when rendering the success page. |
|
| 287 | - * |
|
| 288 | - * @param string $content Success page content. |
|
| 289 | - * @return string |
|
| 290 | - */ |
|
| 291 | - public function confirm_payment( $content ) { |
|
| 292 | - |
|
| 293 | - // Retrieve the invoice. |
|
| 294 | - $invoice_id = getpaid_get_current_invoice_id(); |
|
| 295 | - $invoice = wpinv_get_invoice( $invoice_id ); |
|
| 296 | - |
|
| 297 | - // Ensure that it exists and that it is pending payment. |
|
| 298 | - if ( empty( $invoice_id ) || ! $invoice->needs_payment() ) { |
|
| 299 | - return $content; |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - // Can the user view this invoice?? |
|
| 303 | - if ( ! wpinv_user_can_view_invoice( $invoice ) ) { |
|
| 304 | - return $content; |
|
| 305 | - } |
|
| 306 | - |
|
| 307 | - // Show payment processing indicator. |
|
| 308 | - return wpinv_get_template_html( 'wpinv-payment-processing.php', compact( 'invoice' ) ); |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - /** |
|
| 312 | - * Processes ipns and marks payments as complete. |
|
| 313 | - * |
|
| 314 | - * @return void |
|
| 315 | - */ |
|
| 316 | - public function verify_ipn() {} |
|
| 317 | - |
|
| 318 | - /** |
|
| 319 | - * Processes invoice addons. |
|
| 320 | - * |
|
| 321 | - * @param WPInv_Invoice $invoice |
|
| 322 | - * @param GetPaid_Form_Item[] $items |
|
| 323 | - * @return WPInv_Invoice |
|
| 324 | - */ |
|
| 325 | - public function process_addons( $invoice, $items ) { |
|
| 326 | - |
|
| 327 | - } |
|
| 328 | - |
|
| 329 | - /** |
|
| 330 | - * Get a link to the transaction on the 3rd party gateway site (if applicable). |
|
| 331 | - * |
|
| 332 | - * @param string $transaction_url transaction url. |
|
| 333 | - * @param WPInv_Invoice $invoice Invoice object. |
|
| 334 | - * @return string transaction URL, or empty string. |
|
| 335 | - */ |
|
| 336 | - public function filter_transaction_url( $transaction_url, $invoice ) { |
|
| 337 | - |
|
| 338 | - $transaction_id = $invoice->get_transaction_id(); |
|
| 339 | - |
|
| 340 | - if ( ! empty( $this->view_transaction_url ) && ! empty( $transaction_id ) ) { |
|
| 341 | - $transaction_url = sprintf( $this->view_transaction_url, $transaction_id ); |
|
| 342 | - $replace = $this->is_sandbox( $invoice ) ? 'sandbox' : ''; |
|
| 343 | - $transaction_url = str_replace( '{sandbox}', $replace, $transaction_url ); |
|
| 344 | - } |
|
| 345 | - |
|
| 346 | - return $transaction_url; |
|
| 347 | - } |
|
| 348 | - |
|
| 349 | - /** |
|
| 350 | - * Get a link to the subscription on the 3rd party gateway site (if applicable). |
|
| 351 | - * |
|
| 352 | - * @param string $subscription_url transaction url. |
|
| 353 | - * @param WPInv_Invoice $invoice Invoice object. |
|
| 354 | - * @return string subscription URL, or empty string. |
|
| 355 | - */ |
|
| 356 | - public function filter_subscription_url( $subscription_url, $invoice ) { |
|
| 357 | - |
|
| 358 | - $profile_id = $invoice->get_subscription_id(); |
|
| 359 | - |
|
| 360 | - if ( ! empty( $this->view_subscription_url ) && ! empty( $profile_id ) ) { |
|
| 361 | - |
|
| 362 | - $subscription_url = sprintf( $this->view_subscription_url, $profile_id ); |
|
| 363 | - $replace = $this->is_sandbox( $invoice ) ? 'sandbox' : ''; |
|
| 364 | - $subscription_url = str_replace( '{sandbox}', $replace, $subscription_url ); |
|
| 365 | - |
|
| 366 | - } |
|
| 367 | - |
|
| 368 | - return $subscription_url; |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - /** |
|
| 372 | - * Check if the gateway is available for use. |
|
| 373 | - * |
|
| 374 | - * @return bool |
|
| 375 | - */ |
|
| 376 | - public function is_available() { |
|
| 377 | - return ! empty( $this->enabled ); |
|
| 378 | - } |
|
| 379 | - |
|
| 380 | - /** |
|
| 381 | - * Return the gateway's title. |
|
| 382 | - * |
|
| 383 | - * @return string |
|
| 384 | - */ |
|
| 385 | - public function get_title() { |
|
| 386 | - return apply_filters( 'getpaid_gateway_title', $this->title, $this ); |
|
| 387 | - } |
|
| 388 | - |
|
| 389 | - /** |
|
| 390 | - * Return the gateway's description. |
|
| 391 | - * |
|
| 392 | - * @return string |
|
| 393 | - */ |
|
| 394 | - public function get_description() { |
|
| 395 | - return apply_filters( 'getpaid_gateway_description', $this->description, $this ); |
|
| 396 | - } |
|
| 397 | - |
|
| 398 | - /** |
|
| 399 | - * Process Payment. |
|
| 400 | - * |
|
| 401 | - * |
|
| 402 | - * @param WPInv_Invoice $invoice Invoice. |
|
| 403 | - * @param array $submission_data Posted checkout fields. |
|
| 404 | - * @param GetPaid_Payment_Form_Submission $submission Checkout submission. |
|
| 405 | - * @return void |
|
| 406 | - */ |
|
| 407 | - public function process_payment( $invoice, $submission_data, $submission ) { |
|
| 408 | - // Process the payment then either redirect to the success page or the gateway. |
|
| 409 | - do_action( 'getpaid_process_invoice_payment_' . $this->id, $invoice, $submission_data, $submission ); |
|
| 410 | - } |
|
| 411 | - |
|
| 412 | - /** |
|
| 413 | - * Process refund. |
|
| 414 | - * |
|
| 415 | - * If the gateway declares 'refunds' support, this will allow it to refund. |
|
| 416 | - * a passed in amount. |
|
| 417 | - * |
|
| 418 | - * @param WPInv_Invoice $invoice Invoice. |
|
| 419 | - * @param float $amount Refund amount. |
|
| 420 | - * @param string $reason Refund reason. |
|
| 421 | - * @return WP_Error|bool True or false based on success, or a WP_Error object. |
|
| 422 | - */ |
|
| 423 | - public function process_refund( $invoice, $amount = null, $reason = '' ) { |
|
| 424 | - return apply_filters( 'getpaid_process_invoice_refund_' . $this->id, false, $invoice, $amount, $reason ); |
|
| 425 | - } |
|
| 426 | - |
|
| 427 | - /** |
|
| 428 | - * Displays the payment fields, credit cards etc. |
|
| 429 | - * |
|
| 430 | - * @param int $invoice_id 0 or invoice id. |
|
| 431 | - * @param GetPaid_Payment_Form $form Current payment form. |
|
| 432 | - */ |
|
| 433 | - public function payment_fields( $invoice_id, $form ) { |
|
| 434 | - do_action( 'getpaid_getpaid_gateway_payment_fields_' . $this->id, $invoice_id, $form ); |
|
| 435 | - } |
|
| 436 | - |
|
| 437 | - /** |
|
| 438 | - * Filters the gateway settings. |
|
| 439 | - * |
|
| 440 | - * @param array $admin_settings |
|
| 441 | - */ |
|
| 442 | - public function admin_settings( $admin_settings ) { |
|
| 443 | - return $admin_settings; |
|
| 444 | - } |
|
| 445 | - |
|
| 446 | - /** |
|
| 447 | - * Retrieves the value of a gateway setting. |
|
| 448 | - * |
|
| 449 | - * @param string $option |
|
| 450 | - */ |
|
| 451 | - public function get_option( $option, $default = false ) { |
|
| 452 | - return wpinv_get_option( $this->id . '_' . $option, $default ); |
|
| 453 | - } |
|
| 454 | - |
|
| 455 | - /** |
|
| 456 | - * Check if a gateway supports a given feature. |
|
| 457 | - * |
|
| 458 | - * Gateways should override this to declare support (or lack of support) for a feature. |
|
| 459 | - * For backward compatibility, gateways support 'products' by default, but nothing else. |
|
| 460 | - * |
|
| 461 | - * @param string $feature string The name of a feature to test support for. |
|
| 462 | - * @return bool True if the gateway supports the feature, false otherwise. |
|
| 463 | - * @since 1.0.19 |
|
| 464 | - */ |
|
| 465 | - public function supports( $feature ) { |
|
| 466 | - return apply_filters( 'getpaid_payment_gateway_supports', in_array( $feature, $this->supports ), $feature, $this ); |
|
| 467 | - } |
|
| 468 | - |
|
| 469 | - /** |
|
| 470 | - * Returns the credit card form html. |
|
| 471 | - * |
|
| 472 | - * @param bool $save whether or not to display the save button. |
|
| 473 | - */ |
|
| 346 | + return $transaction_url; |
|
| 347 | + } |
|
| 348 | + |
|
| 349 | + /** |
|
| 350 | + * Get a link to the subscription on the 3rd party gateway site (if applicable). |
|
| 351 | + * |
|
| 352 | + * @param string $subscription_url transaction url. |
|
| 353 | + * @param WPInv_Invoice $invoice Invoice object. |
|
| 354 | + * @return string subscription URL, or empty string. |
|
| 355 | + */ |
|
| 356 | + public function filter_subscription_url( $subscription_url, $invoice ) { |
|
| 357 | + |
|
| 358 | + $profile_id = $invoice->get_subscription_id(); |
|
| 359 | + |
|
| 360 | + if ( ! empty( $this->view_subscription_url ) && ! empty( $profile_id ) ) { |
|
| 361 | + |
|
| 362 | + $subscription_url = sprintf( $this->view_subscription_url, $profile_id ); |
|
| 363 | + $replace = $this->is_sandbox( $invoice ) ? 'sandbox' : ''; |
|
| 364 | + $subscription_url = str_replace( '{sandbox}', $replace, $subscription_url ); |
|
| 365 | + |
|
| 366 | + } |
|
| 367 | + |
|
| 368 | + return $subscription_url; |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + /** |
|
| 372 | + * Check if the gateway is available for use. |
|
| 373 | + * |
|
| 374 | + * @return bool |
|
| 375 | + */ |
|
| 376 | + public function is_available() { |
|
| 377 | + return ! empty( $this->enabled ); |
|
| 378 | + } |
|
| 379 | + |
|
| 380 | + /** |
|
| 381 | + * Return the gateway's title. |
|
| 382 | + * |
|
| 383 | + * @return string |
|
| 384 | + */ |
|
| 385 | + public function get_title() { |
|
| 386 | + return apply_filters( 'getpaid_gateway_title', $this->title, $this ); |
|
| 387 | + } |
|
| 388 | + |
|
| 389 | + /** |
|
| 390 | + * Return the gateway's description. |
|
| 391 | + * |
|
| 392 | + * @return string |
|
| 393 | + */ |
|
| 394 | + public function get_description() { |
|
| 395 | + return apply_filters( 'getpaid_gateway_description', $this->description, $this ); |
|
| 396 | + } |
|
| 397 | + |
|
| 398 | + /** |
|
| 399 | + * Process Payment. |
|
| 400 | + * |
|
| 401 | + * |
|
| 402 | + * @param WPInv_Invoice $invoice Invoice. |
|
| 403 | + * @param array $submission_data Posted checkout fields. |
|
| 404 | + * @param GetPaid_Payment_Form_Submission $submission Checkout submission. |
|
| 405 | + * @return void |
|
| 406 | + */ |
|
| 407 | + public function process_payment( $invoice, $submission_data, $submission ) { |
|
| 408 | + // Process the payment then either redirect to the success page or the gateway. |
|
| 409 | + do_action( 'getpaid_process_invoice_payment_' . $this->id, $invoice, $submission_data, $submission ); |
|
| 410 | + } |
|
| 411 | + |
|
| 412 | + /** |
|
| 413 | + * Process refund. |
|
| 414 | + * |
|
| 415 | + * If the gateway declares 'refunds' support, this will allow it to refund. |
|
| 416 | + * a passed in amount. |
|
| 417 | + * |
|
| 418 | + * @param WPInv_Invoice $invoice Invoice. |
|
| 419 | + * @param float $amount Refund amount. |
|
| 420 | + * @param string $reason Refund reason. |
|
| 421 | + * @return WP_Error|bool True or false based on success, or a WP_Error object. |
|
| 422 | + */ |
|
| 423 | + public function process_refund( $invoice, $amount = null, $reason = '' ) { |
|
| 424 | + return apply_filters( 'getpaid_process_invoice_refund_' . $this->id, false, $invoice, $amount, $reason ); |
|
| 425 | + } |
|
| 426 | + |
|
| 427 | + /** |
|
| 428 | + * Displays the payment fields, credit cards etc. |
|
| 429 | + * |
|
| 430 | + * @param int $invoice_id 0 or invoice id. |
|
| 431 | + * @param GetPaid_Payment_Form $form Current payment form. |
|
| 432 | + */ |
|
| 433 | + public function payment_fields( $invoice_id, $form ) { |
|
| 434 | + do_action( 'getpaid_getpaid_gateway_payment_fields_' . $this->id, $invoice_id, $form ); |
|
| 435 | + } |
|
| 436 | + |
|
| 437 | + /** |
|
| 438 | + * Filters the gateway settings. |
|
| 439 | + * |
|
| 440 | + * @param array $admin_settings |
|
| 441 | + */ |
|
| 442 | + public function admin_settings( $admin_settings ) { |
|
| 443 | + return $admin_settings; |
|
| 444 | + } |
|
| 445 | + |
|
| 446 | + /** |
|
| 447 | + * Retrieves the value of a gateway setting. |
|
| 448 | + * |
|
| 449 | + * @param string $option |
|
| 450 | + */ |
|
| 451 | + public function get_option( $option, $default = false ) { |
|
| 452 | + return wpinv_get_option( $this->id . '_' . $option, $default ); |
|
| 453 | + } |
|
| 454 | + |
|
| 455 | + /** |
|
| 456 | + * Check if a gateway supports a given feature. |
|
| 457 | + * |
|
| 458 | + * Gateways should override this to declare support (or lack of support) for a feature. |
|
| 459 | + * For backward compatibility, gateways support 'products' by default, but nothing else. |
|
| 460 | + * |
|
| 461 | + * @param string $feature string The name of a feature to test support for. |
|
| 462 | + * @return bool True if the gateway supports the feature, false otherwise. |
|
| 463 | + * @since 1.0.19 |
|
| 464 | + */ |
|
| 465 | + public function supports( $feature ) { |
|
| 466 | + return apply_filters( 'getpaid_payment_gateway_supports', in_array( $feature, $this->supports ), $feature, $this ); |
|
| 467 | + } |
|
| 468 | + |
|
| 469 | + /** |
|
| 470 | + * Returns the credit card form html. |
|
| 471 | + * |
|
| 472 | + * @param bool $save whether or not to display the save button. |
|
| 473 | + */ |
|
| 474 | 474 | public function get_cc_form( $save = false ) { |
| 475 | 475 | |
| 476 | - ob_start(); |
|
| 476 | + ob_start(); |
|
| 477 | 477 | |
| 478 | 478 | $id_prefix = esc_attr( uniqid( $this->id ) ); |
| 479 | 479 | |
@@ -568,11 +568,11 @@ discard block |
||
| 568 | 568 | 'name' => $this->id . '[cc_cvv2]', |
| 569 | 569 | 'id' => "$id_prefix-cc-cvv2", |
| 570 | 570 | 'label' => __( 'CCV', 'invoicing' ), |
| 571 | - 'label_type' => 'vertical', |
|
| 572 | - 'class' => 'form-control-sm', |
|
| 573 | - 'extra_attributes' => array( |
|
| 574 | - 'autocomplete' => "cc-csc", |
|
| 575 | - ), |
|
| 571 | + 'label_type' => 'vertical', |
|
| 572 | + 'class' => 'form-control-sm', |
|
| 573 | + 'extra_attributes' => array( |
|
| 574 | + 'autocomplete' => "cc-csc", |
|
| 575 | + ), |
|
| 576 | 576 | ) |
| 577 | 577 | ); |
| 578 | 578 | ?> |
@@ -582,189 +582,189 @@ discard block |
||
| 582 | 582 | |
| 583 | 583 | <?php |
| 584 | 584 | |
| 585 | - if ( $save ) { |
|
| 586 | - echo $this->save_payment_method_checkbox(); |
|
| 587 | - } |
|
| 585 | + if ( $save ) { |
|
| 586 | + echo $this->save_payment_method_checkbox(); |
|
| 587 | + } |
|
| 588 | 588 | |
| 589 | - ?> |
|
| 589 | + ?> |
|
| 590 | 590 | </div> |
| 591 | 591 | |
| 592 | 592 | </div> |
| 593 | 593 | <?php |
| 594 | 594 | |
| 595 | - return ob_get_clean(); |
|
| 595 | + return ob_get_clean(); |
|
| 596 | + |
|
| 597 | + } |
|
| 596 | 598 | |
| 599 | + /** |
|
| 600 | + * Displays a new payment method entry form. |
|
| 601 | + * |
|
| 602 | + * @since 1.0.19 |
|
| 603 | + */ |
|
| 604 | + public function new_payment_method_entry( $form ) { |
|
| 605 | + echo "<div class='getpaid-new-payment-method-form' style='display:none;'>$form</div>"; |
|
| 597 | 606 | } |
| 598 | 607 | |
| 599 | - /** |
|
| 600 | - * Displays a new payment method entry form. |
|
| 601 | - * |
|
| 602 | - * @since 1.0.19 |
|
| 603 | - */ |
|
| 604 | - public function new_payment_method_entry( $form ) { |
|
| 605 | - echo "<div class='getpaid-new-payment-method-form' style='display:none;'>$form</div>"; |
|
| 606 | - } |
|
| 607 | - |
|
| 608 | - /** |
|
| 609 | - * Grab and display our saved payment methods. |
|
| 610 | - * |
|
| 611 | - * @since 1.0.19 |
|
| 612 | - */ |
|
| 613 | - public function saved_payment_methods() { |
|
| 614 | - $html = '<ul class="getpaid-saved-payment-methods list-unstyled m-0 mt-2" data-count="' . esc_attr( count( $this->get_tokens( $this->is_sandbox() ) ) ) . '">'; |
|
| 615 | - |
|
| 616 | - foreach ( $this->get_tokens( $this->is_sandbox() ) as $token ) { |
|
| 617 | - $html .= $this->get_saved_payment_method_option_html( $token ); |
|
| 618 | - } |
|
| 619 | - |
|
| 620 | - $html .= $this->get_new_payment_method_option_html(); |
|
| 621 | - $html .= '</ul>'; |
|
| 622 | - |
|
| 623 | - echo apply_filters( 'getpaid_payment_gateway_form_saved_payment_methods_html', $html, $this ); |
|
| 624 | - } |
|
| 625 | - |
|
| 626 | - /** |
|
| 627 | - * Gets saved payment method HTML from a token. |
|
| 628 | - * |
|
| 629 | - * @since 1.0.19 |
|
| 630 | - * @param array $token Payment Token. |
|
| 631 | - * @return string Generated payment method HTML |
|
| 632 | - */ |
|
| 633 | - public function get_saved_payment_method_option_html( $token ) { |
|
| 634 | - |
|
| 635 | - return sprintf( |
|
| 636 | - '<li class="getpaid-payment-method form-group"> |
|
| 608 | + /** |
|
| 609 | + * Grab and display our saved payment methods. |
|
| 610 | + * |
|
| 611 | + * @since 1.0.19 |
|
| 612 | + */ |
|
| 613 | + public function saved_payment_methods() { |
|
| 614 | + $html = '<ul class="getpaid-saved-payment-methods list-unstyled m-0 mt-2" data-count="' . esc_attr( count( $this->get_tokens( $this->is_sandbox() ) ) ) . '">'; |
|
| 615 | + |
|
| 616 | + foreach ( $this->get_tokens( $this->is_sandbox() ) as $token ) { |
|
| 617 | + $html .= $this->get_saved_payment_method_option_html( $token ); |
|
| 618 | + } |
|
| 619 | + |
|
| 620 | + $html .= $this->get_new_payment_method_option_html(); |
|
| 621 | + $html .= '</ul>'; |
|
| 622 | + |
|
| 623 | + echo apply_filters( 'getpaid_payment_gateway_form_saved_payment_methods_html', $html, $this ); |
|
| 624 | + } |
|
| 625 | + |
|
| 626 | + /** |
|
| 627 | + * Gets saved payment method HTML from a token. |
|
| 628 | + * |
|
| 629 | + * @since 1.0.19 |
|
| 630 | + * @param array $token Payment Token. |
|
| 631 | + * @return string Generated payment method HTML |
|
| 632 | + */ |
|
| 633 | + public function get_saved_payment_method_option_html( $token ) { |
|
| 634 | + |
|
| 635 | + return sprintf( |
|
| 636 | + '<li class="getpaid-payment-method form-group"> |
|
| 637 | 637 | <label> |
| 638 | 638 | <input name="getpaid-%1$s-payment-method" type="radio" value="%2$s" style="width:auto;" class="getpaid-saved-payment-method-token-input" %4$s /> |
| 639 | 639 | <span>%3$s</span> |
| 640 | 640 | </label> |
| 641 | 641 | </li>', |
| 642 | - esc_attr( $this->id ), |
|
| 643 | - esc_attr( $token['id'] ), |
|
| 644 | - esc_html( $token['name'] ), |
|
| 645 | - checked( empty( $token['default'] ), false, false ) |
|
| 646 | - ); |
|
| 642 | + esc_attr( $this->id ), |
|
| 643 | + esc_attr( $token['id'] ), |
|
| 644 | + esc_html( $token['name'] ), |
|
| 645 | + checked( empty( $token['default'] ), false, false ) |
|
| 646 | + ); |
|
| 647 | 647 | |
| 648 | - } |
|
| 648 | + } |
|
| 649 | 649 | |
| 650 | - /** |
|
| 651 | - * Displays a radio button for entering a new payment method (new CC details) instead of using a saved method. |
|
| 652 | - * |
|
| 653 | - * @since 1.0.19 |
|
| 654 | - */ |
|
| 655 | - public function get_new_payment_method_option_html() { |
|
| 650 | + /** |
|
| 651 | + * Displays a radio button for entering a new payment method (new CC details) instead of using a saved method. |
|
| 652 | + * |
|
| 653 | + * @since 1.0.19 |
|
| 654 | + */ |
|
| 655 | + public function get_new_payment_method_option_html() { |
|
| 656 | 656 | |
| 657 | - $label = apply_filters( 'getpaid_new_payment_method_label', $this->new_method_label ? $this->new_method_label : __( 'Use a new payment method', 'invoicing' ), $this ); |
|
| 657 | + $label = apply_filters( 'getpaid_new_payment_method_label', $this->new_method_label ? $this->new_method_label : __( 'Use a new payment method', 'invoicing' ), $this ); |
|
| 658 | 658 | |
| 659 | - return sprintf( |
|
| 660 | - '<li class="getpaid-new-payment-method"> |
|
| 659 | + return sprintf( |
|
| 660 | + '<li class="getpaid-new-payment-method"> |
|
| 661 | 661 | <label> |
| 662 | 662 | <input name="getpaid-%1$s-payment-method" type="radio" value="new" style="width:auto;" /> |
| 663 | 663 | <span>%2$s</span> |
| 664 | 664 | </label> |
| 665 | 665 | </li>', |
| 666 | - esc_attr( $this->id ), |
|
| 667 | - esc_html( $label ) |
|
| 668 | - ); |
|
| 666 | + esc_attr( $this->id ), |
|
| 667 | + esc_html( $label ) |
|
| 668 | + ); |
|
| 669 | 669 | |
| 670 | - } |
|
| 670 | + } |
|
| 671 | 671 | |
| 672 | - /** |
|
| 673 | - * Outputs a checkbox for saving a new payment method to the database. |
|
| 674 | - * |
|
| 675 | - * @since 1.0.19 |
|
| 676 | - */ |
|
| 677 | - public function save_payment_method_checkbox() { |
|
| 672 | + /** |
|
| 673 | + * Outputs a checkbox for saving a new payment method to the database. |
|
| 674 | + * |
|
| 675 | + * @since 1.0.19 |
|
| 676 | + */ |
|
| 677 | + public function save_payment_method_checkbox() { |
|
| 678 | 678 | |
| 679 | - return sprintf( |
|
| 680 | - '<p class="form-group getpaid-save-payment-method"> |
|
| 679 | + return sprintf( |
|
| 680 | + '<p class="form-group getpaid-save-payment-method"> |
|
| 681 | 681 | <label> |
| 682 | 682 | <input name="getpaid-%1$s-new-payment-method" type="checkbox" value="true" style="width:auto;" /> |
| 683 | 683 | <span>%2$s</span> |
| 684 | 684 | </label> |
| 685 | 685 | </p>', |
| 686 | - esc_attr( $this->id ), |
|
| 687 | - esc_html__( 'Save payment method', 'invoicing' ) |
|
| 688 | - ); |
|
| 686 | + esc_attr( $this->id ), |
|
| 687 | + esc_html__( 'Save payment method', 'invoicing' ) |
|
| 688 | + ); |
|
| 689 | 689 | |
| 690 | - } |
|
| 690 | + } |
|
| 691 | 691 | |
| 692 | - /** |
|
| 693 | - * Registers the gateway. |
|
| 694 | - * |
|
| 695 | - * @return array |
|
| 696 | - */ |
|
| 697 | - public function register_gateway( $gateways ) { |
|
| 692 | + /** |
|
| 693 | + * Registers the gateway. |
|
| 694 | + * |
|
| 695 | + * @return array |
|
| 696 | + */ |
|
| 697 | + public function register_gateway( $gateways ) { |
|
| 698 | 698 | |
| 699 | - $gateways[ $this->id ] = array( |
|
| 699 | + $gateways[ $this->id ] = array( |
|
| 700 | 700 | |
| 701 | - 'admin_label' => $this->method_title, |
|
| 701 | + 'admin_label' => $this->method_title, |
|
| 702 | 702 | 'checkout_label' => $this->title, |
| 703 | - 'ordering' => $this->order, |
|
| 703 | + 'ordering' => $this->order, |
|
| 704 | 704 | |
| 705 | - ); |
|
| 705 | + ); |
|
| 706 | 706 | |
| 707 | - return $gateways; |
|
| 707 | + return $gateways; |
|
| 708 | 708 | |
| 709 | - } |
|
| 709 | + } |
|
| 710 | 710 | |
| 711 | - /** |
|
| 712 | - * Checks whether or not this is a sandbox request. |
|
| 713 | - * |
|
| 714 | - * @param WPInv_Invoice|null $invoice Invoice object or null. |
|
| 715 | - * @return bool |
|
| 716 | - */ |
|
| 717 | - public function is_sandbox( $invoice = null ) { |
|
| 711 | + /** |
|
| 712 | + * Checks whether or not this is a sandbox request. |
|
| 713 | + * |
|
| 714 | + * @param WPInv_Invoice|null $invoice Invoice object or null. |
|
| 715 | + * @return bool |
|
| 716 | + */ |
|
| 717 | + public function is_sandbox( $invoice = null ) { |
|
| 718 | 718 | |
| 719 | - if ( ! empty( $invoice ) && ! $invoice->needs_payment() ) { |
|
| 720 | - return $invoice->get_mode() == 'test'; |
|
| 721 | - } |
|
| 719 | + if ( ! empty( $invoice ) && ! $invoice->needs_payment() ) { |
|
| 720 | + return $invoice->get_mode() == 'test'; |
|
| 721 | + } |
|
| 722 | 722 | |
| 723 | - return wpinv_is_test_mode( $this->id ); |
|
| 723 | + return wpinv_is_test_mode( $this->id ); |
|
| 724 | 724 | |
| 725 | - } |
|
| 725 | + } |
|
| 726 | 726 | |
| 727 | - /** |
|
| 728 | - * Renames the checkout button |
|
| 729 | - * |
|
| 730 | - * @return string |
|
| 731 | - */ |
|
| 732 | - public function rename_checkout_button() { |
|
| 733 | - return $this->checkout_button_text; |
|
| 734 | - } |
|
| 727 | + /** |
|
| 728 | + * Renames the checkout button |
|
| 729 | + * |
|
| 730 | + * @return string |
|
| 731 | + */ |
|
| 732 | + public function rename_checkout_button() { |
|
| 733 | + return $this->checkout_button_text; |
|
| 734 | + } |
|
| 735 | 735 | |
| 736 | - /** |
|
| 737 | - * Validate gateway currency |
|
| 738 | - * |
|
| 739 | - * @return bool |
|
| 740 | - */ |
|
| 741 | - public function validate_currency( $validation, $currency ) { |
|
| 736 | + /** |
|
| 737 | + * Validate gateway currency |
|
| 738 | + * |
|
| 739 | + * @return bool |
|
| 740 | + */ |
|
| 741 | + public function validate_currency( $validation, $currency ) { |
|
| 742 | 742 | |
| 743 | - // Required currencies. |
|
| 744 | - if ( ! empty( $this->currencies ) && ! in_array( $currency, $this->currencies ) ) { |
|
| 745 | - return false; |
|
| 746 | - } |
|
| 743 | + // Required currencies. |
|
| 744 | + if ( ! empty( $this->currencies ) && ! in_array( $currency, $this->currencies ) ) { |
|
| 745 | + return false; |
|
| 746 | + } |
|
| 747 | 747 | |
| 748 | - // Excluded currencies. |
|
| 749 | - if ( ! empty( $this->exclude_currencies ) && in_array( $currency, $this->exclude_currencies ) ) { |
|
| 750 | - return false; |
|
| 751 | - } |
|
| 748 | + // Excluded currencies. |
|
| 749 | + if ( ! empty( $this->exclude_currencies ) && in_array( $currency, $this->exclude_currencies ) ) { |
|
| 750 | + return false; |
|
| 751 | + } |
|
| 752 | 752 | |
| 753 | - return $validation; |
|
| 754 | - } |
|
| 753 | + return $validation; |
|
| 754 | + } |
|
| 755 | 755 | |
| 756 | - /** |
|
| 757 | - * Displays an error |
|
| 758 | - * |
|
| 759 | - */ |
|
| 760 | - public function show_error( $code, $message, $type ) { |
|
| 756 | + /** |
|
| 757 | + * Displays an error |
|
| 758 | + * |
|
| 759 | + */ |
|
| 760 | + public function show_error( $code, $message, $type ) { |
|
| 761 | 761 | |
| 762 | - if ( is_admin() ) { |
|
| 763 | - getpaid_admin()->{"show_$type"}( $message ); |
|
| 764 | - } |
|
| 762 | + if ( is_admin() ) { |
|
| 763 | + getpaid_admin()->{"show_$type"}( $message ); |
|
| 764 | + } |
|
| 765 | 765 | |
| 766 | - wpinv_set_error( $code, $message, $type ); |
|
| 766 | + wpinv_set_error( $code, $message, $type ); |
|
| 767 | 767 | |
| 768 | - } |
|
| 768 | + } |
|
| 769 | 769 | |
| 770 | 770 | } |
@@ -15,125 +15,125 @@ discard block |
||
| 15 | 15 | */ |
| 16 | 16 | class WPInv_Subscription extends GetPaid_Data { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * Which data store to load. |
|
| 20 | - * |
|
| 21 | - * @var string |
|
| 22 | - */ |
|
| 23 | - protected $data_store_name = 'subscription'; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * This is the name of this object type. |
|
| 27 | - * |
|
| 28 | - * @var string |
|
| 29 | - */ |
|
| 30 | - protected $object_type = 'subscription'; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * Item Data array. This is the core item data exposed in APIs. |
|
| 34 | - * |
|
| 35 | - * @since 1.0.19 |
|
| 36 | - * @var array |
|
| 37 | - */ |
|
| 38 | - protected $data = array( |
|
| 39 | - 'customer_id' => 0, |
|
| 40 | - 'frequency' => 1, |
|
| 41 | - 'period' => 'D', |
|
| 42 | - 'initial_amount' => null, |
|
| 43 | - 'recurring_amount' => null, |
|
| 44 | - 'bill_times' => 0, |
|
| 45 | - 'transaction_id' => '', |
|
| 46 | - 'parent_payment_id' => null, |
|
| 47 | - 'product_id' => 0, |
|
| 48 | - 'created' => '0000-00-00 00:00:00', |
|
| 49 | - 'expiration' => '0000-00-00 00:00:00', |
|
| 50 | - 'trial_period' => '', |
|
| 51 | - 'status' => 'pending', |
|
| 52 | - 'profile_id' => '', |
|
| 53 | - 'gateway' => '', |
|
| 54 | - 'customer' => '', |
|
| 55 | - ); |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * Stores the status transition information. |
|
| 59 | - * |
|
| 60 | - * @since 1.0.19 |
|
| 61 | - * @var bool |
|
| 62 | - */ |
|
| 63 | - protected $status_transition = false; |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * Get the subscription if ID is passed, otherwise the subscription is new and empty. |
|
| 67 | - * |
|
| 68 | - * @param int|string|object|WPInv_Subscription $subscription Subscription id, profile_id, or object to read. |
|
| 69 | - * @param bool $deprecated |
|
| 70 | - */ |
|
| 71 | - function __construct( $subscription = 0, $deprecated = false ) { |
|
| 72 | - |
|
| 73 | - parent::__construct( $subscription ); |
|
| 74 | - |
|
| 75 | - if ( ! $deprecated && ! empty( $subscription ) && is_numeric( $subscription ) ) { |
|
| 76 | - $this->set_id( $subscription ); |
|
| 77 | - } elseif ( $subscription instanceof self ) { |
|
| 78 | - $this->set_id( $subscription->get_id() ); |
|
| 79 | - } elseif ( ! empty( $subscription->id ) ) { |
|
| 80 | - $this->set_id( $subscription->id ); |
|
| 81 | - } elseif ( $deprecated && $subscription_id = self::get_subscription_id_by_field( $subscription, 'profile_id' ) ) { |
|
| 82 | - $this->set_id( $subscription_id ); |
|
| 83 | - } else { |
|
| 84 | - $this->set_object_read( true ); |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - // Load the datastore. |
|
| 88 | - $this->data_store = GetPaid_Data_Store::load( $this->data_store_name ); |
|
| 89 | - |
|
| 90 | - if ( $this->get_id() > 0 ) { |
|
| 91 | - $this->data_store->read( $this ); |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * Given an invoice id, profile id, transaction id, it returns the subscription's id. |
|
| 98 | - * |
|
| 99 | - * |
|
| 100 | - * @static |
|
| 101 | - * @param string $value |
|
| 102 | - * @param string $field Either invoice_id, transaction_id or profile_id. |
|
| 103 | - * @since 1.0.19 |
|
| 104 | - * @return int |
|
| 105 | - */ |
|
| 106 | - public static function get_subscription_id_by_field( $value, $field = 'profile_id' ) { |
|
| 18 | + /** |
|
| 19 | + * Which data store to load. |
|
| 20 | + * |
|
| 21 | + * @var string |
|
| 22 | + */ |
|
| 23 | + protected $data_store_name = 'subscription'; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * This is the name of this object type. |
|
| 27 | + * |
|
| 28 | + * @var string |
|
| 29 | + */ |
|
| 30 | + protected $object_type = 'subscription'; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * Item Data array. This is the core item data exposed in APIs. |
|
| 34 | + * |
|
| 35 | + * @since 1.0.19 |
|
| 36 | + * @var array |
|
| 37 | + */ |
|
| 38 | + protected $data = array( |
|
| 39 | + 'customer_id' => 0, |
|
| 40 | + 'frequency' => 1, |
|
| 41 | + 'period' => 'D', |
|
| 42 | + 'initial_amount' => null, |
|
| 43 | + 'recurring_amount' => null, |
|
| 44 | + 'bill_times' => 0, |
|
| 45 | + 'transaction_id' => '', |
|
| 46 | + 'parent_payment_id' => null, |
|
| 47 | + 'product_id' => 0, |
|
| 48 | + 'created' => '0000-00-00 00:00:00', |
|
| 49 | + 'expiration' => '0000-00-00 00:00:00', |
|
| 50 | + 'trial_period' => '', |
|
| 51 | + 'status' => 'pending', |
|
| 52 | + 'profile_id' => '', |
|
| 53 | + 'gateway' => '', |
|
| 54 | + 'customer' => '', |
|
| 55 | + ); |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * Stores the status transition information. |
|
| 59 | + * |
|
| 60 | + * @since 1.0.19 |
|
| 61 | + * @var bool |
|
| 62 | + */ |
|
| 63 | + protected $status_transition = false; |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Get the subscription if ID is passed, otherwise the subscription is new and empty. |
|
| 67 | + * |
|
| 68 | + * @param int|string|object|WPInv_Subscription $subscription Subscription id, profile_id, or object to read. |
|
| 69 | + * @param bool $deprecated |
|
| 70 | + */ |
|
| 71 | + function __construct( $subscription = 0, $deprecated = false ) { |
|
| 72 | + |
|
| 73 | + parent::__construct( $subscription ); |
|
| 74 | + |
|
| 75 | + if ( ! $deprecated && ! empty( $subscription ) && is_numeric( $subscription ) ) { |
|
| 76 | + $this->set_id( $subscription ); |
|
| 77 | + } elseif ( $subscription instanceof self ) { |
|
| 78 | + $this->set_id( $subscription->get_id() ); |
|
| 79 | + } elseif ( ! empty( $subscription->id ) ) { |
|
| 80 | + $this->set_id( $subscription->id ); |
|
| 81 | + } elseif ( $deprecated && $subscription_id = self::get_subscription_id_by_field( $subscription, 'profile_id' ) ) { |
|
| 82 | + $this->set_id( $subscription_id ); |
|
| 83 | + } else { |
|
| 84 | + $this->set_object_read( true ); |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + // Load the datastore. |
|
| 88 | + $this->data_store = GetPaid_Data_Store::load( $this->data_store_name ); |
|
| 89 | + |
|
| 90 | + if ( $this->get_id() > 0 ) { |
|
| 91 | + $this->data_store->read( $this ); |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * Given an invoice id, profile id, transaction id, it returns the subscription's id. |
|
| 98 | + * |
|
| 99 | + * |
|
| 100 | + * @static |
|
| 101 | + * @param string $value |
|
| 102 | + * @param string $field Either invoice_id, transaction_id or profile_id. |
|
| 103 | + * @since 1.0.19 |
|
| 104 | + * @return int |
|
| 105 | + */ |
|
| 106 | + public static function get_subscription_id_by_field( $value, $field = 'profile_id' ) { |
|
| 107 | 107 | global $wpdb; |
| 108 | 108 | |
| 109 | - // Trim the value. |
|
| 110 | - $value = trim( $value ); |
|
| 109 | + // Trim the value. |
|
| 110 | + $value = trim( $value ); |
|
| 111 | 111 | |
| 112 | - if ( empty( $value ) ) { |
|
| 113 | - return 0; |
|
| 114 | - } |
|
| 112 | + if ( empty( $value ) ) { |
|
| 113 | + return 0; |
|
| 114 | + } |
|
| 115 | 115 | |
| 116 | - if ( 'invoice_id' == $field ) { |
|
| 117 | - $field = 'parent_payment_id'; |
|
| 118 | - } |
|
| 116 | + if ( 'invoice_id' == $field ) { |
|
| 117 | + $field = 'parent_payment_id'; |
|
| 118 | + } |
|
| 119 | 119 | |
| 120 | 120 | // Valid fields. |
| 121 | 121 | $fields = array( |
| 122 | - 'parent_payment_id', |
|
| 123 | - 'transaction_id', |
|
| 124 | - 'profile_id' |
|
| 125 | - ); |
|
| 126 | - |
|
| 127 | - // Ensure a field has been passed. |
|
| 128 | - if ( empty( $field ) || ! in_array( $field, $fields ) ) { |
|
| 129 | - return 0; |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - // Maybe retrieve from the cache. |
|
| 133 | - $subscription_id = wp_cache_get( $value, "getpaid_subscription_{$field}s_to_subscription_ids" ); |
|
| 134 | - if ( ! empty( $subscription_id ) ) { |
|
| 135 | - return $subscription_id; |
|
| 136 | - } |
|
| 122 | + 'parent_payment_id', |
|
| 123 | + 'transaction_id', |
|
| 124 | + 'profile_id' |
|
| 125 | + ); |
|
| 126 | + |
|
| 127 | + // Ensure a field has been passed. |
|
| 128 | + if ( empty( $field ) || ! in_array( $field, $fields ) ) { |
|
| 129 | + return 0; |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + // Maybe retrieve from the cache. |
|
| 133 | + $subscription_id = wp_cache_get( $value, "getpaid_subscription_{$field}s_to_subscription_ids" ); |
|
| 134 | + if ( ! empty( $subscription_id ) ) { |
|
| 135 | + return $subscription_id; |
|
| 136 | + } |
|
| 137 | 137 | |
| 138 | 138 | // Fetch from the db. |
| 139 | 139 | $table = $wpdb->prefix . 'wpinv_subscriptions'; |
@@ -141,34 +141,34 @@ discard block |
||
| 141 | 141 | $wpdb->prepare( "SELECT `id` FROM $table WHERE `$field`=%s LIMIT 1", $value ) |
| 142 | 142 | ); |
| 143 | 143 | |
| 144 | - if ( empty( $subscription_id ) ) { |
|
| 145 | - return 0; |
|
| 146 | - } |
|
| 144 | + if ( empty( $subscription_id ) ) { |
|
| 145 | + return 0; |
|
| 146 | + } |
|
| 147 | 147 | |
| 148 | - // Update the cache with our data. |
|
| 149 | - wp_cache_set( $value, $subscription_id, "getpaid_subscription_{$field}s_to_subscription_ids" ); |
|
| 148 | + // Update the cache with our data. |
|
| 149 | + wp_cache_set( $value, $subscription_id, "getpaid_subscription_{$field}s_to_subscription_ids" ); |
|
| 150 | 150 | |
| 151 | - return $subscription_id; |
|
| 152 | - } |
|
| 151 | + return $subscription_id; |
|
| 152 | + } |
|
| 153 | 153 | |
| 154 | - /** |
|
| 154 | + /** |
|
| 155 | 155 | * Clears the subscription's cache. |
| 156 | 156 | */ |
| 157 | 157 | public function clear_cache() { |
| 158 | - wp_cache_delete( $this->get_parent_payment_id(), 'getpaid_subscription_parent_payment_ids_to_subscription_ids' ); |
|
| 159 | - wp_cache_delete( $this->get_transaction_id(), 'getpaid_subscription_transaction_ids_to_subscription_ids' ); |
|
| 160 | - wp_cache_delete( $this->get_profile_id(), 'getpaid_subscription_profile_ids_to_subscription_ids' ); |
|
| 161 | - wp_cache_delete( $this->get_id(), 'getpaid_subscriptions' ); |
|
| 162 | - } |
|
| 158 | + wp_cache_delete( $this->get_parent_payment_id(), 'getpaid_subscription_parent_payment_ids_to_subscription_ids' ); |
|
| 159 | + wp_cache_delete( $this->get_transaction_id(), 'getpaid_subscription_transaction_ids_to_subscription_ids' ); |
|
| 160 | + wp_cache_delete( $this->get_profile_id(), 'getpaid_subscription_profile_ids_to_subscription_ids' ); |
|
| 161 | + wp_cache_delete( $this->get_id(), 'getpaid_subscriptions' ); |
|
| 162 | + } |
|
| 163 | 163 | |
| 164 | - /** |
|
| 164 | + /** |
|
| 165 | 165 | * Checks if a subscription key is set. |
| 166 | 166 | */ |
| 167 | 167 | public function _isset( $key ) { |
| 168 | 168 | return isset( $this->data[$key] ) || method_exists( $this, "get_$key" ); |
| 169 | - } |
|
| 169 | + } |
|
| 170 | 170 | |
| 171 | - /* |
|
| 171 | + /* |
|
| 172 | 172 | |-------------------------------------------------------------------------- |
| 173 | 173 | | CRUD methods |
| 174 | 174 | |-------------------------------------------------------------------------- |
@@ -177,545 +177,545 @@ discard block |
||
| 177 | 177 | | |
| 178 | 178 | */ |
| 179 | 179 | |
| 180 | - /* |
|
| 180 | + /* |
|
| 181 | 181 | |-------------------------------------------------------------------------- |
| 182 | 182 | | Getters |
| 183 | 183 | |-------------------------------------------------------------------------- |
| 184 | 184 | */ |
| 185 | 185 | |
| 186 | - /** |
|
| 187 | - * Get customer id. |
|
| 188 | - * |
|
| 189 | - * @since 1.0.19 |
|
| 190 | - * @param string $context View or edit context. |
|
| 191 | - * @return int |
|
| 192 | - */ |
|
| 193 | - public function get_customer_id( $context = 'view' ) { |
|
| 194 | - return (int) $this->get_prop( 'customer_id', $context ); |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * Get customer information. |
|
| 199 | - * |
|
| 200 | - * @since 1.0.19 |
|
| 201 | - * @param string $context View or edit context. |
|
| 202 | - * @return WP_User|false WP_User object on success, false on failure. |
|
| 203 | - */ |
|
| 204 | - public function get_customer( $context = 'view' ) { |
|
| 205 | - return get_userdata( $this->get_customer_id( $context ) ); |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - /** |
|
| 209 | - * Get parent invoice id. |
|
| 210 | - * |
|
| 211 | - * @since 1.0.19 |
|
| 212 | - * @param string $context View or edit context. |
|
| 213 | - * @return int |
|
| 214 | - */ |
|
| 215 | - public function get_parent_invoice_id( $context = 'view' ) { |
|
| 216 | - return (int) $this->get_prop( 'parent_payment_id', $context ); |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - /** |
|
| 220 | - * Alias for self::get_parent_invoice_id(). |
|
| 221 | - * |
|
| 222 | - * @since 1.0.19 |
|
| 223 | - * @param string $context View or edit context. |
|
| 224 | - * @return int |
|
| 225 | - */ |
|
| 186 | + /** |
|
| 187 | + * Get customer id. |
|
| 188 | + * |
|
| 189 | + * @since 1.0.19 |
|
| 190 | + * @param string $context View or edit context. |
|
| 191 | + * @return int |
|
| 192 | + */ |
|
| 193 | + public function get_customer_id( $context = 'view' ) { |
|
| 194 | + return (int) $this->get_prop( 'customer_id', $context ); |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * Get customer information. |
|
| 199 | + * |
|
| 200 | + * @since 1.0.19 |
|
| 201 | + * @param string $context View or edit context. |
|
| 202 | + * @return WP_User|false WP_User object on success, false on failure. |
|
| 203 | + */ |
|
| 204 | + public function get_customer( $context = 'view' ) { |
|
| 205 | + return get_userdata( $this->get_customer_id( $context ) ); |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + /** |
|
| 209 | + * Get parent invoice id. |
|
| 210 | + * |
|
| 211 | + * @since 1.0.19 |
|
| 212 | + * @param string $context View or edit context. |
|
| 213 | + * @return int |
|
| 214 | + */ |
|
| 215 | + public function get_parent_invoice_id( $context = 'view' ) { |
|
| 216 | + return (int) $this->get_prop( 'parent_payment_id', $context ); |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + /** |
|
| 220 | + * Alias for self::get_parent_invoice_id(). |
|
| 221 | + * |
|
| 222 | + * @since 1.0.19 |
|
| 223 | + * @param string $context View or edit context. |
|
| 224 | + * @return int |
|
| 225 | + */ |
|
| 226 | 226 | public function get_parent_payment_id( $context = 'view' ) { |
| 227 | 227 | return $this->get_parent_invoice_id( $context ); |
| 228 | - } |
|
| 228 | + } |
|
| 229 | 229 | |
| 230 | - /** |
|
| 230 | + /** |
|
| 231 | 231 | * Alias for self::get_parent_invoice_id(). |
| 232 | 232 | * |
| 233 | - * @since 1.0.0 |
|
| 234 | - * @return int |
|
| 233 | + * @since 1.0.0 |
|
| 234 | + * @return int |
|
| 235 | + */ |
|
| 236 | + public function get_original_payment_id( $context = 'view' ) { |
|
| 237 | + return $this->get_parent_invoice_id( $context ); |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + /** |
|
| 241 | + * Get parent invoice. |
|
| 242 | + * |
|
| 243 | + * @since 1.0.19 |
|
| 244 | + * @param string $context View or edit context. |
|
| 245 | + * @return WPInv_Invoice |
|
| 246 | + */ |
|
| 247 | + public function get_parent_invoice( $context = 'view' ) { |
|
| 248 | + return new WPInv_Invoice( $this->get_parent_invoice_id( $context ) ); |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + /** |
|
| 252 | + * Alias for self::get_parent_invoice(). |
|
| 253 | + * |
|
| 254 | + * @since 1.0.19 |
|
| 255 | + * @param string $context View or edit context. |
|
| 256 | + * @return WPInv_Invoice |
|
| 257 | + */ |
|
| 258 | + public function get_parent_payment( $context = 'view' ) { |
|
| 259 | + return $this->get_parent_invoice( $context ); |
|
| 260 | + } |
|
| 261 | + |
|
| 262 | + /** |
|
| 263 | + * Get subscription's product id. |
|
| 264 | + * |
|
| 265 | + * @since 1.0.19 |
|
| 266 | + * @param string $context View or edit context. |
|
| 267 | + * @return int |
|
| 268 | + */ |
|
| 269 | + public function get_product_id( $context = 'view' ) { |
|
| 270 | + return (int) $this->get_prop( 'product_id', $context ); |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + /** |
|
| 274 | + * Get the subscription product. |
|
| 275 | + * |
|
| 276 | + * @since 1.0.19 |
|
| 277 | + * @param string $context View or edit context. |
|
| 278 | + * @return WPInv_Item |
|
| 279 | + */ |
|
| 280 | + public function get_product( $context = 'view' ) { |
|
| 281 | + return new WPInv_Item( $this->get_product_id( $context ) ); |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + /** |
|
| 285 | + * Get parent invoice's gateway. |
|
| 286 | + * |
|
| 287 | + * Here for backwards compatibility. |
|
| 288 | + * |
|
| 289 | + * @since 1.0.19 |
|
| 290 | + * @param string $context View or edit context. |
|
| 291 | + * @return string |
|
| 292 | + */ |
|
| 293 | + public function get_gateway( $context = 'view' ) { |
|
| 294 | + return $this->get_parent_invoice( $context )->get_gateway(); |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + /** |
|
| 298 | + * Get the period of a renewal. |
|
| 299 | + * |
|
| 300 | + * @since 1.0.19 |
|
| 301 | + * @param string $context View or edit context. |
|
| 302 | + * @return string |
|
| 303 | + */ |
|
| 304 | + public function get_period( $context = 'view' ) { |
|
| 305 | + return $this->get_prop( 'period', $context ); |
|
| 306 | + } |
|
| 307 | + |
|
| 308 | + /** |
|
| 309 | + * Get number of periods each renewal is valid for. |
|
| 310 | + * |
|
| 311 | + * @since 1.0.19 |
|
| 312 | + * @param string $context View or edit context. |
|
| 313 | + * @return int |
|
| 314 | + */ |
|
| 315 | + public function get_frequency( $context = 'view' ) { |
|
| 316 | + return (int) $this->get_prop( 'frequency', $context ); |
|
| 317 | + } |
|
| 318 | + |
|
| 319 | + /** |
|
| 320 | + * Get the initial amount for the subscription. |
|
| 321 | + * |
|
| 322 | + * @since 1.0.19 |
|
| 323 | + * @param string $context View or edit context. |
|
| 324 | + * @return float |
|
| 325 | + */ |
|
| 326 | + public function get_initial_amount( $context = 'view' ) { |
|
| 327 | + return (float) wpinv_sanitize_amount( $this->get_prop( 'initial_amount', $context ) ); |
|
| 328 | + } |
|
| 329 | + |
|
| 330 | + /** |
|
| 331 | + * Get the recurring amount for the subscription. |
|
| 332 | + * |
|
| 333 | + * @since 1.0.19 |
|
| 334 | + * @param string $context View or edit context. |
|
| 335 | + * @return float |
|
| 336 | + */ |
|
| 337 | + public function get_recurring_amount( $context = 'view' ) { |
|
| 338 | + return (float) wpinv_sanitize_amount( $this->get_prop( 'recurring_amount', $context ) ); |
|
| 339 | + } |
|
| 340 | + |
|
| 341 | + /** |
|
| 342 | + * Get number of times that this subscription can be renewed. |
|
| 343 | + * |
|
| 344 | + * @since 1.0.19 |
|
| 345 | + * @param string $context View or edit context. |
|
| 346 | + * @return int |
|
| 347 | + */ |
|
| 348 | + public function get_bill_times( $context = 'view' ) { |
|
| 349 | + return (int) $this->get_prop( 'bill_times', $context ); |
|
| 350 | + } |
|
| 351 | + |
|
| 352 | + /** |
|
| 353 | + * Get transaction id of this subscription's parent invoice. |
|
| 354 | + * |
|
| 355 | + * @since 1.0.19 |
|
| 356 | + * @param string $context View or edit context. |
|
| 357 | + * @return string |
|
| 358 | + */ |
|
| 359 | + public function get_transaction_id( $context = 'view' ) { |
|
| 360 | + return $this->get_prop( 'transaction_id', $context ); |
|
| 361 | + } |
|
| 362 | + |
|
| 363 | + /** |
|
| 364 | + * Get the date that the subscription was created. |
|
| 365 | + * |
|
| 366 | + * @since 1.0.19 |
|
| 367 | + * @param string $context View or edit context. |
|
| 368 | + * @return string |
|
| 369 | + */ |
|
| 370 | + public function get_created( $context = 'view' ) { |
|
| 371 | + return $this->get_prop( 'created', $context ); |
|
| 372 | + } |
|
| 373 | + |
|
| 374 | + /** |
|
| 375 | + * Alias for self::get_created(). |
|
| 376 | + * |
|
| 377 | + * @since 1.0.19 |
|
| 378 | + * @param string $context View or edit context. |
|
| 379 | + * @return string |
|
| 380 | + */ |
|
| 381 | + public function get_date_created( $context = 'view' ) { |
|
| 382 | + return $this->get_created( $context ); |
|
| 383 | + } |
|
| 384 | + |
|
| 385 | + /** |
|
| 386 | + * Retrieves the creation date in a timestamp |
|
| 387 | + * |
|
| 388 | + * @since 1.0.0 |
|
| 389 | + * @return int |
|
| 390 | + */ |
|
| 391 | + public function get_time_created() { |
|
| 392 | + $created = $this->get_date_created(); |
|
| 393 | + return empty( $created ) ? current_time( 'timestamp' ) : strtotime( $created, current_time( 'timestamp' ) ); |
|
| 394 | + } |
|
| 395 | + |
|
| 396 | + /** |
|
| 397 | + * Get GMT date when the subscription was created. |
|
| 398 | + * |
|
| 399 | + * @since 1.0.19 |
|
| 400 | + * @param string $context View or edit context. |
|
| 401 | + * @return string |
|
| 402 | + */ |
|
| 403 | + public function get_date_created_gmt( $context = 'view' ) { |
|
| 404 | + $date = $this->get_date_created( $context ); |
|
| 405 | + |
|
| 406 | + if ( $date ) { |
|
| 407 | + $date = get_gmt_from_date( $date ); |
|
| 408 | + } |
|
| 409 | + return $date; |
|
| 410 | + } |
|
| 411 | + |
|
| 412 | + /** |
|
| 413 | + * Get the date that the subscription will renew. |
|
| 414 | + * |
|
| 415 | + * @since 1.0.19 |
|
| 416 | + * @param string $context View or edit context. |
|
| 417 | + * @return string |
|
| 418 | + */ |
|
| 419 | + public function get_next_renewal_date( $context = 'view' ) { |
|
| 420 | + return $this->get_prop( 'expiration', $context ); |
|
| 421 | + } |
|
| 422 | + |
|
| 423 | + /** |
|
| 424 | + * Alias for self::get_next_renewal_date(). |
|
| 425 | + * |
|
| 426 | + * @since 1.0.19 |
|
| 427 | + * @param string $context View or edit context. |
|
| 428 | + * @return string |
|
| 429 | + */ |
|
| 430 | + public function get_expiration( $context = 'view' ) { |
|
| 431 | + return $this->get_next_renewal_date( $context ); |
|
| 432 | + } |
|
| 433 | + |
|
| 434 | + /** |
|
| 435 | + * Retrieves the expiration date in a timestamp |
|
| 436 | + * |
|
| 437 | + * @since 1.0.0 |
|
| 438 | + * @return int |
|
| 439 | + */ |
|
| 440 | + public function get_expiration_time() { |
|
| 441 | + $expiration = $this->get_expiration(); |
|
| 442 | + |
|
| 443 | + if ( empty( $expiration ) || '0000-00-00 00:00:00' == $expiration ) { |
|
| 444 | + return current_time( 'timestamp' ); |
|
| 445 | + } |
|
| 446 | + |
|
| 447 | + $expiration = strtotime( $expiration, current_time( 'timestamp' ) ); |
|
| 448 | + return $expiration < current_time( 'timestamp' ) ? current_time( 'timestamp' ) : $expiration; |
|
| 449 | + } |
|
| 450 | + |
|
| 451 | + /** |
|
| 452 | + * Get GMT date when the subscription will renew. |
|
| 453 | + * |
|
| 454 | + * @since 1.0.19 |
|
| 455 | + * @param string $context View or edit context. |
|
| 456 | + * @return string |
|
| 457 | + */ |
|
| 458 | + public function get_next_renewal_date_gmt( $context = 'view' ) { |
|
| 459 | + $date = $this->get_next_renewal_date( $context ); |
|
| 460 | + |
|
| 461 | + if ( $date ) { |
|
| 462 | + $date = get_gmt_from_date( $date ); |
|
| 463 | + } |
|
| 464 | + return $date; |
|
| 465 | + } |
|
| 466 | + |
|
| 467 | + /** |
|
| 468 | + * Get the subscription's trial period. |
|
| 469 | + * |
|
| 470 | + * @since 1.0.19 |
|
| 471 | + * @param string $context View or edit context. |
|
| 472 | + * @return string |
|
| 473 | + */ |
|
| 474 | + public function get_trial_period( $context = 'view' ) { |
|
| 475 | + return $this->get_prop( 'trial_period', $context ); |
|
| 476 | + } |
|
| 477 | + |
|
| 478 | + /** |
|
| 479 | + * Get the subscription's status. |
|
| 480 | + * |
|
| 481 | + * @since 1.0.19 |
|
| 482 | + * @param string $context View or edit context. |
|
| 483 | + * @return string |
|
| 484 | + */ |
|
| 485 | + public function get_status( $context = 'view' ) { |
|
| 486 | + return $this->get_prop( 'status', $context ); |
|
| 487 | + } |
|
| 488 | + |
|
| 489 | + /** |
|
| 490 | + * Get the subscription's profile id. |
|
| 491 | + * |
|
| 492 | + * @since 1.0.19 |
|
| 493 | + * @param string $context View or edit context. |
|
| 494 | + * @return string |
|
| 495 | + */ |
|
| 496 | + public function get_profile_id( $context = 'view' ) { |
|
| 497 | + return $this->get_prop( 'profile_id', $context ); |
|
| 498 | + } |
|
| 499 | + |
|
| 500 | + /* |
|
| 501 | + |-------------------------------------------------------------------------- |
|
| 502 | + | Setters |
|
| 503 | + |-------------------------------------------------------------------------- |
|
| 504 | + */ |
|
| 505 | + |
|
| 506 | + /** |
|
| 507 | + * Set customer id. |
|
| 508 | + * |
|
| 509 | + * @since 1.0.19 |
|
| 510 | + * @param int $value The customer's id. |
|
| 511 | + */ |
|
| 512 | + public function set_customer_id( $value ) { |
|
| 513 | + $this->set_prop( 'customer_id', (int) $value ); |
|
| 514 | + } |
|
| 515 | + |
|
| 516 | + /** |
|
| 517 | + * Set parent invoice id. |
|
| 518 | + * |
|
| 519 | + * @since 1.0.19 |
|
| 520 | + * @param int $value The parent invoice id. |
|
| 521 | + */ |
|
| 522 | + public function set_parent_invoice_id( $value ) { |
|
| 523 | + $this->set_prop( 'parent_payment_id', (int) $value ); |
|
| 524 | + } |
|
| 525 | + |
|
| 526 | + /** |
|
| 527 | + * Alias for self::set_parent_invoice_id(). |
|
| 528 | + * |
|
| 529 | + * @since 1.0.19 |
|
| 530 | + * @param int $value The parent invoice id. |
|
| 531 | + */ |
|
| 532 | + public function set_parent_payment_id( $value ) { |
|
| 533 | + $this->set_parent_invoice_id( $value ); |
|
| 534 | + } |
|
| 535 | + |
|
| 536 | + /** |
|
| 537 | + * Alias for self::set_parent_invoice_id(). |
|
| 538 | + * |
|
| 539 | + * @since 1.0.19 |
|
| 540 | + * @param int $value The parent invoice id. |
|
| 541 | + */ |
|
| 542 | + public function set_original_payment_id( $value ) { |
|
| 543 | + $this->set_parent_invoice_id( $value ); |
|
| 544 | + } |
|
| 545 | + |
|
| 546 | + /** |
|
| 547 | + * Set subscription's product id. |
|
| 548 | + * |
|
| 549 | + * @since 1.0.19 |
|
| 550 | + * @param int $value The subscription product id. |
|
| 551 | + */ |
|
| 552 | + public function set_product_id( $value ) { |
|
| 553 | + $this->set_prop( 'product_id', (int) $value ); |
|
| 554 | + } |
|
| 555 | + |
|
| 556 | + /** |
|
| 557 | + * Set the period of a renewal. |
|
| 558 | + * |
|
| 559 | + * @since 1.0.19 |
|
| 560 | + * @param string $value The renewal period. |
|
| 561 | + */ |
|
| 562 | + public function set_period( $value ) { |
|
| 563 | + $this->set_prop( 'period', $value ); |
|
| 564 | + } |
|
| 565 | + |
|
| 566 | + /** |
|
| 567 | + * Set number of periods each renewal is valid for. |
|
| 568 | + * |
|
| 569 | + * @since 1.0.19 |
|
| 570 | + * @param int $value The subscription frequency. |
|
| 571 | + */ |
|
| 572 | + public function set_frequency( $value ) { |
|
| 573 | + $value = empty( $value ) ? 1 : (int) $value; |
|
| 574 | + $this->set_prop( 'frequency', absint( $value ) ); |
|
| 575 | + } |
|
| 576 | + |
|
| 577 | + /** |
|
| 578 | + * Set the initial amount for the subscription. |
|
| 579 | + * |
|
| 580 | + * @since 1.0.19 |
|
| 581 | + * @param float $value The initial subcription amount. |
|
| 582 | + */ |
|
| 583 | + public function set_initial_amount( $value ) { |
|
| 584 | + $this->set_prop( 'initial_amount', wpinv_sanitize_amount( $value ) ); |
|
| 585 | + } |
|
| 586 | + |
|
| 587 | + /** |
|
| 588 | + * Set the recurring amount for the subscription. |
|
| 589 | + * |
|
| 590 | + * @since 1.0.19 |
|
| 591 | + * @param float $value The recurring subcription amount. |
|
| 592 | + */ |
|
| 593 | + public function set_recurring_amount( $value ) { |
|
| 594 | + $this->set_prop( 'recurring_amount', wpinv_sanitize_amount( $value ) ); |
|
| 595 | + } |
|
| 596 | + |
|
| 597 | + /** |
|
| 598 | + * Set number of times that this subscription can be renewed. |
|
| 599 | + * |
|
| 600 | + * @since 1.0.19 |
|
| 601 | + * @param int $value Bill times. |
|
| 602 | + */ |
|
| 603 | + public function set_bill_times( $value ) { |
|
| 604 | + $this->set_prop( 'bill_times', (int) $value ); |
|
| 605 | + } |
|
| 606 | + |
|
| 607 | + /** |
|
| 608 | + * Get transaction id of this subscription's parent invoice. |
|
| 609 | + * |
|
| 610 | + * @since 1.0.19 |
|
| 611 | + * @param string $value Bill times. |
|
| 235 | 612 | */ |
| 236 | - public function get_original_payment_id( $context = 'view' ) { |
|
| 237 | - return $this->get_parent_invoice_id( $context ); |
|
| 613 | + public function set_transaction_id( $value ) { |
|
| 614 | + $this->set_prop( 'transaction_id', sanitize_text_field( $value ) ); |
|
| 238 | 615 | } |
| 239 | 616 | |
| 240 | - /** |
|
| 241 | - * Get parent invoice. |
|
| 242 | - * |
|
| 243 | - * @since 1.0.19 |
|
| 244 | - * @param string $context View or edit context. |
|
| 245 | - * @return WPInv_Invoice |
|
| 246 | - */ |
|
| 247 | - public function get_parent_invoice( $context = 'view' ) { |
|
| 248 | - return new WPInv_Invoice( $this->get_parent_invoice_id( $context ) ); |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - /** |
|
| 252 | - * Alias for self::get_parent_invoice(). |
|
| 253 | - * |
|
| 254 | - * @since 1.0.19 |
|
| 255 | - * @param string $context View or edit context. |
|
| 256 | - * @return WPInv_Invoice |
|
| 257 | - */ |
|
| 258 | - public function get_parent_payment( $context = 'view' ) { |
|
| 259 | - return $this->get_parent_invoice( $context ); |
|
| 260 | - } |
|
| 261 | - |
|
| 262 | - /** |
|
| 263 | - * Get subscription's product id. |
|
| 264 | - * |
|
| 265 | - * @since 1.0.19 |
|
| 266 | - * @param string $context View or edit context. |
|
| 267 | - * @return int |
|
| 268 | - */ |
|
| 269 | - public function get_product_id( $context = 'view' ) { |
|
| 270 | - return (int) $this->get_prop( 'product_id', $context ); |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - /** |
|
| 274 | - * Get the subscription product. |
|
| 275 | - * |
|
| 276 | - * @since 1.0.19 |
|
| 277 | - * @param string $context View or edit context. |
|
| 278 | - * @return WPInv_Item |
|
| 279 | - */ |
|
| 280 | - public function get_product( $context = 'view' ) { |
|
| 281 | - return new WPInv_Item( $this->get_product_id( $context ) ); |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - /** |
|
| 285 | - * Get parent invoice's gateway. |
|
| 286 | - * |
|
| 287 | - * Here for backwards compatibility. |
|
| 288 | - * |
|
| 289 | - * @since 1.0.19 |
|
| 290 | - * @param string $context View or edit context. |
|
| 291 | - * @return string |
|
| 292 | - */ |
|
| 293 | - public function get_gateway( $context = 'view' ) { |
|
| 294 | - return $this->get_parent_invoice( $context )->get_gateway(); |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - /** |
|
| 298 | - * Get the period of a renewal. |
|
| 299 | - * |
|
| 300 | - * @since 1.0.19 |
|
| 301 | - * @param string $context View or edit context. |
|
| 302 | - * @return string |
|
| 303 | - */ |
|
| 304 | - public function get_period( $context = 'view' ) { |
|
| 305 | - return $this->get_prop( 'period', $context ); |
|
| 306 | - } |
|
| 307 | - |
|
| 308 | - /** |
|
| 309 | - * Get number of periods each renewal is valid for. |
|
| 310 | - * |
|
| 311 | - * @since 1.0.19 |
|
| 312 | - * @param string $context View or edit context. |
|
| 313 | - * @return int |
|
| 314 | - */ |
|
| 315 | - public function get_frequency( $context = 'view' ) { |
|
| 316 | - return (int) $this->get_prop( 'frequency', $context ); |
|
| 317 | - } |
|
| 318 | - |
|
| 319 | - /** |
|
| 320 | - * Get the initial amount for the subscription. |
|
| 321 | - * |
|
| 322 | - * @since 1.0.19 |
|
| 323 | - * @param string $context View or edit context. |
|
| 324 | - * @return float |
|
| 325 | - */ |
|
| 326 | - public function get_initial_amount( $context = 'view' ) { |
|
| 327 | - return (float) wpinv_sanitize_amount( $this->get_prop( 'initial_amount', $context ) ); |
|
| 328 | - } |
|
| 329 | - |
|
| 330 | - /** |
|
| 331 | - * Get the recurring amount for the subscription. |
|
| 332 | - * |
|
| 333 | - * @since 1.0.19 |
|
| 334 | - * @param string $context View or edit context. |
|
| 335 | - * @return float |
|
| 336 | - */ |
|
| 337 | - public function get_recurring_amount( $context = 'view' ) { |
|
| 338 | - return (float) wpinv_sanitize_amount( $this->get_prop( 'recurring_amount', $context ) ); |
|
| 339 | - } |
|
| 340 | - |
|
| 341 | - /** |
|
| 342 | - * Get number of times that this subscription can be renewed. |
|
| 343 | - * |
|
| 344 | - * @since 1.0.19 |
|
| 345 | - * @param string $context View or edit context. |
|
| 346 | - * @return int |
|
| 347 | - */ |
|
| 348 | - public function get_bill_times( $context = 'view' ) { |
|
| 349 | - return (int) $this->get_prop( 'bill_times', $context ); |
|
| 350 | - } |
|
| 351 | - |
|
| 352 | - /** |
|
| 353 | - * Get transaction id of this subscription's parent invoice. |
|
| 354 | - * |
|
| 355 | - * @since 1.0.19 |
|
| 356 | - * @param string $context View or edit context. |
|
| 357 | - * @return string |
|
| 358 | - */ |
|
| 359 | - public function get_transaction_id( $context = 'view' ) { |
|
| 360 | - return $this->get_prop( 'transaction_id', $context ); |
|
| 361 | - } |
|
| 362 | - |
|
| 363 | - /** |
|
| 364 | - * Get the date that the subscription was created. |
|
| 365 | - * |
|
| 366 | - * @since 1.0.19 |
|
| 367 | - * @param string $context View or edit context. |
|
| 368 | - * @return string |
|
| 369 | - */ |
|
| 370 | - public function get_created( $context = 'view' ) { |
|
| 371 | - return $this->get_prop( 'created', $context ); |
|
| 372 | - } |
|
| 373 | - |
|
| 374 | - /** |
|
| 375 | - * Alias for self::get_created(). |
|
| 376 | - * |
|
| 377 | - * @since 1.0.19 |
|
| 378 | - * @param string $context View or edit context. |
|
| 379 | - * @return string |
|
| 380 | - */ |
|
| 381 | - public function get_date_created( $context = 'view' ) { |
|
| 382 | - return $this->get_created( $context ); |
|
| 383 | - } |
|
| 384 | - |
|
| 385 | - /** |
|
| 386 | - * Retrieves the creation date in a timestamp |
|
| 387 | - * |
|
| 388 | - * @since 1.0.0 |
|
| 389 | - * @return int |
|
| 390 | - */ |
|
| 391 | - public function get_time_created() { |
|
| 392 | - $created = $this->get_date_created(); |
|
| 393 | - return empty( $created ) ? current_time( 'timestamp' ) : strtotime( $created, current_time( 'timestamp' ) ); |
|
| 394 | - } |
|
| 395 | - |
|
| 396 | - /** |
|
| 397 | - * Get GMT date when the subscription was created. |
|
| 398 | - * |
|
| 399 | - * @since 1.0.19 |
|
| 400 | - * @param string $context View or edit context. |
|
| 401 | - * @return string |
|
| 402 | - */ |
|
| 403 | - public function get_date_created_gmt( $context = 'view' ) { |
|
| 404 | - $date = $this->get_date_created( $context ); |
|
| 617 | + /** |
|
| 618 | + * Set date when this subscription started. |
|
| 619 | + * |
|
| 620 | + * @since 1.0.19 |
|
| 621 | + * @param string $value strtotime compliant date. |
|
| 622 | + */ |
|
| 623 | + public function set_created( $value ) { |
|
| 624 | + $date = strtotime( $value ); |
|
| 405 | 625 | |
| 406 | - if ( $date ) { |
|
| 407 | - $date = get_gmt_from_date( $date ); |
|
| 626 | + if ( $date && $value !== '0000-00-00 00:00:00' ) { |
|
| 627 | + $this->set_prop( 'created', date( 'Y-m-d H:i:s', $date ) ); |
|
| 628 | + return; |
|
| 408 | 629 | } |
| 409 | - return $date; |
|
| 410 | - } |
|
| 411 | - |
|
| 412 | - /** |
|
| 413 | - * Get the date that the subscription will renew. |
|
| 414 | - * |
|
| 415 | - * @since 1.0.19 |
|
| 416 | - * @param string $context View or edit context. |
|
| 417 | - * @return string |
|
| 418 | - */ |
|
| 419 | - public function get_next_renewal_date( $context = 'view' ) { |
|
| 420 | - return $this->get_prop( 'expiration', $context ); |
|
| 421 | - } |
|
| 422 | - |
|
| 423 | - /** |
|
| 424 | - * Alias for self::get_next_renewal_date(). |
|
| 425 | - * |
|
| 426 | - * @since 1.0.19 |
|
| 427 | - * @param string $context View or edit context. |
|
| 428 | - * @return string |
|
| 429 | - */ |
|
| 430 | - public function get_expiration( $context = 'view' ) { |
|
| 431 | - return $this->get_next_renewal_date( $context ); |
|
| 432 | - } |
|
| 433 | - |
|
| 434 | - /** |
|
| 435 | - * Retrieves the expiration date in a timestamp |
|
| 436 | - * |
|
| 437 | - * @since 1.0.0 |
|
| 438 | - * @return int |
|
| 439 | - */ |
|
| 440 | - public function get_expiration_time() { |
|
| 441 | - $expiration = $this->get_expiration(); |
|
| 442 | - |
|
| 443 | - if ( empty( $expiration ) || '0000-00-00 00:00:00' == $expiration ) { |
|
| 444 | - return current_time( 'timestamp' ); |
|
| 445 | - } |
|
| 446 | - |
|
| 447 | - $expiration = strtotime( $expiration, current_time( 'timestamp' ) ); |
|
| 448 | - return $expiration < current_time( 'timestamp' ) ? current_time( 'timestamp' ) : $expiration; |
|
| 449 | - } |
|
| 450 | - |
|
| 451 | - /** |
|
| 452 | - * Get GMT date when the subscription will renew. |
|
| 453 | - * |
|
| 454 | - * @since 1.0.19 |
|
| 455 | - * @param string $context View or edit context. |
|
| 456 | - * @return string |
|
| 457 | - */ |
|
| 458 | - public function get_next_renewal_date_gmt( $context = 'view' ) { |
|
| 459 | - $date = $this->get_next_renewal_date( $context ); |
|
| 460 | 630 | |
| 461 | - if ( $date ) { |
|
| 462 | - $date = get_gmt_from_date( $date ); |
|
| 463 | - } |
|
| 464 | - return $date; |
|
| 465 | - } |
|
| 466 | - |
|
| 467 | - /** |
|
| 468 | - * Get the subscription's trial period. |
|
| 469 | - * |
|
| 470 | - * @since 1.0.19 |
|
| 471 | - * @param string $context View or edit context. |
|
| 472 | - * @return string |
|
| 473 | - */ |
|
| 474 | - public function get_trial_period( $context = 'view' ) { |
|
| 475 | - return $this->get_prop( 'trial_period', $context ); |
|
| 476 | - } |
|
| 477 | - |
|
| 478 | - /** |
|
| 479 | - * Get the subscription's status. |
|
| 480 | - * |
|
| 481 | - * @since 1.0.19 |
|
| 482 | - * @param string $context View or edit context. |
|
| 483 | - * @return string |
|
| 484 | - */ |
|
| 485 | - public function get_status( $context = 'view' ) { |
|
| 486 | - return $this->get_prop( 'status', $context ); |
|
| 487 | - } |
|
| 488 | - |
|
| 489 | - /** |
|
| 490 | - * Get the subscription's profile id. |
|
| 491 | - * |
|
| 492 | - * @since 1.0.19 |
|
| 493 | - * @param string $context View or edit context. |
|
| 494 | - * @return string |
|
| 495 | - */ |
|
| 496 | - public function get_profile_id( $context = 'view' ) { |
|
| 497 | - return $this->get_prop( 'profile_id', $context ); |
|
| 498 | - } |
|
| 499 | - |
|
| 500 | - /* |
|
| 501 | - |-------------------------------------------------------------------------- |
|
| 502 | - | Setters |
|
| 503 | - |-------------------------------------------------------------------------- |
|
| 504 | - */ |
|
| 631 | + $this->set_prop( 'created', '' ); |
|
| 505 | 632 | |
| 506 | - /** |
|
| 507 | - * Set customer id. |
|
| 508 | - * |
|
| 509 | - * @since 1.0.19 |
|
| 510 | - * @param int $value The customer's id. |
|
| 511 | - */ |
|
| 512 | - public function set_customer_id( $value ) { |
|
| 513 | - $this->set_prop( 'customer_id', (int) $value ); |
|
| 514 | - } |
|
| 515 | - |
|
| 516 | - /** |
|
| 517 | - * Set parent invoice id. |
|
| 518 | - * |
|
| 519 | - * @since 1.0.19 |
|
| 520 | - * @param int $value The parent invoice id. |
|
| 521 | - */ |
|
| 522 | - public function set_parent_invoice_id( $value ) { |
|
| 523 | - $this->set_prop( 'parent_payment_id', (int) $value ); |
|
| 524 | - } |
|
| 525 | - |
|
| 526 | - /** |
|
| 527 | - * Alias for self::set_parent_invoice_id(). |
|
| 528 | - * |
|
| 529 | - * @since 1.0.19 |
|
| 530 | - * @param int $value The parent invoice id. |
|
| 531 | - */ |
|
| 532 | - public function set_parent_payment_id( $value ) { |
|
| 533 | - $this->set_parent_invoice_id( $value ); |
|
| 534 | - } |
|
| 633 | + } |
|
| 535 | 634 | |
| 536 | - /** |
|
| 537 | - * Alias for self::set_parent_invoice_id(). |
|
| 635 | + /** |
|
| 636 | + * Alias for self::set_created(). |
|
| 538 | 637 | * |
| 539 | 638 | * @since 1.0.19 |
| 540 | - * @param int $value The parent invoice id. |
|
| 639 | + * @param string $value strtotime compliant date. |
|
| 541 | 640 | */ |
| 542 | - public function set_original_payment_id( $value ) { |
|
| 543 | - $this->set_parent_invoice_id( $value ); |
|
| 544 | - } |
|
| 545 | - |
|
| 546 | - /** |
|
| 547 | - * Set subscription's product id. |
|
| 548 | - * |
|
| 549 | - * @since 1.0.19 |
|
| 550 | - * @param int $value The subscription product id. |
|
| 551 | - */ |
|
| 552 | - public function set_product_id( $value ) { |
|
| 553 | - $this->set_prop( 'product_id', (int) $value ); |
|
| 554 | - } |
|
| 555 | - |
|
| 556 | - /** |
|
| 557 | - * Set the period of a renewal. |
|
| 558 | - * |
|
| 559 | - * @since 1.0.19 |
|
| 560 | - * @param string $value The renewal period. |
|
| 561 | - */ |
|
| 562 | - public function set_period( $value ) { |
|
| 563 | - $this->set_prop( 'period', $value ); |
|
| 564 | - } |
|
| 565 | - |
|
| 566 | - /** |
|
| 567 | - * Set number of periods each renewal is valid for. |
|
| 568 | - * |
|
| 569 | - * @since 1.0.19 |
|
| 570 | - * @param int $value The subscription frequency. |
|
| 571 | - */ |
|
| 572 | - public function set_frequency( $value ) { |
|
| 573 | - $value = empty( $value ) ? 1 : (int) $value; |
|
| 574 | - $this->set_prop( 'frequency', absint( $value ) ); |
|
| 575 | - } |
|
| 576 | - |
|
| 577 | - /** |
|
| 578 | - * Set the initial amount for the subscription. |
|
| 579 | - * |
|
| 580 | - * @since 1.0.19 |
|
| 581 | - * @param float $value The initial subcription amount. |
|
| 582 | - */ |
|
| 583 | - public function set_initial_amount( $value ) { |
|
| 584 | - $this->set_prop( 'initial_amount', wpinv_sanitize_amount( $value ) ); |
|
| 585 | - } |
|
| 586 | - |
|
| 587 | - /** |
|
| 588 | - * Set the recurring amount for the subscription. |
|
| 589 | - * |
|
| 590 | - * @since 1.0.19 |
|
| 591 | - * @param float $value The recurring subcription amount. |
|
| 592 | - */ |
|
| 593 | - public function set_recurring_amount( $value ) { |
|
| 594 | - $this->set_prop( 'recurring_amount', wpinv_sanitize_amount( $value ) ); |
|
| 595 | - } |
|
| 596 | - |
|
| 597 | - /** |
|
| 598 | - * Set number of times that this subscription can be renewed. |
|
| 599 | - * |
|
| 600 | - * @since 1.0.19 |
|
| 601 | - * @param int $value Bill times. |
|
| 602 | - */ |
|
| 603 | - public function set_bill_times( $value ) { |
|
| 604 | - $this->set_prop( 'bill_times', (int) $value ); |
|
| 605 | - } |
|
| 606 | - |
|
| 607 | - /** |
|
| 608 | - * Get transaction id of this subscription's parent invoice. |
|
| 609 | - * |
|
| 610 | - * @since 1.0.19 |
|
| 611 | - * @param string $value Bill times. |
|
| 612 | - */ |
|
| 613 | - public function set_transaction_id( $value ) { |
|
| 614 | - $this->set_prop( 'transaction_id', sanitize_text_field( $value ) ); |
|
| 615 | - } |
|
| 616 | - |
|
| 617 | - /** |
|
| 618 | - * Set date when this subscription started. |
|
| 619 | - * |
|
| 620 | - * @since 1.0.19 |
|
| 621 | - * @param string $value strtotime compliant date. |
|
| 622 | - */ |
|
| 623 | - public function set_created( $value ) { |
|
| 641 | + public function set_date_created( $value ) { |
|
| 642 | + $this->set_created( $value ); |
|
| 643 | + } |
|
| 644 | + |
|
| 645 | + /** |
|
| 646 | + * Set the date that the subscription will renew. |
|
| 647 | + * |
|
| 648 | + * @since 1.0.19 |
|
| 649 | + * @param string $value strtotime compliant date. |
|
| 650 | + */ |
|
| 651 | + public function set_next_renewal_date( $value ) { |
|
| 624 | 652 | $date = strtotime( $value ); |
| 625 | 653 | |
| 626 | 654 | if ( $date && $value !== '0000-00-00 00:00:00' ) { |
| 627 | - $this->set_prop( 'created', date( 'Y-m-d H:i:s', $date ) ); |
|
| 655 | + $this->set_prop( 'expiration', date( 'Y-m-d H:i:s', $date ) ); |
|
| 628 | 656 | return; |
| 629 | 657 | } |
| 630 | 658 | |
| 631 | - $this->set_prop( 'created', '' ); |
|
| 659 | + $this->set_prop( 'expiration', '' ); |
|
| 660 | + |
|
| 661 | + } |
|
| 632 | 662 | |
| 633 | - } |
|
| 663 | + /** |
|
| 664 | + * Alias for self::set_next_renewal_date(). |
|
| 665 | + * |
|
| 666 | + * @since 1.0.19 |
|
| 667 | + * @param string $value strtotime compliant date. |
|
| 668 | + */ |
|
| 669 | + public function set_expiration( $value ) { |
|
| 670 | + $this->set_next_renewal_date( $value ); |
|
| 671 | + } |
|
| 634 | 672 | |
| 635 | - /** |
|
| 636 | - * Alias for self::set_created(). |
|
| 637 | - * |
|
| 638 | - * @since 1.0.19 |
|
| 639 | - * @param string $value strtotime compliant date. |
|
| 640 | - */ |
|
| 641 | - public function set_date_created( $value ) { |
|
| 642 | - $this->set_created( $value ); |
|
| 673 | + /** |
|
| 674 | + * Set the subscription's trial period. |
|
| 675 | + * |
|
| 676 | + * @since 1.0.19 |
|
| 677 | + * @param string $value trial period e.g 1 year. |
|
| 678 | + */ |
|
| 679 | + public function set_trial_period( $value ) { |
|
| 680 | + $this->set_prop( 'trial_period', $value ); |
|
| 643 | 681 | } |
| 644 | 682 | |
| 645 | - /** |
|
| 646 | - * Set the date that the subscription will renew. |
|
| 647 | - * |
|
| 648 | - * @since 1.0.19 |
|
| 649 | - * @param string $value strtotime compliant date. |
|
| 650 | - */ |
|
| 651 | - public function set_next_renewal_date( $value ) { |
|
| 652 | - $date = strtotime( $value ); |
|
| 683 | + /** |
|
| 684 | + * Set the subscription's status. |
|
| 685 | + * |
|
| 686 | + * @since 1.0.19 |
|
| 687 | + * @param string $new_status New subscription status. |
|
| 688 | + */ |
|
| 689 | + public function set_status( $new_status ) { |
|
| 653 | 690 | |
| 654 | - if ( $date && $value !== '0000-00-00 00:00:00' ) { |
|
| 655 | - $this->set_prop( 'expiration', date( 'Y-m-d H:i:s', $date ) ); |
|
| 691 | + // Abort if this is not a valid status; |
|
| 692 | + if ( ! array_key_exists( $new_status, getpaid_get_subscription_statuses() ) ) { |
|
| 656 | 693 | return; |
| 657 | - } |
|
| 658 | - |
|
| 659 | - $this->set_prop( 'expiration', '' ); |
|
| 660 | - |
|
| 661 | - } |
|
| 662 | - |
|
| 663 | - /** |
|
| 664 | - * Alias for self::set_next_renewal_date(). |
|
| 665 | - * |
|
| 666 | - * @since 1.0.19 |
|
| 667 | - * @param string $value strtotime compliant date. |
|
| 668 | - */ |
|
| 669 | - public function set_expiration( $value ) { |
|
| 670 | - $this->set_next_renewal_date( $value ); |
|
| 671 | - } |
|
| 672 | - |
|
| 673 | - /** |
|
| 674 | - * Set the subscription's trial period. |
|
| 675 | - * |
|
| 676 | - * @since 1.0.19 |
|
| 677 | - * @param string $value trial period e.g 1 year. |
|
| 678 | - */ |
|
| 679 | - public function set_trial_period( $value ) { |
|
| 680 | - $this->set_prop( 'trial_period', $value ); |
|
| 681 | - } |
|
| 682 | - |
|
| 683 | - /** |
|
| 684 | - * Set the subscription's status. |
|
| 685 | - * |
|
| 686 | - * @since 1.0.19 |
|
| 687 | - * @param string $new_status New subscription status. |
|
| 688 | - */ |
|
| 689 | - public function set_status( $new_status ) { |
|
| 690 | - |
|
| 691 | - // Abort if this is not a valid status; |
|
| 692 | - if ( ! array_key_exists( $new_status, getpaid_get_subscription_statuses() ) ) { |
|
| 693 | - return; |
|
| 694 | - } |
|
| 695 | - |
|
| 696 | - $old_status = $this->get_status(); |
|
| 697 | - $this->set_prop( 'status', $new_status ); |
|
| 698 | - |
|
| 699 | - if ( true === $this->object_read && $old_status !== $new_status ) { |
|
| 700 | - $this->status_transition = array( |
|
| 701 | - 'from' => ! empty( $this->status_transition['from'] ) ? $this->status_transition['from'] : $old_status, |
|
| 702 | - 'to' => $new_status, |
|
| 703 | - ); |
|
| 704 | - } |
|
| 705 | - |
|
| 706 | - } |
|
| 707 | - |
|
| 708 | - /** |
|
| 709 | - * Set the subscription's (remote) profile id. |
|
| 710 | - * |
|
| 711 | - * @since 1.0.19 |
|
| 712 | - * @param string $value the remote profile id. |
|
| 713 | - */ |
|
| 714 | - public function set_profile_id( $value ) { |
|
| 715 | - $this->set_prop( 'profile_id', sanitize_text_field( $value ) ); |
|
| 716 | - } |
|
| 717 | - |
|
| 718 | - /* |
|
| 694 | + } |
|
| 695 | + |
|
| 696 | + $old_status = $this->get_status(); |
|
| 697 | + $this->set_prop( 'status', $new_status ); |
|
| 698 | + |
|
| 699 | + if ( true === $this->object_read && $old_status !== $new_status ) { |
|
| 700 | + $this->status_transition = array( |
|
| 701 | + 'from' => ! empty( $this->status_transition['from'] ) ? $this->status_transition['from'] : $old_status, |
|
| 702 | + 'to' => $new_status, |
|
| 703 | + ); |
|
| 704 | + } |
|
| 705 | + |
|
| 706 | + } |
|
| 707 | + |
|
| 708 | + /** |
|
| 709 | + * Set the subscription's (remote) profile id. |
|
| 710 | + * |
|
| 711 | + * @since 1.0.19 |
|
| 712 | + * @param string $value the remote profile id. |
|
| 713 | + */ |
|
| 714 | + public function set_profile_id( $value ) { |
|
| 715 | + $this->set_prop( 'profile_id', sanitize_text_field( $value ) ); |
|
| 716 | + } |
|
| 717 | + |
|
| 718 | + /* |
|
| 719 | 719 | |-------------------------------------------------------------------------- |
| 720 | 720 | | Boolean methods |
| 721 | 721 | |-------------------------------------------------------------------------- |
@@ -724,55 +724,55 @@ discard block |
||
| 724 | 724 | | |
| 725 | 725 | */ |
| 726 | 726 | |
| 727 | - /** |
|
| 727 | + /** |
|
| 728 | 728 | * Checks if the subscription has a given status. |
| 729 | - * |
|
| 730 | - * @param string|array String or array of strings to check for. |
|
| 731 | - * @return bool |
|
| 729 | + * |
|
| 730 | + * @param string|array String or array of strings to check for. |
|
| 731 | + * @return bool |
|
| 732 | 732 | */ |
| 733 | 733 | public function has_status( $status ) { |
| 734 | 734 | return in_array( $this->get_status(), wpinv_clean( wpinv_parse_list( $status ) ) ); |
| 735 | - } |
|
| 735 | + } |
|
| 736 | 736 | |
| 737 | - /** |
|
| 737 | + /** |
|
| 738 | 738 | * Checks if the subscription has a trial period. |
| 739 | - * |
|
| 740 | - * @return bool |
|
| 739 | + * |
|
| 740 | + * @return bool |
|
| 741 | 741 | */ |
| 742 | 742 | public function has_trial_period() { |
| 743 | - $period = $this->get_trial_period(); |
|
| 743 | + $period = $this->get_trial_period(); |
|
| 744 | 744 | return ! empty( $period ); |
| 745 | - } |
|
| 746 | - |
|
| 747 | - /** |
|
| 748 | - * Is the subscription active? |
|
| 749 | - * |
|
| 750 | - * @return bool |
|
| 751 | - */ |
|
| 752 | - public function is_active() { |
|
| 753 | - return $this->has_status( 'active trialling' ) && ! $this->is_expired(); |
|
| 754 | - } |
|
| 755 | - |
|
| 756 | - /** |
|
| 757 | - * Is the subscription expired? |
|
| 758 | - * |
|
| 759 | - * @return bool |
|
| 760 | - */ |
|
| 761 | - public function is_expired() { |
|
| 762 | - return $this->has_status( 'expired' ) || ( $this->has_status( 'active cancelled trialling' ) && $this->get_expiration_time() < current_time( 'mysql' ) ); |
|
| 763 | - } |
|
| 764 | - |
|
| 765 | - /** |
|
| 766 | - * Is this the last renewals? |
|
| 767 | - * |
|
| 768 | - * @return bool |
|
| 769 | - */ |
|
| 770 | - public function is_last_renewal() { |
|
| 771 | - $max_bills = $this->get_bill_times(); |
|
| 772 | - return ! empty( $max_bills ) && $max_bills <= $this->get_times_billed(); |
|
| 773 | - } |
|
| 774 | - |
|
| 775 | - /* |
|
| 745 | + } |
|
| 746 | + |
|
| 747 | + /** |
|
| 748 | + * Is the subscription active? |
|
| 749 | + * |
|
| 750 | + * @return bool |
|
| 751 | + */ |
|
| 752 | + public function is_active() { |
|
| 753 | + return $this->has_status( 'active trialling' ) && ! $this->is_expired(); |
|
| 754 | + } |
|
| 755 | + |
|
| 756 | + /** |
|
| 757 | + * Is the subscription expired? |
|
| 758 | + * |
|
| 759 | + * @return bool |
|
| 760 | + */ |
|
| 761 | + public function is_expired() { |
|
| 762 | + return $this->has_status( 'expired' ) || ( $this->has_status( 'active cancelled trialling' ) && $this->get_expiration_time() < current_time( 'mysql' ) ); |
|
| 763 | + } |
|
| 764 | + |
|
| 765 | + /** |
|
| 766 | + * Is this the last renewals? |
|
| 767 | + * |
|
| 768 | + * @return bool |
|
| 769 | + */ |
|
| 770 | + public function is_last_renewal() { |
|
| 771 | + $max_bills = $this->get_bill_times(); |
|
| 772 | + return ! empty( $max_bills ) && $max_bills <= $this->get_times_billed(); |
|
| 773 | + } |
|
| 774 | + |
|
| 775 | + /* |
|
| 776 | 776 | |-------------------------------------------------------------------------- |
| 777 | 777 | | Additional methods |
| 778 | 778 | |-------------------------------------------------------------------------- |
@@ -781,27 +781,27 @@ discard block |
||
| 781 | 781 | | |
| 782 | 782 | */ |
| 783 | 783 | |
| 784 | - /** |
|
| 785 | - * Backwards compatibilty. |
|
| 786 | - */ |
|
| 787 | - public function create( $data = array() ) { |
|
| 784 | + /** |
|
| 785 | + * Backwards compatibilty. |
|
| 786 | + */ |
|
| 787 | + public function create( $data = array() ) { |
|
| 788 | 788 | |
| 789 | - // Set the properties. |
|
| 790 | - if ( is_array( $data ) ) { |
|
| 791 | - $this->set_props( $data ); |
|
| 792 | - } |
|
| 789 | + // Set the properties. |
|
| 790 | + if ( is_array( $data ) ) { |
|
| 791 | + $this->set_props( $data ); |
|
| 792 | + } |
|
| 793 | 793 | |
| 794 | - // Save the item. |
|
| 795 | - return $this->save(); |
|
| 794 | + // Save the item. |
|
| 795 | + return $this->save(); |
|
| 796 | 796 | |
| 797 | - } |
|
| 797 | + } |
|
| 798 | 798 | |
| 799 | - /** |
|
| 800 | - * Backwards compatibilty. |
|
| 801 | - */ |
|
| 802 | - public function update( $args = array() ) { |
|
| 803 | - return $this->create( $args ); |
|
| 804 | - } |
|
| 799 | + /** |
|
| 800 | + * Backwards compatibilty. |
|
| 801 | + */ |
|
| 802 | + public function update( $args = array() ) { |
|
| 803 | + return $this->create( $args ); |
|
| 804 | + } |
|
| 805 | 805 | |
| 806 | 806 | /** |
| 807 | 807 | * Retrieve renewal payments for a subscription |
@@ -811,22 +811,22 @@ discard block |
||
| 811 | 811 | */ |
| 812 | 812 | public function get_child_payments( $hide_pending = true ) { |
| 813 | 813 | |
| 814 | - $statuses = array( 'publish', 'wpi-processing', 'wpi-renewal' ); |
|
| 814 | + $statuses = array( 'publish', 'wpi-processing', 'wpi-renewal' ); |
|
| 815 | 815 | |
| 816 | - if ( ! $hide_pending ) { |
|
| 817 | - $statuses = array_keys( wpinv_get_invoice_statuses() ); |
|
| 818 | - } |
|
| 816 | + if ( ! $hide_pending ) { |
|
| 817 | + $statuses = array_keys( wpinv_get_invoice_statuses() ); |
|
| 818 | + } |
|
| 819 | 819 | |
| 820 | 820 | return get_posts( |
| 821 | - array( |
|
| 822 | - 'post_parent' => $this->get_parent_payment_id(), |
|
| 823 | - 'numberposts' => -1, |
|
| 824 | - 'post_status' => $statuses, |
|
| 825 | - 'orderby' => 'ID', |
|
| 826 | - 'order' => 'ASC', |
|
| 827 | - 'post_type' => 'wpi_invoice' |
|
| 828 | - ) |
|
| 829 | - ); |
|
| 821 | + array( |
|
| 822 | + 'post_parent' => $this->get_parent_payment_id(), |
|
| 823 | + 'numberposts' => -1, |
|
| 824 | + 'post_status' => $statuses, |
|
| 825 | + 'orderby' => 'ID', |
|
| 826 | + 'order' => 'ASC', |
|
| 827 | + 'post_type' => 'wpi_invoice' |
|
| 828 | + ) |
|
| 829 | + ); |
|
| 830 | 830 | } |
| 831 | 831 | |
| 832 | 832 | /** |
@@ -836,16 +836,16 @@ discard block |
||
| 836 | 836 | * @return int |
| 837 | 837 | */ |
| 838 | 838 | public function get_total_payments() { |
| 839 | - global $wpdb; |
|
| 839 | + global $wpdb; |
|
| 840 | 840 | |
| 841 | - $count = (int) $wpdb->get_var( |
|
| 842 | - $wpdb->prepare( |
|
| 843 | - "SELECT COUNT(ID) FROM $wpdb->posts WHERE post_parent=%d AND post_status IN ( 'publish', 'wpi-processing', 'wpi-renewal' )", |
|
| 844 | - $this->get_parent_invoice_id() |
|
| 845 | - ) |
|
| 846 | - ); |
|
| 841 | + $count = (int) $wpdb->get_var( |
|
| 842 | + $wpdb->prepare( |
|
| 843 | + "SELECT COUNT(ID) FROM $wpdb->posts WHERE post_parent=%d AND post_status IN ( 'publish', 'wpi-processing', 'wpi-renewal' )", |
|
| 844 | + $this->get_parent_invoice_id() |
|
| 845 | + ) |
|
| 846 | + ); |
|
| 847 | 847 | |
| 848 | - // Maybe include parent invoice. |
|
| 848 | + // Maybe include parent invoice. |
|
| 849 | 849 | if ( $this->get_parent_payment()->is_paid() ) { |
| 850 | 850 | $count++; |
| 851 | 851 | } |
@@ -874,57 +874,57 @@ discard block |
||
| 874 | 874 | * |
| 875 | 875 | * @since 2.4 |
| 876 | 876 | * @param array $args Array of values for the payment, including amount and transaction ID |
| 877 | - * @param WPInv_Invoice $invoice If adding an existing invoice. |
|
| 877 | + * @param WPInv_Invoice $invoice If adding an existing invoice. |
|
| 878 | 878 | * @return bool |
| 879 | 879 | */ |
| 880 | 880 | public function add_payment( $args = array(), $invoice = false ) { |
| 881 | 881 | |
| 882 | - // Process each payment once. |
|
| 882 | + // Process each payment once. |
|
| 883 | 883 | if ( ! empty( $args['transaction_id'] ) && $this->payment_exists( $args['transaction_id'] ) ) { |
| 884 | 884 | return false; |
| 885 | 885 | } |
| 886 | 886 | |
| 887 | - // Are we creating a new invoice? |
|
| 888 | - if ( empty( $invoice ) ) { |
|
| 889 | - $invoice = $this->create_payment(); |
|
| 887 | + // Are we creating a new invoice? |
|
| 888 | + if ( empty( $invoice ) ) { |
|
| 889 | + $invoice = $this->create_payment(); |
|
| 890 | 890 | |
| 891 | - if ( empty( $invoice ) ) { |
|
| 892 | - return false; |
|
| 893 | - } |
|
| 891 | + if ( empty( $invoice ) ) { |
|
| 892 | + return false; |
|
| 893 | + } |
|
| 894 | 894 | |
| 895 | - } |
|
| 895 | + } |
|
| 896 | 896 | |
| 897 | - $invoice->set_status( 'wpi-renewal' ); |
|
| 897 | + $invoice->set_status( 'wpi-renewal' ); |
|
| 898 | 898 | |
| 899 | - // Maybe set a transaction id. |
|
| 900 | - if ( ! empty( $args['transaction_id'] ) ) { |
|
| 901 | - $invoice->set_transaction_id( $args['transaction_id'] ); |
|
| 902 | - } |
|
| 899 | + // Maybe set a transaction id. |
|
| 900 | + if ( ! empty( $args['transaction_id'] ) ) { |
|
| 901 | + $invoice->set_transaction_id( $args['transaction_id'] ); |
|
| 902 | + } |
|
| 903 | 903 | |
| 904 | - // Set the completed date. |
|
| 905 | - $invoice->set_completed_date( current_time( 'mysql' ) ); |
|
| 904 | + // Set the completed date. |
|
| 905 | + $invoice->set_completed_date( current_time( 'mysql' ) ); |
|
| 906 | 906 | |
| 907 | - // And the gateway. |
|
| 908 | - if ( ! empty( $args['gateway'] ) ) { |
|
| 909 | - $invoice->set_gateway( $args['gateway'] ); |
|
| 910 | - } |
|
| 907 | + // And the gateway. |
|
| 908 | + if ( ! empty( $args['gateway'] ) ) { |
|
| 909 | + $invoice->set_gateway( $args['gateway'] ); |
|
| 910 | + } |
|
| 911 | 911 | |
| 912 | - $invoice->save(); |
|
| 912 | + $invoice->save(); |
|
| 913 | 913 | |
| 914 | - if ( ! $invoice->exists() ) { |
|
| 915 | - return false; |
|
| 916 | - } |
|
| 914 | + if ( ! $invoice->exists() ) { |
|
| 915 | + return false; |
|
| 916 | + } |
|
| 917 | 917 | |
| 918 | - do_action( 'getpaid_after_create_subscription_renewal_invoice', $invoice, $this ); |
|
| 919 | - do_action( 'wpinv_recurring_add_subscription_payment', $invoice, $this ); |
|
| 918 | + do_action( 'getpaid_after_create_subscription_renewal_invoice', $invoice, $this ); |
|
| 919 | + do_action( 'wpinv_recurring_add_subscription_payment', $invoice, $this ); |
|
| 920 | 920 | do_action( 'wpinv_recurring_record_payment', $invoice->get_id(), $this->get_parent_invoice_id(), $invoice->get_recurring_total(), $invoice->get_transaction_id() ); |
| 921 | 921 | |
| 922 | 922 | update_post_meta( $invoice->get_id(), '_wpinv_subscription_id', $this->id ); |
| 923 | 923 | |
| 924 | 924 | return $invoice->get_id(); |
| 925 | - } |
|
| 925 | + } |
|
| 926 | 926 | |
| 927 | - /** |
|
| 927 | + /** |
|
| 928 | 928 | * Creates a new invoice and returns it. |
| 929 | 929 | * |
| 930 | 930 | * @since 1.0.19 |
@@ -932,97 +932,97 @@ discard block |
||
| 932 | 932 | */ |
| 933 | 933 | public function create_payment() { |
| 934 | 934 | |
| 935 | - $parent_invoice = $this->get_parent_payment(); |
|
| 936 | - |
|
| 937 | - if ( ! $parent_invoice->exists() ) { |
|
| 938 | - return false; |
|
| 939 | - } |
|
| 940 | - |
|
| 941 | - // Duplicate the parent invoice. |
|
| 942 | - $invoice = getpaid_duplicate_invoice( $parent_invoice ); |
|
| 943 | - $invoice->set_parent_id( $parent_invoice->get_id() ); |
|
| 944 | - $invoice->recalculate_total(); |
|
| 945 | - $invoice->set_status( 'wpi-pending' ); |
|
| 946 | - $invoice->save(); |
|
| 947 | - |
|
| 948 | - return $invoice->exists() ? $invoice : false; |
|
| 949 | - } |
|
| 950 | - |
|
| 951 | - /** |
|
| 952 | - * Renews or completes a subscription |
|
| 953 | - * |
|
| 954 | - * @since 1.0.0 |
|
| 955 | - * @return int The subscription's id |
|
| 956 | - */ |
|
| 957 | - public function renew() { |
|
| 958 | - |
|
| 959 | - // Complete subscription if applicable |
|
| 960 | - if ( $this->is_last_renewal() ) { |
|
| 961 | - return $this->complete(); |
|
| 962 | - } |
|
| 963 | - |
|
| 964 | - // Calculate new expiration |
|
| 965 | - $frequency = $this->get_frequency(); |
|
| 966 | - $period = $this->get_period(); |
|
| 967 | - $new_expiration = strtotime( "+ $frequency $period", $this->get_expiration_time() ); |
|
| 968 | - |
|
| 969 | - $this->set_expiration( date( 'Y-m-d H:i:s',$new_expiration ) ); |
|
| 970 | - $this->set_status( 'active' ); |
|
| 971 | - return $this->save(); |
|
| 972 | - |
|
| 973 | - do_action( 'getpaid_subscription_renewed', $this ); |
|
| 974 | - |
|
| 975 | - } |
|
| 976 | - |
|
| 977 | - /** |
|
| 978 | - * Marks a subscription as completed |
|
| 979 | - * |
|
| 980 | - * Subscription is completed when the number of payments matches the billing_times field |
|
| 981 | - * |
|
| 982 | - * @since 1.0.0 |
|
| 983 | - * @return int|bool Subscription id or false if the subscription is cancelled. |
|
| 984 | - */ |
|
| 985 | - public function complete() { |
|
| 986 | - |
|
| 987 | - // Only mark a subscription as complete if it's not already cancelled. |
|
| 988 | - if ( $this->has_status( 'cancelled' ) ) { |
|
| 989 | - return false; |
|
| 990 | - } |
|
| 991 | - |
|
| 992 | - $this->set_status( 'completed' ); |
|
| 993 | - return $this->save(); |
|
| 994 | - |
|
| 995 | - } |
|
| 996 | - |
|
| 997 | - /** |
|
| 998 | - * Marks a subscription as expired |
|
| 999 | - * |
|
| 1000 | - * @since 1.0.0 |
|
| 1001 | - * @param bool $check_expiration |
|
| 1002 | - * @return int|bool Subscription id or false if $check_expiration is true and expiration date is in the future. |
|
| 1003 | - */ |
|
| 1004 | - public function expire( $check_expiration = false ) { |
|
| 1005 | - |
|
| 1006 | - if ( $check_expiration && $this->get_expiration_time() > current_time( 'timestamp' ) ) { |
|
| 1007 | - // Do not mark as expired since real expiration date is in the future |
|
| 1008 | - return false; |
|
| 1009 | - } |
|
| 1010 | - |
|
| 1011 | - $this->set_status( 'expired' ); |
|
| 1012 | - return $this->save(); |
|
| 1013 | - |
|
| 1014 | - } |
|
| 1015 | - |
|
| 1016 | - /** |
|
| 1017 | - * Marks a subscription as failing |
|
| 1018 | - * |
|
| 1019 | - * @since 2.4.2 |
|
| 1020 | - * @return int Subscription id. |
|
| 1021 | - */ |
|
| 1022 | - public function failing() { |
|
| 1023 | - $this->set_status( 'failing' ); |
|
| 1024 | - return $this->save(); |
|
| 1025 | - } |
|
| 935 | + $parent_invoice = $this->get_parent_payment(); |
|
| 936 | + |
|
| 937 | + if ( ! $parent_invoice->exists() ) { |
|
| 938 | + return false; |
|
| 939 | + } |
|
| 940 | + |
|
| 941 | + // Duplicate the parent invoice. |
|
| 942 | + $invoice = getpaid_duplicate_invoice( $parent_invoice ); |
|
| 943 | + $invoice->set_parent_id( $parent_invoice->get_id() ); |
|
| 944 | + $invoice->recalculate_total(); |
|
| 945 | + $invoice->set_status( 'wpi-pending' ); |
|
| 946 | + $invoice->save(); |
|
| 947 | + |
|
| 948 | + return $invoice->exists() ? $invoice : false; |
|
| 949 | + } |
|
| 950 | + |
|
| 951 | + /** |
|
| 952 | + * Renews or completes a subscription |
|
| 953 | + * |
|
| 954 | + * @since 1.0.0 |
|
| 955 | + * @return int The subscription's id |
|
| 956 | + */ |
|
| 957 | + public function renew() { |
|
| 958 | + |
|
| 959 | + // Complete subscription if applicable |
|
| 960 | + if ( $this->is_last_renewal() ) { |
|
| 961 | + return $this->complete(); |
|
| 962 | + } |
|
| 963 | + |
|
| 964 | + // Calculate new expiration |
|
| 965 | + $frequency = $this->get_frequency(); |
|
| 966 | + $period = $this->get_period(); |
|
| 967 | + $new_expiration = strtotime( "+ $frequency $period", $this->get_expiration_time() ); |
|
| 968 | + |
|
| 969 | + $this->set_expiration( date( 'Y-m-d H:i:s',$new_expiration ) ); |
|
| 970 | + $this->set_status( 'active' ); |
|
| 971 | + return $this->save(); |
|
| 972 | + |
|
| 973 | + do_action( 'getpaid_subscription_renewed', $this ); |
|
| 974 | + |
|
| 975 | + } |
|
| 976 | + |
|
| 977 | + /** |
|
| 978 | + * Marks a subscription as completed |
|
| 979 | + * |
|
| 980 | + * Subscription is completed when the number of payments matches the billing_times field |
|
| 981 | + * |
|
| 982 | + * @since 1.0.0 |
|
| 983 | + * @return int|bool Subscription id or false if the subscription is cancelled. |
|
| 984 | + */ |
|
| 985 | + public function complete() { |
|
| 986 | + |
|
| 987 | + // Only mark a subscription as complete if it's not already cancelled. |
|
| 988 | + if ( $this->has_status( 'cancelled' ) ) { |
|
| 989 | + return false; |
|
| 990 | + } |
|
| 991 | + |
|
| 992 | + $this->set_status( 'completed' ); |
|
| 993 | + return $this->save(); |
|
| 994 | + |
|
| 995 | + } |
|
| 996 | + |
|
| 997 | + /** |
|
| 998 | + * Marks a subscription as expired |
|
| 999 | + * |
|
| 1000 | + * @since 1.0.0 |
|
| 1001 | + * @param bool $check_expiration |
|
| 1002 | + * @return int|bool Subscription id or false if $check_expiration is true and expiration date is in the future. |
|
| 1003 | + */ |
|
| 1004 | + public function expire( $check_expiration = false ) { |
|
| 1005 | + |
|
| 1006 | + if ( $check_expiration && $this->get_expiration_time() > current_time( 'timestamp' ) ) { |
|
| 1007 | + // Do not mark as expired since real expiration date is in the future |
|
| 1008 | + return false; |
|
| 1009 | + } |
|
| 1010 | + |
|
| 1011 | + $this->set_status( 'expired' ); |
|
| 1012 | + return $this->save(); |
|
| 1013 | + |
|
| 1014 | + } |
|
| 1015 | + |
|
| 1016 | + /** |
|
| 1017 | + * Marks a subscription as failing |
|
| 1018 | + * |
|
| 1019 | + * @since 2.4.2 |
|
| 1020 | + * @return int Subscription id. |
|
| 1021 | + */ |
|
| 1022 | + public function failing() { |
|
| 1023 | + $this->set_status( 'failing' ); |
|
| 1024 | + return $this->save(); |
|
| 1025 | + } |
|
| 1026 | 1026 | |
| 1027 | 1027 | /** |
| 1028 | 1028 | * Marks a subscription as cancelled |
@@ -1031,19 +1031,19 @@ discard block |
||
| 1031 | 1031 | * @return int Subscription id. |
| 1032 | 1032 | */ |
| 1033 | 1033 | public function cancel() { |
| 1034 | - $this->set_status( 'cancelled' ); |
|
| 1035 | - return $this->save(); |
|
| 1034 | + $this->set_status( 'cancelled' ); |
|
| 1035 | + return $this->save(); |
|
| 1036 | 1036 | } |
| 1037 | 1037 | |
| 1038 | - /** |
|
| 1039 | - * Determines if a subscription can be cancelled both locally and with a payment processor. |
|
| 1040 | - * |
|
| 1041 | - * @since 1.0.0 |
|
| 1042 | - * @return bool |
|
| 1043 | - */ |
|
| 1044 | - public function can_cancel() { |
|
| 1045 | - return apply_filters( 'wpinv_subscription_can_cancel', $this->has_status( $this->get_cancellable_statuses() ), $this ); |
|
| 1046 | - } |
|
| 1038 | + /** |
|
| 1039 | + * Determines if a subscription can be cancelled both locally and with a payment processor. |
|
| 1040 | + * |
|
| 1041 | + * @since 1.0.0 |
|
| 1042 | + * @return bool |
|
| 1043 | + */ |
|
| 1044 | + public function can_cancel() { |
|
| 1045 | + return apply_filters( 'wpinv_subscription_can_cancel', $this->has_status( $this->get_cancellable_statuses() ), $this ); |
|
| 1046 | + } |
|
| 1047 | 1047 | |
| 1048 | 1048 | /** |
| 1049 | 1049 | * Returns an array of subscription statuses that can be cancelled |
@@ -1056,96 +1056,96 @@ discard block |
||
| 1056 | 1056 | return apply_filters( 'wpinv_recurring_cancellable_statuses', array( 'active', 'trialling', 'failing' ) ); |
| 1057 | 1057 | } |
| 1058 | 1058 | |
| 1059 | - /** |
|
| 1060 | - * Retrieves the URL to cancel subscription |
|
| 1061 | - * |
|
| 1062 | - * @since 1.0.0 |
|
| 1063 | - * @return string |
|
| 1064 | - */ |
|
| 1065 | - public function get_cancel_url() { |
|
| 1066 | - $url = getpaid_get_authenticated_action_url( 'subscription_cancel', $this->get_view_url() ); |
|
| 1067 | - return apply_filters( 'wpinv_subscription_cancel_url', $url, $this ); |
|
| 1068 | - } |
|
| 1069 | - |
|
| 1070 | - /** |
|
| 1071 | - * Retrieves the URL to view a subscription |
|
| 1072 | - * |
|
| 1073 | - * @since 1.0.19 |
|
| 1074 | - * @return string |
|
| 1075 | - */ |
|
| 1076 | - public function get_view_url() { |
|
| 1077 | - |
|
| 1078 | - $url = getpaid_get_tab_url( 'gp-subscriptions', get_permalink( (int) wpinv_get_option( 'invoice_subscription_page' ) ) ); |
|
| 1079 | - $url = add_query_arg( 'subscription', $this->get_id(), $url ); |
|
| 1080 | - |
|
| 1081 | - return apply_filters( 'getpaid_get_subscription_view_url', $url, $this ); |
|
| 1082 | - } |
|
| 1083 | - |
|
| 1084 | - /** |
|
| 1085 | - * Determines if subscription can be manually renewed |
|
| 1086 | - * |
|
| 1087 | - * This method is filtered by payment gateways in order to return true on subscriptions |
|
| 1088 | - * that can be renewed manually |
|
| 1089 | - * |
|
| 1090 | - * @since 2.5 |
|
| 1091 | - * @return bool |
|
| 1092 | - */ |
|
| 1093 | - public function can_renew() { |
|
| 1094 | - return apply_filters( 'wpinv_subscription_can_renew', true, $this ); |
|
| 1095 | - } |
|
| 1096 | - |
|
| 1097 | - /** |
|
| 1098 | - * Retrieves the URL to renew a subscription |
|
| 1099 | - * |
|
| 1100 | - * @since 2.5 |
|
| 1101 | - * @return string |
|
| 1102 | - */ |
|
| 1103 | - public function get_renew_url() { |
|
| 1104 | - $url = wp_nonce_url( add_query_arg( array( 'getpaid-action' => 'renew_subscription', 'sub_id' => $this->get_id ) ), 'getpaid-nonce' ); |
|
| 1105 | - return apply_filters( 'wpinv_subscription_renew_url', $url, $this ); |
|
| 1106 | - } |
|
| 1107 | - |
|
| 1108 | - /** |
|
| 1109 | - * Determines if subscription can have their payment method updated |
|
| 1110 | - * |
|
| 1111 | - * @since 1.0.0 |
|
| 1112 | - * @return bool |
|
| 1113 | - */ |
|
| 1114 | - public function can_update() { |
|
| 1115 | - return apply_filters( 'wpinv_subscription_can_update', false, $this ); |
|
| 1116 | - } |
|
| 1117 | - |
|
| 1118 | - /** |
|
| 1119 | - * Retrieves the URL to update subscription |
|
| 1120 | - * |
|
| 1121 | - * @since 1.0.0 |
|
| 1122 | - * @return string |
|
| 1123 | - */ |
|
| 1124 | - public function get_update_url() { |
|
| 1125 | - $url = add_query_arg( array( 'action' => 'update', 'subscription_id' => $this->get_id() ) ); |
|
| 1126 | - return apply_filters( 'wpinv_subscription_update_url', $url, $this ); |
|
| 1127 | - } |
|
| 1128 | - |
|
| 1129 | - /** |
|
| 1130 | - * Retrieves the subscription status label |
|
| 1131 | - * |
|
| 1132 | - * @since 1.0.0 |
|
| 1133 | - * @return string |
|
| 1134 | - */ |
|
| 1135 | - public function get_status_label() { |
|
| 1136 | - return getpaid_get_subscription_status_label( $this->get_status() ); |
|
| 1137 | - } |
|
| 1138 | - |
|
| 1139 | - /** |
|
| 1140 | - * Retrieves the subscription status class |
|
| 1141 | - * |
|
| 1142 | - * @since 1.0.19 |
|
| 1143 | - * @return string |
|
| 1144 | - */ |
|
| 1145 | - public function get_status_class() { |
|
| 1146 | - $statuses = getpaid_get_subscription_status_classes(); |
|
| 1147 | - return isset( $statuses[ $this->get_status() ] ) ? $statuses[ $this->get_status() ] : 'badge-dark'; |
|
| 1148 | - } |
|
| 1059 | + /** |
|
| 1060 | + * Retrieves the URL to cancel subscription |
|
| 1061 | + * |
|
| 1062 | + * @since 1.0.0 |
|
| 1063 | + * @return string |
|
| 1064 | + */ |
|
| 1065 | + public function get_cancel_url() { |
|
| 1066 | + $url = getpaid_get_authenticated_action_url( 'subscription_cancel', $this->get_view_url() ); |
|
| 1067 | + return apply_filters( 'wpinv_subscription_cancel_url', $url, $this ); |
|
| 1068 | + } |
|
| 1069 | + |
|
| 1070 | + /** |
|
| 1071 | + * Retrieves the URL to view a subscription |
|
| 1072 | + * |
|
| 1073 | + * @since 1.0.19 |
|
| 1074 | + * @return string |
|
| 1075 | + */ |
|
| 1076 | + public function get_view_url() { |
|
| 1077 | + |
|
| 1078 | + $url = getpaid_get_tab_url( 'gp-subscriptions', get_permalink( (int) wpinv_get_option( 'invoice_subscription_page' ) ) ); |
|
| 1079 | + $url = add_query_arg( 'subscription', $this->get_id(), $url ); |
|
| 1080 | + |
|
| 1081 | + return apply_filters( 'getpaid_get_subscription_view_url', $url, $this ); |
|
| 1082 | + } |
|
| 1083 | + |
|
| 1084 | + /** |
|
| 1085 | + * Determines if subscription can be manually renewed |
|
| 1086 | + * |
|
| 1087 | + * This method is filtered by payment gateways in order to return true on subscriptions |
|
| 1088 | + * that can be renewed manually |
|
| 1089 | + * |
|
| 1090 | + * @since 2.5 |
|
| 1091 | + * @return bool |
|
| 1092 | + */ |
|
| 1093 | + public function can_renew() { |
|
| 1094 | + return apply_filters( 'wpinv_subscription_can_renew', true, $this ); |
|
| 1095 | + } |
|
| 1096 | + |
|
| 1097 | + /** |
|
| 1098 | + * Retrieves the URL to renew a subscription |
|
| 1099 | + * |
|
| 1100 | + * @since 2.5 |
|
| 1101 | + * @return string |
|
| 1102 | + */ |
|
| 1103 | + public function get_renew_url() { |
|
| 1104 | + $url = wp_nonce_url( add_query_arg( array( 'getpaid-action' => 'renew_subscription', 'sub_id' => $this->get_id ) ), 'getpaid-nonce' ); |
|
| 1105 | + return apply_filters( 'wpinv_subscription_renew_url', $url, $this ); |
|
| 1106 | + } |
|
| 1107 | + |
|
| 1108 | + /** |
|
| 1109 | + * Determines if subscription can have their payment method updated |
|
| 1110 | + * |
|
| 1111 | + * @since 1.0.0 |
|
| 1112 | + * @return bool |
|
| 1113 | + */ |
|
| 1114 | + public function can_update() { |
|
| 1115 | + return apply_filters( 'wpinv_subscription_can_update', false, $this ); |
|
| 1116 | + } |
|
| 1117 | + |
|
| 1118 | + /** |
|
| 1119 | + * Retrieves the URL to update subscription |
|
| 1120 | + * |
|
| 1121 | + * @since 1.0.0 |
|
| 1122 | + * @return string |
|
| 1123 | + */ |
|
| 1124 | + public function get_update_url() { |
|
| 1125 | + $url = add_query_arg( array( 'action' => 'update', 'subscription_id' => $this->get_id() ) ); |
|
| 1126 | + return apply_filters( 'wpinv_subscription_update_url', $url, $this ); |
|
| 1127 | + } |
|
| 1128 | + |
|
| 1129 | + /** |
|
| 1130 | + * Retrieves the subscription status label |
|
| 1131 | + * |
|
| 1132 | + * @since 1.0.0 |
|
| 1133 | + * @return string |
|
| 1134 | + */ |
|
| 1135 | + public function get_status_label() { |
|
| 1136 | + return getpaid_get_subscription_status_label( $this->get_status() ); |
|
| 1137 | + } |
|
| 1138 | + |
|
| 1139 | + /** |
|
| 1140 | + * Retrieves the subscription status class |
|
| 1141 | + * |
|
| 1142 | + * @since 1.0.19 |
|
| 1143 | + * @return string |
|
| 1144 | + */ |
|
| 1145 | + public function get_status_class() { |
|
| 1146 | + $statuses = getpaid_get_subscription_status_classes(); |
|
| 1147 | + return isset( $statuses[ $this->get_status() ] ) ? $statuses[ $this->get_status() ] : 'badge-dark'; |
|
| 1148 | + } |
|
| 1149 | 1149 | |
| 1150 | 1150 | /** |
| 1151 | 1151 | * Retrieves the subscription status label |
@@ -1155,11 +1155,11 @@ discard block |
||
| 1155 | 1155 | */ |
| 1156 | 1156 | public function get_status_label_html() { |
| 1157 | 1157 | |
| 1158 | - $status_label = sanitize_text_field( $this->get_status_label() ); |
|
| 1159 | - $class = esc_attr( $this->get_status_class() ); |
|
| 1160 | - $status = sanitize_html_class( $this->get_status() ); |
|
| 1158 | + $status_label = sanitize_text_field( $this->get_status_label() ); |
|
| 1159 | + $class = esc_attr( $this->get_status_class() ); |
|
| 1160 | + $status = sanitize_html_class( $this->get_status() ); |
|
| 1161 | 1161 | |
| 1162 | - return "<span class='bsui'><span class='badge $class $status'>$status_label</span></span>"; |
|
| 1162 | + return "<span class='bsui'><span class='badge $class $status'>$status_label</span></span>"; |
|
| 1163 | 1163 | } |
| 1164 | 1164 | |
| 1165 | 1165 | /** |
@@ -1170,75 +1170,75 @@ discard block |
||
| 1170 | 1170 | * @return bool |
| 1171 | 1171 | */ |
| 1172 | 1172 | public function payment_exists( $txn_id = '' ) { |
| 1173 | - $invoice_id = WPInv_Invoice::get_invoice_id_by_field( $txn_id, 'transaction_id' ); |
|
| 1173 | + $invoice_id = WPInv_Invoice::get_invoice_id_by_field( $txn_id, 'transaction_id' ); |
|
| 1174 | 1174 | return ! empty( $invoice_id ); |
| 1175 | - } |
|
| 1176 | - |
|
| 1177 | - /** |
|
| 1178 | - * Handle the status transition. |
|
| 1179 | - */ |
|
| 1180 | - protected function status_transition() { |
|
| 1181 | - $status_transition = $this->status_transition; |
|
| 1182 | - |
|
| 1183 | - // Reset status transition variable. |
|
| 1184 | - $this->status_transition = false; |
|
| 1185 | - |
|
| 1186 | - if ( $status_transition ) { |
|
| 1187 | - try { |
|
| 1188 | - |
|
| 1189 | - // Fire a hook for the status change. |
|
| 1190 | - do_action( 'wpinv_subscription_' . $status_transition['to'], $this->get_id(), $this, $status_transition ); |
|
| 1191 | - do_action( 'getpaid_subscription_' . $status_transition['to'], $this, $status_transition ); |
|
| 1192 | - |
|
| 1193 | - if ( ! empty( $status_transition['from'] ) ) { |
|
| 1194 | - |
|
| 1195 | - /* translators: 1: old subscription status 2: new subscription status */ |
|
| 1196 | - $transition_note = sprintf( __( 'Subscription status changed from %1$s to %2$s.', 'invoicing' ), getpaid_get_subscription_status_label( $status_transition['from'] ), getpaid_get_subscription_status_label( $status_transition['to'] ) ); |
|
| 1197 | - |
|
| 1198 | - // Note the transition occurred. |
|
| 1199 | - $this->get_parent_payment()->add_note( $transition_note, false, false, true ); |
|
| 1200 | - |
|
| 1201 | - // Fire another hook. |
|
| 1202 | - do_action( 'getpaid_subscription_status_' . $status_transition['from'] . '_to_' . $status_transition['to'], $this->get_id(), $this ); |
|
| 1203 | - do_action( 'getpaid_subscription_status_changed', $this, $status_transition['from'], $status_transition['to'] ); |
|
| 1204 | - |
|
| 1205 | - } else { |
|
| 1206 | - /* translators: %s: new invoice status */ |
|
| 1207 | - $transition_note = sprintf( __( 'Subscription status set to %s.', 'invoicing' ), getpaid_get_subscription_status_label( $status_transition['to'] ) ); |
|
| 1208 | - |
|
| 1209 | - // Note the transition occurred. |
|
| 1210 | - $this->get_parent_payment()->add_note( $transition_note, false, false, true ); |
|
| 1211 | - |
|
| 1212 | - } |
|
| 1213 | - } catch ( Exception $e ) { |
|
| 1214 | - $this->get_parent_payment()->add_note( __( 'Error during subscription status transition.', 'invoicing' ) . ' ' . $e->getMessage() ); |
|
| 1215 | - } |
|
| 1216 | - } |
|
| 1217 | - |
|
| 1218 | - } |
|
| 1219 | - |
|
| 1220 | - /** |
|
| 1221 | - * Save data to the database. |
|
| 1222 | - * |
|
| 1223 | - * @since 1.0.19 |
|
| 1224 | - * @return int subscription ID |
|
| 1225 | - */ |
|
| 1226 | - public function save() { |
|
| 1227 | - parent::save(); |
|
| 1228 | - $this->status_transition(); |
|
| 1229 | - return $this->get_id(); |
|
| 1230 | - } |
|
| 1231 | - |
|
| 1232 | - /** |
|
| 1233 | - * Activates a subscription. |
|
| 1234 | - * |
|
| 1235 | - * @since 1.0.19 |
|
| 1236 | - * @return int subscription ID |
|
| 1237 | - */ |
|
| 1238 | - public function activate() { |
|
| 1239 | - $status = 'trialling' == $this->get_status() ? 'trialling' : 'active'; |
|
| 1240 | - $this->set_status( $status ); |
|
| 1241 | - return $this->save(); |
|
| 1242 | - } |
|
| 1175 | + } |
|
| 1176 | + |
|
| 1177 | + /** |
|
| 1178 | + * Handle the status transition. |
|
| 1179 | + */ |
|
| 1180 | + protected function status_transition() { |
|
| 1181 | + $status_transition = $this->status_transition; |
|
| 1182 | + |
|
| 1183 | + // Reset status transition variable. |
|
| 1184 | + $this->status_transition = false; |
|
| 1185 | + |
|
| 1186 | + if ( $status_transition ) { |
|
| 1187 | + try { |
|
| 1188 | + |
|
| 1189 | + // Fire a hook for the status change. |
|
| 1190 | + do_action( 'wpinv_subscription_' . $status_transition['to'], $this->get_id(), $this, $status_transition ); |
|
| 1191 | + do_action( 'getpaid_subscription_' . $status_transition['to'], $this, $status_transition ); |
|
| 1192 | + |
|
| 1193 | + if ( ! empty( $status_transition['from'] ) ) { |
|
| 1194 | + |
|
| 1195 | + /* translators: 1: old subscription status 2: new subscription status */ |
|
| 1196 | + $transition_note = sprintf( __( 'Subscription status changed from %1$s to %2$s.', 'invoicing' ), getpaid_get_subscription_status_label( $status_transition['from'] ), getpaid_get_subscription_status_label( $status_transition['to'] ) ); |
|
| 1197 | + |
|
| 1198 | + // Note the transition occurred. |
|
| 1199 | + $this->get_parent_payment()->add_note( $transition_note, false, false, true ); |
|
| 1200 | + |
|
| 1201 | + // Fire another hook. |
|
| 1202 | + do_action( 'getpaid_subscription_status_' . $status_transition['from'] . '_to_' . $status_transition['to'], $this->get_id(), $this ); |
|
| 1203 | + do_action( 'getpaid_subscription_status_changed', $this, $status_transition['from'], $status_transition['to'] ); |
|
| 1204 | + |
|
| 1205 | + } else { |
|
| 1206 | + /* translators: %s: new invoice status */ |
|
| 1207 | + $transition_note = sprintf( __( 'Subscription status set to %s.', 'invoicing' ), getpaid_get_subscription_status_label( $status_transition['to'] ) ); |
|
| 1208 | + |
|
| 1209 | + // Note the transition occurred. |
|
| 1210 | + $this->get_parent_payment()->add_note( $transition_note, false, false, true ); |
|
| 1211 | + |
|
| 1212 | + } |
|
| 1213 | + } catch ( Exception $e ) { |
|
| 1214 | + $this->get_parent_payment()->add_note( __( 'Error during subscription status transition.', 'invoicing' ) . ' ' . $e->getMessage() ); |
|
| 1215 | + } |
|
| 1216 | + } |
|
| 1217 | + |
|
| 1218 | + } |
|
| 1219 | + |
|
| 1220 | + /** |
|
| 1221 | + * Save data to the database. |
|
| 1222 | + * |
|
| 1223 | + * @since 1.0.19 |
|
| 1224 | + * @return int subscription ID |
|
| 1225 | + */ |
|
| 1226 | + public function save() { |
|
| 1227 | + parent::save(); |
|
| 1228 | + $this->status_transition(); |
|
| 1229 | + return $this->get_id(); |
|
| 1230 | + } |
|
| 1231 | + |
|
| 1232 | + /** |
|
| 1233 | + * Activates a subscription. |
|
| 1234 | + * |
|
| 1235 | + * @since 1.0.19 |
|
| 1236 | + * @return int subscription ID |
|
| 1237 | + */ |
|
| 1238 | + public function activate() { |
|
| 1239 | + $status = 'trialling' == $this->get_status() ? 'trialling' : 'active'; |
|
| 1240 | + $this->set_status( $status ); |
|
| 1241 | + return $this->save(); |
|
| 1242 | + } |
|
| 1243 | 1243 | |
| 1244 | 1244 | } |
@@ -5,7 +5,7 @@ discard block |
||
| 5 | 5 | * |
| 6 | 6 | */ |
| 7 | 7 | if ( ! defined( 'ABSPATH' ) ) { |
| 8 | - exit; |
|
| 8 | + exit; |
|
| 9 | 9 | } |
| 10 | 10 | |
| 11 | 11 | /** |
@@ -15,197 +15,197 @@ discard block |
||
| 15 | 15 | */ |
| 16 | 16 | class GetPaid_Subscription_Data_Store { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * A map of database fields to data types. |
|
| 20 | - * |
|
| 21 | - * @since 1.0.19 |
|
| 22 | - * @var array |
|
| 23 | - */ |
|
| 24 | - protected $database_fields_to_data_type = array( |
|
| 25 | - 'id' => '%d', |
|
| 26 | - 'customer_id' => '%d', |
|
| 27 | - 'frequency' => '%d', |
|
| 28 | - 'period' => '%s', |
|
| 29 | - 'initial_amount' => '%s', |
|
| 30 | - 'recurring_amount' => '%s', |
|
| 31 | - 'bill_times' => '%d', |
|
| 32 | - 'transaction_id' => '%s', |
|
| 33 | - 'parent_payment_id' => '%d', |
|
| 34 | - 'product_id' => '%d', |
|
| 35 | - 'created' => '%s', |
|
| 36 | - 'expiration' => '%s', |
|
| 37 | - 'trial_period' => '%s', |
|
| 38 | - 'status' => '%s', |
|
| 39 | - 'profile_id' => '%s', |
|
| 40 | - ); |
|
| 41 | - |
|
| 42 | - /* |
|
| 18 | + /** |
|
| 19 | + * A map of database fields to data types. |
|
| 20 | + * |
|
| 21 | + * @since 1.0.19 |
|
| 22 | + * @var array |
|
| 23 | + */ |
|
| 24 | + protected $database_fields_to_data_type = array( |
|
| 25 | + 'id' => '%d', |
|
| 26 | + 'customer_id' => '%d', |
|
| 27 | + 'frequency' => '%d', |
|
| 28 | + 'period' => '%s', |
|
| 29 | + 'initial_amount' => '%s', |
|
| 30 | + 'recurring_amount' => '%s', |
|
| 31 | + 'bill_times' => '%d', |
|
| 32 | + 'transaction_id' => '%s', |
|
| 33 | + 'parent_payment_id' => '%d', |
|
| 34 | + 'product_id' => '%d', |
|
| 35 | + 'created' => '%s', |
|
| 36 | + 'expiration' => '%s', |
|
| 37 | + 'trial_period' => '%s', |
|
| 38 | + 'status' => '%s', |
|
| 39 | + 'profile_id' => '%s', |
|
| 40 | + ); |
|
| 41 | + |
|
| 42 | + /* |
|
| 43 | 43 | |-------------------------------------------------------------------------- |
| 44 | 44 | | CRUD Methods |
| 45 | 45 | |-------------------------------------------------------------------------- |
| 46 | 46 | */ |
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Method to create a new subscription in the database. |
|
| 50 | - * |
|
| 51 | - * @param WPInv_Subscription $subscription Subscription object. |
|
| 52 | - */ |
|
| 53 | - public function create( &$subscription ) { |
|
| 54 | - global $wpdb; |
|
| 55 | - |
|
| 56 | - $values = array(); |
|
| 57 | - $formats = array(); |
|
| 58 | - |
|
| 59 | - $fields = $this->database_fields_to_data_type; |
|
| 60 | - unset( $fields['id'] ); |
|
| 61 | - |
|
| 62 | - foreach ( $fields as $key => $format ) { |
|
| 63 | - $method = "get_$key"; |
|
| 64 | - $values[$key] = $subscription->$method( 'edit' ); |
|
| 65 | - $formats[] = $format; |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - $result = $wpdb->insert( $wpdb->prefix . 'wpinv_subscriptions', $values, $formats ); |
|
| 69 | - |
|
| 70 | - if ( $result ) { |
|
| 71 | - $subscription->set_id( $wpdb->insert_id ); |
|
| 72 | - $subscription->apply_changes(); |
|
| 73 | - $subscription->clear_cache(); |
|
| 74 | - update_post_meta( $subscription->get_parent_invoice_id(), '_wpinv_subscription_id', $subscription->get_id() ); |
|
| 75 | - do_action( 'getpaid_new_subscription', $subscription ); |
|
| 76 | - return true; |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - return false; |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * Method to read a subscription from the database. |
|
| 84 | - * |
|
| 85 | - * @param WPInv_Subscription $subscription Subscription object. |
|
| 86 | - * |
|
| 87 | - */ |
|
| 88 | - public function read( &$subscription ) { |
|
| 89 | - global $wpdb; |
|
| 90 | - |
|
| 91 | - $subscription->set_defaults(); |
|
| 92 | - |
|
| 93 | - if ( ! $subscription->get_id() ) { |
|
| 94 | - $subscription->last_error = __( 'Invalid subscription ID.', 'invoicing' ); |
|
| 95 | - $subscription->set_id( 0 ); |
|
| 96 | - return false; |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - // Maybe retrieve from the cache. |
|
| 100 | - $raw_subscription = wp_cache_get( $subscription->get_id(), 'getpaid_subscriptions' ); |
|
| 101 | - |
|
| 102 | - // If not found, retrieve from the db. |
|
| 103 | - if ( false === $raw_subscription ) { |
|
| 104 | - |
|
| 105 | - $raw_subscription = $wpdb->get_row( |
|
| 106 | - $wpdb->prepare( |
|
| 107 | - "SELECT * FROM {$wpdb->prefix}wpinv_subscriptions WHERE id = %d", |
|
| 108 | - $subscription->get_id() |
|
| 109 | - ) |
|
| 110 | - ); |
|
| 111 | - |
|
| 112 | - // Update the cache with our data |
|
| 113 | - wp_cache_set( $subscription->get_id(), $raw_subscription, 'getpaid_subscriptions' ); |
|
| 114 | - |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - if ( ! $raw_subscription ) { |
|
| 118 | - $subscription->last_error = __( 'Invalid subscription ID.', 'invoicing' ); |
|
| 119 | - return false; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - foreach ( array_keys( $this->database_fields_to_data_type ) as $key ) { |
|
| 123 | - $method = "set_$key"; |
|
| 124 | - $subscription->$method( $raw_subscription->$key ); |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - $subscription->set_object_read( true ); |
|
| 128 | - do_action( 'getpaid_read_subscription', $subscription ); |
|
| 129 | - |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * Method to update a subscription in the database. |
|
| 134 | - * |
|
| 135 | - * @param WPInv_Subscription $subscription Subscription object. |
|
| 136 | - */ |
|
| 137 | - public function update( &$subscription ) { |
|
| 138 | - global $wpdb; |
|
| 139 | - |
|
| 140 | - $changes = $subscription->get_changes(); |
|
| 141 | - $values = array(); |
|
| 142 | - $formats = array(); |
|
| 143 | - |
|
| 144 | - foreach ( $this->database_fields_to_data_type as $key => $format ) { |
|
| 145 | - if ( array_key_exists( $key, $changes ) ) { |
|
| 146 | - $method = "get_$key"; |
|
| 147 | - $values[$key] = $subscription->$method( 'edit' ); |
|
| 148 | - $formats[] = $format; |
|
| 149 | - } |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - if ( empty( $values ) ) { |
|
| 153 | - return; |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - $wpdb->update( |
|
| 157 | - $wpdb->prefix . 'wpinv_subscriptions', |
|
| 158 | - $values, |
|
| 159 | - array( |
|
| 160 | - 'id' => $subscription->get_id(), |
|
| 161 | - ), |
|
| 162 | - $formats, |
|
| 163 | - '%d' |
|
| 164 | - ); |
|
| 165 | - |
|
| 166 | - // Apply the changes. |
|
| 167 | - $subscription->apply_changes(); |
|
| 168 | - |
|
| 169 | - // Delete cache. |
|
| 170 | - $subscription->clear_cache(); |
|
| 171 | - |
|
| 172 | - update_post_meta( $subscription->get_parent_invoice_id(), '_wpinv_subscr_profile_id', $subscription->get_profile_id() ); |
|
| 173 | - update_post_meta( $subscription->get_parent_invoice_id(), '_wpinv_subscription_id', $subscription->get_id() ); |
|
| 174 | - |
|
| 175 | - // Fire a hook. |
|
| 176 | - do_action( 'getpaid_update_subscription', $subscription ); |
|
| 177 | - |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * Method to delete a subscription from the database. |
|
| 182 | - * |
|
| 183 | - * @param WPInv_Subscription $subscription |
|
| 184 | - */ |
|
| 185 | - public function delete( &$subscription ) { |
|
| 186 | - global $wpdb; |
|
| 187 | - |
|
| 188 | - $wpdb->query( |
|
| 189 | - $wpdb->prepare( |
|
| 190 | - "DELETE FROM {$wpdb->prefix}wpinv_subscriptions |
|
| 48 | + /** |
|
| 49 | + * Method to create a new subscription in the database. |
|
| 50 | + * |
|
| 51 | + * @param WPInv_Subscription $subscription Subscription object. |
|
| 52 | + */ |
|
| 53 | + public function create( &$subscription ) { |
|
| 54 | + global $wpdb; |
|
| 55 | + |
|
| 56 | + $values = array(); |
|
| 57 | + $formats = array(); |
|
| 58 | + |
|
| 59 | + $fields = $this->database_fields_to_data_type; |
|
| 60 | + unset( $fields['id'] ); |
|
| 61 | + |
|
| 62 | + foreach ( $fields as $key => $format ) { |
|
| 63 | + $method = "get_$key"; |
|
| 64 | + $values[$key] = $subscription->$method( 'edit' ); |
|
| 65 | + $formats[] = $format; |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + $result = $wpdb->insert( $wpdb->prefix . 'wpinv_subscriptions', $values, $formats ); |
|
| 69 | + |
|
| 70 | + if ( $result ) { |
|
| 71 | + $subscription->set_id( $wpdb->insert_id ); |
|
| 72 | + $subscription->apply_changes(); |
|
| 73 | + $subscription->clear_cache(); |
|
| 74 | + update_post_meta( $subscription->get_parent_invoice_id(), '_wpinv_subscription_id', $subscription->get_id() ); |
|
| 75 | + do_action( 'getpaid_new_subscription', $subscription ); |
|
| 76 | + return true; |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + return false; |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * Method to read a subscription from the database. |
|
| 84 | + * |
|
| 85 | + * @param WPInv_Subscription $subscription Subscription object. |
|
| 86 | + * |
|
| 87 | + */ |
|
| 88 | + public function read( &$subscription ) { |
|
| 89 | + global $wpdb; |
|
| 90 | + |
|
| 91 | + $subscription->set_defaults(); |
|
| 92 | + |
|
| 93 | + if ( ! $subscription->get_id() ) { |
|
| 94 | + $subscription->last_error = __( 'Invalid subscription ID.', 'invoicing' ); |
|
| 95 | + $subscription->set_id( 0 ); |
|
| 96 | + return false; |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + // Maybe retrieve from the cache. |
|
| 100 | + $raw_subscription = wp_cache_get( $subscription->get_id(), 'getpaid_subscriptions' ); |
|
| 101 | + |
|
| 102 | + // If not found, retrieve from the db. |
|
| 103 | + if ( false === $raw_subscription ) { |
|
| 104 | + |
|
| 105 | + $raw_subscription = $wpdb->get_row( |
|
| 106 | + $wpdb->prepare( |
|
| 107 | + "SELECT * FROM {$wpdb->prefix}wpinv_subscriptions WHERE id = %d", |
|
| 108 | + $subscription->get_id() |
|
| 109 | + ) |
|
| 110 | + ); |
|
| 111 | + |
|
| 112 | + // Update the cache with our data |
|
| 113 | + wp_cache_set( $subscription->get_id(), $raw_subscription, 'getpaid_subscriptions' ); |
|
| 114 | + |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + if ( ! $raw_subscription ) { |
|
| 118 | + $subscription->last_error = __( 'Invalid subscription ID.', 'invoicing' ); |
|
| 119 | + return false; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + foreach ( array_keys( $this->database_fields_to_data_type ) as $key ) { |
|
| 123 | + $method = "set_$key"; |
|
| 124 | + $subscription->$method( $raw_subscription->$key ); |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + $subscription->set_object_read( true ); |
|
| 128 | + do_action( 'getpaid_read_subscription', $subscription ); |
|
| 129 | + |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * Method to update a subscription in the database. |
|
| 134 | + * |
|
| 135 | + * @param WPInv_Subscription $subscription Subscription object. |
|
| 136 | + */ |
|
| 137 | + public function update( &$subscription ) { |
|
| 138 | + global $wpdb; |
|
| 139 | + |
|
| 140 | + $changes = $subscription->get_changes(); |
|
| 141 | + $values = array(); |
|
| 142 | + $formats = array(); |
|
| 143 | + |
|
| 144 | + foreach ( $this->database_fields_to_data_type as $key => $format ) { |
|
| 145 | + if ( array_key_exists( $key, $changes ) ) { |
|
| 146 | + $method = "get_$key"; |
|
| 147 | + $values[$key] = $subscription->$method( 'edit' ); |
|
| 148 | + $formats[] = $format; |
|
| 149 | + } |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + if ( empty( $values ) ) { |
|
| 153 | + return; |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + $wpdb->update( |
|
| 157 | + $wpdb->prefix . 'wpinv_subscriptions', |
|
| 158 | + $values, |
|
| 159 | + array( |
|
| 160 | + 'id' => $subscription->get_id(), |
|
| 161 | + ), |
|
| 162 | + $formats, |
|
| 163 | + '%d' |
|
| 164 | + ); |
|
| 165 | + |
|
| 166 | + // Apply the changes. |
|
| 167 | + $subscription->apply_changes(); |
|
| 168 | + |
|
| 169 | + // Delete cache. |
|
| 170 | + $subscription->clear_cache(); |
|
| 171 | + |
|
| 172 | + update_post_meta( $subscription->get_parent_invoice_id(), '_wpinv_subscr_profile_id', $subscription->get_profile_id() ); |
|
| 173 | + update_post_meta( $subscription->get_parent_invoice_id(), '_wpinv_subscription_id', $subscription->get_id() ); |
|
| 174 | + |
|
| 175 | + // Fire a hook. |
|
| 176 | + do_action( 'getpaid_update_subscription', $subscription ); |
|
| 177 | + |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * Method to delete a subscription from the database. |
|
| 182 | + * |
|
| 183 | + * @param WPInv_Subscription $subscription |
|
| 184 | + */ |
|
| 185 | + public function delete( &$subscription ) { |
|
| 186 | + global $wpdb; |
|
| 187 | + |
|
| 188 | + $wpdb->query( |
|
| 189 | + $wpdb->prepare( |
|
| 190 | + "DELETE FROM {$wpdb->prefix}wpinv_subscriptions |
|
| 191 | 191 | WHERE id = %d", |
| 192 | - $subscription->get_id() |
|
| 193 | - ) |
|
| 194 | - ); |
|
| 192 | + $subscription->get_id() |
|
| 193 | + ) |
|
| 194 | + ); |
|
| 195 | 195 | |
| 196 | - delete_post_meta( $subscription->get_parent_invoice_id(), '_wpinv_subscr_profile_id' ); |
|
| 197 | - delete_post_meta( $subscription->get_parent_invoice_id(), '_wpinv_subscription_id' ); |
|
| 196 | + delete_post_meta( $subscription->get_parent_invoice_id(), '_wpinv_subscr_profile_id' ); |
|
| 197 | + delete_post_meta( $subscription->get_parent_invoice_id(), '_wpinv_subscription_id' ); |
|
| 198 | 198 | |
| 199 | - // Delete cache. |
|
| 200 | - $subscription->clear_cache(); |
|
| 199 | + // Delete cache. |
|
| 200 | + $subscription->clear_cache(); |
|
| 201 | 201 | |
| 202 | - // Fire a hook. |
|
| 203 | - do_action( 'getpaid_delete_subscription', $subscription ); |
|
| 202 | + // Fire a hook. |
|
| 203 | + do_action( 'getpaid_delete_subscription', $subscription ); |
|
| 204 | 204 | |
| 205 | - $subscription->set_id( 0 ); |
|
| 206 | - } |
|
| 205 | + $subscription->set_id( 0 ); |
|
| 206 | + } |
|
| 207 | 207 | |
| 208 | - /* |
|
| 208 | + /* |
|
| 209 | 209 | |-------------------------------------------------------------------------- |
| 210 | 210 | | Additional Methods |
| 211 | 211 | |-------------------------------------------------------------------------- |
@@ -9,7 +9,7 @@ discard block |
||
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | 11 | if ( ! defined( 'ABSPATH' ) ) { |
| 12 | - exit; |
|
| 12 | + exit; |
|
| 13 | 13 | } |
| 14 | 14 | |
| 15 | 15 | /** |
@@ -21,356 +21,356 @@ discard block |
||
| 21 | 21 | */ |
| 22 | 22 | abstract class GetPaid_Data { |
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * ID for this object. |
|
| 26 | - * |
|
| 27 | - * @since 1.0.19 |
|
| 28 | - * @var int |
|
| 29 | - */ |
|
| 30 | - protected $id = 0; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * Core data for this object. Name value pairs (name + default value). |
|
| 34 | - * |
|
| 35 | - * @since 1.0.19 |
|
| 36 | - * @var array |
|
| 37 | - */ |
|
| 38 | - protected $data = array(); |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * Core data changes for this object. |
|
| 42 | - * |
|
| 43 | - * @since 1.0.19 |
|
| 44 | - * @var array |
|
| 45 | - */ |
|
| 46 | - protected $changes = array(); |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * This is false until the object is read from the DB. |
|
| 50 | - * |
|
| 51 | - * @since 1.0.19 |
|
| 52 | - * @var bool |
|
| 53 | - */ |
|
| 54 | - protected $object_read = false; |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * This is the name of this object type. |
|
| 58 | - * |
|
| 59 | - * @since 1.0.19 |
|
| 60 | - * @var string |
|
| 61 | - */ |
|
| 62 | - protected $object_type = 'data'; |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Extra data for this object. Name value pairs (name + default value). |
|
| 66 | - * Used as a standard way for sub classes (like item types) to add |
|
| 67 | - * additional information to an inherited class. |
|
| 68 | - * |
|
| 69 | - * @since 1.0.19 |
|
| 70 | - * @var array |
|
| 71 | - */ |
|
| 72 | - protected $extra_data = array(); |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * Set to _data on construct so we can track and reset data if needed. |
|
| 76 | - * |
|
| 77 | - * @since 1.0.19 |
|
| 78 | - * @var array |
|
| 79 | - */ |
|
| 80 | - protected $default_data = array(); |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * Contains a reference to the data store for this class. |
|
| 84 | - * |
|
| 85 | - * @since 1.0.19 |
|
| 86 | - * @var GetPaid_Data_Store |
|
| 87 | - */ |
|
| 88 | - protected $data_store; |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Stores meta in cache for future reads. |
|
| 92 | - * A group must be set to to enable caching. |
|
| 93 | - * |
|
| 94 | - * @since 1.0.19 |
|
| 95 | - * @var string |
|
| 96 | - */ |
|
| 97 | - protected $cache_group = ''; |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * Stores the last error. |
|
| 101 | - * |
|
| 102 | - * @since 1.0.19 |
|
| 103 | - * @var string |
|
| 104 | - */ |
|
| 105 | - public $last_error = ''; |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * Stores additional meta data. |
|
| 109 | - * |
|
| 110 | - * @since 1.0.19 |
|
| 111 | - * @var array |
|
| 112 | - */ |
|
| 113 | - protected $meta_data = null; |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * Default constructor. |
|
| 117 | - * |
|
| 118 | - * @param int|object|array|string $read ID to load from the DB (optional) or already queried data. |
|
| 119 | - */ |
|
| 120 | - public function __construct( $read = 0 ) { |
|
| 121 | - $this->data = array_merge( $this->data, $this->extra_data ); |
|
| 122 | - $this->default_data = $this->data; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * Only store the object ID to avoid serializing the data object instance. |
|
| 127 | - * |
|
| 128 | - * @return array |
|
| 129 | - */ |
|
| 130 | - public function __sleep() { |
|
| 131 | - return array( 'id' ); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * Re-run the constructor with the object ID. |
|
| 136 | - * |
|
| 137 | - * If the object no longer exists, remove the ID. |
|
| 138 | - */ |
|
| 139 | - public function __wakeup() { |
|
| 140 | - $this->__construct( absint( $this->id ) ); |
|
| 141 | - |
|
| 142 | - if ( ! empty( $this->last_error ) ) { |
|
| 143 | - $this->set_id( 0 ); |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - /** |
|
| 149 | - * When the object is cloned, make sure meta is duplicated correctly. |
|
| 150 | - * |
|
| 151 | - * @since 1.0.19 |
|
| 152 | - */ |
|
| 153 | - public function __clone() { |
|
| 154 | - $this->maybe_read_meta_data(); |
|
| 155 | - if ( ! empty( $this->meta_data ) ) { |
|
| 156 | - foreach ( $this->meta_data as $array_key => $meta ) { |
|
| 157 | - $this->meta_data[ $array_key ] = clone $meta; |
|
| 158 | - if ( ! empty( $meta->id ) ) { |
|
| 159 | - $this->meta_data[ $array_key ]->id = null; |
|
| 160 | - } |
|
| 161 | - } |
|
| 162 | - } |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - /** |
|
| 166 | - * Get the data store. |
|
| 167 | - * |
|
| 168 | - * @since 1.0.19 |
|
| 169 | - * @return object |
|
| 170 | - */ |
|
| 171 | - public function get_data_store() { |
|
| 172 | - return $this->data_store; |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - /** |
|
| 176 | - * Get the object type. |
|
| 177 | - * |
|
| 178 | - * @since 1.0.19 |
|
| 179 | - * @return string |
|
| 180 | - */ |
|
| 181 | - public function get_object_type() { |
|
| 182 | - return $this->object_type; |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - /** |
|
| 186 | - * Returns the unique ID for this object. |
|
| 187 | - * |
|
| 188 | - * @since 1.0.19 |
|
| 189 | - * @return int |
|
| 190 | - */ |
|
| 191 | - public function get_id() { |
|
| 192 | - return $this->id; |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - /** |
|
| 196 | - * Get form status. |
|
| 197 | - * |
|
| 198 | - * @since 1.0.19 |
|
| 199 | - * @param string $context View or edit context. |
|
| 200 | - * @return string |
|
| 201 | - */ |
|
| 202 | - public function get_status( $context = 'view' ) { |
|
| 203 | - return $this->get_prop( 'status', $context ); |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - /** |
|
| 207 | - * Delete an object, set the ID to 0, and return result. |
|
| 208 | - * |
|
| 209 | - * @since 1.0.19 |
|
| 210 | - * @param bool $force_delete Should the data be deleted permanently. |
|
| 211 | - * @return bool result |
|
| 212 | - */ |
|
| 213 | - public function delete( $force_delete = false ) { |
|
| 214 | - if ( $this->data_store && $this->exists() ) { |
|
| 215 | - $this->data_store->delete( $this, array( 'force_delete' => $force_delete ) ); |
|
| 216 | - $this->set_id( 0 ); |
|
| 217 | - return true; |
|
| 218 | - } |
|
| 219 | - return false; |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - /** |
|
| 223 | - * Save should create or update based on object existence. |
|
| 224 | - * |
|
| 225 | - * @since 1.0.19 |
|
| 226 | - * @return int |
|
| 227 | - */ |
|
| 228 | - public function save() { |
|
| 229 | - if ( ! $this->data_store ) { |
|
| 230 | - return $this->get_id(); |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - /** |
|
| 234 | - * Trigger action before saving to the DB. Allows you to adjust object props before save. |
|
| 235 | - * |
|
| 236 | - * @param GetPaid_Data $this The object being saved. |
|
| 237 | - * @param GetPaid_Data_Store_WP $data_store The data store persisting the data. |
|
| 238 | - */ |
|
| 239 | - do_action( 'getpaid_before_' . $this->object_type . '_object_save', $this, $this->data_store ); |
|
| 240 | - |
|
| 241 | - if ( $this->get_id() ) { |
|
| 242 | - $this->data_store->update( $this ); |
|
| 243 | - } else { |
|
| 244 | - $this->data_store->create( $this ); |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - /** |
|
| 248 | - * Trigger action after saving to the DB. |
|
| 249 | - * |
|
| 250 | - * @param GetPaid_Data $this The object being saved. |
|
| 251 | - * @param GetPaid_Data_Store_WP $data_store The data store persisting the data. |
|
| 252 | - */ |
|
| 253 | - do_action( 'getpaid_after_' . $this->object_type . '_object_save', $this, $this->data_store ); |
|
| 254 | - |
|
| 255 | - return $this->get_id(); |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - /** |
|
| 259 | - * Change data to JSON format. |
|
| 260 | - * |
|
| 261 | - * @since 1.0.19 |
|
| 262 | - * @return string Data in JSON format. |
|
| 263 | - */ |
|
| 264 | - public function __toString() { |
|
| 265 | - return wp_json_encode( $this->get_data() ); |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - /** |
|
| 269 | - * Returns all data for this object. |
|
| 270 | - * |
|
| 271 | - * @since 1.0.19 |
|
| 272 | - * @return array |
|
| 273 | - */ |
|
| 274 | - public function get_data() { |
|
| 275 | - return array_merge( array( 'id' => $this->get_id() ), $this->data, array( 'meta_data' => $this->get_meta_data() ) ); |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - /** |
|
| 279 | - * Returns array of expected data keys for this object. |
|
| 280 | - * |
|
| 281 | - * @since 1.0.19 |
|
| 282 | - * @return array |
|
| 283 | - */ |
|
| 284 | - public function get_data_keys() { |
|
| 285 | - return array_keys( $this->data ); |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - /** |
|
| 289 | - * Returns all "extra" data keys for an object (for sub objects like item types). |
|
| 290 | - * |
|
| 291 | - * @since 1.0.19 |
|
| 292 | - * @return array |
|
| 293 | - */ |
|
| 294 | - public function get_extra_data_keys() { |
|
| 295 | - return array_keys( $this->extra_data ); |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - /** |
|
| 299 | - * Filter null meta values from array. |
|
| 300 | - * |
|
| 301 | - * @since 1.0.19 |
|
| 302 | - * @param mixed $meta Meta value to check. |
|
| 303 | - * @return bool |
|
| 304 | - */ |
|
| 305 | - protected function filter_null_meta( $meta ) { |
|
| 306 | - return ! is_null( $meta->value ); |
|
| 307 | - } |
|
| 308 | - |
|
| 309 | - /** |
|
| 310 | - * Get All Meta Data. |
|
| 311 | - * |
|
| 312 | - * @since 1.0.19 |
|
| 313 | - * @return array of objects. |
|
| 314 | - */ |
|
| 315 | - public function get_meta_data() { |
|
| 316 | - $this->maybe_read_meta_data(); |
|
| 317 | - return array_values( array_filter( $this->meta_data, array( $this, 'filter_null_meta' ) ) ); |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - /** |
|
| 321 | - * Check if the key is an internal one. |
|
| 322 | - * |
|
| 323 | - * @since 1.0.19 |
|
| 324 | - * @param string $key Key to check. |
|
| 325 | - * @return bool true if it's an internal key, false otherwise |
|
| 326 | - */ |
|
| 327 | - protected function is_internal_meta_key( $key ) { |
|
| 328 | - $internal_meta_key = ! empty( $key ) && $this->data_store && in_array( $key, $this->data_store->get_internal_meta_keys(), true ); |
|
| 329 | - |
|
| 330 | - if ( ! $internal_meta_key ) { |
|
| 331 | - return false; |
|
| 332 | - } |
|
| 333 | - |
|
| 334 | - $has_setter_or_getter = is_callable( array( $this, 'set_' . $key ) ) || is_callable( array( $this, 'get_' . $key ) ); |
|
| 335 | - |
|
| 336 | - if ( ! $has_setter_or_getter ) { |
|
| 337 | - return false; |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - /* translators: %s: $key Key to check */ |
|
| 341 | - getpaid_doing_it_wrong( __FUNCTION__, sprintf( __( 'Generic add/update/get meta methods should not be used for internal meta data, including "%s". Use getters and setters.', 'invoicing' ), $key ), '1.0.19' ); |
|
| 342 | - |
|
| 343 | - return true; |
|
| 344 | - } |
|
| 345 | - |
|
| 346 | - /** |
|
| 347 | - * Magic method for setting data fields. |
|
| 348 | - * |
|
| 349 | - * This method does not update custom fields in the database. |
|
| 350 | - * |
|
| 351 | - * @since 1.0.19 |
|
| 352 | - * @access public |
|
| 353 | - * |
|
| 354 | - */ |
|
| 355 | - public function __set( $key, $value ) { |
|
| 356 | - |
|
| 357 | - if ( 'id' == strtolower( $key ) ) { |
|
| 358 | - return $this->set_id( $value ); |
|
| 359 | - } |
|
| 360 | - |
|
| 361 | - if ( method_exists( $this, "set_$key") ) { |
|
| 362 | - |
|
| 363 | - /* translators: %s: $key Key to set */ |
|
| 364 | - getpaid_doing_it_wrong( __FUNCTION__, sprintf( __( 'Object data such as "%s" should not be accessed directly. Use getters and setters.', 'invoicing' ), $key ), '1.0.19' ); |
|
| 365 | - |
|
| 366 | - call_user_func( array( $this, "set_$key" ), $value ); |
|
| 367 | - } else { |
|
| 368 | - $this->set_prop( $key, $value ); |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - } |
|
| 372 | - |
|
| 373 | - /** |
|
| 24 | + /** |
|
| 25 | + * ID for this object. |
|
| 26 | + * |
|
| 27 | + * @since 1.0.19 |
|
| 28 | + * @var int |
|
| 29 | + */ |
|
| 30 | + protected $id = 0; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * Core data for this object. Name value pairs (name + default value). |
|
| 34 | + * |
|
| 35 | + * @since 1.0.19 |
|
| 36 | + * @var array |
|
| 37 | + */ |
|
| 38 | + protected $data = array(); |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * Core data changes for this object. |
|
| 42 | + * |
|
| 43 | + * @since 1.0.19 |
|
| 44 | + * @var array |
|
| 45 | + */ |
|
| 46 | + protected $changes = array(); |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * This is false until the object is read from the DB. |
|
| 50 | + * |
|
| 51 | + * @since 1.0.19 |
|
| 52 | + * @var bool |
|
| 53 | + */ |
|
| 54 | + protected $object_read = false; |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * This is the name of this object type. |
|
| 58 | + * |
|
| 59 | + * @since 1.0.19 |
|
| 60 | + * @var string |
|
| 61 | + */ |
|
| 62 | + protected $object_type = 'data'; |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Extra data for this object. Name value pairs (name + default value). |
|
| 66 | + * Used as a standard way for sub classes (like item types) to add |
|
| 67 | + * additional information to an inherited class. |
|
| 68 | + * |
|
| 69 | + * @since 1.0.19 |
|
| 70 | + * @var array |
|
| 71 | + */ |
|
| 72 | + protected $extra_data = array(); |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * Set to _data on construct so we can track and reset data if needed. |
|
| 76 | + * |
|
| 77 | + * @since 1.0.19 |
|
| 78 | + * @var array |
|
| 79 | + */ |
|
| 80 | + protected $default_data = array(); |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * Contains a reference to the data store for this class. |
|
| 84 | + * |
|
| 85 | + * @since 1.0.19 |
|
| 86 | + * @var GetPaid_Data_Store |
|
| 87 | + */ |
|
| 88 | + protected $data_store; |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Stores meta in cache for future reads. |
|
| 92 | + * A group must be set to to enable caching. |
|
| 93 | + * |
|
| 94 | + * @since 1.0.19 |
|
| 95 | + * @var string |
|
| 96 | + */ |
|
| 97 | + protected $cache_group = ''; |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * Stores the last error. |
|
| 101 | + * |
|
| 102 | + * @since 1.0.19 |
|
| 103 | + * @var string |
|
| 104 | + */ |
|
| 105 | + public $last_error = ''; |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * Stores additional meta data. |
|
| 109 | + * |
|
| 110 | + * @since 1.0.19 |
|
| 111 | + * @var array |
|
| 112 | + */ |
|
| 113 | + protected $meta_data = null; |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * Default constructor. |
|
| 117 | + * |
|
| 118 | + * @param int|object|array|string $read ID to load from the DB (optional) or already queried data. |
|
| 119 | + */ |
|
| 120 | + public function __construct( $read = 0 ) { |
|
| 121 | + $this->data = array_merge( $this->data, $this->extra_data ); |
|
| 122 | + $this->default_data = $this->data; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * Only store the object ID to avoid serializing the data object instance. |
|
| 127 | + * |
|
| 128 | + * @return array |
|
| 129 | + */ |
|
| 130 | + public function __sleep() { |
|
| 131 | + return array( 'id' ); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * Re-run the constructor with the object ID. |
|
| 136 | + * |
|
| 137 | + * If the object no longer exists, remove the ID. |
|
| 138 | + */ |
|
| 139 | + public function __wakeup() { |
|
| 140 | + $this->__construct( absint( $this->id ) ); |
|
| 141 | + |
|
| 142 | + if ( ! empty( $this->last_error ) ) { |
|
| 143 | + $this->set_id( 0 ); |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + /** |
|
| 149 | + * When the object is cloned, make sure meta is duplicated correctly. |
|
| 150 | + * |
|
| 151 | + * @since 1.0.19 |
|
| 152 | + */ |
|
| 153 | + public function __clone() { |
|
| 154 | + $this->maybe_read_meta_data(); |
|
| 155 | + if ( ! empty( $this->meta_data ) ) { |
|
| 156 | + foreach ( $this->meta_data as $array_key => $meta ) { |
|
| 157 | + $this->meta_data[ $array_key ] = clone $meta; |
|
| 158 | + if ( ! empty( $meta->id ) ) { |
|
| 159 | + $this->meta_data[ $array_key ]->id = null; |
|
| 160 | + } |
|
| 161 | + } |
|
| 162 | + } |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + /** |
|
| 166 | + * Get the data store. |
|
| 167 | + * |
|
| 168 | + * @since 1.0.19 |
|
| 169 | + * @return object |
|
| 170 | + */ |
|
| 171 | + public function get_data_store() { |
|
| 172 | + return $this->data_store; |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + /** |
|
| 176 | + * Get the object type. |
|
| 177 | + * |
|
| 178 | + * @since 1.0.19 |
|
| 179 | + * @return string |
|
| 180 | + */ |
|
| 181 | + public function get_object_type() { |
|
| 182 | + return $this->object_type; |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + /** |
|
| 186 | + * Returns the unique ID for this object. |
|
| 187 | + * |
|
| 188 | + * @since 1.0.19 |
|
| 189 | + * @return int |
|
| 190 | + */ |
|
| 191 | + public function get_id() { |
|
| 192 | + return $this->id; |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + /** |
|
| 196 | + * Get form status. |
|
| 197 | + * |
|
| 198 | + * @since 1.0.19 |
|
| 199 | + * @param string $context View or edit context. |
|
| 200 | + * @return string |
|
| 201 | + */ |
|
| 202 | + public function get_status( $context = 'view' ) { |
|
| 203 | + return $this->get_prop( 'status', $context ); |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + /** |
|
| 207 | + * Delete an object, set the ID to 0, and return result. |
|
| 208 | + * |
|
| 209 | + * @since 1.0.19 |
|
| 210 | + * @param bool $force_delete Should the data be deleted permanently. |
|
| 211 | + * @return bool result |
|
| 212 | + */ |
|
| 213 | + public function delete( $force_delete = false ) { |
|
| 214 | + if ( $this->data_store && $this->exists() ) { |
|
| 215 | + $this->data_store->delete( $this, array( 'force_delete' => $force_delete ) ); |
|
| 216 | + $this->set_id( 0 ); |
|
| 217 | + return true; |
|
| 218 | + } |
|
| 219 | + return false; |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + /** |
|
| 223 | + * Save should create or update based on object existence. |
|
| 224 | + * |
|
| 225 | + * @since 1.0.19 |
|
| 226 | + * @return int |
|
| 227 | + */ |
|
| 228 | + public function save() { |
|
| 229 | + if ( ! $this->data_store ) { |
|
| 230 | + return $this->get_id(); |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + /** |
|
| 234 | + * Trigger action before saving to the DB. Allows you to adjust object props before save. |
|
| 235 | + * |
|
| 236 | + * @param GetPaid_Data $this The object being saved. |
|
| 237 | + * @param GetPaid_Data_Store_WP $data_store The data store persisting the data. |
|
| 238 | + */ |
|
| 239 | + do_action( 'getpaid_before_' . $this->object_type . '_object_save', $this, $this->data_store ); |
|
| 240 | + |
|
| 241 | + if ( $this->get_id() ) { |
|
| 242 | + $this->data_store->update( $this ); |
|
| 243 | + } else { |
|
| 244 | + $this->data_store->create( $this ); |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + /** |
|
| 248 | + * Trigger action after saving to the DB. |
|
| 249 | + * |
|
| 250 | + * @param GetPaid_Data $this The object being saved. |
|
| 251 | + * @param GetPaid_Data_Store_WP $data_store The data store persisting the data. |
|
| 252 | + */ |
|
| 253 | + do_action( 'getpaid_after_' . $this->object_type . '_object_save', $this, $this->data_store ); |
|
| 254 | + |
|
| 255 | + return $this->get_id(); |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + /** |
|
| 259 | + * Change data to JSON format. |
|
| 260 | + * |
|
| 261 | + * @since 1.0.19 |
|
| 262 | + * @return string Data in JSON format. |
|
| 263 | + */ |
|
| 264 | + public function __toString() { |
|
| 265 | + return wp_json_encode( $this->get_data() ); |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + /** |
|
| 269 | + * Returns all data for this object. |
|
| 270 | + * |
|
| 271 | + * @since 1.0.19 |
|
| 272 | + * @return array |
|
| 273 | + */ |
|
| 274 | + public function get_data() { |
|
| 275 | + return array_merge( array( 'id' => $this->get_id() ), $this->data, array( 'meta_data' => $this->get_meta_data() ) ); |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + /** |
|
| 279 | + * Returns array of expected data keys for this object. |
|
| 280 | + * |
|
| 281 | + * @since 1.0.19 |
|
| 282 | + * @return array |
|
| 283 | + */ |
|
| 284 | + public function get_data_keys() { |
|
| 285 | + return array_keys( $this->data ); |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + /** |
|
| 289 | + * Returns all "extra" data keys for an object (for sub objects like item types). |
|
| 290 | + * |
|
| 291 | + * @since 1.0.19 |
|
| 292 | + * @return array |
|
| 293 | + */ |
|
| 294 | + public function get_extra_data_keys() { |
|
| 295 | + return array_keys( $this->extra_data ); |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + /** |
|
| 299 | + * Filter null meta values from array. |
|
| 300 | + * |
|
| 301 | + * @since 1.0.19 |
|
| 302 | + * @param mixed $meta Meta value to check. |
|
| 303 | + * @return bool |
|
| 304 | + */ |
|
| 305 | + protected function filter_null_meta( $meta ) { |
|
| 306 | + return ! is_null( $meta->value ); |
|
| 307 | + } |
|
| 308 | + |
|
| 309 | + /** |
|
| 310 | + * Get All Meta Data. |
|
| 311 | + * |
|
| 312 | + * @since 1.0.19 |
|
| 313 | + * @return array of objects. |
|
| 314 | + */ |
|
| 315 | + public function get_meta_data() { |
|
| 316 | + $this->maybe_read_meta_data(); |
|
| 317 | + return array_values( array_filter( $this->meta_data, array( $this, 'filter_null_meta' ) ) ); |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + /** |
|
| 321 | + * Check if the key is an internal one. |
|
| 322 | + * |
|
| 323 | + * @since 1.0.19 |
|
| 324 | + * @param string $key Key to check. |
|
| 325 | + * @return bool true if it's an internal key, false otherwise |
|
| 326 | + */ |
|
| 327 | + protected function is_internal_meta_key( $key ) { |
|
| 328 | + $internal_meta_key = ! empty( $key ) && $this->data_store && in_array( $key, $this->data_store->get_internal_meta_keys(), true ); |
|
| 329 | + |
|
| 330 | + if ( ! $internal_meta_key ) { |
|
| 331 | + return false; |
|
| 332 | + } |
|
| 333 | + |
|
| 334 | + $has_setter_or_getter = is_callable( array( $this, 'set_' . $key ) ) || is_callable( array( $this, 'get_' . $key ) ); |
|
| 335 | + |
|
| 336 | + if ( ! $has_setter_or_getter ) { |
|
| 337 | + return false; |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + /* translators: %s: $key Key to check */ |
|
| 341 | + getpaid_doing_it_wrong( __FUNCTION__, sprintf( __( 'Generic add/update/get meta methods should not be used for internal meta data, including "%s". Use getters and setters.', 'invoicing' ), $key ), '1.0.19' ); |
|
| 342 | + |
|
| 343 | + return true; |
|
| 344 | + } |
|
| 345 | + |
|
| 346 | + /** |
|
| 347 | + * Magic method for setting data fields. |
|
| 348 | + * |
|
| 349 | + * This method does not update custom fields in the database. |
|
| 350 | + * |
|
| 351 | + * @since 1.0.19 |
|
| 352 | + * @access public |
|
| 353 | + * |
|
| 354 | + */ |
|
| 355 | + public function __set( $key, $value ) { |
|
| 356 | + |
|
| 357 | + if ( 'id' == strtolower( $key ) ) { |
|
| 358 | + return $this->set_id( $value ); |
|
| 359 | + } |
|
| 360 | + |
|
| 361 | + if ( method_exists( $this, "set_$key") ) { |
|
| 362 | + |
|
| 363 | + /* translators: %s: $key Key to set */ |
|
| 364 | + getpaid_doing_it_wrong( __FUNCTION__, sprintf( __( 'Object data such as "%s" should not be accessed directly. Use getters and setters.', 'invoicing' ), $key ), '1.0.19' ); |
|
| 365 | + |
|
| 366 | + call_user_func( array( $this, "set_$key" ), $value ); |
|
| 367 | + } else { |
|
| 368 | + $this->set_prop( $key, $value ); |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + } |
|
| 372 | + |
|
| 373 | + /** |
|
| 374 | 374 | * Margic method for retrieving a property. |
| 375 | 375 | */ |
| 376 | 376 | public function __get( $key ) { |
@@ -378,10 +378,10 @@ discard block |
||
| 378 | 378 | // Check if we have a helper method for that. |
| 379 | 379 | if ( method_exists( $this, 'get_' . $key ) ) { |
| 380 | 380 | |
| 381 | - if ( 'post_type' != $key ) { |
|
| 382 | - /* translators: %s: $key Key to set */ |
|
| 383 | - getpaid_doing_it_wrong( __FUNCTION__, sprintf( __( 'Object data such as "%s" should not be accessed directly. Use getters and setters.', 'invoicing' ), $key ), '1.0.19' ); |
|
| 384 | - } |
|
| 381 | + if ( 'post_type' != $key ) { |
|
| 382 | + /* translators: %s: $key Key to set */ |
|
| 383 | + getpaid_doing_it_wrong( __FUNCTION__, sprintf( __( 'Object data such as "%s" should not be accessed directly. Use getters and setters.', 'invoicing' ), $key ), '1.0.19' ); |
|
| 384 | + } |
|
| 385 | 385 | |
| 386 | 386 | return call_user_func( array( $this, 'get_' . $key ) ); |
| 387 | 387 | } |
@@ -391,512 +391,512 @@ discard block |
||
| 391 | 391 | return $this->post->$key; |
| 392 | 392 | } |
| 393 | 393 | |
| 394 | - return $this->get_prop( $key ); |
|
| 395 | - |
|
| 396 | - } |
|
| 397 | - |
|
| 398 | - /** |
|
| 399 | - * Get Meta Data by Key. |
|
| 400 | - * |
|
| 401 | - * @since 1.0.19 |
|
| 402 | - * @param string $key Meta Key. |
|
| 403 | - * @param bool $single return first found meta with key, or all with $key. |
|
| 404 | - * @param string $context What the value is for. Valid values are view and edit. |
|
| 405 | - * @return mixed |
|
| 406 | - */ |
|
| 407 | - public function get_meta( $key = '', $single = true, $context = 'view' ) { |
|
| 408 | - |
|
| 409 | - // Check if this is an internal meta key. |
|
| 410 | - $_key = str_replace( '_wpinv', '', $key ); |
|
| 411 | - $_key = str_replace( 'wpinv', '', $_key ); |
|
| 412 | - if ( $this->is_internal_meta_key( $_key ) ) { |
|
| 413 | - $function = 'get_' . $_key; |
|
| 414 | - |
|
| 415 | - if ( is_callable( array( $this, $function ) ) ) { |
|
| 416 | - return $this->{$function}(); |
|
| 417 | - } |
|
| 418 | - } |
|
| 419 | - |
|
| 420 | - // Read the meta data if not yet read. |
|
| 421 | - $this->maybe_read_meta_data(); |
|
| 422 | - $meta_data = $this->get_meta_data(); |
|
| 423 | - $array_keys = array_keys( wp_list_pluck( $meta_data, 'key' ), $key, true ); |
|
| 424 | - $value = $single ? '' : array(); |
|
| 425 | - |
|
| 426 | - if ( ! empty( $array_keys ) ) { |
|
| 427 | - // We don't use the $this->meta_data property directly here because we don't want meta with a null value (i.e. meta which has been deleted via $this->delete_meta_data()). |
|
| 428 | - if ( $single ) { |
|
| 429 | - $value = $meta_data[ current( $array_keys ) ]->value; |
|
| 430 | - } else { |
|
| 431 | - $value = array_intersect_key( $meta_data, array_flip( $array_keys ) ); |
|
| 432 | - } |
|
| 433 | - } |
|
| 434 | - |
|
| 435 | - if ( 'view' === $context ) { |
|
| 436 | - $value = apply_filters( $this->get_hook_prefix() . $key, $value, $this ); |
|
| 437 | - } |
|
| 438 | - |
|
| 439 | - return $value; |
|
| 440 | - } |
|
| 441 | - |
|
| 442 | - /** |
|
| 443 | - * See if meta data exists, since get_meta always returns a '' or array(). |
|
| 444 | - * |
|
| 445 | - * @since 1.0.19 |
|
| 446 | - * @param string $key Meta Key. |
|
| 447 | - * @return boolean |
|
| 448 | - */ |
|
| 449 | - public function meta_exists( $key = '' ) { |
|
| 450 | - $this->maybe_read_meta_data(); |
|
| 451 | - $array_keys = wp_list_pluck( $this->get_meta_data(), 'key' ); |
|
| 452 | - return in_array( $key, $array_keys, true ); |
|
| 453 | - } |
|
| 454 | - |
|
| 455 | - /** |
|
| 456 | - * Set all meta data from array. |
|
| 457 | - * |
|
| 458 | - * @since 1.0.19 |
|
| 459 | - * @param array $data Key/Value pairs. |
|
| 460 | - */ |
|
| 461 | - public function set_meta_data( $data ) { |
|
| 462 | - if ( ! empty( $data ) && is_array( $data ) ) { |
|
| 463 | - $this->maybe_read_meta_data(); |
|
| 464 | - foreach ( $data as $meta ) { |
|
| 465 | - $meta = (array) $meta; |
|
| 466 | - if ( isset( $meta['key'], $meta['value'], $meta['id'] ) ) { |
|
| 467 | - $this->meta_data[] = new GetPaid_Meta_Data( |
|
| 468 | - array( |
|
| 469 | - 'id' => $meta['id'], |
|
| 470 | - 'key' => $meta['key'], |
|
| 471 | - 'value' => $meta['value'], |
|
| 472 | - ) |
|
| 473 | - ); |
|
| 474 | - } |
|
| 475 | - } |
|
| 476 | - } |
|
| 477 | - } |
|
| 478 | - |
|
| 479 | - /** |
|
| 480 | - * Add meta data. |
|
| 481 | - * |
|
| 482 | - * @since 1.0.19 |
|
| 483 | - * |
|
| 484 | - * @param string $key Meta key. |
|
| 485 | - * @param string|array $value Meta value. |
|
| 486 | - * @param bool $unique Should this be a unique key?. |
|
| 487 | - */ |
|
| 488 | - public function add_meta_data( $key, $value, $unique = false ) { |
|
| 489 | - if ( $this->is_internal_meta_key( $key ) ) { |
|
| 490 | - $function = 'set_' . $key; |
|
| 491 | - |
|
| 492 | - if ( is_callable( array( $this, $function ) ) ) { |
|
| 493 | - return $this->{$function}( $value ); |
|
| 494 | - } |
|
| 495 | - } |
|
| 496 | - |
|
| 497 | - $this->maybe_read_meta_data(); |
|
| 498 | - if ( $unique ) { |
|
| 499 | - $this->delete_meta_data( $key ); |
|
| 500 | - } |
|
| 501 | - $this->meta_data[] = new GetPaid_Meta_Data( |
|
| 502 | - array( |
|
| 503 | - 'key' => $key, |
|
| 504 | - 'value' => $value, |
|
| 505 | - ) |
|
| 506 | - ); |
|
| 507 | - } |
|
| 508 | - |
|
| 509 | - /** |
|
| 510 | - * Update meta data by key or ID, if provided. |
|
| 511 | - * |
|
| 512 | - * @since 1.0.19 |
|
| 513 | - * |
|
| 514 | - * @param string $key Meta key. |
|
| 515 | - * @param string|array $value Meta value. |
|
| 516 | - * @param int $meta_id Meta ID. |
|
| 517 | - */ |
|
| 518 | - public function update_meta_data( $key, $value, $meta_id = 0 ) { |
|
| 519 | - if ( $this->is_internal_meta_key( $key ) ) { |
|
| 520 | - $function = 'set_' . $key; |
|
| 521 | - |
|
| 522 | - if ( is_callable( array( $this, $function ) ) ) { |
|
| 523 | - return $this->{$function}( $value ); |
|
| 524 | - } |
|
| 525 | - } |
|
| 526 | - |
|
| 527 | - $this->maybe_read_meta_data(); |
|
| 528 | - |
|
| 529 | - $array_key = false; |
|
| 530 | - |
|
| 531 | - if ( $meta_id ) { |
|
| 532 | - $array_keys = array_keys( wp_list_pluck( $this->meta_data, 'id' ), $meta_id, true ); |
|
| 533 | - $array_key = $array_keys ? current( $array_keys ) : false; |
|
| 534 | - } else { |
|
| 535 | - // Find matches by key. |
|
| 536 | - $matches = array(); |
|
| 537 | - foreach ( $this->meta_data as $meta_data_array_key => $meta ) { |
|
| 538 | - if ( $meta->key === $key ) { |
|
| 539 | - $matches[] = $meta_data_array_key; |
|
| 540 | - } |
|
| 541 | - } |
|
| 542 | - |
|
| 543 | - if ( ! empty( $matches ) ) { |
|
| 544 | - // Set matches to null so only one key gets the new value. |
|
| 545 | - foreach ( $matches as $meta_data_array_key ) { |
|
| 546 | - $this->meta_data[ $meta_data_array_key ]->value = null; |
|
| 547 | - } |
|
| 548 | - $array_key = current( $matches ); |
|
| 549 | - } |
|
| 550 | - } |
|
| 551 | - |
|
| 552 | - if ( false !== $array_key ) { |
|
| 553 | - $meta = $this->meta_data[ $array_key ]; |
|
| 554 | - $meta->key = $key; |
|
| 555 | - $meta->value = $value; |
|
| 556 | - } else { |
|
| 557 | - $this->add_meta_data( $key, $value, true ); |
|
| 558 | - } |
|
| 559 | - } |
|
| 560 | - |
|
| 561 | - /** |
|
| 562 | - * Delete meta data. |
|
| 563 | - * |
|
| 564 | - * @since 1.0.19 |
|
| 565 | - * @param string $key Meta key. |
|
| 566 | - */ |
|
| 567 | - public function delete_meta_data( $key ) { |
|
| 568 | - $this->maybe_read_meta_data(); |
|
| 569 | - $array_keys = array_keys( wp_list_pluck( $this->meta_data, 'key' ), $key, true ); |
|
| 570 | - |
|
| 571 | - if ( $array_keys ) { |
|
| 572 | - foreach ( $array_keys as $array_key ) { |
|
| 573 | - $this->meta_data[ $array_key ]->value = null; |
|
| 574 | - } |
|
| 575 | - } |
|
| 576 | - } |
|
| 577 | - |
|
| 578 | - /** |
|
| 579 | - * Delete meta data. |
|
| 580 | - * |
|
| 581 | - * @since 1.0.19 |
|
| 582 | - * @param int $mid Meta ID. |
|
| 583 | - */ |
|
| 584 | - public function delete_meta_data_by_mid( $mid ) { |
|
| 585 | - $this->maybe_read_meta_data(); |
|
| 586 | - $array_keys = array_keys( wp_list_pluck( $this->meta_data, 'id' ), (int) $mid, true ); |
|
| 587 | - |
|
| 588 | - if ( $array_keys ) { |
|
| 589 | - foreach ( $array_keys as $array_key ) { |
|
| 590 | - $this->meta_data[ $array_key ]->value = null; |
|
| 591 | - } |
|
| 592 | - } |
|
| 593 | - } |
|
| 594 | - |
|
| 595 | - /** |
|
| 596 | - * Read meta data if null. |
|
| 597 | - * |
|
| 598 | - * @since 1.0.19 |
|
| 599 | - */ |
|
| 600 | - protected function maybe_read_meta_data() { |
|
| 601 | - if ( is_null( $this->meta_data ) ) { |
|
| 602 | - $this->read_meta_data(); |
|
| 603 | - } |
|
| 604 | - } |
|
| 605 | - |
|
| 606 | - /** |
|
| 607 | - * Read Meta Data from the database. Ignore any internal properties. |
|
| 608 | - * Uses it's own caches because get_metadata does not provide meta_ids. |
|
| 609 | - * |
|
| 610 | - * @since 1.0.19 |
|
| 611 | - * @param bool $force_read True to force a new DB read (and update cache). |
|
| 612 | - */ |
|
| 613 | - public function read_meta_data( $force_read = false ) { |
|
| 614 | - |
|
| 615 | - // Reset meta data. |
|
| 616 | - $this->meta_data = array(); |
|
| 617 | - |
|
| 618 | - // Maybe abort early. |
|
| 619 | - if ( ! $this->get_id() || ! $this->data_store ) { |
|
| 620 | - return; |
|
| 621 | - } |
|
| 622 | - |
|
| 623 | - // Only read from cache if the cache key is set. |
|
| 624 | - $cache_key = null; |
|
| 625 | - if ( ! $force_read && ! empty( $this->cache_group ) ) { |
|
| 626 | - $cache_key = GetPaid_Cache_Helper::get_cache_prefix( $this->cache_group ) . GetPaid_Cache_Helper::get_cache_prefix( 'object_' . $this->get_id() ) . 'object_meta_' . $this->get_id(); |
|
| 627 | - $raw_meta_data = wp_cache_get( $cache_key, $this->cache_group ); |
|
| 628 | - } |
|
| 629 | - |
|
| 630 | - // Should we force read? |
|
| 631 | - if ( empty( $raw_meta_data ) ) { |
|
| 632 | - $raw_meta_data = $this->data_store->read_meta( $this ); |
|
| 633 | - |
|
| 634 | - if ( ! empty( $cache_key ) ) { |
|
| 635 | - wp_cache_set( $cache_key, $raw_meta_data, $this->cache_group ); |
|
| 636 | - } |
|
| 637 | - |
|
| 638 | - } |
|
| 639 | - |
|
| 640 | - // Set meta data. |
|
| 641 | - if ( is_array( $raw_meta_data ) ) { |
|
| 642 | - |
|
| 643 | - foreach ( $raw_meta_data as $meta ) { |
|
| 644 | - $this->meta_data[] = new GetPaid_Meta_Data( |
|
| 645 | - array( |
|
| 646 | - 'id' => (int) $meta->meta_id, |
|
| 647 | - 'key' => $meta->meta_key, |
|
| 648 | - 'value' => maybe_unserialize( $meta->meta_value ), |
|
| 649 | - ) |
|
| 650 | - ); |
|
| 651 | - } |
|
| 652 | - |
|
| 653 | - } |
|
| 654 | - |
|
| 655 | - } |
|
| 656 | - |
|
| 657 | - /** |
|
| 658 | - * Update Meta Data in the database. |
|
| 659 | - * |
|
| 660 | - * @since 1.0.19 |
|
| 661 | - */ |
|
| 662 | - public function save_meta_data() { |
|
| 663 | - if ( ! $this->data_store || is_null( $this->meta_data ) ) { |
|
| 664 | - return; |
|
| 665 | - } |
|
| 666 | - foreach ( $this->meta_data as $array_key => $meta ) { |
|
| 667 | - if ( is_null( $meta->value ) ) { |
|
| 668 | - if ( ! empty( $meta->id ) ) { |
|
| 669 | - $this->data_store->delete_meta( $this, $meta ); |
|
| 670 | - unset( $this->meta_data[ $array_key ] ); |
|
| 671 | - } |
|
| 672 | - } elseif ( empty( $meta->id ) ) { |
|
| 673 | - $meta->id = $this->data_store->add_meta( $this, $meta ); |
|
| 674 | - $meta->apply_changes(); |
|
| 675 | - } else { |
|
| 676 | - if ( $meta->get_changes() ) { |
|
| 677 | - $this->data_store->update_meta( $this, $meta ); |
|
| 678 | - $meta->apply_changes(); |
|
| 679 | - } |
|
| 680 | - } |
|
| 681 | - } |
|
| 682 | - if ( ! empty( $this->cache_group ) ) { |
|
| 683 | - $cache_key = GetPaid_Cache_Helper::get_cache_prefix( $this->cache_group ) . GetPaid_Cache_Helper::get_cache_prefix( 'object_' . $this->get_id() ) . 'object_meta_' . $this->get_id(); |
|
| 684 | - wp_cache_delete( $cache_key, $this->cache_group ); |
|
| 685 | - } |
|
| 686 | - } |
|
| 687 | - |
|
| 688 | - /** |
|
| 689 | - * Set ID. |
|
| 690 | - * |
|
| 691 | - * @since 1.0.19 |
|
| 692 | - * @param int $id ID. |
|
| 693 | - */ |
|
| 694 | - public function set_id( $id ) { |
|
| 695 | - $this->id = absint( $id ); |
|
| 696 | - } |
|
| 697 | - |
|
| 698 | - /** |
|
| 699 | - * Sets item status. |
|
| 700 | - * |
|
| 701 | - * @since 1.0.19 |
|
| 702 | - * @param string $status New status. |
|
| 703 | - * @return array details of change. |
|
| 704 | - */ |
|
| 705 | - public function set_status( $status ) { |
|
| 394 | + return $this->get_prop( $key ); |
|
| 395 | + |
|
| 396 | + } |
|
| 397 | + |
|
| 398 | + /** |
|
| 399 | + * Get Meta Data by Key. |
|
| 400 | + * |
|
| 401 | + * @since 1.0.19 |
|
| 402 | + * @param string $key Meta Key. |
|
| 403 | + * @param bool $single return first found meta with key, or all with $key. |
|
| 404 | + * @param string $context What the value is for. Valid values are view and edit. |
|
| 405 | + * @return mixed |
|
| 406 | + */ |
|
| 407 | + public function get_meta( $key = '', $single = true, $context = 'view' ) { |
|
| 408 | + |
|
| 409 | + // Check if this is an internal meta key. |
|
| 410 | + $_key = str_replace( '_wpinv', '', $key ); |
|
| 411 | + $_key = str_replace( 'wpinv', '', $_key ); |
|
| 412 | + if ( $this->is_internal_meta_key( $_key ) ) { |
|
| 413 | + $function = 'get_' . $_key; |
|
| 414 | + |
|
| 415 | + if ( is_callable( array( $this, $function ) ) ) { |
|
| 416 | + return $this->{$function}(); |
|
| 417 | + } |
|
| 418 | + } |
|
| 419 | + |
|
| 420 | + // Read the meta data if not yet read. |
|
| 421 | + $this->maybe_read_meta_data(); |
|
| 422 | + $meta_data = $this->get_meta_data(); |
|
| 423 | + $array_keys = array_keys( wp_list_pluck( $meta_data, 'key' ), $key, true ); |
|
| 424 | + $value = $single ? '' : array(); |
|
| 425 | + |
|
| 426 | + if ( ! empty( $array_keys ) ) { |
|
| 427 | + // We don't use the $this->meta_data property directly here because we don't want meta with a null value (i.e. meta which has been deleted via $this->delete_meta_data()). |
|
| 428 | + if ( $single ) { |
|
| 429 | + $value = $meta_data[ current( $array_keys ) ]->value; |
|
| 430 | + } else { |
|
| 431 | + $value = array_intersect_key( $meta_data, array_flip( $array_keys ) ); |
|
| 432 | + } |
|
| 433 | + } |
|
| 434 | + |
|
| 435 | + if ( 'view' === $context ) { |
|
| 436 | + $value = apply_filters( $this->get_hook_prefix() . $key, $value, $this ); |
|
| 437 | + } |
|
| 438 | + |
|
| 439 | + return $value; |
|
| 440 | + } |
|
| 441 | + |
|
| 442 | + /** |
|
| 443 | + * See if meta data exists, since get_meta always returns a '' or array(). |
|
| 444 | + * |
|
| 445 | + * @since 1.0.19 |
|
| 446 | + * @param string $key Meta Key. |
|
| 447 | + * @return boolean |
|
| 448 | + */ |
|
| 449 | + public function meta_exists( $key = '' ) { |
|
| 450 | + $this->maybe_read_meta_data(); |
|
| 451 | + $array_keys = wp_list_pluck( $this->get_meta_data(), 'key' ); |
|
| 452 | + return in_array( $key, $array_keys, true ); |
|
| 453 | + } |
|
| 454 | + |
|
| 455 | + /** |
|
| 456 | + * Set all meta data from array. |
|
| 457 | + * |
|
| 458 | + * @since 1.0.19 |
|
| 459 | + * @param array $data Key/Value pairs. |
|
| 460 | + */ |
|
| 461 | + public function set_meta_data( $data ) { |
|
| 462 | + if ( ! empty( $data ) && is_array( $data ) ) { |
|
| 463 | + $this->maybe_read_meta_data(); |
|
| 464 | + foreach ( $data as $meta ) { |
|
| 465 | + $meta = (array) $meta; |
|
| 466 | + if ( isset( $meta['key'], $meta['value'], $meta['id'] ) ) { |
|
| 467 | + $this->meta_data[] = new GetPaid_Meta_Data( |
|
| 468 | + array( |
|
| 469 | + 'id' => $meta['id'], |
|
| 470 | + 'key' => $meta['key'], |
|
| 471 | + 'value' => $meta['value'], |
|
| 472 | + ) |
|
| 473 | + ); |
|
| 474 | + } |
|
| 475 | + } |
|
| 476 | + } |
|
| 477 | + } |
|
| 478 | + |
|
| 479 | + /** |
|
| 480 | + * Add meta data. |
|
| 481 | + * |
|
| 482 | + * @since 1.0.19 |
|
| 483 | + * |
|
| 484 | + * @param string $key Meta key. |
|
| 485 | + * @param string|array $value Meta value. |
|
| 486 | + * @param bool $unique Should this be a unique key?. |
|
| 487 | + */ |
|
| 488 | + public function add_meta_data( $key, $value, $unique = false ) { |
|
| 489 | + if ( $this->is_internal_meta_key( $key ) ) { |
|
| 490 | + $function = 'set_' . $key; |
|
| 491 | + |
|
| 492 | + if ( is_callable( array( $this, $function ) ) ) { |
|
| 493 | + return $this->{$function}( $value ); |
|
| 494 | + } |
|
| 495 | + } |
|
| 496 | + |
|
| 497 | + $this->maybe_read_meta_data(); |
|
| 498 | + if ( $unique ) { |
|
| 499 | + $this->delete_meta_data( $key ); |
|
| 500 | + } |
|
| 501 | + $this->meta_data[] = new GetPaid_Meta_Data( |
|
| 502 | + array( |
|
| 503 | + 'key' => $key, |
|
| 504 | + 'value' => $value, |
|
| 505 | + ) |
|
| 506 | + ); |
|
| 507 | + } |
|
| 508 | + |
|
| 509 | + /** |
|
| 510 | + * Update meta data by key or ID, if provided. |
|
| 511 | + * |
|
| 512 | + * @since 1.0.19 |
|
| 513 | + * |
|
| 514 | + * @param string $key Meta key. |
|
| 515 | + * @param string|array $value Meta value. |
|
| 516 | + * @param int $meta_id Meta ID. |
|
| 517 | + */ |
|
| 518 | + public function update_meta_data( $key, $value, $meta_id = 0 ) { |
|
| 519 | + if ( $this->is_internal_meta_key( $key ) ) { |
|
| 520 | + $function = 'set_' . $key; |
|
| 521 | + |
|
| 522 | + if ( is_callable( array( $this, $function ) ) ) { |
|
| 523 | + return $this->{$function}( $value ); |
|
| 524 | + } |
|
| 525 | + } |
|
| 526 | + |
|
| 527 | + $this->maybe_read_meta_data(); |
|
| 528 | + |
|
| 529 | + $array_key = false; |
|
| 530 | + |
|
| 531 | + if ( $meta_id ) { |
|
| 532 | + $array_keys = array_keys( wp_list_pluck( $this->meta_data, 'id' ), $meta_id, true ); |
|
| 533 | + $array_key = $array_keys ? current( $array_keys ) : false; |
|
| 534 | + } else { |
|
| 535 | + // Find matches by key. |
|
| 536 | + $matches = array(); |
|
| 537 | + foreach ( $this->meta_data as $meta_data_array_key => $meta ) { |
|
| 538 | + if ( $meta->key === $key ) { |
|
| 539 | + $matches[] = $meta_data_array_key; |
|
| 540 | + } |
|
| 541 | + } |
|
| 542 | + |
|
| 543 | + if ( ! empty( $matches ) ) { |
|
| 544 | + // Set matches to null so only one key gets the new value. |
|
| 545 | + foreach ( $matches as $meta_data_array_key ) { |
|
| 546 | + $this->meta_data[ $meta_data_array_key ]->value = null; |
|
| 547 | + } |
|
| 548 | + $array_key = current( $matches ); |
|
| 549 | + } |
|
| 550 | + } |
|
| 551 | + |
|
| 552 | + if ( false !== $array_key ) { |
|
| 553 | + $meta = $this->meta_data[ $array_key ]; |
|
| 554 | + $meta->key = $key; |
|
| 555 | + $meta->value = $value; |
|
| 556 | + } else { |
|
| 557 | + $this->add_meta_data( $key, $value, true ); |
|
| 558 | + } |
|
| 559 | + } |
|
| 560 | + |
|
| 561 | + /** |
|
| 562 | + * Delete meta data. |
|
| 563 | + * |
|
| 564 | + * @since 1.0.19 |
|
| 565 | + * @param string $key Meta key. |
|
| 566 | + */ |
|
| 567 | + public function delete_meta_data( $key ) { |
|
| 568 | + $this->maybe_read_meta_data(); |
|
| 569 | + $array_keys = array_keys( wp_list_pluck( $this->meta_data, 'key' ), $key, true ); |
|
| 570 | + |
|
| 571 | + if ( $array_keys ) { |
|
| 572 | + foreach ( $array_keys as $array_key ) { |
|
| 573 | + $this->meta_data[ $array_key ]->value = null; |
|
| 574 | + } |
|
| 575 | + } |
|
| 576 | + } |
|
| 577 | + |
|
| 578 | + /** |
|
| 579 | + * Delete meta data. |
|
| 580 | + * |
|
| 581 | + * @since 1.0.19 |
|
| 582 | + * @param int $mid Meta ID. |
|
| 583 | + */ |
|
| 584 | + public function delete_meta_data_by_mid( $mid ) { |
|
| 585 | + $this->maybe_read_meta_data(); |
|
| 586 | + $array_keys = array_keys( wp_list_pluck( $this->meta_data, 'id' ), (int) $mid, true ); |
|
| 587 | + |
|
| 588 | + if ( $array_keys ) { |
|
| 589 | + foreach ( $array_keys as $array_key ) { |
|
| 590 | + $this->meta_data[ $array_key ]->value = null; |
|
| 591 | + } |
|
| 592 | + } |
|
| 593 | + } |
|
| 594 | + |
|
| 595 | + /** |
|
| 596 | + * Read meta data if null. |
|
| 597 | + * |
|
| 598 | + * @since 1.0.19 |
|
| 599 | + */ |
|
| 600 | + protected function maybe_read_meta_data() { |
|
| 601 | + if ( is_null( $this->meta_data ) ) { |
|
| 602 | + $this->read_meta_data(); |
|
| 603 | + } |
|
| 604 | + } |
|
| 605 | + |
|
| 606 | + /** |
|
| 607 | + * Read Meta Data from the database. Ignore any internal properties. |
|
| 608 | + * Uses it's own caches because get_metadata does not provide meta_ids. |
|
| 609 | + * |
|
| 610 | + * @since 1.0.19 |
|
| 611 | + * @param bool $force_read True to force a new DB read (and update cache). |
|
| 612 | + */ |
|
| 613 | + public function read_meta_data( $force_read = false ) { |
|
| 614 | + |
|
| 615 | + // Reset meta data. |
|
| 616 | + $this->meta_data = array(); |
|
| 617 | + |
|
| 618 | + // Maybe abort early. |
|
| 619 | + if ( ! $this->get_id() || ! $this->data_store ) { |
|
| 620 | + return; |
|
| 621 | + } |
|
| 622 | + |
|
| 623 | + // Only read from cache if the cache key is set. |
|
| 624 | + $cache_key = null; |
|
| 625 | + if ( ! $force_read && ! empty( $this->cache_group ) ) { |
|
| 626 | + $cache_key = GetPaid_Cache_Helper::get_cache_prefix( $this->cache_group ) . GetPaid_Cache_Helper::get_cache_prefix( 'object_' . $this->get_id() ) . 'object_meta_' . $this->get_id(); |
|
| 627 | + $raw_meta_data = wp_cache_get( $cache_key, $this->cache_group ); |
|
| 628 | + } |
|
| 629 | + |
|
| 630 | + // Should we force read? |
|
| 631 | + if ( empty( $raw_meta_data ) ) { |
|
| 632 | + $raw_meta_data = $this->data_store->read_meta( $this ); |
|
| 633 | + |
|
| 634 | + if ( ! empty( $cache_key ) ) { |
|
| 635 | + wp_cache_set( $cache_key, $raw_meta_data, $this->cache_group ); |
|
| 636 | + } |
|
| 637 | + |
|
| 638 | + } |
|
| 639 | + |
|
| 640 | + // Set meta data. |
|
| 641 | + if ( is_array( $raw_meta_data ) ) { |
|
| 642 | + |
|
| 643 | + foreach ( $raw_meta_data as $meta ) { |
|
| 644 | + $this->meta_data[] = new GetPaid_Meta_Data( |
|
| 645 | + array( |
|
| 646 | + 'id' => (int) $meta->meta_id, |
|
| 647 | + 'key' => $meta->meta_key, |
|
| 648 | + 'value' => maybe_unserialize( $meta->meta_value ), |
|
| 649 | + ) |
|
| 650 | + ); |
|
| 651 | + } |
|
| 652 | + |
|
| 653 | + } |
|
| 654 | + |
|
| 655 | + } |
|
| 656 | + |
|
| 657 | + /** |
|
| 658 | + * Update Meta Data in the database. |
|
| 659 | + * |
|
| 660 | + * @since 1.0.19 |
|
| 661 | + */ |
|
| 662 | + public function save_meta_data() { |
|
| 663 | + if ( ! $this->data_store || is_null( $this->meta_data ) ) { |
|
| 664 | + return; |
|
| 665 | + } |
|
| 666 | + foreach ( $this->meta_data as $array_key => $meta ) { |
|
| 667 | + if ( is_null( $meta->value ) ) { |
|
| 668 | + if ( ! empty( $meta->id ) ) { |
|
| 669 | + $this->data_store->delete_meta( $this, $meta ); |
|
| 670 | + unset( $this->meta_data[ $array_key ] ); |
|
| 671 | + } |
|
| 672 | + } elseif ( empty( $meta->id ) ) { |
|
| 673 | + $meta->id = $this->data_store->add_meta( $this, $meta ); |
|
| 674 | + $meta->apply_changes(); |
|
| 675 | + } else { |
|
| 676 | + if ( $meta->get_changes() ) { |
|
| 677 | + $this->data_store->update_meta( $this, $meta ); |
|
| 678 | + $meta->apply_changes(); |
|
| 679 | + } |
|
| 680 | + } |
|
| 681 | + } |
|
| 682 | + if ( ! empty( $this->cache_group ) ) { |
|
| 683 | + $cache_key = GetPaid_Cache_Helper::get_cache_prefix( $this->cache_group ) . GetPaid_Cache_Helper::get_cache_prefix( 'object_' . $this->get_id() ) . 'object_meta_' . $this->get_id(); |
|
| 684 | + wp_cache_delete( $cache_key, $this->cache_group ); |
|
| 685 | + } |
|
| 686 | + } |
|
| 687 | + |
|
| 688 | + /** |
|
| 689 | + * Set ID. |
|
| 690 | + * |
|
| 691 | + * @since 1.0.19 |
|
| 692 | + * @param int $id ID. |
|
| 693 | + */ |
|
| 694 | + public function set_id( $id ) { |
|
| 695 | + $this->id = absint( $id ); |
|
| 696 | + } |
|
| 697 | + |
|
| 698 | + /** |
|
| 699 | + * Sets item status. |
|
| 700 | + * |
|
| 701 | + * @since 1.0.19 |
|
| 702 | + * @param string $status New status. |
|
| 703 | + * @return array details of change. |
|
| 704 | + */ |
|
| 705 | + public function set_status( $status ) { |
|
| 706 | 706 | $old_status = $this->get_status(); |
| 707 | 707 | |
| 708 | - $this->set_prop( 'status', $status ); |
|
| 709 | - |
|
| 710 | - return array( |
|
| 711 | - 'from' => $old_status, |
|
| 712 | - 'to' => $status, |
|
| 713 | - ); |
|
| 714 | - } |
|
| 715 | - |
|
| 716 | - /** |
|
| 717 | - * Set all props to default values. |
|
| 718 | - * |
|
| 719 | - * @since 1.0.19 |
|
| 720 | - */ |
|
| 721 | - public function set_defaults() { |
|
| 722 | - $this->data = $this->default_data; |
|
| 723 | - $this->changes = array(); |
|
| 724 | - $this->set_object_read( false ); |
|
| 725 | - } |
|
| 726 | - |
|
| 727 | - /** |
|
| 728 | - * Set object read property. |
|
| 729 | - * |
|
| 730 | - * @since 1.0.19 |
|
| 731 | - * @param boolean $read Should read?. |
|
| 732 | - */ |
|
| 733 | - public function set_object_read( $read = true ) { |
|
| 734 | - $this->object_read = (bool) $read; |
|
| 735 | - } |
|
| 736 | - |
|
| 737 | - /** |
|
| 738 | - * Get object read property. |
|
| 739 | - * |
|
| 740 | - * @since 1.0.19 |
|
| 741 | - * @return boolean |
|
| 742 | - */ |
|
| 743 | - public function get_object_read() { |
|
| 744 | - return (bool) $this->object_read; |
|
| 745 | - } |
|
| 746 | - |
|
| 747 | - /** |
|
| 748 | - * Set a collection of props in one go, collect any errors, and return the result. |
|
| 749 | - * Only sets using public methods. |
|
| 750 | - * |
|
| 751 | - * @since 1.0.19 |
|
| 752 | - * |
|
| 753 | - * @param array $props Key value pairs to set. Key is the prop and should map to a setter function name. |
|
| 754 | - * @param string $context In what context to run this. |
|
| 755 | - * |
|
| 756 | - * @return bool|WP_Error |
|
| 757 | - */ |
|
| 758 | - public function set_props( $props, $context = 'set' ) { |
|
| 759 | - $errors = false; |
|
| 760 | - |
|
| 761 | - foreach ( $props as $prop => $value ) { |
|
| 762 | - try { |
|
| 763 | - /** |
|
| 764 | - * Checks if the prop being set is allowed, and the value is not null. |
|
| 765 | - */ |
|
| 766 | - if ( is_null( $value ) || in_array( $prop, array( 'prop', 'date_prop', 'meta_data' ), true ) ) { |
|
| 767 | - continue; |
|
| 768 | - } |
|
| 769 | - $setter = "set_$prop"; |
|
| 770 | - |
|
| 771 | - if ( is_callable( array( $this, $setter ) ) ) { |
|
| 772 | - $this->{$setter}( $value ); |
|
| 773 | - } |
|
| 774 | - } catch ( Exception $e ) { |
|
| 775 | - if ( ! $errors ) { |
|
| 776 | - $errors = new WP_Error(); |
|
| 777 | - } |
|
| 778 | - $errors->add( $e->getCode(), $e->getMessage() ); |
|
| 779 | - $this->last_error = $e->getMessage(); |
|
| 780 | - } |
|
| 781 | - } |
|
| 782 | - |
|
| 783 | - return $errors && count( $errors->get_error_codes() ) ? $errors : true; |
|
| 784 | - } |
|
| 785 | - |
|
| 786 | - /** |
|
| 787 | - * Sets a prop for a setter method. |
|
| 788 | - * |
|
| 789 | - * This stores changes in a special array so we can track what needs saving |
|
| 790 | - * the the DB later. |
|
| 791 | - * |
|
| 792 | - * @since 1.0.19 |
|
| 793 | - * @param string $prop Name of prop to set. |
|
| 794 | - * @param mixed $value Value of the prop. |
|
| 795 | - */ |
|
| 796 | - protected function set_prop( $prop, $value ) { |
|
| 797 | - if ( array_key_exists( $prop, $this->data ) ) { |
|
| 798 | - if ( true === $this->object_read ) { |
|
| 799 | - if ( $value !== $this->data[ $prop ] || array_key_exists( $prop, $this->changes ) ) { |
|
| 800 | - $this->changes[ $prop ] = $value; |
|
| 801 | - } |
|
| 802 | - } else { |
|
| 803 | - $this->data[ $prop ] = $value; |
|
| 804 | - } |
|
| 805 | - } |
|
| 806 | - } |
|
| 807 | - |
|
| 808 | - /** |
|
| 809 | - * Return data changes only. |
|
| 810 | - * |
|
| 811 | - * @since 1.0.19 |
|
| 812 | - * @return array |
|
| 813 | - */ |
|
| 814 | - public function get_changes() { |
|
| 815 | - return $this->changes; |
|
| 816 | - } |
|
| 817 | - |
|
| 818 | - /** |
|
| 819 | - * Merge changes with data and clear. |
|
| 820 | - * |
|
| 821 | - * @since 1.0.19 |
|
| 822 | - */ |
|
| 823 | - public function apply_changes() { |
|
| 824 | - $this->data = array_replace_recursive( $this->data, $this->changes ); |
|
| 825 | - $this->changes = array(); |
|
| 826 | - } |
|
| 827 | - |
|
| 828 | - /** |
|
| 829 | - * Prefix for action and filter hooks on data. |
|
| 830 | - * |
|
| 831 | - * @since 1.0.19 |
|
| 832 | - * @return string |
|
| 833 | - */ |
|
| 834 | - protected function get_hook_prefix() { |
|
| 835 | - return 'wpinv_get_' . $this->object_type . '_'; |
|
| 836 | - } |
|
| 837 | - |
|
| 838 | - /** |
|
| 839 | - * Gets a prop for a getter method. |
|
| 840 | - * |
|
| 841 | - * Gets the value from either current pending changes, or the data itself. |
|
| 842 | - * Context controls what happens to the value before it's returned. |
|
| 843 | - * |
|
| 844 | - * @since 1.0.19 |
|
| 845 | - * @param string $prop Name of prop to get. |
|
| 846 | - * @param string $context What the value is for. Valid values are view and edit. |
|
| 847 | - * @return mixed |
|
| 848 | - */ |
|
| 849 | - protected function get_prop( $prop, $context = 'view' ) { |
|
| 850 | - $value = null; |
|
| 851 | - |
|
| 852 | - if ( array_key_exists( $prop, $this->data ) ) { |
|
| 853 | - $value = array_key_exists( $prop, $this->changes ) ? $this->changes[ $prop ] : $this->data[ $prop ]; |
|
| 854 | - |
|
| 855 | - if ( 'view' === $context ) { |
|
| 856 | - $value = apply_filters( $this->get_hook_prefix() . $prop, $value, $this ); |
|
| 857 | - } |
|
| 858 | - } |
|
| 859 | - |
|
| 860 | - return $value; |
|
| 861 | - } |
|
| 862 | - |
|
| 863 | - /** |
|
| 864 | - * Sets a date prop whilst handling formatting and datetime objects. |
|
| 865 | - * |
|
| 866 | - * @since 1.0.19 |
|
| 867 | - * @param string $prop Name of prop to set. |
|
| 868 | - * @param string|integer $value Value of the prop. |
|
| 869 | - */ |
|
| 870 | - protected function set_date_prop( $prop, $value ) { |
|
| 871 | - |
|
| 872 | - if ( empty( $value ) ) { |
|
| 873 | - $this->set_prop( $prop, null ); |
|
| 874 | - return; |
|
| 875 | - } |
|
| 876 | - $this->set_prop( $prop, $value ); |
|
| 877 | - |
|
| 878 | - } |
|
| 879 | - |
|
| 880 | - /** |
|
| 881 | - * When invalid data is found, throw an exception unless reading from the DB. |
|
| 882 | - * |
|
| 883 | - * @since 1.0.19 |
|
| 884 | - * @param string $code Error code. |
|
| 885 | - * @param string $message Error message. |
|
| 886 | - */ |
|
| 887 | - protected function error( $code, $message ) { |
|
| 888 | - $this->last_error = $message; |
|
| 889 | - } |
|
| 890 | - |
|
| 891 | - /** |
|
| 892 | - * Checks if the object is saved in the database |
|
| 893 | - * |
|
| 894 | - * @since 1.0.19 |
|
| 895 | - * @return bool |
|
| 896 | - */ |
|
| 897 | - public function exists() { |
|
| 898 | - $id = $this->get_id(); |
|
| 899 | - return ! empty( $id ); |
|
| 900 | - } |
|
| 708 | + $this->set_prop( 'status', $status ); |
|
| 709 | + |
|
| 710 | + return array( |
|
| 711 | + 'from' => $old_status, |
|
| 712 | + 'to' => $status, |
|
| 713 | + ); |
|
| 714 | + } |
|
| 715 | + |
|
| 716 | + /** |
|
| 717 | + * Set all props to default values. |
|
| 718 | + * |
|
| 719 | + * @since 1.0.19 |
|
| 720 | + */ |
|
| 721 | + public function set_defaults() { |
|
| 722 | + $this->data = $this->default_data; |
|
| 723 | + $this->changes = array(); |
|
| 724 | + $this->set_object_read( false ); |
|
| 725 | + } |
|
| 726 | + |
|
| 727 | + /** |
|
| 728 | + * Set object read property. |
|
| 729 | + * |
|
| 730 | + * @since 1.0.19 |
|
| 731 | + * @param boolean $read Should read?. |
|
| 732 | + */ |
|
| 733 | + public function set_object_read( $read = true ) { |
|
| 734 | + $this->object_read = (bool) $read; |
|
| 735 | + } |
|
| 736 | + |
|
| 737 | + /** |
|
| 738 | + * Get object read property. |
|
| 739 | + * |
|
| 740 | + * @since 1.0.19 |
|
| 741 | + * @return boolean |
|
| 742 | + */ |
|
| 743 | + public function get_object_read() { |
|
| 744 | + return (bool) $this->object_read; |
|
| 745 | + } |
|
| 746 | + |
|
| 747 | + /** |
|
| 748 | + * Set a collection of props in one go, collect any errors, and return the result. |
|
| 749 | + * Only sets using public methods. |
|
| 750 | + * |
|
| 751 | + * @since 1.0.19 |
|
| 752 | + * |
|
| 753 | + * @param array $props Key value pairs to set. Key is the prop and should map to a setter function name. |
|
| 754 | + * @param string $context In what context to run this. |
|
| 755 | + * |
|
| 756 | + * @return bool|WP_Error |
|
| 757 | + */ |
|
| 758 | + public function set_props( $props, $context = 'set' ) { |
|
| 759 | + $errors = false; |
|
| 760 | + |
|
| 761 | + foreach ( $props as $prop => $value ) { |
|
| 762 | + try { |
|
| 763 | + /** |
|
| 764 | + * Checks if the prop being set is allowed, and the value is not null. |
|
| 765 | + */ |
|
| 766 | + if ( is_null( $value ) || in_array( $prop, array( 'prop', 'date_prop', 'meta_data' ), true ) ) { |
|
| 767 | + continue; |
|
| 768 | + } |
|
| 769 | + $setter = "set_$prop"; |
|
| 770 | + |
|
| 771 | + if ( is_callable( array( $this, $setter ) ) ) { |
|
| 772 | + $this->{$setter}( $value ); |
|
| 773 | + } |
|
| 774 | + } catch ( Exception $e ) { |
|
| 775 | + if ( ! $errors ) { |
|
| 776 | + $errors = new WP_Error(); |
|
| 777 | + } |
|
| 778 | + $errors->add( $e->getCode(), $e->getMessage() ); |
|
| 779 | + $this->last_error = $e->getMessage(); |
|
| 780 | + } |
|
| 781 | + } |
|
| 782 | + |
|
| 783 | + return $errors && count( $errors->get_error_codes() ) ? $errors : true; |
|
| 784 | + } |
|
| 785 | + |
|
| 786 | + /** |
|
| 787 | + * Sets a prop for a setter method. |
|
| 788 | + * |
|
| 789 | + * This stores changes in a special array so we can track what needs saving |
|
| 790 | + * the the DB later. |
|
| 791 | + * |
|
| 792 | + * @since 1.0.19 |
|
| 793 | + * @param string $prop Name of prop to set. |
|
| 794 | + * @param mixed $value Value of the prop. |
|
| 795 | + */ |
|
| 796 | + protected function set_prop( $prop, $value ) { |
|
| 797 | + if ( array_key_exists( $prop, $this->data ) ) { |
|
| 798 | + if ( true === $this->object_read ) { |
|
| 799 | + if ( $value !== $this->data[ $prop ] || array_key_exists( $prop, $this->changes ) ) { |
|
| 800 | + $this->changes[ $prop ] = $value; |
|
| 801 | + } |
|
| 802 | + } else { |
|
| 803 | + $this->data[ $prop ] = $value; |
|
| 804 | + } |
|
| 805 | + } |
|
| 806 | + } |
|
| 807 | + |
|
| 808 | + /** |
|
| 809 | + * Return data changes only. |
|
| 810 | + * |
|
| 811 | + * @since 1.0.19 |
|
| 812 | + * @return array |
|
| 813 | + */ |
|
| 814 | + public function get_changes() { |
|
| 815 | + return $this->changes; |
|
| 816 | + } |
|
| 817 | + |
|
| 818 | + /** |
|
| 819 | + * Merge changes with data and clear. |
|
| 820 | + * |
|
| 821 | + * @since 1.0.19 |
|
| 822 | + */ |
|
| 823 | + public function apply_changes() { |
|
| 824 | + $this->data = array_replace_recursive( $this->data, $this->changes ); |
|
| 825 | + $this->changes = array(); |
|
| 826 | + } |
|
| 827 | + |
|
| 828 | + /** |
|
| 829 | + * Prefix for action and filter hooks on data. |
|
| 830 | + * |
|
| 831 | + * @since 1.0.19 |
|
| 832 | + * @return string |
|
| 833 | + */ |
|
| 834 | + protected function get_hook_prefix() { |
|
| 835 | + return 'wpinv_get_' . $this->object_type . '_'; |
|
| 836 | + } |
|
| 837 | + |
|
| 838 | + /** |
|
| 839 | + * Gets a prop for a getter method. |
|
| 840 | + * |
|
| 841 | + * Gets the value from either current pending changes, or the data itself. |
|
| 842 | + * Context controls what happens to the value before it's returned. |
|
| 843 | + * |
|
| 844 | + * @since 1.0.19 |
|
| 845 | + * @param string $prop Name of prop to get. |
|
| 846 | + * @param string $context What the value is for. Valid values are view and edit. |
|
| 847 | + * @return mixed |
|
| 848 | + */ |
|
| 849 | + protected function get_prop( $prop, $context = 'view' ) { |
|
| 850 | + $value = null; |
|
| 851 | + |
|
| 852 | + if ( array_key_exists( $prop, $this->data ) ) { |
|
| 853 | + $value = array_key_exists( $prop, $this->changes ) ? $this->changes[ $prop ] : $this->data[ $prop ]; |
|
| 854 | + |
|
| 855 | + if ( 'view' === $context ) { |
|
| 856 | + $value = apply_filters( $this->get_hook_prefix() . $prop, $value, $this ); |
|
| 857 | + } |
|
| 858 | + } |
|
| 859 | + |
|
| 860 | + return $value; |
|
| 861 | + } |
|
| 862 | + |
|
| 863 | + /** |
|
| 864 | + * Sets a date prop whilst handling formatting and datetime objects. |
|
| 865 | + * |
|
| 866 | + * @since 1.0.19 |
|
| 867 | + * @param string $prop Name of prop to set. |
|
| 868 | + * @param string|integer $value Value of the prop. |
|
| 869 | + */ |
|
| 870 | + protected function set_date_prop( $prop, $value ) { |
|
| 871 | + |
|
| 872 | + if ( empty( $value ) ) { |
|
| 873 | + $this->set_prop( $prop, null ); |
|
| 874 | + return; |
|
| 875 | + } |
|
| 876 | + $this->set_prop( $prop, $value ); |
|
| 877 | + |
|
| 878 | + } |
|
| 879 | + |
|
| 880 | + /** |
|
| 881 | + * When invalid data is found, throw an exception unless reading from the DB. |
|
| 882 | + * |
|
| 883 | + * @since 1.0.19 |
|
| 884 | + * @param string $code Error code. |
|
| 885 | + * @param string $message Error message. |
|
| 886 | + */ |
|
| 887 | + protected function error( $code, $message ) { |
|
| 888 | + $this->last_error = $message; |
|
| 889 | + } |
|
| 890 | + |
|
| 891 | + /** |
|
| 892 | + * Checks if the object is saved in the database |
|
| 893 | + * |
|
| 894 | + * @since 1.0.19 |
|
| 895 | + * @return bool |
|
| 896 | + */ |
|
| 897 | + public function exists() { |
|
| 898 | + $id = $this->get_id(); |
|
| 899 | + return ! empty( $id ); |
|
| 900 | + } |
|
| 901 | 901 | |
| 902 | 902 | } |
@@ -5,7 +5,7 @@ discard block |
||
| 5 | 5 | * |
| 6 | 6 | */ |
| 7 | 7 | if ( ! defined( 'ABSPATH' ) ) { |
| 8 | - exit; |
|
| 8 | + exit; |
|
| 9 | 9 | } |
| 10 | 10 | |
| 11 | 11 | /** |
@@ -15,539 +15,539 @@ discard block |
||
| 15 | 15 | */ |
| 16 | 16 | class GetPaid_Invoice_Data_Store extends GetPaid_Data_Store_WP { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * Data stored in meta keys, but not considered "meta" for a discount. |
|
| 20 | - * |
|
| 21 | - * @since 1.0.19 |
|
| 22 | - * @var array |
|
| 23 | - */ |
|
| 24 | - protected $internal_meta_keys = array( |
|
| 25 | - '_wpinv_subscr_profile_id', |
|
| 26 | - '_wpinv_subscription_id', |
|
| 27 | - '_wpinv_taxes', |
|
| 28 | - '_wpinv_fees', |
|
| 29 | - '_wpinv_discounts', |
|
| 30 | - '_wpinv_submission_id', |
|
| 31 | - '_wpinv_payment_form', |
|
| 32 | - '_wpinv_is_viewed', |
|
| 33 | - '_wpinv_phone', |
|
| 34 | - 'wpinv_email_cc', |
|
| 35 | - 'wpinv_template', |
|
| 36 | - 'wpinv_created_via' |
|
| 37 | - ); |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * A map of meta keys to data props. |
|
| 41 | - * |
|
| 42 | - * @since 1.0.19 |
|
| 43 | - * |
|
| 44 | - * @var array |
|
| 45 | - */ |
|
| 46 | - protected $meta_key_to_props = array( |
|
| 47 | - '_wpinv_subscr_profile_id' => 'remote_subscription_id', |
|
| 48 | - '_wpinv_subscription_id' => 'subscription_id', |
|
| 49 | - '_wpinv_taxes' => 'taxes', |
|
| 50 | - '_wpinv_fees' => 'fees', |
|
| 51 | - '_wpinv_discounts' => 'discounts', |
|
| 52 | - '_wpinv_submission_id' => 'submission_id', |
|
| 53 | - '_wpinv_payment_form' => 'payment_form', |
|
| 54 | - '_wpinv_is_viewed' => 'is_viewed', |
|
| 55 | - 'wpinv_email_cc' => 'email_cc', |
|
| 56 | - 'wpinv_template' => 'template', |
|
| 57 | - 'wpinv_created_via' => 'created_via', |
|
| 58 | - '_wpinv_phone' => 'phone', |
|
| 59 | - ); |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * A map of database fields to data props. |
|
| 63 | - * |
|
| 64 | - * @since 1.0.19 |
|
| 65 | - * |
|
| 66 | - * @var array |
|
| 67 | - */ |
|
| 68 | - protected $database_fields_to_props = array( |
|
| 69 | - 'post_id' => 'id', |
|
| 70 | - 'number' => 'number', |
|
| 71 | - 'currency' => 'currency', |
|
| 72 | - 'key' => 'key', |
|
| 73 | - 'type' => 'type', |
|
| 74 | - 'mode' => 'mode', |
|
| 75 | - 'user_ip' => 'user_ip', |
|
| 76 | - 'first_name' => 'first_name', |
|
| 77 | - 'last_name' => 'last_name', |
|
| 78 | - 'address' => 'address', |
|
| 79 | - 'city' => 'city', |
|
| 80 | - 'state' => 'state', |
|
| 81 | - 'country' => 'country', |
|
| 82 | - 'zip' => 'zip', |
|
| 83 | - 'zip' => 'zip', |
|
| 84 | - 'adddress_confirmed' => 'address_confirmed', |
|
| 85 | - 'gateway' => 'gateway', |
|
| 86 | - 'transaction_id' => 'transaction_id', |
|
| 87 | - 'currency' => 'currency', |
|
| 88 | - 'subtotal' => 'subtotal', |
|
| 89 | - 'tax' => 'total_tax', |
|
| 90 | - 'fees_total' => 'total_fees', |
|
| 91 | - 'discount' => 'total_discount', |
|
| 92 | - 'total' => 'total', |
|
| 93 | - 'discount_code' => 'discount_code', |
|
| 94 | - 'disable_taxes' => 'disable_taxes', |
|
| 95 | - 'due_date' => 'due_date', |
|
| 96 | - 'completed_date' => 'completed_date', |
|
| 97 | - 'company' => 'company', |
|
| 98 | - 'vat_number' => 'vat_number', |
|
| 99 | - 'vat_rate' => 'vat_rate', |
|
| 100 | - ); |
|
| 101 | - |
|
| 102 | - /* |
|
| 18 | + /** |
|
| 19 | + * Data stored in meta keys, but not considered "meta" for a discount. |
|
| 20 | + * |
|
| 21 | + * @since 1.0.19 |
|
| 22 | + * @var array |
|
| 23 | + */ |
|
| 24 | + protected $internal_meta_keys = array( |
|
| 25 | + '_wpinv_subscr_profile_id', |
|
| 26 | + '_wpinv_subscription_id', |
|
| 27 | + '_wpinv_taxes', |
|
| 28 | + '_wpinv_fees', |
|
| 29 | + '_wpinv_discounts', |
|
| 30 | + '_wpinv_submission_id', |
|
| 31 | + '_wpinv_payment_form', |
|
| 32 | + '_wpinv_is_viewed', |
|
| 33 | + '_wpinv_phone', |
|
| 34 | + 'wpinv_email_cc', |
|
| 35 | + 'wpinv_template', |
|
| 36 | + 'wpinv_created_via' |
|
| 37 | + ); |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * A map of meta keys to data props. |
|
| 41 | + * |
|
| 42 | + * @since 1.0.19 |
|
| 43 | + * |
|
| 44 | + * @var array |
|
| 45 | + */ |
|
| 46 | + protected $meta_key_to_props = array( |
|
| 47 | + '_wpinv_subscr_profile_id' => 'remote_subscription_id', |
|
| 48 | + '_wpinv_subscription_id' => 'subscription_id', |
|
| 49 | + '_wpinv_taxes' => 'taxes', |
|
| 50 | + '_wpinv_fees' => 'fees', |
|
| 51 | + '_wpinv_discounts' => 'discounts', |
|
| 52 | + '_wpinv_submission_id' => 'submission_id', |
|
| 53 | + '_wpinv_payment_form' => 'payment_form', |
|
| 54 | + '_wpinv_is_viewed' => 'is_viewed', |
|
| 55 | + 'wpinv_email_cc' => 'email_cc', |
|
| 56 | + 'wpinv_template' => 'template', |
|
| 57 | + 'wpinv_created_via' => 'created_via', |
|
| 58 | + '_wpinv_phone' => 'phone', |
|
| 59 | + ); |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * A map of database fields to data props. |
|
| 63 | + * |
|
| 64 | + * @since 1.0.19 |
|
| 65 | + * |
|
| 66 | + * @var array |
|
| 67 | + */ |
|
| 68 | + protected $database_fields_to_props = array( |
|
| 69 | + 'post_id' => 'id', |
|
| 70 | + 'number' => 'number', |
|
| 71 | + 'currency' => 'currency', |
|
| 72 | + 'key' => 'key', |
|
| 73 | + 'type' => 'type', |
|
| 74 | + 'mode' => 'mode', |
|
| 75 | + 'user_ip' => 'user_ip', |
|
| 76 | + 'first_name' => 'first_name', |
|
| 77 | + 'last_name' => 'last_name', |
|
| 78 | + 'address' => 'address', |
|
| 79 | + 'city' => 'city', |
|
| 80 | + 'state' => 'state', |
|
| 81 | + 'country' => 'country', |
|
| 82 | + 'zip' => 'zip', |
|
| 83 | + 'zip' => 'zip', |
|
| 84 | + 'adddress_confirmed' => 'address_confirmed', |
|
| 85 | + 'gateway' => 'gateway', |
|
| 86 | + 'transaction_id' => 'transaction_id', |
|
| 87 | + 'currency' => 'currency', |
|
| 88 | + 'subtotal' => 'subtotal', |
|
| 89 | + 'tax' => 'total_tax', |
|
| 90 | + 'fees_total' => 'total_fees', |
|
| 91 | + 'discount' => 'total_discount', |
|
| 92 | + 'total' => 'total', |
|
| 93 | + 'discount_code' => 'discount_code', |
|
| 94 | + 'disable_taxes' => 'disable_taxes', |
|
| 95 | + 'due_date' => 'due_date', |
|
| 96 | + 'completed_date' => 'completed_date', |
|
| 97 | + 'company' => 'company', |
|
| 98 | + 'vat_number' => 'vat_number', |
|
| 99 | + 'vat_rate' => 'vat_rate', |
|
| 100 | + ); |
|
| 101 | + |
|
| 102 | + /* |
|
| 103 | 103 | |-------------------------------------------------------------------------- |
| 104 | 104 | | CRUD Methods |
| 105 | 105 | |-------------------------------------------------------------------------- |
| 106 | 106 | */ |
| 107 | 107 | |
| 108 | - /** |
|
| 109 | - * Method to create a new invoice in the database. |
|
| 110 | - * |
|
| 111 | - * @param WPInv_Invoice $invoice Invoice object. |
|
| 112 | - */ |
|
| 113 | - public function create( &$invoice ) { |
|
| 114 | - $invoice->set_version( WPINV_VERSION ); |
|
| 115 | - $invoice->set_date_created( current_time('mysql') ); |
|
| 116 | - |
|
| 117 | - // Create a new post. |
|
| 118 | - $id = wp_insert_post( |
|
| 119 | - apply_filters( |
|
| 120 | - 'getpaid_new_invoice_data', |
|
| 121 | - array( |
|
| 122 | - 'post_date' => $invoice->get_date_created( 'edit' ), |
|
| 123 | - 'post_type' => $invoice->get_post_type( 'edit' ), |
|
| 124 | - 'post_status' => $this->get_post_status( $invoice ), |
|
| 125 | - 'ping_status' => 'closed', |
|
| 126 | - 'post_author' => $invoice->get_user_id( 'edit' ), |
|
| 127 | - 'post_title' => $invoice->get_title( 'edit' ), |
|
| 128 | - 'post_excerpt' => $invoice->get_description( 'edit' ), |
|
| 129 | - 'post_parent' => $invoice->get_parent_id( 'edit' ), |
|
| 130 | - ) |
|
| 131 | - ), |
|
| 132 | - true |
|
| 133 | - ); |
|
| 134 | - |
|
| 135 | - if ( $id && ! is_wp_error( $id ) ) { |
|
| 136 | - |
|
| 137 | - // Update the new id and regenerate a title. |
|
| 138 | - $invoice->set_id( $id ); |
|
| 139 | - |
|
| 140 | - $invoice->maybe_set_number(); |
|
| 141 | - |
|
| 142 | - wp_update_post( |
|
| 143 | - array( |
|
| 144 | - 'ID' => $invoice->get_id(), |
|
| 145 | - 'post_title' => $invoice->get_number( 'edit' ), |
|
| 146 | - 'post_name' => $invoice->get_path( 'edit' ) |
|
| 147 | - ) |
|
| 148 | - ); |
|
| 149 | - |
|
| 150 | - // Save special fields and items. |
|
| 151 | - $this->save_special_fields( $invoice ); |
|
| 152 | - $this->save_items( $invoice ); |
|
| 153 | - |
|
| 154 | - // Update meta data. |
|
| 155 | - $this->update_post_meta( $invoice ); |
|
| 156 | - $invoice->save_meta_data(); |
|
| 157 | - |
|
| 158 | - // Apply changes. |
|
| 159 | - $invoice->apply_changes(); |
|
| 160 | - $this->clear_caches( $invoice ); |
|
| 161 | - |
|
| 162 | - // Fires after a new invoice is created. |
|
| 163 | - do_action( 'getpaid_new_invoice', $invoice ); |
|
| 164 | - return true; |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - if ( is_wp_error( $id ) ) { |
|
| 168 | - $invoice->last_error = $id->get_error_message(); |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - return false; |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - /** |
|
| 175 | - * Method to read an invoice from the database. |
|
| 176 | - * |
|
| 177 | - * @param WPInv_Invoice $invoice Invoice object. |
|
| 178 | - * |
|
| 179 | - */ |
|
| 180 | - public function read( &$invoice ) { |
|
| 181 | - |
|
| 182 | - $invoice->set_defaults(); |
|
| 183 | - $invoice_object = get_post( $invoice->get_id() ); |
|
| 184 | - |
|
| 185 | - if ( ! $invoice->get_id() || ! $invoice_object || ! getpaid_is_invoice_post_type( $invoice_object->post_type ) ) { |
|
| 186 | - $invoice->last_error = __( 'Invalid invoice.', 'invoicing' ); |
|
| 187 | - $invoice->set_id( 0 ); |
|
| 188 | - return false; |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - $invoice->set_props( |
|
| 192 | - array( |
|
| 193 | - 'date_created' => 0 < $invoice_object->post_date ? $invoice_object->post_date : null, |
|
| 194 | - 'date_modified' => 0 < $invoice_object->post_modified ? $invoice_object->post_modified : null, |
|
| 195 | - 'status' => $invoice_object->post_status, |
|
| 196 | - 'author' => $invoice_object->post_author, |
|
| 197 | - 'description' => $invoice_object->post_excerpt, |
|
| 198 | - 'parent_id' => $invoice_object->post_parent, |
|
| 199 | - 'name' => $invoice_object->post_title, |
|
| 200 | - 'path' => $invoice_object->post_name, |
|
| 201 | - 'post_type' => $invoice_object->post_type, |
|
| 202 | - ) |
|
| 203 | - ); |
|
| 204 | - |
|
| 205 | - $invoice->set_type( $invoice_object->post_type ); |
|
| 206 | - |
|
| 207 | - $this->read_object_data( $invoice, $invoice_object ); |
|
| 208 | - $this->add_special_fields( $invoice ); |
|
| 209 | - $this->add_items( $invoice ); |
|
| 210 | - $invoice->read_meta_data(); |
|
| 211 | - $invoice->set_object_read( true ); |
|
| 212 | - do_action( 'getpaid_read_invoice', $invoice ); |
|
| 213 | - |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - /** |
|
| 217 | - * Method to update an invoice in the database. |
|
| 218 | - * |
|
| 219 | - * @param WPInv_Invoice $invoice Invoice object. |
|
| 220 | - */ |
|
| 221 | - public function update( &$invoice ) { |
|
| 222 | - $invoice->save_meta_data(); |
|
| 223 | - $invoice->set_version( WPINV_VERSION ); |
|
| 224 | - |
|
| 225 | - if ( null === $invoice->get_date_created( 'edit' ) ) { |
|
| 226 | - $invoice->set_date_created( current_time('mysql') ); |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - // Ensure both the key and number are set. |
|
| 230 | - $invoice->get_path(); |
|
| 231 | - |
|
| 232 | - // Grab the current status so we can compare. |
|
| 233 | - $previous_status = get_post_status( $invoice->get_id() ); |
|
| 234 | - |
|
| 235 | - $changes = $invoice->get_changes(); |
|
| 236 | - |
|
| 237 | - // Only update the post when the post data changes. |
|
| 238 | - if ( array_intersect( array( 'date_created', 'date_modified', 'status', 'name', 'author', 'description', 'parent_id', 'post_excerpt', 'path' ), array_keys( $changes ) ) ) { |
|
| 239 | - $post_data = array( |
|
| 240 | - 'post_date' => $invoice->get_date_created( 'edit' ), |
|
| 241 | - 'post_date_gmt' => $invoice->get_date_created_gmt( 'edit' ), |
|
| 242 | - 'post_status' => $invoice->get_status( 'edit' ), |
|
| 243 | - 'post_title' => $invoice->get_name( 'edit' ), |
|
| 244 | - 'post_author' => $invoice->get_user_id( 'edit' ), |
|
| 245 | - 'post_modified' => $invoice->get_date_modified( 'edit' ), |
|
| 246 | - 'post_excerpt' => $invoice->get_description( 'edit' ), |
|
| 247 | - 'post_parent' => $invoice->get_parent_id( 'edit' ), |
|
| 248 | - 'post_name' => $invoice->get_path( 'edit' ), |
|
| 249 | - 'post_type' => $invoice->get_post_type( 'edit' ), |
|
| 250 | - ); |
|
| 251 | - |
|
| 252 | - /** |
|
| 253 | - * When updating this object, to prevent infinite loops, use $wpdb |
|
| 254 | - * to update data, since wp_update_post spawns more calls to the |
|
| 255 | - * save_post action. |
|
| 256 | - * |
|
| 257 | - * This ensures hooks are fired by either WP itself (admin screen save), |
|
| 258 | - * or an update purely from CRUD. |
|
| 259 | - */ |
|
| 260 | - if ( doing_action( 'save_post' ) ) { |
|
| 261 | - $GLOBALS['wpdb']->update( $GLOBALS['wpdb']->posts, $post_data, array( 'ID' => $invoice->get_id() ) ); |
|
| 262 | - clean_post_cache( $invoice->get_id() ); |
|
| 263 | - } else { |
|
| 264 | - wp_update_post( array_merge( array( 'ID' => $invoice->get_id() ), $post_data ) ); |
|
| 265 | - } |
|
| 266 | - $invoice->read_meta_data( true ); // Refresh internal meta data, in case things were hooked into `save_post` or another WP hook. |
|
| 267 | - } |
|
| 268 | - |
|
| 269 | - // Update meta data. |
|
| 270 | - $this->update_post_meta( $invoice ); |
|
| 271 | - |
|
| 272 | - // Save special fields and items. |
|
| 273 | - $this->save_special_fields( $invoice ); |
|
| 274 | - $this->save_items( $invoice ); |
|
| 275 | - |
|
| 276 | - // Apply the changes. |
|
| 277 | - $invoice->apply_changes(); |
|
| 278 | - |
|
| 279 | - // Clear caches. |
|
| 280 | - $this->clear_caches( $invoice ); |
|
| 281 | - |
|
| 282 | - // Fire a hook depending on the status - this should be considered a creation if it was previously draft status. |
|
| 283 | - $new_status = $invoice->get_status( 'edit' ); |
|
| 284 | - |
|
| 285 | - if ( $new_status !== $previous_status && in_array( $previous_status, array( 'new', 'auto-draft', 'draft' ), true ) ) { |
|
| 286 | - do_action( 'getpaid_new_invoice', $invoice ); |
|
| 287 | - } else { |
|
| 288 | - do_action( 'getpaid_update_invoice', $invoice ); |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - /* |
|
| 108 | + /** |
|
| 109 | + * Method to create a new invoice in the database. |
|
| 110 | + * |
|
| 111 | + * @param WPInv_Invoice $invoice Invoice object. |
|
| 112 | + */ |
|
| 113 | + public function create( &$invoice ) { |
|
| 114 | + $invoice->set_version( WPINV_VERSION ); |
|
| 115 | + $invoice->set_date_created( current_time('mysql') ); |
|
| 116 | + |
|
| 117 | + // Create a new post. |
|
| 118 | + $id = wp_insert_post( |
|
| 119 | + apply_filters( |
|
| 120 | + 'getpaid_new_invoice_data', |
|
| 121 | + array( |
|
| 122 | + 'post_date' => $invoice->get_date_created( 'edit' ), |
|
| 123 | + 'post_type' => $invoice->get_post_type( 'edit' ), |
|
| 124 | + 'post_status' => $this->get_post_status( $invoice ), |
|
| 125 | + 'ping_status' => 'closed', |
|
| 126 | + 'post_author' => $invoice->get_user_id( 'edit' ), |
|
| 127 | + 'post_title' => $invoice->get_title( 'edit' ), |
|
| 128 | + 'post_excerpt' => $invoice->get_description( 'edit' ), |
|
| 129 | + 'post_parent' => $invoice->get_parent_id( 'edit' ), |
|
| 130 | + ) |
|
| 131 | + ), |
|
| 132 | + true |
|
| 133 | + ); |
|
| 134 | + |
|
| 135 | + if ( $id && ! is_wp_error( $id ) ) { |
|
| 136 | + |
|
| 137 | + // Update the new id and regenerate a title. |
|
| 138 | + $invoice->set_id( $id ); |
|
| 139 | + |
|
| 140 | + $invoice->maybe_set_number(); |
|
| 141 | + |
|
| 142 | + wp_update_post( |
|
| 143 | + array( |
|
| 144 | + 'ID' => $invoice->get_id(), |
|
| 145 | + 'post_title' => $invoice->get_number( 'edit' ), |
|
| 146 | + 'post_name' => $invoice->get_path( 'edit' ) |
|
| 147 | + ) |
|
| 148 | + ); |
|
| 149 | + |
|
| 150 | + // Save special fields and items. |
|
| 151 | + $this->save_special_fields( $invoice ); |
|
| 152 | + $this->save_items( $invoice ); |
|
| 153 | + |
|
| 154 | + // Update meta data. |
|
| 155 | + $this->update_post_meta( $invoice ); |
|
| 156 | + $invoice->save_meta_data(); |
|
| 157 | + |
|
| 158 | + // Apply changes. |
|
| 159 | + $invoice->apply_changes(); |
|
| 160 | + $this->clear_caches( $invoice ); |
|
| 161 | + |
|
| 162 | + // Fires after a new invoice is created. |
|
| 163 | + do_action( 'getpaid_new_invoice', $invoice ); |
|
| 164 | + return true; |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + if ( is_wp_error( $id ) ) { |
|
| 168 | + $invoice->last_error = $id->get_error_message(); |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + return false; |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + /** |
|
| 175 | + * Method to read an invoice from the database. |
|
| 176 | + * |
|
| 177 | + * @param WPInv_Invoice $invoice Invoice object. |
|
| 178 | + * |
|
| 179 | + */ |
|
| 180 | + public function read( &$invoice ) { |
|
| 181 | + |
|
| 182 | + $invoice->set_defaults(); |
|
| 183 | + $invoice_object = get_post( $invoice->get_id() ); |
|
| 184 | + |
|
| 185 | + if ( ! $invoice->get_id() || ! $invoice_object || ! getpaid_is_invoice_post_type( $invoice_object->post_type ) ) { |
|
| 186 | + $invoice->last_error = __( 'Invalid invoice.', 'invoicing' ); |
|
| 187 | + $invoice->set_id( 0 ); |
|
| 188 | + return false; |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + $invoice->set_props( |
|
| 192 | + array( |
|
| 193 | + 'date_created' => 0 < $invoice_object->post_date ? $invoice_object->post_date : null, |
|
| 194 | + 'date_modified' => 0 < $invoice_object->post_modified ? $invoice_object->post_modified : null, |
|
| 195 | + 'status' => $invoice_object->post_status, |
|
| 196 | + 'author' => $invoice_object->post_author, |
|
| 197 | + 'description' => $invoice_object->post_excerpt, |
|
| 198 | + 'parent_id' => $invoice_object->post_parent, |
|
| 199 | + 'name' => $invoice_object->post_title, |
|
| 200 | + 'path' => $invoice_object->post_name, |
|
| 201 | + 'post_type' => $invoice_object->post_type, |
|
| 202 | + ) |
|
| 203 | + ); |
|
| 204 | + |
|
| 205 | + $invoice->set_type( $invoice_object->post_type ); |
|
| 206 | + |
|
| 207 | + $this->read_object_data( $invoice, $invoice_object ); |
|
| 208 | + $this->add_special_fields( $invoice ); |
|
| 209 | + $this->add_items( $invoice ); |
|
| 210 | + $invoice->read_meta_data(); |
|
| 211 | + $invoice->set_object_read( true ); |
|
| 212 | + do_action( 'getpaid_read_invoice', $invoice ); |
|
| 213 | + |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + /** |
|
| 217 | + * Method to update an invoice in the database. |
|
| 218 | + * |
|
| 219 | + * @param WPInv_Invoice $invoice Invoice object. |
|
| 220 | + */ |
|
| 221 | + public function update( &$invoice ) { |
|
| 222 | + $invoice->save_meta_data(); |
|
| 223 | + $invoice->set_version( WPINV_VERSION ); |
|
| 224 | + |
|
| 225 | + if ( null === $invoice->get_date_created( 'edit' ) ) { |
|
| 226 | + $invoice->set_date_created( current_time('mysql') ); |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + // Ensure both the key and number are set. |
|
| 230 | + $invoice->get_path(); |
|
| 231 | + |
|
| 232 | + // Grab the current status so we can compare. |
|
| 233 | + $previous_status = get_post_status( $invoice->get_id() ); |
|
| 234 | + |
|
| 235 | + $changes = $invoice->get_changes(); |
|
| 236 | + |
|
| 237 | + // Only update the post when the post data changes. |
|
| 238 | + if ( array_intersect( array( 'date_created', 'date_modified', 'status', 'name', 'author', 'description', 'parent_id', 'post_excerpt', 'path' ), array_keys( $changes ) ) ) { |
|
| 239 | + $post_data = array( |
|
| 240 | + 'post_date' => $invoice->get_date_created( 'edit' ), |
|
| 241 | + 'post_date_gmt' => $invoice->get_date_created_gmt( 'edit' ), |
|
| 242 | + 'post_status' => $invoice->get_status( 'edit' ), |
|
| 243 | + 'post_title' => $invoice->get_name( 'edit' ), |
|
| 244 | + 'post_author' => $invoice->get_user_id( 'edit' ), |
|
| 245 | + 'post_modified' => $invoice->get_date_modified( 'edit' ), |
|
| 246 | + 'post_excerpt' => $invoice->get_description( 'edit' ), |
|
| 247 | + 'post_parent' => $invoice->get_parent_id( 'edit' ), |
|
| 248 | + 'post_name' => $invoice->get_path( 'edit' ), |
|
| 249 | + 'post_type' => $invoice->get_post_type( 'edit' ), |
|
| 250 | + ); |
|
| 251 | + |
|
| 252 | + /** |
|
| 253 | + * When updating this object, to prevent infinite loops, use $wpdb |
|
| 254 | + * to update data, since wp_update_post spawns more calls to the |
|
| 255 | + * save_post action. |
|
| 256 | + * |
|
| 257 | + * This ensures hooks are fired by either WP itself (admin screen save), |
|
| 258 | + * or an update purely from CRUD. |
|
| 259 | + */ |
|
| 260 | + if ( doing_action( 'save_post' ) ) { |
|
| 261 | + $GLOBALS['wpdb']->update( $GLOBALS['wpdb']->posts, $post_data, array( 'ID' => $invoice->get_id() ) ); |
|
| 262 | + clean_post_cache( $invoice->get_id() ); |
|
| 263 | + } else { |
|
| 264 | + wp_update_post( array_merge( array( 'ID' => $invoice->get_id() ), $post_data ) ); |
|
| 265 | + } |
|
| 266 | + $invoice->read_meta_data( true ); // Refresh internal meta data, in case things were hooked into `save_post` or another WP hook. |
|
| 267 | + } |
|
| 268 | + |
|
| 269 | + // Update meta data. |
|
| 270 | + $this->update_post_meta( $invoice ); |
|
| 271 | + |
|
| 272 | + // Save special fields and items. |
|
| 273 | + $this->save_special_fields( $invoice ); |
|
| 274 | + $this->save_items( $invoice ); |
|
| 275 | + |
|
| 276 | + // Apply the changes. |
|
| 277 | + $invoice->apply_changes(); |
|
| 278 | + |
|
| 279 | + // Clear caches. |
|
| 280 | + $this->clear_caches( $invoice ); |
|
| 281 | + |
|
| 282 | + // Fire a hook depending on the status - this should be considered a creation if it was previously draft status. |
|
| 283 | + $new_status = $invoice->get_status( 'edit' ); |
|
| 284 | + |
|
| 285 | + if ( $new_status !== $previous_status && in_array( $previous_status, array( 'new', 'auto-draft', 'draft' ), true ) ) { |
|
| 286 | + do_action( 'getpaid_new_invoice', $invoice ); |
|
| 287 | + } else { |
|
| 288 | + do_action( 'getpaid_update_invoice', $invoice ); |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + /* |
|
| 294 | 294 | |-------------------------------------------------------------------------- |
| 295 | 295 | | Additional Methods |
| 296 | 296 | |-------------------------------------------------------------------------- |
| 297 | 297 | */ |
| 298 | 298 | |
| 299 | - /** |
|
| 299 | + /** |
|
| 300 | 300 | * Retrieves special fields and adds to the invoice. |
| 301 | - * |
|
| 302 | - * @param WPInv_Invoice $invoice Invoice object. |
|
| 301 | + * |
|
| 302 | + * @param WPInv_Invoice $invoice Invoice object. |
|
| 303 | 303 | */ |
| 304 | 304 | public function add_special_fields( &$invoice ) { |
| 305 | - global $wpdb; |
|
| 305 | + global $wpdb; |
|
| 306 | 306 | |
| 307 | - // Maybe retrieve from the cache. |
|
| 308 | - $data = wp_cache_get( $invoice->get_id(), 'getpaid_invoice_special_fields' ); |
|
| 307 | + // Maybe retrieve from the cache. |
|
| 308 | + $data = wp_cache_get( $invoice->get_id(), 'getpaid_invoice_special_fields' ); |
|
| 309 | 309 | |
| 310 | - // If not found, retrieve from the db. |
|
| 311 | - if ( false === $data ) { |
|
| 312 | - $table = $wpdb->prefix . 'getpaid_invoices'; |
|
| 310 | + // If not found, retrieve from the db. |
|
| 311 | + if ( false === $data ) { |
|
| 312 | + $table = $wpdb->prefix . 'getpaid_invoices'; |
|
| 313 | 313 | |
| 314 | - $data = $wpdb->get_row( |
|
| 315 | - $wpdb->prepare( "SELECT * FROM $table WHERE `post_id`=%d LIMIT 1", $invoice->get_id() ), |
|
| 316 | - ARRAY_A |
|
| 317 | - ); |
|
| 314 | + $data = $wpdb->get_row( |
|
| 315 | + $wpdb->prepare( "SELECT * FROM $table WHERE `post_id`=%d LIMIT 1", $invoice->get_id() ), |
|
| 316 | + ARRAY_A |
|
| 317 | + ); |
|
| 318 | 318 | |
| 319 | - // Update the cache with our data |
|
| 320 | - wp_cache_set( $invoice->get_id(), $data, 'getpaid_invoice_special_fields' ); |
|
| 319 | + // Update the cache with our data |
|
| 320 | + wp_cache_set( $invoice->get_id(), $data, 'getpaid_invoice_special_fields' ); |
|
| 321 | 321 | |
| 322 | - } |
|
| 322 | + } |
|
| 323 | 323 | |
| 324 | - // Abort if the data does not exist. |
|
| 325 | - if ( empty( $data ) ) { |
|
| 326 | - $invoice->set_object_read( true ); |
|
| 327 | - $invoice->set_props( wpinv_get_user_address( $invoice->get_user_id() ) ); |
|
| 328 | - return; |
|
| 329 | - } |
|
| 324 | + // Abort if the data does not exist. |
|
| 325 | + if ( empty( $data ) ) { |
|
| 326 | + $invoice->set_object_read( true ); |
|
| 327 | + $invoice->set_props( wpinv_get_user_address( $invoice->get_user_id() ) ); |
|
| 328 | + return; |
|
| 329 | + } |
|
| 330 | 330 | |
| 331 | - $props = array(); |
|
| 331 | + $props = array(); |
|
| 332 | 332 | |
| 333 | - foreach ( $this->database_fields_to_props as $db_field => $prop ) { |
|
| 333 | + foreach ( $this->database_fields_to_props as $db_field => $prop ) { |
|
| 334 | 334 | |
| 335 | - if ( $db_field == 'post_id' ) { |
|
| 336 | - continue; |
|
| 337 | - } |
|
| 338 | - |
|
| 339 | - $props[ $prop ] = $data[ $db_field ]; |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - $invoice->set_props( $props ); |
|
| 343 | - |
|
| 344 | - } |
|
| 345 | - |
|
| 346 | - /** |
|
| 347 | - * Gets a list of special fields that need updated based on change state |
|
| 348 | - * or if they are present in the database or not. |
|
| 349 | - * |
|
| 350 | - * @param WPInv_Invoice $invoice The Invoice object. |
|
| 351 | - * @return array A mapping of field keys => prop names, filtered by ones that should be updated. |
|
| 352 | - */ |
|
| 353 | - protected function get_special_fields_to_update( $invoice ) { |
|
| 354 | - $fields_to_update = array(); |
|
| 355 | - $changed_props = $invoice->get_changes(); |
|
| 356 | - |
|
| 357 | - // Props should be updated if they are a part of the $changed array or don't exist yet. |
|
| 358 | - foreach ( $this->database_fields_to_props as $database_field => $prop ) { |
|
| 359 | - if ( array_key_exists( $prop, $changed_props ) ) { |
|
| 360 | - $fields_to_update[ $database_field ] = $prop; |
|
| 361 | - } |
|
| 362 | - } |
|
| 363 | - |
|
| 364 | - return $fields_to_update; |
|
| 365 | - } |
|
| 366 | - |
|
| 367 | - /** |
|
| 368 | - * Helper method that updates all the database fields for an invoice based on it's settings in the WPInv_Invoice class. |
|
| 369 | - * |
|
| 370 | - * @param WPInv_Invoice $invoice WPInv_Invoice object. |
|
| 371 | - * @since 1.0.19 |
|
| 372 | - */ |
|
| 373 | - protected function update_special_fields( &$invoice ) { |
|
| 374 | - global $wpdb; |
|
| 375 | - |
|
| 376 | - $updated_props = array(); |
|
| 377 | - $fields_to_update = $this->get_special_fields_to_update( $invoice ); |
|
| 378 | - |
|
| 379 | - foreach ( $fields_to_update as $database_field => $prop ) { |
|
| 380 | - $value = $invoice->{"get_$prop"}( 'edit' ); |
|
| 381 | - $value = is_string( $value ) ? wp_slash( $value ) : $value; |
|
| 382 | - $value = is_bool( $value ) ? ( int ) $value : $value; |
|
| 383 | - $updated_props[ $database_field ] = maybe_serialize( $value ); |
|
| 384 | - } |
|
| 385 | - |
|
| 386 | - if ( ! empty( $updated_props ) ) { |
|
| 387 | - |
|
| 388 | - $table = $wpdb->prefix . 'getpaid_invoices'; |
|
| 389 | - $wpdb->update( $table, $updated_props, array( 'post_id' => $invoice->get_id() ) ); |
|
| 390 | - wp_cache_delete( $invoice->get_id(), 'getpaid_invoice_special_fields' ); |
|
| 391 | - do_action( "getpaid_invoice_update_database_fields", $invoice, $updated_props ); |
|
| 392 | - |
|
| 393 | - } |
|
| 394 | - |
|
| 395 | - } |
|
| 396 | - |
|
| 397 | - /** |
|
| 398 | - * Helper method that inserts special fields to the database. |
|
| 399 | - * |
|
| 400 | - * @param WPInv_Invoice $invoice WPInv_Invoice object. |
|
| 401 | - * @since 1.0.19 |
|
| 402 | - */ |
|
| 403 | - protected function insert_special_fields( &$invoice ) { |
|
| 404 | - global $wpdb; |
|
| 405 | - |
|
| 406 | - $updated_props = array(); |
|
| 407 | - |
|
| 408 | - foreach ( $this->database_fields_to_props as $database_field => $prop ) { |
|
| 409 | - $value = $invoice->{"get_$prop"}( 'edit' ); |
|
| 410 | - $value = is_string( $value ) ? wp_slash( $value ) : $value; |
|
| 411 | - $value = is_bool( $value ) ? ( int ) $value : $value; |
|
| 412 | - $updated_props[ $database_field ] = maybe_serialize( $value ); |
|
| 413 | - } |
|
| 414 | - |
|
| 415 | - $table = $wpdb->prefix . 'getpaid_invoices'; |
|
| 416 | - $wpdb->insert( $table, $updated_props ); |
|
| 417 | - wp_cache_delete( $invoice->get_id(), 'getpaid_invoice_special_fields' ); |
|
| 418 | - do_action( "getpaid_invoice_insert_database_fields", $invoice, $updated_props ); |
|
| 419 | - |
|
| 420 | - } |
|
| 421 | - |
|
| 422 | - /** |
|
| 335 | + if ( $db_field == 'post_id' ) { |
|
| 336 | + continue; |
|
| 337 | + } |
|
| 338 | + |
|
| 339 | + $props[ $prop ] = $data[ $db_field ]; |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + $invoice->set_props( $props ); |
|
| 343 | + |
|
| 344 | + } |
|
| 345 | + |
|
| 346 | + /** |
|
| 347 | + * Gets a list of special fields that need updated based on change state |
|
| 348 | + * or if they are present in the database or not. |
|
| 349 | + * |
|
| 350 | + * @param WPInv_Invoice $invoice The Invoice object. |
|
| 351 | + * @return array A mapping of field keys => prop names, filtered by ones that should be updated. |
|
| 352 | + */ |
|
| 353 | + protected function get_special_fields_to_update( $invoice ) { |
|
| 354 | + $fields_to_update = array(); |
|
| 355 | + $changed_props = $invoice->get_changes(); |
|
| 356 | + |
|
| 357 | + // Props should be updated if they are a part of the $changed array or don't exist yet. |
|
| 358 | + foreach ( $this->database_fields_to_props as $database_field => $prop ) { |
|
| 359 | + if ( array_key_exists( $prop, $changed_props ) ) { |
|
| 360 | + $fields_to_update[ $database_field ] = $prop; |
|
| 361 | + } |
|
| 362 | + } |
|
| 363 | + |
|
| 364 | + return $fields_to_update; |
|
| 365 | + } |
|
| 366 | + |
|
| 367 | + /** |
|
| 368 | + * Helper method that updates all the database fields for an invoice based on it's settings in the WPInv_Invoice class. |
|
| 369 | + * |
|
| 370 | + * @param WPInv_Invoice $invoice WPInv_Invoice object. |
|
| 371 | + * @since 1.0.19 |
|
| 372 | + */ |
|
| 373 | + protected function update_special_fields( &$invoice ) { |
|
| 374 | + global $wpdb; |
|
| 375 | + |
|
| 376 | + $updated_props = array(); |
|
| 377 | + $fields_to_update = $this->get_special_fields_to_update( $invoice ); |
|
| 378 | + |
|
| 379 | + foreach ( $fields_to_update as $database_field => $prop ) { |
|
| 380 | + $value = $invoice->{"get_$prop"}( 'edit' ); |
|
| 381 | + $value = is_string( $value ) ? wp_slash( $value ) : $value; |
|
| 382 | + $value = is_bool( $value ) ? ( int ) $value : $value; |
|
| 383 | + $updated_props[ $database_field ] = maybe_serialize( $value ); |
|
| 384 | + } |
|
| 385 | + |
|
| 386 | + if ( ! empty( $updated_props ) ) { |
|
| 387 | + |
|
| 388 | + $table = $wpdb->prefix . 'getpaid_invoices'; |
|
| 389 | + $wpdb->update( $table, $updated_props, array( 'post_id' => $invoice->get_id() ) ); |
|
| 390 | + wp_cache_delete( $invoice->get_id(), 'getpaid_invoice_special_fields' ); |
|
| 391 | + do_action( "getpaid_invoice_update_database_fields", $invoice, $updated_props ); |
|
| 392 | + |
|
| 393 | + } |
|
| 394 | + |
|
| 395 | + } |
|
| 396 | + |
|
| 397 | + /** |
|
| 398 | + * Helper method that inserts special fields to the database. |
|
| 399 | + * |
|
| 400 | + * @param WPInv_Invoice $invoice WPInv_Invoice object. |
|
| 401 | + * @since 1.0.19 |
|
| 402 | + */ |
|
| 403 | + protected function insert_special_fields( &$invoice ) { |
|
| 404 | + global $wpdb; |
|
| 405 | + |
|
| 406 | + $updated_props = array(); |
|
| 407 | + |
|
| 408 | + foreach ( $this->database_fields_to_props as $database_field => $prop ) { |
|
| 409 | + $value = $invoice->{"get_$prop"}( 'edit' ); |
|
| 410 | + $value = is_string( $value ) ? wp_slash( $value ) : $value; |
|
| 411 | + $value = is_bool( $value ) ? ( int ) $value : $value; |
|
| 412 | + $updated_props[ $database_field ] = maybe_serialize( $value ); |
|
| 413 | + } |
|
| 414 | + |
|
| 415 | + $table = $wpdb->prefix . 'getpaid_invoices'; |
|
| 416 | + $wpdb->insert( $table, $updated_props ); |
|
| 417 | + wp_cache_delete( $invoice->get_id(), 'getpaid_invoice_special_fields' ); |
|
| 418 | + do_action( "getpaid_invoice_insert_database_fields", $invoice, $updated_props ); |
|
| 419 | + |
|
| 420 | + } |
|
| 421 | + |
|
| 422 | + /** |
|
| 423 | 423 | * Saves all special fields. |
| 424 | - * |
|
| 425 | - * @param WPInv_Invoice $invoice Invoice object. |
|
| 424 | + * |
|
| 425 | + * @param WPInv_Invoice $invoice Invoice object. |
|
| 426 | 426 | */ |
| 427 | 427 | public function save_special_fields( & $invoice ) { |
| 428 | - global $wpdb; |
|
| 428 | + global $wpdb; |
|
| 429 | 429 | |
| 430 | - // The invoices table. |
|
| 431 | - $table = $wpdb->prefix . 'getpaid_invoices'; |
|
| 432 | - $id = (int) $invoice->get_id(); |
|
| 433 | - $invoice->maybe_set_key(); |
|
| 430 | + // The invoices table. |
|
| 431 | + $table = $wpdb->prefix . 'getpaid_invoices'; |
|
| 432 | + $id = (int) $invoice->get_id(); |
|
| 433 | + $invoice->maybe_set_key(); |
|
| 434 | 434 | |
| 435 | - if ( $wpdb->get_var( "SELECT `post_id` FROM $table WHERE `post_id`= $id" ) ) { |
|
| 435 | + if ( $wpdb->get_var( "SELECT `post_id` FROM $table WHERE `post_id`= $id" ) ) { |
|
| 436 | 436 | |
| 437 | - $this->update_special_fields( $invoice ); |
|
| 437 | + $this->update_special_fields( $invoice ); |
|
| 438 | 438 | |
| 439 | - } else { |
|
| 439 | + } else { |
|
| 440 | 440 | |
| 441 | - $this->insert_special_fields( $invoice ); |
|
| 441 | + $this->insert_special_fields( $invoice ); |
|
| 442 | 442 | |
| 443 | - } |
|
| 443 | + } |
|
| 444 | 444 | |
| 445 | - } |
|
| 445 | + } |
|
| 446 | 446 | |
| 447 | - /** |
|
| 447 | + /** |
|
| 448 | 448 | * Set's up cart details. |
| 449 | - * |
|
| 450 | - * @param WPInv_Invoice $invoice Invoice object. |
|
| 449 | + * |
|
| 450 | + * @param WPInv_Invoice $invoice Invoice object. |
|
| 451 | 451 | */ |
| 452 | 452 | public function add_items( &$invoice ) { |
| 453 | - global $wpdb; |
|
| 453 | + global $wpdb; |
|
| 454 | 454 | |
| 455 | - // Maybe retrieve from the cache. |
|
| 456 | - $items = wp_cache_get( $invoice->get_id(), 'getpaid_invoice_cart_details' ); |
|
| 455 | + // Maybe retrieve from the cache. |
|
| 456 | + $items = wp_cache_get( $invoice->get_id(), 'getpaid_invoice_cart_details' ); |
|
| 457 | 457 | |
| 458 | - // If not found, retrieve from the db. |
|
| 459 | - if ( false === $items ) { |
|
| 460 | - $table = $wpdb->prefix . 'getpaid_invoice_items'; |
|
| 458 | + // If not found, retrieve from the db. |
|
| 459 | + if ( false === $items ) { |
|
| 460 | + $table = $wpdb->prefix . 'getpaid_invoice_items'; |
|
| 461 | 461 | |
| 462 | - $items = $wpdb->get_results( |
|
| 463 | - $wpdb->prepare( "SELECT * FROM $table WHERE `post_id`=%d", $invoice->get_id() ) |
|
| 464 | - ); |
|
| 462 | + $items = $wpdb->get_results( |
|
| 463 | + $wpdb->prepare( "SELECT * FROM $table WHERE `post_id`=%d", $invoice->get_id() ) |
|
| 464 | + ); |
|
| 465 | 465 | |
| 466 | - // Update the cache with our data |
|
| 467 | - wp_cache_set( $invoice->get_id(), $items, 'getpaid_invoice_cart_details' ); |
|
| 466 | + // Update the cache with our data |
|
| 467 | + wp_cache_set( $invoice->get_id(), $items, 'getpaid_invoice_cart_details' ); |
|
| 468 | 468 | |
| 469 | - } |
|
| 469 | + } |
|
| 470 | 470 | |
| 471 | - // Abort if no items found. |
|
| 471 | + // Abort if no items found. |
|
| 472 | 472 | if ( empty( $items ) ) { |
| 473 | 473 | return; |
| 474 | - } |
|
| 474 | + } |
|
| 475 | 475 | |
| 476 | - foreach ( $items as $item_data ) { |
|
| 477 | - $item = new GetPaid_Form_Item( $item_data->item_id ); |
|
| 476 | + foreach ( $items as $item_data ) { |
|
| 477 | + $item = new GetPaid_Form_Item( $item_data->item_id ); |
|
| 478 | 478 | |
| 479 | - // Set item data. |
|
| 480 | - $item->item_tax = wpinv_sanitize_amount( $item_data->tax ); |
|
| 481 | - $item->item_discount = wpinv_sanitize_amount( $item_data->discount ); |
|
| 482 | - $item->set_name( $item_data->item_name ); |
|
| 483 | - $item->set_description( $item_data->item_description ); |
|
| 484 | - $item->set_price( $item_data->item_price ); |
|
| 485 | - $item->set_quantity( $item_data->quantity ); |
|
| 486 | - $item->set_item_meta( $item_data->meta ); |
|
| 479 | + // Set item data. |
|
| 480 | + $item->item_tax = wpinv_sanitize_amount( $item_data->tax ); |
|
| 481 | + $item->item_discount = wpinv_sanitize_amount( $item_data->discount ); |
|
| 482 | + $item->set_name( $item_data->item_name ); |
|
| 483 | + $item->set_description( $item_data->item_description ); |
|
| 484 | + $item->set_price( $item_data->item_price ); |
|
| 485 | + $item->set_quantity( $item_data->quantity ); |
|
| 486 | + $item->set_item_meta( $item_data->meta ); |
|
| 487 | 487 | |
| 488 | - $invoice->add_item( $item ); |
|
| 489 | - } |
|
| 488 | + $invoice->add_item( $item ); |
|
| 489 | + } |
|
| 490 | 490 | |
| 491 | - } |
|
| 491 | + } |
|
| 492 | 492 | |
| 493 | - /** |
|
| 493 | + /** |
|
| 494 | 494 | * Saves cart details. |
| 495 | - * |
|
| 496 | - * @param WPInv_Invoice $invoice Invoice object. |
|
| 495 | + * |
|
| 496 | + * @param WPInv_Invoice $invoice Invoice object. |
|
| 497 | 497 | */ |
| 498 | 498 | public function save_items( $invoice ) { |
| 499 | 499 | |
| 500 | - // Delete previously existing items. |
|
| 501 | - $this->delete_items( $invoice ); |
|
| 500 | + // Delete previously existing items. |
|
| 501 | + $this->delete_items( $invoice ); |
|
| 502 | 502 | |
| 503 | - $table = $GLOBALS['wpdb']->prefix . 'getpaid_invoice_items'; |
|
| 503 | + $table = $GLOBALS['wpdb']->prefix . 'getpaid_invoice_items'; |
|
| 504 | 504 | |
| 505 | - foreach ( $invoice->get_cart_details() as $item_data ) { |
|
| 506 | - $item_data = array_map( 'maybe_serialize', $item_data ); |
|
| 507 | - $GLOBALS['wpdb']->insert( $table, $item_data ); |
|
| 508 | - } |
|
| 505 | + foreach ( $invoice->get_cart_details() as $item_data ) { |
|
| 506 | + $item_data = array_map( 'maybe_serialize', $item_data ); |
|
| 507 | + $GLOBALS['wpdb']->insert( $table, $item_data ); |
|
| 508 | + } |
|
| 509 | 509 | |
| 510 | - wp_cache_delete( $invoice->get_id(), 'getpaid_invoice_cart_details' ); |
|
| 511 | - do_action( "getpaid_invoice_save_items", $invoice ); |
|
| 510 | + wp_cache_delete( $invoice->get_id(), 'getpaid_invoice_cart_details' ); |
|
| 511 | + do_action( "getpaid_invoice_save_items", $invoice ); |
|
| 512 | 512 | |
| 513 | - } |
|
| 513 | + } |
|
| 514 | 514 | |
| 515 | - /** |
|
| 515 | + /** |
|
| 516 | 516 | * Deletes an invoice's cart details from the database. |
| 517 | - * |
|
| 518 | - * @param WPInv_Invoice $invoice Invoice object. |
|
| 517 | + * |
|
| 518 | + * @param WPInv_Invoice $invoice Invoice object. |
|
| 519 | 519 | */ |
| 520 | 520 | public function delete_items( $invoice ) { |
| 521 | - $table = $GLOBALS['wpdb']->prefix . 'getpaid_invoice_items'; |
|
| 522 | - return $GLOBALS['wpdb']->delete( $table, array( 'post_id' => $invoice->get_id() ) ); |
|
| 523 | - } |
|
| 521 | + $table = $GLOBALS['wpdb']->prefix . 'getpaid_invoice_items'; |
|
| 522 | + return $GLOBALS['wpdb']->delete( $table, array( 'post_id' => $invoice->get_id() ) ); |
|
| 523 | + } |
|
| 524 | 524 | |
| 525 | - /** |
|
| 525 | + /** |
|
| 526 | 526 | * Deletes an invoice's special fields from the database. |
| 527 | - * |
|
| 528 | - * @param WPInv_Invoice $invoice Invoice object. |
|
| 527 | + * |
|
| 528 | + * @param WPInv_Invoice $invoice Invoice object. |
|
| 529 | 529 | */ |
| 530 | 530 | public function delete_special_fields( $invoice ) { |
| 531 | - $table = $GLOBALS['wpdb']->prefix . 'getpaid_invoices'; |
|
| 532 | - return $GLOBALS['wpdb']->delete( $table, array( 'post_id' => $invoice->get_id() ) ); |
|
| 533 | - } |
|
| 531 | + $table = $GLOBALS['wpdb']->prefix . 'getpaid_invoices'; |
|
| 532 | + return $GLOBALS['wpdb']->delete( $table, array( 'post_id' => $invoice->get_id() ) ); |
|
| 533 | + } |
|
| 534 | 534 | |
| 535 | - /** |
|
| 536 | - * Get the status to save to the post object. |
|
| 537 | - * |
|
| 538 | - * |
|
| 539 | - * @since 1.0.19 |
|
| 540 | - * @param WPInv_Invoice $object GetPaid_Data object. |
|
| 541 | - * @return string |
|
| 542 | - */ |
|
| 543 | - protected function get_post_status( $object ) { |
|
| 544 | - $object_status = $object->get_status( 'edit' ); |
|
| 545 | - |
|
| 546 | - if ( ! $object_status ) { |
|
| 547 | - $object_status = $object->get_default_status(); |
|
| 548 | - } |
|
| 549 | - |
|
| 550 | - return $object_status; |
|
| 551 | - } |
|
| 535 | + /** |
|
| 536 | + * Get the status to save to the post object. |
|
| 537 | + * |
|
| 538 | + * |
|
| 539 | + * @since 1.0.19 |
|
| 540 | + * @param WPInv_Invoice $object GetPaid_Data object. |
|
| 541 | + * @return string |
|
| 542 | + */ |
|
| 543 | + protected function get_post_status( $object ) { |
|
| 544 | + $object_status = $object->get_status( 'edit' ); |
|
| 545 | + |
|
| 546 | + if ( ! $object_status ) { |
|
| 547 | + $object_status = $object->get_default_status(); |
|
| 548 | + } |
|
| 549 | + |
|
| 550 | + return $object_status; |
|
| 551 | + } |
|
| 552 | 552 | |
| 553 | 553 | } |
@@ -12,49 +12,49 @@ discard block |
||
| 12 | 12 | */ |
| 13 | 13 | class GetPaid_Reports { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * Class constructor. |
|
| 17 | - * |
|
| 18 | - */ |
|
| 19 | - public function __construct() { |
|
| 20 | - add_action( 'admin_menu', array( $this, 'register_reports_page' ), 20 ); |
|
| 21 | - add_action( 'wpinv_reports_tab_reports', array( $this, 'display_reports_tab' ) ); |
|
| 22 | - add_action( 'wpinv_reports_tab_export', array( $this, 'display_exports_tab' ) ); |
|
| 23 | - add_action( 'getpaid_authenticated_admin_action_download_graph', array( $this, 'download_graph' ) ); |
|
| 24 | - add_action( 'getpaid_authenticated_admin_action_export_invoices', array( $this, 'export_invoices' ) ); |
|
| 25 | - |
|
| 26 | - } |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * Registers the reports page. |
|
| 30 | - * |
|
| 31 | - */ |
|
| 32 | - public function register_reports_page() { |
|
| 33 | - |
|
| 34 | - add_submenu_page( |
|
| 15 | + /** |
|
| 16 | + * Class constructor. |
|
| 17 | + * |
|
| 18 | + */ |
|
| 19 | + public function __construct() { |
|
| 20 | + add_action( 'admin_menu', array( $this, 'register_reports_page' ), 20 ); |
|
| 21 | + add_action( 'wpinv_reports_tab_reports', array( $this, 'display_reports_tab' ) ); |
|
| 22 | + add_action( 'wpinv_reports_tab_export', array( $this, 'display_exports_tab' ) ); |
|
| 23 | + add_action( 'getpaid_authenticated_admin_action_download_graph', array( $this, 'download_graph' ) ); |
|
| 24 | + add_action( 'getpaid_authenticated_admin_action_export_invoices', array( $this, 'export_invoices' ) ); |
|
| 25 | + |
|
| 26 | + } |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * Registers the reports page. |
|
| 30 | + * |
|
| 31 | + */ |
|
| 32 | + public function register_reports_page() { |
|
| 33 | + |
|
| 34 | + add_submenu_page( |
|
| 35 | 35 | 'wpinv', |
| 36 | 36 | __( 'Reports', 'invoicing' ), |
| 37 | 37 | __( 'Reports', 'invoicing' ), |
| 38 | 38 | wpinv_get_capability(), |
| 39 | 39 | 'wpinv-reports', |
| 40 | 40 | array( $this, 'display_reports_page' ) |
| 41 | - ); |
|
| 41 | + ); |
|
| 42 | 42 | |
| 43 | - } |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * Displays the reports page. |
|
| 47 | - * |
|
| 48 | - */ |
|
| 49 | - public function display_reports_page() { |
|
| 45 | + /** |
|
| 46 | + * Displays the reports page. |
|
| 47 | + * |
|
| 48 | + */ |
|
| 49 | + public function display_reports_page() { |
|
| 50 | 50 | |
| 51 | - // Prepare variables. |
|
| 52 | - $tabs = $this->get_tabs(); |
|
| 53 | - $current_tab = isset( $_GET['tab'] ) ? sanitize_text_field( $_GET['tab'] ) : 'reports'; |
|
| 54 | - $current_tab = array_key_exists( $current_tab, $tabs ) ? $current_tab : 'reports'; |
|
| 51 | + // Prepare variables. |
|
| 52 | + $tabs = $this->get_tabs(); |
|
| 53 | + $current_tab = isset( $_GET['tab'] ) ? sanitize_text_field( $_GET['tab'] ) : 'reports'; |
|
| 54 | + $current_tab = array_key_exists( $current_tab, $tabs ) ? $current_tab : 'reports'; |
|
| 55 | 55 | |
| 56 | - // Display the current tab. |
|
| 57 | - ?> |
|
| 56 | + // Display the current tab. |
|
| 57 | + ?> |
|
| 58 | 58 | |
| 59 | 59 | <div class="wrap"> |
| 60 | 60 | |
@@ -63,19 +63,19 @@ discard block |
||
| 63 | 63 | <nav class="nav-tab-wrapper"> |
| 64 | 64 | |
| 65 | 65 | <?php |
| 66 | - foreach( $tabs as $key => $label ) { |
|
| 66 | + foreach( $tabs as $key => $label ) { |
|
| 67 | 67 | |
| 68 | - $key = sanitize_text_field( $key ); |
|
| 69 | - $label = sanitize_text_field( $label ); |
|
| 70 | - $class = $key == $current_tab ? 'nav-tab nav-tab-active' : 'nav-tab'; |
|
| 71 | - $url = esc_url( |
|
| 72 | - add_query_arg( 'tab', $key, admin_url( 'admin.php?page=wpinv-reports' ) ) |
|
| 73 | - ); |
|
| 68 | + $key = sanitize_text_field( $key ); |
|
| 69 | + $label = sanitize_text_field( $label ); |
|
| 70 | + $class = $key == $current_tab ? 'nav-tab nav-tab-active' : 'nav-tab'; |
|
| 71 | + $url = esc_url( |
|
| 72 | + add_query_arg( 'tab', $key, admin_url( 'admin.php?page=wpinv-reports' ) ) |
|
| 73 | + ); |
|
| 74 | 74 | |
| 75 | - echo "\n\t\t\t<a href='$url' class='$class'>$label</a>"; |
|
| 75 | + echo "\n\t\t\t<a href='$url' class='$class'>$label</a>"; |
|
| 76 | 76 | |
| 77 | - } |
|
| 78 | - ?> |
|
| 77 | + } |
|
| 78 | + ?> |
|
| 79 | 79 | |
| 80 | 80 | </nav> |
| 81 | 81 | |
@@ -86,77 +86,77 @@ discard block |
||
| 86 | 86 | </div> |
| 87 | 87 | <?php |
| 88 | 88 | |
| 89 | - // Wordfence loads an unsupported version of chart js on our page. |
|
| 90 | - wp_deregister_style( 'chart-js' ); |
|
| 91 | - wp_deregister_script( 'chart-js' ); |
|
| 92 | - wp_enqueue_script( 'chart-js', WPINV_PLUGIN_URL . 'assets/js/chart.bundle.min.js', array( 'jquery' ), '2.9.4', true ); |
|
| 93 | - wp_enqueue_style( 'chart-js', WPINV_PLUGIN_URL . 'assets/css/chart.min.css', array(), '2.9.4' ); |
|
| 94 | - |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * Retrieves reports page tabs. |
|
| 99 | - * |
|
| 100 | - * @return array |
|
| 101 | - */ |
|
| 102 | - public function get_tabs() { |
|
| 103 | - |
|
| 104 | - $tabs = array( |
|
| 105 | - 'reports' => __( 'Reports', 'invoicing' ), |
|
| 106 | - 'export' => __( 'Export', 'invoicing' ), |
|
| 107 | - ); |
|
| 108 | - |
|
| 109 | - return apply_filters( 'getpaid_report_tabs', $tabs ); |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * Displays the reports tab. |
|
| 114 | - * |
|
| 115 | - */ |
|
| 116 | - public function display_reports_tab() { |
|
| 117 | - |
|
| 118 | - $reports = new GetPaid_Reports_Report(); |
|
| 119 | - $reports->display(); |
|
| 120 | - |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Displays the exports tab. |
|
| 125 | - * |
|
| 126 | - */ |
|
| 127 | - public function display_exports_tab() { |
|
| 128 | - |
|
| 129 | - $exports = new GetPaid_Reports_Export(); |
|
| 130 | - $exports->display(); |
|
| 131 | - |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * Donwnloads a graph. |
|
| 136 | - * |
|
| 137 | - * @param array $args |
|
| 138 | - */ |
|
| 139 | - public function download_graph( $args ) { |
|
| 140 | - |
|
| 141 | - if ( ! empty( $args['graph'] ) ) { |
|
| 142 | - $downloader = new GetPaid_Graph_Downloader(); |
|
| 143 | - $downloader->download( $args['graph'] ); |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - /** |
|
| 149 | - * Exports invoices. |
|
| 150 | - * |
|
| 151 | - * @param array $args |
|
| 152 | - */ |
|
| 153 | - public function export_invoices( $args ) { |
|
| 154 | - |
|
| 155 | - if ( ! empty( $args['post_type'] ) ) { |
|
| 156 | - $downloader = new GetPaid_Invoice_Exporter(); |
|
| 157 | - $downloader->export( $args['post_type'], $args ); |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - } |
|
| 89 | + // Wordfence loads an unsupported version of chart js on our page. |
|
| 90 | + wp_deregister_style( 'chart-js' ); |
|
| 91 | + wp_deregister_script( 'chart-js' ); |
|
| 92 | + wp_enqueue_script( 'chart-js', WPINV_PLUGIN_URL . 'assets/js/chart.bundle.min.js', array( 'jquery' ), '2.9.4', true ); |
|
| 93 | + wp_enqueue_style( 'chart-js', WPINV_PLUGIN_URL . 'assets/css/chart.min.css', array(), '2.9.4' ); |
|
| 94 | + |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * Retrieves reports page tabs. |
|
| 99 | + * |
|
| 100 | + * @return array |
|
| 101 | + */ |
|
| 102 | + public function get_tabs() { |
|
| 103 | + |
|
| 104 | + $tabs = array( |
|
| 105 | + 'reports' => __( 'Reports', 'invoicing' ), |
|
| 106 | + 'export' => __( 'Export', 'invoicing' ), |
|
| 107 | + ); |
|
| 108 | + |
|
| 109 | + return apply_filters( 'getpaid_report_tabs', $tabs ); |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * Displays the reports tab. |
|
| 114 | + * |
|
| 115 | + */ |
|
| 116 | + public function display_reports_tab() { |
|
| 117 | + |
|
| 118 | + $reports = new GetPaid_Reports_Report(); |
|
| 119 | + $reports->display(); |
|
| 120 | + |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Displays the exports tab. |
|
| 125 | + * |
|
| 126 | + */ |
|
| 127 | + public function display_exports_tab() { |
|
| 128 | + |
|
| 129 | + $exports = new GetPaid_Reports_Export(); |
|
| 130 | + $exports->display(); |
|
| 131 | + |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * Donwnloads a graph. |
|
| 136 | + * |
|
| 137 | + * @param array $args |
|
| 138 | + */ |
|
| 139 | + public function download_graph( $args ) { |
|
| 140 | + |
|
| 141 | + if ( ! empty( $args['graph'] ) ) { |
|
| 142 | + $downloader = new GetPaid_Graph_Downloader(); |
|
| 143 | + $downloader->download( $args['graph'] ); |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + /** |
|
| 149 | + * Exports invoices. |
|
| 150 | + * |
|
| 151 | + * @param array $args |
|
| 152 | + */ |
|
| 153 | + public function export_invoices( $args ) { |
|
| 154 | + |
|
| 155 | + if ( ! empty( $args['post_type'] ) ) { |
|
| 156 | + $downloader = new GetPaid_Invoice_Exporter(); |
|
| 157 | + $downloader->export( $args['post_type'], $args ); |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + } |
|
| 161 | 161 | |
| 162 | 162 | } |