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
2 changes: 1 addition & 1 deletion build/block-checks.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('react-jsx-runtime', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-compose', 'wp-data', 'wp-editor', 'wp-element', 'wp-hooks', 'wp-i18n', 'wp-plugins', 'wp-primitives'), 'version' => '85e98543fc6a31ad69bd');
<?php return array('dependencies' => array('react-jsx-runtime', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-compose', 'wp-data', 'wp-editor', 'wp-element', 'wp-hooks', 'wp-i18n', 'wp-plugins', 'wp-primitives'), 'version' => '58b74445fa984a5955c3');
6 changes: 3 additions & 3 deletions build/block-checks.js

Large diffs are not rendered by default.

8 changes: 0 additions & 8 deletions docs-backup/api-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,14 +233,6 @@ register_post_meta( 'band', 'band_origin', [

---

### `MetaValidation::detect_post_type_from_context()`
Detect post type from the current request context.

**Returns:**
- `string|false`: Post type or false if not detected

---

## See Also
- [Post Meta Validation](./meta-validation.md)
- [Hooks Reference](./hooks.md)
Expand Down
12 changes: 6 additions & 6 deletions docs/integration/external-plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ External plugins can register their own validation checks for custom blocks. The
// In your plugin's main file or integration class
add_action( 'ba11yc_ready', function( $registry ) {
// Use the simplified registration method with automatic plugin detection
$registry->register_check_with_plugin_detection(
$registry->register_block_check(
'your-plugin/your-block',
'check_name',
array(
Expand Down Expand Up @@ -75,7 +75,7 @@ Checks registered with `'type' => 'settings'` will:

## Automatic Plugin Detection

The `register_check_with_plugin_detection()` method automatically:
The `register_block_check()` method automatically:

1. Detects which plugin is registering the check
2. Extracts plugin name, version, and file path
Expand Down Expand Up @@ -109,7 +109,7 @@ class MyCustomBlocksIntegration {

public function register_checks( $registry ) {
// Card block checks
$registry->register_check_with_plugin_detection(
$registry->register_block_check(
'my-custom-blocks/card',
'check_title',
array(
Expand All @@ -121,7 +121,7 @@ class MyCustomBlocksIntegration {
)
);

$registry->register_check_with_plugin_detection(
$registry->register_block_check(
'my-custom-blocks/card',
'check_image_alt',
array(
Expand Down Expand Up @@ -231,7 +231,7 @@ Use a consistent namespace for all blocks from your plugin:

### Plugin Not Appearing in Settings

1. Ensure you're using `register_check_with_plugin_detection()`
1. Ensure you're using `register_block_check()`
2. Check that your plugin file has a proper plugin header
3. Verify that at least one check has `'type' => 'settings'`

Expand All @@ -245,7 +245,7 @@ Use a consistent namespace for all blocks from your plugin:

1. Use the same namespace for all blocks from your plugin
2. Avoid using different namespaces for blocks from the same plugin
3. Use `register_check_with_plugin_detection()` for automatic grouping
3. Use `register_block_check()` for automatic grouping

## See Also

Expand Down
43 changes: 34 additions & 9 deletions docs/reference/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ This document provides comprehensive API documentation for all registry methods

Use `BlockChecksRegistry::get_instance()` to access the block checks registry.

### `register_check( $block_type, $check_name, $check_args )`
### `register_check( $block_type, $check_name, $check_args, $plugin_info = [] )`

Register a new accessibility check for a block type.
Register a new accessibility check for a block type with optional manual plugin information.

**Parameters:**
- `$block_type` (string): Block type (e.g., 'core/image', 'my-plugin/custom-block')
- `$check_name` (string): Unique check name within the block type
- `$check_args` (array): Check configuration
- `$plugin_info` (array): Optional plugin information for manual grouping

**Returns:**
- `bool`: True on success, false on failure
Expand All @@ -31,6 +32,37 @@ $check_args = array(
);
```

### `register_block_check( $block_type, $check_name, $check_args )`

**Recommended for External Plugins** - Register a block accessibility check with automatic plugin detection.

This is a convenience method that automatically detects your plugin's information (name, version, file path) and properly groups all your blocks together in the admin settings interface.

**Parameters:**
- `$block_type` (string): Block type (e.g., 'my-plugin/card-block')
- `$check_name` (string): Unique check name within the block type
- `$check_args` (array): Check configuration (same as `register_check`)

**Returns:**
- `bool`: True on success, false on failure

**Example:**
```php
add_action( 'ba11yc_ready', function( $registry ) {
$registry->register_block_check(
'my-plugin/card-block',
'card_title_required',
array(
'error_msg' => __( 'Card title is required', 'my-plugin' ),
'warning_msg' => __( 'Card title is recommended', 'my-plugin' ),
'description' => __( 'Card title validation', 'my-plugin' ),
'type' => 'settings',
'category' => 'accessibility',
)
);
});
```

### `unregister_check( $block_type, $check_name )`

Remove a previously registered check.
Expand Down Expand Up @@ -276,13 +308,6 @@ register_post_meta( 'band', 'band_origin', [
] );
```

### `MetaValidation::detect_post_type_from_context()`

Detect post type from the current request context.

**Returns:**
- `string|false`: Post type or false if not detected

## Usage Examples

### Block Checks
Expand Down
12 changes: 0 additions & 12 deletions includes/Block/CoreChecks.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,16 +164,4 @@ private function get_core_block_check_definitions(): array {

return $this->definitions_cache;
}

/**
* Get supported core block types
*
* Returns an array of core block types that have accessibility checks.
* Derived dynamically from the check definitions for single source of truth.
*
* @return array Array of supported core block types.
*/
public function get_supported_core_block_types(): array {
return \array_keys( $this->get_core_block_check_definitions() );
}
}
2 changes: 1 addition & 1 deletion includes/Block/HeadingLevels.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public function modify_heading_levels_globally( array $args, string $block_type
}

// Only apply restrictions when we're in a content editor context.
if ( ! $this->is_content_editor() ) {
if ( ! $this->is_post_editor() ) {
return $args;
}

Expand Down
10 changes: 7 additions & 3 deletions includes/Block/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -444,14 +444,18 @@ public function get_all_plugin_info(): array {
}

/**
* Register a check with automatic plugin detection
* Register a block accessibility check with automatic plugin detection
*
* @param string $block_type Block type (e.g., 'core/image').
* Convenience method for external plugins to register block validation checks
* without manually providing plugin information. Plugin details are automatically
* detected and used for proper admin interface grouping.
*
* @param string $block_type Block type (e.g., 'my-plugin/card-block').
* @param string $check_name Unique check name.
* @param array $check_args Check configuration.
* @return bool True on success, false on failure.
*/
public function register_check_with_plugin_detection( string $block_type, string $check_name, array $check_args ): bool {
public function register_block_check( string $block_type, string $check_name, array $check_args ): bool {
// Automatically detect plugin information.
$plugin_info = $this->detect_plugin_info();

Expand Down
10 changes: 0 additions & 10 deletions includes/Core/Traits/EditorDetection.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,4 @@ private function should_load_validation(): bool {
$context = $this->get_editor_context();
return in_array( $context, array( 'post-editor', 'post-editor-template', 'site-editor' ), true );
}

/**
* Check if we're in the content editor (legacy method for backward compatibility).
*
* @deprecated Use get_editor_context() or is_post_editor() instead.
* @return bool True if in content editor, false otherwise.
*/
private function is_content_editor(): bool {
return $this->is_post_editor();
}
}
76 changes: 0 additions & 76 deletions includes/Meta/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,80 +105,4 @@ public static function required( string $post_type, string $meta_key, array $arg
return true;
};
}

/**
* Detect post type from current context
*
* Attempts to determine the post type being edited from various sources:
* - REST API request
* - Admin screen
* - Global $post
* - Request parameters
*
* @return string|false Post type or false if not detected.
*/
public static function detect_post_type_from_context() {
// Method 1: Check REST request.
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
$route = isset( $_SERVER['REQUEST_URI'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '';
// Match patterns like /wp/v2/{post_type}/{id}.
if ( preg_match( '#/wp/v2/([^/]+)/(\d+)#', $route, $matches ) ) {
$post_type = $matches[1];

// Get the actual post type from the post ID.
$post_id = absint( $matches[2] );
if ( $post_id ) {
$actual_post_type = \get_post_type( $post_id );
if ( $actual_post_type ) {
return $actual_post_type;
}
}

// Fallback: Handle plural to singular conversion.
// Common patterns: posts -> post, pages -> page.
$singular_map = array(
'posts' => 'post',
'pages' => 'page',
);

if ( isset( $singular_map[ $post_type ] ) ) {
return $singular_map[ $post_type ];
}

// For custom post types, try removing trailing 's'.
return rtrim( $post_type, 's' );
}
}

// Method 2: Check current admin screen.
if ( function_exists( 'get_current_screen' ) ) {
$screen = \get_current_screen();
if ( $screen && ! empty( $screen->post_type ) ) {
return $screen->post_type;
}
}

// Method 3: Check global $post.
global $post;
if ( $post && ! empty( $post->post_type ) ) {
return $post->post_type;
}

// Method 4: Check $_REQUEST parameters.
// phpcs:disable WordPress.Security.NonceVerification.Recommended -- Reading context, not processing form submission.
if ( ! empty( $_REQUEST['post_type'] ) ) {
return \sanitize_key( wp_unslash( $_REQUEST['post_type'] ) );
}

if ( ! empty( $_REQUEST['post'] ) ) {
$post_id = \absint( $_REQUEST['post'] );
// phpcs:enable WordPress.Security.NonceVerification.Recommended
$post_type = \get_post_type( $post_id );
if ( $post_type ) {
return $post_type;
}
}

return false;
}
}
2 changes: 0 additions & 2 deletions src/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,3 @@ if (typeof window.BlockAccessibilityChecks === 'undefined') {
}

window.BlockAccessibilityChecks.useMetaField = useMetaField;
// Alias for backwards compatibility during refactor
window.BlockAccessibilityChecks.useMetaValidationProps = useMetaField;
Loading