Skip to content

Commit

Permalink
Add more taxonomy options to the post navigation link (#48912)
Browse files Browse the repository at this point in the history
* Add 3 options to the post navigation link

* Update core__post-navigation-link.json

* Support custom post types and custom taxonomies

* fix CS issues

* Update index.php

* Update index.php

* Update edit.js

* try to fix spacing in core-blocks.md

* Remove the taxonomy filter toggle and update the PHP conditions.

* Update core__post-navigation-link.json

* Update help text about excluding terms

* Update help text again.

* Remove the exclude terms feature, move filter to advanced panel

* Try to fix CS issues

* Update docs

* Try to fix CS issues

* Use context to get the postType
  • Loading branch information
carolinan committed Jan 12, 2024
1 parent 7e74cc8 commit f461b17
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 6 deletions.
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ Displays the next or previous post link that is adjacent to the current post. ([
- **Name:** core/post-navigation-link
- **Category:** theme
- **Supports:** color (background, link, text), typography (fontSize, lineHeight), ~~html~~, ~~reusable~~
- **Attributes:** arrow, label, linkLabel, showTitle, textAlign, type
- **Attributes:** arrow, inSameTerm, label, linkLabel, showTitle, taxonomy, textAlign, type

## Post Template

Expand Down
8 changes: 8 additions & 0 deletions packages/block-library/src/post-navigation-link/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,16 @@
"arrow": {
"type": "string",
"default": "none"
},
"inSameTerm": {
"type": "boolean"
},
"taxonomy": {
"type": "string",
"default": ""
}
},
"usesContext": [ "postType" ],
"supports": {
"reusable": false,
"html": false,
Expand Down
64 changes: 63 additions & 1 deletion packages/block-library/src/post-navigation-link/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
__experimentalToggleGroupControl as ToggleGroupControl,
__experimentalToggleGroupControlOption as ToggleGroupControlOption,
ToggleControl,
SelectControl,
PanelBody,
} from '@wordpress/components';
import {
Expand All @@ -20,9 +21,20 @@ import {
useBlockProps,
} from '@wordpress/block-editor';
import { __, _x } from '@wordpress/i18n';
import { useSelect } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';

export default function PostNavigationLinkEdit( {
attributes: { type, label, showTitle, textAlign, linkLabel, arrow },
context: { postType },
attributes: {
type,
label,
showTitle,
textAlign,
linkLabel,
arrow,
taxonomy,
},
setAttributes,
} ) {
const isNext = type === 'next';
Expand All @@ -47,6 +59,40 @@ export default function PostNavigationLinkEdit( {
[ `has-text-align-${ textAlign }` ]: textAlign,
} ),
} );

const taxonomies = useSelect(
( select ) => {
const { getTaxonomies } = select( coreStore );
const filteredTaxonomies = getTaxonomies( {
type: postType,
per_page: -1,
context: 'view',
} );
return filteredTaxonomies;
},
[ postType ]
);
const getTaxonomyOptions = () => {
const selectOption = {
label: __( 'Unfiltered' ),
value: '',
};
const taxonomyOptions = ( taxonomies ?? [] )
.filter(
( tax ) =>
tax.slug !== 'nav_menu' &&
tax.slug !== 'wp_pattern_category'
)
.map( ( item ) => {
return {
value: item.slug,
label: item.name,
};
} );

return [ selectOption, ...taxonomyOptions ];
};

return (
<>
<InspectorControls>
Expand Down Expand Up @@ -114,6 +160,22 @@ export default function PostNavigationLinkEdit( {
</ToggleGroupControl>
</PanelBody>
</InspectorControls>
<InspectorControls group="advanced">
<SelectControl
label={ __( 'Filter by taxonomy' ) }
value={ taxonomy }
options={ getTaxonomyOptions() }
onChange={ ( value ) =>
setAttributes( {
taxonomy: value,
inSameTerm: value === '' ? false : true,
} )
}
help={ __(
'Only link to posts that have the same taxonomy terms as the current post. For example the same tags or categories.'
) }
/>
</InspectorControls>
<BlockControls>
<AlignmentToolbar
value={ textAlign }
Expand Down
20 changes: 17 additions & 3 deletions packages/block-library/src/post-navigation-link/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,24 @@ function render_block_core_post_navigation_link( $attributes, $content ) {
}
}

// The dynamic portion of the function name, `$navigation_type`,
// refers to the type of adjacency, 'next' or 'previous'.
$in_same_term = isset( $attributes['inSameTerm'] ) ? $attributes['inSameTerm'] : false;
$taxonomy = isset( $attributes['taxonomy'] ) && $in_same_term ? $attributes['taxonomy'] : '';

/**
* The dynamic portion of the function name, `$navigation_type`,
* Refers to the type of adjacency, 'next' or 'previous'.
*
* @See https://developer.wordpress.org/reference/functions/get_previous_post_link/
* @See https://developer.wordpress.org/reference/functions/get_next_post_link/
*/
$get_link_function = "get_{$navigation_type}_post_link";
$content = $get_link_function( $format, $link );

if ( $in_same_term ) {
$content = $get_link_function( $format, $link, $in_same_term, '', $taxonomy );
} else {
$content = $get_link_function( $format, $link );
}

return sprintf(
'<div %1$s>%2$s</div>',
$wrapper_attributes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"type": "next",
"showTitle": false,
"linkLabel": false,
"arrow": "none"
"arrow": "none",
"taxonomy": ""
},
"innerBlocks": []
}
Expand Down

0 comments on commit f461b17

Please sign in to comment.