PluginProbe
Elementor Website Builder – more than just a page builder / 3.8.0
Elementor Website Builder – more than just a page builder v3.8.0
4.3.0-beta3 4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 All 452 releases
elementor / app / modules / import-export / assets / js / hooks / use-plugins.js

use-plugins.js in Elementor Website Builder – more than just a page builder 3.8.0, at app/modules/import-export/assets/js/hooks/use-plugins.js

129 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { useState, useEffect, useRef } from 'react';
2
3 export const PLUGINS_RESPONSE_MAP = Object.freeze( {
4 INITIAL: 'initial',
5 SUCCESS: 'success',
6 ERROR: 'error',
7 } );
8
9 export const PLUGIN_STATUS_MAP = Object.freeze( {
10 ACTIVE: 'active',
11 MULTISITE_ACTIVE: 'network-active',
12 INACTIVE: 'inactive',
13 NOT_INSTALLED: 'Not Installed',
14 } );
15
16 const baseEndpoint = elementorCommon.config.urls.rest + 'wp/v2/plugins/',
17 getInitialState = () => ( {
18 status: PLUGINS_RESPONSE_MAP.INITIAL,
19 data: null,
20 } );
21
22 export default function usePlugins() {
23 const [ response, setResponse ] = useState( () => getInitialState() ),
24 allowResponseUpdate = useRef( true ),
25 fetchRest = ( { body, method, endpoint = '' } ) => {
26 const data = {
27 method,
28 headers: {
29 'Content-Type': 'application/json; charset=utf-8',
30 'X-WP-Nonce': wpApiSettings.nonce,
31 },
32 };
33
34 if ( body ) {
35 data.body = JSON.stringify( body );
36 }
37
38 if ( response.data ) {
39 reset();
40 }
41
42 return new Promise( ( resolve, reject ) => {
43 fetch( baseEndpoint + endpoint, data )
44 .then( ( res ) => res.json() )
45 .then( ( res ) => {
46 if ( allowResponseUpdate.current ) {
47 setResponse( {
48 status: PLUGINS_RESPONSE_MAP.SUCCESS,
49 data: res,
50 } );
51 }
52
53 resolve( res );
54 } )
55 .catch( ( error ) => {
56 setResponse( {
57 status: PLUGINS_RESPONSE_MAP.ERROR,
58 data: error,
59 } );
60
61 reject( error );
62 } );
63 } );
64 },
65 fetchData = ( slug ) => {
66 return fetchRest( {
67 method: 'GET',
68 endpoint: slug,
69 } );
70 },
71 install = ( slug ) => {
72 slug = slug.split( '/' )[ 0 ];
73
74 return fetchRest( {
75 method: 'POST',
76 body: {
77 slug,
78 },
79 } );
80 },
81 activate = ( slug ) => {
82 return fetchRest( {
83 endpoint: slug,
84 method: 'PUT',
85 body: {
86 status: PLUGIN_STATUS_MAP.ACTIVE,
87 },
88 } );
89 },
90 deactivate = ( slug ) => {
91 return fetchRest( {
92 endpoint: slug,
93 method: 'PUT',
94 body: {
95 status: PLUGIN_STATUS_MAP.INACTIVE,
96 },
97 } );
98 },
99 remove = ( slug ) => {
100 return fetchRest( {
101 endpoint: slug,
102 method: 'DELETE',
103 } );
104 },
105 reset = () => setResponse( getInitialState() );
106
107 // On load.
108 useEffect( () => {
109 fetchData();
110
111 // Cleanup on destroy.
112 return () => {
113 allowResponseUpdate.current = false;
114 };
115 }, [] );
116
117 return {
118 response,
119 pluginsActions: {
120 fetch: fetchData,
121 install,
122 activate,
123 deactivate,
124 remove,
125 reset,
126 },
127 };
128 }
129