PluginProbe
Depicter — Popup & Slider Builder / trunk
Depicter — Popup & Slider Builder vtrunk
4.8.1 trunk 1.0.0 1.1.0 1.1.2 1.1.4 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.5 1.3.8 1.5.0 1.5.1 1.5.2 1.5.5 1.6.0 1.6.1 1.6.2 1.7.0 All 76 releases
depicter / app / src / Dashboard / DashboardPage.php

DashboardPage.php in Depicter — Popup & Slider Builder trunk, at app/src/Dashboard/DashboardPage.php

296 lines 9.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Depicter\Dashboard;
3
4 use Averta\Core\Utility\Data;
5 use Averta\Core\Utility\Extract;
6 use Averta\WordPress\Utility\Escape;
7 use Averta\WordPress\Utility\JSON;
8 use Averta\WordPress\Utility\Plugin;
9 use Depicter\GuzzleHttp\Exception\GuzzleException;
10 use Depicter\Security\CSRF;
11 use Depicter\Services\UserAPIService;
12
13 class DashboardPage
14 {
15
16 const HOOK_SUFFIX = 'toplevel_page_depicter-dashboard';
17 const PAGE_ID = 'depicter-dashboard';
18
19 /**
20 * @var string
21 */
22 var $hook_suffix = '';
23
24 public function bootstrap(){
25 add_action( 'admin_menu', [ $this, 'registerPage' ] );
26 add_action( 'admin_enqueue_scripts', [ $this, 'enqueueScripts' ] );
27 add_action( 'admin_head', array( $this, 'disable_admin_notices' ) );
28 add_action( 'admin_init', [ $this, 'externalPageRedirect' ] );
29 }
30
31 /**
32 * Register admin pages.
33 *
34 * @return void
35 */
36 public function registerPage() {
37
38 $depicterMenuTitle = __( 'Depicter', 'depicter' );
39 $notifNumber = $this->countOfUnreadNotifications();
40 $depicterMenuTitle = $notifNumber ? $depicterMenuTitle . " <span class='awaiting-mod depicter-notif-number'>" . $notifNumber . "</span>" : $depicterMenuTitle;
41
42 $this->hook_suffix = add_menu_page(
43 __('Depicter', 'depicter'),
44 $depicterMenuTitle,
45 'access_depicter',
46 self::PAGE_ID,
47 [ $this, 'render' ], // called to output the content for this page
48 \Depicter::core()->assets()->getUrl() . '/resources/images/svg/wp-logo.svg',
49 58
50 );
51
52 add_submenu_page(
53 self::PAGE_ID,
54 __( 'Dashboard', 'depicter' ),
55 __( 'Dashboard', 'depicter' ),
56 'access_depicter',
57 self::PAGE_ID
58 );
59
60 add_submenu_page(
61 self::PAGE_ID,
62 __( 'Support', 'depicter' ),
63 __( 'Support', 'depicter' ),
64 'access_depicter',
65 self::PAGE_ID . '-goto-support',
66 [ $this, 'externalPageRedirect' ]
67 );
68
69 if ( ! \Depicter::auth()->isPaid() && empty( $_GET['depicter_upgraded'] ) ) {
70 add_submenu_page(
71 self::PAGE_ID,
72 __( 'Upgrade to PRO', 'depicter' ),
73 __( 'Upgrade to PRO', 'depicter' ),
74 'access_depicter',
75 self::PAGE_ID . '-goto-pro',
76 [ $this, 'externalPageRedirect' ]
77 );
78 }
79
80 add_action( 'admin_print_scripts-' . $this->hook_suffix, [ $this, 'printScripts' ] );
81 }
82
83 /**
84 * Process redirect after clicking on Depicter admin menu
85 *
86 * @return void
87 */
88 public function externalPageRedirect(){
89 if ( empty( $_GET['page'] ) ) {
90 return;
91 }
92
93 if ( self::PAGE_ID . '-goto-support' === $_GET['page'] ) {
94 wp_redirect( 'https://wordpress.org/support/plugin/depicter/' );
95 die;
96 }
97
98 if ( self::PAGE_ID . '-goto-pro' === $_GET['page'] ) {
99 wp_redirect( 'https://depicter.com/pricing?utm_source=depicter&utm_medium=depicter-free&utm_campaign=free-to-pro&utm_term=unlock-submenu' );
100 die;
101 }
102 }
103
104 /**
105 * Disable all admin notices in dashboard page
106 *
107 * @return void
108 */
109 public function disable_admin_notices() {
110 $screen = get_current_screen();
111 if ( $screen->id == $this->hook_suffix ) {
112 remove_all_actions( 'admin_notices' );
113 }
114 }
115
116 public function render(){
117 $this->renewTokens();
118 echo Escape::content( \Depicter::view( 'admin/dashboard/index.php' )->toString() );
119 }
120
121 /**
122 * Load dashboard scripts
123 *
124 * @param string $hook_suffix
125 */
126 public function enqueueScripts( $hook_suffix = '' ){
127
128 // If not dashboard page, don't load dashboard assets
129 if ( $hook_suffix !== $this->hook_suffix ) {
130 return;
131 }
132
133 // Enqueue scripts.
134 \Depicter::core()->assets()->enqueueScriptModule(
135 'depicter--dashboard',
136 \Depicter::core()->assets()->getUrl() . '/resources/scripts/dashboard/depicter-dashboard.js'
137 );
138
139 // Enqueue styles.
140 \Depicter::core()->assets()->enqueueStyle(
141 'depicter-dashboard-styles',
142 \Depicter::core()->assets()->getUrl() . '/resources/scripts/dashboard/depicter-dashboard-styles.css'
143 );
144 \Depicter::core()->assets()->enqueueStyle(
145 'depicter-dashboard',
146 \Depicter::core()->assets()->getUrl() . '/resources/styles/dashboard/index.css'
147 );
148 }
149
150 /**
151 * Print required scripts in Dashboard page
152 *
153 * @return void
154 * @throws GuzzleException
155 */
156 public function printScripts()
157 {
158 global $wp_version;
159 $currentUser = wp_get_current_user();
160
161 try {
162 $googleClientId = UserAPIService::googleClientID()['clientId'] ?? '';
163 } catch ( \Exception $e ) {
164 $googleClientId = '';
165 }
166
167 $upgradeLink = add_query_arg([
168 'action' => 'upgrade-plugin',
169 'plugin' => 'depicter/depicter.php',
170 '_wpnonce' => wp_create_nonce( 'upgrade-plugin_depicter/depicter.php')
171 ], self_admin_url('update.php') );
172
173 // retrieve refresh token
174 $refreshToken = \Depicter::cache('base')->get( 'refresh_token', null );
175 $refreshTokenPayload = Extract::JWTPayload( $refreshToken );
176 $displayReviewNotice = !empty( $refreshTokenPayload['ict'] ) && ( time() - Data::cast( $refreshTokenPayload['ict'], 'int' ) > 5 * DAY_IN_SECONDS );
177
178 wp_print_inline_script_tag('window.depicterEnv = '. JSON::encode(
179 [
180 'wpVersion' => $wp_version,
181 "scriptsPath" => \Depicter::core()->assets()->getUrl(). '/resources/scripts/dashboard/',
182 'clientKey' => \Depicter::auth()->getClientKey(),
183 'csrfToken' => \Depicter::csrf()->getToken( CSRF::DASHBOARD_ACTION ),
184 'updateInfo' => [
185 'from' => \Depicter::options()->get('version_previous') ?: null,
186 'to' => \Depicter::options()->get('version'),
187 'url' => $upgradeLink,
188 ],
189 "assetsAPI" => Escape::url('https://wp-api.depicter.com/' ),
190 "wpRestApi" => Escape::url( get_rest_url() ),
191 "pluginAPI" => admin_url( 'admin-ajax.php' ),
192 "editorPath" => \Depicter::editor()->getEditUrl( '__id__' ),
193 "documentPreviewPath" => \Depicter::editor()->getEditUrl( '__id__' ),
194 'user' => [
195 'tier' => \Depicter::auth()->getTier(),
196 'name' => Escape::html( $currentUser->display_name ),
197 'email' => Escape::html( $currentUser->user_email ),
198 'onboarding' => \Depicter::client()->isNewClient(),
199 'joinedNewsletter' => !! \Depicter::options()->get('has_subscribed'),
200 'dataCollectionConsent' => \Depicter::options()->get('data_collect_consent', 'not-set')
201 ],
202 'activation' => [
203 'status' => \Depicter::auth()->getActivationStatus(),
204 'errorMessage' => \Depicter::options()->get('activation_error_message', ''),
205 'expiresAt' => \Depicter::options()->get('subscription_expires_at' , ''),
206 'isNew' => isset( $_GET['depicter_upgraded'] )
207 ],
208 'subscription' => [
209 'id' => \Depicter::options()->get('subscription_id', null),
210 'status' => \Depicter::auth()->getSubscriptionStatus(),
211 'overdue' => \Depicter::auth()->isSubscriptionExpired()
212 ],
213 'integrations' => [
214 'unfilteredUploadAllowed' => \Depicter::options()->get('allow_unfiltered_data_upload' ) === 'on',
215 'woocommerce' => [
216 'label' => __( 'WooCommerce Plugin', 'depicter' ),
217 'enabled' => Plugin::isActive( 'woocommerce/woocommerce.php' )
218 ],
219 'googleReviews' => \Depicter::dataSource()->googlePlaces()->hasValidApiKey()
220 ],
221 'AIWizard' => [
222 'introVideoSrc' => 'https://www.youtube.com/embed/kdR9Jw0yWjU?rel=0'
223 ],
224 'googleClientId' => $googleClientId,
225 'tokens' => [
226 'idToken' => \Depicter::cache('base')->get( 'id_token' , null ),
227 'accessToken' => \Depicter::cache('base')->get( 'access_token' , null ),
228 'refreshToken' => $refreshToken
229 ],
230 'display' => [
231 'reviewNotice' => $displayReviewNotice
232 ]
233 ]
234 ) );
235
236 }
237
238 /**
239 * Renew member tokens before expire date
240 *
241 * @return void
242 */
243 public function renewTokens() {
244 if ( false === \Depicter::cache('base')->get( 'access_token' ) ) {
245 UserAPIService::renewTokens();
246 }
247 }
248
249 public function get_notifications() {
250 if ( false === $notifications = \Depicter::cache()->get( 'retrievedNotifications' ) ) {
251 try{
252 $response = \Depicter::remote()->get( 'v1/core/notifications', [
253 'query' => [
254 'page' => 1,
255 'perPage' => 20
256 ]
257 ]);
258
259 if ($response->getStatusCode() === 200) {
260 $notifications = $response->getBody()->getContents();
261 \Depicter::cache()->set( 'retrievedNotifications', $notifications, 12 * HOUR_IN_SECONDS );
262 return JSON::decode( $notifications, true );
263 }
264 } catch ( GuzzleException $e ){
265 }
266
267 return false;
268
269 }
270
271 return JSON::decode( $notifications, true );
272 }
273
274 protected function countOfUnreadNotifications(): ?int
275 {
276
277 if ( false === $notifications = $this->get_notifications() ) {
278 return 0;
279 }
280
281 if ( false === $lastSeenDate = \Depicter::options()->get('lastSeenNotificationDate', false) ) {
282 return count( $notifications['hits'] );
283 }
284
285 $unreadNotifications = 0;
286 $lastSeenDate = strtotime( $lastSeenDate );
287 foreach ( $notifications['hits'] as $notification ) {
288 if ( strtotime( $notification['date'] ) > $lastSeenDate ) {
289 $unreadNotifications++;
290 }
291 }
292
293 return $unreadNotifications;
294 }
295 }
296