-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclass-plugin-upgrade-tasks.php
More file actions
206 lines (170 loc) · 6.07 KB
/
class-plugin-upgrade-tasks.php
File metadata and controls
206 lines (170 loc) · 6.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
/**
* Handle plugin onboarding and upgrade task related functionality.
*
* @package Progress_Planner
*/
namespace Progress_Planner;
/**
* Plugin_Upgrade_Tasks class.
*/
class Plugin_Upgrade_Tasks {
/**
* Constructor.
*/
public function __construct() {
// Plugin (possibly 3rd party) activated.
\add_action( 'activated_plugin', [ $this, 'plugin_activated' ], 10 );
// Progress Planner plugin updated.
\add_action( 'progress_planner_plugin_updated', [ $this, 'plugin_updated' ], 10 );
// Check if the plugin was upgraded or new plugin was activated.
\add_action( 'init', [ $this, 'handle_activation_or_upgrade' ], 100 ); // We need to run this after the Local_Tasks_Manager::init() is called.
// Add the action to add the upgrade tasks popover.
\add_action( 'progress_planner_admin_page_after_widgets', [ $this, 'add_upgrade_tasks_popover' ] );
}
/**
* Plugin upgraded or (3rd party) plugin was activated.
*
* @param string $plugin The plugin file.
*
* @return void
*/
public function plugin_activated( $plugin ) {
if ( 'progress-planner/progress-planner.php' !== $plugin ) {
return;
}
\update_option( 'progress_planner_plugin_was_activated', true );
}
/**
* Plugin upgraded.
*
* @return void
*/
public function plugin_updated() {
\update_option( 'progress_planner_plugin_was_updated', true );
}
/**
* If the plugin was upgraded or new plugin was activated, check if we need to add onboarding tasks.
*
* @return void
*/
public function handle_activation_or_upgrade() {
// Plugin was installed.
if ( \get_option( 'progress_planner_plugin_was_activated', false ) ) {
$this->add_initial_onboarding_tasks();
\delete_option( 'progress_planner_plugin_was_activated' );
return;
}
// Plugin was updated.
if ( \get_option( 'progress_planner_plugin_was_updated', false ) ) {
$this->maybe_add_onboarding_tasks();
\delete_option( 'progress_planner_plugin_was_updated' );
return;
}
}
/**
* Add initial onboarding tasks.
*
* @return void
*/
protected function add_initial_onboarding_tasks() {
// Check if this is a fresh install (not a re-activation).
// If the option doesn't exist, it's a fresh install.
$fresh_install = false === \get_option( 'progress_planner_previous_version_task_providers', false );
// If this is the first time the plugin is installed, save the task providers.
if ( $fresh_install ) {
$onboard_task_provider_ids = \apply_filters( 'prpl_onboarding_task_providers', [] );
// Update 'progress_planner_previous_version_task_providers' option.
\update_option( 'progress_planner_previous_version_task_providers', \array_unique( $onboard_task_provider_ids ), SORT_REGULAR );
}
}
/**
* Add onboarding tasks.
*
* @return void
*/
public function maybe_add_onboarding_tasks() {
$onboard_task_provider_ids = \apply_filters( 'prpl_onboarding_task_providers', [] );
// Privacy policy is not accepted, so it's a fresh install.
$fresh_install = ! \progress_planner()->is_privacy_policy_accepted();
// Check if task providers option exists, it will not on fresh installs and v1.0.4 and older.
$old_task_providers = \get_option( 'progress_planner_previous_version_task_providers', [] );
// We're upgrading from v1.0.4 or older, set the old task providers to what we had before the upgrade.
if ( ! $fresh_install && empty( $old_task_providers ) ) {
$old_task_providers = [
'core-blogdescription',
'wp-debug-display',
'sample-page',
'hello-world',
'core-siteicon',
];
}
$newly_added_task_provider_ids = \get_option( 'progress_planner_upgrade_popover_task_provider_ids', [] );
foreach ( $onboard_task_provider_ids as $task_provider_id ) {
if ( ! empty( $task_provider_id ) && ! \in_array( $task_provider_id, $old_task_providers, true ) && ! \in_array( $task_provider_id, $newly_added_task_provider_ids, true ) ) {
$newly_added_task_provider_ids[] = $task_provider_id;
}
}
// Update 'progress_planner_previous_version_task_providers' option.
\update_option( 'progress_planner_previous_version_task_providers', \array_unique( \array_merge( $old_task_providers, $onboard_task_provider_ids ), SORT_REGULAR ) );
// Update 'progress_planner_upgrade_popover_task_providers' option.
\update_option( 'progress_planner_upgrade_popover_task_provider_ids', $newly_added_task_provider_ids );
}
/**
* Get the newly added task providers.
*
* @return array
*/
public function get_newly_added_task_providers() {
static $newly_added_task_providers;
if ( ! $this->should_show_upgrade_popover() ) {
return [];
}
if ( null === $newly_added_task_providers ) {
$task_provider_ids = $this->get_upgrade_popover_task_provider_ids();
$task_providers = [];
foreach ( $task_provider_ids as $task_provider_id ) {
$task_provider = \progress_planner()->get_suggested_tasks()->get_tasks_manager()->get_task_provider( $task_provider_id ); // @phpstan-ignore-line method.nonObject
if ( $task_provider ) { // @phpstan-ignore-line
$task_providers[] = $task_provider;
}
}
$newly_added_task_providers = $task_providers;
}
return $newly_added_task_providers;
}
/**
* Check if we should show the upgrade popover.
*
* @return bool
*/
public function should_show_upgrade_popover() {
return \progress_planner()->is_on_progress_planner_dashboard_page() && ! empty( $this->get_upgrade_popover_task_provider_ids() );
}
/**
* Get the upgrade popover task provider IDs.
*
* @return array
*/
public function get_upgrade_popover_task_provider_ids() {
return \get_option( 'progress_planner_upgrade_popover_task_provider_ids', [] );
}
/**
* Delete the upgrade popover task providers, for example after they've been shown.
*
* @return void
*/
public function delete_upgrade_popover_task_providers() {
\delete_option( 'progress_planner_upgrade_popover_task_provider_ids' );
}
/**
* Add the upgrade tasks popover.
*
* @return void
*/
public function add_upgrade_tasks_popover() {
if ( $this->should_show_upgrade_popover() ) {
\progress_planner()->get_ui__popover()->the_popover( 'upgrade-tasks' )->render();
}
}
}