639 lines
24 KiB
PHP
Executable File
639 lines
24 KiB
PHP
Executable File
<?php
|
|
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
class KGV_Termine_Plugin {
|
|
private static $instance = null;
|
|
private $table_name;
|
|
|
|
public static function instance() {
|
|
if (self::$instance === null) {
|
|
self::$instance = new self();
|
|
}
|
|
return self::$instance;
|
|
}
|
|
|
|
private function __construct() {
|
|
global $wpdb;
|
|
$this->table_name = $wpdb->prefix . KGV_TERMINE_TABLE;
|
|
|
|
register_activation_hook(KGV_TERMINE_FILE, array($this, 'activate'));
|
|
|
|
add_action('admin_menu', array($this, 'register_admin_menu'));
|
|
add_action('admin_enqueue_scripts', array($this, 'enqueue_admin_assets'));
|
|
add_action('wp_enqueue_scripts', array($this, 'enqueue_front_assets'));
|
|
add_action('init', array($this, 'register_shortcodes'));
|
|
add_action('init', array($this, 'maybe_upgrade_table'));
|
|
add_action('admin_post_kgv_save_termin', array($this, 'handle_save_termin'));
|
|
add_action('admin_post_kgv_delete_termin', array($this, 'handle_delete_termin'));
|
|
add_filter('query_vars', array($this, 'register_query_vars'));
|
|
}
|
|
|
|
public function activate() {
|
|
$this->create_table();
|
|
}
|
|
|
|
private function create_table() {
|
|
global $wpdb;
|
|
|
|
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
|
|
|
|
$charset_collate = $wpdb->get_charset_collate();
|
|
$sql = "CREATE TABLE {$this->table_name} (
|
|
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
|
|
title VARCHAR(255) NOT NULL,
|
|
event_date DATETIME NOT NULL,
|
|
owner VARCHAR(255) NULL,
|
|
location VARCHAR(255) NULL,
|
|
summary TEXT NULL,
|
|
description LONGTEXT NULL,
|
|
is_published TINYINT(1) NOT NULL DEFAULT 1,
|
|
created_at DATETIME NOT NULL,
|
|
updated_at DATETIME NOT NULL,
|
|
post_id BIGINT UNSIGNED NULL DEFAULT NULL,
|
|
PRIMARY KEY (id),
|
|
KEY event_date (event_date),
|
|
KEY is_published (is_published)
|
|
) {$charset_collate};";
|
|
|
|
dbDelta($sql);
|
|
}
|
|
|
|
public function maybe_upgrade_table() {
|
|
global $wpdb;
|
|
|
|
$col = $wpdb->get_results(
|
|
$wpdb->prepare(
|
|
"SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = %s AND TABLE_NAME = %s AND COLUMN_NAME = 'post_id'",
|
|
DB_NAME,
|
|
$this->table_name
|
|
)
|
|
);
|
|
|
|
if ( empty( $col ) ) {
|
|
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
|
|
$charset_collate = $wpdb->get_charset_collate();
|
|
$sql = "CREATE TABLE {$this->table_name} (
|
|
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
|
|
title VARCHAR(255) NOT NULL,
|
|
event_date DATETIME NOT NULL,
|
|
owner VARCHAR(255) NULL,
|
|
location VARCHAR(255) NULL,
|
|
summary TEXT NULL,
|
|
description LONGTEXT NULL,
|
|
is_published TINYINT(1) NOT NULL DEFAULT 1,
|
|
created_at DATETIME NOT NULL,
|
|
updated_at DATETIME NOT NULL,
|
|
post_id BIGINT UNSIGNED NULL DEFAULT NULL,
|
|
PRIMARY KEY (id),
|
|
KEY event_date (event_date),
|
|
KEY is_published (is_published)
|
|
) {$charset_collate};";
|
|
dbDelta($sql);
|
|
}
|
|
}
|
|
|
|
public function enqueue_admin_assets($hook) {
|
|
if (strpos((string) $hook, 'kgv-termine') === false) {
|
|
return;
|
|
}
|
|
|
|
wp_enqueue_style(
|
|
'kgv-termine-admin-style',
|
|
KGV_TERMINE_URL . 'assets/css/admin.css',
|
|
array(),
|
|
'1.0.2'
|
|
);
|
|
}
|
|
|
|
public function enqueue_front_assets() {
|
|
wp_enqueue_style(
|
|
'kgv-termine-front-style',
|
|
KGV_TERMINE_URL . 'assets/css/front.css',
|
|
array(),
|
|
'1.0.2'
|
|
);
|
|
}
|
|
|
|
public function register_query_vars($vars) {
|
|
$vars[] = 'kgv_termin';
|
|
return $vars;
|
|
}
|
|
|
|
public function register_admin_menu() {
|
|
add_menu_page(
|
|
'Termine',
|
|
'Termine',
|
|
'manage_options',
|
|
'kgv-termine',
|
|
array($this, 'render_list_page'),
|
|
'dashicons-calendar-alt',
|
|
25
|
|
);
|
|
|
|
add_submenu_page(
|
|
'kgv-termine',
|
|
'Alle Termine',
|
|
'Alle Termine',
|
|
'manage_options',
|
|
'kgv-termine',
|
|
array($this, 'render_list_page')
|
|
);
|
|
|
|
add_submenu_page(
|
|
'kgv-termine',
|
|
'Neuer Termin',
|
|
'Neuer Termin',
|
|
'manage_options',
|
|
'kgv-termine-new',
|
|
array($this, 'render_form_page')
|
|
);
|
|
}
|
|
|
|
public function register_shortcodes() {
|
|
add_shortcode('kgv_termine', array($this, 'render_termine_shortcode'));
|
|
add_shortcode('kgv_termin_detail', array($this, 'render_detail_shortcode'));
|
|
add_shortcode('kgv_termine_sidebar', array($this, 'render_sidebar_shortcode'));
|
|
}
|
|
|
|
public function handle_save_termin() {
|
|
if (!current_user_can('manage_options')) {
|
|
wp_die('Keine Berechtigung.');
|
|
}
|
|
|
|
check_admin_referer('kgv_save_termin');
|
|
$this->save_termin_from_request();
|
|
}
|
|
|
|
public function handle_delete_termin() {
|
|
if (!current_user_can('manage_options')) {
|
|
wp_die('Keine Berechtigung.');
|
|
}
|
|
|
|
$id = isset($_GET['termin_id']) ? absint($_GET['termin_id']) : 0;
|
|
|
|
if (!$id || !isset($_GET['_wpnonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_GET['_wpnonce'])), 'kgv_delete_termin_' . $id)) {
|
|
wp_safe_redirect(admin_url('admin.php?page=kgv-termine&message=delete_failed'));
|
|
exit;
|
|
}
|
|
|
|
global $wpdb;
|
|
$wpdb->delete($this->table_name, array('id' => $id), array('%d'));
|
|
wp_safe_redirect(admin_url('admin.php?page=kgv-termine&message=deleted'));
|
|
exit;
|
|
}
|
|
|
|
private function save_termin_from_request() {
|
|
global $wpdb;
|
|
|
|
$id = isset($_POST['termin_id']) ? absint($_POST['termin_id']) : 0;
|
|
$title = sanitize_text_field(wp_unslash($_POST['title'] ?? ''));
|
|
$event_date = sanitize_text_field(wp_unslash($_POST['event_date'] ?? ''));
|
|
$owner = sanitize_text_field(wp_unslash($_POST['owner'] ?? ''));
|
|
$location = sanitize_text_field(wp_unslash($_POST['location'] ?? ''));
|
|
$summary = sanitize_textarea_field(wp_unslash($_POST['summary'] ?? ''));
|
|
$description = wp_kses_post(wp_unslash($_POST['description'] ?? ''));
|
|
$is_published = isset($_POST['is_published']) ? (int) wp_unslash($_POST['is_published']) : 1;
|
|
$is_published = $is_published === 1 ? 1 : 0;
|
|
$post_id = isset($_POST['post_id']) ? absint($_POST['post_id']) : 0;
|
|
$post_id = $post_id > 0 ? $post_id : null;
|
|
|
|
if ($title === '' || $event_date === '' || strtotime($event_date) === false) {
|
|
$target = $id ? admin_url('admin.php?page=kgv-termine-new&termin_id=' . $id . '&message=missing') : admin_url('admin.php?page=kgv-termine-new&message=missing');
|
|
wp_safe_redirect($target);
|
|
exit;
|
|
}
|
|
|
|
$mysql_date = date('Y-m-d H:i:s', strtotime($event_date));
|
|
$now = current_time('mysql');
|
|
|
|
$data = array(
|
|
'title' => $title,
|
|
'event_date' => $mysql_date,
|
|
'owner' => $owner,
|
|
'location' => $location,
|
|
'summary' => $summary,
|
|
'description' => $description,
|
|
'is_published' => $is_published,
|
|
'post_id' => $post_id,
|
|
'updated_at' => $now,
|
|
);
|
|
|
|
$formats = array('%s', '%s', '%s', '%s', '%s', '%s', '%d', '%d', '%s');
|
|
|
|
if ($id) {
|
|
$wpdb->update($this->table_name, $data, array('id' => $id), $formats, array('%d'));
|
|
wp_safe_redirect(admin_url('admin.php?page=kgv-termine&message=updated'));
|
|
exit;
|
|
}
|
|
|
|
$data['created_at'] = $now;
|
|
$wpdb->insert($this->table_name, $data, array('%s', '%s', '%s', '%s', '%s', '%s', '%d', '%d', '%s', '%s'));
|
|
|
|
wp_safe_redirect(admin_url('admin.php?page=kgv-termine&message=created'));
|
|
exit;
|
|
}
|
|
|
|
public function render_list_page() {
|
|
global $wpdb;
|
|
|
|
$rows = $wpdb->get_results("SELECT * FROM {$this->table_name} ORDER BY event_date ASC, id ASC");
|
|
$message = isset($_GET['message']) ? sanitize_text_field(wp_unslash($_GET['message'])) : '';
|
|
?>
|
|
<div class="wrap kgv-admin-wrap">
|
|
<div class="kgv-admin-header">
|
|
<h1>Termine</h1>
|
|
<a href="<?php echo esc_url(admin_url('admin.php?page=kgv-termine-new')); ?>" class="page-title-action">Neuen Termin anlegen</a>
|
|
</div>
|
|
|
|
<?php $this->render_notice($message); ?>
|
|
|
|
<div class="kgv-admin-card">
|
|
<table class="widefat striped kgv-admin-table">
|
|
<thead>
|
|
<tr>
|
|
<th>Titel</th>
|
|
<th>Datum</th>
|
|
<th>Verantwortlich</th>
|
|
<th>Ort</th>
|
|
<th>Status</th>
|
|
<th>Aktionen</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php if (empty($rows)) : ?>
|
|
<tr>
|
|
<td colspan="6">Noch keine Termine angelegt.</td>
|
|
</tr>
|
|
<?php else : ?>
|
|
<?php foreach ($rows as $row) : ?>
|
|
<tr>
|
|
<td><strong><?php echo esc_html($row->title); ?></strong></td>
|
|
<td><?php echo esc_html(wp_date('d.m.Y H:i', strtotime($row->event_date))); ?></td>
|
|
<td><?php echo esc_html($row->owner ?: '—'); ?></td>
|
|
<td><?php echo esc_html($row->location ?: '—'); ?></td>
|
|
<td><?php echo (int) $row->is_published === 1 ? 'Veröffentlicht' : 'Entwurf'; ?></td>
|
|
<td>
|
|
<a href="<?php echo esc_url(admin_url('admin.php?page=kgv-termine-new&termin_id=' . (int) $row->id)); ?>">Bearbeiten</a>
|
|
|
|
|
<a href="<?php echo esc_url(wp_nonce_url(admin_url('admin-post.php?action=kgv_delete_termin&termin_id=' . (int) $row->id), 'kgv_delete_termin_' . (int) $row->id)); ?>" onclick="return confirm('Termin wirklich löschen?');">Löschen</a>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
<?php endif; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
<?php
|
|
}
|
|
|
|
public function render_form_page() {
|
|
global $wpdb;
|
|
|
|
$id = isset($_GET['termin_id']) ? absint($_GET['termin_id']) : 0;
|
|
$message = isset($_GET['message']) ? sanitize_text_field(wp_unslash($_GET['message'])) : '';
|
|
$row = null;
|
|
|
|
if ($id) {
|
|
$row = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$this->table_name} WHERE id = %d", $id));
|
|
}
|
|
|
|
$title = $row ? $row->title : '';
|
|
$event_date = $row ? date('Y-m-d\TH:i', strtotime($row->event_date)) : '';
|
|
$owner = $row ? $row->owner : '';
|
|
$location = $row ? $row->location : '';
|
|
$summary = $row ? $row->summary : '';
|
|
$description = $row ? $row->description : '';
|
|
$is_published = $row ? (int) $row->is_published : 1;
|
|
$post_id = $row && !empty($row->post_id) ? (int) $row->post_id : 0;
|
|
$posts = get_posts(array('post_type' => 'post', 'post_status' => 'publish', 'numberposts' => -1, 'orderby' => 'title', 'order' => 'ASC'));
|
|
?>
|
|
<div class="wrap kgv-admin-wrap">
|
|
<div class="kgv-admin-header">
|
|
<h1><?php echo $id ? 'Termin bearbeiten' : 'Neuen Termin anlegen'; ?></h1>
|
|
</div>
|
|
|
|
<?php $this->render_notice($message); ?>
|
|
|
|
<form method="post" action="<?php echo esc_url(admin_url('admin-post.php')); ?>" class="kgv-admin-form">
|
|
<?php wp_nonce_field('kgv_save_termin'); ?>
|
|
<input type="hidden" name="action" value="kgv_save_termin">
|
|
<input type="hidden" name="termin_id" value="<?php echo esc_attr($id); ?>">
|
|
|
|
<div class="kgv-admin-card">
|
|
<div class="kgv-form-grid">
|
|
<div class="kgv-form-field kgv-form-field-full">
|
|
<label for="kgv-title">Titel</label>
|
|
<input type="text" id="kgv-title" name="title" value="<?php echo esc_attr($title); ?>" required>
|
|
</div>
|
|
|
|
<div class="kgv-form-field">
|
|
<label for="kgv-event-date">Datum / Uhrzeit</label>
|
|
<input type="datetime-local" id="kgv-event-date" name="event_date" value="<?php echo esc_attr($event_date); ?>" required>
|
|
</div>
|
|
|
|
<div class="kgv-form-field">
|
|
<label for="kgv-owner">Verantwortlich</label>
|
|
<input type="text" id="kgv-owner" name="owner" value="<?php echo esc_attr($owner); ?>">
|
|
</div>
|
|
|
|
<div class="kgv-form-field">
|
|
<label for="kgv-location">Ort</label>
|
|
<input type="text" id="kgv-location" name="location" value="<?php echo esc_attr($location); ?>">
|
|
</div>
|
|
|
|
<div class="kgv-form-field">
|
|
<label for="kgv-published">Status</label>
|
|
<select id="kgv-published" name="is_published">
|
|
<option value="1" <?php selected($is_published, 1); ?>>Veröffentlicht</option>
|
|
<option value="0" <?php selected($is_published, 0); ?>>Entwurf</option>
|
|
</select>
|
|
</div>
|
|
|
|
<div class="kgv-form-field">
|
|
<label for="kgv-post-id">Verknüpfter Beitrag</label>
|
|
<select id="kgv-post-id" name="post_id">
|
|
<option value="0">— kein Beitrag —</option>
|
|
<?php foreach ($posts as $p) : ?>
|
|
<option value="<?php echo esc_attr($p->ID); ?>" <?php selected($post_id, $p->ID); ?>><?php echo esc_html($p->post_title); ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
|
|
<div class="kgv-form-field kgv-form-field-full">
|
|
<label for="kgv-summary">Kurzbeschreibung</label>
|
|
<textarea id="kgv-summary" name="summary" rows="3"><?php echo esc_textarea($summary); ?></textarea>
|
|
</div>
|
|
|
|
<div class="kgv-form-field kgv-form-field-full">
|
|
<label for="kgv-description">Detailbeschreibung</label>
|
|
<textarea id="kgv-description" name="description" rows="8"><?php echo esc_textarea($description); ?></textarea>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="kgv-admin-actions">
|
|
<button type="submit" class="button button-primary">Termin speichern</button>
|
|
<a href="<?php echo esc_url(admin_url('admin.php?page=kgv-termine')); ?>" class="button">Abbrechen</a>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
<?php
|
|
}
|
|
|
|
private function render_notice($message) {
|
|
$map = array(
|
|
'created' => array('success', 'Termin wurde angelegt.'),
|
|
'updated' => array('success', 'Termin wurde aktualisiert.'),
|
|
'deleted' => array('success', 'Termin wurde gelöscht.'),
|
|
'delete_failed' => array('error', 'Termin konnte nicht gelöscht werden.'),
|
|
'missing' => array('error', 'Titel und Datum sind Pflichtfelder.'),
|
|
);
|
|
|
|
if (!isset($map[$message])) {
|
|
return;
|
|
}
|
|
|
|
list($type, $text) = $map[$message];
|
|
printf('<div class="notice notice-%1$s is-dismissible"><p>%2$s</p></div>', esc_attr($type), esc_html($text));
|
|
}
|
|
|
|
private function get_frontend_rows($limit = 0) {
|
|
global $wpdb;
|
|
|
|
if ($limit > 0) {
|
|
return $wpdb->get_results($wpdb->prepare(
|
|
"SELECT * FROM {$this->table_name} WHERE is_published = 1 ORDER BY event_date ASC, id ASC LIMIT %d",
|
|
$limit
|
|
));
|
|
}
|
|
|
|
return $wpdb->get_results("SELECT * FROM {$this->table_name} WHERE is_published = 1 ORDER BY event_date ASC, id ASC");
|
|
}
|
|
|
|
private function get_row($id) {
|
|
global $wpdb;
|
|
return $wpdb->get_row($wpdb->prepare("SELECT * FROM {$this->table_name} WHERE id = %d", $id));
|
|
}
|
|
|
|
public function render_termine_shortcode($atts) {
|
|
$atts = shortcode_atts(array(
|
|
'limit' => 0,
|
|
'detail' => 1,
|
|
'base_url' => '',
|
|
), $atts, 'kgv_termine');
|
|
|
|
$detail_id = absint(get_query_var('kgv_termin'));
|
|
if (!$detail_id && isset($_GET['kgv_termin'])) {
|
|
$detail_id = absint(wp_unslash($_GET['kgv_termin']));
|
|
}
|
|
|
|
$base_url = $this->resolve_termine_base_url($atts['base_url']);
|
|
|
|
if ($detail_id && (int) $atts['detail'] === 1) {
|
|
return $this->render_detail_by_id($detail_id, $base_url);
|
|
}
|
|
|
|
$rows = $this->get_frontend_rows(absint($atts['limit']));
|
|
|
|
if (empty($rows)) {
|
|
return '<div class="kgv-termine-empty">Aktuell sind keine Termine verfügbar.</div>';
|
|
}
|
|
|
|
ob_start();
|
|
echo '<div class="kgv-termine-wrap">';
|
|
foreach ($rows as $row) {
|
|
$link = add_query_arg('kgv_termin', (int) $row->id, $base_url);
|
|
echo '<article class="kgv-termin-card">';
|
|
echo '<div class="kgv-termin-card-inner">';
|
|
echo '<div class="kgv-termin-datebox">' . esc_html(wp_date('d.m.Y H:i', strtotime($row->event_date))) . '</div>';
|
|
echo '<h3 class="kgv-termin-title">' . esc_html($row->title) . '</h3>';
|
|
echo '<div class="kgv-termin-meta">';
|
|
if (!empty($row->owner)) {
|
|
echo '<span>Verantwortlich: ' . esc_html($row->owner) . '</span>';
|
|
}
|
|
if (!empty($row->location)) {
|
|
echo '<span>Ort: ' . esc_html($row->location) . '</span>';
|
|
}
|
|
echo '</div>';
|
|
if (!empty($row->summary)) {
|
|
echo '<div class="kgv-termin-excerpt">' . esc_html($row->summary) . '</div>';
|
|
}
|
|
echo '<div class="kgv-termin-actions"><a class="kgv-termin-btn" href="' . esc_url($link) . '">Details ansehen</a>';
|
|
if (!empty($row->post_id)) {
|
|
$post_url = get_permalink((int) $row->post_id);
|
|
if ($post_url) {
|
|
echo '<a class="kgv-termin-btn kgv-termin-btn-secondary" href="' . esc_url($post_url) . '">Mehr Infos</a>';
|
|
}
|
|
}
|
|
echo '</div>';
|
|
echo '</div>';
|
|
echo '</article>';
|
|
}
|
|
echo '</div>';
|
|
return ob_get_clean();
|
|
}
|
|
|
|
public function render_detail_shortcode($atts) {
|
|
$atts = shortcode_atts(array(
|
|
'id' => 0,
|
|
), $atts, 'kgv_termin_detail');
|
|
|
|
$id = absint($atts['id']);
|
|
if (!$id) {
|
|
return '<div class="kgv-termine-notice">Keine Termin-ID übergeben.</div>';
|
|
}
|
|
|
|
return $this->render_detail_by_id($id, '');
|
|
}
|
|
|
|
private function render_detail_by_id($id, $back_url = '') {
|
|
$row = $this->get_row($id);
|
|
|
|
if (!$row || !(int) $row->is_published) {
|
|
return '<div class="kgv-termine-notice">Termin nicht gefunden.</div>';
|
|
}
|
|
|
|
ob_start();
|
|
echo '<article class="kgv-termin-single">';
|
|
echo '<div class="kgv-termin-single-hero">';
|
|
if ($back_url) {
|
|
echo '<a class="kgv-termin-back" href="' . esc_url($back_url) . '">← Zurück zur Übersicht</a>';
|
|
}
|
|
echo '<div class="kgv-termin-datebox">' . esc_html(wp_date('d.m.Y H:i', strtotime($row->event_date))) . '</div>';
|
|
echo '<h2>' . esc_html($row->title) . '</h2>';
|
|
echo '<div class="kgv-termin-meta">';
|
|
if (!empty($row->owner)) {
|
|
echo '<span>Verantwortlich: ' . esc_html($row->owner) . '</span>';
|
|
}
|
|
if (!empty($row->location)) {
|
|
echo '<span>Ort: ' . esc_html($row->location) . '</span>';
|
|
}
|
|
echo '</div>';
|
|
echo '</div>';
|
|
echo '<div class="kgv-termin-single-inner">';
|
|
if (!empty($row->summary)) {
|
|
echo '<p class="kgv-termin-summary"><strong>' . esc_html($row->summary) . '</strong></p>';
|
|
}
|
|
echo '<div class="kgv-termin-content">' . wpautop(wp_kses_post($row->description)) . '</div>';
|
|
if (!empty($row->post_id)) {
|
|
$post_url = get_permalink((int) $row->post_id);
|
|
if ($post_url) {
|
|
echo '<div class="kgv-termin-more-info"><a class="kgv-termin-btn" href="' . esc_url($post_url) . '">Mehr Infos</a></div>';
|
|
}
|
|
}
|
|
echo '</div>';
|
|
echo '</article>';
|
|
return ob_get_clean();
|
|
}
|
|
|
|
|
|
|
|
public function render_sidebar_shortcode($atts) {
|
|
$atts = shortcode_atts(array(
|
|
'limit' => 5,
|
|
'base_url' => '',
|
|
), $atts, 'kgv_termine_sidebar');
|
|
|
|
$rows = $this->get_frontend_rows(max(1, min(20, absint($atts['limit']))));
|
|
|
|
if (empty($rows)) {
|
|
return '<div class="kgv-sidebar-box">Keine Termine</div>';
|
|
}
|
|
|
|
$base_url = $this->resolve_termine_base_url($atts['base_url']);
|
|
|
|
ob_start();
|
|
echo '<div class="kgv-sidebar-box">';
|
|
echo '<h3 class="kgv-sidebar-title">Nächste Termine</h3>';
|
|
echo '<div class="kgv-sidebar-list">';
|
|
|
|
foreach ($rows as $row) {
|
|
$link = add_query_arg('kgv_termin', (int) $row->id, $base_url);
|
|
|
|
echo '<a class="kgv-sidebar-item" href="' . esc_url($link) . '">';
|
|
echo '<span class="kgv-sidebar-date">' . esc_html(wp_date('d.m.Y', strtotime($row->event_date))) . '</span>';
|
|
echo '<span class="kgv-sidebar-text">' . esc_html($row->title) . '</span>';
|
|
echo '</a>';
|
|
}
|
|
|
|
echo '</div>';
|
|
echo '</div>';
|
|
|
|
return ob_get_clean();
|
|
}
|
|
|
|
private function resolve_termine_base_url($base_url = '') {
|
|
if (!empty($base_url)) {
|
|
$clean_url = esc_url_raw((string) $base_url);
|
|
if ($clean_url) {
|
|
return remove_query_arg(array_keys($_GET), $clean_url);
|
|
}
|
|
}
|
|
|
|
$shortcode_page_url = $this->find_termine_shortcode_page_url();
|
|
if ($shortcode_page_url) {
|
|
return $shortcode_page_url;
|
|
}
|
|
|
|
return $this->get_current_page_url(false);
|
|
}
|
|
|
|
private function find_termine_shortcode_page_url() {
|
|
static $cached_url = null;
|
|
|
|
if (null !== $cached_url) {
|
|
return $cached_url;
|
|
}
|
|
|
|
global $wpdb;
|
|
|
|
$like = '%' . $wpdb->esc_like('[kgv_termine') . '%';
|
|
$post_id = (int) $wpdb->get_var(
|
|
$wpdb->prepare(
|
|
"SELECT ID
|
|
FROM {$wpdb->posts}
|
|
WHERE post_status = 'publish'
|
|
AND post_type IN ('page', 'post')
|
|
AND post_content LIKE %s
|
|
ORDER BY CASE WHEN post_type = 'page' THEN 0 ELSE 1 END, ID ASC
|
|
LIMIT 1",
|
|
$like
|
|
)
|
|
);
|
|
|
|
if ($post_id) {
|
|
$permalink = get_permalink($post_id);
|
|
if ($permalink) {
|
|
$cached_url = remove_query_arg(array_keys($_GET), $permalink);
|
|
return $cached_url;
|
|
}
|
|
}
|
|
|
|
$cached_url = '';
|
|
return $cached_url;
|
|
}
|
|
|
|
private function get_current_page_url($with_query = true) {
|
|
$page_url = '';
|
|
|
|
if (is_singular() && get_queried_object_id()) {
|
|
$page_url = get_permalink(get_queried_object_id());
|
|
}
|
|
|
|
if (!$page_url) {
|
|
global $wp;
|
|
$page_url = home_url(add_query_arg(array(), $wp->request ? '/' . ltrim($wp->request, '/') . '/' : '/'));
|
|
}
|
|
|
|
if (!$with_query) {
|
|
return remove_query_arg(array_keys($_GET), $page_url);
|
|
}
|
|
|
|
return $page_url;
|
|
}
|
|
}
|