churchthemes/church-theme-content — pull request #2 by chrisburgess7 (2014-07-25 00:55:11+00:00)
Title: Custom event recurrence

What I've attempted to do here is to make it much easier to add event recurrence options, both for CTC developers, as well as for CTC users if they wish to define their own custom recurrence not currently supported. I've done this by defining a set of functions that return all the information needed for event recurrence throughout the rest of the code.

The first function is ctc_get_event_recurrences, which returns a data structure containing all the information needed for the event recurrence options supported by CTC, aside from the actual increment logic in ctc_increment_future_date.

Notably, the array returned by this function is filterable, so users can extend this with their own. For example, I added an nth weekday in the month recurrence via the following:

```
function ctc_apac_add_custom_event_recurrences( $recurrences ) {
$recurrences['nth_day_in_month'] = array(
'meta_box_option' => _x( 'Nth Day in Month', 'event meta box', 'church-theme-content' ),
'description_callback' => true,
'description' => 'ctc_apac_nth_day_in_month_description_callback',
'increment_function' => 'ctc_apac_nth_day_in_month_increment_function'
);
return $recurrences;
}

add_filter( 'ctc_event_recurrence_options', 'ctc_apac_add_custom_event_recurrences' );
```

'meta_box_option' is the text for the drop down option

'description_callback' and 'description' elements refer to the description of the recurrence displayed in the event list page. If 'description_callback' either doesn't exist or doesn't have the value true, then the 'description' field is used as a plain string. Otherwise, the 'description' field is called as a callback function, with the start and end date passed as arguments for more informative description. For example, for Nth Day in Month, I used the callback to return a string like 'Recurs every Fourth Wednesday of the Month' depending on the date of the event.

'increment_function' is a callback to increment the event date. It is passed the date in 'yy-mm-dd', as well as the year, month, and day values separately. It should return the new date as an array with year, month, day.

The other methods return elements of the data structure required at certain points:

ctc_get_event_recurrence_metabox_options pulls out the recurrence keys and meta box names

ctc_get_event_recurrence_description( $recurrence, $start_date, $end_date ) returns the description for the given recurrence

ctc_get_event_recurrence_keys pulls out just the keys for the event query in ctc_update_recurring_event_dates

ctc_call_event_recurrence_increment_function( $recurrence, $date, $y, $m, $d ) calls the date increment callback for the given custom recurrence

The other change I made was added a condition so that the recursive call to ctc_increment_future_date is only performed if the current call of the function has changed the date. This avoids infinite recursion in the event that an event recurrence increment callback method doesn't actually change the date. This was relevant for Nth weekday of the month, if the date of the event date was set as the fifth weekday day of the month. I handled that situation my simply ignoring it and not incrementing the date, as I didn't anticipate anyone would actually organise events on the infrequent fifth day of any month.

Finally, as I've mentioned, I have worked out an nth weekday of the month event recurrence. If you like this pull request, I can add that event recurrence on top of this. Alternatively I can simply submit another pull request with the only change being the addition of that recurrence option.