via wp_head. Werkt op ELK thema/builder zonder themawijziging,
* zolang het thema wp_head() aanroept. Opslag: post-meta `_aigeschikt_schema`
* (genormaliseerde JSON-string).
*
* @package AIGeschikt
*/
defined( 'ABSPATH' ) || exit;
class AIGeschikt_Schema {
const META_KEY = '_aigeschikt_schema';
const MODE_META = '_aigeschikt_schema_mode';
const MODE_OPTION = 'aigeschikt_schema_mode';
/**
* Hooks.
*/
public static function init() {
add_action( 'wp', array( __CLASS__, 'maybe_suppress_seo' ) );
add_action( 'wp_head', array( __CLASS__, 'output' ), 99 );
add_action( 'rest_api_init', array( __CLASS__, 'register_routes' ) );
}
/**
* Effectieve modus voor een pagina: per-pagina override, anders de site-default.
*
* @param int $post_id Post.
* @return string 'add' | 'replace'
*/
public static function effective_mode( $post_id ) {
$per = get_post_meta( (int) $post_id, self::MODE_META, true );
if ( 'add' === $per || 'replace' === $per ) {
return $per;
}
return 'replace' === get_option( self::MODE_OPTION, 'add' ) ? 'replace' : 'add';
}
/**
* Onderdruk de schema van de SEO-plugin op pagina's waar AIGeschikt schema
* levert én de modus 'replace' is. Pagina's zonder AIGeschikt-schema houden
* gewoon de RankMath/Yoast-baseline.
*/
public static function maybe_suppress_seo() {
if ( is_admin() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) || ! is_singular() ) {
return;
}
$id = get_queried_object_id();
if ( ! $id || '' === self::get( $id ) || 'replace' !== self::effective_mode( $id ) ) {
return;
}
add_filter( 'rank_math/json_ld', '__return_empty_array', 99 ); // RankMath.
add_filter( 'wpseo_json_ld_output', '__return_false', 99 ); // Yoast.
}
/**
* Opgeslagen JSON-LD-string voor een post.
*
* @param int $post_id Post.
* @return string Lege string als er niets is.
*/
public static function get( $post_id ) {
$raw = get_post_meta( (int) $post_id, self::META_KEY, true );
return is_string( $raw ) ? $raw : '';
}
/* ------------------------------------------------------------------ */
/* Output */
/* ------------------------------------------------------------------ */
/**
* Print het JSON-LD-blok in de
voor singular views.
*/
public static function output() {
if ( ! is_singular() ) {
return;
}
$id = get_queried_object_id();
if ( ! $id ) {
return;
}
$json = self::get( $id );
if ( '' === $json ) {
return;
}
$data = json_decode( $json, true );
if ( null === $data ) {
return;
}
// JSON_HEX_TAG zet < en > om naar < / > → neutraliseert .
$safe = wp_json_encode( $data, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
if ( false === $safe ) {
return;
}
echo "\n\n"; // phpcs:ignore WordPress.Security.EscapeOutput
}
/* ------------------------------------------------------------------ */
/* REST: /schema/{id} */
/* ------------------------------------------------------------------ */
public static function register_routes() {
register_rest_route(
AIGESCHIKT_REST_NS,
'/schema/(?P\d+)',
array(
// Per OBJECT (0.8.8): het schema van een post zet wie die post mag bewerken.
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( __CLASS__, 'rest_get' ),
'permission_callback' => 'aigeschikt_can_edit_post',
),
array(
'methods' => WP_REST_Server::EDITABLE,
'callback' => array( __CLASS__, 'rest_put' ),
'permission_callback' => 'aigeschikt_can_edit_post',
),
array(
'methods' => WP_REST_Server::DELETABLE,
'callback' => array( __CLASS__, 'rest_delete' ),
'permission_callback' => 'aigeschikt_can_edit_post',
),
)
);
}
public static function rest_get( WP_REST_Request $request ) {
$id = (int) $request['id'];
if ( ! get_post( $id ) ) {
return new WP_Error( 'aigeschikt_not_found', 'Onbekende post.', array( 'status' => 404 ) );
}
$json = self::get( $id );
return rest_ensure_response(
array(
'post_id' => $id,
'schema' => '' === $json ? null : json_decode( $json, true ),
'mode' => self::effective_mode( $id ),
)
);
}
public static function rest_put( WP_REST_Request $request ) {
$id = (int) $request['id'];
if ( ! get_post( $id ) ) {
return new WP_Error( 'aigeschikt_not_found', 'Onbekende post.', array( 'status' => 404 ) );
}
$body = $request->get_json_params();
if ( ! isset( $body['schema'] ) ) {
return new WP_Error( 'aigeschikt_missing', 'Veld "schema" ontbreekt.', array( 'status' => 400 ) );
}
$schema = $body['schema'];
// Accepteer zowel een JSON-string als een al-geparset object/array.
if ( is_string( $schema ) ) {
$decoded = json_decode( $schema, true );
if ( null === $decoded && 'null' !== strtolower( trim( $schema ) ) ) {
return new WP_Error( 'aigeschikt_invalid_json', 'Ongeldige JSON.', array( 'status' => 400 ) );
}
$schema = $decoded;
}
$json = wp_json_encode( $schema, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
if ( false === $json ) {
return new WP_Error( 'aigeschikt_encode', 'Kon schema niet coderen.', array( 'status' => 400 ) );
}
// wp_slash: update_metadata() haalt backslashes weg, en dan wordt \" in een
// tekstwaarde een kale " en is het blok geen JSON meer (1.0.0).
update_post_meta( $id, self::META_KEY, wp_slash( $json ) );
// Optionele per-pagina modus. 'add'/'replace' zet de override; iets anders
// (of weglaten) → val terug op de site-default.
if ( isset( $body['mode'] ) ) {
$mode = sanitize_key( $body['mode'] );
if ( 'add' === $mode || 'replace' === $mode ) {
update_post_meta( $id, self::MODE_META, $mode );
} else {
delete_post_meta( $id, self::MODE_META );
}
}
// Full-page-cache purgen, anders serveert Kinsta/LiteSpeed/etc. de oude HTML.
AIGeschikt_Cache::purge_post( $id );
return rest_ensure_response( array( 'post_id' => $id, 'schema' => $schema, 'mode' => self::effective_mode( $id ) ) );
}
public static function rest_delete( WP_REST_Request $request ) {
$id = (int) $request['id'];
delete_post_meta( $id, self::META_KEY );
delete_post_meta( $id, self::MODE_META );
AIGeschikt_Cache::purge_post( $id );
return rest_ensure_response( array( 'post_id' => $id, 'schema' => null ) );
}
}
AIGeschikt_Schema::init();