Skip to content

Commit

Permalink
Merge branch 'main' into feature/issue-629/allow-custom-post-lookup-f…
Browse files Browse the repository at this point in the history
…unction
  • Loading branch information
mogmarsh authored Apr 9, 2024
2 parents cadaa9a + 225d7ba commit 621037b
Show file tree
Hide file tree
Showing 14 changed files with 110 additions and 10 deletions.
6 changes: 6 additions & 0 deletions .changeset/grumpy-planes-work.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@alleyinteractive/block-editor-tools": minor
"alley-scripts-demo-plugin": minor
---

Introducing new hooks
7 changes: 5 additions & 2 deletions packages/block-editor-tools/src/hooks/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
export { default as useCurrentPostId } from './use-current-post-id';
export { default as useCurrentTheme } from './use-current-theme';
export { default as useDebounce } from './use-debounce';
export { default as useHasInnerBlocks } from './use-has-inner-blocks';
export { default as useInnerBlocksIndex } from './use-inner-block-index';
export { default as useInnerBlocks } from './use-inner-blocks';
export { default as useInnerBlocksAttributes } from './use-inner-blocks-attributes';
export { default as useInnerBlocksCount } from './use-inner-blocks-count';
export { default as useInnerBlocksIndex } from './use-inner-block-index';
export { default as useMedia } from './use-media';
export { default as useOption } from './use-option';
export { default as useParentBlock } from './use-parent-block';
export { default as useParentBlockAttributes } from './use-parent-block-attributes';
export { default as usePost } from './use-post';
export { default as useCurrentPostId } from './use-current-post-id';
export { default as usePostById } from './use-post-by-id';
export { default as usePostMeta } from './use-post-meta';
export { default as usePostMetaValue } from './use-post-meta-value';
export { default as usePosts } from './use-posts';
export { default as usePreviewLink } from './use-preview-link';
export { default as useTerm } from './use-term';
export { default as useTerms } from './use-terms';
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useSelect } from '@wordpress/data';

/**
* Gets the current post ID.
* Get the current post ID.
*
* @returns {?number} Returns the ID of the post currently being edited,
* or null if the post has not yet been saved or the redux store
Expand Down
15 changes: 15 additions & 0 deletions packages/block-editor-tools/src/hooks/use-current-theme/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Custom Hooks: useCurrentTheme

A custom React hook to retrieve the current active theme.

## Usage

```jsx
const MyBlock = () => {
const theme = useCurrentTheme();

if (theme) {
...
}
};
```
15 changes: 15 additions & 0 deletions packages/block-editor-tools/src/hooks/use-current-theme/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { store } from '@wordpress/core-data';
import { useSelect } from '@wordpress/data';

/**
* Get current theme.
*
* @returns {?string} Returns the current active theme,
* or null if the redux store is not initialized.
*/
const useCurrentTheme = () => useSelect((select) => {
const editorStore = select(store);
return editorStore ? editorStore.getCurrentTheme()?.stylesheet : null;
}, []);

export default useCurrentTheme;
3 changes: 2 additions & 1 deletion packages/block-editor-tools/src/hooks/use-media/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { store } from '@wordpress/core-data';
import { useSelect } from '@wordpress/data';

/**
Expand All @@ -7,7 +8,7 @@ import { useSelect } from '@wordpress/data';
* @returns {object} Media data.
*/
const useMedia = (mediaId) => useSelect(
(select) => select('core').getMedia(mediaId),
(select) => select(store).getMedia(mediaId),
[mediaId],
);

Expand Down
3 changes: 2 additions & 1 deletion packages/block-editor-tools/src/hooks/use-post/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { store } from '@wordpress/core-data';
import { useSelect } from '@wordpress/data';

/**
Expand All @@ -10,7 +11,7 @@ import { useSelect } from '@wordpress/data';
* and the returned post object.
*/
const usePost = (postId, postType = 'post', options = { context: 'view' }) => useSelect(
(select) => select('core').getEntityRecord('postType', postType, postId, options),
(select) => select(store).getEntityRecord('postType', postType, postId, options),
[postId, postType],
);

Expand Down
3 changes: 2 additions & 1 deletion packages/block-editor-tools/src/hooks/use-posts/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { store } from '@wordpress/core-data';
import { useSelect } from '@wordpress/data';

/**
Expand All @@ -9,7 +10,7 @@ import { useSelect } from '@wordpress/data';
* and an array of returned post objects.
*/
const usePosts = (postIds, postType = 'post') => useSelect((select) => {
const { getEntityRecords } = select('core');
const { getEntityRecords } = select(store);
return getEntityRecords(
'postType',
postType,
Expand Down
11 changes: 11 additions & 0 deletions packages/block-editor-tools/src/hooks/use-preview-link/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Custom Hooks: usePreviewLink

A custom React hook to retrieve a post preview link.

## Usage

```jsx
const MyBlock = () => {
const previewLink = usePreviewLink();
};
```
11 changes: 11 additions & 0 deletions packages/block-editor-tools/src/hooks/use-preview-link/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { store } from '@wordpress/core-data';
import { useSelect } from '@wordpress/data';

/**
* Get preview link.
*
* @returns string
*/
const usePreviewLink = () => useSelect((select) => select(store).getEditedPostPreviewLink(), []);

export default usePreviewLink;
18 changes: 18 additions & 0 deletions packages/block-editor-tools/src/hooks/use-term/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Custom Hooks: useTerm

A custom React hook to retrieve term data given a term ID and taxonomy.

## Usage

```jsx
const MyBlock = ({
termId,
taxonomy
}) => {
const termObject = useTerm(termId, taxonomy);

if (termObject) {
...
}
};
```
18 changes: 18 additions & 0 deletions packages/block-editor-tools/src/hooks/use-term/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { store } from '@wordpress/core-data';
import { useSelect } from '@wordpress/data';

/**
* Gets term data for a specific term given its ID and taxonomy.
*
* @param {int} termId The ID for the term to return.
* @param {string} taxonomy Optional. The taxonomy name. Defaults to 'category'.
* @param {object} options Optional object containing parameters to pass to getEntityRecord.
* @returns {object} An object containing a hasResolved property
* and the returned term object.
*/
const useTerm = (termId, taxonomy = 'category', options = { context: 'view' }) => useSelect(
(select) => select(store).getEntityRecord('taxonomy', taxonomy, termId, options),
[termId, taxonomy],
);

export default useTerm;
4 changes: 2 additions & 2 deletions plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
* Version: 0.1.0
* Author: Alley
* Author URI: https://alley.com/
* Requires at least: 5.9
* Tested up to: 5.9
* Requires at least: 6.4
* Tested up to: 6.4
*
* @package Alley_Scripts
*/
Expand Down
4 changes: 2 additions & 2 deletions plugin/plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
* Version: 0.1.0
* Author: Alley
* Author URI: https://github.com/alleyinteractive/alley-scripts
* Requires at least: 5.9
* Tested up to: 6.2
* Requires at least: 6.4
* Tested up to: 6.4
*
* Text Domain: alley-scripts-demo-plugin
* Domain Path: /languages/
Expand Down

0 comments on commit 621037b

Please sign in to comment.