itmar-block-packages 1.3.20 → 1.4.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/README.md +45 -1
- package/build/index.asset.php +1 -1
- package/build/index.js +2 -2
- package/package.json +1 -1
- package/src/UpdateAllPostsBlockAttributes.js +117 -0
- package/src/index.js +3 -0
- package/src/wordpressApi.js +2 -3
package/package.json
CHANGED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { useState, useRef } from "@wordpress/element";
|
|
2
|
+
import { __ } from "@wordpress/i18n";
|
|
3
|
+
import { ProgressBar, Button } from "@wordpress/components";
|
|
4
|
+
import apiFetch from "@wordpress/api-fetch";
|
|
5
|
+
|
|
6
|
+
//特定の投稿タイプの投稿に含まれる本ブロックの属性を書き換える
|
|
7
|
+
|
|
8
|
+
export default UpdateAllPostsBlockAttributes = ({
|
|
9
|
+
postType,
|
|
10
|
+
blockName,
|
|
11
|
+
newAttributes,
|
|
12
|
+
onProcessEnd,
|
|
13
|
+
onProcessCancel,
|
|
14
|
+
}) => {
|
|
15
|
+
const [progress, setProgress] = useState(0);
|
|
16
|
+
// stateではなくrefを使用してキャンセル状態を管理する
|
|
17
|
+
const cancelRef = useRef(false);
|
|
18
|
+
const updatePostBlockAttributes = async () => {
|
|
19
|
+
try {
|
|
20
|
+
// REST APIを使ってすべての投稿を取得(投稿タイプを指定)
|
|
21
|
+
const path = `/wp/v2/${postType}?per_page=100&context=edit`;
|
|
22
|
+
const posts = await apiFetch({ path });
|
|
23
|
+
|
|
24
|
+
let allCount = posts.length;
|
|
25
|
+
let processCount = 0;
|
|
26
|
+
// キャンセルフラグをリセット
|
|
27
|
+
cancelRef.current = false;
|
|
28
|
+
|
|
29
|
+
for (const post of posts) {
|
|
30
|
+
if (!post.content || !post.content.raw) {
|
|
31
|
+
console.warn(`Post ID ${post.id} does not contain raw content.`);
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
// ループの各回でキャンセルがリクエストされているかチェック
|
|
35
|
+
if (cancelRef.current) {
|
|
36
|
+
console.log("処理がキャンセルされました。");
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const content = post.content.raw;
|
|
41
|
+
const blocks = parse(content);
|
|
42
|
+
// 特定のブロックの属性を更新
|
|
43
|
+
const updatedBlocks = blocks.map((block) => {
|
|
44
|
+
if (block.name === blockName) {
|
|
45
|
+
// 属性をマージして更新
|
|
46
|
+
block.attributes = {
|
|
47
|
+
...block.attributes,
|
|
48
|
+
...newAttributes,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
return block;
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
// 更新後のブロックをシリアライズ
|
|
55
|
+
const updatedContent = serialize(updatedBlocks);
|
|
56
|
+
|
|
57
|
+
// REST APIを使って投稿を更新
|
|
58
|
+
const updateResponse = await fetch(
|
|
59
|
+
`/wp-json/wp/v2/${postType}/${post.id}`,
|
|
60
|
+
{
|
|
61
|
+
method: "POST",
|
|
62
|
+
headers: {
|
|
63
|
+
"Content-Type": "application/json",
|
|
64
|
+
"X-WP-Nonce": wpApiSettings.nonce, // WP Nonceを指定
|
|
65
|
+
},
|
|
66
|
+
body: JSON.stringify({
|
|
67
|
+
content: updatedContent,
|
|
68
|
+
}),
|
|
69
|
+
}
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
if (!updateResponse.ok) {
|
|
73
|
+
console.error(
|
|
74
|
+
`Failed to update post ID ${post.id}: ${updateResponse.statusText}`
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
//カウンターセットとプログレスバーの処理
|
|
78
|
+
processCount++;
|
|
79
|
+
setProgress(Math.round((processCount / allCount) * 100));
|
|
80
|
+
}
|
|
81
|
+
//終了処理
|
|
82
|
+
onProcessEnd();
|
|
83
|
+
} catch (error) {
|
|
84
|
+
console.error("Error updating block attributes:", error);
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
return (
|
|
88
|
+
<>
|
|
89
|
+
<ProgressBar value={progress} className="markdown_copy_progress" />
|
|
90
|
+
<p>{progress}%</p>
|
|
91
|
+
<div style={{ width: "fit-content", margin: "20px auto 0" }}>
|
|
92
|
+
<Button
|
|
93
|
+
variant="primary"
|
|
94
|
+
onClick={updatePostBlockAttributes}
|
|
95
|
+
disabled={progress > 0 && progress < 100}
|
|
96
|
+
>
|
|
97
|
+
{__("Start Process", "markdown-block")}
|
|
98
|
+
</Button>
|
|
99
|
+
<Button
|
|
100
|
+
variant="secondary"
|
|
101
|
+
onClick={() => {
|
|
102
|
+
if (progress === 0 || cancelRef.current) {
|
|
103
|
+
onProcessEnd(); // 処理が始まる前ならすぐに終了処理を実行
|
|
104
|
+
} else {
|
|
105
|
+
// キャンセルフラグを更新(refならすぐに反映される)
|
|
106
|
+
cancelRef.current = true;
|
|
107
|
+
onProcessCancel(); //親コンポーネントでキャンセル処理
|
|
108
|
+
}
|
|
109
|
+
}}
|
|
110
|
+
style={{ marginLeft: "10px" }}
|
|
111
|
+
>
|
|
112
|
+
{__("Cancel", "markdown-block")}
|
|
113
|
+
</Button>
|
|
114
|
+
</div>
|
|
115
|
+
</>
|
|
116
|
+
);
|
|
117
|
+
};
|
package/src/index.js
CHANGED
package/src/wordpressApi.js
CHANGED
|
@@ -37,7 +37,6 @@ const SelectControl = (props) => {
|
|
|
37
37
|
}, [fetchOptions]);
|
|
38
38
|
|
|
39
39
|
const selectedInfo = options.find((info) => info.slug === selectedSlug);
|
|
40
|
-
|
|
41
40
|
return (
|
|
42
41
|
<ComboboxControl
|
|
43
42
|
label={label}
|
|
@@ -69,6 +68,7 @@ const ChoiceControl = (props) => {
|
|
|
69
68
|
const fetchData = async () => {
|
|
70
69
|
try {
|
|
71
70
|
const fetchChoices = await fetchFunction(selectedSlug);
|
|
71
|
+
|
|
72
72
|
setChoices(fetchChoices);
|
|
73
73
|
} catch (error) {
|
|
74
74
|
console.error("Error fetching data:", error.message);
|
|
@@ -274,7 +274,7 @@ const ChoiceControl = (props) => {
|
|
|
274
274
|
}}
|
|
275
275
|
/>
|
|
276
276
|
)}
|
|
277
|
-
{choice.featured_media && (
|
|
277
|
+
{(choice.featured_media || choice.featured_media === 0) && (
|
|
278
278
|
<ToggleControl
|
|
279
279
|
className="field_choice"
|
|
280
280
|
label={__("Featured Image", "block-collections")}
|
|
@@ -495,7 +495,6 @@ export const restFieldes = async (rest_base) => {
|
|
|
495
495
|
const response = await apiFetch({
|
|
496
496
|
path: `/wp/v2/${rest_base}?_fields=${fieldsParam}&per_page=1&order=desc`,
|
|
497
497
|
});
|
|
498
|
-
|
|
499
498
|
return response;
|
|
500
499
|
};
|
|
501
500
|
|