| 1 |
<?php |
| 2 |
namespace Depicter\Dashboard; |
| 3 |
|
| 4 |
use Averta\Core\Utility\Arr; |
| 5 |
use Averta\WordPress\Utility\Escape; |
| 6 |
use Averta\WordPress\Utility\JSON; |
| 7 |
use Averta\WordPress\Utility\Plugin; |
| 8 |
use Depicter\Security\CSRF; |
| 9 |
|
| 10 |
class DashboardPage |
| 11 |
{ |
| 12 |
|
| 13 |
const HOOK_SUFFIX = 'toplevel_page_depicter-dashboard'; |
| 14 |
const PAGE_ID = 'depicter-dashboard'; |
| 15 |
|
| 16 |
/** |
| 17 |
* @var string |
| 18 |
*/ |
| 19 |
var $hook_suffix = ''; |
| 20 |
|
| 21 |
public function bootstrap(){ |
| 22 |
add_action( 'admin_menu', [ $this, 'registerPage' ] ); |
| 23 |
add_action( 'admin_enqueue_scripts', [ $this, 'enqueueScripts' ] ); |
| 24 |
add_action( 'admin_head', array( $this, 'disable_admin_notices' ) ); |
| 25 |
add_action( 'admin_init', [ $this, 'externalPageRedirect' ] ); |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Register admin pages. |
| 30 |
* |
| 31 |
* @return void |
| 32 |
*/ |
| 33 |
public function registerPage() { |
| 34 |
$this->hook_suffix = add_menu_page( |
| 35 |
__('Depicter', 'depicter'), |
| 36 |
__('Depicter', 'depicter'), |
| 37 |
'manage_options', |
| 38 |
self::PAGE_ID, |
| 39 |
[ $this, 'render' ], // called to output the content for this page |
| 40 |
\Depicter::core()->assets()->getUrl() . '/resources/images/svg/wp-logo.svg' |
| 41 |
); |
| 42 |
|
| 43 |
add_submenu_page( |
| 44 |
self::PAGE_ID, |
| 45 |
__( 'Dashboard', 'depicter' ), |
| 46 |
__( 'Dashboard', 'depicter' ), |
| 47 |
'manage_options', |
| 48 |
self::PAGE_ID |
| 49 |
); |
| 50 |
|
| 51 |
add_submenu_page( |
| 52 |
self::PAGE_ID, |
| 53 |
__( 'Add New', 'depicter' ), |
| 54 |
__( 'Add New', 'depicter' ), |
| 55 |
'manage_options', |
| 56 |
self::PAGE_ID . '-create-slider', |
| 57 |
[ $this, 'externalPageRedirect' ] |
| 58 |
); |
| 59 |
|
| 60 |
add_submenu_page( |
| 61 |
self::PAGE_ID, |
| 62 |
__( 'Support', 'depicter' ), |
| 63 |
__( 'Support', 'depicter' ), |
| 64 |
'manage_options', |
| 65 |
self::PAGE_ID . '-goto-support', |
| 66 |
[ $this, 'externalPageRedirect' ] |
| 67 |
); |
| 68 |
|
| 69 |
add_action( 'admin_print_scripts-' . $this->hook_suffix, [ $this, 'printScripts' ] ); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Process redirect after clicking on Depicter admin menu |
| 74 |
* |
| 75 |
* @return void |
| 76 |
*/ |
| 77 |
public function externalPageRedirect(){ |
| 78 |
if ( empty( $_GET['page'] ) ) { |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
if ( self::PAGE_ID . '-create-slider' === $_GET['page'] ) { |
| 83 |
wp_redirect( menu_page_url( self::PAGE_ID, false ) . '#/templates/custom' ); |
| 84 |
die; |
| 85 |
} |
| 86 |
|
| 87 |
if ( self::PAGE_ID . '-goto-support' === $_GET['page'] ) { |
| 88 |
wp_redirect( 'https://depicter.com/?utm_source=wp-submenu#support' ); |
| 89 |
die; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Disable all admin notices in dashboard page |
| 95 |
* |
| 96 |
* @return void |
| 97 |
*/ |
| 98 |
public function disable_admin_notices() { |
| 99 |
$screen = get_current_screen(); |
| 100 |
if ( $screen->id == $this->hook_suffix ) { |
| 101 |
remove_all_actions( 'admin_notices' ); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
public function render(){ |
| 106 |
echo Escape::content( \Depicter::view( 'admin/dashboard/index.php' )->toString() ); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Load dashboard scripts |
| 111 |
* |
| 112 |
* @param string $hook_suffix |
| 113 |
*/ |
| 114 |
public function enqueueScripts( $hook_suffix = '' ){ |
| 115 |
|
| 116 |
if( $hook_suffix !== $this->hook_suffix ){ |
| 117 |
return; |
| 118 |
} |
| 119 |
|
| 120 |
// Enqueue scripts. |
| 121 |
\Depicter::core()->assets()->enqueueScript( |
| 122 |
'depicter--dashboard', |
| 123 |
\Depicter::core()->assets()->getUrl() . '/resources/scripts/dashboard/depicter-dashboard.js', |
| 124 |
[], |
| 125 |
true |
| 126 |
); |
| 127 |
|
| 128 |
// Enqueue styles. |
| 129 |
\Depicter::core()->assets()->enqueueStyle( |
| 130 |
'depicter-dashboard', |
| 131 |
\Depicter::core()->assets()->getUrl() . '/resources/styles/dashboard/index.css' |
| 132 |
); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Print required scripts in Dashboard page |
| 137 |
* |
| 138 |
* @return void |
| 139 |
*/ |
| 140 |
public function printScripts() |
| 141 |
{ |
| 142 |
global $wp_version; |
| 143 |
$currentUser = wp_get_current_user(); |
| 144 |
|
| 145 |
wp_add_inline_script('depicter--dashboard', 'window.depicterEnv = '. JSON::encode( |
| 146 |
[ |
| 147 |
'wpVersion' => $wp_version, |
| 148 |
"scriptsPath" => \Depicter::core()->assets()->getUrl(). '/resources/scripts/dashboard/', |
| 149 |
'clientKey' => \Depicter::options()->get( 'client_key', 'anon' ), |
| 150 |
'csrfToken' => \Depicter::csrf()->getToken( CSRF::DASHBOARD_ACTION ), |
| 151 |
'updateInfo' => [ |
| 152 |
'from' => \Depicter::options()->get('version_previous') ?: null, |
| 153 |
'to' => \Depicter::options()->get('version') |
| 154 |
], |
| 155 |
"assetsAPI" => Escape::url('https://wp-api.depicter.com/' ), |
| 156 |
"wpRestApi" => Escape::url( get_rest_url() ), |
| 157 |
"pluginAPI" => admin_url( 'admin-ajax.php' ), |
| 158 |
"editorPath" => \Depicter::editor()->getEditUrl( '__id__' ), |
| 159 |
"documentPreviewPath" => \Depicter::editor()->getEditUrl( '__id__' ), |
| 160 |
'user' => [ |
| 161 |
'tier' => \Depicter::options()->get('user_plan', 'free-user'), |
| 162 |
'name' => Escape::html( $currentUser->display_name ), |
| 163 |
'email' => Escape::html( $currentUser->user_email ), |
| 164 |
'joinedNewsletter' => !! \Depicter::options()->get('has_subscribed') |
| 165 |
], |
| 166 |
'integrations' => [ |
| 167 |
'woocommerce' => Plugin::isActive( 'woocommerce/woocommerce.php' ) |
| 168 |
] |
| 169 |
] |
| 170 |
), 'before' ); |
| 171 |
|
| 172 |
} |
| 173 |
|
| 174 |
} |
| 175 |
|