$settings Module settings. * * @return void */ private static function render_post_markdown( array $settings ): void { $post_id = get_queried_object_id(); if ( $post_id <= 0 ) { return; } $post = get_post( $post_id ); if ( ! ( $post instanceof \WP_Post ) ) { return; } if ( 'publish' !== $post->post_status || post_password_required( $post ) ) { self::send_not_found(); return; } $allowed_types = isset( $settings['post_types'] ) && is_array( $settings['post_types'] ) ? array_values( array_filter( array_map( 'strval', $settings['post_types'] ) ) ) : array(); if ( ! in_array( $post->post_type, $allowed_types, true ) ) { return; } $repo = new MarkdownPostRepository(); $cached = $repo->get_by_post_id( $post_id ); $content = ''; if ( is_array( $cached ) && empty( $cached['is_deleted'] ) && ! empty( $cached['markdown_content'] ) ) { $content = (string) $cached['markdown_content']; } else { $payload = MarkdownExporter::export( $post_id, $settings ); if ( is_array( $payload ) ) { $repo->upsert( $payload ); $content = (string) $payload['markdown_content']; } } if ( '' === $content ) { return; } nocache_headers(); header( 'Vary: Accept' ); header( 'Content-Type: text/markdown; charset=UTF-8' ); // Escaped late with wp_kses_post(): strips dangerous markup (script/iframe/event // handlers) while leaving standard markdown intact, so the body is safe even if a // client renders it as HTML. echo wp_kses_post( $content ); exit; } /** * Emit a 404 plain-text response and stop further output. * * @return void */ private static function send_not_found(): void { nocache_headers(); status_header( 404 ); header( 'Content-Type: text/plain; charset=UTF-8' ); echo "Not Found\n"; exit; } }