Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 94 additions & 26 deletions Plugin/Magento/Backend/Model/Menu/BuilderPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace Magefan\Community\Plugin\Magento\Backend\Model\Menu;

use Magefan\Community\Api\GetModuleInfoInterface;
use Magefan\Community\Api\GetModuleVersionInterface;
use Magento\Backend\Model\Menu\Builder;
use Magento\Backend\Model\Menu;
use Magento\Backend\Model\Menu\ItemFactory;
Expand Down Expand Up @@ -59,6 +60,11 @@ class BuilderPlugin
*/
private $getModuleInfo;

/**
* @var GetModuleVersionInterface
*/
private $getModuleVersion;

/**
* BuilderPlugin constructor.
* @param ItemFactory $menuItemFactory
Expand All @@ -67,14 +73,16 @@ class BuilderPlugin
* @param ModuleListInterface $moduleList
* @param Manager $moduleManager
* @param GetModuleInfoInterface $getModuleInfo
* @param GetModuleVersionInterface|null $getModuleVersion
*/
public function __construct(
ItemFactory $menuItemFactory,
Config $config,
Structure $structure,
ModuleListInterface $moduleList,
Manager $moduleManager,
GetModuleInfoInterface $getModuleInfo
GetModuleInfoInterface $getModuleInfo,
?GetModuleVersionInterface $getModuleVersion = null
) {
$this->menuItemFactory = $menuItemFactory;
$this->config = $config;
Expand All @@ -83,6 +91,9 @@ public function __construct(
$this->moduleManager = $moduleManager;
$this->magefanModules = $this->getMagefanModules();
$this->getModuleInfo = $getModuleInfo;
$this->getModuleVersion = $getModuleVersion ?: \Magento\Framework\App\ObjectManager::getInstance()->get(
GetModuleVersionInterface::class
);
}

/**
Expand Down Expand Up @@ -236,32 +247,12 @@ private function addUserGuideLinks(Menu $menu): void
$added[] = $id;

// extract module name from id e.g. Magefan_Blog::elements -> Magefan_Blog
$module = explode('::', $id)[0];
$module = explode('_', $module)[1];
$url = !empty($modulesInfo[$module]) ? $modulesInfo[$module]->getDocumentationUrl() : '';
// unique id per module to avoid conflicts
$newItemId = $id . '_user_guides';
if (!$url) {
continue;
}

try {
$encodedUrl = 'mf-ug-url-start' . rtrim(strtr(base64_encode($url), '+/', '-_'), '=') . 'mf-ug-url-end';

$userGuideItem = $this->menuItemFactory->create([
'data' => [
'id' => $newItemId,
'title' => 'User Guides',
'module' => 'Magefan_Community',
'resource' => 'Magefan_Community::elements',
'action' => $encodedUrl,
]
]);
$moduleFullName = explode('::', $id)[0];
$module = explode('_', $moduleFullName)[1];
$moduleInfo = !empty($modulesInfo[$module]) ? $modulesInfo[$module] : null;

$menu->add($userGuideItem, $id, 6000);

} catch (\Exception $e) {
}
$this->addUserGuideLink($menu, $id, $moduleInfo);
$this->addUpgradePlanLink($menu, $id, $moduleFullName, $moduleInfo);
}
}
}
Expand All @@ -270,6 +261,83 @@ private function addUserGuideLinks(Menu $menu): void
}
}

/**
* Add "User Guides" sub menu item for a module
*
* @param Menu $menu
* @param string $id
* @param \Magento\Framework\DataObject|null $moduleInfo
* @return void
*/
private function addUserGuideLink(Menu $menu, string $id, $moduleInfo): void
{
$url = $moduleInfo ? $moduleInfo->getDocumentationUrl() : '';
if (!$url) {
return;
}

try {
$encodedUrl = 'mf-ug-url-start' . rtrim(strtr(base64_encode($url), '+/', '-_'), '=') . 'mf-ug-url-end';

$userGuideItem = $this->menuItemFactory->create([
'data' => [
'id' => $id . '_user_guides',
'title' => 'User Guides',
'module' => 'Magefan_Community',
'resource' => 'Magefan_Community::elements',
'action' => $encodedUrl,
]
]);

$menu->add($userGuideItem, $id, 6000);
} catch (\Exception $e) {
}
}

/**
* Add "Upgrade Plan" sub menu item for a module, if a higher paid plan is available for it
*
* @param Menu $menu
* @param string $id
* @param string $moduleFullName
* @param \Magento\Framework\DataObject|null $moduleInfo
* @return void
*/
private function addUpgradePlanLink(Menu $menu, string $id, string $moduleFullName, $moduleInfo): void
{
if (!$moduleInfo || !$moduleInfo->getMaxPlan()) {
return;
}

$canUpgradeToMaxPlan = !$this->getModuleVersion->execute(
$moduleFullName . ucfirst($moduleInfo->getMaxPlan())
);

if (!$canUpgradeToMaxPlan) {
return;
}

$moduleUrl = $moduleInfo->getProductUrl() ?: 'https://magefan.com/';
$url = rtrim($moduleUrl, '/') . '/pricing';

try {
$encodedUrl = 'mf-upg-url-start' . rtrim(strtr(base64_encode($url), '+/', '-_'), '=') . 'mf-upg-url-end';

$upgradePlanItem = $this->menuItemFactory->create([
'data' => [
'id' => $id . '_upgrade_plan',
'title' => 'Upgrade Plan',
'module' => 'Magefan_Community',
'resource' => 'Magefan_Community::elements',
'action' => $encodedUrl,
]
]);

$menu->add($upgradePlanItem, $id, 6001);
} catch (\Exception $e) {
}
}

/**
* Ge config by module name
*
Expand Down
21 changes: 16 additions & 5 deletions view/adminhtml/web/js/magefan-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -733,10 +733,11 @@ var MagefanMenuManager = {
});
}

function addUserGuideLinks() {
document.querySelectorAll('a[href*="mf-ug-url-start"]').forEach(function(link) {
var match = link.getAttribute('href')
.match(/mf-ug-url-start(.+?)mf-ug-url-end/);
function addEncodedMenuLinks(marker, endMarker, utmCampaign) {
var regex = new RegExp(marker + '(.+?)' + endMarker);

document.querySelectorAll('a[href*="' + marker + '"]').forEach(function(link) {
var match = link.getAttribute('href').match(regex);
if (!match) return;

var encoded = match[1];
Expand All @@ -753,7 +754,8 @@ var MagefanMenuManager = {

if (!decoded) return;

link.href = decoded + '?utm_source=admin&utm_medium=menu&utm_campaign=sub-guide';
var separator = decoded.indexOf('?') !== -1 ? '&' : '?';
link.href = decoded + separator + 'utm_source=admin&utm_medium=menu&utm_campaign=' + utmCampaign;
link.target = '_blank';
link.addEventListener('click', function(e) {
e.preventDefault();
Expand All @@ -762,6 +764,15 @@ var MagefanMenuManager = {
});
}

function addUserGuideLinks() {
addEncodedMenuLinks('mf-ug-url-start', 'mf-ug-url-end', 'sub-guide');
}

function addUpgradePlanLinks() {
addEncodedMenuLinks('mf-upg-url-start', 'mf-upg-url-end', 'upgrade-plan');
}

addMainUserGuideAndMarketplaceLinks();
addUserGuideLinks();
addUpgradePlanLinks();
})();