AddOns.php
4 years ago
Categories.php
3 years ago
Packages.php
4 days ago
Settings.php
5 months ago
Stats.php
4 years ago
Templates.php
2 years ago
Welcome.php
5 months ago
Welcome.php
166 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPDM\Admin\Menu; |
| 4 | |
| 5 | class Welcome |
| 6 | { |
| 7 | function __construct() |
| 8 | { |
| 9 | add_action('admin_menu', array($this, 'Menu')); |
| 10 | add_action('admin_init', array($this, 'maybeRedirect'), 1); // Priority 1 to run early |
| 11 | add_action('wp_ajax_wpdm_create_dashboard_page', array($this, 'createDashboardPage')); |
| 12 | |
| 13 | // Handle immediate redirect after plugin activation (non-AJAX activations) |
| 14 | add_action('activated_plugin', array($this, 'activationRedirect'), 10, 2); |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * Immediate redirect after plugin activation (for non-AJAX activations) |
| 19 | */ |
| 20 | function activationRedirect($plugin, $network_wide) |
| 21 | { |
| 22 | // Only for this plugin (WPDM_BASE_DIR already has trailing slash) |
| 23 | if ($plugin !== plugin_basename(WPDM_BASE_DIR . 'download-manager.php')) { |
| 24 | return; |
| 25 | } |
| 26 | |
| 27 | // Skip for network activation |
| 28 | if ($network_wide) { |
| 29 | return; |
| 30 | } |
| 31 | |
| 32 | // Skip for bulk activations (check both the action and the flag) |
| 33 | // During bulk activation, action is 'activate-selected', activate-multi is set after redirect |
| 34 | if (isset($_GET['activate-multi']) || |
| 35 | (isset($_REQUEST['action']) && $_REQUEST['action'] === 'activate-selected') || |
| 36 | (isset($_REQUEST['action2']) && $_REQUEST['action2'] === 'activate-selected') || |
| 37 | (isset($_POST['checked']) && is_array($_POST['checked']) && count($_POST['checked']) > 1)) { |
| 38 | return; |
| 39 | } |
| 40 | |
| 41 | // Skip for AJAX requests (will be handled by maybeRedirect on next page load) |
| 42 | if (wp_doing_ajax()) { |
| 43 | return; |
| 44 | } |
| 45 | |
| 46 | // Skip if headers already sent |
| 47 | if (headers_sent()) { |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | // Redirect immediately |
| 52 | wp_safe_redirect(admin_url('index.php?page=wpdm-welcome')); |
| 53 | exit; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * AJAX handler to create dashboard page |
| 58 | */ |
| 59 | function createDashboardPage() |
| 60 | { |
| 61 | if (!wp_verify_nonce($_POST['_wpnonce'] ?? '', 'wpdm_create_dashboard')) { |
| 62 | wp_send_json_error(__('Security check failed', 'download-manager')); |
| 63 | } |
| 64 | |
| 65 | if (!current_user_can('publish_pages')) { |
| 66 | wp_send_json_error(__('Permission denied', 'download-manager')); |
| 67 | } |
| 68 | |
| 69 | $page_id = wp_insert_post([ |
| 70 | 'post_title' => __('User Dashboard', 'download-manager'), |
| 71 | 'post_content' => '[wpdm_user_dashboard]', |
| 72 | 'post_status' => 'publish', |
| 73 | 'post_type' => 'page', |
| 74 | ]); |
| 75 | |
| 76 | if (is_wp_error($page_id)) { |
| 77 | wp_send_json_error($page_id->get_error_message()); |
| 78 | } |
| 79 | |
| 80 | update_option('__wpdm_user_dashboard', $page_id); |
| 81 | |
| 82 | wp_send_json_success([ |
| 83 | 'page_id' => $page_id, |
| 84 | 'title' => get_the_title($page_id), |
| 85 | 'edit_url' => get_edit_post_link($page_id, 'raw'), |
| 86 | ]); |
| 87 | } |
| 88 | |
| 89 | function Menu() |
| 90 | { |
| 91 | add_dashboard_page('Welcome', 'Welcome', 'read', 'wpdm-welcome', array($this, 'UI')); |
| 92 | } |
| 93 | |
| 94 | function UI() |
| 95 | { |
| 96 | update_option('__wpdm_welcome', WPDM_VERSION, false); |
| 97 | delete_transient('wpdm_activation_redirect'); |
| 98 | delete_option('__wpdm_activation_redirect'); |
| 99 | //remove_submenu_page('index.php', 'wpdm-welcome'); |
| 100 | include wpdm_admin_tpl_path('welcome.php', dirname(__DIR__) . '/views'); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Redirect to welcome page if activation flag is set |
| 105 | * The flag is set in WordPressDownloadManager::install() via register_activation_hook |
| 106 | */ |
| 107 | function maybeRedirect() |
| 108 | { |
| 109 | // Check for redirect flag (transient or option as backup) |
| 110 | $redirect_transient = get_transient('wpdm_activation_redirect'); |
| 111 | $redirect_option = get_option('__wpdm_activation_redirect'); |
| 112 | |
| 113 | if ($redirect_transient !== 'yes' && $redirect_option !== 'yes') { |
| 114 | return; |
| 115 | } |
| 116 | |
| 117 | // Delete the flags immediately to prevent redirect loops |
| 118 | delete_transient('wpdm_activation_redirect'); |
| 119 | delete_option('__wpdm_activation_redirect'); |
| 120 | |
| 121 | // Skip for WP-CLI |
| 122 | if (defined('WP_CLI') && WP_CLI) { |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | // Skip for bulk activations |
| 127 | if (isset($_GET['activate-multi'])) { |
| 128 | return; |
| 129 | } |
| 130 | |
| 131 | // Skip for AJAX requests |
| 132 | if (wp_doing_ajax()) { |
| 133 | return; |
| 134 | } |
| 135 | |
| 136 | // Skip for cron |
| 137 | if (wp_doing_cron()) { |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | // Skip for REST API requests |
| 142 | if (defined('REST_REQUEST') && REST_REQUEST) { |
| 143 | return; |
| 144 | } |
| 145 | |
| 146 | // Skip if user doesn't have capability |
| 147 | if (!current_user_can('manage_options')) { |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | // Skip if headers already sent |
| 152 | if (headers_sent()) { |
| 153 | return; |
| 154 | } |
| 155 | |
| 156 | // Skip if already on the welcome page |
| 157 | if (isset($_GET['page']) && $_GET['page'] === 'wpdm-welcome') { |
| 158 | return; |
| 159 | } |
| 160 | |
| 161 | // Redirect to welcome page |
| 162 | wp_safe_redirect(admin_url('index.php?page=wpdm-welcome')); |
| 163 | exit; |
| 164 | } |
| 165 | } |
| 166 |