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

[Block Library]: Add new Read More block (post link) #37649

Merged
merged 4 commits into from
Feb 4, 2022
Merged
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
9 changes: 9 additions & 0 deletions docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,15 @@ Give quoted text visual emphasis. "In quoting others, we cite ourselves." — Ju
- **Supports:** anchor, typography (fontSize, lineHeight)
- **Attributes:** align, citation, value

## Read More

Displays the link of a post, page, or any other content-type. ([Source](https://github.com/WordPress/gutenberg/tree/trunk/packages/block-library/src/read-more))

- **Name:** core/read-more
- **Category:** theme
- **Supports:** color (background, gradients, ~~text~~), spacing (margin, padding), typography (fontSize, lineHeight), ~~html~~
- **Attributes:** content, linkTarget

## RSS

Display entries from any RSS or Atom feed. ([Source](https://github.com/WordPress/gutenberg/tree/trunk/packages/block-library/src/rss))
Expand Down
1 change: 1 addition & 0 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ function gutenberg_reregister_core_block_types() {
'query-pagination-numbers.php' => 'core/query-pagination-numbers',
'query-pagination-previous.php' => 'core/query-pagination-previous',
'query-title.php' => 'core/query-title',
'read-more.php' => 'core/read-more',
'rss.php' => 'core/rss',
'search.php' => 'core/search',
'shortcode.php' => 'core/shortcode',
Expand Down
2 changes: 2 additions & 0 deletions packages/block-library/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ import * as queryPaginationPrevious from './query-pagination-previous';
import * as queryTitle from './query-title';
import * as quote from './quote';
import * as reusableBlock from './block';
import * as readMore from './read-more';
import * as rss from './rss';
import * as search from './search';
import * as separator from './separator';
Expand Down Expand Up @@ -271,6 +272,7 @@ export const __experimentalRegisterExperimentalCoreBlocks = process.env
postCommentsCount,
postCommentsForm,
postCommentsLink,
readMore,
]
: [] ),
].forEach( registerBlock );
Expand Down
56 changes: 56 additions & 0 deletions packages/block-library/src/read-more/block.json
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"
}
50 changes: 50 additions & 0 deletions packages/block-library/src/read-more/edit.js
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 }
/>
</>
);
}
18 changes: 18 additions & 0 deletions packages/block-library/src/read-more/index.js
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,
};
45 changes: 45 additions & 0 deletions packages/block-library/src/read-more/index.php
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
ntsekouras marked this conversation as resolved.
Show resolved Hide resolved
);
}

/**
* 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' );
12 changes: 12 additions & 0 deletions packages/block-library/src/read-more/style.scss
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;
}
}
}
1 change: 1 addition & 0 deletions packages/block-library/src/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
@import "./post-template/style.scss";
@import "./query-pagination/style.scss";
@import "./quote/style.scss";
@import "./read-more/style.scss";
@import "./rss/style.scss";
@import "./search/style.scss";
@import "./separator/style.scss";
Expand Down
1 change: 1 addition & 0 deletions test/integration/fixtures/blocks/core__read-more.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- wp:read-more /-->
12 changes: 12 additions & 0 deletions test/integration/fixtures/blocks/core__read-more.json
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": ""
}
]
9 changes: 9 additions & 0 deletions test/integration/fixtures/blocks/core__read-more.parsed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[
{
"blockName": "core/read-more",
"attrs": {},
"innerBlocks": [],
"innerHTML": "",
"innerContent": []
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- wp:read-more /-->