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 line 1', 'woocommerce' ),  
  36. 'description' => '',  
  37. ),  
  38. 'billing_address_2' => array( 
  39. 'label' => __( 'Address line 2', 'woocommerce' ),  
  40. 'description' => '',  
  41. ),  
  42. 'billing_city' => array( 
  43. 'label' => __( 'City', 'woocommerce' ),  
  44. 'description' => '',  
  45. ),  
  46. 'billing_postcode' => array( 
  47. 'label' => __( 'Postcode / ZIP', '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' => __( 'Phone', 'woocommerce' ),  
  64. 'description' => '',  
  65. ),  
  66. 'billing_email' => array( 
  67. 'label' => __( 'Email address', 'woocommerce' ),  
  68. 'description' => '',  
  69. ),  
  70. ),  
  71. ),  
  72. 'shipping' => array( 
  73. 'title' => __( 'Customer shipping address', 'woocommerce' ),  
  74. 'fields' => array( 
  75. 'shipping_first_name' => array( 
  76. 'label' => __( 'First name', 'woocommerce' ),  
  77. 'description' => '',  
  78. ),  
  79. 'shipping_last_name' => array( 
  80. 'label' => __( 'Last name', 'woocommerce' ),  
  81. 'description' => '',  
  82. ),  
  83. 'shipping_company' => array( 
  84. 'label' => __( 'Company', 'woocommerce' ),  
  85. 'description' => '',  
  86. ),  
  87. 'shipping_address_1' => array( 
  88. 'label' => __( 'Address line 1', 'woocommerce' ),  
  89. 'description' => '',  
  90. ),  
  91. 'shipping_address_2' => array( 
  92. 'label' => __( 'Address line 2', 'woocommerce' ),  
  93. 'description' => '',  
  94. ),  
  95. 'shipping_city' => array( 
  96. 'label' => __( 'City', 'woocommerce' ),  
  97. 'description' => '',  
  98. ),  
  99. 'shipping_postcode' => array( 
  100. 'label' => __( 'Postcode / ZIP', 'woocommerce' ),  
  101. 'description' => '',  
  102. ),  
  103. 'shipping_country' => array( 
  104. 'label' => __( 'Country', 'woocommerce' ),  
  105. 'description' => '',  
  106. 'class' => 'js_field-country',  
  107. 'type' => 'select',  
  108. 'options' => array( '' => __( 'Select a country…', 'woocommerce' ) ) + WC()->countries->get_allowed_countries(),  
  109. ),  
  110. 'shipping_state' => array( 
  111. 'label' => __( 'State / County', 'woocommerce' ),  
  112. 'description' => __( 'State / County or state code', 'woocommerce' ),  
  113. 'class' => 'js_field-state',  
  114. ),  
  115. ),  
  116. ),  
  117. ) ); 
  118. return $show_fields; 
  119.  
  120. /** 
  121. * Show Address Fields on edit user pages. 
  122. * @param WP_User $user 
  123. */ 
  124. public function add_customer_meta_fields( $user ) { 
  125. if ( ! current_user_can( 'manage_woocommerce' ) ) { 
  126. return; 
  127.  
  128. $show_fields = $this->get_customer_meta_fields(); 
  129.  
  130. foreach ( $show_fields as $fieldset ) : 
  131. ?> 
  132. <h2><?php echo $fieldset['title']; ?></h2> 
  133. <table class="form-table"> 
  134. <?php 
  135. foreach ( $fieldset['fields'] as $key => $field ) : 
  136. ?> 
  137. <tr> 
  138. <th><label for="<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $field['label'] ); ?></label></th> 
  139. <td> 
  140. <?php if ( ! empty( $field['type'] ) && 'select' === $field['type'] ) : ?> 
  141. <select name="<?php echo esc_attr( $key ); ?>" id="<?php echo esc_attr( $key ); ?>" class="<?php echo ( ! empty( $field['class'] ) ? $field['class'] : '' ); ?>" style="width: 25em;"> 
  142. <?php 
  143. $selected = esc_attr( get_user_meta( $user->ID, $key, true ) ); 
  144. foreach ( $field['options'] as $option_key => $option_value ) : ?> 
  145. <option value="<?php echo esc_attr( $option_key ); ?>" <?php selected( $selected, $option_key, true ); ?>><?php echo esc_attr( $option_value ); ?></option> 
  146. <?php endforeach; ?> 
  147. </select> 
  148. <?php elseif ( ! empty( $field['type'] ) && 'checkbox' === $field['type'] ) : ?> 
  149. <input type="checkbox" name="<?php echo esc_attr( $key ); ?>" id="<?php echo esc_attr( $key ); ?>" value="1" class="<?php echo ( ! empty( $field['class'] ) ? $field['class'] : '' ); ?>" <?php checked( (int) get_user_meta( $user->ID, $key, true ), 1, true ); ?> /> 
  150. <?php else : ?> 
  151. <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' ); ?>" /> 
  152. <?php endif; ?> 
  153. <br/> 
  154. <span class="description"><?php echo wp_kses_post( $field['description'] ); ?></span> 
  155. </td> 
  156. </tr> 
  157. <?php 
  158. endforeach; 
  159. ?> 
  160. </table> 
  161. <?php 
  162. endforeach; 
  163.  
  164. /** 
  165. * Save Address Fields on edit user pages. 
  166. * @param int $user_id User ID of the user being saved 
  167. */ 
  168. public function save_customer_meta_fields( $user_id ) { 
  169. $save_fields = $this->get_customer_meta_fields(); 
  170.  
  171. foreach ( $save_fields as $fieldset ) { 
  172.  
  173. foreach ( $fieldset['fields'] as $key => $field ) { 
  174.  
  175. if ( isset( $field['type'] ) && 'checkbox' === $field['type'] ) { 
  176. update_user_meta( $user_id, $key, isset( $_POST[ $key ] ) ); 
  177. } elseif ( isset( $_POST[ $key ] ) ) { 
  178. update_user_meta( $user_id, $key, wc_clean( $_POST[ $key ] ) );