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

Add handbook meta link block #558

Merged
merged 6 commits into from
Jan 22, 2024
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
70 changes: 70 additions & 0 deletions mu-plugins/blocks/handbook-meta-link/block.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
namespace WordPressdotorg\MU_Plugins\Handbook_Meta_Link;

add_action( 'init', __NAMESPACE__ . '\init' );

/**
* Registers the block using the metadata loaded from the `block.json` file.
* Behind the scenes, it registers also all assets so they can be enqueued
* through the block editor in the corresponding context.
*
* @see https://developer.wordpress.org/reference/functions/register_block_type/
*/
function init() {
register_block_type(
__DIR__ . '/build',
array(
'render_callback' => __NAMESPACE__ . '\render',
)
);
}

/**
* Render the block content.
*
* @param array $attributes Block attributes.
* @param string $content Block default content.
* @param WP_Block $block Block instance.
*
* @return string Returns the block markup.
*/
function render( $attributes, $content, $block ) {
$post_type = $block->context['postType'];
$post_id = $block->context['postId'];

if (
! isset( $post_id ) || ! isset( $post_type )
) {
return '';
}

$title = get_the_title( $post_id );
$link_url = $attributes['linkURL'];

return sprintf(
do_blocks(
'<!-- wp:group {"style":{"spacing":{"blockGap":"0"}},"layout":{"type":"constrained"}} -->
<div class="wp-block-group">

<!-- wp:paragraph {"style":{"typography":{"fontStyle":"normal","fontWeight":"700"}}} -->
<p style="font-style:normal;font-weight:700">%1$s</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph {"className":"external-link"} -->
<p class="external-link"><a href="%2$s">%3$s</a></p>
<!-- /wp:paragraph -->

</div>
<!-- /wp:group -->'
),
esc_html( $attributes['heading'] ),
// Don't use esc_url for a shortcode.
preg_match( '/^\[.*\]$/', $link_url ) ? esc_html( $link_url ) : esc_url( $link_url ),
sprintf(
/* translators: %1$s: call to action, %2$s: article title */
__( '%1$s<span class="screen-reader-text">: %2$s"</span>', 'wporg' ),
esc_html( $attributes['linkText'] ),
esc_html( $title )
)
);
}
30 changes: 30 additions & 0 deletions mu-plugins/blocks/handbook-meta-link/src/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2,
"name": "wporg/handbook-meta-link",
"version": "0.1.0",
"title": "Handbook Meta Link",
"category": "widgets",
"icon": "smiley",
"description": "",
"usesContext": [ "postId", "postType" ],
"attributes": {
"heading": {
"type": "string",
"default": ""
},
"linkText": {
"type": "string",
"default": ""
},
"linkUrl": {
"type": "string",
"default": ""
}
},
"supports": {
"inserter": false
},
"textdomain": "wporg",
"editorScript": "file:./index.js"
}
20 changes: 20 additions & 0 deletions mu-plugins/blocks/handbook-meta-link/src/edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* WordPress dependencies
*/
import { useBlockProps } from '@wordpress/block-editor';
import ServerSideRender from '@wordpress/server-side-render';

export default function Edit( { name, attributes, context } ) {
const blockProps = useBlockProps();
const { postId } = context;
return (
<div { ...blockProps }>
<ServerSideRender
block={ name }
attributes={ attributes }
skipBlockSupportAttributes
urlQueryArgs={ { post_id: postId } }
/>
</div>
);
}
17 changes: 17 additions & 0 deletions mu-plugins/blocks/handbook-meta-link/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* WordPress dependencies
*/
import { registerBlockType } from '@wordpress/blocks';

/**
* Internal dependencies
*/
import Edit from './edit';
import metadata from './block.json';

registerBlockType( metadata.name, {
/**
* @see ./edit.js
*/
edit: Edit,
} );
1 change: 1 addition & 0 deletions mu-plugins/loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
require_once __DIR__ . '/helpers/helpers.php';
require_once __DIR__ . '/blocks/global-header-footer/blocks.php';
require_once __DIR__ . '/blocks/google-map/index.php';
require_once __DIR__ . '/blocks/handbook-meta-link/block.php';
require_once __DIR__ . '/blocks/horizontal-slider/horizontal-slider.php';
require_once __DIR__ . '/blocks/language-suggest/language-suggest.php';
require_once __DIR__ . '/blocks/local-navigation-bar/index.php';
Expand Down
Loading