function wbmz_check_notifications($post_ID){
    $current_user = wp_get_current_user();
    $notifications = get_posts(array(
      'post_type' => 'notificaties',
      'meta_key'		=> 'gekoppelde_cursus_id',
      'meta_value'	=> $post_ID,
      'author' => $current_user->ID
    ));
    $aantal_notis = count($notifications);
    $permalink = get_permalink($post_ID);
    if( $notifications ): // als er notificaties zijn
      return 1;
    else:
      return 0;
    endif;
}

add_filter('acf/update_value/name=status', 'wbmz_check_status_cursus', 10, 3);
function wbmz_check_status_cursus($value, $post_id, $field) {
  // 24 = aankondiging | 26 == open voor inschrijving
  $old_value = get_post_meta($post_id, 'status', true);
  if($old_value == 24 && $value == 26):
    $notifications = get_posts(array(
      'post_type' => 'notificaties',
      'meta_key'		=> 'gekoppelde_cursus_id',
      'meta_value'	=> $post_id,
      'tax_query'              => array(
        array(
          'taxonomy'         => 'status_notificatie',
          'terms'            => 'verstuurd',
          'field'            => 'slug',
          'operator'         => 'NOT IN'
        ),
      ),
    ));
    $aantal_notis = count($notifications);
    if( $aantal_notis >= 1 ): // als er notificaties zijn
      $email_notification = array();
      foreach( $notifications as $notification ):
        //op verstuurd zetten
        wp_set_post_terms( $notification->ID, 168, 'status_notificatie' );
        $email_noti = get_field('email_adres_notificatie',$notification);
        $title = $notification->post_title;
        $auteur = $notification->post_author;
        $link_gast = site_url().'/uitschrijven-voor-notificaties/';
        $link = site_url().'/mijn-osr/notificaties/';
        $permalink = get_permalink($post_id);
        $to = $email_noti;
        $subject = sprintf( 'Nieuwe datum  %s | OSR Juridische opleidingen', $title );
        $message = sprintf ('Zojuist hebben wij een nieuwe datum ingepland voor de %s.' . "\n\n", $title );
        $message .= sprintf( 'Klik op onderstaande link voor meer informatie of om je in te schrijven.<br/><a href="%s">Ga naar de website</a>', $permalink );
        if($auteur == '0'):
          $message .= sprintf( '<br/>Klik op onderstaande link om je uit te schrijven voor updates<br/><a href="%s">Uitschrijven</a>', $link_gast );
        else:
          $message .= sprintf( '<br/>Klik op onderstaande link om notificaties te beheren.<br/> <a href="%s">Notificaties beheren</a>', $link );
        endif;
        $headers[] = '';
        wp_mail( $email_noti, $subject, $message, $headers );
      endforeach;
    endif;
  else:
    $to[] = sprintf( 'mitchell@webmazing.nl', $email );
    $subject = sprintf( 'status NIET gewijzigd en mail gestuurd?', $cursusnaam );
    $message = sprintf( 'statussen NIET gewijzigd!', $achternaam, $cursusnaam, $datum_cursus, $link);
    $headers[] = '';
    //wp_mail( $to, $subject, $message, $headers );
  endif;
  return $value;
}

