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

Add more taxonomy options to the post navigation link #48912

Merged
merged 30 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
2c86378
Add 3 options to the post navigation link
carolinan Mar 7, 2023
aefb961
Update core__post-navigation-link.json
carolinan Mar 7, 2023
ec7dc9d
Support custom post types and custom taxonomies
carolinan Mar 8, 2023
393afa8
fix CS issues
carolinan Mar 8, 2023
3341d55
Update index.php
carolinan Mar 10, 2023
7836c63
Update index.php
carolinan Mar 14, 2023
1a11e8a
Update edit.js
carolinan Jun 17, 2023
8976803
Merge branch 'trunk' into update/post-navigation-link
carolinan Jul 11, 2023
de19789
Merge branch 'trunk' into update/post-navigation-link
carolinan Jul 12, 2023
449a40a
try to fix spacing in core-blocks.md
carolinan Jul 12, 2023
c40a61d
Merge branch 'trunk' into update/post-navigation-link
carolinan Jul 13, 2023
156bde0
Merge branch 'trunk' into update/post-navigation-link
carolinan Jul 21, 2023
d5cd379
Remove the taxonomy filter toggle and update the PHP conditions.
carolinan Jul 21, 2023
9279206
Update core__post-navigation-link.json
carolinan Jul 21, 2023
5ae72a6
Merge branch 'trunk' into update/post-navigation-link
carolinan Aug 11, 2023
9e215f0
Update help text about excluding terms
carolinan Aug 11, 2023
80f5a95
Update help text again.
carolinan Aug 11, 2023
a39c201
Merge branch 'trunk' into update/post-navigation-link
carolinan Sep 12, 2023
074518f
Merge branch 'trunk' into update/post-navigation-link
carolinan Sep 26, 2023
969aeaf
Merge branch 'trunk' into update/post-navigation-link
carolinan Sep 28, 2023
4c40be4
Merge branch 'trunk' into update/post-navigation-link
carolinan Oct 25, 2023
f2ac603
Merge branch 'trunk' into update/post-navigation-link
carolinan Oct 31, 2023
bf28bda
Remove the exclude terms feature, move filter to advanced panel
carolinan Nov 6, 2023
a0f1da9
Try to fix CS issues
carolinan Nov 6, 2023
6f73362
Update docs
carolinan Nov 6, 2023
c94aecb
Try to fix CS issues
carolinan Nov 6, 2023
54a58bb
Use context to get the postType
carolinan Nov 6, 2023
b2106f9
Merge branch 'trunk' into update/post-navigation-link
carolinan Nov 23, 2023
9c14221
Merge branch 'trunk' into update/post-navigation-link
carolinan Jan 2, 2024
261926a
Merge branch 'trunk' into update/post-navigation-link
carolinan Jan 3, 2024
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
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,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, excludedTerms, inSameTerm, label, linkLabel, showTitle, taxonomy, textAlign, type

## Post Template

Expand Down
10 changes: 10 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,6 +28,16 @@
"arrow": {
"type": "string",
"default": "none"
},
"excludedTerms": {
"type": "string"
mikachan marked this conversation as resolved.
Show resolved Hide resolved
},
"inSameTerm": {
"type": "boolean"
},
"taxonomy": {
"type": "string",
"default": ""
}
},
"supports": {
Expand Down
79 changes: 78 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,7 +10,9 @@ import {
__experimentalToggleGroupControl as ToggleGroupControl,
__experimentalToggleGroupControlOption as ToggleGroupControlOption,
ToggleControl,
SelectControl,
PanelBody,
__experimentalInputControl as InputControl,
} from '@wordpress/components';
import {
InspectorControls,
Expand All @@ -20,9 +22,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 },
attributes: {
type,
label,
showTitle,
textAlign,
linkLabel,
arrow,
taxonomy,
excludedTerms,
},
setAttributes,
} ) {
const isNext = type === 'next';
Expand All @@ -47,6 +60,44 @@ export default function PostNavigationLinkEdit( {
[ `has-text-align-${ textAlign }` ]: textAlign,
} ),
} );

// We need to know the post type in order to get all supported taxonomies.
const postType = useSelect(
// FIXME: @wordpress/block-library should not depend on @wordpress/editor.
carolinan marked this conversation as resolved.
Show resolved Hide resolved
// Blocks can be loaded into a *non-post* block editor.
// eslint-disable-next-line @wordpress/data-no-store-string-literals
( select ) => select( 'core/editor' ).getCurrentPostType(),
[]
);
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' )
.map( ( item ) => {
return {
value: item.slug,
label: item.name,
};
} );

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

return (
<>
<InspectorControls>
Expand Down Expand Up @@ -113,6 +164,32 @@ export default function PostNavigationLinkEdit( {
/>
</ToggleGroupControl>
</PanelBody>
<PanelBody title={ __( 'Filters' ) }>
<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 categories.'
) }
/>
<InputControl
label={ __( 'Add terms to exclude' ) }
value={ excludedTerms }
onChange={ ( value ) =>
setAttributes( { excludedTerms: value } )
}
help={ __(
'Exclude terms from any taxonomy by entering the term name, slug, or ID. Separate multiple terms with a comma. Posts with excluded terms will not be linked to, even if they have other matching terms.'
) }
/>
</PanelBody>
</InspectorControls>
<BlockControls>
<AlignmentToolbar
Expand Down
34 changes: 33 additions & 1 deletion packages/block-library/src/post-navigation-link/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,42 @@ function render_block_core_post_navigation_link( $attributes, $content ) {
}
}

$in_same_term = isset( $attributes['inSameTerm'] ) ? $attributes['inSameTerm'] : false;
$taxonomy = isset( $attributes['taxonomy'] ) && $in_same_term ? $attributes['taxonomy'] : '';
$excluded_terms = '';

// The dynamic portion of the function name, `$navigation_type`,
// refers to the type of adjacency, 'next' or 'previous'.
$get_link_function = "get_{$navigation_type}_post_link";
$content = $get_link_function( $format, $link );

// Get the term id of each excluded term.
if ( isset( $attributes['excludedTerms'] ) && ! empty( $attributes['excludedTerms'] ) ) {
$excluded_terms = explode( ',', sanitize_text_field( $attributes['excludedTerms'] ) );
// Get the taxonomies of the current post type.
$taxonomies = get_object_taxonomies( get_post_type() );
// The user may have entered the taxonomy slug, name or id.
$types = array( 'slug', 'name', 'term_id', 'id', 'ID', 'term_taxonomy_id' );
$result = array();
foreach ( $excluded_terms as $term ) {
$term = trim( $term );
foreach ( $taxonomies as $tax ) {
foreach ( $types as $type ) {
$termid = get_term_by( $type, $term, $tax );
if ( $termid ) {
$result[] = $termid->term_taxonomy_id;
}
}
}
}
$excluded_terms = array_unique( $result );
}

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

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
Loading