Commit 5adb698d authored by Filipe B. de Souza's avatar Filipe B. de Souza
Browse files

create new block co-authores meta

parent e25e1cf8
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2,
"version": "0.1.0",
"name": "hacklab-blocks/co-authors-meta",
"title": "Co-authors Meta",
"category": "theme",
"icon": "admin-post",
"description": "Add metadata of the co-authors plugin.",
"keywords": ["postlist","publicações"],
"textdomain": "hacklab-blocks",
"editorScript": "file:../../build/js/co-authors-meta-index/co-authors-meta-index.js",
"attributes": {
"prefix": {
"type": "string"
}
},
"usesContext": [ "postId", "postType" ],
"supports": {
"html": false
}
}
\ No newline at end of file
<?php
namespace hacklabBlocks;
/**
* Server-side rendering of the `hacklab-blocks/post-meta` block.
*
* @package WordPress
*/
/**
* Renders the `hacklab-blocks/post-meta` 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 filtered post terms for the current post wrapped inside "a" tags.
*/
function co_authors_meta_callback($attributes, $content, $block)
{
if (!isset($block->context['postId'])) {
return '';
}
if( !class_exists( 'CoAuthors_Plus' ) ){
return '';
}
$post_id = $block->context['postId'];
$coauthors = get_coauthors($post_id);
$prefix = isset( $attributes['prefix'] ) ? $attributes['prefix'] : '';
$html = '';
$html .= '<span class="co-authors-meta-block">';
if($prefix){
$html .= '<span class="prefix">';
$html .= esc_html($prefix);
$html .= '</span>';
}
foreach($coauthors as $coauthor){
$html .= $coauthor->display_name;
$html .= ' ';
}
$html .= '</span>';
return $html;
}
/**
* External dependencies
*/
import classnames from 'classnames';
import { unescape } from 'lodash';
import ServerSideRender from '@wordpress/server-side-render';
/**
* WordPress dependencies
*/
import {
InspectorControls,
BlockControls,
useBlockProps,
} from '@wordpress/block-editor';
import {
TextControl,
Panel,
PanelBody,
FormTokenField
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
export default function Edit(props) {
const attributes = props.attributes;
const context = props.context;
const setAttributes = props.setAttributes;
const { prefix } = attributes;
const { postId, postType } = context;
const hasPost = postId && postType;
const blockProps = useBlockProps({
className: classnames({
}),
});
return (
<div { ...useBlockProps() }>
<InspectorControls key="setting">
<Panel>
<PanelBody title={__('Configurações do bloco')}>
<TextControl
autoComplete="off"
label={__('Prefixo')}
value={prefix || ''}
onChange={(nextValue) => {
setAttributes({ prefix: nextValue });
}}
help={__('Adicione um prefixo antes dos autores.')}
/>
</PanelBody>
</Panel>
</InspectorControls>
<div className="configBlock">
<header><p>CoAuthorsMeta</p></header>
</div>
</div>
);
}
\ No newline at end of file
/**
* The following styles get applied inside the editor only.
*
* Replace them with your own styles or remove the file completely.
*/
.wp-block-create-block-gutenpride {
border: 1px dotted #f00;
}
\ No newline at end of file
/**
* Registers a new block provided a unique name and an object defining its behavior.
*
* @see https://developer.wordpress.org/block-editor/developers/block-api/#registering-a-block
*/
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. All other files
* get applied to the editor only.
*
* @see https://www.npmjs.com/package/@wordpress/scripts#using-css
*/
import './style.scss';
import './editor.scss';
/**
* Internal dependencies
*/
import Edit from './edit';
import save from './save';
/**
* Every block starts by registering a new block type definition.
*
* @see https://developer.wordpress.org/block-editor/developers/block-api/#registering-a-block
*/
registerBlockType( 'hacklab-blocks/co-authors-meta', {
/**
* @see ./edit.js
*/
edit: Edit,
/**
* @see ./save.js
*/
save,
} );
\ No newline at end of file
/**
* React hook that is used to mark the block wrapper element.
* It provides all the necessary props like the class name.
*
* @see https://developer.wordpress.org/block-editor/packages/packages-block-editor/#useBlockProps
*/
import { useBlockProps } from '@wordpress/block-editor';
/**
* The save function defines the way in which the different attributes should
* be combined into the final markup, which is then serialized by the block
* editor into `post_content`.
*
* @see https://developer.wordpress.org/block-editor/developers/block-api/block-edit-save/#save
*
* @param {Object} props Properties passed to the function.
* @param {Object} props.attributes Available block attributes.
* @return {WPElement} Element to render.
*/
export default function save( { attributes } ) {
const blockProps = useBlockProps.save();
return <div { ...blockProps }>{ attributes.message }</div>;
}
.wp-block-post-terms__separator {
white-space: pre-wrap;
}
\ No newline at end of file
......@@ -62,6 +62,9 @@ function hacklab_blocks_init() {
'post-meta' => array(
'render_callback' => 'hacklabBlocks\\post_meta_callback',
),
'co-authors-meta' => array(
'render_callback' => 'hacklabBlocks\\co_authors_meta_callback',
),
'filter-by-taxonomy' => array(
'render_callback' => 'hacklabBlocks\\filter_by_taxonomy_callback',
),
......@@ -78,7 +81,7 @@ function hacklab_blocks_init() {
foreach ($blocos_ativos as $block_name => $block_args ) {
$args = array();
if ($block_args){
include $plugin_folder . '/blocks/' . $block_name . '/' . $block_name . '.php';
include $plugin_folder . 'blocks/' . $block_name . '/' . $block_name . '.php';
foreach ($block_args as $arg => $value) {
$args[$arg] = $value;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment