Skip to content
Open
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
21 changes: 21 additions & 0 deletions includes/blocks/simplified-summary/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 3,
"name": "edac/simplified-summary",
"title": "Simplified Summary",
"description": "Displays this post's simplified summary from Accessibility Checker.",
"category": "accessibility-checker",
"icon": "universal-access-alt",
"keywords": [ "accessibility", "summary", "readability" ],
"textdomain": "accessibility-checker",
"usesContext": [ "postId" ],
"supports": {
"html": false,
"multiple": false,
"spacing": {
"margin": true
}
},
"editorScript": "edac-simplified-summary-block",
"editorStyle": "edac-simplified-summary-block-editor"
}
182 changes: 182 additions & 0 deletions includes/classes/Blocks/SimplifiedSummaryBlock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
<?php
/**
* Registers the simplified summary block.
*
* @package Accessibility_Checker
*/

namespace EqualizeDigital\AccessibilityChecker\Blocks;

use EDAC\Inc\Simplified_Summary;
use WP_Block;

/**
* Registers and renders the edac/simplified-summary dynamic block.
*
* Lets users place the simplified summary manually in post content or in a
* block theme (FSE) template instead of relying on the automatic insertion
* that runs on the_content.
*
* @since 1.xx.x
*/
class SimplifiedSummaryBlock {

/**
* The block name.
*
* @var string
*/
const BLOCK_NAME = 'edac/simplified-summary';

/**
* The editor script handle referenced from block.json.
*
* @var string
*/
const SCRIPT_HANDLE = 'edac-simplified-summary-block';

/**
* The editor style handle referenced from block.json.
*
* @var string
*/
const STYLE_HANDLE = 'edac-simplified-summary-block-editor';

/**
* The block category slug.
*
* @var string
*/
const CATEGORY = 'accessibility-checker';

/**
* Initialize WordPress hooks.
*
* @since 1.xx.x
*
* @return void
*/
public function init_hooks() {
add_action( 'init', [ $this, 'register' ] );
add_filter( 'block_categories_all', [ $this, 'register_block_category' ] );
}

/**
* Register the Accessibility Checker block category.
*
* @since 1.xx.x
*
* @param array $categories The registered block categories.
* @return array
*/
public function register_block_category( $categories ) {
foreach ( $categories as $category ) {
if ( self::CATEGORY === $category['slug'] ) {
return $categories;
}
}

$categories[] = [
'slug' => self::CATEGORY,
'title' => esc_html__( 'Accessibility Checker', 'accessibility-checker' ),
'icon' => null,
];

return $categories;
}

/**
* Register the block and its editor assets.
*
* The build does not generate *.asset.php files, so the editor script is
* registered here with an explicit dependency list and block.json
* references the handle rather than a file.
*
* @since 1.xx.x
*
* @return void
*/
public function register() {
if ( \WP_Block_Type_Registry::get_instance()->is_registered( self::BLOCK_NAME ) ) {
return;
}

wp_register_script(
self::SCRIPT_HANDLE,
plugin_dir_url( EDAC_PLUGIN_FILE ) . 'build/simplifiedSummaryBlock.bundle.js',
[ 'wp-blocks', 'wp-element', 'wp-i18n', 'wp-block-editor' ],
EDAC_VERSION,
true
);

wp_set_script_translations(
self::SCRIPT_HANDLE,
'accessibility-checker',
plugin_dir_path( EDAC_PLUGIN_FILE ) . 'languages'
);

wp_localize_script(
self::SCRIPT_HANDLE,
'edacSimplifiedSummaryBlock',
[
/** This filter is documented in includes/classes/class-simplified-summary.php */
'heading' => apply_filters(
'edac_filter_simplified_summary_heading',
esc_html__( 'Simplified Summary', 'accessibility-checker' )
),
]
);

wp_register_style(
self::STYLE_HANDLE,
plugin_dir_url( EDAC_PLUGIN_FILE ) . 'build/css/simplifiedSummaryBlock.css',
[],
EDAC_VERSION
);

register_block_type(
EDAC_PLUGIN_DIR . 'includes/blocks/simplified-summary',
[ 'render_callback' => [ $this, 'render' ] ]
);
}

/**
* Render the block on the front end.
*
* Uses the postId block context when available (FSE templates, Query
* Loop) and falls back to the global post. Renders regardless of the
* edac_simplified_summary_prompt option because manual placement is a
* deliberate act, matching edac_get_simplified_summary().
*
* @since 1.xx.x
*
* @param array $attributes The block attributes.
* @param string $content The block content.
* @param WP_Block|null $block The block instance.
* @return string
*/
public function render( $attributes, $content, $block = null ) {
$post_id = ( $block instanceof WP_Block && isset( $block->context['postId'] ) )
? (int) $block->context['postId']
: (int) get_the_ID();

if ( ! $post_id ) {
return '';
}

$markup = ( new Simplified_Summary() )->simplified_summary_markup( $post_id );

if ( ! $markup ) {
return '';
}

// The wrapper carries the block supports output (margin classes/styles).
// get_block_wrapper_attributes() requires an active block render; guard
// against direct calls to this callback outside of one.
$wrapper_attributes = null !== \WP_Block_Supports::$block_to_render
? get_block_wrapper_attributes()
: 'class="wp-block-edac-simplified-summary"';

return sprintf( '<div %s>%s</div>', $wrapper_attributes, $markup );
}
}
75 changes: 75 additions & 0 deletions includes/classes/Shortcodes/SimplifiedSummaryShortcode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
/**
* Registers the simplified summary shortcode.
*
* @package Accessibility_Checker
*/

