|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Handles plugin activation redirect to welcome page. |
| 4 | + * |
| 5 | + * @package Accessibility_Checker\Admin |
| 6 | + */ |
| 7 | + |
| 8 | +namespace EqualizeDigital\AccessibilityChecker\Admin; |
| 9 | + |
| 10 | +if ( ! defined( 'ABSPATH' ) ) { |
| 11 | + exit; // Exit if accessed directly. |
| 12 | +} |
| 13 | + |
| 14 | +/** |
| 15 | + * Class Activation_Redirect |
| 16 | + * |
| 17 | + * Handles redirecting to the welcome page after plugin activation. |
| 18 | + * |
| 19 | + * @since 1.36.0 |
| 20 | + */ |
| 21 | +class Activation_Redirect { |
| 22 | + |
| 23 | + /** |
| 24 | + * The page slug for the welcome page. |
| 25 | + * |
| 26 | + * @since 1.36.0 |
| 27 | + * @var string |
| 28 | + */ |
| 29 | + const WELCOME_PAGE_SLUG = 'accessibility_checker'; |
| 30 | + |
| 31 | + /** |
| 32 | + * Initialize the activation redirect. |
| 33 | + * |
| 34 | + * @since 1.36.0 |
| 35 | + */ |
| 36 | + public function init(): void { |
| 37 | + add_action( 'admin_init', [ $this, 'maybe_redirect_to_welcome' ] ); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Get the welcome page URL. |
| 42 | + * |
| 43 | + * @since 1.36.0 |
| 44 | + * @return string The URL to the welcome page. |
| 45 | + */ |
| 46 | + public function get_welcome_page_url(): string { |
| 47 | + return admin_url( 'admin.php?page=' . self::WELCOME_PAGE_SLUG ); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Redirect to welcome page after activation if conditions are met. |
| 52 | + * |
| 53 | + * This will only redirect if: |
| 54 | + * - The activation redirect transient is set |
| 55 | + * - We're not doing an AJAX request |
| 56 | + * - We're not in the network admin (multisite) |
| 57 | + * - We're not activating multiple plugins at once |
| 58 | + * - User has permission to access the welcome page |
| 59 | + * |
| 60 | + * @since 1.36.0 |
| 61 | + * @return void |
| 62 | + */ |
| 63 | + public function maybe_redirect_to_welcome(): void { |
| 64 | + // Check if the activation redirect transient exists. |
| 65 | + if ( ! get_transient( 'edac_activation_redirect' ) ) { |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + // Don't redirect during AJAX requests. |
| 70 | + if ( wp_doing_ajax() ) { |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + // Don't redirect during REST API requests. |
| 75 | + if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) { |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + // Don't redirect in network admin (multisite). |
| 80 | + if ( is_network_admin() ) { |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + // Don't redirect if multiple plugins are being activated at once. |
| 85 | + // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- We're checking $_GET, not processing form data. |
| 86 | + if ( isset( $_GET['activate-multi'] ) ) { |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + // Don't redirect if user doesn't have permission to see the welcome page. |
| 91 | + // Uses 'edit_posts' to match the welcome page's capability check (see includes/options-page.php). |
| 92 | + // This allows Authors and above to access the welcome page, as intended by the plugin design. |
| 93 | + if ( ! current_user_can( 'edit_posts' ) ) { |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + // Don't redirect if we're in a test environment. |
| 98 | + if ( defined( 'WP_TESTS_DOMAIN' ) ) { |
| 99 | + return; |
| 100 | + } |
| 101 | + |
| 102 | + // Delete the transient to prevent redirect loops. |
| 103 | + delete_transient( 'edac_activation_redirect' ); |
| 104 | + |
| 105 | + // Perform the redirect to the welcome page. |
| 106 | + wp_safe_redirect( $this->get_welcome_page_url() ); |
| 107 | + exit; |
| 108 | + } |
| 109 | +} |
0 commit comments