124 lines
3.7 KiB
PHP
124 lines
3.7 KiB
PHP
<?php
|
|
/**
|
|
* Plugin Name: Skrift Ratgeber Felder
|
|
* Description: Registriert die Ratgeber-Meta-Felder REST-schreibbar (kurzantwort, faq, cta_ziel, stand_datum) und liefert einen Shortcode fuer die FAQ-Ausgabe.
|
|
* Version: 1.0.0
|
|
* Author: Lucas Orth
|
|
*
|
|
* Ablage: wp-content/mu-plugins/skrift-ratgeber-felder.php
|
|
* Als mu-plugin ist keine Aktivierung noetig und es kann nicht versehentlich
|
|
* deaktiviert werden. Alternativ als normales Plugin in einem eigenen Ordner.
|
|
*/
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Nur Nutzer, die den Beitrag bearbeiten duerfen, duerfen die Felder schreiben.
|
|
* Das gilt auch fuer REST-Zugriffe mit Application Password.
|
|
*/
|
|
function skrift_ratgeber_meta_auth( $allowed, $meta_key, $post_id ) {
|
|
return current_user_can( 'edit_post', $post_id );
|
|
}
|
|
|
|
add_action( 'init', function () {
|
|
|
|
// Kurzantwort: ein Absatz, der die Suchfrage sofort beantwortet.
|
|
register_post_meta( 'post', 'kurzantwort', array(
|
|
'type' => 'string',
|
|
'single' => true,
|
|
'show_in_rest' => true,
|
|
'sanitize_callback' => 'wp_kses_post',
|
|
'auth_callback' => 'skrift_ratgeber_meta_auth',
|
|
'description' => 'Kurzantwort auf die Hauptfrage des Artikels',
|
|
) );
|
|
|
|
// Ziel des Call-to-Action, als URL.
|
|
register_post_meta( 'post', 'cta_ziel', array(
|
|
'type' => 'string',
|
|
'single' => true,
|
|
'show_in_rest' => true,
|
|
'sanitize_callback' => 'esc_url_raw',
|
|
'auth_callback' => 'skrift_ratgeber_meta_auth',
|
|
'description' => 'Zieladresse des Call-to-Action',
|
|
) );
|
|
|
|
// Stand des Artikels als ISO-Datum (YYYY-MM-DD).
|
|
register_post_meta( 'post', 'stand_datum', array(
|
|
'type' => 'string',
|
|
'single' => true,
|
|
'show_in_rest' => true,
|
|
'sanitize_callback' => function ( $wert ) {
|
|
return preg_match( '/^\d{4}-\d{2}-\d{2}$/', (string) $wert ) ? $wert : '';
|
|
},
|
|
'auth_callback' => 'skrift_ratgeber_meta_auth',
|
|
'description' => 'Stand des Artikels, Format YYYY-MM-DD',
|
|
) );
|
|
|
|
/**
|
|
* FAQ als wiederholbare Gruppe.
|
|
* WordPress braucht fuer verschachtelte Werte ein explizites Schema,
|
|
* sonst lehnt die REST-API das Schreiben ab.
|
|
*/
|
|
register_post_meta( 'post', 'faq', array(
|
|
'type' => 'array',
|
|
'single' => true,
|
|
'default' => array(),
|
|
'show_in_rest' => array(
|
|
'schema' => array(
|
|
'type' => 'array',
|
|
'items' => array(
|
|
'type' => 'object',
|
|
'properties' => array(
|
|
'frage' => array( 'type' => 'string' ),
|
|
'antwort' => array( 'type' => 'string' ),
|
|
),
|
|
'additionalProperties' => false,
|
|
),
|
|
),
|
|
),
|
|
'auth_callback' => 'skrift_ratgeber_meta_auth',
|
|
'description' => 'Liste aus Frage-Antwort-Paaren',
|
|
) );
|
|
} );
|
|
|
|
/**
|
|
* Ausgabe der FAQ im Beitrag.
|
|
* Verwendung im Elementor-Shortcode-Widget: [skrift_faq]
|
|
* Optional mit Ueberschrift: [skrift_faq titel="Haeufige Fragen"]
|
|
*/
|
|
add_shortcode( 'skrift_faq', function ( $atts ) {
|
|
$atts = shortcode_atts( array(
|
|
'titel' => '',
|
|
'post_id' => 0,
|
|
), $atts, 'skrift_faq' );
|
|
|
|
$post_id = $atts['post_id'] ? (int) $atts['post_id'] : get_the_ID();
|
|
$faq = get_post_meta( $post_id, 'faq', true );
|
|
|
|
if ( empty( $faq ) || ! is_array( $faq ) ) {
|
|
return '';
|
|
}
|
|
|
|
$html = '<div class="skrift-faq">';
|
|
|
|
if ( $atts['titel'] ) {
|
|
$html .= '<h2 class="skrift-faq__titel">' . esc_html( $atts['titel'] ) . '</h2>';
|
|
}
|
|
|
|
foreach ( $faq as $eintrag ) {
|
|
if ( empty( $eintrag['frage'] ) ) {
|
|
continue;
|
|
}
|
|
$html .= '<details class="skrift-faq__eintrag">';
|
|
$html .= '<summary class="skrift-faq__frage">' . esc_html( $eintrag['frage'] ) . '</summary>';
|
|
$html .= '<div class="skrift-faq__antwort">' . wp_kses_post( wpautop( $eintrag['antwort'] ?? '' ) ) . '</div>';
|
|
$html .= '</details>';
|
|
}
|
|
|
|
$html .= '</div>';
|
|
|
|
return $html;
|
|
} );
|