-
Notifications
You must be signed in to change notification settings - Fork 19
Add Simplified Summary block and shortcode for manual placement #1837
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SteveJonesDev
wants to merge
5
commits into
develop
Choose a base branch
from
steve/pro-1226-block-to-insert-the-simplified-summary-manually-in-the-block
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6422d2b
Add Simplified Summary block and shortcode for manual placement
SteveJonesDev a9ff60e
Address code review feedback
SteveJonesDev 62b4fc6
Preview the filtered summary heading in the block editor
SteveJonesDev 3ff46d5
Add an Accessibility Checker block category
SteveJonesDev 6cd21c0
Mention both authoring locations in the block placeholder
SteveJonesDev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
75
includes/classes/Shortcodes/SimplifiedSummaryShortcode.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 ) ) { | ||
| 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 ); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 explicitpost_idcan pass this check andsimplified_summary_markup()will read its summary meta directly without requiring the password. WordPress core’s post-meta block binding handles this separately withpost_password_required(). Could we add that guard here and cover a password-protected published post in the shortcode tests?