Skip to content
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

Link Wrapper: Default to the permalink if the url isn't defined #655

Draft
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Draft
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
29 changes: 28 additions & 1 deletion mu-plugins/blocks/link-wrapper/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,32 @@
* @see https://developer.wordpress.org/reference/functions/register_block_type/
*/
function init() {
register_block_type( __DIR__ . '/build' );
register_block_type(
__DIR__ . '/build',
array(
'render_callback' => __NAMESPACE__ . '\render',
)
);
}

/**
* Renders the Link Wrapper block.
*
* @param array $attributes Block attributes.
* @param string $content Block content.
* @return string Rendered block HTML.
*/
function render( $attributes, $content, $block ) {
$post_id = $block->context['postId'];
$post_slug = get_post_field( 'post_name', $post_id );
$link = isset( $attributes['url'] ) ? ' ' . $attributes['url'] : site_url( $post_slug );

$wrapper_attributes = get_block_wrapper_attributes();

return sprintf(
'<a href="%1$s" %2$s>%3$s</a>',
esc_url( $link ),
$wrapper_attributes,
do_blocks( $content )
);
}
4 changes: 4 additions & 0 deletions mu-plugins/blocks/link-wrapper/src/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"icon": "admin-links",
"category": "layout",
"description": "Link a set of blocks to a given page.",
"usesContext": [ "postId" ],
"textdomain": "wporg",
"attributes": {
"url": {
Expand All @@ -30,6 +31,9 @@
"padding": true
}
},
"dimensions": {
"minHeight": true
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Allow configuring the wrapper to fill the grid item.

},
"typography": {
"fontSize": true,
"lineHeight": true
Expand Down
10 changes: 3 additions & 7 deletions mu-plugins/blocks/link-wrapper/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ function Edit( { attributes, setAttributes } ) {
<>
<InspectorControls key="setting">
<PanelBody title={ __( 'Link destination', 'wporg' ) }>
<p>{ __( 'Defaults to the permalink if not set.', 'wporg' ) }</p>
<TextControl
label={ __( 'Link destination', 'wporg' ) }
hideLabelFromVision
Expand Down Expand Up @@ -55,12 +56,7 @@ function Edit( { attributes, setAttributes } ) {

registerBlockType( metadata.name, {
edit: Edit,
save: ( { attributes } ) => {
const blockProps = useBlockProps.save();
return (
<a { ...blockProps } href={ attributes.url }>
<InnerBlocks.Content />
</a>
);
save: () => {
return <InnerBlocks.Content />;
},
} );
Loading