@@ -4,7 +4,7 @@ discard block |
||
| 4 | 4 | * |
| 5 | 5 | */ |
| 6 | 6 | if ( ! defined( 'ABSPATH' ) ) { |
| 7 | - exit; |
|
| 7 | + exit; |
|
| 8 | 8 | } |
| 9 | 9 | |
| 10 | 10 | /** |
@@ -14,228 +14,228 @@ discard block |
||
| 14 | 14 | */ |
| 15 | 15 | class GetPaid_Item_Data_Store extends GetPaid_Data_Store_WP { |
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * Data stored in meta keys, but not considered "meta" for an item. |
|
| 19 | - * |
|
| 20 | - * @since 1.0.19 |
|
| 21 | - * @var array |
|
| 22 | - */ |
|
| 23 | - protected $internal_meta_keys = array( |
|
| 24 | - '_wpinv_price', |
|
| 25 | - '_wpinv_vat_rule', |
|
| 26 | - '_wpinv_vat_class', |
|
| 27 | - '_wpinv_type', |
|
| 28 | - '_wpinv_custom_id', |
|
| 29 | - '_wpinv_custom_name', |
|
| 30 | - '_wpinv_custom_singular_name', |
|
| 31 | - '_wpinv_editable', |
|
| 32 | - '_wpinv_dynamic_pricing', |
|
| 33 | - '_minimum_price', |
|
| 34 | - '_wpinv_is_recurring', |
|
| 35 | - '_wpinv_recurring_period', |
|
| 36 | - '_wpinv_recurring_interval', |
|
| 37 | - '_wpinv_recurring_limit', |
|
| 38 | - '_wpinv_free_trial', |
|
| 39 | - '_wpinv_trial_period', |
|
| 40 | - '_wpinv_trial_interval', |
|
| 41 | - ); |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * A map of meta keys to data props. |
|
| 45 | - * |
|
| 46 | - * @since 1.0.19 |
|
| 47 | - * |
|
| 48 | - * @var array |
|
| 49 | - */ |
|
| 50 | - protected $meta_key_to_props = array( |
|
| 51 | - '_wpinv_price' => 'price', |
|
| 52 | - '_wpinv_vat_rule' => 'vat_rule', |
|
| 53 | - '_wpinv_vat_class' => 'vat_class', |
|
| 54 | - '_wpinv_type' => 'type', |
|
| 55 | - '_wpinv_custom_id' => 'custom_id', |
|
| 56 | - '_wpinv_custom_name' => 'custom_name', |
|
| 57 | - '_wpinv_custom_singular_name' => 'custom_singular_name', |
|
| 58 | - '_wpinv_editable' => 'is_editable', |
|
| 59 | - '_wpinv_dynamic_pricing' => 'is_dynamic_pricing', |
|
| 60 | - '_minimum_price' => 'minimum_price', |
|
| 61 | - '_wpinv_custom_name' => 'custom_name', |
|
| 62 | - '_wpinv_is_recurring' => 'is_recurring', |
|
| 63 | - '_wpinv_recurring_period' => 'recurring_period', |
|
| 64 | - '_wpinv_recurring_interval' => 'recurring_interval', |
|
| 65 | - '_wpinv_recurring_limit' => 'recurring_limit', |
|
| 66 | - '_wpinv_free_trial' => 'is_free_trial', |
|
| 67 | - '_wpinv_trial_period' => 'trial_period', |
|
| 68 | - '_wpinv_trial_interval' => 'trial_interval', |
|
| 69 | - '_wpinv_version' => 'version', |
|
| 70 | - ); |
|
| 71 | - |
|
| 72 | - /* |
|
| 17 | + /** |
|
| 18 | + * Data stored in meta keys, but not considered "meta" for an item. |
|
| 19 | + * |
|
| 20 | + * @since 1.0.19 |
|
| 21 | + * @var array |
|
| 22 | + */ |
|
| 23 | + protected $internal_meta_keys = array( |
|
| 24 | + '_wpinv_price', |
|
| 25 | + '_wpinv_vat_rule', |
|
| 26 | + '_wpinv_vat_class', |
|
| 27 | + '_wpinv_type', |
|
| 28 | + '_wpinv_custom_id', |
|
| 29 | + '_wpinv_custom_name', |
|
| 30 | + '_wpinv_custom_singular_name', |
|
| 31 | + '_wpinv_editable', |
|
| 32 | + '_wpinv_dynamic_pricing', |
|
| 33 | + '_minimum_price', |
|
| 34 | + '_wpinv_is_recurring', |
|
| 35 | + '_wpinv_recurring_period', |
|
| 36 | + '_wpinv_recurring_interval', |
|
| 37 | + '_wpinv_recurring_limit', |
|
| 38 | + '_wpinv_free_trial', |
|
| 39 | + '_wpinv_trial_period', |
|
| 40 | + '_wpinv_trial_interval', |
|
| 41 | + ); |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * A map of meta keys to data props. |
|
| 45 | + * |
|
| 46 | + * @since 1.0.19 |
|
| 47 | + * |
|
| 48 | + * @var array |
|
| 49 | + */ |
|
| 50 | + protected $meta_key_to_props = array( |
|
| 51 | + '_wpinv_price' => 'price', |
|
| 52 | + '_wpinv_vat_rule' => 'vat_rule', |
|
| 53 | + '_wpinv_vat_class' => 'vat_class', |
|
| 54 | + '_wpinv_type' => 'type', |
|
| 55 | + '_wpinv_custom_id' => 'custom_id', |
|
| 56 | + '_wpinv_custom_name' => 'custom_name', |
|
| 57 | + '_wpinv_custom_singular_name' => 'custom_singular_name', |
|
| 58 | + '_wpinv_editable' => 'is_editable', |
|
| 59 | + '_wpinv_dynamic_pricing' => 'is_dynamic_pricing', |
|
| 60 | + '_minimum_price' => 'minimum_price', |
|
| 61 | + '_wpinv_custom_name' => 'custom_name', |
|
| 62 | + '_wpinv_is_recurring' => 'is_recurring', |
|
| 63 | + '_wpinv_recurring_period' => 'recurring_period', |
|
| 64 | + '_wpinv_recurring_interval' => 'recurring_interval', |
|
| 65 | + '_wpinv_recurring_limit' => 'recurring_limit', |
|
| 66 | + '_wpinv_free_trial' => 'is_free_trial', |
|
| 67 | + '_wpinv_trial_period' => 'trial_period', |
|
| 68 | + '_wpinv_trial_interval' => 'trial_interval', |
|
| 69 | + '_wpinv_version' => 'version', |
|
| 70 | + ); |
|
| 71 | + |
|
| 72 | + /* |
|
| 73 | 73 | |-------------------------------------------------------------------------- |
| 74 | 74 | | CRUD Methods |
| 75 | 75 | |-------------------------------------------------------------------------- |
| 76 | 76 | */ |
| 77 | 77 | |
| 78 | - /** |
|
| 79 | - * Method to create a new item in the database. |
|
| 80 | - * |
|
| 81 | - * @param WPInv_Item $item Item object. |
|
| 82 | - */ |
|
| 83 | - public function create( &$item ) { |
|
| 84 | - $item->set_version( WPINV_VERSION ); |
|
| 85 | - $item->set_date_created( current_time( 'mysql' ) ); |
|
| 86 | - |
|
| 87 | - // Create a new post. |
|
| 88 | - $id = wp_insert_post( |
|
| 89 | - apply_filters( |
|
| 90 | - 'getpaid_new_item_data', |
|
| 91 | - array( |
|
| 92 | - 'post_date' => $item->get_date_created( 'edit' ), |
|
| 93 | - 'post_type' => 'wpi_item', |
|
| 94 | - 'post_status' => $this->get_post_status( $item ), |
|
| 95 | - 'ping_status' => 'closed', |
|
| 96 | - 'post_author' => $item->get_author( 'edit' ), |
|
| 97 | - 'post_title' => $item->get_name( 'edit' ), |
|
| 98 | - 'post_parent' => $item->get_parent_id(), |
|
| 99 | - 'post_excerpt' => $item->get_description( 'edit' ), |
|
| 100 | - ) |
|
| 101 | - ), |
|
| 102 | - true |
|
| 103 | - ); |
|
| 104 | - |
|
| 105 | - if ( $id && ! is_wp_error( $id ) ) { |
|
| 106 | - $item->set_id( $id ); |
|
| 107 | - $this->update_post_meta( $item ); |
|
| 108 | - $item->save_meta_data(); |
|
| 109 | - $item->apply_changes(); |
|
| 110 | - $this->clear_caches( $item ); |
|
| 111 | - do_action( 'getpaid_new_item', $item ); |
|
| 112 | - return true; |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - if ( is_wp_error( $id ) ) { |
|
| 116 | - $item->last_error = $id->get_error_message(); |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - return false; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * Method to read an item from the database. |
|
| 124 | - * |
|
| 125 | - * @param WPInv_Item $item Item object. |
|
| 126 | - * |
|
| 127 | - */ |
|
| 128 | - public function read( &$item ) { |
|
| 129 | - |
|
| 130 | - $item->set_defaults(); |
|
| 131 | - $item_object = get_post( $item->get_id() ); |
|
| 132 | - |
|
| 133 | - if ( ! $item->get_id() || ! $item_object || $item_object->post_type != 'wpi_item' ) { |
|
| 134 | - $item->last_error = __( 'Invalid item.', 'invoicing' ); |
|
| 135 | - $item->set_id( 0 ); |
|
| 136 | - return false; |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - $item->set_props( |
|
| 140 | - array( |
|
| 141 | - 'parent_id' => $item_object->post_parent, |
|
| 142 | - 'date_created' => 0 < $item_object->post_date ? $item_object->post_date : null, |
|
| 143 | - 'date_modified' => 0 < $item_object->post_modified ? $item_object->post_modified : null, |
|
| 144 | - 'status' => $item_object->post_status, |
|
| 145 | - 'name' => $item_object->post_title, |
|
| 146 | - 'description' => $item_object->post_excerpt, |
|
| 147 | - 'author' => $item_object->post_author, |
|
| 148 | - ) |
|
| 149 | - ); |
|
| 150 | - |
|
| 151 | - $this->read_object_data( $item, $item_object ); |
|
| 152 | - $item->read_meta_data(); |
|
| 153 | - $item->set_object_read( true ); |
|
| 154 | - do_action( 'getpaid_read_item', $item ); |
|
| 155 | - |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * Method to update an item in the database. |
|
| 160 | - * |
|
| 161 | - * @param WPInv_Item $item Item object. |
|
| 162 | - */ |
|
| 163 | - public function update( &$item ) { |
|
| 164 | - $item->save_meta_data(); |
|
| 165 | - $item->set_version( WPINV_VERSION ); |
|
| 166 | - |
|
| 167 | - if ( null === $item->get_date_created( 'edit' ) ) { |
|
| 168 | - $item->set_date_created( current_time( 'mysql' ) ); |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - // Grab the current status so we can compare. |
|
| 172 | - $previous_status = get_post_status( $item->get_id() ); |
|
| 173 | - |
|
| 174 | - $changes = $item->get_changes(); |
|
| 175 | - |
|
| 176 | - // Only update the post when the post data changes. |
|
| 177 | - if ( array_intersect( array( 'date_created', 'date_modified', 'status', 'parent_id', 'description', 'name', 'author' ), array_keys( $changes ) ) ) { |
|
| 178 | - $post_data = array( |
|
| 179 | - 'post_date' => $item->get_date_created( 'edit' ), |
|
| 180 | - 'post_status' => $item->get_status( 'edit' ), |
|
| 181 | - 'post_parent' => $item->get_parent_id( 'edit' ), |
|
| 182 | - 'post_excerpt' => $item->get_description( 'edit' ), |
|
| 183 | - 'post_modified' => $item->get_date_modified( 'edit' ), |
|
| 184 | - 'post_title' => $item->get_name( 'edit' ), |
|
| 185 | - 'post_author' => $item->get_author( 'edit' ), |
|
| 186 | - ); |
|
| 187 | - |
|
| 188 | - /** |
|
| 189 | - * When updating this object, to prevent infinite loops, use $wpdb |
|
| 190 | - * to update data, since wp_update_post spawns more calls to the |
|
| 191 | - * save_post action. |
|
| 192 | - * |
|
| 193 | - * This ensures hooks are fired by either WP itself (admin screen save), |
|
| 194 | - * or an update purely from CRUD. |
|
| 195 | - */ |
|
| 196 | - if ( doing_action( 'save_post' ) ) { |
|
| 197 | - $GLOBALS['wpdb']->update( $GLOBALS['wpdb']->posts, $post_data, array( 'ID' => $item->get_id() ) ); |
|
| 198 | - clean_post_cache( $item->get_id() ); |
|
| 199 | - } else { |
|
| 200 | - wp_update_post( array_merge( array( 'ID' => $item->get_id() ), $post_data ) ); |
|
| 201 | - } |
|
| 202 | - $item->read_meta_data( true ); // Refresh internal meta data, in case things were hooked into `save_post` or another WP hook. |
|
| 203 | - } |
|
| 204 | - $this->update_post_meta( $item ); |
|
| 205 | - $item->apply_changes(); |
|
| 206 | - $this->clear_caches( $item ); |
|
| 207 | - |
|
| 208 | - // Fire a hook depending on the status - this should be considered a creation if it was previously draft status. |
|
| 209 | - $new_status = $item->get_status( 'edit' ); |
|
| 210 | - |
|
| 211 | - if ( $new_status !== $previous_status && in_array( $previous_status, array( 'new', 'auto-draft', 'draft' ), true ) ) { |
|
| 212 | - do_action( 'getpaid_new_item', $item ); |
|
| 213 | - } else { |
|
| 214 | - do_action( 'getpaid_update_item', $item ); |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - /* |
|
| 78 | + /** |
|
| 79 | + * Method to create a new item in the database. |
|
| 80 | + * |
|
| 81 | + * @param WPInv_Item $item Item object. |
|
| 82 | + */ |
|
| 83 | + public function create( &$item ) { |
|
| 84 | + $item->set_version( WPINV_VERSION ); |
|
| 85 | + $item->set_date_created( current_time( 'mysql' ) ); |
|
| 86 | + |
|
| 87 | + // Create a new post. |
|
| 88 | + $id = wp_insert_post( |
|
| 89 | + apply_filters( |
|
| 90 | + 'getpaid_new_item_data', |
|
| 91 | + array( |
|
| 92 | + 'post_date' => $item->get_date_created( 'edit' ), |
|
| 93 | + 'post_type' => 'wpi_item', |
|
| 94 | + 'post_status' => $this->get_post_status( $item ), |
|
| 95 | + 'ping_status' => 'closed', |
|
| 96 | + 'post_author' => $item->get_author( 'edit' ), |
|
| 97 | + 'post_title' => $item->get_name( 'edit' ), |
|
| 98 | + 'post_parent' => $item->get_parent_id(), |
|
| 99 | + 'post_excerpt' => $item->get_description( 'edit' ), |
|
| 100 | + ) |
|
| 101 | + ), |
|
| 102 | + true |
|
| 103 | + ); |
|
| 104 | + |
|
| 105 | + if ( $id && ! is_wp_error( $id ) ) { |
|
| 106 | + $item->set_id( $id ); |
|
| 107 | + $this->update_post_meta( $item ); |
|
| 108 | + $item->save_meta_data(); |
|
| 109 | + $item->apply_changes(); |
|
| 110 | + $this->clear_caches( $item ); |
|
| 111 | + do_action( 'getpaid_new_item', $item ); |
|
| 112 | + return true; |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + if ( is_wp_error( $id ) ) { |
|
| 116 | + $item->last_error = $id->get_error_message(); |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + return false; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * Method to read an item from the database. |
|
| 124 | + * |
|
| 125 | + * @param WPInv_Item $item Item object. |
|
| 126 | + * |
|
| 127 | + */ |
|
| 128 | + public function read( &$item ) { |
|
| 129 | + |
|
| 130 | + $item->set_defaults(); |
|
| 131 | + $item_object = get_post( $item->get_id() ); |
|
| 132 | + |
|
| 133 | + if ( ! $item->get_id() || ! $item_object || $item_object->post_type != 'wpi_item' ) { |
|
| 134 | + $item->last_error = __( 'Invalid item.', 'invoicing' ); |
|
| 135 | + $item->set_id( 0 ); |
|
| 136 | + return false; |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + $item->set_props( |
|
| 140 | + array( |
|
| 141 | + 'parent_id' => $item_object->post_parent, |
|
| 142 | + 'date_created' => 0 < $item_object->post_date ? $item_object->post_date : null, |
|
| 143 | + 'date_modified' => 0 < $item_object->post_modified ? $item_object->post_modified : null, |
|
| 144 | + 'status' => $item_object->post_status, |
|
| 145 | + 'name' => $item_object->post_title, |
|
| 146 | + 'description' => $item_object->post_excerpt, |
|
| 147 | + 'author' => $item_object->post_author, |
|
| 148 | + ) |
|
| 149 | + ); |
|
| 150 | + |
|
| 151 | + $this->read_object_data( $item, $item_object ); |
|
| 152 | + $item->read_meta_data(); |
|
| 153 | + $item->set_object_read( true ); |
|
| 154 | + do_action( 'getpaid_read_item', $item ); |
|
| 155 | + |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * Method to update an item in the database. |
|
| 160 | + * |
|
| 161 | + * @param WPInv_Item $item Item object. |
|
| 162 | + */ |
|
| 163 | + public function update( &$item ) { |
|
| 164 | + $item->save_meta_data(); |
|
| 165 | + $item->set_version( WPINV_VERSION ); |
|
| 166 | + |
|
| 167 | + if ( null === $item->get_date_created( 'edit' ) ) { |
|
| 168 | + $item->set_date_created( current_time( 'mysql' ) ); |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + // Grab the current status so we can compare. |
|
| 172 | + $previous_status = get_post_status( $item->get_id() ); |
|
| 173 | + |
|
| 174 | + $changes = $item->get_changes(); |
|
| 175 | + |
|
| 176 | + // Only update the post when the post data changes. |
|
| 177 | + if ( array_intersect( array( 'date_created', 'date_modified', 'status', 'parent_id', 'description', 'name', 'author' ), array_keys( $changes ) ) ) { |
|
| 178 | + $post_data = array( |
|
| 179 | + 'post_date' => $item->get_date_created( 'edit' ), |
|
| 180 | + 'post_status' => $item->get_status( 'edit' ), |
|
| 181 | + 'post_parent' => $item->get_parent_id( 'edit' ), |
|
| 182 | + 'post_excerpt' => $item->get_description( 'edit' ), |
|
| 183 | + 'post_modified' => $item->get_date_modified( 'edit' ), |
|
| 184 | + 'post_title' => $item->get_name( 'edit' ), |
|
| 185 | + 'post_author' => $item->get_author( 'edit' ), |
|
| 186 | + ); |
|
| 187 | + |
|
| 188 | + /** |
|
| 189 | + * When updating this object, to prevent infinite loops, use $wpdb |
|
| 190 | + * to update data, since wp_update_post spawns more calls to the |
|
| 191 | + * save_post action. |
|
| 192 | + * |
|
| 193 | + * This ensures hooks are fired by either WP itself (admin screen save), |
|
| 194 | + * or an update purely from CRUD. |
|
| 195 | + */ |
|
| 196 | + if ( doing_action( 'save_post' ) ) { |
|
| 197 | + $GLOBALS['wpdb']->update( $GLOBALS['wpdb']->posts, $post_data, array( 'ID' => $item->get_id() ) ); |
|
| 198 | + clean_post_cache( $item->get_id() ); |
|
| 199 | + } else { |
|
| 200 | + wp_update_post( array_merge( array( 'ID' => $item->get_id() ), $post_data ) ); |
|
| 201 | + } |
|
| 202 | + $item->read_meta_data( true ); // Refresh internal meta data, in case things were hooked into `save_post` or another WP hook. |
|
| 203 | + } |
|
| 204 | + $this->update_post_meta( $item ); |
|
| 205 | + $item->apply_changes(); |
|
| 206 | + $this->clear_caches( $item ); |
|
| 207 | + |
|
| 208 | + // Fire a hook depending on the status - this should be considered a creation if it was previously draft status. |
|
| 209 | + $new_status = $item->get_status( 'edit' ); |
|
| 210 | + |
|
| 211 | + if ( $new_status !== $previous_status && in_array( $previous_status, array( 'new', 'auto-draft', 'draft' ), true ) ) { |
|
| 212 | + do_action( 'getpaid_new_item', $item ); |
|
| 213 | + } else { |
|
| 214 | + do_action( 'getpaid_update_item', $item ); |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + /* |
|
| 220 | 220 | |-------------------------------------------------------------------------- |
| 221 | 221 | | Additional Methods |
| 222 | 222 | |-------------------------------------------------------------------------- |
| 223 | 223 | */ |
| 224 | 224 | |
| 225 | - /** |
|
| 226 | - * Helper method that updates all the post meta for an item based on it's settings in the WPInv_Item class. |
|
| 227 | - * |
|
| 228 | - * @param WPInv_Item $item WPInv_Item object. |
|
| 229 | - * @since 1.0.19 |
|
| 230 | - */ |
|
| 231 | - protected function update_post_meta( &$item ) { |
|
| 225 | + /** |
|
| 226 | + * Helper method that updates all the post meta for an item based on it's settings in the WPInv_Item class. |
|
| 227 | + * |
|
| 228 | + * @param WPInv_Item $item WPInv_Item object. |
|
| 229 | + * @since 1.0.19 |
|
| 230 | + */ |
|
| 231 | + protected function update_post_meta( &$item ) { |
|
| 232 | 232 | |
| 233 | - // Ensure that we have a custom id. |
|
| 233 | + // Ensure that we have a custom id. |
|
| 234 | 234 | if ( ! $item->get_custom_id() ) { |
| 235 | 235 | $item->set_custom_id( $item->get_id() ); |
| 236 | - } |
|
| 236 | + } |
|
| 237 | 237 | |
| 238 | - parent::update_post_meta( $item ); |
|
| 239 | - } |
|
| 238 | + parent::update_post_meta( $item ); |
|
| 239 | + } |
|
| 240 | 240 | |
| 241 | 241 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | - exit; |
|
| 3 | + exit; |
|
| 4 | 4 | } |
| 5 | 5 | |
| 6 | 6 | /** |
@@ -10,199 +10,199 @@ discard block |
||
| 10 | 10 | class GetPaid_Payment_Form_Submission { |
| 11 | 11 | |
| 12 | 12 | /** |
| 13 | - * Submission ID |
|
| 14 | - * |
|
| 15 | - * @var string |
|
| 16 | - */ |
|
| 17 | - public $id = null; |
|
| 18 | - |
|
| 19 | - /** |
|
| 20 | - * The raw submission data. |
|
| 21 | - * |
|
| 22 | - * @var array |
|
| 23 | - */ |
|
| 24 | - protected $data = null; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * Submission totals |
|
| 28 | - * |
|
| 29 | - * @var array |
|
| 30 | - */ |
|
| 31 | - protected $totals = array( |
|
| 32 | - |
|
| 33 | - 'subtotal' => array( |
|
| 34 | - 'initial' => 0, |
|
| 35 | - 'recurring' => 0, |
|
| 36 | - ), |
|
| 37 | - |
|
| 38 | - 'discount' => array( |
|
| 39 | - 'initial' => 0, |
|
| 40 | - 'recurring' => 0, |
|
| 41 | - ), |
|
| 42 | - |
|
| 43 | - 'fees' => array( |
|
| 44 | - 'initial' => 0, |
|
| 45 | - 'recurring' => 0, |
|
| 46 | - ), |
|
| 47 | - |
|
| 48 | - 'taxes' => array( |
|
| 49 | - 'initial' => 0, |
|
| 50 | - 'recurring' => 0, |
|
| 51 | - ), |
|
| 52 | - |
|
| 53 | - 'shipping' => array( |
|
| 54 | - 'initial' => 0, |
|
| 55 | - 'recurring' => 0, |
|
| 56 | - ), |
|
| 57 | - |
|
| 58 | - ); |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * Sets the associated payment form. |
|
| 62 | - * |
|
| 63 | - * @var GetPaid_Payment_Form |
|
| 64 | - */ |
|
| 13 | + * Submission ID |
|
| 14 | + * |
|
| 15 | + * @var string |
|
| 16 | + */ |
|
| 17 | + public $id = null; |
|
| 18 | + |
|
| 19 | + /** |
|
| 20 | + * The raw submission data. |
|
| 21 | + * |
|
| 22 | + * @var array |
|
| 23 | + */ |
|
| 24 | + protected $data = null; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * Submission totals |
|
| 28 | + * |
|
| 29 | + * @var array |
|
| 30 | + */ |
|
| 31 | + protected $totals = array( |
|
| 32 | + |
|
| 33 | + 'subtotal' => array( |
|
| 34 | + 'initial' => 0, |
|
| 35 | + 'recurring' => 0, |
|
| 36 | + ), |
|
| 37 | + |
|
| 38 | + 'discount' => array( |
|
| 39 | + 'initial' => 0, |
|
| 40 | + 'recurring' => 0, |
|
| 41 | + ), |
|
| 42 | + |
|
| 43 | + 'fees' => array( |
|
| 44 | + 'initial' => 0, |
|
| 45 | + 'recurring' => 0, |
|
| 46 | + ), |
|
| 47 | + |
|
| 48 | + 'taxes' => array( |
|
| 49 | + 'initial' => 0, |
|
| 50 | + 'recurring' => 0, |
|
| 51 | + ), |
|
| 52 | + |
|
| 53 | + 'shipping' => array( |
|
| 54 | + 'initial' => 0, |
|
| 55 | + 'recurring' => 0, |
|
| 56 | + ), |
|
| 57 | + |
|
| 58 | + ); |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * Sets the associated payment form. |
|
| 62 | + * |
|
| 63 | + * @var GetPaid_Payment_Form |
|
| 64 | + */ |
|
| 65 | 65 | protected $payment_form = null; |
| 66 | 66 | |
| 67 | 67 | /** |
| 68 | - * The country for the submission. |
|
| 69 | - * |
|
| 70 | - * @var string |
|
| 71 | - */ |
|
| 72 | - public $country = null; |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * The state for the submission. |
|
| 76 | - * |
|
| 77 | - * @since 1.0.19 |
|
| 78 | - * @var string |
|
| 79 | - */ |
|
| 80 | - public $state = null; |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * The invoice associated with the submission. |
|
| 84 | - * |
|
| 85 | - * @var WPInv_Invoice |
|
| 86 | - */ |
|
| 87 | - protected $invoice = null; |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * The recurring item for the submission. |
|
| 91 | - * |
|
| 92 | - * @var int |
|
| 93 | - */ |
|
| 94 | - public $has_recurring = 0; |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * An array of fees for the submission. |
|
| 98 | - * |
|
| 99 | - * @var array |
|
| 100 | - */ |
|
| 101 | - protected $fees = array(); |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * An array of discounts for the submission. |
|
| 105 | - * |
|
| 106 | - * @var array |
|
| 107 | - */ |
|
| 108 | - protected $discounts = array(); |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * An array of taxes for the submission. |
|
| 112 | - * |
|
| 113 | - * @var array |
|
| 114 | - */ |
|
| 115 | - protected $taxes = array(); |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * An array of items for the submission. |
|
| 119 | - * |
|
| 120 | - * @var GetPaid_Form_Item[] |
|
| 121 | - */ |
|
| 122 | - protected $items = array(); |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * The last error. |
|
| 126 | - * |
|
| 127 | - * @var string |
|
| 128 | - */ |
|
| 129 | - public $last_error = null; |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * The last error code. |
|
| 133 | - * |
|
| 134 | - * @var string |
|
| 135 | - */ |
|
| 136 | - public $last_error_code = null; |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * Class constructor. |
|
| 140 | - * |
|
| 141 | - */ |
|
| 142 | - public function __construct() { |
|
| 143 | - |
|
| 144 | - // Set the state and country to the default state and country. |
|
| 145 | - $this->country = wpinv_default_billing_country(); |
|
| 146 | - $this->state = wpinv_get_default_state(); |
|
| 147 | - |
|
| 148 | - // Do we have an actual submission? |
|
| 149 | - if ( isset( $_POST['getpaid_payment_form_submission'] ) ) { |
|
| 150 | - $this->load_data( wp_kses_post_deep( wp_unslash( $_POST ) ) ); |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * Loads submission data. |
|
| 157 | - * |
|
| 158 | - * @param array $data |
|
| 159 | - */ |
|
| 160 | - public function load_data( $data ) { |
|
| 161 | - |
|
| 162 | - // Allow plugins to filter the data. |
|
| 163 | - $data = apply_filters( 'getpaid_submission_data', $data, $this ); |
|
| 164 | - |
|
| 165 | - // Cache it... |
|
| 166 | - $this->data = $data; |
|
| 167 | - |
|
| 168 | - // Then generate a unique id from the data. |
|
| 169 | - $this->id = md5( wp_json_encode( $data ) ); |
|
| 170 | - |
|
| 171 | - // Finally, process the submission. |
|
| 172 | - try { |
|
| 173 | - |
|
| 174 | - // Each process is passed an instance of the class (with reference) |
|
| 175 | - // and should throw an Exception whenever it encounters one. |
|
| 176 | - $processors = apply_filters( |
|
| 177 | - 'getpaid_payment_form_submission_processors', |
|
| 178 | - array( |
|
| 179 | - array( $this, 'process_payment_form' ), |
|
| 180 | - array( $this, 'process_invoice' ), |
|
| 181 | - array( $this, 'process_fees' ), |
|
| 182 | - array( $this, 'process_items' ), |
|
| 183 | - array( $this, 'process_discount' ), |
|
| 184 | - array( $this, 'process_taxes' ), |
|
| 185 | - ), |
|
| 186 | - $this |
|
| 187 | - ); |
|
| 188 | - |
|
| 189 | - foreach ( $processors as $processor ) { |
|
| 190 | - call_user_func_array( $processor, array( &$this ) ); |
|
| 191 | - } |
|
| 192 | - } catch ( GetPaid_Payment_Exception $e ) { |
|
| 193 | - $this->last_error = $e->getMessage(); |
|
| 194 | - $this->last_error_code = $e->getErrorCode(); |
|
| 195 | - } catch ( Exception $e ) { |
|
| 196 | - $this->last_error = $e->getMessage(); |
|
| 197 | - $this->last_error_code = $e->getCode(); |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - // Fired when we are done processing a submission. |
|
| 201 | - do_action_ref_array( 'getpaid_process_submission', array( &$this ) ); |
|
| 202 | - |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - /* |
|
| 68 | + * The country for the submission. |
|
| 69 | + * |
|
| 70 | + * @var string |
|
| 71 | + */ |
|
| 72 | + public $country = null; |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * The state for the submission. |
|
| 76 | + * |
|
| 77 | + * @since 1.0.19 |
|
| 78 | + * @var string |
|
| 79 | + */ |
|
| 80 | + public $state = null; |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * The invoice associated with the submission. |
|
| 84 | + * |
|
| 85 | + * @var WPInv_Invoice |
|
| 86 | + */ |
|
| 87 | + protected $invoice = null; |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * The recurring item for the submission. |
|
| 91 | + * |
|
| 92 | + * @var int |
|
| 93 | + */ |
|
| 94 | + public $has_recurring = 0; |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * An array of fees for the submission. |
|
| 98 | + * |
|
| 99 | + * @var array |
|
| 100 | + */ |
|
| 101 | + protected $fees = array(); |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * An array of discounts for the submission. |
|
| 105 | + * |
|
| 106 | + * @var array |
|
| 107 | + */ |
|
| 108 | + protected $discounts = array(); |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * An array of taxes for the submission. |
|
| 112 | + * |
|
| 113 | + * @var array |
|
| 114 | + */ |
|
| 115 | + protected $taxes = array(); |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * An array of items for the submission. |
|
| 119 | + * |
|
| 120 | + * @var GetPaid_Form_Item[] |
|
| 121 | + */ |
|
| 122 | + protected $items = array(); |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * The last error. |
|
| 126 | + * |
|
| 127 | + * @var string |
|
| 128 | + */ |
|
| 129 | + public $last_error = null; |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * The last error code. |
|
| 133 | + * |
|
| 134 | + * @var string |
|
| 135 | + */ |
|
| 136 | + public $last_error_code = null; |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * Class constructor. |
|
| 140 | + * |
|
| 141 | + */ |
|
| 142 | + public function __construct() { |
|
| 143 | + |
|
| 144 | + // Set the state and country to the default state and country. |
|
| 145 | + $this->country = wpinv_default_billing_country(); |
|
| 146 | + $this->state = wpinv_get_default_state(); |
|
| 147 | + |
|
| 148 | + // Do we have an actual submission? |
|
| 149 | + if ( isset( $_POST['getpaid_payment_form_submission'] ) ) { |
|
| 150 | + $this->load_data( wp_kses_post_deep( wp_unslash( $_POST ) ) ); |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * Loads submission data. |
|
| 157 | + * |
|
| 158 | + * @param array $data |
|
| 159 | + */ |
|
| 160 | + public function load_data( $data ) { |
|
| 161 | + |
|
| 162 | + // Allow plugins to filter the data. |
|
| 163 | + $data = apply_filters( 'getpaid_submission_data', $data, $this ); |
|
| 164 | + |
|
| 165 | + // Cache it... |
|
| 166 | + $this->data = $data; |
|
| 167 | + |
|
| 168 | + // Then generate a unique id from the data. |
|
| 169 | + $this->id = md5( wp_json_encode( $data ) ); |
|
| 170 | + |
|
| 171 | + // Finally, process the submission. |
|
| 172 | + try { |
|
| 173 | + |
|
| 174 | + // Each process is passed an instance of the class (with reference) |
|
| 175 | + // and should throw an Exception whenever it encounters one. |
|
| 176 | + $processors = apply_filters( |
|
| 177 | + 'getpaid_payment_form_submission_processors', |
|
| 178 | + array( |
|
| 179 | + array( $this, 'process_payment_form' ), |
|
| 180 | + array( $this, 'process_invoice' ), |
|
| 181 | + array( $this, 'process_fees' ), |
|
| 182 | + array( $this, 'process_items' ), |
|
| 183 | + array( $this, 'process_discount' ), |
|
| 184 | + array( $this, 'process_taxes' ), |
|
| 185 | + ), |
|
| 186 | + $this |
|
| 187 | + ); |
|
| 188 | + |
|
| 189 | + foreach ( $processors as $processor ) { |
|
| 190 | + call_user_func_array( $processor, array( &$this ) ); |
|
| 191 | + } |
|
| 192 | + } catch ( GetPaid_Payment_Exception $e ) { |
|
| 193 | + $this->last_error = $e->getMessage(); |
|
| 194 | + $this->last_error_code = $e->getErrorCode(); |
|
| 195 | + } catch ( Exception $e ) { |
|
| 196 | + $this->last_error = $e->getMessage(); |
|
| 197 | + $this->last_error_code = $e->getCode(); |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + // Fired when we are done processing a submission. |
|
| 201 | + do_action_ref_array( 'getpaid_process_submission', array( &$this ) ); |
|
| 202 | + |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + /* |
|
| 206 | 206 | |-------------------------------------------------------------------------- |
| 207 | 207 | | Payment Forms. |
| 208 | 208 | |-------------------------------------------------------------------------- |
@@ -211,39 +211,39 @@ discard block |
||
| 211 | 211 | | submission has an active payment form etc. |
| 212 | 212 | */ |
| 213 | 213 | |
| 214 | - /** |
|
| 215 | - * Prepares the submission's payment form. |
|
| 216 | - * |
|
| 217 | - * @since 1.0.19 |
|
| 218 | - */ |
|
| 219 | - public function process_payment_form() { |
|
| 214 | + /** |
|
| 215 | + * Prepares the submission's payment form. |
|
| 216 | + * |
|
| 217 | + * @since 1.0.19 |
|
| 218 | + */ |
|
| 219 | + public function process_payment_form() { |
|
| 220 | 220 | |
| 221 | - // Every submission needs an active payment form. |
|
| 222 | - if ( empty( $this->data['form_id'] ) ) { |
|
| 223 | - throw new Exception( __( 'Missing payment form', 'invoicing' ) ); |
|
| 224 | - } |
|
| 221 | + // Every submission needs an active payment form. |
|
| 222 | + if ( empty( $this->data['form_id'] ) ) { |
|
| 223 | + throw new Exception( __( 'Missing payment form', 'invoicing' ) ); |
|
| 224 | + } |
|
| 225 | 225 | |
| 226 | - // Fetch the payment form. |
|
| 227 | - $this->payment_form = new GetPaid_Payment_Form( $this->data['form_id'] ); |
|
| 226 | + // Fetch the payment form. |
|
| 227 | + $this->payment_form = new GetPaid_Payment_Form( $this->data['form_id'] ); |
|
| 228 | 228 | |
| 229 | - if ( ! $this->payment_form->is_active() ) { |
|
| 230 | - throw new Exception( __( 'Payment form not active', 'invoicing' ) ); |
|
| 231 | - } |
|
| 229 | + if ( ! $this->payment_form->is_active() ) { |
|
| 230 | + throw new Exception( __( 'Payment form not active', 'invoicing' ) ); |
|
| 231 | + } |
|
| 232 | 232 | |
| 233 | - do_action_ref_array( 'getpaid_submissions_process_payment_form', array( &$this ) ); |
|
| 234 | - } |
|
| 233 | + do_action_ref_array( 'getpaid_submissions_process_payment_form', array( &$this ) ); |
|
| 234 | + } |
|
| 235 | 235 | |
| 236 | 236 | /** |
| 237 | - * Returns the payment form. |
|
| 238 | - * |
|
| 239 | - * @since 1.0.19 |
|
| 240 | - * @return GetPaid_Payment_Form |
|
| 241 | - */ |
|
| 242 | - public function get_payment_form() { |
|
| 243 | - return $this->payment_form; |
|
| 244 | - } |
|
| 237 | + * Returns the payment form. |
|
| 238 | + * |
|
| 239 | + * @since 1.0.19 |
|
| 240 | + * @return GetPaid_Payment_Form |
|
| 241 | + */ |
|
| 242 | + public function get_payment_form() { |
|
| 243 | + return $this->payment_form; |
|
| 244 | + } |
|
| 245 | 245 | |
| 246 | - /* |
|
| 246 | + /* |
|
| 247 | 247 | |-------------------------------------------------------------------------- |
| 248 | 248 | | Invoices. |
| 249 | 249 | |-------------------------------------------------------------------------- |
@@ -252,95 +252,95 @@ discard block |
||
| 252 | 252 | | might be for an existing invoice. |
| 253 | 253 | */ |
| 254 | 254 | |
| 255 | - /** |
|
| 256 | - * Prepares the submission's invoice. |
|
| 257 | - * |
|
| 258 | - * @since 1.0.19 |
|
| 259 | - */ |
|
| 260 | - public function process_invoice() { |
|
| 261 | - |
|
| 262 | - // Abort if there is no invoice. |
|
| 263 | - if ( empty( $this->data['invoice_id'] ) ) { |
|
| 264 | - |
|
| 265 | - // Check if we are resuming a payment. |
|
| 266 | - if ( empty( $this->data['maybe_use_invoice'] ) ) { |
|
| 267 | - return; |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - $invoice = wpinv_get_invoice( $this->data['maybe_use_invoice'] ); |
|
| 271 | - if ( empty( $invoice ) || ! $invoice->has_status( 'draft, auto-draft, wpi-pending' ) ) { |
|
| 272 | - return; |
|
| 273 | - } |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - // If the submission is for an existing invoice, ensure that it exists |
|
| 277 | - // and that it is not paid for. |
|
| 278 | - if ( empty( $invoice ) ) { |
|
| 279 | - $invoice = wpinv_get_invoice( $this->data['invoice_id'] ); |
|
| 280 | - } |
|
| 255 | + /** |
|
| 256 | + * Prepares the submission's invoice. |
|
| 257 | + * |
|
| 258 | + * @since 1.0.19 |
|
| 259 | + */ |
|
| 260 | + public function process_invoice() { |
|
| 261 | + |
|
| 262 | + // Abort if there is no invoice. |
|
| 263 | + if ( empty( $this->data['invoice_id'] ) ) { |
|
| 264 | + |
|
| 265 | + // Check if we are resuming a payment. |
|
| 266 | + if ( empty( $this->data['maybe_use_invoice'] ) ) { |
|
| 267 | + return; |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + $invoice = wpinv_get_invoice( $this->data['maybe_use_invoice'] ); |
|
| 271 | + if ( empty( $invoice ) || ! $invoice->has_status( 'draft, auto-draft, wpi-pending' ) ) { |
|
| 272 | + return; |
|
| 273 | + } |
|
| 274 | + } |
|
| 275 | + |
|
| 276 | + // If the submission is for an existing invoice, ensure that it exists |
|
| 277 | + // and that it is not paid for. |
|
| 278 | + if ( empty( $invoice ) ) { |
|
| 279 | + $invoice = wpinv_get_invoice( $this->data['invoice_id'] ); |
|
| 280 | + } |
|
| 281 | 281 | |
| 282 | 282 | if ( empty( $invoice ) ) { |
| 283 | - throw new Exception( __( 'Invalid invoice', 'invoicing' ) ); |
|
| 284 | - } |
|
| 285 | - |
|
| 286 | - if ( $invoice->is_paid() ) { |
|
| 287 | - throw new Exception( __( 'This invoice is already paid for.', 'invoicing' ) ); |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - $this->payment_form->invoice = $invoice; |
|
| 291 | - if ( ! $this->payment_form->is_default() ) { |
|
| 292 | - |
|
| 293 | - $items = array(); |
|
| 294 | - $item_ids = array(); |
|
| 295 | - |
|
| 296 | - foreach ( $invoice->get_items() as $item ) { |
|
| 297 | - if ( ! in_array( $item->get_id(), $item_ids ) ) { |
|
| 298 | - $item_ids[] = $item->get_id(); |
|
| 299 | - $items[] = $item; |
|
| 300 | - } |
|
| 301 | - } |
|
| 302 | - |
|
| 303 | - foreach ( $this->payment_form->get_items() as $item ) { |
|
| 304 | - if ( ! in_array( $item->get_id(), $item_ids ) ) { |
|
| 305 | - $item_ids[] = $item->get_id(); |
|
| 306 | - $items[] = $item; |
|
| 307 | - } |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - $this->payment_form->set_items( $items ); |
|
| 311 | - |
|
| 312 | - } else { |
|
| 313 | - $this->payment_form->set_items( $invoice->get_items() ); |
|
| 314 | - } |
|
| 315 | - |
|
| 316 | - $this->country = $invoice->get_country(); |
|
| 317 | - $this->state = $invoice->get_state(); |
|
| 318 | - $this->invoice = $invoice; |
|
| 319 | - |
|
| 320 | - do_action_ref_array( 'getpaid_submissions_process_invoice', array( &$this ) ); |
|
| 321 | - } |
|
| 322 | - |
|
| 323 | - /** |
|
| 324 | - * Returns the associated invoice. |
|
| 325 | - * |
|
| 326 | - * @since 1.0.19 |
|
| 327 | - * @return WPInv_Invoice |
|
| 328 | - */ |
|
| 329 | - public function get_invoice() { |
|
| 330 | - return $this->invoice; |
|
| 331 | - } |
|
| 332 | - |
|
| 333 | - /** |
|
| 334 | - * Checks whether there is an invoice associated with this submission. |
|
| 335 | - * |
|
| 336 | - * @since 1.0.19 |
|
| 337 | - * @return bool |
|
| 338 | - */ |
|
| 339 | - public function has_invoice() { |
|
| 340 | - return ! empty( $this->invoice ); |
|
| 341 | - } |
|
| 342 | - |
|
| 343 | - /* |
|
| 283 | + throw new Exception( __( 'Invalid invoice', 'invoicing' ) ); |
|
| 284 | + } |
|
| 285 | + |
|
| 286 | + if ( $invoice->is_paid() ) { |
|
| 287 | + throw new Exception( __( 'This invoice is already paid for.', 'invoicing' ) ); |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + $this->payment_form->invoice = $invoice; |
|
| 291 | + if ( ! $this->payment_form->is_default() ) { |
|
| 292 | + |
|
| 293 | + $items = array(); |
|
| 294 | + $item_ids = array(); |
|
| 295 | + |
|
| 296 | + foreach ( $invoice->get_items() as $item ) { |
|
| 297 | + if ( ! in_array( $item->get_id(), $item_ids ) ) { |
|
| 298 | + $item_ids[] = $item->get_id(); |
|
| 299 | + $items[] = $item; |
|
| 300 | + } |
|
| 301 | + } |
|
| 302 | + |
|
| 303 | + foreach ( $this->payment_form->get_items() as $item ) { |
|
| 304 | + if ( ! in_array( $item->get_id(), $item_ids ) ) { |
|
| 305 | + $item_ids[] = $item->get_id(); |
|
| 306 | + $items[] = $item; |
|
| 307 | + } |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + $this->payment_form->set_items( $items ); |
|
| 311 | + |
|
| 312 | + } else { |
|
| 313 | + $this->payment_form->set_items( $invoice->get_items() ); |
|
| 314 | + } |
|
| 315 | + |
|
| 316 | + $this->country = $invoice->get_country(); |
|
| 317 | + $this->state = $invoice->get_state(); |
|
| 318 | + $this->invoice = $invoice; |
|
| 319 | + |
|
| 320 | + do_action_ref_array( 'getpaid_submissions_process_invoice', array( &$this ) ); |
|
| 321 | + } |
|
| 322 | + |
|
| 323 | + /** |
|
| 324 | + * Returns the associated invoice. |
|
| 325 | + * |
|
| 326 | + * @since 1.0.19 |
|
| 327 | + * @return WPInv_Invoice |
|
| 328 | + */ |
|
| 329 | + public function get_invoice() { |
|
| 330 | + return $this->invoice; |
|
| 331 | + } |
|
| 332 | + |
|
| 333 | + /** |
|
| 334 | + * Checks whether there is an invoice associated with this submission. |
|
| 335 | + * |
|
| 336 | + * @since 1.0.19 |
|
| 337 | + * @return bool |
|
| 338 | + */ |
|
| 339 | + public function has_invoice() { |
|
| 340 | + return ! empty( $this->invoice ); |
|
| 341 | + } |
|
| 342 | + |
|
| 343 | + /* |
|
| 344 | 344 | |-------------------------------------------------------------------------- |
| 345 | 345 | | Items. |
| 346 | 346 | |-------------------------------------------------------------------------- |
@@ -349,129 +349,129 @@ discard block |
||
| 349 | 349 | | recurring item. But can have an unlimited number of non-recurring items. |
| 350 | 350 | */ |
| 351 | 351 | |
| 352 | - /** |
|
| 353 | - * Prepares the submission's items. |
|
| 354 | - * |
|
| 355 | - * @since 1.0.19 |
|
| 356 | - */ |
|
| 357 | - public function process_items() { |
|
| 358 | - |
|
| 359 | - $processor = new GetPaid_Payment_Form_Submission_Items( $this ); |
|
| 360 | - |
|
| 361 | - foreach ( $processor->items as $item ) { |
|
| 362 | - $this->add_item( $item ); |
|
| 363 | - } |
|
| 364 | - |
|
| 365 | - do_action_ref_array( 'getpaid_submissions_process_items', array( &$this ) ); |
|
| 366 | - } |
|
| 367 | - |
|
| 368 | - /** |
|
| 369 | - * Adds an item to the submission. |
|
| 370 | - * |
|
| 371 | - * @since 1.0.19 |
|
| 372 | - * @param GetPaid_Form_Item $item |
|
| 373 | - */ |
|
| 374 | - public function add_item( $item ) { |
|
| 375 | - |
|
| 376 | - // Make sure that it is available for purchase. |
|
| 377 | - if ( ! $item->can_purchase() || isset( $this->items[ $item->get_id() ] ) ) { |
|
| 378 | - return; |
|
| 379 | - } |
|
| 380 | - |
|
| 381 | - // Each submission can only contain one recurring item. |
|
| 382 | - if ( $item->is_recurring() ) { |
|
| 383 | - $this->has_recurring = $item->get_id(); |
|
| 384 | - } |
|
| 385 | - |
|
| 386 | - // Update the items and totals. |
|
| 387 | - $this->items[ $item->get_id() ] = $item; |
|
| 388 | - $this->totals['subtotal']['initial'] += $item->get_sub_total(); |
|
| 389 | - $this->totals['subtotal']['recurring'] += $item->get_recurring_sub_total(); |
|
| 390 | - |
|
| 391 | - } |
|
| 392 | - |
|
| 393 | - /** |
|
| 394 | - * Removes a specific item. |
|
| 395 | - * |
|
| 396 | - * You should not call this method after the discounts and taxes |
|
| 397 | - * have been calculated. |
|
| 398 | - * |
|
| 399 | - * @since 1.0.19 |
|
| 400 | - */ |
|
| 401 | - public function remove_item( $item_id ) { |
|
| 402 | - |
|
| 403 | - if ( isset( $this->items[ $item_id ] ) ) { |
|
| 404 | - $this->totals['subtotal']['initial'] -= $this->items[ $item_id ]->get_sub_total(); |
|
| 405 | - $this->totals['subtotal']['recurring'] -= $this->items[ $item_id ]->get_recurring_sub_total(); |
|
| 406 | - |
|
| 407 | - if ( $this->items[ $item_id ]->is_recurring() ) { |
|
| 408 | - $this->has_recurring = 0; |
|
| 409 | - } |
|
| 410 | - |
|
| 411 | - unset( $this->items[ $item_id ] ); |
|
| 412 | - } |
|
| 413 | - |
|
| 414 | - } |
|
| 415 | - |
|
| 416 | - /** |
|
| 417 | - * Returns the subtotal. |
|
| 418 | - * |
|
| 419 | - * @since 1.0.19 |
|
| 420 | - */ |
|
| 421 | - public function get_subtotal() { |
|
| 422 | - |
|
| 423 | - if ( wpinv_prices_include_tax() ) { |
|
| 424 | - return $this->totals['subtotal']['initial'] - $this->totals['taxes']['initial']; |
|
| 425 | - } |
|
| 426 | - |
|
| 427 | - return $this->totals['subtotal']['initial']; |
|
| 428 | - } |
|
| 429 | - |
|
| 430 | - /** |
|
| 431 | - * Returns the recurring subtotal. |
|
| 432 | - * |
|
| 433 | - * @since 1.0.19 |
|
| 434 | - */ |
|
| 435 | - public function get_recurring_subtotal() { |
|
| 436 | - |
|
| 437 | - if ( wpinv_prices_include_tax() ) { |
|
| 438 | - return $this->totals['subtotal']['recurring'] - $this->totals['taxes']['recurring']; |
|
| 439 | - } |
|
| 440 | - |
|
| 441 | - return $this->totals['subtotal']['recurring']; |
|
| 442 | - } |
|
| 443 | - |
|
| 444 | - /** |
|
| 445 | - * Returns all items. |
|
| 446 | - * |
|
| 447 | - * @since 1.0.19 |
|
| 448 | - * @return GetPaid_Form_Item[] |
|
| 449 | - */ |
|
| 450 | - public function get_items() { |
|
| 451 | - return $this->items; |
|
| 452 | - } |
|
| 453 | - |
|
| 454 | - /** |
|
| 455 | - * Checks if there's a single subscription group in the submission. |
|
| 456 | - * |
|
| 457 | - * @since 2.3.0 |
|
| 458 | - * @return bool |
|
| 459 | - */ |
|
| 460 | - public function has_subscription_group() { |
|
| 461 | - return $this->has_recurring && getpaid_should_group_subscriptions( $this ) && 1 == count( getpaid_get_subscription_groups( $this ) ); |
|
| 462 | - } |
|
| 463 | - |
|
| 464 | - /** |
|
| 465 | - * Checks if there are multipe subscription groups in the submission. |
|
| 466 | - * |
|
| 467 | - * @since 2.3.0 |
|
| 468 | - * @return bool |
|
| 469 | - */ |
|
| 470 | - public function has_multiple_subscription_groups() { |
|
| 471 | - return $this->has_recurring && 1 < count( getpaid_get_subscription_groups( $this ) ); |
|
| 472 | - } |
|
| 473 | - |
|
| 474 | - /* |
|
| 352 | + /** |
|
| 353 | + * Prepares the submission's items. |
|
| 354 | + * |
|
| 355 | + * @since 1.0.19 |
|
| 356 | + */ |
|
| 357 | + public function process_items() { |
|
| 358 | + |
|
| 359 | + $processor = new GetPaid_Payment_Form_Submission_Items( $this ); |
|
| 360 | + |
|
| 361 | + foreach ( $processor->items as $item ) { |
|
| 362 | + $this->add_item( $item ); |
|
| 363 | + } |
|
| 364 | + |
|
| 365 | + do_action_ref_array( 'getpaid_submissions_process_items', array( &$this ) ); |
|
| 366 | + } |
|
| 367 | + |
|
| 368 | + /** |
|
| 369 | + * Adds an item to the submission. |
|
| 370 | + * |
|
| 371 | + * @since 1.0.19 |
|
| 372 | + * @param GetPaid_Form_Item $item |
|
| 373 | + */ |
|
| 374 | + public function add_item( $item ) { |
|
| 375 | + |
|
| 376 | + // Make sure that it is available for purchase. |
|
| 377 | + if ( ! $item->can_purchase() || isset( $this->items[ $item->get_id() ] ) ) { |
|
| 378 | + return; |
|
| 379 | + } |
|
| 380 | + |
|
| 381 | + // Each submission can only contain one recurring item. |
|
| 382 | + if ( $item->is_recurring() ) { |
|
| 383 | + $this->has_recurring = $item->get_id(); |
|
| 384 | + } |
|
| 385 | + |
|
| 386 | + // Update the items and totals. |
|
| 387 | + $this->items[ $item->get_id() ] = $item; |
|
| 388 | + $this->totals['subtotal']['initial'] += $item->get_sub_total(); |
|
| 389 | + $this->totals['subtotal']['recurring'] += $item->get_recurring_sub_total(); |
|
| 390 | + |
|
| 391 | + } |
|
| 392 | + |
|
| 393 | + /** |
|
| 394 | + * Removes a specific item. |
|
| 395 | + * |
|
| 396 | + * You should not call this method after the discounts and taxes |
|
| 397 | + * have been calculated. |
|
| 398 | + * |
|
| 399 | + * @since 1.0.19 |
|
| 400 | + */ |
|
| 401 | + public function remove_item( $item_id ) { |
|
| 402 | + |
|
| 403 | + if ( isset( $this->items[ $item_id ] ) ) { |
|
| 404 | + $this->totals['subtotal']['initial'] -= $this->items[ $item_id ]->get_sub_total(); |
|
| 405 | + $this->totals['subtotal']['recurring'] -= $this->items[ $item_id ]->get_recurring_sub_total(); |
|
| 406 | + |
|
| 407 | + if ( $this->items[ $item_id ]->is_recurring() ) { |
|
| 408 | + $this->has_recurring = 0; |
|
| 409 | + } |
|
| 410 | + |
|
| 411 | + unset( $this->items[ $item_id ] ); |
|
| 412 | + } |
|
| 413 | + |
|
| 414 | + } |
|
| 415 | + |
|
| 416 | + /** |
|
| 417 | + * Returns the subtotal. |
|
| 418 | + * |
|
| 419 | + * @since 1.0.19 |
|
| 420 | + */ |
|
| 421 | + public function get_subtotal() { |
|
| 422 | + |
|
| 423 | + if ( wpinv_prices_include_tax() ) { |
|
| 424 | + return $this->totals['subtotal']['initial'] - $this->totals['taxes']['initial']; |
|
| 425 | + } |
|
| 426 | + |
|
| 427 | + return $this->totals['subtotal']['initial']; |
|
| 428 | + } |
|
| 429 | + |
|
| 430 | + /** |
|
| 431 | + * Returns the recurring subtotal. |
|
| 432 | + * |
|
| 433 | + * @since 1.0.19 |
|
| 434 | + */ |
|
| 435 | + public function get_recurring_subtotal() { |
|
| 436 | + |
|
| 437 | + if ( wpinv_prices_include_tax() ) { |
|
| 438 | + return $this->totals['subtotal']['recurring'] - $this->totals['taxes']['recurring']; |
|
| 439 | + } |
|
| 440 | + |
|
| 441 | + return $this->totals['subtotal']['recurring']; |
|
| 442 | + } |
|
| 443 | + |
|
| 444 | + /** |
|
| 445 | + * Returns all items. |
|
| 446 | + * |
|
| 447 | + * @since 1.0.19 |
|
| 448 | + * @return GetPaid_Form_Item[] |
|
| 449 | + */ |
|
| 450 | + public function get_items() { |
|
| 451 | + return $this->items; |
|
| 452 | + } |
|
| 453 | + |
|
| 454 | + /** |
|
| 455 | + * Checks if there's a single subscription group in the submission. |
|
| 456 | + * |
|
| 457 | + * @since 2.3.0 |
|
| 458 | + * @return bool |
|
| 459 | + */ |
|
| 460 | + public function has_subscription_group() { |
|
| 461 | + return $this->has_recurring && getpaid_should_group_subscriptions( $this ) && 1 == count( getpaid_get_subscription_groups( $this ) ); |
|
| 462 | + } |
|
| 463 | + |
|
| 464 | + /** |
|
| 465 | + * Checks if there are multipe subscription groups in the submission. |
|
| 466 | + * |
|
| 467 | + * @since 2.3.0 |
|
| 468 | + * @return bool |
|
| 469 | + */ |
|
| 470 | + public function has_multiple_subscription_groups() { |
|
| 471 | + return $this->has_recurring && 1 < count( getpaid_get_subscription_groups( $this ) ); |
|
| 472 | + } |
|
| 473 | + |
|
| 474 | + /* |
|
| 475 | 475 | |-------------------------------------------------------------------------- |
| 476 | 476 | | Taxes |
| 477 | 477 | |-------------------------------------------------------------------------- |
@@ -480,128 +480,128 @@ discard block |
||
| 480 | 480 | | or only one-time. |
| 481 | 481 | */ |
| 482 | 482 | |
| 483 | - /** |
|
| 484 | - * Prepares the submission's taxes. |
|
| 485 | - * |
|
| 486 | - * @since 1.0.19 |
|
| 487 | - */ |
|
| 488 | - public function process_taxes() { |
|
| 489 | - |
|
| 490 | - // Abort if we're not using taxes. |
|
| 491 | - if ( ! $this->use_taxes() ) { |
|
| 492 | - return; |
|
| 493 | - } |
|
| 494 | - |
|
| 495 | - // If a custom country && state has been passed in, use it to calculate taxes. |
|
| 496 | - $country = $this->get_field( 'wpinv_country', 'billing' ); |
|
| 497 | - if ( ! empty( $country ) ) { |
|
| 498 | - $this->country = $country; |
|
| 499 | - } |
|
| 500 | - |
|
| 501 | - $state = $this->get_field( 'wpinv_state', 'billing' ); |
|
| 502 | - if ( ! empty( $state ) ) { |
|
| 503 | - $this->state = $state; |
|
| 504 | - } |
|
| 505 | - |
|
| 506 | - // Confirm if the provided country and the ip country are similar. |
|
| 507 | - $address_confirmed = $this->get_field( 'confirm-address' ); |
|
| 508 | - if ( isset( $_POST['billing']['country'] ) && wpinv_should_validate_vat_number() && getpaid_get_ip_country() != $this->country && empty( $address_confirmed ) ) { |
|
| 509 | - throw new Exception( __( 'The country of your current location must be the same as the country of your billing location or you must confirm the billing address is your home country.', 'invoicing' ) ); |
|
| 510 | - } |
|
| 511 | - |
|
| 512 | - // Abort if the country is not taxable. |
|
| 513 | - if ( ! wpinv_is_country_taxable( $this->country ) ) { |
|
| 514 | - return; |
|
| 515 | - } |
|
| 516 | - |
|
| 517 | - $processor = new GetPaid_Payment_Form_Submission_Taxes( $this ); |
|
| 518 | - |
|
| 519 | - foreach ( $processor->taxes as $tax ) { |
|
| 520 | - $this->add_tax( $tax ); |
|
| 521 | - } |
|
| 522 | - |
|
| 523 | - do_action_ref_array( 'getpaid_submissions_process_taxes', array( &$this ) ); |
|
| 524 | - } |
|
| 525 | - |
|
| 526 | - /** |
|
| 527 | - * Adds a tax to the submission. |
|
| 528 | - * |
|
| 529 | - * @param array $tax An array of tax details. name, initial_tax, and recurring_tax are required. |
|
| 530 | - * @since 1.0.19 |
|
| 531 | - */ |
|
| 532 | - public function add_tax( $tax ) { |
|
| 533 | - |
|
| 534 | - if ( wpinv_round_tax_per_tax_rate() ) { |
|
| 535 | - $tax['initial_tax'] = wpinv_round_amount( $tax['initial_tax'] ); |
|
| 536 | - $tax['recurring_tax'] = wpinv_round_amount( $tax['recurring_tax'] ); |
|
| 537 | - } |
|
| 538 | - |
|
| 539 | - $this->taxes[ $tax['name'] ] = $tax; |
|
| 540 | - $this->totals['taxes']['initial'] += wpinv_sanitize_amount( $tax['initial_tax'] ); |
|
| 541 | - $this->totals['taxes']['recurring'] += wpinv_sanitize_amount( $tax['recurring_tax'] ); |
|
| 542 | - |
|
| 543 | - } |
|
| 544 | - |
|
| 545 | - /** |
|
| 546 | - * Removes a specific tax. |
|
| 547 | - * |
|
| 548 | - * @since 1.0.19 |
|
| 549 | - */ |
|
| 550 | - public function remove_tax( $tax_name ) { |
|
| 551 | - |
|
| 552 | - if ( isset( $this->taxes[ $tax_name ] ) ) { |
|
| 553 | - $this->totals['taxes']['initial'] -= $this->taxes[ $tax_name ]['initial_tax']; |
|
| 554 | - $this->totals['taxes']['recurring'] -= $this->taxes[ $tax_name ]['recurring_tax']; |
|
| 555 | - unset( $this->taxes[ $tax_name ] ); |
|
| 556 | - } |
|
| 557 | - |
|
| 558 | - } |
|
| 559 | - |
|
| 560 | - /** |
|
| 561 | - * Whether or not we'll use taxes for the submission. |
|
| 562 | - * |
|
| 563 | - * @since 1.0.19 |
|
| 564 | - */ |
|
| 565 | - public function use_taxes() { |
|
| 566 | - |
|
| 567 | - $use_taxes = wpinv_use_taxes(); |
|
| 568 | - |
|
| 569 | - if ( $this->has_invoice() && ! $this->invoice->is_taxable() ) { |
|
| 570 | - $use_taxes = false; |
|
| 571 | - } |
|
| 572 | - |
|
| 573 | - return apply_filters( 'getpaid_submission_use_taxes', $use_taxes, $this ); |
|
| 574 | - |
|
| 575 | - } |
|
| 576 | - |
|
| 577 | - /** |
|
| 578 | - * Returns the tax. |
|
| 579 | - * |
|
| 580 | - * @since 1.0.19 |
|
| 581 | - */ |
|
| 582 | - public function get_tax() { |
|
| 583 | - return $this->totals['taxes']['initial']; |
|
| 584 | - } |
|
| 585 | - |
|
| 586 | - /** |
|
| 587 | - * Returns the recurring tax. |
|
| 588 | - * |
|
| 589 | - * @since 1.0.19 |
|
| 590 | - */ |
|
| 591 | - public function get_recurring_tax() { |
|
| 592 | - return $this->totals['taxes']['recurring']; |
|
| 593 | - } |
|
| 594 | - |
|
| 595 | - /** |
|
| 596 | - * Returns all taxes. |
|
| 597 | - * |
|
| 598 | - * @since 1.0.19 |
|
| 599 | - */ |
|
| 600 | - public function get_taxes() { |
|
| 601 | - return $this->taxes; |
|
| 602 | - } |
|
| 603 | - |
|
| 604 | - /* |
|
| 483 | + /** |
|
| 484 | + * Prepares the submission's taxes. |
|
| 485 | + * |
|
| 486 | + * @since 1.0.19 |
|
| 487 | + */ |
|
| 488 | + public function process_taxes() { |
|
| 489 | + |
|
| 490 | + // Abort if we're not using taxes. |
|
| 491 | + if ( ! $this->use_taxes() ) { |
|
| 492 | + return; |
|
| 493 | + } |
|
| 494 | + |
|
| 495 | + // If a custom country && state has been passed in, use it to calculate taxes. |
|
| 496 | + $country = $this->get_field( 'wpinv_country', 'billing' ); |
|
| 497 | + if ( ! empty( $country ) ) { |
|
| 498 | + $this->country = $country; |
|
| 499 | + } |
|
| 500 | + |
|
| 501 | + $state = $this->get_field( 'wpinv_state', 'billing' ); |
|
| 502 | + if ( ! empty( $state ) ) { |
|
| 503 | + $this->state = $state; |
|
| 504 | + } |
|
| 505 | + |
|
| 506 | + // Confirm if the provided country and the ip country are similar. |
|
| 507 | + $address_confirmed = $this->get_field( 'confirm-address' ); |
|
| 508 | + if ( isset( $_POST['billing']['country'] ) && wpinv_should_validate_vat_number() && getpaid_get_ip_country() != $this->country && empty( $address_confirmed ) ) { |
|
| 509 | + throw new Exception( __( 'The country of your current location must be the same as the country of your billing location or you must confirm the billing address is your home country.', 'invoicing' ) ); |
|
| 510 | + } |
|
| 511 | + |
|
| 512 | + // Abort if the country is not taxable. |
|
| 513 | + if ( ! wpinv_is_country_taxable( $this->country ) ) { |
|
| 514 | + return; |
|
| 515 | + } |
|
| 516 | + |
|
| 517 | + $processor = new GetPaid_Payment_Form_Submission_Taxes( $this ); |
|
| 518 | + |
|
| 519 | + foreach ( $processor->taxes as $tax ) { |
|
| 520 | + $this->add_tax( $tax ); |
|
| 521 | + } |
|
| 522 | + |
|
| 523 | + do_action_ref_array( 'getpaid_submissions_process_taxes', array( &$this ) ); |
|
| 524 | + } |
|
| 525 | + |
|
| 526 | + /** |
|
| 527 | + * Adds a tax to the submission. |
|
| 528 | + * |
|
| 529 | + * @param array $tax An array of tax details. name, initial_tax, and recurring_tax are required. |
|
| 530 | + * @since 1.0.19 |
|
| 531 | + */ |
|
| 532 | + public function add_tax( $tax ) { |
|
| 533 | + |
|
| 534 | + if ( wpinv_round_tax_per_tax_rate() ) { |
|
| 535 | + $tax['initial_tax'] = wpinv_round_amount( $tax['initial_tax'] ); |
|
| 536 | + $tax['recurring_tax'] = wpinv_round_amount( $tax['recurring_tax'] ); |
|
| 537 | + } |
|
| 538 | + |
|
| 539 | + $this->taxes[ $tax['name'] ] = $tax; |
|
| 540 | + $this->totals['taxes']['initial'] += wpinv_sanitize_amount( $tax['initial_tax'] ); |
|
| 541 | + $this->totals['taxes']['recurring'] += wpinv_sanitize_amount( $tax['recurring_tax'] ); |
|
| 542 | + |
|
| 543 | + } |
|
| 544 | + |
|
| 545 | + /** |
|
| 546 | + * Removes a specific tax. |
|
| 547 | + * |
|
| 548 | + * @since 1.0.19 |
|
| 549 | + */ |
|
| 550 | + public function remove_tax( $tax_name ) { |
|
| 551 | + |
|
| 552 | + if ( isset( $this->taxes[ $tax_name ] ) ) { |
|
| 553 | + $this->totals['taxes']['initial'] -= $this->taxes[ $tax_name ]['initial_tax']; |
|
| 554 | + $this->totals['taxes']['recurring'] -= $this->taxes[ $tax_name ]['recurring_tax']; |
|
| 555 | + unset( $this->taxes[ $tax_name ] ); |
|
| 556 | + } |
|
| 557 | + |
|
| 558 | + } |
|
| 559 | + |
|
| 560 | + /** |
|
| 561 | + * Whether or not we'll use taxes for the submission. |
|
| 562 | + * |
|
| 563 | + * @since 1.0.19 |
|
| 564 | + */ |
|
| 565 | + public function use_taxes() { |
|
| 566 | + |
|
| 567 | + $use_taxes = wpinv_use_taxes(); |
|
| 568 | + |
|
| 569 | + if ( $this->has_invoice() && ! $this->invoice->is_taxable() ) { |
|
| 570 | + $use_taxes = false; |
|
| 571 | + } |
|
| 572 | + |
|
| 573 | + return apply_filters( 'getpaid_submission_use_taxes', $use_taxes, $this ); |
|
| 574 | + |
|
| 575 | + } |
|
| 576 | + |
|
| 577 | + /** |
|
| 578 | + * Returns the tax. |
|
| 579 | + * |
|
| 580 | + * @since 1.0.19 |
|
| 581 | + */ |
|
| 582 | + public function get_tax() { |
|
| 583 | + return $this->totals['taxes']['initial']; |
|
| 584 | + } |
|
| 585 | + |
|
| 586 | + /** |
|
| 587 | + * Returns the recurring tax. |
|
| 588 | + * |
|
| 589 | + * @since 1.0.19 |
|
| 590 | + */ |
|
| 591 | + public function get_recurring_tax() { |
|
| 592 | + return $this->totals['taxes']['recurring']; |
|
| 593 | + } |
|
| 594 | + |
|
| 595 | + /** |
|
| 596 | + * Returns all taxes. |
|
| 597 | + * |
|
| 598 | + * @since 1.0.19 |
|
| 599 | + */ |
|
| 600 | + public function get_taxes() { |
|
| 601 | + return $this->taxes; |
|
| 602 | + } |
|
| 603 | + |
|
| 604 | + /* |
|
| 605 | 605 | |-------------------------------------------------------------------------- |
| 606 | 606 | | Discounts |
| 607 | 607 | |-------------------------------------------------------------------------- |
@@ -610,99 +610,99 @@ discard block |
||
| 610 | 610 | | or only one-time. They also do not have to come from a discount code. |
| 611 | 611 | */ |
| 612 | 612 | |
| 613 | - /** |
|
| 614 | - * Prepares the submission's discount. |
|
| 615 | - * |
|
| 616 | - * @since 1.0.19 |
|
| 617 | - */ |
|
| 618 | - public function process_discount() { |
|
| 619 | - |
|
| 620 | - $initial_total = $this->get_subtotal() + $this->get_fee() + $this->get_tax(); |
|
| 621 | - $recurring_total = $this->get_recurring_subtotal() + $this->get_recurring_fee() + $this->get_recurring_tax(); |
|
| 622 | - $processor = new GetPaid_Payment_Form_Submission_Discount( $this, $initial_total, $recurring_total ); |
|
| 623 | - |
|
| 624 | - foreach ( $processor->discounts as $discount ) { |
|
| 625 | - $this->add_discount( $discount ); |
|
| 626 | - } |
|
| 627 | - |
|
| 628 | - do_action_ref_array( 'getpaid_submissions_process_discounts', array( &$this ) ); |
|
| 629 | - } |
|
| 630 | - |
|
| 631 | - /** |
|
| 632 | - * Adds a discount to the submission. |
|
| 633 | - * |
|
| 634 | - * @param array $discount An array of discount details. name, initial_discount, and recurring_discount are required. Include discount_code if the discount is from a discount code. |
|
| 635 | - * @since 1.0.19 |
|
| 636 | - */ |
|
| 637 | - public function add_discount( $discount ) { |
|
| 638 | - $this->discounts[ $discount['name'] ] = $discount; |
|
| 639 | - $this->totals['discount']['initial'] += wpinv_sanitize_amount( $discount['initial_discount'] ); |
|
| 640 | - $this->totals['discount']['recurring'] += wpinv_sanitize_amount( $discount['recurring_discount'] ); |
|
| 641 | - } |
|
| 642 | - |
|
| 643 | - /** |
|
| 644 | - * Removes a discount from the submission. |
|
| 645 | - * |
|
| 646 | - * @since 1.0.19 |
|
| 647 | - */ |
|
| 648 | - public function remove_discount( $name ) { |
|
| 649 | - |
|
| 650 | - if ( isset( $this->discounts[ $name ] ) ) { |
|
| 651 | - $this->totals['discount']['initial'] -= $this->discounts[ $name ]['initial_discount']; |
|
| 652 | - $this->totals['discount']['recurring'] -= $this->discounts[ $name ]['recurring_discount']; |
|
| 653 | - unset( $this->discounts[ $name ] ); |
|
| 654 | - } |
|
| 655 | - |
|
| 656 | - } |
|
| 657 | - |
|
| 658 | - /** |
|
| 659 | - * Checks whether there is a discount code associated with this submission. |
|
| 660 | - * |
|
| 661 | - * @since 1.0.19 |
|
| 662 | - * @return bool |
|
| 663 | - */ |
|
| 664 | - public function has_discount_code() { |
|
| 665 | - return ! empty( $this->discounts['discount_code'] ); |
|
| 666 | - } |
|
| 667 | - |
|
| 668 | - /** |
|
| 669 | - * Returns the discount code. |
|
| 670 | - * |
|
| 671 | - * @since 1.0.19 |
|
| 672 | - * @return string |
|
| 673 | - */ |
|
| 674 | - public function get_discount_code() { |
|
| 675 | - return $this->has_discount_code() ? $this->discounts['discount_code']['discount_code'] : ''; |
|
| 676 | - } |
|
| 677 | - |
|
| 678 | - /** |
|
| 679 | - * Returns the discount. |
|
| 680 | - * |
|
| 681 | - * @since 1.0.19 |
|
| 682 | - */ |
|
| 683 | - public function get_discount() { |
|
| 684 | - return $this->totals['discount']['initial']; |
|
| 685 | - } |
|
| 686 | - |
|
| 687 | - /** |
|
| 688 | - * Returns the recurring discount. |
|
| 689 | - * |
|
| 690 | - * @since 1.0.19 |
|
| 691 | - */ |
|
| 692 | - public function get_recurring_discount() { |
|
| 693 | - return $this->totals['discount']['recurring']; |
|
| 694 | - } |
|
| 695 | - |
|
| 696 | - /** |
|
| 697 | - * Returns all discounts. |
|
| 698 | - * |
|
| 699 | - * @since 1.0.19 |
|
| 700 | - */ |
|
| 701 | - public function get_discounts() { |
|
| 702 | - return $this->discounts; |
|
| 703 | - } |
|
| 704 | - |
|
| 705 | - /* |
|
| 613 | + /** |
|
| 614 | + * Prepares the submission's discount. |
|
| 615 | + * |
|
| 616 | + * @since 1.0.19 |
|
| 617 | + */ |
|
| 618 | + public function process_discount() { |
|
| 619 | + |
|
| 620 | + $initial_total = $this->get_subtotal() + $this->get_fee() + $this->get_tax(); |
|
| 621 | + $recurring_total = $this->get_recurring_subtotal() + $this->get_recurring_fee() + $this->get_recurring_tax(); |
|
| 622 | + $processor = new GetPaid_Payment_Form_Submission_Discount( $this, $initial_total, $recurring_total ); |
|
| 623 | + |
|
| 624 | + foreach ( $processor->discounts as $discount ) { |
|
| 625 | + $this->add_discount( $discount ); |
|
| 626 | + } |
|
| 627 | + |
|
| 628 | + do_action_ref_array( 'getpaid_submissions_process_discounts', array( &$this ) ); |
|
| 629 | + } |
|
| 630 | + |
|
| 631 | + /** |
|
| 632 | + * Adds a discount to the submission. |
|
| 633 | + * |
|
| 634 | + * @param array $discount An array of discount details. name, initial_discount, and recurring_discount are required. Include discount_code if the discount is from a discount code. |
|
| 635 | + * @since 1.0.19 |
|
| 636 | + */ |
|
| 637 | + public function add_discount( $discount ) { |
|
| 638 | + $this->discounts[ $discount['name'] ] = $discount; |
|
| 639 | + $this->totals['discount']['initial'] += wpinv_sanitize_amount( $discount['initial_discount'] ); |
|
| 640 | + $this->totals['discount']['recurring'] += wpinv_sanitize_amount( $discount['recurring_discount'] ); |
|
| 641 | + } |
|
| 642 | + |
|
| 643 | + /** |
|
| 644 | + * Removes a discount from the submission. |
|
| 645 | + * |
|
| 646 | + * @since 1.0.19 |
|
| 647 | + */ |
|
| 648 | + public function remove_discount( $name ) { |
|
| 649 | + |
|
| 650 | + if ( isset( $this->discounts[ $name ] ) ) { |
|
| 651 | + $this->totals['discount']['initial'] -= $this->discounts[ $name ]['initial_discount']; |
|
| 652 | + $this->totals['discount']['recurring'] -= $this->discounts[ $name ]['recurring_discount']; |
|
| 653 | + unset( $this->discounts[ $name ] ); |
|
| 654 | + } |
|
| 655 | + |
|
| 656 | + } |
|
| 657 | + |
|
| 658 | + /** |
|
| 659 | + * Checks whether there is a discount code associated with this submission. |
|
| 660 | + * |
|
| 661 | + * @since 1.0.19 |
|
| 662 | + * @return bool |
|
| 663 | + */ |
|
| 664 | + public function has_discount_code() { |
|
| 665 | + return ! empty( $this->discounts['discount_code'] ); |
|
| 666 | + } |
|
| 667 | + |
|
| 668 | + /** |
|
| 669 | + * Returns the discount code. |
|
| 670 | + * |
|
| 671 | + * @since 1.0.19 |
|
| 672 | + * @return string |
|
| 673 | + */ |
|
| 674 | + public function get_discount_code() { |
|
| 675 | + return $this->has_discount_code() ? $this->discounts['discount_code']['discount_code'] : ''; |
|
| 676 | + } |
|
| 677 | + |
|
| 678 | + /** |
|
| 679 | + * Returns the discount. |
|
| 680 | + * |
|
| 681 | + * @since 1.0.19 |
|
| 682 | + */ |
|
| 683 | + public function get_discount() { |
|
| 684 | + return $this->totals['discount']['initial']; |
|
| 685 | + } |
|
| 686 | + |
|
| 687 | + /** |
|
| 688 | + * Returns the recurring discount. |
|
| 689 | + * |
|
| 690 | + * @since 1.0.19 |
|
| 691 | + */ |
|
| 692 | + public function get_recurring_discount() { |
|
| 693 | + return $this->totals['discount']['recurring']; |
|
| 694 | + } |
|
| 695 | + |
|
| 696 | + /** |
|
| 697 | + * Returns all discounts. |
|
| 698 | + * |
|
| 699 | + * @since 1.0.19 |
|
| 700 | + */ |
|
| 701 | + public function get_discounts() { |
|
| 702 | + return $this->discounts; |
|
| 703 | + } |
|
| 704 | + |
|
| 705 | + /* |
|
| 706 | 706 | |-------------------------------------------------------------------------- |
| 707 | 707 | | Fees |
| 708 | 708 | |-------------------------------------------------------------------------- |
@@ -712,100 +712,100 @@ discard block |
||
| 712 | 712 | | fees. |
| 713 | 713 | */ |
| 714 | 714 | |
| 715 | - /** |
|
| 716 | - * Prepares the submission's fees. |
|
| 717 | - * |
|
| 718 | - * @since 1.0.19 |
|
| 719 | - */ |
|
| 720 | - public function process_fees() { |
|
| 721 | - |
|
| 722 | - $fees_processor = new GetPaid_Payment_Form_Submission_Fees( $this ); |
|
| 723 | - |
|
| 724 | - foreach ( $fees_processor->fees as $fee ) { |
|
| 725 | - $this->add_fee( $fee ); |
|
| 726 | - } |
|
| 727 | - |
|
| 728 | - do_action_ref_array( 'getpaid_submissions_process_fees', array( &$this ) ); |
|
| 729 | - } |
|
| 730 | - |
|
| 731 | - /** |
|
| 732 | - * Adds a fee to the submission. |
|
| 733 | - * |
|
| 734 | - * @param array $fee An array of fee details. name, initial_fee, and recurring_fee are required. |
|
| 735 | - * @since 1.0.19 |
|
| 736 | - */ |
|
| 737 | - public function add_fee( $fee ) { |
|
| 738 | - |
|
| 739 | - if ( $fee['name'] == 'shipping' ) { |
|
| 740 | - $this->totals['shipping']['initial'] += wpinv_sanitize_amount( $fee['initial_fee'] ); |
|
| 741 | - $this->totals['shipping']['recurring'] += wpinv_sanitize_amount( $fee['recurring_fee'] ); |
|
| 742 | - return; |
|
| 743 | - } |
|
| 744 | - |
|
| 745 | - $this->fees[ $fee['name'] ] = $fee; |
|
| 746 | - $this->totals['fees']['initial'] += wpinv_sanitize_amount( $fee['initial_fee'] ); |
|
| 747 | - $this->totals['fees']['recurring'] += wpinv_sanitize_amount( $fee['recurring_fee'] ); |
|
| 748 | - |
|
| 749 | - } |
|
| 750 | - |
|
| 751 | - /** |
|
| 752 | - * Removes a fee from the submission. |
|
| 753 | - * |
|
| 754 | - * @since 1.0.19 |
|
| 755 | - */ |
|
| 756 | - public function remove_fee( $name ) { |
|
| 757 | - |
|
| 758 | - if ( isset( $this->fees[ $name ] ) ) { |
|
| 759 | - $this->totals['fees']['initial'] -= $this->fees[ $name ]['initial_fee']; |
|
| 760 | - $this->totals['fees']['recurring'] -= $this->fees[ $name ]['recurring_fee']; |
|
| 761 | - unset( $this->fees[ $name ] ); |
|
| 762 | - } |
|
| 763 | - |
|
| 764 | - if ( 'shipping' == $name ) { |
|
| 765 | - $this->totals['shipping']['initial'] = 0; |
|
| 766 | - $this->totals['shipping']['recurring'] = 0; |
|
| 767 | - } |
|
| 768 | - |
|
| 769 | - } |
|
| 770 | - |
|
| 771 | - /** |
|
| 772 | - * Returns the fees. |
|
| 773 | - * |
|
| 774 | - * @since 1.0.19 |
|
| 775 | - */ |
|
| 776 | - public function get_fee() { |
|
| 777 | - return $this->totals['fees']['initial']; |
|
| 778 | - } |
|
| 779 | - |
|
| 780 | - /** |
|
| 781 | - * Returns the recurring fees. |
|
| 782 | - * |
|
| 783 | - * @since 1.0.19 |
|
| 784 | - */ |
|
| 785 | - public function get_recurring_fee() { |
|
| 786 | - return $this->totals['fees']['recurring']; |
|
| 787 | - } |
|
| 788 | - |
|
| 789 | - /** |
|
| 790 | - * Returns all fees. |
|
| 791 | - * |
|
| 792 | - * @since 1.0.19 |
|
| 793 | - */ |
|
| 794 | - public function get_fees() { |
|
| 795 | - return $this->fees; |
|
| 796 | - } |
|
| 797 | - |
|
| 798 | - /** |
|
| 799 | - * Checks if there are any fees for the form. |
|
| 800 | - * |
|
| 801 | - * @return bool |
|
| 802 | - * @since 1.0.19 |
|
| 803 | - */ |
|
| 804 | - public function has_fees() { |
|
| 805 | - return count( $this->fees ) !== 0; |
|
| 806 | - } |
|
| 807 | - |
|
| 808 | - /* |
|
| 715 | + /** |
|
| 716 | + * Prepares the submission's fees. |
|
| 717 | + * |
|
| 718 | + * @since 1.0.19 |
|
| 719 | + */ |
|
| 720 | + public function process_fees() { |
|
| 721 | + |
|
| 722 | + $fees_processor = new GetPaid_Payment_Form_Submission_Fees( $this ); |
|
| 723 | + |
|
| 724 | + foreach ( $fees_processor->fees as $fee ) { |
|
| 725 | + $this->add_fee( $fee ); |
|
| 726 | + } |
|
| 727 | + |
|
| 728 | + do_action_ref_array( 'getpaid_submissions_process_fees', array( &$this ) ); |
|
| 729 | + } |
|
| 730 | + |
|
| 731 | + /** |
|
| 732 | + * Adds a fee to the submission. |
|
| 733 | + * |
|
| 734 | + * @param array $fee An array of fee details. name, initial_fee, and recurring_fee are required. |
|
| 735 | + * @since 1.0.19 |
|
| 736 | + */ |
|
| 737 | + public function add_fee( $fee ) { |
|
| 738 | + |
|
| 739 | + if ( $fee['name'] == 'shipping' ) { |
|
| 740 | + $this->totals['shipping']['initial'] += wpinv_sanitize_amount( $fee['initial_fee'] ); |
|
| 741 | + $this->totals['shipping']['recurring'] += wpinv_sanitize_amount( $fee['recurring_fee'] ); |
|
| 742 | + return; |
|
| 743 | + } |
|
| 744 | + |
|
| 745 | + $this->fees[ $fee['name'] ] = $fee; |
|
| 746 | + $this->totals['fees']['initial'] += wpinv_sanitize_amount( $fee['initial_fee'] ); |
|
| 747 | + $this->totals['fees']['recurring'] += wpinv_sanitize_amount( $fee['recurring_fee'] ); |
|
| 748 | + |
|
| 749 | + } |
|
| 750 | + |
|
| 751 | + /** |
|
| 752 | + * Removes a fee from the submission. |
|
| 753 | + * |
|
| 754 | + * @since 1.0.19 |
|
| 755 | + */ |
|
| 756 | + public function remove_fee( $name ) { |
|
| 757 | + |
|
| 758 | + if ( isset( $this->fees[ $name ] ) ) { |
|
| 759 | + $this->totals['fees']['initial'] -= $this->fees[ $name ]['initial_fee']; |
|
| 760 | + $this->totals['fees']['recurring'] -= $this->fees[ $name ]['recurring_fee']; |
|
| 761 | + unset( $this->fees[ $name ] ); |
|
| 762 | + } |
|
| 763 | + |
|
| 764 | + if ( 'shipping' == $name ) { |
|
| 765 | + $this->totals['shipping']['initial'] = 0; |
|
| 766 | + $this->totals['shipping']['recurring'] = 0; |
|
| 767 | + } |
|
| 768 | + |
|
| 769 | + } |
|
| 770 | + |
|
| 771 | + /** |
|
| 772 | + * Returns the fees. |
|
| 773 | + * |
|
| 774 | + * @since 1.0.19 |
|
| 775 | + */ |
|
| 776 | + public function get_fee() { |
|
| 777 | + return $this->totals['fees']['initial']; |
|
| 778 | + } |
|
| 779 | + |
|
| 780 | + /** |
|
| 781 | + * Returns the recurring fees. |
|
| 782 | + * |
|
| 783 | + * @since 1.0.19 |
|
| 784 | + */ |
|
| 785 | + public function get_recurring_fee() { |
|
| 786 | + return $this->totals['fees']['recurring']; |
|
| 787 | + } |
|
| 788 | + |
|
| 789 | + /** |
|
| 790 | + * Returns all fees. |
|
| 791 | + * |
|
| 792 | + * @since 1.0.19 |
|
| 793 | + */ |
|
| 794 | + public function get_fees() { |
|
| 795 | + return $this->fees; |
|
| 796 | + } |
|
| 797 | + |
|
| 798 | + /** |
|
| 799 | + * Checks if there are any fees for the form. |
|
| 800 | + * |
|
| 801 | + * @return bool |
|
| 802 | + * @since 1.0.19 |
|
| 803 | + */ |
|
| 804 | + public function has_fees() { |
|
| 805 | + return count( $this->fees ) !== 0; |
|
| 806 | + } |
|
| 807 | + |
|
| 808 | + /* |
|
| 809 | 809 | |-------------------------------------------------------------------------- |
| 810 | 810 | | MISC |
| 811 | 811 | |-------------------------------------------------------------------------- |
@@ -813,147 +813,147 @@ discard block |
||
| 813 | 813 | | Extra submission functions. |
| 814 | 814 | */ |
| 815 | 815 | |
| 816 | - /** |
|
| 817 | - * Returns the shipping amount. |
|
| 818 | - * |
|
| 819 | - * @since 1.0.19 |
|
| 820 | - */ |
|
| 821 | - public function get_shipping() { |
|
| 822 | - return $this->totals['shipping']['initial']; |
|
| 823 | - } |
|
| 824 | - |
|
| 825 | - /** |
|
| 826 | - * Returns the recurring shipping. |
|
| 827 | - * |
|
| 828 | - * @since 1.0.19 |
|
| 829 | - */ |
|
| 830 | - public function get_recurring_shipping() { |
|
| 831 | - return $this->totals['shipping']['recurring']; |
|
| 832 | - } |
|
| 833 | - |
|
| 834 | - /** |
|
| 835 | - * Checks if there are any shipping fees for the form. |
|
| 836 | - * |
|
| 837 | - * @return bool |
|
| 838 | - * @since 1.0.19 |
|
| 839 | - */ |
|
| 840 | - public function has_shipping() { |
|
| 841 | - return apply_filters( 'getpaid_payment_form_has_shipping', false, $this ); |
|
| 842 | - } |
|
| 843 | - |
|
| 844 | - /** |
|
| 845 | - * Checks if this is the initial fetch. |
|
| 846 | - * |
|
| 847 | - * @return bool |
|
| 848 | - * @since 1.0.19 |
|
| 849 | - */ |
|
| 850 | - public function is_initial_fetch() { |
|
| 851 | - return isset( $this->data['initial_state'] ) && empty( $this->data['initial_state'] ); |
|
| 852 | - } |
|
| 853 | - |
|
| 854 | - /** |
|
| 855 | - * Returns the total amount to collect for this submission. |
|
| 856 | - * |
|
| 857 | - * @since 1.0.19 |
|
| 858 | - */ |
|
| 859 | - public function get_total() { |
|
| 860 | - $total = $this->get_subtotal() + $this->get_fee() + $this->get_tax() + $this->get_shipping() - $this->get_discount(); |
|
| 861 | - return max( $total, 0 ); |
|
| 862 | - } |
|
| 863 | - |
|
| 864 | - /** |
|
| 865 | - * Returns the recurring total amount to collect for this submission. |
|
| 866 | - * |
|
| 867 | - * @since 1.0.19 |
|
| 868 | - */ |
|
| 869 | - public function get_recurring_total() { |
|
| 870 | - $total = $this->get_recurring_subtotal() + $this->get_recurring_fee() + $this->get_recurring_tax() + $this->get_recurring_shipping() - $this->get_recurring_discount(); |
|
| 871 | - return max( $total, 0 ); |
|
| 872 | - } |
|
| 873 | - |
|
| 874 | - /** |
|
| 875 | - * Whether payment details should be collected for this submission. |
|
| 876 | - * |
|
| 877 | - * @since 1.0.19 |
|
| 878 | - */ |
|
| 879 | - public function should_collect_payment_details() { |
|
| 880 | - $initial = $this->get_total(); |
|
| 881 | - $recurring = $this->get_recurring_total(); |
|
| 882 | - |
|
| 883 | - if ( $this->has_recurring == 0 ) { |
|
| 884 | - $recurring = 0; |
|
| 885 | - } |
|
| 886 | - |
|
| 887 | - $collect = $initial > 0 || $recurring > 0; |
|
| 888 | - return apply_filters( 'getpaid_submission_should_collect_payment_details', $collect, $this ); |
|
| 889 | - } |
|
| 890 | - |
|
| 891 | - /** |
|
| 892 | - * Returns the billing email of the user. |
|
| 893 | - * |
|
| 894 | - * @since 1.0.19 |
|
| 895 | - */ |
|
| 896 | - public function get_billing_email() { |
|
| 897 | - return apply_filters( 'getpaid_get_submission_billing_email', $this->get_field( 'billing_email' ), $this ); |
|
| 898 | - } |
|
| 899 | - |
|
| 900 | - /** |
|
| 901 | - * Checks if the submitter has a billing email. |
|
| 902 | - * |
|
| 903 | - * @since 1.0.19 |
|
| 904 | - */ |
|
| 905 | - public function has_billing_email() { |
|
| 906 | - $billing_email = $this->get_billing_email(); |
|
| 907 | - return ! empty( $billing_email ) && is_email( $billing_email ); |
|
| 908 | - } |
|
| 909 | - |
|
| 910 | - /** |
|
| 911 | - * Returns the appropriate currency for the submission. |
|
| 912 | - * |
|
| 913 | - * @since 1.0.19 |
|
| 914 | - * @return string |
|
| 915 | - */ |
|
| 916 | - public function get_currency() { |
|
| 917 | - return $this->has_invoice() ? $this->invoice->get_currency() : wpinv_get_currency(); |
|
| 918 | - } |
|
| 919 | - |
|
| 920 | - /** |
|
| 921 | - * Returns the raw submission data. |
|
| 922 | - * |
|
| 923 | - * @since 1.0.19 |
|
| 924 | - * @return array |
|
| 925 | - */ |
|
| 926 | - public function get_data() { |
|
| 927 | - return $this->data; |
|
| 928 | - } |
|
| 929 | - |
|
| 930 | - /** |
|
| 931 | - * Returns a field from the submission data |
|
| 932 | - * |
|
| 933 | - * @param string $field |
|
| 934 | - * @since 1.0.19 |
|
| 935 | - * @return mixed|null |
|
| 936 | - */ |
|
| 937 | - public function get_field( $field, $sub_array_key = null ) { |
|
| 938 | - return getpaid_get_array_field( $this->data, $field, $sub_array_key ); |
|
| 939 | - } |
|
| 940 | - |
|
| 941 | - /** |
|
| 942 | - * Checks if a required field is set. |
|
| 943 | - * |
|
| 944 | - * @since 1.0.19 |
|
| 945 | - */ |
|
| 946 | - public function is_required_field_set( $field ) { |
|
| 947 | - return empty( $field['required'] ) || ! empty( $this->data[ $field['id'] ] ); |
|
| 948 | - } |
|
| 949 | - |
|
| 950 | - /** |
|
| 951 | - * Formats an amount |
|
| 952 | - * |
|
| 953 | - * @since 1.0.19 |
|
| 954 | - */ |
|
| 955 | - public function format_amount( $amount ) { |
|
| 956 | - return wpinv_price( $amount, $this->get_currency() ); |
|
| 957 | - } |
|
| 816 | + /** |
|
| 817 | + * Returns the shipping amount. |
|
| 818 | + * |
|
| 819 | + * @since 1.0.19 |
|
| 820 | + */ |
|
| 821 | + public function get_shipping() { |
|
| 822 | + return $this->totals['shipping']['initial']; |
|
| 823 | + } |
|
| 824 | + |
|
| 825 | + /** |
|
| 826 | + * Returns the recurring shipping. |
|
| 827 | + * |
|
| 828 | + * @since 1.0.19 |
|
| 829 | + */ |
|
| 830 | + public function get_recurring_shipping() { |
|
| 831 | + return $this->totals['shipping']['recurring']; |
|
| 832 | + } |
|
| 833 | + |
|
| 834 | + /** |
|
| 835 | + * Checks if there are any shipping fees for the form. |
|
| 836 | + * |
|
| 837 | + * @return bool |
|
| 838 | + * @since 1.0.19 |
|
| 839 | + */ |
|
| 840 | + public function has_shipping() { |
|
| 841 | + return apply_filters( 'getpaid_payment_form_has_shipping', false, $this ); |
|
| 842 | + } |
|
| 843 | + |
|
| 844 | + /** |
|
| 845 | + * Checks if this is the initial fetch. |
|
| 846 | + * |
|
| 847 | + * @return bool |
|
| 848 | + * @since 1.0.19 |
|
| 849 | + */ |
|
| 850 | + public function is_initial_fetch() { |
|
| 851 | + return isset( $this->data['initial_state'] ) && empty( $this->data['initial_state'] ); |
|
| 852 | + } |
|
| 853 | + |
|
| 854 | + /** |
|
| 855 | + * Returns the total amount to collect for this submission. |
|
| 856 | + * |
|
| 857 | + * @since 1.0.19 |
|
| 858 | + */ |
|
| 859 | + public function get_total() { |
|
| 860 | + $total = $this->get_subtotal() + $this->get_fee() + $this->get_tax() + $this->get_shipping() - $this->get_discount(); |
|
| 861 | + return max( $total, 0 ); |
|
| 862 | + } |
|
| 863 | + |
|
| 864 | + /** |
|
| 865 | + * Returns the recurring total amount to collect for this submission. |
|
| 866 | + * |
|
| 867 | + * @since 1.0.19 |
|
| 868 | + */ |
|
| 869 | + public function get_recurring_total() { |
|
| 870 | + $total = $this->get_recurring_subtotal() + $this->get_recurring_fee() + $this->get_recurring_tax() + $this->get_recurring_shipping() - $this->get_recurring_discount(); |
|
| 871 | + return max( $total, 0 ); |
|
| 872 | + } |
|
| 873 | + |
|
| 874 | + /** |
|
| 875 | + * Whether payment details should be collected for this submission. |
|
| 876 | + * |
|
| 877 | + * @since 1.0.19 |
|
| 878 | + */ |
|
| 879 | + public function should_collect_payment_details() { |
|
| 880 | + $initial = $this->get_total(); |
|
| 881 | + $recurring = $this->get_recurring_total(); |
|
| 882 | + |
|
| 883 | + if ( $this->has_recurring == 0 ) { |
|
| 884 | + $recurring = 0; |
|
| 885 | + } |
|
| 886 | + |
|
| 887 | + $collect = $initial > 0 || $recurring > 0; |
|
| 888 | + return apply_filters( 'getpaid_submission_should_collect_payment_details', $collect, $this ); |
|
| 889 | + } |
|
| 890 | + |
|
| 891 | + /** |
|
| 892 | + * Returns the billing email of the user. |
|
| 893 | + * |
|
| 894 | + * @since 1.0.19 |
|
| 895 | + */ |
|
| 896 | + public function get_billing_email() { |
|
| 897 | + return apply_filters( 'getpaid_get_submission_billing_email', $this->get_field( 'billing_email' ), $this ); |
|
| 898 | + } |
|
| 899 | + |
|
| 900 | + /** |
|
| 901 | + * Checks if the submitter has a billing email. |
|
| 902 | + * |
|
| 903 | + * @since 1.0.19 |
|
| 904 | + */ |
|
| 905 | + public function has_billing_email() { |
|
| 906 | + $billing_email = $this->get_billing_email(); |
|
| 907 | + return ! empty( $billing_email ) && is_email( $billing_email ); |
|
| 908 | + } |
|
| 909 | + |
|
| 910 | + /** |
|
| 911 | + * Returns the appropriate currency for the submission. |
|
| 912 | + * |
|
| 913 | + * @since 1.0.19 |
|
| 914 | + * @return string |
|
| 915 | + */ |
|
| 916 | + public function get_currency() { |
|
| 917 | + return $this->has_invoice() ? $this->invoice->get_currency() : wpinv_get_currency(); |
|
| 918 | + } |
|
| 919 | + |
|
| 920 | + /** |
|
| 921 | + * Returns the raw submission data. |
|
| 922 | + * |
|
| 923 | + * @since 1.0.19 |
|
| 924 | + * @return array |
|
| 925 | + */ |
|
| 926 | + public function get_data() { |
|
| 927 | + return $this->data; |
|
| 928 | + } |
|
| 929 | + |
|
| 930 | + /** |
|
| 931 | + * Returns a field from the submission data |
|
| 932 | + * |
|
| 933 | + * @param string $field |
|
| 934 | + * @since 1.0.19 |
|
| 935 | + * @return mixed|null |
|
| 936 | + */ |
|
| 937 | + public function get_field( $field, $sub_array_key = null ) { |
|
| 938 | + return getpaid_get_array_field( $this->data, $field, $sub_array_key ); |
|
| 939 | + } |
|
| 940 | + |
|
| 941 | + /** |
|
| 942 | + * Checks if a required field is set. |
|
| 943 | + * |
|
| 944 | + * @since 1.0.19 |
|
| 945 | + */ |
|
| 946 | + public function is_required_field_set( $field ) { |
|
| 947 | + return empty( $field['required'] ) || ! empty( $this->data[ $field['id'] ] ); |
|
| 948 | + } |
|
| 949 | + |
|
| 950 | + /** |
|
| 951 | + * Formats an amount |
|
| 952 | + * |
|
| 953 | + * @since 1.0.19 |
|
| 954 | + */ |
|
| 955 | + public function format_amount( $amount ) { |
|
| 956 | + return wpinv_price( $amount, $this->get_currency() ); |
|
| 957 | + } |
|
| 958 | 958 | |
| 959 | 959 | } |
@@ -52,27 +52,27 @@ |
||
| 52 | 52 | |
| 53 | 53 | // Total Fee. |
| 54 | 54 | if ( 'fee' === $key ) { |
| 55 | - wpinv_the_price( $invoice->get_total_fees(), $invoice->get_currency() ); |
|
| 55 | + wpinv_the_price( $invoice->get_total_fees(), $invoice->get_currency() ); |
|
| 56 | 56 | } |
| 57 | 57 | |
| 58 | 58 | // Total discount. |
| 59 | 59 | if ( 'discount' === $key ) { |
| 60 | - wpinv_the_price( $invoice->get_total_discount(), $invoice->get_currency() ); |
|
| 60 | + wpinv_the_price( $invoice->get_total_discount(), $invoice->get_currency() ); |
|
| 61 | 61 | } |
| 62 | 62 | |
| 63 | 63 | // Shipping. |
| 64 | 64 | if ( 'shipping' === $key ) { |
| 65 | - wpinv_the_price( $invoice->get_shipping(), $invoice->get_currency() ); |
|
| 65 | + wpinv_the_price( $invoice->get_shipping(), $invoice->get_currency() ); |
|
| 66 | 66 | } |
| 67 | 67 | |
| 68 | 68 | // Sub total. |
| 69 | 69 | if ( 'subtotal' === $key ) { |
| 70 | - wpinv_the_price( $invoice->get_subtotal(), $invoice->get_currency() ); |
|
| 70 | + wpinv_the_price( $invoice->get_subtotal(), $invoice->get_currency() ); |
|
| 71 | 71 | } |
| 72 | 72 | |
| 73 | 73 | // Total. |
| 74 | 74 | if ( 'total' === $key ) { |
| 75 | - wpinv_the_price( $invoice->get_total(), $invoice->get_currency() ); |
|
| 75 | + wpinv_the_price( $invoice->get_total(), $invoice->get_currency() ); |
|
| 76 | 76 | } |
| 77 | 77 | |
| 78 | 78 | // Fires when printing a cart total. |
@@ -12,294 +12,294 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class GetPaid_Reports_Helper { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * Get report totals such as invoice totals and discount amounts. |
|
| 17 | - * |
|
| 18 | - * Data example: |
|
| 19 | - * |
|
| 20 | - * 'subtotal' => array( |
|
| 21 | - * 'type' => 'invoice_data', |
|
| 22 | - * 'function' => 'SUM', |
|
| 23 | - * 'name' => 'subtotal' |
|
| 24 | - * ) |
|
| 25 | - * |
|
| 26 | - * @param array $args |
|
| 27 | - * @return mixed depending on query_type |
|
| 28 | - */ |
|
| 29 | - public static function get_invoice_report_data( $args = array() ) { |
|
| 30 | - global $wpdb; |
|
| 31 | - |
|
| 32 | - $default_args = array( |
|
| 33 | - 'data' => array(), // The data to retrieve. |
|
| 34 | - 'where' => array(), // An array of where queries. |
|
| 35 | - 'query_type' => 'get_row', // wpdb query to run. |
|
| 36 | - 'group_by' => '', // What to group results by. |
|
| 37 | - 'order_by' => '', // What to order by. |
|
| 38 | - 'limit' => '', // Results limit. |
|
| 39 | - 'filter_range' => array(), // An array of before and after dates to limit results by. |
|
| 40 | - 'invoice_types' => array( 'wpi_invoice' ), // An array of post types to retrieve. |
|
| 41 | - 'invoice_status' => array( 'publish', 'wpi-processing', 'wpi-onhold' ), |
|
| 42 | - 'parent_invoice_status' => false, // Optionally filter by parent invoice status. |
|
| 43 | - ); |
|
| 44 | - |
|
| 45 | - $args = apply_filters( 'getpaid_reports_get_invoice_report_data_args', $args ); |
|
| 46 | - $args = wp_parse_args( $args, $default_args ); |
|
| 47 | - |
|
| 48 | - extract( $args ); |
|
| 49 | - |
|
| 50 | - if ( empty( $data ) ) { |
|
| 51 | - return ''; |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - $query = array(); |
|
| 55 | - $query['select'] = 'SELECT ' . implode( ',', self::prepare_invoice_data( $data ) ); |
|
| 56 | - $query['from'] = "FROM {$wpdb->posts} AS posts"; |
|
| 57 | - $query['join'] = implode( ' ', self::prepare_invoice_joins( $data + $where, ! empty( $parent_invoice_status ) ) ); |
|
| 58 | - |
|
| 59 | - $query['where'] = " |
|
| 15 | + /** |
|
| 16 | + * Get report totals such as invoice totals and discount amounts. |
|
| 17 | + * |
|
| 18 | + * Data example: |
|
| 19 | + * |
|
| 20 | + * 'subtotal' => array( |
|
| 21 | + * 'type' => 'invoice_data', |
|
| 22 | + * 'function' => 'SUM', |
|
| 23 | + * 'name' => 'subtotal' |
|
| 24 | + * ) |
|
| 25 | + * |
|
| 26 | + * @param array $args |
|
| 27 | + * @return mixed depending on query_type |
|
| 28 | + */ |
|
| 29 | + public static function get_invoice_report_data( $args = array() ) { |
|
| 30 | + global $wpdb; |
|
| 31 | + |
|
| 32 | + $default_args = array( |
|
| 33 | + 'data' => array(), // The data to retrieve. |
|
| 34 | + 'where' => array(), // An array of where queries. |
|
| 35 | + 'query_type' => 'get_row', // wpdb query to run. |
|
| 36 | + 'group_by' => '', // What to group results by. |
|
| 37 | + 'order_by' => '', // What to order by. |
|
| 38 | + 'limit' => '', // Results limit. |
|
| 39 | + 'filter_range' => array(), // An array of before and after dates to limit results by. |
|
| 40 | + 'invoice_types' => array( 'wpi_invoice' ), // An array of post types to retrieve. |
|
| 41 | + 'invoice_status' => array( 'publish', 'wpi-processing', 'wpi-onhold' ), |
|
| 42 | + 'parent_invoice_status' => false, // Optionally filter by parent invoice status. |
|
| 43 | + ); |
|
| 44 | + |
|
| 45 | + $args = apply_filters( 'getpaid_reports_get_invoice_report_data_args', $args ); |
|
| 46 | + $args = wp_parse_args( $args, $default_args ); |
|
| 47 | + |
|
| 48 | + extract( $args ); |
|
| 49 | + |
|
| 50 | + if ( empty( $data ) ) { |
|
| 51 | + return ''; |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + $query = array(); |
|
| 55 | + $query['select'] = 'SELECT ' . implode( ',', self::prepare_invoice_data( $data ) ); |
|
| 56 | + $query['from'] = "FROM {$wpdb->posts} AS posts"; |
|
| 57 | + $query['join'] = implode( ' ', self::prepare_invoice_joins( $data + $where, ! empty( $parent_invoice_status ) ) ); |
|
| 58 | + |
|
| 59 | + $query['where'] = " |
|
| 60 | 60 | WHERE posts.post_type IN ( '" . implode( "','", $invoice_types ) . "' ) |
| 61 | 61 | "; |
| 62 | 62 | |
| 63 | - if ( ! empty( $invoice_status ) ) { |
|
| 64 | - $query['where'] .= " |
|
| 63 | + if ( ! empty( $invoice_status ) ) { |
|
| 64 | + $query['where'] .= " |
|
| 65 | 65 | AND posts.post_status IN ( '" . implode( "','", $invoice_status ) . "' ) |
| 66 | 66 | "; |
| 67 | - } |
|
| 68 | - |
|
| 69 | - if ( ! empty( $parent_invoice_status ) ) { |
|
| 70 | - if ( ! empty( $invoice_status ) ) { |
|
| 71 | - $query['where'] .= " AND ( parent.post_status IN ( '" . implode( "','", $parent_invoice_status ) . "' ) OR parent.ID IS NULL ) "; |
|
| 72 | - } else { |
|
| 73 | - $query['where'] .= " AND parent.post_status IN ( '" . implode( "','", $parent_invoice_status ) . "' ) "; |
|
| 74 | - } |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - if ( ! empty( $filter_range['before'] ) ) { |
|
| 78 | - $query['where'] .= " |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + if ( ! empty( $parent_invoice_status ) ) { |
|
| 70 | + if ( ! empty( $invoice_status ) ) { |
|
| 71 | + $query['where'] .= " AND ( parent.post_status IN ( '" . implode( "','", $parent_invoice_status ) . "' ) OR parent.ID IS NULL ) "; |
|
| 72 | + } else { |
|
| 73 | + $query['where'] .= " AND parent.post_status IN ( '" . implode( "','", $parent_invoice_status ) . "' ) "; |
|
| 74 | + } |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + if ( ! empty( $filter_range['before'] ) ) { |
|
| 78 | + $query['where'] .= " |
|
| 79 | 79 | AND posts.post_date <= '" . gmdate( 'Y-m-d 23:59:59', strtotime( $filter_range['before'] ) ) . "' |
| 80 | 80 | "; |
| 81 | - } |
|
| 81 | + } |
|
| 82 | 82 | |
| 83 | - if ( ! empty( $filter_range['after'] ) ) { |
|
| 84 | - $query['where'] .= " |
|
| 83 | + if ( ! empty( $filter_range['after'] ) ) { |
|
| 84 | + $query['where'] .= " |
|
| 85 | 85 | AND posts.post_date >= '" . gmdate( 'Y-m-d 00:00:00', strtotime( $filter_range['after'] ) ) . "' |
| 86 | 86 | "; |
| 87 | - } |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | - if ( ! empty( $where ) ) { |
|
| 89 | + if ( ! empty( $where ) ) { |
|
| 90 | 90 | |
| 91 | - foreach ( $where as $value ) { |
|
| 91 | + foreach ( $where as $value ) { |
|
| 92 | 92 | |
| 93 | - if ( strtolower( $value['operator'] ) === 'in' || strtolower( $value['operator'] ) === 'not in' ) { |
|
| 94 | - |
|
| 95 | - if ( is_array( $value['value'] ) ) { |
|
| 96 | - $value['value'] = implode( "','", $value['value'] ); |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - if ( ! empty( $value['value'] ) ) { |
|
| 100 | - $where_value = "{$value['operator']} ('{$value['value']}')"; |
|
| 101 | - } |
|
| 102 | - } else { |
|
| 103 | - $where_value = "{$value['operator']} '{$value['value']}'"; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - if ( ! empty( $where_value ) ) { |
|
| 107 | - $query['where'] .= " AND {$value['key']} {$where_value}"; |
|
| 108 | - } |
|
| 109 | - } |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - if ( $group_by ) { |
|
| 113 | - $query['group_by'] = "GROUP BY {$group_by}"; |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - if ( $order_by ) { |
|
| 117 | - $query['order_by'] = "ORDER BY {$order_by}"; |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - if ( $limit ) { |
|
| 121 | - $query['limit'] = "LIMIT {$limit}"; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - $query = apply_filters( 'getpaid_reports_get_invoice_report_query', $query, $data ); |
|
| 125 | - $query = implode( ' ', $query ); |
|
| 126 | - |
|
| 127 | - return self::execute( $query_type, $query ); |
|
| 128 | - |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * Prepares the data to select. |
|
| 133 | - * |
|
| 134 | - * |
|
| 135 | - * @param array $data |
|
| 136 | - * @return array |
|
| 137 | - */ |
|
| 138 | - public static function prepare_invoice_data( $data ) { |
|
| 139 | - |
|
| 140 | - $prepared = array(); |
|
| 141 | - |
|
| 142 | - foreach ( $data as $raw_key => $value ) { |
|
| 143 | - $key = sanitize_key( $raw_key ); |
|
| 144 | - $distinct = ''; |
|
| 145 | - |
|
| 146 | - if ( isset( $value['distinct'] ) ) { |
|
| 147 | - $distinct = 'DISTINCT'; |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - $get_key = self::get_invoice_table_key( $key, $value['type'] ); |
|
| 151 | - |
|
| 152 | - if ( false === $get_key ) { |
|
| 153 | - // Skip to the next foreach iteration else the query will be invalid. |
|
| 154 | - continue; |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - if ( ! empty( $value['function'] ) ) { |
|
| 158 | - $get = "{$value['function']}({$distinct} {$get_key})"; |
|
| 159 | - } else { |
|
| 160 | - $get = "{$distinct} {$get_key}"; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - $prepared[] = "{$get} as {$value['name']}"; |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - return $prepared; |
|
| 167 | - |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * Prepares the joins to use. |
|
| 172 | - * |
|
| 173 | - * |
|
| 174 | - * @param array $data |
|
| 175 | - * @param bool $with_parent |
|
| 176 | - * @return array |
|
| 177 | - */ |
|
| 178 | - public static function prepare_invoice_joins( $data, $with_parent ) { |
|
| 179 | - global $wpdb; |
|
| 180 | - |
|
| 181 | - $prepared = array(); |
|
| 182 | - |
|
| 183 | - foreach ( $data as $raw_key => $value ) { |
|
| 184 | - $join_type = isset( $value['join_type'] ) ? $value['join_type'] : 'INNER'; |
|
| 185 | - $type = isset( $value['type'] ) ? $value['type'] : false; |
|
| 186 | - $key = sanitize_key( $raw_key ); |
|
| 187 | - |
|
| 188 | - switch ( $type ) { |
|
| 189 | - case 'meta': |
|
| 190 | - $prepared[ "meta_{$key}" ] = "{$join_type} JOIN {$wpdb->postmeta} AS meta_{$key} ON ( posts.ID = meta_{$key}.post_id AND meta_{$key}.meta_key = '{$raw_key}' )"; |
|
| 191 | - break; |
|
| 192 | - case 'parent_meta': |
|
| 193 | - $prepared[ "parent_meta_{$key}" ] = "{$join_type} JOIN {$wpdb->postmeta} AS parent_meta_{$key} ON (posts.post_parent = parent_meta_{$key}.post_id) AND (parent_meta_{$key}.meta_key = '{$raw_key}')"; |
|
| 194 | - break; |
|
| 195 | - case 'invoice_data': |
|
| 196 | - $prepared['invoices'] = "{$join_type} JOIN {$wpdb->prefix}getpaid_invoices AS invoices ON posts.ID = invoices.post_id"; |
|
| 197 | - break; |
|
| 198 | - case 'invoice_item': |
|
| 199 | - $prepared['invoice_items'] = "{$join_type} JOIN {$wpdb->prefix}getpaid_invoice_items AS invoice_items ON posts.ID = invoice_items.post_id"; |
|
| 200 | - break; |
|
| 201 | - } |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - if ( $with_parent ) { |
|
| 205 | - $prepared['parent'] = "LEFT JOIN {$wpdb->posts} AS parent ON posts.post_parent = parent.ID"; |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - return $prepared; |
|
| 209 | - |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - /** |
|
| 213 | - * Retrieves the appropriate table key to use. |
|
| 214 | - * |
|
| 215 | - * |
|
| 216 | - * @param string $key |
|
| 217 | - * @param string $table |
|
| 218 | - * @return string|false |
|
| 219 | - */ |
|
| 220 | - public static function get_invoice_table_key( $key, $table ) { |
|
| 221 | - |
|
| 222 | - $keys = array( |
|
| 223 | - 'meta' => "meta_{$key}.meta_value", |
|
| 224 | - 'parent_meta' => "parent_meta_{$key}.meta_value", |
|
| 225 | - 'post_data' => "posts.{$key}", |
|
| 226 | - 'invoice_data' => "invoices.{$key}", |
|
| 227 | - 'invoice_item' => "invoice_items.{$key}", |
|
| 228 | - ); |
|
| 229 | - |
|
| 230 | - return isset( $keys[ $table ] ) ? $keys[ $table ] : false; |
|
| 231 | - |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - /** |
|
| 235 | - * Executes a query and caches the result for a minute. |
|
| 236 | - * |
|
| 237 | - * |
|
| 238 | - * @param string $query_type |
|
| 239 | - * @param string $query |
|
| 240 | - * @return mixed depending on query_type |
|
| 241 | - */ |
|
| 242 | - public static function execute( $query_type, $query ) { |
|
| 243 | - global $wpdb; |
|
| 244 | - |
|
| 245 | - $query_hash = md5( $query_type . $query ); |
|
| 246 | - $result = self::get_cached_query( $query_hash ); |
|
| 247 | - if ( $result === false ) { |
|
| 248 | - self::enable_big_selects(); |
|
| 249 | - |
|
| 250 | - $result = $wpdb->$query_type( $query ); |
|
| 251 | - self::set_cached_query( $query_hash, $result ); |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - return $result; |
|
| 255 | - |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - /** |
|
| 259 | - * Enables big mysql selects for reports, just once for this session. |
|
| 260 | - */ |
|
| 261 | - protected static function enable_big_selects() { |
|
| 262 | - static $big_selects = false; |
|
| 263 | - |
|
| 264 | - global $wpdb; |
|
| 265 | - |
|
| 266 | - if ( ! $big_selects ) { |
|
| 267 | - $wpdb->query( 'SET SESSION SQL_BIG_SELECTS=1' ); |
|
| 268 | - $big_selects = true; |
|
| 269 | - } |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - /** |
|
| 273 | - * Get the cached query result or null if it's not in the cache. |
|
| 274 | - * |
|
| 275 | - * @param string $query_hash The query hash. |
|
| 276 | - * |
|
| 277 | - * @return mixed|false The cache contents on success, false on failure to retrieve contents. |
|
| 278 | - */ |
|
| 279 | - protected static function get_cached_query( $query_hash ) { |
|
| 280 | - |
|
| 281 | - return wp_cache_get( |
|
| 282 | - $query_hash, |
|
| 283 | - strtolower( __CLASS__ ) |
|
| 284 | - ); |
|
| 285 | - |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - /** |
|
| 289 | - * Set the cached query result. |
|
| 290 | - * |
|
| 291 | - * @param string $query_hash The query hash. |
|
| 292 | - * @param mixed $data The data to cache. |
|
| 293 | - */ |
|
| 294 | - protected static function set_cached_query( $query_hash, $data ) { |
|
| 295 | - |
|
| 296 | - wp_cache_set( |
|
| 297 | - $query_hash, |
|
| 298 | - $data, |
|
| 299 | - strtolower( __CLASS__ ), |
|
| 300 | - MINUTE_IN_SECONDS |
|
| 301 | - ); |
|
| 302 | - |
|
| 303 | - } |
|
| 93 | + if ( strtolower( $value['operator'] ) === 'in' || strtolower( $value['operator'] ) === 'not in' ) { |
|
| 94 | + |
|
| 95 | + if ( is_array( $value['value'] ) ) { |
|
| 96 | + $value['value'] = implode( "','", $value['value'] ); |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + if ( ! empty( $value['value'] ) ) { |
|
| 100 | + $where_value = "{$value['operator']} ('{$value['value']}')"; |
|
| 101 | + } |
|
| 102 | + } else { |
|
| 103 | + $where_value = "{$value['operator']} '{$value['value']}'"; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + if ( ! empty( $where_value ) ) { |
|
| 107 | + $query['where'] .= " AND {$value['key']} {$where_value}"; |
|
| 108 | + } |
|
| 109 | + } |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + if ( $group_by ) { |
|
| 113 | + $query['group_by'] = "GROUP BY {$group_by}"; |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + if ( $order_by ) { |
|
| 117 | + $query['order_by'] = "ORDER BY {$order_by}"; |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + if ( $limit ) { |
|
| 121 | + $query['limit'] = "LIMIT {$limit}"; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + $query = apply_filters( 'getpaid_reports_get_invoice_report_query', $query, $data ); |
|
| 125 | + $query = implode( ' ', $query ); |
|
| 126 | + |
|
| 127 | + return self::execute( $query_type, $query ); |
|
| 128 | + |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * Prepares the data to select. |
|
| 133 | + * |
|
| 134 | + * |
|
| 135 | + * @param array $data |
|
| 136 | + * @return array |
|
| 137 | + */ |
|
| 138 | + public static function prepare_invoice_data( $data ) { |
|
| 139 | + |
|
| 140 | + $prepared = array(); |
|
| 141 | + |
|
| 142 | + foreach ( $data as $raw_key => $value ) { |
|
| 143 | + $key = sanitize_key( $raw_key ); |
|
| 144 | + $distinct = ''; |
|
| 145 | + |
|
| 146 | + if ( isset( $value['distinct'] ) ) { |
|
| 147 | + $distinct = 'DISTINCT'; |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + $get_key = self::get_invoice_table_key( $key, $value['type'] ); |
|
| 151 | + |
|
| 152 | + if ( false === $get_key ) { |
|
| 153 | + // Skip to the next foreach iteration else the query will be invalid. |
|
| 154 | + continue; |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + if ( ! empty( $value['function'] ) ) { |
|
| 158 | + $get = "{$value['function']}({$distinct} {$get_key})"; |
|
| 159 | + } else { |
|
| 160 | + $get = "{$distinct} {$get_key}"; |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + $prepared[] = "{$get} as {$value['name']}"; |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + return $prepared; |
|
| 167 | + |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * Prepares the joins to use. |
|
| 172 | + * |
|
| 173 | + * |
|
| 174 | + * @param array $data |
|
| 175 | + * @param bool $with_parent |
|
| 176 | + * @return array |
|
| 177 | + */ |
|
| 178 | + public static function prepare_invoice_joins( $data, $with_parent ) { |
|
| 179 | + global $wpdb; |
|
| 180 | + |
|
| 181 | + $prepared = array(); |
|
| 182 | + |
|
| 183 | + foreach ( $data as $raw_key => $value ) { |
|
| 184 | + $join_type = isset( $value['join_type'] ) ? $value['join_type'] : 'INNER'; |
|
| 185 | + $type = isset( $value['type'] ) ? $value['type'] : false; |
|
| 186 | + $key = sanitize_key( $raw_key ); |
|
| 187 | + |
|
| 188 | + switch ( $type ) { |
|
| 189 | + case 'meta': |
|
| 190 | + $prepared[ "meta_{$key}" ] = "{$join_type} JOIN {$wpdb->postmeta} AS meta_{$key} ON ( posts.ID = meta_{$key}.post_id AND meta_{$key}.meta_key = '{$raw_key}' )"; |
|
| 191 | + break; |
|
| 192 | + case 'parent_meta': |
|
| 193 | + $prepared[ "parent_meta_{$key}" ] = "{$join_type} JOIN {$wpdb->postmeta} AS parent_meta_{$key} ON (posts.post_parent = parent_meta_{$key}.post_id) AND (parent_meta_{$key}.meta_key = '{$raw_key}')"; |
|
| 194 | + break; |
|
| 195 | + case 'invoice_data': |
|
| 196 | + $prepared['invoices'] = "{$join_type} JOIN {$wpdb->prefix}getpaid_invoices AS invoices ON posts.ID = invoices.post_id"; |
|
| 197 | + break; |
|
| 198 | + case 'invoice_item': |
|
| 199 | + $prepared['invoice_items'] = "{$join_type} JOIN {$wpdb->prefix}getpaid_invoice_items AS invoice_items ON posts.ID = invoice_items.post_id"; |
|
| 200 | + break; |
|
| 201 | + } |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + if ( $with_parent ) { |
|
| 205 | + $prepared['parent'] = "LEFT JOIN {$wpdb->posts} AS parent ON posts.post_parent = parent.ID"; |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + return $prepared; |
|
| 209 | + |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + /** |
|
| 213 | + * Retrieves the appropriate table key to use. |
|
| 214 | + * |
|
| 215 | + * |
|
| 216 | + * @param string $key |
|
| 217 | + * @param string $table |
|
| 218 | + * @return string|false |
|
| 219 | + */ |
|
| 220 | + public static function get_invoice_table_key( $key, $table ) { |
|
| 221 | + |
|
| 222 | + $keys = array( |
|
| 223 | + 'meta' => "meta_{$key}.meta_value", |
|
| 224 | + 'parent_meta' => "parent_meta_{$key}.meta_value", |
|
| 225 | + 'post_data' => "posts.{$key}", |
|
| 226 | + 'invoice_data' => "invoices.{$key}", |
|
| 227 | + 'invoice_item' => "invoice_items.{$key}", |
|
| 228 | + ); |
|
| 229 | + |
|
| 230 | + return isset( $keys[ $table ] ) ? $keys[ $table ] : false; |
|
| 231 | + |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + /** |
|
| 235 | + * Executes a query and caches the result for a minute. |
|
| 236 | + * |
|
| 237 | + * |
|
| 238 | + * @param string $query_type |
|
| 239 | + * @param string $query |
|
| 240 | + * @return mixed depending on query_type |
|
| 241 | + */ |
|
| 242 | + public static function execute( $query_type, $query ) { |
|
| 243 | + global $wpdb; |
|
| 244 | + |
|
| 245 | + $query_hash = md5( $query_type . $query ); |
|
| 246 | + $result = self::get_cached_query( $query_hash ); |
|
| 247 | + if ( $result === false ) { |
|
| 248 | + self::enable_big_selects(); |
|
| 249 | + |
|
| 250 | + $result = $wpdb->$query_type( $query ); |
|
| 251 | + self::set_cached_query( $query_hash, $result ); |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + return $result; |
|
| 255 | + |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + /** |
|
| 259 | + * Enables big mysql selects for reports, just once for this session. |
|
| 260 | + */ |
|
| 261 | + protected static function enable_big_selects() { |
|
| 262 | + static $big_selects = false; |
|
| 263 | + |
|
| 264 | + global $wpdb; |
|
| 265 | + |
|
| 266 | + if ( ! $big_selects ) { |
|
| 267 | + $wpdb->query( 'SET SESSION SQL_BIG_SELECTS=1' ); |
|
| 268 | + $big_selects = true; |
|
| 269 | + } |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + /** |
|
| 273 | + * Get the cached query result or null if it's not in the cache. |
|
| 274 | + * |
|
| 275 | + * @param string $query_hash The query hash. |
|
| 276 | + * |
|
| 277 | + * @return mixed|false The cache contents on success, false on failure to retrieve contents. |
|
| 278 | + */ |
|
| 279 | + protected static function get_cached_query( $query_hash ) { |
|
| 280 | + |
|
| 281 | + return wp_cache_get( |
|
| 282 | + $query_hash, |
|
| 283 | + strtolower( __CLASS__ ) |
|
| 284 | + ); |
|
| 285 | + |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + /** |
|
| 289 | + * Set the cached query result. |
|
| 290 | + * |
|
| 291 | + * @param string $query_hash The query hash. |
|
| 292 | + * @param mixed $data The data to cache. |
|
| 293 | + */ |
|
| 294 | + protected static function set_cached_query( $query_hash, $data ) { |
|
| 295 | + |
|
| 296 | + wp_cache_set( |
|
| 297 | + $query_hash, |
|
| 298 | + $data, |
|
| 299 | + strtolower( __CLASS__ ), |
|
| 300 | + MINUTE_IN_SECONDS |
|
| 301 | + ); |
|
| 302 | + |
|
| 303 | + } |
|
| 304 | 304 | |
| 305 | 305 | } |
@@ -39,62 +39,62 @@ discard block |
||
| 39 | 39 | <td style="width: 65%"> |
| 40 | 40 | <?php |
| 41 | 41 | |
| 42 | - switch ( $key ) { |
|
| 42 | + switch ( $key ) { |
|
| 43 | 43 | |
| 44 | - case 'status': |
|
| 45 | - echo esc_html( $subscription->get_status_label() ); |
|
| 46 | - break; |
|
| 44 | + case 'status': |
|
| 45 | + echo esc_html( $subscription->get_status_label() ); |
|
| 46 | + break; |
|
| 47 | 47 | |
| 48 | - case 'start_date': |
|
| 49 | - echo esc_html( getpaid_format_date_value( $subscription->get_date_created() ) ); |
|
| 50 | - break; |
|
| 48 | + case 'start_date': |
|
| 49 | + echo esc_html( getpaid_format_date_value( $subscription->get_date_created() ) ); |
|
| 50 | + break; |
|
| 51 | 51 | |
| 52 | - case 'expiry_date': |
|
| 53 | - echo esc_html( getpaid_format_date_value( $subscription->get_next_renewal_date() ) ); |
|
| 54 | - break; |
|
| 52 | + case 'expiry_date': |
|
| 53 | + echo esc_html( getpaid_format_date_value( $subscription->get_next_renewal_date() ) ); |
|
| 54 | + break; |
|
| 55 | 55 | |
| 56 | - case 'initial_amount': |
|
| 57 | - echo wp_kses_post( wpinv_price( $subscription->get_initial_amount(), $subscription->get_parent_payment()->get_currency() ) ); |
|
| 56 | + case 'initial_amount': |
|
| 57 | + echo wp_kses_post( wpinv_price( $subscription->get_initial_amount(), $subscription->get_parent_payment()->get_currency() ) ); |
|
| 58 | 58 | |
| 59 | - if ( $subscription->has_trial_period() ) { |
|
| 59 | + if ( $subscription->has_trial_period() ) { |
|
| 60 | 60 | |
| 61 | - echo "<small class='text-muted'> "; |
|
| 62 | - printf( |
|
| 63 | - esc_html_x( '( %1$s trial )', 'Subscription trial period. (e.g.: 1 month trial)', 'invoicing' ), |
|
| 64 | - esc_html( $subscription->get_trial_period() ) |
|
| 65 | - ); |
|
| 66 | - echo '</small>'; |
|
| 61 | + echo "<small class='text-muted'> "; |
|
| 62 | + printf( |
|
| 63 | + esc_html_x( '( %1$s trial )', 'Subscription trial period. (e.g.: 1 month trial)', 'invoicing' ), |
|
| 64 | + esc_html( $subscription->get_trial_period() ) |
|
| 65 | + ); |
|
| 66 | + echo '</small>'; |
|
| 67 | 67 | |
| 68 | - } |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - break; |
|
| 70 | + break; |
|
| 71 | 71 | |
| 72 | - case 'recurring_amount': |
|
| 73 | - $frequency = getpaid_get_subscription_period_label( $subscription->get_period(), $subscription->get_frequency(), '' ); |
|
| 74 | - $amount = wpinv_price( $subscription->get_recurring_amount(), $subscription->get_parent_payment()->get_currency() ); |
|
| 75 | - echo wp_kses_post( strtolower( "<strong style='font-weight: 500;'>$amount</strong> / <span class='getpaid-item-recurring-period'>$frequency</span>" ) ); |
|
| 76 | - break; |
|
| 72 | + case 'recurring_amount': |
|
| 73 | + $frequency = getpaid_get_subscription_period_label( $subscription->get_period(), $subscription->get_frequency(), '' ); |
|
| 74 | + $amount = wpinv_price( $subscription->get_recurring_amount(), $subscription->get_parent_payment()->get_currency() ); |
|
| 75 | + echo wp_kses_post( strtolower( "<strong style='font-weight: 500;'>$amount</strong> / <span class='getpaid-item-recurring-period'>$frequency</span>" ) ); |
|
| 76 | + break; |
|
| 77 | 77 | |
| 78 | - case 'item': |
|
| 79 | - if ( empty( $subscription_group ) ) { |
|
| 80 | - echo wp_kses_post( WPInv_Subscriptions_List_Table::generate_item_markup( $subscription->get_product_id() ) ); |
|
| 81 | - } else { |
|
| 82 | - $markup = array_map( array( 'WPInv_Subscriptions_List_Table', 'generate_item_markup' ), array_keys( $subscription_group['items'] ) ); |
|
| 83 | - echo wp_kses_post( implode( ' | ', $markup ) ); |
|
| 84 | - } |
|
| 78 | + case 'item': |
|
| 79 | + if ( empty( $subscription_group ) ) { |
|
| 80 | + echo wp_kses_post( WPInv_Subscriptions_List_Table::generate_item_markup( $subscription->get_product_id() ) ); |
|
| 81 | + } else { |
|
| 82 | + $markup = array_map( array( 'WPInv_Subscriptions_List_Table', 'generate_item_markup' ), array_keys( $subscription_group['items'] ) ); |
|
| 83 | + echo wp_kses_post( implode( ' | ', $markup ) ); |
|
| 84 | + } |
|
| 85 | 85 | |
| 86 | - break; |
|
| 86 | + break; |
|
| 87 | 87 | |
| 88 | - case 'payments': |
|
| 89 | - $max_activations = (int) $subscription->get_bill_times(); |
|
| 90 | - echo ( (int) $subscription->get_times_billed() ) . ' / ' . ( empty( $max_activations ) ? '∞' : (int) $max_activations ); |
|
| 88 | + case 'payments': |
|
| 89 | + $max_activations = (int) $subscription->get_bill_times(); |
|
| 90 | + echo ( (int) $subscription->get_times_billed() ) . ' / ' . ( empty( $max_activations ) ? '∞' : (int) $max_activations ); |
|
| 91 | 91 | |
| 92 | - break; |
|
| 92 | + break; |
|
| 93 | 93 | |
| 94 | - } |
|
| 95 | - do_action( "getpaid_render_single_subscription_column_$key", $subscription ); |
|
| 94 | + } |
|
| 95 | + do_action( "getpaid_render_single_subscription_column_$key", $subscription ); |
|
| 96 | 96 | |
| 97 | - ?> |
|
| 97 | + ?> |
|
| 98 | 98 | </td> |
| 99 | 99 | |
| 100 | 100 | </tr> |
@@ -121,17 +121,17 @@ discard block |
||
| 121 | 121 | <span class="form-text"> |
| 122 | 122 | |
| 123 | 123 | <?php |
| 124 | - if ( $subscription->can_cancel() ) { |
|
| 125 | - printf( |
|
| 124 | + if ( $subscription->can_cancel() ) { |
|
| 125 | + printf( |
|
| 126 | 126 | '<a href="%s" class="btn btn-danger btn-sm" onclick="return confirm(\'%s\')">%s</a> ', |
| 127 | 127 | esc_url( $subscription->get_cancel_url() ), |
| 128 | 128 | esc_attr__( 'Are you sure you want to cancel this subscription?', 'invoicing' ), |
| 129 | 129 | esc_html__( 'Cancel Subscription', 'invoicing' ) |
| 130 | 130 | ); |
| 131 | - } |
|
| 131 | + } |
|
| 132 | 132 | |
| 133 | - do_action( 'getpaid-single-subscription-page-actions', $subscription ); |
|
| 134 | - ?> |
|
| 133 | + do_action( 'getpaid-single-subscription-page-actions', $subscription ); |
|
| 134 | + ?> |
|
| 135 | 135 | |
| 136 | 136 | <a href="<?php echo esc_url( getpaid_get_tab_url( 'gp-subscriptions', get_permalink( (int) wpinv_get_option( 'invoice_subscription_page' ) ) ) ); ?>" class="btn btn-secondary btn-sm"><?php esc_html_e( 'Go Back', 'invoicing' ); ?></a> |
| 137 | 137 | </span> |
@@ -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 | /** |
@@ -16,85 +16,85 @@ discard block |
||
| 16 | 16 | */ |
| 17 | 17 | class GetPaid_Meta_Box_Invoice_Address { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * Output the metabox. |
|
| 21 | - * |
|
| 22 | - * @param WP_Post $post |
|
| 23 | - */ |
|
| 24 | - public static function output( $post ) { |
|
| 25 | - |
|
| 26 | - // Prepare the invoice. |
|
| 27 | - $invoice = new WPInv_Invoice( $post ); |
|
| 28 | - $customer = $invoice->exists() ? $invoice->get_user_id( 'edit' ) : get_current_user_id(); |
|
| 29 | - $customer = new WP_User( $customer ); |
|
| 30 | - $display = sprintf( _x( '%1$s (%2$s)', 'user dropdown', 'invoicing' ), $customer->display_name, $customer->user_email ); |
|
| 31 | - wp_nonce_field( 'getpaid_meta_nonce', 'getpaid_meta_nonce' ); |
|
| 32 | - |
|
| 33 | - // Address fields. |
|
| 34 | - $address_fields = array( |
|
| 35 | - 'first_name' => array( |
|
| 36 | - 'label' => __( 'First Name', 'invoicing' ), |
|
| 37 | - 'type' => 'text', |
|
| 38 | - ), |
|
| 39 | - 'last_name' => array( |
|
| 40 | - 'label' => __( 'Last Name', 'invoicing' ), |
|
| 41 | - 'type' => 'text', |
|
| 42 | - ), |
|
| 43 | - 'company' => array( |
|
| 44 | - 'label' => __( 'Company', 'invoicing' ), |
|
| 45 | - 'type' => 'text', |
|
| 46 | - 'class' => 'getpaid-recalculate-prices-on-change', |
|
| 47 | - ), |
|
| 48 | - 'vat_number' => array( |
|
| 49 | - 'label' => __( 'VAT Number', 'invoicing' ), |
|
| 50 | - 'type' => 'text', |
|
| 51 | - 'class' => 'getpaid-recalculate-prices-on-change', |
|
| 52 | - ), |
|
| 53 | - 'address' => array( |
|
| 54 | - 'label' => __( 'Address', 'invoicing' ), |
|
| 55 | - 'type' => 'text', |
|
| 56 | - ), |
|
| 57 | - 'city' => array( |
|
| 58 | - 'label' => __( 'City', 'invoicing' ), |
|
| 59 | - 'type' => 'text', |
|
| 60 | - ), |
|
| 61 | - 'country' => array( |
|
| 62 | - 'label' => __( 'Country', 'invoicing' ), |
|
| 63 | - 'type' => 'select', |
|
| 64 | - 'class' => 'getpaid-recalculate-prices-on-change', |
|
| 65 | - 'options' => wpinv_get_country_list(), |
|
| 66 | - 'placeholder' => __( 'Choose a country', 'invoicing' ), |
|
| 67 | - ), |
|
| 68 | - 'state' => array( |
|
| 69 | - 'label' => __( 'State', 'invoicing' ), |
|
| 70 | - 'type' => 'text', |
|
| 71 | - 'class' => 'getpaid-recalculate-prices-on-change', |
|
| 72 | - ), |
|
| 73 | - 'zip' => array( |
|
| 74 | - 'label' => __( 'Zip', 'invoicing' ), |
|
| 75 | - 'type' => 'text', |
|
| 76 | - ), |
|
| 77 | - 'phone' => array( |
|
| 78 | - 'label' => __( 'Phone', 'invoicing' ), |
|
| 79 | - 'type' => 'text', |
|
| 80 | - ), |
|
| 81 | - ); |
|
| 82 | - |
|
| 83 | - $states = wpinv_get_country_states( $invoice->get_country( 'edit' ) ); |
|
| 84 | - |
|
| 85 | - if ( ! empty( $states ) ) { |
|
| 86 | - $address_fields['state']['type'] = 'select'; |
|
| 87 | - $address_fields['state']['options'] = $states; |
|
| 88 | - $address_fields['state']['placeholder'] = __( 'Choose a state', 'invoicing' ); |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - // Maybe remove the VAT field. |
|
| 92 | - if ( ! wpinv_use_taxes() ) { |
|
| 93 | - unset( $address_fields['vat_number'] ); |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - $address_fields = apply_filters( 'getpaid_admin_edit_invoice_address_fields', $address_fields, $invoice ); |
|
| 97 | - ?> |
|
| 19 | + /** |
|
| 20 | + * Output the metabox. |
|
| 21 | + * |
|
| 22 | + * @param WP_Post $post |
|
| 23 | + */ |
|
| 24 | + public static function output( $post ) { |
|
| 25 | + |
|
| 26 | + // Prepare the invoice. |
|
| 27 | + $invoice = new WPInv_Invoice( $post ); |
|
| 28 | + $customer = $invoice->exists() ? $invoice->get_user_id( 'edit' ) : get_current_user_id(); |
|
| 29 | + $customer = new WP_User( $customer ); |
|
| 30 | + $display = sprintf( _x( '%1$s (%2$s)', 'user dropdown', 'invoicing' ), $customer->display_name, $customer->user_email ); |
|
| 31 | + wp_nonce_field( 'getpaid_meta_nonce', 'getpaid_meta_nonce' ); |
|
| 32 | + |
|
| 33 | + // Address fields. |
|
| 34 | + $address_fields = array( |
|
| 35 | + 'first_name' => array( |
|
| 36 | + 'label' => __( 'First Name', 'invoicing' ), |
|
| 37 | + 'type' => 'text', |
|
| 38 | + ), |
|
| 39 | + 'last_name' => array( |
|
| 40 | + 'label' => __( 'Last Name', 'invoicing' ), |
|
| 41 | + 'type' => 'text', |
|
| 42 | + ), |
|
| 43 | + 'company' => array( |
|
| 44 | + 'label' => __( 'Company', 'invoicing' ), |
|
| 45 | + 'type' => 'text', |
|
| 46 | + 'class' => 'getpaid-recalculate-prices-on-change', |
|
| 47 | + ), |
|
| 48 | + 'vat_number' => array( |
|
| 49 | + 'label' => __( 'VAT Number', 'invoicing' ), |
|
| 50 | + 'type' => 'text', |
|
| 51 | + 'class' => 'getpaid-recalculate-prices-on-change', |
|
| 52 | + ), |
|
| 53 | + 'address' => array( |
|
| 54 | + 'label' => __( 'Address', 'invoicing' ), |
|
| 55 | + 'type' => 'text', |
|
| 56 | + ), |
|
| 57 | + 'city' => array( |
|
| 58 | + 'label' => __( 'City', 'invoicing' ), |
|
| 59 | + 'type' => 'text', |
|
| 60 | + ), |
|
| 61 | + 'country' => array( |
|
| 62 | + 'label' => __( 'Country', 'invoicing' ), |
|
| 63 | + 'type' => 'select', |
|
| 64 | + 'class' => 'getpaid-recalculate-prices-on-change', |
|
| 65 | + 'options' => wpinv_get_country_list(), |
|
| 66 | + 'placeholder' => __( 'Choose a country', 'invoicing' ), |
|
| 67 | + ), |
|
| 68 | + 'state' => array( |
|
| 69 | + 'label' => __( 'State', 'invoicing' ), |
|
| 70 | + 'type' => 'text', |
|
| 71 | + 'class' => 'getpaid-recalculate-prices-on-change', |
|
| 72 | + ), |
|
| 73 | + 'zip' => array( |
|
| 74 | + 'label' => __( 'Zip', 'invoicing' ), |
|
| 75 | + 'type' => 'text', |
|
| 76 | + ), |
|
| 77 | + 'phone' => array( |
|
| 78 | + 'label' => __( 'Phone', 'invoicing' ), |
|
| 79 | + 'type' => 'text', |
|
| 80 | + ), |
|
| 81 | + ); |
|
| 82 | + |
|
| 83 | + $states = wpinv_get_country_states( $invoice->get_country( 'edit' ) ); |
|
| 84 | + |
|
| 85 | + if ( ! empty( $states ) ) { |
|
| 86 | + $address_fields['state']['type'] = 'select'; |
|
| 87 | + $address_fields['state']['options'] = $states; |
|
| 88 | + $address_fields['state']['placeholder'] = __( 'Choose a state', 'invoicing' ); |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + // Maybe remove the VAT field. |
|
| 92 | + if ( ! wpinv_use_taxes() ) { |
|
| 93 | + unset( $address_fields['vat_number'] ); |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + $address_fields = apply_filters( 'getpaid_admin_edit_invoice_address_fields', $address_fields, $invoice ); |
|
| 97 | + ?> |
|
| 98 | 98 | |
| 99 | 99 | <style> |
| 100 | 100 | #wpinv-address label { |
@@ -119,19 +119,19 @@ discard block |
||
| 119 | 119 | <div id="getpaid-invoice-email-wrapper" class="d-none"> |
| 120 | 120 | <input type="hidden" id="getpaid-invoice-create-new-user" name="wpinv_new_user" value="" /> |
| 121 | 121 | <?php |
| 122 | - aui()->input( |
|
| 123 | - array( |
|
| 124 | - 'type' => 'text', |
|
| 125 | - 'id' => 'getpaid-invoice-new-user-email', |
|
| 126 | - 'name' => 'wpinv_email', |
|
| 127 | - 'label' => __( 'Email', 'invoicing' ) . '<span class="required">*</span>', |
|
| 128 | - 'label_type' => 'vertical', |
|
| 129 | - 'placeholder' => '[email protected]', |
|
| 130 | - 'class' => 'form-control-sm', |
|
| 131 | - ), |
|
| 132 | - true |
|
| 133 | - ); |
|
| 134 | - ?> |
|
| 122 | + aui()->input( |
|
| 123 | + array( |
|
| 124 | + 'type' => 'text', |
|
| 125 | + 'id' => 'getpaid-invoice-new-user-email', |
|
| 126 | + 'name' => 'wpinv_email', |
|
| 127 | + 'label' => __( 'Email', 'invoicing' ) . '<span class="required">*</span>', |
|
| 128 | + 'label_type' => 'vertical', |
|
| 129 | + 'placeholder' => '[email protected]', |
|
| 130 | + 'class' => 'form-control-sm', |
|
| 131 | + ), |
|
| 132 | + true |
|
| 133 | + ); |
|
| 134 | + ?> |
|
| 135 | 135 | </div> |
| 136 | 136 | </div> |
| 137 | 137 | <div class="col-12 col-sm-6 form-group mb-3 mt-sm-4"> |
@@ -155,39 +155,39 @@ discard block |
||
| 155 | 155 | <div class="col-12 col-sm-6 getpaid-invoice-address-field__<?php echo esc_attr( $key ); ?>--wrapper"> |
| 156 | 156 | <?php |
| 157 | 157 | |
| 158 | - if ( 'select' === $field['type'] ) { |
|
| 159 | - aui()->select( |
|
| 160 | - array( |
|
| 161 | - 'id' => 'wpinv_' . $key, |
|
| 162 | - 'name' => 'wpinv_' . $key, |
|
| 163 | - 'label' => $field['label'], |
|
| 164 | - 'label_type' => 'vertical', |
|
| 165 | - 'placeholder' => isset( $field['placeholder'] ) ? $field['placeholder'] : '', |
|
| 166 | - 'class' => 'form-control-sm ' . ( isset( $field['class'] ) ? $field['class'] : '' ), |
|
| 167 | - 'value' => $invoice->get( $key, 'edit' ), |
|
| 168 | - 'options' => $field['options'], |
|
| 169 | - 'data-allow-clear' => 'false', |
|
| 170 | - 'select2' => true, |
|
| 171 | - ), |
|
| 172 | - true |
|
| 173 | - ); |
|
| 174 | - } else { |
|
| 175 | - aui()->input( |
|
| 176 | - array( |
|
| 177 | - 'type' => $field['type'], |
|
| 178 | - 'id' => 'wpinv_' . $key, |
|
| 179 | - 'name' => 'wpinv_' . $key, |
|
| 180 | - 'label' => $field['label'], |
|
| 181 | - 'label_type' => 'vertical', |
|
| 182 | - 'placeholder' => isset( $field['placeholder'] ) ? $field['placeholder'] : '', |
|
| 183 | - 'class' => 'form-control-sm ' . ( isset( $field['class'] ) ? $field['class'] : '' ), |
|
| 184 | - 'value' => $invoice->get( $key, 'edit' ), |
|
| 185 | - ), |
|
| 186 | - true |
|
| 187 | - ); |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - ?> |
|
| 158 | + if ( 'select' === $field['type'] ) { |
|
| 159 | + aui()->select( |
|
| 160 | + array( |
|
| 161 | + 'id' => 'wpinv_' . $key, |
|
| 162 | + 'name' => 'wpinv_' . $key, |
|
| 163 | + 'label' => $field['label'], |
|
| 164 | + 'label_type' => 'vertical', |
|
| 165 | + 'placeholder' => isset( $field['placeholder'] ) ? $field['placeholder'] : '', |
|
| 166 | + 'class' => 'form-control-sm ' . ( isset( $field['class'] ) ? $field['class'] : '' ), |
|
| 167 | + 'value' => $invoice->get( $key, 'edit' ), |
|
| 168 | + 'options' => $field['options'], |
|
| 169 | + 'data-allow-clear' => 'false', |
|
| 170 | + 'select2' => true, |
|
| 171 | + ), |
|
| 172 | + true |
|
| 173 | + ); |
|
| 174 | + } else { |
|
| 175 | + aui()->input( |
|
| 176 | + array( |
|
| 177 | + 'type' => $field['type'], |
|
| 178 | + 'id' => 'wpinv_' . $key, |
|
| 179 | + 'name' => 'wpinv_' . $key, |
|
| 180 | + 'label' => $field['label'], |
|
| 181 | + 'label_type' => 'vertical', |
|
| 182 | + 'placeholder' => isset( $field['placeholder'] ) ? $field['placeholder'] : '', |
|
| 183 | + 'class' => 'form-control-sm ' . ( isset( $field['class'] ) ? $field['class'] : '' ), |
|
| 184 | + 'value' => $invoice->get( $key, 'edit' ), |
|
| 185 | + ), |
|
| 186 | + true |
|
| 187 | + ); |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + ?> |
|
| 191 | 191 | </div> |
| 192 | 192 | <?php endforeach; ?> |
| 193 | 193 | </div> |
@@ -198,48 +198,48 @@ discard block |
||
| 198 | 198 | <div class="row"> |
| 199 | 199 | <div class="col-12 col-sm-6"> |
| 200 | 200 | <?php |
| 201 | - aui()->select( |
|
| 202 | - array( |
|
| 203 | - 'id' => 'wpinv_template', |
|
| 204 | - 'name' => 'wpinv_template', |
|
| 205 | - 'label' => __( 'Template', 'invoicing' ), |
|
| 206 | - 'label_type' => 'vertical', |
|
| 207 | - 'placeholder' => __( 'Choose a template', 'invoicing' ), |
|
| 208 | - 'class' => 'form-control-sm', |
|
| 209 | - 'value' => $invoice->get_template( 'edit' ), |
|
| 210 | - 'options' => array( |
|
| 211 | - 'quantity' => __( 'Quantity', 'invoicing' ), |
|
| 212 | - 'hours' => __( 'Hours', 'invoicing' ), |
|
| 213 | - ), |
|
| 214 | - 'data-allow-clear' => 'false', |
|
| 215 | - 'select2' => true, |
|
| 216 | - ), |
|
| 217 | - true |
|
| 218 | - ); |
|
| 219 | - ?> |
|
| 201 | + aui()->select( |
|
| 202 | + array( |
|
| 203 | + 'id' => 'wpinv_template', |
|
| 204 | + 'name' => 'wpinv_template', |
|
| 205 | + 'label' => __( 'Template', 'invoicing' ), |
|
| 206 | + 'label_type' => 'vertical', |
|
| 207 | + 'placeholder' => __( 'Choose a template', 'invoicing' ), |
|
| 208 | + 'class' => 'form-control-sm', |
|
| 209 | + 'value' => $invoice->get_template( 'edit' ), |
|
| 210 | + 'options' => array( |
|
| 211 | + 'quantity' => __( 'Quantity', 'invoicing' ), |
|
| 212 | + 'hours' => __( 'Hours', 'invoicing' ), |
|
| 213 | + ), |
|
| 214 | + 'data-allow-clear' => 'false', |
|
| 215 | + 'select2' => true, |
|
| 216 | + ), |
|
| 217 | + true |
|
| 218 | + ); |
|
| 219 | + ?> |
|
| 220 | 220 | </div> |
| 221 | 221 | <div class="col-12 col-sm-6"> |
| 222 | 222 | <?php |
| 223 | 223 | |
| 224 | - // Set currency. |
|
| 225 | - aui()->select( |
|
| 226 | - array( |
|
| 227 | - 'id' => 'wpinv_currency', |
|
| 228 | - 'name' => 'wpinv_currency', |
|
| 229 | - 'label' => __( 'Currency', 'invoicing' ), |
|
| 230 | - 'label_type' => 'vertical', |
|
| 231 | - 'placeholder' => __( 'Select Invoice Currency', 'invoicing' ), |
|
| 232 | - 'class' => 'form-control-sm getpaid-recalculate-prices-on-change', |
|
| 233 | - 'value' => $invoice->get_currency( 'edit' ), |
|
| 234 | - 'required' => false, |
|
| 235 | - 'data-allow-clear' => 'false', |
|
| 236 | - 'select2' => true, |
|
| 237 | - 'options' => wpinv_get_currencies(), |
|
| 238 | - ), |
|
| 239 | - true |
|
| 240 | - ); |
|
| 241 | - |
|
| 242 | - ?> |
|
| 224 | + // Set currency. |
|
| 225 | + aui()->select( |
|
| 226 | + array( |
|
| 227 | + 'id' => 'wpinv_currency', |
|
| 228 | + 'name' => 'wpinv_currency', |
|
| 229 | + 'label' => __( 'Currency', 'invoicing' ), |
|
| 230 | + 'label_type' => 'vertical', |
|
| 231 | + 'placeholder' => __( 'Select Invoice Currency', 'invoicing' ), |
|
| 232 | + 'class' => 'form-control-sm getpaid-recalculate-prices-on-change', |
|
| 233 | + 'value' => $invoice->get_currency( 'edit' ), |
|
| 234 | + 'required' => false, |
|
| 235 | + 'data-allow-clear' => 'false', |
|
| 236 | + 'select2' => true, |
|
| 237 | + 'options' => wpinv_get_currencies(), |
|
| 238 | + ), |
|
| 239 | + true |
|
| 240 | + ); |
|
| 241 | + |
|
| 242 | + ?> |
|
| 243 | 243 | </div> |
| 244 | 244 | </div> |
| 245 | 245 | |
@@ -249,123 +249,123 @@ discard block |
||
| 249 | 249 | <div class="row"> |
| 250 | 250 | <div class="col-12 col-sm-6"> |
| 251 | 251 | <?php |
| 252 | - aui()->input( |
|
| 253 | - array( |
|
| 254 | - 'type' => 'text', |
|
| 255 | - 'id' => 'wpinv_company_id', |
|
| 256 | - 'name' => 'wpinv_company_id', |
|
| 257 | - 'label' => __( 'Company ID', 'invoicing' ), |
|
| 258 | - 'label_type' => 'vertical', |
|
| 259 | - 'placeholder' => '', |
|
| 260 | - 'class' => 'form-control-sm', |
|
| 261 | - 'value' => $invoice->get_company_id( 'edit' ), |
|
| 262 | - ), |
|
| 263 | - true |
|
| 264 | - ); |
|
| 265 | - ?> |
|
| 252 | + aui()->input( |
|
| 253 | + array( |
|
| 254 | + 'type' => 'text', |
|
| 255 | + 'id' => 'wpinv_company_id', |
|
| 256 | + 'name' => 'wpinv_company_id', |
|
| 257 | + 'label' => __( 'Company ID', 'invoicing' ), |
|
| 258 | + 'label_type' => 'vertical', |
|
| 259 | + 'placeholder' => '', |
|
| 260 | + 'class' => 'form-control-sm', |
|
| 261 | + 'value' => $invoice->get_company_id( 'edit' ), |
|
| 262 | + ), |
|
| 263 | + true |
|
| 264 | + ); |
|
| 265 | + ?> |
|
| 266 | 266 | </div> |
| 267 | 267 | </div> |
| 268 | 268 | |
| 269 | 269 | <?php do_action( 'getpaid_after_metabox_invoice_address', $invoice ); ?> |
| 270 | 270 | </div> |
| 271 | 271 | <?php |
| 272 | - } |
|
| 273 | - |
|
| 274 | - /** |
|
| 275 | - * Save meta box data. |
|
| 276 | - * |
|
| 277 | - * @param int $post_id |
|
| 278 | - * @param array $posted the posted data. |
|
| 279 | - */ |
|
| 280 | - public static function save( $post_id, $posted ) { |
|
| 281 | - |
|
| 282 | - // Prepare the invoice. |
|
| 283 | - $invoice = new WPInv_Invoice( $post_id ); |
|
| 284 | - |
|
| 285 | - // Load new data. |
|
| 286 | - $invoice->set_props( |
|
| 287 | - array( |
|
| 288 | - 'template' => isset( $posted['wpinv_template'] ) ? wpinv_clean( $posted['wpinv_template'] ) : null, |
|
| 289 | - 'email_cc' => isset( $posted['wpinv_cc'] ) ? wpinv_clean( $posted['wpinv_cc'] ) : null, |
|
| 290 | - 'disable_taxes' => ! empty( $posted['disable_taxes'] ), |
|
| 291 | - 'currency' => isset( $posted['wpinv_currency'] ) ? wpinv_clean( $posted['wpinv_currency'] ) : null, |
|
| 292 | - 'gateway' => ( $invoice->needs_payment() && isset( $posted['wpinv_gateway'] ) ) ? wpinv_clean( $posted['wpinv_gateway'] ) : null, |
|
| 293 | - 'address' => isset( $posted['wpinv_address'] ) ? wpinv_clean( $posted['wpinv_address'] ) : null, |
|
| 294 | - 'vat_number' => isset( $posted['wpinv_vat_number'] ) ? wpinv_clean( $posted['wpinv_vat_number'] ) : null, |
|
| 295 | - 'company' => isset( $posted['wpinv_company'] ) ? wpinv_clean( $posted['wpinv_company'] ) : null, |
|
| 296 | - 'company_id' => isset( $posted['wpinv_company_id'] ) ? wpinv_clean( $posted['wpinv_company_id'] ) : null, |
|
| 297 | - 'zip' => isset( $posted['wpinv_zip'] ) ? wpinv_clean( $posted['wpinv_zip'] ) : null, |
|
| 298 | - 'state' => isset( $posted['wpinv_state'] ) ? wpinv_clean( $posted['wpinv_state'] ) : null, |
|
| 299 | - 'city' => isset( $posted['wpinv_city'] ) ? wpinv_clean( $posted['wpinv_city'] ) : null, |
|
| 300 | - 'country' => isset( $posted['wpinv_country'] ) ? wpinv_clean( $posted['wpinv_country'] ) : null, |
|
| 301 | - 'phone' => isset( $posted['wpinv_phone'] ) ? wpinv_clean( $posted['wpinv_phone'] ) : null, |
|
| 302 | - 'first_name' => isset( $posted['wpinv_first_name'] ) ? wpinv_clean( $posted['wpinv_first_name'] ) : null, |
|
| 303 | - 'last_name' => isset( $posted['wpinv_last_name'] ) ? wpinv_clean( $posted['wpinv_last_name'] ) : null, |
|
| 304 | - 'author' => isset( $posted['post_author_override'] ) ? wpinv_clean( $posted['post_author_override'] ) : null, |
|
| 305 | - 'date_created' => isset( $posted['date_created'] ) ? wpinv_clean( $posted['date_created'] ) : null, |
|
| 306 | - 'date_completed' => isset( $posted['wpinv_date_completed'] ) ? wpinv_clean( $posted['wpinv_date_completed'] ) : null, |
|
| 307 | - 'due_date' => isset( $posted['wpinv_due_date'] ) ? wpinv_clean( $posted['wpinv_due_date'] ) : null, |
|
| 308 | - 'number' => isset( $posted['wpinv_number'] ) ? wpinv_clean( $posted['wpinv_number'] ) : null, |
|
| 309 | - 'status' => isset( $posted['wpinv_status'] ) ? wpinv_clean( $posted['wpinv_status'] ) : null, |
|
| 310 | - ) |
|
| 311 | - ); |
|
| 312 | - |
|
| 313 | - // Discount code. |
|
| 314 | - if ( ! $invoice->is_paid() && ! $invoice->is_refunded() ) { |
|
| 315 | - |
|
| 316 | - if ( isset( $posted['wpinv_discount_code'] ) ) { |
|
| 317 | - $invoice->set_discount_code( wpinv_clean( $posted['wpinv_discount_code'] ) ); |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - $discount = new WPInv_Discount( $invoice->get_discount_code() ); |
|
| 321 | - if ( $discount->exists() ) { |
|
| 322 | - $invoice->add_discount( getpaid_calculate_invoice_discount( $invoice, $discount ) ); |
|
| 323 | - } else { |
|
| 324 | - $invoice->remove_discount( 'discount_code' ); |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - // Recalculate totals. |
|
| 328 | - $invoice->recalculate_total(); |
|
| 329 | - |
|
| 330 | - } |
|
| 331 | - |
|
| 332 | - // If we're creating a new user... |
|
| 333 | - if ( ! empty( $posted['wpinv_new_user'] ) && is_email( stripslashes( $posted['wpinv_email'] ) ) ) { |
|
| 334 | - |
|
| 335 | - // Attempt to create the user. |
|
| 336 | - $user = wpinv_create_user( sanitize_email( stripslashes( $posted['wpinv_email'] ) ), $invoice->get_first_name() . $invoice->get_last_name() ); |
|
| 337 | - |
|
| 338 | - // If successful, update the invoice author. |
|
| 339 | - if ( is_numeric( $user ) ) { |
|
| 340 | - $invoice->set_author( $user ); |
|
| 341 | - } else { |
|
| 342 | - wpinv_error_log( $user->get_error_message(), __( 'Invoice add new user', 'invoicing' ), __FILE__, __LINE__ ); |
|
| 343 | - } |
|
| 344 | - } |
|
| 345 | - |
|
| 346 | - // Do not send new invoice notifications. |
|
| 347 | - $GLOBALS['wpinv_skip_invoice_notification'] = true; |
|
| 348 | - |
|
| 349 | - // Save the invoice. |
|
| 350 | - $invoice->save(); |
|
| 351 | - |
|
| 352 | - // Save the user address. |
|
| 353 | - getpaid_save_invoice_user_address( $invoice ); |
|
| 354 | - |
|
| 355 | - // Undo do not send new invoice notifications. |
|
| 356 | - $GLOBALS['wpinv_skip_invoice_notification'] = false; |
|
| 357 | - |
|
| 358 | - // (Maybe) send new user notification. |
|
| 359 | - $should_send_notification = wpinv_get_option( 'disable_new_user_emails' ); |
|
| 360 | - if ( ! empty( $user ) && is_numeric( $user ) && apply_filters( 'getpaid_send_new_user_notification', empty( $should_send_notification ) ) ) { |
|
| 361 | - wp_send_new_user_notifications( $user, 'user' ); |
|
| 362 | - } |
|
| 363 | - |
|
| 364 | - if ( ! empty( $posted['send_to_customer'] ) && ! $invoice->is_draft() ) { |
|
| 365 | - getpaid()->get( 'invoice_emails' )->user_invoice( $invoice, true ); |
|
| 366 | - } |
|
| 367 | - |
|
| 368 | - // Fires after an invoice is saved. |
|
| 369 | - do_action( 'wpinv_invoice_metabox_saved', $invoice ); |
|
| 370 | - } |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + /** |
|
| 275 | + * Save meta box data. |
|
| 276 | + * |
|
| 277 | + * @param int $post_id |
|
| 278 | + * @param array $posted the posted data. |
|
| 279 | + */ |
|
| 280 | + public static function save( $post_id, $posted ) { |
|
| 281 | + |
|
| 282 | + // Prepare the invoice. |
|
| 283 | + $invoice = new WPInv_Invoice( $post_id ); |
|
| 284 | + |
|
| 285 | + // Load new data. |
|
| 286 | + $invoice->set_props( |
|
| 287 | + array( |
|
| 288 | + 'template' => isset( $posted['wpinv_template'] ) ? wpinv_clean( $posted['wpinv_template'] ) : null, |
|
| 289 | + 'email_cc' => isset( $posted['wpinv_cc'] ) ? wpinv_clean( $posted['wpinv_cc'] ) : null, |
|
| 290 | + 'disable_taxes' => ! empty( $posted['disable_taxes'] ), |
|
| 291 | + 'currency' => isset( $posted['wpinv_currency'] ) ? wpinv_clean( $posted['wpinv_currency'] ) : null, |
|
| 292 | + 'gateway' => ( $invoice->needs_payment() && isset( $posted['wpinv_gateway'] ) ) ? wpinv_clean( $posted['wpinv_gateway'] ) : null, |
|
| 293 | + 'address' => isset( $posted['wpinv_address'] ) ? wpinv_clean( $posted['wpinv_address'] ) : null, |
|
| 294 | + 'vat_number' => isset( $posted['wpinv_vat_number'] ) ? wpinv_clean( $posted['wpinv_vat_number'] ) : null, |
|
| 295 | + 'company' => isset( $posted['wpinv_company'] ) ? wpinv_clean( $posted['wpinv_company'] ) : null, |
|
| 296 | + 'company_id' => isset( $posted['wpinv_company_id'] ) ? wpinv_clean( $posted['wpinv_company_id'] ) : null, |
|
| 297 | + 'zip' => isset( $posted['wpinv_zip'] ) ? wpinv_clean( $posted['wpinv_zip'] ) : null, |
|
| 298 | + 'state' => isset( $posted['wpinv_state'] ) ? wpinv_clean( $posted['wpinv_state'] ) : null, |
|
| 299 | + 'city' => isset( $posted['wpinv_city'] ) ? wpinv_clean( $posted['wpinv_city'] ) : null, |
|
| 300 | + 'country' => isset( $posted['wpinv_country'] ) ? wpinv_clean( $posted['wpinv_country'] ) : null, |
|
| 301 | + 'phone' => isset( $posted['wpinv_phone'] ) ? wpinv_clean( $posted['wpinv_phone'] ) : null, |
|
| 302 | + 'first_name' => isset( $posted['wpinv_first_name'] ) ? wpinv_clean( $posted['wpinv_first_name'] ) : null, |
|
| 303 | + 'last_name' => isset( $posted['wpinv_last_name'] ) ? wpinv_clean( $posted['wpinv_last_name'] ) : null, |
|
| 304 | + 'author' => isset( $posted['post_author_override'] ) ? wpinv_clean( $posted['post_author_override'] ) : null, |
|
| 305 | + 'date_created' => isset( $posted['date_created'] ) ? wpinv_clean( $posted['date_created'] ) : null, |
|
| 306 | + 'date_completed' => isset( $posted['wpinv_date_completed'] ) ? wpinv_clean( $posted['wpinv_date_completed'] ) : null, |
|
| 307 | + 'due_date' => isset( $posted['wpinv_due_date'] ) ? wpinv_clean( $posted['wpinv_due_date'] ) : null, |
|
| 308 | + 'number' => isset( $posted['wpinv_number'] ) ? wpinv_clean( $posted['wpinv_number'] ) : null, |
|
| 309 | + 'status' => isset( $posted['wpinv_status'] ) ? wpinv_clean( $posted['wpinv_status'] ) : null, |
|
| 310 | + ) |
|
| 311 | + ); |
|
| 312 | + |
|
| 313 | + // Discount code. |
|
| 314 | + if ( ! $invoice->is_paid() && ! $invoice->is_refunded() ) { |
|
| 315 | + |
|
| 316 | + if ( isset( $posted['wpinv_discount_code'] ) ) { |
|
| 317 | + $invoice->set_discount_code( wpinv_clean( $posted['wpinv_discount_code'] ) ); |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + $discount = new WPInv_Discount( $invoice->get_discount_code() ); |
|
| 321 | + if ( $discount->exists() ) { |
|
| 322 | + $invoice->add_discount( getpaid_calculate_invoice_discount( $invoice, $discount ) ); |
|
| 323 | + } else { |
|
| 324 | + $invoice->remove_discount( 'discount_code' ); |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + // Recalculate totals. |
|
| 328 | + $invoice->recalculate_total(); |
|
| 329 | + |
|
| 330 | + } |
|
| 331 | + |
|
| 332 | + // If we're creating a new user... |
|
| 333 | + if ( ! empty( $posted['wpinv_new_user'] ) && is_email( stripslashes( $posted['wpinv_email'] ) ) ) { |
|
| 334 | + |
|
| 335 | + // Attempt to create the user. |
|
| 336 | + $user = wpinv_create_user( sanitize_email( stripslashes( $posted['wpinv_email'] ) ), $invoice->get_first_name() . $invoice->get_last_name() ); |
|
| 337 | + |
|
| 338 | + // If successful, update the invoice author. |
|
| 339 | + if ( is_numeric( $user ) ) { |
|
| 340 | + $invoice->set_author( $user ); |
|
| 341 | + } else { |
|
| 342 | + wpinv_error_log( $user->get_error_message(), __( 'Invoice add new user', 'invoicing' ), __FILE__, __LINE__ ); |
|
| 343 | + } |
|
| 344 | + } |
|
| 345 | + |
|
| 346 | + // Do not send new invoice notifications. |
|
| 347 | + $GLOBALS['wpinv_skip_invoice_notification'] = true; |
|
| 348 | + |
|
| 349 | + // Save the invoice. |
|
| 350 | + $invoice->save(); |
|
| 351 | + |
|
| 352 | + // Save the user address. |
|
| 353 | + getpaid_save_invoice_user_address( $invoice ); |
|
| 354 | + |
|
| 355 | + // Undo do not send new invoice notifications. |
|
| 356 | + $GLOBALS['wpinv_skip_invoice_notification'] = false; |
|
| 357 | + |
|
| 358 | + // (Maybe) send new user notification. |
|
| 359 | + $should_send_notification = wpinv_get_option( 'disable_new_user_emails' ); |
|
| 360 | + if ( ! empty( $user ) && is_numeric( $user ) && apply_filters( 'getpaid_send_new_user_notification', empty( $should_send_notification ) ) ) { |
|
| 361 | + wp_send_new_user_notifications( $user, 'user' ); |
|
| 362 | + } |
|
| 363 | + |
|
| 364 | + if ( ! empty( $posted['send_to_customer'] ) && ! $invoice->is_draft() ) { |
|
| 365 | + getpaid()->get( 'invoice_emails' )->user_invoice( $invoice, true ); |
|
| 366 | + } |
|
| 367 | + |
|
| 368 | + // Fires after an invoice is saved. |
|
| 369 | + do_action( 'wpinv_invoice_metabox_saved', $invoice ); |
|
| 370 | + } |
|
| 371 | 371 | } |
@@ -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 | } |