class-wc-admin-profile.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. if ( ! defined( 'ABSPATH' ) ) {
  11. exit; // Exit if accessed directly
  12. }
  13. if ( ! class_exists( 'WC_Admin_Profile', false ) ) :
  14. /**
  15. * WC_Admin_Profile Class.
  16. */
  17. class WC_Admin_Profile {
  18. /**
  19. * Hook in tabs.
  20. */
  21. public function __construct() {
  22. add_action( 'show_user_profile', array( $this, 'add_customer_meta_fields' ) );
  23. add_action( 'edit_user_profile', array( $this, 'add_customer_meta_fields' ) );
  24. add_action( 'personal_options_update', array( $this, 'save_customer_meta_fields' ) );
  25. add_action( 'edit_user_profile_update', array( $this, 'save_customer_meta_fields' ) );
  26. }
  27. /**
  28. * Get Address Fields for the edit user pages.
  29. *
  30. * @return array Fields to display which are filtered through woocommerce_customer_meta_fields before being returned
  31. */
  32. public function get_customer_meta_fields() {
  33. $show_fields = apply_filters(
  34. 'woocommerce_customer_meta_fields', array(
  35. 'billing' => array(
  36. 'title' => __( 'Customer billing address', 'woocommerce' ),
  37. 'fields' => array(
  38. 'billing_first_name' => array(
  39. 'label' => __( 'First name', 'woocommerce' ),
  40. 'description' => '',
  41. ),
  42. 'billing_last_name' => array(
  43. 'label' => __( 'Last name', 'woocommerce' ),
  44. 'description' => '',
  45. ),
  46. 'billing_company' => array(
  47. 'label' => __( 'Company', 'woocommerce' ),
  48. 'description' => '',
  49. ),
  50. 'billing_address_1' => array(
  51. 'label' => __( 'Address line 1', 'woocommerce' ),
  52. 'description' => '',
  53. ),
  54. 'billing_address_2' => array(
  55. 'label' => __( 'Address line 2', 'woocommerce' ),
  56. 'description' => '',
  57. ),
  58. 'billing_city' => array(
  59. 'label' => __( 'City', 'woocommerce' ),
  60. 'description' => '',
  61. ),
  62. 'billing_postcode' => array(
  63. 'label' => __( 'Postcode / ZIP', 'woocommerce' ),
  64. 'description' => '',
  65. ),
  66. 'billing_country' => array(
  67. 'label' => __( 'Country', 'woocommerce' ),
  68. 'description' => '',
  69. 'class' => 'js_field-country',
  70. 'type' => 'select',
  71. 'options' => array( '' => __( 'Select a country&hellip;', 'woocommerce' ) ) + WC()->countries->get_allowed_countries(),
  72. ),
  73. 'billing_state' => array(
  74. 'label' => __( 'State / County', 'woocommerce' ),
  75. 'description' => __( 'State / County or state code', 'woocommerce' ),
  76. 'class' => 'js_field-state',
  77. ),
  78. 'billing_phone' => array(
  79. 'label' => __( 'Phone', 'woocommerce' ),
  80. 'description' => '',
  81. ),
  82. 'billing_email' => array(
  83. 'label' => __( 'Email address', 'woocommerce' ),
  84. 'description' => '',
  85. ),
  86. ),
  87. ),
  88. 'shipping' => array(
  89. 'title' => __( 'Customer shipping address', 'woocommerce' ),
  90. 'fields' => array(
  91. 'copy_billing' => array(
  92. 'label' => __( 'Copy from billing address', 'woocommerce' ),
  93. 'description' => '',
  94. 'class' => 'js_copy-billing',
  95. 'type' => 'button',
  96. 'text' => __( 'Copy', 'woocommerce' ),
  97. ),
  98. 'shipping_first_name' => array(
  99. 'label' => __( 'First name', 'woocommerce' ),
  100. 'description' => '',
  101. ),
  102. 'shipping_last_name' => array(
  103. 'label' => __( 'Last name', 'woocommerce' ),
  104. 'description' => '',
  105. ),
  106. 'shipping_company' => array(
  107. 'label' => __( 'Company', 'woocommerce' ),
  108. 'description' => '',
  109. ),
  110. 'shipping_address_1' => array(
  111. 'label' => __( 'Address line 1', 'woocommerce' ),
  112. 'description' => '',
  113. ),
  114. 'shipping_address_2' => array(
  115. 'label' => __( 'Address line 2', 'woocommerce' ),
  116. 'description' => '',
  117. ),
  118. 'shipping_city' => array(
  119. 'label' => __( 'City', 'woocommerce' ),
  120. 'description' => '',
  121. ),
  122. 'shipping_postcode' => array(
  123. 'label' => __( 'Postcode / ZIP', 'woocommerce' ),
  124. 'description' => '',
  125. ),
  126. 'shipping_country' => array(
  127. 'label' => __( 'Country', 'woocommerce' ),
  128. 'description' => '',
  129. 'class' => 'js_field-country',
  130. 'type' => 'select',
  131. 'options' => array( '' => __( 'Select a country&hellip;', 'woocommerce' ) ) + WC()->countries->get_allowed_countries(),
  132. ),
  133. 'shipping_state' => array(
  134. 'label' => __( 'State / County', 'woocommerce' ),
  135. 'description' => __( 'State / County or state code', 'woocommerce' ),
  136. 'class' => 'js_field-state',
  137. ),
  138. ),
  139. ),
  140. )
  141. );
  142. return $show_fields;
  143. }
  144. /**
  145. * Show Address Fields on edit user pages.
  146. *
  147. * @param WP_User $user
  148. */
  149. public function add_customer_meta_fields( $user ) {
  150. if ( ! current_user_can( 'manage_woocommerce' ) ) {
  151. return;
  152. }
  153. $show_fields = $this->get_customer_meta_fields();
  154. foreach ( $show_fields as $fieldset_key => $fieldset ) :
  155. ?>
  156. <h2><?php echo $fieldset['title']; ?></h2>
  157. <table class="form-table" id="<?php echo esc_attr( 'fieldset-' . $fieldset_key ); ?>">
  158. <?php foreach ( $fieldset['fields'] as $key => $field ) : ?>
  159. <tr>
  160. <th>
  161. <label for="<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $field['label'] ); ?></label>
  162. </th>
  163. <td>
  164. <?php if ( ! empty( $field['type'] ) && 'select' === $field['type'] ) : ?>
  165. <select name="<?php echo esc_attr( $key ); ?>" id="<?php echo esc_attr( $key ); ?>" class="<?php echo esc_attr( $field['class'] ); ?>" style="width: 25em;">
  166. <?php
  167. $selected = esc_attr( get_user_meta( $user->ID, $key, true ) );
  168. foreach ( $field['options'] as $option_key => $option_value ) :
  169. ?>
  170. <option value="<?php echo esc_attr( $option_key ); ?>" <?php selected( $selected, $option_key, true ); ?>><?php echo esc_attr( $option_value ); ?></option>
  171. <?php endforeach; ?>
  172. </select>
  173. <?php elseif ( ! empty( $field['type'] ) && 'checkbox' === $field['type'] ) : ?>
  174. <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 ); ?> />
  175. <?php elseif ( ! empty( $field['type'] ) && 'button' === $field['type'] ) : ?>
  176. <button type="button" id="<?php echo esc_attr( $key ); ?>" class="button <?php echo esc_attr( $field['class'] ); ?>"><?php echo esc_html( $field['text'] ); ?></button>
  177. <?php else : ?>
  178. <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' ); ?>" />
  179. <?php endif; ?>
  180. <br/>
  181. <span class="description"><?php echo wp_kses_post( $field['description'] ); ?></span>
  182. </td>
  183. </tr>
  184. <?php endforeach; ?>
  185. </table>
  186. <?php
  187. endforeach;
  188. }
  189. /**
  190. * Save Address Fields on edit user pages.
  191. *
  192. * @param int $user_id User ID of the user being saved
  193. */
  194. public function save_customer_meta_fields( $user_id ) {
  195. $save_fields = $this->get_customer_meta_fields();
  196. foreach ( $save_fields as $fieldset ) {
  197. foreach ( $fieldset['fields'] as $key => $field ) {
  198. if ( isset( $field['type'] ) && 'checkbox' === $field['type'] ) {
  199. update_user_meta( $user_id, $key, isset( $_POST[ $key ] ) );
  200. } elseif ( isset( $_POST[ $key ] ) ) {
  201. update_user_meta( $user_id, $key, wc_clean( $_POST[ $key ] ) );
  202. }
  203. }
  204. }
  205. }
  206. /**
  207. * Get user meta for a given key, with fallbacks to core user info for pre-existing fields.
  208. *
  209. * @since 3.1.0
  210. * @param int $user_id User ID of the user being edited
  211. * @param string $key Key for user meta field
  212. * @return string
  213. */
  214. protected function get_user_meta( $user_id, $key ) {
  215. $value = get_user_meta( $user_id, $key, true );
  216. $existing_fields = array( 'billing_first_name', 'billing_last_name' );
  217. if ( ! $value && in_array( $key, $existing_fields ) ) {
  218. $value = get_user_meta( $user_id, str_replace( 'billing_', '', $key ), true );
  219. } elseif ( ! $value && ( 'billing_email' === $key ) ) {
  220. $user = get_userdata( $user_id );
  221. $value = $user->user_email;
  222. }
  223. return $value;
  224. }
  225. }
  226. endif;
  227. return new WC_Admin_Profile();