WIP suggested fields, additional theming, cron, shortcodes.

This commit is contained in:
ekes 2019-04-29 20:07:19 +02:00
parent 98872a46bc
commit a6f657092d
5 changed files with 284 additions and 115 deletions

View file

@ -9,16 +9,21 @@ class Squat_Radar_Formatter {
// Filters to turn each individual field into HTML.
//
// $value is the data from the field and can be an array or string.
//
// These filters extract data from arrays based on the field structure.
// If you make a change it is a requirement to sanitize
// anything that will be output.
add_filter('squat_radar_field_html', [__CLASS__, 'field_date_html'], 5, 4);
add_filter('squat_radar_field_html', [__CLASS__, 'field_location_html'], 5, 4);
add_filter('squat_radar_field_html', [__CLASS__, 'field_link_html'], 5, 4);
// field 'url' was turned into a more link, example of an override with more specificity.
// Field 'url' was already turned into a <a> link, by field_link_html.
// The field_image_html is an example of an override with more specificity.
add_filter('squat_radar_field_html', [__CLASS__, 'field_image_html'], 7, 4);
// If $value is an array it is flattened into a string here.
// If $value != $original it will _not_ be sanitized, assumption is that it has been already.
add_filter('squat_radar_field_html', [__CLASS__, 'field_html'], 10, 4);
// $value is always a string. These filters just add additional wrapper markup.
// $value is always a string from this point.
// These filters just add additional wrapper markup.
add_filter('squat_radar_field_html', [__CLASS__, 'field_title_html'], 15, 4);
}
@ -26,7 +31,8 @@ class Squat_Radar_Formatter {
$context['event'] = $event;
$output = [];
$output[] = '<div class="squat-radar radar-event">';
$event_status = self::getValue( $event, ['event_status'] );
$output[] = '<div class="squat-radar radar-event radar-event-' . $event_status . '">';
foreach ($fields as $field) {
$field_tree = explode(':', $field);
$value = self::getValue($event, $field_tree);
@ -70,10 +76,11 @@ class Squat_Radar_Formatter {
// There can only be one date. With repeat etc. but just one.
// Repeating events will appear as a new item for each repeat in the feed.
$value = $value[0];
$output = '<span class="squat-radar-event-start-end">';
$output .= self::field_date_format($value['time_start'], 'start');
$output = '<span class="squat-radar-event-start-end">';
$output .= self::field_date_format( $value['time_start'], 'start' );
if ($value['time_start'] != $value['time_end']) {
$output .= self::field_date_format($value['time_end'], 'end');
$time_only = ( substr($value['time_start'], 0, 10) == substr($value['time_end'], 0, 10) );
$output .= self::field_date_format( $value['time_end'], 'end', $time_only );
}
$output .= '</span>';
return $output;
@ -97,15 +104,21 @@ class Squat_Radar_Formatter {
return $value;
}
private static function field_date_format($time, $start_end) {
private static function field_date_format($time, $start_end, $time_only = FALSE) {
$date_format = get_option('squat_radar_date_format', 'j M Y');
$time_format = get_option('squat_radar_time_format', 'H:i');
// Remove offset to stop time being converted to UTC.
$time = substr($time, 0, -6);
$output = '<span class="squat-radar-datetime squat-radar-datetime-' . $start_end .'">';
$output .= '<span class="squat-radar-date">';
$output .= date_i18n($date_format, strtotime($time));
$output .= '</span> <span class="squat-radar-time">';
if ( ! $time_only ) {
$output .= '<span class="squat-radar-date">';
$output .= date_i18n($date_format, strtotime($time));
$output .= '</span> ';
}
$output .= '<span class="squat-radar-time">';
$output .= date_i18n($time_format, strtotime($time));
$output .= '</span></span>';
@ -142,11 +155,11 @@ class Squat_Radar_Formatter {
case 'map':
$output = [];
foreach ($value as $map) {
if ( is_array($value) && $value['lat'] !== NULL && $value['lon'] !== NULL ) {
if ( is_array($map) && $map['lat'] !== NULL && $map['lon'] !== NULL ) {
$this_output = '<span class="squat-radar-location squat-radar-location-map-link">';
$lat = $value['lat'];
$lon = $value['lon'];
$this_output .= "<a href=\"https://www.openstreetmap.org/?mlat=$lat&mlon=$lon#map=12/$lat/$lon\">";
$lat = $map['lat'];
$lon = $map['lon'];
$this_output .= "<a href=\"https://www.openstreetmap.org/?mlat=$lat&mlon=$lon#map=14/$lat/$lon\">";
$this_output .= __('[Map]', 'squat-radar');
$this_output .= '</a></span>';
$output[] = $this_output;
@ -157,19 +170,19 @@ class Squat_Radar_Formatter {
case 'address':
$output = [];
foreach ($value as $address) {
if ( is_array($value) ) {
if ( is_array($address) ) {
$this_address = [];
foreach (['name_line', 'thoroughfare', 'locality', 'postal_code', 'country'] as $field_name) {
if (! empty($value[$field_name])) {
if (! empty($address[$field_name])) {
$this_line = '<span class="squat-radar-location-' . $field_name . '">';
$this_line .= sanitize_text_field($value[$field_name]);
$this_line .= sanitize_text_field($address[$field_name]);
$this_line .= '</span>';
$this_address[] = $this_line;
}
}
$this_output = '<span class="squat-radar-location squat-radar-location-address">';
$this_output .= implode(' ,', $this_address);
$this_output .= implode(', ', $this_address);
$this_output .= '</span>';
$output[] = $this_output;
}
@ -184,27 +197,28 @@ class Squat_Radar_Formatter {
* Item Radar links implementation of 'squat_radar_field_html' filter.
*/
function field_link_html($value, $original, $field, $context) {
if ($field[0] == 'title' && ! empty($context['event']['url'])) {
if ( ($field[0] == 'title' || $field[0] == 'title_field') && ! empty($context['event']['url'])) {
return '<a href="' . esc_url($context['event']['url']) . '" class="squat-radar-url squat-radar-url-title">' . sanitize_text_field( $value ) . '</a>';
}
if ($field[0] == 'url' && count($field) == 1) {
return '<a href="' . esc_url_raw($value) . '" class="squat-radar-url squat-radar-url-more">' . __('more…', 'squat-radar') . '</a>';
}
if ($field[0] == 'url') {
array_shift($field);
$field_tree = array_reverse($field);
$sibling_fields = self::getValue($event, $field_tree);
elseif ($field[0] == 'url') {
$title = esc_url($value);
$class = 'squat-radar-url-link';
if (! empty($sibling_fields['title']) ) {
$title = sanitize_text_field( $sibling_fields['title']);
$class = 'squat-radar-url-title';
}
elseif ( ! empty($sibling_fields['name']) ) {
$title = sanitize_text_field( $sibling_fields['name']);
$class = 'squat-radar-url-name';
array_shift($field);
if (is_array($field)) {
$field_tree = array_reverse($field);
$sibling_fields = self::getValue($context['event'], $field_tree);
$class = 'squat-radar-url-link';
if (! empty($sibling_fields['title']) ) {
$title = sanitize_text_field( $sibling_fields['title']);
$class = 'squat-radar-url-title';
}
elseif ( ! empty($sibling_fields['name']) ) {
$title = sanitize_text_field( $sibling_fields['name']);
$class = 'squat-radar-url-name';
}
}
return '<a href="' . esc_url_raw($value) . '" class="squat-radar-url ' . $class . '">' . $title . '</a>';
}
@ -223,11 +237,11 @@ class Squat_Radar_Formatter {
* image:file:url
*/
function field_image_html($value, $original, $field, $context) {
if ($field[0] == 'url' && $field[1] == 'file' && $field[2] == 'image') {
return '<img src="'. esc_url_raw($original) .'" class="squat-radar-image" \>';
}
if ($field[0] == 'url' && $field[1] == 'file' && $field[2] == 'image') {
return '<img src="'. esc_url_raw($original) .'" class="squat-radar-image" \>';
}
return $value;
return $value;
}
/**