| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534 |
- <?php
- namespace MailPoet\Config;
- if (!defined('ABSPATH')) exit;
- use MailPoet\AdminPages\Pages\ExperimentalFeatures;
- use MailPoet\AdminPages\Pages\FormEditor;
- use MailPoet\AdminPages\Pages\Forms;
- use MailPoet\AdminPages\Pages\Help;
- use MailPoet\AdminPages\Pages\Logs;
- use MailPoet\AdminPages\Pages\MP2Migration;
- use MailPoet\AdminPages\Pages\NewsletterEditor;
- use MailPoet\AdminPages\Pages\Newsletters;
- use MailPoet\AdminPages\Pages\Premium;
- use MailPoet\AdminPages\Pages\Segments;
- use MailPoet\AdminPages\Pages\Settings;
- use MailPoet\AdminPages\Pages\Subscribers;
- use MailPoet\AdminPages\Pages\SubscribersExport;
- use MailPoet\AdminPages\Pages\SubscribersImport;
- use MailPoet\AdminPages\Pages\WelcomeWizard;
- use MailPoet\AdminPages\Pages\WooCommerceSetup;
- use MailPoet\DI\ContainerWrapper;
- use MailPoet\Util\License\License;
- use MailPoet\WP\Functions as WPFunctions;
- class Menu {
- const MAIN_PAGE_SLUG = 'mailpoet-newsletters';
- public $mpApiKeyValid;
- public $premiumKeyValid;
- /** @var AccessControl */
- private $accessControl;
- /** @var WPFunctions */
- private $wp;
- /** @var ServicesChecker */
- private $servicesChecker;
- /** @var ContainerWrapper */
- private $container;
- /** @var Router */
- private $router;
- public function __construct(
- AccessControl $accessControl,
- WPFunctions $wp,
- ServicesChecker $servicesChecker,
- ContainerWrapper $container,
- Router $router
- ) {
- $this->accessControl = $accessControl;
- $this->wp = $wp;
- $this->servicesChecker = $servicesChecker;
- $this->container = $container;
- $this->router = $router;
- }
- public function init() {
- $this->checkPremiumKey();
- $this->wp->addAction(
- 'admin_menu',
- [
- $this,
- 'setup',
- ]
- );
- }
- public function setup() {
- if (!$this->accessControl->validatePermission(AccessControl::PERMISSION_ACCESS_PLUGIN_ADMIN)) return;
- $this->router->checkRedirects();
- if (self::isOnMailPoetAdminPage()) {
- $this->wp->doAction('mailpoet_conflict_resolver_styles');
- $this->wp->doAction('mailpoet_conflict_resolver_scripts');
- if ($_REQUEST['page'] === 'mailpoet-newsletter-editor') {
- // Disable WP emojis to not interfere with the newsletter editor emoji handling
- $this->disableWPEmojis();
- $this->wp->addAction('admin_head', function() {
- $fonts = 'Arvo:400,400i,700,700i'
- . '|Lato:400,400i,700,700i'
- . '|Lora:400,400i,700,700i'
- . '|Merriweather:400,400i,700,700i'
- . '|Merriweather+Sans:400,400i,700,700i'
- . '|Noticia+Text:400,400i,700,700i'
- . '|Open+Sans:400,400i,700,700i'
- . '|Playfair+Display:400,400i,700,700i'
- . '|Roboto:400,400i,700,700i'
- . '|Source+Sans+Pro:400,400i,700,700i'
- . '|Oswald:400,400i,700,700i'
- . '|Raleway:400,400i,700,700i'
- . '|Permanent+Marker:400,400i,700,700i'
- . '|Pacifico:400,400i,700,700i';
- echo '<link href="https://fonts.googleapis.com/css?family=' . $fonts . '" rel="stylesheet">';
- });
- }
- }
- // Main page
- $this->wp->addMenuPage(
- 'MailPoet',
- 'MailPoet',
- AccessControl::PERMISSION_ACCESS_PLUGIN_ADMIN,
- self::MAIN_PAGE_SLUG,
- null,
- 'none',
- 30
- );
- // Emails page
- $newslettersPage = $this->wp->addSubmenuPage(
- self::MAIN_PAGE_SLUG,
- $this->setPageTitle(__('Emails', 'mailpoet')),
- $this->wp->__('Emails', 'mailpoet'),
- AccessControl::PERMISSION_MANAGE_EMAILS,
- self::MAIN_PAGE_SLUG,
- [
- $this,
- 'newsletters',
- ]
- );
- // add limit per page to screen options
- $this->wp->addAction('load-' . $newslettersPage, function() {
- $this->wp->addScreenOption('per_page', [
- 'label' => $this->wp->_x(
- 'Number of newsletters per page',
- 'newsletters per page (screen options)',
- 'mailpoet'
- ),
- 'option' => 'mailpoet_newsletters_per_page',
- ]);
- });
- // newsletter editor
- $this->wp->addSubmenuPage(
- true,
- $this->setPageTitle(__('Newsletter', 'mailpoet')),
- $this->wp->__('Newsletter Editor', 'mailpoet'),
- AccessControl::PERMISSION_MANAGE_EMAILS,
- 'mailpoet-newsletter-editor',
- [
- $this,
- 'newletterEditor',
- ]
- );
- // Forms page
- $formsPage = $this->wp->addSubmenuPage(
- self::MAIN_PAGE_SLUG,
- $this->setPageTitle(__('Forms', 'mailpoet')),
- $this->wp->__('Forms', 'mailpoet'),
- AccessControl::PERMISSION_MANAGE_FORMS,
- 'mailpoet-forms',
- [
- $this,
- 'forms',
- ]
- );
- // add limit per page to screen options
- $this->wp->addAction('load-' . $formsPage, function() {
- $this->wp->addScreenOption('per_page', [
- 'label' => $this->wp->_x(
- 'Number of forms per page',
- 'forms per page (screen options)',
- 'mailpoet'
- ),
- 'option' => 'mailpoet_forms_per_page',
- ]);
- });
- // form editor
- $formEditorPage = $this->wp->addSubmenuPage(
- true,
- $this->setPageTitle(__('Form Editor', 'mailpoet')),
- $this->wp->__('Form Editor', 'mailpoet'),
- AccessControl::PERMISSION_MANAGE_FORMS,
- 'mailpoet-form-editor',
- [
- $this,
- 'formEditor',
- ]
- );
- // add body class for form editor page
- $this->wp->addAction('load-' . $formEditorPage, function() {
- $this->wp->addAction('admin_body_class', function ($classes) {
- return ltrim($classes . ' block-editor-page');
- });
- });
- // form editor templates
- $formTemplateSelectionEditorPage = $this->wp->addSubmenuPage(
- true,
- $this->setPageTitle(__('Select Form Template', 'mailpoet')),
- $this->wp->__('Select Form Template', 'mailpoet'),
- AccessControl::PERMISSION_MANAGE_FORMS,
- 'mailpoet-form-editor-template-selection',
- [
- $this,
- 'formEditorTemplateSelection',
- ]
- );
- // add body class for form editor page
- $this->wp->addAction('load-' . $formTemplateSelectionEditorPage, function() {
- $this->wp->addAction('admin_body_class', function ($classes) {
- return ltrim($classes . ' block-editor-page');
- });
- });
- // Subscribers page
- $subscribersPage = $this->wp->addSubmenuPage(
- self::MAIN_PAGE_SLUG,
- $this->setPageTitle(__('Subscribers', 'mailpoet')),
- $this->wp->__('Subscribers', 'mailpoet'),
- AccessControl::PERMISSION_MANAGE_SUBSCRIBERS,
- 'mailpoet-subscribers',
- [
- $this,
- 'subscribers',
- ]
- );
- // add limit per page to screen options
- $this->wp->addAction('load-' . $subscribersPage, function() {
- $this->wp->addScreenOption('per_page', [
- 'label' => $this->wp->_x(
- 'Number of subscribers per page',
- 'subscribers per page (screen options)',
- 'mailpoet'
- ),
- 'option' => 'mailpoet_subscribers_per_page',
- ]);
- });
- // import
- $this->wp->addSubmenuPage(
- 'admin.php?page=mailpoet-subscribers',
- $this->setPageTitle(__('Import', 'mailpoet')),
- $this->wp->__('Import', 'mailpoet'),
- AccessControl::PERMISSION_MANAGE_SUBSCRIBERS,
- 'mailpoet-import',
- [
- $this,
- 'import',
- ]
- );
- // export
- $this->wp->addSubmenuPage(
- true,
- $this->setPageTitle(__('Export', 'mailpoet')),
- $this->wp->__('Export', 'mailpoet'),
- AccessControl::PERMISSION_MANAGE_SUBSCRIBERS,
- 'mailpoet-export',
- [
- $this,
- 'export',
- ]
- );
- // Segments page
- $segmentsPage = $this->wp->addSubmenuPage(
- self::MAIN_PAGE_SLUG,
- $this->setPageTitle(__('Lists', 'mailpoet')),
- $this->wp->__('Lists', 'mailpoet'),
- AccessControl::PERMISSION_MANAGE_SEGMENTS,
- 'mailpoet-segments',
- [
- $this,
- 'segments',
- ]
- );
- // add limit per page to screen options
- $this->wp->addAction('load-' . $segmentsPage, function() {
- $this->wp->addScreenOption('per_page', [
- 'label' => $this->wp->_x(
- 'Number of segments per page',
- 'segments per page (screen options)',
- 'mailpoet'
- ),
- 'option' => 'mailpoet_segments_per_page',
- ]);
- });
- // Settings page
- $this->wp->addSubmenuPage(
- self::MAIN_PAGE_SLUG,
- $this->setPageTitle(__('Settings', 'mailpoet')),
- $this->wp->__('Settings', 'mailpoet'),
- AccessControl::PERMISSION_MANAGE_SETTINGS,
- 'mailpoet-settings',
- [
- $this,
- 'settings',
- ]
- );
- // Help page
- $this->wp->addSubmenuPage(
- self::MAIN_PAGE_SLUG,
- $this->setPageTitle(__('Help', 'mailpoet')),
- $this->wp->__('Help', 'mailpoet'),
- AccessControl::PERMISSION_ACCESS_PLUGIN_ADMIN,
- 'mailpoet-help',
- [
- $this,
- 'help',
- ]
- );
- // Premium page
- // Only show this page in menu if the Premium plugin is not activated
- $this->wp->addSubmenuPage(
- License::getLicense() ? true : self::MAIN_PAGE_SLUG,
- $this->setPageTitle(__('Premium', 'mailpoet')),
- $this->wp->__('Premium', 'mailpoet'),
- AccessControl::PERMISSION_ACCESS_PLUGIN_ADMIN,
- 'mailpoet-premium',
- [
- $this,
- 'premium',
- ]
- );
- // Welcome wizard page
- $this->wp->addSubmenuPage(
- true,
- $this->setPageTitle(__('Welcome Wizard', 'mailpoet')),
- $this->wp->__('Welcome Wizard', 'mailpoet'),
- AccessControl::PERMISSION_ACCESS_PLUGIN_ADMIN,
- 'mailpoet-welcome-wizard',
- [
- $this,
- 'welcomeWizard',
- ]
- );
- // WooCommerce Setup
- $this->wp->addSubmenuPage(
- true,
- $this->setPageTitle($this->wp->__('WooCommerce Setup', 'mailpoet')),
- $this->wp->__('WooCommerce Setup', 'mailpoet'),
- AccessControl::PERMISSION_ACCESS_PLUGIN_ADMIN,
- 'mailpoet-woocommerce-setup',
- [
- $this,
- 'wooCommerceSetup',
- ]
- );
- // Migration page
- $this->wp->addSubmenuPage(
- true,
- $this->setPageTitle(__('Migration', 'mailpoet')),
- '',
- AccessControl::PERMISSION_ACCESS_PLUGIN_ADMIN,
- 'mailpoet-migration',
- [
- $this,
- 'migration',
- ]
- );
- // Experimental page
- $this->wp->addSubmenuPage(
- true,
- $this->setPageTitle('Experimental Features'),
- '',
- AccessControl::PERMISSION_MANAGE_FEATURES,
- 'mailpoet-experimental',
- [$this, 'experimentalFeatures']
- );
- // display loggs page
- $this->wp->addSubmenuPage(
- true,
- $this->setPageTitle('Logs'),
- '',
- AccessControl::PERMISSION_ACCESS_PLUGIN_ADMIN,
- 'mailpoet-logs',
- [$this, 'logs']
- );
- }
- public function disableWPEmojis() {
- $this->wp->removeAction('admin_print_scripts', 'print_emoji_detection_script');
- $this->wp->removeAction('admin_print_styles', 'print_emoji_styles');
- }
- public function migration() {
- $this->container->get(MP2Migration::class)->render();
- }
- public function welcomeWizard() {
- $this->container->get(WelcomeWizard::class)->render();
- }
- public function wooCommerceSetup() {
- $this->container->get(WooCommerceSetup::class)->render();
- }
- public function premium() {
- $this->container->get(Premium::class)->render();
- }
- public function settings() {
- $this->container->get(Settings::class)->render();
- }
- public function help() {
- $this->container->get(Help::class)->render();
- }
- public function experimentalFeatures() {
- $this->container->get(ExperimentalFeatures::class)->render();
- }
- public function logs() {
- $this->container->get(Logs::class)->render();
- }
- public function subscribers() {
- $this->container->get(Subscribers::class)->render();
- }
- public function segments() {
- $this->container->get(Segments::class)->render();
- }
- public function forms() {
- $this->container->get(Forms::class)->render();
- }
- public function newsletters() {
- $this->container->get(Newsletters::class)->render();
- }
- public function newletterEditor() {
- $this->container->get(NewsletterEditor::class)->render();
- }
- public function import() {
- $this->container->get(SubscribersImport::class)->render();
- }
- public function export() {
- $this->container->get(SubscribersExport::class)->render();
- }
- public function formEditor() {
- $this->container->get(FormEditor::class)->render();
- }
- public function formEditorTemplateSelection() {
- $this->container->get(FormEditor::class)->renderTemplateSelection();
- }
- public function setPageTitle($title) {
- return sprintf(
- '%s - %s',
- $this->wp->__('MailPoet', 'mailpoet'),
- $title
- );
- }
- public static function isOnMailPoetAdminPage(array $exclude = null, $screenId = null) {
- if (is_null($screenId)) {
- if (empty($_REQUEST['page'])) {
- return false;
- }
- $screenId = $_REQUEST['page'];
- }
- if (!empty($exclude)) {
- foreach ($exclude as $slug) {
- if (stripos($screenId, $slug) !== false) {
- return false;
- }
- }
- }
- return (stripos($screenId, 'mailpoet-') !== false);
- }
- /**
- * This error page is used when the initialization is failed
- * to display admin notices only
- */
- public static function addErrorPage(AccessControl $accessControl) {
- if (!self::isOnMailPoetAdminPage()) {
- return false;
- }
- // Check if page already exists
- if (get_plugin_page_hook($_REQUEST['page'], '')
- || WPFunctions::get()->getPluginPageHook($_REQUEST['page'], self::MAIN_PAGE_SLUG)
- ) {
- return false;
- }
- WPFunctions::get()->addSubmenuPage(
- true,
- 'MailPoet',
- 'MailPoet',
- AccessControl::PERMISSION_ACCESS_PLUGIN_ADMIN,
- $_REQUEST['page'],
- [
- __CLASS__,
- 'errorPageCallback',
- ]
- );
- }
- public static function errorPageCallback() {
- // Used for displaying admin notices only
- }
- public function checkPremiumKey(ServicesChecker $checker = null) {
- $showNotices = isset($_SERVER['SCRIPT_NAME'])
- && stripos($_SERVER['SCRIPT_NAME'], 'plugins.php') !== false;
- $checker = $checker ?: $this->servicesChecker;
- $this->premiumKeyValid = $checker->isPremiumKeyValid($showNotices);
- }
- }
|