-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathclass-activation-redirect.php
More file actions
110 lines (95 loc) · 2.65 KB
/
Copy pathclass-activation-redirect.php
File metadata and controls
110 lines (95 loc) · 2.65 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
<?php
/**
* Handles plugin activation redirect to welcome page.
*
* @package Accessibility_Checker\Admin
*/
namespace EqualizeDigital\AccessibilityChecker\Admin;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class Activation_Redirect
*
* Handles redirecting to the welcome page after plugin activation.
*
* @since 1.36.0
*/
class Activation_Redirect {
/**
* The page slug for the welcome page.
*
* @since 1.36.0
* @var string
*/
const WELCOME_PAGE_SLUG = 'accessibility_checker';
/**
* Initialize the activation redirect.
*
* @since 1.36.0
* @return void
*/
public function init(): void {
add_action( 'admin_init', [ $this, 'maybe_redirect_to_welcome' ] );
}
/**
* Get the welcome page URL.
*
* @since 1.36.0
* @return string The URL to the welcome page.
*/
public function get_welcome_page_url(): string {
return admin_url( 'admin.php?page=' . self::WELCOME_PAGE_SLUG );
}
/**
* Redirect to welcome page after activation if conditions are met.
*
* This will only redirect if:
* - The activation redirect transient is set
* - We're not doing an AJAX request
* - We're not in the network admin (multisite)
* - We're not activating multiple plugins at once
* - User has permission to access the welcome page
*
* @since 1.36.0
* @return void
*/
public function maybe_redirect_to_welcome(): void {
// Check if the activation redirect transient exists.
if ( ! get_transient( 'edac_activation_redirect' ) ) {
return;
}
// Don't redirect during AJAX requests.
if ( wp_doing_ajax() ) {
return;
}
// Don't redirect during REST API requests.
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
return;
}
// Don't redirect in network admin (multisite).
if ( is_network_admin() ) {
return;
}
// Don't redirect if multiple plugins are being activated at once.
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- We're checking $_GET, not processing form data.
if ( isset( $_GET['activate-multi'] ) ) {
return;
}
// Don't redirect if user doesn't have permission to see the welcome page.
// Uses 'edit_posts' to match the welcome page's capability check (see includes/options-page.php).
// This allows Authors and above to access the welcome page, as intended by the plugin design.
if ( ! current_user_can( 'edit_posts' ) ) {
return;
}
// Don't redirect if we're in a test environment.
if ( defined( 'WP_TESTS_DOMAIN' ) ) {
return;
}
// Delete the transient to prevent redirect loops.
delete_transient( 'edac_activation_redirect' );
// Perform the redirect to the welcome page.
wp_safe_redirect( $this->get_welcome_page_url() );
exit;
}
}