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 all 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 @@ -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'
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The pattern category shows in the option in Site Editor unless it is removed here.

If the "New admin views" Gutenberg experiment is enabled, data views also show in the option. I did not hide it since it is still experimental.

Copy link
Contributor

Choose a reason for hiding this comment

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

Is this conditional statement really necessary? I hesitate to hardcode specific taxonomy slugs into blocks. For example, in a query loop block, no such condition exists.

export const useTaxonomies = ( postType ) => {
const taxonomies = useSelect(
( select ) => {
const { getTaxonomies } = select( coreStore );
const filteredTaxonomies = getTaxonomies( {
type: postType,
per_page: -1,
context: 'view',
} );
return filteredTaxonomies;
},
[ postType ]
);
return taxonomies;
};

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I simply deleted the filter and nothing changed in the interface. I am not sure what changed since November but neither the nav menu, pattern categories or the admin views are showing up now. Custom taxonomies, for standard and for custom post types are showing correctly as well.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't know about the navigation menu and admin view, but it seems that public has been changed to false in the 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,
} )
Comment on lines +169 to +172
Copy link
Contributor

Choose a reason for hiding this comment

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

The value of isSameTerm depends only on whether the value of taxonomy is empty. So, can this attribute be removed and not determined during dynamic rendering?

}
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
Loading