1
0
Fork 0
forked from lino/radar-wp

First alpha: working style, dynamic sidebars, part tested cron.

This commit is contained in:
ekes 2019-04-30 19:30:25 +02:00
parent a6f657092d
commit 2342ebb052
6 changed files with 81 additions and 77 deletions

View file

@ -29,7 +29,15 @@
font-weight: bold;
}
.squat-radar .squat-radar-datetime::before {
.squat-radar .squat-radar-datetime.squat-radar-datetime-start::before {
content: '';
display: block;
float: none;
clear: both;
}
.squat-radar .squat-radar-event-start-end::after,
.squat-radar .squat-radar-event-start {
content: '';
display: block;
float: none;
@ -38,6 +46,15 @@
.squat-radar .squat-radar-list li {
display: inline;
list-style: none;
}
.squat-radar .squat-radar-list li:after {
content: ", ";
}
.squat-radar .squat-radar-list li:last-child:after {
content: "";
}
.squat-radar a.squat-radar-url-more {

View file

@ -18,6 +18,7 @@ jQuery(function($){
);
}
} else {
$(widget).empty();
$(widget).append(result.html);
// $(".squat-li a").on('click', function(e){

View file

@ -58,7 +58,8 @@ class Squat_Radar_Connector {
}
function events( $facets, $fields = [], $language = NULL, $limit = 10, $expiration = 10800, $reset = FALSE ) {
$fields = array_merge($fields, ['uuid', 'title', 'url', 'event_status']);
// Fields we often want to get data out of but not necessarily are chosen to be shown.
$fields = array_merge($fields, ['uuid', 'title', 'body:value', 'url', 'event_status']);
$transient_key = 'squat_radar_events_' . sha1(implode($facets) . implode($fields) . $language . $limit);
if (! $reset && $data = get_transient( $transient_key )) {
return $data;

View file

@ -16,6 +16,7 @@ class Squat_Radar_Formatter {
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);
add_filter('squat_radar_field_html', [__CLASS__, 'field_summary_html'], 5, 4);
// 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);
@ -76,26 +77,26 @@ 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 = '<span class="squat-radar-event-start-end">';
$output .= self::field_date_format( $value['time_start'], 'start' );
if ($value['time_start'] != $value['time_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 .= ' - ' . self::field_date_format( $value['time_end'], 'end', $time_only );
}
$output .= '</span>';
return $output;
case 'time_start':
$value = $value[0];
$output = '<span class="squat-radar-event-start">';
$output .= self::field_date_format($value, 'start');
$output .= '</span>';
return $output;
case 'time_end':
$output = '<span class="squat-radar-event-start">';
$output .= self::field_date_format($value, 'start');
$value = $value[0];
$output = '<span class="squat-radar-event-end">';
$output .= self::field_date_format($value, 'end');
$output .= '</span>';
return $output;
@ -150,16 +151,16 @@ class Squat_Radar_Formatter {
* }
* ]
*/
function field_location_html($value, $original, $field, $context) {
static public function field_location_html($value, $original, $field, $context) {
switch ($field[0]) {
case 'map':
$output = [];
foreach ($value as $map) {
if ( is_array($map) && $map['lat'] !== NULL && $map['lon'] !== NULL ) {
if ( is_array($map) && ! empty($map['lat']) && $map['lat'] !== NULL && $map['lon'] !== NULL ) {
$this_output = '<span class="squat-radar-location squat-radar-location-map-link">';
$lat = $map['lat'];
$lon = $map['lon'];
$this_output .= "<a href=\"https://www.openstreetmap.org/?mlat=$lat&mlon=$lon#map=14/$lat/$lon\">";
$this_output .= "<a href=\"https://www.openstreetmap.org/?mlat=$lat&mlon=$lon#map=14/$lat/$lon\" target=\"_blank\">";
$this_output .= __('[Map]', 'squat-radar');
$this_output .= '</a></span>';
$output[] = $this_output;
@ -196,7 +197,7 @@ class Squat_Radar_Formatter {
/**
* Item Radar links implementation of 'squat_radar_field_html' filter.
*/
function field_link_html($value, $original, $field, $context) {
static public function field_link_html($value, $original, $field, $context) {
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>';
}
@ -230,13 +231,40 @@ class Squat_Radar_Formatter {
return $value;
}
/**
* Item Radar summary implementation of 'squat_radar_field_html' filter.
*/
static public function field_summary_html($value, $original, $field, $context) {
if ( $field[0] == 'summary' ) {
// Summary is only populated if there is an explict summary.
if ( empty( trim($value) ) ) {
array_shift($field);
if (is_array($field)) {
$field_tree = array_reverse($field);
$sibling_fields = self::getValue($context['event'], $field_tree);
$value = print_r($context['event'], true);
if (! empty( $sibling_fields['value'] ) ) {
$value = wp_trim_words( $sibling_fields['value'], 30 );
}
}
}
if ( ! empty($value) ) {
$value = '<span class="squat-radar-body-summary">' . wp_kses_post( $value ) . '</span>';
}
}
return $value;
}
/**
* Format image implementation of 'squat_radar_field_html' filter.
*
* Deliberatly run after field_link_html. Showing how to override an existing filter.
* image:file:url
*/
function field_image_html($value, $original, $field, $context) {
static public 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" \>';
}
@ -261,7 +289,7 @@ class Squat_Radar_Formatter {
* @return string
* Flattend array with additional default classes.
*/
function field_html($value, $original, $field, $context) {
static public function field_html($value, $original, $field, $context) {
if ($value != $original) {
return $value;
}
@ -306,7 +334,7 @@ class Squat_Radar_Formatter {
return $output;
}
else {
$value = '<span class="squat-radar-' . sanitize_html_class($field[0]) . '">' . $value . '</span>';
$value = '<span class="squat-radar-' . sanitize_html_class($field[0]) . '">' . wp_kses_post( $value ) . '</span>';
}
return $value;
@ -315,8 +343,8 @@ class Squat_Radar_Formatter {
/**
* Title field HTML implentation of 'squat_radar_field_html' filter.
*/
function field_title_html($value, $original, $field, $context) {
if ($field[0] == 'title' && count($field) == 1) {
static public function field_title_html($value, $original, $field, $context) {
if (($field[0] == 'title' || $field[0] == 'title_field') && count($field) == 1) {
$value = '<h3 class="squat-radar-title">' . $value . '</h3>';
}

View file

@ -62,7 +62,7 @@ class Squat_Radar_Instance {
register_sidebar([
'name' => __( 'Squat Radar Shortcodes'),
'description'=> __( 'This widget area is not by default displayed on frontend. It can be displayed with all its widgets with the [squat_radar] shortcode; or used to hold active widgets displayed with their own [squat_radar_widget id="X"] shortcode, see instructions on widget configuration for the id.', 'squat-radar' ),
'description'=> __( 'This widget area is not by default displayed on frontend. It can be displayed with all its widgets with the [squat_radar_sidebar] shortcode.', 'squat-radar' ),
'id' => 'squat_radar_widget_shortcode',
'before_widget' => '<div class="widget %2$s">',
'after_widget' => '</div>',

View file

@ -22,9 +22,7 @@ class Squat_Radar_Widget extends WP_Widget {
add_action( 'wp_ajax_squat_radar_events', [__CLASS__, 'ajax_callback'] );
add_action( 'wp_ajax_nopriv_squat_radar_events', [__CLASS__, 'ajax_callback'] );
add_action( 'wp_enqueue_scripts', [__CLASS__, 'widget_script'] );
add_action( 'wp_enqueue_style', [__CLASS__, 'widget_style'] );
add_shortcode( 'squat_radar_widget', [__CLASS__, 'shortcode' ] );
add_action( 'wp_enqueue_scripts', [__CLASS__, 'widget_style'] );
add_action( 'squat_radar_widget_cache_cron', [__CLASS__, 'cache_cron'] );
add_option( 'squat_radar_widget_cron_run', []);
@ -32,44 +30,13 @@ class Squat_Radar_Widget extends WP_Widget {
static public function widget_style() {
wp_register_style( 'squat-radar-widget', SQUAT_RADAR_URL . '/assets/squat-radar.css' );
wp_enqueue_style( 'squat-radar-widget' );
}
static public function widget_script() {
wp_register_script( 'squat-radar-widget', SQUAT_RADAR_URL . '/assets/squat-radar.js', ['jquery'] );
}
/**
* Output a widget using 'squat_radar_widget' shortcode.
*
* Requires the widget ID.
*
* @example [squat_radar_widget id="1"]
*/
static public function shortcode( $attributes, $content = '' ) {
$defaults = [
'id' => '__i__',
];
$attributes = shortcode_atts($defaults, $attributes, 'squat_radar_widget');
// Return early if ID is unknown.
$option = get_option( 'widget_squat_radar' );
if (! isset( $option[$attributes['id']] )) {
if ( current_user_can( 'administrator' ) ) {
$content = '<span class="error">' . __('Squat Radar Widget shortcode ID not recognised. Check the suggestion at the top of the widget in the adminstration interface.', 'squat-radar') . '</span>';
}
return $content;
}
$instance = $option[$attributes['id']];
// render the widget
ob_start();
// To allow overriding the args here? For before after etc.
the_widget( __CLASS__, $instance);
$content = ob_get_clean();
return $content;
}
public static function cache_cron() {
$now = time();
$last_run = get_option('squat_radar_widget_cron_run', []);
@ -86,12 +53,15 @@ class Squat_Radar_Widget extends WP_Widget {
protected static function cache_refresh($instance) {
$connector = new Squat_Radar_Connector();
// @todo Languages...
try {
$data = $connector->events($instance['url']['keys']['facets'], $instance['fields'], $language, $instance['limit'], 0, TRUE );
}
catch ( Squat_Radar_Connector_Exception $e ) {
return FALSE;
$languages = apply_filters( 'wpml_active_languages', NULL);
$languages = array_merge($instance['url']['keys']['language'], (array) $languages);
foreach ($languages as $language) {
try {
$data = $connector->events($instance['url']['keys']['facets'], $instance['fields'], $language, $instance['limit'], 0, TRUE );
}
catch ( Squat_Radar_Connector_Exception $e ) {
return FALSE;
}
}
return TRUE;
@ -99,7 +69,6 @@ class Squat_Radar_Widget extends WP_Widget {
public function widget( $args, $instance ) {
wp_enqueue_style( 'squat-radar-widget' );
$widget_id = 'squat_radar_widget_' . $this->number;
echo $args['before_widget'];
@ -158,7 +127,7 @@ class Squat_Radar_Widget extends WP_Widget {
public static function instance_events_html($instance) {
$language = defined('ICL_LANGUAGE_CODE') ? ICL_LANGUAGE_CODE : $instance['url']['keys']['language'];
$connector = new Squat_Radar_Connector();
$data = $connector->events($instance['url']['keys']['facets'], $instance['fields'], $language, $instance['limit'], $instance['cache_expire'], TRUE );
$data = $connector->events($instance['url']['keys']['facets'], $instance['fields'], $language, $instance['limit'], $instance['cache_expire']);
$html = '';
foreach ($data['result'] as $id => $event) {
$output = apply_filters( 'squat_radar_format_event', $event, $instance['fields'], ['instance' => $instance] );
@ -170,19 +139,6 @@ class Squat_Radar_Widget extends WP_Widget {
public function form( $instance ) {
//
// Introduction.
//
echo "<p>";
echo __('If you want to display this single widget in a page or post use the Shortcode:') . '&nbsp;';
if ( $this->number == '__i__' ) {
echo '<em>' . __( 'Shortcode will be available once you have saved this the first time') . '</em>';
}
else {
esc_attr_e( '[squat_radar_widget id="' . $this->number . '"]' );
}
echo "</p>";
//
// Title.
//
@ -279,7 +235,7 @@ class Squat_Radar_Widget extends WP_Widget {
echo "<label for=\"$field_id\">$field_label</label>";
echo "<input class=\"$field_class\" id=\"$field_id\" name=\"$field_name\" type=\"text\" value=\"$field_value\">";
echo "</p>";
echo '<div class="description">' . __('A comma seperated list of field API names. Examples: phone, price, flyer, offline:address:thoroughfare. Some fields might need an additonal filter to format them properly. Can also be used instead of checkboxes to define the order fields are displayed in.') . '</div>';
echo '<div class="description">' . __('A comma seperated list of field API names. Examples: phone, price, flyer, offline:address:thoroughfare. Some fields might need an additonal filter to format them properly.') . '</div>';
//
// Cache expiry.
@ -306,7 +262,7 @@ class Squat_Radar_Widget extends WP_Widget {
$checked = checked( $use_cron, TRUE, FALSE );
echo "<input type=\"checkbox\" class=\"checkbox\" id=\"$field_id\" name=\"$field_name\"$checked />";
echo "<label for=\"$field_id\">$field_label</label><br />";
echo '<div class="description">' . __('Do not use AJAX, but always display the cached version of the events. Update the cache after the expiry length using cron. Works best if you have a regular external cronjob running.') . '</div>';
echo '<div class="description">' . __('Experimental. Do not use AJAX, but always display the cached version of the events. Update the cache after the expiry length using cron. Works best if you have a regular external cronjob running.') . '</div>';
echo '</fieldset>';
@ -369,7 +325,7 @@ class Squat_Radar_Widget extends WP_Widget {
}
else {
$options['use_cron'] = TRUE;
cache_refresh($options);
self::cache_refresh($options);
if ( ! wp_next_scheduled( 'squat_radar_widget_cache_cron' ) ) {
wp_schedule_event( time() + $options['cache_expire'], 'hourly', 'squat_radar_widget_cache_cron');
}
@ -385,6 +341,7 @@ class Squat_Radar_Widget extends WP_Widget {
'date_time' => __( 'Date and Time (start and optional end)' ),
'date_time:time_start' => __( 'Date and Time (start only)' ),
'body' => __( 'Body' ),
'body:summary' => __( 'Body (teaser, summary)' ),
'category' => __( 'Categories' ),
'topic' => __( 'Tags' ),
'offline:address' => __( 'Address' ),