| 1 |
<?php |
| 2 |
|
| 3 |
namespace Premmerce\Premmerce\Admin; |
| 4 |
|
| 5 |
use Premmerce\Premmerce\Container; |
| 6 |
use Premmerce\Premmerce\PremmercePlugin; |
| 7 |
/** |
| 8 |
* Class Admin |
| 9 |
* |
| 10 |
* @package Premmerce\Premium\Admin |
| 11 |
*/ |
| 12 |
class Admin { |
| 13 |
/** |
| 14 |
* @var Container |
| 15 |
*/ |
| 16 |
private $container; |
| 17 |
|
| 18 |
/** |
| 19 |
* Admin constructor. |
| 20 |
* |
| 21 |
* Register menu items and handlers |
| 22 |
* |
| 23 |
* @param Container $container |
| 24 |
*/ |
| 25 |
public function __construct( Container $container ) { |
| 26 |
$this->container = $container; |
| 27 |
$this->addActions(); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Add plugin actions |
| 32 |
*/ |
| 33 |
public function addActions() { |
| 34 |
add_action( 'admin_menu', array($this, 'addMenuPage'), 1 ); |
| 35 |
add_action( 'admin_enqueue_scripts', array($this, 'enqueueScripts') ); |
| 36 |
add_action( 'wp_enqueue_scripts', array($this, 'enqueueScripts') ); |
| 37 |
add_action( 'admin_post_premmerce_actions', function () { |
| 38 |
call_user_func( array($this->container->getPluginsHandler(), 'runAction') ); |
| 39 |
} ); |
| 40 |
add_action( 'wp_ajax_premmerce_actions', function () { |
| 41 |
call_user_func( array($this->container->getPluginsHandler(), 'runAction') ); |
| 42 |
} ); |
| 43 |
add_action( 'wp_ajax_premmerce_wizard_actions', function () { |
| 44 |
call_user_func( array($this->container->getWizardHandler(), 'runAction') ); |
| 45 |
} ); |
| 46 |
add_action( 'wp_before_admin_bar_render', function () { |
| 47 |
call_user_func( array($this->container->getToolbarHandler(), 'renderAdminBarItem') ); |
| 48 |
} ); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Add premmerce to menu |
| 53 |
*/ |
| 54 |
public function addMenuPage() { |
| 55 |
$tips = $this->container->getWizardApi()->getUncompletedTipsCount(); |
| 56 |
add_menu_page( |
| 57 |
__( 'Premmerce', 'premmerce' ), |
| 58 |
__( 'Premmerce', 'premmerce' ) . " <span class='update-plugins count-{$tips} premmerce-wizard-counter' ><span class='plugin-count' data-premmerce-count>{$tips}</span></span>", |
| 59 |
'manage_options', |
| 60 |
'premmerce', |
| 61 |
array($this, 'optionsPage'), |
| 62 |
$this->getIcon() |
| 63 |
); |
| 64 |
//Remove notifications from hook |
| 65 |
global $admin_page_hooks; |
| 66 |
$admin_page_hooks['premmerce'] = 'premmerce'; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Manage plugins page |
| 71 |
*/ |
| 72 |
public function optionsPage() { |
| 73 |
$current = $this->getCurrentTab(); |
| 74 |
$allowed_tabs = $this->getTabs(); |
| 75 |
if ( !isset( $allowed_tabs[$current] ) ) { |
| 76 |
$current = 'wizard'; |
| 77 |
} |
| 78 |
$data = array( |
| 79 |
'current' => $current, |
| 80 |
'tabs' => $allowed_tabs, |
| 81 |
'url' => menu_page_url( PremmercePlugin::SLUG, false ), |
| 82 |
); |
| 83 |
switch ( $current ) { |
| 84 |
case 'wizard': |
| 85 |
$data['wizard'] = $this->container->getWizardApi(); |
| 86 |
break; |
| 87 |
case 'plugins': |
| 88 |
$data['dep'] = $this->container->getPluginsApi(); |
| 89 |
break; |
| 90 |
case 'addons': |
| 91 |
$data['addons'] = $this->container->getAddonsManager(); |
| 92 |
break; |
| 93 |
} |
| 94 |
$this->container->getFileManager()->includeTemplate( 'admin/main.php', $data ); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* @param string $hook |
| 99 |
*/ |
| 100 |
public function enqueueScripts( $hook ) { |
| 101 |
$fm = $this->container->getFileManager(); |
| 102 |
wp_enqueue_style( 'premmerce-wizard-bar', $fm->locateAsset( 'admin/css/wizard-admin-bar.css' ) ); |
| 103 |
if ( $hook == 'toplevel_page_premmerce' ) { |
| 104 |
wp_enqueue_style( 'premmerce-tabs', $fm->locateAsset( 'admin/css/premmerce-tabs.css' ) ); |
| 105 |
wp_register_script( 'premmerce_actions', $fm->locateAsset( 'admin/js/premmerce_actions.js' ), array('updates') ); |
| 106 |
wp_register_style( 'premmerce-spinner', $fm->locateAsset( 'admin/css/spinner.css' ) ); |
| 107 |
switch ( $this->getCurrentTab() ) { |
| 108 |
case 'wizard': |
| 109 |
wp_enqueue_script( 'premmerce-wizard', $fm->locateAsset( 'admin/js/wizard.js' ), array('premmerce_actions', 'jquery-ui-sortable') ); |
| 110 |
wp_localize_script( 'premmerce-wizard', 'premmerceWizard', array( |
| 111 |
'nonce' => wp_create_nonce( 'premmerce_wizard_actions' ), |
| 112 |
) ); |
| 113 |
wp_enqueue_style( 'premmerce-wizard', $fm->locateAsset( 'admin/css/wizard.css' ), array('premmerce-spinner') ); |
| 114 |
break; |
| 115 |
case 'plugins': |
| 116 |
wp_enqueue_script( 'premmerce_plugins', $fm->locateAsset( 'admin/js/plugins.js' ), array('premmerce_actions') ); |
| 117 |
wp_enqueue_style( 'premmerce-plugins', $fm->locateAsset( 'admin/css/plugins.css' ), array('premmerce-spinner') ); |
| 118 |
break; |
| 119 |
case 'settings': |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* |
| 126 |
* @return array |
| 127 |
*/ |
| 128 |
private function getTabs() { |
| 129 |
$tabs['wizard'] = __( 'Wizard', 'premmerce' ); |
| 130 |
$tabs['plugins'] = __( 'Plugins', 'premmerce' ); |
| 131 |
$tabs['settings'] = __( 'Settings', 'premmerce' ); |
| 132 |
$tabs['addons'] = __( 'Addons', 'premmerce' ); |
| 133 |
if ( function_exists( 'premmerce_pwk_fs' ) ) { |
| 134 |
if ( premmerce_pwk_fs()->is_registered() ) { |
| 135 |
$tabs['account'] = __( 'Account', 'premmerce' ); |
| 136 |
} |
| 137 |
$tabs['contact'] = __( 'Contact Us', 'premmerce' ); |
| 138 |
// $tabs['pricing'] = __('Pricing', 'premmerce'); |
| 139 |
} |
| 140 |
return $tabs; |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Tab from get parameter of default |
| 145 |
* @return string |
| 146 |
*/ |
| 147 |
private function getCurrentTab() { |
| 148 |
return ( isset( $_GET['tab'] ) ? $_GET['tab'] : 'wizard' ); |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Inline svg icon |
| 153 |
* |
| 154 |
* @return string |
| 155 |
*/ |
| 156 |
private function getIcon() { |
| 157 |
$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>'; |
| 158 |
$svg = 'data:image/svg+xml;base64,' . base64_encode( $svg ); |
| 159 |
return $svg; |
| 160 |
} |
| 161 |
|
| 162 |
} |
| 163 |
|