namespace EqualizeDigital\AccessibilityChecker\Shortcodes;

use EDAC\Inc\Simplified_Summary;

/**
* Registers and renders the [edac_simplified_summary] shortcode.
*
* Lets users output the simplified summary manually in classic content or
* shortcode-aware areas. Accepts an optional post_id attribute and falls
* back to the current post.
*
* @since 1.xx.x
*/
class SimplifiedSummaryShortcode {

/**
* The shortcode tag.
*
* @var string
*/
const SHORTCODE = 'edac_simplified_summary';

/**
* Initialize WordPress hooks.
*
* @since 1.xx.x
*
* @return void
*/
public function init_hooks() {
add_shortcode( self::SHORTCODE, [ $this, 'render' ] );
}

/**
* Render the shortcode.
*
* Renders regardless of the edac_simplified_summary_prompt option because
* manual placement is a deliberate act, matching edac_get_simplified_summary().
* An explicit post_id must reference a publicly viewable post so the
* shortcode cannot expose summaries of draft or private posts.
*
* @since 1.xx.x
*
* @param array|string $atts The shortcode attributes.
* @return string
*/
public function render( $atts ) {
$atts = shortcode_atts(
[ 'post_id' => 0 ],
$atts,
self::SHORTCODE
);

$explicit_post_id = absint( $atts['post_id'] );

if ( $explicit_post_id && ! is_post_publicly_viewable( $explicit_post_id ) ) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is_post_publicly_viewable() does not account for a published post being password protected, so an explicit post_id can pass this check and simplified_summary_markup() will read its summary meta directly without requiring the password. WordPress core’s post-meta block binding handles this separately with post_password_required(). Could we add that guard here and cover a password-protected published post in the shortcode tests?

return '';
}

$post_id = $explicit_post_id ? $explicit_post_id : (int) get_the_ID();

if ( ! $post_id ) {
return '';
}

return ( new Simplified_Summary() )->simplified_summary_markup( $post_id );
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}
}
9 changes: 9 additions & 0 deletions includes/classes/class-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
use EDAC\Admin\Meta_Boxes;
use EDAC\Admin\Orphaned_Issues_Cleanup;
use EqualizeDigital\AccessibilityChecker\Admin\AdminPage\AccessibilityReportsPage;
use EqualizeDigital\AccessibilityChecker\Blocks\SimplifiedSummaryBlock;
use EqualizeDigital\AccessibilityChecker\MyDot\Connector;
use EqualizeDigital\AccessibilityChecker\Shortcodes\SimplifiedSummaryShortcode;
use EqualizeDigital\AccessibilityChecker\WPCLI\BootstrapCLI;
use EqualizeDigital\AccessibilityChecker\Fixes\FixesManager;

