Esempio n. 1
0
 private function get_recurrence_type(array $recurrence)
 {
     $invalid_type = empty($recurrence['type']);
     if ($invalid_type) {
         return 'invalid';
     }
     $no_custom_type = empty($recurrence['custom']['type']);
     $valid_type = in_array($recurrence['type'], Tribe__Events__Pro__Recurrence__Custom_Types::get_legit_recurrence_types());
     if ($no_custom_type && $valid_type) {
         return $recurrence['type'];
     }
     $is_valid_custom_type = $recurrence['type'] == Tribe__Events__Pro__Recurrence__Custom_Types::CUSTOM_TYPE && in_array($recurrence['custom']['type'], Tribe__Events__Pro__Recurrence__Custom_Types::get_legit_custom_types());
     if ($is_valid_custom_type) {
         return $recurrence['custom']['type'];
     }
     return 'invalid';
 }
 /**
  * Convert the event recurrence meta into a human readable string
  *
  * @TODO: there's a great deal of duplication between this method and tribe_events_pro_admin.recurrence.update_rule_recurrence_text (events-recurrence.js)
  *        let's consider generating once (by JS?) and saving the result for re-use instead
  *
  * @param array   $rule
  * @param string  $start_date
  * @param int     $event_id
  *
  * @return string human readable string
  */
 public static function recurrenceToText($rule, $start_date, $event_id)
 {
     $text = '';
     $recurrence_strings = Tribe__Events__Pro__Recurrence__Strings::recurrence_strings();
     $date_strings = self::date_strings();
     $interval = 1;
     $is_custom = false;
     $same_time = true;
     $year_filtered = false;
     $rule['type'] = str_replace(' ', '-', strtolower($rule['type']));
     $rule['end-type'] = str_replace(' ', '-', strtolower($rule['end-type']));
     $formatted_end = _x('an unspecified date', 'An unspecified end date', 'tribe-events-calendar-pro');
     if (!empty($rule['end'])) {
         $formatted_end = date(tribe_get_date_format(true), strtotime($rule['end']));
     }
     // if the type is "none", then there's no rules to parse
     if ('none' === $rule['type']) {
         return;
     }
     // if there isn't an end date, then there isn't a recurrence set up
     if ('on' === $rule['end-type'] && empty($rule['end'])) {
         return;
     }
     if ('custom' === $rule['type']) {
         $is_custom = true;
         $same_time = false;
         if ('yes' === @$rule['custom'][Tribe__Events__Pro__Recurrence__Custom_Types::to_key($rule['custom']['type'])]['same-time']) {
             $same_time = true;
         }
         if ('Yearly' === $rule['custom']['type']) {
             $year_filtered = !empty($rule['custom']['year']['filter']);
         }
     }
     $start_date = strtotime(tribe_get_start_date($event_id, true, Tribe__Date_Utils::DBDATETIMEFORMAT));
     $end_date = strtotime(tribe_get_end_date($event_id, true, Tribe__Date_Utils::DBDATETIMEFORMAT));
     $num_days = floor(($end_date - $start_date) / DAY_IN_SECONDS);
     // make sure we always round hours UP to when dealing with decimal lengths more than 2. Example: 4.333333 would become 4.34
     $num_hours = ceil((($end_date - $start_date) / HOUR_IN_SECONDS - $num_days * 24) * 100) / 100;
     if ($is_custom && 'custom' === $rule['type'] && !$same_time) {
         $formatted_start = date('Y-m-d', $start_date) . ' ' . $rule['custom']['start-time']['hour'] . ':' . $rule['custom']['start-time']['minute'];
         if (isset($rule['custom']['start-time']['meridian'])) {
             $formatted_start .= ' ' . $rule['custom']['start-time']['meridian'];
         }
         if (isset($rule['custom']['duration'])) {
             try {
                 $end_date = new DateTime('@' . $start_date);
                 $end_date->modify('+' . absint($rule['custom']['duration']['days']) . ' days');
                 $end_date->modify('+' . absint($rule['custom']['duration']['hours']) . ' hours');
                 $end_date->modify('+' . absint($rule['custom']['duration']['minutes']) . ' minutes');
                 $formatted_end = Tribe__View_Helpers::is_24hr_format() ? $end_date->format('Y-m-d H:i') : $end_date->format('Y-m-d g:i');
                 $end_date = $end_date->format('U');
             } catch (Exception $e) {
                 // $formatted_end will default to the "unspecified end date" text in this case
             }
         }
         $new_num_days = floor(($end_date - $start_date) / DAY_IN_SECONDS);
         // make sure we always round hours UP to when dealing with decimal lengths more than 2. Example: 4.333333 would become 4.34
         $new_num_hours = ceil((($end_date - $start_date) / HOUR_IN_SECONDS - $new_num_days * 24) * 100) / 100;
     }
     $weekdays = array();
     $months = array();
     $month_number = null;
     $month_day = null;
     $month_day_description = null;
     if ($is_custom && 'Weekly' === $rule['custom']['type'] && !empty($rule['custom']['week']['day'])) {
         foreach ($rule['custom']['week']['day'] as $day) {
             $weekdays[] = $date_strings['weekdays'][$day - 1];
         }
         if (!$weekdays) {
             $weekdays = $date_strings['day_placeholder'];
         } elseif (2 === count($weekdays)) {
             $weekdays = implode(" {$date_strings['collection_joiner']} ", $weekdays);
         } else {
             $weekdays = implode(', ', $weekdays);
             $weekdays = preg_replace('/(.*),/', '$1, ' . $date_strings['collection_joiner'], $weekdays);
         }
     } elseif ($is_custom && 'Monthly' === $rule['custom']['type'] && !empty($rule['custom']['month']['number']) && !empty($rule['custom']['month']['day'])) {
         $month_number = $rule['custom']['month']['number'];
         $month_day = $rule['custom']['month']['day'];
     } elseif ($is_custom && 'Yearly' === $rule['custom']['type'] && !empty($rule['custom']['year']['month-number']) && !empty($rule['custom']['year']['month-day'])) {
         $month_number = $rule['custom']['year']['month-number'];
         $month_day = $rule['custom']['year']['month-day'];
         if (!empty($rule['custom']['year']['month'])) {
             foreach ($rule['custom']['year']['month'] as $month) {
                 $months[] = $date_strings['months'][$month - 1];
             }
         }
         if (!$months) {
             $months = $date_strings['month_placeholder'];
         } elseif (2 === count($months)) {
             $months = implode(" {$date_strings['collection_joiner']} ", $months);
         } else {
             $months = implode(', ', $months);
             $months = preg_replace('/(.*),/', '$1, ' . $date_strings['collection_joiner'], $months);
         }
     }
     $key = $rule['type'];
     if ('custom' === $rule['type']) {
         $key .= "-{$rule['custom']['type']}-{$rule['end-type']}-" . ($same_time ? 'same' : 'diff') . '-time';
         if ('monthly' === $rule['custom']['type'] && is_numeric($month_number)) {
             $key .= '-numeric';
         } elseif ('yearly' === $rule['custom']['type'] && !$year_filtered) {
             $key .= '-unfiltered';
         }
     } else {
         $key = "simple-{$key}";
         $key .= "-{$rule['end-type']}";
     }
     $key = strtolower($key);
     // if custom rules were set but the custom-specific data is missing, then revert to standard
     // rules (weekly, monthly, and yearly)
     if ($is_custom && 'Weekly' === $rule['custom']['type'] && !$weekdays) {
         $key = 'every-week-on';
     } elseif ($is_custom && 'Monthly' === $rule['custom']['type'] && !$month_number && !$month_day) {
         $key = 'every-month-on';
     } elseif ($is_custom && 'Yearly' === $rule['custom']['type'] && !$month_number && !$month_day) {
         $key = 'every-year-on';
     }
     $text = $recurrence_strings[$key];
     switch ($key) {
         case 'simple-every-day-on':
         case 'simple-every-week-on':
         case 'simple-every-month-on':
         case 'simple-every-year-on':
             $text = sprintf($text, $formatted_end);
             break;
         case 'every-day-on':
         case 'every-week-on':
         case 'every-month-on':
         case 'every-year-on':
         case 'every-day-never':
         case 'every-week-never':
         case 'every-month-never':
         case 'every-year-never':
             $text = sprintf($text, $num_days, $num_hours, $formatted_end);
             break;
         case 'every-day-after':
         case 'every-week-after':
         case 'every-month-after':
         case 'every-year-after':
             $text = sprintf($text, $num_days, $num_hours, $rule['end-count']);
             break;
         case 'custom-daily-on-same-time':
         case 'custom-daily-never-same-time':
             $text = sprintf($text, $interval, $num_days, $num_hours, $formatted_end);
             break;
         case 'custom-daily-after-same-time':
             $text = sprintf($text, $interval, $num_days, $num_hours, $rule['end-count']);
             break;
         case 'custom-daily-on-diff-time':
         case 'custom-daily-never-diff-time':
             $text = sprintf($text, $interval, $formatted_start, $new_num_days, $new_num_hours, $formatted_end);
             break;
         case 'custom-daily-after-diff-time':
             $text = sprintf($text, $interval, $formatted_start, $new_num_days, $new_num_hours, $rule['end-count']);
             break;
         case 'custom-weekly-on-same-time':
         case 'custom-weekly-never-same-time':
             $text = sprintf($text, $interval, $weekdays, $num_days, $num_hours, $formatted_end);
             break;
         case 'custom-weekly-after-same-time':
             $text = sprintf($text, $interval, $weekdays, $num_days, $num_hours, $rule['end-count']);
             break;
         case 'custom-weekly-on-diff-time':
         case 'custom-weekly-never-diff-time':
             $text = sprintf($text, $interval, $weekdays, $formatted_start, $new_num_days, $new_num_hours, $formatted_end);
             break;
         case 'custom-weekly-after-diff-time':
             $text = sprintf($text, $interval, $weekdays, $formatted_start, $new_num_days, $new_num_hours, $rule['end-count']);
             break;
         case 'custom-monthly-on-same-time-numeric':
         case 'custom-monthly-never-same-time-numeric':
         case 'custom-monthly-on-same-time':
         case 'custom-monthly-never-same-time':
             $text = sprintf($text, $interval, $month_day_description, $num_days, $num_hours, $formatted_end);
             break;
         case 'custom-monthly-after-same-time-numeric':
         case 'custom-monthly-after-same-time':
             $text = sprintf($text, $interval, $month_day_description, $num_days, $num_hours, $rule['end-count']);
             break;
         case 'custom-monthly-on-diff-time-numeric':
         case 'custom-monthly-never-diff-time-numeric':
         case 'custom-monthly-on-diff-time':
         case 'custom-monthly-never-diff-time':
             $text = sprintf($text, $interval, $month_day_description, $formatted_start, $new_num_days, $new_num_hours, $formatted_end);
             break;
         case 'custom-monthly-after-diff-time-numeric':
         case 'custom-monthly-after-diff-time':
             $text = sprintf($text, $interval, $month_day_description, $formatted_start, $new_num_days, $new_num_hours, $rule['end-count']);
             break;
         case 'custom-yearly-on-same-time-unfiltered':
         case 'custom-yearly-never-same-time-unfiltered':
         case 'custom-yearly-on-same-time':
         case 'custom-yearly-never-same-time':
             $text = sprintf($text, $interval, $months, $month_day_description, $num_days, $num_hours, $formatted_end);
             break;
         case 'custom-yearly-after-same-time-unfiltered':
         case 'custom-yearly-after-same-time':
             $text = sprintf($text, $interval, $months, $month_day_description, $num_days, $num_hours, $rule['end-count']);
             break;
         case 'custom-yearly-on-diff-time-unfiltered':
         case 'custom-yearly-never-diff-time-unfiltered':
         case 'custom-yearly-on-diff-time':
         case 'custom-yearly-never-diff-time':
             $text = sprintf($text, $interval, $months, $month_day_description, $formatted_start, $new_num_days, $new_num_hours, $formatted_end);
             break;
         case 'custom-yearly-after-diff-time-unfiltered':
         case 'custom-yearly-after-diff-time':
             $text = sprintf($text, $interval, $months, $month_day_description, $formatted_start, $new_num_days, $new_num_hours, $rule['end-count']);
             break;
     }
     return $text;
 }
 protected function ensure_all_data()
 {
     $empty_custom_key = empty($this->recurrence_meta['custom']);
     $custom_keys = array_intersect(Tribe__Events__Pro__Recurrence__Custom_Types::data_keys(), array_keys($this->recurrence_meta['custom']));
     $not_a_valid_custom = empty($custom_keys);
     if ($empty_custom_key || $not_a_valid_custom) {
         throw new RuntimeException(__('Custom recurrences must have all data present.', 'tribe-events-calendar-pro'));
     }
 }
 public function to_key($custom_type)
 {
     return Tribe__Events__Pro__Recurrence__Custom_Types::to_key($custom_type);
 }