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

@ -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' ),