Files
WP-Multi-Wiki/includes/class-wmw-metaboxes.php
2026-03-18 21:56:45 +01:00

166 lines
7.1 KiB
PHP

<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }
class WMW_Metaboxes {
public static function init() {
add_action( 'add_meta_boxes', [ __CLASS__, 'register' ] );
add_action( 'save_post_wmw_article', [ __CLASS__, 'save_article' ], 10, 2 );
add_action( 'save_post_wmw_wiki', [ __CLASS__, 'save_wiki' ], 10, 2 );
}
public static function register() {
add_meta_box(
'wmw_article_settings',
'Wiki-Einstellungen',
[ __CLASS__, 'article_settings_html' ],
'wmw_article',
'side',
'high'
);
add_meta_box(
'wmw_wiki_settings',
'Wiki-Einstellungen',
[ __CLASS__, 'wiki_settings_html' ],
'wmw_wiki',
'normal',
'high'
);
}
// ── Artikel Meta-Box ────────────────────────────────────────────────────
public static function article_settings_html( $post ) {
wp_nonce_field( 'wmw_save_article', 'wmw_article_nonce' );
$wiki_id = get_post_meta( $post->ID, '_wmw_wiki_id', true );
$order = get_post_meta( $post->ID, '_wmw_order', true ) ?: 0;
$hide_toc = get_post_meta( $post->ID, '_wmw_hide_toc', true );
// Alle Wikis laden
$wikis = get_posts( [
'post_type' => 'wmw_wiki',
'numberposts' => -1,
'orderby' => 'title',
'order' => 'ASC',
] );
?>
<p>
<label for="wmw_wiki_id"><strong>Zugehoeriges Wiki</strong></label><br>
<select name="wmw_wiki_id" id="wmw_wiki_id" style="width:100%;">
<option value="">-- Wiki waehlen --</option>
<?php foreach ( $wikis as $wiki ) : ?>
<option value="<?php echo esc_attr( $wiki->ID ); ?>"
<?php selected( $wiki_id, $wiki->ID ); ?>>
<?php echo esc_html( $wiki->post_title ); ?>
</option>
<?php endforeach; ?>
</select>
</p>
<p>
<label for="wmw_order"><strong>Reihenfolge</strong></label><br>
<input type="number" name="wmw_order" id="wmw_order"
value="<?php echo esc_attr( $order ); ?>"
style="width:100%;" min="0" step="1">
<em style="font-size:11px;">Niedrigere Zahl = weiter oben</em>
</p>
<p>
<label>
<input type="checkbox" name="wmw_hide_toc" value="1" <?php checked( $hide_toc, '1' ); ?>>
Inhaltsverzeichnis ausblenden
</label>
</p>
<?php
}
// ── Wiki Meta-Box ───────────────────────────────────────────────────────
public static function wiki_settings_html( $post ) {
wp_nonce_field( 'wmw_save_wiki', 'wmw_wiki_nonce' );
$icon_url = get_post_meta( $post->ID, '_wmw_icon_url', true );
$icon_class = get_post_meta( $post->ID, '_wmw_icon_class', true ) ?: 'dashicons-book-alt';
$color = get_post_meta( $post->ID, '_wmw_color', true ) ?: '#0073aa';
$version = get_post_meta( $post->ID, '_wmw_version', true );
?>
<table class="form-table" style="margin:0;">
<tr>
<th><label for="wmw_color">Farbe</label></th>
<td>
<input type="color" name="wmw_color" id="wmw_color"
value="<?php echo esc_attr( $color ); ?>">
</td>
</tr>
<tr>
<th><label for="wmw_icon_class">Dashicon</label></th>
<td>
<input type="text" name="wmw_icon_class" id="wmw_icon_class"
value="<?php echo esc_attr( $icon_class ); ?>"
placeholder="dashicons-book-alt" style="width:100%;">
<em style="font-size:11px;">
<a href="https://developer.wordpress.org/resource/dashicons/" target="_blank">Dashicons anzeigen</a>
</em>
</td>
</tr>
<tr>
<th><label for="wmw_version">Version</label></th>
<td>
<input type="text" name="wmw_version" id="wmw_version"
value="<?php echo esc_attr( $version ); ?>"
placeholder="z.B. 2.3.1" style="width:100%;">
</td>
</tr>
<tr>
<th><label for="wmw_icon_url">Icon URL</label></th>
<td>
<input type="url" name="wmw_icon_url" id="wmw_icon_url"
value="<?php echo esc_attr( $icon_url ); ?>"
placeholder="https://..." style="width:100%;">
<em style="font-size:11px;">Optionales eigenes Icon (ueberschreibt Dashicon)</em>
</td>
</tr>
</table>
<?php
}
// ── Speichern: Artikel ──────────────────────────────────────────────────
public static function save_article( $post_id, $post ) {
if ( ! isset( $_POST['wmw_article_nonce'] ) ) return;
if ( ! wp_verify_nonce( $_POST['wmw_article_nonce'], 'wmw_save_article' ) ) return;
if ( wp_is_post_autosave( $post_id ) || wp_is_post_revision( $post_id ) ) return;
if ( ! current_user_can( 'manage_options' ) ) return;
if ( isset( $_POST['wmw_wiki_id'] ) ) {
$wiki_id = intval( $_POST['wmw_wiki_id'] );
if ( $wiki_id > 0 ) {
update_post_meta( $post_id, '_wmw_wiki_id', $wiki_id );
} else {
delete_post_meta( $post_id, '_wmw_wiki_id' );
}
}
if ( isset( $_POST['wmw_order'] ) ) {
update_post_meta( $post_id, '_wmw_order', intval( $_POST['wmw_order'] ) );
}
if ( isset( $_POST['wmw_hide_toc'] ) ) {
update_post_meta( $post_id, '_wmw_hide_toc', '1' );
} else {
delete_post_meta( $post_id, '_wmw_hide_toc' );
}
}
// ── Speichern: Wiki ─────────────────────────────────────────────────────
public static function save_wiki( $post_id, $post ) {
if ( ! isset( $_POST['wmw_wiki_nonce'] ) ) return;
if ( ! wp_verify_nonce( $_POST['wmw_wiki_nonce'], 'wmw_save_wiki' ) ) return;
if ( wp_is_post_autosave( $post_id ) || wp_is_post_revision( $post_id ) ) return;
if ( ! current_user_can( 'manage_options' ) ) return;
$fields = [ 'wmw_icon_url', 'wmw_icon_class', 'wmw_color', 'wmw_version' ];
foreach ( $fields as $field ) {
if ( isset( $_POST[ $field ] ) ) {
update_post_meta( $post_id, "_$field", sanitize_text_field( $_POST[ $field ] ) );
}
}
}
}