function my_frontend_script() {
  wp_enqueue_script( 'noti', get_template_directory_uri() . '/js/noti.js', array( 'jquery' ), '1.0.0', true );
  wp_localize_script( 'noti', 'NotiAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
}

add_action( 'wp_enqueue_scripts', 'my_frontend_script' );

add_action( 'wp_ajax_verwijder_notificatie', 'verwijder_notificatie' );
function verwijder_notificatie(){

  $permission = check_ajax_referer( 'verwijder_notificatie_nonce', 'nonce', false );
  if( $permission == false ) {
    //echo 'error';
    header('Location: ' . $_SERVER['HTTP_REFERER']);
  }
  else {
    wp_delete_post( $_REQUEST['id'] );
    header('Location: ' . $_SERVER['HTTP_REFERER']);
  }

  die();

}

add_filter("cron_schedules", function ($cron_schedules) {

      $cron_schedules["wbmz_10_minuten"] = [
          "display" => "Elke 10 minuten",
          "interval" => 10 * MINUTE_IN_SECONDS,
      ];

      return $cron_schedules;

  });
if (!wp_next_scheduled("check_nieuwe_subcursussen")) {
    wp_schedule_event(1, "wbmz_10_minuten", "check_nieuwe_subcursussen");
}
function check_nieuwe_subcursussen() {
  $args = array(
    'post_type'		=> 'cursussen',
    'posts_per_page'	=> -1,
    'post_parent' => 0,
    'facetwp' => false
  );
  $hoofdcursussen = new WP_Query($args);
  while($hoofdcursussen->have_posts()): $hoofdcursussen->the_post();
  $hoofdid = get_the_ID();
  $today = getdate();
  $args = array(
    'post_type' => 'cursussen',
    'post_parent' => $hoofdid,
    'posts_per_page' => -1,
    'tax_query'              => array(
      array(
        'taxonomy'         => 'status_cursus',
        'terms'            => '1',
        'field'            => 'slug',
      ),
    ),
    'date_query' => array(
        array(
            'year'  => $today['year'],
            'month' => $today['mon'],
            'day'   => $today['mday'],
        ),
    )
 );
  // haal alle NIEUWE child posts op
  $ChildIDs = get_posts( $args );
  $aantal_nieuw = count($ChildIDs);
  wp_reset_postdata();
  // haal notificaties op op de hoofdcursus niveau
  $notifications = get_posts(array(
    'post_type' => 'notificaties',
    'meta_key'		=> 'gekoppelde_cursus_id',
    'meta_value'	=> $hoofdid,
    'tax_query'              => array(
      array(
        'taxonomy'         => 'status_notificatie',
        'terms'            => 'verstuurd',
        'field'            => 'slug',
        'operator'         => 'NOT IN'
      ),
    ),
  ));
  // check open voor inschrijving NIEUW
  // voeg de bijbehorende child ID posts toe
  $aantal_notis = count($notifications);
  if($aantal_nieuw >= 1): // als er nieuwe posts zijn
    if($aantal_notis >= 1): // als er notificaties ingesteld zijn
      foreach( $notifications as $notification ):
        //op verstuurd zetten
        wp_set_post_terms( $notification->ID, 168, 'status_notificatie' );
        $email_noti = get_field('email_adres_notificatie',$notification);
        $id_gekoppelde_cursus = get_field('gekoppelde_cursus_id',$notification);
        $title = $notification->post_title;
        $auteur = $notification->post_author;
        $link_gast = site_url().'/uitschrijven-voor-notificaties/';
        $link = site_url().'/mijn-osr/notificaties/';
        $permalink = get_permalink($id_gekoppelde_cursus);
        $nu = current_time('U');
        if($email_noti):
          $to = $email_noti;
          $subject = sprintf( 'Nieuwe datum %s | OSR Juridische opleidingen', $title );
          $message = sprintf ('Zojuist hebben wij een nieuwe datum ingepland voor de %s.' . "\n\n", $title );
          $message .= sprintf( 'Klik op onderstaande link voor meer informatie of om je in te schrijven.<br/><a href="%s">Ga naar de website</a>', $permalink );
          if($auteur == '0'):
            $message .= sprintf( '<br/>Klik op onderstaande link om je uit te schrijven voor updates<br/><a href="%s">Uitschrijven</a>', $link_gast );
          else:
            $message .= sprintf( '<br/>Klik op onderstaande link om notificaties te beheren.<br/> <a href="%s">Notificaties beheren</a>', $link );
          endif;
          $headers[] = '';
          wp_mail( $to, $subject, $message, $headers );
        endif;
      endforeach;
    endif;
  endif;
  endwhile; wp_reset_query();
  }

function check_ingeschreven($post_id) {
  global $current_user;
  wp_get_current_user();
  $notifications = get_posts(array(
    'post_type' => 'notificaties',
    'meta_key'		=> 'gekoppelde_cursus_id',
    'meta_value'	=> $post_id,
    'author' => $current_user->ID
  ));
  // check open voor inschrijving NIEUW
  // voeg de bijbehorende child ID posts toe
  $aantal_notis = count($notifications);
  if($aantal_notis >= 1):
    return true;
  else:
    return false;
  endif;
}
Geef een reactie 0