Advertisement
verygoodplugins

Untitled

Apr 16th, 2020
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.87 KB | None | 0 0
  1. // Register the quiz fields in the WPF settings
  2.  
  3. function wpf_add_learndash_quiz_fields( $meta_fields ) {
  4.  
  5.     $args = array(
  6.         'post_type' => 'sfwd-quiz',
  7.         'nopaging'  => true,
  8.     );
  9.  
  10.     $quizzes = get_posts( $args );
  11.  
  12.     foreach ( $quizzes as $quiz ) {
  13.  
  14.         $meta_fields[ 'quiz_' . $quiz->ID . '_pass' ] = array(
  15.             'label' => $quiz->post_title . ' - Pass',
  16.             'type'  => 'checkbox',
  17.             'group' => 'learndash_progress',
  18.         );
  19.  
  20.         $meta_fields[ 'quiz_' . $quiz->ID . '_pass_date' ] = array(
  21.             'label' => $quiz->post_title . ' - Pass Date',
  22.             'type'  => 'date',
  23.             'group' => 'learndash_progress',
  24.         );
  25.  
  26.         $meta_fields[ 'quiz_' . $quiz->ID . '_fail' ] = array(
  27.             'label' => $quiz->post_title . ' - Fail',
  28.             'type'  => 'checkbox',
  29.             'group' => 'learndash_progress',
  30.         );
  31.  
  32.         $meta_fields[ 'quiz_' . $quiz->ID . '_fail_date' ] = array(
  33.             'label' => $quiz->post_title . ' - Fail Date',
  34.             'type'  => 'date',
  35.             'group' => 'learndash_progress',
  36.         );
  37.  
  38.     }
  39.  
  40.     return $meta_fields;
  41.  
  42. }
  43.  
  44.  
  45. add_filter( 'wpf_meta_fields', 'wpf_add_learndash_quiz_fields' );
  46.  
  47. // Sync the pass / fail data to the custom fields
  48.  
  49. function wpf_quiz_completed( $data, $user ) {
  50.  
  51.     if ( isset( $data['quiz']->ID ) ) {
  52.         $quiz_id = $data['quiz']->ID;
  53.     } else {
  54.         // For grading in the admin
  55.         $quiz_id = $data['quiz'];
  56.     }
  57.  
  58.     if ( true == $data['pass'] ) {
  59.  
  60.         $update_data = array(
  61.             'quiz_' . $quiz_id . '_pass'      => true,
  62.             'quiz_' . $quiz_id . '_pass_date' => time(),
  63.             'quiz_' . $quiz_id . '_fail'      => false,
  64.         );
  65.  
  66.     } elseif ( false == $data['pass'] ) {
  67.  
  68.         $update_data = array(
  69.             'quiz_' . $quiz_id . '_pass'      => false,
  70.             'quiz_' . $quiz_id . '_fail'      => true,
  71.             'quiz_' . $quiz_id . '_fail_date' => time(),
  72.         );
  73.  
  74.     }
  75.  
  76.     wp_fusion()->user->push_user_meta( $user->ID, $update_data );
  77.  
  78. }
  79.  
  80. add_action( 'learndash_quiz_completed', 'wpf_quiz_completed', 5, 2 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement