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

[MOOSE-52] Terms Block #81

Merged
merged 22 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
fcac9e4
add terms block
GeoffDusome Aug 4, 2023
8bf5a7a
fall back to first term if yoast fails
GeoffDusome Aug 4, 2023
1ab516b
use custom icon
GeoffDusome Aug 4, 2023
3444cea
Merge branch 'main' into feature/terms-block
GeoffDusome Aug 11, 2023
cf66410
update terms block category / icon
GeoffDusome Aug 11, 2023
88a0bd5
Merge branch 'main' into feature/terms-block
GeoffDusome Aug 11, 2023
f2185f1
Merge branch 'main' into feature/terms-block
GeoffDusome Aug 17, 2023
7e4e6e9
update block apiVersion to 3
GeoffDusome Aug 17, 2023
c82035d
Merge branch 'main' into feature/terms-block
GeoffDusome Aug 30, 2023
dd7952a
move no results text to an HTML comment
GeoffDusome Aug 30, 2023
871ae98
block styles; primary terms
GeoffDusome Sep 7, 2023
a5ddcce
Merge branch 'main' into feature/terms-block
GeoffDusome Sep 13, 2023
738ed27
fix issue with get_post_meta rank_math_primary term call
GeoffDusome Sep 13, 2023
8cd09f4
Update terms php
ChrisMKindred Sep 25, 2023
62dd68f
Force boolean return on display_as_links
ChrisMKindred Sep 25, 2023
164ff43
Early exit if no terms match
ChrisMKindred Sep 25, 2023
7555194
Remove extra span
ChrisMKindred Sep 25, 2023
e7047e9
Merge pull request #106 from moderntribe/terms-block-php-update
ChrisMKindred Sep 25, 2023
78f43e4
Fixes phpstan error for undefined type.
ChrisMKindred Sep 25, 2023
92ef389
Merge remote-tracking branch 'origin' into feature/terms-block
ChrisMKindred Sep 25, 2023
6c896fe
only pull taxonomies from current post type
GeoffDusome Sep 27, 2023
009e678
update svg; move category/tag code to mixins; add utility classes
GeoffDusome Sep 27, 2023
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
1 change: 1 addition & 0 deletions wp-content/plugins/core/src/Blocks/Blocks_Definer.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public function define(): array {
'tribe/post-type-name',
'tribe/post-permalink',
'tribe/query-results-count',
'tribe/terms',
] ),

