@@ -57,8 +57,8 @@ discard block |
||
57 | 57 | 'getpaid-nonce', |
58 | 58 | 'getpaid-nonce' |
59 | 59 | ); |
60 | - $anchor = __( 'Deactivate', 'invoicing' ); |
|
61 | - $title = esc_attr__( 'Are you sure you want to deactivate this discount?', 'invoicing' ); |
|
60 | + $anchor = __( 'Deactivate', 'invoicing' ); |
|
61 | + $title = esc_attr__( 'Are you sure you want to deactivate this discount?', 'invoicing' ); |
|
62 | 62 | $row_actions['deactivate'] = "<a href='$url' onclick='return confirm(\"$title\")'>$anchor</a>"; |
63 | 63 | |
64 | 64 | } elseif ( in_array( strtolower( $discount->post_status ), array( 'pending', 'draft' ) ) && wpinv_current_user_can( 'activate_discount', array( 'discount' => (int) $discount->ID ) ) ) { |
@@ -73,8 +73,8 @@ discard block |
||
73 | 73 | 'getpaid-nonce', |
74 | 74 | 'getpaid-nonce' |
75 | 75 | ); |
76 | - $anchor = __( 'Activate', 'invoicing' ); |
|
77 | - $title = esc_attr__( 'Are you sure you want to activate this discount?', 'invoicing' ); |
|
76 | + $anchor = __( 'Activate', 'invoicing' ); |
|
77 | + $title = esc_attr__( 'Are you sure you want to activate this discount?', 'invoicing' ); |
|
78 | 78 | $row_actions['activate'] = "<a href='$url' onclick='return confirm(\"$title\")'>$anchor</a>"; |
79 | 79 | |
80 | 80 | } |
@@ -121,13 +121,13 @@ discard block |
||
121 | 121 | $types = wpinv_get_discount_types(); |
122 | 122 | |
123 | 123 | foreach ( $types as $name => $type ) { |
124 | - echo '<option value="' . esc_attr( $name ) . '"'; |
|
124 | + echo '<option value="' . esc_attr( $name ) . '"'; |
|
125 | 125 | |
126 | - if ( isset( $_GET['discount_type'] ) ) { |
|
127 | - selected( $name, sanitize_text_field( $_GET['discount_type'] ) ); |
|
126 | + if ( isset( $_GET['discount_type'] ) ) { |
|
127 | + selected( $name, sanitize_text_field( $_GET['discount_type'] ) ); |
|
128 | 128 | } |
129 | 129 | |
130 | - echo '>' . esc_html__( $type, 'invoicing' ) . '</option>'; |
|
130 | + echo '>' . esc_html__( $type, 'invoicing' ) . '</option>'; |
|
131 | 131 | } |
132 | 132 | ?> |
133 | 133 | </select> |
@@ -154,15 +154,15 @@ discard block |
||
154 | 154 | // Filter vat rule type |
155 | 155 | if ( isset( $_GET['discount_type'] ) && $_GET['discount_type'] !== '' ) { |
156 | 156 | $meta_query[] = array( |
157 | - 'key' => '_wpi_discount_type', |
|
158 | - 'value' => sanitize_key( urldecode( $_GET['discount_type'] ) ), |
|
159 | - 'compare' => '=', |
|
160 | - ); |
|
161 | - } |
|
157 | + 'key' => '_wpi_discount_type', |
|
158 | + 'value' => sanitize_key( urldecode( $_GET['discount_type'] ) ), |
|
159 | + 'compare' => '=', |
|
160 | + ); |
|
161 | + } |
|
162 | 162 | |
163 | 163 | if ( ! empty( $meta_query ) ) { |
164 | 164 | $vars['meta_query'] = $meta_query; |
165 | - } |
|
165 | + } |
|
166 | 166 | } |
167 | 167 | |
168 | 168 | return $vars; |
@@ -11,187 +11,187 @@ |
||
11 | 11 | */ |
12 | 12 | class GetPaid_Data_Store { |
13 | 13 | |
14 | - /** |
|
15 | - * Contains an instance of the data store class that we are working with. |
|
16 | - * |
|
17 | - * @var GetPaid_Data_Store |
|
18 | - */ |
|
19 | - private $instance = null; |
|
20 | - |
|
21 | - /** |
|
22 | - * Contains an array of default supported data stores. |
|
23 | - * Format of object name => class name. |
|
24 | - * Example: 'item' => 'GetPaid_Item_Data_Store' |
|
25 | - * You can also pass something like item-<type> for item stores and |
|
26 | - * that type will be used first when available, if a store is requested like |
|
27 | - * this and doesn't exist, then the store would fall back to 'item'. |
|
28 | - * Ran through `getpaid_data_stores`. |
|
29 | - * |
|
30 | - * @var array |
|
31 | - */ |
|
32 | - private $stores = array( |
|
33 | - 'item' => 'GetPaid_Item_Data_Store', |
|
34 | - 'payment_form' => 'GetPaid_Payment_Form_Data_Store', |
|
35 | - 'discount' => 'GetPaid_Discount_Data_Store', |
|
36 | - 'invoice' => 'GetPaid_Invoice_Data_Store', |
|
37 | - 'subscription' => 'GetPaid_Subscription_Data_Store', |
|
38 | - 'customer' => 'GetPaid_Customer_Data_Store', |
|
39 | - ); |
|
40 | - |
|
41 | - /** |
|
42 | - * Contains the name of the current data store's class name. |
|
43 | - * |
|
44 | - * @var string |
|
45 | - */ |
|
46 | - private $current_class_name = ''; |
|
47 | - |
|
48 | - /** |
|
49 | - * The object type this store works with. |
|
50 | - * |
|
51 | - * @var string |
|
52 | - */ |
|
53 | - private $object_type = ''; |
|
54 | - |
|
55 | - /** |
|
56 | - * Tells GetPaid_Data_Store which object |
|
57 | - * store we want to work with. |
|
58 | - * |
|
59 | - * @param string $object_type Name of object. |
|
60 | - */ |
|
61 | - public function __construct( $object_type ) { |
|
62 | - $this->object_type = $object_type; |
|
63 | - $this->stores = apply_filters( 'getpaid_data_stores', $this->stores ); |
|
64 | - |
|
65 | - // If this object type can't be found, check to see if we can load one |
|
66 | - // level up (so if item-type isn't found, we try item). |
|
67 | - if ( ! array_key_exists( $object_type, $this->stores ) ) { |
|
68 | - $pieces = explode( '-', $object_type ); |
|
69 | - $object_type = $pieces[0]; |
|
70 | - } |
|
71 | - |
|
72 | - if ( array_key_exists( $object_type, $this->stores ) ) { |
|
73 | - $store = apply_filters( 'getpaid_' . $object_type . '_data_store', $this->stores[ $object_type ] ); |
|
74 | - if ( is_object( $store ) ) { |
|
75 | - $this->current_class_name = get_class( $store ); |
|
76 | - $this->instance = $store; |
|
77 | - } else { |
|
78 | - if ( ! class_exists( $store ) ) { |
|
79 | - throw new Exception( __( 'Data store class does not exist.', 'invoicing' ) ); |
|
80 | - } |
|
81 | - $this->current_class_name = $store; |
|
82 | - $this->instance = new $store(); |
|
83 | - } |
|
84 | - } else { |
|
85 | - throw new Exception( __( 'Invalid data store.', 'invoicing' ) ); |
|
86 | - } |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * Only store the object type to avoid serializing the data store instance. |
|
91 | - * |
|
92 | - * @return array |
|
93 | - */ |
|
94 | - public function __sleep() { |
|
95 | - return array( 'object_type' ); |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * Re-run the constructor with the object type. |
|
100 | - * |
|
101 | - * @throws Exception When validation fails. |
|
102 | - */ |
|
103 | - public function __wakeup() { |
|
104 | - $this->__construct( $this->object_type ); |
|
105 | - } |
|
106 | - |
|
107 | - /** |
|
108 | - * Loads a data store. |
|
109 | - * |
|
110 | - * @param string $object_type Name of object. |
|
111 | - * |
|
112 | - * @since 1.0.19 |
|
113 | - * @throws Exception When validation fails. |
|
114 | - * @return GetPaid_Data_Store |
|
115 | - */ |
|
116 | - public static function load( $object_type ) { |
|
117 | - return new GetPaid_Data_Store( $object_type ); |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * Returns the class name of the current data store. |
|
122 | - * |
|
123 | - * @since 1.0.19 |
|
124 | - * @return string |
|
125 | - */ |
|
126 | - public function get_current_class_name() { |
|
127 | - return $this->current_class_name; |
|
128 | - } |
|
129 | - |
|
130 | - /** |
|
131 | - * Returns the object type of the current data store. |
|
132 | - * |
|
133 | - * @since 1.0.19 |
|
134 | - * @return string |
|
135 | - */ |
|
136 | - public function get_object_type() { |
|
137 | - return $this->object_type; |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * Reads an object from the data store. |
|
142 | - * |
|
143 | - * @since 1.0.19 |
|
144 | - * @param GetPaid_Data $data GetPaid data instance. |
|
145 | - */ |
|
146 | - public function read( &$data ) { |
|
147 | - $this->instance->read( $data ); |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * Create an object in the data store. |
|
152 | - * |
|
153 | - * @since 1.0.19 |
|
154 | - * @param GetPaid_Data $data GetPaid data instance. |
|
155 | - */ |
|
156 | - public function create( &$data ) { |
|
157 | - $this->instance->create( $data ); |
|
158 | - } |
|
159 | - |
|
160 | - /** |
|
161 | - * Update an object in the data store. |
|
162 | - * |
|
163 | - * @since 1.0.19 |
|
164 | - * @param GetPaid_Data $data GetPaid data instance. |
|
165 | - */ |
|
166 | - public function update( &$data ) { |
|
167 | - $this->instance->update( $data ); |
|
168 | - } |
|
169 | - |
|
170 | - /** |
|
171 | - * Delete an object from the data store. |
|
172 | - * |
|
173 | - * @since 1.0.19 |
|
174 | - * @param GetPaid_Data $data GetPaid data instance. |
|
175 | - * @param array $args Array of args to pass to the delete method. |
|
176 | - */ |
|
177 | - public function delete( &$data, $args = array() ) { |
|
178 | - $this->instance->delete( $data, $args ); |
|
179 | - } |
|
180 | - |
|
181 | - /** |
|
182 | - * Data stores can define additional function. This passes |
|
183 | - * through to the instance if that function exists. |
|
184 | - * |
|
185 | - * @since 1.0.19 |
|
186 | - * @param string $method Method. |
|
187 | - * @return mixed |
|
188 | - */ |
|
189 | - public function __call( $method, $parameters ) { |
|
190 | - if ( is_callable( array( $this->instance, $method ) ) ) { |
|
191 | - $object = array_shift( $parameters ); |
|
192 | - $parameters = array_merge( array( &$object ), $parameters ); |
|
193 | - return call_user_func_array( array( $this->instance, $method ), $parameters ); |
|
194 | - } |
|
195 | - } |
|
14 | + /** |
|
15 | + * Contains an instance of the data store class that we are working with. |
|
16 | + * |
|
17 | + * @var GetPaid_Data_Store |
|
18 | + */ |
|
19 | + private $instance = null; |
|
20 | + |
|
21 | + /** |
|
22 | + * Contains an array of default supported data stores. |
|
23 | + * Format of object name => class name. |
|
24 | + * Example: 'item' => 'GetPaid_Item_Data_Store' |
|
25 | + * You can also pass something like item-<type> for item stores and |
|
26 | + * that type will be used first when available, if a store is requested like |
|
27 | + * this and doesn't exist, then the store would fall back to 'item'. |
|
28 | + * Ran through `getpaid_data_stores`. |
|
29 | + * |
|
30 | + * @var array |
|
31 | + */ |
|
32 | + private $stores = array( |
|
33 | + 'item' => 'GetPaid_Item_Data_Store', |
|
34 | + 'payment_form' => 'GetPaid_Payment_Form_Data_Store', |
|
35 | + 'discount' => 'GetPaid_Discount_Data_Store', |
|
36 | + 'invoice' => 'GetPaid_Invoice_Data_Store', |
|
37 | + 'subscription' => 'GetPaid_Subscription_Data_Store', |
|
38 | + 'customer' => 'GetPaid_Customer_Data_Store', |
|
39 | + ); |
|
40 | + |
|
41 | + /** |
|
42 | + * Contains the name of the current data store's class name. |
|
43 | + * |
|
44 | + * @var string |
|
45 | + */ |
|
46 | + private $current_class_name = ''; |
|
47 | + |
|
48 | + /** |
|
49 | + * The object type this store works with. |
|
50 | + * |
|
51 | + * @var string |
|
52 | + */ |
|
53 | + private $object_type = ''; |
|
54 | + |
|
55 | + /** |
|
56 | + * Tells GetPaid_Data_Store which object |
|
57 | + * store we want to work with. |
|
58 | + * |
|
59 | + * @param string $object_type Name of object. |
|
60 | + */ |
|
61 | + public function __construct( $object_type ) { |
|
62 | + $this->object_type = $object_type; |
|
63 | + $this->stores = apply_filters( 'getpaid_data_stores', $this->stores ); |
|
64 | + |
|
65 | + // If this object type can't be found, check to see if we can load one |
|
66 | + // level up (so if item-type isn't found, we try item). |
|
67 | + if ( ! array_key_exists( $object_type, $this->stores ) ) { |
|
68 | + $pieces = explode( '-', $object_type ); |
|
69 | + $object_type = $pieces[0]; |
|
70 | + } |
|
71 | + |
|
72 | + if ( array_key_exists( $object_type, $this->stores ) ) { |
|
73 | + $store = apply_filters( 'getpaid_' . $object_type . '_data_store', $this->stores[ $object_type ] ); |
|
74 | + if ( is_object( $store ) ) { |
|
75 | + $this->current_class_name = get_class( $store ); |
|
76 | + $this->instance = $store; |
|
77 | + } else { |
|
78 | + if ( ! class_exists( $store ) ) { |
|
79 | + throw new Exception( __( 'Data store class does not exist.', 'invoicing' ) ); |
|
80 | + } |
|
81 | + $this->current_class_name = $store; |
|
82 | + $this->instance = new $store(); |
|
83 | + } |
|
84 | + } else { |
|
85 | + throw new Exception( __( 'Invalid data store.', 'invoicing' ) ); |
|
86 | + } |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * Only store the object type to avoid serializing the data store instance. |
|
91 | + * |
|
92 | + * @return array |
|
93 | + */ |
|
94 | + public function __sleep() { |
|
95 | + return array( 'object_type' ); |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * Re-run the constructor with the object type. |
|
100 | + * |
|
101 | + * @throws Exception When validation fails. |
|
102 | + */ |
|
103 | + public function __wakeup() { |
|
104 | + $this->__construct( $this->object_type ); |
|
105 | + } |
|
106 | + |
|
107 | + /** |
|
108 | + * Loads a data store. |
|
109 | + * |
|
110 | + * @param string $object_type Name of object. |
|
111 | + * |
|
112 | + * @since 1.0.19 |
|
113 | + * @throws Exception When validation fails. |
|
114 | + * @return GetPaid_Data_Store |
|
115 | + */ |
|
116 | + public static function load( $object_type ) { |
|
117 | + return new GetPaid_Data_Store( $object_type ); |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * Returns the class name of the current data store. |
|
122 | + * |
|
123 | + * @since 1.0.19 |
|
124 | + * @return string |
|
125 | + */ |
|
126 | + public function get_current_class_name() { |
|
127 | + return $this->current_class_name; |
|
128 | + } |
|
129 | + |
|
130 | + /** |
|
131 | + * Returns the object type of the current data store. |
|
132 | + * |
|
133 | + * @since 1.0.19 |
|
134 | + * @return string |
|
135 | + */ |
|
136 | + public function get_object_type() { |
|
137 | + return $this->object_type; |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * Reads an object from the data store. |
|
142 | + * |
|
143 | + * @since 1.0.19 |
|
144 | + * @param GetPaid_Data $data GetPaid data instance. |
|
145 | + */ |
|
146 | + public function read( &$data ) { |
|
147 | + $this->instance->read( $data ); |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * Create an object in the data store. |
|
152 | + * |
|
153 | + * @since 1.0.19 |
|
154 | + * @param GetPaid_Data $data GetPaid data instance. |
|
155 | + */ |
|
156 | + public function create( &$data ) { |
|
157 | + $this->instance->create( $data ); |
|
158 | + } |
|
159 | + |
|
160 | + /** |
|
161 | + * Update an object in the data store. |
|
162 | + * |
|
163 | + * @since 1.0.19 |
|
164 | + * @param GetPaid_Data $data GetPaid data instance. |
|
165 | + */ |
|
166 | + public function update( &$data ) { |
|
167 | + $this->instance->update( $data ); |
|
168 | + } |
|
169 | + |
|
170 | + /** |
|
171 | + * Delete an object from the data store. |
|
172 | + * |
|
173 | + * @since 1.0.19 |
|
174 | + * @param GetPaid_Data $data GetPaid data instance. |
|
175 | + * @param array $args Array of args to pass to the delete method. |
|
176 | + */ |
|
177 | + public function delete( &$data, $args = array() ) { |
|
178 | + $this->instance->delete( $data, $args ); |
|
179 | + } |
|
180 | + |
|
181 | + /** |
|
182 | + * Data stores can define additional function. This passes |
|
183 | + * through to the instance if that function exists. |
|
184 | + * |
|
185 | + * @since 1.0.19 |
|
186 | + * @param string $method Method. |
|
187 | + * @return mixed |
|
188 | + */ |
|
189 | + public function __call( $method, $parameters ) { |
|
190 | + if ( is_callable( array( $this->instance, $method ) ) ) { |
|
191 | + $object = array_shift( $parameters ); |
|
192 | + $parameters = array_merge( array( &$object ), $parameters ); |
|
193 | + return call_user_func_array( array( $this->instance, $method ), $parameters ); |
|
194 | + } |
|
195 | + } |
|
196 | 196 | |
197 | 197 | } |
@@ -5,7 +5,7 @@ discard block |
||
5 | 5 | * |
6 | 6 | */ |
7 | 7 | if ( ! defined( 'ABSPATH' ) ) { |
8 | - exit; |
|
8 | + exit; |
|
9 | 9 | } |
10 | 10 | |
11 | 11 | /** |
@@ -15,196 +15,196 @@ discard block |
||
15 | 15 | */ |
16 | 16 | class GetPaid_Customer_Data_Store { |
17 | 17 | |
18 | - /* |
|
18 | + /* |
|
19 | 19 | |-------------------------------------------------------------------------- |
20 | 20 | | CRUD Methods |
21 | 21 | |-------------------------------------------------------------------------- |
22 | 22 | */ |
23 | 23 | |
24 | - /** |
|
25 | - * Method to create a new customer in the database. |
|
26 | - * |
|
27 | - * @param GetPaid_Customer $customer customer object. |
|
28 | - */ |
|
29 | - public function create( &$customer ) { |
|
30 | - global $wpdb; |
|
31 | - |
|
32 | - $values = array(); |
|
33 | - $formats = array(); |
|
34 | - |
|
35 | - $fields = self::get_database_fields(); |
|
36 | - unset( $fields['id'] ); |
|
37 | - |
|
38 | - foreach ( $fields as $key => $format ) { |
|
39 | - $values[ $key ] = $customer->get( $key, 'edit' ); |
|
40 | - $formats[] = $format; |
|
41 | - } |
|
42 | - |
|
43 | - $result = $wpdb->insert( $wpdb->prefix . 'getpaid_customers', $values, $formats ); |
|
44 | - |
|
45 | - if ( $result ) { |
|
46 | - $customer->set_id( $wpdb->insert_id ); |
|
47 | - $customer->apply_changes(); |
|
48 | - $customer->clear_cache(); |
|
49 | - do_action( 'getpaid_new_customer', $customer ); |
|
50 | - return true; |
|
51 | - } |
|
52 | - |
|
53 | - return false; |
|
54 | - } |
|
55 | - |
|
56 | - /** |
|
57 | - * Method to read a customer from the database. |
|
58 | - * |
|
59 | - * @param GetPaid_Customer $customer customer object. |
|
60 | - * |
|
61 | - */ |
|
62 | - public function read( &$customer ) { |
|
63 | - global $wpdb; |
|
64 | - |
|
65 | - $customer->set_defaults(); |
|
66 | - |
|
67 | - if ( ! $customer->get_id() ) { |
|
68 | - $customer->last_error = 'Invalid customer.'; |
|
69 | - $customer->set_id( 0 ); |
|
70 | - return false; |
|
71 | - } |
|
72 | - |
|
73 | - // Maybe retrieve from the cache. |
|
74 | - $raw_customer = wp_cache_get( $customer->get_id(), 'getpaid_customers' ); |
|
75 | - |
|
76 | - // If not found, retrieve from the db. |
|
77 | - if ( false === $raw_customer ) { |
|
78 | - |
|
79 | - $raw_customer = $wpdb->get_row( |
|
80 | - $wpdb->prepare( |
|
81 | - "SELECT * FROM {$wpdb->prefix}getpaid_customers WHERE id = %d", |
|
82 | - $customer->get_id() |
|
83 | - ) |
|
84 | - ); |
|
85 | - |
|
86 | - // Update the cache with our data |
|
87 | - wp_cache_set( $customer->get_id(), $raw_customer, 'getpaid_customers' ); |
|
88 | - |
|
89 | - } |
|
90 | - |
|
91 | - if ( ! $raw_customer ) { |
|
92 | - $raw_customer->last_error = 'Invalid customer.'; |
|
93 | - return false; |
|
94 | - } |
|
95 | - |
|
96 | - // Loop through raw customer fields. |
|
97 | - foreach ( (array) $raw_customer as $key => $value ) { |
|
98 | - $customer->set( $key, $value ); |
|
99 | - } |
|
100 | - |
|
101 | - $customer->set_object_read( true ); |
|
102 | - do_action( 'getpaid_read_customer', $customer ); |
|
103 | - |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * Method to update a customer in the database. |
|
108 | - * |
|
109 | - * @param GetPaid_Customer $customer Customer object. |
|
110 | - */ |
|
111 | - public function update( &$customer ) { |
|
112 | - global $wpdb; |
|
113 | - |
|
114 | - do_action( 'getpaid_before_update_customer', $customer, $customer->get_changes() ); |
|
115 | - |
|
116 | - $changes = $customer->get_changes(); |
|
117 | - $values = array(); |
|
118 | - $format = array(); |
|
119 | - |
|
120 | - foreach ( self::get_database_fields() as $key => $format ) { |
|
121 | - if ( array_key_exists( $key, $changes ) ) { |
|
122 | - $values[ $key ] = $customer->get( $key, 'edit' ); |
|
123 | - $formats[] = $format; |
|
124 | - } |
|
125 | - } |
|
126 | - |
|
127 | - if ( empty( $values ) ) { |
|
128 | - return; |
|
129 | - } |
|
130 | - |
|
131 | - $wpdb->update( |
|
132 | - $wpdb->prefix . 'getpaid_customers', |
|
133 | - $values, |
|
134 | - array( |
|
135 | - 'id' => $customer->get_id(), |
|
136 | - ), |
|
137 | - $formats, |
|
138 | - '%d' |
|
139 | - ); |
|
140 | - |
|
141 | - // Apply the changes. |
|
142 | - $customer->apply_changes(); |
|
143 | - |
|
144 | - // Delete cache. |
|
145 | - $customer->clear_cache(); |
|
146 | - |
|
147 | - // Fire a hook. |
|
148 | - do_action( 'getpaid_update_customer', $customer ); |
|
149 | - |
|
150 | - } |
|
151 | - |
|
152 | - /** |
|
153 | - * Method to delete a customer from the database. |
|
154 | - * |
|
155 | - * @param GetPaid_Customer $customer |
|
156 | - */ |
|
157 | - public function delete( &$customer ) { |
|
158 | - global $wpdb; |
|
159 | - |
|
160 | - do_action( 'getpaid_before_delete_customer', $customer ); |
|
161 | - |
|
162 | - $wpdb->delete( |
|
163 | - $wpdb->prefix . 'getpaid_customers', |
|
164 | - array( |
|
165 | - 'id' => $customer->get_id(), |
|
166 | - ), |
|
167 | - '%d' |
|
168 | - ); |
|
169 | - |
|
170 | - // Delete cache. |
|
171 | - $customer->clear_cache(); |
|
172 | - |
|
173 | - // Fire a hook. |
|
174 | - do_action( 'getpaid_delete_customer', $customer ); |
|
175 | - |
|
176 | - $customer->set_id( 0 ); |
|
177 | - } |
|
178 | - |
|
179 | - /* |
|
24 | + /** |
|
25 | + * Method to create a new customer in the database. |
|
26 | + * |
|
27 | + * @param GetPaid_Customer $customer customer object. |
|
28 | + */ |
|
29 | + public function create( &$customer ) { |
|
30 | + global $wpdb; |
|
31 | + |
|
32 | + $values = array(); |
|
33 | + $formats = array(); |
|
34 | + |
|
35 | + $fields = self::get_database_fields(); |
|
36 | + unset( $fields['id'] ); |
|
37 | + |
|
38 | + foreach ( $fields as $key => $format ) { |
|
39 | + $values[ $key ] = $customer->get( $key, 'edit' ); |
|
40 | + $formats[] = $format; |
|
41 | + } |
|
42 | + |
|
43 | + $result = $wpdb->insert( $wpdb->prefix . 'getpaid_customers', $values, $formats ); |
|
44 | + |
|
45 | + if ( $result ) { |
|
46 | + $customer->set_id( $wpdb->insert_id ); |
|
47 | + $customer->apply_changes(); |
|
48 | + $customer->clear_cache(); |
|
49 | + do_action( 'getpaid_new_customer', $customer ); |
|
50 | + return true; |
|
51 | + } |
|
52 | + |
|
53 | + return false; |
|
54 | + } |
|
55 | + |
|
56 | + /** |
|
57 | + * Method to read a customer from the database. |
|
58 | + * |
|
59 | + * @param GetPaid_Customer $customer customer object. |
|
60 | + * |
|
61 | + */ |
|
62 | + public function read( &$customer ) { |
|
63 | + global $wpdb; |
|
64 | + |
|
65 | + $customer->set_defaults(); |
|
66 | + |
|
67 | + if ( ! $customer->get_id() ) { |
|
68 | + $customer->last_error = 'Invalid customer.'; |
|
69 | + $customer->set_id( 0 ); |
|
70 | + return false; |
|
71 | + } |
|
72 | + |
|
73 | + // Maybe retrieve from the cache. |
|
74 | + $raw_customer = wp_cache_get( $customer->get_id(), 'getpaid_customers' ); |
|
75 | + |
|
76 | + // If not found, retrieve from the db. |
|
77 | + if ( false === $raw_customer ) { |
|
78 | + |
|
79 | + $raw_customer = $wpdb->get_row( |
|
80 | + $wpdb->prepare( |
|
81 | + "SELECT * FROM {$wpdb->prefix}getpaid_customers WHERE id = %d", |
|
82 | + $customer->get_id() |
|
83 | + ) |
|
84 | + ); |
|
85 | + |
|
86 | + // Update the cache with our data |
|
87 | + wp_cache_set( $customer->get_id(), $raw_customer, 'getpaid_customers' ); |
|
88 | + |
|
89 | + } |
|
90 | + |
|
91 | + if ( ! $raw_customer ) { |
|
92 | + $raw_customer->last_error = 'Invalid customer.'; |
|
93 | + return false; |
|
94 | + } |
|
95 | + |
|
96 | + // Loop through raw customer fields. |
|
97 | + foreach ( (array) $raw_customer as $key => $value ) { |
|
98 | + $customer->set( $key, $value ); |
|
99 | + } |
|
100 | + |
|
101 | + $customer->set_object_read( true ); |
|
102 | + do_action( 'getpaid_read_customer', $customer ); |
|
103 | + |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * Method to update a customer in the database. |
|
108 | + * |
|
109 | + * @param GetPaid_Customer $customer Customer object. |
|
110 | + */ |
|
111 | + public function update( &$customer ) { |
|
112 | + global $wpdb; |
|
113 | + |
|
114 | + do_action( 'getpaid_before_update_customer', $customer, $customer->get_changes() ); |
|
115 | + |
|
116 | + $changes = $customer->get_changes(); |
|
117 | + $values = array(); |
|
118 | + $format = array(); |
|
119 | + |
|
120 | + foreach ( self::get_database_fields() as $key => $format ) { |
|
121 | + if ( array_key_exists( $key, $changes ) ) { |
|
122 | + $values[ $key ] = $customer->get( $key, 'edit' ); |
|
123 | + $formats[] = $format; |
|
124 | + } |
|
125 | + } |
|
126 | + |
|
127 | + if ( empty( $values ) ) { |
|
128 | + return; |
|
129 | + } |
|
130 | + |
|
131 | + $wpdb->update( |
|
132 | + $wpdb->prefix . 'getpaid_customers', |
|
133 | + $values, |
|
134 | + array( |
|
135 | + 'id' => $customer->get_id(), |
|
136 | + ), |
|
137 | + $formats, |
|
138 | + '%d' |
|
139 | + ); |
|
140 | + |
|
141 | + // Apply the changes. |
|
142 | + $customer->apply_changes(); |
|
143 | + |
|
144 | + // Delete cache. |
|
145 | + $customer->clear_cache(); |
|
146 | + |
|
147 | + // Fire a hook. |
|
148 | + do_action( 'getpaid_update_customer', $customer ); |
|
149 | + |
|
150 | + } |
|
151 | + |
|
152 | + /** |
|
153 | + * Method to delete a customer from the database. |
|
154 | + * |
|
155 | + * @param GetPaid_Customer $customer |
|
156 | + */ |
|
157 | + public function delete( &$customer ) { |
|
158 | + global $wpdb; |
|
159 | + |
|
160 | + do_action( 'getpaid_before_delete_customer', $customer ); |
|
161 | + |
|
162 | + $wpdb->delete( |
|
163 | + $wpdb->prefix . 'getpaid_customers', |
|
164 | + array( |
|
165 | + 'id' => $customer->get_id(), |
|
166 | + ), |
|
167 | + '%d' |
|
168 | + ); |
|
169 | + |
|
170 | + // Delete cache. |
|
171 | + $customer->clear_cache(); |
|
172 | + |
|
173 | + // Fire a hook. |
|
174 | + do_action( 'getpaid_delete_customer', $customer ); |
|
175 | + |
|
176 | + $customer->set_id( 0 ); |
|
177 | + } |
|
178 | + |
|
179 | + /* |
|
180 | 180 | |-------------------------------------------------------------------------- |
181 | 181 | | Additional Methods |
182 | 182 | |-------------------------------------------------------------------------- |
183 | 183 | */ |
184 | - public static function get_database_fields() { |
|
185 | - |
|
186 | - $fields = array( |
|
187 | - 'id' => '%d', |
|
188 | - 'user_id' => '%d', |
|
189 | - 'email' => '%s', |
|
190 | - 'email_cc' => '%s', |
|
191 | - 'status' => '%s', |
|
192 | - 'purchase_value' => '%f', |
|
193 | - 'purchase_count' => '%d', |
|
194 | - 'date_created' => '%s', |
|
195 | - 'date_modified' => '%s', |
|
196 | - 'uuid' => '%s', |
|
197 | - ); |
|
198 | - |
|
199 | - // Add address fields. |
|
200 | - foreach ( array_keys( getpaid_user_address_fields() ) as $field ) { |
|
201 | - |
|
202 | - // Skip id, user_id and email. |
|
203 | - if ( ! in_array( $field, array( 'id', 'user_id', 'email', 'purchase_value', 'purchase_count', 'date_created', 'date_modified', 'uuid' ), true ) ) { |
|
204 | - $fields[ $field ] = '%s'; |
|
205 | - } |
|
206 | - } |
|
207 | - |
|
208 | - return $fields; |
|
209 | - } |
|
184 | + public static function get_database_fields() { |
|
185 | + |
|
186 | + $fields = array( |
|
187 | + 'id' => '%d', |
|
188 | + 'user_id' => '%d', |
|
189 | + 'email' => '%s', |
|
190 | + 'email_cc' => '%s', |
|
191 | + 'status' => '%s', |
|
192 | + 'purchase_value' => '%f', |
|
193 | + 'purchase_count' => '%d', |
|
194 | + 'date_created' => '%s', |
|
195 | + 'date_modified' => '%s', |
|
196 | + 'uuid' => '%s', |
|
197 | + ); |
|
198 | + |
|
199 | + // Add address fields. |
|
200 | + foreach ( array_keys( getpaid_user_address_fields() ) as $field ) { |
|
201 | + |
|
202 | + // Skip id, user_id and email. |
|
203 | + if ( ! in_array( $field, array( 'id', 'user_id', 'email', 'purchase_value', 'purchase_count', 'date_created', 'date_modified', 'uuid' ), true ) ) { |
|
204 | + $fields[ $field ] = '%s'; |
|
205 | + } |
|
206 | + } |
|
207 | + |
|
208 | + return $fields; |
|
209 | + } |
|
210 | 210 | } |
@@ -5,7 +5,7 @@ discard block |
||
5 | 5 | * |
6 | 6 | */ |
7 | 7 | if ( ! defined( 'ABSPATH' ) ) { |
8 | - exit; |
|
8 | + exit; |
|
9 | 9 | } |
10 | 10 | |
11 | 11 | /** |
@@ -15,545 +15,545 @@ discard block |
||
15 | 15 | */ |
16 | 16 | class GetPaid_Invoice_Data_Store extends GetPaid_Data_Store_WP { |
17 | 17 | |
18 | - /** |
|
19 | - * Data stored in meta keys, but not considered "meta" for a discount. |
|
20 | - * |
|
21 | - * @since 1.0.19 |
|
22 | - * @var array |
|
23 | - */ |
|
24 | - protected $internal_meta_keys = array( |
|
25 | - '_wpinv_subscr_profile_id', |
|
26 | - '_wpinv_subscription_id', |
|
27 | - '_wpinv_taxes', |
|
28 | - '_wpinv_fees', |
|
29 | - '_wpinv_discounts', |
|
30 | - '_wpinv_submission_id', |
|
31 | - '_wpinv_payment_form', |
|
32 | - '_wpinv_is_viewed', |
|
33 | - '_wpinv_phone', |
|
34 | - '_wpinv_company_id', |
|
35 | - 'wpinv_shipping', |
|
36 | - 'wpinv_email_cc', |
|
37 | - 'wpinv_template', |
|
38 | - 'wpinv_created_via', |
|
39 | - ); |
|
40 | - |
|
41 | - /** |
|
42 | - * A map of meta keys to data props. |
|
43 | - * |
|
44 | - * @since 1.0.19 |
|
45 | - * |
|
46 | - * @var array |
|
47 | - */ |
|
48 | - protected $meta_key_to_props = array( |
|
49 | - '_wpinv_subscr_profile_id' => 'remote_subscription_id', |
|
50 | - '_wpinv_subscription_id' => 'subscription_id', |
|
51 | - '_wpinv_taxes' => 'taxes', |
|
52 | - '_wpinv_fees' => 'fees', |
|
53 | - '_wpinv_discounts' => 'discounts', |
|
54 | - '_wpinv_submission_id' => 'submission_id', |
|
55 | - '_wpinv_payment_form' => 'payment_form', |
|
56 | - '_wpinv_is_viewed' => 'is_viewed', |
|
57 | - 'wpinv_email_cc' => 'email_cc', |
|
58 | - 'wpinv_template' => 'template', |
|
59 | - 'wpinv_created_via' => 'created_via', |
|
60 | - '_wpinv_phone' => 'phone', |
|
61 | - '_wpinv_company_id' => 'company_id', |
|
62 | - 'wpinv_shipping' => 'shipping', |
|
63 | - ); |
|
64 | - |
|
65 | - /** |
|
66 | - * A map of database fields to data props. |
|
67 | - * |
|
68 | - * @since 1.0.19 |
|
69 | - * |
|
70 | - * @var array |
|
71 | - */ |
|
72 | - protected $database_fields_to_props = array( |
|
73 | - 'post_id' => 'id', |
|
74 | - 'number' => 'number', |
|
75 | - 'currency' => 'currency', |
|
76 | - 'invoice_key' => 'key', |
|
77 | - 'type' => 'type', |
|
78 | - 'mode' => 'mode', |
|
79 | - 'user_ip' => 'user_ip', |
|
80 | - 'first_name' => 'first_name', |
|
81 | - 'last_name' => 'last_name', |
|
82 | - 'address' => 'address', |
|
83 | - 'city' => 'city', |
|
84 | - 'state' => 'state', |
|
85 | - 'country' => 'country', |
|
86 | - 'zip' => 'zip', |
|
87 | - 'zip' => 'zip', |
|
88 | - 'adddress_confirmed' => 'address_confirmed', |
|
89 | - 'gateway' => 'gateway', |
|
90 | - 'transaction_id' => 'transaction_id', |
|
91 | - 'currency' => 'currency', |
|
92 | - 'subtotal' => 'subtotal', |
|
93 | - 'tax' => 'total_tax', |
|
94 | - 'fees_total' => 'total_fees', |
|
95 | - 'discount' => 'total_discount', |
|
96 | - 'total' => 'total', |
|
97 | - 'discount_code' => 'discount_code', |
|
98 | - 'disable_taxes' => 'disable_taxes', |
|
99 | - 'due_date' => 'due_date', |
|
100 | - 'completed_date' => 'completed_date', |
|
101 | - 'company' => 'company', |
|
102 | - 'vat_number' => 'vat_number', |
|
103 | - 'vat_rate' => 'vat_rate', |
|
104 | - 'customer_id' => 'customer_id', |
|
105 | - ); |
|
106 | - |
|
107 | - /* |
|
18 | + /** |
|
19 | + * Data stored in meta keys, but not considered "meta" for a discount. |
|
20 | + * |
|
21 | + * @since 1.0.19 |
|
22 | + * @var array |
|
23 | + */ |
|
24 | + protected $internal_meta_keys = array( |
|
25 | + '_wpinv_subscr_profile_id', |
|
26 | + '_wpinv_subscription_id', |
|
27 | + '_wpinv_taxes', |
|
28 | + '_wpinv_fees', |
|
29 | + '_wpinv_discounts', |
|
30 | + '_wpinv_submission_id', |
|
31 | + '_wpinv_payment_form', |
|
32 | + '_wpinv_is_viewed', |
|
33 | + '_wpinv_phone', |
|
34 | + '_wpinv_company_id', |
|
35 | + 'wpinv_shipping', |
|
36 | + 'wpinv_email_cc', |
|
37 | + 'wpinv_template', |
|
38 | + 'wpinv_created_via', |
|
39 | + ); |
|
40 | + |
|
41 | + /** |
|
42 | + * A map of meta keys to data props. |
|
43 | + * |
|
44 | + * @since 1.0.19 |
|
45 | + * |
|
46 | + * @var array |
|
47 | + */ |
|
48 | + protected $meta_key_to_props = array( |
|
49 | + '_wpinv_subscr_profile_id' => 'remote_subscription_id', |
|
50 | + '_wpinv_subscription_id' => 'subscription_id', |
|
51 | + '_wpinv_taxes' => 'taxes', |
|
52 | + '_wpinv_fees' => 'fees', |
|
53 | + '_wpinv_discounts' => 'discounts', |
|
54 | + '_wpinv_submission_id' => 'submission_id', |
|
55 | + '_wpinv_payment_form' => 'payment_form', |
|
56 | + '_wpinv_is_viewed' => 'is_viewed', |
|
57 | + 'wpinv_email_cc' => 'email_cc', |
|
58 | + 'wpinv_template' => 'template', |
|
59 | + 'wpinv_created_via' => 'created_via', |
|
60 | + '_wpinv_phone' => 'phone', |
|
61 | + '_wpinv_company_id' => 'company_id', |
|
62 | + 'wpinv_shipping' => 'shipping', |
|
63 | + ); |
|
64 | + |
|
65 | + /** |
|
66 | + * A map of database fields to data props. |
|
67 | + * |
|
68 | + * @since 1.0.19 |
|
69 | + * |
|
70 | + * @var array |
|
71 | + */ |
|
72 | + protected $database_fields_to_props = array( |
|
73 | + 'post_id' => 'id', |
|
74 | + 'number' => 'number', |
|
75 | + 'currency' => 'currency', |
|
76 | + 'invoice_key' => 'key', |
|
77 | + 'type' => 'type', |
|
78 | + 'mode' => 'mode', |
|
79 | + 'user_ip' => 'user_ip', |
|
80 | + 'first_name' => 'first_name', |
|
81 | + 'last_name' => 'last_name', |
|
82 | + 'address' => 'address', |
|
83 | + 'city' => 'city', |
|
84 | + 'state' => 'state', |
|
85 | + 'country' => 'country', |
|
86 | + 'zip' => 'zip', |
|
87 | + 'zip' => 'zip', |
|
88 | + 'adddress_confirmed' => 'address_confirmed', |
|
89 | + 'gateway' => 'gateway', |
|
90 | + 'transaction_id' => 'transaction_id', |
|
91 | + 'currency' => 'currency', |
|
92 | + 'subtotal' => 'subtotal', |
|
93 | + 'tax' => 'total_tax', |
|
94 | + 'fees_total' => 'total_fees', |
|
95 | + 'discount' => 'total_discount', |
|
96 | + 'total' => 'total', |
|
97 | + 'discount_code' => 'discount_code', |
|
98 | + 'disable_taxes' => 'disable_taxes', |
|
99 | + 'due_date' => 'due_date', |
|
100 | + 'completed_date' => 'completed_date', |
|
101 | + 'company' => 'company', |
|
102 | + 'vat_number' => 'vat_number', |
|
103 | + 'vat_rate' => 'vat_rate', |
|
104 | + 'customer_id' => 'customer_id', |
|
105 | + ); |
|
106 | + |
|
107 | + /* |
|
108 | 108 | |-------------------------------------------------------------------------- |
109 | 109 | | CRUD Methods |
110 | 110 | |-------------------------------------------------------------------------- |
111 | 111 | */ |
112 | 112 | |
113 | - /** |
|
114 | - * Method to create a new invoice in the database. |
|
115 | - * |
|
116 | - * @param WPInv_Invoice $invoice Invoice object. |
|
117 | - */ |
|
118 | - public function create( &$invoice ) { |
|
119 | - $invoice->set_version( WPINV_VERSION ); |
|
120 | - $invoice->set_date_created( current_time( 'mysql' ) ); |
|
121 | - |
|
122 | - // Create a new post. |
|
123 | - $id = wp_insert_post( |
|
124 | - apply_filters( |
|
125 | - 'getpaid_new_invoice_data', |
|
126 | - array( |
|
127 | - 'post_date' => $invoice->get_date_created( 'edit' ), |
|
128 | - 'post_type' => $invoice->get_post_type( 'edit' ), |
|
129 | - 'post_status' => $this->get_post_status( $invoice ), |
|
130 | - 'ping_status' => 'closed', |
|
131 | - 'post_author' => $invoice->get_user_id( 'edit' ), |
|
132 | - 'post_title' => $invoice->get_title( 'edit' ), |
|
133 | - 'post_excerpt' => $invoice->get_description( 'edit' ), |
|
134 | - 'post_parent' => $invoice->get_parent_id( 'edit' ), |
|
135 | - ) |
|
136 | - ), |
|
137 | - true |
|
138 | - ); |
|
139 | - |
|
140 | - if ( $id && ! is_wp_error( $id ) ) { |
|
141 | - |
|
142 | - // Update the new id and regenerate a title. |
|
143 | - $invoice->set_id( $id ); |
|
144 | - |
|
145 | - $invoice->maybe_set_number(); |
|
146 | - |
|
147 | - wp_update_post( |
|
148 | - array( |
|
149 | - 'ID' => $invoice->get_id(), |
|
150 | - 'post_title' => $invoice->get_number( 'edit' ), |
|
151 | - 'post_name' => $invoice->get_path( 'edit' ), |
|
152 | - ) |
|
153 | - ); |
|
154 | - |
|
155 | - // Save special fields and items. |
|
156 | - $this->save_special_fields( $invoice ); |
|
157 | - $this->save_items( $invoice ); |
|
158 | - |
|
159 | - // Update meta data. |
|
160 | - $this->update_post_meta( $invoice ); |
|
161 | - $invoice->save_meta_data(); |
|
162 | - |
|
163 | - // Apply changes. |
|
164 | - $invoice->apply_changes(); |
|
165 | - $this->clear_caches( $invoice ); |
|
166 | - |
|
167 | - // Fires after a new invoice is created. |
|
168 | - do_action( 'getpaid_new_invoice', $invoice ); |
|
169 | - return true; |
|
170 | - } |
|
171 | - |
|
172 | - if ( is_wp_error( $id ) ) { |
|
173 | - $invoice->last_error = $id->get_error_message(); |
|
174 | - } |
|
175 | - |
|
176 | - return false; |
|
177 | - } |
|
178 | - |
|
179 | - /** |
|
180 | - * Method to read an invoice from the database. |
|
181 | - * |
|
182 | - * @param WPInv_Invoice $invoice Invoice object. |
|
183 | - * |
|
184 | - */ |
|
185 | - public function read( &$invoice ) { |
|
186 | - |
|
187 | - $invoice->set_defaults(); |
|
188 | - $invoice_object = get_post( $invoice->get_id() ); |
|
189 | - |
|
190 | - if ( ! $invoice->get_id() || ! $invoice_object || ! getpaid_is_invoice_post_type( $invoice_object->post_type ) ) { |
|
191 | - $invoice->last_error = __( 'Invalid invoice.', 'invoicing' ); |
|
192 | - $invoice->set_id( 0 ); |
|
193 | - return false; |
|
194 | - } |
|
195 | - |
|
196 | - $invoice->set_props( |
|
197 | - array( |
|
198 | - 'date_created' => 0 < $invoice_object->post_date ? $invoice_object->post_date : null, |
|
199 | - 'date_modified' => 0 < $invoice_object->post_modified ? $invoice_object->post_modified : null, |
|
200 | - 'status' => $invoice_object->post_status, |
|
201 | - 'author' => $invoice_object->post_author, |
|
202 | - 'description' => $invoice_object->post_excerpt, |
|
203 | - 'parent_id' => $invoice_object->post_parent, |
|
204 | - 'name' => $invoice_object->post_title, |
|
205 | - 'path' => $invoice_object->post_name, |
|
206 | - 'post_type' => $invoice_object->post_type, |
|
207 | - ) |
|
208 | - ); |
|
209 | - |
|
210 | - $invoice->set_type( $invoice_object->post_type ); |
|
211 | - |
|
212 | - $this->read_object_data( $invoice, $invoice_object ); |
|
213 | - $this->add_special_fields( $invoice ); |
|
214 | - $this->add_items( $invoice ); |
|
215 | - $invoice->read_meta_data(); |
|
216 | - $invoice->set_object_read( true ); |
|
217 | - do_action( 'getpaid_read_invoice', $invoice ); |
|
218 | - |
|
219 | - } |
|
220 | - |
|
221 | - /** |
|
222 | - * Method to update an invoice in the database. |
|
223 | - * |
|
224 | - * @param WPInv_Invoice $invoice Invoice object. |
|
225 | - */ |
|
226 | - public function update( &$invoice ) { |
|
227 | - $invoice->save_meta_data(); |
|
228 | - $invoice->set_version( WPINV_VERSION ); |
|
229 | - |
|
230 | - if ( null === $invoice->get_date_created( 'edit' ) ) { |
|
231 | - $invoice->set_date_created( current_time( 'mysql' ) ); |
|
232 | - } |
|
233 | - |
|
234 | - // Ensure both the key and number are set. |
|
235 | - $invoice->get_path(); |
|
236 | - |
|
237 | - // Grab the current status so we can compare. |
|
238 | - $previous_status = get_post_status( $invoice->get_id() ); |
|
239 | - |
|
240 | - $changes = $invoice->get_changes(); |
|
241 | - |
|
242 | - // Only update the post when the post data changes. |
|
243 | - if ( array_intersect( array( 'date_created', 'date_modified', 'status', 'name', 'author', 'description', 'parent_id', 'post_excerpt', 'path' ), array_keys( $changes ) ) ) { |
|
244 | - $post_data = array( |
|
245 | - 'post_date' => $invoice->get_date_created( 'edit' ), |
|
246 | - 'post_date_gmt' => $invoice->get_date_created_gmt( 'edit' ), |
|
247 | - 'post_status' => $invoice->get_status( 'edit' ), |
|
248 | - 'post_title' => $invoice->get_name( 'edit' ), |
|
249 | - 'post_author' => $invoice->get_user_id( 'edit' ), |
|
250 | - 'post_modified' => $invoice->get_date_modified( 'edit' ), |
|
251 | - 'post_excerpt' => $invoice->get_description( 'edit' ), |
|
252 | - 'post_parent' => $invoice->get_parent_id( 'edit' ), |
|
253 | - 'post_name' => $invoice->get_path( 'edit' ), |
|
254 | - 'post_type' => $invoice->get_post_type( 'edit' ), |
|
255 | - ); |
|
256 | - |
|
257 | - /** |
|
258 | - * When updating this object, to prevent infinite loops, use $wpdb |
|
259 | - * to update data, since wp_update_post spawns more calls to the |
|
260 | - * save_post action. |
|
261 | - * |
|
262 | - * This ensures hooks are fired by either WP itself (admin screen save), |
|
263 | - * or an update purely from CRUD. |
|
264 | - */ |
|
265 | - if ( doing_action( 'save_post' ) ) { |
|
266 | - $GLOBALS['wpdb']->update( $GLOBALS['wpdb']->posts, $post_data, array( 'ID' => $invoice->get_id() ) ); |
|
267 | - clean_post_cache( $invoice->get_id() ); |
|
268 | - } else { |
|
269 | - wp_update_post( array_merge( array( 'ID' => $invoice->get_id() ), $post_data ) ); |
|
270 | - } |
|
271 | - $invoice->read_meta_data( true ); // Refresh internal meta data, in case things were hooked into `save_post` or another WP hook. |
|
272 | - } |
|
273 | - |
|
274 | - // Update meta data. |
|
275 | - $this->update_post_meta( $invoice ); |
|
276 | - |
|
277 | - // Save special fields and items. |
|
278 | - $this->save_special_fields( $invoice ); |
|
279 | - $this->save_items( $invoice ); |
|
280 | - |
|
281 | - // Apply the changes. |
|
282 | - $invoice->apply_changes(); |
|
283 | - |
|
284 | - // Clear caches. |
|
285 | - $this->clear_caches( $invoice ); |
|
286 | - |
|
287 | - // Fire a hook depending on the status - this should be considered a creation if it was previously draft status. |
|
288 | - $new_status = $invoice->get_status( 'edit' ); |
|
289 | - |
|
290 | - if ( $new_status !== $previous_status && in_array( $previous_status, array( 'new', 'auto-draft', 'draft' ), true ) ) { |
|
291 | - do_action( 'getpaid_new_invoice', $invoice ); |
|
292 | - } else { |
|
293 | - do_action( 'getpaid_update_invoice', $invoice ); |
|
294 | - } |
|
295 | - |
|
296 | - } |
|
297 | - |
|
298 | - /* |
|
113 | + /** |
|
114 | + * Method to create a new invoice in the database. |
|
115 | + * |
|
116 | + * @param WPInv_Invoice $invoice Invoice object. |
|
117 | + */ |
|
118 | + public function create( &$invoice ) { |
|
119 | + $invoice->set_version( WPINV_VERSION ); |
|
120 | + $invoice->set_date_created( current_time( 'mysql' ) ); |
|
121 | + |
|
122 | + // Create a new post. |
|
123 | + $id = wp_insert_post( |
|
124 | + apply_filters( |
|
125 | + 'getpaid_new_invoice_data', |
|
126 | + array( |
|
127 | + 'post_date' => $invoice->get_date_created( 'edit' ), |
|
128 | + 'post_type' => $invoice->get_post_type( 'edit' ), |
|
129 | + 'post_status' => $this->get_post_status( $invoice ), |
|
130 | + 'ping_status' => 'closed', |
|
131 | + 'post_author' => $invoice->get_user_id( 'edit' ), |
|
132 | + 'post_title' => $invoice->get_title( 'edit' ), |
|
133 | + 'post_excerpt' => $invoice->get_description( 'edit' ), |
|
134 | + 'post_parent' => $invoice->get_parent_id( 'edit' ), |
|
135 | + ) |
|
136 | + ), |
|
137 | + true |
|
138 | + ); |
|
139 | + |
|
140 | + if ( $id && ! is_wp_error( $id ) ) { |
|
141 | + |
|
142 | + // Update the new id and regenerate a title. |
|
143 | + $invoice->set_id( $id ); |
|
144 | + |
|
145 | + $invoice->maybe_set_number(); |
|
146 | + |
|
147 | + wp_update_post( |
|
148 | + array( |
|
149 | + 'ID' => $invoice->get_id(), |
|
150 | + 'post_title' => $invoice->get_number( 'edit' ), |
|
151 | + 'post_name' => $invoice->get_path( 'edit' ), |
|
152 | + ) |
|
153 | + ); |
|
154 | + |
|
155 | + // Save special fields and items. |
|
156 | + $this->save_special_fields( $invoice ); |
|
157 | + $this->save_items( $invoice ); |
|
158 | + |
|
159 | + // Update meta data. |
|
160 | + $this->update_post_meta( $invoice ); |
|
161 | + $invoice->save_meta_data(); |
|
162 | + |
|
163 | + // Apply changes. |
|
164 | + $invoice->apply_changes(); |
|
165 | + $this->clear_caches( $invoice ); |
|
166 | + |
|
167 | + // Fires after a new invoice is created. |
|
168 | + do_action( 'getpaid_new_invoice', $invoice ); |
|
169 | + return true; |
|
170 | + } |
|
171 | + |
|
172 | + if ( is_wp_error( $id ) ) { |
|
173 | + $invoice->last_error = $id->get_error_message(); |
|
174 | + } |
|
175 | + |
|
176 | + return false; |
|
177 | + } |
|
178 | + |
|
179 | + /** |
|
180 | + * Method to read an invoice from the database. |
|
181 | + * |
|
182 | + * @param WPInv_Invoice $invoice Invoice object. |
|
183 | + * |
|
184 | + */ |
|
185 | + public function read( &$invoice ) { |
|
186 | + |
|
187 | + $invoice->set_defaults(); |
|
188 | + $invoice_object = get_post( $invoice->get_id() ); |
|
189 | + |
|
190 | + if ( ! $invoice->get_id() || ! $invoice_object || ! getpaid_is_invoice_post_type( $invoice_object->post_type ) ) { |
|
191 | + $invoice->last_error = __( 'Invalid invoice.', 'invoicing' ); |
|
192 | + $invoice->set_id( 0 ); |
|
193 | + return false; |
|
194 | + } |
|
195 | + |
|
196 | + $invoice->set_props( |
|
197 | + array( |
|
198 | + 'date_created' => 0 < $invoice_object->post_date ? $invoice_object->post_date : null, |
|
199 | + 'date_modified' => 0 < $invoice_object->post_modified ? $invoice_object->post_modified : null, |
|
200 | + 'status' => $invoice_object->post_status, |
|
201 | + 'author' => $invoice_object->post_author, |
|
202 | + 'description' => $invoice_object->post_excerpt, |
|
203 | + 'parent_id' => $invoice_object->post_parent, |
|
204 | + 'name' => $invoice_object->post_title, |
|
205 | + 'path' => $invoice_object->post_name, |
|
206 | + 'post_type' => $invoice_object->post_type, |
|
207 | + ) |
|
208 | + ); |
|
209 | + |
|
210 | + $invoice->set_type( $invoice_object->post_type ); |
|
211 | + |
|
212 | + $this->read_object_data( $invoice, $invoice_object ); |
|
213 | + $this->add_special_fields( $invoice ); |
|
214 | + $this->add_items( $invoice ); |
|
215 | + $invoice->read_meta_data(); |
|
216 | + $invoice->set_object_read( true ); |
|
217 | + do_action( 'getpaid_read_invoice', $invoice ); |
|
218 | + |
|
219 | + } |
|
220 | + |
|
221 | + /** |
|
222 | + * Method to update an invoice in the database. |
|
223 | + * |
|
224 | + * @param WPInv_Invoice $invoice Invoice object. |
|
225 | + */ |
|
226 | + public function update( &$invoice ) { |
|
227 | + $invoice->save_meta_data(); |
|
228 | + $invoice->set_version( WPINV_VERSION ); |
|
229 | + |
|
230 | + if ( null === $invoice->get_date_created( 'edit' ) ) { |
|
231 | + $invoice->set_date_created( current_time( 'mysql' ) ); |
|
232 | + } |
|
233 | + |
|
234 | + // Ensure both the key and number are set. |
|
235 | + $invoice->get_path(); |
|
236 | + |
|
237 | + // Grab the current status so we can compare. |
|
238 | + $previous_status = get_post_status( $invoice->get_id() ); |
|
239 | + |
|
240 | + $changes = $invoice->get_changes(); |
|
241 | + |
|
242 | + // Only update the post when the post data changes. |
|
243 | + if ( array_intersect( array( 'date_created', 'date_modified', 'status', 'name', 'author', 'description', 'parent_id', 'post_excerpt', 'path' ), array_keys( $changes ) ) ) { |
|
244 | + $post_data = array( |
|
245 | + 'post_date' => $invoice->get_date_created( 'edit' ), |
|
246 | + 'post_date_gmt' => $invoice->get_date_created_gmt( 'edit' ), |
|
247 | + 'post_status' => $invoice->get_status( 'edit' ), |
|
248 | + 'post_title' => $invoice->get_name( 'edit' ), |
|
249 | + 'post_author' => $invoice->get_user_id( 'edit' ), |
|
250 | + 'post_modified' => $invoice->get_date_modified( 'edit' ), |
|
251 | + 'post_excerpt' => $invoice->get_description( 'edit' ), |
|
252 | + 'post_parent' => $invoice->get_parent_id( 'edit' ), |
|
253 | + 'post_name' => $invoice->get_path( 'edit' ), |
|
254 | + 'post_type' => $invoice->get_post_type( 'edit' ), |
|
255 | + ); |
|
256 | + |
|
257 | + /** |
|
258 | + * When updating this object, to prevent infinite loops, use $wpdb |
|
259 | + * to update data, since wp_update_post spawns more calls to the |
|
260 | + * save_post action. |
|
261 | + * |
|
262 | + * This ensures hooks are fired by either WP itself (admin screen save), |
|
263 | + * or an update purely from CRUD. |
|
264 | + */ |
|
265 | + if ( doing_action( 'save_post' ) ) { |
|
266 | + $GLOBALS['wpdb']->update( $GLOBALS['wpdb']->posts, $post_data, array( 'ID' => $invoice->get_id() ) ); |
|
267 | + clean_post_cache( $invoice->get_id() ); |
|
268 | + } else { |
|
269 | + wp_update_post( array_merge( array( 'ID' => $invoice->get_id() ), $post_data ) ); |
|
270 | + } |
|
271 | + $invoice->read_meta_data( true ); // Refresh internal meta data, in case things were hooked into `save_post` or another WP hook. |
|
272 | + } |
|
273 | + |
|
274 | + // Update meta data. |
|
275 | + $this->update_post_meta( $invoice ); |
|
276 | + |
|
277 | + // Save special fields and items. |
|
278 | + $this->save_special_fields( $invoice ); |
|
279 | + $this->save_items( $invoice ); |
|
280 | + |
|
281 | + // Apply the changes. |
|
282 | + $invoice->apply_changes(); |
|
283 | + |
|
284 | + // Clear caches. |
|
285 | + $this->clear_caches( $invoice ); |
|
286 | + |
|
287 | + // Fire a hook depending on the status - this should be considered a creation if it was previously draft status. |
|
288 | + $new_status = $invoice->get_status( 'edit' ); |
|
289 | + |
|
290 | + if ( $new_status !== $previous_status && in_array( $previous_status, array( 'new', 'auto-draft', 'draft' ), true ) ) { |
|
291 | + do_action( 'getpaid_new_invoice', $invoice ); |
|
292 | + } else { |
|
293 | + do_action( 'getpaid_update_invoice', $invoice ); |
|
294 | + } |
|
295 | + |
|
296 | + } |
|
297 | + |
|
298 | + /* |
|
299 | 299 | |-------------------------------------------------------------------------- |
300 | 300 | | Additional Methods |
301 | 301 | |-------------------------------------------------------------------------- |
302 | 302 | */ |
303 | 303 | |
304 | - /** |
|
304 | + /** |
|
305 | 305 | * Retrieves special fields and adds to the invoice. |
306 | - * |
|
307 | - * @param WPInv_Invoice $invoice Invoice object. |
|
306 | + * |
|
307 | + * @param WPInv_Invoice $invoice Invoice object. |
|
308 | 308 | */ |
309 | 309 | public function add_special_fields( &$invoice ) { |
310 | - global $wpdb; |
|
311 | - |
|
312 | - // Maybe retrieve from the cache. |
|
313 | - $data = wp_cache_get( $invoice->get_id(), 'getpaid_invoice_special_fields' ); |
|
314 | - |
|
315 | - // If not found, retrieve from the db. |
|
316 | - if ( false === $data ) { |
|
317 | - $table = $wpdb->prefix . 'getpaid_invoices'; |
|
318 | - |
|
319 | - $data = $wpdb->get_row( |
|
320 | - $wpdb->prepare( "SELECT * FROM $table WHERE `post_id`=%d LIMIT 1", $invoice->get_id() ), |
|
321 | - ARRAY_A |
|
322 | - ); |
|
323 | - |
|
324 | - // Update the cache with our data |
|
325 | - wp_cache_set( $invoice->get_id(), $data, 'getpaid_invoice_special_fields' ); |
|
326 | - |
|
327 | - } |
|
328 | - |
|
329 | - // Abort if the data does not exist. |
|
330 | - if ( empty( $data ) ) { |
|
331 | - $invoice->set_object_read( true ); |
|
332 | - $invoice->set_props( wpinv_get_user_address( $invoice->get_user_id() ) ); |
|
333 | - return; |
|
334 | - } |
|
335 | - |
|
336 | - $props = array(); |
|
337 | - |
|
338 | - foreach ( $this->database_fields_to_props as $db_field => $prop ) { |
|
339 | - |
|
340 | - if ( $db_field == 'post_id' ) { |
|
341 | - continue; |
|
342 | - } |
|
343 | - |
|
344 | - $props[ $prop ] = $data[ $db_field ]; |
|
345 | - } |
|
346 | - |
|
347 | - $invoice->set_props( $props ); |
|
348 | - |
|
349 | - } |
|
350 | - |
|
351 | - /** |
|
352 | - * Gets a list of special fields that need updated based on change state |
|
353 | - * or if they are present in the database or not. |
|
354 | - * |
|
355 | - * @param WPInv_Invoice $invoice The Invoice object. |
|
356 | - * @return array A mapping of field keys => prop names, filtered by ones that should be updated. |
|
357 | - */ |
|
358 | - protected function get_special_fields_to_update( $invoice ) { |
|
359 | - $fields_to_update = array(); |
|
360 | - $changed_props = $invoice->get_changes(); |
|
361 | - |
|
362 | - // Props should be updated if they are a part of the $changed array or don't exist yet. |
|
363 | - foreach ( $this->database_fields_to_props as $database_field => $prop ) { |
|
364 | - if ( array_key_exists( $prop, $changed_props ) ) { |
|
365 | - $fields_to_update[ $database_field ] = $prop; |
|
366 | - } |
|
367 | - } |
|
368 | - |
|
369 | - return $fields_to_update; |
|
370 | - } |
|
371 | - |
|
372 | - /** |
|
373 | - * Helper method that updates all the database fields for an invoice based on it's settings in the WPInv_Invoice class. |
|
374 | - * |
|
375 | - * @param WPInv_Invoice $invoice WPInv_Invoice object. |
|
376 | - * @since 1.0.19 |
|
377 | - */ |
|
378 | - protected function update_special_fields( &$invoice ) { |
|
379 | - global $wpdb; |
|
380 | - |
|
381 | - $updated_props = array(); |
|
382 | - $fields_to_update = $this->get_special_fields_to_update( $invoice ); |
|
383 | - |
|
384 | - foreach ( $fields_to_update as $database_field => $prop ) { |
|
385 | - $value = $invoice->{"get_$prop"}( 'edit' ); |
|
386 | - $value = is_string( $value ) ? wp_slash( $value ) : $value; |
|
387 | - $value = is_bool( $value ) ? (int) $value : $value; |
|
388 | - $updated_props[ $database_field ] = maybe_serialize( $value ); |
|
389 | - } |
|
390 | - |
|
391 | - if ( ! empty( $updated_props ) ) { |
|
392 | - |
|
393 | - $table = $wpdb->prefix . 'getpaid_invoices'; |
|
394 | - $wpdb->update( $table, $updated_props, array( 'post_id' => $invoice->get_id() ) ); |
|
395 | - wp_cache_delete( $invoice->get_id(), 'getpaid_invoice_special_fields' ); |
|
396 | - do_action( 'getpaid_invoice_update_database_fields', $invoice, $updated_props ); |
|
397 | - |
|
398 | - } |
|
399 | - |
|
400 | - } |
|
401 | - |
|
402 | - /** |
|
403 | - * Helper method that inserts special fields to the database. |
|
404 | - * |
|
405 | - * @param WPInv_Invoice $invoice WPInv_Invoice object. |
|
406 | - * @since 1.0.19 |
|
407 | - */ |
|
408 | - protected function insert_special_fields( &$invoice ) { |
|
409 | - global $wpdb; |
|
410 | - |
|
411 | - $updated_props = array(); |
|
412 | - |
|
413 | - foreach ( $this->database_fields_to_props as $database_field => $prop ) { |
|
414 | - $value = $invoice->{"get_$prop"}( 'edit' ); |
|
415 | - $value = is_string( $value ) ? wp_slash( $value ) : $value; |
|
416 | - $value = is_bool( $value ) ? (int) $value : $value; |
|
417 | - $updated_props[ $database_field ] = maybe_serialize( $value ); |
|
418 | - } |
|
419 | - |
|
420 | - $table = $wpdb->prefix . 'getpaid_invoices'; |
|
421 | - $wpdb->insert( $table, $updated_props ); |
|
422 | - wp_cache_delete( $invoice->get_id(), 'getpaid_invoice_special_fields' ); |
|
423 | - do_action( 'getpaid_invoice_insert_database_fields', $invoice, $updated_props ); |
|
424 | - |
|
425 | - } |
|
426 | - |
|
427 | - /** |
|
310 | + global $wpdb; |
|
311 | + |
|
312 | + // Maybe retrieve from the cache. |
|
313 | + $data = wp_cache_get( $invoice->get_id(), 'getpaid_invoice_special_fields' ); |
|
314 | + |
|
315 | + // If not found, retrieve from the db. |
|
316 | + if ( false === $data ) { |
|
317 | + $table = $wpdb->prefix . 'getpaid_invoices'; |
|
318 | + |
|
319 | + $data = $wpdb->get_row( |
|
320 | + $wpdb->prepare( "SELECT * FROM $table WHERE `post_id`=%d LIMIT 1", $invoice->get_id() ), |
|
321 | + ARRAY_A |
|
322 | + ); |
|
323 | + |
|
324 | + // Update the cache with our data |
|
325 | + wp_cache_set( $invoice->get_id(), $data, 'getpaid_invoice_special_fields' ); |
|
326 | + |
|
327 | + } |
|
328 | + |
|
329 | + // Abort if the data does not exist. |
|
330 | + if ( empty( $data ) ) { |
|
331 | + $invoice->set_object_read( true ); |
|
332 | + $invoice->set_props( wpinv_get_user_address( $invoice->get_user_id() ) ); |
|
333 | + return; |
|
334 | + } |
|
335 | + |
|
336 | + $props = array(); |
|
337 | + |
|
338 | + foreach ( $this->database_fields_to_props as $db_field => $prop ) { |
|
339 | + |
|
340 | + if ( $db_field == 'post_id' ) { |
|
341 | + continue; |
|
342 | + } |
|
343 | + |
|
344 | + $props[ $prop ] = $data[ $db_field ]; |
|
345 | + } |
|
346 | + |
|
347 | + $invoice->set_props( $props ); |
|
348 | + |
|
349 | + } |
|
350 | + |
|
351 | + /** |
|
352 | + * Gets a list of special fields that need updated based on change state |
|
353 | + * or if they are present in the database or not. |
|
354 | + * |
|
355 | + * @param WPInv_Invoice $invoice The Invoice object. |
|
356 | + * @return array A mapping of field keys => prop names, filtered by ones that should be updated. |
|
357 | + */ |
|
358 | + protected function get_special_fields_to_update( $invoice ) { |
|
359 | + $fields_to_update = array(); |
|
360 | + $changed_props = $invoice->get_changes(); |
|
361 | + |
|
362 | + // Props should be updated if they are a part of the $changed array or don't exist yet. |
|
363 | + foreach ( $this->database_fields_to_props as $database_field => $prop ) { |
|
364 | + if ( array_key_exists( $prop, $changed_props ) ) { |
|
365 | + $fields_to_update[ $database_field ] = $prop; |
|
366 | + } |
|
367 | + } |
|
368 | + |
|
369 | + return $fields_to_update; |
|
370 | + } |
|
371 | + |
|
372 | + /** |
|
373 | + * Helper method that updates all the database fields for an invoice based on it's settings in the WPInv_Invoice class. |
|
374 | + * |
|
375 | + * @param WPInv_Invoice $invoice WPInv_Invoice object. |
|
376 | + * @since 1.0.19 |
|
377 | + */ |
|
378 | + protected function update_special_fields( &$invoice ) { |
|
379 | + global $wpdb; |
|
380 | + |
|
381 | + $updated_props = array(); |
|
382 | + $fields_to_update = $this->get_special_fields_to_update( $invoice ); |
|
383 | + |
|
384 | + foreach ( $fields_to_update as $database_field => $prop ) { |
|
385 | + $value = $invoice->{"get_$prop"}( 'edit' ); |
|
386 | + $value = is_string( $value ) ? wp_slash( $value ) : $value; |
|
387 | + $value = is_bool( $value ) ? (int) $value : $value; |
|
388 | + $updated_props[ $database_field ] = maybe_serialize( $value ); |
|
389 | + } |
|
390 | + |
|
391 | + if ( ! empty( $updated_props ) ) { |
|
392 | + |
|
393 | + $table = $wpdb->prefix . 'getpaid_invoices'; |
|
394 | + $wpdb->update( $table, $updated_props, array( 'post_id' => $invoice->get_id() ) ); |
|
395 | + wp_cache_delete( $invoice->get_id(), 'getpaid_invoice_special_fields' ); |
|
396 | + do_action( 'getpaid_invoice_update_database_fields', $invoice, $updated_props ); |
|
397 | + |
|
398 | + } |
|
399 | + |
|
400 | + } |
|
401 | + |
|
402 | + /** |
|
403 | + * Helper method that inserts special fields to the database. |
|
404 | + * |
|
405 | + * @param WPInv_Invoice $invoice WPInv_Invoice object. |
|
406 | + * @since 1.0.19 |
|
407 | + */ |
|
408 | + protected function insert_special_fields( &$invoice ) { |
|
409 | + global $wpdb; |
|
410 | + |
|
411 | + $updated_props = array(); |
|
412 | + |
|
413 | + foreach ( $this->database_fields_to_props as $database_field => $prop ) { |
|
414 | + $value = $invoice->{"get_$prop"}( 'edit' ); |
|
415 | + $value = is_string( $value ) ? wp_slash( $value ) : $value; |
|
416 | + $value = is_bool( $value ) ? (int) $value : $value; |
|
417 | + $updated_props[ $database_field ] = maybe_serialize( $value ); |
|
418 | + } |
|
419 | + |
|
420 | + $table = $wpdb->prefix . 'getpaid_invoices'; |
|
421 | + $wpdb->insert( $table, $updated_props ); |
|
422 | + wp_cache_delete( $invoice->get_id(), 'getpaid_invoice_special_fields' ); |
|
423 | + do_action( 'getpaid_invoice_insert_database_fields', $invoice, $updated_props ); |
|
424 | + |
|
425 | + } |
|
426 | + |
|
427 | + /** |
|
428 | 428 | * Saves all special fields. |
429 | - * |
|
430 | - * @param WPInv_Invoice $invoice Invoice object. |
|
429 | + * |
|
430 | + * @param WPInv_Invoice $invoice Invoice object. |
|
431 | 431 | */ |
432 | 432 | public function save_special_fields( &$invoice ) { |
433 | - global $wpdb; |
|
433 | + global $wpdb; |
|
434 | 434 | |
435 | - // The invoices table. |
|
436 | - $table = $wpdb->prefix . 'getpaid_invoices'; |
|
437 | - $id = (int) $invoice->get_id(); |
|
438 | - $invoice->maybe_set_key(); |
|
435 | + // The invoices table. |
|
436 | + $table = $wpdb->prefix . 'getpaid_invoices'; |
|
437 | + $id = (int) $invoice->get_id(); |
|
438 | + $invoice->maybe_set_key(); |
|
439 | 439 | |
440 | - if ( $wpdb->get_var( "SELECT `post_id` FROM $table WHERE `post_id`= $id" ) ) { |
|
440 | + if ( $wpdb->get_var( "SELECT `post_id` FROM $table WHERE `post_id`= $id" ) ) { |
|
441 | 441 | |
442 | - $this->update_special_fields( $invoice ); |
|
442 | + $this->update_special_fields( $invoice ); |
|
443 | 443 | |
444 | - } else { |
|
444 | + } else { |
|
445 | 445 | |
446 | - $this->insert_special_fields( $invoice ); |
|
446 | + $this->insert_special_fields( $invoice ); |
|
447 | 447 | |
448 | - } |
|
448 | + } |
|
449 | 449 | |
450 | - } |
|
450 | + } |
|
451 | 451 | |
452 | - /** |
|
452 | + /** |
|
453 | 453 | * Set's up cart details. |
454 | - * |
|
455 | - * @param WPInv_Invoice $invoice Invoice object. |
|
454 | + * |
|
455 | + * @param WPInv_Invoice $invoice Invoice object. |
|
456 | 456 | */ |
457 | 457 | public function add_items( &$invoice ) { |
458 | - global $wpdb; |
|
458 | + global $wpdb; |
|
459 | 459 | |
460 | - // Maybe retrieve from the cache. |
|
461 | - $items = wp_cache_get( $invoice->get_id(), 'getpaid_invoice_cart_details' ); |
|
460 | + // Maybe retrieve from the cache. |
|
461 | + $items = wp_cache_get( $invoice->get_id(), 'getpaid_invoice_cart_details' ); |
|
462 | 462 | |
463 | - // If not found, retrieve from the db. |
|
464 | - if ( false === $items ) { |
|
465 | - $table = $wpdb->prefix . 'getpaid_invoice_items'; |
|
463 | + // If not found, retrieve from the db. |
|
464 | + if ( false === $items ) { |
|
465 | + $table = $wpdb->prefix . 'getpaid_invoice_items'; |
|
466 | 466 | |
467 | - $items = $wpdb->get_results( |
|
468 | - $wpdb->prepare( "SELECT * FROM $table WHERE `post_id`=%d", $invoice->get_id() ) |
|
469 | - ); |
|
467 | + $items = $wpdb->get_results( |
|
468 | + $wpdb->prepare( "SELECT * FROM $table WHERE `post_id`=%d", $invoice->get_id() ) |
|
469 | + ); |
|
470 | 470 | |
471 | - // Update the cache with our data |
|
472 | - wp_cache_set( $invoice->get_id(), $items, 'getpaid_invoice_cart_details' ); |
|
471 | + // Update the cache with our data |
|
472 | + wp_cache_set( $invoice->get_id(), $items, 'getpaid_invoice_cart_details' ); |
|
473 | 473 | |
474 | - } |
|
474 | + } |
|
475 | 475 | |
476 | - // Abort if no items found. |
|
476 | + // Abort if no items found. |
|
477 | 477 | if ( empty( $items ) ) { |
478 | 478 | return; |
479 | - } |
|
480 | - |
|
481 | - $_items = array(); |
|
482 | - foreach ( $items as $item_data ) { |
|
483 | - $item = new GetPaid_Form_Item( $item_data->item_id ); |
|
484 | - |
|
485 | - // Set item data. |
|
486 | - $item->item_tax = wpinv_sanitize_amount( $item_data->tax ); |
|
487 | - $item->item_discount = wpinv_sanitize_amount( $item_data->discount ); |
|
488 | - $item->set_name( $item_data->item_name ); |
|
489 | - $item->set_description( $item_data->item_description ); |
|
490 | - $item->set_price( $item_data->item_price ); |
|
491 | - $item->set_quantity( $item_data->quantity ); |
|
492 | - $item->set_item_meta( $item_data->meta ); |
|
493 | - $_items[] = $item; |
|
494 | - } |
|
495 | - |
|
496 | - $invoice->set_items( $_items ); |
|
497 | - } |
|
498 | - |
|
499 | - /** |
|
479 | + } |
|
480 | + |
|
481 | + $_items = array(); |
|
482 | + foreach ( $items as $item_data ) { |
|
483 | + $item = new GetPaid_Form_Item( $item_data->item_id ); |
|
484 | + |
|
485 | + // Set item data. |
|
486 | + $item->item_tax = wpinv_sanitize_amount( $item_data->tax ); |
|
487 | + $item->item_discount = wpinv_sanitize_amount( $item_data->discount ); |
|
488 | + $item->set_name( $item_data->item_name ); |
|
489 | + $item->set_description( $item_data->item_description ); |
|
490 | + $item->set_price( $item_data->item_price ); |
|
491 | + $item->set_quantity( $item_data->quantity ); |
|
492 | + $item->set_item_meta( $item_data->meta ); |
|
493 | + $_items[] = $item; |
|
494 | + } |
|
495 | + |
|
496 | + $invoice->set_items( $_items ); |
|
497 | + } |
|
498 | + |
|
499 | + /** |
|
500 | 500 | * Saves cart details. |
501 | - * |
|
502 | - * @param WPInv_Invoice $invoice Invoice object. |
|
501 | + * |
|
502 | + * @param WPInv_Invoice $invoice Invoice object. |
|
503 | 503 | */ |
504 | 504 | public function save_items( $invoice ) { |
505 | 505 | |
506 | - // Delete previously existing items. |
|
507 | - $this->delete_items( $invoice ); |
|
506 | + // Delete previously existing items. |
|
507 | + $this->delete_items( $invoice ); |
|
508 | 508 | |
509 | - $table = $GLOBALS['wpdb']->prefix . 'getpaid_invoice_items'; |
|
509 | + $table = $GLOBALS['wpdb']->prefix . 'getpaid_invoice_items'; |
|
510 | 510 | |
511 | - foreach ( $invoice->get_cart_details() as $item_data ) { |
|
512 | - $item_data = array_map( 'maybe_serialize', $item_data ); |
|
513 | - $GLOBALS['wpdb']->insert( $table, $item_data ); |
|
514 | - } |
|
511 | + foreach ( $invoice->get_cart_details() as $item_data ) { |
|
512 | + $item_data = array_map( 'maybe_serialize', $item_data ); |
|
513 | + $GLOBALS['wpdb']->insert( $table, $item_data ); |
|
514 | + } |
|
515 | 515 | |
516 | - wp_cache_delete( $invoice->get_id(), 'getpaid_invoice_cart_details' ); |
|
517 | - do_action( 'getpaid_invoice_save_items', $invoice ); |
|
516 | + wp_cache_delete( $invoice->get_id(), 'getpaid_invoice_cart_details' ); |
|
517 | + do_action( 'getpaid_invoice_save_items', $invoice ); |
|
518 | 518 | |
519 | - } |
|
519 | + } |
|
520 | 520 | |
521 | - /** |
|
521 | + /** |
|
522 | 522 | * Deletes an invoice's cart details from the database. |
523 | - * |
|
524 | - * @param WPInv_Invoice $invoice Invoice object. |
|
523 | + * |
|
524 | + * @param WPInv_Invoice $invoice Invoice object. |
|
525 | 525 | */ |
526 | 526 | public function delete_items( $invoice ) { |
527 | - $table = $GLOBALS['wpdb']->prefix . 'getpaid_invoice_items'; |
|
528 | - return $GLOBALS['wpdb']->delete( $table, array( 'post_id' => $invoice->get_id() ) ); |
|
529 | - } |
|
527 | + $table = $GLOBALS['wpdb']->prefix . 'getpaid_invoice_items'; |
|
528 | + return $GLOBALS['wpdb']->delete( $table, array( 'post_id' => $invoice->get_id() ) ); |
|
529 | + } |
|
530 | 530 | |
531 | - /** |
|
531 | + /** |
|
532 | 532 | * Deletes an invoice's special fields from the database. |
533 | - * |
|
534 | - * @param WPInv_Invoice $invoice Invoice object. |
|
533 | + * |
|
534 | + * @param WPInv_Invoice $invoice Invoice object. |
|
535 | 535 | */ |
536 | 536 | public function delete_special_fields( $invoice ) { |
537 | - $table = $GLOBALS['wpdb']->prefix . 'getpaid_invoices'; |
|
538 | - return $GLOBALS['wpdb']->delete( $table, array( 'post_id' => $invoice->get_id() ) ); |
|
539 | - } |
|
540 | - |
|
541 | - /** |
|
542 | - * Get the status to save to the post object. |
|
543 | - * |
|
544 | - * |
|
545 | - * @since 1.0.19 |
|
546 | - * @param WPInv_Invoice $object GetPaid_Data object. |
|
547 | - * @return string |
|
548 | - */ |
|
549 | - protected function get_post_status( $object ) { |
|
550 | - $object_status = $object->get_status( 'edit' ); |
|
551 | - |
|
552 | - if ( ! $object_status ) { |
|
553 | - $object_status = $object->get_default_status(); |
|
554 | - } |
|
555 | - |
|
556 | - return $object_status; |
|
557 | - } |
|
537 | + $table = $GLOBALS['wpdb']->prefix . 'getpaid_invoices'; |
|
538 | + return $GLOBALS['wpdb']->delete( $table, array( 'post_id' => $invoice->get_id() ) ); |
|
539 | + } |
|
540 | + |
|
541 | + /** |
|
542 | + * Get the status to save to the post object. |
|
543 | + * |
|
544 | + * |
|
545 | + * @since 1.0.19 |
|
546 | + * @param WPInv_Invoice $object GetPaid_Data object. |
|
547 | + * @return string |
|
548 | + */ |
|
549 | + protected function get_post_status( $object ) { |
|
550 | + $object_status = $object->get_status( 'edit' ); |
|
551 | + |
|
552 | + if ( ! $object_status ) { |
|
553 | + $object_status = $object->get_default_status(); |
|
554 | + } |
|
555 | + |
|
556 | + return $object_status; |
|
557 | + } |
|
558 | 558 | |
559 | 559 | } |
@@ -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. |
@@ -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 | } |