itmar-block-packages 1.4.2 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "itmar-block-packages",
3
- "version": "1.4.2",
3
+ "version": "1.5.0",
4
4
  "description": "We have put together a package of common React components used for WordPress custom blocks.",
5
5
  "main": "build/index.js",
6
6
  "scripts": {
@@ -68,24 +68,17 @@ export default function UpdateAllPostsBlockAttributes({
68
68
  const updatedContent = serialize(updatedBlocks);
69
69
 
70
70
  // REST APIを使って投稿を更新
71
- const updateResponse = await fetch(
72
- `/wp-json/wp/v2/${postType}/${post.id}`,
73
- {
71
+ try {
72
+ await apiFetch({
73
+ path: `/wp/v2/${postType}/${post.id}`,
74
74
  method: "POST",
75
- headers: {
76
- "Content-Type": "application/json",
77
- "X-WP-Nonce": wpApiSettings.nonce, // WP Nonceを指定
78
- },
79
- body: JSON.stringify({
80
- content: updatedContent,
81
- }),
75
+ data: { content: updatedContent },
76
+ });
77
+ } catch (error) {
78
+ console.error(`Failed to update post ID ${post.id}:`, error.message);
79
+ if (error.data) {
80
+ console.error("Error details:", error.data);
82
81
  }
83
- );
84
-
85
- if (!updateResponse.ok) {
86
- console.error(
87
- `Failed to update post ID ${post.id}: ${updateResponse.statusText}`
88
- );
89
82
  }
90
83
  //カウンターセットとプログレスバーの処理
91
84
  processCount++;
package/src/blockStore.js CHANGED
@@ -1,3 +1,47 @@
1
+ import { useSelect } from "@wordpress/data";
2
+ import { store as blockEditorStore } from "@wordpress/block-editor";
3
+
4
+ export const useTargetBlocks = (
5
+ clientId,
6
+ blockName,
7
+ attributeFilter = null,
8
+ includeNested = false
9
+ ) => {
10
+ const targetblocks = useSelect(
11
+ (select) => {
12
+ const { getBlockRootClientId, getBlock } = select(blockEditorStore);
13
+
14
+ const parentId = getBlockRootClientId(clientId); // 自分の親の clientId を取得
15
+ if (!parentId) return attributeFilter ? null : [];
16
+
17
+ const parentBlock = getBlock(parentId);
18
+ if (!parentBlock) return attributeFilter ? null : [];
19
+
20
+ const siblingBlocks = includeNested
21
+ ? flattenBlocks(parentBlock.innerBlocks || []) //ネストされたブロックも含んで検索
22
+ : parentBlock.innerBlocks || []; //兄弟ブロックのみ
23
+
24
+ const filtered = siblingBlocks.filter(
25
+ (block) => block.name === blockName && block.clientId !== clientId
26
+ );
27
+
28
+ if (attributeFilter) {
29
+ return (
30
+ filtered.find((block) => {
31
+ return Object.entries(attributeFilter).every(
32
+ ([key, value]) => block.attributes[key] === value
33
+ );
34
+ }) || null
35
+ );
36
+ }
37
+
38
+ return filtered;
39
+ },
40
+ [clientId, blockName, JSON.stringify(attributeFilter), includeNested]
41
+ );
42
+ return targetblocks;
43
+ };
44
+
1
45
  //ネストしたブロックを平坦化
2
46
  export const flattenBlocks = (blocks) => {
3
47
  return blocks.reduce((acc, block) => {
package/src/index.js CHANGED
@@ -89,7 +89,7 @@ export {
89
89
  } from "./DateElm";
90
90
 
91
91
  //インナーブロック関連の関数
92
- export { flattenBlocks } from "./blockStore";
92
+ export { flattenBlocks, useTargetBlocks } from "./blockStore";
93
93
 
94
94
  //特定の投稿タイプの投稿に含まれる本ブロックの属性を書き換える
95
95
  export { default as UpdateAllPostsBlockAttributes } from "./UpdateAllPostsBlockAttributes";