@@ -5,129 +5,129 @@ discard block |
||
| 5 | 5 | */ |
| 6 | 6 | |
| 7 | 7 | if ( ! defined( 'ABSPATH' ) ) { |
| 8 | - exit; // Exit if accessed directly |
|
| 8 | + exit; // Exit if accessed directly |
|
| 9 | 9 | } |
| 10 | 10 | |
| 11 | 11 | if ( ! class_exists( 'GetPaid_Admin_Profile', false ) ) : |
| 12 | 12 | |
| 13 | - /** |
|
| 14 | - * GetPaid_Admin_Profile Class. |
|
| 15 | - */ |
|
| 16 | - class GetPaid_Admin_Profile { |
|
| 17 | - |
|
| 18 | - /** |
|
| 19 | - * Hook in tabs. |
|
| 20 | - */ |
|
| 21 | - public function __construct() { |
|
| 22 | - add_action( 'show_user_profile', array( $this, 'add_customer_meta_fields' ), 100 ); |
|
| 23 | - add_action( 'edit_user_profile', array( $this, 'add_customer_meta_fields' ), 100 ); |
|
| 24 | - |
|
| 25 | - add_action( 'personal_options_update', array( $this, 'save_customer_meta_fields' ) ); |
|
| 26 | - add_action( 'edit_user_profile_update', array( $this, 'save_customer_meta_fields' ) ); |
|
| 27 | - } |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * Get Address Fields for the edit user pages. |
|
| 31 | - * |
|
| 32 | - * @return array Fields to display which are filtered through invoicing_customer_meta_fields before being returned |
|
| 33 | - */ |
|
| 34 | - public function get_customer_meta_fields() { |
|
| 35 | - |
|
| 36 | - $show_fields = apply_filters( |
|
| 37 | - 'getpaid_customer_meta_fields', |
|
| 38 | - array( |
|
| 39 | - 'billing' => array( |
|
| 40 | - 'title' => __( 'Billing Details (GetPaid)', 'invoicing' ), |
|
| 41 | - 'fields' => array( |
|
| 42 | - '_wpinv_first_name' => array( |
|
| 43 | - 'label' => __( 'First name', 'invoicing' ), |
|
| 44 | - 'description' => '', |
|
| 45 | - ), |
|
| 46 | - '_wpinv_last_name' => array( |
|
| 47 | - 'label' => __( 'Last name', 'invoicing' ), |
|
| 48 | - 'description' => '', |
|
| 49 | - ), |
|
| 50 | - '_wpinv_company' => array( |
|
| 51 | - 'label' => __( 'Company', 'invoicing' ), |
|
| 52 | - 'description' => '', |
|
| 53 | - ), |
|
| 54 | - '_wpinv_company_id' => array( |
|
| 55 | - 'label' => __( 'Company ID', 'invoicing' ), |
|
| 56 | - 'description' => '', |
|
| 57 | - ), |
|
| 58 | - '_wpinv_address' => array( |
|
| 59 | - 'label' => __( 'Address', 'invoicing' ), |
|
| 60 | - 'description' => '', |
|
| 61 | - ), |
|
| 62 | - '_wpinv_city' => array( |
|
| 63 | - 'label' => __( 'City', 'invoicing' ), |
|
| 64 | - 'description' => '', |
|
| 65 | - ), |
|
| 66 | - '_wpinv_zip' => array( |
|
| 67 | - 'label' => __( 'Postcode / ZIP', 'invoicing' ), |
|
| 68 | - 'description' => '', |
|
| 69 | - ), |
|
| 70 | - '_wpinv_country' => array( |
|
| 71 | - 'label' => __( 'Country / Region', 'invoicing' ), |
|
| 72 | - 'description' => '', |
|
| 73 | - 'class' => 'getpaid_js_field-country', |
|
| 74 | - 'type' => 'select', |
|
| 75 | - 'options' => array( '' => __( 'Select a country / region…', 'invoicing' ) ) + wpinv_get_country_list(), |
|
| 76 | - ), |
|
| 77 | - '_wpinv_state' => array( |
|
| 78 | - 'label' => __( 'State / County', 'invoicing' ), |
|
| 79 | - 'description' => __( 'State / County or state code', 'invoicing' ), |
|
| 80 | - 'class' => 'getpaid_js_field-state regular-text', |
|
| 81 | - ), |
|
| 82 | - '_wpinv_phone' => array( |
|
| 83 | - 'label' => __( 'Phone', 'invoicing' ), |
|
| 84 | - 'description' => '', |
|
| 85 | - ), |
|
| 86 | - '_wpinv_vat_number' => array( |
|
| 87 | - 'label' => __( 'VAT Number', 'invoicing' ), |
|
| 88 | - 'description' => '', |
|
| 89 | - ), |
|
| 90 | - ), |
|
| 91 | - ), |
|
| 92 | - ) |
|
| 93 | - ); |
|
| 94 | - return $show_fields; |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * Show Address Fields on edit user pages. |
|
| 99 | - * |
|
| 100 | - * @param WP_User $user |
|
| 101 | - */ |
|
| 102 | - public function add_customer_meta_fields( $user ) { |
|
| 103 | - if ( ! apply_filters( 'getpaid_current_user_can_edit_customer_meta_fields', current_user_can( 'manage_options' ), $user->ID ) ) { |
|
| 104 | - return; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - $show_fields = $this->get_customer_meta_fields(); |
|
| 108 | - |
|
| 109 | - $customer = getpaid_get_customer_by_user_id( (int) $user->ID ); |
|
| 110 | - |
|
| 111 | - foreach ( $show_fields as $fieldset_key => $fieldset ) : |
|
| 112 | - if ( ! wpinv_use_taxes() && isset( $fieldset['fields']['_wpinv_vat_number'] ) ) { |
|
| 113 | - unset( $fieldset['fields']['_wpinv_vat_number'] ); |
|
| 114 | - } |
|
| 115 | - ?> |
|
| 13 | + /** |
|
| 14 | + * GetPaid_Admin_Profile Class. |
|
| 15 | + */ |
|
| 16 | + class GetPaid_Admin_Profile { |
|
| 17 | + |
|
| 18 | + /** |
|
| 19 | + * Hook in tabs. |
|
| 20 | + */ |
|
| 21 | + public function __construct() { |
|
| 22 | + add_action( 'show_user_profile', array( $this, 'add_customer_meta_fields' ), 100 ); |
|
| 23 | + add_action( 'edit_user_profile', array( $this, 'add_customer_meta_fields' ), 100 ); |
|
| 24 | + |
|
| 25 | + add_action( 'personal_options_update', array( $this, 'save_customer_meta_fields' ) ); |
|
| 26 | + add_action( 'edit_user_profile_update', array( $this, 'save_customer_meta_fields' ) ); |
|
| 27 | + } |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * Get Address Fields for the edit user pages. |
|
| 31 | + * |
|
| 32 | + * @return array Fields to display which are filtered through invoicing_customer_meta_fields before being returned |
|
| 33 | + */ |
|
| 34 | + public function get_customer_meta_fields() { |
|
| 35 | + |
|
| 36 | + $show_fields = apply_filters( |
|
| 37 | + 'getpaid_customer_meta_fields', |
|
| 38 | + array( |
|
| 39 | + 'billing' => array( |
|
| 40 | + 'title' => __( 'Billing Details (GetPaid)', 'invoicing' ), |
|
| 41 | + 'fields' => array( |
|
| 42 | + '_wpinv_first_name' => array( |
|
| 43 | + 'label' => __( 'First name', 'invoicing' ), |
|
| 44 | + 'description' => '', |
|
| 45 | + ), |
|
| 46 | + '_wpinv_last_name' => array( |
|
| 47 | + 'label' => __( 'Last name', 'invoicing' ), |
|
| 48 | + 'description' => '', |
|
| 49 | + ), |
|
| 50 | + '_wpinv_company' => array( |
|
| 51 | + 'label' => __( 'Company', 'invoicing' ), |
|
| 52 | + 'description' => '', |
|
| 53 | + ), |
|
| 54 | + '_wpinv_company_id' => array( |
|
| 55 | + 'label' => __( 'Company ID', 'invoicing' ), |
|
| 56 | + 'description' => '', |
|
| 57 | + ), |
|
| 58 | + '_wpinv_address' => array( |
|
| 59 | + 'label' => __( 'Address', 'invoicing' ), |
|
| 60 | + 'description' => '', |
|
| 61 | + ), |
|
| 62 | + '_wpinv_city' => array( |
|
| 63 | + 'label' => __( 'City', 'invoicing' ), |
|
| 64 | + 'description' => '', |
|
| 65 | + ), |
|
| 66 | + '_wpinv_zip' => array( |
|
| 67 | + 'label' => __( 'Postcode / ZIP', 'invoicing' ), |
|
| 68 | + 'description' => '', |
|
| 69 | + ), |
|
| 70 | + '_wpinv_country' => array( |
|
| 71 | + 'label' => __( 'Country / Region', 'invoicing' ), |
|
| 72 | + 'description' => '', |
|
| 73 | + 'class' => 'getpaid_js_field-country', |
|
| 74 | + 'type' => 'select', |
|
| 75 | + 'options' => array( '' => __( 'Select a country / region…', 'invoicing' ) ) + wpinv_get_country_list(), |
|
| 76 | + ), |
|
| 77 | + '_wpinv_state' => array( |
|
| 78 | + 'label' => __( 'State / County', 'invoicing' ), |
|
| 79 | + 'description' => __( 'State / County or state code', 'invoicing' ), |
|
| 80 | + 'class' => 'getpaid_js_field-state regular-text', |
|
| 81 | + ), |
|
| 82 | + '_wpinv_phone' => array( |
|
| 83 | + 'label' => __( 'Phone', 'invoicing' ), |
|
| 84 | + 'description' => '', |
|
| 85 | + ), |
|
| 86 | + '_wpinv_vat_number' => array( |
|
| 87 | + 'label' => __( 'VAT Number', 'invoicing' ), |
|
| 88 | + 'description' => '', |
|
| 89 | + ), |
|
| 90 | + ), |
|
| 91 | + ), |
|
| 92 | + ) |
|
| 93 | + ); |
|
| 94 | + return $show_fields; |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * Show Address Fields on edit user pages. |
|
| 99 | + * |
|
| 100 | + * @param WP_User $user |
|
| 101 | + */ |
|
| 102 | + public function add_customer_meta_fields( $user ) { |
|
| 103 | + if ( ! apply_filters( 'getpaid_current_user_can_edit_customer_meta_fields', current_user_can( 'manage_options' ), $user->ID ) ) { |
|
| 104 | + return; |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + $show_fields = $this->get_customer_meta_fields(); |
|
| 108 | + |
|
| 109 | + $customer = getpaid_get_customer_by_user_id( (int) $user->ID ); |
|
| 110 | + |
|
| 111 | + foreach ( $show_fields as $fieldset_key => $fieldset ) : |
|
| 112 | + if ( ! wpinv_use_taxes() && isset( $fieldset['fields']['_wpinv_vat_number'] ) ) { |
|
| 113 | + unset( $fieldset['fields']['_wpinv_vat_number'] ); |
|
| 114 | + } |
|
| 115 | + ?> |
|
| 116 | 116 | <h2><?php echo esc_html( $fieldset['title'] ); ?></h2> |
| 117 | 117 | <table class="form-table" id="<?php echo esc_attr( 'getpaid-fieldset-' . $fieldset_key ); ?>"> |
| 118 | 118 | <?php foreach ( $fieldset['fields'] as $key => $field ) : |
| 119 | - if ( ! empty( $customer ) ) { |
|
| 120 | - if ( strpos( $key, '_wpinv_' ) === 0 ) { |
|
| 121 | - $save_key = substr( $key , 7 ); |
|
| 122 | - } else { |
|
| 123 | - $save_key = $key; |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - $value = $customer->get( $save_key ); |
|
| 127 | - } else { |
|
| 128 | - $value = $this->get_user_meta( $user->ID, $key ); |
|
| 129 | - } |
|
| 130 | - ?> |
|
| 119 | + if ( ! empty( $customer ) ) { |
|
| 120 | + if ( strpos( $key, '_wpinv_' ) === 0 ) { |
|
| 121 | + $save_key = substr( $key , 7 ); |
|
| 122 | + } else { |
|
| 123 | + $save_key = $key; |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + $value = $customer->get( $save_key ); |
|
| 127 | + } else { |
|
| 128 | + $value = $this->get_user_meta( $user->ID, $key ); |
|
| 129 | + } |
|
| 130 | + ?> |
|
| 131 | 131 | <tr> |
| 132 | 132 | <th> |
| 133 | 133 | <label for="<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $field['label'] ); ?></label> |
@@ -150,75 +150,75 @@ discard block |
||
| 150 | 150 | <?php endforeach; ?> |
| 151 | 151 | </table> |
| 152 | 152 | <?php |
| 153 | - endforeach; |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - /** |
|
| 157 | - * Save Address Fields on edit user pages. |
|
| 158 | - * |
|
| 159 | - * @param int $user_id User ID of the user being saved |
|
| 160 | - */ |
|
| 161 | - public function save_customer_meta_fields( $user_id ) { |
|
| 162 | - if ( ! apply_filters( 'getpaid_current_user_can_edit_customer_meta_fields', current_user_can( 'manage_options' ), $user_id ) ) { |
|
| 163 | - return; |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - $save_fields = $this->get_customer_meta_fields(); |
|
| 167 | - $save_data = array(); |
|
| 168 | - |
|
| 169 | - foreach ( $save_fields as $fieldset ) { |
|
| 170 | - foreach ( $fieldset['fields'] as $key => $field ) { |
|
| 171 | - if ( strpos( $key, '_wpinv_' ) === 0 ) { |
|
| 172 | - $save_key = substr( $key , 7 ); |
|
| 173 | - } else { |
|
| 174 | - $save_key = $key; |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - if ( $save_key && isset( $field['type'] ) && 'checkbox' === $field['type'] ) { |
|
| 178 | - $save_data[ $save_key ] = ! empty( $_POST[ $key ] ) ? true : false; |
|
| 179 | - } else if ( $save_key && isset( $_POST[ $key ] ) ) { |
|
| 180 | - $save_data[ $save_key ] = wpinv_clean( $_POST[ $key ] ); |
|
| 181 | - } |
|
| 182 | - } |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - if ( empty( $save_data ) ) { |
|
| 186 | - return; |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - $customer = getpaid_get_customer_by_user_id( (int) $user_id ); |
|
| 190 | - |
|
| 191 | - if ( empty( $customer ) ) { |
|
| 192 | - $customer = new GetPaid_Customer( 0 ); |
|
| 193 | - $customer->clone_user( (int) $user_id ); |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - foreach ( $save_data as $key => $value ) { |
|
| 197 | - $customer->set( $key, $value ); |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - $customer->save(); |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - /** |
|
| 204 | - * Get user meta for a given key, with fallbacks to core user info for pre-existing fields. |
|
| 205 | - * |
|
| 206 | - * @since 3.1.0 |
|
| 207 | - * @param int $user_id User ID of the user being edited |
|
| 208 | - * @param string $key Key for user meta field |
|
| 209 | - * @return string |
|
| 210 | - */ |
|
| 211 | - protected function get_user_meta( $user_id, $key ) { |
|
| 212 | - $value = get_user_meta( $user_id, $key, true ); |
|
| 213 | - $existing_fields = array( '_wpinv_first_name', '_wpinv_last_name' ); |
|
| 214 | - |
|
| 215 | - if ( ! $value && in_array( $key, $existing_fields ) ) { |
|
| 216 | - $value = get_user_meta( $user_id, str_replace( '_wpinv_', '', $key ), true ); |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - return $value; |
|
| 220 | - } |
|
| 221 | - } |
|
| 153 | + endforeach; |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + /** |
|
| 157 | + * Save Address Fields on edit user pages. |
|
| 158 | + * |
|
| 159 | + * @param int $user_id User ID of the user being saved |
|
| 160 | + */ |
|
| 161 | + public function save_customer_meta_fields( $user_id ) { |
|
| 162 | + if ( ! apply_filters( 'getpaid_current_user_can_edit_customer_meta_fields', current_user_can( 'manage_options' ), $user_id ) ) { |
|
| 163 | + return; |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + $save_fields = $this->get_customer_meta_fields(); |
|
| 167 | + $save_data = array(); |
|
| 168 | + |
|
| 169 | + foreach ( $save_fields as $fieldset ) { |
|
| 170 | + foreach ( $fieldset['fields'] as $key => $field ) { |
|
| 171 | + if ( strpos( $key, '_wpinv_' ) === 0 ) { |
|
| 172 | + $save_key = substr( $key , 7 ); |
|
| 173 | + } else { |
|
| 174 | + $save_key = $key; |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + if ( $save_key && isset( $field['type'] ) && 'checkbox' === $field['type'] ) { |
|
| 178 | + $save_data[ $save_key ] = ! empty( $_POST[ $key ] ) ? true : false; |
|
| 179 | + } else if ( $save_key && isset( $_POST[ $key ] ) ) { |
|
| 180 | + $save_data[ $save_key ] = wpinv_clean( $_POST[ $key ] ); |
|
| 181 | + } |
|
| 182 | + } |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + if ( empty( $save_data ) ) { |
|
| 186 | + return; |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + $customer = getpaid_get_customer_by_user_id( (int) $user_id ); |
|
| 190 | + |
|
| 191 | + if ( empty( $customer ) ) { |
|
| 192 | + $customer = new GetPaid_Customer( 0 ); |
|
| 193 | + $customer->clone_user( (int) $user_id ); |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + foreach ( $save_data as $key => $value ) { |
|
| 197 | + $customer->set( $key, $value ); |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + $customer->save(); |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + /** |
|
| 204 | + * Get user meta for a given key, with fallbacks to core user info for pre-existing fields. |
|
| 205 | + * |
|
| 206 | + * @since 3.1.0 |
|
| 207 | + * @param int $user_id User ID of the user being edited |
|
| 208 | + * @param string $key Key for user meta field |
|
| 209 | + * @return string |
|
| 210 | + */ |
|
| 211 | + protected function get_user_meta( $user_id, $key ) { |
|
| 212 | + $value = get_user_meta( $user_id, $key, true ); |
|
| 213 | + $existing_fields = array( '_wpinv_first_name', '_wpinv_last_name' ); |
|
| 214 | + |
|
| 215 | + if ( ! $value && in_array( $key, $existing_fields ) ) { |
|
| 216 | + $value = get_user_meta( $user_id, str_replace( '_wpinv_', '', $key ), true ); |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + return $value; |
|
| 220 | + } |
|
| 221 | + } |
|
| 222 | 222 | |
| 223 | 223 | endif; |
| 224 | 224 | |