wc_update_240_api_keys

The WooCommerce wc update 240 api keys function.

Description

wc_update_240_api_keys(); 

Usage

  1. if ( !function_exists( 'wc_update_240_api_keys' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-update-functions.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = wc_update_240_api_keys(); 
  7.  

Defined (1)

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

/includes/wc-update-functions.php  
  1. function wc_update_240_api_keys() { 
  2. global $wpdb; 
  3. /** 
  4. * Update the old user API keys to the new Apps keys. 
  5. */ 
  6. $api_users = $wpdb->get_results( "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = 'woocommerce_api_consumer_key'" ); 
  7. $apps_keys = array(); 
  8.  
  9. // Get user data 
  10. foreach ( $api_users as $_user ) { 
  11. $user = get_userdata( $_user->user_id ); 
  12. $apps_keys[] = array( 
  13. 'user_id' => $user->ID,  
  14. 'permissions' => $user->woocommerce_api_key_permissions,  
  15. 'consumer_key' => wc_api_hash( $user->woocommerce_api_consumer_key ),  
  16. 'consumer_secret' => $user->woocommerce_api_consumer_secret,  
  17. 'truncated_key' => substr( $user->woocommerce_api_consumer_secret, -7 ),  
  18. ); 
  19.  
  20. if ( ! empty( $apps_keys ) ) { 
  21. // Create new apps 
  22. foreach ( $apps_keys as $app ) { 
  23. $wpdb->insert( 
  24. $wpdb->prefix . 'woocommerce_api_keys',  
  25. $app,  
  26. array( 
  27. '%d',  
  28. '%s',  
  29. '%s',  
  30. '%s',  
  31. '%s',  
  32. ); 
  33.  
  34. // Delete old user keys from usermeta 
  35. foreach ( $api_users as $_user ) { 
  36. $user_id = intval( $_user->user_id ); 
  37. delete_user_meta( $user_id, 'woocommerce_api_consumer_key' ); 
  38. delete_user_meta( $user_id, 'woocommerce_api_consumer_secret' ); 
  39. delete_user_meta( $user_id, 'woocommerce_api_key_permissions' );