__( 'Türchen', 'adventura-interactive-advent-calendar' ),
'singular_name' => __( 'Türchen', 'adventura-interactive-advent-calendar' ),
'menu_name' => __( 'Adventura', 'adventura-interactive-advent-calendar' ),
'add_new' => __( 'Türchen hinzufügen', 'adventura-interactive-advent-calendar' ),
'add_new_item' => __( 'Neues Türchen', 'adventura-interactive-advent-calendar' ),
'edit_item' => __( 'Türchen bearbeiten', 'adventura-interactive-advent-calendar' ),
'new_item' => __( 'Neues Türchen', 'adventura-interactive-advent-calendar' ),
'view_item' => __( 'Türchen ansehen', 'adventura-interactive-advent-calendar' ),
'search_items' => __( 'Türchen suchen', 'adventura-interactive-advent-calendar' ),
'not_found' => __( 'Keine Türchen gefunden', 'adventura-interactive-advent-calendar' ),
'all_items' => __( 'Alle Türchen', 'adventura-interactive-advent-calendar' ),
);
register_post_type(
self::POST_TYPE,
array(
'labels' => $labels,
'public' => false,
'show_ui' => true,
'show_in_menu' => 'advk-dashboard',
'supports' => array( 'title' ),
'has_archive' => false,
'rewrite' => false,
)
);
}
public function admin_assets( $hook ) {
global $post_type;
if ( self::POST_TYPE === $post_type && in_array( $hook, array( 'post.php', 'post-new.php' ), true ) ) {
wp_enqueue_media();
wp_enqueue_style(
'advk-admin-door',
ADVK_URL . 'assets/css/admin-door.css',
array(),
ADVK_VERSION
);
wp_enqueue_script(
'advk-admin-door',
ADVK_URL . 'assets/js/admin-door.js',
array( 'jquery' ),
ADVK_VERSION,
true
);
}
}
public function add_meta_boxes() {
add_meta_box(
'advk_door_content',
__( 'Türchen-Inhalt', 'adventura-interactive-advent-calendar' ),
array( $this, 'render_meta_box' ),
self::POST_TYPE,
'normal',
'high'
);
}
public function render_meta_box( $post ) {
wp_nonce_field( 'advk_save_door', 'advk_door_nonce' );
// Global display settings decide which editor sections are relevant.
$gopts = ADVK_Settings::get_options();
$show_sponsor = ! empty( $gopts['show_sponsor'] );
$show_prizes = ! empty( $gopts['show_prizes'] );
$social_enabled = ! empty( $gopts['social_enabled'] );
$hide_sponsor = $show_sponsor ? '' : ' style="display:none"';
$hide_prizes = $show_prizes ? '' : ' style="display:none"';
$hide_social = ( $show_sponsor && $social_enabled ) ? '' : ' style="display:none"';
$day = (int) get_post_meta( $post->ID, '_advk_day', true );
$sponsor_text = (string) get_post_meta( $post->ID, '_advk_sponsor_text', true );
$prize = (string) get_post_meta( $post->ID, '_advk_prize', true );
$prize_text = (string) get_post_meta( $post->ID, '_advk_prize_text', true );
$value = (string) get_post_meta( $post->ID, '_advk_value', true );
$social = get_post_meta( $post->ID, '_advk_social', true );
$social = is_array( $social ) ? $social : array();
$social_urls = get_post_meta( $post->ID, '_advk_social_urls', true );
$social_urls = is_array( $social_urls ) ? $social_urls : array();
$quiz = get_post_meta( $post->ID, '_advk_quiz', true );
$quiz = is_array( $quiz ) ? $quiz : array();
$logo_id = (int) get_post_meta( $post->ID, '_advk_logo_id', true );
$prize_id = (int) get_post_meta( $post->ID, '_advk_prize_image_id', true );
$door_img_id = (int) get_post_meta( $post->ID, '_advk_door_image_id', true );
// Prize rows (multi-prize). Fall back to the legacy single prize so doors
// created before multi-prize support show their prize as the first row.
$prizes_raw = get_post_meta( $post->ID, '_advk_prizes', true );
if ( ! is_array( $prizes_raw ) || empty( $prizes_raw ) ) {
$prizes_raw = array(
array( 'title' => $prize, 'text' => $prize_text, 'value' => $value, 'image_id' => $prize_id ),
);
}
$prizes_raw = array_values( $prizes_raw );
// Total number of prize items (sum of per-prize "Anzahl") — for the hint.
$prize_total = 0;
foreach ( $prizes_raw as $r ) {
$prize_total += ( is_array( $r ) && isset( $r['qty'] ) ) ? max( 1, (int) $r['qty'] ) : 1;
}
// Number of winners is independent of the prize quantities: a sponsor can
// give 4 prizes to ONE winner (a bundle) or to four winners. Default to one
// winner per prize item when nothing was set yet.
$winner_count = (int) get_post_meta( $post->ID, '_advk_winner_count', true );
if ( $winner_count < 1 ) {
$winner_count = max( 1, $prize_total );
}
$donate_url = (string) get_post_meta( $post->ID, '_advk_donate_url', true );
$donate_text = (string) get_post_meta( $post->ID, '_advk_donate_text', true );
// Ensure we always render exactly 2 question rows.
for ( $i = 0; $i < 2; $i++ ) {
if ( ! isset( $quiz[ $i ] ) || ! is_array( $quiz[ $i ] ) ) {
$quiz[ $i ] = array( 'q' => '', 'options' => array( '', '', '', '' ), 'correct' => 0 );
}
$quiz[ $i ]['options'] = array_pad( (array) $quiz[ $i ]['options'], 4, '' );
}
$social_labels = array(
'instagram' => 'Instagram',
'facebook' => 'Facebook',
'tiktok' => 'TikTok',
'youtube' => 'YouTube',
'linkedin' => 'LinkedIn',
'newsletter' => 'Newsletter',
);
?>
>
'https://instagram.com/dein-profil',
'facebook' => 'https://facebook.com/deine-seite',
'tiktok' => 'https://tiktok.com/@dein-profil',
'youtube' => 'https://youtube.com/@dein-kanal',
'linkedin' => 'https://linkedin.com/company/dein-unternehmen',
'newsletter' => 'https://… (Anmeldeseite)',
);
?>
$q ) : ?>
= 1 && $day <= ADVK_Data::door_count() ) {
update_post_meta( $post_id, '_advk_day', $day );
}
update_post_meta( $post_id, '_advk_sponsor_text', wp_kses_post( wp_unslash( $_POST['advk_sponsor_text'] ?? '' ) ) );
update_post_meta( $post_id, '_advk_sponsor_url', esc_url_raw( wp_unslash( $_POST['advk_sponsor_url'] ?? '' ) ) );
// Prizes (multi-prize). Sanitize every field; drop fully-empty rows.
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Each field is sanitized in the loop below.
$prizes_in = isset( $_POST['advk_prizes'] ) ? (array) wp_unslash( $_POST['advk_prizes'] ) : array();
$prizes_out = array();
foreach ( $prizes_in as $row ) {
if ( ! is_array( $row ) ) {
continue;
}
$title = sanitize_text_field( $row['title'] ?? '' );
$text = wp_kses_post( $row['text'] ?? '' );
$value = sanitize_text_field( $row['value'] ?? '' );
$imgid = isset( $row['image_id'] ) ? (int) $row['image_id'] : 0;
$qty = isset( $row['qty'] ) ? min( 1000, max( 1, (int) $row['qty'] ) ) : 1;
if ( '' === $title && '' === $text && '' === $value && ! $imgid ) {
continue;
}
$prizes_out[] = array( 'title' => $title, 'text' => $text, 'value' => $value, 'qty' => $qty, 'image_id' => $imgid );
}
update_post_meta( $post_id, '_advk_prizes', $prizes_out );
// Number of winners (independent of prize quantities). Default to one
// winner per prize item (sum of quantities) when left empty.
$prize_total = 0;
foreach ( $prizes_out as $pr ) {
$prize_total += isset( $pr['qty'] ) ? max( 1, (int) $pr['qty'] ) : 1;
}
$winner_count = isset( $_POST['advk_winner_count'] ) ? (int) $_POST['advk_winner_count'] : 0;
if ( $winner_count < 1 ) {
$winner_count = max( 1, $prize_total );
}
update_post_meta( $post_id, '_advk_winner_count', min( 100000, $winner_count ) );
// Mirror the first prize into the legacy single-prize meta for compat.
$first = isset( $prizes_out[0] ) ? $prizes_out[0] : array( 'title' => '', 'text' => '', 'value' => '', 'image_id' => 0 );
update_post_meta( $post_id, '_advk_prize', $first['title'] );
update_post_meta( $post_id, '_advk_prize_text', $first['text'] );
update_post_meta( $post_id, '_advk_value', $first['value'] );
update_post_meta( $post_id, '_advk_prize_image_id', (int) $first['image_id'] );
update_post_meta( $post_id, '_advk_logo_id', isset( $_POST['advk_logo_id'] ) ? (int) $_POST['advk_logo_id'] : 0 );
update_post_meta( $post_id, '_advk_door_image_id', isset( $_POST['advk_door_image_id'] ) ? (int) $_POST['advk_door_image_id'] : 0 );
update_post_meta( $post_id, '_advk_donate_url', esc_url_raw( wp_unslash( $_POST['advk_donate_url'] ?? '' ) ) );
update_post_meta( $post_id, '_advk_donate_text', wp_kses_post( wp_unslash( $_POST['advk_donate_text'] ?? '' ) ) );
// Social channels.
$allowed = ADVK_Data::social_keys();
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Each element is run through sanitize_key() on the next line.
$social = isset( $_POST['advk_social'] ) ? (array) wp_unslash( $_POST['advk_social'] ) : array();
$social = array_values( array_intersect( array_map( 'sanitize_key', $social ), $allowed ) );
update_post_meta( $post_id, '_advk_social', $social );
// Social channel URLs (one per allowed key; only valid URLs are kept).
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Each value is run through esc_url_raw() in the loop below.
$social_url_in = isset( $_POST['advk_social_url'] ) ? (array) wp_unslash( $_POST['advk_social_url'] ) : array();
$social_urls = array();
foreach ( $allowed as $skey ) {
$raw = isset( $social_url_in[ $skey ] ) ? trim( (string) $social_url_in[ $skey ] ) : '';
if ( '' !== $raw ) {
$social_urls[ $skey ] = esc_url_raw( $raw );
}
}
update_post_meta( $post_id, '_advk_social_urls', $social_urls );
// Quiz.
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Every field is run through sanitize_text_field() in the loop below.
$quiz_in = isset( $_POST['advk_quiz'] ) ? (array) wp_unslash( $_POST['advk_quiz'] ) : array();
$quiz_out = array();
foreach ( $quiz_in as $q ) {
$text = sanitize_text_field( $q['q'] ?? '' );
$opts = array();
foreach ( (array) ( $q['options'] ?? array() ) as $opt ) {
$opt = sanitize_text_field( $opt );
if ( '' !== $opt ) {
$opts[] = $opt;
}
}
if ( '' === $text || count( $opts ) < 2 ) {
continue;
}
$correct = isset( $q['correct'] ) ? (int) $q['correct'] : 0;
if ( $correct < 0 || $correct >= count( $opts ) ) {
$correct = 0;
}
$quiz_out[] = array(
'q' => $text,
'options' => $opts,
'correct' => $correct,
);
}
update_post_meta( $post_id, '_advk_quiz', $quiz_out );
/**
* Extension point: add-ons can persist their own door meta here. Fires
* after the door nonce/capability checks and the core meta have been saved.
*
* @param int $post_id The door post id.
*/
do_action( 'advk_door_save', $post_id );
}
public function columns( $columns ) {
$new = array();
foreach ( $columns as $key => $label ) {
if ( 'title' === $key ) {
$new['advk_day'] = __( 'Tag', 'adventura-interactive-advent-calendar' );
}
$new[ $key ] = $label;
}
return $new;
}
public function column_content( $column, $post_id ) {
if ( 'advk_day' === $column ) {
$day = (int) get_post_meta( $post_id, '_advk_day', true );
echo $day ? esc_html( $day ) : '—';
}
}
public function sortable_columns( $columns ) {
$columns['advk_day'] = 'advk_day';
return $columns;
}
public function order_by_day( $query ) {
if ( ! is_admin() || ! $query->is_main_query() ) {
return;
}
if ( $query->get( 'post_type' ) !== self::POST_TYPE ) {
return;
}
$orderby = $query->get( 'orderby' );
if ( '' === $orderby || 'advk_day' === $orderby ) {
$query->set( 'meta_key', '_advk_day' );
$query->set( 'orderby', 'meta_value_num' );
if ( '' === $query->get( 'order' ) ) {
$query->set( 'order', 'ASC' );
}
}
}
}