-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Block Library - Post Link]: Add new post link block
- Loading branch information
1 parent
62b8326
commit b28c202
Showing
13 changed files
with
218 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,57 @@ | ||
{ | ||
"$schema": "https://schemas.wp.org/trunk/block.json", | ||
"apiVersion": 2, | ||
"name": "core/post-link", | ||
"title": "Post Link", | ||
"category": "theme", | ||
"description": "Add the link of this post.", | ||
"keywords": [ "read more" ], | ||
"textdomain": "default", | ||
"attributes": { | ||
"content": { | ||
"type": "string" | ||
}, | ||
"linkTarget": { | ||
"type": "string", | ||
"default": "_self" | ||
} | ||
}, | ||
"usesContext": [ "postId" ], | ||
"supports": { | ||
"html": false, | ||
"color": { | ||
"gradients": true, | ||
"text": false | ||
}, | ||
"typography": { | ||
"fontSize": true, | ||
"lineHeight": true, | ||
"__experimentalFontFamily": true, | ||
"__experimentalFontWeight": true, | ||
"__experimentalFontStyle": true, | ||
"__experimentalTextTransform": true, | ||
"__experimentalLetterSpacing": true, | ||
"__experimentalTextDecoration": true, | ||
"__experimentalDefaultControls": { | ||
"fontSize": true, | ||
"textDecoration": true | ||
} | ||
}, | ||
"spacing": { | ||
"margin": [ "top", "bottom" ], | ||
"padding": true, | ||
"__experimentalDefaultControls": { | ||
"padding": true | ||
} | ||
}, | ||
"__experimentalBorder": { | ||
"color": true, | ||
"radius": true, | ||
"width": true, | ||
"__experimentalDefaultControls": { | ||
"width": true | ||
} | ||
} | ||
}, | ||
"style": "wp-block-post-link" | ||
} |
This file contains 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,50 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { | ||
InspectorControls, | ||
RichText, | ||
useBlockProps, | ||
} from '@wordpress/block-editor'; | ||
import { ToggleControl, PanelBody } from '@wordpress/components'; | ||
import { createBlock, getDefaultBlockName } from '@wordpress/blocks'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
export default function PostLink( { | ||
attributes: { content, linkTarget }, | ||
setAttributes, | ||
insertBlocksAfter, | ||
} ) { | ||
const blockProps = useBlockProps(); | ||
return ( | ||
<> | ||
<InspectorControls> | ||
<PanelBody title={ __( 'Link settings' ) }> | ||
<ToggleControl | ||
label={ __( 'Open in new tab' ) } | ||
onChange={ ( value ) => | ||
setAttributes( { | ||
linkTarget: value ? '_blank' : '_self', | ||
} ) | ||
} | ||
checked={ linkTarget === '_blank' } | ||
/> | ||
</PanelBody> | ||
</InspectorControls> | ||
<RichText | ||
tagName="a" | ||
aria-label={ __( '"Read more" link text' ) } | ||
placeholder={ __( 'Add "read more" link text' ) } | ||
value={ content } | ||
onChange={ ( newValue ) => | ||
setAttributes( { content: newValue } ) | ||
} | ||
__unstableOnSplitAtEnd={ () => | ||
insertBlocksAfter( createBlock( getDefaultBlockName() ) ) | ||
} | ||
withoutInteractiveFormatting={ true } | ||
{ ...blockProps } | ||
/> | ||
</> | ||
); | ||
} |
This file contains 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,18 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { link as icon } from '@wordpress/icons'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import metadata from './block.json'; | ||
import edit from './edit'; | ||
|
||
const { name } = metadata; | ||
export { metadata, name }; | ||
|
||
export const settings = { | ||
icon, | ||
edit, | ||
}; |
This file contains 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,45 @@ | ||
<?php | ||
/** | ||
* Server-side rendering of the `core/post-link` block. | ||
* | ||
* @package WordPress | ||
*/ | ||
|
||
/** | ||
* Renders the `core/post-link` block on the server. | ||
* | ||
* @param array $attributes Block attributes. | ||
* @param string $content Block default content. | ||
* @param WP_Block $block Block instance. | ||
* @return string Returns the post link. | ||
*/ | ||
function render_block_core_post_link( $attributes, $content, $block ) { | ||
if ( ! isset( $block->context['postId'] ) ) { | ||
return ''; | ||
} | ||
|
||
$post_ID = $block->context['postId']; | ||
$justify_class_name = empty( $attributes['justifyContent'] ) ? '' : "is-justified-{$attributes['justifyContent']}"; | ||
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $justify_class_name ) ); | ||
$more_text = ! empty( $attributes['content'] ) ? $attributes['content'] : __( 'Read more' ); | ||
return sprintf( | ||
'<a %1s href="%2s" target="%3s">%4s</a>', | ||
$wrapper_attributes, | ||
get_the_permalink( $post_ID ), | ||
esc_attr( $attributes['linkTarget'] ), | ||
$more_text | ||
); | ||
} | ||
|
||
/** | ||
* Registers the `core/post-link` block on the server. | ||
*/ | ||
function register_block_core_post_link() { | ||
register_block_type_from_metadata( | ||
__DIR__ . '/post-link', | ||
array( | ||
'render_callback' => 'render_block_core_post_link', | ||
) | ||
); | ||
} | ||
add_action( 'init', 'register_block_core_post_link' ); |
This file contains 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,12 @@ | ||
.wp-block-post-link { | ||
display: block; | ||
width: fit-content; | ||
&:not([style*="text-decoration"]) { | ||
text-decoration: none; | ||
|
||
&:focus, | ||
&:active { | ||
text-decoration: none; | ||
} | ||
} | ||
} |
This file contains 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 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 @@ | ||
<!-- wp:post-link /--> |
This file contains 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,12 @@ | ||
[ | ||
{ | ||
"clientId": "_clientId_0", | ||
"name": "core/post-link", | ||
"isValid": true, | ||
"attributes": { | ||
"linkTarget": "_self" | ||
}, | ||
"innerBlocks": [], | ||
"originalContent": "" | ||
} | ||
] |
This file contains 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,9 @@ | ||
[ | ||
{ | ||
"blockName": "core/post-link", | ||
"attrs": {}, | ||
"innerBlocks": [], | ||
"innerHTML": "", | ||
"innerContent": [] | ||
} | ||
] |
1 change: 1 addition & 0 deletions
1
test/integration/fixtures/blocks/core__post-link.serialized.html
This file contains 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 @@ | ||
<!-- wp:post-link /--> |