| @@ -1,7 +1,11 @@ | ||
| 1 | 1 | import { recordPluginActivity } from '@shared/api/DataApi'; |
| 2 | 2 | import { digest } from '@shared/api/digest'; |
| 3 | 3 | import { enableAutoUpdate } from '@shared/api/wp'; |
| 4 | +import { | |
| 5 | + failedDependencies, | |
| 6 | + processWithSecondPass, | |
| 7 | +} from '@shared/lib/patterns'; | |
| 4 | 8 | import apiFetch from '@wordpress/api-fetch'; |
| 5 | 9 | import { addQueryArgs } from '@wordpress/url'; |
| 6 | 10 | |
| 7 | 11 | export const getActivePlugins = () => |
| @@ -58,9 +62,13 @@ | ||
| 58 | 62 | return await fn(); |
| 59 | 63 | } catch (error) { |
| 60 | 64 | digest({ |
| 61 | 65 | error, |
| 62 | - details: { source: 'auto-launch', caller: 'installPlugin' }, | |
| 66 | + details: { | |
| 67 | + source: 'auto-launch', | |
| 68 | + caller: 'installPlugin', | |
| 69 | + plugin: slug, | |
| 70 | + }, | |
| 63 | 71 | }); |
| 64 | 72 | return null; |
| 65 | 73 | } |
| 66 | 74 | } |
| @@ -93,21 +101,23 @@ | ||
| 93 | 101 | |
| 94 | 102 | try { |
| 95 | 103 | await fn(slug); |
| 96 | 104 | return true; |
| 97 | - } catch (_) { | |
| 98 | - try { | |
| 99 | - // try once more but get the slug first | |
| 100 | - const { plugin } = await getPlugin(slug); | |
| 101 | - await fn(plugin); | |
| 102 | - return true; | |
| 103 | - } catch (error) { | |
| 105 | + } catch (error) { | |
| 106 | + const report = (e) => { | |
| 104 | 107 | digest({ |
| 105 | - error, | |
| 106 | - details: { source: 'auto-launch', caller: 'activatePlugin' }, | |
| 108 | + error: e, | |
| 109 | + details: { | |
| 110 | + source: 'auto-launch', | |
| 111 | + caller: 'activatePlugin', | |
| 112 | + plugin: slug, | |
| 113 | + }, | |
| 107 | 114 | }); |
| 108 | 115 | return false; |
| 109 | - } | |
| 116 | + }; | |
| 117 | + const found = await getPlugin(slug).catch(() => undefined); | |
| 118 | + if (!found?.plugin) return report(error); | |
| 119 | + return fn(found.plugin).then(() => true, report); | |
| 110 | 120 | } |
| 111 | 121 | }; |
| 112 | 122 | |
| 113 | 123 | // Exact match, not substring: woocommerce-payments would mask a missing woocommerce. |
| @@ -159,8 +169,26 @@ | ||
| 159 | 169 | }, |
| 160 | 170 | }); |
| 161 | 171 | }; |
| 162 | 172 | |
| 173 | +export const reportFailedDependencies = (pages) => { | |
| 174 | + const failed = [ | |
| 175 | + ...new Set(pages.flatMap(({ patterns }) => failedDependencies(patterns))), | |
| 176 | + ]; | |
| 177 | + if (!failed.length) return; | |
| 178 | + | |
| 179 | + digest({ | |
| 180 | + error: { | |
| 181 | + message: `Patterns left static after a failed dependency: ${failed.join(', ')}`, | |
| 182 | + }, | |
| 183 | + details: { | |
| 184 | + source: 'auto-launch', | |
| 185 | + caller: 'reportFailedDependencies', | |
| 186 | + failed, | |
| 187 | + }, | |
| 188 | + }); | |
| 189 | +}; | |
| 190 | + | |
| 163 | 191 | // Currently this only processes patterns with placeholders |
| 164 | 192 | // by swapping out the placeholders with the actual code |
| 165 | 193 | // returns the patterns as blocks with the placeholders replaced |
| 166 | 194 | export const replacePlaceholderPatterns = async (patterns) => { |
| @@ -195,16 +223,9 @@ | ||
| 195 | 223 | source: 'auto-launch', |
| 196 | 224 | }); |
| 197 | 225 | } |
| 198 | 226 | |
| 199 | - try { | |
| 200 | - return await processPlaceholders(patterns); | |
| 201 | - } catch (_e) { | |
| 202 | - // Try one more time (plugins installed may not be fully loaded) | |
| 203 | - return await processPlaceholders(patterns) | |
| 204 | - // If this fails, just return the original patterns | |
| 205 | - .catch(() => patterns); | |
| 206 | - } | |
| 227 | + return await processWithSecondPass(processPlaceholders, patterns); | |
| 207 | 228 | }; |
| 208 | 229 | |
| 209 | 230 | // This endpoint installs pattern dependencies from PHP, outside the queue. |
| 210 | 231 | export const processPlaceholders = async (patterns) => { |