PluginProbe
UpStream: a Project Management Plugin for WordPress / 1.39.2
UpStream: a Project Management Plugin for WordPress v1.39.2
trunk 1.39.0 1.39.1 1.39.2 1.39.3 2.0.7 2.1.0
upstream / includes / up-install.php

up-install.php in UpStream: a Project Management Plugin for WordPress 1.39.2, at includes/up-install.php

988 lines 31.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // Exit if accessed directly
4 if ( ! defined('ABSPATH')) {
5 exit;
6 }
7
8 /**
9 * Install
10 *
11 * Runs on plugin install by setting up the post types, custom taxonomies,
12 * flushing rewrite rules to initiate the new 'downloads' slug and also
13 * creates the plugin and populates the settings fields for those plugin
14 * pages. After successful install, the user is redirected to the UpStream Welcome
15 * screen.
16 *
17 * @param bool $network_side If the plugin is being network-activated
18 *
19 * @return void
20 * @global $upstream_options
21 * @global $wp_version
22 *
23 * @since 1.0
24 * @global $wpdb
25 */
26
27 /**
28 * Check UpStream minimum requirements: PHP and WordPress versions.
29 * This function calls wp_die() if any of the minimum requirements is not satisfied.
30 *
31 * @since 1.10.1
32 *
33 * @uses wp_die()
34 */
35 function upstream_check_min_requirements()
36 {
37 global $wp_version;
38
39 $minWPVersionRequired = "4.5";
40 $minPHPVersionRequired = "5.6.20";
41
42 // Check PHP version.
43 if (version_compare(PHP_VERSION, $minPHPVersionRequired, '<')) {
44 $errorMessage = sprintf(
45 '<p>' . __('It seems you are running an outdated version of PHP: <code>%s</code>.', 'upstream') . '</p>' .
46 '<p>' . __(
47 'For security reasons, UpStream requires at least PHP version <code>%s</code> to run.',
48 'upstream'
49 ) . '</p>' .
50 '<p>' . __('Please, consider upgrading your PHP version.', 'upstream') . '</p><br /><br />',
51 PHP_VERSION,
52 $minPHPVersionRequired
53 );
54 } // Check WordPress version.
55 elseif (version_compare($wp_version, $minWPVersionRequired, '<')) {
56 $errorMessage = sprintf(
57 '<p>' . __(
58 'It seems you are running an outdated version of WordPress: <code>%s</code>.',
59 'upstream'
60 ) . '</p>' .
61 '<p>' . __(
62 'For security reasons, UpStream requires at least version <code>%s</code> to run.',
63 'upstream'
64 ) . '</p>' .
65 '<p>' . __('Please, consider upgrading your WordPress.', 'upstream') . '</p><br /><br />',
66 $wp_version,
67 $minWPVersionRequired
68 );
69 }
70
71 if (isset($errorMessage)) {
72 $errorMessage .= '<a class="button" href="javascript:history.back();">' . __('Go Back', 'upstream') . '</a>';
73
74 wp_die($errorMessage);
75 }
76 }
77
78
79 function upstream_install_debug($message)
80 {
81
82 $message = sprintf('[%s] %s', date('Y-m-d H:i:s T O'), $message) . "\n";
83 error_log($message, 3, str_replace('//', '/', WP_CONTENT_DIR . '/debug-upstream.log'));
84 }
85
86 function upstream_install($network_wide = false)
87 {
88 global $wpdb;
89
90 upstream_install_debug('upstream_check_min_requirements');
91 upstream_check_min_requirements();
92
93 if (is_multisite() && $network_wide) {
94 upstream_install_debug('is_multisite/network wide');
95
96 foreach ($wpdb->get_col("SELECT blog_id FROM $wpdb->blogs LIMIT 100") as $blog_id) {
97 upstream_install_debug('handling blog' . $blog_id);
98 switch_to_blog($blog_id);
99
100 upstream_install_debug('upstream_run_install');
101 upstream_run_install();
102
103 upstream_install_debug('restore_current_blog');
104 restore_current_blog();
105 }
106 } else {
107 upstream_install_debug('upstream_run_install');
108 upstream_run_install();
109 }
110
111 upstream_install_debug('flush_rewrite_rules');
112 flush_rewrite_rules();
113 }
114
115 register_activation_hook(UPSTREAM_PLUGIN_FILE, 'upstream_install');
116 register_deactivation_hook(UPSTREAM_PLUGIN_FILE, 'upstream_uninstall');
117 add_action('upstream_update_data', 'upstream_update_data', 10, 2);
118
119 /**
120 * Run the UpStream Install process
121 *
122 * @return void
123 * @since 2.5
124 */
125 function upstream_run_install()
126 {
127
128 upstream_install_debug('wp_get_current_user');
129 $user = wp_get_current_user();
130
131 upstream_install_debug('user->add_cap');
132 $user->add_cap('manage_upstream');
133
134 // Setup the Downloads Custom Post Type
135 upstream_install_debug('upstream_setup_post_types');
136 upstream_setup_post_types();
137
138 // Setup the Download Taxonomies
139 upstream_install_debug('upstream_setup_taxonomies');
140 upstream_setup_taxonomies();
141
142 // Add the default options
143 upstream_install_debug('upstream_add_default_options');
144 upstream_add_default_options();
145
146 // Clear the permalinks
147 upstream_install_debug('flush_rewrite_rules');
148 flush_rewrite_rules(false);
149
150 // Add upgraded_from option
151 upstream_install_debug('current_version = get_option');
152 $current_version = get_option('upstream_version', false);
153 $freshInstall = empty($current_version);
154
155 if ( ! $freshInstall) {
156 update_option('upstream_version_upgraded_from', $current_version);
157 }
158
159 upstream_install_debug('update_option');
160 update_option('upstream_version', UPSTREAM_VERSION);
161
162 // Create UpStream roles
163 upstream_install_debug('roles = new UpStream_Roles');
164 $roles = new UpStream_Roles;
165
166 upstream_install_debug('roles->add_roles');
167 $roles->add_roles();
168
169 if ($freshInstall) {
170 upstream_install_debug('upstream_run_fresh_install');
171 upstream_run_fresh_install();
172
173 // Make sure we don't redirect if activating from network, or bulk.
174 if ( ! is_network_admin() && ! isset($_GET['activate-multi'])) {
175 // Add the transient to redirect
176 upstream_install_debug('set_transient');
177 set_transient('_upstream_activation_redirect', true, 30);
178 }
179 } else {
180 upstream_install_debug('upstream_run_reinstall');
181 upstream_run_reinstall();
182
183 upstream_install_debug('do_action');
184 do_action('upstream_update_data', $current_version, UPSTREAM_VERSION);
185 }
186 }
187
188 /**
189 * Run the fresh UpStream Install process
190 *
191 * @return void
192 * @since 2.5
193 */
194 function upstream_run_fresh_install()
195 {
196 // Add default capabilities for roles.
197 $roles = new UpStream_Roles;
198 $roles->add_default_caps();
199 }
200
201 /**
202 * Run the UpStream Reinstall process
203 *
204 * @return void
205 * @since 2.5
206 */
207 function upstream_run_reinstall()
208 {
209
210 }
211
212 /**
213 * Runs the UpStream uninstall process.
214 */
215 function upstream_uninstall()
216 {
217
218 flush_rewrite_rules();
219
220 // RSD: deactivate any child plugins
221 if (is_plugin_active('UpStream-Reports-PDF/upstream-reports-pdf.php') ||
222 is_plugin_active('upstream-reports-pdf/upstream-reports-pdf.php')
223 ) {
224 add_action('update_option_active_plugins', 'upstream_deactivate_dependency_reports_pdf');
225 }
226 if (is_plugin_active('UpStream-Reports/upstream-reports.php') ||
227 is_plugin_active('upstream-reports/upstream-reports.php')
228 ) {
229 add_action('update_option_active_plugins', 'upstream_deactivate_dependency_reports');
230 }
231 if (is_plugin_active('UpStream-Copy-Project/upstream-copy-project.php') ||
232 is_plugin_active('upstream-copy-project/upstream-copy-project.php')
233 ) {
234 add_action('update_option_active_plugins', 'upstream_deactivate_dependency_copy_project');
235 }
236 if (is_plugin_active('UpStream-Custom-Fields/upstream-custom-fields.php') ||
237 is_plugin_active('upstream-custom-fields/upstream-custom-fields.php')
238 ) {
239 add_action('update_option_active_plugins', 'upstream_deactivate_dependency_custom_fields');
240 }
241 if (is_plugin_active('UpStream-Customizer/upstream-customizer.php') ||
242 is_plugin_active('upstream-customizer/upstream-customizer.php')
243 ) {
244 add_action('update_option_active_plugins', 'upstream_deactivate_dependency_customizer');
245 }
246 if (is_plugin_active('UpStream-Email-Notifications/upstream-email-notifications.php') ||
247 is_plugin_active('upstream-email-notifications/upstream-email-notifications.php')
248 ) {
249 add_action('update_option_active_plugins', 'upstream_deactivate_dependency_email_notifications');
250 }
251 if (is_plugin_active('UpStream-Calendar-View/upstream-calendar-view.php') ||
252 is_plugin_active('upstream-calendar-view/upstream-calendar-view.php')
253 ) {
254 add_action('update_option_active_plugins', 'upstream_deactivate_dependency_calendar_view');
255 }
256 if (is_plugin_active('UpStream-Frontend-Edit/upstream-frontend-edit.php') ||
257 is_plugin_active('upstream-frontend-edit/upstream-frontend-edit.php')
258 ) {
259 add_action('update_option_active_plugins', 'upstream_deactivate_dependency_frontend_edit');
260 }
261 if (is_plugin_active('UpStream-Project-Timeline/upstream-project-timeline.php') ||
262 is_plugin_active('upstream-project-timeline/upstream-project-timeline.php')
263 ) {
264 add_action('update_option_active_plugins', 'upstream_deactivate_dependency_project_timeline');
265 }
266
267 if (is_plugin_active('UpStream-Advanced-Permissions/upstream-advanced-permissions.php') ||
268 is_plugin_active('upstream-advanced-permissions/upstream-advanced-permissions.php')
269 ) {
270 add_action('update_option_active_plugins', 'upstream_deactivate_dependency_advanced_permissions');
271 }
272
273 if (is_plugin_active('UpStream-API/upstream-api.php') ||
274 is_plugin_active('upstream-api/upstream-api.php')
275 ) {
276 add_action('update_option_active_plugins', 'upstream_deactivate_dependency_api');
277 }
278
279 if (is_plugin_active('UpStream-Forms/upstream-forms.php') ||
280 is_plugin_active('upstream-forms/upstream-forms.php')
281 ) {
282 add_action('update_option_active_plugins', 'upstream_deactivate_dependency_forms');
283 }
284
285 if (is_plugin_active('UpStream-Shortcodes/upstream-shortcodes.php') ||
286 is_plugin_active('upstream-shortcodes/upstream-shortcodes.php')
287 ) {
288 add_action('update_option_active_plugins', 'upstream_deactivate_dependency_shortcodes');
289 }
290 }
291
292
293 function upstream_deactivate_dependency_shortcodes()
294
295 {
296 deactivate_plugins(['UpStream-Shortcodes/upstream-shortcodes.php',
297 'upstream-shortcodes/upstream-shortcodes.php']
298 );
299 }
300
301 function upstream_deactivate_dependency_forms()
302
303 {
304 deactivate_plugins(['UpStream-Forms/upstream-forms.php',
305 'upstream-forms/upstream-forms.php']
306 );
307 }
308
309 function upstream_deactivate_dependency_api()
310
311 {
312 deactivate_plugins(['UpStream-API/upstream-api.php',
313 'upstream-api/upstream-api.php']
314 );
315 }
316
317
318 function upstream_deactivate_dependency_advanced_permissions()
319
320 {
321 deactivate_plugins(['UpStream-Advanced-Permissions/upstream-advanced-permissions.php',
322 'upstream-advanced-permissions/upstream-advanced-permissions.php']
323 );
324 }
325
326
327
328
329 function upstream_deactivate_dependency_calendar_view()
330
331 {
332 deactivate_plugins(['UpStream-Calendar-View/upstream-calendar-view.php',
333 'upstream-calendar-view/upstream-calendar-view.php']
334 );
335 }
336
337 function upstream_deactivate_dependency_reports_pdf()
338
339 {
340 deactivate_plugins(['UpStream-Reports-PDF/upstream-reports-pdf.php',
341 'upstream-reports-pdf/upstream-reports-pdf.php']);
342 }
343
344 function upstream_deactivate_dependency_reports()
345
346 {
347 deactivate_plugins(['UpStream-Reports/upstream-reports.php',
348 'upstream-reports/upstream-reports.php']);
349 }
350
351 function upstream_deactivate_dependency_copy_project()
352
353 {
354 deactivate_plugins(['UpStream-Copy-Project/upstream-copy-project.php',
355 'upstream-copy-project/upstream-copy-project.php']);
356 }
357
358 function upstream_deactivate_dependency_project_timeline()
359
360 {
361 deactivate_plugins(['UpStream-Project-Timeline/upstream-project-timeline.php',
362 'upstream-project-timeline/upstream-project-timeline.php']);
363 }
364
365 function upstream_deactivate_dependency_frontend_edit()
366
367 {
368 deactivate_plugins(['UpStream-Frontend-Edit/upstream-frontend-edit.php',
369 'upstream-frontend-edit/upstream-frontend-edit.php']);
370 }
371
372 function upstream_deactivate_dependency_email_notifications()
373
374 {
375 deactivate_plugins(['UpStream-Email-Notifications/upstream-email-notifications.php',
376 'upstream-email-notifications/upstream-email-notifications.php']);
377 }
378
379 function upstream_deactivate_dependency_customizer()
380
381 {
382 deactivate_plugins(['UpStream-Customizer/upstream-customizer.php',
383 'upstream-customizer/upstream-customizer.php']);
384 }
385
386 function upstream_deactivate_dependency_custom_fields()
387
388 {
389 deactivate_plugins(['UpStream-Custom-Fields/upstream-custom-fields.php',
390 'upstream-custom-fields/upstream-custom-fields.php']);
391 }
392
393 function upstream_add_default_options()
394 {
395 // general options
396 $general = get_option('upstream_general');
397 if ( ! $general || empty($general)) {
398 $general['project']['single'] = 'Project';
399 $general['project']['plural'] = 'Projects';
400 $general['client']['single'] = 'Client';
401 $general['client']['plural'] = 'Clients';
402 $general['milestone']['single'] = 'Milestone';
403 $general['milestone']['plural'] = 'Milestones';
404 $general['milestone_category']['single'] = 'Milestone Category';
405 $general['milestone_category']['plural'] = 'Milestone Categories';
406 $general['task']['single'] = 'Task';
407 $general['task']['plural'] = 'Tasks';
408 $general['bug']['single'] = 'Bug';
409 $general['bug']['plural'] = 'Bugs';
410 $general['file']['single'] = 'File';
411 $general['file']['plural'] = 'Files';
412
413 $general['login_heading'] = 'Project Login';
414 $general['admin_email'] = get_bloginfo('admin_email');
415
416 update_option('upstream_general', $general);
417 }
418
419 $cachedIds = [];
420 $generateRandomId = function () use (&$cachedIds) {
421 do {
422 $randomId = upstreamGenerateRandomString(5, 'abcdefghijklmnopqrstuvwxyz0123456789');
423 } while (isset($cachedIds[$randomId])); // Isset is faster than in_array in this case.
424
425 $cachedIds[$randomId] = null;
426
427 return $randomId;
428 };
429
430 // project options
431 $projects = get_option('upstream_projects');
432 if ( ! $projects || empty($projects)) {
433 $projects['statuses'][0]['name'] = 'In Progress';
434 $projects['statuses'][0]['color'] = '#5cbfd1';
435 $projects['statuses'][0]['type'] = 'open';
436 $projects['statuses'][0]['id'] = $generateRandomId();
437
438 $projects['statuses'][1]['name'] = 'Overdue';
439 $projects['statuses'][1]['color'] = '#d15d5c';
440 $projects['statuses'][1]['type'] = 'open';
441 $projects['statuses'][1]['id'] = $generateRandomId();
442
443 $projects['statuses'][2]['name'] = 'Closed';
444 $projects['statuses'][2]['color'] = '#6b6b6b';
445 $projects['statuses'][2]['type'] = 'closed';
446 $projects['statuses'][2]['id'] = $generateRandomId();
447
448 update_option('upstream_projects', $projects);
449 }
450
451 // task options
452 $tasks = get_option('upstream_tasks');
453 if ( ! $tasks || empty($tasks)) {
454 $tasks['statuses'][0]['name'] = 'In Progress';
455 $tasks['statuses'][0]['color'] = '#5cbfd1';
456 $tasks['statuses'][0]['type'] = 'open';
457 $tasks['statuses'][0]['id'] = $generateRandomId();
458
459 $tasks['statuses'][1]['name'] = 'Overdue';
460 $tasks['statuses'][1]['color'] = '#d15d5c';
461 $tasks['statuses'][1]['type'] = 'open';
462 $tasks['statuses'][1]['id'] = $generateRandomId();
463
464 $tasks['statuses'][2]['name'] = 'Completed';
465 $tasks['statuses'][2]['color'] = '#5cd165';
466 $tasks['statuses'][2]['type'] = 'closed';
467 $tasks['statuses'][2]['id'] = $generateRandomId();
468
469 $tasks['statuses'][3]['name'] = 'Closed';
470 $tasks['statuses'][3]['color'] = '#6b6b6b';
471 $tasks['statuses'][3]['type'] = 'closed';
472 $tasks['statuses'][3]['id'] = $generateRandomId();
473
474 update_option('upstream_tasks', $tasks);
475 }
476
477 // bug options
478 $bugs = get_option('upstream_bugs');
479 if ( ! $bugs || empty($bugs)) {
480 $bugs['statuses'][0]['name'] = 'In Progress';
481 $bugs['statuses'][0]['color'] = '#5cbfd1';
482 $bugs['statuses'][0]['type'] = 'open';
483 $bugs['statuses'][0]['id'] = $generateRandomId();
484
485 $bugs['statuses'][1]['name'] = 'Overdue';
486 $bugs['statuses'][1]['color'] = '#d15d5c';
487 $bugs['statuses'][1]['type'] = 'open';
488 $bugs['statuses'][1]['id'] = $generateRandomId();
489
490 $bugs['statuses'][2]['name'] = 'Completed';
491 $bugs['statuses'][2]['color'] = '#5cd165';
492 $bugs['statuses'][2]['type'] = 'closed';
493 $bugs['statuses'][2]['id'] = $generateRandomId();
494
495 $bugs['statuses'][3]['name'] = 'Closed';
496 $bugs['statuses'][3]['color'] = '#6b6b6b';
497 $bugs['statuses'][3]['type'] = 'closed';
498 $bugs['statuses'][3]['id'] = $generateRandomId();
499
500 $bugs['severities'][0]['name'] = 'Critical';
501 $bugs['severities'][0]['color'] = '#d15d5c';
502 $bugs['severities'][0]['id'] = $generateRandomId();
503
504 $bugs['severities'][1]['name'] = 'Standard';
505 $bugs['severities'][1]['color'] = '#d17f5c';
506 $bugs['severities'][1]['id'] = $generateRandomId();
507
508 $bugs['severities'][2]['name'] = 'Minor';
509 $bugs['severities'][2]['color'] = '#d1a65c';
510 $bugs['severities'][2]['id'] = $generateRandomId();
511
512 update_option('upstream_bugs', $bugs);
513 }
514 }
515
516
517 if (version_compare(get_bloginfo('version'),'5.1', '>=')) {
518
519 function upstream_new_blog_created($site)
520 {
521 upstream_install_debug('upstream_new_blog_created ' . $site);
522
523 $blog_id = $site->blog_id;
524
525 if (is_plugin_active_for_network(plugin_basename(UPSTREAM_PLUGIN_FILE))) {
526
527 upstream_install_debug('switch_to_blog');
528 switch_to_blog($blog_id);
529
530 upstream_install_debug('upstream_install');
531 upstream_install();
532
533 upstream_install_debug('restore_current_blog');
534 restore_current_blog();
535 }
536 }
537
538 add_action('wp_insert_site', 'upstream_new_blog_created', 10, 6);
539
540 } else {
541
542 /**
543 * When a new Blog is created in multisite, see if UpStream is network activated, and run the installer
544 *
545 * @param int $blog_id The Blog ID created
546 * @param int $user_id The User ID set as the admin
547 * @param string $domain The URL
548 * @param string $path Site Path
549 * @param int $site_id The Site ID
550 * @param array $meta Blog Meta
551 *
552 * @return void
553 * @since 1.0.0
554 *
555 */
556 function upstream_new_blog_created($blog_id, $user_id, $domain, $path, $site_id, $meta)
557 {
558 up_debug();
559
560 if (is_plugin_active_for_network(plugin_basename(UPSTREAM_PLUGIN_FILE))) {
561 switch_to_blog($blog_id);
562 upstream_install();
563 restore_current_blog();
564 }
565 }
566
567 add_action('wpmu_new_blog', 'upstream_new_blog_created', 10, 6);
568
569 }
570
571 /**
572 * Post-installation
573 *
574 * Runs just after plugin installation and exposes the
575 * upstream_after_install hook.
576 *
577 * @return void
578 * @since 1.0.0
579 */
580 function upstream_after_install()
581 {
582 up_debug();
583
584 if ( ! is_admin()) {
585 return;
586 }
587
588 $activated = get_transient('_upstream_activation_redirect');
589
590 if (false !== $activated) {
591
592 // add the default options
593 //upstream_add_default_project();
594 delete_transient('_upstream_activation_redirect');
595
596 if ( ! isset($_GET['activate-multi'])) {
597 set_transient('_upstream_redirected', true, 360);
598 wp_redirect(admin_url('post-new.php?post_type=project'));
599 exit;
600 }
601 }
602 }
603
604 add_action('admin_init', 'upstream_after_install', 100);
605
606
607 /**
608 * Show a success notice after install and redirect.
609 */
610 function upstream_install_success_notice()
611 {
612 up_debug();
613
614 $redirected = get_transient('_upstream_redirected');
615
616 if (false !== $redirected && isset($_GET['page']) && sanitize_text_field($_GET['page']) == 'upstream_general') {
617 // Delete the transient
618 //delete_transient( '_upstream_redirected' );
619
620 $class = 'notice notice-info is-dismissible';
621 $message = '<strong>' . __('Success! UpStream is up and running.', 'upstream') . '</strong><br>';
622 $message .= __(
623 'Step 1. Please go through each settings tab below and configure the options.',
624 'upstream'
625 ) . '<br>';
626 $message .= __(
627 'Step 2. Add a new Client by navigating to <strong>Projects > New Client</strong>',
628 'upstream'
629 ) . '<br>';
630 $message .= __(
631 'Step 3. Add your first Project by navigating to <strong>Projects > New Project</strong>',
632 'upstream'
633 ) . '<br>';
634
635 printf('<div class="%1$s"><p>%2$s</p></div>', $class, esc_html($message));
636 }
637 }
638
639 add_action('admin_notices', 'upstream_install_success_notice');
640
641
642 /**
643 * The original data update function. This is superceded by rev2 as of version 1.27.
644 *
645 * @param $old_version
646 * @param $new_version
647 * @throws Exception
648 */
649 function upstream_update_data($old_version, $new_version)
650 {
651 up_debug();
652
653 // Ignore if we are on the same version.
654 if ($old_version === $new_version) {
655 return;
656 }
657
658 if (version_compare($new_version, '1.22.0', '=')) {
659 // Make sure administrator and managers are able to
660 $roles = [
661 'upstream_manager',
662 'administrator',
663 'upstream_user',
664 ];
665
666 foreach ($roles as $role) {
667 $role = get_role($role);
668
669 if (is_object($role)) {
670 $role->add_cap('project_title_field', true);
671 $role->add_cap('project_status_field', true);
672 $role->add_cap('project_owner_field', true);
673 $role->add_cap('project_client_field', true);
674 $role->add_cap('project_users_field', true);
675 $role->add_cap('project_start_date_field', true);
676 $role->add_cap('project_end_date_field', true);
677 }
678 }
679 }
680
681 if (version_compare($old_version, '1.22.1', '<')) {
682 // Force to fix bug statuses and severities with empty ID.
683 delete_option('upstream:created_bugs_args_ids');
684
685 UpStream_Options_Bugs::createBugsStatusesIds();
686 }
687
688 $hasFinishedMigration = get_option('_upstream_migration_finished_1.24.0', null);
689 if (empty($hasFinishedMigration) && version_compare($old_version, '1.24.0', '<')) {
690 $this->upstreamMilestoneTags();
691
692 // Default labels for Milestone Categories
693 $general = get_option('upstream_general');
694
695 $general['milestone_category']['single'] = 'Milestone Category';
696 $general['milestone_category']['plural'] = 'Milestone Categories';
697
698 update_option('upstream_general', $general);
699
700
701 // Make sure administrator and managers are able to work with the new milestones.
702 $roles = [
703 'upstream_manager',
704 'administrator',
705 'upstream_user',
706 ];
707
708 foreach ($roles as $role) {
709 $role = get_role($role);
710
711 if (is_object($role)) {
712 $capabilities = [
713 // Post type
714 'edit_milestone',
715 'read_milestone',
716 'delete_milestone',
717 'edit_milestones',
718 'edit_others_milestones',
719 'publish_milestones',
720 'read_private_milestones',
721 'delete_milestones',
722 'delete_private_milestones',
723 'delete_published_milestones',
724 'delete_others_milestones',
725 'edit_private_milestones',
726 'edit_published_milestones',
727
728 // Terms
729 'manage_milestone_terms',
730 'edit_milestone_terms',
731 'delete_milestone_terms',
732 'assign_milestone_terms',
733 ];
734
735 foreach ($capabilities as $capability) {
736 $role->add_cap($capability, true);
737 }
738 }
739 }
740
741 // If we have projects, create new milestones based on current ones.
742 $projects = get_posts(
743 [
744 'post_type' => 'project',
745 'post_status' => 'any',
746 'posts_per_page' => -1,
747 'meta_query' => [
748 'relation' => 'OR',
749 [
750 'key' => '_upstream_milestones_migrated',
751 'compare' => 'NOT EXISTS',
752 ],
753 [
754 'key' => '_upstream_milestones_migrated',
755 'value' => 1,
756 'compare' => '!=',
757 ],
758 ],
759 ]
760 );
761
762 if ( ! empty($projects)) {
763 // Migrate the milestones.
764 $defaultMilestones = get_option('upstream_milestones', []);
765
766 if ( ! empty($defaultMilestones)) {
767 foreach ($projects as $project) {
768 \UpStream\Milestones::migrateLegacyMilestonesForProject($project->ID);
769 }
770 }
771 }
772
773 update_option('_upstream_migration_finished_1.24.0', true);
774 }
775
776 $hasFinishedMigration = get_option('_upstream_migration_finished_1.24.1', null);
777 if (empty($hasFinishedMigration) && version_compare($old_version, '1.24.1', '<')) {
778 // If we have unpublished projects, create new milestones based on current ones.
779 $projects = get_posts(
780 [
781 'post_type' => 'project',
782 'post_status' => 'any',
783 'posts_per_page' => -1,
784 'meta_query' => [
785 'relation' => 'OR',
786 [
787 'key' => '_upstream_milestones_migrated',
788 'compare' => 'NOT EXISTS',
789 ],
790 [
791 'key' => '_upstream_milestones_migrated',
792 'value' => 1,
793 'compare' => '!=',
794 ],
795 ],
796 ]
797 );
798
799 if ( ! empty($projects)) {
800 // Migrate the milestones.
801 $defaultMilestones = get_option('upstream_milestones', []);
802
803 if ( ! empty($defaultMilestones)) {
804 foreach ($projects as $project) {
805 \UpStream\Milestones::migrateLegacyMilestonesForProject($project->ID);
806 }
807 }
808 }
809
810 update_option('_upstream_migration_finished_1.24.1', true);
811 }
812
813 $version = '1.24.2';
814 $migrationOption = '_upstream_migration_finished_' . $version;
815 $hasFinishedMigration = get_option($migrationOption, null);
816 if (empty($hasFinishedMigration) && version_compare($old_version, $version, '<')) {
817 // If we have unpublished projects, create new milestones based on current ones.
818 $projects = get_posts(
819 [
820 'post_type' => 'project',
821 'post_status' => 'any',
822 'posts_per_page' => -1,
823 ]
824 );
825
826 if ( ! empty($projects)) {
827 foreach ($projects as $project) {
828 \UpStream\Milestones::fixMilestoneOrdersOnProject($project->ID);
829 }
830 }
831
832 update_option($migrationOption, true);
833 }
834
835
836 // do the first new migration to get everything to 1.27
837
838 $migrationId = 'M0000001';
839 $migrationOption = '_upstream_migration_finished_' . $migrationId;
840 $hasFinishedMigration = get_option($migrationOption, null);
841 if (empty($hasFinishedMigration)) {
842
843 if ( ! class_exists('Upstream_Counts')) {
844 include_once UPSTREAM_PLUGIN_DIR . '/includes/class-up-counts.php';
845 }
846
847 $counts = new Upstream_Counts(0);
848 $projects = $counts->projects;
849
850 if ( ! empty($projects)) {
851 foreach ($projects as $project) {
852 $projectObject = new UpStream_Project($project->ID);
853 $projectObject->update_project_meta();
854 }
855 }
856
857 update_option($migrationOption, true);
858 }
859
860 }
861
862 /**
863 * Show a message if the plugin needs to be reactivated
864 */
865 function upstream_reactivate_notice()
866 {
867 up_debug();
868
869 $class = 'notice notice-info is-dismissible';
870 $message = '<strong>' . __('UpStream needs to be reactivated.', 'upstream') . '</strong><br>';
871 $message .= __(
872 'In order to complete the upgrade to this version, you will need to deactivate and re-activate Upstream. Make sure Remove Data under the UpStream menu is unchecked so you do not lose any data.',
873 'upstream'
874 ) . '<br>';
875
876 printf('<div class="%1$s"><p>%2$s</p></div>', $class, $message);
877 }
878
879
880 /**
881 * This is a replacement for upstream_update_data(). It is used to update any data from the
882 * 1.27 release and after.
883 *
884 * @since 1.27
885 */
886 function upstream_update_data_rev_2()
887 {
888 if (!is_admin() || !current_user_can('activate_plugins')) {
889 return;
890 }
891
892 $current_version = get_option('upstream_version', false);
893
894 if (empty($current_version)) {
895
896 // if current version is not set, this is a new install
897
898 }
899
900 else if ($current_version === UPSTREAM_VERSION) {
901
902 // already at current verison... check if last migration was done
903 // (meaning all should have been done) otherwise return
904
905 $migrationId = 'M0000002';
906 $migrationOption = '_upstream_migration_finished_' . $migrationId;
907 $hasFinishedMigration = get_option($migrationOption, null);
908
909 if ($hasFinishedMigration) {
910 return;
911 } else {
912 $hasFinishedMigration = false;
913 }
914 }
915
916 else if (version_compare($current_version, '1.26.0', '<')) {
917
918 // if current version is less than 1.26 don't use this function at all
919 // wait until someone reactivates the plugin and then this function can run.
920 add_action('admin_notices', 'upstream_reactivate_notice');
921
922 return;
923 }
924
925 $migrationId = 'M0000001';
926 $migrationOption = '_upstream_migration_finished_' . $migrationId;
927 $hasFinishedMigration = get_option($migrationOption, null);
928 if (empty($hasFinishedMigration)) {
929
930 if ( ! class_exists('Upstream_Counts')) {
931 include_once UPSTREAM_PLUGIN_DIR . '/includes/class-up-counts.php';
932 }
933
934 $counts = new Upstream_Counts(0);
935 $projects = $counts->projects;
936
937 if ( ! empty($projects)) {
938 foreach ($projects as $project) {
939 $projectObject = new UpStream_Project($project->ID);
940 $projectObject->update_project_meta();
941 }
942 }
943
944 update_option($migrationOption, true);
945 }
946
947
948
949 $migrationId = 'M0000002';
950 $migrationOption = '_upstream_migration_finished_' . $migrationId;
951 $hasFinishedMigration = get_option($migrationOption, null);
952 if (empty($hasFinishedMigration)) {
953
954 global $wpdb;
955
956 require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
957
958 if (function_exists('maybe_create_table')) {
959 $sql = 'CREATE TABLE ' . $wpdb->prefix . 'upfs_files ( upfsid VARCHAR(60), orig_filename VARCHAR(255), saved_filename VARCHAR(255), mime_type VARCHAR(255), file_size INT, access_rules TEXT, PRIMARY KEY (upfsid) )';
960 $r = maybe_create_table($wpdb->prefix . 'upfs_files', $sql);
961 }
962
963 update_option($migrationOption, true);
964 }
965
966
967
968 // if we made any migrations, update the upstream version to the current one
969 update_option('upstream_version_upgraded_from', $current_version);
970 update_option('upstream_version', UPSTREAM_VERSION);
971
972 }
973
974 add_action('admin_init', 'upstream_update_data_rev_2', 90);
975
976
977
978 function upstream_change_role_name() {
979 global $wp_roles;
980
981 if ( ! isset( $wp_roles ) )
982 $wp_roles = new WP_Roles();
983
984 $wp_roles->roles['upstream_client_user']['name'] = __('UpStream Client User', 'upstream');
985 $wp_roles->role_names['upstream_client_user'] = __('UpStream Client User', 'upstream');
986
987 }
988 add_action('init', 'upstream_change_role_name');