@@ -15,69 +15,69 @@ discard block |
||
| 15 | 15 | */ |
| 16 | 16 | class GetPaid_Customer extends GetPaid_Data { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * Which data store to load. |
|
| 20 | - * |
|
| 21 | - * @var string |
|
| 22 | - */ |
|
| 18 | + /** |
|
| 19 | + * Which data store to load. |
|
| 20 | + * |
|
| 21 | + * @var string |
|
| 22 | + */ |
|
| 23 | 23 | protected $data_store_name = 'customer'; |
| 24 | 24 | |
| 25 | 25 | /** |
| 26 | - * This is the name of this object type. |
|
| 27 | - * |
|
| 28 | - * @var string |
|
| 29 | - */ |
|
| 30 | - protected $object_type = 'customer'; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * Get the customer if ID is passed, otherwise the customer is new and empty. |
|
| 34 | - * |
|
| 35 | - * @param int|string|GetPaid_Customer|object $customer customer id, object, or email. |
|
| 36 | - */ |
|
| 37 | - public function __construct( $customer = 0 ) { |
|
| 26 | + * This is the name of this object type. |
|
| 27 | + * |
|
| 28 | + * @var string |
|
| 29 | + */ |
|
| 30 | + protected $object_type = 'customer'; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * Get the customer if ID is passed, otherwise the customer is new and empty. |
|
| 34 | + * |
|
| 35 | + * @param int|string|GetPaid_Customer|object $customer customer id, object, or email. |
|
| 36 | + */ |
|
| 37 | + public function __construct( $customer = 0 ) { |
|
| 38 | 38 | |
| 39 | 39 | // Setup default customer data. |
| 40 | 40 | $this->setup_default_data(); |
| 41 | 41 | |
| 42 | - if ( is_numeric( $customer ) ) { |
|
| 43 | - $this->set_id( $customer ); |
|
| 44 | - } elseif ( $customer instanceof self ) { |
|
| 45 | - $this->set_id( $customer->get_id() ); |
|
| 46 | - } elseif ( is_string( $customer ) && $customer_id = self::get_customer_id_by( $customer, 'email' ) ) { |
|
| 47 | - $this->set_id( $customer_id ); |
|
| 48 | - } elseif ( ! empty( $customer->id ) ) { |
|
| 49 | - $this->set_id( $customer->id ); |
|
| 50 | - } |
|
| 42 | + if ( is_numeric( $customer ) ) { |
|
| 43 | + $this->set_id( $customer ); |
|
| 44 | + } elseif ( $customer instanceof self ) { |
|
| 45 | + $this->set_id( $customer->get_id() ); |
|
| 46 | + } elseif ( is_string( $customer ) && $customer_id = self::get_customer_id_by( $customer, 'email' ) ) { |
|
| 47 | + $this->set_id( $customer_id ); |
|
| 48 | + } elseif ( ! empty( $customer->id ) ) { |
|
| 49 | + $this->set_id( $customer->id ); |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | 52 | // Load the datastore. |
| 53 | - $this->data_store = GetPaid_Data_Store::load( $this->data_store_name ); |
|
| 53 | + $this->data_store = GetPaid_Data_Store::load( $this->data_store_name ); |
|
| 54 | 54 | |
| 55 | - if ( $this->get_id() > 0 ) { |
|
| 56 | - $this->data_store->read( $this ); |
|
| 55 | + if ( $this->get_id() > 0 ) { |
|
| 56 | + $this->data_store->read( $this ); |
|
| 57 | 57 | } |
| 58 | 58 | |
| 59 | 59 | $this->set_object_read( true ); |
| 60 | - } |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | 62 | /** |
| 63 | - * Sets up default customer data. |
|
| 64 | - */ |
|
| 65 | - private function setup_default_data() { |
|
| 63 | + * Sets up default customer data. |
|
| 64 | + */ |
|
| 65 | + private function setup_default_data() { |
|
| 66 | 66 | |
| 67 | 67 | $this->data = array( |
| 68 | - 'user_id' => 0, |
|
| 69 | - 'email' => '', |
|
| 70 | - 'email_cc' => '', |
|
| 71 | - 'status' => 'active', |
|
| 72 | - 'purchase_value' => 0, |
|
| 73 | - 'purchase_count' => 0, |
|
| 74 | - 'date_created' => current_time( 'mysql' ), |
|
| 75 | - 'date_modified' => current_time( 'mysql' ), |
|
| 76 | - 'uuid' => wp_generate_uuid4(), |
|
| 77 | - ); |
|
| 68 | + 'user_id' => 0, |
|
| 69 | + 'email' => '', |
|
| 70 | + 'email_cc' => '', |
|
| 71 | + 'status' => 'active', |
|
| 72 | + 'purchase_value' => 0, |
|
| 73 | + 'purchase_count' => 0, |
|
| 74 | + 'date_created' => current_time( 'mysql' ), |
|
| 75 | + 'date_modified' => current_time( 'mysql' ), |
|
| 76 | + 'uuid' => wp_generate_uuid4(), |
|
| 77 | + ); |
|
| 78 | 78 | |
| 79 | 79 | // Add address fields. |
| 80 | - foreach ( array_keys( getpaid_user_address_fields() ) as $field ) { |
|
| 80 | + foreach ( array_keys( getpaid_user_address_fields() ) as $field ) { |
|
| 81 | 81 | |
| 82 | 82 | if ( isset( $this->data[ $field ] ) ) { |
| 83 | 83 | continue; |
@@ -95,22 +95,22 @@ discard block |
||
| 95 | 95 | continue; |
| 96 | 96 | } |
| 97 | 97 | |
| 98 | - $this->data[ $field ] = ''; |
|
| 99 | - } |
|
| 98 | + $this->data[ $field ] = ''; |
|
| 99 | + } |
|
| 100 | 100 | |
| 101 | 101 | $this->default_data = $this->data; |
| 102 | - } |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * Given a customer email or user id, it returns a customer id. |
|
| 106 | - * |
|
| 107 | - * @static |
|
| 108 | - * @param string $value |
|
| 109 | - * @since 1.0.15 |
|
| 110 | - * @return int |
|
| 111 | - */ |
|
| 112 | - public static function get_customer_id_by( $value, $by = 'email' ) { |
|
| 113 | - global $wpdb; |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * Given a customer email or user id, it returns a customer id. |
|
| 106 | + * |
|
| 107 | + * @static |
|
| 108 | + * @param string $value |
|
| 109 | + * @since 1.0.15 |
|
| 110 | + * @return int |
|
| 111 | + */ |
|
| 112 | + public static function get_customer_id_by( $value, $by = 'email' ) { |
|
| 113 | + global $wpdb; |
|
| 114 | 114 | |
| 115 | 115 | // Prepare value. |
| 116 | 116 | if ( 'email' === $by ) { |
@@ -125,12 +125,12 @@ discard block |
||
| 125 | 125 | return 0; |
| 126 | 126 | } |
| 127 | 127 | |
| 128 | - // Maybe retrieve from the cache. |
|
| 128 | + // Maybe retrieve from the cache. |
|
| 129 | 129 | $cache_key = 'getpaid_customer_ids_by_' . $by; |
| 130 | - $customer_id = wp_cache_get( $value, $cache_key ); |
|
| 131 | - if ( false !== $customer_id ) { |
|
| 132 | - return $customer_id; |
|
| 133 | - } |
|
| 130 | + $customer_id = wp_cache_get( $value, $cache_key ); |
|
| 131 | + if ( false !== $customer_id ) { |
|
| 132 | + return $customer_id; |
|
| 133 | + } |
|
| 134 | 134 | |
| 135 | 135 | if ( 'email' === $by ) { |
| 136 | 136 | $customer_id = (int) $wpdb->get_var( |
@@ -142,23 +142,23 @@ discard block |
||
| 142 | 142 | ); |
| 143 | 143 | } |
| 144 | 144 | |
| 145 | - // Update the cache with our data |
|
| 146 | - wp_cache_set( $value, $customer_id, $cache_key ); |
|
| 145 | + // Update the cache with our data |
|
| 146 | + wp_cache_set( $value, $customer_id, $cache_key ); |
|
| 147 | 147 | |
| 148 | - return $customer_id; |
|
| 148 | + return $customer_id; |
|
| 149 | 149 | |
| 150 | - } |
|
| 150 | + } |
|
| 151 | 151 | |
| 152 | - /** |
|
| 152 | + /** |
|
| 153 | 153 | * Clears the customer's cache. |
| 154 | 154 | */ |
| 155 | 155 | public function clear_cache() { |
| 156 | 156 | wp_cache_delete( $this->get( 'email' ), 'getpaid_customer_ids_by_email' ); |
| 157 | 157 | wp_cache_delete( $this->get( 'user_id' ), 'getpaid_customer_ids_by_user_id' ); |
| 158 | - wp_cache_delete( $this->get_id(), 'getpaid_customers' ); |
|
| 159 | - } |
|
| 158 | + wp_cache_delete( $this->get_id(), 'getpaid_customers' ); |
|
| 159 | + } |
|
| 160 | 160 | |
| 161 | - /* |
|
| 161 | + /* |
|
| 162 | 162 | |-------------------------------------------------------------------------- |
| 163 | 163 | | CRUD methods |
| 164 | 164 | |-------------------------------------------------------------------------- |
@@ -189,11 +189,11 @@ discard block |
||
| 189 | 189 | return call_user_func( array( $this, 'get_' . $key ), $context ); |
| 190 | 190 | } |
| 191 | 191 | |
| 192 | - return $this->get_prop( $key, $context ); |
|
| 192 | + return $this->get_prop( $key, $context ); |
|
| 193 | 193 | |
| 194 | 194 | } |
| 195 | 195 | |
| 196 | - /* |
|
| 196 | + /* |
|
| 197 | 197 | |-------------------------------------------------------------------------- |
| 198 | 198 | | Setters |
| 199 | 199 | |-------------------------------------------------------------------------- |
@@ -216,114 +216,114 @@ discard block |
||
| 216 | 216 | return call_user_func( array( $this, 'set_' . $key ), $value ); |
| 217 | 217 | } |
| 218 | 218 | |
| 219 | - return $this->set_prop( $key, $value ); |
|
| 219 | + return $this->set_prop( $key, $value ); |
|
| 220 | 220 | |
| 221 | 221 | } |
| 222 | 222 | |
| 223 | - /** |
|
| 224 | - * Sets customer status. |
|
| 225 | - * |
|
| 226 | - * @since 1.0.0 |
|
| 227 | - * @param string $status New status. |
|
| 228 | - */ |
|
| 229 | - public function set_status( $status ) { |
|
| 230 | - |
|
| 231 | - if ( in_array( $status, array( 'active', 'inactive', 'blocked' ), true ) ) { |
|
| 232 | - return $this->set_prop( 'status', $status ); |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - $this->set_prop( 'status', 'inactive' ); |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - /** |
|
| 239 | - * Sets the purchase value. |
|
| 240 | - * |
|
| 241 | - * @since 1.0.0 |
|
| 242 | - * @param float $purchase_value. |
|
| 243 | - */ |
|
| 244 | - public function set_purchase_value( $purchase_value ) { |
|
| 245 | - $this->set_prop( 'purchase_value', (float) $purchase_value ); |
|
| 246 | - } |
|
| 223 | + /** |
|
| 224 | + * Sets customer status. |
|
| 225 | + * |
|
| 226 | + * @since 1.0.0 |
|
| 227 | + * @param string $status New status. |
|
| 228 | + */ |
|
| 229 | + public function set_status( $status ) { |
|
| 230 | + |
|
| 231 | + if ( in_array( $status, array( 'active', 'inactive', 'blocked' ), true ) ) { |
|
| 232 | + return $this->set_prop( 'status', $status ); |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + $this->set_prop( 'status', 'inactive' ); |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + /** |
|
| 239 | + * Sets the purchase value. |
|
| 240 | + * |
|
| 241 | + * @since 1.0.0 |
|
| 242 | + * @param float $purchase_value. |
|
| 243 | + */ |
|
| 244 | + public function set_purchase_value( $purchase_value ) { |
|
| 245 | + $this->set_prop( 'purchase_value', (float) $purchase_value ); |
|
| 246 | + } |
|
| 247 | 247 | |
| 248 | 248 | /** |
| 249 | - * Sets the purchase count. |
|
| 250 | - * |
|
| 251 | - * @since 1.0.0 |
|
| 252 | - * @param int $purchase_count. |
|
| 253 | - */ |
|
| 254 | - public function set_purchase_count( $purchase_count ) { |
|
| 255 | - $this->set_prop( 'purchase_count', absint( $purchase_count ) ); |
|
| 256 | - } |
|
| 249 | + * Sets the purchase count. |
|
| 250 | + * |
|
| 251 | + * @since 1.0.0 |
|
| 252 | + * @param int $purchase_count. |
|
| 253 | + */ |
|
| 254 | + public function set_purchase_count( $purchase_count ) { |
|
| 255 | + $this->set_prop( 'purchase_count', absint( $purchase_count ) ); |
|
| 256 | + } |
|
| 257 | 257 | |
| 258 | 258 | /** |
| 259 | - * Sets the user id. |
|
| 260 | - * |
|
| 261 | - * @since 1.0.0 |
|
| 262 | - * @param int $user_id. |
|
| 263 | - */ |
|
| 264 | - public function set_user_id( $user_id ) { |
|
| 265 | - $this->set_prop( 'user_id', absint( $user_id ) ); |
|
| 266 | - } |
|
| 259 | + * Sets the user id. |
|
| 260 | + * |
|
| 261 | + * @since 1.0.0 |
|
| 262 | + * @param int $user_id. |
|
| 263 | + */ |
|
| 264 | + public function set_user_id( $user_id ) { |
|
| 265 | + $this->set_prop( 'user_id', absint( $user_id ) ); |
|
| 266 | + } |
|
| 267 | 267 | |
| 268 | 268 | /** |
| 269 | - * Sets the email. |
|
| 270 | - * |
|
| 271 | - * @since 1.0.0 |
|
| 272 | - * @param string $email. |
|
| 273 | - */ |
|
| 274 | - public function set_email( $email ) { |
|
| 269 | + * Sets the email. |
|
| 270 | + * |
|
| 271 | + * @since 1.0.0 |
|
| 272 | + * @param string $email. |
|
| 273 | + */ |
|
| 274 | + public function set_email( $email ) { |
|
| 275 | 275 | $email = is_string( $email ) ? sanitize_email( $email ) : ''; |
| 276 | - $this->set_prop( 'email', $email ); |
|
| 277 | - } |
|
| 276 | + $this->set_prop( 'email', $email ); |
|
| 277 | + } |
|
| 278 | 278 | |
| 279 | 279 | /** |
| 280 | - * Sets the email cc. |
|
| 281 | - * |
|
| 282 | - * @since 1.0.0 |
|
| 283 | - * @param string $email_cc. |
|
| 284 | - */ |
|
| 285 | - public function set_email_cc( $email_cc ) { |
|
| 280 | + * Sets the email cc. |
|
| 281 | + * |
|
| 282 | + * @since 1.0.0 |
|
| 283 | + * @param string $email_cc. |
|
| 284 | + */ |
|
| 285 | + public function set_email_cc( $email_cc ) { |
|
| 286 | 286 | $email_cc = implode( ', ', wp_parse_list( $email_cc ) ); |
| 287 | - $this->set_prop( 'email_cc', $email_cc ); |
|
| 288 | - } |
|
| 287 | + $this->set_prop( 'email_cc', $email_cc ); |
|
| 288 | + } |
|
| 289 | 289 | |
| 290 | 290 | /** |
| 291 | - * Sets the created date. |
|
| 292 | - * |
|
| 293 | - * @since 1.0.0 |
|
| 294 | - * @param string $date_created date created. |
|
| 295 | - */ |
|
| 296 | - public function set_date_created( $date_created ) { |
|
| 291 | + * Sets the created date. |
|
| 292 | + * |
|
| 293 | + * @since 1.0.0 |
|
| 294 | + * @param string $date_created date created. |
|
| 295 | + */ |
|
| 296 | + public function set_date_created( $date_created ) { |
|
| 297 | 297 | |
| 298 | - $date = strtotime( $date_created ); |
|
| 298 | + $date = strtotime( $date_created ); |
|
| 299 | 299 | |
| 300 | 300 | if ( $date && $date_created !== '0000-00-00 00:00:00' && $date_created !== '0000-00-00 00:00' ) { |
| 301 | 301 | $this->set_prop( 'date_created', gmdate( 'Y-m-d H:i:s', $date ) ); |
| 302 | 302 | return; |
| 303 | - } |
|
| 303 | + } |
|
| 304 | 304 | |
| 305 | - $this->set_prop( 'date_created', null ); |
|
| 306 | - } |
|
| 305 | + $this->set_prop( 'date_created', null ); |
|
| 306 | + } |
|
| 307 | 307 | |
| 308 | 308 | /** |
| 309 | - * Sets the created date. |
|
| 310 | - * |
|
| 311 | - * @since 1.0.0 |
|
| 312 | - * @param string $date_modified date created. |
|
| 313 | - */ |
|
| 314 | - public function set_date_modified( $date_modified ) { |
|
| 309 | + * Sets the created date. |
|
| 310 | + * |
|
| 311 | + * @since 1.0.0 |
|
| 312 | + * @param string $date_modified date created. |
|
| 313 | + */ |
|
| 314 | + public function set_date_modified( $date_modified ) { |
|
| 315 | 315 | |
| 316 | - $date = strtotime( $date_modified ); |
|
| 316 | + $date = strtotime( $date_modified ); |
|
| 317 | 317 | |
| 318 | 318 | if ( $date && $date_modified !== '0000-00-00 00:00:00' && $date_modified !== '0000-00-00 00:00' ) { |
| 319 | 319 | $this->set_prop( 'date_modified', gmdate( 'Y-m-d H:i:s', $date ) ); |
| 320 | 320 | return; |
| 321 | - } |
|
| 321 | + } |
|
| 322 | 322 | |
| 323 | - $this->set_prop( 'date_modified', null ); |
|
| 324 | - } |
|
| 323 | + $this->set_prop( 'date_modified', null ); |
|
| 324 | + } |
|
| 325 | 325 | |
| 326 | - /* |
|
| 326 | + /* |
|
| 327 | 327 | |-------------------------------------------------------------------------- |
| 328 | 328 | | Additional methods |
| 329 | 329 | |-------------------------------------------------------------------------- |
@@ -332,12 +332,12 @@ discard block |
||
| 332 | 332 | | |
| 333 | 333 | */ |
| 334 | 334 | |
| 335 | - /** |
|
| 336 | - * Saves the customer. |
|
| 337 | - * |
|
| 338 | - * @since 1.0.0 |
|
| 339 | - */ |
|
| 340 | - public function save() { |
|
| 335 | + /** |
|
| 336 | + * Saves the customer. |
|
| 337 | + * |
|
| 338 | + * @since 1.0.0 |
|
| 339 | + */ |
|
| 340 | + public function save() { |
|
| 341 | 341 | |
| 342 | 342 | $maybe_set = array( |
| 343 | 343 | 'uuid' => wp_generate_uuid4(), |
@@ -354,29 +354,29 @@ discard block |
||
| 354 | 354 | |
| 355 | 355 | $this->set( 'date_modified', current_time( 'mysql' ) ); |
| 356 | 356 | |
| 357 | - return parent::save(); |
|
| 358 | - } |
|
| 357 | + return parent::save(); |
|
| 358 | + } |
|
| 359 | 359 | |
| 360 | 360 | /** |
| 361 | - * Helper method to clone a customer from a user ID. |
|
| 362 | - * |
|
| 363 | - * @since 1.0.0 |
|
| 364 | - * @param int $user_id. |
|
| 365 | - */ |
|
| 366 | - public function clone_user( $user_id ) { |
|
| 361 | + * Helper method to clone a customer from a user ID. |
|
| 362 | + * |
|
| 363 | + * @since 1.0.0 |
|
| 364 | + * @param int $user_id. |
|
| 365 | + */ |
|
| 366 | + public function clone_user( $user_id ) { |
|
| 367 | 367 | $user = get_userdata( $user_id ); |
| 368 | 368 | |
| 369 | 369 | if ( empty( $user ) ) { |
| 370 | 370 | return; |
| 371 | 371 | } |
| 372 | 372 | |
| 373 | - $this->set_user_id( $user->ID ); |
|
| 373 | + $this->set_user_id( $user->ID ); |
|
| 374 | 374 | $this->set_email( $user->user_email ); |
| 375 | 375 | $this->set_purchase_value( getpaid_get_user_total_spend( $user->ID ) ); |
| 376 | 376 | $this->set_purchase_count( getpaid_count_user_invoices( $user->ID ) ); |
| 377 | 377 | $this->set( 'first_name', $user->first_name ); |
| 378 | 378 | $this->set( 'last_name', $user->last_name ); |
| 379 | - $this->set_date_created( $user->user_registered ); |
|
| 379 | + $this->set_date_created( $user->user_registered ); |
|
| 380 | 380 | |
| 381 | 381 | // Fetch extra data from WC or old GetPaid. |
| 382 | 382 | $prefixes = array( |
@@ -400,18 +400,18 @@ discard block |
||
| 400 | 400 | continue; |
| 401 | 401 | } |
| 402 | 402 | } |
| 403 | - } |
|
| 404 | - } |
|
| 403 | + } |
|
| 404 | + } |
|
| 405 | 405 | |
| 406 | 406 | /** |
| 407 | - * Helper method to migrate an existing user ID to the new customers table. |
|
| 408 | - * |
|
| 409 | - * @since 1.0.0 |
|
| 410 | - * @param int $user_id. |
|
| 411 | - */ |
|
| 412 | - public function migrate_from_user( $user_id ) { |
|
| 407 | + * Helper method to migrate an existing user ID to the new customers table. |
|
| 408 | + * |
|
| 409 | + * @since 1.0.0 |
|
| 410 | + * @param int $user_id. |
|
| 411 | + */ |
|
| 412 | + public function migrate_from_user( $user_id ) { |
|
| 413 | 413 | $this->clone_user( $user_id ); |
| 414 | 414 | do_action( 'getpaid_customer_migrated_from_user', $this, $user_id ); |
| 415 | 415 | $this->save(); |
| 416 | - } |
|
| 416 | + } |
|
| 417 | 417 | } |
@@ -8,7 +8,7 @@ discard block |
||
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | 10 | if ( ! defined( 'ABSPATH' ) ) { |
| 11 | - exit; // Exit if accessed directly |
|
| 11 | + exit; // Exit if accessed directly |
|
| 12 | 12 | } |
| 13 | 13 | |
| 14 | 14 | /** |
@@ -75,16 +75,16 @@ discard block |
||
| 75 | 75 | $class .= ' wpi-recurring'; |
| 76 | 76 | } |
| 77 | 77 | |
| 78 | - $refund_url = wp_nonce_url( |
|
| 79 | - add_query_arg( |
|
| 80 | - array( |
|
| 81 | - 'getpaid-admin-action' => 'refund_invoice', |
|
| 82 | - 'invoice_id' => $invoice->get_id(), |
|
| 83 | - ) |
|
| 84 | - ), |
|
| 85 | - 'getpaid-nonce', |
|
| 86 | - 'getpaid-nonce' |
|
| 87 | - ); |
|
| 78 | + $refund_url = wp_nonce_url( |
|
| 79 | + add_query_arg( |
|
| 80 | + array( |
|
| 81 | + 'getpaid-admin-action' => 'refund_invoice', |
|
| 82 | + 'invoice_id' => $invoice->get_id(), |
|
| 83 | + ) |
|
| 84 | + ), |
|
| 85 | + 'getpaid-nonce', |
|
| 86 | + 'getpaid-nonce' |
|
| 87 | + ); |
|
| 88 | 88 | ?> |
| 89 | 89 | |
| 90 | 90 | <div class="wpinv-items-wrap<?php echo esc_attr( $class ); ?>" id="wpinv_items_wrap" data-status="<?php echo esc_attr( $invoice->get_status() ); ?>"> |
@@ -96,8 +96,8 @@ discard block |
||
| 96 | 96 | <th class=" |
| 97 | 97 | <?php |
| 98 | 98 | echo esc_attr( $key ); |
| 99 | - echo 'total' == $key || 'qty' == $key ? ' hide-if-amount' : ''; |
|
| 100 | - ?> |
|
| 99 | + echo 'total' == $key || 'qty' == $key ? ' hide-if-amount' : ''; |
|
| 100 | + ?> |
|
| 101 | 101 | "><?php echo wp_kses_post( $label ); ?></th> |
| 102 | 102 | <?php endforeach; ?> |
| 103 | 103 | </tr> |
@@ -106,7 +106,7 @@ discard block |
||
| 106 | 106 | <tbody class="wpinv-line-items"> |
| 107 | 107 | <?php |
| 108 | 108 | foreach ( $invoice->get_items() as $int => $item ) { |
| 109 | - self::output_row( $columns, $item, $invoice, $int % 2 == 0 ? 'even' : 'odd' ); |
|
| 109 | + self::output_row( $columns, $item, $invoice, $int % 2 == 0 ? 'even' : 'odd' ); |
|
| 110 | 110 | } |
| 111 | 111 | ?> |
| 112 | 112 | </tbody> |
@@ -151,13 +151,13 @@ discard block |
||
| 151 | 151 | <?php |
| 152 | 152 | wpinv_html_select( |
| 153 | 153 | array( |
| 154 | - 'options' => $item_types, |
|
| 155 | - 'name' => '_wpinv_quick[type]', |
|
| 156 | - 'id' => '_wpinv_quick_type', |
|
| 157 | - 'selected' => 'custom', |
|
| 158 | - 'show_option_all' => false, |
|
| 159 | - 'show_option_none' => false, |
|
| 160 | - 'class' => 'gdmbx2-text-medium wpinv-quick-type', |
|
| 154 | + 'options' => $item_types, |
|
| 155 | + 'name' => '_wpinv_quick[type]', |
|
| 156 | + 'id' => '_wpinv_quick_type', |
|
| 157 | + 'selected' => 'custom', |
|
| 158 | + 'show_option_all' => false, |
|
| 159 | + 'show_option_none' => false, |
|
| 160 | + 'class' => 'gdmbx2-text-medium wpinv-quick-type', |
|
| 161 | 161 | ) |
| 162 | 162 | ); |
| 163 | 163 | ?> |
@@ -172,16 +172,16 @@ discard block |
||
| 172 | 172 | <?php |
| 173 | 173 | wpinv_html_select( |
| 174 | 174 | array( |
| 175 | - 'options' => array_merge( |
|
| 176 | - array( '' => __( 'Select VAT Rule', 'invoicing' ) ), |
|
| 177 | - getpaid_get_tax_rules() |
|
| 178 | - ), |
|
| 179 | - 'name' => '_wpinv_quick[vat_rule]', |
|
| 180 | - 'id' => '_wpinv_quick_vat_rule', |
|
| 181 | - 'show_option_all' => false, |
|
| 182 | - 'show_option_none' => false, |
|
| 183 | - 'class' => 'gdmbx2-text-medium wpinv-quick-vat-rule', |
|
| 184 | - 'selected' => 'digital', |
|
| 175 | + 'options' => array_merge( |
|
| 176 | + array( '' => __( 'Select VAT Rule', 'invoicing' ) ), |
|
| 177 | + getpaid_get_tax_rules() |
|
| 178 | + ), |
|
| 179 | + 'name' => '_wpinv_quick[vat_rule]', |
|
| 180 | + 'id' => '_wpinv_quick_vat_rule', |
|
| 181 | + 'show_option_all' => false, |
|
| 182 | + 'show_option_none' => false, |
|
| 183 | + 'class' => 'gdmbx2-text-medium wpinv-quick-vat-rule', |
|
| 184 | + 'selected' => 'digital', |
|
| 185 | 185 | ) |
| 186 | 186 | ); |
| 187 | 187 | ?> |
@@ -194,16 +194,16 @@ discard block |
||
| 194 | 194 | <?php |
| 195 | 195 | wpinv_html_select( |
| 196 | 196 | array( |
| 197 | - 'options' => array_merge( |
|
| 198 | - array( '' => __( 'Select VAT Class', 'invoicing' ) ), |
|
| 199 | - getpaid_get_tax_classes() |
|
| 200 | - ), |
|
| 201 | - 'name' => '_wpinv_quick[vat_class]', |
|
| 202 | - 'id' => '_wpinv_quick_vat_class', |
|
| 203 | - 'show_option_all' => false, |
|
| 204 | - 'show_option_none' => false, |
|
| 205 | - 'class' => 'gdmbx2-text-medium wpinv-quick-vat-class', |
|
| 206 | - 'selected' => '_standard', |
|
| 197 | + 'options' => array_merge( |
|
| 198 | + array( '' => __( 'Select VAT Class', 'invoicing' ) ), |
|
| 199 | + getpaid_get_tax_classes() |
|
| 200 | + ), |
|
| 201 | + 'name' => '_wpinv_quick[vat_class]', |
|
| 202 | + 'id' => '_wpinv_quick_vat_class', |
|
| 203 | + 'show_option_all' => false, |
|
| 204 | + 'show_option_none' => false, |
|
| 205 | + 'class' => 'gdmbx2-text-medium wpinv-quick-vat-class', |
|
| 206 | + 'selected' => '_standard', |
|
| 207 | 207 | ) |
| 208 | 208 | ); |
| 209 | 209 | ?> |
@@ -294,37 +294,37 @@ discard block |
||
| 294 | 294 | </p> |
| 295 | 295 | <?php if ( getpaid_payment_gateway_supports( $invoice->get_gateway(), 'refunds' ) ) : ?> |
| 296 | 296 | <?php |
| 297 | - aui()->input( |
|
| 298 | - array( |
|
| 299 | - 'type' => 'checkbox', |
|
| 300 | - 'name' => 'getpaid_refund_remote', |
|
| 301 | - 'id' => 'getpaid_refund_remote', |
|
| 302 | - 'label' => sprintf( |
|
| 303 | - 'Refund payment in %s', |
|
| 304 | - wpinv_get_gateway_admin_label( $invoice->get_gateway() ) |
|
| 305 | - ), |
|
| 306 | - 'value' => 1, |
|
| 307 | - 'class' => 'getpaid-refund-field', |
|
| 308 | - ), |
|
| 309 | - true |
|
| 310 | - ); |
|
| 311 | - ?> |
|
| 297 | + aui()->input( |
|
| 298 | + array( |
|
| 299 | + 'type' => 'checkbox', |
|
| 300 | + 'name' => 'getpaid_refund_remote', |
|
| 301 | + 'id' => 'getpaid_refund_remote', |
|
| 302 | + 'label' => sprintf( |
|
| 303 | + 'Refund payment in %s', |
|
| 304 | + wpinv_get_gateway_admin_label( $invoice->get_gateway() ) |
|
| 305 | + ), |
|
| 306 | + 'value' => 1, |
|
| 307 | + 'class' => 'getpaid-refund-field', |
|
| 308 | + ), |
|
| 309 | + true |
|
| 310 | + ); |
|
| 311 | + ?> |
|
| 312 | 312 | <?php endif; ?> |
| 313 | 313 | |
| 314 | 314 | <?php if ( getpaid_get_invoice_subscriptions( $invoice ) ) : ?> |
| 315 | 315 | <?php |
| 316 | - aui()->input( |
|
| 317 | - array( |
|
| 318 | - 'type' => 'checkbox', |
|
| 319 | - 'name' => 'getpaid_cancel_subscription', |
|
| 320 | - 'id' => 'getpaid_cancel_subscription', |
|
| 321 | - 'label' => __( 'Cancel subscription', 'invoicing' ), |
|
| 322 | - 'value' => 1, |
|
| 323 | - 'class' => 'getpaid-refund-field', |
|
| 324 | - ), |
|
| 325 | - true |
|
| 326 | - ); |
|
| 327 | - ?> |
|
| 316 | + aui()->input( |
|
| 317 | + array( |
|
| 318 | + 'type' => 'checkbox', |
|
| 319 | + 'name' => 'getpaid_cancel_subscription', |
|
| 320 | + 'id' => 'getpaid_cancel_subscription', |
|
| 321 | + 'label' => __( 'Cancel subscription', 'invoicing' ), |
|
| 322 | + 'value' => 1, |
|
| 323 | + 'class' => 'getpaid-refund-field', |
|
| 324 | + ), |
|
| 325 | + true |
|
| 326 | + ); |
|
| 327 | + ?> |
|
| 328 | 328 | <?php endif; ?> |
| 329 | 329 | </div> |
| 330 | 330 | <div class="modal-footer"> |
@@ -364,23 +364,23 @@ discard block |
||
| 364 | 364 | <?php |
| 365 | 365 | if ( $invoice->is_paid() ) { |
| 366 | 366 | |
| 367 | - printf( |
|
| 368 | - '<span class="bsui"><button type="button" class="button button-primary" data-toggle="modal" data-bs-toggle="modal" data-bs-target="#getpaid-refund-invoice-modal" data-target="#getpaid-refund-invoice-modal">%s</button></span>', |
|
| 369 | - esc_html__( 'Refund', 'invoicing' ) |
|
| 370 | - ); |
|
| 367 | + printf( |
|
| 368 | + '<span class="bsui"><button type="button" class="button button-primary" data-toggle="modal" data-bs-toggle="modal" data-bs-target="#getpaid-refund-invoice-modal" data-target="#getpaid-refund-invoice-modal">%s</button></span>', |
|
| 369 | + esc_html__( 'Refund', 'invoicing' ) |
|
| 370 | + ); |
|
| 371 | 371 | } elseif ( ! $invoice->is_refunded() ) { |
| 372 | - wpinv_item_dropdown( |
|
| 373 | - array( |
|
| 374 | - 'name' => 'wpinv_invoice_item', |
|
| 375 | - 'id' => 'wpinv_invoice_item', |
|
| 376 | - 'show_recurring' => true, |
|
| 377 | - 'class' => 'wpi_select2', |
|
| 378 | - ) |
|
| 379 | - ); |
|
| 380 | - |
|
| 381 | - echo ' ' . '<button type="button" class="button button-primary" id="wpinv-add-item">' . sprintf( esc_html__( 'Add item to %s', 'invoicing' ), esc_html( $invoice->get_label() ) ) . '</button>'; |
|
| 382 | - echo ' ' . '<button type="button" class="button button-primary" id="wpinv-new-item">' . esc_html__( 'Create new item', 'invoicing' ) . '</button>'; |
|
| 383 | - echo ' ' . '<button type="button" class="button button-primary wpinv-flr" id="wpinv-recalc-totals">' . esc_html__( 'Recalculate Totals', 'invoicing' ) . '</button>'; |
|
| 372 | + wpinv_item_dropdown( |
|
| 373 | + array( |
|
| 374 | + 'name' => 'wpinv_invoice_item', |
|
| 375 | + 'id' => 'wpinv_invoice_item', |
|
| 376 | + 'show_recurring' => true, |
|
| 377 | + 'class' => 'wpi_select2', |
|
| 378 | + ) |
|
| 379 | + ); |
|
| 380 | + |
|
| 381 | + echo ' ' . '<button type="button" class="button button-primary" id="wpinv-add-item">' . sprintf( esc_html__( 'Add item to %s', 'invoicing' ), esc_html( $invoice->get_label() ) ) . '</button>'; |
|
| 382 | + echo ' ' . '<button type="button" class="button button-primary" id="wpinv-new-item">' . esc_html__( 'Create new item', 'invoicing' ) . '</button>'; |
|
| 383 | + echo ' ' . '<button type="button" class="button button-primary wpinv-flr" id="wpinv-recalc-totals">' . esc_html__( 'Recalculate Totals', 'invoicing' ) . '</button>'; |
|
| 384 | 384 | |
| 385 | 385 | } |
| 386 | 386 | ?> |
@@ -397,72 +397,72 @@ discard block |
||
| 397 | 397 | <?php foreach ( array_keys( $columns ) as $column ) : ?> |
| 398 | 398 | <td class=" |
| 399 | 399 | <?php |
| 400 | - echo esc_attr( $column ); |
|
| 401 | - echo 'total' == $column || 'qty' == $column ? ' hide-if-amount' : ''; |
|
| 402 | - ?> |
|
| 400 | + echo esc_attr( $column ); |
|
| 401 | + echo 'total' == $column || 'qty' == $column ? ' hide-if-amount' : ''; |
|
| 402 | + ?> |
|
| 403 | 403 | "> |
| 404 | 404 | <?php |
| 405 | 405 | switch ( $column ) { |
| 406 | - case 'id': |
|
| 407 | - echo (int) $item->get_id(); |
|
| 408 | - break; |
|
| 409 | - case 'title': |
|
| 410 | - printf( |
|
| 411 | - '<a href="%s" target="_blank">%s</a>', |
|
| 412 | - esc_url( get_edit_post_link( $item->get_id() ) ), |
|
| 413 | - esc_html( $item->get_raw_name() ) |
|
| 414 | - ); |
|
| 415 | - |
|
| 416 | - $summary = apply_filters( 'getpaid_admin_invoice_line_item_summary', $item->get_description(), $item, $invoice ); |
|
| 417 | - if ( $summary !== '' ) { |
|
| 418 | - printf( |
|
| 419 | - '<span class="meta">%s</span>', |
|
| 420 | - wp_kses_post( wpautop( $summary ) ) |
|
| 421 | - ); |
|
| 422 | - } |
|
| 423 | - |
|
| 424 | - printf( |
|
| 425 | - '<input type="hidden" value="%s" name="getpaid_items[%s][name]" class="getpaid-recalculate-prices-on-change" />', |
|
| 426 | - esc_attr( $item->get_raw_name() ), |
|
| 427 | - (int) $item->get_id() |
|
| 428 | - ); |
|
| 429 | - |
|
| 430 | - printf( |
|
| 431 | - '<textarea style="display: none;" name="getpaid_items[%s][description]" class="getpaid-recalculate-prices-on-change">%s</textarea>', |
|
| 432 | - (int) $item->get_id(), |
|
| 433 | - esc_attr( $item->get_description() ) |
|
| 434 | - ); |
|
| 435 | - |
|
| 436 | - break; |
|
| 437 | - case 'price': |
|
| 438 | - printf( |
|
| 439 | - '<input type="text" value="%s" name="getpaid_items[%s][price]" style="width: 100px;" class="getpaid-admin-invoice-item-price getpaid-recalculate-prices-on-change" />', |
|
| 440 | - esc_attr( getpaid_unstandardize_amount( $item->get_price() ) ), |
|
| 441 | - (int) $item->get_id() |
|
| 442 | - ); |
|
| 443 | - |
|
| 444 | - break; |
|
| 445 | - case 'qty': |
|
| 446 | - printf( |
|
| 447 | - '<input type="text" style="width: 100px;" value="%s" name="getpaid_items[%s][quantity]" class="getpaid-admin-invoice-item-quantity getpaid-recalculate-prices-on-change" />', |
|
| 448 | - floatval( $item->get_quantity() ), |
|
| 449 | - (int) $item->get_id() |
|
| 450 | - ); |
|
| 451 | - |
|
| 452 | - break; |
|
| 453 | - case 'total': |
|
| 454 | - wpinv_the_price( $item->get_sub_total(), $invoice->get_currency() ); |
|
| 455 | - |
|
| 456 | - break; |
|
| 457 | - case 'tax': |
|
| 458 | - echo floatval( wpinv_round_amount( getpaid_get_invoice_tax_rate( $invoice, $item ), 2 ) ) . '%'; |
|
| 459 | - |
|
| 460 | - break; |
|
| 461 | - case 'action': |
|
| 462 | - if ( ! $invoice->is_paid() && ! $invoice->is_refunded() ) { |
|
| 463 | - echo '<i class="fa fa-trash wpinv-item-remove"></i>'; |
|
| 406 | + case 'id': |
|
| 407 | + echo (int) $item->get_id(); |
|
| 408 | + break; |
|
| 409 | + case 'title': |
|
| 410 | + printf( |
|
| 411 | + '<a href="%s" target="_blank">%s</a>', |
|
| 412 | + esc_url( get_edit_post_link( $item->get_id() ) ), |
|
| 413 | + esc_html( $item->get_raw_name() ) |
|
| 414 | + ); |
|
| 415 | + |
|
| 416 | + $summary = apply_filters( 'getpaid_admin_invoice_line_item_summary', $item->get_description(), $item, $invoice ); |
|
| 417 | + if ( $summary !== '' ) { |
|
| 418 | + printf( |
|
| 419 | + '<span class="meta">%s</span>', |
|
| 420 | + wp_kses_post( wpautop( $summary ) ) |
|
| 421 | + ); |
|
| 422 | + } |
|
| 423 | + |
|
| 424 | + printf( |
|
| 425 | + '<input type="hidden" value="%s" name="getpaid_items[%s][name]" class="getpaid-recalculate-prices-on-change" />', |
|
| 426 | + esc_attr( $item->get_raw_name() ), |
|
| 427 | + (int) $item->get_id() |
|
| 428 | + ); |
|
| 429 | + |
|
| 430 | + printf( |
|
| 431 | + '<textarea style="display: none;" name="getpaid_items[%s][description]" class="getpaid-recalculate-prices-on-change">%s</textarea>', |
|
| 432 | + (int) $item->get_id(), |
|
| 433 | + esc_attr( $item->get_description() ) |
|
| 434 | + ); |
|
| 435 | + |
|
| 436 | + break; |
|
| 437 | + case 'price': |
|
| 438 | + printf( |
|
| 439 | + '<input type="text" value="%s" name="getpaid_items[%s][price]" style="width: 100px;" class="getpaid-admin-invoice-item-price getpaid-recalculate-prices-on-change" />', |
|
| 440 | + esc_attr( getpaid_unstandardize_amount( $item->get_price() ) ), |
|
| 441 | + (int) $item->get_id() |
|
| 442 | + ); |
|
| 443 | + |
|
| 444 | + break; |
|
| 445 | + case 'qty': |
|
| 446 | + printf( |
|
| 447 | + '<input type="text" style="width: 100px;" value="%s" name="getpaid_items[%s][quantity]" class="getpaid-admin-invoice-item-quantity getpaid-recalculate-prices-on-change" />', |
|
| 448 | + floatval( $item->get_quantity() ), |
|
| 449 | + (int) $item->get_id() |
|
| 450 | + ); |
|
| 451 | + |
|
| 452 | + break; |
|
| 453 | + case 'total': |
|
| 454 | + wpinv_the_price( $item->get_sub_total(), $invoice->get_currency() ); |
|
| 455 | + |
|
| 456 | + break; |
|
| 457 | + case 'tax': |
|
| 458 | + echo floatval( wpinv_round_amount( getpaid_get_invoice_tax_rate( $invoice, $item ), 2 ) ) . '%'; |
|
| 459 | + |
|
| 460 | + break; |
|
| 461 | + case 'action': |
|
| 462 | + if ( ! $invoice->is_paid() && ! $invoice->is_refunded() ) { |
|
| 463 | + echo '<i class="fa fa-trash wpinv-item-remove"></i>'; |
|
| 464 | 464 | } |
| 465 | - break; |
|
| 465 | + break; |
|
| 466 | 466 | } |
| 467 | 467 | do_action( 'getpaid_admin_edit_invoice_item_' . $column, $item, $invoice ); |
| 468 | 468 | ?> |
@@ -473,10 +473,10 @@ discard block |
||
| 473 | 473 | } |
| 474 | 474 | |
| 475 | 475 | /** |
| 476 | - * Output the metabox. |
|
| 477 | - * |
|
| 478 | - * @param WP_Post $post |
|
| 479 | - */ |
|
| 476 | + * Output the metabox. |
|
| 477 | + * |
|
| 478 | + * @param WP_Post $post |
|
| 479 | + */ |
|
| 480 | 480 | public static function output2( $post ) { |
| 481 | 481 | |
| 482 | 482 | // Prepare the invoice. |
@@ -10,211 +10,211 @@ |
||
| 10 | 10 | */ |
| 11 | 11 | class GetPaid_PayPal_API { |
| 12 | 12 | |
| 13 | - /** |
|
| 14 | - * Retrieves the bearer token. |
|
| 15 | - * |
|
| 13 | + /** |
|
| 14 | + * Retrieves the bearer token. |
|
| 15 | + * |
|
| 16 | 16 | * @return string|\WP_Error |
| 17 | - */ |
|
| 18 | - public static function get_token( $mode = 'live' ) { |
|
| 17 | + */ |
|
| 18 | + public static function get_token( $mode = 'live' ) { |
|
| 19 | 19 | |
| 20 | - $token = get_transient( 'getpaid_paypal_' . $mode . '_token' ); |
|
| 20 | + $token = get_transient( 'getpaid_paypal_' . $mode . '_token' ); |
|
| 21 | 21 | |
| 22 | - if ( $token ) { |
|
| 23 | - return $token; |
|
| 24 | - } |
|
| 22 | + if ( $token ) { |
|
| 23 | + return $token; |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | - $client_id = 'live' === $mode ? wpinv_get_option( 'paypal_client_id' ) : wpinv_get_option( 'paypal_sandbox_client_id' ); |
|
| 27 | - $secret_key = 'live' === $mode ? wpinv_get_option( 'paypal_secret_key' ) : wpinv_get_option( 'paypal_sandbox_secret_key' ); |
|
| 28 | - $url = self::get_api_url( 'v1/oauth2/token?grant_type=client_credentials', $mode ); |
|
| 26 | + $client_id = 'live' === $mode ? wpinv_get_option( 'paypal_client_id' ) : wpinv_get_option( 'paypal_sandbox_client_id' ); |
|
| 27 | + $secret_key = 'live' === $mode ? wpinv_get_option( 'paypal_secret_key' ) : wpinv_get_option( 'paypal_sandbox_secret_key' ); |
|
| 28 | + $url = self::get_api_url( 'v1/oauth2/token?grant_type=client_credentials', $mode ); |
|
| 29 | 29 | |
| 30 | 30 | if ( empty( $client_id ) || empty( $secret_key ) ) { |
| 31 | 31 | return new \WP_Error( 'invalid_request', 'Missing client id or secret key.', array( 'status' => 400 ) ); |
| 32 | 32 | } |
| 33 | 33 | |
| 34 | - $args = array( |
|
| 35 | - 'method' => 'POST', |
|
| 36 | - 'timeout' => 30, |
|
| 37 | - 'headers' => array( |
|
| 38 | - 'Authorization' => 'Basic ' . base64_encode( $client_id . ':' . $secret_key ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode |
|
| 39 | - 'Accept' => 'application/json', |
|
| 40 | - 'Content-Type' => 'application/x-www-form-urlencoded', |
|
| 41 | - ), |
|
| 42 | - ); |
|
| 43 | - |
|
| 44 | - $response = self::response_or_error( wp_remote_post( $url, $args ) ); |
|
| 45 | - |
|
| 46 | - if ( is_wp_error( $response ) ) { |
|
| 47 | - return $response; |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - if ( ! isset( $response->access_token ) ) { |
|
| 51 | - return new \WP_Error( 'invalid_request', 'Could not create token.', array( 'status' => 400 ) ); |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - set_transient( 'getpaid_paypal_' . $mode . '_token', $response->access_token, $response->expires_in - 600 ); |
|
| 55 | - return $response->access_token; |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * Retrieves the PayPal API URL. |
|
| 60 | - * |
|
| 61 | - * @param string $endpoint |
|
| 62 | - * @return string |
|
| 63 | - */ |
|
| 64 | - public static function get_api_url( $endpoint = '', $mode = 'live' ) { |
|
| 65 | - $endpoint = ltrim( $endpoint, '/' ); |
|
| 66 | - return 'live' === $mode ? 'https://api-m.paypal.com/' . $endpoint : 'https://api-m.sandbox.paypal.com/' . $endpoint; |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * Handles a post request. |
|
| 71 | - * |
|
| 72 | - * @param string $path The path to the endpoint. |
|
| 73 | - * @param mixed $data The data to send. |
|
| 74 | - * @param string $method The method to use. |
|
| 75 | - * |
|
| 76 | - * @return true|\WP_Error |
|
| 77 | - */ |
|
| 78 | - public static function post( $path, $data, $mode = 'live', $method = 'POST' ) { |
|
| 79 | - |
|
| 80 | - $access_token = self::get_token( $mode ); |
|
| 81 | - |
|
| 82 | - if ( is_wp_error( $access_token ) ) { |
|
| 83 | - return $access_token; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - $url = self::get_api_url( $path, $mode ); |
|
| 87 | - $args = array( |
|
| 88 | - 'method' => $method, |
|
| 89 | - 'headers' => array( |
|
| 90 | - 'Authorization' => 'Bearer ' . $access_token, |
|
| 91 | - 'Content-Type' => 'application/json', |
|
| 92 | - ), |
|
| 93 | - 'body' => wp_json_encode( $data ), |
|
| 94 | - ); |
|
| 95 | - |
|
| 96 | - return self::response_or_error( wp_remote_post( $url, $args ) ); |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * Handles a get request. |
|
| 101 | - * |
|
| 102 | - * @param string $path The path to the endpoint. |
|
| 103 | - * @param string $method |
|
| 104 | - * @return object|\WP_Error |
|
| 105 | - */ |
|
| 106 | - public static function get( $path, $mode = 'live', $method = 'GET' ) { |
|
| 107 | - |
|
| 108 | - $access_token = self::get_token( $mode ); |
|
| 109 | - |
|
| 110 | - if ( is_wp_error( $access_token ) ) { |
|
| 111 | - return $access_token; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - $url = self::get_api_url( $path, $mode ); |
|
| 115 | - $args = array( |
|
| 116 | - 'method' => $method, |
|
| 117 | - 'headers' => array( |
|
| 118 | - 'Authorization' => 'Bearer ' . $access_token, |
|
| 119 | - ), |
|
| 120 | - ); |
|
| 121 | - |
|
| 122 | - return self::response_or_error( wp_remote_get( $url, $args ) ); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * Returns the response body |
|
| 127 | - * |
|
| 128 | - * @since 1.0.0 |
|
| 129 | - * @version 1.0.0 |
|
| 130 | - * @param \WP_Error|array $response |
|
| 131 | - * @return \WP_Error|object |
|
| 132 | - */ |
|
| 133 | - public static function response_or_error( $response ) { |
|
| 134 | - |
|
| 135 | - if ( is_wp_error( $response ) ) { |
|
| 136 | - return new \WP_Error( 'paypal_error', __( 'There was a problem connecting to the PayPal API endpoint.', 'invoicing' ) ); |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - if ( empty( $response['body'] ) ) { |
|
| 140 | - return true; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - $response_body = json_decode( wp_remote_retrieve_body( $response ) ); |
|
| 144 | - |
|
| 145 | - if ( wp_remote_retrieve_response_code( $response ) > 299 ) { |
|
| 146 | - |
|
| 147 | - // Normal errors. |
|
| 148 | - if ( $response_body && isset( $response_body->message ) ) { |
|
| 149 | - $error_message = $response_body->message; |
|
| 150 | - |
|
| 151 | - // Identity errors. |
|
| 152 | - } elseif ( $response_body && isset( $response_body->error_description ) ) { |
|
| 153 | - $error_message = $response_body->error_description; |
|
| 154 | - return new \WP_Error( 'paypal_error', wp_kses_post( $response_body->error_description ) ); |
|
| 155 | - } else { |
|
| 156 | - $error_message = __( 'There was an error connecting to the PayPal API endpoint.', 'invoicing' ); |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - return new \WP_Error( 'paypal_error', $error_message ); |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - return $response_body; |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - /** |
|
| 166 | - * Fetches an order. |
|
| 167 | - * |
|
| 168 | - * @since 1.0.0 |
|
| 169 | - * @version 1.0.0 |
|
| 170 | - * @param string $order_id |
|
| 171 | - * @link https://developer.paypal.com/docs/api/orders/v2/#orders_get |
|
| 172 | - * @return \WP_Error|object |
|
| 173 | - */ |
|
| 174 | - public static function get_order( $order_id, $mode = 'live' ) { |
|
| 175 | - return self::get( '/v2/checkout/orders/' . $order_id, $mode ); |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * Fetches a subscription. |
|
| 180 | - * |
|
| 181 | - * @since 1.0.0 |
|
| 182 | - * @version 1.0.0 |
|
| 183 | - * @param string $subscription_id |
|
| 184 | - * @link https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_get |
|
| 185 | - * @return \WP_Error|object |
|
| 186 | - */ |
|
| 187 | - public static function get_subscription( $subscription_id, $mode = 'live' ) { |
|
| 188 | - return self::get( '/v1/billing/subscriptions/' . $subscription_id, $mode ); |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - /** |
|
| 192 | - * Fetches a subscription's latest transactions (limits search to last one day). |
|
| 193 | - * |
|
| 194 | - * @since 1.0.0 |
|
| 195 | - * @version 1.0.0 |
|
| 196 | - * @param string $subscription_id |
|
| 197 | - * @link https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_transactions |
|
| 198 | - * @return \WP_Error|object |
|
| 199 | - */ |
|
| 200 | - public static function get_subscription_transaction( $subscription_id, $mode = 'live' ) { |
|
| 201 | - $start_time = gmdate( 'Y-m-d\TH:i:s\Z', strtotime( '-1 day' ) ); |
|
| 202 | - $end_time = gmdate( 'Y-m-d\TH:i:s\Z' ); |
|
| 203 | - return self::get( "/v1/billing/subscriptions/$subscription_id/transactions?start_time=$start_time&end_time=$end_time", $mode ); |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - /** |
|
| 207 | - * Refunds a capture. |
|
| 208 | - * |
|
| 209 | - * @since 1.0.0 |
|
| 210 | - * @version 1.0.0 |
|
| 211 | - * @param string $capture_id |
|
| 212 | - * @param array $args |
|
| 213 | - * @link https://developer.paypal.com/docs/api/payments/v2/#captures_refund |
|
| 214 | - * @return \WP_Error|object |
|
| 215 | - */ |
|
| 216 | - public static function refund_capture( $capture_id, $args = array(), $mode = 'live' ) { |
|
| 217 | - return self::post( '/v2/payments/captures/' . $capture_id . '/refund', $args, $mode ); |
|
| 218 | - } |
|
| 34 | + $args = array( |
|
| 35 | + 'method' => 'POST', |
|
| 36 | + 'timeout' => 30, |
|
| 37 | + 'headers' => array( |
|
| 38 | + 'Authorization' => 'Basic ' . base64_encode( $client_id . ':' . $secret_key ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode |
|
| 39 | + 'Accept' => 'application/json', |
|
| 40 | + 'Content-Type' => 'application/x-www-form-urlencoded', |
|
| 41 | + ), |
|
| 42 | + ); |
|
| 43 | + |
|
| 44 | + $response = self::response_or_error( wp_remote_post( $url, $args ) ); |
|
| 45 | + |
|
| 46 | + if ( is_wp_error( $response ) ) { |
|
| 47 | + return $response; |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + if ( ! isset( $response->access_token ) ) { |
|
| 51 | + return new \WP_Error( 'invalid_request', 'Could not create token.', array( 'status' => 400 ) ); |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + set_transient( 'getpaid_paypal_' . $mode . '_token', $response->access_token, $response->expires_in - 600 ); |
|
| 55 | + return $response->access_token; |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * Retrieves the PayPal API URL. |
|
| 60 | + * |
|
| 61 | + * @param string $endpoint |
|
| 62 | + * @return string |
|
| 63 | + */ |
|
| 64 | + public static function get_api_url( $endpoint = '', $mode = 'live' ) { |
|
| 65 | + $endpoint = ltrim( $endpoint, '/' ); |
|
| 66 | + return 'live' === $mode ? 'https://api-m.paypal.com/' . $endpoint : 'https://api-m.sandbox.paypal.com/' . $endpoint; |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * Handles a post request. |
|
| 71 | + * |
|
| 72 | + * @param string $path The path to the endpoint. |
|
| 73 | + * @param mixed $data The data to send. |
|
| 74 | + * @param string $method The method to use. |
|
| 75 | + * |
|
| 76 | + * @return true|\WP_Error |
|
| 77 | + */ |
|
| 78 | + public static function post( $path, $data, $mode = 'live', $method = 'POST' ) { |
|
| 79 | + |
|
| 80 | + $access_token = self::get_token( $mode ); |
|
| 81 | + |
|
| 82 | + if ( is_wp_error( $access_token ) ) { |
|
| 83 | + return $access_token; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + $url = self::get_api_url( $path, $mode ); |
|
| 87 | + $args = array( |
|
| 88 | + 'method' => $method, |
|
| 89 | + 'headers' => array( |
|
| 90 | + 'Authorization' => 'Bearer ' . $access_token, |
|
| 91 | + 'Content-Type' => 'application/json', |
|
| 92 | + ), |
|
| 93 | + 'body' => wp_json_encode( $data ), |
|
| 94 | + ); |
|
| 95 | + |
|
| 96 | + return self::response_or_error( wp_remote_post( $url, $args ) ); |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * Handles a get request. |
|
| 101 | + * |
|
| 102 | + * @param string $path The path to the endpoint. |
|
| 103 | + * @param string $method |
|
| 104 | + * @return object|\WP_Error |
|
| 105 | + */ |
|
| 106 | + public static function get( $path, $mode = 'live', $method = 'GET' ) { |
|
| 107 | + |
|
| 108 | + $access_token = self::get_token( $mode ); |
|
| 109 | + |
|
| 110 | + if ( is_wp_error( $access_token ) ) { |
|
| 111 | + return $access_token; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + $url = self::get_api_url( $path, $mode ); |
|
| 115 | + $args = array( |
|
| 116 | + 'method' => $method, |
|
| 117 | + 'headers' => array( |
|
| 118 | + 'Authorization' => 'Bearer ' . $access_token, |
|
| 119 | + ), |
|
| 120 | + ); |
|
| 121 | + |
|
| 122 | + return self::response_or_error( wp_remote_get( $url, $args ) ); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * Returns the response body |
|
| 127 | + * |
|
| 128 | + * @since 1.0.0 |
|
| 129 | + * @version 1.0.0 |
|
| 130 | + * @param \WP_Error|array $response |
|
| 131 | + * @return \WP_Error|object |
|
| 132 | + */ |
|
| 133 | + public static function response_or_error( $response ) { |
|
| 134 | + |
|
| 135 | + if ( is_wp_error( $response ) ) { |
|
| 136 | + return new \WP_Error( 'paypal_error', __( 'There was a problem connecting to the PayPal API endpoint.', 'invoicing' ) ); |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + if ( empty( $response['body'] ) ) { |
|
| 140 | + return true; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + $response_body = json_decode( wp_remote_retrieve_body( $response ) ); |
|
| 144 | + |
|
| 145 | + if ( wp_remote_retrieve_response_code( $response ) > 299 ) { |
|
| 146 | + |
|
| 147 | + // Normal errors. |
|
| 148 | + if ( $response_body && isset( $response_body->message ) ) { |
|
| 149 | + $error_message = $response_body->message; |
|
| 150 | + |
|
| 151 | + // Identity errors. |
|
| 152 | + } elseif ( $response_body && isset( $response_body->error_description ) ) { |
|
| 153 | + $error_message = $response_body->error_description; |
|
| 154 | + return new \WP_Error( 'paypal_error', wp_kses_post( $response_body->error_description ) ); |
|
| 155 | + } else { |
|
| 156 | + $error_message = __( 'There was an error connecting to the PayPal API endpoint.', 'invoicing' ); |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + return new \WP_Error( 'paypal_error', $error_message ); |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + return $response_body; |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + /** |
|
| 166 | + * Fetches an order. |
|
| 167 | + * |
|
| 168 | + * @since 1.0.0 |
|
| 169 | + * @version 1.0.0 |
|
| 170 | + * @param string $order_id |
|
| 171 | + * @link https://developer.paypal.com/docs/api/orders/v2/#orders_get |
|
| 172 | + * @return \WP_Error|object |
|
| 173 | + */ |
|
| 174 | + public static function get_order( $order_id, $mode = 'live' ) { |
|
| 175 | + return self::get( '/v2/checkout/orders/' . $order_id, $mode ); |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * Fetches a subscription. |
|
| 180 | + * |
|
| 181 | + * @since 1.0.0 |
|
| 182 | + * @version 1.0.0 |
|
| 183 | + * @param string $subscription_id |
|
| 184 | + * @link https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_get |
|
| 185 | + * @return \WP_Error|object |
|
| 186 | + */ |
|
| 187 | + public static function get_subscription( $subscription_id, $mode = 'live' ) { |
|
| 188 | + return self::get( '/v1/billing/subscriptions/' . $subscription_id, $mode ); |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + /** |
|
| 192 | + * Fetches a subscription's latest transactions (limits search to last one day). |
|
| 193 | + * |
|
| 194 | + * @since 1.0.0 |
|
| 195 | + * @version 1.0.0 |
|
| 196 | + * @param string $subscription_id |
|
| 197 | + * @link https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_transactions |
|
| 198 | + * @return \WP_Error|object |
|
| 199 | + */ |
|
| 200 | + public static function get_subscription_transaction( $subscription_id, $mode = 'live' ) { |
|
| 201 | + $start_time = gmdate( 'Y-m-d\TH:i:s\Z', strtotime( '-1 day' ) ); |
|
| 202 | + $end_time = gmdate( 'Y-m-d\TH:i:s\Z' ); |
|
| 203 | + return self::get( "/v1/billing/subscriptions/$subscription_id/transactions?start_time=$start_time&end_time=$end_time", $mode ); |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + /** |
|
| 207 | + * Refunds a capture. |
|
| 208 | + * |
|
| 209 | + * @since 1.0.0 |
|
| 210 | + * @version 1.0.0 |
|
| 211 | + * @param string $capture_id |
|
| 212 | + * @param array $args |
|
| 213 | + * @link https://developer.paypal.com/docs/api/payments/v2/#captures_refund |
|
| 214 | + * @return \WP_Error|object |
|
| 215 | + */ |
|
| 216 | + public static function refund_capture( $capture_id, $args = array(), $mode = 'live' ) { |
|
| 217 | + return self::post( '/v2/payments/captures/' . $capture_id . '/refund', $args, $mode ); |
|
| 218 | + } |
|
| 219 | 219 | |
| 220 | 220 | } |
@@ -13,199 +13,199 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class GetPaid_Tax { |
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Calculates tax for a line item. |
|
| 18 | - * |
|
| 19 | - * @param float $price The price to calc tax on. |
|
| 20 | - * @param array $rates The rates to apply. |
|
| 21 | - * @param boolean $price_includes_tax Whether the passed price has taxes included. |
|
| 22 | - * @return array Array of tax name => tax amount. |
|
| 23 | - */ |
|
| 24 | - public static function calc_tax( $price, $rates, $price_includes_tax = false ) { |
|
| 25 | - |
|
| 26 | - if ( $price_includes_tax ) { |
|
| 27 | - $taxes = self::calc_inclusive_tax( $price, $rates ); |
|
| 28 | - } else { |
|
| 29 | - $taxes = self::calc_exclusive_tax( $price, $rates ); |
|
| 30 | - } |
|
| 31 | - |
|
| 32 | - return apply_filters( 'getpaid_calc_tax', $taxes, $price, $rates, $price_includes_tax ); |
|
| 33 | - |
|
| 34 | - } |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * Calc tax from inclusive price. |
|
| 38 | - * |
|
| 39 | - * @param float $price Price to calculate tax for. |
|
| 40 | - * @param array $rates Array of tax rates. |
|
| 41 | - * @return array |
|
| 42 | - */ |
|
| 43 | - public static function calc_inclusive_tax( $price, $rates ) { |
|
| 44 | - $taxes = array(); |
|
| 45 | - $tax_rates = wp_list_pluck( $rates, 'rate', 'name' ); |
|
| 46 | - |
|
| 47 | - // Add tax rates. |
|
| 48 | - $tax_rate = 1 + ( array_sum( $tax_rates ) / 100 ); |
|
| 49 | - |
|
| 50 | - foreach ( $tax_rates as $name => $rate ) { |
|
| 51 | - $the_rate = ( $rate / 100 ) / $tax_rate; |
|
| 52 | - $net_price = $price - ( $the_rate * $price ); |
|
| 53 | - $tax_amount = apply_filters( 'getpaid_price_inc_tax_amount', $price - $net_price, $name, $rate, $price ); |
|
| 54 | - $taxes[ $name ] = $tax_amount; |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - // Round all taxes to precision (4DP) before passing them back. |
|
| 58 | - $taxes = array_map( array( __CLASS__, 'round' ), $taxes ); |
|
| 59 | - |
|
| 60 | - return $taxes; |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * Calc tax from exclusive price. |
|
| 65 | - * |
|
| 66 | - * @param float $price Price to calculate tax for. |
|
| 67 | - * @param array $rates Array of tax rates. |
|
| 68 | - * @return array |
|
| 69 | - */ |
|
| 70 | - public static function calc_exclusive_tax( $price, $rates ) { |
|
| 71 | - $taxes = array(); |
|
| 72 | - $tax_rates = wp_list_pluck( $rates, 'rate', 'name' ); |
|
| 73 | - |
|
| 74 | - foreach ( $tax_rates as $name => $rate ) { |
|
| 75 | - |
|
| 76 | - $tax_amount = $price * ( $rate / 100 ); |
|
| 77 | - $taxes[ $name ] = apply_filters( 'getpaid_price_ex_tax_amount', $tax_amount, $name, $rate, $price ); |
|
| 78 | - |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - // Round all taxes to precision (4DP) before passing them back. |
|
| 82 | - $taxes = array_map( array( __CLASS__, 'round' ), $taxes ); |
|
| 83 | - |
|
| 84 | - return $taxes; |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * Get's an array of all tax rates. |
|
| 89 | - * |
|
| 90 | - * @return array |
|
| 91 | - */ |
|
| 92 | - public static function get_all_tax_rates() { |
|
| 93 | - |
|
| 94 | - $rates = get_option( 'wpinv_tax_rates', array() ); |
|
| 95 | - |
|
| 96 | - return apply_filters( |
|
| 97 | - 'getpaid_get_all_tax_rates', |
|
| 98 | - array_filter( wpinv_parse_list( $rates ) ) |
|
| 99 | - ); |
|
| 100 | - |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * Get's an array of default tax rates. |
|
| 105 | - * |
|
| 106 | - * @return array |
|
| 107 | - */ |
|
| 108 | - public static function get_default_tax_rates() { |
|
| 109 | - |
|
| 110 | - return apply_filters( |
|
| 111 | - 'getpaid_get_default_tax_rates', |
|
| 112 | - array( |
|
| 113 | - array( |
|
| 114 | - 'country' => wpinv_get_default_country(), |
|
| 115 | - 'state' => wpinv_get_default_state(), |
|
| 116 | - 'global' => true, |
|
| 117 | - 'rate' => wpinv_get_default_tax_rate(), |
|
| 118 | - 'name' => __( 'Tax', 'invoicing' ), |
|
| 119 | - ), |
|
| 120 | - ) |
|
| 121 | - ); |
|
| 122 | - |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * Get's an array of all tax rules. |
|
| 127 | - * |
|
| 128 | - * @return array |
|
| 129 | - */ |
|
| 130 | - public static function get_all_tax_rules() { |
|
| 131 | - |
|
| 132 | - $rules = get_option( |
|
| 133 | - 'wpinv_tax_rules', |
|
| 134 | - array( |
|
| 135 | - array( |
|
| 136 | - 'key' => 'physical', |
|
| 137 | - 'label' => __( 'Physical Item', 'invoicing' ), |
|
| 138 | - 'tax_base' => wpinv_get_option( 'tax_base', 'billing' ), |
|
| 139 | - 'same_country_rule' => wpinv_get_option( 'vat_same_country_rule', 'vat_too' ), |
|
| 140 | - ), |
|
| 141 | - array( |
|
| 142 | - 'key' => 'digital', |
|
| 143 | - 'label' => __( 'Digital Item', 'invoicing' ), |
|
| 144 | - 'tax_base' => wpinv_get_option( 'tax_base', 'billing' ), |
|
| 145 | - 'same_country_rule' => wpinv_get_option( 'vat_same_country_rule', 'vat_too' ), |
|
| 146 | - ), |
|
| 147 | - ) |
|
| 148 | - ); |
|
| 149 | - |
|
| 150 | - return apply_filters( |
|
| 151 | - 'getpaid_tax_rules', |
|
| 152 | - array_filter( array_values( wpinv_parse_list( $rules ) ) ) |
|
| 153 | - ); |
|
| 154 | - |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - /** |
|
| 158 | - * Get's an array of tax rates for a given address. |
|
| 159 | - * |
|
| 160 | - * @param string $country |
|
| 161 | - * @param string $state |
|
| 162 | - * @return array |
|
| 163 | - */ |
|
| 164 | - public static function get_address_tax_rates( $country, $state ) { |
|
| 165 | - |
|
| 166 | - $all_tax_rates = self::get_all_tax_rates(); |
|
| 167 | - $matching_rates = array_merge( |
|
| 168 | - wp_list_filter( $all_tax_rates, array( 'country' => $country ) ), |
|
| 169 | - wp_list_filter( $all_tax_rates, array( 'country' => '' ) ) |
|
| 170 | - ); |
|
| 171 | - |
|
| 172 | - foreach ( $matching_rates as $i => $rate ) { |
|
| 173 | - |
|
| 174 | - $states = array_filter( wpinv_clean( explode( ',', strtolower( $rate['state'] ) ) ) ); |
|
| 175 | - if ( empty( $rate['global'] ) && ! in_array( strtolower( $state ), $states ) ) { |
|
| 176 | - unset( $matching_rates[ $i ] ); |
|
| 177 | - } |
|
| 16 | + /** |
|
| 17 | + * Calculates tax for a line item. |
|
| 18 | + * |
|
| 19 | + * @param float $price The price to calc tax on. |
|
| 20 | + * @param array $rates The rates to apply. |
|
| 21 | + * @param boolean $price_includes_tax Whether the passed price has taxes included. |
|
| 22 | + * @return array Array of tax name => tax amount. |
|
| 23 | + */ |
|
| 24 | + public static function calc_tax( $price, $rates, $price_includes_tax = false ) { |
|
| 25 | + |
|
| 26 | + if ( $price_includes_tax ) { |
|
| 27 | + $taxes = self::calc_inclusive_tax( $price, $rates ); |
|
| 28 | + } else { |
|
| 29 | + $taxes = self::calc_exclusive_tax( $price, $rates ); |
|
| 30 | + } |
|
| 31 | + |
|
| 32 | + return apply_filters( 'getpaid_calc_tax', $taxes, $price, $rates, $price_includes_tax ); |
|
| 33 | + |
|
| 34 | + } |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * Calc tax from inclusive price. |
|
| 38 | + * |
|
| 39 | + * @param float $price Price to calculate tax for. |
|
| 40 | + * @param array $rates Array of tax rates. |
|
| 41 | + * @return array |
|
| 42 | + */ |
|
| 43 | + public static function calc_inclusive_tax( $price, $rates ) { |
|
| 44 | + $taxes = array(); |
|
| 45 | + $tax_rates = wp_list_pluck( $rates, 'rate', 'name' ); |
|
| 46 | + |
|
| 47 | + // Add tax rates. |
|
| 48 | + $tax_rate = 1 + ( array_sum( $tax_rates ) / 100 ); |
|
| 49 | + |
|
| 50 | + foreach ( $tax_rates as $name => $rate ) { |
|
| 51 | + $the_rate = ( $rate / 100 ) / $tax_rate; |
|
| 52 | + $net_price = $price - ( $the_rate * $price ); |
|
| 53 | + $tax_amount = apply_filters( 'getpaid_price_inc_tax_amount', $price - $net_price, $name, $rate, $price ); |
|
| 54 | + $taxes[ $name ] = $tax_amount; |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + // Round all taxes to precision (4DP) before passing them back. |
|
| 58 | + $taxes = array_map( array( __CLASS__, 'round' ), $taxes ); |
|
| 59 | + |
|
| 60 | + return $taxes; |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * Calc tax from exclusive price. |
|
| 65 | + * |
|
| 66 | + * @param float $price Price to calculate tax for. |
|
| 67 | + * @param array $rates Array of tax rates. |
|
| 68 | + * @return array |
|
| 69 | + */ |
|
| 70 | + public static function calc_exclusive_tax( $price, $rates ) { |
|
| 71 | + $taxes = array(); |
|
| 72 | + $tax_rates = wp_list_pluck( $rates, 'rate', 'name' ); |
|
| 73 | + |
|
| 74 | + foreach ( $tax_rates as $name => $rate ) { |
|
| 75 | + |
|
| 76 | + $tax_amount = $price * ( $rate / 100 ); |
|
| 77 | + $taxes[ $name ] = apply_filters( 'getpaid_price_ex_tax_amount', $tax_amount, $name, $rate, $price ); |
|
| 78 | + |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + // Round all taxes to precision (4DP) before passing them back. |
|
| 82 | + $taxes = array_map( array( __CLASS__, 'round' ), $taxes ); |
|
| 83 | + |
|
| 84 | + return $taxes; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * Get's an array of all tax rates. |
|
| 89 | + * |
|
| 90 | + * @return array |
|
| 91 | + */ |
|
| 92 | + public static function get_all_tax_rates() { |
|
| 93 | + |
|
| 94 | + $rates = get_option( 'wpinv_tax_rates', array() ); |
|
| 95 | + |
|
| 96 | + return apply_filters( |
|
| 97 | + 'getpaid_get_all_tax_rates', |
|
| 98 | + array_filter( wpinv_parse_list( $rates ) ) |
|
| 99 | + ); |
|
| 100 | + |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * Get's an array of default tax rates. |
|
| 105 | + * |
|
| 106 | + * @return array |
|
| 107 | + */ |
|
| 108 | + public static function get_default_tax_rates() { |
|
| 109 | + |
|
| 110 | + return apply_filters( |
|
| 111 | + 'getpaid_get_default_tax_rates', |
|
| 112 | + array( |
|
| 113 | + array( |
|
| 114 | + 'country' => wpinv_get_default_country(), |
|
| 115 | + 'state' => wpinv_get_default_state(), |
|
| 116 | + 'global' => true, |
|
| 117 | + 'rate' => wpinv_get_default_tax_rate(), |
|
| 118 | + 'name' => __( 'Tax', 'invoicing' ), |
|
| 119 | + ), |
|
| 120 | + ) |
|
| 121 | + ); |
|
| 122 | + |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * Get's an array of all tax rules. |
|
| 127 | + * |
|
| 128 | + * @return array |
|
| 129 | + */ |
|
| 130 | + public static function get_all_tax_rules() { |
|
| 131 | + |
|
| 132 | + $rules = get_option( |
|
| 133 | + 'wpinv_tax_rules', |
|
| 134 | + array( |
|
| 135 | + array( |
|
| 136 | + 'key' => 'physical', |
|
| 137 | + 'label' => __( 'Physical Item', 'invoicing' ), |
|
| 138 | + 'tax_base' => wpinv_get_option( 'tax_base', 'billing' ), |
|
| 139 | + 'same_country_rule' => wpinv_get_option( 'vat_same_country_rule', 'vat_too' ), |
|
| 140 | + ), |
|
| 141 | + array( |
|
| 142 | + 'key' => 'digital', |
|
| 143 | + 'label' => __( 'Digital Item', 'invoicing' ), |
|
| 144 | + 'tax_base' => wpinv_get_option( 'tax_base', 'billing' ), |
|
| 145 | + 'same_country_rule' => wpinv_get_option( 'vat_same_country_rule', 'vat_too' ), |
|
| 146 | + ), |
|
| 147 | + ) |
|
| 148 | + ); |
|
| 149 | + |
|
| 150 | + return apply_filters( |
|
| 151 | + 'getpaid_tax_rules', |
|
| 152 | + array_filter( array_values( wpinv_parse_list( $rules ) ) ) |
|
| 153 | + ); |
|
| 154 | + |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + /** |
|
| 158 | + * Get's an array of tax rates for a given address. |
|
| 159 | + * |
|
| 160 | + * @param string $country |
|
| 161 | + * @param string $state |
|
| 162 | + * @return array |
|
| 163 | + */ |
|
| 164 | + public static function get_address_tax_rates( $country, $state ) { |
|
| 165 | + |
|
| 166 | + $all_tax_rates = self::get_all_tax_rates(); |
|
| 167 | + $matching_rates = array_merge( |
|
| 168 | + wp_list_filter( $all_tax_rates, array( 'country' => $country ) ), |
|
| 169 | + wp_list_filter( $all_tax_rates, array( 'country' => '' ) ) |
|
| 170 | + ); |
|
| 171 | + |
|
| 172 | + foreach ( $matching_rates as $i => $rate ) { |
|
| 173 | + |
|
| 174 | + $states = array_filter( wpinv_clean( explode( ',', strtolower( $rate['state'] ) ) ) ); |
|
| 175 | + if ( empty( $rate['global'] ) && ! in_array( strtolower( $state ), $states ) ) { |
|
| 176 | + unset( $matching_rates[ $i ] ); |
|
| 177 | + } |
|
| 178 | 178 | } |
| 179 | 179 | |
| 180 | - return apply_filters( 'getpaid_get_address_tax_rates', $matching_rates, $country, $state ); |
|
| 181 | - |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - /** |
|
| 185 | - * Sums a set of taxes to form a single total. Result is rounded to precision. |
|
| 186 | - * |
|
| 187 | - * @param array $taxes Array of taxes. |
|
| 188 | - * @return float |
|
| 189 | - */ |
|
| 190 | - public static function get_tax_total( $taxes ) { |
|
| 191 | - return self::round( array_sum( $taxes ) ); |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - /** |
|
| 195 | - * Round to precision. |
|
| 196 | - * |
|
| 197 | - * Filter example: to return rounding to .5 cents you'd use: |
|
| 198 | - * |
|
| 199 | - * function euro_5cent_rounding( $in ) { |
|
| 200 | - * return round( $in / 5, 2 ) * 5; |
|
| 201 | - * } |
|
| 202 | - * add_filter( 'getpaid_tax_round', 'euro_5cent_rounding' ); |
|
| 203 | - * |
|
| 204 | - * @param float|int $in Value to round. |
|
| 205 | - * @return float |
|
| 206 | - */ |
|
| 207 | - public static function round( $in ) { |
|
| 208 | - return apply_filters( 'getpaid_tax_round', round( $in, 4 ), $in ); |
|
| 209 | - } |
|
| 180 | + return apply_filters( 'getpaid_get_address_tax_rates', $matching_rates, $country, $state ); |
|
| 181 | + |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + /** |
|
| 185 | + * Sums a set of taxes to form a single total. Result is rounded to precision. |
|
| 186 | + * |
|
| 187 | + * @param array $taxes Array of taxes. |
|
| 188 | + * @return float |
|
| 189 | + */ |
|
| 190 | + public static function get_tax_total( $taxes ) { |
|
| 191 | + return self::round( array_sum( $taxes ) ); |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + /** |
|
| 195 | + * Round to precision. |
|
| 196 | + * |
|
| 197 | + * Filter example: to return rounding to .5 cents you'd use: |
|
| 198 | + * |
|
| 199 | + * function euro_5cent_rounding( $in ) { |
|
| 200 | + * return round( $in / 5, 2 ) * 5; |
|
| 201 | + * } |
|
| 202 | + * add_filter( 'getpaid_tax_round', 'euro_5cent_rounding' ); |
|
| 203 | + * |
|
| 204 | + * @param float|int $in Value to round. |
|
| 205 | + * @return float |
|
| 206 | + */ |
|
| 207 | + public static function round( $in ) { |
|
| 208 | + return apply_filters( 'getpaid_tax_round', round( $in, 4 ), $in ); |
|
| 209 | + } |
|
| 210 | 210 | |
| 211 | 211 | } |
@@ -12,301 +12,301 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class GetPaid_Payment_Form_Submission_Refresh_Prices { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * Contains the response for refreshing prices. |
|
| 17 | - * @var array |
|
| 18 | - */ |
|
| 19 | - public $response = array(); |
|
| 15 | + /** |
|
| 16 | + * Contains the response for refreshing prices. |
|
| 17 | + * @var array |
|
| 18 | + */ |
|
| 19 | + public $response = array(); |
|
| 20 | 20 | |
| 21 | 21 | /** |
| 22 | - * Class constructor |
|
| 23 | - * |
|
| 24 | - * @param GetPaid_Payment_Form_Submission $submission |
|
| 25 | - */ |
|
| 26 | - public function __construct( $submission ) { |
|
| 27 | - |
|
| 28 | - $this->response = array( |
|
| 29 | - 'submission_id' => $submission->id, |
|
| 22 | + * Class constructor |
|
| 23 | + * |
|
| 24 | + * @param GetPaid_Payment_Form_Submission $submission |
|
| 25 | + */ |
|
| 26 | + public function __construct( $submission ) { |
|
| 27 | + |
|
| 28 | + $this->response = array( |
|
| 29 | + 'submission_id' => $submission->id, |
|
| 30 | 30 | 'has_recurring' => $submission->has_recurring, |
| 31 | - 'has_subscription_group' => $submission->has_subscription_group(), |
|
| 32 | - 'has_multiple_subscription_groups' => $submission->has_multiple_subscription_groups(), |
|
| 31 | + 'has_subscription_group' => $submission->has_subscription_group(), |
|
| 32 | + 'has_multiple_subscription_groups' => $submission->has_multiple_subscription_groups(), |
|
| 33 | 33 | 'is_free' => ! $submission->should_collect_payment_details(), |
| 34 | - ); |
|
| 35 | - |
|
| 36 | - $payment_form = $submission->get_payment_form(); |
|
| 37 | - if ( ! empty( $payment_form->invoice ) ) { |
|
| 38 | - $this->response['invoice'] = $payment_form->invoice->get_id(); |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - $this->add_totals( $submission ); |
|
| 42 | - $this->add_texts( $submission ); |
|
| 43 | - $this->add_items( $submission ); |
|
| 44 | - $this->add_fees( $submission ); |
|
| 45 | - $this->add_discounts( $submission ); |
|
| 46 | - $this->add_taxes( $submission ); |
|
| 47 | - $this->add_gateways( $submission ); |
|
| 48 | - $this->add_data( $submission ); |
|
| 49 | - |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * Adds totals to a response for submission refresh prices. |
|
| 54 | - * |
|
| 55 | - * @param GetPaid_Payment_Form_Submission $submission |
|
| 56 | - */ |
|
| 57 | - public function add_totals( $submission ) { |
|
| 58 | - |
|
| 59 | - $this->response = array_merge( |
|
| 60 | - $this->response, |
|
| 61 | - array( |
|
| 62 | - |
|
| 63 | - 'totals' => array( |
|
| 64 | - 'subtotal' => $submission->format_amount( $submission->get_subtotal() ), |
|
| 65 | - 'discount' => $submission->format_amount( $submission->get_discount() ), |
|
| 66 | - 'fees' => $submission->format_amount( $submission->get_fee() ), |
|
| 67 | - 'tax' => $submission->format_amount( $submission->get_tax() ), |
|
| 68 | - 'total' => $submission->format_amount( $submission->get_total() ), |
|
| 69 | - 'raw_total' => html_entity_decode( sanitize_text_field( $submission->format_amount( $submission->get_total() ) ), ENT_QUOTES ), |
|
| 70 | - ), |
|
| 71 | - |
|
| 72 | - 'recurring' => array( |
|
| 73 | - 'subtotal' => $submission->format_amount( $submission->get_recurring_subtotal() ), |
|
| 74 | - 'discount' => $submission->format_amount( $submission->get_recurring_discount() ), |
|
| 75 | - 'fees' => $submission->format_amount( $submission->get_recurring_fee() ), |
|
| 76 | - 'tax' => $submission->format_amount( $submission->get_recurring_tax() ), |
|
| 77 | - 'total' => $submission->format_amount( $submission->get_recurring_total() ), |
|
| 78 | - ), |
|
| 79 | - |
|
| 80 | - 'initial_amt' => wpinv_round_amount( $submission->get_total(), null, true ), |
|
| 81 | - 'currency' => $submission->get_currency(), |
|
| 82 | - |
|
| 83 | - ) |
|
| 84 | - ); |
|
| 85 | - |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * Adds texts to a response for submission refresh prices. |
|
| 90 | - * |
|
| 91 | - * @param GetPaid_Payment_Form_Submission $submission |
|
| 92 | - */ |
|
| 93 | - public function add_texts( $submission ) { |
|
| 94 | - |
|
| 95 | - $payable = $submission->format_amount( $submission->get_total() ); |
|
| 96 | - $groups = getpaid_get_subscription_groups( $submission ); |
|
| 97 | - |
|
| 98 | - if ( $submission->has_recurring && 2 > count( $groups ) ) { |
|
| 99 | - |
|
| 100 | - $recurring = new WPInv_Item( $submission->has_recurring ); |
|
| 101 | - $period = getpaid_get_subscription_period_label( $recurring->get_recurring_period( true ), $recurring->get_recurring_interval(), '' ); |
|
| 102 | - $main_item = reset( $groups ); |
|
| 103 | - |
|
| 104 | - if ( $submission->get_total() == $submission->get_recurring_total() ) { |
|
| 105 | - $payable = "$payable / $period"; |
|
| 106 | - } elseif ( $main_item ) { |
|
| 107 | - |
|
| 108 | - $main_item = reset( $main_item ); |
|
| 109 | - |
|
| 110 | - // Calculate the next renewal date. |
|
| 111 | - $_period = $main_item->get_recurring_period( true ); |
|
| 112 | - $_interval = $main_item->get_recurring_interval(); |
|
| 113 | - |
|
| 114 | - // If the subscription item has a trial period... |
|
| 115 | - if ( $main_item->has_free_trial() ) { |
|
| 116 | - $_period = $main_item->get_trial_period( true ); |
|
| 117 | - $_interval = $main_item->get_trial_interval(); |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - $payable = sprintf( |
|
| 121 | - __( '%1$s (renews at %2$s / %3$s)', 'invoicing' ), |
|
| 122 | - $submission->format_amount( $submission->get_total() ), |
|
| 123 | - $submission->format_amount( $submission->get_recurring_total() ), |
|
| 124 | - $period |
|
| 125 | - ); |
|
| 126 | - |
|
| 127 | - $payable .= sprintf( |
|
| 128 | - '<small class="text-muted form-text">%s</small>', |
|
| 129 | - sprintf( |
|
| 130 | - __( 'First renewal on %s', 'invoicing' ), |
|
| 131 | - getpaid_format_date( date( 'Y-m-d H:i:s', strtotime( "+$_interval $_period", current_time( 'timestamp' ) ) ) ) |
|
| 132 | - ) |
|
| 133 | - ); |
|
| 134 | - |
|
| 135 | - } else { |
|
| 136 | - $payable = sprintf( |
|
| 137 | - __( '%1$s (renews at %2$s / %3$s)', 'invoicing' ), |
|
| 138 | - $submission->format_amount( $submission->get_total() ), |
|
| 139 | - $submission->format_amount( $submission->get_recurring_total() ), |
|
| 140 | - $period |
|
| 141 | - ); |
|
| 142 | - } |
|
| 143 | -} |
|
| 34 | + ); |
|
| 35 | + |
|
| 36 | + $payment_form = $submission->get_payment_form(); |
|
| 37 | + if ( ! empty( $payment_form->invoice ) ) { |
|
| 38 | + $this->response['invoice'] = $payment_form->invoice->get_id(); |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + $this->add_totals( $submission ); |
|
| 42 | + $this->add_texts( $submission ); |
|
| 43 | + $this->add_items( $submission ); |
|
| 44 | + $this->add_fees( $submission ); |
|
| 45 | + $this->add_discounts( $submission ); |
|
| 46 | + $this->add_taxes( $submission ); |
|
| 47 | + $this->add_gateways( $submission ); |
|
| 48 | + $this->add_data( $submission ); |
|
| 49 | + |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * Adds totals to a response for submission refresh prices. |
|
| 54 | + * |
|
| 55 | + * @param GetPaid_Payment_Form_Submission $submission |
|
| 56 | + */ |
|
| 57 | + public function add_totals( $submission ) { |
|
| 58 | + |
|
| 59 | + $this->response = array_merge( |
|
| 60 | + $this->response, |
|
| 61 | + array( |
|
| 62 | + |
|
| 63 | + 'totals' => array( |
|
| 64 | + 'subtotal' => $submission->format_amount( $submission->get_subtotal() ), |
|
| 65 | + 'discount' => $submission->format_amount( $submission->get_discount() ), |
|
| 66 | + 'fees' => $submission->format_amount( $submission->get_fee() ), |
|
| 67 | + 'tax' => $submission->format_amount( $submission->get_tax() ), |
|
| 68 | + 'total' => $submission->format_amount( $submission->get_total() ), |
|
| 69 | + 'raw_total' => html_entity_decode( sanitize_text_field( $submission->format_amount( $submission->get_total() ) ), ENT_QUOTES ), |
|
| 70 | + ), |
|
| 71 | + |
|
| 72 | + 'recurring' => array( |
|
| 73 | + 'subtotal' => $submission->format_amount( $submission->get_recurring_subtotal() ), |
|
| 74 | + 'discount' => $submission->format_amount( $submission->get_recurring_discount() ), |
|
| 75 | + 'fees' => $submission->format_amount( $submission->get_recurring_fee() ), |
|
| 76 | + 'tax' => $submission->format_amount( $submission->get_recurring_tax() ), |
|
| 77 | + 'total' => $submission->format_amount( $submission->get_recurring_total() ), |
|
| 78 | + ), |
|
| 79 | + |
|
| 80 | + 'initial_amt' => wpinv_round_amount( $submission->get_total(), null, true ), |
|
| 81 | + 'currency' => $submission->get_currency(), |
|
| 82 | + |
|
| 83 | + ) |
|
| 84 | + ); |
|
| 85 | + |
|
| 86 | + } |
|
| 144 | 87 | |
| 145 | - $texts = array( |
|
| 146 | - '.getpaid-checkout-total-payable' => $payable, |
|
| 147 | - ); |
|
| 88 | + /** |
|
| 89 | + * Adds texts to a response for submission refresh prices. |
|
| 90 | + * |
|
| 91 | + * @param GetPaid_Payment_Form_Submission $submission |
|
| 92 | + */ |
|
| 93 | + public function add_texts( $submission ) { |
|
| 94 | + |
|
| 95 | + $payable = $submission->format_amount( $submission->get_total() ); |
|
| 96 | + $groups = getpaid_get_subscription_groups( $submission ); |
|
| 97 | + |
|
| 98 | + if ( $submission->has_recurring && 2 > count( $groups ) ) { |
|
| 99 | + |
|
| 100 | + $recurring = new WPInv_Item( $submission->has_recurring ); |
|
| 101 | + $period = getpaid_get_subscription_period_label( $recurring->get_recurring_period( true ), $recurring->get_recurring_interval(), '' ); |
|
| 102 | + $main_item = reset( $groups ); |
|
| 103 | + |
|
| 104 | + if ( $submission->get_total() == $submission->get_recurring_total() ) { |
|
| 105 | + $payable = "$payable / $period"; |
|
| 106 | + } elseif ( $main_item ) { |
|
| 107 | + |
|
| 108 | + $main_item = reset( $main_item ); |
|
| 109 | + |
|
| 110 | + // Calculate the next renewal date. |
|
| 111 | + $_period = $main_item->get_recurring_period( true ); |
|
| 112 | + $_interval = $main_item->get_recurring_interval(); |
|
| 113 | + |
|
| 114 | + // If the subscription item has a trial period... |
|
| 115 | + if ( $main_item->has_free_trial() ) { |
|
| 116 | + $_period = $main_item->get_trial_period( true ); |
|
| 117 | + $_interval = $main_item->get_trial_interval(); |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + $payable = sprintf( |
|
| 121 | + __( '%1$s (renews at %2$s / %3$s)', 'invoicing' ), |
|
| 122 | + $submission->format_amount( $submission->get_total() ), |
|
| 123 | + $submission->format_amount( $submission->get_recurring_total() ), |
|
| 124 | + $period |
|
| 125 | + ); |
|
| 126 | + |
|
| 127 | + $payable .= sprintf( |
|
| 128 | + '<small class="text-muted form-text">%s</small>', |
|
| 129 | + sprintf( |
|
| 130 | + __( 'First renewal on %s', 'invoicing' ), |
|
| 131 | + getpaid_format_date( date( 'Y-m-d H:i:s', strtotime( "+$_interval $_period", current_time( 'timestamp' ) ) ) ) |
|
| 132 | + ) |
|
| 133 | + ); |
|
| 134 | + |
|
| 135 | + } else { |
|
| 136 | + $payable = sprintf( |
|
| 137 | + __( '%1$s (renews at %2$s / %3$s)', 'invoicing' ), |
|
| 138 | + $submission->format_amount( $submission->get_total() ), |
|
| 139 | + $submission->format_amount( $submission->get_recurring_total() ), |
|
| 140 | + $period |
|
| 141 | + ); |
|
| 142 | + } |
|
| 143 | +} |
|
| 148 | 144 | |
| 149 | - foreach ( $submission->get_items() as $item ) { |
|
| 150 | - $item_id = $item->get_id(); |
|
| 151 | - $initial_price = $submission->format_amount( $item->get_sub_total() - $item->item_discount ); |
|
| 152 | - $recurring_price = $submission->format_amount( $item->get_recurring_sub_total() - $item->recurring_item_discount ); |
|
| 153 | - $texts[ ".item-$item_id .getpaid-form-item-price-desc" ] = getpaid_item_recurring_price_help_text( $item, $submission->get_currency(), $initial_price, $recurring_price ); |
|
| 154 | - $texts[ ".item-$item_id .getpaid-mobile-item-subtotal" ] = sprintf( __( 'Subtotal: %s', 'invoicing' ), $submission->format_amount( $item->get_sub_total() ) ); |
|
| 145 | + $texts = array( |
|
| 146 | + '.getpaid-checkout-total-payable' => $payable, |
|
| 147 | + ); |
|
| 155 | 148 | |
| 156 | - if ( $item->get_quantity() == 1 ) { |
|
| 157 | - $texts[ ".item-$item_id .getpaid-mobile-item-subtotal" ] = ''; |
|
| 158 | - } |
|
| 149 | + foreach ( $submission->get_items() as $item ) { |
|
| 150 | + $item_id = $item->get_id(); |
|
| 151 | + $initial_price = $submission->format_amount( $item->get_sub_total() - $item->item_discount ); |
|
| 152 | + $recurring_price = $submission->format_amount( $item->get_recurring_sub_total() - $item->recurring_item_discount ); |
|
| 153 | + $texts[ ".item-$item_id .getpaid-form-item-price-desc" ] = getpaid_item_recurring_price_help_text( $item, $submission->get_currency(), $initial_price, $recurring_price ); |
|
| 154 | + $texts[ ".item-$item_id .getpaid-mobile-item-subtotal" ] = sprintf( __( 'Subtotal: %s', 'invoicing' ), $submission->format_amount( $item->get_sub_total() ) ); |
|
| 155 | + |
|
| 156 | + if ( $item->get_quantity() == 1 ) { |
|
| 157 | + $texts[ ".item-$item_id .getpaid-mobile-item-subtotal" ] = ''; |
|
| 158 | + } |
|
| 159 | 159 | } |
| 160 | 160 | |
| 161 | - $this->response = array_merge( $this->response, array( 'texts' => $texts ) ); |
|
| 161 | + $this->response = array_merge( $this->response, array( 'texts' => $texts ) ); |
|
| 162 | 162 | |
| 163 | - } |
|
| 163 | + } |
|
| 164 | 164 | |
| 165 | - /** |
|
| 166 | - * Adds items to a response for submission refresh prices. |
|
| 167 | - * |
|
| 168 | - * @param GetPaid_Payment_Form_Submission $submission |
|
| 169 | - */ |
|
| 170 | - public function add_items( $submission ) { |
|
| 165 | + /** |
|
| 166 | + * Adds items to a response for submission refresh prices. |
|
| 167 | + * |
|
| 168 | + * @param GetPaid_Payment_Form_Submission $submission |
|
| 169 | + */ |
|
| 170 | + public function add_items( $submission ) { |
|
| 171 | 171 | |
| 172 | - // Add items. |
|
| 173 | - $items = array(); |
|
| 174 | - $selected_items = array(); |
|
| 172 | + // Add items. |
|
| 173 | + $items = array(); |
|
| 174 | + $selected_items = array(); |
|
| 175 | 175 | |
| 176 | 176 | foreach ( $submission->get_items() as $item ) { |
| 177 | - $item_id = $item->get_id(); |
|
| 178 | - $items[ "$item_id" ] = $submission->format_amount( $item->get_sub_total() ); |
|
| 179 | - |
|
| 180 | - $selected_items[ "$item_id" ] = array( |
|
| 181 | - 'quantity' => $item->get_quantity(), |
|
| 182 | - 'price' => $item->get_price(), |
|
| 183 | - 'price_fmt' => $submission->format_amount( $item->get_price() ), |
|
| 184 | - ); |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - $this->response = array_merge( |
|
| 188 | - $this->response, |
|
| 189 | - array( |
|
| 190 | - 'items' => $items, |
|
| 191 | - 'selected_items' => $selected_items, |
|
| 192 | - ) |
|
| 193 | - ); |
|
| 194 | - |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * Adds fees to a response for submission refresh prices. |
|
| 199 | - * |
|
| 200 | - * @param GetPaid_Payment_Form_Submission $submission |
|
| 201 | - */ |
|
| 202 | - public function add_fees( $submission ) { |
|
| 203 | - |
|
| 204 | - $fees = array(); |
|
| 177 | + $item_id = $item->get_id(); |
|
| 178 | + $items[ "$item_id" ] = $submission->format_amount( $item->get_sub_total() ); |
|
| 179 | + |
|
| 180 | + $selected_items[ "$item_id" ] = array( |
|
| 181 | + 'quantity' => $item->get_quantity(), |
|
| 182 | + 'price' => $item->get_price(), |
|
| 183 | + 'price_fmt' => $submission->format_amount( $item->get_price() ), |
|
| 184 | + ); |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + $this->response = array_merge( |
|
| 188 | + $this->response, |
|
| 189 | + array( |
|
| 190 | + 'items' => $items, |
|
| 191 | + 'selected_items' => $selected_items, |
|
| 192 | + ) |
|
| 193 | + ); |
|
| 194 | + |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * Adds fees to a response for submission refresh prices. |
|
| 199 | + * |
|
| 200 | + * @param GetPaid_Payment_Form_Submission $submission |
|
| 201 | + */ |
|
| 202 | + public function add_fees( $submission ) { |
|
| 203 | + |
|
| 204 | + $fees = array(); |
|
| 205 | 205 | |
| 206 | 206 | foreach ( $submission->get_fees() as $name => $data ) { |
| 207 | - $fees[ $name ] = $submission->format_amount( $data['initial_fee'] ); |
|
| 208 | - } |
|
| 207 | + $fees[ $name ] = $submission->format_amount( $data['initial_fee'] ); |
|
| 208 | + } |
|
| 209 | 209 | |
| 210 | - $this->response = array_merge( |
|
| 211 | - $this->response, |
|
| 212 | - array( 'fees' => $fees ) |
|
| 213 | - ); |
|
| 210 | + $this->response = array_merge( |
|
| 211 | + $this->response, |
|
| 212 | + array( 'fees' => $fees ) |
|
| 213 | + ); |
|
| 214 | 214 | |
| 215 | - } |
|
| 215 | + } |
|
| 216 | 216 | |
| 217 | - /** |
|
| 218 | - * Adds discounts to a response for submission refresh prices. |
|
| 219 | - * |
|
| 220 | - * @param GetPaid_Payment_Form_Submission $submission |
|
| 221 | - */ |
|
| 222 | - public function add_discounts( $submission ) { |
|
| 217 | + /** |
|
| 218 | + * Adds discounts to a response for submission refresh prices. |
|
| 219 | + * |
|
| 220 | + * @param GetPaid_Payment_Form_Submission $submission |
|
| 221 | + */ |
|
| 222 | + public function add_discounts( $submission ) { |
|
| 223 | 223 | |
| 224 | - $discounts = array(); |
|
| 224 | + $discounts = array(); |
|
| 225 | 225 | |
| 226 | 226 | foreach ( $submission->get_discounts() as $name => $data ) { |
| 227 | - $discounts[ $name ] = $submission->format_amount( $data['initial_discount'] ); |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - $this->response = array_merge( |
|
| 231 | - $this->response, |
|
| 232 | - array( 'discounts' => $discounts ) |
|
| 233 | - ); |
|
| 227 | + $discounts[ $name ] = $submission->format_amount( $data['initial_discount'] ); |
|
| 228 | + } |
|
| 234 | 229 | |
| 235 | - } |
|
| 230 | + $this->response = array_merge( |
|
| 231 | + $this->response, |
|
| 232 | + array( 'discounts' => $discounts ) |
|
| 233 | + ); |
|
| 236 | 234 | |
| 237 | - /** |
|
| 238 | - * Adds taxes to a response for submission refresh prices. |
|
| 239 | - * |
|
| 240 | - * @param GetPaid_Payment_Form_Submission $submission |
|
| 241 | - */ |
|
| 242 | - public function add_taxes( $submission ) { |
|
| 235 | + } |
|
| 243 | 236 | |
| 244 | - $taxes = array(); |
|
| 245 | - $markup = ''; |
|
| 237 | + /** |
|
| 238 | + * Adds taxes to a response for submission refresh prices. |
|
| 239 | + * |
|
| 240 | + * @param GetPaid_Payment_Form_Submission $submission |
|
| 241 | + */ |
|
| 242 | + public function add_taxes( $submission ) { |
|
| 243 | + |
|
| 244 | + $taxes = array(); |
|
| 245 | + $markup = ''; |
|
| 246 | 246 | foreach ( $submission->get_taxes() as $name => $data ) { |
| 247 | - $name = sanitize_text_field( $name ); |
|
| 248 | - $amount = $submission->format_amount( $data['initial_tax'] ); |
|
| 249 | - $taxes[ $name ] = $amount; |
|
| 250 | - $markup .= "<small class='form-text'>$name : $amount</small>"; |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - $this->response = array_merge( |
|
| 254 | - $this->response, |
|
| 255 | - array( 'taxes' => $taxes ) |
|
| 256 | - ); |
|
| 257 | - |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - /** |
|
| 261 | - * Adds gateways to a response for submission refresh prices. |
|
| 262 | - * |
|
| 263 | - * @param GetPaid_Payment_Form_Submission $submission |
|
| 264 | - */ |
|
| 265 | - public function add_gateways( $submission ) { |
|
| 266 | - |
|
| 267 | - $gateways = array_keys( wpinv_get_enabled_payment_gateways() ); |
|
| 268 | - |
|
| 269 | - if ( $this->response['has_recurring'] ) { |
|
| 270 | - |
|
| 271 | - foreach ( $gateways as $i => $gateway ) { |
|
| 272 | - |
|
| 273 | - if ( |
|
| 274 | - ! getpaid_payment_gateway_supports( $gateway, 'subscription' ) |
|
| 275 | - || ( $this->response['has_subscription_group'] && ! getpaid_payment_gateway_supports( $gateway, 'single_subscription_group' ) ) |
|
| 276 | - || ( $this->response['has_multiple_subscription_groups'] && ! getpaid_payment_gateway_supports( $gateway, 'multiple_subscription_groups' ) ) ) { |
|
| 277 | - unset( $gateways[ $i ] ); |
|
| 278 | - } |
|
| 247 | + $name = sanitize_text_field( $name ); |
|
| 248 | + $amount = $submission->format_amount( $data['initial_tax'] ); |
|
| 249 | + $taxes[ $name ] = $amount; |
|
| 250 | + $markup .= "<small class='form-text'>$name : $amount</small>"; |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + $this->response = array_merge( |
|
| 254 | + $this->response, |
|
| 255 | + array( 'taxes' => $taxes ) |
|
| 256 | + ); |
|
| 257 | + |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + /** |
|
| 261 | + * Adds gateways to a response for submission refresh prices. |
|
| 262 | + * |
|
| 263 | + * @param GetPaid_Payment_Form_Submission $submission |
|
| 264 | + */ |
|
| 265 | + public function add_gateways( $submission ) { |
|
| 266 | + |
|
| 267 | + $gateways = array_keys( wpinv_get_enabled_payment_gateways() ); |
|
| 268 | + |
|
| 269 | + if ( $this->response['has_recurring'] ) { |
|
| 270 | + |
|
| 271 | + foreach ( $gateways as $i => $gateway ) { |
|
| 272 | + |
|
| 273 | + if ( |
|
| 274 | + ! getpaid_payment_gateway_supports( $gateway, 'subscription' ) |
|
| 275 | + || ( $this->response['has_subscription_group'] && ! getpaid_payment_gateway_supports( $gateway, 'single_subscription_group' ) ) |
|
| 276 | + || ( $this->response['has_multiple_subscription_groups'] && ! getpaid_payment_gateway_supports( $gateway, 'multiple_subscription_groups' ) ) ) { |
|
| 277 | + unset( $gateways[ $i ] ); |
|
| 278 | + } |
|
| 279 | 279 | } |
| 280 | 280 | } |
| 281 | 281 | |
| 282 | - $gateways = apply_filters( 'getpaid_submission_gateways', $gateways, $submission ); |
|
| 283 | - $this->response = array_merge( |
|
| 284 | - $this->response, |
|
| 285 | - array( 'gateways' => $gateways ) |
|
| 286 | - ); |
|
| 287 | - |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - /** |
|
| 291 | - * Adds data to a response for submission refresh prices. |
|
| 292 | - * |
|
| 293 | - * @param GetPaid_Payment_Form_Submission $submission |
|
| 294 | - */ |
|
| 295 | - public function add_data( $submission ) { |
|
| 296 | - |
|
| 297 | - $this->response = array_merge( |
|
| 298 | - $this->response, |
|
| 299 | - array( |
|
| 300 | - 'js_data' => apply_filters( |
|
| 301 | - 'getpaid_submission_js_data', |
|
| 302 | - array( |
|
| 303 | - 'is_recurring' => $this->response['has_recurring'], |
|
| 304 | - ), |
|
| 305 | - $submission |
|
| 306 | - ), |
|
| 307 | - ) |
|
| 308 | - ); |
|
| 309 | - |
|
| 310 | - } |
|
| 282 | + $gateways = apply_filters( 'getpaid_submission_gateways', $gateways, $submission ); |
|
| 283 | + $this->response = array_merge( |
|
| 284 | + $this->response, |
|
| 285 | + array( 'gateways' => $gateways ) |
|
| 286 | + ); |
|
| 287 | + |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + /** |
|
| 291 | + * Adds data to a response for submission refresh prices. |
|
| 292 | + * |
|
| 293 | + * @param GetPaid_Payment_Form_Submission $submission |
|
| 294 | + */ |
|
| 295 | + public function add_data( $submission ) { |
|
| 296 | + |
|
| 297 | + $this->response = array_merge( |
|
| 298 | + $this->response, |
|
| 299 | + array( |
|
| 300 | + 'js_data' => apply_filters( |
|
| 301 | + 'getpaid_submission_js_data', |
|
| 302 | + array( |
|
| 303 | + 'is_recurring' => $this->response['has_recurring'], |
|
| 304 | + ), |
|
| 305 | + $submission |
|
| 306 | + ), |
|
| 307 | + ) |
|
| 308 | + ); |
|
| 309 | + |
|
| 310 | + } |
|
| 311 | 311 | |
| 312 | 312 | } |
@@ -16,502 +16,502 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class GetPaid_Customers_Query { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * Query vars, after parsing |
|
| 21 | - * |
|
| 22 | - * @since 1.0.19 |
|
| 23 | - * @var array |
|
| 24 | - */ |
|
| 25 | - public $query_vars = array(); |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * List of found customers. |
|
| 29 | - * |
|
| 30 | - * @since 1.0.19 |
|
| 31 | - * @var array |
|
| 32 | - */ |
|
| 33 | - private $results; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * Total number of found customers for the current query |
|
| 37 | - * |
|
| 38 | - * @since 1.0.19 |
|
| 39 | - * @var int |
|
| 40 | - */ |
|
| 41 | - private $total_customers = 0; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * The SQL query used to fetch matching customers. |
|
| 45 | - * |
|
| 46 | - * @since 1.0.19 |
|
| 47 | - * @var string |
|
| 48 | - */ |
|
| 49 | - public $request; |
|
| 50 | - |
|
| 51 | - // SQL clauses |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * Contains the 'FIELDS' sql clause |
|
| 55 | - * |
|
| 56 | - * @since 1.0.19 |
|
| 57 | - * @var string |
|
| 58 | - */ |
|
| 59 | - public $query_fields; |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * Contains the 'FROM' sql clause |
|
| 63 | - * |
|
| 64 | - * @since 1.0.19 |
|
| 65 | - * @var string |
|
| 66 | - */ |
|
| 67 | - public $query_from; |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * Contains the 'WHERE' sql clause |
|
| 71 | - * |
|
| 72 | - * @since 1.0.19 |
|
| 73 | - * @var string |
|
| 74 | - */ |
|
| 75 | - public $query_where; |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * Contains the 'ORDER BY' sql clause |
|
| 79 | - * |
|
| 80 | - * @since 1.0.19 |
|
| 81 | - * @var string |
|
| 82 | - */ |
|
| 83 | - public $query_orderby; |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * Contains the 'LIMIT' sql clause |
|
| 87 | - * |
|
| 88 | - * @since 1.0.19 |
|
| 89 | - * @var string |
|
| 90 | - */ |
|
| 91 | - public $query_limit; |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * Class constructor. |
|
| 95 | - * |
|
| 96 | - * @since 1.0.19 |
|
| 97 | - * |
|
| 98 | - * @param null|string|array $query Optional. The query variables. |
|
| 99 | - */ |
|
| 100 | - public function __construct( $query = null ) { |
|
| 101 | - if ( ! is_null( $query ) ) { |
|
| 102 | - $this->prepare_query( $query ); |
|
| 103 | - $this->query(); |
|
| 104 | - } |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * Fills in missing query variables with default values. |
|
| 109 | - * |
|
| 110 | - * @since 1.0.19 |
|
| 111 | - * |
|
| 112 | - * @param string|array $args Query vars, as passed to `GetPaid_Subscriptions_Query`. |
|
| 113 | - * @return array Complete query variables with undefined ones filled in with defaults. |
|
| 114 | - */ |
|
| 115 | - public static function fill_query_vars( $args ) { |
|
| 116 | - $defaults = array( |
|
| 117 | - 'include' => array(), |
|
| 118 | - 'exclude' => array(), |
|
| 119 | - 'orderby' => 'id', |
|
| 120 | - 'order' => 'DESC', |
|
| 121 | - 'offset' => '', |
|
| 122 | - 'number' => 10, |
|
| 123 | - 'paged' => 1, |
|
| 124 | - 'count_total' => true, |
|
| 125 | - 'fields' => 'all', |
|
| 126 | - 's' => '', |
|
| 127 | - ); |
|
| 128 | - |
|
| 129 | - foreach ( GetPaid_Customer_Data_Store::get_database_fields() as $field => $type ) { |
|
| 130 | - $defaults[ $field ] = 'any'; |
|
| 131 | - |
|
| 132 | - if ( '%f' === $type || '%d' === $type ) { |
|
| 133 | - $defaults[ $field . '_min' ] = ''; |
|
| 134 | - $defaults[ $field . '_max' ] = ''; |
|
| 135 | - } |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - return wp_parse_args( $args, $defaults ); |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - /** |
|
| 142 | - * Prepare the query variables. |
|
| 143 | - * |
|
| 144 | - * @since 1.0.19 |
|
| 145 | - * |
|
| 146 | - * @see self::fill_query_vars() For allowede args and their defaults. |
|
| 147 | - */ |
|
| 148 | - public function prepare_query( $query = array() ) { |
|
| 149 | - global $wpdb; |
|
| 150 | - |
|
| 151 | - if ( empty( $this->query_vars ) || ! empty( $query ) ) { |
|
| 152 | - $this->query_limit = null; |
|
| 153 | - $this->query_vars = $this->fill_query_vars( $query ); |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - if ( ! empty( $this->query_vars['fields'] ) && 'all' !== $this->query_vars['fields'] ) { |
|
| 157 | - $this->query_vars['fields'] = wpinv_parse_list( $this->query_vars['fields'] ); |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - do_action( 'getpaid_pre_get_customers', array( &$this ) ); |
|
| 161 | - |
|
| 162 | - // Ensure that query vars are filled after 'getpaid_pre_get_customers'. |
|
| 163 | - $qv = & $this->query_vars; |
|
| 164 | - $qv = $this->fill_query_vars( $qv ); |
|
| 165 | - $table = $wpdb->prefix . 'getpaid_customers'; |
|
| 166 | - $this->query_from = "FROM $table"; |
|
| 167 | - |
|
| 168 | - // Prepare query fields. |
|
| 169 | - $this->prepare_query_fields( $qv, $table ); |
|
| 170 | - |
|
| 171 | - // Prepare query where. |
|
| 172 | - $this->prepare_query_where( $qv, $table ); |
|
| 173 | - |
|
| 174 | - // Prepare query order. |
|
| 175 | - $this->prepare_query_order( $qv, $table ); |
|
| 176 | - |
|
| 177 | - // limit |
|
| 178 | - if ( isset( $qv['number'] ) && $qv['number'] > 0 ) { |
|
| 179 | - if ( $qv['offset'] ) { |
|
| 180 | - $this->query_limit = $wpdb->prepare( 'LIMIT %d, %d', $qv['offset'], $qv['number'] ); |
|
| 181 | - } else { |
|
| 182 | - $this->query_limit = $wpdb->prepare( 'LIMIT %d, %d', $qv['number'] * ( $qv['paged'] - 1 ), $qv['number'] ); |
|
| 183 | - } |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - do_action_ref_array( 'getpaid_after_customers_query', array( &$this ) ); |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - /** |
|
| 190 | - * Prepares the query fields. |
|
| 191 | - * |
|
| 192 | - * @since 1.0.19 |
|
| 193 | - * |
|
| 194 | - * @param array $qv Query vars. |
|
| 195 | - * @param string $table Table name. |
|
| 196 | - */ |
|
| 197 | - protected function prepare_query_fields( &$qv, $table ) { |
|
| 198 | - |
|
| 199 | - if ( is_array( $qv['fields'] ) ) { |
|
| 200 | - $qv['fields'] = array_unique( $qv['fields'] ); |
|
| 201 | - $allowed_fields = array_keys( GetPaid_Customer_Data_Store::get_database_fields() ); |
|
| 202 | - |
|
| 203 | - $query_fields = array(); |
|
| 204 | - foreach ( $qv['fields'] as $field ) { |
|
| 205 | - if ( ! in_array( $field, $allowed_fields ) ) { |
|
| 206 | - continue; |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - $field = sanitize_key( $field ); |
|
| 210 | - $query_fields[] = "$table.`$field`"; |
|
| 211 | - } |
|
| 212 | - $this->query_fields = implode( ',', $query_fields ); |
|
| 213 | - } else { |
|
| 214 | - $this->query_fields = "$table.*"; |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - if ( isset( $qv['count_total'] ) && $qv['count_total'] ) { |
|
| 218 | - $this->query_fields = 'SQL_CALC_FOUND_ROWS ' . $this->query_fields; |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - /** |
|
| 224 | - * Prepares the query where. |
|
| 225 | - * |
|
| 226 | - * @since 1.0.19 |
|
| 227 | - * |
|
| 228 | - * @param array $qv Query vars. |
|
| 229 | - * @param string $table Table name. |
|
| 230 | - */ |
|
| 231 | - protected function prepare_query_where( &$qv, $table ) { |
|
| 232 | - global $wpdb; |
|
| 233 | - $this->query_where = 'WHERE 1=1'; |
|
| 234 | - |
|
| 235 | - // Fields. |
|
| 236 | - foreach ( GetPaid_Customer_Data_Store::get_database_fields() as $field => $type ) { |
|
| 237 | - if ( 'any' !== $qv[ $field ] ) { |
|
| 238 | - |
|
| 239 | - // In. |
|
| 240 | - if ( is_array( $qv[ $field ] ) ) { |
|
| 241 | - $in = join( ',', array_fill( 0, count( $qv[ $field ] ), $type ) ); |
|
| 242 | - $this->query_where .= $wpdb->prepare( " AND $table.`status` IN ( $in )", $qv[ $field ] ); |
|
| 243 | - } elseif ( ! empty( $qv[ $field ] ) ) { |
|
| 244 | - $this->query_where .= $wpdb->prepare( " AND $table.`$field` = $type", $qv[ $field ] ); |
|
| 245 | - } |
|
| 246 | - } |
|
| 247 | - |
|
| 248 | - // Min/Max. |
|
| 249 | - if ( '%f' === $type || '%d' === $type ) { |
|
| 250 | - |
|
| 251 | - // Min. |
|
| 252 | - if ( is_numeric( $qv[ $field . '_min' ] ) ) { |
|
| 253 | - $this->query_where .= $wpdb->prepare( " AND $table.`$field` >= $type", $qv[ $field . '_min' ] ); |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - // Max. |
|
| 257 | - if ( is_numeric( $qv[ $field . '_max' ] ) ) { |
|
| 258 | - $this->query_where .= $wpdb->prepare( " AND $table.`$field` <= $type", $qv[ $field . '_max' ] ); |
|
| 259 | - } |
|
| 260 | - } |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - if ( ! empty( $qv['include'] ) ) { |
|
| 264 | - $include = implode( ',', wp_parse_id_list( $qv['include'] ) ); |
|
| 265 | - $this->query_where .= " AND $table.`id` IN ($include)"; |
|
| 266 | - } elseif ( ! empty( $qv['exclude'] ) ) { |
|
| 267 | - $exclude = implode( ',', wp_parse_id_list( $qv['exclude'] ) ); |
|
| 268 | - $this->query_where .= " AND $table.`id` NOT IN ($exclude)"; |
|
| 269 | - } |
|
| 270 | - |
|
| 271 | - // Date queries are allowed for the customer creation date. |
|
| 272 | - if ( ! empty( $qv['date_created_query'] ) && is_array( $qv['date_created_query'] ) ) { |
|
| 273 | - $date_created_query = new WP_Date_Query( $qv['date_created_query'], "$table.date_created" ); |
|
| 274 | - $this->query_where .= $date_created_query->get_sql(); |
|
| 275 | - } |
|
| 276 | - |
|
| 277 | - // Search. |
|
| 278 | - if ( ! empty( $qv['s'] ) ) { |
|
| 279 | - $this->query_where .= $this->get_search_sql( $qv['s'] ); |
|
| 280 | - } |
|
| 281 | - } |
|
| 282 | - |
|
| 283 | - /** |
|
| 284 | - * Used internally to generate an SQL string for searching across multiple columns |
|
| 285 | - * |
|
| 286 | - * @since 1.2.7 |
|
| 287 | - * |
|
| 288 | - * @global wpdb $wpdb WordPress database abstraction object. |
|
| 289 | - * |
|
| 290 | - * @param string $string The string to search for. |
|
| 291 | - * @return string |
|
| 292 | - */ |
|
| 293 | - protected function get_search_sql( $string ) { |
|
| 294 | - global $wpdb; |
|
| 295 | - |
|
| 296 | - $searches = array(); |
|
| 297 | - $string = trim( $string, '%' ); |
|
| 298 | - $like = '%' . $wpdb->esc_like( $string ) . '%'; |
|
| 299 | - |
|
| 300 | - foreach ( array_keys( GetPaid_Customer_Data_Store::get_database_fields() ) as $col ) { |
|
| 301 | - if ( 'id' === $col || 'user_id' === $col ) { |
|
| 302 | - $searches[] = $wpdb->prepare( "$col = %s", $string ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
|
| 303 | - } else { |
|
| 304 | - $searches[] = $wpdb->prepare( "$col LIKE %s", $like ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
|
| 305 | - } |
|
| 306 | - } |
|
| 307 | - |
|
| 308 | - return ' AND (' . implode( ' OR ', $searches ) . ')'; |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - /** |
|
| 312 | - * Prepares the query order. |
|
| 313 | - * |
|
| 314 | - * @since 1.0.19 |
|
| 315 | - * |
|
| 316 | - * @param array $qv Query vars. |
|
| 317 | - * @param string $table Table name. |
|
| 318 | - */ |
|
| 319 | - protected function prepare_query_order( &$qv, $table ) { |
|
| 320 | - |
|
| 321 | - // sorting. |
|
| 322 | - $qv['order'] = isset( $qv['order'] ) ? strtoupper( $qv['order'] ) : ''; |
|
| 323 | - $order = $this->parse_order( $qv['order'] ); |
|
| 324 | - |
|
| 325 | - // Default order is by 'id' (latest customers). |
|
| 326 | - if ( empty( $qv['orderby'] ) ) { |
|
| 327 | - $qv['orderby'] = array( 'id' ); |
|
| 328 | - } |
|
| 329 | - |
|
| 330 | - // 'orderby' values may be an array, comma- or space-separated list. |
|
| 331 | - $ordersby = array_filter( wpinv_parse_list( $qv['orderby'] ) ); |
|
| 332 | - |
|
| 333 | - $orderby_array = array(); |
|
| 334 | - foreach ( $ordersby as $_key => $_value ) { |
|
| 335 | - |
|
| 336 | - if ( is_int( $_key ) ) { |
|
| 337 | - // Integer key means this is a flat array of 'orderby' fields. |
|
| 338 | - $_orderby = $_value; |
|
| 339 | - $_order = $order; |
|
| 340 | - } else { |
|
| 341 | - // Non-integer key means that the key is the field and the value is ASC/DESC. |
|
| 342 | - $_orderby = $_key; |
|
| 343 | - $_order = $_value; |
|
| 344 | - } |
|
| 345 | - |
|
| 346 | - $parsed = $this->parse_orderby( $_orderby, $table ); |
|
| 347 | - |
|
| 348 | - if ( $parsed ) { |
|
| 349 | - $orderby_array[] = $parsed . ' ' . $this->parse_order( $_order ); |
|
| 350 | - } |
|
| 351 | - } |
|
| 352 | - |
|
| 353 | - // If no valid clauses were found, order by id. |
|
| 354 | - if ( empty( $orderby_array ) ) { |
|
| 355 | - $orderby_array[] = "id $order"; |
|
| 356 | - } |
|
| 357 | - |
|
| 358 | - $this->query_orderby = 'ORDER BY ' . implode( ', ', $orderby_array ); |
|
| 359 | - |
|
| 360 | - } |
|
| 361 | - |
|
| 362 | - /** |
|
| 363 | - * Execute the query, with the current variables. |
|
| 364 | - * |
|
| 365 | - * @since 1.0.19 |
|
| 366 | - * |
|
| 367 | - * @global wpdb $wpdb WordPress database abstraction object. |
|
| 368 | - */ |
|
| 369 | - public function query() { |
|
| 370 | - global $wpdb; |
|
| 371 | - |
|
| 372 | - $qv =& $this->query_vars; |
|
| 373 | - |
|
| 374 | - // Return a non-null value to bypass the default GetPaid customers query and remember to set the |
|
| 375 | - // total_customers property. |
|
| 376 | - $this->results = apply_filters_ref_array( 'getpaid_customers_pre_query', array( null, &$this ) ); |
|
| 377 | - |
|
| 378 | - if ( null === $this->results ) { |
|
| 379 | - $this->request = "SELECT $this->query_fields $this->query_from $this->query_where $this->query_orderby $this->query_limit"; |
|
| 380 | - |
|
| 381 | - if ( ( is_array( $qv['fields'] ) && 1 !== count( $qv['fields'] ) ) || 'all' === $qv['fields'] ) { |
|
| 382 | - $this->results = $wpdb->get_results( $this->request ); |
|
| 383 | - } else { |
|
| 384 | - $this->results = $wpdb->get_col( $this->request ); |
|
| 385 | - } |
|
| 386 | - |
|
| 387 | - if ( isset( $qv['count_total'] ) && $qv['count_total'] ) { |
|
| 388 | - $found_customers_query = apply_filters( 'getpaid_found_customers_query', 'SELECT FOUND_ROWS()', $this ); |
|
| 389 | - $this->total_customers = (int) $wpdb->get_var( $found_customers_query ); |
|
| 390 | - } |
|
| 391 | - } |
|
| 392 | - |
|
| 393 | - if ( 'all' === $qv['fields'] ) { |
|
| 394 | - foreach ( $this->results as $key => $customer ) { |
|
| 395 | - $this->set_cache( $customer->id, $customer, 'getpaid_customers' ); |
|
| 396 | - $this->set_cache( $customer->user_id, $customer->id, 'getpaid_customer_ids_by_user_id' ); |
|
| 397 | - $this->set_cache( $customer->email, $customer->id, 'getpaid_customer_ids_by_email' ); |
|
| 398 | - $this->results[ $key ] = new GetPaid_Customer( $customer ); |
|
| 399 | - } |
|
| 400 | - } |
|
| 401 | - |
|
| 402 | - } |
|
| 403 | - |
|
| 404 | - /** |
|
| 405 | - * Set cache |
|
| 406 | - * |
|
| 407 | - * @param string $id |
|
| 408 | - * @param mixed $data |
|
| 409 | - * @param string $group |
|
| 410 | - * @param integer $expire |
|
| 411 | - * @return boolean |
|
| 412 | - */ |
|
| 413 | - public function set_cache( $key, $data, $group = '', $expire = 0 ) { |
|
| 414 | - |
|
| 415 | - if ( empty( $key ) ) { |
|
| 416 | - return false; |
|
| 417 | - } |
|
| 418 | - |
|
| 419 | - wp_cache_set( $key, $data, $group, $expire ); |
|
| 420 | - } |
|
| 421 | - |
|
| 422 | - /** |
|
| 423 | - * Retrieve query variable. |
|
| 424 | - * |
|
| 425 | - * @since 1.0.19 |
|
| 426 | - * |
|
| 427 | - * @param string $query_var Query variable key. |
|
| 428 | - * @return mixed |
|
| 429 | - */ |
|
| 430 | - public function get( $query_var ) { |
|
| 431 | - if ( isset( $this->query_vars[ $query_var ] ) ) { |
|
| 432 | - return $this->query_vars[ $query_var ]; |
|
| 433 | - } |
|
| 434 | - |
|
| 435 | - return null; |
|
| 436 | - } |
|
| 437 | - |
|
| 438 | - /** |
|
| 439 | - * Set query variable. |
|
| 440 | - * |
|
| 441 | - * @since 1.0.19 |
|
| 442 | - * |
|
| 443 | - * @param string $query_var Query variable key. |
|
| 444 | - * @param mixed $value Query variable value. |
|
| 445 | - */ |
|
| 446 | - public function set( $query_var, $value ) { |
|
| 447 | - $this->query_vars[ $query_var ] = $value; |
|
| 448 | - } |
|
| 449 | - |
|
| 450 | - /** |
|
| 451 | - * Return the list of customers. |
|
| 452 | - * |
|
| 453 | - * @since 1.0.19 |
|
| 454 | - * |
|
| 455 | - * @return GetPaid_Customer[]|array Found customers. |
|
| 456 | - */ |
|
| 457 | - public function get_results() { |
|
| 458 | - return $this->results; |
|
| 459 | - } |
|
| 460 | - |
|
| 461 | - /** |
|
| 462 | - * Return the total number of customers for the current query. |
|
| 463 | - * |
|
| 464 | - * @since 1.0.19 |
|
| 465 | - * |
|
| 466 | - * @return int Number of total customers. |
|
| 467 | - */ |
|
| 468 | - public function get_total() { |
|
| 469 | - return $this->total_customers; |
|
| 470 | - } |
|
| 471 | - |
|
| 472 | - /** |
|
| 473 | - * Parse and sanitize 'orderby' keys passed to the customers query. |
|
| 474 | - * |
|
| 475 | - * @since 1.0.19 |
|
| 476 | - * |
|
| 477 | - * @param string $orderby Alias for the field to order by. |
|
| 478 | - * @param string $table The current table. |
|
| 479 | - * @return string Value to use in the ORDER clause, if `$orderby` is valid. |
|
| 480 | - */ |
|
| 481 | - protected function parse_orderby( $orderby, $table ) { |
|
| 482 | - |
|
| 483 | - $_orderby = ''; |
|
| 484 | - if ( in_array( $orderby, array_keys( GetPaid_Customer_Data_Store::get_database_fields() ), true ) ) { |
|
| 485 | - $_orderby = "$table.`$orderby`"; |
|
| 486 | - } elseif ( 'id' === strtolower( $orderby ) ) { |
|
| 487 | - $_orderby = "$table.id"; |
|
| 488 | - } elseif ( 'include' === $orderby && ! empty( $this->query_vars['include'] ) ) { |
|
| 489 | - $include = wp_parse_id_list( $this->query_vars['include'] ); |
|
| 490 | - $include_sql = implode( ',', $include ); |
|
| 491 | - $_orderby = "FIELD( $table.id, $include_sql )"; |
|
| 492 | - } |
|
| 493 | - |
|
| 494 | - return $_orderby; |
|
| 495 | - } |
|
| 496 | - |
|
| 497 | - /** |
|
| 498 | - * Parse an 'order' query variable and cast it to ASC or DESC as necessary. |
|
| 499 | - * |
|
| 500 | - * @since 1.0.19 |
|
| 501 | - * |
|
| 502 | - * @param string $order The 'order' query variable. |
|
| 503 | - * @return string The sanitized 'order' query variable. |
|
| 504 | - */ |
|
| 505 | - protected function parse_order( $order ) { |
|
| 506 | - if ( ! is_string( $order ) || empty( $order ) ) { |
|
| 507 | - return 'DESC'; |
|
| 508 | - } |
|
| 509 | - |
|
| 510 | - if ( 'ASC' === strtoupper( $order ) ) { |
|
| 511 | - return 'ASC'; |
|
| 512 | - } else { |
|
| 513 | - return 'DESC'; |
|
| 514 | - } |
|
| 515 | - } |
|
| 19 | + /** |
|
| 20 | + * Query vars, after parsing |
|
| 21 | + * |
|
| 22 | + * @since 1.0.19 |
|
| 23 | + * @var array |
|
| 24 | + */ |
|
| 25 | + public $query_vars = array(); |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * List of found customers. |
|
| 29 | + * |
|
| 30 | + * @since 1.0.19 |
|
| 31 | + * @var array |
|
| 32 | + */ |
|
| 33 | + private $results; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * Total number of found customers for the current query |
|
| 37 | + * |
|
| 38 | + * @since 1.0.19 |
|
| 39 | + * @var int |
|
| 40 | + */ |
|
| 41 | + private $total_customers = 0; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * The SQL query used to fetch matching customers. |
|
| 45 | + * |
|
| 46 | + * @since 1.0.19 |
|
| 47 | + * @var string |
|
| 48 | + */ |
|
| 49 | + public $request; |
|
| 50 | + |
|
| 51 | + // SQL clauses |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * Contains the 'FIELDS' sql clause |
|
| 55 | + * |
|
| 56 | + * @since 1.0.19 |
|
| 57 | + * @var string |
|
| 58 | + */ |
|
| 59 | + public $query_fields; |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * Contains the 'FROM' sql clause |
|
| 63 | + * |
|
| 64 | + * @since 1.0.19 |
|
| 65 | + * @var string |
|
| 66 | + */ |
|
| 67 | + public $query_from; |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * Contains the 'WHERE' sql clause |
|
| 71 | + * |
|
| 72 | + * @since 1.0.19 |
|
| 73 | + * @var string |
|
| 74 | + */ |
|
| 75 | + public $query_where; |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * Contains the 'ORDER BY' sql clause |
|
| 79 | + * |
|
| 80 | + * @since 1.0.19 |
|
| 81 | + * @var string |
|
| 82 | + */ |
|
| 83 | + public $query_orderby; |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * Contains the 'LIMIT' sql clause |
|
| 87 | + * |
|
| 88 | + * @since 1.0.19 |
|
| 89 | + * @var string |
|
| 90 | + */ |
|
| 91 | + public $query_limit; |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Class constructor. |
|
| 95 | + * |
|
| 96 | + * @since 1.0.19 |
|
| 97 | + * |
|
| 98 | + * @param null|string|array $query Optional. The query variables. |
|
| 99 | + */ |
|
| 100 | + public function __construct( $query = null ) { |
|
| 101 | + if ( ! is_null( $query ) ) { |
|
| 102 | + $this->prepare_query( $query ); |
|
| 103 | + $this->query(); |
|
| 104 | + } |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * Fills in missing query variables with default values. |
|
| 109 | + * |
|
| 110 | + * @since 1.0.19 |
|
| 111 | + * |
|
| 112 | + * @param string|array $args Query vars, as passed to `GetPaid_Subscriptions_Query`. |
|
| 113 | + * @return array Complete query variables with undefined ones filled in with defaults. |
|
| 114 | + */ |
|
| 115 | + public static function fill_query_vars( $args ) { |
|
| 116 | + $defaults = array( |
|
| 117 | + 'include' => array(), |
|
| 118 | + 'exclude' => array(), |
|
| 119 | + 'orderby' => 'id', |
|
| 120 | + 'order' => 'DESC', |
|
| 121 | + 'offset' => '', |
|
| 122 | + 'number' => 10, |
|
| 123 | + 'paged' => 1, |
|
| 124 | + 'count_total' => true, |
|
| 125 | + 'fields' => 'all', |
|
| 126 | + 's' => '', |
|
| 127 | + ); |
|
| 128 | + |
|
| 129 | + foreach ( GetPaid_Customer_Data_Store::get_database_fields() as $field => $type ) { |
|
| 130 | + $defaults[ $field ] = 'any'; |
|
| 131 | + |
|
| 132 | + if ( '%f' === $type || '%d' === $type ) { |
|
| 133 | + $defaults[ $field . '_min' ] = ''; |
|
| 134 | + $defaults[ $field . '_max' ] = ''; |
|
| 135 | + } |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + return wp_parse_args( $args, $defaults ); |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + /** |
|
| 142 | + * Prepare the query variables. |
|
| 143 | + * |
|
| 144 | + * @since 1.0.19 |
|
| 145 | + * |
|
| 146 | + * @see self::fill_query_vars() For allowede args and their defaults. |
|
| 147 | + */ |
|
| 148 | + public function prepare_query( $query = array() ) { |
|
| 149 | + global $wpdb; |
|
| 150 | + |
|
| 151 | + if ( empty( $this->query_vars ) || ! empty( $query ) ) { |
|
| 152 | + $this->query_limit = null; |
|
| 153 | + $this->query_vars = $this->fill_query_vars( $query ); |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + if ( ! empty( $this->query_vars['fields'] ) && 'all' !== $this->query_vars['fields'] ) { |
|
| 157 | + $this->query_vars['fields'] = wpinv_parse_list( $this->query_vars['fields'] ); |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + do_action( 'getpaid_pre_get_customers', array( &$this ) ); |
|
| 161 | + |
|
| 162 | + // Ensure that query vars are filled after 'getpaid_pre_get_customers'. |
|
| 163 | + $qv = & $this->query_vars; |
|
| 164 | + $qv = $this->fill_query_vars( $qv ); |
|
| 165 | + $table = $wpdb->prefix . 'getpaid_customers'; |
|
| 166 | + $this->query_from = "FROM $table"; |
|
| 167 | + |
|
| 168 | + // Prepare query fields. |
|
| 169 | + $this->prepare_query_fields( $qv, $table ); |
|
| 170 | + |
|
| 171 | + // Prepare query where. |
|
| 172 | + $this->prepare_query_where( $qv, $table ); |
|
| 173 | + |
|
| 174 | + // Prepare query order. |
|
| 175 | + $this->prepare_query_order( $qv, $table ); |
|
| 176 | + |
|
| 177 | + // limit |
|
| 178 | + if ( isset( $qv['number'] ) && $qv['number'] > 0 ) { |
|
| 179 | + if ( $qv['offset'] ) { |
|
| 180 | + $this->query_limit = $wpdb->prepare( 'LIMIT %d, %d', $qv['offset'], $qv['number'] ); |
|
| 181 | + } else { |
|
| 182 | + $this->query_limit = $wpdb->prepare( 'LIMIT %d, %d', $qv['number'] * ( $qv['paged'] - 1 ), $qv['number'] ); |
|
| 183 | + } |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + do_action_ref_array( 'getpaid_after_customers_query', array( &$this ) ); |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + /** |
|
| 190 | + * Prepares the query fields. |
|
| 191 | + * |
|
| 192 | + * @since 1.0.19 |
|
| 193 | + * |
|
| 194 | + * @param array $qv Query vars. |
|
| 195 | + * @param string $table Table name. |
|
| 196 | + */ |
|
| 197 | + protected function prepare_query_fields( &$qv, $table ) { |
|
| 198 | + |
|
| 199 | + if ( is_array( $qv['fields'] ) ) { |
|
| 200 | + $qv['fields'] = array_unique( $qv['fields'] ); |
|
| 201 | + $allowed_fields = array_keys( GetPaid_Customer_Data_Store::get_database_fields() ); |
|
| 202 | + |
|
| 203 | + $query_fields = array(); |
|
| 204 | + foreach ( $qv['fields'] as $field ) { |
|
| 205 | + if ( ! in_array( $field, $allowed_fields ) ) { |
|
| 206 | + continue; |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + $field = sanitize_key( $field ); |
|
| 210 | + $query_fields[] = "$table.`$field`"; |
|
| 211 | + } |
|
| 212 | + $this->query_fields = implode( ',', $query_fields ); |
|
| 213 | + } else { |
|
| 214 | + $this->query_fields = "$table.*"; |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + if ( isset( $qv['count_total'] ) && $qv['count_total'] ) { |
|
| 218 | + $this->query_fields = 'SQL_CALC_FOUND_ROWS ' . $this->query_fields; |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + /** |
|
| 224 | + * Prepares the query where. |
|
| 225 | + * |
|
| 226 | + * @since 1.0.19 |
|
| 227 | + * |
|
| 228 | + * @param array $qv Query vars. |
|
| 229 | + * @param string $table Table name. |
|
| 230 | + */ |
|
| 231 | + protected function prepare_query_where( &$qv, $table ) { |
|
| 232 | + global $wpdb; |
|
| 233 | + $this->query_where = 'WHERE 1=1'; |
|
| 234 | + |
|
| 235 | + // Fields. |
|
| 236 | + foreach ( GetPaid_Customer_Data_Store::get_database_fields() as $field => $type ) { |
|
| 237 | + if ( 'any' !== $qv[ $field ] ) { |
|
| 238 | + |
|
| 239 | + // In. |
|
| 240 | + if ( is_array( $qv[ $field ] ) ) { |
|
| 241 | + $in = join( ',', array_fill( 0, count( $qv[ $field ] ), $type ) ); |
|
| 242 | + $this->query_where .= $wpdb->prepare( " AND $table.`status` IN ( $in )", $qv[ $field ] ); |
|
| 243 | + } elseif ( ! empty( $qv[ $field ] ) ) { |
|
| 244 | + $this->query_where .= $wpdb->prepare( " AND $table.`$field` = $type", $qv[ $field ] ); |
|
| 245 | + } |
|
| 246 | + } |
|
| 247 | + |
|
| 248 | + // Min/Max. |
|
| 249 | + if ( '%f' === $type || '%d' === $type ) { |
|
| 250 | + |
|
| 251 | + // Min. |
|
| 252 | + if ( is_numeric( $qv[ $field . '_min' ] ) ) { |
|
| 253 | + $this->query_where .= $wpdb->prepare( " AND $table.`$field` >= $type", $qv[ $field . '_min' ] ); |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + // Max. |
|
| 257 | + if ( is_numeric( $qv[ $field . '_max' ] ) ) { |
|
| 258 | + $this->query_where .= $wpdb->prepare( " AND $table.`$field` <= $type", $qv[ $field . '_max' ] ); |
|
| 259 | + } |
|
| 260 | + } |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + if ( ! empty( $qv['include'] ) ) { |
|
| 264 | + $include = implode( ',', wp_parse_id_list( $qv['include'] ) ); |
|
| 265 | + $this->query_where .= " AND $table.`id` IN ($include)"; |
|
| 266 | + } elseif ( ! empty( $qv['exclude'] ) ) { |
|
| 267 | + $exclude = implode( ',', wp_parse_id_list( $qv['exclude'] ) ); |
|
| 268 | + $this->query_where .= " AND $table.`id` NOT IN ($exclude)"; |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + // Date queries are allowed for the customer creation date. |
|
| 272 | + if ( ! empty( $qv['date_created_query'] ) && is_array( $qv['date_created_query'] ) ) { |
|
| 273 | + $date_created_query = new WP_Date_Query( $qv['date_created_query'], "$table.date_created" ); |
|
| 274 | + $this->query_where .= $date_created_query->get_sql(); |
|
| 275 | + } |
|
| 276 | + |
|
| 277 | + // Search. |
|
| 278 | + if ( ! empty( $qv['s'] ) ) { |
|
| 279 | + $this->query_where .= $this->get_search_sql( $qv['s'] ); |
|
| 280 | + } |
|
| 281 | + } |
|
| 282 | + |
|
| 283 | + /** |
|
| 284 | + * Used internally to generate an SQL string for searching across multiple columns |
|
| 285 | + * |
|
| 286 | + * @since 1.2.7 |
|
| 287 | + * |
|
| 288 | + * @global wpdb $wpdb WordPress database abstraction object. |
|
| 289 | + * |
|
| 290 | + * @param string $string The string to search for. |
|
| 291 | + * @return string |
|
| 292 | + */ |
|
| 293 | + protected function get_search_sql( $string ) { |
|
| 294 | + global $wpdb; |
|
| 295 | + |
|
| 296 | + $searches = array(); |
|
| 297 | + $string = trim( $string, '%' ); |
|
| 298 | + $like = '%' . $wpdb->esc_like( $string ) . '%'; |
|
| 299 | + |
|
| 300 | + foreach ( array_keys( GetPaid_Customer_Data_Store::get_database_fields() ) as $col ) { |
|
| 301 | + if ( 'id' === $col || 'user_id' === $col ) { |
|
| 302 | + $searches[] = $wpdb->prepare( "$col = %s", $string ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
|
| 303 | + } else { |
|
| 304 | + $searches[] = $wpdb->prepare( "$col LIKE %s", $like ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
|
| 305 | + } |
|
| 306 | + } |
|
| 307 | + |
|
| 308 | + return ' AND (' . implode( ' OR ', $searches ) . ')'; |
|
| 309 | + } |
|
| 310 | + |
|
| 311 | + /** |
|
| 312 | + * Prepares the query order. |
|
| 313 | + * |
|
| 314 | + * @since 1.0.19 |
|
| 315 | + * |
|
| 316 | + * @param array $qv Query vars. |
|
| 317 | + * @param string $table Table name. |
|
| 318 | + */ |
|
| 319 | + protected function prepare_query_order( &$qv, $table ) { |
|
| 320 | + |
|
| 321 | + // sorting. |
|
| 322 | + $qv['order'] = isset( $qv['order'] ) ? strtoupper( $qv['order'] ) : ''; |
|
| 323 | + $order = $this->parse_order( $qv['order'] ); |
|
| 324 | + |
|
| 325 | + // Default order is by 'id' (latest customers). |
|
| 326 | + if ( empty( $qv['orderby'] ) ) { |
|
| 327 | + $qv['orderby'] = array( 'id' ); |
|
| 328 | + } |
|
| 329 | + |
|
| 330 | + // 'orderby' values may be an array, comma- or space-separated list. |
|
| 331 | + $ordersby = array_filter( wpinv_parse_list( $qv['orderby'] ) ); |
|
| 332 | + |
|
| 333 | + $orderby_array = array(); |
|
| 334 | + foreach ( $ordersby as $_key => $_value ) { |
|
| 335 | + |
|
| 336 | + if ( is_int( $_key ) ) { |
|
| 337 | + // Integer key means this is a flat array of 'orderby' fields. |
|
| 338 | + $_orderby = $_value; |
|
| 339 | + $_order = $order; |
|
| 340 | + } else { |
|
| 341 | + // Non-integer key means that the key is the field and the value is ASC/DESC. |
|
| 342 | + $_orderby = $_key; |
|
| 343 | + $_order = $_value; |
|
| 344 | + } |
|
| 345 | + |
|
| 346 | + $parsed = $this->parse_orderby( $_orderby, $table ); |
|
| 347 | + |
|
| 348 | + if ( $parsed ) { |
|
| 349 | + $orderby_array[] = $parsed . ' ' . $this->parse_order( $_order ); |
|
| 350 | + } |
|
| 351 | + } |
|
| 352 | + |
|
| 353 | + // If no valid clauses were found, order by id. |
|
| 354 | + if ( empty( $orderby_array ) ) { |
|
| 355 | + $orderby_array[] = "id $order"; |
|
| 356 | + } |
|
| 357 | + |
|
| 358 | + $this->query_orderby = 'ORDER BY ' . implode( ', ', $orderby_array ); |
|
| 359 | + |
|
| 360 | + } |
|
| 361 | + |
|
| 362 | + /** |
|
| 363 | + * Execute the query, with the current variables. |
|
| 364 | + * |
|
| 365 | + * @since 1.0.19 |
|
| 366 | + * |
|
| 367 | + * @global wpdb $wpdb WordPress database abstraction object. |
|
| 368 | + */ |
|
| 369 | + public function query() { |
|
| 370 | + global $wpdb; |
|
| 371 | + |
|
| 372 | + $qv =& $this->query_vars; |
|
| 373 | + |
|
| 374 | + // Return a non-null value to bypass the default GetPaid customers query and remember to set the |
|
| 375 | + // total_customers property. |
|
| 376 | + $this->results = apply_filters_ref_array( 'getpaid_customers_pre_query', array( null, &$this ) ); |
|
| 377 | + |
|
| 378 | + if ( null === $this->results ) { |
|
| 379 | + $this->request = "SELECT $this->query_fields $this->query_from $this->query_where $this->query_orderby $this->query_limit"; |
|
| 380 | + |
|
| 381 | + if ( ( is_array( $qv['fields'] ) && 1 !== count( $qv['fields'] ) ) || 'all' === $qv['fields'] ) { |
|
| 382 | + $this->results = $wpdb->get_results( $this->request ); |
|
| 383 | + } else { |
|
| 384 | + $this->results = $wpdb->get_col( $this->request ); |
|
| 385 | + } |
|
| 386 | + |
|
| 387 | + if ( isset( $qv['count_total'] ) && $qv['count_total'] ) { |
|
| 388 | + $found_customers_query = apply_filters( 'getpaid_found_customers_query', 'SELECT FOUND_ROWS()', $this ); |
|
| 389 | + $this->total_customers = (int) $wpdb->get_var( $found_customers_query ); |
|
| 390 | + } |
|
| 391 | + } |
|
| 392 | + |
|
| 393 | + if ( 'all' === $qv['fields'] ) { |
|
| 394 | + foreach ( $this->results as $key => $customer ) { |
|
| 395 | + $this->set_cache( $customer->id, $customer, 'getpaid_customers' ); |
|
| 396 | + $this->set_cache( $customer->user_id, $customer->id, 'getpaid_customer_ids_by_user_id' ); |
|
| 397 | + $this->set_cache( $customer->email, $customer->id, 'getpaid_customer_ids_by_email' ); |
|
| 398 | + $this->results[ $key ] = new GetPaid_Customer( $customer ); |
|
| 399 | + } |
|
| 400 | + } |
|
| 401 | + |
|
| 402 | + } |
|
| 403 | + |
|
| 404 | + /** |
|
| 405 | + * Set cache |
|
| 406 | + * |
|
| 407 | + * @param string $id |
|
| 408 | + * @param mixed $data |
|
| 409 | + * @param string $group |
|
| 410 | + * @param integer $expire |
|
| 411 | + * @return boolean |
|
| 412 | + */ |
|
| 413 | + public function set_cache( $key, $data, $group = '', $expire = 0 ) { |
|
| 414 | + |
|
| 415 | + if ( empty( $key ) ) { |
|
| 416 | + return false; |
|
| 417 | + } |
|
| 418 | + |
|
| 419 | + wp_cache_set( $key, $data, $group, $expire ); |
|
| 420 | + } |
|
| 421 | + |
|
| 422 | + /** |
|
| 423 | + * Retrieve query variable. |
|
| 424 | + * |
|
| 425 | + * @since 1.0.19 |
|
| 426 | + * |
|
| 427 | + * @param string $query_var Query variable key. |
|
| 428 | + * @return mixed |
|
| 429 | + */ |
|
| 430 | + public function get( $query_var ) { |
|
| 431 | + if ( isset( $this->query_vars[ $query_var ] ) ) { |
|
| 432 | + return $this->query_vars[ $query_var ]; |
|
| 433 | + } |
|
| 434 | + |
|
| 435 | + return null; |
|
| 436 | + } |
|
| 437 | + |
|
| 438 | + /** |
|
| 439 | + * Set query variable. |
|
| 440 | + * |
|
| 441 | + * @since 1.0.19 |
|
| 442 | + * |
|
| 443 | + * @param string $query_var Query variable key. |
|
| 444 | + * @param mixed $value Query variable value. |
|
| 445 | + */ |
|
| 446 | + public function set( $query_var, $value ) { |
|
| 447 | + $this->query_vars[ $query_var ] = $value; |
|
| 448 | + } |
|
| 449 | + |
|
| 450 | + /** |
|
| 451 | + * Return the list of customers. |
|
| 452 | + * |
|
| 453 | + * @since 1.0.19 |
|
| 454 | + * |
|
| 455 | + * @return GetPaid_Customer[]|array Found customers. |
|
| 456 | + */ |
|
| 457 | + public function get_results() { |
|
| 458 | + return $this->results; |
|
| 459 | + } |
|
| 460 | + |
|
| 461 | + /** |
|
| 462 | + * Return the total number of customers for the current query. |
|
| 463 | + * |
|
| 464 | + * @since 1.0.19 |
|
| 465 | + * |
|
| 466 | + * @return int Number of total customers. |
|
| 467 | + */ |
|
| 468 | + public function get_total() { |
|
| 469 | + return $this->total_customers; |
|
| 470 | + } |
|
| 471 | + |
|
| 472 | + /** |
|
| 473 | + * Parse and sanitize 'orderby' keys passed to the customers query. |
|
| 474 | + * |
|
| 475 | + * @since 1.0.19 |
|
| 476 | + * |
|
| 477 | + * @param string $orderby Alias for the field to order by. |
|
| 478 | + * @param string $table The current table. |
|
| 479 | + * @return string Value to use in the ORDER clause, if `$orderby` is valid. |
|
| 480 | + */ |
|
| 481 | + protected function parse_orderby( $orderby, $table ) { |
|
| 482 | + |
|
| 483 | + $_orderby = ''; |
|
| 484 | + if ( in_array( $orderby, array_keys( GetPaid_Customer_Data_Store::get_database_fields() ), true ) ) { |
|
| 485 | + $_orderby = "$table.`$orderby`"; |
|
| 486 | + } elseif ( 'id' === strtolower( $orderby ) ) { |
|
| 487 | + $_orderby = "$table.id"; |
|
| 488 | + } elseif ( 'include' === $orderby && ! empty( $this->query_vars['include'] ) ) { |
|
| 489 | + $include = wp_parse_id_list( $this->query_vars['include'] ); |
|
| 490 | + $include_sql = implode( ',', $include ); |
|
| 491 | + $_orderby = "FIELD( $table.id, $include_sql )"; |
|
| 492 | + } |
|
| 493 | + |
|
| 494 | + return $_orderby; |
|
| 495 | + } |
|
| 496 | + |
|
| 497 | + /** |
|
| 498 | + * Parse an 'order' query variable and cast it to ASC or DESC as necessary. |
|
| 499 | + * |
|
| 500 | + * @since 1.0.19 |
|
| 501 | + * |
|
| 502 | + * @param string $order The 'order' query variable. |
|
| 503 | + * @return string The sanitized 'order' query variable. |
|
| 504 | + */ |
|
| 505 | + protected function parse_order( $order ) { |
|
| 506 | + if ( ! is_string( $order ) || empty( $order ) ) { |
|
| 507 | + return 'DESC'; |
|
| 508 | + } |
|
| 509 | + |
|
| 510 | + if ( 'ASC' === strtoupper( $order ) ) { |
|
| 511 | + return 'ASC'; |
|
| 512 | + } else { |
|
| 513 | + return 'DESC'; |
|
| 514 | + } |
|
| 515 | + } |
|
| 516 | 516 | |
| 517 | 517 | } |
@@ -11,7 +11,7 @@ discard block |
||
| 11 | 11 | |
| 12 | 12 | // Load WP_List_Table if not loaded |
| 13 | 13 | if ( ! class_exists( 'WP_List_Table' ) ) { |
| 14 | - require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; |
|
| 14 | + require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; |
|
| 15 | 15 | } |
| 16 | 16 | |
| 17 | 17 | /** |
@@ -23,303 +23,303 @@ discard block |
||
| 23 | 23 | */ |
| 24 | 24 | class WPInv_Customers_Table extends WP_List_Table { |
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * @var int Number of items per page |
|
| 28 | - * @since 1.0.19 |
|
| 29 | - */ |
|
| 30 | - public $per_page = 25; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * @var int Number of items |
|
| 34 | - * @since 1.0.19 |
|
| 35 | - */ |
|
| 36 | - public $total_count = 0; |
|
| 37 | - |
|
| 38 | - public $query; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * Get things started |
|
| 42 | - * |
|
| 43 | - * @since 1.0.19 |
|
| 44 | - * @see WP_List_Table::__construct() |
|
| 45 | - */ |
|
| 46 | - public function __construct() { |
|
| 47 | - |
|
| 48 | - // Set parent defaults |
|
| 49 | - parent::__construct( |
|
| 26 | + /** |
|
| 27 | + * @var int Number of items per page |
|
| 28 | + * @since 1.0.19 |
|
| 29 | + */ |
|
| 30 | + public $per_page = 25; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * @var int Number of items |
|
| 34 | + * @since 1.0.19 |
|
| 35 | + */ |
|
| 36 | + public $total_count = 0; |
|
| 37 | + |
|
| 38 | + public $query; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * Get things started |
|
| 42 | + * |
|
| 43 | + * @since 1.0.19 |
|
| 44 | + * @see WP_List_Table::__construct() |
|
| 45 | + */ |
|
| 46 | + public function __construct() { |
|
| 47 | + |
|
| 48 | + // Set parent defaults |
|
| 49 | + parent::__construct( |
|
| 50 | 50 | array( |
| 51 | - 'singular' => 'id', |
|
| 52 | - 'plural' => 'ids', |
|
| 53 | - 'ajax' => false, |
|
| 51 | + 'singular' => 'id', |
|
| 52 | + 'plural' => 'ids', |
|
| 53 | + 'ajax' => false, |
|
| 54 | 54 | ) |
| 55 | 55 | ); |
| 56 | 56 | |
| 57 | - } |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * Gets the name of the primary column. |
|
| 61 | - * |
|
| 62 | - * @since 1.0.19 |
|
| 63 | - * @access protected |
|
| 64 | - * |
|
| 65 | - * @return string Name of the primary column. |
|
| 66 | - */ |
|
| 67 | - protected function get_primary_column_name() { |
|
| 68 | - return 'customer'; |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * This function renders most of the columns in the list table. |
|
| 73 | - * |
|
| 74 | - * @since 1.0.19 |
|
| 75 | - * |
|
| 76 | - * @param GetPaid_Customer $customer |
|
| 77 | - * @param string $column_name The name of the column |
|
| 78 | - * |
|
| 79 | - * @return string Column Name |
|
| 80 | - */ |
|
| 81 | - public function column_default( $customer, $column_name ) { |
|
| 82 | - $value = esc_html( $customer->get( $column_name ) ); |
|
| 83 | - return apply_filters( 'wpinv_customers_table_column' . $column_name, $value, $customer ); |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * Displays the country column. |
|
| 88 | - * |
|
| 89 | - * @since 1.0.19 |
|
| 90 | - * |
|
| 91 | - * @param GetPaid_Customer $customer |
|
| 92 | - * |
|
| 93 | - * @return string Column Name |
|
| 94 | - */ |
|
| 95 | - public function column_country( $customer ) { |
|
| 96 | - $country = wpinv_sanitize_country( $customer->get( 'country' ) ); |
|
| 97 | - if ( $country ) { |
|
| 98 | - $country = wpinv_country_name( $country ); |
|
| 99 | - } |
|
| 100 | - return esc_html( $country ); |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * Displays the state column. |
|
| 105 | - * |
|
| 106 | - * @since 1.0.19 |
|
| 107 | - * |
|
| 108 | - * @param GetPaid_Customer $customer |
|
| 109 | - * |
|
| 110 | - * @return string Column Name |
|
| 111 | - */ |
|
| 112 | - public function column_state( $customer ) { |
|
| 113 | - $country = wpinv_sanitize_country( $customer->get( 'country' ) ); |
|
| 114 | - $state = $customer->get( 'state' ); |
|
| 115 | - if ( $state ) { |
|
| 116 | - $state = wpinv_state_name( $state, $country ); |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - return esc_html( $state ); |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * Displays the signup column. |
|
| 124 | - * |
|
| 125 | - * @since 1.0.19 |
|
| 126 | - * |
|
| 127 | - * @param GetPaid_Customer $customer |
|
| 128 | - * |
|
| 129 | - * @return string Column Name |
|
| 130 | - */ |
|
| 131 | - public function column_date_created( $customer ) { |
|
| 132 | - return getpaid_format_date_value( $customer->get( 'date_created' ) ); |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * Displays the total spent column. |
|
| 137 | - * |
|
| 138 | - * @since 1.0.19 |
|
| 139 | - * |
|
| 140 | - * @param GetPaid_Customer $customer |
|
| 141 | - * |
|
| 142 | - * @return string Column Name |
|
| 143 | - */ |
|
| 144 | - public function column_purchase_value( $customer ) { |
|
| 145 | - return wpinv_price( (float) $customer->get( 'purchase_value' ) ); |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - /** |
|
| 149 | - * Displays the total spent column. |
|
| 150 | - * |
|
| 151 | - * @since 1.0.19 |
|
| 152 | - * |
|
| 153 | - * @param GetPaid_Customer $customer |
|
| 154 | - * |
|
| 155 | - * @return string Column Name |
|
| 156 | - */ |
|
| 157 | - public function column_purchase_count( $customer ) { |
|
| 158 | - $value = $customer->get( 'purchase_count' ); |
|
| 159 | - $url = $customer->get( 'user_id' ) ? add_query_arg( array( 'post_type' => 'wpi_invoice', 'author' => $customer->get( 'user_id' ), ), admin_url( 'edit.php' ) ) : ''; |
|
| 160 | - |
|
| 161 | - return ( empty( $value ) || empty( $url ) ) ? (int) $value : '<a href="' . esc_url( $url ) . '">' . absint( $value ) . '</a>'; |
|
| 162 | - |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - /** |
|
| 166 | - * Displays the customers name |
|
| 167 | - * |
|
| 168 | - * @param GetPaid_Customer $customer customer. |
|
| 169 | - * @return string |
|
| 170 | - */ |
|
| 171 | - public function column_customer( $customer ) { |
|
| 172 | - |
|
| 173 | - $first_name = $customer->get( 'first_name' ); |
|
| 174 | - $last_name = $customer->get( 'last_name' ); |
|
| 175 | - $email = $customer->get( 'email' ); |
|
| 176 | - $avatar = get_avatar( $customer->get( 'user_id' ) ? $customer->get( 'user_id' ) : $email, 32 ); |
|
| 177 | - |
|
| 178 | - // Customer view URL. |
|
| 179 | - $view_url = $customer->get( 'user_id' ) ? esc_url( add_query_arg( 'user_id', $customer->get( 'user_id' ), admin_url( 'user-edit.php' ) ) ) : false; |
|
| 180 | - $row_actions = $view_url ? $this->row_actions( |
|
| 181 | - array( |
|
| 182 | - 'view' => '<a href="' . $view_url . '#getpaid-fieldset-billing">' . __( 'Edit Details', 'invoicing' ) . '</a>', |
|
| 183 | - ) |
|
| 184 | - ) : ''; |
|
| 185 | - |
|
| 186 | - // Customer's name. |
|
| 187 | - $name = esc_html( trim( "$first_name $last_name" ) ); |
|
| 188 | - |
|
| 189 | - if ( ! empty( $name ) ) { |
|
| 190 | - $name = "<div style='overflow: hidden;height: 18px;'>$name</div>"; |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - $email = "<div class='row-title'><a href='mailto:$email'>$email</a></div>"; |
|
| 194 | - |
|
| 195 | - return "<div style='display: flex;'><div>$avatar</div><div style='margin-left: 10px;'>$name<strong>$email</strong>$row_actions</div></div>"; |
|
| 196 | - |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - /** |
|
| 200 | - * Retrieve the current page number |
|
| 201 | - * |
|
| 202 | - * @since 1.0.19 |
|
| 203 | - * @return int Current page number |
|
| 204 | - */ |
|
| 205 | - public function get_paged() { |
|
| 206 | - return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1; |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - /** |
|
| 210 | - * Returns bulk actions. |
|
| 211 | - * |
|
| 212 | - * @since 1.0.19 |
|
| 213 | - * @return void |
|
| 214 | - */ |
|
| 215 | - public function bulk_actions( $which = '' ) { |
|
| 216 | - return array(); |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - /** |
|
| 220 | - * Prepares the display query |
|
| 221 | - */ |
|
| 222 | - public function prepare_query() { |
|
| 223 | - |
|
| 224 | - // Prepare query args. |
|
| 225 | - $query = array( |
|
| 226 | - 'number' => $this->per_page, |
|
| 227 | - 'paged' => $this->get_paged(), |
|
| 228 | - ); |
|
| 229 | - |
|
| 230 | - foreach ( array( 'orderby', 'order', 's' ) as $field ) { |
|
| 231 | - if ( isset( $_GET[ $field ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 232 | - $query[ $field ] = wpinv_clean( rawurlencode_deep( $_GET[ $field ] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 233 | - } |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - foreach ( GetPaid_Customer_Data_Store::get_database_fields() as $field => $type ) { |
|
| 237 | - |
|
| 238 | - if ( isset( $_GET[ $field ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 239 | - $query[ $field ] = wpinv_clean( rawurlencode_deep( $_GET[ $field ] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - // Min max. |
|
| 243 | - if ( '%f' === $type || '%d' === $type ) { |
|
| 244 | - |
|
| 245 | - if ( isset( $_GET[ $field . '_min' ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 246 | - $query[ $field . '_min' ] = floatval( $_GET[ $field . '_min' ] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - if ( isset( $_GET[ $field . '_max' ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 250 | - $query[ $field . '_max' ] = floatval( $_GET[ $field . '_max' ] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 251 | - } |
|
| 252 | - } |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - // Prepare class properties. |
|
| 256 | - $this->query = getpaid_get_customers( $query, 'query' ); |
|
| 257 | - $this->total_count = $this->query->get_total(); |
|
| 258 | - $this->items = $this->query->get_results(); |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - /** |
|
| 262 | - * Setup the final data for the table |
|
| 263 | - * |
|
| 264 | - */ |
|
| 265 | - public function prepare_items() { |
|
| 266 | - |
|
| 267 | - $columns = $this->get_columns(); |
|
| 268 | - $hidden = array(); |
|
| 269 | - $sortable = $this->get_sortable_columns(); |
|
| 270 | - $this->prepare_query(); |
|
| 271 | - |
|
| 272 | - $this->_column_headers = array( $columns, $hidden, $sortable ); |
|
| 273 | - |
|
| 274 | - $this->set_pagination_args( |
|
| 275 | - array( |
|
| 276 | - 'total_items' => $this->total_count, |
|
| 277 | - 'per_page' => $this->per_page, |
|
| 278 | - 'total_pages' => ceil( $this->total_count / $this->per_page ), |
|
| 279 | - ) |
|
| 280 | - ); |
|
| 281 | - } |
|
| 282 | - |
|
| 283 | - /** |
|
| 284 | - * Sortable table columns. |
|
| 285 | - * |
|
| 286 | - * @return array |
|
| 287 | - */ |
|
| 288 | - public function get_sortable_columns() { |
|
| 289 | - $sortable = array( |
|
| 290 | - 'customer' => array( 'first_name', true ), |
|
| 291 | - ); |
|
| 292 | - |
|
| 293 | - foreach ( GetPaid_Customer_Data_Store::get_database_fields() as $field => $type ) { |
|
| 294 | - $sortable[ $field ] = array( $field, true ); |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - return apply_filters( 'manage_getpaid_customers_sortable_table_columns', $sortable ); |
|
| 298 | - } |
|
| 299 | - |
|
| 300 | - /** |
|
| 301 | - * Table columns |
|
| 302 | - * |
|
| 303 | - * @return array |
|
| 304 | - */ |
|
| 305 | - public function get_columns() { |
|
| 306 | - $columns = array( |
|
| 307 | - 'customer' => __( 'Customer', 'invoicing' ), |
|
| 308 | - ); |
|
| 309 | - |
|
| 310 | - // Add address fields. |
|
| 311 | - foreach ( getpaid_user_address_fields() as $key => $value ) { |
|
| 312 | - |
|
| 313 | - // Skip id, user_id and email. |
|
| 314 | - if ( ! in_array( $key, array( 'id', 'user_id', 'email', 'purchase_value', 'purchase_count', 'date_created', 'date_modified', 'uuid', 'first_name', 'last_name' ), true ) ) { |
|
| 315 | - $columns[ $key ] = $value; |
|
| 316 | - } |
|
| 317 | - } |
|
| 318 | - |
|
| 319 | - $columns['purchase_value'] = __( 'Total Spend', 'invoicing' ); |
|
| 320 | - $columns['purchase_count'] = __( 'Invoices', 'invoicing' ); |
|
| 321 | - $columns['date_created'] = __( 'Date created', 'invoicing' ); |
|
| 322 | - |
|
| 323 | - return apply_filters( 'manage_getpaid_customers_table_columns', $columns ); |
|
| 324 | - } |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * Gets the name of the primary column. |
|
| 61 | + * |
|
| 62 | + * @since 1.0.19 |
|
| 63 | + * @access protected |
|
| 64 | + * |
|
| 65 | + * @return string Name of the primary column. |
|
| 66 | + */ |
|
| 67 | + protected function get_primary_column_name() { |
|
| 68 | + return 'customer'; |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * This function renders most of the columns in the list table. |
|
| 73 | + * |
|
| 74 | + * @since 1.0.19 |
|
| 75 | + * |
|
| 76 | + * @param GetPaid_Customer $customer |
|
| 77 | + * @param string $column_name The name of the column |
|
| 78 | + * |
|
| 79 | + * @return string Column Name |
|
| 80 | + */ |
|
| 81 | + public function column_default( $customer, $column_name ) { |
|
| 82 | + $value = esc_html( $customer->get( $column_name ) ); |
|
| 83 | + return apply_filters( 'wpinv_customers_table_column' . $column_name, $value, $customer ); |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * Displays the country column. |
|
| 88 | + * |
|
| 89 | + * @since 1.0.19 |
|
| 90 | + * |
|
| 91 | + * @param GetPaid_Customer $customer |
|
| 92 | + * |
|
| 93 | + * @return string Column Name |
|
| 94 | + */ |
|
| 95 | + public function column_country( $customer ) { |
|
| 96 | + $country = wpinv_sanitize_country( $customer->get( 'country' ) ); |
|
| 97 | + if ( $country ) { |
|
| 98 | + $country = wpinv_country_name( $country ); |
|
| 99 | + } |
|
| 100 | + return esc_html( $country ); |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * Displays the state column. |
|
| 105 | + * |
|
| 106 | + * @since 1.0.19 |
|
| 107 | + * |
|
| 108 | + * @param GetPaid_Customer $customer |
|
| 109 | + * |
|
| 110 | + * @return string Column Name |
|
| 111 | + */ |
|
| 112 | + public function column_state( $customer ) { |
|
| 113 | + $country = wpinv_sanitize_country( $customer->get( 'country' ) ); |
|
| 114 | + $state = $customer->get( 'state' ); |
|
| 115 | + if ( $state ) { |
|
| 116 | + $state = wpinv_state_name( $state, $country ); |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + return esc_html( $state ); |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * Displays the signup column. |
|
| 124 | + * |
|
| 125 | + * @since 1.0.19 |
|
| 126 | + * |
|
| 127 | + * @param GetPaid_Customer $customer |
|
| 128 | + * |
|
| 129 | + * @return string Column Name |
|
| 130 | + */ |
|
| 131 | + public function column_date_created( $customer ) { |
|
| 132 | + return getpaid_format_date_value( $customer->get( 'date_created' ) ); |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * Displays the total spent column. |
|
| 137 | + * |
|
| 138 | + * @since 1.0.19 |
|
| 139 | + * |
|
| 140 | + * @param GetPaid_Customer $customer |
|
| 141 | + * |
|
| 142 | + * @return string Column Name |
|
| 143 | + */ |
|
| 144 | + public function column_purchase_value( $customer ) { |
|
| 145 | + return wpinv_price( (float) $customer->get( 'purchase_value' ) ); |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + /** |
|
| 149 | + * Displays the total spent column. |
|
| 150 | + * |
|
| 151 | + * @since 1.0.19 |
|
| 152 | + * |
|
| 153 | + * @param GetPaid_Customer $customer |
|
| 154 | + * |
|
| 155 | + * @return string Column Name |
|
| 156 | + */ |
|
| 157 | + public function column_purchase_count( $customer ) { |
|
| 158 | + $value = $customer->get( 'purchase_count' ); |
|
| 159 | + $url = $customer->get( 'user_id' ) ? add_query_arg( array( 'post_type' => 'wpi_invoice', 'author' => $customer->get( 'user_id' ), ), admin_url( 'edit.php' ) ) : ''; |
|
| 160 | + |
|
| 161 | + return ( empty( $value ) || empty( $url ) ) ? (int) $value : '<a href="' . esc_url( $url ) . '">' . absint( $value ) . '</a>'; |
|
| 162 | + |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + /** |
|
| 166 | + * Displays the customers name |
|
| 167 | + * |
|
| 168 | + * @param GetPaid_Customer $customer customer. |
|
| 169 | + * @return string |
|
| 170 | + */ |
|
| 171 | + public function column_customer( $customer ) { |
|
| 172 | + |
|
| 173 | + $first_name = $customer->get( 'first_name' ); |
|
| 174 | + $last_name = $customer->get( 'last_name' ); |
|
| 175 | + $email = $customer->get( 'email' ); |
|
| 176 | + $avatar = get_avatar( $customer->get( 'user_id' ) ? $customer->get( 'user_id' ) : $email, 32 ); |
|
| 177 | + |
|
| 178 | + // Customer view URL. |
|
| 179 | + $view_url = $customer->get( 'user_id' ) ? esc_url( add_query_arg( 'user_id', $customer->get( 'user_id' ), admin_url( 'user-edit.php' ) ) ) : false; |
|
| 180 | + $row_actions = $view_url ? $this->row_actions( |
|
| 181 | + array( |
|
| 182 | + 'view' => '<a href="' . $view_url . '#getpaid-fieldset-billing">' . __( 'Edit Details', 'invoicing' ) . '</a>', |
|
| 183 | + ) |
|
| 184 | + ) : ''; |
|
| 185 | + |
|
| 186 | + // Customer's name. |
|
| 187 | + $name = esc_html( trim( "$first_name $last_name" ) ); |
|
| 188 | + |
|
| 189 | + if ( ! empty( $name ) ) { |
|
| 190 | + $name = "<div style='overflow: hidden;height: 18px;'>$name</div>"; |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + $email = "<div class='row-title'><a href='mailto:$email'>$email</a></div>"; |
|
| 194 | + |
|
| 195 | + return "<div style='display: flex;'><div>$avatar</div><div style='margin-left: 10px;'>$name<strong>$email</strong>$row_actions</div></div>"; |
|
| 196 | + |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + /** |
|
| 200 | + * Retrieve the current page number |
|
| 201 | + * |
|
| 202 | + * @since 1.0.19 |
|
| 203 | + * @return int Current page number |
|
| 204 | + */ |
|
| 205 | + public function get_paged() { |
|
| 206 | + return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1; |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + /** |
|
| 210 | + * Returns bulk actions. |
|
| 211 | + * |
|
| 212 | + * @since 1.0.19 |
|
| 213 | + * @return void |
|
| 214 | + */ |
|
| 215 | + public function bulk_actions( $which = '' ) { |
|
| 216 | + return array(); |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + /** |
|
| 220 | + * Prepares the display query |
|
| 221 | + */ |
|
| 222 | + public function prepare_query() { |
|
| 223 | + |
|
| 224 | + // Prepare query args. |
|
| 225 | + $query = array( |
|
| 226 | + 'number' => $this->per_page, |
|
| 227 | + 'paged' => $this->get_paged(), |
|
| 228 | + ); |
|
| 229 | + |
|
| 230 | + foreach ( array( 'orderby', 'order', 's' ) as $field ) { |
|
| 231 | + if ( isset( $_GET[ $field ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 232 | + $query[ $field ] = wpinv_clean( rawurlencode_deep( $_GET[ $field ] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 233 | + } |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + foreach ( GetPaid_Customer_Data_Store::get_database_fields() as $field => $type ) { |
|
| 237 | + |
|
| 238 | + if ( isset( $_GET[ $field ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 239 | + $query[ $field ] = wpinv_clean( rawurlencode_deep( $_GET[ $field ] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + // Min max. |
|
| 243 | + if ( '%f' === $type || '%d' === $type ) { |
|
| 244 | + |
|
| 245 | + if ( isset( $_GET[ $field . '_min' ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 246 | + $query[ $field . '_min' ] = floatval( $_GET[ $field . '_min' ] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + if ( isset( $_GET[ $field . '_max' ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 250 | + $query[ $field . '_max' ] = floatval( $_GET[ $field . '_max' ] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 251 | + } |
|
| 252 | + } |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + // Prepare class properties. |
|
| 256 | + $this->query = getpaid_get_customers( $query, 'query' ); |
|
| 257 | + $this->total_count = $this->query->get_total(); |
|
| 258 | + $this->items = $this->query->get_results(); |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + /** |
|
| 262 | + * Setup the final data for the table |
|
| 263 | + * |
|
| 264 | + */ |
|
| 265 | + public function prepare_items() { |
|
| 266 | + |
|
| 267 | + $columns = $this->get_columns(); |
|
| 268 | + $hidden = array(); |
|
| 269 | + $sortable = $this->get_sortable_columns(); |
|
| 270 | + $this->prepare_query(); |
|
| 271 | + |
|
| 272 | + $this->_column_headers = array( $columns, $hidden, $sortable ); |
|
| 273 | + |
|
| 274 | + $this->set_pagination_args( |
|
| 275 | + array( |
|
| 276 | + 'total_items' => $this->total_count, |
|
| 277 | + 'per_page' => $this->per_page, |
|
| 278 | + 'total_pages' => ceil( $this->total_count / $this->per_page ), |
|
| 279 | + ) |
|
| 280 | + ); |
|
| 281 | + } |
|
| 282 | + |
|
| 283 | + /** |
|
| 284 | + * Sortable table columns. |
|
| 285 | + * |
|
| 286 | + * @return array |
|
| 287 | + */ |
|
| 288 | + public function get_sortable_columns() { |
|
| 289 | + $sortable = array( |
|
| 290 | + 'customer' => array( 'first_name', true ), |
|
| 291 | + ); |
|
| 292 | + |
|
| 293 | + foreach ( GetPaid_Customer_Data_Store::get_database_fields() as $field => $type ) { |
|
| 294 | + $sortable[ $field ] = array( $field, true ); |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + return apply_filters( 'manage_getpaid_customers_sortable_table_columns', $sortable ); |
|
| 298 | + } |
|
| 299 | + |
|
| 300 | + /** |
|
| 301 | + * Table columns |
|
| 302 | + * |
|
| 303 | + * @return array |
|
| 304 | + */ |
|
| 305 | + public function get_columns() { |
|
| 306 | + $columns = array( |
|
| 307 | + 'customer' => __( 'Customer', 'invoicing' ), |
|
| 308 | + ); |
|
| 309 | + |
|
| 310 | + // Add address fields. |
|
| 311 | + foreach ( getpaid_user_address_fields() as $key => $value ) { |
|
| 312 | + |
|
| 313 | + // Skip id, user_id and email. |
|
| 314 | + if ( ! in_array( $key, array( 'id', 'user_id', 'email', 'purchase_value', 'purchase_count', 'date_created', 'date_modified', 'uuid', 'first_name', 'last_name' ), true ) ) { |
|
| 315 | + $columns[ $key ] = $value; |
|
| 316 | + } |
|
| 317 | + } |
|
| 318 | + |
|
| 319 | + $columns['purchase_value'] = __( 'Total Spend', 'invoicing' ); |
|
| 320 | + $columns['purchase_count'] = __( 'Invoices', 'invoicing' ); |
|
| 321 | + $columns['date_created'] = __( 'Date created', 'invoicing' ); |
|
| 322 | + |
|
| 323 | + return apply_filters( 'manage_getpaid_customers_table_columns', $columns ); |
|
| 324 | + } |
|
| 325 | 325 | } |
@@ -67,7 +67,7 @@ discard block |
||
| 67 | 67 | * Checks if the current user cna view an invoice receipt. |
| 68 | 68 | */ |
| 69 | 69 | function wpinv_can_view_receipt( $invoice ) { |
| 70 | - return (bool) apply_filters( 'wpinv_can_view_receipt', wpinv_user_can_view_invoice( $invoice ), $invoice ); |
|
| 70 | + return (bool) apply_filters( 'wpinv_can_view_receipt', wpinv_user_can_view_invoice( $invoice ), $invoice ); |
|
| 71 | 71 | } |
| 72 | 72 | |
| 73 | 73 | /** |
@@ -556,37 +556,37 @@ discard block |
||
| 556 | 556 | $label = empty( $label ) ? __( 'Invoice', 'invoicing' ) : sanitize_text_field( $label ); |
| 557 | 557 | $columns = array( |
| 558 | 558 | |
| 559 | - 'invoice-number' => array( |
|
| 560 | - 'title' => $label, |
|
| 561 | - 'class' => 'text-left', |
|
| 562 | - ), |
|
| 559 | + 'invoice-number' => array( |
|
| 560 | + 'title' => $label, |
|
| 561 | + 'class' => 'text-left', |
|
| 562 | + ), |
|
| 563 | 563 | |
| 564 | - 'created-date' => array( |
|
| 565 | - 'title' => __( 'Created Date', 'invoicing' ), |
|
| 566 | - 'class' => 'text-left', |
|
| 567 | - ), |
|
| 564 | + 'created-date' => array( |
|
| 565 | + 'title' => __( 'Created Date', 'invoicing' ), |
|
| 566 | + 'class' => 'text-left', |
|
| 567 | + ), |
|
| 568 | 568 | |
| 569 | - 'payment-date' => array( |
|
| 570 | - 'title' => __( 'Payment Date', 'invoicing' ), |
|
| 571 | - 'class' => 'text-left', |
|
| 572 | - ), |
|
| 569 | + 'payment-date' => array( |
|
| 570 | + 'title' => __( 'Payment Date', 'invoicing' ), |
|
| 571 | + 'class' => 'text-left', |
|
| 572 | + ), |
|
| 573 | 573 | |
| 574 | - 'invoice-status' => array( |
|
| 575 | - 'title' => __( 'Status', 'invoicing' ), |
|
| 576 | - 'class' => 'text-center', |
|
| 577 | - ), |
|
| 574 | + 'invoice-status' => array( |
|
| 575 | + 'title' => __( 'Status', 'invoicing' ), |
|
| 576 | + 'class' => 'text-center', |
|
| 577 | + ), |
|
| 578 | 578 | |
| 579 | - 'invoice-total' => array( |
|
| 580 | - 'title' => __( 'Total', 'invoicing' ), |
|
| 581 | - 'class' => 'text-right', |
|
| 582 | - ), |
|
| 579 | + 'invoice-total' => array( |
|
| 580 | + 'title' => __( 'Total', 'invoicing' ), |
|
| 581 | + 'class' => 'text-right', |
|
| 582 | + ), |
|
| 583 | 583 | |
| 584 | - 'invoice-actions' => array( |
|
| 585 | - 'title' => ' ', |
|
| 586 | - 'class' => 'text-center', |
|
| 587 | - ), |
|
| 584 | + 'invoice-actions' => array( |
|
| 585 | + 'title' => ' ', |
|
| 586 | + 'class' => 'text-center', |
|
| 587 | + ), |
|
| 588 | 588 | |
| 589 | - ); |
|
| 589 | + ); |
|
| 590 | 590 | |
| 591 | 591 | return apply_filters( 'wpinv_user_invoices_columns', $columns, $post_type ); |
| 592 | 592 | } |
@@ -1297,22 +1297,22 @@ discard block |
||
| 1297 | 1297 | */ |
| 1298 | 1298 | function getpaid_get_invoice_status_classes() { |
| 1299 | 1299 | |
| 1300 | - return apply_filters( |
|
| 1301 | - 'getpaid_get_invoice_status_classes', |
|
| 1302 | - array( |
|
| 1300 | + return apply_filters( |
|
| 1301 | + 'getpaid_get_invoice_status_classes', |
|
| 1302 | + array( |
|
| 1303 | 1303 | 'wpi-quote-declined' => 'bg-danger', |
| 1304 | 1304 | 'wpi-failed' => 'bg-danger', |
| 1305 | - 'wpi-processing' => 'bg-info', |
|
| 1306 | - 'wpi-onhold' => 'bg-warning text-dark', |
|
| 1307 | - 'wpi-quote-accepted' => 'bg-success', |
|
| 1308 | - 'publish' => 'bg-success', |
|
| 1309 | - 'wpi-renewal' => 'bg-primary', |
|
| 1305 | + 'wpi-processing' => 'bg-info', |
|
| 1306 | + 'wpi-onhold' => 'bg-warning text-dark', |
|
| 1307 | + 'wpi-quote-accepted' => 'bg-success', |
|
| 1308 | + 'publish' => 'bg-success', |
|
| 1309 | + 'wpi-renewal' => 'bg-primary', |
|
| 1310 | 1310 | 'wpi-cancelled' => 'bg-secondary', |
| 1311 | 1311 | 'wpi-pending' => 'bg-dark text-white', |
| 1312 | 1312 | 'wpi-quote-pending' => 'bg-dark text-white', |
| 1313 | 1313 | 'wpi-refunded' => 'bg-secondary', |
| 1314 | - ) |
|
| 1315 | - ); |
|
| 1314 | + ) |
|
| 1315 | + ); |
|
| 1316 | 1316 | |
| 1317 | 1317 | } |
| 1318 | 1318 | |
@@ -1326,7 +1326,7 @@ discard block |
||
| 1326 | 1326 | function getpaid_get_invoice_tax_rate( $invoice, $item ) { |
| 1327 | 1327 | |
| 1328 | 1328 | $rates = getpaid_get_item_tax_rates( $item, $invoice->get_country(), $invoice->get_state() ); |
| 1329 | - $rates = getpaid_filter_item_tax_rates( $item, $rates ); |
|
| 1329 | + $rates = getpaid_filter_item_tax_rates( $item, $rates ); |
|
| 1330 | 1330 | $rates = wp_list_pluck( $rates, 'rate' ); |
| 1331 | 1331 | |
| 1332 | 1332 | return array_sum( $rates ); |
@@ -20,411 +20,411 @@ discard block |
||
| 20 | 20 | */ |
| 21 | 21 | class GetPaid_Installer { |
| 22 | 22 | |
| 23 | - private static $schema = null; |
|
| 24 | - private static $schema_version = null; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * Upgrades the install. |
|
| 28 | - * |
|
| 29 | - * @param string $upgrade_from The current invoicing version. |
|
| 30 | - */ |
|
| 31 | - public function upgrade_db( $upgrade_from ) { |
|
| 32 | - |
|
| 33 | - // Save the current invoicing version. |
|
| 34 | - update_option( 'wpinv_version', WPINV_VERSION ); |
|
| 35 | - |
|
| 36 | - // Setup the invoice Custom Post Type. |
|
| 37 | - GetPaid_Post_Types::register_post_types(); |
|
| 38 | - |
|
| 39 | - // Clear the permalinks |
|
| 40 | - flush_rewrite_rules(); |
|
| 41 | - |
|
| 42 | - // Maybe create new/missing pages. |
|
| 43 | - $this->create_pages(); |
|
| 44 | - |
|
| 45 | - // Maybe re(add) admin capabilities. |
|
| 46 | - $this->add_capabilities(); |
|
| 47 | - |
|
| 48 | - // Maybe create the default payment form. |
|
| 49 | - wpinv_get_default_payment_form(); |
|
| 50 | - |
|
| 51 | - // Create any missing database tables. |
|
| 52 | - $method = "upgrade_from_$upgrade_from"; |
|
| 53 | - |
|
| 54 | - $installed = get_option( 'gepaid_installed_on' ); |
|
| 55 | - |
|
| 56 | - if ( empty( $installed ) ) { |
|
| 57 | - update_option( 'gepaid_installed_on', time() ); |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - if ( method_exists( $this, $method ) ) { |
|
| 61 | - $this->$method(); |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * Do a fresh install. |
|
| 68 | - * |
|
| 69 | - */ |
|
| 70 | - public function upgrade_from_0() { |
|
| 71 | - |
|
| 72 | - // Save default tax rates. |
|
| 73 | - update_option( 'wpinv_tax_rates', wpinv_get_data( 'tax-rates' ) ); |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * Upgrade to 0.0.5 |
|
| 78 | - * |
|
| 79 | - */ |
|
| 80 | - public function upgrade_from_004() { |
|
| 81 | - global $wpdb; |
|
| 82 | - |
|
| 83 | - // Invoices. |
|
| 84 | - $results = $wpdb->get_results( "SELECT ID FROM {$wpdb->posts} WHERE post_type = 'wpi_invoice' AND post_status IN( 'pending', 'processing', 'onhold', 'refunded', 'cancelled', 'failed', 'renewal' )" ); |
|
| 85 | - if ( ! empty( $results ) ) { |
|
| 86 | - $wpdb->query( "UPDATE {$wpdb->posts} SET post_status = CONCAT( 'wpi-', post_status ) WHERE post_type = 'wpi_invoice' AND post_status IN( 'pending', 'processing', 'onhold', 'refunded', 'cancelled', 'failed', 'renewal' )" ); |
|
| 87 | - |
|
| 88 | - // Clean post cache |
|
| 89 | - foreach ( $results as $row ) { |
|
| 90 | - clean_post_cache( $row->ID ); |
|
| 91 | - } |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - // Item meta key changes |
|
| 95 | - $query = 'SELECT DISTINCT post_id FROM ' . $wpdb->postmeta . " WHERE meta_key IN( '_wpinv_item_id', '_wpinv_package_id', '_wpinv_post_id', '_wpinv_cpt_name', '_wpinv_cpt_singular_name' )"; |
|
| 96 | - $results = $wpdb->get_results( $query ); |
|
| 97 | - |
|
| 98 | - if ( ! empty( $results ) ) { |
|
| 99 | - $wpdb->query( 'UPDATE ' . $wpdb->postmeta . " SET meta_key = '_wpinv_custom_id' WHERE meta_key IN( '_wpinv_item_id', '_wpinv_package_id', '_wpinv_post_id' )" ); |
|
| 100 | - $wpdb->query( 'UPDATE ' . $wpdb->postmeta . " SET meta_key = '_wpinv_custom_name' WHERE meta_key = '_wpinv_cpt_name'" ); |
|
| 101 | - $wpdb->query( 'UPDATE ' . $wpdb->postmeta . " SET meta_key = '_wpinv_custom_singular_name' WHERE meta_key = '_wpinv_cpt_singular_name'" ); |
|
| 102 | - |
|
| 103 | - foreach ( $results as $row ) { |
|
| 104 | - clean_post_cache( $row->post_id ); |
|
| 105 | - } |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - $this->upgrade_from_118(); |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * Upgrade to version 2.0.0. |
|
| 113 | - * |
|
| 114 | - */ |
|
| 115 | - public function upgrade_from_118() { |
|
| 116 | - $this->migrate_old_invoices(); |
|
| 117 | - $this->upgrade_from_279(); |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * Upgrade to version 2.0.0. |
|
| 122 | - * |
|
| 123 | - */ |
|
| 124 | - public function upgrade_from_279() { |
|
| 125 | - self::migrate_old_customers(); |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * Give administrators the capability to manage GetPaid. |
|
| 130 | - * |
|
| 131 | - */ |
|
| 132 | - public function add_capabilities() { |
|
| 133 | - $GLOBALS['wp_roles']->add_cap( 'administrator', 'manage_invoicing' ); |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Retreives GetPaid pages. |
|
| 138 | - * |
|
| 139 | - */ |
|
| 140 | - public static function get_pages() { |
|
| 141 | - |
|
| 142 | - return apply_filters( |
|
| 143 | - 'wpinv_create_pages', |
|
| 144 | - array( |
|
| 145 | - |
|
| 146 | - // Checkout page. |
|
| 147 | - 'checkout_page' => array( |
|
| 148 | - 'name' => _x( 'gp-checkout', 'Page slug', 'invoicing' ), |
|
| 149 | - 'title' => _x( 'Checkout', 'Page title', 'invoicing' ), |
|
| 150 | - 'content' => ' |
|
| 23 | + private static $schema = null; |
|
| 24 | + private static $schema_version = null; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * Upgrades the install. |
|
| 28 | + * |
|
| 29 | + * @param string $upgrade_from The current invoicing version. |
|
| 30 | + */ |
|
| 31 | + public function upgrade_db( $upgrade_from ) { |
|
| 32 | + |
|
| 33 | + // Save the current invoicing version. |
|
| 34 | + update_option( 'wpinv_version', WPINV_VERSION ); |
|
| 35 | + |
|
| 36 | + // Setup the invoice Custom Post Type. |
|
| 37 | + GetPaid_Post_Types::register_post_types(); |
|
| 38 | + |
|
| 39 | + // Clear the permalinks |
|
| 40 | + flush_rewrite_rules(); |
|
| 41 | + |
|
| 42 | + // Maybe create new/missing pages. |
|
| 43 | + $this->create_pages(); |
|
| 44 | + |
|
| 45 | + // Maybe re(add) admin capabilities. |
|
| 46 | + $this->add_capabilities(); |
|
| 47 | + |
|
| 48 | + // Maybe create the default payment form. |
|
| 49 | + wpinv_get_default_payment_form(); |
|
| 50 | + |
|
| 51 | + // Create any missing database tables. |
|
| 52 | + $method = "upgrade_from_$upgrade_from"; |
|
| 53 | + |
|
| 54 | + $installed = get_option( 'gepaid_installed_on' ); |
|
| 55 | + |
|
| 56 | + if ( empty( $installed ) ) { |
|
| 57 | + update_option( 'gepaid_installed_on', time() ); |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + if ( method_exists( $this, $method ) ) { |
|
| 61 | + $this->$method(); |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * Do a fresh install. |
|
| 68 | + * |
|
| 69 | + */ |
|
| 70 | + public function upgrade_from_0() { |
|
| 71 | + |
|
| 72 | + // Save default tax rates. |
|
| 73 | + update_option( 'wpinv_tax_rates', wpinv_get_data( 'tax-rates' ) ); |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * Upgrade to 0.0.5 |
|
| 78 | + * |
|
| 79 | + */ |
|
| 80 | + public function upgrade_from_004() { |
|
| 81 | + global $wpdb; |
|
| 82 | + |
|
| 83 | + // Invoices. |
|
| 84 | + $results = $wpdb->get_results( "SELECT ID FROM {$wpdb->posts} WHERE post_type = 'wpi_invoice' AND post_status IN( 'pending', 'processing', 'onhold', 'refunded', 'cancelled', 'failed', 'renewal' )" ); |
|
| 85 | + if ( ! empty( $results ) ) { |
|
| 86 | + $wpdb->query( "UPDATE {$wpdb->posts} SET post_status = CONCAT( 'wpi-', post_status ) WHERE post_type = 'wpi_invoice' AND post_status IN( 'pending', 'processing', 'onhold', 'refunded', 'cancelled', 'failed', 'renewal' )" ); |
|
| 87 | + |
|
| 88 | + // Clean post cache |
|
| 89 | + foreach ( $results as $row ) { |
|
| 90 | + clean_post_cache( $row->ID ); |
|
| 91 | + } |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + // Item meta key changes |
|
| 95 | + $query = 'SELECT DISTINCT post_id FROM ' . $wpdb->postmeta . " WHERE meta_key IN( '_wpinv_item_id', '_wpinv_package_id', '_wpinv_post_id', '_wpinv_cpt_name', '_wpinv_cpt_singular_name' )"; |
|
| 96 | + $results = $wpdb->get_results( $query ); |
|
| 97 | + |
|
| 98 | + if ( ! empty( $results ) ) { |
|
| 99 | + $wpdb->query( 'UPDATE ' . $wpdb->postmeta . " SET meta_key = '_wpinv_custom_id' WHERE meta_key IN( '_wpinv_item_id', '_wpinv_package_id', '_wpinv_post_id' )" ); |
|
| 100 | + $wpdb->query( 'UPDATE ' . $wpdb->postmeta . " SET meta_key = '_wpinv_custom_name' WHERE meta_key = '_wpinv_cpt_name'" ); |
|
| 101 | + $wpdb->query( 'UPDATE ' . $wpdb->postmeta . " SET meta_key = '_wpinv_custom_singular_name' WHERE meta_key = '_wpinv_cpt_singular_name'" ); |
|
| 102 | + |
|
| 103 | + foreach ( $results as $row ) { |
|
| 104 | + clean_post_cache( $row->post_id ); |
|
| 105 | + } |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + $this->upgrade_from_118(); |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * Upgrade to version 2.0.0. |
|
| 113 | + * |
|
| 114 | + */ |
|
| 115 | + public function upgrade_from_118() { |
|
| 116 | + $this->migrate_old_invoices(); |
|
| 117 | + $this->upgrade_from_279(); |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * Upgrade to version 2.0.0. |
|
| 122 | + * |
|
| 123 | + */ |
|
| 124 | + public function upgrade_from_279() { |
|
| 125 | + self::migrate_old_customers(); |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * Give administrators the capability to manage GetPaid. |
|
| 130 | + * |
|
| 131 | + */ |
|
| 132 | + public function add_capabilities() { |
|
| 133 | + $GLOBALS['wp_roles']->add_cap( 'administrator', 'manage_invoicing' ); |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Retreives GetPaid pages. |
|
| 138 | + * |
|
| 139 | + */ |
|
| 140 | + public static function get_pages() { |
|
| 141 | + |
|
| 142 | + return apply_filters( |
|
| 143 | + 'wpinv_create_pages', |
|
| 144 | + array( |
|
| 145 | + |
|
| 146 | + // Checkout page. |
|
| 147 | + 'checkout_page' => array( |
|
| 148 | + 'name' => _x( 'gp-checkout', 'Page slug', 'invoicing' ), |
|
| 149 | + 'title' => _x( 'Checkout', 'Page title', 'invoicing' ), |
|
| 150 | + 'content' => ' |
|
| 151 | 151 | <!-- wp:shortcode --> |
| 152 | 152 | [wpinv_checkout] |
| 153 | 153 | <!-- /wp:shortcode --> |
| 154 | 154 | ', |
| 155 | - 'parent' => '', |
|
| 156 | - ), |
|
| 157 | - |
|
| 158 | - // Invoice history page. |
|
| 159 | - 'invoice_history_page' => array( |
|
| 160 | - 'name' => _x( 'gp-invoices', 'Page slug', 'invoicing' ), |
|
| 161 | - 'title' => _x( 'My Invoices', 'Page title', 'invoicing' ), |
|
| 162 | - 'content' => ' |
|
| 155 | + 'parent' => '', |
|
| 156 | + ), |
|
| 157 | + |
|
| 158 | + // Invoice history page. |
|
| 159 | + 'invoice_history_page' => array( |
|
| 160 | + 'name' => _x( 'gp-invoices', 'Page slug', 'invoicing' ), |
|
| 161 | + 'title' => _x( 'My Invoices', 'Page title', 'invoicing' ), |
|
| 162 | + 'content' => ' |
|
| 163 | 163 | <!-- wp:shortcode --> |
| 164 | 164 | [wpinv_history] |
| 165 | 165 | <!-- /wp:shortcode --> |
| 166 | 166 | ', |
| 167 | - 'parent' => '', |
|
| 168 | - ), |
|
| 169 | - |
|
| 170 | - // Success page content. |
|
| 171 | - 'success_page' => array( |
|
| 172 | - 'name' => _x( 'gp-receipt', 'Page slug', 'invoicing' ), |
|
| 173 | - 'title' => _x( 'Payment Confirmation', 'Page title', 'invoicing' ), |
|
| 174 | - 'content' => ' |
|
| 167 | + 'parent' => '', |
|
| 168 | + ), |
|
| 169 | + |
|
| 170 | + // Success page content. |
|
| 171 | + 'success_page' => array( |
|
| 172 | + 'name' => _x( 'gp-receipt', 'Page slug', 'invoicing' ), |
|
| 173 | + 'title' => _x( 'Payment Confirmation', 'Page title', 'invoicing' ), |
|
| 174 | + 'content' => ' |
|
| 175 | 175 | <!-- wp:shortcode --> |
| 176 | 176 | [wpinv_receipt] |
| 177 | 177 | <!-- /wp:shortcode --> |
| 178 | 178 | ', |
| 179 | - 'parent' => 'gp-checkout', |
|
| 180 | - ), |
|
| 181 | - |
|
| 182 | - // Failure page content. |
|
| 183 | - 'failure_page' => array( |
|
| 184 | - 'name' => _x( 'gp-transaction-failed', 'Page slug', 'invoicing' ), |
|
| 185 | - 'title' => _x( 'Transaction Failed', 'Page title', 'invoicing' ), |
|
| 186 | - 'content' => __( 'Your transaction failed, please try again or contact site support.', 'invoicing' ), |
|
| 187 | - 'parent' => 'gp-checkout', |
|
| 188 | - ), |
|
| 189 | - |
|
| 190 | - // Subscriptions history page. |
|
| 191 | - 'invoice_subscription_page' => array( |
|
| 192 | - 'name' => _x( 'gp-subscriptions', 'Page slug', 'invoicing' ), |
|
| 193 | - 'title' => _x( 'My Subscriptions', 'Page title', 'invoicing' ), |
|
| 194 | - 'content' => ' |
|
| 179 | + 'parent' => 'gp-checkout', |
|
| 180 | + ), |
|
| 181 | + |
|
| 182 | + // Failure page content. |
|
| 183 | + 'failure_page' => array( |
|
| 184 | + 'name' => _x( 'gp-transaction-failed', 'Page slug', 'invoicing' ), |
|
| 185 | + 'title' => _x( 'Transaction Failed', 'Page title', 'invoicing' ), |
|
| 186 | + 'content' => __( 'Your transaction failed, please try again or contact site support.', 'invoicing' ), |
|
| 187 | + 'parent' => 'gp-checkout', |
|
| 188 | + ), |
|
| 189 | + |
|
| 190 | + // Subscriptions history page. |
|
| 191 | + 'invoice_subscription_page' => array( |
|
| 192 | + 'name' => _x( 'gp-subscriptions', 'Page slug', 'invoicing' ), |
|
| 193 | + 'title' => _x( 'My Subscriptions', 'Page title', 'invoicing' ), |
|
| 194 | + 'content' => ' |
|
| 195 | 195 | <!-- wp:shortcode --> |
| 196 | 196 | [wpinv_subscriptions] |
| 197 | 197 | <!-- /wp:shortcode --> |
| 198 | 198 | ', |
| 199 | - 'parent' => '', |
|
| 200 | - ), |
|
| 201 | - |
|
| 202 | - ) |
|
| 203 | - ); |
|
| 204 | - |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - /** |
|
| 208 | - * Re-create GetPaid pages. |
|
| 209 | - * |
|
| 210 | - */ |
|
| 211 | - public function create_pages() { |
|
| 212 | - |
|
| 213 | - foreach ( self::get_pages() as $key => $page ) { |
|
| 214 | - wpinv_create_page( esc_sql( $page['name'] ), $key, $page['title'], $page['content'], $page['parent'] ); |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - /** |
|
| 220 | - * Migrates old invoices to new invoices. |
|
| 221 | - * |
|
| 222 | - */ |
|
| 223 | - public function migrate_old_invoices() { |
|
| 224 | - global $wpdb; |
|
| 225 | - |
|
| 226 | - $invoices_table = $wpdb->prefix . 'getpaid_invoices'; |
|
| 227 | - $invoice_items_table = $wpdb->prefix . 'getpaid_invoice_items'; |
|
| 228 | - $migrated = $wpdb->get_col( "SELECT post_id FROM $invoices_table" ); |
|
| 229 | - $invoices = array_unique( |
|
| 230 | - get_posts( |
|
| 231 | - array( |
|
| 232 | - 'post_type' => array( 'wpi_invoice', 'wpi_quote' ), |
|
| 233 | - 'posts_per_page' => -1, |
|
| 234 | - 'fields' => 'ids', |
|
| 235 | - 'post_status' => array_keys( get_post_stati() ), |
|
| 236 | - 'exclude' => (array) $migrated, |
|
| 237 | - ) |
|
| 238 | - ) |
|
| 239 | - ); |
|
| 240 | - |
|
| 241 | - // Abort if we do not have any invoices. |
|
| 242 | - if ( empty( $invoices ) ) { |
|
| 243 | - return; |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - require_once WPINV_PLUGIN_DIR . 'includes/class-wpinv-legacy-invoice.php'; |
|
| 247 | - |
|
| 248 | - $invoice_rows = array(); |
|
| 249 | - foreach ( $invoices as $invoice ) { |
|
| 250 | - |
|
| 251 | - $invoice = new WPInv_Legacy_Invoice( $invoice ); |
|
| 252 | - |
|
| 253 | - if ( empty( $invoice->ID ) ) { |
|
| 254 | - return; |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - $fields = array( |
|
| 258 | - 'post_id' => $invoice->ID, |
|
| 259 | - 'number' => $invoice->get_number(), |
|
| 260 | - 'key' => $invoice->get_key(), |
|
| 261 | - 'type' => str_replace( 'wpi_', '', $invoice->post_type ), |
|
| 262 | - 'mode' => $invoice->mode, |
|
| 263 | - 'user_ip' => $invoice->get_ip(), |
|
| 264 | - 'first_name' => $invoice->get_first_name(), |
|
| 265 | - 'last_name' => $invoice->get_last_name(), |
|
| 266 | - 'address' => $invoice->get_address(), |
|
| 267 | - 'city' => $invoice->city, |
|
| 268 | - 'state' => $invoice->state, |
|
| 269 | - 'country' => $invoice->country, |
|
| 270 | - 'zip' => $invoice->zip, |
|
| 271 | - 'adddress_confirmed' => (int) $invoice->adddress_confirmed, |
|
| 272 | - 'gateway' => $invoice->get_gateway(), |
|
| 273 | - 'transaction_id' => $invoice->get_transaction_id(), |
|
| 274 | - 'currency' => $invoice->get_currency(), |
|
| 275 | - 'subtotal' => $invoice->get_subtotal(), |
|
| 276 | - 'tax' => $invoice->get_tax(), |
|
| 277 | - 'fees_total' => $invoice->get_fees_total(), |
|
| 278 | - 'total' => $invoice->get_total(), |
|
| 279 | - 'discount' => $invoice->get_discount(), |
|
| 280 | - 'discount_code' => $invoice->get_discount_code(), |
|
| 281 | - 'disable_taxes' => $invoice->disable_taxes, |
|
| 282 | - 'due_date' => $invoice->get_due_date(), |
|
| 283 | - 'completed_date' => $invoice->get_completed_date(), |
|
| 284 | - 'company' => $invoice->company, |
|
| 285 | - 'vat_number' => $invoice->vat_number, |
|
| 286 | - 'vat_rate' => $invoice->vat_rate, |
|
| 287 | - 'custom_meta' => $invoice->payment_meta, |
|
| 288 | - ); |
|
| 289 | - |
|
| 290 | - foreach ( $fields as $key => $val ) { |
|
| 291 | - if ( is_null( $val ) ) { |
|
| 292 | - $val = ''; |
|
| 293 | - } |
|
| 294 | - $val = maybe_serialize( $val ); |
|
| 295 | - $fields[ $key ] = $wpdb->prepare( '%s', $val ); |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - $fields = implode( ', ', $fields ); |
|
| 299 | - $invoice_rows[] = "($fields)"; |
|
| 300 | - |
|
| 301 | - $item_rows = array(); |
|
| 302 | - $item_columns = array(); |
|
| 303 | - foreach ( $invoice->get_cart_details() as $details ) { |
|
| 304 | - $fields = array( |
|
| 305 | - 'post_id' => $invoice->ID, |
|
| 306 | - 'item_id' => $details['id'], |
|
| 307 | - 'item_name' => $details['name'], |
|
| 308 | - 'item_description' => empty( $details['meta']['description'] ) ? '' : $details['meta']['description'], |
|
| 309 | - 'vat_rate' => $details['vat_rate'], |
|
| 310 | - 'vat_class' => empty( $details['vat_class'] ) ? '_standard' : $details['vat_class'], |
|
| 311 | - 'tax' => $details['tax'], |
|
| 312 | - 'item_price' => $details['item_price'], |
|
| 313 | - 'custom_price' => $details['custom_price'], |
|
| 314 | - 'quantity' => $details['quantity'], |
|
| 315 | - 'discount' => $details['discount'], |
|
| 316 | - 'subtotal' => $details['subtotal'], |
|
| 317 | - 'price' => $details['price'], |
|
| 318 | - 'meta' => $details['meta'], |
|
| 319 | - 'fees' => $details['fees'], |
|
| 320 | - ); |
|
| 321 | - |
|
| 322 | - $item_columns = array_keys( $fields ); |
|
| 323 | - |
|
| 324 | - foreach ( $fields as $key => $val ) { |
|
| 325 | - if ( is_null( $val ) ) { |
|
| 326 | - $val = ''; |
|
| 327 | - } |
|
| 328 | - $val = maybe_serialize( $val ); |
|
| 329 | - $fields[ $key ] = $wpdb->prepare( '%s', $val ); |
|
| 330 | - } |
|
| 331 | - |
|
| 332 | - $fields = implode( ', ', $fields ); |
|
| 333 | - $item_rows[] = "($fields)"; |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - $item_rows = implode( ', ', $item_rows ); |
|
| 337 | - $item_columns = implode( ', ', $item_columns ); |
|
| 338 | - $wpdb->query( "INSERT INTO $invoice_items_table ($item_columns) VALUES $item_rows" ); |
|
| 339 | - } |
|
| 340 | - |
|
| 341 | - if ( empty( $invoice_rows ) ) { |
|
| 342 | - return; |
|
| 343 | - } |
|
| 344 | - |
|
| 345 | - $invoice_rows = implode( ', ', $invoice_rows ); |
|
| 346 | - $wpdb->query( "INSERT INTO $invoices_table VALUES $invoice_rows" ); |
|
| 347 | - |
|
| 348 | - } |
|
| 349 | - |
|
| 350 | - /** |
|
| 351 | - * Migrates old customers to new table. |
|
| 352 | - * |
|
| 353 | - */ |
|
| 354 | - public static function migrate_old_customers() { |
|
| 355 | - global $wpdb; |
|
| 356 | - |
|
| 357 | - // Fetch post_id from $wpdb->prefix . 'getpaid_invoices' where customer_id = 0 or null. |
|
| 358 | - $invoice_ids = $wpdb->get_col( "SELECT post_id FROM {$wpdb->prefix}getpaid_invoices WHERE customer_id = 0 OR customer_id IS NULL" ); |
|
| 359 | - |
|
| 360 | - foreach ( $invoice_ids as $invoice_id ) { |
|
| 361 | - $invoice = wpinv_get_invoice( $invoice_id ); |
|
| 362 | - |
|
| 363 | - if ( empty( $invoice ) ) { |
|
| 364 | - continue; |
|
| 365 | - } |
|
| 366 | - |
|
| 367 | - // Fetch customer from the user ID. |
|
| 368 | - $user_id = $invoice->get_user_id(); |
|
| 369 | - |
|
| 370 | - if ( empty( $user_id ) ) { |
|
| 371 | - continue; |
|
| 372 | - } |
|
| 373 | - |
|
| 374 | - $customer = getpaid_get_customer_by_user_id( $user_id ); |
|
| 375 | - |
|
| 376 | - // Create if not exists. |
|
| 377 | - if ( empty( $customer ) ) { |
|
| 378 | - $customer = new GetPaid_Customer( 0 ); |
|
| 379 | - $customer->clone_user( $user_id ); |
|
| 380 | - $customer->save(); |
|
| 381 | - } |
|
| 382 | - |
|
| 383 | - $invoice->set_customer_id( $customer->get_id() ); |
|
| 384 | - $invoice->save(); |
|
| 385 | - } |
|
| 386 | - |
|
| 387 | - } |
|
| 388 | - |
|
| 389 | - /** |
|
| 390 | - * Migrates old invoices to new invoices. |
|
| 391 | - * |
|
| 392 | - */ |
|
| 393 | - public static function rename_gateways_label() { |
|
| 394 | - global $wpdb; |
|
| 395 | - |
|
| 396 | - foreach ( array_keys( wpinv_get_payment_gateways() ) as $gateway ) { |
|
| 397 | - |
|
| 398 | - $wpdb->update( |
|
| 399 | - $wpdb->prefix . 'getpaid_invoices', |
|
| 400 | - array( 'gateway' => $gateway ), |
|
| 401 | - array( 'gateway' => wpinv_get_gateway_admin_label( $gateway ) ), |
|
| 402 | - '%s', |
|
| 403 | - '%s' |
|
| 404 | - ); |
|
| 405 | - |
|
| 406 | - } |
|
| 407 | - } |
|
| 408 | - |
|
| 409 | - /** |
|
| 410 | - * Returns the DB schema. |
|
| 411 | - * |
|
| 412 | - */ |
|
| 413 | - public static function get_db_schema() { |
|
| 414 | - global $wpdb; |
|
| 415 | - |
|
| 416 | - if ( ! empty( self::$schema ) ) { |
|
| 417 | - return self::$schema; |
|
| 418 | - } |
|
| 199 | + 'parent' => '', |
|
| 200 | + ), |
|
| 201 | + |
|
| 202 | + ) |
|
| 203 | + ); |
|
| 204 | + |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + /** |
|
| 208 | + * Re-create GetPaid pages. |
|
| 209 | + * |
|
| 210 | + */ |
|
| 211 | + public function create_pages() { |
|
| 212 | + |
|
| 213 | + foreach ( self::get_pages() as $key => $page ) { |
|
| 214 | + wpinv_create_page( esc_sql( $page['name'] ), $key, $page['title'], $page['content'], $page['parent'] ); |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + /** |
|
| 220 | + * Migrates old invoices to new invoices. |
|
| 221 | + * |
|
| 222 | + */ |
|
| 223 | + public function migrate_old_invoices() { |
|
| 224 | + global $wpdb; |
|
| 225 | + |
|
| 226 | + $invoices_table = $wpdb->prefix . 'getpaid_invoices'; |
|
| 227 | + $invoice_items_table = $wpdb->prefix . 'getpaid_invoice_items'; |
|
| 228 | + $migrated = $wpdb->get_col( "SELECT post_id FROM $invoices_table" ); |
|
| 229 | + $invoices = array_unique( |
|
| 230 | + get_posts( |
|
| 231 | + array( |
|
| 232 | + 'post_type' => array( 'wpi_invoice', 'wpi_quote' ), |
|
| 233 | + 'posts_per_page' => -1, |
|
| 234 | + 'fields' => 'ids', |
|
| 235 | + 'post_status' => array_keys( get_post_stati() ), |
|
| 236 | + 'exclude' => (array) $migrated, |
|
| 237 | + ) |
|
| 238 | + ) |
|
| 239 | + ); |
|
| 240 | + |
|
| 241 | + // Abort if we do not have any invoices. |
|
| 242 | + if ( empty( $invoices ) ) { |
|
| 243 | + return; |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + require_once WPINV_PLUGIN_DIR . 'includes/class-wpinv-legacy-invoice.php'; |
|
| 247 | + |
|
| 248 | + $invoice_rows = array(); |
|
| 249 | + foreach ( $invoices as $invoice ) { |
|
| 250 | + |
|
| 251 | + $invoice = new WPInv_Legacy_Invoice( $invoice ); |
|
| 252 | + |
|
| 253 | + if ( empty( $invoice->ID ) ) { |
|
| 254 | + return; |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + $fields = array( |
|
| 258 | + 'post_id' => $invoice->ID, |
|
| 259 | + 'number' => $invoice->get_number(), |
|
| 260 | + 'key' => $invoice->get_key(), |
|
| 261 | + 'type' => str_replace( 'wpi_', '', $invoice->post_type ), |
|
| 262 | + 'mode' => $invoice->mode, |
|
| 263 | + 'user_ip' => $invoice->get_ip(), |
|
| 264 | + 'first_name' => $invoice->get_first_name(), |
|
| 265 | + 'last_name' => $invoice->get_last_name(), |
|
| 266 | + 'address' => $invoice->get_address(), |
|
| 267 | + 'city' => $invoice->city, |
|
| 268 | + 'state' => $invoice->state, |
|
| 269 | + 'country' => $invoice->country, |
|
| 270 | + 'zip' => $invoice->zip, |
|
| 271 | + 'adddress_confirmed' => (int) $invoice->adddress_confirmed, |
|
| 272 | + 'gateway' => $invoice->get_gateway(), |
|
| 273 | + 'transaction_id' => $invoice->get_transaction_id(), |
|
| 274 | + 'currency' => $invoice->get_currency(), |
|
| 275 | + 'subtotal' => $invoice->get_subtotal(), |
|
| 276 | + 'tax' => $invoice->get_tax(), |
|
| 277 | + 'fees_total' => $invoice->get_fees_total(), |
|
| 278 | + 'total' => $invoice->get_total(), |
|
| 279 | + 'discount' => $invoice->get_discount(), |
|
| 280 | + 'discount_code' => $invoice->get_discount_code(), |
|
| 281 | + 'disable_taxes' => $invoice->disable_taxes, |
|
| 282 | + 'due_date' => $invoice->get_due_date(), |
|
| 283 | + 'completed_date' => $invoice->get_completed_date(), |
|
| 284 | + 'company' => $invoice->company, |
|
| 285 | + 'vat_number' => $invoice->vat_number, |
|
| 286 | + 'vat_rate' => $invoice->vat_rate, |
|
| 287 | + 'custom_meta' => $invoice->payment_meta, |
|
| 288 | + ); |
|
| 289 | + |
|
| 290 | + foreach ( $fields as $key => $val ) { |
|
| 291 | + if ( is_null( $val ) ) { |
|
| 292 | + $val = ''; |
|
| 293 | + } |
|
| 294 | + $val = maybe_serialize( $val ); |
|
| 295 | + $fields[ $key ] = $wpdb->prepare( '%s', $val ); |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + $fields = implode( ', ', $fields ); |
|
| 299 | + $invoice_rows[] = "($fields)"; |
|
| 300 | + |
|
| 301 | + $item_rows = array(); |
|
| 302 | + $item_columns = array(); |
|
| 303 | + foreach ( $invoice->get_cart_details() as $details ) { |
|
| 304 | + $fields = array( |
|
| 305 | + 'post_id' => $invoice->ID, |
|
| 306 | + 'item_id' => $details['id'], |
|
| 307 | + 'item_name' => $details['name'], |
|
| 308 | + 'item_description' => empty( $details['meta']['description'] ) ? '' : $details['meta']['description'], |
|
| 309 | + 'vat_rate' => $details['vat_rate'], |
|
| 310 | + 'vat_class' => empty( $details['vat_class'] ) ? '_standard' : $details['vat_class'], |
|
| 311 | + 'tax' => $details['tax'], |
|
| 312 | + 'item_price' => $details['item_price'], |
|
| 313 | + 'custom_price' => $details['custom_price'], |
|
| 314 | + 'quantity' => $details['quantity'], |
|
| 315 | + 'discount' => $details['discount'], |
|
| 316 | + 'subtotal' => $details['subtotal'], |
|
| 317 | + 'price' => $details['price'], |
|
| 318 | + 'meta' => $details['meta'], |
|
| 319 | + 'fees' => $details['fees'], |
|
| 320 | + ); |
|
| 321 | + |
|
| 322 | + $item_columns = array_keys( $fields ); |
|
| 323 | + |
|
| 324 | + foreach ( $fields as $key => $val ) { |
|
| 325 | + if ( is_null( $val ) ) { |
|
| 326 | + $val = ''; |
|
| 327 | + } |
|
| 328 | + $val = maybe_serialize( $val ); |
|
| 329 | + $fields[ $key ] = $wpdb->prepare( '%s', $val ); |
|
| 330 | + } |
|
| 331 | + |
|
| 332 | + $fields = implode( ', ', $fields ); |
|
| 333 | + $item_rows[] = "($fields)"; |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + $item_rows = implode( ', ', $item_rows ); |
|
| 337 | + $item_columns = implode( ', ', $item_columns ); |
|
| 338 | + $wpdb->query( "INSERT INTO $invoice_items_table ($item_columns) VALUES $item_rows" ); |
|
| 339 | + } |
|
| 340 | + |
|
| 341 | + if ( empty( $invoice_rows ) ) { |
|
| 342 | + return; |
|
| 343 | + } |
|
| 344 | + |
|
| 345 | + $invoice_rows = implode( ', ', $invoice_rows ); |
|
| 346 | + $wpdb->query( "INSERT INTO $invoices_table VALUES $invoice_rows" ); |
|
| 347 | + |
|
| 348 | + } |
|
| 349 | + |
|
| 350 | + /** |
|
| 351 | + * Migrates old customers to new table. |
|
| 352 | + * |
|
| 353 | + */ |
|
| 354 | + public static function migrate_old_customers() { |
|
| 355 | + global $wpdb; |
|
| 356 | + |
|
| 357 | + // Fetch post_id from $wpdb->prefix . 'getpaid_invoices' where customer_id = 0 or null. |
|
| 358 | + $invoice_ids = $wpdb->get_col( "SELECT post_id FROM {$wpdb->prefix}getpaid_invoices WHERE customer_id = 0 OR customer_id IS NULL" ); |
|
| 359 | + |
|
| 360 | + foreach ( $invoice_ids as $invoice_id ) { |
|
| 361 | + $invoice = wpinv_get_invoice( $invoice_id ); |
|
| 362 | + |
|
| 363 | + if ( empty( $invoice ) ) { |
|
| 364 | + continue; |
|
| 365 | + } |
|
| 366 | + |
|
| 367 | + // Fetch customer from the user ID. |
|
| 368 | + $user_id = $invoice->get_user_id(); |
|
| 369 | + |
|
| 370 | + if ( empty( $user_id ) ) { |
|
| 371 | + continue; |
|
| 372 | + } |
|
| 373 | + |
|
| 374 | + $customer = getpaid_get_customer_by_user_id( $user_id ); |
|
| 375 | + |
|
| 376 | + // Create if not exists. |
|
| 377 | + if ( empty( $customer ) ) { |
|
| 378 | + $customer = new GetPaid_Customer( 0 ); |
|
| 379 | + $customer->clone_user( $user_id ); |
|
| 380 | + $customer->save(); |
|
| 381 | + } |
|
| 382 | + |
|
| 383 | + $invoice->set_customer_id( $customer->get_id() ); |
|
| 384 | + $invoice->save(); |
|
| 385 | + } |
|
| 386 | + |
|
| 387 | + } |
|
| 388 | + |
|
| 389 | + /** |
|
| 390 | + * Migrates old invoices to new invoices. |
|
| 391 | + * |
|
| 392 | + */ |
|
| 393 | + public static function rename_gateways_label() { |
|
| 394 | + global $wpdb; |
|
| 395 | + |
|
| 396 | + foreach ( array_keys( wpinv_get_payment_gateways() ) as $gateway ) { |
|
| 397 | + |
|
| 398 | + $wpdb->update( |
|
| 399 | + $wpdb->prefix . 'getpaid_invoices', |
|
| 400 | + array( 'gateway' => $gateway ), |
|
| 401 | + array( 'gateway' => wpinv_get_gateway_admin_label( $gateway ) ), |
|
| 402 | + '%s', |
|
| 403 | + '%s' |
|
| 404 | + ); |
|
| 405 | + |
|
| 406 | + } |
|
| 407 | + } |
|
| 408 | + |
|
| 409 | + /** |
|
| 410 | + * Returns the DB schema. |
|
| 411 | + * |
|
| 412 | + */ |
|
| 413 | + public static function get_db_schema() { |
|
| 414 | + global $wpdb; |
|
| 415 | + |
|
| 416 | + if ( ! empty( self::$schema ) ) { |
|
| 417 | + return self::$schema; |
|
| 418 | + } |
|
| 419 | 419 | |
| 420 | - require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
|
| 420 | + require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
|
| 421 | 421 | |
| 422 | - $charset_collate = $wpdb->get_charset_collate(); |
|
| 422 | + $charset_collate = $wpdb->get_charset_collate(); |
|
| 423 | 423 | |
| 424 | - $schema = array(); |
|
| 424 | + $schema = array(); |
|
| 425 | 425 | |
| 426 | - // Subscriptions. |
|
| 427 | - $schema['subscriptions'] = "CREATE TABLE {$wpdb->prefix}wpinv_subscriptions ( |
|
| 426 | + // Subscriptions. |
|
| 427 | + $schema['subscriptions'] = "CREATE TABLE {$wpdb->prefix}wpinv_subscriptions ( |
|
| 428 | 428 | id bigint(20) unsigned NOT NULL auto_increment, |
| 429 | 429 | customer_id bigint(20) NOT NULL, |
| 430 | 430 | frequency int(11) NOT NULL DEFAULT '1', |
@@ -447,8 +447,8 @@ discard block |
||
| 447 | 447 | KEY customer_and_status (customer_id, status) |
| 448 | 448 | ) $charset_collate;"; |
| 449 | 449 | |
| 450 | - // Invoices. |
|
| 451 | - $schema['invoices'] = "CREATE TABLE {$wpdb->prefix}getpaid_invoices ( |
|
| 450 | + // Invoices. |
|
| 451 | + $schema['invoices'] = "CREATE TABLE {$wpdb->prefix}getpaid_invoices ( |
|
| 452 | 452 | post_id BIGINT(20) NOT NULL, |
| 453 | 453 | customer_id BIGINT(20) NOT NULL DEFAULT 0, |
| 454 | 454 | `number` VARCHAR(100), |
@@ -485,8 +485,8 @@ discard block |
||
| 485 | 485 | KEY invoice_key (invoice_key) |
| 486 | 486 | ) $charset_collate;"; |
| 487 | 487 | |
| 488 | - // Invoice items. |
|
| 489 | - $schema['items'] = "CREATE TABLE {$wpdb->prefix}getpaid_invoice_items ( |
|
| 488 | + // Invoice items. |
|
| 489 | + $schema['items'] = "CREATE TABLE {$wpdb->prefix}getpaid_invoice_items ( |
|
| 490 | 490 | ID BIGINT(20) NOT NULL AUTO_INCREMENT, |
| 491 | 491 | post_id BIGINT(20) NOT NULL, |
| 492 | 492 | item_id BIGINT(20) NOT NULL, |
@@ -508,8 +508,8 @@ discard block |
||
| 508 | 508 | KEY post_id (post_id) |
| 509 | 509 | ) $charset_collate;"; |
| 510 | 510 | |
| 511 | - // Customers. |
|
| 512 | - $schema['customers'] = "CREATE TABLE {$wpdb->prefix}getpaid_customers ( |
|
| 511 | + // Customers. |
|
| 512 | + $schema['customers'] = "CREATE TABLE {$wpdb->prefix}getpaid_customers ( |
|
| 513 | 513 | id BIGINT(20) NOT NULL AUTO_INCREMENT, |
| 514 | 514 | user_id BIGINT(20) NOT NULL, |
| 515 | 515 | email VARCHAR(100) NOT NULL, |
@@ -519,38 +519,38 @@ discard block |
||
| 519 | 519 | purchase_count BIGINT(20) NOT NULL DEFAULT 0, |
| 520 | 520 | "; |
| 521 | 521 | |
| 522 | - // Add address fields. |
|
| 523 | - foreach ( array_keys( getpaid_user_address_fields( true ) ) as $field ) { |
|
| 524 | - // Skip id, user_id and email. |
|
| 525 | - if ( in_array( $field, array( 'id', 'user_id', 'email', 'purchase_value', 'purchase_count', 'date_created', 'date_modified', 'uuid' ), true ) ) { |
|
| 526 | - continue; |
|
| 527 | - } |
|
| 528 | - |
|
| 529 | - $field = sanitize_key( $field ); |
|
| 530 | - $length = 100; |
|
| 531 | - $default = ''; |
|
| 532 | - |
|
| 533 | - // Country. |
|
| 534 | - if ( 'country' === $field ) { |
|
| 535 | - $length = 2; |
|
| 536 | - $default = wpinv_get_default_country(); |
|
| 537 | - } |
|
| 538 | - |
|
| 539 | - // State. |
|
| 540 | - if ( 'state' === $field ) { |
|
| 541 | - $default = wpinv_get_default_state(); |
|
| 542 | - } |
|
| 543 | - |
|
| 544 | - // Phone, zip. |
|
| 545 | - if ( in_array( $field, array( 'phone', 'zip' ), true ) ) { |
|
| 546 | - $length = 20; |
|
| 547 | - } |
|
| 548 | - |
|
| 549 | - $schema['customers'] .= "`$field` VARCHAR($length) NOT NULL DEFAULT '$default', |
|
| 522 | + // Add address fields. |
|
| 523 | + foreach ( array_keys( getpaid_user_address_fields( true ) ) as $field ) { |
|
| 524 | + // Skip id, user_id and email. |
|
| 525 | + if ( in_array( $field, array( 'id', 'user_id', 'email', 'purchase_value', 'purchase_count', 'date_created', 'date_modified', 'uuid' ), true ) ) { |
|
| 526 | + continue; |
|
| 527 | + } |
|
| 528 | + |
|
| 529 | + $field = sanitize_key( $field ); |
|
| 530 | + $length = 100; |
|
| 531 | + $default = ''; |
|
| 532 | + |
|
| 533 | + // Country. |
|
| 534 | + if ( 'country' === $field ) { |
|
| 535 | + $length = 2; |
|
| 536 | + $default = wpinv_get_default_country(); |
|
| 537 | + } |
|
| 538 | + |
|
| 539 | + // State. |
|
| 540 | + if ( 'state' === $field ) { |
|
| 541 | + $default = wpinv_get_default_state(); |
|
| 542 | + } |
|
| 543 | + |
|
| 544 | + // Phone, zip. |
|
| 545 | + if ( in_array( $field, array( 'phone', 'zip' ), true ) ) { |
|
| 546 | + $length = 20; |
|
| 547 | + } |
|
| 548 | + |
|
| 549 | + $schema['customers'] .= "`$field` VARCHAR($length) NOT NULL DEFAULT '$default', |
|
| 550 | 550 | "; |
| 551 | - } |
|
| 551 | + } |
|
| 552 | 552 | |
| 553 | - $schema['customers'] .= "date_created DATETIME NOT NULL, |
|
| 553 | + $schema['customers'] .= "date_created DATETIME NOT NULL, |
|
| 554 | 554 | date_modified DATETIME NOT NULL, |
| 555 | 555 | uuid VARCHAR(100) NOT NULL, |
| 556 | 556 | PRIMARY KEY (id), |
@@ -558,8 +558,8 @@ discard block |
||
| 558 | 558 | KEY email (email) |
| 559 | 559 | ) $charset_collate;"; |
| 560 | 560 | |
| 561 | - // Customer meta. |
|
| 562 | - $schema['customer_meta'] = "CREATE TABLE {$wpdb->prefix}getpaid_customer_meta ( |
|
| 561 | + // Customer meta. |
|
| 562 | + $schema['customer_meta'] = "CREATE TABLE {$wpdb->prefix}getpaid_customer_meta ( |
|
| 563 | 563 | meta_id BIGINT(20) NOT NULL AUTO_INCREMENT, |
| 564 | 564 | customer_id BIGINT(20) NOT NULL, |
| 565 | 565 | meta_key VARCHAR(255) NOT NULL, |
@@ -569,75 +569,75 @@ discard block |
||
| 569 | 569 | KEY meta_key (meta_key(191)) |
| 570 | 570 | ) $charset_collate;"; |
| 571 | 571 | |
| 572 | - // Filter. |
|
| 573 | - $schema = apply_filters( 'getpaid_db_schema', $schema ); |
|
| 574 | - |
|
| 575 | - self::$schema = implode( "\n", array_values( $schema ) ); |
|
| 576 | - self::$schema_version = md5( sanitize_key( self::$schema ) ); |
|
| 577 | - |
|
| 578 | - return self::$schema; |
|
| 579 | - } |
|
| 580 | - |
|
| 581 | - /** |
|
| 582 | - * Returns the DB schema version. |
|
| 583 | - * |
|
| 584 | - */ |
|
| 585 | - public static function get_db_schema_version() { |
|
| 586 | - if ( ! empty( self::$schema_version ) ) { |
|
| 587 | - return self::$schema_version; |
|
| 588 | - } |
|
| 589 | - |
|
| 590 | - self::get_db_schema(); |
|
| 591 | - |
|
| 592 | - return self::$schema_version; |
|
| 593 | - } |
|
| 594 | - |
|
| 595 | - /** |
|
| 596 | - * Checks if the db schema is up to date. |
|
| 597 | - * |
|
| 598 | - * @return bool |
|
| 599 | - */ |
|
| 600 | - public static function is_db_schema_up_to_date() { |
|
| 601 | - return self::get_db_schema_version() === get_option( 'getpaid_db_schema' ); |
|
| 602 | - } |
|
| 603 | - |
|
| 604 | - /** |
|
| 605 | - * Set up the database tables which the plugin needs to function. |
|
| 606 | - */ |
|
| 607 | - public static function create_db_tables() { |
|
| 608 | - global $wpdb; |
|
| 609 | - |
|
| 610 | - $wpdb->hide_errors(); |
|
| 611 | - |
|
| 612 | - require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
|
| 613 | - |
|
| 614 | - $schema = self::get_db_schema(); |
|
| 615 | - |
|
| 616 | - // If invoices table exists, rename key to invoice_key. |
|
| 617 | - $invoices_table = "{$wpdb->prefix}getpaid_invoices"; |
|
| 618 | - |
|
| 619 | - if ( $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}getpaid_invoices'" ) === $invoices_table ) { |
|
| 620 | - $fields = $wpdb->get_results( "SHOW COLUMNS FROM {$wpdb->prefix}getpaid_invoices" ); |
|
| 621 | - |
|
| 622 | - foreach ( $fields as $field ) { |
|
| 623 | - if ( 'key' === $field->Field ) { |
|
| 624 | - $wpdb->query( "ALTER TABLE {$wpdb->prefix}getpaid_invoices CHANGE `key` `invoice_key` VARCHAR(100)" ); |
|
| 625 | - break; |
|
| 626 | - } |
|
| 627 | - } |
|
| 628 | - } |
|
| 629 | - |
|
| 630 | - dbDelta( $schema ); |
|
| 631 | - wp_cache_flush(); |
|
| 632 | - update_option( 'getpaid_db_schema', self::get_db_schema_version() ); |
|
| 633 | - } |
|
| 634 | - |
|
| 635 | - /** |
|
| 636 | - * Creates tables if schema is not up to date. |
|
| 637 | - */ |
|
| 638 | - public static function maybe_create_db_tables() { |
|
| 639 | - if ( ! self::is_db_schema_up_to_date() ) { |
|
| 640 | - self::create_db_tables(); |
|
| 641 | - } |
|
| 642 | - } |
|
| 572 | + // Filter. |
|
| 573 | + $schema = apply_filters( 'getpaid_db_schema', $schema ); |
|
| 574 | + |
|
| 575 | + self::$schema = implode( "\n", array_values( $schema ) ); |
|
| 576 | + self::$schema_version = md5( sanitize_key( self::$schema ) ); |
|
| 577 | + |
|
| 578 | + return self::$schema; |
|
| 579 | + } |
|
| 580 | + |
|
| 581 | + /** |
|
| 582 | + * Returns the DB schema version. |
|
| 583 | + * |
|
| 584 | + */ |
|
| 585 | + public static function get_db_schema_version() { |
|
| 586 | + if ( ! empty( self::$schema_version ) ) { |
|
| 587 | + return self::$schema_version; |
|
| 588 | + } |
|
| 589 | + |
|
| 590 | + self::get_db_schema(); |
|
| 591 | + |
|
| 592 | + return self::$schema_version; |
|
| 593 | + } |
|
| 594 | + |
|
| 595 | + /** |
|
| 596 | + * Checks if the db schema is up to date. |
|
| 597 | + * |
|
| 598 | + * @return bool |
|
| 599 | + */ |
|
| 600 | + public static function is_db_schema_up_to_date() { |
|
| 601 | + return self::get_db_schema_version() === get_option( 'getpaid_db_schema' ); |
|
| 602 | + } |
|
| 603 | + |
|
| 604 | + /** |
|
| 605 | + * Set up the database tables which the plugin needs to function. |
|
| 606 | + */ |
|
| 607 | + public static function create_db_tables() { |
|
| 608 | + global $wpdb; |
|
| 609 | + |
|
| 610 | + $wpdb->hide_errors(); |
|
| 611 | + |
|
| 612 | + require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
|
| 613 | + |
|
| 614 | + $schema = self::get_db_schema(); |
|
| 615 | + |
|
| 616 | + // If invoices table exists, rename key to invoice_key. |
|
| 617 | + $invoices_table = "{$wpdb->prefix}getpaid_invoices"; |
|
| 618 | + |
|
| 619 | + if ( $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}getpaid_invoices'" ) === $invoices_table ) { |
|
| 620 | + $fields = $wpdb->get_results( "SHOW COLUMNS FROM {$wpdb->prefix}getpaid_invoices" ); |
|
| 621 | + |
|
| 622 | + foreach ( $fields as $field ) { |
|
| 623 | + if ( 'key' === $field->Field ) { |
|
| 624 | + $wpdb->query( "ALTER TABLE {$wpdb->prefix}getpaid_invoices CHANGE `key` `invoice_key` VARCHAR(100)" ); |
|
| 625 | + break; |
|
| 626 | + } |
|
| 627 | + } |
|
| 628 | + } |
|
| 629 | + |
|
| 630 | + dbDelta( $schema ); |
|
| 631 | + wp_cache_flush(); |
|
| 632 | + update_option( 'getpaid_db_schema', self::get_db_schema_version() ); |
|
| 633 | + } |
|
| 634 | + |
|
| 635 | + /** |
|
| 636 | + * Creates tables if schema is not up to date. |
|
| 637 | + */ |
|
| 638 | + public static function maybe_create_db_tables() { |
|
| 639 | + if ( ! self::is_db_schema_up_to_date() ) { |
|
| 640 | + self::create_db_tables(); |
|
| 641 | + } |
|
| 642 | + } |
|
| 643 | 643 | } |