wc-user-functions.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. <?php
  2. /**
  3. * WooCommerce Customer Functions
  4. *
  5. * Functions for customers.
  6. *
  7. * @package WooCommerce/Functions
  8. * @version 2.2.0
  9. */
  10. defined( 'ABSPATH' ) || exit;
  11. /**
  12. * Prevent any user who cannot 'edit_posts' (subscribers, customers etc) from seeing the admin bar.
  13. *
  14. * Note: get_option( 'woocommerce_lock_down_admin', true ) is a deprecated option here for backwards compatibility. Defaults to true.
  15. *
  16. * @param bool $show_admin_bar If should display admin bar.
  17. * @return bool
  18. */
  19. function wc_disable_admin_bar( $show_admin_bar ) {
  20. if ( apply_filters( 'woocommerce_disable_admin_bar', get_option( 'woocommerce_lock_down_admin', 'yes' ) === 'yes' ) && ! ( current_user_can( 'edit_posts' ) || current_user_can( 'manage_woocommerce' ) ) ) {
  21. $show_admin_bar = false;
  22. }
  23. return $show_admin_bar;
  24. }
  25. add_filter( 'show_admin_bar', 'wc_disable_admin_bar', 10, 1 ); // phpcs:ignore WordPress.VIP.AdminBarRemoval.RemovalDetected
  26. if ( ! function_exists( 'wc_create_new_customer' ) ) {
  27. /**
  28. * Create a new customer.
  29. *
  30. * @param string $email Customer email.
  31. * @param string $username Customer username.
  32. * @param string $password Customer password.
  33. * @return int|WP_Error Returns WP_Error on failure, Int (user ID) on success.
  34. */
  35. function wc_create_new_customer( $email, $username = '', $password = '' ) {
  36. // Check the email address.
  37. if ( empty( $email ) || ! is_email( $email ) ) {
  38. return new WP_Error( 'registration-error-invalid-email', __( 'Please provide a valid email address.', 'woocommerce' ) );
  39. }
  40. if ( email_exists( $email ) ) {
  41. return new WP_Error( 'registration-error-email-exists', apply_filters( 'woocommerce_registration_error_email_exists', __( 'An account is already registered with your email address. Please log in.', 'woocommerce' ), $email ) );
  42. }
  43. // Handle username creation.
  44. if ( 'no' === get_option( 'woocommerce_registration_generate_username' ) || ! empty( $username ) ) {
  45. $username = sanitize_user( $username );
  46. if ( empty( $username ) || ! validate_username( $username ) ) {
  47. return new WP_Error( 'registration-error-invalid-username', __( 'Please enter a valid account username.', 'woocommerce' ) );
  48. }
  49. if ( username_exists( $username ) ) {
  50. return new WP_Error( 'registration-error-username-exists', __( 'An account is already registered with that username. Please choose another.', 'woocommerce' ) );
  51. }
  52. } else {
  53. $username = sanitize_user( current( explode( '@', $email ) ), true );
  54. // Ensure username is unique.
  55. $append = 1;
  56. $o_username = $username;
  57. while ( username_exists( $username ) ) {
  58. $username = $o_username . $append;
  59. $append++;
  60. }
  61. }
  62. // Handle password creation.
  63. if ( 'yes' === get_option( 'woocommerce_registration_generate_password' ) && empty( $password ) ) {
  64. $password = wp_generate_password();
  65. $password_generated = true;
  66. } elseif ( empty( $password ) ) {
  67. return new WP_Error( 'registration-error-missing-password', __( 'Please enter an account password.', 'woocommerce' ) );
  68. } else {
  69. $password_generated = false;
  70. }
  71. // Use WP_Error to handle registration errors.
  72. $errors = new WP_Error();
  73. do_action( 'woocommerce_register_post', $username, $email, $errors );
  74. $errors = apply_filters( 'woocommerce_registration_errors', $errors, $username, $email );
  75. if ( $errors->get_error_code() ) {
  76. return $errors;
  77. }
  78. $new_customer_data = apply_filters(
  79. 'woocommerce_new_customer_data', array(
  80. 'user_login' => $username,
  81. 'user_pass' => $password,
  82. 'user_email' => $email,
  83. 'role' => 'customer',
  84. )
  85. );
  86. $customer_id = wp_insert_user( $new_customer_data );
  87. if ( is_wp_error( $customer_id ) ) {
  88. return new WP_Error( 'registration-error', '<strong>' . __( 'Error:', 'woocommerce' ) . '</strong> ' . __( 'Couldn&#8217;t register you&hellip; please contact us if you continue to have problems.', 'woocommerce' ) );
  89. }
  90. do_action( 'woocommerce_created_customer', $customer_id, $new_customer_data, $password_generated );
  91. return $customer_id;
  92. }
  93. }
  94. /**
  95. * Login a customer (set auth cookie and set global user object).
  96. *
  97. * @param int $customer_id Customer ID.
  98. */
  99. function wc_set_customer_auth_cookie( $customer_id ) {
  100. global $current_user;
  101. $current_user = get_user_by( 'id', $customer_id ); // WPCS: override ok.
  102. wp_set_auth_cookie( $customer_id, true );
  103. }
  104. /**
  105. * Get past orders (by email) and update them.
  106. *
  107. * @param int $customer_id Customer ID.
  108. * @return int
  109. */
  110. function wc_update_new_customer_past_orders( $customer_id ) {
  111. $linked = 0;
  112. $complete = 0;
  113. $customer = get_user_by( 'id', absint( $customer_id ) );
  114. $customer_orders = wc_get_orders(
  115. array(
  116. 'limit' => -1,
  117. 'customer' => array( array( 0, $customer->user_email ) ),
  118. 'return' => 'ids',
  119. )
  120. );
  121. if ( ! empty( $customer_orders ) ) {
  122. foreach ( $customer_orders as $order_id ) {
  123. $order = wc_get_order( $order_id );
  124. if ( ! $order ) {
  125. continue;
  126. }
  127. $order->set_customer_id( $customer->ID );
  128. $order->save();
  129. if ( $order->has_downloadable_item() ) {
  130. $data_store = WC_Data_Store::load( 'customer-download' );
  131. $data_store->delete_by_order_id( $order->get_id() );
  132. wc_downloadable_product_permissions( $order->get_id(), true );
  133. }
  134. do_action( 'woocommerce_update_new_customer_past_order', $order_id, $customer );
  135. if ( get_post_status( $order_id ) === 'wc-completed' ) {
  136. $complete++;
  137. }
  138. $linked++;
  139. }
  140. }
  141. if ( $complete ) {
  142. update_user_meta( $customer_id, 'paying_customer', 1 );
  143. update_user_meta( $customer_id, '_order_count', '' );
  144. update_user_meta( $customer_id, '_money_spent', '' );
  145. }
  146. return $linked;
  147. }
  148. /**
  149. * Order Status completed - This is a paying customer.
  150. *
  151. * @param int $order_id Order ID.
  152. */
  153. function wc_paying_customer( $order_id ) {
  154. $order = wc_get_order( $order_id );
  155. $customer_id = $order->get_customer_id();
  156. if ( $customer_id > 0 && 'shop_order_refund' !== $order->get_type() ) {
  157. $customer = new WC_Customer( $customer_id );
  158. $customer->set_is_paying_customer( true );
  159. $customer->save();
  160. }
  161. }
  162. add_action( 'woocommerce_order_status_completed', 'wc_paying_customer' );
  163. /**
  164. * Checks if a user (by email or ID or both) has bought an item.
  165. *
  166. * @param string $customer_email Customer email to check.
  167. * @param int $user_id User ID to check.
  168. * @param int $product_id Product ID to check.
  169. * @return bool
  170. */
  171. function wc_customer_bought_product( $customer_email, $user_id, $product_id ) {
  172. global $wpdb;
  173. $result = apply_filters( 'woocommerce_pre_customer_bought_product', null, $customer_email, $user_id, $product_id );
  174. if ( null !== $result ) {
  175. return $result;
  176. }
  177. $transient_name = 'wc_cbp_' . md5( $customer_email . $user_id . WC_Cache_Helper::get_transient_version( 'orders' ) );
  178. $result = get_transient( $transient_name );
  179. if ( false === $result ) {
  180. $customer_data = array( $user_id );
  181. if ( $user_id ) {
  182. $user = get_user_by( 'id', $user_id );
  183. if ( isset( $user->user_email ) ) {
  184. $customer_data[] = $user->user_email;
  185. }
  186. }
  187. if ( is_email( $customer_email ) ) {
  188. $customer_data[] = $customer_email;
  189. }
  190. $customer_data = array_map( 'esc_sql', array_filter( array_unique( $customer_data ) ) );
  191. $statuses = array_map( 'esc_sql', wc_get_is_paid_statuses() );
  192. if ( count( $customer_data ) === 0 ) {
  193. return false;
  194. }
  195. $result = $wpdb->get_col(
  196. "
  197. SELECT im.meta_value FROM {$wpdb->posts} AS p
  198. INNER JOIN {$wpdb->postmeta} AS pm ON p.ID = pm.post_id
  199. INNER JOIN {$wpdb->prefix}woocommerce_order_items AS i ON p.ID = i.order_id
  200. INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS im ON i.order_item_id = im.order_item_id
  201. WHERE p.post_status IN ( 'wc-" . implode( "','wc-", $statuses ) . "' )
  202. AND pm.meta_key IN ( '_billing_email', '_customer_user' )
  203. AND im.meta_key IN ( '_product_id', '_variation_id' )
  204. AND im.meta_value != 0
  205. AND pm.meta_value IN ( '" . implode( "','", $customer_data ) . "' )
  206. "
  207. ); // WPCS: unprepared SQL ok.
  208. $result = array_map( 'absint', $result );
  209. set_transient( $transient_name, $result, DAY_IN_SECONDS * 30 );
  210. }
  211. return in_array( absint( $product_id ), $result, true );
  212. }
  213. /**
  214. * Checks if a user has a certain capability.
  215. *
  216. * @param array $allcaps All capabilities.
  217. * @param array $caps Capabilities.
  218. * @param array $args Arguments.
  219. * @return bool
  220. */
  221. function wc_customer_has_capability( $allcaps, $caps, $args ) {
  222. if ( isset( $caps[0] ) ) {
  223. switch ( $caps[0] ) {
  224. case 'view_order':
  225. $user_id = intval( $args[1] );
  226. $order = wc_get_order( $args[2] );
  227. if ( $order && $user_id === $order->get_user_id() ) {
  228. $allcaps['view_order'] = true;
  229. }
  230. break;
  231. case 'pay_for_order':
  232. $user_id = intval( $args[1] );
  233. $order_id = isset( $args[2] ) ? $args[2] : null;
  234. // When no order ID, we assume it's a new order
  235. // and thus, customer can pay for it.
  236. if ( ! $order_id ) {
  237. $allcaps['pay_for_order'] = true;
  238. break;
  239. }
  240. $order = wc_get_order( $order_id );
  241. if ( $order && ( $user_id === $order->get_user_id() || ! $order->get_user_id() ) ) {
  242. $allcaps['pay_for_order'] = true;
  243. }
  244. break;
  245. case 'order_again':
  246. $user_id = intval( $args[1] );
  247. $order = wc_get_order( $args[2] );
  248. if ( $order && $user_id === $order->get_user_id() ) {
  249. $allcaps['order_again'] = true;
  250. }
  251. break;
  252. case 'cancel_order':
  253. $user_id = intval( $args[1] );
  254. $order = wc_get_order( $args[2] );
  255. if ( $order && $user_id === $order->get_user_id() ) {
  256. $allcaps['cancel_order'] = true;
  257. }
  258. break;
  259. case 'download_file':
  260. $user_id = intval( $args[1] );
  261. $download = $args[2];
  262. if ( $download && $user_id === $download->get_user_id() ) {
  263. $allcaps['download_file'] = true;
  264. }
  265. break;
  266. }
  267. }
  268. return $allcaps;
  269. }
  270. add_filter( 'user_has_cap', 'wc_customer_has_capability', 10, 3 );
  271. /**
  272. * Modify the list of editable roles to prevent non-admin adding admin users.
  273. *
  274. * @param array $roles Roles.
  275. * @return array
  276. */
  277. function wc_modify_editable_roles( $roles ) {
  278. if ( ! current_user_can( 'administrator' ) ) {
  279. unset( $roles['administrator'] );
  280. }
  281. return $roles;
  282. }
  283. add_filter( 'editable_roles', 'wc_modify_editable_roles' );
  284. /**
  285. * Modify capabilities to prevent non-admin users editing admin users.
  286. *
  287. * $args[0] will be the user being edited in this case.
  288. *
  289. * @param array $caps Array of caps.
  290. * @param string $cap Name of the cap we are checking.
  291. * @param int $user_id ID of the user being checked against.
  292. * @param array $args Arguments.
  293. * @return array
  294. */
  295. function wc_modify_map_meta_cap( $caps, $cap, $user_id, $args ) {
  296. switch ( $cap ) {
  297. case 'edit_user':
  298. case 'remove_user':
  299. case 'promote_user':
  300. case 'delete_user':
  301. if ( ! isset( $args[0] ) || $args[0] === $user_id ) {
  302. break;
  303. } else {
  304. if ( user_can( $args[0], 'administrator' ) && ! current_user_can( 'administrator' ) ) {
  305. $caps[] = 'do_not_allow';
  306. }
  307. }
  308. break;
  309. }
  310. return $caps;
  311. }
  312. add_filter( 'map_meta_cap', 'wc_modify_map_meta_cap', 10, 4 );
  313. /**
  314. * Get customer download permissions from the database.
  315. *
  316. * @param int $customer_id Customer/User ID.
  317. * @return array
  318. */
  319. function wc_get_customer_download_permissions( $customer_id ) {
  320. $data_store = WC_Data_Store::load( 'customer-download' );
  321. return apply_filters( 'woocommerce_permission_list', $data_store->get_downloads_for_customer( $customer_id ), $customer_id );
  322. }
  323. /**
  324. * Get customer available downloads.
  325. *
  326. * @param int $customer_id Customer/User ID.
  327. * @return array
  328. */
  329. function wc_get_customer_available_downloads( $customer_id ) {
  330. $downloads = array();
  331. $_product = null;
  332. $order = null;
  333. $file_number = 0;
  334. // Get results from valid orders only.
  335. $results = wc_get_customer_download_permissions( $customer_id );
  336. if ( $results ) {
  337. foreach ( $results as $result ) {
  338. $order_id = intval( $result->order_id );
  339. if ( ! $order || $order->get_id() !== $order_id ) {
  340. // New order.
  341. $order = wc_get_order( $order_id );
  342. $_product = null;
  343. }
  344. // Make sure the order exists for this download.
  345. if ( ! $order ) {
  346. continue;
  347. }
  348. // Check if downloads are permitted.
  349. if ( ! $order->is_download_permitted() ) {
  350. continue;
  351. }
  352. $product_id = intval( $result->product_id );
  353. if ( ! $_product || $_product->get_id() !== $product_id ) {
  354. // New product.
  355. $file_number = 0;
  356. $_product = wc_get_product( $product_id );
  357. }
  358. // Check product exists and has the file.
  359. if ( ! $_product || ! $_product->exists() || ! $_product->has_file( $result->download_id ) ) {
  360. continue;
  361. }
  362. $download_file = $_product->get_file( $result->download_id );
  363. // Download name will be 'Product Name' for products with a single downloadable file, and 'Product Name - File X' for products with multiple files.
  364. $download_name = apply_filters(
  365. 'woocommerce_downloadable_product_name',
  366. $download_file['name'],
  367. $_product,
  368. $result->download_id,
  369. $file_number
  370. );
  371. $downloads[] = array(
  372. 'download_url' => add_query_arg(
  373. array(
  374. 'download_file' => $product_id,
  375. 'order' => $result->order_key,
  376. 'email' => rawurlencode( $result->user_email ),
  377. 'key' => $result->download_id,
  378. ),
  379. home_url( '/' )
  380. ),
  381. 'download_id' => $result->download_id,
  382. 'product_id' => $_product->get_id(),
  383. 'product_name' => $_product->get_name(),
  384. 'product_url' => $_product->is_visible() ? $_product->get_permalink() : '', // Since 3.3.0.
  385. 'download_name' => $download_name,
  386. 'order_id' => $order->get_id(),
  387. 'order_key' => $order->get_order_key(),
  388. 'downloads_remaining' => $result->downloads_remaining,
  389. 'access_expires' => $result->access_expires,
  390. 'file' => array(
  391. 'name' => $download_file->get_name(),
  392. 'file' => $download_file->get_file(),
  393. ),
  394. );
  395. $file_number++;
  396. }
  397. }
  398. return apply_filters( 'woocommerce_customer_available_downloads', $downloads, $customer_id );
  399. }
  400. /**
  401. * Get total spent by customer.
  402. *
  403. * @param int $user_id User ID.
  404. * @return string
  405. */
  406. function wc_get_customer_total_spent( $user_id ) {
  407. $customer = new WC_Customer( $user_id );
  408. return $customer->get_total_spent();
  409. }
  410. /**
  411. * Get total orders by customer.
  412. *
  413. * @param int $user_id User ID.
  414. * @return int
  415. */
  416. function wc_get_customer_order_count( $user_id ) {
  417. $customer = new WC_Customer( $user_id );
  418. return $customer->get_order_count();
  419. }
  420. /**
  421. * Reset _customer_user on orders when a user is deleted.
  422. *
  423. * @param int $user_id User ID.
  424. */
  425. function wc_reset_order_customer_id_on_deleted_user( $user_id ) {
  426. global $wpdb;
  427. $wpdb->update(
  428. $wpdb->postmeta, array( 'meta_value' => 0 ), array(
  429. 'meta_key' => '_customer_user',
  430. 'meta_value' => $user_id,
  431. )
  432. ); // WPCS: slow query ok.
  433. }
  434. add_action( 'deleted_user', 'wc_reset_order_customer_id_on_deleted_user' );
  435. /**
  436. * Get review verification status.
  437. *
  438. * @param int $comment_id Comment ID.
  439. * @return bool
  440. */
  441. function wc_review_is_from_verified_owner( $comment_id ) {
  442. $verified = get_comment_meta( $comment_id, 'verified', true );
  443. // If no "verified" meta is present, generate it (if this is a product review).
  444. if ( '' === $verified ) {
  445. $verified = WC_Comments::add_comment_purchase_verification( $comment_id );
  446. }
  447. return (bool) $verified;
  448. }
  449. /**
  450. * Disable author archives for customers.
  451. *
  452. * @since 2.5.0
  453. */
  454. function wc_disable_author_archives_for_customers() {
  455. global $author;
  456. if ( is_author() ) {
  457. $user = get_user_by( 'id', $author );
  458. if ( user_can( $user, 'customer' ) && ! user_can( $user, 'edit_posts' ) ) {
  459. wp_redirect( wc_get_page_permalink( 'shop' ) );
  460. }
  461. }
  462. }
  463. add_action( 'template_redirect', 'wc_disable_author_archives_for_customers' );
  464. /**
  465. * Hooks into the `profile_update` hook to set the user last updated timestamp.
  466. *
  467. * @since 2.6.0
  468. * @param int $user_id The user that was updated.
  469. * @param array $old The profile fields pre-change.
  470. */
  471. function wc_update_profile_last_update_time( $user_id, $old ) {
  472. wc_set_user_last_update_time( $user_id );
  473. }
  474. add_action( 'profile_update', 'wc_update_profile_last_update_time', 10, 2 );
  475. /**
  476. * Hooks into the update user meta function to set the user last updated timestamp.
  477. *
  478. * @since 2.6.0
  479. * @param int $meta_id ID of the meta object that was changed.
  480. * @param int $user_id The user that was updated.
  481. * @param string $meta_key Name of the meta key that was changed.
  482. * @param string $_meta_value Value of the meta that was changed.
  483. */
  484. function wc_meta_update_last_update_time( $meta_id, $user_id, $meta_key, $_meta_value ) {
  485. $keys_to_track = apply_filters( 'woocommerce_user_last_update_fields', array( 'first_name', 'last_name' ) );
  486. $update_time = false;
  487. if ( in_array( $meta_key, $keys_to_track, true ) ) {
  488. $update_time = true;
  489. }
  490. if ( 'billing_' === substr( $meta_key, 0, 8 ) ) {
  491. $update_time = true;
  492. }
  493. if ( 'shipping_' === substr( $meta_key, 0, 9 ) ) {
  494. $update_time = true;
  495. }
  496. if ( $update_time ) {
  497. wc_set_user_last_update_time( $user_id );
  498. }
  499. }
  500. add_action( 'update_user_meta', 'wc_meta_update_last_update_time', 10, 4 );
  501. /**
  502. * Sets a user's "last update" time to the current timestamp.
  503. *
  504. * @since 2.6.0
  505. * @param int $user_id The user to set a timestamp for.
  506. */
  507. function wc_set_user_last_update_time( $user_id ) {
  508. update_user_meta( $user_id, 'last_update', gmdate( 'U' ) );
  509. }
  510. /**
  511. * Get customer saved payment methods list.
  512. *
  513. * @since 2.6.0
  514. * @param int $customer_id Customer ID.
  515. * @return array
  516. */
  517. function wc_get_customer_saved_methods_list( $customer_id ) {
  518. return apply_filters( 'woocommerce_saved_payment_methods_list', array(), $customer_id );
  519. }
  520. /**
  521. * Get info about customer's last order.
  522. *
  523. * @since 2.6.0
  524. * @param int $customer_id Customer ID.
  525. * @return WC_Order|bool Order object if successful or false.
  526. */
  527. function wc_get_customer_last_order( $customer_id ) {
  528. $customer = new WC_Customer( $customer_id );
  529. return $customer->get_last_order();
  530. }
  531. /**
  532. * Add support for searching by display_name.
  533. *
  534. * @since 3.2.0
  535. * @param array $search_columns Column names.
  536. * @return array
  537. */
  538. function wc_user_search_columns( $search_columns ) {
  539. $search_columns[] = 'display_name';
  540. return $search_columns;
  541. }
  542. add_filter( 'user_search_columns', 'wc_user_search_columns' );
  543. /**
  544. * When a user is deleted in WordPress, delete corresponding WooCommerce data.
  545. *
  546. * @param int $user_id User ID being deleted.
  547. */
  548. function wc_delete_user_data( $user_id ) {
  549. global $wpdb;
  550. // Clean up sessions.
  551. $wpdb->delete(
  552. $wpdb->prefix . 'woocommerce_sessions',
  553. array(
  554. 'session_key' => $user_id,
  555. )
  556. );
  557. // Revoke API keys.
  558. $wpdb->delete(
  559. $wpdb->prefix . 'woocommerce_api_keys',
  560. array(
  561. 'user_id' => $user_id,
  562. )
  563. );
  564. // Clean up payment tokens.
  565. $payment_tokens = WC_Payment_Tokens::get_customer_tokens( $user_id );
  566. foreach ( $payment_tokens as $payment_token ) {
  567. $payment_token->delete();
  568. }
  569. }
  570. add_action( 'delete_user', 'wc_delete_user_data' );
  571. /**
  572. * Store user agents. Used for tracker.
  573. *
  574. * @since 3.0.0
  575. * @param string $user_login User login.
  576. * @param int|object $user User.
  577. */
  578. function wc_maybe_store_user_agent( $user_login, $user ) {
  579. if ( 'yes' === get_option( 'woocommerce_allow_tracking', 'no' ) && user_can( $user, 'manage_woocommerce' ) ) {
  580. $admin_user_agents = array_filter( (array) get_option( 'woocommerce_tracker_ua', array() ) );
  581. $admin_user_agents[] = wc_get_user_agent();
  582. update_option( 'woocommerce_tracker_ua', array_unique( $admin_user_agents ) );
  583. }
  584. }
  585. add_action( 'wp_login', 'wc_maybe_store_user_agent', 10, 2 );
  586. /**
  587. * Update logic triggered on login.
  588. *
  589. * @since 3.4.0
  590. * @param string $user_login User login.
  591. * @param object $user User.
  592. */
  593. function wc_user_logged_in( $user_login, $user ) {
  594. wc_update_user_last_active( $user->ID );
  595. }
  596. add_action( 'wp_login', 'wc_user_logged_in', 10, 2 );
  597. /**
  598. * Update when the user was last active.
  599. *
  600. * @since 3.4.0
  601. */
  602. function wc_current_user_is_active() {
  603. if ( ! is_user_logged_in() ) {
  604. return;
  605. }
  606. wc_update_user_last_active( get_current_user_id() );
  607. }
  608. add_action( 'wp', 'wc_current_user_is_active', 10 );
  609. /**
  610. * Set the user last active timestamp to now.
  611. *
  612. * @since 3.4.0
  613. * @param int $user_id User ID to mark active.
  614. */
  615. function wc_update_user_last_active( $user_id ) {
  616. if ( ! $user_id ) {
  617. return;
  618. }
  619. update_user_meta( $user_id, 'wc_last_active', (string) strtotime( date( 'Y-m-d', current_time( 'timestamp', true ) ) ) );
  620. }