helper
1 year ago
importers
1 year ago
list-tables
1 year ago
marketplace-suggestions
1 year ago
meta-boxes
1 year ago
notes
1 year ago
plugin-updates
1 year ago
reports
1 year ago
settings
1 year ago
views
1 year ago
class-wc-admin-addons.php
1 year ago
class-wc-admin-api-keys-table-list.php
1 year ago
class-wc-admin-api-keys.php
1 year ago
class-wc-admin-assets.php
1 year ago
class-wc-admin-attributes.php
1 year ago
class-wc-admin-customize.php
1 year ago
class-wc-admin-dashboard-setup.php
1 year ago
class-wc-admin-dashboard.php
1 year ago
class-wc-admin-duplicate-product.php
1 year ago
class-wc-admin-exporters.php
1 year ago
class-wc-admin-help.php
1 year ago
class-wc-admin-importers.php
1 year ago
class-wc-admin-log-table-list.php
1 year ago
class-wc-admin-marketplace-promotions.php
1 year ago
class-wc-admin-menus.php
1 year ago
class-wc-admin-meta-boxes.php
1 year ago
class-wc-admin-notices.php
1 year ago
class-wc-admin-permalink-settings.php
1 year ago
class-wc-admin-pointers.php
1 year ago
class-wc-admin-post-types.php
1 year ago
class-wc-admin-profile.php
1 year ago
class-wc-admin-reports.php
1 year ago
class-wc-admin-settings.php
1 year ago
class-wc-admin-setup-wizard.php
1 year ago
class-wc-admin-status.php
1 year ago
class-wc-admin-taxonomies.php
1 year ago
class-wc-admin-upload-downloadable-product.php
1 year ago
class-wc-admin-webhooks-table-list.php
1 year ago
class-wc-admin-webhooks.php
1 year ago
class-wc-admin.php
1 year ago
wc-admin-functions.php
1 year ago
wc-meta-box-functions.php
1 year ago
class-wc-admin-profile.php
272 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Add extra profile fields for users in admin |
| 4 | * |
| 5 | * @author WooThemes |
| 6 | * @category Admin |
| 7 | * @package WooCommerce\Admin |
| 8 | * @version 2.4.0 |
| 9 | */ |
| 10 | |
| 11 | if ( ! defined( 'ABSPATH' ) ) { |
| 12 | exit; // Exit if accessed directly |
| 13 | } |
| 14 | |
| 15 | if ( ! class_exists( 'WC_Admin_Profile', false ) ) : |
| 16 | |
| 17 | /** |
| 18 | * WC_Admin_Profile Class. |
| 19 | */ |
| 20 | class WC_Admin_Profile { |
| 21 | |
| 22 | /** |
| 23 | * Hook in tabs. |
| 24 | */ |
| 25 | public function __construct() { |
| 26 | add_action( 'show_user_profile', array( $this, 'add_customer_meta_fields' ) ); |
| 27 | add_action( 'edit_user_profile', array( $this, 'add_customer_meta_fields' ) ); |
| 28 | |
| 29 | add_action( 'personal_options_update', array( $this, 'save_customer_meta_fields' ) ); |
| 30 | add_action( 'edit_user_profile_update', array( $this, 'save_customer_meta_fields' ) ); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Get Address Fields for the edit user pages. |
| 35 | * |
| 36 | * @return array Fields to display which are filtered through woocommerce_customer_meta_fields before being returned |
| 37 | */ |
| 38 | public function get_customer_meta_fields() { |
| 39 | $show_fields = apply_filters( |
| 40 | 'woocommerce_customer_meta_fields', |
| 41 | array( |
| 42 | 'billing' => array( |
| 43 | 'title' => __( 'Customer billing address', 'woocommerce' ), |
| 44 | 'fields' => array( |
| 45 | 'billing_first_name' => array( |
| 46 | 'label' => __( 'First name', 'woocommerce' ), |
| 47 | 'description' => '', |
| 48 | ), |
| 49 | 'billing_last_name' => array( |
| 50 | 'label' => __( 'Last name', 'woocommerce' ), |
| 51 | 'description' => '', |
| 52 | ), |
| 53 | 'billing_company' => array( |
| 54 | 'label' => __( 'Company', 'woocommerce' ), |
| 55 | 'description' => '', |
| 56 | ), |
| 57 | 'billing_address_1' => array( |
| 58 | 'label' => __( 'Address line 1', 'woocommerce' ), |
| 59 | 'description' => '', |
| 60 | ), |
| 61 | 'billing_address_2' => array( |
| 62 | 'label' => __( 'Address line 2', 'woocommerce' ), |
| 63 | 'description' => '', |
| 64 | ), |
| 65 | 'billing_city' => array( |
| 66 | 'label' => __( 'City', 'woocommerce' ), |
| 67 | 'description' => '', |
| 68 | ), |
| 69 | 'billing_postcode' => array( |
| 70 | 'label' => __( 'Postcode / ZIP', 'woocommerce' ), |
| 71 | 'description' => '', |
| 72 | ), |
| 73 | 'billing_country' => array( |
| 74 | 'label' => __( 'Country / Region', 'woocommerce' ), |
| 75 | 'description' => '', |
| 76 | 'class' => 'js_field-country', |
| 77 | 'type' => 'select', |
| 78 | 'options' => array( '' => __( 'Select a country / region…', 'woocommerce' ) ) + WC()->countries->get_allowed_countries(), |
| 79 | ), |
| 80 | 'billing_state' => array( |
| 81 | 'label' => __( 'State / County', 'woocommerce' ), |
| 82 | 'description' => __( 'State / County or state code', 'woocommerce' ), |
| 83 | 'class' => 'js_field-state', |
| 84 | ), |
| 85 | 'billing_phone' => array( |
| 86 | 'label' => __( 'Phone', 'woocommerce' ), |
| 87 | 'description' => '', |
| 88 | ), |
| 89 | 'billing_email' => array( |
| 90 | 'label' => __( 'Email address', 'woocommerce' ), |
| 91 | 'description' => '', |
| 92 | ), |
| 93 | ), |
| 94 | ), |
| 95 | 'shipping' => array( |
| 96 | 'title' => __( 'Customer shipping address', 'woocommerce' ), |
| 97 | 'fields' => array( |
| 98 | 'copy_billing' => array( |
| 99 | 'label' => __( 'Copy from billing address', 'woocommerce' ), |
| 100 | 'description' => '', |
| 101 | 'class' => 'js_copy-billing', |
| 102 | 'type' => 'button', |
| 103 | 'text' => __( 'Copy', 'woocommerce' ), |
| 104 | ), |
| 105 | 'shipping_first_name' => array( |
| 106 | 'label' => __( 'First name', 'woocommerce' ), |
| 107 | 'description' => '', |
| 108 | ), |
| 109 | 'shipping_last_name' => array( |
| 110 | 'label' => __( 'Last name', 'woocommerce' ), |
| 111 | 'description' => '', |
| 112 | ), |
| 113 | 'shipping_company' => array( |
| 114 | 'label' => __( 'Company', 'woocommerce' ), |
| 115 | 'description' => '', |
| 116 | ), |
| 117 | 'shipping_address_1' => array( |
| 118 | 'label' => __( 'Address line 1', 'woocommerce' ), |
| 119 | 'description' => '', |
| 120 | ), |
| 121 | 'shipping_address_2' => array( |
| 122 | 'label' => __( 'Address line 2', 'woocommerce' ), |
| 123 | 'description' => '', |
| 124 | ), |
| 125 | 'shipping_city' => array( |
| 126 | 'label' => __( 'City', 'woocommerce' ), |
| 127 | 'description' => '', |
| 128 | ), |
| 129 | 'shipping_postcode' => array( |
| 130 | 'label' => __( 'Postcode / ZIP', 'woocommerce' ), |
| 131 | 'description' => '', |
| 132 | ), |
| 133 | 'shipping_country' => array( |
| 134 | 'label' => __( 'Country / Region', 'woocommerce' ), |
| 135 | 'description' => '', |
| 136 | 'class' => 'js_field-country', |
| 137 | 'type' => 'select', |
| 138 | 'options' => array( '' => __( 'Select a country / region…', 'woocommerce' ) ) + WC()->countries->get_allowed_countries(), |
| 139 | ), |
| 140 | 'shipping_state' => array( |
| 141 | 'label' => __( 'State / County', 'woocommerce' ), |
| 142 | 'description' => __( 'State / County or state code', 'woocommerce' ), |
| 143 | 'class' => 'js_field-state', |
| 144 | ), |
| 145 | 'shipping_phone' => array( |
| 146 | 'label' => __( 'Phone', 'woocommerce' ), |
| 147 | 'description' => '', |
| 148 | ), |
| 149 | ), |
| 150 | ), |
| 151 | ) |
| 152 | ); |
| 153 | return $show_fields; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Show Address Fields on edit user pages. |
| 158 | * |
| 159 | * @param WP_User $user |
| 160 | */ |
| 161 | public function add_customer_meta_fields( $user ) { |
| 162 | if ( ! apply_filters( 'woocommerce_current_user_can_edit_customer_meta_fields', current_user_can( 'manage_woocommerce' ), $user->ID ) ) { |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | $show_fields = $this->get_customer_meta_fields(); |
| 167 | |
| 168 | foreach ( $show_fields as $fieldset_key => $fieldset ) : |
| 169 | ?> |
| 170 | <h2><?php echo $fieldset['title']; ?></h2> |
| 171 | <table class="form-table" id="<?php echo esc_attr( 'fieldset-' . $fieldset_key ); ?>"> |
| 172 | <?php foreach ( $fieldset['fields'] as $key => $field ) : ?> |
| 173 | <tr> |
| 174 | <th> |
| 175 | <label for="<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $field['label'] ); ?></label> |
| 176 | </th> |
| 177 | <td> |
| 178 | <?php if ( ! empty( $field['type'] ) && 'select' === $field['type'] ) : ?> |
| 179 | <select name="<?php echo esc_attr( $key ); ?>" id="<?php echo esc_attr( $key ); ?>" class="<?php echo esc_attr( $field['class'] ); ?>" style="width: 25em;"> |
| 180 | <?php |
| 181 | $selected = esc_attr( get_user_meta( $user->ID, $key, true ) ); |
| 182 | foreach ( $field['options'] as $option_key => $option_value ) : |
| 183 | ?> |
| 184 | <option value="<?php echo esc_attr( $option_key ); ?>" <?php selected( $selected, $option_key, true ); ?>><?php echo esc_html( $option_value ); ?></option> |
| 185 | <?php endforeach; ?> |
| 186 | </select> |
| 187 | <?php elseif ( ! empty( $field['type'] ) && 'checkbox' === $field['type'] ) : ?> |
| 188 | <input type="checkbox" name="<?php echo esc_attr( $key ); ?>" id="<?php echo esc_attr( $key ); ?>" value="1" class="<?php echo esc_attr( $field['class'] ); ?>" <?php checked( (int) get_user_meta( $user->ID, $key, true ), 1, true ); ?> /> |
| 189 | <?php elseif ( ! empty( $field['type'] ) && 'button' === $field['type'] ) : ?> |
| 190 | <button type="button" id="<?php echo esc_attr( $key ); ?>" class="button <?php echo esc_attr( $field['class'] ); ?>"><?php echo esc_html( $field['text'] ); ?></button> |
| 191 | <?php else : ?> |
| 192 | <input type="text" name="<?php echo esc_attr( $key ); ?>" id="<?php echo esc_attr( $key ); ?>" value="<?php echo esc_attr( $this->get_user_meta( $user->ID, $key ) ); ?>" class="<?php echo ( ! empty( $field['class'] ) ? esc_attr( $field['class'] ) : 'regular-text' ); ?>" /> |
| 193 | <?php endif; ?> |
| 194 | <p class="description"><?php echo wp_kses_post( $field['description'] ); ?></p> |
| 195 | </td> |
| 196 | </tr> |
| 197 | <?php endforeach; ?> |
| 198 | </table> |
| 199 | <?php |
| 200 | endforeach; |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Save Address Fields on edit user pages. |
| 205 | * |
| 206 | * @param int $user_id User ID of the user being saved |
| 207 | */ |
| 208 | public function save_customer_meta_fields( $user_id ) { |
| 209 | if ( ! apply_filters( 'woocommerce_current_user_can_edit_customer_meta_fields', current_user_can( 'manage_woocommerce' ), $user_id ) ) { |
| 210 | return; |
| 211 | } |
| 212 | |
| 213 | $save_fields = $this->get_customer_meta_fields(); |
| 214 | |
| 215 | foreach ( $save_fields as $fieldset_type => $fieldset ) { |
| 216 | |
| 217 | foreach ( $fieldset['fields'] as $key => $field ) { |
| 218 | |
| 219 | if ( isset( $field['type'] ) && 'checkbox' === $field['type'] ) { |
| 220 | update_user_meta( $user_id, $key, isset( $_POST[ $key ] ) ); |
| 221 | } elseif ( isset( $_POST[ $key ] ) ) { |
| 222 | update_user_meta( $user_id, $key, wc_clean( $_POST[ $key ] ) ); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | // Skip firing the action for any non-internal fieldset types. |
| 227 | if ( ! in_array( $fieldset_type, array( 'billing', 'shipping' ), true ) ) { |
| 228 | continue; |
| 229 | } |
| 230 | |
| 231 | // Fieldset type is an internal address type. |
| 232 | $address_type = $fieldset_type; |
| 233 | |
| 234 | /** |
| 235 | * Hook: woocommerce_customer_save_address. |
| 236 | * |
| 237 | * Fires after a customer address has been saved on the user profile admin screen. |
| 238 | * |
| 239 | * @since 8.5.0 |
| 240 | * @param int $user_id User ID being saved. |
| 241 | * @param string $address_type Type of address; 'billing' or 'shipping'. |
| 242 | */ |
| 243 | do_action( 'woocommerce_customer_save_address', $user_id, $address_type ); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Get user meta for a given key, with fallbacks to core user info for pre-existing fields. |
| 249 | * |
| 250 | * @since 3.1.0 |
| 251 | * @param int $user_id User ID of the user being edited |
| 252 | * @param string $key Key for user meta field |
| 253 | * @return string |
| 254 | */ |
| 255 | protected function get_user_meta( $user_id, $key ) { |
| 256 | $value = get_user_meta( $user_id, $key, true ); |
| 257 | $existing_fields = array( 'billing_first_name', 'billing_last_name' ); |
| 258 | if ( ! $value && in_array( $key, $existing_fields ) ) { |
| 259 | $value = get_user_meta( $user_id, str_replace( 'billing_', '', $key ), true ); |
| 260 | } elseif ( ! $value && ( 'billing_email' === $key ) ) { |
| 261 | $user = get_userdata( $user_id ); |
| 262 | $value = $user->user_email; |
| 263 | } |
| 264 | |
| 265 | return $value; |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | endif; |
| 270 | |
| 271 | return new WC_Admin_Profile(); |
| 272 |