| 1 |
<?php |
| 2 |
namespace Depicter\Dashboard; |
| 3 |
|
| 4 |
use Averta\Core\Utility\Arr; |
| 5 |
use Averta\WordPress\Utility\Escape; |
| 6 |
use Averta\WordPress\Utility\Plugin; |
| 7 |
use Depicter\Security\CSRF; |
| 8 |
|
| 9 |
class DashboardPage |
| 10 |
{ |
| 11 |
|
| 12 |
const HOOK_SUFFIX = 'toplevel_page_depicter-dashboard'; |
| 13 |
|
| 14 |
/** |
| 15 |
* @var string |
| 16 |
*/ |
| 17 |
var $hook_suffix = ''; |
| 18 |
|
| 19 |
public function bootstrap(){ |
| 20 |
add_action( 'admin_menu', [ $this, 'registerPage' ] ); |
| 21 |
add_action( 'admin_enqueue_scripts', [ $this, 'enqueueScripts' ] ); |
| 22 |
add_action( 'admin_head', array( $this, 'disable_admin_notices' ) ); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Register admin pages. |
| 27 |
* |
| 28 |
* @return void |
| 29 |
*/ |
| 30 |
public function registerPage() { |
| 31 |
$this->hook_suffix = add_menu_page( |
| 32 |
__('Depicter', 'depicter'), |
| 33 |
__('Depicter', 'depicter'), |
| 34 |
'manage_options', |
| 35 |
'depicter-dashboard', |
| 36 |
[ $this, 'render' ], // called to output the content for this page |
| 37 |
\Depicter::core()->assets()->getUrl() . '/resources/images/svg/wp-logo.svg' |
| 38 |
); |
| 39 |
|
| 40 |
add_action( 'admin_print_scripts-' . $this->hook_suffix, [ $this, 'printScripts' ] ); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Disable all admin notices in dashboard page |
| 45 |
* |
| 46 |
* @return void |
| 47 |
*/ |
| 48 |
public function disable_admin_notices() { |
| 49 |
$screen = get_current_screen(); |
| 50 |
if ( $screen->id == $this->hook_suffix ) { |
| 51 |
remove_all_actions( 'admin_notices' ); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
public function render(){ |
| 56 |
echo Escape::content( \Depicter::view( 'admin/dashboard/index.php' )->toString() ); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Load dashboard scripts |
| 61 |
* |
| 62 |
* @param string $hook_suffix |
| 63 |
*/ |
| 64 |
public function enqueueScripts( $hook_suffix = '' ){ |
| 65 |
|
| 66 |
if( $hook_suffix !== $this->hook_suffix ){ |
| 67 |
return; |
| 68 |
} |
| 69 |
|
| 70 |
// Enqueue scripts. |
| 71 |
\Depicter::core()->assets()->enqueueScript( |
| 72 |
'depicter-dashboard-js', |
| 73 |
\Depicter::core()->assets()->getUrl() . '/resources/scripts/dashboard/depicter-dashboard.js', |
| 74 |
[], |
| 75 |
true |
| 76 |
); |
| 77 |
|
| 78 |
// Enqueue styles. |
| 79 |
\Depicter::core()->assets()->enqueueStyle( |
| 80 |
'depicter-dashboard-css', |
| 81 |
\Depicter::core()->assets()->getUrl() . '/resources/styles/dashboard/index.css' |
| 82 |
); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Print required scripts in Dashboard page |
| 87 |
* |
| 88 |
* @return void |
| 89 |
*/ |
| 90 |
public function printScripts() |
| 91 |
{ |
| 92 |
global $wp_version; |
| 93 |
$currentUser = wp_get_current_user(); |
| 94 |
|
| 95 |
wp_localize_script('depicter-dashboard-js', 'depicterDashboardEnv', |
| 96 |
Arr::merge( $this->getCommonEnvParams(), [ |
| 97 |
"assetsAPI" => Escape::url('https://wp-api.depicter.com/' ), |
| 98 |
"wpRestApi" => Escape::url( get_rest_url() ), |
| 99 |
"dashboardAPI" => admin_url( 'admin-ajax.php' ), |
| 100 |
"editorPath" => \Depicter::editor()->getEditUrl( '__id__' ), |
| 101 |
"documentPreviewPath" => \Depicter::editor()->getEditUrl( '__id__' ), |
| 102 |
'user' => [ |
| 103 |
'subscriptionPlan' => \Depicter::options()->get('user_plan', 'free-user'), |
| 104 |
'name' => Escape::html( $currentUser->display_name ), |
| 105 |
'email' => Escape::html( $currentUser->user_email ), |
| 106 |
'hasSubscribed' => !! \Depicter::options()->get('has_subscribed') |
| 107 |
] |
| 108 |
]) |
| 109 |
); |
| 110 |
|
| 111 |
wp_localize_script('depicter-dashboard-js', 'depicterKitEnv', |
| 112 |
Arr::merge( $this->getCommonEnvParams(), [ |
| 113 |
'editorAPI' => admin_url( 'admin-ajax.php' ), |
| 114 |
'user' => [ |
| 115 |
'subscriptionPlan' => \Depicter::options()->get('user_plan', 'free-user') |
| 116 |
] |
| 117 |
]) |
| 118 |
); |
| 119 |
|
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Get common ENV variables |
| 124 |
* |
| 125 |
* @return array |
| 126 |
*/ |
| 127 |
protected function getCommonEnvParams(){ |
| 128 |
global $wp_version; |
| 129 |
|
| 130 |
return [ |
| 131 |
'wpVersion' => $wp_version, |
| 132 |
"scriptsPath" => \Depicter::core()->assets()->getUrl(). '/resources/scripts/dashboard/', |
| 133 |
'clientKey' => \Depicter::options()->get( 'client_key', 'anon' ), |
| 134 |
'csrfToken' => \Depicter::csrf()->getToken( CSRF::DASHBOARD_ACTION ), |
| 135 |
'updateInfo' => [ |
| 136 |
'from' => \Depicter::options()->get('version_previous') ?: null, |
| 137 |
'to' => \Depicter::options()->get('version') |
| 138 |
], |
| 139 |
'integrations' => [ |
| 140 |
'woocommerce' => Plugin::isActive( 'woocommerce/woocommerce.php' ) |
| 141 |
] |
| 142 |
]; |
| 143 |
} |
| 144 |
} |
| 145 |
|