diff --git a/includes/class-kgv-termine-plugin.php b/includes/class-kgv-termine-plugin.php index ce8a83f..0e3cdc8 100755 --- a/includes/class-kgv-termine-plugin.php +++ b/includes/class-kgv-termine-plugin.php @@ -25,6 +25,7 @@ class KGV_Termine_Plugin { 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')); @@ -51,6 +52,7 @@ class KGV_Termine_Plugin { 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) @@ -59,6 +61,40 @@ class KGV_Termine_Plugin { 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; @@ -161,6 +197,8 @@ class KGV_Termine_Plugin { $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'); @@ -179,10 +217,11 @@ class KGV_Termine_Plugin { 'summary' => $summary, 'description' => $description, 'is_published' => $is_published, + 'post_id' => $post_id, 'updated_at' => $now, ); - $formats = array('%s', '%s', '%s', '%s', '%s', '%s', '%d', '%s'); + $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')); @@ -191,7 +230,7 @@ class KGV_Termine_Plugin { } $data['created_at'] = $now; - $wpdb->insert($this->table_name, $data, array('%s', '%s', '%s', '%s', '%s', '%s', '%d', '%s', '%s')); + $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; @@ -269,6 +308,8 @@ class KGV_Termine_Plugin { $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')); ?>
' . esc_html($row->summary) . '
'; } echo '