PluginProbe
Metricool – Social media and site statistics / trunk
Metricool – Social media and site statistics vtrunk
2.1.0 2.0.2 2.0.1 2.0.0 1.27 trunk
metricool / app / Services / DashboardService.php

DashboardService.php in Metricool – Social media and site statistics trunk, at app/Services/DashboardService.php

158 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Metricool\Services;
4
5 use Metricool\Support\Helpers\Storages\RequestStorage;
6 use Metricool\Http\Metricool\MetricoolApi;
7 use Metricool\Support\Helpers\Storages\EnvironmentConfig;
8
9 class DashboardService
10 {
11 public const ONBOARDING_COMPLETED_OPTION = 'metricool_onboarding_completed_at';
12 public const FORCED_LOGIN_OPTION = 'metricool_force_login';
13
14 private EnvironmentConfig $env;
15 private MetricoolApi $api;
16 private RequestStorage $request;
17
18 public function __construct(EnvironmentConfig $env, MetricoolApi $api, RequestStorage $request)
19 {
20 $this->env = $env;
21 $this->api = $api;
22 $this->request = $request;
23 }
24
25 /**
26 * Returns the current state of the onboarding process
27 *
28 * [
29 * // the onboarding is completed, the user can start using the plugin
30 * 'completed' => true,
31 * // the plugin is authenticated with the Metricool API
32 * 'authenticated' => true,
33 * // submit a blog_id when false
34 * 'blog_id_selected' => true
35 * ]
36 */
37 public function state(): array
38 {
39 return [
40 'completed' => $this->isOnboardingCompleted(),
41 'authenticated' => $this->api->hasAuthentication(),
42 'blog_id_selected' => $this->api->hasBlogId(),
43 ];
44 }
45
46 /**
47 * Returns the current mode of the onboarding process
48 *
49 * [
50 * // the welcome screen should be shown
51 * 'show_welcome_screen' => true,
52 * // force the user to log in with a forced login screen
53 * 'forced_login' => true
54 * ]
55 */
56 public function mode(): array
57 {
58 return [
59 'show_welcome_screen' => $this->shouldShowWelcomeScreen(),
60 'forced_login' => $this->isForcedLogin(),
61 ];
62 }
63
64 /**
65 * Enable the forced login screen
66 */
67 public function enableForcedLogin(): bool
68 {
69 return $this->setForcedLogin(true);
70 }
71
72 /**
73 * Disable the forced login state
74 */
75 public function disableForcedLogin(): bool
76 {
77 return delete_option(self::FORCED_LOGIN_OPTION);
78 }
79
80 /**
81 * Check if the front-end should show the forced login screen
82 */
83 public function isForcedLogin(): bool
84 {
85 return (bool) get_option(self::FORCED_LOGIN_OPTION, false);
86 }
87
88 /**
89 * Set the forced login state
90 */
91 public function setForcedLogin(bool $forcedLogin): bool
92 {
93 return update_option(self::FORCED_LOGIN_OPTION, $forcedLogin, false);
94 }
95
96 /**
97 * Check if the current admin screen is the Leadinfo dashboard page
98 */
99 public function isUserOnDashboard(): bool
100 {
101 $pageVisitedByUser = $this->request->getString('global.page');
102 $dashboardUrl = $this->env->getString('plugin.dashboard_url');
103
104 $pluginPageQueryString = wp_parse_url($dashboardUrl, PHP_URL_QUERY);
105 parse_str($pluginPageQueryString, $parsedQuery);
106 $pluginDashboardPage = ($parsedQuery['page'] ?? '');
107
108 return $pageVisitedByUser === $pluginDashboardPage;
109 }
110
111 /**
112 * Check if the onboarding was completed
113 */
114 public function isOnboardingCompleted(): bool
115 {
116 return $this->api->hasUserToken() && $this->api->hasBlogId() && get_option(self::ONBOARDING_COMPLETED_OPTION, false);
117 }
118
119 /**
120 * Completes the onboarding process by removing all onboarding data and storing the timestamp
121 */
122 public function setOnboardingCompleted(): bool
123 {
124 $timestamp = time();
125
126 do_action('metricool_onboarding_completed', $timestamp);
127
128 return update_option(self::ONBOARDING_COMPLETED_OPTION, $timestamp, false);
129 }
130
131 /**
132 * Check if the welcome screen should be shown
133 */
134 public function shouldShowWelcomeScreen(): bool
135 {
136 return $this->isOnboardingCompleted() && $this->showWelcomeScreenOnce();
137 }
138
139 /**
140 * Check if the welcome screen should be shown once
141 */
142 public function showWelcomeScreenOnce(): bool
143 {
144 $show = (bool) get_option('metricool_show_welcome_screen', false);
145 delete_option('metricool_show_welcome_screen');
146
147 return $show;
148 }
149
150 /**
151 * Set the welcome screen as shown
152 */
153 public function setShowWelcomeScreen(): void
154 {
155 update_option('metricool_show_welcome_screen', true, false);
156 }
157 }
158