admin-script.js
10 months ago
dashboard-posts.js
10 months ago
front-script.js
10 months ago
jquery.notify.min.js
10 months ago
license_script.js
10 months ago
plugin-admin.js
10 months ago
template-admin.js
10 months ago
plugin-admin.js
178 lines
| 1 | document.addEventListener('DOMContentLoaded', function () { |
| 2 | async function getLatestVersionsInBatches(slugs, batchSize = 20) { |
| 3 | const versions = {}; |
| 4 | for (let i = 0; i < slugs.length; i += batchSize) { |
| 5 | const batch = slugs.slice(i, i + batchSize); |
| 6 | try { |
| 7 | const versionRequests = batch.map(slug => |
| 8 | fetch(`https://api.wordpress.org/plugins/info/1.0/${slug}.json`) |
| 9 | .then(res => res.ok ? res.json() : null) |
| 10 | .catch(() => null) |
| 11 | ); |
| 12 | const responses = await Promise.all(versionRequests); |
| 13 | responses.forEach((data, index) => { |
| 14 | const slug = batch[index]; |
| 15 | versions[slug] = data?.version || 'N/A'; |
| 16 | const versionCell = document.querySelector(`#plugin-version-${slug}`); |
| 17 | if (versionCell) versionCell.textContent = versions[slug]; |
| 18 | }); |
| 19 | } catch (error) { |
| 20 | console.error("Batch error fetching plugin versions:", error); |
| 21 | } |
| 22 | // hold |
| 23 | await new Promise(r => setTimeout(r, 100)); |
| 24 | } |
| 25 | return versions; |
| 26 | } |
| 27 | |
| 28 | const apiEndpoint = fleximp_ajax_object.apiEndpoint; |
| 29 | let isFetching = false; |
| 30 | let allPlugins = []; |
| 31 | |
| 32 | async function fetchPlugins() { |
| 33 | if (isFetching) return; |
| 34 | isFetching = true; |
| 35 | |
| 36 | try { |
| 37 | const response = await fetch(apiEndpoint, { |
| 38 | method: 'POST', |
| 39 | headers: { 'Content-Type': 'application/json' }, |
| 40 | body: JSON.stringify({ action: 'getPlugins' }) |
| 41 | }); |
| 42 | |
| 43 | const data = await response.json(); |
| 44 | if (data.success && Array.isArray(data.data)) { |
| 45 | allPlugins = data.data; |
| 46 | await displayPlugins(allPlugins); |
| 47 | |
| 48 | // Collect slugs |
| 49 | const slugs = allPlugins.map(plugin => |
| 50 | plugin.text_domain || plugin.name.toLowerCase().replace(/ /g, '-') |
| 51 | ); |
| 52 | |
| 53 | getLatestVersionsInBatches(slugs, 30).then(versions => { |
| 54 | allPlugins.forEach(plugin => { |
| 55 | const slug = plugin.text_domain || plugin.name.toLowerCase().replace(/ /g, '-'); |
| 56 | plugin.latest_version = versions[slug] || 'N/A'; |
| 57 | }); |
| 58 | }); |
| 59 | const plugins = allPlugins.map(plugin => ({ |
| 60 | slug: plugin.text_domain || plugin.name.toLowerCase().replace(/ /g, '-'), |
| 61 | main_file: plugin.main_file, |
| 62 | })); |
| 63 | fetchPluginStatuses(plugins); |
| 64 | } else { |
| 65 | console.error('Error fetching plugins:', data); |
| 66 | } |
| 67 | } catch (error) { |
| 68 | console.error('Error:', error); |
| 69 | } finally { |
| 70 | isFetching = false; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | async function displayPlugins(plugins) { |
| 75 | const pluginsListContainer = document.getElementById('plugins-list'); |
| 76 | pluginsListContainer.innerHTML = ''; |
| 77 | |
| 78 | let tableHTML = ` |
| 79 | <div class="col plugin-tbl"> |
| 80 | <table class="table table-striped table-responsive-1"> |
| 81 | <thead class="plugin-table-head"> |
| 82 | <tr class="text-center"> |
| 83 | <th>Select</th> |
| 84 | <th>Image</th> |
| 85 | <th>Plugin Name</th> |
| 86 | <th>Version</th> |
| 87 | <th class="flex-plugin-main-file">Main File</th> |
| 88 | <th>Status</th> |
| 89 | <th>Action</th> |
| 90 | </tr> |
| 91 | </thead> |
| 92 | <tbody> |
| 93 | `; |
| 94 | |
| 95 | for (const plugin of plugins) { |
| 96 | const pluginSlug = plugin.text_domain || plugin.name.toLowerCase().replace(/ /g, '-'); |
| 97 | const pluginUrl = `https://downloads.wordpress.org/plugin/${pluginSlug}.latest-stable.zip`; |
| 98 | |
| 99 | tableHTML += ` |
| 100 | <tr class="text-center"> |
| 101 | <td class="plugin-input flex-align"> |
| 102 | <input type="checkbox" class="plugin-checkbox" |
| 103 | data-plugin-name="${plugin.name}" |
| 104 | data-plugin-url="${pluginUrl}" |
| 105 | data-plugin-main-file="${plugin.main_file}"> |
| 106 | </td> |
| 107 | <td class="flex-align"><img src="${plugin.image}" alt="${plugin.name}" style="width: 50px; height: 50px;"></td> |
| 108 | <td><p class="flex-plugin-title">${plugin.name}</p></td> |
| 109 | <td id="plugin-version-${pluginSlug}"> |
| 110 | <p class="flex-plugin-title">${plugin.latest_version || "Loading…"}</p> |
| 111 | </td> |
| 112 | <td class="flex-plugin-main-file"><p class="flex-plugin-title">${plugin.main_file}</p></td> |
| 113 | <td id="plugin-status-${pluginSlug}" class="flex-plugin-title-1"><p class="flex-plugin-title">Checking status...</p></td> |
| 114 | <td class="flex-align"> |
| 115 | <button class="download-btn" |
| 116 | data-plugin-name="${plugin.name}" |
| 117 | data-plugin-url="${pluginUrl}" |
| 118 | data-plugin-main-file="${plugin.main_file}"> |
| 119 | Install/Activate |
| 120 | </button> |
| 121 | </td> |
| 122 | </tr> |
| 123 | `; |
| 124 | } |
| 125 | |
| 126 | tableHTML += ` |
| 127 | </tbody> |
| 128 | </table> |
| 129 | </div> |
| 130 | `; |
| 131 | |
| 132 | pluginsListContainer.innerHTML = tableHTML; |
| 133 | } |
| 134 | |
| 135 | document.getElementById('plugin-search').addEventListener('input', async function (event) { |
| 136 | const searchQuery = event.target.value.toLowerCase(); |
| 137 | const filteredPlugins = allPlugins.filter(plugin => { |
| 138 | return plugin.name.toLowerCase().includes(searchQuery) || plugin.version.toLowerCase().includes(searchQuery); |
| 139 | }); |
| 140 | |
| 141 | await displayPlugins(filteredPlugins); |
| 142 | const plugins = filteredPlugins.map(plugin => ({ |
| 143 | slug: plugin.text_domain || plugin.name.toLowerCase().replace(/ /g, '-'), |
| 144 | main_file: plugin.main_file, |
| 145 | })); |
| 146 | fetchPluginStatuses(plugins); |
| 147 | }); |
| 148 | |
| 149 | function fetchPluginStatuses(plugins) { |
| 150 | fetch(fleximp_ajax_object.ajax_url, { |
| 151 | method: 'POST', |
| 152 | headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, |
| 153 | body: `action=fleximp_get_plugin_statuses&security=${fleximp_ajax_object.fleximp_nonce}&plugins=${encodeURIComponent(JSON.stringify(plugins))}`, |
| 154 | }) |
| 155 | .then(response => response.json()) |
| 156 | .then(data => { |
| 157 | if (data.success) { |
| 158 | const statuses = data.data; |
| 159 | Object.keys(statuses).forEach(slug => { |
| 160 | const statusElement = document.getElementById(`plugin-status-${slug}`); |
| 161 | const buttonElement = document.querySelector(`.download-btn[data-plugin-url*="${slug}"]`); |
| 162 | if (statusElement) statusElement.textContent = statuses[slug]; |
| 163 | if (buttonElement && statuses[slug] === 'Activated') { |
| 164 | buttonElement.textContent = 'Activated'; |
| 165 | buttonElement.classList.add('activated'); |
| 166 | buttonElement.disabled = true; |
| 167 | } |
| 168 | }); |
| 169 | } else { |
| 170 | console.error('Error fetching plugin statuses:', data); |
| 171 | } |
| 172 | }) |
| 173 | .catch(error => console.error('Error fetching plugin statuses:', error)); |
| 174 | } |
| 175 | |
| 176 | fetchPlugins(); |
| 177 | }); |
| 178 |