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

Blocks: Tag Cloud #7875

Merged
merged 19 commits into from
Feb 15, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Add tag cloud block
  • Loading branch information
jahvi committed Dec 15, 2018
commit e5a56254309cd513d80f9892f34c585c28fd7e7a
8 changes: 3 additions & 5 deletions packages/block-library/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import * as template from './template';
import * as textColumns from './text-columns';
import * as verse from './verse';
import * as video from './video';
import * as tagCloud from './tag-cloud';

import * as classic from './classic';

Expand Down Expand Up @@ -94,11 +95,8 @@ export const registerCoreBlocks = () => {
textColumns,
verse,
video,
].forEach( ( block ) => {
if ( ! block ) {
return;
}
gziolo marked this conversation as resolved.
Show resolved Hide resolved
const { name, settings } = block;
tagCloud,
].forEach( ( { name, settings } ) => {
registerBlockType( name, settings );
} );

Expand Down
123 changes: 123 additions & 0 deletions packages/block-library/src/tag-cloud/edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/**
* External dependencies
*/
import { map, filter } from 'lodash';

/**
* WordPress dependencies
*/
import { Component, Fragment } from '@wordpress/element';
import {
PanelBody,
Placeholder,
ToggleControl,
SelectControl,
ServerSideRender,
} from '@wordpress/components';
import { withSelect } from '@wordpress/data';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import { InspectorControls } from '@wordpress/editor';
gziolo marked this conversation as resolved.
Show resolved Hide resolved

class TagCloudEdit extends Component {
constructor() {
super( ...arguments );

this.setTaxonomy = this.setTaxonomy.bind( this );
this.toggleShowTagCounts = this.toggleShowTagCounts.bind( this );
}

getTaxonomies() {
const taxonomies = filter( this.props.taxonomies, 'show_cloud' );

return map( taxonomies, ( taxonomy ) => {
return {
value: taxonomy.slug,
label: taxonomy.name,
};
} );
}

setTaxonomy( taxonomy ) {
const { setAttributes } = this.props;

setAttributes( { taxonomy } );
}

toggleShowTagCounts() {
const { attributes, setAttributes } = this.props;
const { showTagCounts } = attributes;

setAttributes( { showTagCounts: ! showTagCounts } );
}

render() {
const { attributes } = this.props;
const { taxonomy, showTagCounts } = attributes;
const taxonomies = this.getTaxonomies();
const options = [
{
label: __( '- Select -' ),
value: '',
},
...taxonomies,
];
gziolo marked this conversation as resolved.
Show resolved Hide resolved

const inspectorControls = (
<InspectorControls>
<PanelBody title={ __( 'Tag Cloud Settings' ) }>
<SelectControl
label={ __( 'Taxonomy' ) }
options={ options }
value={ taxonomy }
onChange={ this.setTaxonomy }
/>
<ToggleControl
label={ __( 'Show tag counts' ) }
checked={ showTagCounts }
onChange={ this.toggleShowTagCounts }
/>
</PanelBody>
</InspectorControls>
);

if ( ! taxonomy ) {
return (
<Fragment>
{ inspectorControls }
<Placeholder
key="placeholder"
icon="tag"
label={ __( 'Tag Cloud' ) }
instructions={ __( 'Select a Taxonomy' ) }
>
<SelectControl
options={ options }
onChange={ this.setTaxonomy }
/>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This select element misses a properly associated <label> element. Should use a prop
label={ __( 'Select a Taxonomy' ) }
And the instructions prop should be removed from the placeholder.

</Placeholder>
</Fragment>
);
}

return (
<Fragment>
{ inspectorControls }
<ServerSideRender
key="tag-cloud"
block="core/tag-cloud"
attributes={ attributes }
/>
</Fragment>
);
}
}

export default withSelect( ( select ) => {
return {
taxonomies: select( 'core' ).getTaxonomies(),
};
} )( TagCloudEdit );
13 changes: 13 additions & 0 deletions packages/block-library/src/tag-cloud/editor.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.gutenberg .wp-block-tag-cloud {
gziolo marked this conversation as resolved.
Show resolved Hide resolved
a {
display: inline-block;
margin-right: 5px;
}

span {
display: inline-block;
margin-left: 5px;
color: $dark-gray-100;
text-decoration: none;
}
}
33 changes: 33 additions & 0 deletions packages/block-library/src/tag-cloud/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import './editor.scss';
gziolo marked this conversation as resolved.
Show resolved Hide resolved
import edit from './edit';

export const name = 'core/tag-cloud';

export const settings = {
title: __( 'Tag Cloud' ),

description: __( 'A cloud of your most used tags.' ),

icon: 'tag',

category: 'widgets',

supports: {
html: false,
align: true,
},

edit,

save() {
return null;
},
};
58 changes: 58 additions & 0 deletions packages/block-library/src/tag-cloud/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/**
* Server-side rendering of the `core/tag-cloud` block.
*
* @package gutenberg
gziolo marked this conversation as resolved.
Show resolved Hide resolved
*/

/**
* Renders the `core/tag-cloud` block on server.
*
* @param array $attributes The block attributes.
*
* @return string Returns the tag cloud for selected taxonomy.
*/
function render_block_core_tag_cloud( $attributes ) {
$class = "wp-block-tag-cloud align{$attributes['align']}";
gziolo marked this conversation as resolved.
Show resolved Hide resolved
$wrapper_markup = '<p class="%1$s">%2$s</p>';

$args = array(
'echo' => false,
'taxonomy' => $attributes['taxonomy'],
gziolo marked this conversation as resolved.
Show resolved Hide resolved
'show_count' => ! empty( $attributes['showTagCounts'] ),
gziolo marked this conversation as resolved.
Show resolved Hide resolved
);

$tag_cloud = wp_tag_cloud( $args );

$block_content = sprintf(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: this variable is obsolete, you can return here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be resolved now 👍🏽

$wrapper_markup,
gziolo marked this conversation as resolved.
Show resolved Hide resolved
esc_attr( $class ),
$tag_cloud
);

return $block_content;
}

/**
* Registers the `core/tag-cloud` block on server.
*/
function register_block_core_tag_cloud() {
register_block_type( 'core/tag-cloud', array(
'attributes' => array(
'taxonomy' => array(
'type' => 'string',
),
'showTagCounts' => array(
'type' => 'boolean',
'default' => false,
),
'align' => array(
'type' => 'string',
'default' => 'center',
),
),
'render_callback' => 'render_block_core_tag_cloud',
) );
}

add_action( 'init', 'register_block_core_tag_cloud' );
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- wp:tag-cloud {"taxonomy":"category"} /-->
14 changes: 14 additions & 0 deletions test/integration/full-content/fixtures/core__tag-cloud.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"clientId": "_clientId_0",
"name": "core/tag-cloud",
"isValid": true,
"attributes": {
"taxonomy": "category",
"showTagCounts": false,
"align": "center"
},
"innerBlocks": [],
"originalContent": ""
}
]
10 changes: 10 additions & 0 deletions test/integration/full-content/fixtures/core__tag-cloud.parsed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"blockName": "core/tag-cloud",
"attrs": {
"taxonomy": "category"
},
"innerBlocks": [],
"innerHTML": ""
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- wp:tag-cloud {"taxonomy":"category"} /-->
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- wp:tag-cloud {"taxonomy":"category","showTagCounts":true} /-->
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"clientId": "_clientId_0",
"name": "core/tag-cloud",
"isValid": true,
"attributes": {
"taxonomy": "category",
"showTagCounts": true,
"align": "center"
},
"innerBlocks": [],
"originalContent": ""
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[
{
"blockName": "core/tag-cloud",
"attrs": {
"taxonomy": "category",
"showTagCounts": true
},
"innerBlocks": [],
"innerHTML": ""
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- wp:tag-cloud {"taxonomy":"category","showTagCounts":true} /-->
2 changes: 1 addition & 1 deletion test/integration/full-content/server-registered.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"core\/block":{"attributes":{"ref":{"type":"number"}}},"core\/latest-comments":{"attributes":{"className":{"type":"string"},"commentsToShow":{"type":"number","default":5,"minimum":1,"maximum":100},"displayAvatar":{"type":"boolean","default":true},"displayDate":{"type":"boolean","default":true},"displayExcerpt":{"type":"boolean","default":true},"align":{"type":"string","enum":["center","left","right","wide","full",""]}}},"core\/archives":{"attributes":{"align":{"type":"string"},"className":{"type":"string"},"displayAsDropdown":{"type":"boolean","default":false},"showPostCounts":{"type":"boolean","default":false}}},"core\/latest-posts":{"attributes":{"categories":{"type":"string"},"className":{"type":"string"},"postsToShow":{"type":"number","default":5},"displayPostDate":{"type":"boolean","default":false},"postLayout":{"type":"string","default":"list"},"columns":{"type":"number","default":3},"align":{"type":"string"},"order":{"type":"string","default":"desc"},"orderBy":{"type":"string","default":"date"}}}}
{"core\/block":{"attributes":{"ref":{"type":"number"}}},"core\/latest-comments":{"attributes":{"className":{"type":"string"},"commentsToShow":{"type":"number","default":5,"minimum":1,"maximum":100},"displayAvatar":{"type":"boolean","default":true},"displayDate":{"type":"boolean","default":true},"displayExcerpt":{"type":"boolean","default":true},"align":{"type":"string","enum":["center","left","right","wide","full",""]}}},"core\/archives":{"attributes":{"align":{"type":"string"},"className":{"type":"string"},"displayAsDropdown":{"type":"boolean","default":false},"showPostCounts":{"type":"boolean","default":false}}},"core\/latest-posts":{"attributes":{"categories":{"type":"string"},"className":{"type":"string"},"postsToShow":{"type":"number","default":5},"displayPostDate":{"type":"boolean","default":false},"postLayout":{"type":"string","default":"list"},"columns":{"type":"number","default":3},"align":{"type":"string"},"order":{"type":"string","default":"desc"},"orderBy":{"type":"string","default":"date"}}},"core\/tag-cloud":{"attributes":{"taxonomy":{"type":"string"},"showTagCounts":{"type":"boolean","default":false},"align":{"type":"string","default":"center"}}}}