| 1 |
import { PluginSearchBanner } from '@recommendations/components/PluginSearchBanner'; |
| 2 |
import domReady from '@wordpress/dom-ready'; |
| 3 |
import '@recommendations/recommendations.css'; |
| 4 |
import { render } from '@shared/lib/dom'; |
| 5 |
|
| 6 |
domReady(() => { |
| 7 |
// Check the current page to know what elements we need to insert. |
| 8 |
const currentUrl = new URL(window.location.href); |
| 9 |
const isPluginInstall = currentUrl.pathname.endsWith('plugin-install.php'); |
| 10 |
const isNewPost = |
| 11 |
currentUrl.pathname.endsWith('post-new.php') && |
| 12 |
currentUrl.searchParams.get('post_type') !== 'page'; |
| 13 |
const isNewPage = |
| 14 |
currentUrl.pathname.endsWith('post-new.php') && |
| 15 |
currentUrl.searchParams.get('post_type') === 'page'; |
| 16 |
|
| 17 |
// Returns early if we are not in a page that shows recommendations. |
| 18 |
if (!isPluginInstall && !isNewPost && !isNewPage) { |
| 19 |
return; |
| 20 |
} |
| 21 |
|
| 22 |
if (isPluginInstall) { |
| 23 |
// The element `plugin-filter` wraps the search results, |
| 24 |
const pluginResults = document.getElementById('plugin-filter'); |
| 25 |
|
| 26 |
if (pluginResults) { |
| 27 |
const pluginSearchContainerId = 'ext-recommendations-plugin-search'; |
| 28 |
|
| 29 |
// If our component is already inserted, return early. |
| 30 |
if (document.getElementById(pluginSearchContainerId)) { |
| 31 |
return; |
| 32 |
} |
| 33 |
|
| 34 |
const pluginSearchContainer = Object.assign( |
| 35 |
document.createElement('div'), |
| 36 |
{ |
| 37 |
id: pluginSearchContainerId, |
| 38 |
className: 'extendify-recommendations', |
| 39 |
}, |
| 40 |
); |
| 41 |
|
| 42 |
// Inserts our component just before the plugin search results. |
| 43 |
pluginResults.parentNode.insertBefore( |
| 44 |
pluginSearchContainer, |
| 45 |
pluginResults, |
| 46 |
); |
| 47 |
|
| 48 |
return render(<PluginSearchBanner />, pluginSearchContainer); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
if (isNewPost) { |
| 53 |
// TODO: Implement injection of components in new post. |
| 54 |
} |
| 55 |
|
| 56 |
if (isNewPage) { |
| 57 |
// TODO: Implement injection of components in new page. |
| 58 |
} |
| 59 |
}); |
| 60 |
|