| 1 |
<?php namespace Premmerce\Premmerce\Admin; |
| 2 |
|
| 3 |
use Premmerce\Premmerce\Container; |
| 4 |
use Premmerce\Premmerce\PremmercePlugin; |
| 5 |
use Premmerce\Premmerce\WordpressSDK\FileManager\FileManager; |
| 6 |
|
| 7 |
/** |
| 8 |
* Class Admin |
| 9 |
* |
| 10 |
* @package Premmerce\Premium\Admin |
| 11 |
*/ |
| 12 |
class Admin{ |
| 13 |
|
| 14 |
|
| 15 |
/** |
| 16 |
* @var Container |
| 17 |
*/ |
| 18 |
private $container; |
| 19 |
|
| 20 |
/** |
| 21 |
* Admin constructor. |
| 22 |
* |
| 23 |
* Register menu items and handlers |
| 24 |
* |
| 25 |
* @param Container $container |
| 26 |
*/ |
| 27 |
public function __construct(Container $container){ |
| 28 |
|
| 29 |
$this->container = $container; |
| 30 |
$this->addActions(); |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
/** |
| 35 |
* Add plugin actions |
| 36 |
*/ |
| 37 |
public function addActions(){ |
| 38 |
|
| 39 |
add_action('admin_menu', [$this, 'addMenuPage'], 1); |
| 40 |
add_action('admin_enqueue_scripts', [$this, 'enqueueScripts']); |
| 41 |
add_action('wp_enqueue_scripts', [$this, 'enqueueScripts']); |
| 42 |
|
| 43 |
|
| 44 |
add_action('admin_post_premmerce_actions', function(){ |
| 45 |
call_user_func([$this->container->getPluginsHandler(), 'runAction']); |
| 46 |
}); |
| 47 |
|
| 48 |
add_action('wp_ajax_premmerce_actions', function(){ |
| 49 |
call_user_func([$this->container->getPluginsHandler(), 'runAction']); |
| 50 |
}); |
| 51 |
|
| 52 |
add_action('wp_ajax_premmerce_wizard_actions', function(){ |
| 53 |
call_user_func([$this->container->getWizardHandler(), 'runAction']); |
| 54 |
}); |
| 55 |
|
| 56 |
add_action('wp_before_admin_bar_render', function(){ |
| 57 |
call_user_func([$this->container->getToolbarHandler(), 'renderAdminBarItem']); |
| 58 |
}); |
| 59 |
|
| 60 |
add_action('admin_post_toggle_navigation', function(){ |
| 61 |
call_user_func([$this->container->getToolbarHandler(), 'toggleNavigation']); |
| 62 |
}); |
| 63 |
|
| 64 |
if(PremmercePlugin::IS_PREMIUM){ |
| 65 |
add_action('wp_before_admin_bar_render', function(){ |
| 66 |
call_user_func([$this->container->getPremiumHandler(), 'replaceToolbarWpItem']); |
| 67 |
}); |
| 68 |
|
| 69 |
add_action('wp_ajax_enable_ecommerce_menu', [$this->container->getPremiumHandler(), 'enableMenu']); |
| 70 |
|
| 71 |
} |
| 72 |
|
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
/** |
| 77 |
* Add premmerce to menu |
| 78 |
*/ |
| 79 |
public function addMenuPage(){ |
| 80 |
$tips = $this->container->getWizardApi()->getUncompletedTipsCount(); |
| 81 |
add_menu_page( |
| 82 |
__('Premmerce', 'premmerce'), |
| 83 |
__('Premmerce', 'premmerce') . " <span class='update-plugins count-{$tips} premmerce-wizard-counter' ><span class='plugin-count' data-premmerce-count>{$tips}</span></span>", |
| 84 |
'manage_options', |
| 85 |
'premmerce', |
| 86 |
[$this, 'optionsPage'], |
| 87 |
$this->getIcon()); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Manage plugins page |
| 92 |
*/ |
| 93 |
public function optionsPage(){ |
| 94 |
|
| 95 |
$current = $this->getCurrentTab(); |
| 96 |
|
| 97 |
|
| 98 |
$data = [ |
| 99 |
'current' => $current, |
| 100 |
'tabs' => $this->getTabs(), |
| 101 |
'url' => menu_page_url(PremmercePlugin::SLUG, false), |
| 102 |
]; |
| 103 |
|
| 104 |
switch($current){ |
| 105 |
case 'wizard': |
| 106 |
$data['wizard'] = $this->container->getWizardApi(); |
| 107 |
break; |
| 108 |
case 'plugins': |
| 109 |
$data['dep'] = $this->container->getPluginsApi(); |
| 110 |
break; |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
$this->container->getFileManager()->includeTemplate('admin/main.php', $data); |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
/** |
| 119 |
* @param string $hook |
| 120 |
*/ |
| 121 |
public function enqueueScripts($hook){ |
| 122 |
|
| 123 |
$fm = $this->container->getFileManager(); |
| 124 |
|
| 125 |
|
| 126 |
if(PremmercePlugin::IS_PREMIUM){ |
| 127 |
wp_enqueue_style('premmerce-toolbar', $fm->locateAsset('admin/css/premmerce-toolbar.css')); |
| 128 |
} |
| 129 |
|
| 130 |
wp_enqueue_style('premmerce-wizard-bar', $fm->locateAsset('admin/css/wizard-admin-bar.css')); |
| 131 |
wp_enqueue_style('premmerce-tabs', $fm->locateAsset('admin/css/premmerce-tabs.css')); |
| 132 |
|
| 133 |
if($hook == 'toplevel_page_premmerce'){ |
| 134 |
|
| 135 |
wp_register_script('premmerce_actions', $fm->locateAsset('admin/js/premmerce_actions.js'), ['updates']); |
| 136 |
wp_register_style('premmerce-spinner', $fm->locateAsset('admin/css/spinner.css')); |
| 137 |
|
| 138 |
switch($this->getCurrentTab()){ |
| 139 |
case 'wizard': |
| 140 |
wp_enqueue_script('premmerce-wizard', $fm->locateAsset('admin/js/wizard.js'), [ |
| 141 |
'premmerce_actions', |
| 142 |
'jquery-ui-sortable', |
| 143 |
]); |
| 144 |
wp_enqueue_style('premmerce-wizard', $fm->locateAsset('admin/css/wizard.css'), ['premmerce-spinner']); |
| 145 |
break; |
| 146 |
case 'plugins': |
| 147 |
wp_enqueue_script('premmerce_plugins', $fm->locateAsset('admin/js/plugins.js'), ['premmerce_actions']); |
| 148 |
wp_enqueue_style('premmerce-plugins', $fm->locateAsset('admin/css/plugins.css'), ['premmerce-spinner']); |
| 149 |
break; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* |
| 158 |
* @return array |
| 159 |
*/ |
| 160 |
private function getTabs(){ |
| 161 |
$tabs['wizard'] = __('Wizard', 'premmerce'); |
| 162 |
$tabs['plugins'] = __('Plugins', 'premmerce'); |
| 163 |
|
| 164 |
if(function_exists('premmerce_pwk_fs')){ |
| 165 |
$tabs['contact'] = __('Support', 'premmerce'); |
| 166 |
if(premmerce_pwk_fs()->is_registered()){ |
| 167 |
$tabs['account'] = __('Account', 'premmerce'); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
return $tabs; |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Tab from get parameter of default |
| 176 |
* @return string |
| 177 |
*/ |
| 178 |
private function getCurrentTab(){ |
| 179 |
return isset($_GET['tab'])? $_GET['tab'] : 'wizard'; |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Inline svg icon |
| 184 |
* |
| 185 |
* @return string |
| 186 |
*/ |
| 187 |
private function getIcon(){ |
| 188 |
$svg = '<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xml:space="preserve" width="20" height="16" style="fill:#82878c" viewBox="0 0 20 16"><g id="Rectangle_7"> <path d="M17.8,4l-0.5,1C15.8,7.3,14.4,8,14,8c0,0,0,0,0,0H8h0V4.3C8,4.1,8.1,4,8.3,4H17.8 M4,0H1C0.4,0,0,0.4,0,1c0,0.6,0.4,1,1,1 h1.7C2.9,2,3,2.1,3,2.3V12c0,0.6,0.4,1,1,1c0.6,0,1-0.4,1-1V1C5,0.4,4.6,0,4,0L4,0z M18,2H7.3C6.6,2,6,2.6,6,3.3V12 c0,0.6,0.4,1,1,1c0.6,0,1-0.4,1-1v-1.7C8,10.1,8.1,10,8.3,10H14c1.1,0,3.2-1.1,5-4l0.7-1.4C20,4,20,3.2,19.5,2.6 C19.1,2.2,18.6,2,18,2L18,2z M14,11h-4c-0.6,0-1,0.4-1,1c0,0.6,0.4,1,1,1h4c0.6,0,1-0.4,1-1C15,11.4,14.6,11,14,11L14,11z M14,14 c-0.6,0-1,0.4-1,1c0,0.6,0.4,1,1,1c0.6,0,1-0.4,1-1C15,14.4,14.6,14,14,14L14,14z M4,14c-0.6,0-1,0.4-1,1c0,0.6,0.4,1,1,1 c0.6,0,1-0.4,1-1C5,14.4,4.6,14,4,14L4,14z"/></g></svg>'; |
| 189 |
$svg = 'data:image/svg+xml;base64,' . base64_encode($svg); |
| 190 |
|
| 191 |
return $svg; |
| 192 |
} |
| 193 |
|
| 194 |
} |