-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Block Library]: Add new
Read More
block (post link) (#37649)
* [Block Library - Post Link]: Add new post link block * rename to `read more` * move block declaration * change placeholder text
- Loading branch information
1 parent
fda7d3e
commit 8e31571
Showing
13 changed files
with
217 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,56 @@ | ||
{ | ||
"$schema": "https://schemas.wp.org/trunk/block.json", | ||
"apiVersion": 2, | ||
"name": "core/read-more", | ||
"title": "Read More", | ||
"category": "theme", | ||
"description": "Displays the link of a post, page, or any other content-type.", | ||
"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-read-more" | ||
} |
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 ReadMore( { | ||
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={ __( 'Read more' ) } | ||
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/read-more` block. | ||
* | ||
* @package WordPress | ||
*/ | ||
|
||
/** | ||
* Renders the `core/read-more` 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_read_more( $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/read-more` block on the server. | ||
*/ | ||
function register_block_core_read_more() { | ||
register_block_type_from_metadata( | ||
__DIR__ . '/read-more', | ||
array( | ||
'render_callback' => 'render_block_core_read_more', | ||
) | ||
); | ||
} | ||
add_action( 'init', 'register_block_core_read_more' ); |
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-read-more { | ||
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:read-more /--> |
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/read-more", | ||
"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/read-more", | ||
"attrs": {}, | ||
"innerBlocks": [], | ||
"innerHTML": "", | ||
"innerContent": [] | ||
} | ||
] |
1 change: 1 addition & 0 deletions
1
test/integration/fixtures/blocks/core__read-more.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:read-more /--> |