Exemple #1
0
 /**
  * Create custom recurrence dates.
  */
 private function create_recurrences($post_id, $start, $end, $type, $repeat, $until, $weekly_days, $monthly_day_type)
 {
     $em_helper = new Events_Maker_Helper();
     if ($em_helper->is_after_date($start, $end) || $em_helper->is_after_date($start, $until)) {
         return;
     }
     $format = 'Y-m-d H:i:s';
     $occurrences = array();
     $diff = strtotime($end) - strtotime($start);
     $finish = strtotime($until);
     if ($type === 'daily') {
         $repeat *= 86400;
         $current = strtotime($start);
         while ($current <= $finish) {
             $occurrences[] = array('start' => date($format, $current), 'end' => date($format, $current + $diff));
             // create new current date
             $current += $repeat;
         }
     } elseif ($type === 'weekly') {
         $current = $start_date = strtotime($start);
         $weekdays = array();
         $repeat *= 7;
         $i = $counter = 0;
         $day = date('N', $current);
         foreach ($weekly_days as $weekday) {
             $weekdays[] = $weekday - $day;
         }
         $number_of_days = count($weekdays);
         while ($current <= $finish) {
             if (($more_days = $weekdays[$i++] + $repeat * $counter) >= 0) {
                 // create new current date
                 $current = strtotime('+' . $more_days . ' days', $start_date);
                 if ($current <= $finish) {
                     $occurrences[] = array('start' => date($format, $current), 'end' => date($format, $current + $diff));
                 }
             }
             if ($i === $number_of_days) {
                 $counter++;
                 $i = 0;
             }
         }
     } elseif ($type === 'monthly') {
         $current = strtotime($start);
         $start_date = date_parse($start);
         // is it day of week?
         if ($monthly_day_type === 2) {
             // 1-7
             $day_of_week = date('N', $current);
             // 1-31 / 7 rounded down
             $which = (int) floor(date('j', $current) / 7);
             // time
             $diff_time = $start_date['second'] + $start_date['minute'] * 60 + $start_date['hour'] * 3600;
         } else {
             $diff_time = 0;
         }
         while ($current <= $finish) {
             $occurrences[] = array('start' => date($format, $current + $diff_time), 'end' => date($format, $current + $diff + $diff_time));
             // current date
             $date = date_parse(date('Y-m-d', $current));
             // create new current date
             if ($start_date['day'] > 28) {
                 $values = date('Y-m-t', strtotime('+' . $repeat . ' months', strtotime($date['year'] . '-' . $date['month'] . '-01')));
                 $values = explode('-', $values);
                 if ($values[2] < $date['day']) {
                     $current = strtotime($values[0] . '-' . $values[1] . '-' . $values[2]);
                 } else {
                     $current = strtotime($values[0] . '-' . $values[1] . '-' . $start_date['day']);
                 }
             } else {
                 $current = strtotime('+' . $repeat . ' months', $current);
             }
             if ($monthly_day_type === 2) {
                 // due to PHP 5.2 bugs lets do some craziness
                 $year = date('Y', $current);
                 $month = date('m', $current);
                 $day_of_month = date('N', strtotime($year . '-' . $month . '-01'));
                 if ($day_of_month <= $day_of_week) {
                     $number = $day_of_week - $day_of_month + 1;
                 } else {
                     $number = $day_of_week - $day_of_month + 8;
                 }
                 $number += 7 * $which;
                 // is it valid date?
                 while (!checkdate((int) $month, $number, $year)) {
                     $number -= 7;
                 }
                 $current = strtotime($year . '-' . $month . '-' . str_pad($number, 2, '0', STR_PAD_LEFT));
             }
         }
     } elseif ($type === 'yearly') {
         $current = strtotime($start);
         while ($current <= $finish) {
             $occurrences[] = array('start' => date($format, $current), 'end' => date($format, $current + $diff));
             // create new current date
             $current = strtotime('+1 year', $current);
         }
     }
     if (!empty($occurrences)) {
         global $wpdb;
         $query = array();
         foreach ($occurrences as $id => $occurrence) {
             if ($id > 0) {
                 $query[] = "(" . $post_id . ", '_event_occurrence_date', '" . $occurrence['start'] . "|" . $occurrence['end'] . "')";
             }
         }
         if (!empty($query)) {
             $wpdb->query('INSERT INTO ' . $wpdb->postmeta . ' (post_id, meta_key, meta_value) VALUES ' . implode(', ', $query));
         }
     }
     // get last occurrence
     $last = end($occurrences);
     // add last occurrence
     add_post_meta($post_id, '_event_occurrence_last_date', $last['start'] . '|' . $last['end']);
 }