| 1 |
<?php |
| 2 |
|
| 3 |
namespace MailPoet\Config; |
| 4 |
|
| 5 |
if (!defined('ABSPATH')) exit; |
| 6 |
|
| 7 |
|
| 8 |
use MailPoet\AdminPages\Pages\ExperimentalFeatures; |
| 9 |
use MailPoet\AdminPages\Pages\FormEditor; |
| 10 |
use MailPoet\AdminPages\Pages\Forms; |
| 11 |
use MailPoet\AdminPages\Pages\Help; |
| 12 |
use MailPoet\AdminPages\Pages\Logs; |
| 13 |
use MailPoet\AdminPages\Pages\MP2Migration; |
| 14 |
use MailPoet\AdminPages\Pages\NewsletterEditor; |
| 15 |
use MailPoet\AdminPages\Pages\Newsletters; |
| 16 |
use MailPoet\AdminPages\Pages\Premium; |
| 17 |
use MailPoet\AdminPages\Pages\Segments; |
| 18 |
use MailPoet\AdminPages\Pages\Settings; |
| 19 |
use MailPoet\AdminPages\Pages\Subscribers; |
| 20 |
use MailPoet\AdminPages\Pages\SubscribersExport; |
| 21 |
use MailPoet\AdminPages\Pages\SubscribersImport; |
| 22 |
use MailPoet\AdminPages\Pages\WelcomeWizard; |
| 23 |
use MailPoet\AdminPages\Pages\WooCommerceSetup; |
| 24 |
use MailPoet\DI\ContainerWrapper; |
| 25 |
use MailPoet\Util\License\License; |
| 26 |
use MailPoet\WP\Functions as WPFunctions; |
| 27 |
|
| 28 |
class Menu { |
| 29 |
const MAIN_PAGE_SLUG = 'mailpoet-newsletters'; |
| 30 |
|
| 31 |
public $mpApiKeyValid; |
| 32 |
public $premiumKeyValid; |
| 33 |
|
| 34 |
/** @var AccessControl */ |
| 35 |
private $accessControl; |
| 36 |
|
| 37 |
/** @var WPFunctions */ |
| 38 |
private $wp; |
| 39 |
|
| 40 |
/** @var ServicesChecker */ |
| 41 |
private $servicesChecker; |
| 42 |
|
| 43 |
/** @var ContainerWrapper */ |
| 44 |
private $container; |
| 45 |
|
| 46 |
/** @var Router */ |
| 47 |
private $router; |
| 48 |
|
| 49 |
public function __construct( |
| 50 |
AccessControl $accessControl, |
| 51 |
WPFunctions $wp, |
| 52 |
ServicesChecker $servicesChecker, |
| 53 |
ContainerWrapper $container, |
| 54 |
Router $router |
| 55 |
) { |
| 56 |
$this->accessControl = $accessControl; |
| 57 |
$this->wp = $wp; |
| 58 |
$this->servicesChecker = $servicesChecker; |
| 59 |
$this->container = $container; |
| 60 |
$this->router = $router; |
| 61 |
} |
| 62 |
|
| 63 |
public function init() { |
| 64 |
$this->checkPremiumKey(); |
| 65 |
|
| 66 |
$this->wp->addAction( |
| 67 |
'admin_menu', |
| 68 |
[ |
| 69 |
$this, |
| 70 |
'setup', |
| 71 |
] |
| 72 |
); |
| 73 |
} |
| 74 |
|
| 75 |
public function setup() { |
| 76 |
if (!$this->accessControl->validatePermission(AccessControl::PERMISSION_ACCESS_PLUGIN_ADMIN)) return; |
| 77 |
|
| 78 |
$this->router->checkRedirects(); |
| 79 |
|
| 80 |
if (self::isOnMailPoetAdminPage()) { |
| 81 |
$this->wp->doAction('mailpoet_conflict_resolver_styles'); |
| 82 |
$this->wp->doAction('mailpoet_conflict_resolver_scripts'); |
| 83 |
|
| 84 |
if ($_REQUEST['page'] === 'mailpoet-newsletter-editor') { |
| 85 |
// Disable WP emojis to not interfere with the newsletter editor emoji handling |
| 86 |
$this->disableWPEmojis(); |
| 87 |
$this->wp->addAction('admin_head', function() { |
| 88 |
$fonts = 'Arvo:400,400i,700,700i' |
| 89 |
. '|Lato:400,400i,700,700i' |
| 90 |
. '|Lora:400,400i,700,700i' |
| 91 |
. '|Merriweather:400,400i,700,700i' |
| 92 |
. '|Merriweather+Sans:400,400i,700,700i' |
| 93 |
. '|Noticia+Text:400,400i,700,700i' |
| 94 |
. '|Open+Sans:400,400i,700,700i' |
| 95 |
. '|Playfair+Display:400,400i,700,700i' |
| 96 |
. '|Roboto:400,400i,700,700i' |
| 97 |
. '|Source+Sans+Pro:400,400i,700,700i' |
| 98 |
. '|Oswald:400,400i,700,700i' |
| 99 |
. '|Raleway:400,400i,700,700i' |
| 100 |
. '|Permanent+Marker:400,400i,700,700i' |
| 101 |
. '|Pacifico:400,400i,700,700i'; |
| 102 |
echo '<!--[if !mso]><link href="https://fonts.googleapis.com/css?family=' . $fonts . '" rel="stylesheet"><![endif]-->'; |
| 103 |
}); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
// Main page |
| 108 |
$this->wp->addMenuPage( |
| 109 |
'MailPoet', |
| 110 |
'MailPoet', |
| 111 |
AccessControl::PERMISSION_ACCESS_PLUGIN_ADMIN, |
| 112 |
self::MAIN_PAGE_SLUG, |
| 113 |
null, |
| 114 |
'none', |
| 115 |
30 |
| 116 |
); |
| 117 |
|
| 118 |
// Emails page |
| 119 |
$newslettersPage = $this->wp->addSubmenuPage( |
| 120 |
self::MAIN_PAGE_SLUG, |
| 121 |
$this->setPageTitle(__('Emails', 'mailpoet')), |
| 122 |
$this->wp->__('Emails', 'mailpoet'), |
| 123 |
AccessControl::PERMISSION_MANAGE_EMAILS, |
| 124 |
self::MAIN_PAGE_SLUG, |
| 125 |
[ |
| 126 |
$this, |
| 127 |
'newsletters', |
| 128 |
] |
| 129 |
); |
| 130 |
|
| 131 |
// add limit per page to screen options |
| 132 |
$this->wp->addAction('load-' . $newslettersPage, function() { |
| 133 |
$this->wp->addScreenOption('per_page', [ |
| 134 |
'label' => $this->wp->_x( |
| 135 |
'Number of newsletters per page', |
| 136 |
'newsletters per page (screen options)', |
| 137 |
'mailpoet' |
| 138 |
), |
| 139 |
'option' => 'mailpoet_newsletters_per_page', |
| 140 |
]); |
| 141 |
}); |
| 142 |
|
| 143 |
// newsletter editor |
| 144 |
$this->wp->addSubmenuPage( |
| 145 |
true, |
| 146 |
$this->setPageTitle(__('Newsletter', 'mailpoet')), |
| 147 |
$this->wp->__('Newsletter Editor', 'mailpoet'), |
| 148 |
AccessControl::PERMISSION_MANAGE_EMAILS, |
| 149 |
'mailpoet-newsletter-editor', |
| 150 |
[ |
| 151 |
$this, |
| 152 |
'newletterEditor', |
| 153 |
] |
| 154 |
); |
| 155 |
|
| 156 |
// Forms page |
| 157 |
$formsPage = $this->wp->addSubmenuPage( |
| 158 |
self::MAIN_PAGE_SLUG, |
| 159 |
$this->setPageTitle(__('Forms', 'mailpoet')), |
| 160 |
$this->wp->__('Forms', 'mailpoet'), |
| 161 |
AccessControl::PERMISSION_MANAGE_FORMS, |
| 162 |
'mailpoet-forms', |
| 163 |
[ |
| 164 |
$this, |
| 165 |
'forms', |
| 166 |
] |
| 167 |
); |
| 168 |
|
| 169 |
// add limit per page to screen options |
| 170 |
$this->wp->addAction('load-' . $formsPage, function() { |
| 171 |
$this->wp->addScreenOption('per_page', [ |
| 172 |
'label' => $this->wp->_x( |
| 173 |
'Number of forms per page', |
| 174 |
'forms per page (screen options)', |
| 175 |
'mailpoet' |
| 176 |
), |
| 177 |
'option' => 'mailpoet_forms_per_page', |
| 178 |
]); |
| 179 |
}); |
| 180 |
|
| 181 |
// form editor |
| 182 |
$formEditorPage = $this->wp->addSubmenuPage( |
| 183 |
true, |
| 184 |
$this->setPageTitle(__('Form Editor', 'mailpoet')), |
| 185 |
$this->wp->__('Form Editor', 'mailpoet'), |
| 186 |
AccessControl::PERMISSION_MANAGE_FORMS, |
| 187 |
'mailpoet-form-editor', |
| 188 |
[ |
| 189 |
$this, |
| 190 |
'formEditor', |
| 191 |
] |
| 192 |
); |
| 193 |
|
| 194 |
// add body class for form editor page |
| 195 |
$this->wp->addAction('load-' . $formEditorPage, function() { |
| 196 |
$this->wp->addAction('admin_body_class', function ($classes) { |
| 197 |
return ltrim($classes . ' block-editor-page'); |
| 198 |
}); |
| 199 |
}); |
| 200 |
|
| 201 |
// form editor templates |
| 202 |
$formTemplateSelectionEditorPage = $this->wp->addSubmenuPage( |
| 203 |
true, |
| 204 |
$this->setPageTitle(__('Select Form Template', 'mailpoet')), |
| 205 |
$this->wp->__('Select Form Template', 'mailpoet'), |
| 206 |
AccessControl::PERMISSION_MANAGE_FORMS, |
| 207 |
'mailpoet-form-editor-template-selection', |
| 208 |
[ |
| 209 |
$this, |
| 210 |
'formEditorTemplateSelection', |
| 211 |
] |
| 212 |
); |
| 213 |
|
| 214 |
// add body class for form editor page |
| 215 |
$this->wp->addAction('load-' . $formTemplateSelectionEditorPage, function() { |
| 216 |
$this->wp->addAction('admin_body_class', function ($classes) { |
| 217 |
return ltrim($classes . ' block-editor-page'); |
| 218 |
}); |
| 219 |
}); |
| 220 |
|
| 221 |
|
| 222 |
// Subscribers page |
| 223 |
$subscribersPage = $this->wp->addSubmenuPage( |
| 224 |
self::MAIN_PAGE_SLUG, |
| 225 |
$this->setPageTitle(__('Subscribers', 'mailpoet')), |
| 226 |
$this->wp->__('Subscribers', 'mailpoet'), |
| 227 |
AccessControl::PERMISSION_MANAGE_SUBSCRIBERS, |
| 228 |
'mailpoet-subscribers', |
| 229 |
[ |
| 230 |
$this, |
| 231 |
'subscribers', |
| 232 |
] |
| 233 |
); |
| 234 |
|
| 235 |
// add limit per page to screen options |
| 236 |
$this->wp->addAction('load-' . $subscribersPage, function() { |
| 237 |
$this->wp->addScreenOption('per_page', [ |
| 238 |
'label' => $this->wp->_x( |
| 239 |
'Number of subscribers per page', |
| 240 |
'subscribers per page (screen options)', |
| 241 |
'mailpoet' |
| 242 |
), |
| 243 |
'option' => 'mailpoet_subscribers_per_page', |
| 244 |
]); |
| 245 |
}); |
| 246 |
|
| 247 |
// import |
| 248 |
$this->wp->addSubmenuPage( |
| 249 |
'admin.php?page=mailpoet-subscribers', |
| 250 |
$this->setPageTitle(__('Import', 'mailpoet')), |
| 251 |
$this->wp->__('Import', 'mailpoet'), |
| 252 |
AccessControl::PERMISSION_MANAGE_SUBSCRIBERS, |
| 253 |
'mailpoet-import', |
| 254 |
[ |
| 255 |
$this, |
| 256 |
'import', |
| 257 |
] |
| 258 |
); |
| 259 |
|
| 260 |
// export |
| 261 |
$this->wp->addSubmenuPage( |
| 262 |
true, |
| 263 |
$this->setPageTitle(__('Export', 'mailpoet')), |
| 264 |
$this->wp->__('Export', 'mailpoet'), |
| 265 |
AccessControl::PERMISSION_MANAGE_SUBSCRIBERS, |
| 266 |
'mailpoet-export', |
| 267 |
[ |
| 268 |
$this, |
| 269 |
'export', |
| 270 |
] |
| 271 |
); |
| 272 |
|
| 273 |
// Segments page |
| 274 |
$segmentsPage = $this->wp->addSubmenuPage( |
| 275 |
self::MAIN_PAGE_SLUG, |
| 276 |
$this->setPageTitle(__('Lists', 'mailpoet')), |
| 277 |
$this->wp->__('Lists', 'mailpoet'), |
| 278 |
AccessControl::PERMISSION_MANAGE_SEGMENTS, |
| 279 |
'mailpoet-segments', |
| 280 |
[ |
| 281 |
$this, |
| 282 |
'segments', |
| 283 |
] |
| 284 |
); |
| 285 |
|
| 286 |
// add limit per page to screen options |
| 287 |
$this->wp->addAction('load-' . $segmentsPage, function() { |
| 288 |
$this->wp->addScreenOption('per_page', [ |
| 289 |
'label' => $this->wp->_x( |
| 290 |
'Number of segments per page', |
| 291 |
'segments per page (screen options)', |
| 292 |
'mailpoet' |
| 293 |
), |
| 294 |
'option' => 'mailpoet_segments_per_page', |
| 295 |
]); |
| 296 |
}); |
| 297 |
|
| 298 |
// Settings page |
| 299 |
$this->wp->addSubmenuPage( |
| 300 |
self::MAIN_PAGE_SLUG, |
| 301 |
$this->setPageTitle(__('Settings', 'mailpoet')), |
| 302 |
$this->wp->__('Settings', 'mailpoet'), |
| 303 |
AccessControl::PERMISSION_MANAGE_SETTINGS, |
| 304 |
'mailpoet-settings', |
| 305 |
[ |
| 306 |
$this, |
| 307 |
'settings', |
| 308 |
] |
| 309 |
); |
| 310 |
|
| 311 |
// Help page |
| 312 |
$this->wp->addSubmenuPage( |
| 313 |
self::MAIN_PAGE_SLUG, |
| 314 |
$this->setPageTitle(__('Help', 'mailpoet')), |
| 315 |
$this->wp->__('Help', 'mailpoet'), |
| 316 |
AccessControl::PERMISSION_ACCESS_PLUGIN_ADMIN, |
| 317 |
'mailpoet-help', |
| 318 |
[ |
| 319 |
$this, |
| 320 |
'help', |
| 321 |
] |
| 322 |
); |
| 323 |
|
| 324 |
// Premium page |
| 325 |
// Only show this page in menu if the Premium plugin is not activated |
| 326 |
$this->wp->addSubmenuPage( |
| 327 |
License::getLicense() ? true : self::MAIN_PAGE_SLUG, |
| 328 |
$this->setPageTitle(__('Premium', 'mailpoet')), |
| 329 |
$this->wp->__('Premium', 'mailpoet'), |
| 330 |
AccessControl::PERMISSION_ACCESS_PLUGIN_ADMIN, |
| 331 |
'mailpoet-premium', |
| 332 |
[ |
| 333 |
$this, |
| 334 |
'premium', |
| 335 |
] |
| 336 |
); |
| 337 |
|
| 338 |
// Welcome wizard page |
| 339 |
$this->wp->addSubmenuPage( |
| 340 |
true, |
| 341 |
$this->setPageTitle(__('Welcome Wizard', 'mailpoet')), |
| 342 |
$this->wp->__('Welcome Wizard', 'mailpoet'), |
| 343 |
AccessControl::PERMISSION_ACCESS_PLUGIN_ADMIN, |
| 344 |
'mailpoet-welcome-wizard', |
| 345 |
[ |
| 346 |
$this, |
| 347 |
'welcomeWizard', |
| 348 |
] |
| 349 |
); |
| 350 |
|
| 351 |
// WooCommerce Setup |
| 352 |
$this->wp->addSubmenuPage( |
| 353 |
true, |
| 354 |
$this->setPageTitle($this->wp->__('WooCommerce Setup', 'mailpoet')), |
| 355 |
$this->wp->__('WooCommerce Setup', 'mailpoet'), |
| 356 |
AccessControl::PERMISSION_ACCESS_PLUGIN_ADMIN, |
| 357 |
'mailpoet-woocommerce-setup', |
| 358 |
[ |
| 359 |
$this, |
| 360 |
'wooCommerceSetup', |
| 361 |
] |
| 362 |
); |
| 363 |
|
| 364 |
// Migration page |
| 365 |
$this->wp->addSubmenuPage( |
| 366 |
true, |
| 367 |
$this->setPageTitle(__('Migration', 'mailpoet')), |
| 368 |
'', |
| 369 |
AccessControl::PERMISSION_ACCESS_PLUGIN_ADMIN, |
| 370 |
'mailpoet-migration', |
| 371 |
[ |
| 372 |
$this, |
| 373 |
'migration', |
| 374 |
] |
| 375 |
); |
| 376 |
|
| 377 |
// Experimental page |
| 378 |
$this->wp->addSubmenuPage( |
| 379 |
true, |
| 380 |
$this->setPageTitle('Experimental Features'), |
| 381 |
'', |
| 382 |
AccessControl::PERMISSION_MANAGE_FEATURES, |
| 383 |
'mailpoet-experimental', |
| 384 |
[$this, 'experimentalFeatures'] |
| 385 |
); |
| 386 |
|
| 387 |
// display loggs page |
| 388 |
$this->wp->addSubmenuPage( |
| 389 |
true, |
| 390 |
$this->setPageTitle('Logs'), |
| 391 |
'', |
| 392 |
AccessControl::PERMISSION_ACCESS_PLUGIN_ADMIN, |
| 393 |
'mailpoet-logs', |
| 394 |
[$this, 'logs'] |
| 395 |
); |
| 396 |
} |
| 397 |
|
| 398 |
public function disableWPEmojis() { |
| 399 |
$this->wp->removeAction('admin_print_scripts', 'print_emoji_detection_script'); |
| 400 |
$this->wp->removeAction('admin_print_styles', 'print_emoji_styles'); |
| 401 |
} |
| 402 |
|
| 403 |
public function migration() { |
| 404 |
$this->container->get(MP2Migration::class)->render(); |
| 405 |
} |
| 406 |
|
| 407 |
public function welcomeWizard() { |
| 408 |
$this->container->get(WelcomeWizard::class)->render(); |
| 409 |
} |
| 410 |
|
| 411 |
public function wooCommerceSetup() { |
| 412 |
$this->container->get(WooCommerceSetup::class)->render(); |
| 413 |
} |
| 414 |
|
| 415 |
public function premium() { |
| 416 |
$this->container->get(Premium::class)->render(); |
| 417 |
} |
| 418 |
|
| 419 |
public function settings() { |
| 420 |
$this->container->get(Settings::class)->render(); |
| 421 |
} |
| 422 |
|
| 423 |
public function help() { |
| 424 |
$this->container->get(Help::class)->render(); |
| 425 |
} |
| 426 |
|
| 427 |
public function experimentalFeatures() { |
| 428 |
$this->container->get(ExperimentalFeatures::class)->render(); |
| 429 |
} |
| 430 |
|
| 431 |
public function logs() { |
| 432 |
$this->container->get(Logs::class)->render(); |
| 433 |
} |
| 434 |
|
| 435 |
public function subscribers() { |
| 436 |
$this->container->get(Subscribers::class)->render(); |
| 437 |
} |
| 438 |
|
| 439 |
public function segments() { |
| 440 |
$this->container->get(Segments::class)->render(); |
| 441 |
} |
| 442 |
|
| 443 |
public function forms() { |
| 444 |
$this->container->get(Forms::class)->render(); |
| 445 |
} |
| 446 |
|
| 447 |
public function newsletters() { |
| 448 |
$this->container->get(Newsletters::class)->render(); |
| 449 |
} |
| 450 |
|
| 451 |
public function newletterEditor() { |
| 452 |
$this->container->get(NewsletterEditor::class)->render(); |
| 453 |
} |
| 454 |
|
| 455 |
public function import() { |
| 456 |
$this->container->get(SubscribersImport::class)->render(); |
| 457 |
} |
| 458 |
|
| 459 |
public function export() { |
| 460 |
$this->container->get(SubscribersExport::class)->render(); |
| 461 |
} |
| 462 |
|
| 463 |
public function formEditor() { |
| 464 |
$this->container->get(FormEditor::class)->render(); |
| 465 |
} |
| 466 |
|
| 467 |
public function formEditorTemplateSelection() { |
| 468 |
$this->container->get(FormEditor::class)->renderTemplateSelection(); |
| 469 |
} |
| 470 |
|
| 471 |
public function setPageTitle($title) { |
| 472 |
return sprintf( |
| 473 |
'%s - %s', |
| 474 |
$this->wp->__('MailPoet', 'mailpoet'), |
| 475 |
$title |
| 476 |
); |
| 477 |
} |
| 478 |
|
| 479 |
public static function isOnMailPoetAdminPage(array $exclude = null, $screenId = null) { |
| 480 |
if (is_null($screenId)) { |
| 481 |
if (empty($_REQUEST['page'])) { |
| 482 |
return false; |
| 483 |
} |
| 484 |
$screenId = $_REQUEST['page']; |
| 485 |
} |
| 486 |
if (!empty($exclude)) { |
| 487 |
foreach ($exclude as $slug) { |
| 488 |
if (stripos($screenId, $slug) !== false) { |
| 489 |
return false; |
| 490 |
} |
| 491 |
} |
| 492 |
} |
| 493 |
return (stripos($screenId, 'mailpoet-') !== false); |
| 494 |
} |
| 495 |
|
| 496 |
/** |
| 497 |
* This error page is used when the initialization is failed |
| 498 |
* to display admin notices only |
| 499 |
*/ |
| 500 |
public static function addErrorPage(AccessControl $accessControl) { |
| 501 |
if (!self::isOnMailPoetAdminPage()) { |
| 502 |
return false; |
| 503 |
} |
| 504 |
// Check if page already exists |
| 505 |
if (get_plugin_page_hook($_REQUEST['page'], '') |
| 506 |
|| WPFunctions::get()->getPluginPageHook($_REQUEST['page'], self::MAIN_PAGE_SLUG) |
| 507 |
) { |
| 508 |
return false; |
| 509 |
} |
| 510 |
WPFunctions::get()->addSubmenuPage( |
| 511 |
true, |
| 512 |
'MailPoet', |
| 513 |
'MailPoet', |
| 514 |
AccessControl::PERMISSION_ACCESS_PLUGIN_ADMIN, |
| 515 |
$_REQUEST['page'], |
| 516 |
[ |
| 517 |
__CLASS__, |
| 518 |
'errorPageCallback', |
| 519 |
] |
| 520 |
); |
| 521 |
} |
| 522 |
|
| 523 |
public static function errorPageCallback() { |
| 524 |
// Used for displaying admin notices only |
| 525 |
} |
| 526 |
|
| 527 |
public function checkPremiumKey(ServicesChecker $checker = null) { |
| 528 |
$showNotices = isset($_SERVER['SCRIPT_NAME']) |
| 529 |
&& stripos($_SERVER['SCRIPT_NAME'], 'plugins.php') !== false; |
| 530 |
$checker = $checker ?: $this->servicesChecker; |
| 531 |
$this->premiumKeyValid = $checker->isPremiumKeyValid($showNotices); |
| 532 |
} |
| 533 |
} |
| 534 |
|