Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 68 additions & 21 deletions bibliography-builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1804,50 +1804,97 @@ function bibliography_builder_block_init() {
* active. If BAC is not present, this function is never called.
*
* Registered checks:
* - empty_bibliography (error) — block has no citations.
* - heading_missing (warning) — headingText is blank; no visible heading.
* - empty_bibliography (error) - block has no citations.
* - heading_missing (warning) - citations exist with no visible heading.
* - raw_url_link_text (warning) - citation link text is a raw URL/DOI.
* - all_metadata_disabled (warning) - metadata outputs are disabled.
*
* @param object $registry BAC check registry instance.
* @since 1.1.0
*/
function bibliography_builder_register_a11y_checks( $registry ) {
if ( ! is_object( $registry ) || ! method_exists( $registry, 'register_check' ) ) {
function bibliography_builder_register_a11y_checks() {
if ( ! function_exists( 'ba11yc_register_block_check' ) ) {
return;
}

$heading_missing_message = __(
$raw_url_link_text_message = __(
'One or more citations use a URL or DOI as link text. Add a descriptive title for screen readers.',
'borges-bibliography-builder'
);
$all_metadata_disabled_message = __(
'All machine-readable metadata outputs are disabled. Enable JSON-LD, COinS, or CSL-JSON for interoperability.',
'borges-bibliography-builder'
);
$heading_missing_message = __(
'Bibliography block has no heading. Screen reader users may not find this section.',
'borges-bibliography-builder'
);

$registry->register_check(
\ba11yc_register_block_check(
'bibliography-builder/bibliography',
'empty_bibliography',
array(
'error_msg' => __( 'Bibliography block contains no citations.', 'borges-bibliography-builder' ),
'warning_msg' => __( 'Bibliography block contains no citations.', 'borges-bibliography-builder' ),
'description' => __( 'Add at least one citation before publishing.', 'borges-bibliography-builder' ),
'type' => 'error',
'category' => 'accessibility',
'namespace' => 'borges-bibliography-builder',
'name' => 'empty_bibliography',
'error_msg' => __( 'Bibliography block contains no citations.', 'borges-bibliography-builder' ),
'warning_msg' => __( 'Bibliography block contains no citations.', 'borges-bibliography-builder' ),
'description' => __( 'Add at least one citation before publishing.', 'borges-bibliography-builder' ),
'level' => 'error',
'configurable' => true,
'category' => 'accessibility',
)
);

$registry->register_check(
\ba11yc_register_block_check(
'bibliography-builder/bibliography',
'heading_missing',
array(
'error_msg' => $heading_missing_message,
'warning_msg' => $heading_missing_message,
'description' => __(
'namespace' => 'borges-bibliography-builder',
'name' => 'heading_missing',
'error_msg' => $heading_missing_message,
'warning_msg' => $heading_missing_message,
'description' => __(
'Add a heading in block settings so the bibliography is announced as a document section.',
'borges-bibliography-builder'
),
'type' => 'warning',
'category' => 'accessibility',
'level' => 'warning',
'configurable' => true,
'category' => 'accessibility',
)
);

\ba11yc_register_block_check(
'bibliography-builder/bibliography',
array(
'namespace' => 'borges-bibliography-builder',
'name' => 'raw_url_link_text',
'error_msg' => $raw_url_link_text_message,
'warning_msg' => $raw_url_link_text_message,
'description' => __(
'Citations with a URL or DOI should have a descriptive title as link text.',
'borges-bibliography-builder'
),
'level' => 'warning',
'configurable' => true,
'category' => 'accessibility',
)
);

\ba11yc_register_block_check(
'bibliography-builder/bibliography',
array(
'namespace' => 'borges-bibliography-builder',
'name' => 'all_metadata_disabled',
'error_msg' => $all_metadata_disabled_message,
'warning_msg' => $all_metadata_disabled_message,
'description' => __(
'Enable at least one metadata output to improve citation-manager interoperability.',
'borges-bibliography-builder'
),
'level' => 'warning',
'configurable' => true,
'category' => 'accessibility',
)
);
}
Comment thread
dknauss marked this conversation as resolved.
add_action( 'ba11yc_ready', 'bibliography_builder_register_a11y_checks' );
add_action( 'ba11yc_ready', 'bibliography_builder_register_a11y_checks', 10, 0 );

/**
* Conditionally enqueue the BAC validation script in the block editor.
Expand Down
21 changes: 3 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
DEFAULT_CITATION_STYLE,
getDefaultHeadingText,
} from './lib/formatting';
import { registerBACChecks } from './lib/block-accessibility-checks';

registerBlockType(metadata.name, {
edit: Edit,
Expand All @@ -26,5 +25,3 @@ registerBlockType(metadata.name, {
},
],
});

registerBACChecks();
86 changes: 3 additions & 83 deletions src/lib/block-accessibility-checks.js
Original file line number Diff line number Diff line change
@@ -1,83 +1,3 @@
const BLOCK_NAME = 'bibliography-builder/bibliography';

const RAW_URL_RE = /^https?:\/\/\S+$|^10\.\d{4,}\/\S+$/i;

function isRawUrlText(text) {
return typeof text === 'string' && RAW_URL_RE.test(text.trim());
}

function hasRawUrlLinks(citations) {
if (!Array.isArray(citations)) {
return false;
}
return citations.some((csl) => {
const url = csl?.URL || csl?.DOI;
const title = csl?.title;
if (!url) {
return false;
}
return !title || isRawUrlText(title);
});
}

const checks = [
{
id: 'empty_bibliography',
message:
'This bibliography block has no citations. Empty blocks are omitted from the published page.',
severity: 'error',
test(attributes) {
const { citations } = attributes;
return !Array.isArray(citations) || citations.length === 0;
},
},
{
id: 'heading_missing',
message:
'This bibliography block has citations but no visible heading or accessible label. Add a heading so readers can identify the reference list.',
severity: 'warning',
test(attributes) {
const { citations, headingText } = attributes;
const hasCitations =
Array.isArray(citations) && citations.length > 0;
const hasHeading =
typeof headingText === 'string' && headingText.trim() !== '';
return hasCitations && !hasHeading;
},
},
{
id: 'raw_url_link_text',
message:
'One or more citations have a URL or DOI as the only identifier. A descriptive title helps screen reader users understand the link destination.',
severity: 'warning',
test(attributes) {
return hasRawUrlLinks(attributes.citations);
},
},
{
id: 'all_metadata_disabled',
message:
'All machine-readable metadata outputs (JSON-LD, COinS, CSL-JSON) are disabled. Enabling at least one improves citation-manager interoperability.',
severity: 'warning',
test(attributes) {
const { outputJsonLd, outputCoins, outputCslJson } = attributes;
return !outputJsonLd && !outputCoins && !outputCslJson;
},
},
];

export function registerBACChecks() {
if (
typeof window === 'undefined' ||
typeof window.BlockAccessibilityChecks === 'undefined'
) {
return;
}

const { registerBlockChecks } = window.BlockAccessibilityChecks;
if (typeof registerBlockChecks !== 'function') {
return;
}

registerBlockChecks(BLOCK_NAME, checks);
}
// BAC v3 window-global integration path removed in v4.
// All checks are now registered via ba11yc_register_block_check() in PHP
// and validated via the ba11yc.validateBlock filter in src/validation.js.
Loading
Loading