'squat-radar-widget', 'description' => 'Radar Events List', ); $this->connector = new Squat_Radar_Connector(); parent::__construct( 'Squat_Radar', 'Squat Radar Events', $widget_ops ); } /** * Register the widget */ public static function register_widget() { register_widget( __CLASS__ ); 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'] ); } static public function widget_style() { wp_register_style( 'squat-radar-widget', SQUAT_RADAR_URL . '/assets/squat-radar.css' ); } static public function widget_script() { wp_register_script( 'squat-radar-widget', SQUAT_RADAR_URL . '/assets/squat-radar.js', ['jquery'] ); } 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 '
'; echo $args['after_widget']; } public static function ajax_callback() { if ( ! array_key_exists('instance', $_POST) ) { wp_die(); } $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 ); } catch ( Squat_Radar_Connector_Exception $e ) { $data = ['is_error' => TRUE]; if ( current_user_can( 'administrator' ) ) { $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 function form( $instance ) { // // Introduction. // echo ""; echo __('If you want to display this single widget in a page or post use the Shortcode:') . ' '; if ( $this->number == '__i__' ) { echo '' . __( 'Shortcode will be available once you have saved this the first time') . ''; } else { esc_attr_e( '[squat_radar_widget id="' . $this->number . '"]' ); } echo "
"; // // Title. // $field_id = esc_attr( $this->get_field_id( 'title' ) ); $field_name = esc_attr( $this->get_field_name( 'title' ) ); $field_label = esc_attr( 'Title:', 'squat-radar' ); $field_value = empty( $instance['title'] ) ? '' : esc_attr( $instance['title'] ); $field_class = 'widefat'; echo ""; echo ""; echo ""; echo "
"; // // Limit // $field_id = esc_attr( $this->get_field_id( 'limit' ) ); $field_name = esc_attr( $this->get_field_name( 'limit' ) ); $field_label = esc_attr( 'Number of events to display:', 'squat-radar' ); $field_value = empty( $instance['limit'] ) ? '10' : (int) $instance['limit']; $field_class = 'tiny-text'; echo ""; echo ""; echo ""; echo "
"; // // URL. // $field_error = ! empty( $instance['url']['error'] ); $field_id = esc_attr( $this->get_field_id( 'url' ) ); $field_name = esc_attr( $this->get_field_name( 'url' ) ); $field_label = esc_attr( 'Event Search URL:', 'squat-radar' ); $field_value = empty( $instance['url']['value'] ) ? '' : esc_attr( $instance['url']['value'] ); $field_class = 'widefat' . $field_error ? ' error' : ''; echo ""; echo ""; echo ""; echo "
"; if ( $field_error ) { echo '' . __('Currently selecting events:', 'squat-radar') . '
'; echo '