Expand Down Expand Up @@ -47,6 +49,13 @@ public function __construct() {
$cleanup = new Orphaned_Issues_Cleanup();
$cleanup->init_hooks();

// The block and shortcode must register in admin (for the editor) and on the front end.
$simplified_summary_block = new SimplifiedSummaryBlock();
$simplified_summary_block->init_hooks();

$simplified_summary_shortcode = new SimplifiedSummaryShortcode();
$simplified_summary_shortcode->init_hooks();

$this->register_fixes_manager();
$this->register_sr_only_meta_hooks();

Expand Down
55 changes: 55 additions & 0 deletions includes/classes/class-simplified-summary.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public function output_simplified_summary( $content ) {
if ( 'none' === $simplified_summary_prompt ) {
return $content;
}
if ( $this->is_manually_placed( get_the_ID() ) ) {
return $content;
}
$simplified_summary = $this->simplified_summary_markup( get_the_ID() );
$simplified_summary_position = get_option( 'edac_simplified_summary_position', $default = false );

Expand All @@ -54,6 +57,58 @@ public function output_simplified_summary( $content ) {
return $content;
}

/**
* Check whether the simplified summary has been manually placed for a post.
*
* Detects the edac/simplified-summary block or [edac_simplified_summary]
* shortcode in the post content, and the block in the current block theme
* template. Blocks nested inside template parts or synced patterns cannot
* be detected by has_block(); the filter below is the escape hatch for
* those cases.
*
* @since 1.xx.x
*
* @param int|\WP_Post|null $post Post ID or post object.
* @return bool
*/
public function is_manually_placed( $post = null ): bool {
$manually_placed = false;

$post = get_post( $post );
if ( $post instanceof \WP_Post ) {
if (
has_block( 'edac/simplified-summary', $post ) ||
has_shortcode( (string) $post->post_content, 'edac_simplified_summary' )
) {
$manually_placed = true;
}
}

// Set by WordPress when rendering a block theme template.
global $_wp_current_template_content;
if (
! $manually_placed &&
! empty( $_wp_current_template_content ) &&
(
has_block( 'edac/simplified-summary', $_wp_current_template_content ) ||
has_shortcode( $_wp_current_template_content, 'edac_simplified_summary' )
)
) {
$manually_placed = true;
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

/**
* Filter whether the simplified summary is manually placed, which
* suppresses the automatic insertion on the_content.
*
* @since 1.xx.x
*
* @param bool $manually_placed Whether the summary is manually placed.
* @param \WP_Post|null $post The post being checked.
*/
return apply_filters( 'edac_filter_simplified_summary_is_manually_placed', $manually_placed, $post );
}

/**
* Simplified summary markup
*
Expand Down
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ Current settings in the free plugin include:
* Control if you want scans to run on both pages and posts.
* Control when the plugin prompts for a simplified summary.
* Choose the position of the simplified summary above content, below content, or manually in a template.
* Place the simplified summary anywhere with the Simplified Summary block or the `[edac_simplified_summary]` shortcode (accepts an optional `post_id` attribute); manual placement automatically disables the automatic insertion for that post.
* Add footer accessibility statement.
* Choose positioning for the front-end Accessibility Checker.
* Show or hide the Accessibility Checker metabox in the block editor.
Expand Down
Loading
Loading