self::EXTENDED => DI\add( [
Expand Down
11 changes: 11 additions & 0 deletions wp-content/themes/core/assets/pcss/typography/_mixins.pcss
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,17 @@
text-decoration: none;
}

/* -------------------------------------------------------------------------
* Tag
* ------------------------------------------------------------------------- */

@define-mixin t-tag {
font-family: var(--wp--preset--font-family--roboto);
font-size: var(--wp--preset--font-size--10);
line-height: 1.6;
font-weight: 700;
}

/* -------------------------------------------------------------------------
* Caption
* Defined in theme.json but used in other places (post permalink block)
Expand Down
36 changes: 36 additions & 0 deletions wp-content/themes/core/blocks/tribe/terms/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 3,
"name": "tribe/terms",
"version": "0.1.0",
"title": "Terms",
"category": "theme",
"description": "Displays chosen taxonomy terms in different formats",
"supports": {
"html": false,
"align": true,
"spacing": {
"margin": true,
"padding": true
}
},
"attributes": {
"taxonomyToUse": {
"type": "string"
},
"onlyPrimaryTerm": {
"type": "boolean"
},
"hasLinks": {
"type": "boolean"
},
"termStyle": {
"type": "string"
}
},
"textdomain": "tribe",
"editorScript": "file:./index.js",
"editorStyle": "file:./index.css",
"style": "file:./style-index.css",
"render": "file:./render.php"
}
127 changes: 127 additions & 0 deletions wp-content/themes/core/blocks/tribe/terms/edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/**
* 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/reference-guides/packages/packages-block-editor/#useblockprops
*/
import { useBlockProps, InspectorControls } from '@wordpress/block-editor';
import {
PanelBody,
RadioControl,
SelectControl,
ToggleControl,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useSelect } from '@wordpress/data';

/**
* Server-side rendering of the block in the editor view
*
* @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-server-side-render/
*/
import ServerSideRender from '@wordpress/server-side-render';

/**
* 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.
*
* @ignore
*
* @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, setAttributes, isSelected } ) {
const blockProps = useBlockProps();
const { taxonomyToUse, onlyPrimaryTerm, hasLinks, termStyle } = attributes;

const taxonomies = useSelect( ( select ) =>
select( 'core' ).getTaxonomies( { per_page: -1 } )
);

return (
<div { ...blockProps }>
<ServerSideRender block="tribe/terms" attributes={ attributes } />
{ isSelected && (
<InspectorControls>
<PanelBody title={ __( 'Block Settings', 'tribe' ) }>
<SelectControl
label={ __( 'Taxonomy', 'tribe' ) }
value={ taxonomyToUse ?? '' }
help={ __(
'The taxonomy to pull terms from.',
'tribe'
) }
onChange={ ( newValue ) => {
setAttributes( {
taxonomyToUse: newValue,
} );
} }
options={ taxonomies.map( ( taxonomy ) => {
return {
label: taxonomy.name,
value: taxonomy.slug,
};
} ) }
/>
<ToggleControl
label={ __(
'Should only grab the primary term',
'tribe'
) }
help={ __(
'Default functionality is to grab all terms in the taxonomy.',
'tribe'
) }
checked={ !! onlyPrimaryTerm }
onChange={ ( newValue ) =>
setAttributes( {
onlyPrimaryTerm: newValue,
} )
}
/>
<ToggleControl
label={ __(
'Terms should display as links',
'tribe'
) }
help={ __(
'Default functionality is that terms do not display as links.',
'tribe'
) }
checked={ !! hasLinks }
onChange={ ( newValue ) =>
setAttributes( {
hasLinks: newValue,
} )
}
/>
<RadioControl
label={ __( 'Term Style', 'tribe' ) }
help={ __(
'The format the terms should display.',
'tribe'
) }
options={ [
{
label: __( 'Default', 'tribe' ),
value: 'default',
},
{
label: __( 'Pills', 'tribe' ),
value: 'pills',
},
] }
selected={ termStyle ?? 'default' }
onChange={ ( newValue ) =>
setAttributes( {
termStyle: newValue,
} )
}
/>
GeoffDusome marked this conversation as resolved.
Show resolved Hide resolved
</PanelBody>
</InspectorControls>
) }
</div>
);
}
66 changes: 66 additions & 0 deletions wp-content/themes/core/blocks/tribe/terms/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* 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.pcss';

/**
* 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( metadata.name, {
...metadata,

icon: {
src: (
<svg
width="200"
height="200"
viewBox="0 0 200 200"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
fillRule="evenodd"
clipRule="evenodd"
d="M157.458 4C161.876 4 165.458 7.58172 165.458 12V77.7122C165.458 79.8339 164.615 81.8688 163.115 83.369L86.4713 160.012C84.2927 162.191 81.7062 163.92 78.8597 165.099C76.0131 166.278 72.9621 166.885 69.881 166.885C66.7999 166.885 63.7489 166.278 60.9023 165.099C58.0557 163.92 55.4692 162.191 53.2906 160.012L9.44543 116.167C7.26669 113.989 5.53841 111.402 4.35927 108.556C3.18013 105.709 2.57324 102.658 2.57324 99.5769C2.57324 96.4958 3.18013 93.4449 4.35927 90.5983C5.53841 87.7517 7.26669 85.1652 9.44544 82.9866L86.0889 6.34315C87.5892 4.84285 89.624 4 91.7458 4L157.458 4ZM149.458 20L95.0594 20L20.759 94.3005C20.0661 94.9933 19.5163 95.8161 19.1413 96.7214C18.7663 97.6267 18.5732 98.597 18.5732 99.577C18.5732 100.557 18.7663 101.527 19.1413 102.433C19.5163 103.338 20.0659 104.16 20.7589 104.853L64.6045 148.699C65.2974 149.392 66.1201 149.942 67.0254 150.317C67.9307 150.692 68.9011 150.885 69.881 150.885C70.8609 150.885 71.8312 150.692 72.7365 150.317C73.6418 149.942 74.4644 149.392 75.1573 148.699L149.458 74.3985V20Z"
fill="currentcolor"
/>
<path
fillRule="evenodd"
clipRule="evenodd"
d="M189 34.8835C193.418 34.8835 197 38.4652 197 42.8835V93.0196C197 95.1413 196.157 97.1762 194.657 98.6764L99.7925 193.54C96.6683 196.665 91.603 196.665 88.4788 193.54C85.3546 190.416 85.3546 185.351 88.4788 182.227L181 89.7059V42.8835C181 38.4652 184.581 34.8835 189 34.8835Z"
fill="currentcolor"
/>
<path
fillRule="evenodd"
clipRule="evenodd"
d="M119 53.5C119 50.4624 116.537 48 113.5 48C110.462 48 108 50.4624 108 53.5C108 56.5376 110.462 59 113.5 59C116.537 59 119 56.5376 119 53.5ZM113.5 32C125.374 32 135 41.6259 135 53.5C135 65.3741 125.374 75 113.5 75C101.626 75 91.9997 65.3741 91.9997 53.5C91.9997 41.6259 101.626 32 113.5 32Z"
fill="currentcolor"
/>
</svg>
GeoffDusome marked this conversation as resolved.
Show resolved Hide resolved
),
},

/**
* @see ./edit.js
*/
edit: Edit,
} );
85 changes: 85 additions & 0 deletions wp-content/themes/core/blocks/tribe/terms/render.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php declare(strict_types=1);

