1
0
Fork 0
forked from lino/radar-wp

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

@ -23,6 +23,11 @@ class Squat_Radar_Widget extends WP_Widget {
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( 'squat_radar_widget_cache_cron', [__CLASS__, 'cache_cron'] );
add_option( 'squat_radar_widget_cron_run', []);
}
static public function widget_style() {
@ -33,23 +38,98 @@ class Squat_Radar_Widget extends WP_Widget {
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', []);
foreach (self::cron_instances() as $number => $instance) {
if (! isset($last_run[$number]) || $last_run[$number] + $instance['cache_expire'] < $now ) {
if (self::cache_refresh($instance)) {
$last_run[$number] = $now;
}
}
}
set_option('squat_radar_widget_cron_run', $last_run);
}
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;
}
return TRUE;
}
public function widget( $args, $instance ) {
wp_enqueue_style( 'squat-radar-widget' );
wp_enqueue_script( 'squat-radar-widget');
wp_localize_script( 'squat-radar-widget', 'squat_radar_widget', [ 'ajaxurl' => admin_url( 'admin-ajax.php' ) ] );
// Seems non-trivial to send (potentially) multiple values for different widget instances appending an array/hash.
$widget_id = 'squat_radar_widget_' . $this->number;
wp_localize_script( 'squat-radar-widget', $widget_id, $instance );
echo $args['before_widget'];
if ( ! empty( $instance['title'] ) ) {
echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title'];
}
echo '<div id="' . $widget_id . '" class="squat-radar-widget squat-radar-ajax"><a href="' . esc_url_raw( $instance['url']['value'] ) . '">'
. esc_url( $instance['url']['value'] )
. '</a></div>';
if ( ! empty($instance['use_cron']) ) {
try {
echo self::instance_events_html($instance);
}
catch ( Squat_Radar_Connector_Exception $e ) {
if ( current_user_can( 'administrator' ) ) {
echo $e->getCode() . ': ' . $e->getMessage();
}
echo '<div id="' . $widget_id . '" class="squat-radar-widget"><a href="' . esc_url_raw( $instance['url']['value'] ) . '">'
. esc_url( $instance['url']['value'] )
. '</a></div>';
}
}
else {
wp_enqueue_script( 'squat-radar-widget');
wp_localize_script( 'squat-radar-widget', 'squat_radar_widget', [ 'ajaxurl' => admin_url( 'admin-ajax.php' ) ] );
wp_localize_script( 'squat-radar-widget', $widget_id, $instance );
echo '<div id="' . $widget_id . '" class="squat-radar-widget squat-radar-ajax"><a href="' . esc_url_raw( $instance['url']['value'] ) . '">'
. esc_url( $instance['url']['value'] )
. '</a></div>';
}
echo $args['after_widget'];
}
@ -59,11 +139,10 @@ class Squat_Radar_Widget extends WP_Widget {
wp_die();
}
$data = [];
$instance = $_POST['instance'];
$language = defined('ICL_LANGUAGE_CODE') ? ICL_LANGUAGE_CODE : $instance['url']['keys']['language'];
$connector = new Squat_Radar_Connector();
try {
$data = $connector->events($instance['url']['keys']['facets'], $instance['fields'], $language, $instance['limit'], $instance['cache_expire'], TRUE );
$data['html'] = self::instance_events_html($instance);
}
catch ( Squat_Radar_Connector_Exception $e ) {
$data = ['is_error' => TRUE];
@ -71,20 +150,26 @@ class Squat_Radar_Widget extends WP_Widget {
$data['error']['code'] = $e->getCode();
$data['error']['message'] = $e->getMessage();
}
wp_send_json($data);
}
$html = '';
foreach ($data['result'] as $id => $event) {
$output = apply_filters( 'squat_radar_format_event', $event, $instance['fields'], ['instance' => $instance] );
$html .= implode($output);
}
$data['html'] = $html;
wp_send_json($data);
}
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 );
$html = '';
foreach ($data['result'] as $id => $event) {
$output = apply_filters( 'squat_radar_format_event', $event, $instance['fields'], ['instance' => $instance] );
$html .= implode(' ', $output);
}
return $html;
}
public function form( $instance ) {
//
// Introduction.
//
@ -159,6 +244,25 @@ class Squat_Radar_Widget extends WP_Widget {
}
echo '<hr>';
echo '<fieldset>';
echo '<legend>' . __('Fields', 'squat-radar') . '</legend>';
echo '<p>';
foreach ($this->preset_fields() as $api_field_name => $field_label) {
$field_id = esc_attr( $this->get_field_id( 'field-' . $api_field_name ) );
$field_name = esc_attr( $this->get_field_name( 'field-' . $api_field_name ) );
$field_label = esc_attr( $field_label );
$checked = '';
if ( isset($instance['fields'][$api_field_name]) ) {
unset($instance['fields'][$api_field_name]);
$checked = ' checked="checked"';
}
echo "<input type=\"checkbox\" class=\"checkbox\" id=\"$field_id\" name=\"$field_name\"$checked />";
echo "<label for=\"$field_id\">$field_label</label><br />";
}
echo '</p>';
echo '</fieldset>';
// ADVANCED
echo '<hr>';
echo '<fieldset>';
@ -168,14 +272,14 @@ class Squat_Radar_Widget extends WP_Widget {
//
$field_id = esc_attr( $this->get_field_id( 'fields' ) );
$field_name = esc_attr( $this->get_field_name( 'fields' ) );
$field_label = esc_attr( 'Fields:', 'squat-radar' );
$field_value = empty( $instance['fields'] ) ? '' : esc_attr( implode( ' ,', $instance['fields'] ) );
$field_label = esc_attr( 'Additional fields:', 'squat-radar' );
$field_value = empty( $instance['fields'] ) ? '' : esc_attr( implode( ', ', $instance['fields'] ) );
$field_class = 'widefat';
echo "<p>";
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 list of fields to display. Presently these are API names, hence advanced field. Examples: title, body, topic, category, date_time, image, flyer, tags, offline:map') . '</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. Can also be used instead of checkboxes to define the order fields are displayed in.') . '</div>';
//
// Cache expiry.
@ -194,6 +298,15 @@ class Squat_Radar_Widget extends WP_Widget {
echo "</select>";
echo "</p>";
echo '<div class="description">' . __('Length of time the cache of events will be kept. Longer faster, but updated less often.') . '</div>';
$field_id = esc_attr( $this->get_field_id( 'use_cron' ) );
$field_name = esc_attr( $this->get_field_name( 'use_cron' ) );
$field_label = esc_attr__( 'Use cron' );
$use_cron = isset($instance['use_cron']) ? (bool) $instance['use_cron'] : false;
$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 '</fieldset>';
@ -202,7 +315,6 @@ class Squat_Radar_Widget extends WP_Widget {
public function update( $new_instance, $old_instance ) {
$options = [];
$options['debug'] = $new_instance;
if ( ! empty( $new_instance['title'] ) ) {
$options['title'] = sanitize_text_field( $new_instance['title'] );
@ -223,13 +335,17 @@ class Squat_Radar_Widget extends WP_Widget {
$options['url'] = ['value' => '', 'keys' => []];
}
$options['fields'] = [];
foreach ($this->preset_fields() as $field_name => $field_label) {
if ( ! empty($new_instance['field-' . $field_name]) ) {
$options['fields'][$field_name] = $field_name;
}
}
if ( ! empty($new_instance['fields']) ) {
$matches = [];
preg_match_all('/([a-zA-Z_:]+)/', $new_instance['fields'], $matches);
$options['fields'] = $matches[0];
}
else {
$options['fields'] = [];
$options['fields'] += array_combine($matches[0], $matches[0]);
}
if ( ! empty( $new_instance['limit'] ) ) {
@ -243,8 +359,54 @@ class Squat_Radar_Widget extends WP_Widget {
$options['cache_expire'] = 10800;
}
if ( empty( $new_instance['use_cron'] )) {
$options['use_cron'] = FALSE;
$cron_instances = self::cron_instances();
unset($cron_instances[$this->number]);
if ( empty($cron_instances) && ($timestamp = wp_next_scheduled( 'squat_radar_widget_cache_cron' ) )) {
wp_unschedule_event( $timestamp, 'squat_radar_widget_cache_cron' );
}
}
else {
$options['use_cron'] = TRUE;
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');
}
}
return $options;
}
}
public function preset_fields() {
return [
'title_field' => __( 'Title' ),
'event_status' => __( 'Event status (proposed, or cancelled)' ),
'date_time' => __( 'Date and Time (start and optional end)' ),
'date_time:time_start' => __( 'Date and Time (start only)' ),
'body' => __( 'Body' ),
'category' => __( 'Categories' ),
'topic' => __( 'Tags' ),
'offline:address' => __( 'Address' ),
'offline:map' => __( 'Map (link)' ),
'og_group_ref' => __( 'Groups' ),
'price_category' => __( 'Price category' ),
'image:file:url' => __( 'Image' ),
'link' => __( 'Event URL (entered not Radar)' ),
'url' => __( 'More link (to event on Radar)' ),
];
}
public static function cron_instances() {
$cron_instances = [];
$instances = get_option( 'widget_squat_radar' );
foreach ($instances as $number => $instance) {
if (! empty($instance['use_cron']) ) {
$cron_instances[$number] = $instance;
}
}
return $cron_instances;
}
}