Menu.php
| 1 | <?php |
| 2 | |
| 3 | namespace MailPoet\Config; |
| 4 | |
| 5 | use Carbon\Carbon; |
| 6 | use MailPoet\Cron\CronHelper; |
| 7 | use MailPoet\Cron\CronTrigger; |
| 8 | use MailPoet\Form\Block; |
| 9 | use MailPoet\Form\Renderer as FormRenderer; |
| 10 | use MailPoet\Helpscout\Beacon; |
| 11 | use MailPoet\Listing; |
| 12 | use MailPoet\Mailer\MailerLog; |
| 13 | use MailPoet\Models\CustomField; |
| 14 | use MailPoet\Models\Form; |
| 15 | use MailPoet\Models\Segment; |
| 16 | use MailPoet\Models\Setting; |
| 17 | use MailPoet\Models\Subscriber; |
| 18 | use MailPoet\Newsletter\Shortcodes\ShortcodesHelper; |
| 19 | use MailPoet\Router\Endpoints\CronDaemon; |
| 20 | use MailPoet\Services\Bridge; |
| 21 | use MailPoet\Settings\Hosts; |
| 22 | use MailPoet\Settings\Pages; |
| 23 | use MailPoet\Subscribers\ImportExport\ImportExportFactory; |
| 24 | use MailPoet\Tasks\Sending; |
| 25 | use MailPoet\Tasks\State; |
| 26 | use MailPoet\Util\License\Features\Subscribers as SubscribersFeature; |
| 27 | use MailPoet\Util\License\License; |
| 28 | use MailPoet\WP\DateTime; |
| 29 | use MailPoet\WP\Notice as WPNotice; |
| 30 | use MailPoet\WP\Readme; |
| 31 | use MailPoet\WP\Functions as WPFunctions; |
| 32 | |
| 33 | if(!defined('ABSPATH')) exit; |
| 34 | |
| 35 | class Menu { |
| 36 | const MAIN_PAGE_SLUG = 'mailpoet-newsletters'; |
| 37 | |
| 38 | public $renderer; |
| 39 | private $access_control; |
| 40 | private $subscribers_over_limit; |
| 41 | |
| 42 | function __construct($renderer, AccessControl $access_control) { |
| 43 | $this->renderer = $renderer; |
| 44 | $this->access_control = $access_control; |
| 45 | } |
| 46 | |
| 47 | function init() { |
| 48 | $subscribers_feature = new SubscribersFeature(); |
| 49 | $this->subscribers_over_limit = $subscribers_feature->check(); |
| 50 | $this->checkMailPoetAPIKey(); |
| 51 | $this->checkPremiumKey(); |
| 52 | |
| 53 | add_action( |
| 54 | 'admin_menu', |
| 55 | array( |
| 56 | $this, |
| 57 | 'setup' |
| 58 | ) |
| 59 | ); |
| 60 | } |
| 61 | |
| 62 | function setup() { |
| 63 | if(!$this->access_control->validatePermission(AccessControl::PERMISSION_ACCESS_PLUGIN_ADMIN)) return; |
| 64 | if(self::isOnMailPoetAdminPage()) { |
| 65 | do_action('mailpoet_conflict_resolver_styles'); |
| 66 | do_action('mailpoet_conflict_resolver_scripts'); |
| 67 | |
| 68 | if($_REQUEST['page'] === 'mailpoet-newsletter-editor') { |
| 69 | // Disable WP emojis to not interfere with the newsletter editor emoji handling |
| 70 | $this->disableWPEmojis(); |
| 71 | add_action('admin_head', function() { |
| 72 | echo '<!--[if !mso]><link href="https://fonts.googleapis.com/css?family=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" rel="stylesheet"><![endif]-->'; |
| 73 | }); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // Main page |
| 78 | add_menu_page( |
| 79 | 'MailPoet', |
| 80 | 'MailPoet', |
| 81 | AccessControl::PERMISSION_ACCESS_PLUGIN_ADMIN, |
| 82 | self::MAIN_PAGE_SLUG, |
| 83 | null, |
| 84 | 'none', |
| 85 | 30 |
| 86 | ); |
| 87 | |
| 88 | // Emails page |
| 89 | $newsletters_page = add_submenu_page( |
| 90 | self::MAIN_PAGE_SLUG, |
| 91 | $this->setPageTitle(__('Emails', 'mailpoet')), |
| 92 | __('Emails', 'mailpoet'), |
| 93 | AccessControl::PERMISSION_MANAGE_EMAILS, |
| 94 | self::MAIN_PAGE_SLUG, |
| 95 | array( |
| 96 | $this, |
| 97 | 'newsletters' |
| 98 | ) |
| 99 | ); |
| 100 | |
| 101 | // add limit per page to screen options |
| 102 | add_action('load-' . $newsletters_page, function() { |
| 103 | add_screen_option('per_page', array( |
| 104 | 'label' => _x( |
| 105 | 'Number of newsletters per page', |
| 106 | 'newsletters per page (screen options)', |
| 107 | 'mailpoet' |
| 108 | ), |
| 109 | 'option' => 'mailpoet_newsletters_per_page' |
| 110 | )); |
| 111 | }); |
| 112 | |
| 113 | // newsletter editor |
| 114 | add_submenu_page( |
| 115 | true, |
| 116 | $this->setPageTitle(__('Newsletter', 'mailpoet')), |
| 117 | __('Newsletter Editor', 'mailpoet'), |
| 118 | AccessControl::PERMISSION_MANAGE_EMAILS, |
| 119 | 'mailpoet-newsletter-editor', |
| 120 | array( |
| 121 | $this, |
| 122 | 'newletterEditor' |
| 123 | ) |
| 124 | ); |
| 125 | |
| 126 | // Forms page |
| 127 | $forms_page = add_submenu_page( |
| 128 | self::MAIN_PAGE_SLUG, |
| 129 | $this->setPageTitle(__('Forms', 'mailpoet')), |
| 130 | __('Forms', 'mailpoet'), |
| 131 | AccessControl::PERMISSION_MANAGE_FORMS, |
| 132 | 'mailpoet-forms', |
| 133 | array( |
| 134 | $this, |
| 135 | 'forms' |
| 136 | ) |
| 137 | ); |
| 138 | |
| 139 | // add limit per page to screen options |
| 140 | add_action('load-' . $forms_page, function() { |
| 141 | add_screen_option('per_page', array( |
| 142 | 'label' => _x( |
| 143 | 'Number of forms per page', |
| 144 | 'forms per page (screen options)', |
| 145 | 'mailpoet' |
| 146 | ), |
| 147 | 'option' => 'mailpoet_forms_per_page' |
| 148 | )); |
| 149 | }); |
| 150 | |
| 151 | // form editor |
| 152 | add_submenu_page( |
| 153 | true, |
| 154 | $this->setPageTitle(__('Form Editor', 'mailpoet')), |
| 155 | __('Form Editor', 'mailpoet'), |
| 156 | AccessControl::PERMISSION_MANAGE_FORMS, |
| 157 | 'mailpoet-form-editor', |
| 158 | array( |
| 159 | $this, |
| 160 | 'formEditor' |
| 161 | ) |
| 162 | ); |
| 163 | |
| 164 | // Subscribers page |
| 165 | $subscribers_page = add_submenu_page( |
| 166 | self::MAIN_PAGE_SLUG, |
| 167 | $this->setPageTitle(__('Subscribers', 'mailpoet')), |
| 168 | __('Subscribers', 'mailpoet'), |
| 169 | AccessControl::PERMISSION_MANAGE_SUBSCRIBERS, |
| 170 | 'mailpoet-subscribers', |
| 171 | array( |
| 172 | $this, |
| 173 | 'subscribers' |
| 174 | ) |
| 175 | ); |
| 176 | |
| 177 | // add limit per page to screen options |
| 178 | add_action('load-' . $subscribers_page, function() { |
| 179 | add_screen_option('per_page', array( |
| 180 | 'label' => _x( |
| 181 | 'Number of subscribers per page', |
| 182 | 'subscribers per page (screen options)', |
| 183 | 'mailpoet' |
| 184 | ), |
| 185 | 'option' => 'mailpoet_subscribers_per_page' |
| 186 | )); |
| 187 | }); |
| 188 | |
| 189 | // import |
| 190 | add_submenu_page( |
| 191 | 'admin.php?page=mailpoet-subscribers', |
| 192 | $this->setPageTitle(__('Import', 'mailpoet')), |
| 193 | __('Import', 'mailpoet'), |
| 194 | AccessControl::PERMISSION_MANAGE_SUBSCRIBERS, |
| 195 | 'mailpoet-import', |
| 196 | array( |
| 197 | $this, |
| 198 | 'import' |
| 199 | ) |
| 200 | ); |
| 201 | |
| 202 | // export |
| 203 | add_submenu_page( |
| 204 | true, |
| 205 | $this->setPageTitle(__('Export', 'mailpoet')), |
| 206 | __('Export', 'mailpoet'), |
| 207 | AccessControl::PERMISSION_MANAGE_SUBSCRIBERS, |
| 208 | 'mailpoet-export', |
| 209 | array( |
| 210 | $this, |
| 211 | 'export' |
| 212 | ) |
| 213 | ); |
| 214 | |
| 215 | // Segments page |
| 216 | $segments_page = add_submenu_page( |
| 217 | self::MAIN_PAGE_SLUG, |
| 218 | $this->setPageTitle(__('Lists', 'mailpoet')), |
| 219 | __('Lists', 'mailpoet'), |
| 220 | AccessControl::PERMISSION_MANAGE_SEGMENTS, |
| 221 | 'mailpoet-segments', |
| 222 | array( |
| 223 | $this, |
| 224 | 'segments' |
| 225 | ) |
| 226 | ); |
| 227 | |
| 228 | // add limit per page to screen options |
| 229 | add_action('load-' . $segments_page, function() { |
| 230 | add_screen_option('per_page', array( |
| 231 | 'label' => _x( |
| 232 | 'Number of segments per page', |
| 233 | 'segments per page (screen options)', |
| 234 | 'mailpoet' |
| 235 | ), |
| 236 | 'option' => 'mailpoet_segments_per_page' |
| 237 | )); |
| 238 | }); |
| 239 | |
| 240 | do_action('mailpoet_menu_after_lists'); |
| 241 | |
| 242 | // Settings page |
| 243 | add_submenu_page( |
| 244 | self::MAIN_PAGE_SLUG, |
| 245 | $this->setPageTitle(__('Settings', 'mailpoet')), |
| 246 | __('Settings', 'mailpoet'), |
| 247 | AccessControl::PERMISSION_MANAGE_SETTINGS, |
| 248 | 'mailpoet-settings', |
| 249 | array( |
| 250 | $this, |
| 251 | 'settings' |
| 252 | ) |
| 253 | ); |
| 254 | |
| 255 | // Help page |
| 256 | add_submenu_page( |
| 257 | self::MAIN_PAGE_SLUG, |
| 258 | $this->setPageTitle(__('Help', 'mailpoet')), |
| 259 | __('Help', 'mailpoet'), |
| 260 | AccessControl::PERMISSION_ACCESS_PLUGIN_ADMIN, |
| 261 | 'mailpoet-help', |
| 262 | array( |
| 263 | $this, |
| 264 | 'help' |
| 265 | ) |
| 266 | ); |
| 267 | |
| 268 | // Premium page |
| 269 | // Only show this page in menu if the Premium plugin is not activated |
| 270 | add_submenu_page( |
| 271 | License::getLicense() ? true : self::MAIN_PAGE_SLUG, |
| 272 | $this->setPageTitle(__('Premium', 'mailpoet')), |
| 273 | __('Premium', 'mailpoet'), |
| 274 | AccessControl::PERMISSION_ACCESS_PLUGIN_ADMIN, |
| 275 | 'mailpoet-premium', |
| 276 | array( |
| 277 | $this, |
| 278 | 'premium' |
| 279 | ) |
| 280 | ); |
| 281 | |
| 282 | // Welcome wizard page |
| 283 | add_submenu_page( |
| 284 | true, |
| 285 | $this->setPageTitle(__('Welcome Wizard', 'mailpoet')), |
| 286 | __('Welcome Wizard', 'mailpoet'), |
| 287 | AccessControl::PERMISSION_ACCESS_PLUGIN_ADMIN, |
| 288 | 'mailpoet-welcome-wizard', |
| 289 | array( |
| 290 | $this, |
| 291 | 'welcomeWizard' |
| 292 | ) |
| 293 | ); |
| 294 | |
| 295 | // Update page |
| 296 | add_submenu_page( |
| 297 | true, |
| 298 | $this->setPageTitle(__('Update', 'mailpoet')), |
| 299 | __('Update', 'mailpoet'), |
| 300 | AccessControl::PERMISSION_ACCESS_PLUGIN_ADMIN, |
| 301 | 'mailpoet-update', |
| 302 | array( |
| 303 | $this, |
| 304 | 'update' |
| 305 | ) |
| 306 | ); |
| 307 | |
| 308 | // Migration page |
| 309 | add_submenu_page( |
| 310 | true, |
| 311 | $this->setPageTitle(__('Migration', 'mailpoet')), |
| 312 | '', |
| 313 | AccessControl::PERMISSION_ACCESS_PLUGIN_ADMIN, |
| 314 | 'mailpoet-migration', |
| 315 | array( |
| 316 | $this, |
| 317 | 'migration' |
| 318 | ) |
| 319 | ); |
| 320 | } |
| 321 | |
| 322 | function disableWPEmojis() { |
| 323 | remove_action('admin_print_scripts', 'print_emoji_detection_script'); |
| 324 | remove_action('admin_print_styles', 'print_emoji_styles'); |
| 325 | } |
| 326 | |
| 327 | function migration() { |
| 328 | $mp2_migrator = new MP2Migrator(); |
| 329 | $mp2_migrator->init(); |
| 330 | $data = array( |
| 331 | 'log_file_url' => $mp2_migrator->log_file_url, |
| 332 | 'progress_url' => $mp2_migrator->progressbar->url, |
| 333 | ); |
| 334 | $this->displayPage('mp2migration.html', $data); |
| 335 | } |
| 336 | |
| 337 | function welcomeWizard() { |
| 338 | if((bool)(defined('DOING_AJAX') && DOING_AJAX)) return; |
| 339 | $data = [ |
| 340 | 'is_mp2_migration_complete' => (bool)Setting::getValue('mailpoet_migration_complete'), |
| 341 | 'is_woocommerce_active' => class_exists('WooCommerce'), |
| 342 | 'finish_wizard_url' => admin_url('admin.php?page=' . self::MAIN_PAGE_SLUG), |
| 343 | 'sender' => Setting::getValue('sender') |
| 344 | ]; |
| 345 | $this->displayPage('welcome_wizard.html', $data); |
| 346 | } |
| 347 | |
| 348 | function update() { |
| 349 | global $wp; |
| 350 | $current_url = home_url(add_query_arg($wp->query_string, $wp->request)); |
| 351 | $redirect_url = |
| 352 | (!empty($_GET['mailpoet_redirect'])) |
| 353 | ? urldecode($_GET['mailpoet_redirect']) |
| 354 | : wp_get_referer(); |
| 355 | |
| 356 | if( |
| 357 | $redirect_url === $current_url |
| 358 | or |
| 359 | strpos($redirect_url, 'mailpoet') === false |
| 360 | ) { |
| 361 | $redirect_url = admin_url('admin.php?page=' . self::MAIN_PAGE_SLUG); |
| 362 | } |
| 363 | |
| 364 | $data = array( |
| 365 | 'settings' => Setting::getAll(), |
| 366 | 'current_user' => wp_get_current_user(), |
| 367 | 'redirect_url' => $redirect_url, |
| 368 | 'sub_menu' => self::MAIN_PAGE_SLUG, |
| 369 | ); |
| 370 | |
| 371 | $data['is_new_user'] = true; |
| 372 | $data['is_old_user'] = false; |
| 373 | if(!empty($data['settings']['installed_at'])) { |
| 374 | $installed_at = Carbon::createFromTimestamp(strtotime($data['settings']['installed_at'])); |
| 375 | $current_time = Carbon::createFromTimestamp(WPFunctions::currentTime('timestamp')); |
| 376 | $data['is_new_user'] = $current_time->diffInDays($installed_at) <= 30; |
| 377 | $data['is_old_user'] = $current_time->diffInMonths($installed_at) >= 6; |
| 378 | $data['stop_call_for_rating'] = isset($data['settings']['stop_call_for_rating']) ? $data['settings']['stop_call_for_rating'] : false; |
| 379 | } |
| 380 | |
| 381 | $readme_file = Env::$path . '/readme.txt'; |
| 382 | if(is_readable($readme_file)) { |
| 383 | $changelog = Readme::parseChangelog(file_get_contents($readme_file), 1); |
| 384 | if($changelog) { |
| 385 | $data['changelog'] = $changelog; |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | $this->displayPage('update.html', $data); |
| 390 | } |
| 391 | |
| 392 | function premium() { |
| 393 | $data = array( |
| 394 | 'subscriber_count' => Subscriber::getTotalSubscribers(), |
| 395 | 'sub_menu' => self::MAIN_PAGE_SLUG, |
| 396 | 'display_discount' => time() <= strtotime('2018-11-30 23:59:59') |
| 397 | ); |
| 398 | |
| 399 | $this->displayPage('premium.html', $data); |
| 400 | } |
| 401 | |
| 402 | |
| 403 | function settings() { |
| 404 | $settings = Setting::getAll(); |
| 405 | $flags = $this->_getFlags(); |
| 406 | |
| 407 | // force MSS key check even if the method isn't active |
| 408 | $checker = new ServicesChecker(); |
| 409 | $mp_api_key_valid = $checker->isMailPoetAPIKeyValid(false, true); |
| 410 | |
| 411 | $data = array( |
| 412 | 'settings' => $settings, |
| 413 | 'segments' => Segment::getSegmentsWithSubscriberCount(), |
| 414 | 'cron_trigger' => CronTrigger::getAvailableMethods(), |
| 415 | 'total_subscribers' => Subscriber::getTotalSubscribers(), |
| 416 | 'premium_plugin_active' => License::getLicense(), |
| 417 | 'premium_key_valid' => !empty($this->premium_key_valid), |
| 418 | 'mss_active' => Bridge::isMPSendingServiceEnabled(), |
| 419 | 'mss_key_valid' => !empty($mp_api_key_valid), |
| 420 | 'members_plugin_active' => is_plugin_active('members/members.php'), |
| 421 | 'pages' => Pages::getAll(), |
| 422 | 'flags' => $flags, |
| 423 | 'current_user' => wp_get_current_user(), |
| 424 | 'linux_cron_path' => dirname(dirname(__DIR__)), |
| 425 | 'ABSPATH' => ABSPATH, |
| 426 | 'hosts' => array( |
| 427 | 'web' => Hosts::getWebHosts(), |
| 428 | 'smtp' => Hosts::getSMTPHosts() |
| 429 | ) |
| 430 | ); |
| 431 | |
| 432 | $data['is_new_user'] = $this->isNewUser(); |
| 433 | |
| 434 | $data = array_merge($data, Installer::getPremiumStatus()); |
| 435 | |
| 436 | $this->displayPage('settings.html', $data); |
| 437 | } |
| 438 | |
| 439 | |
| 440 | function help() { |
| 441 | $tasks_state = new State(); |
| 442 | $system_info_data = Beacon::getData(); |
| 443 | $system_status_data = [ |
| 444 | 'cron' => [ |
| 445 | 'url' => CronHelper::getCronUrl(CronDaemon::ACTION_PING), |
| 446 | 'isReachable' => CronHelper::pingDaemon(true) |
| 447 | ], |
| 448 | 'mss' => [ |
| 449 | 'enabled' => (Bridge::isMPSendingServiceEnabled()) ? |
| 450 | ['isReachable' => Bridge::pingBridge()] : |
| 451 | false |
| 452 | ], |
| 453 | 'cronStatus' => CronHelper::getDaemon(), |
| 454 | 'queueStatus' => MailerLog::getMailerLog(), |
| 455 | ]; |
| 456 | $system_status_data['cronStatus']['accessible'] = CronHelper::isDaemonAccessible(); |
| 457 | $system_status_data['queueStatus']['tasksStatusCounts'] = $tasks_state->getCountsPerStatus(); |
| 458 | $system_status_data['queueStatus']['latestTasks'] = $tasks_state->getLatestTasks(Sending::TASK_TYPE); |
| 459 | $this->displayPage( |
| 460 | 'help.html', |
| 461 | array( |
| 462 | 'systemInfoData' => $system_info_data, |
| 463 | 'systemStatusData' => $system_status_data |
| 464 | ) |
| 465 | ); |
| 466 | } |
| 467 | |
| 468 | private function _getFlags() { |
| 469 | // flags (available features on WP install) |
| 470 | $flags = array(); |
| 471 | |
| 472 | if(is_multisite()) { |
| 473 | // get multisite registration option |
| 474 | $registration = apply_filters( |
| 475 | 'wpmu_registration_enabled', |
| 476 | get_site_option('registration', 'all') |
| 477 | ); |
| 478 | |
| 479 | // check if users can register |
| 480 | $flags['registration_enabled'] = |
| 481 | !(in_array($registration, array( |
| 482 | 'none', |
| 483 | 'blog' |
| 484 | ))); |
| 485 | } else { |
| 486 | // check if users can register |
| 487 | $flags['registration_enabled'] = |
| 488 | (bool)get_option('users_can_register', false); |
| 489 | } |
| 490 | |
| 491 | return $flags; |
| 492 | } |
| 493 | |
| 494 | function subscribers() { |
| 495 | $data = array(); |
| 496 | |
| 497 | $data['items_per_page'] = $this->getLimitPerPage('subscribers'); |
| 498 | $segments = Segment::getSegmentsWithSubscriberCount($type = false); |
| 499 | $segments = apply_filters('mailpoet_segments_with_subscriber_count', $segments); |
| 500 | usort($segments, function ($a, $b) { |
| 501 | return strcasecmp($a["name"], $b["name"]); |
| 502 | }); |
| 503 | $data['segments'] = $segments; |
| 504 | |
| 505 | $data['custom_fields'] = array_map(function($field) { |
| 506 | $field['params'] = unserialize($field['params']); |
| 507 | |
| 508 | if(!empty($field['params']['values'])) { |
| 509 | $values = array(); |
| 510 | |
| 511 | foreach($field['params']['values'] as $value) { |
| 512 | $values[$value['value']] = $value['value']; |
| 513 | } |
| 514 | $field['params']['values'] = $values; |
| 515 | } |
| 516 | return $field; |
| 517 | }, CustomField::findArray()); |
| 518 | |
| 519 | $data['date_formats'] = Block\Date::getDateFormats(); |
| 520 | $data['month_names'] = Block\Date::getMonthNames(); |
| 521 | |
| 522 | $data['premium_plugin_active'] = License::getLicense(); |
| 523 | $data['mss_active'] = Bridge::isMPSendingServiceEnabled(); |
| 524 | |
| 525 | $this->displayPage('subscribers/subscribers.html', $data); |
| 526 | } |
| 527 | |
| 528 | function segments() { |
| 529 | $data = array(); |
| 530 | $data['items_per_page'] = $this->getLimitPerPage('segments'); |
| 531 | $this->displayPage('segments.html', $data); |
| 532 | } |
| 533 | |
| 534 | function forms() { |
| 535 | if($this->subscribers_over_limit) return $this->displaySubscriberLimitExceededTemplate(); |
| 536 | |
| 537 | $data = array(); |
| 538 | |
| 539 | $data['items_per_page'] = $this->getLimitPerPage('forms'); |
| 540 | $data['segments'] = Segment::findArray(); |
| 541 | |
| 542 | $data['is_new_user'] = $this->isNewUser(); |
| 543 | |
| 544 | $this->displayPage('forms.html', $data); |
| 545 | } |
| 546 | |
| 547 | function newsletters() { |
| 548 | if($this->subscribers_over_limit) return $this->displaySubscriberLimitExceededTemplate(); |
| 549 | if(isset($this->mp_api_key_valid) && $this->mp_api_key_valid === false) { |
| 550 | return $this->displayMailPoetAPIKeyInvalidTemplate(); |
| 551 | } |
| 552 | |
| 553 | global $wp_roles; |
| 554 | |
| 555 | $data = array(); |
| 556 | |
| 557 | $data['items_per_page'] = $this->getLimitPerPage('newsletters'); |
| 558 | $segments = Segment::getSegmentsWithSubscriberCount($type = false); |
| 559 | $segments = apply_filters('mailpoet_segments_with_subscriber_count', $segments); |
| 560 | usort($segments, function ($a, $b) { |
| 561 | return strcasecmp($a["name"], $b["name"]); |
| 562 | }); |
| 563 | $data['segments'] = $segments; |
| 564 | $data['settings'] = Setting::getAll(); |
| 565 | $data['current_wp_user'] = wp_get_current_user()->to_array(); |
| 566 | $data['site_url'] = site_url(); |
| 567 | $data['roles'] = $wp_roles->get_names(); |
| 568 | $data['roles']['mailpoet_all'] = __('In any WordPress role', 'mailpoet'); |
| 569 | |
| 570 | $date_time = new DateTime(); |
| 571 | $data['current_date'] = $date_time->getCurrentDate(DateTime::DEFAULT_DATE_FORMAT); |
| 572 | $data['current_time'] = $date_time->getCurrentTime(); |
| 573 | $data['schedule_time_of_day'] = $date_time->getTimeInterval( |
| 574 | '00:00:00', |
| 575 | '+1 hour', |
| 576 | 24 |
| 577 | ); |
| 578 | $data['mailpoet_main_page'] = admin_url('admin.php?page=' . self::MAIN_PAGE_SLUG); |
| 579 | $data['show_congratulate_after_first_newsletter'] = isset($data['settings']['show_congratulate_after_first_newsletter'])?$data['settings']['show_congratulate_after_first_newsletter']:'false'; |
| 580 | |
| 581 | $data['tracking_enabled'] = Setting::getValue('tracking.enabled'); |
| 582 | $data['premium_plugin_active'] = License::getLicense(); |
| 583 | $data['is_woocommerce_active'] = class_exists('WooCommerce'); |
| 584 | |
| 585 | $data['automatic_emails'] = array( |
| 586 | array( |
| 587 | 'slug' => 'woocommerce', |
| 588 | 'beta' => true, |
| 589 | 'premium' => true, |
| 590 | 'title' => __('WooCommerce', 'mailpoet'), |
| 591 | 'description' => __('Automatically send an email when there is a new WooCommerce product, order and some other action takes place.', 'mailpoet'), |
| 592 | 'events' => array( |
| 593 | array( |
| 594 | 'slug' => 'woocommerce_abandoned_shopping_cart', |
| 595 | 'title' => __('Abandoned Shopping Cart', 'mailpoet'), |
| 596 | 'description' => __('Send an email to logged-in visitors who have items in their shopping carts but left your website without checking out. Can convert up to 5% of abandoned carts.', 'mailpoet'), |
| 597 | 'soon' => true, |
| 598 | 'badge' => array( |
| 599 | 'text' => __('Must-have', 'mailpoet'), |
| 600 | 'style' => 'red' |
| 601 | ) |
| 602 | ), |
| 603 | array( |
| 604 | 'slug' => 'woocommerce_big_spender', |
| 605 | 'title' => __('Big Spender', 'mailpoet'), |
| 606 | 'description' => __('Let MailPoet send an email to customers who have spent a certain amount to thank them, possibly with a coupon.', 'mailpoet'), |
| 607 | 'soon' => true, |
| 608 | 'badge' => array( |
| 609 | 'text' => __('Smart to have', 'mailpoet'), |
| 610 | 'style' => 'teal' |
| 611 | ) |
| 612 | ), |
| 613 | array( |
| 614 | 'slug' => 'woocommerce_first_purchase', |
| 615 | 'title' => __('First Purchase', 'mailpoet'), |
| 616 | 'description' => __('Let MailPoet send an email to customers who make their first purchase.', 'mailpoet'), |
| 617 | 'badge' => array( |
| 618 | 'text' => __('Must-have', 'mailpoet'), |
| 619 | 'style' => 'red' |
| 620 | ) |
| 621 | ), |
| 622 | array( |
| 623 | 'slug' => 'woocommerce_product_purchased_in_category', |
| 624 | 'title' => __('Purchased In This Category', 'mailpoet'), |
| 625 | 'description' => __('Let MailPoet send an email to customers who purchase a product from a specific category.', 'mailpoet'), |
| 626 | 'soon' => true |
| 627 | ), |
| 628 | array( |
| 629 | 'slug' => 'woocommerce_product_purchased', |
| 630 | 'title' => __('Purchased This Product', 'mailpoet'), |
| 631 | 'description' => __('Let MailPoet send an email to customers who purchase a specific product.', 'mailpoet'), |
| 632 | ) |
| 633 | ) |
| 634 | ) |
| 635 | ); |
| 636 | |
| 637 | $data['is_new_user'] = $this->isNewUser(); |
| 638 | |
| 639 | wp_enqueue_script('jquery-ui'); |
| 640 | wp_enqueue_script('jquery-ui-datepicker'); |
| 641 | |
| 642 | $this->displayPage('newsletters.html', $data); |
| 643 | } |
| 644 | |
| 645 | function newletterEditor() { |
| 646 | $subscriber = Subscriber::getCurrentWPUser(); |
| 647 | $subscriber_data = $subscriber ? $subscriber->asArray() : []; |
| 648 | $data = array( |
| 649 | 'shortcodes' => ShortcodesHelper::getShortcodes(), |
| 650 | 'settings' => Setting::getAll(), |
| 651 | 'current_wp_user' => array_merge($subscriber_data, wp_get_current_user()->to_array()), |
| 652 | 'sub_menu' => self::MAIN_PAGE_SLUG, |
| 653 | 'mss_active' => Bridge::isMPSendingServiceEnabled() |
| 654 | ); |
| 655 | wp_enqueue_media(); |
| 656 | wp_enqueue_script('tinymce-wplink', includes_url('js/tinymce/plugins/wplink/plugin.js')); |
| 657 | wp_enqueue_style('editor', includes_url('css/editor.css')); |
| 658 | |
| 659 | $this->displayPage('newsletter/editor.html', $data); |
| 660 | } |
| 661 | |
| 662 | function import() { |
| 663 | $import = new ImportExportFactory(ImportExportFactory::IMPORT_ACTION); |
| 664 | $data = $import->bootstrap(); |
| 665 | $data = array_merge($data, array( |
| 666 | 'date_types' => Block\Date::getDateTypes(), |
| 667 | 'date_formats' => Block\Date::getDateFormats(), |
| 668 | 'month_names' => Block\Date::getMonthNames(), |
| 669 | 'sub_menu' => 'mailpoet-subscribers' |
| 670 | )); |
| 671 | |
| 672 | $data['is_new_user'] = $this->isNewUser(); |
| 673 | |
| 674 | $this->displayPage('subscribers/importExport/import.html', $data); |
| 675 | } |
| 676 | |
| 677 | function export() { |
| 678 | $export = new ImportExportFactory(ImportExportFactory::EXPORT_ACTION); |
| 679 | $data = $export->bootstrap(); |
| 680 | $data['sub_menu'] = 'mailpoet-subscribers'; |
| 681 | $this->displayPage('subscribers/importExport/export.html', $data); |
| 682 | } |
| 683 | |
| 684 | function formEditor() { |
| 685 | $id = (isset($_GET['id']) ? (int)$_GET['id'] : 0); |
| 686 | $form = Form::findOne($id); |
| 687 | if($form !== false) { |
| 688 | $form = $form->asArray(); |
| 689 | } |
| 690 | |
| 691 | $data = array( |
| 692 | 'form' => $form, |
| 693 | 'pages' => Pages::getAll(), |
| 694 | 'segments' => Segment::getSegmentsWithSubscriberCount(), |
| 695 | 'styles' => FormRenderer::getStyles($form), |
| 696 | 'date_types' => Block\Date::getDateTypes(), |
| 697 | 'date_formats' => Block\Date::getDateFormats(), |
| 698 | 'month_names' => Block\Date::getMonthNames(), |
| 699 | 'sub_menu' => 'mailpoet-forms' |
| 700 | ); |
| 701 | |
| 702 | $this->displayPage('form/editor.html', $data); |
| 703 | } |
| 704 | |
| 705 | function setPageTitle($title) { |
| 706 | return sprintf( |
| 707 | '%s - %s', |
| 708 | __('MailPoet', 'mailpoet'), |
| 709 | $title |
| 710 | ); |
| 711 | } |
| 712 | |
| 713 | function displaySubscriberLimitExceededTemplate() { |
| 714 | $this->displayPage('limit.html', array( |
| 715 | 'limit' => SubscribersFeature::SUBSCRIBERS_LIMIT |
| 716 | )); |
| 717 | exit; |
| 718 | } |
| 719 | |
| 720 | function displayMailPoetAPIKeyInvalidTemplate() { |
| 721 | $this->displayPage('invalidkey.html', array( |
| 722 | 'subscriber_count' => Subscriber::getTotalSubscribers() |
| 723 | )); |
| 724 | exit; |
| 725 | } |
| 726 | |
| 727 | static function isOnMailPoetAdminPage(array $exclude = null, $screen_id = null) { |
| 728 | if(is_null($screen_id)) { |
| 729 | if(empty($_REQUEST['page'])) { |
| 730 | return false; |
| 731 | } |
| 732 | $screen_id = $_REQUEST['page']; |
| 733 | } |
| 734 | if(!empty($exclude)) { |
| 735 | foreach($exclude as $slug) { |
| 736 | if(stripos($screen_id, $slug) !== false) { |
| 737 | return false; |
| 738 | } |
| 739 | } |
| 740 | } |
| 741 | return (stripos($screen_id, 'mailpoet-') !== false); |
| 742 | } |
| 743 | |
| 744 | /** |
| 745 | * This error page is used when the initialization is failed |
| 746 | * to display admin notices only |
| 747 | */ |
| 748 | static function addErrorPage(AccessControl $access_control) { |
| 749 | if(!self::isOnMailPoetAdminPage()) { |
| 750 | return false; |
| 751 | } |
| 752 | // Check if page already exists |
| 753 | if(get_plugin_page_hook($_REQUEST['page'], '') |
| 754 | || get_plugin_page_hook($_REQUEST['page'], self::MAIN_PAGE_SLUG) |
| 755 | ) { |
| 756 | return false; |
| 757 | } |
| 758 | add_submenu_page( |
| 759 | true, |
| 760 | 'MailPoet', |
| 761 | 'MailPoet', |
| 762 | AccessControl::PERMISSION_ACCESS_PLUGIN_ADMIN, |
| 763 | $_REQUEST['page'], |
| 764 | array( |
| 765 | __CLASS__, |
| 766 | 'errorPageCallback' |
| 767 | ) |
| 768 | ); |
| 769 | } |
| 770 | |
| 771 | static function errorPageCallback() { |
| 772 | // Used for displaying admin notices only |
| 773 | } |
| 774 | |
| 775 | function checkMailPoetAPIKey(ServicesChecker $checker = null) { |
| 776 | if(self::isOnMailPoetAdminPage()) { |
| 777 | $show_notices = isset($_REQUEST['page']) |
| 778 | && stripos($_REQUEST['page'], self::MAIN_PAGE_SLUG) === false; |
| 779 | $checker = $checker ?: new ServicesChecker(); |
| 780 | $this->mp_api_key_valid = $checker->isMailPoetAPIKeyValid($show_notices); |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | function checkPremiumKey(ServicesChecker $checker = null) { |
| 785 | $show_notices = isset($_SERVER['SCRIPT_NAME']) |
| 786 | && stripos($_SERVER['SCRIPT_NAME'], 'plugins.php') !== false; |
| 787 | $checker = $checker ?: new ServicesChecker(); |
| 788 | $this->premium_key_valid = $checker->isPremiumKeyValid($show_notices); |
| 789 | } |
| 790 | |
| 791 | function getLimitPerPage($model = null) { |
| 792 | if($model === null) { |
| 793 | return Listing\Handler::DEFAULT_LIMIT_PER_PAGE; |
| 794 | } |
| 795 | |
| 796 | $listing_per_page = get_user_meta( |
| 797 | get_current_user_id(), 'mailpoet_' . $model . '_per_page', true |
| 798 | ); |
| 799 | return (!empty($listing_per_page)) |
| 800 | ? (int)$listing_per_page |
| 801 | : Listing\Handler::DEFAULT_LIMIT_PER_PAGE; |
| 802 | } |
| 803 | |
| 804 | function displayPage($template, $data) { |
| 805 | try { |
| 806 | echo $this->renderer->render($template, $data); |
| 807 | } catch(\Exception $e) { |
| 808 | $notice = new WPNotice(WPNotice::TYPE_ERROR, $e->getMessage()); |
| 809 | $notice->displayWPNotice(); |
| 810 | } |
| 811 | } |
| 812 | |
| 813 | function isNewUser() { |
| 814 | $installed_at = Setting::getValue('installed_at'); |
| 815 | if(is_null($installed_at)) { |
| 816 | return true; |
| 817 | } |
| 818 | $installed_at = Carbon::createFromTimestamp(strtotime($installed_at)); |
| 819 | $current_time = Carbon::createFromTimestamp(WPFunctions::currentTime('timestamp')); |
| 820 | return $current_time->diffInDays($installed_at) <= 30; |
| 821 | } |
| 822 | } |
| 823 |