Advertisement
Guest User

Untitled

a guest
Feb 29th, 2020
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.64 KB | None | 0 0
  1. /*
  2.  * @param int $user id
  3.  * @param string $meta_key
  4.  * @param string $new_value - the new value to be added to the array
  5.  */
  6. function meta_update($user_id, $meta_key, $new_value) {
  7.     // Get the existing meta for 'meta_key'
  8.     $meta = get_user_meta($user_id, $meta_key, true);
  9.     // Do some defensive coding - if it's not an array, set it up
  10.     if ( ! is_array($meta) ) {
  11.         $meta = array();
  12.     }
  13.     // Push a new value onto the array
  14.     $meta[] = $new_value;
  15.     // Write the user meta record with the new value in it
  16.     update_user_meta($user_id, $meta_key, $meta);
  17. }
  18.  
  19. /*
  20.  * @param int $user_id
  21.  * @param int $certification_id - certifications post_id
  22.  * @param string $name - TODO: auto pull post title
  23.  * @param string $renewal_date - user provided date
  24.  */
  25. function cpe_enroll_user( $user_id, $certification_id, $name, $renewal_date ) {
  26.    
  27.     // $meta = get_user_meta( $user_id, 'certifications', false)[0];
  28.     $meta = get_user_meta( $user_id, 'certifications', true);
  29.    
  30.     if ( !array($meta) ) {
  31.         $meta = array();
  32.     }
  33.    
  34.     $enrollment_data = array(array(
  35.         'certification_id'          =>  $certification_id,
  36.         'certification_name'        =>  $name,             
  37.         'cpes_required'             =>  $cpes_required,
  38.         'renewal_date'              =>  $renewal_date,
  39.         ),
  40.     );
  41.    
  42.     if(!empty($meta)) {    
  43.         // foreach($meta as $key => $value) {          
  44.             if(!in_array($enrollment_data[0]['certification_id'], array_column($meta, 'certification_id'))) {
  45.                 $meta_update = array_merge($meta, $enrollment_data);
  46.                 update_user_meta( $user_id, 'certifications', $meta_update);   
  47.             }
  48.         // }
  49.     } else {       
  50.         update_user_meta( $user_id, 'certifications', $enrollment_data);
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement