WC_Admin_Profile

WC_Admin_Profile Class.

Defined (1)

The class is defined in the following location(s).

/includes/admin/class-wc-admin-profile.php  
  1. class WC_Admin_Profile { 
  2.  
  3. /** 
  4. * Hook in tabs. 
  5. */ 
  6. public function __construct() { 
  7. add_action( 'show_user_profile', array( $this, 'add_customer_meta_fields' ) ); 
  8. add_action( 'edit_user_profile', array( $this, 'add_customer_meta_fields' ) ); 
  9.  
  10. add_action( 'personal_options_update', array( $this, 'save_customer_meta_fields' ) ); 
  11. add_action( 'edit_user_profile_update', array( $this, 'save_customer_meta_fields' ) ); 
  12.  
  13. /** 
  14. * Get Address Fields for the edit user pages. 
  15. * @return array Fields to display which are filtered through woocommerce_customer_meta_fields before being returned 
  16. */ 
  17. public function get_customer_meta_fields() { 
  18. $show_fields = apply_filters('woocommerce_customer_meta_fields', array( 
  19. 'billing' => array( 
  20. 'title' => __( 'Customer Billing Address', 'woocommerce' ),  
  21. 'fields' => array( 
  22. 'billing_first_name' => array( 
  23. 'label' => __( 'First name', 'woocommerce' ),  
  24. 'description' => '' 
  25. ),  
  26. 'billing_last_name' => array( 
  27. 'label' => __( 'Last name', 'woocommerce' ),  
  28. 'description' => '' 
  29. ),  
  30. 'billing_company' => array( 
  31. 'label' => __( 'Company', 'woocommerce' ),  
  32. 'description' => '' 
  33. ),  
  34. 'billing_address_1' => array( 
  35. 'label' => __( 'Address 1', 'woocommerce' ),  
  36. 'description' => '' 
  37. ),  
  38. 'billing_address_2' => array( 
  39. 'label' => __( 'Address 2', 'woocommerce' ),  
  40. 'description' => '' 
  41. ),  
  42. 'billing_city' => array( 
  43. 'label' => __( 'City', 'woocommerce' ),  
  44. 'description' => '' 
  45. ),  
  46. 'billing_postcode' => array( 
  47. 'label' => __( 'Postcode', 'woocommerce' ),  
  48. 'description' => '' 
  49. ),  
  50. 'billing_country' => array( 
  51. 'label' => __( 'Country', 'woocommerce' ),  
  52. 'description' => '',  
  53. 'class' => 'js_field-country',  
  54. 'type' => 'select',  
  55. 'options' => array( '' => __( 'Select a country…', 'woocommerce' ) ) + WC()->countries->get_allowed_countries() 
  56. ),  
  57. 'billing_state' => array( 
  58. 'label' => __( 'State/County', 'woocommerce' ),  
  59. 'description' => __( 'State/County or state code', 'woocommerce' ),  
  60. 'class' => 'js_field-state' 
  61. ),  
  62. 'billing_phone' => array( 
  63. 'label' => __( 'Telephone', 'woocommerce' ),  
  64. 'description' => '' 
  65. ),  
  66. 'billing_email' => array( 
  67. 'label' => __( 'Email', 'woocommerce' ),  
  68. 'description' => '' 
  69. ),  
  70. 'shipping' => array( 
  71. 'title' => __( 'Customer Shipping Address', 'woocommerce' ),  
  72. 'fields' => array( 
  73. 'shipping_first_name' => array( 
  74. 'label' => __( 'First name', 'woocommerce' ),  
  75. 'description' => '' 
  76. ),  
  77. 'shipping_last_name' => array( 
  78. 'label' => __( 'Last name', 'woocommerce' ),  
  79. 'description' => '' 
  80. ),  
  81. 'shipping_company' => array( 
  82. 'label' => __( 'Company', 'woocommerce' ),  
  83. 'description' => '' 
  84. ),  
  85. 'shipping_address_1' => array( 
  86. 'label' => __( 'Address 1', 'woocommerce' ),  
  87. 'description' => '' 
  88. ),  
  89. 'shipping_address_2' => array( 
  90. 'label' => __( 'Address 2', 'woocommerce' ),  
  91. 'description' => '' 
  92. ),  
  93. 'shipping_city' => array( 
  94. 'label' => __( 'City', 'woocommerce' ),  
  95. 'description' => '' 
  96. ),  
  97. 'shipping_postcode' => array( 
  98. 'label' => __( 'Postcode', 'woocommerce' ),  
  99. 'description' => '' 
  100. ),  
  101. 'shipping_country' => array( 
  102. 'label' => __( 'Country', 'woocommerce' ),  
  103. 'description' => '',  
  104. 'class' => 'js_field-country',  
  105. 'type' => 'select',  
  106. 'options' => array( '' => __( 'Select a country…', 'woocommerce' ) ) + WC()->countries->get_allowed_countries() 
  107. ),  
  108. 'shipping_state' => array( 
  109. 'label' => __( 'State/County', 'woocommerce' ),  
  110. 'description' => __( 'State/County or state code', 'woocommerce' ),  
  111. 'class' => 'js_field-state' 
  112. ) ); 
  113. return $show_fields; 
  114.  
  115. /** 
  116. * Show Address Fields on edit user pages. 
  117. * @param WP_User $user 
  118. */ 
  119. public function add_customer_meta_fields( $user ) { 
  120. if ( ! current_user_can( 'manage_woocommerce' ) ) { 
  121. return; 
  122.  
  123. $show_fields = $this->get_customer_meta_fields(); 
  124.  
  125. foreach ( $show_fields as $fieldset ) : 
  126. ?> 
  127. <h2><?php echo $fieldset['title']; ?></h2> 
  128. <table class="form-table"> 
  129. <?php 
  130. foreach ( $fieldset['fields'] as $key => $field ) : 
  131. ?> 
  132. <tr> 
  133. <th><label for="<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $field['label'] ); ?></label></th> 
  134. <td> 
  135. <?php if ( ! empty( $field['type'] ) && 'select' == $field['type'] ) : ?> 
  136. <select name="<?php echo esc_attr( $key ); ?>" id="<?php echo esc_attr( $key ); ?>" class="<?php echo ( ! empty( $field['class'] ) ? $field['class'] : '' ); ?>" style="width: 25em;"> 
  137. <?php 
  138. $selected = esc_attr( get_user_meta( $user->ID, $key, true ) ); 
  139. foreach ( $field['options'] as $option_key => $option_value ) : ?> 
  140. <option value="<?php echo esc_attr( $option_key ); ?>" <?php selected( $selected, $option_key, true ); ?>><?php echo esc_attr( $option_value ); ?></option> 
  141. <?php endforeach; ?> 
  142. </select> 
  143. <?php else : ?> 
  144. <input type="text" name="<?php echo esc_attr( $key ); ?>" id="<?php echo esc_attr( $key ); ?>" value="<?php echo esc_attr( get_user_meta( $user->ID, $key, true ) ); ?>" class="<?php echo ( ! empty( $field['class'] ) ? $field['class'] : 'regular-text' ); ?>" /> 
  145. <?php endif; ?> 
  146. <br/> 
  147. <span class="description"><?php echo wp_kses_post( $field['description'] ); ?></span> 
  148. </td> 
  149. </tr> 
  150. <?php 
  151. endforeach; 
  152. ?> 
  153. </table> 
  154. <?php 
  155. endforeach; 
  156.  
  157. /** 
  158. * Save Address Fields on edit user pages. 
  159. * @param int $user_id User ID of the user being saved 
  160. */ 
  161. public function save_customer_meta_fields( $user_id ) { 
  162. $save_fields = $this->get_customer_meta_fields(); 
  163.  
  164. foreach ( $save_fields as $fieldset ) { 
  165.  
  166. foreach ( $fieldset['fields'] as $key => $field ) { 
  167.  
  168. if ( isset( $_POST[ $key ] ) ) { 
  169. update_user_meta( $user_id, $key, wc_clean( $_POST[ $key ] ) );