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'])) : ''; ?>
| Titel | Datum | Verantwortlich | Ort | Status | Aktionen |
|---|---|---|---|---|---|
| Noch keine Termine angelegt. | |||||
| title); ?> | event_date))); ?> | owner ?: '—'); ?> | location ?: '—'); ?> | is_published === 1 ? 'Veröffentlicht' : 'Entwurf'; ?> | Bearbeiten | Löschen |
%2$s
' . esc_html($row->summary) . '
'; } echo '