PluginProbe
Extendify / 3.1.0
Extendify v3.1.0
3.2.0 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.6 3.0.5 3.0.4 trunk 0.1.0 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.2.0 0.3.0 0.3.1 0.4.0 0.5.0 0.6.0 0.7.0 All 126 releases
extendify / src / Shared / api / wp.js

wp.js in Extendify 3.1.0, at src/Shared/api/wp.js

156 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { AI_HOST } from '@constants';
2 import apiFetch from '@wordpress/api-fetch';
3 import { addQueryArgs } from '@wordpress/url';
4
5 export const getPlugin = async (slug) => {
6 const response = await apiFetch({
7 path: addQueryArgs('/wp/v2/plugins', { search: slug }),
8 });
9
10 const plugin = response?.[0];
11
12 if (!plugin) throw new Error('Plugin not found');
13
14 return plugin;
15 };
16
17 export const getAllPlugins = async () => {
18 const response = await apiFetch({
19 path: '/wp/v2/plugins',
20 });
21
22 if (!response) {
23 throw new Error('Failed to fetch installed plugins');
24 }
25
26 return response;
27 };
28
29 export const enableAutoUpdate = async (plugin) => {
30 if (!plugin) return;
31 try {
32 await apiFetch({
33 path: '/extendify/v1/shared/enable-auto-update',
34 method: 'POST',
35 data: { plugin },
36 });
37 } catch (_e) {
38 // Best-effort: the install already succeeded.
39 }
40 };
41
42 export const installPlugin = async (slug) => {
43 const plugin = await apiFetch({
44 path: '/wp/v2/plugins',
45 method: 'POST',
46 data: {
47 slug,
48 },
49 });
50
51 await enableAutoUpdate(plugin?.plugin);
52
53 return plugin;
54 };
55
56 export const activatePlugin = async (slug) => {
57 const plugin = await getPlugin(slug);
58
59 return await apiFetch({
60 path: `/wp/v2/plugins/${plugin.plugin}`,
61 method: 'POST',
62 data: {
63 status: 'active',
64 },
65 });
66 };
67
68 export const loadImage = (img) => {
69 return new Promise((resolve, reject) => {
70 img.onload = () => resolve(img);
71 img.onerror = (e) => reject(e);
72 });
73 };
74
75 export const importImage = async (imageUrl, metadata = {}) => {
76 const image = new Image();
77 image.src = imageUrl;
78 image.crossOrigin = 'anonymous';
79 await loadImage(image);
80
81 const canvas = document.createElement('canvas');
82 canvas.width = image.width;
83 canvas.height = image.height;
84
85 const ctx = canvas.getContext('2d');
86 if (!ctx) return;
87 ctx.drawImage(image, 0, 0);
88
89 const blob = await new Promise((resolve) => {
90 canvas.toBlob((blob) => {
91 blob && resolve(blob);
92 }, 'image/jpeg');
93 });
94
95 const formData = new FormData();
96 formData.append('file', new File([blob], metadata.filename));
97 formData.append('alt_text', metadata.alt ?? '');
98 formData.append('caption', metadata.caption ?? '');
99 formData.append('status', 'publish');
100
101 return await apiFetch({
102 path: 'wp/v2/media',
103 method: 'POST',
104 body: formData,
105 });
106 };
107
108 export const importImageServer = async (src, metadata = {}) => {
109 const formData = new FormData();
110 formData.append('source', src);
111 // Fallback doesn't support custom file_name
112 formData.append('alt_text', metadata.alt ?? '');
113 formData.append('caption', metadata.caption ?? '');
114
115 return await apiFetch({
116 path: '/extendify/v1/draft/upload-image',
117 method: 'POST',
118 body: formData,
119 });
120 };
121
122 export const downloadImage = async (
123 id,
124 src,
125 source,
126 unsplashId,
127 metadata = { alt: '', caption: '' },
128 ) => {
129 let image;
130 if (unsplashId) {
131 await downloadPing(id, source, { unsplashId });
132 }
133 try {
134 image = await importImage(src, {
135 alt: metadata.alt,
136 filename: 'image.jpg',
137 caption: metadata.caption,
138 });
139 } catch (_e) {
140 image = await importImageServer(src, {
141 alt: metadata.alt,
142 filename: 'image.jpg',
143 caption: metadata.caption,
144 });
145 }
146
147 return image;
148 };
149
150 export const downloadPing = (id, source, details = {}) =>
151 fetch(`${AI_HOST}/api/draft/image/download`, {
152 method: 'POST',
153 headers: { 'Content-Type': 'application/json' },
154 body: JSON.stringify({ id, source, ...details }),
155 });
156