PageRenderTime 25ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/haque.mdmanzurul/barongbarong
PHP | 304 lines | 212 code | 46 blank | 46 comment | 21 complexity | 00cae268b338244bedcaadc28d61f0d1 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.1.0
  9. */
  10. if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
  11. if ( ! class_exists( 'WC_Admin_Profile' ) ) :
  12. /**
  13. * WC_Admin_Profile Class
  14. */
  15. class WC_Admin_Profile {
  16. /**
  17. * Hook in tabs.
  18. */
  19. public function __construct() {
  20. add_action( 'show_user_profile', array( $this, 'add_customer_meta_fields' ) );
  21. add_action( 'edit_user_profile', array( $this, 'add_customer_meta_fields' ) );
  22. add_action( 'personal_options_update', array( $this, 'save_customer_meta_fields' ) );
  23. add_action( 'edit_user_profile_update', array( $this, 'save_customer_meta_fields' ) );
  24. add_action( 'show_user_profile', array( $this, 'add_api_key_field' ) );
  25. add_action( 'edit_user_profile', array( $this, 'add_api_key_field' ) );
  26. add_action( 'personal_options_update', array( $this, 'generate_api_key' ) );
  27. add_action( 'edit_user_profile_update', array( $this, 'generate_api_key' ) );
  28. }
  29. /**
  30. * Get Address Fields for the edit user pages.
  31. *
  32. * @return array Fields to display which are filtered through woocommerce_customer_meta_fields before being returned
  33. */
  34. public function get_customer_meta_fields() {
  35. $show_fields = apply_filters('woocommerce_customer_meta_fields', 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 1', 'woocommerce' ),
  53. 'description' => ''
  54. ),
  55. 'billing_address_2' => array(
  56. 'label' => __( 'Address 2', 'woocommerce' ),
  57. 'description' => ''
  58. ),
  59. 'billing_city' => array(
  60. 'label' => __( 'City', 'woocommerce' ),
  61. 'description' => ''
  62. ),
  63. 'billing_postcode' => array(
  64. 'label' => __( 'Postcode', 'woocommerce' ),
  65. 'description' => ''
  66. ),
  67. 'billing_state' => array(
  68. 'label' => __( 'State/County', 'woocommerce' ),
  69. 'description' => __( 'State/County or state code', 'woocommerce' ),
  70. ),
  71. 'billing_country' => array(
  72. 'label' => __( 'Country', 'woocommerce' ),
  73. 'description' => __( '2 letter Country code', 'woocommerce' ),
  74. ),
  75. 'billing_phone' => array(
  76. 'label' => __( 'Telephone', 'woocommerce' ),
  77. 'description' => ''
  78. ),
  79. 'billing_email' => array(
  80. 'label' => __( 'Email', 'woocommerce' ),
  81. 'description' => ''
  82. )
  83. )
  84. ),
  85. 'shipping' => array(
  86. 'title' => __( 'Customer Shipping Address', 'woocommerce' ),
  87. 'fields' => array(
  88. 'shipping_first_name' => array(
  89. 'label' => __( 'First name', 'woocommerce' ),
  90. 'description' => ''
  91. ),
  92. 'shipping_last_name' => array(
  93. 'label' => __( 'Last name', 'woocommerce' ),
  94. 'description' => ''
  95. ),
  96. 'shipping_company' => array(
  97. 'label' => __( 'Company', 'woocommerce' ),
  98. 'description' => ''
  99. ),
  100. 'shipping_address_1' => array(
  101. 'label' => __( 'Address 1', 'woocommerce' ),
  102. 'description' => ''
  103. ),
  104. 'shipping_address_2' => array(
  105. 'label' => __( 'Address 2', 'woocommerce' ),
  106. 'description' => ''
  107. ),
  108. 'shipping_city' => array(
  109. 'label' => __( 'City', 'woocommerce' ),
  110. 'description' => ''
  111. ),
  112. 'shipping_postcode' => array(
  113. 'label' => __( 'Postcode', 'woocommerce' ),
  114. 'description' => ''
  115. ),
  116. 'shipping_state' => array(
  117. 'label' => __( 'State/County', 'woocommerce' ),
  118. 'description' => __( 'State/County or state code', 'woocommerce' )
  119. ),
  120. 'shipping_country' => array(
  121. 'label' => __( 'Country', 'woocommerce' ),
  122. 'description' => __( '2 letter Country code', 'woocommerce' )
  123. )
  124. )
  125. )
  126. ));
  127. return $show_fields;
  128. }
  129. /**
  130. * Show Address Fields on edit user pages.
  131. *
  132. * @param mixed $user User (object) being displayed
  133. */
  134. public function add_customer_meta_fields( $user ) {
  135. if ( ! current_user_can( 'manage_woocommerce' ) )
  136. return;
  137. $show_fields = $this->get_customer_meta_fields();
  138. foreach( $show_fields as $fieldset ) :
  139. ?>
  140. <h3><?php echo $fieldset['title']; ?></h3>
  141. <table class="form-table">
  142. <?php
  143. foreach( $fieldset['fields'] as $key => $field ) :
  144. ?>
  145. <tr>
  146. <th><label for="<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $field['label'] ); ?></label></th>
  147. <td>
  148. <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="regular-text" /><br/>
  149. <span class="description"><?php echo wp_kses_post( $field['description'] ); ?></span>
  150. </td>
  151. </tr>
  152. <?php
  153. endforeach;
  154. ?>
  155. </table>
  156. <?php
  157. endforeach;
  158. }
  159. /**
  160. * Save Address Fields on edit user pages
  161. *
  162. * @param mixed $user_id User ID of the user being saved
  163. */
  164. public function save_customer_meta_fields( $user_id ) {
  165. $save_fields = $this->get_customer_meta_fields();
  166. foreach( $save_fields as $fieldset )
  167. foreach( $fieldset['fields'] as $key => $field )
  168. if ( isset( $_POST[ $key ] ) )
  169. update_user_meta( $user_id, $key, wc_clean( $_POST[ $key ] ) );
  170. }
  171. /**
  172. * Display the API key info for a user
  173. *
  174. * @since 2.1
  175. * @param WP_User $user
  176. */
  177. public function add_api_key_field( $user ) {
  178. if ( ! current_user_can( 'manage_woocommerce' ) )
  179. return;
  180. $permissions = array(
  181. 'read' => __( 'Read', 'woocommerce' ),
  182. 'write' => __( 'Write', 'woocommerce' ),
  183. 'read_write' => __( 'Read/Write', 'woocommerce' ),
  184. );
  185. if ( current_user_can( 'edit_user', $user->ID ) ) {
  186. ?>
  187. <table class="form-table">
  188. <tbody>
  189. <tr>
  190. <th><label for="woocommerce_api_keys"><?php _e( 'WooCommerce API Keys', 'woocommerce' ); ?></label></th>
  191. <td>
  192. <?php if ( empty( $user->woocommerce_api_consumer_key ) ) : ?>
  193. <input name="woocommerce_generate_api_key" type="checkbox" id="woocommerce_generate_api_key" value="0" />
  194. <span class="description"><?php _e( 'Generate API Key', 'woocommerce' ); ?></span>
  195. <?php else : ?>
  196. <strong><?php _e( 'Consumer Key:', 'woocommerce' ); ?>&nbsp;</strong><code id="woocommerce_api_consumer_key"><?php echo $user->woocommerce_api_consumer_key ?></code><br/>
  197. <strong><?php _e( 'Consumer Secret:', 'woocommerce' ); ?>&nbsp;</strong><code id="woocommerce_api_consumer_secret"><?php echo $user->woocommerce_api_consumer_secret; ?></code><br/>
  198. <strong><?php _e( 'Permissions:', 'woocommerce' ); ?>&nbsp;</strong><span id="woocommerce_api_key_permissions"><select name="woocommerce_api_key_permissions" id="woocommerce_api_key_permissions"><?php
  199. foreach ( $permissions as $permission_key => $permission_name ) { echo '<option value="' . esc_attr( $permission_key ) . '" '.selected($permission_key, $user->woocommerce_api_key_permissions, false).'>'.esc_html( $permission_name ) . '</option>';} ?>
  200. </select></span><br/>
  201. <input name="woocommerce_generate_api_key" type="checkbox" id="woocommerce_generate_api_key" value="0" />
  202. <span class="description"><?php _e( 'Revoke API Key', 'woocommerce' ); ?></span>
  203. <?php endif; ?>
  204. </td>
  205. </tr>
  206. </tbody>
  207. </table>
  208. <?php
  209. }
  210. }
  211. /**
  212. * Generate and save (or delete) the API keys for a user
  213. *
  214. * @since 2.1
  215. * @param int $user_id
  216. */
  217. public function generate_api_key( $user_id ) {
  218. if ( current_user_can( 'edit_user', $user_id ) ) {
  219. $user = get_userdata( $user_id );
  220. // creating/deleting key
  221. if ( isset( $_POST['woocommerce_generate_api_key'] ) ) {
  222. // consumer key
  223. if ( empty( $user->woocommerce_api_consumer_key ) ) {
  224. $consumer_key = 'ck_' . hash( 'md5', $user->user_login . date( 'U' ) . mt_rand() );
  225. update_user_meta( $user_id, 'woocommerce_api_consumer_key', $consumer_key );
  226. } else {
  227. delete_user_meta( $user_id, 'woocommerce_api_consumer_key' );
  228. }
  229. // consumer secret
  230. if ( empty( $user->woocommerce_api_consumer_secret ) ) {
  231. $consumer_secret = 'cs_' . hash( 'md5', $user->ID . date( 'U' ) . mt_rand() );
  232. update_user_meta( $user_id, 'woocommerce_api_consumer_secret', $consumer_secret );
  233. } else {
  234. delete_user_meta( $user_id, 'woocommerce_api_consumer_secret' );
  235. }
  236. // permissions
  237. if ( empty( $user->woocommerce_api_key_permissions ) ) {
  238. if ( isset( $_POST['woocommerce_api_key_permissions'] ) ) {
  239. $permissions = ( in_array( $_POST['woocommerce_api_key_permissions'], array( 'read', 'write', 'read_write' ) ) ) ? $_POST['woocommerce_api_key_permissions'] : 'read';
  240. } else {
  241. $permissions = 'read';
  242. }
  243. update_user_meta( $user_id, 'woocommerce_api_key_permissions', $permissions );
  244. } else {
  245. delete_user_meta( $user_id, 'woocommerce_api_key_permissions' );
  246. }
  247. } else {
  248. // updating permissions for key
  249. if ( ! empty( $_POST['woocommerce_api_key_permissions'] ) && $user->woocommerce_api_key_permissions !== $_POST['woocommerce_api_key_permissions'] ) {
  250. $permissions = ( ! in_array( $_POST['woocommerce_api_key_permissions'], array( 'read', 'write', 'read_write' ) ) ) ? 'read' : $_POST['woocommerce_api_key_permissions'];
  251. update_user_meta( $user_id, 'woocommerce_api_key_permissions', $permissions );
  252. }
  253. }
  254. }
  255. }
  256. }
  257. endif;
  258. return new WC_Admin_Profile();