// get block settings (attributes)
$taxonomy = $attributes['taxonomyToUse'] ?? 'category';
$only_primay_term = $attributes['onlyPrimaryTerm'] ?? false;
$has_links = $attributes['hasLinks'] ?? false;
$style = $attributes['termStyle'] ?? 'default';
$terms = false;
$primary_term = false;

/**
* If we should only display the primary term:
* - If we have yoast we can get the primary term that way
* - Otherwise, we'll have to grab the first term in the list
*
* If we should display all terms:
* - Grab all terms for the taxonomy
*/
if ( $taxonomy === 'category' && $only_primay_term && class_exists( 'WPSEO_Primary_Term' ) ) {
$wpseo_primary_term = new WPSEO_Primary_Term( $taxonomy, get_the_ID() );
$wpseo_primary_term = $wpseo_primary_term->get_primary_term();
if ( $wpseo_primary_term ) {
$primary_term = get_term( $wpseo_primary_term );
} else {
$terms = get_the_terms( get_the_ID(), $taxonomy );
$primary_term = $terms ? $terms[0] : false;
}
} elseif ( $only_primay_term ) {
$terms = get_the_terms( get_the_ID(), $taxonomy );
$primary_term = $terms ? $terms[0] : false;
} else {
$terms = get_the_terms( get_the_ID(), $taxonomy );
}

if ( ! $terms && ! $primary_term && ! is_admin() ) {
return;
}
?>
GeoffDusome marked this conversation as resolved.
Show resolved Hide resolved

<div <?php echo get_block_wrapper_attributes(); ?>>
ChrisMKindred marked this conversation as resolved.
Show resolved Hide resolved
<?php if ( $primary_term ) {
// display only primary term
// displays differently if we should have links or not
echo $has_links ? sprintf(
'<span class="%s"><a href="%s" target="%s" class="%s">%s</a></span>',
esc_attr( "wp-block-tribe-terms__term is-style-$style" ),
esc_url( get_term_link( $primary_term ) ),
esc_attr( '_self' ),
esc_attr( 'wp-block-tribe-terms__link' ),
esc_html( $primary_term->name )
) : sprintf(
'<span class="%s"><span class="%s">%s</span></span>',
esc_attr( "wp-block-tribe-terms__term is-style-$style" ),
esc_attr( 'wp-block-tribe-terms__link' ),
esc_html( $primary_term->name )
);
} elseif ( $terms ) {
// display terms in a list
echo sprintf(
'<ul class="%s">',
esc_attr( 'wp-block-tribe-terms__list' )
);
// loop through terms to create a list
// displays differently if we should have links or not
foreach ( $terms as $term ) {
echo $has_links ? sprintf(
'<li class="%s"><a href="%s" target="%s" class="%s">%s</a></li>',
esc_attr( "wp-block-tribe-terms__term is-style-$style" ),
esc_url( get_term_link( $term ) ),
esc_attr( '_self' ),
esc_attr( 'wp-block-tribe-terms__link' ),
esc_html( $term->name )
) : sprintf(
'<li class="%s"><span class="%s">%s</span></li>',
esc_attr( "wp-block-tribe-terms__term is-style-$style" ),
esc_attr( 'wp-block-tribe-terms__link' ),
esc_html( $term->name )
);
}
echo '</ul>';
} else {
// display if no terms are available
echo '<!-- Terms block: No terms to list. -->';
} ?>
</div>
Loading