Skip to content

Add a Display filter to allow folks to filter the visibility of QuickPost#16

Merged
aurooba merged 6 commits intomainfrom
enhance/filter-visibility
Mar 6, 2022
Merged

Add a Display filter to allow folks to filter the visibility of QuickPost#16
aurooba merged 6 commits intomainfrom
enhance/filter-visibility

Conversation

@aurooba
Copy link
Member

@aurooba aurooba commented Mar 6, 2022

This PR closes #4, and adds a new prop to the QuickPostButton component called visibility, as well as wrapping that component in WithFilters to allow filtering with addFilter by other people.

The updated README includes an example on how to change when QuickPost is visible using the filter.

import { addFilter } from "@wordpress/hooks";
import { useSelect } from "@wordpress/data";
import { store as coreStore } from "@wordpress/core-data";

function onlyAdmins(FilteredComponent) {
	const adminUser = () => {
		const { isAdmin } = useSelect((select) => {
			const { canUser } = select(coreStore);
			// check if current user can create users, because only admins can do that
			const isAdmin = canUser("create", "users"); // returns undefined, true, or false
			return {
				isAdmin: isAdmin,
			};
		});
		return isAdmin;
	};
	return (props) => (
		<>
			<FilteredComponent {...props} visibility={adminUser()} />
		</>
	);
}

addFilter(
	"QuickPost.Display",
	"my-plugin/only-admins-quickpost",
	onlyAdmins
);

@aurooba
Copy link
Member Author

aurooba commented Mar 6, 2022

Changed the filter to be called QuickPost.Display so that it's easy to add more filters later if needed in the same format.

@aurooba aurooba merged commit 7d4c3e5 into main Mar 6, 2022
@aurooba aurooba deleted the enhance/filter-visibility branch February 26, 2023 23:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add filter so other people can filter the button's visibility

1 participant