PluginProbe
Extendify / 2.2.0
Extendify v2.2.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 2.2.0, at src/Shared/api/wp.js

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