| @@ -1,0 +1,308 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace AcyMailing\WpInit; | |
| 4 | + | |
| 5 | +defined('ABSPATH') || die('Restricted Access'); | |
| 6 | + | |
| 7 | +use AcyMailing\Classes\PluginClass; | |
| 8 | + | |
| 9 | +class Router | |
| 10 | +{ | |
| 11 | + public function __construct() | |
| 12 | + { | |
| 13 | + // Back router | |
| 14 | + add_action('wp_ajax_acymailing_router', [$this, 'router']); | |
| 15 | + // Front router | |
| 16 | + if (!acym_isAdmin()) { | |
| 17 | + add_action('wp_loaded', [$this, 'frontRouter']); | |
| 18 | + } | |
| 19 | + | |
| 20 | + // Make sure we can redirect / download / modify headers if needed after some checks | |
| 21 | + $pages = [ | |
| 22 | + 'automation', | |
| 23 | + 'bounces', | |
| 24 | + 'campaigns', | |
| 25 | + 'configuration', | |
| 26 | + 'dashboard', | |
| 27 | + 'dynamics', | |
| 28 | + 'fields', | |
| 29 | + 'file', | |
| 30 | + 'followups', | |
| 31 | + 'forms', | |
| 32 | + 'language', | |
| 33 | + 'lists', | |
| 34 | + 'mails', | |
| 35 | + 'override', | |
| 36 | + 'queue', | |
| 37 | + 'segments', | |
| 38 | + 'stats', | |
| 39 | + 'users', | |
| 40 | + ]; | |
| 41 | + $headerPages = [ | |
| 42 | + 'automation', | |
| 43 | + 'bounces', | |
| 44 | + 'campaigns', | |
| 45 | + 'configuration', | |
| 46 | + 'dashboard', | |
| 47 | + 'fields', | |
| 48 | + 'followups', | |
| 49 | + 'forms', | |
| 50 | + 'lists', | |
| 51 | + 'mails', | |
| 52 | + 'override', | |
| 53 | + 'queue', | |
| 54 | + 'segments', | |
| 55 | + 'stats', | |
| 56 | + 'users', | |
| 57 | + ]; | |
| 58 | + foreach ($pages as $page) { | |
| 59 | + if (in_array($page, $headerPages)) { | |
| 60 | + // Ensure we can set headers in the plugin | |
| 61 | + add_action('load-acymailing_page_acymailing_'.$page, [$this, 'waitHeaders']); | |
| 62 | + } | |
| 63 | + // Disable WP emojis in AcyMailing only | |
| 64 | + add_action('admin_print_scripts-acymailing_page_acymailing_'.$page, [$this, 'disableJsBreakingPages']); | |
| 65 | + add_action('admin_print_styles-acymailing_page_acymailing_'.$page, [$this, 'removeCssBreakingPages']); | |
| 66 | + } | |
| 67 | + add_action('admin_print_scripts-toplevel_page_acymailing_dashboard', [$this, 'disableJsBreakingPages']); | |
| 68 | + add_action('admin_print_styles-toplevel_page_acymailing_dashboard', [$this, 'removeCssBreakingPages']); | |
| 69 | + add_action('wp_enqueue_media', [$this, 'protectAcyMailingPages'], 100); | |
| 70 | + add_action('init', [$this, 'autologin']); | |
| 71 | + } | |
| 72 | + | |
| 73 | + public function protectAcyMailingPages() | |
| 74 | + { | |
| 75 | + // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Make sure we're on an AcyMailing page. | |
| 76 | + $page = isset($_REQUEST['page']) ? sanitize_text_field(wp_unslash($_REQUEST['page'])) : ''; | |
| 77 | + if (empty($page) || strpos($page, 'acymailing_') === false) { | |
| 78 | + return; | |
| 79 | + } | |
| 80 | + | |
| 81 | + wp_dequeue_script('responsive-lightbox-admin-select2'); | |
| 82 | + wp_dequeue_style('responsive-lightbox-admin-select2'); | |
| 83 | + | |
| 84 | + // Remove theme loading select2 from Supreme module pro for divi | |
| 85 | + wp_dequeue_script('dsm-select-two'); | |
| 86 | + wp_dequeue_style('dsm-select-two'); | |
| 87 | + } | |
| 88 | + | |
| 89 | + public function waitHeaders() | |
| 90 | + { | |
| 91 | + ob_start(); | |
| 92 | + } | |
| 93 | + | |
| 94 | + public function disableJsBreakingPages() | |
| 95 | + { | |
| 96 | + // Show normal emojis on AcyMailing pages | |
| 97 | + remove_action('admin_print_scripts', 'print_emoji_detection_script'); | |
| 98 | + | |
| 99 | + // Slideshow ck breaks the editor | |
| 100 | + remove_action('wp_enqueue_media', '\\Slideshowck\\Tinymce\\register_scripts_styles'); | |
| 101 | + | |
| 102 | + // Skaut Google Drive gallery breaks the editor | |
| 103 | + remove_action('wp_enqueue_media', '\\Sgdg\\Admin\\TinyMCE\\register_scripts_styles'); | |
| 104 | + | |
| 105 | + // Remove theme loading select2 which breaks select2 in vueJS | |
| 106 | + wp_dequeue_script('select2.js'); | |
| 107 | + | |
| 108 | + // The "Checkout Field Manager for WooCommerce" plugin breaks the js on every pages | |
| 109 | + wp_dequeue_script('checkout_fields_js'); | |
| 110 | + | |
| 111 | + // Fixed editor incompatibility | |
| 112 | + wp_dequeue_script('wp-optimize-minify-admin-purge'); | |
| 113 | + | |
| 114 | + // Remove Happy Elementor Addons select2 which breaks our select2 | |
| 115 | + wp_dequeue_script('happy-elementor-addons-select2'); | |
| 116 | + wp_dequeue_script('select2'); | |
| 117 | + } | |
| 118 | + | |
| 119 | + public function removeCssBreakingPages() | |
| 120 | + { | |
| 121 | + wp_dequeue_style('saswp-main-css'); | |
| 122 | + wp_dequeue_style('WP REST API Controller'); | |
| 123 | + wp_dequeue_style('wpml-select-2'); | |
| 124 | + wp_dequeue_style('swcfpc_admin_css'); | |
| 125 | + | |
| 126 | + // Remove Happy Elementor Addons select2 which breaks our select2 | |
| 127 | + wp_dequeue_style('happy-elementor-addons-select2'); | |
| 128 | + wp_dequeue_style('select2'); | |
| 129 | + | |
| 130 | + // Messes with the tooltips, the schedule date in emails for example | |
| 131 | + wp_dequeue_style('qlwapp-admin-menu'); | |
| 132 | + } | |
| 133 | + | |
| 134 | + public function frontRouter() | |
| 135 | + { | |
| 136 | + $page = acym_getVar('string', 'page'); | |
| 137 | + if (empty($page) || $page !== ACYM_COMPONENT.'_front') return; | |
| 138 | + | |
| 139 | + $this->router(true); | |
| 140 | + } | |
| 141 | + | |
| 142 | + public function router(bool $front = false): void | |
| 143 | + { | |
| 144 | + acym_displayFreeTrialMessage(); | |
| 145 | + | |
| 146 | + | |
| 147 | + if (!$front) { | |
| 148 | + auth_redirect(); | |
| 149 | + | |
| 150 | + // The router only enforces auth_redirect() and the 'dashboard' controller is exempt from call()'s ACL, so mirror Menu.php's back-office access gate (wp_access groups) to keep arbitrary logged-in users out. | |
| 151 | + if (!acym_hasBackofficeAccess()) { | |
| 152 | + wp_die(esc_html(acym_translation('ACYM_ACCESS_DENIED')), '', ['response' => 403]); | |
| 153 | + } | |
| 154 | + } | |
| 155 | + | |
| 156 | + if (file_exists(ACYM_FOLDER.'update.php')) { | |
| 157 | + $acyActivation = new Activation(); | |
| 158 | + if (is_multisite()) { | |
| 159 | + $currentBlog = get_current_blog_id(); | |
| 160 | + $sites = get_sites(); | |
| 161 | + | |
| 162 | + foreach ($sites as $site) { | |
| 163 | + if (is_object($site)) { | |
| 164 | + $site = get_object_vars($site); | |
| 165 | + } | |
| 166 | + switch_to_blog($site['blog_id']); | |
| 167 | + acym_config(true); | |
| 168 | + $acyActivation->updateAcym(); | |
| 169 | + } | |
| 170 | + | |
| 171 | + switch_to_blog($currentBlog); | |
| 172 | + } else { | |
| 173 | + $acyActivation->updateAcym(); | |
| 174 | + } | |
| 175 | + | |
| 176 | + acym_deleteFile(ACYM_FOLDER.'update.php'); | |
| 177 | + } | |
| 178 | + | |
| 179 | + $config = acym_config(true); | |
| 180 | + | |
| 181 | + // Get controller. If not found, take it from the page | |
| 182 | + $ctrl = acym_getVar('cmd', 'ctrl', ''); | |
| 183 | + $task = acym_getVar('cmd', 'task', ''); | |
| 184 | + | |
| 185 | + if (!$front && acym_isAdmin() && file_exists(ACYM_NEW_FEATURES_SPLASHSCREEN_JSON) && acym_isWritable(ACYM_NEW_FEATURES_SPLASHSCREEN_JSON)) { | |
| 186 | + $ctrl = 'dashboard'; | |
| 187 | + $task = 'features'; | |
| 188 | + acym_setVar('ctrl', $ctrl); | |
| 189 | + acym_setVar('task', $task); | |
| 190 | + } | |
| 191 | + | |
| 192 | + $needToMigrate = $config->get('migration') == 0 && acym_existsAcyMailing59() && acym_getVar('string', 'task') !== 'migrationDone'; | |
| 193 | + $forceDashboard = !$front && ($needToMigrate || $config->get('walk_through', 0) == 1) && !(defined('DOING_AJAX') && DOING_AJAX) && 'dynamics' !== $ctrl; | |
| 194 | + if ($forceDashboard) { | |
| 195 | + $ctrl = 'dashboard'; | |
| 196 | + acym_setVar('ctrl', $ctrl); | |
| 197 | + } | |
| 198 | + | |
| 199 | + if (empty($ctrl)) { | |
| 200 | + $ctrl = str_replace(ACYM_COMPONENT.'_', '', acym_getVar('cmd', 'page', '')); | |
| 201 | + | |
| 202 | + if (empty($ctrl)) { | |
| 203 | + echo esc_html(acym_translation('ACYM_PAGE_NOT_FOUND')); | |
| 204 | + | |
| 205 | + // For Google search console in the frontend to prevent from doing 404 errors | |
| 206 | + $noCache = acym_getVar('int', 'nocache', 0); | |
| 207 | + if (!empty($noCache)) { | |
| 208 | + acym_redirect(acym_rootURI()); | |
| 209 | + } | |
| 210 | + | |
| 211 | + return; | |
| 212 | + } | |
| 213 | + | |
| 214 | + acym_setVar('ctrl', $ctrl); | |
| 215 | + } | |
| 216 | + | |
| 217 | + $this->deactivateHookAdminFooter(); | |
| 218 | + $subNamespace = $front ? 'Front' : ''; | |
| 219 | + $controllerNamespace = 'AcyMailing\\'.$subNamespace.'Controllers\\'.ucfirst($ctrl).'Controller'; | |
| 220 | + | |
| 221 | + if (!class_exists($controllerNamespace)) { | |
| 222 | + echo esc_html(acym_translation('ACYM_PAGE_NOT_FOUND').': '.$ctrl); | |
| 223 | + | |
| 224 | + return; | |
| 225 | + } | |
| 226 | + | |
| 227 | + if (!$front && acym_isAdmin() && $task != 'edit' && !(defined('DOING_AJAX') && DOING_AJAX)) { | |
| 228 | + $pluginClass = new PluginClass(); | |
| 229 | + $installedPlugins = $pluginClass->getAll('title'); | |
| 230 | + foreach (ACYM_AVAILABLE_PLUGINS as $onePlugin) { | |
| 231 | + if (empty($installedPlugins[$onePlugin['name']])) continue; | |
| 232 | + if ($installedPlugins[$onePlugin['name']]->type !== 'ADDON') continue; | |
| 233 | + | |
| 234 | + acym_enqueueMessage( | |
| 235 | + acym_translationSprintf( | |
| 236 | + 'ACYM_NEW_PLUGIN_FORMAT', | |
| 237 | + $onePlugin['name'], | |
| 238 | + '<a target="_blank" style="color: #00a5ff;" href="'.$onePlugin['downloadlink'].'">'.acym_translation('ACYM_CLICK_HERE').'</a>' | |
| 239 | + ), | |
| 240 | + 'error' | |
| 241 | + ); | |
| 242 | + } | |
| 243 | + } | |
| 244 | + | |
| 245 | + $controller = new $controllerNamespace(); | |
| 246 | + if (empty($task)) { | |
| 247 | + $task = acym_getVar('cmd', 'defaulttask', $controller->defaulttask); | |
| 248 | + } | |
| 249 | + | |
| 250 | + if ($forceDashboard && !method_exists($controller, $task)) { | |
| 251 | + $task = 'listing'; | |
| 252 | + acym_setVar('task', $task); | |
| 253 | + } | |
| 254 | + | |
| 255 | + $controller->call($task, $front); | |
| 256 | + } | |
| 257 | + | |
| 258 | + public function autologin() | |
| 259 | + { | |
| 260 | + $subId = acym_getVar('int', 'autoSubId'); | |
| 261 | + $subToken = acym_getVar('string', 'subKey'); | |
| 262 | + | |
| 263 | + if (empty($subId) || empty($subToken)) { | |
| 264 | + return; | |
| 265 | + } | |
| 266 | + | |
| 267 | + $currentUrl = acym_currentURL(); | |
| 268 | + $cleanedUrl = acym_cleanUrl($currentUrl, ['autoSubId', 'subKey']); | |
| 269 | + | |
| 270 | + $config = acym_config(); | |
| 271 | + if ($config->get('autologin_urls', 0) == 0) { | |
| 272 | + acym_redirect($cleanedUrl); | |
| 273 | + } | |
| 274 | + | |
| 275 | + $subscriber = acym_loadObject('SELECT `cms_id`, `key` FROM #__acym_user WHERE `id` = '.intval($subId)); | |
| 276 | + if (empty($subscriber->cms_id) || $subscriber->cms_id === acym_currentUserId()) { | |
| 277 | + acym_redirect($cleanedUrl); | |
| 278 | + } | |
| 279 | + | |
| 280 | + if (!acym_verifyAutologinToken($subId, $subToken, $subscriber->key)) { | |
| 281 | + acym_redirect($cleanedUrl); | |
| 282 | + } | |
| 283 | + | |
| 284 | + $user = get_user_by('id', $subscriber->cms_id); | |
| 285 | + | |
| 286 | + if (is_wp_error($user) || !$user) { | |
| 287 | + acym_redirect($cleanedUrl); | |
| 288 | + } | |
| 289 | + | |
| 290 | + // Never allow autologin for administrator accounts | |
| 291 | + if ($user->has_cap('manage_options')) { | |
| 292 | + acym_redirect($cleanedUrl); | |
| 293 | + } | |
| 294 | + | |
| 295 | + wp_clear_auth_cookie(); | |
| 296 | + wp_set_current_user($user->ID); | |
| 297 | + wp_set_auth_cookie($user->ID); | |
| 298 | + | |
| 299 | + acym_redirect($cleanedUrl); | |
| 300 | + } | |
| 301 | + | |
| 302 | + private function deactivateHookAdminFooter() | |
| 303 | + { | |
| 304 | + //Remove hook function which break AcyMailing pages | |
| 305 | + remove_action('admin_footer', 'Freemius::_enrich_ajax_url'); | |
| 306 | + remove_action('admin_footer', 'Freemius::_open_support_forum_in_new_page'); | |
| 307 | + } | |
| 308 | +} | |