/wp-content/plugins/woocommerce/includes/admin/class-wc-admin-profile.php

https://gitlab.com/campus-academy/krowkaramel · PHP · 252 lines · 197 code · 18 blank · 37 comment · 14 complexity · ada960246acc83f18d8e029e47689493 MD5 · raw file

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