Skip to content

Commit

Permalink
Merge pull request #54 from alleyinteractive/feature/issue-48/change-…
Browse files Browse the repository at this point in the history
…single-post-inner-blocks-more-blocks

Feature/issue 48/change single post inner blocks more blocks
  • Loading branch information
mogmarsh authored Jan 17, 2024
2 parents 29a9d19 + 6557281 commit 03f66a1
Show file tree
Hide file tree
Showing 35 changed files with 370 additions and 75 deletions.
2 changes: 1 addition & 1 deletion blocks/email-settings/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"category": "design",
"icon": "email",
"description": "Block to set the email subject, preview text, and list.",
"textdomain": "email-settings",
"textdomain": "wp-newsletter-builder",
"editorScript": "file:index.ts",
"editorStyle": "file:index.css",
"style": [
Expand Down
2 changes: 0 additions & 2 deletions blocks/email-settings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ registerBlockType(
/* @ts-expect-error Provided types are inaccurate to the actual plugin API. */
metadata,
{
apiVersion: 2,
edit,
title: metadata.title,
},
);
3 changes: 1 addition & 2 deletions blocks/footer/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
"category": "design",
"icon": "align-wide",
"description": "Outputs the company logo, social links, address, and footer links.",
"textdomain": "footer",
"textdomain": "wp-newsletter-builder",
"editorScript": "file:index.ts",
"editorStyle": "file:index.css",
"style": [
"file:style-index.css"
],
Expand Down
8 changes: 0 additions & 8 deletions blocks/footer/edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,6 @@ import { Spinner } from '@wordpress/components';
import { useSelect } from '@wordpress/data';
import { useEffect, useState } from '@wordpress/element';

/**
* Lets webpack process CSS, SASS or SCSS files referenced in JavaScript files.
* Those files can contain any CSS code that gets applied to the editor.
*
* @see https://www.npmjs.com/package/@wordpress/scripts#using-css
*/
import './index.scss';

interface EditProps {
attributes: {
narrow_separator: boolean,
Expand Down
12 changes: 0 additions & 12 deletions blocks/footer/index.scss

This file was deleted.

2 changes: 0 additions & 2 deletions blocks/footer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ registerBlockType(
/* @ts-expect-error Provided types are inaccurate to the actual plugin API. */
metadata,
{
apiVersion: 2,
edit,
title: metadata.title,
},
);
2 changes: 1 addition & 1 deletion blocks/header/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"category": "design",
"icon": "format-image",
"description": "Displays the header image for a newsletter",
"textdomain": "header",
"textdomain": "wp-newsletter-builder",
"editorScript": "file:index.ts",
"editorStyle": "file:index.css",
"style": [
Expand Down
2 changes: 0 additions & 2 deletions blocks/header/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ registerBlockType(
/* @ts-expect-error Provided types are inaccurate to the actual plugin API. */
metadata,
{
apiVersion: 2,
edit,
title: metadata.title,
},
);
23 changes: 23 additions & 0 deletions blocks/post-byline/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2,
"name": "wp-newsletter-builder/post-byline",
"version": "0.1.0",
"title": "Newsletter Post Byline",
"category": "design",
"icon": "admin-post",
"description": "Displays a post byline",
"textdomain": "wp-newsletter-builder",
"editorScript": "file:index.ts",
"style": [
"file:style-index.css"
],
"render": "file:render.php",
"attributes": {
"overrideByline": {
"type": "string",
"default": ""
}
},
"usesContext": ["postId"]
}
55 changes: 55 additions & 0 deletions blocks/post-byline/edit.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/* eslint-disable camelcase */
import { usePostById } from '@alleyinteractive/block-editor-tools';
import { __ } from '@wordpress/i18n';
import { RichText, useBlockProps } from '@wordpress/block-editor';
import { WP_REST_API_Post } from 'wp-types';

interface EditProps {
attributes: {
overrideByline?: string;
};
context: {
postId: number;
};
setAttributes: (attributes: {}) => void;
}

interface PostWithByline extends WP_REST_API_Post {
wp_newsletter_builder_byline: string;
}

/**
* The edit function describes the structure of your block in the context of the
* editor. This represents what the editor will render when the block is used.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-edit-save/#edit
*
* @return {WPElement} Element to render.
*/
export default function Edit({
attributes: {
overrideByline,
},
context: {
postId,
},
setAttributes,
}: EditProps) {
// @ts-ignore
const record: PostWithByline = usePostById(postId) ?? null;

let postByline = record ? record.wp_newsletter_builder_byline : __('Post Byline', 'wp-newsletter-builder');

postByline = overrideByline || postByline;

return (
<p {...useBlockProps({ className: 'post__byline' })}>
<RichText
value={postByline}
tagName="span"
onChange={(value) => setAttributes({ overrideByline: value })}
allowedFormats={[]}
/>
</p>
);
}
21 changes: 21 additions & 0 deletions blocks/post-byline/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* Block Name: Post.
*
* @package wp-newsletter-builder
*/

/**
* 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 wp_newsletter_builder_post_byline_block_init() {
// Register the block by passing the location of block.json.
register_block_type(
__DIR__
);
}
add_action( 'init', 'wp_newsletter_builder_post_byline_block_init' );
34 changes: 34 additions & 0 deletions blocks/post-byline/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Registers a new block provided a unique name and an object defining its behavior.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-registration/
*/
import { registerBlockType } from '@wordpress/blocks';

/**
* Lets webpack process CSS, SASS or SCSS files referenced in JavaScript files.
* All files containing `style` keyword are bundled together. The code used
* gets applied both to the front of your site and to the editor.
*
* @see https://www.npmjs.com/package/@wordpress/scripts#using-css
*/
import './style.scss';

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

/**
* Every block starts by registering a new block type definition.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-registration/
*/
registerBlockType(
/* @ts-expect-error Provided types are inaccurate to the actual plugin API. */
metadata,
{
edit,
},
);
29 changes: 29 additions & 0 deletions blocks/post-byline/render.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
* All of the parameters passed to the function where this file is being required are accessible in this scope:
*
* @param array $attributes The array of attributes for this block.
* @param string $content Rendered block output. ie. <InnerBlocks.Content />.
* @param WP_Block $block The instance of the WP_Block class that represents the block being rendered.
*
* @package wp-newsletter-builder
*/

use function WP_Newsletter_Builder\get_byline;

$wp_newsletter_builder_block_post = $block->context['postId'] ?? null;

$wp_newsletter_builder_block_post = get_post( $wp_newsletter_builder_block_post );
if ( empty( $wp_newsletter_builder_block_post ) || ! $wp_newsletter_builder_block_post ) {
return;
}
$wp_newsletter_builder_byline = ! empty( $attributes['overrideByline'] ) ? $attributes['overrideByline'] : get_byline( $wp_newsletter_builder_block_post );

?>
<p class="post__byline">
<?php
echo wp_kses_post(
$wp_newsletter_builder_byline
);
?>
</p>
19 changes: 19 additions & 0 deletions blocks/post-byline/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* The following styles get applied both on the front of your site
* and in the editor.
*
* Imported style.css file(s) (applies to SASS and SCSS extensions)
* get bundled into one style-index.css file that is meant to be
* used both on the front-end and in the editor.
*
* Replace them with your own styles or remove the file completely.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-metadata/#block-styles
*/

.post__byline {
font-family: Georgia,serif;
font-size: 15px;
font-weight: bold;
text-align: center;
}
24 changes: 24 additions & 0 deletions blocks/post-excerpt/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2,
"name": "wp-newsletter-builder/post-excerpt",
"version": "0.1.0",
"title": "Newsletter Post Excerpt",
"category": "design",
"icon": "admin-post",
"description": "Displays a post excerpt",
"textdomain": "wp-newsletter-builder",
"editorScript": "file:index.ts",
"editorStyle": "file:index.css",
"style": [
"file:style-index.css"
],
"render": "file:render.php",
"attributes": {
"overrideExcerpt": {
"type": "string",
"default": ""
}
},
"usesContext": ["postId"]
}
51 changes: 51 additions & 0 deletions blocks/post-excerpt/edit.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* eslint-disable camelcase */
import { usePostById } from '@alleyinteractive/block-editor-tools';
import { __ } from '@wordpress/i18n';
import { RichText, useBlockProps } from '@wordpress/block-editor';
import { WP_REST_API_Post } from 'wp-types';

interface EditProps {
attributes: {
overrideExcerpt?: string;
};
context: {
postId: number;
};
setAttributes: (attributes: {}) => void;
}

/**
* The edit function describes the structure of your block in the context of the
* editor. This represents what the editor will render when the block is used.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-edit-save/#edit
*
* @return {WPElement} Element to render.
*/
export default function Edit({
attributes: {
overrideExcerpt,
},
context: {
postId,
},
setAttributes,
}: EditProps) {
// @ts-ignore
const record: WP_REST_API_Post = usePostById(postId) ?? null;

let postExcerpt = record ? record.excerpt.rendered : __('This block will display the excerpt.', 'wp-newsletter-builder');

postExcerpt = overrideExcerpt || postExcerpt;

return (
<div {...useBlockProps({ className: 'post__dek' })}>
<RichText
value={postExcerpt}
tagName="p"
multiline={false}
onChange={(value) => setAttributes({ overrideExcerpt: value })}
/>
</div>
);
}
21 changes: 21 additions & 0 deletions blocks/post-excerpt/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* Block Name: Post.
*
* @package wp-newsletter-builder
*/

/**
* 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 wp_newsletter_builder_post_excerpt_block_init() {
// Register the block by passing the location of block.json.
register_block_type(
__DIR__
);
}
add_action( 'init', 'wp_newsletter_builder_post_excerpt_block_init' );
Loading

0 comments on commit 03f66a1

Please sign in to comment.