Admin
4 years ago
Compatibility
4 years ago
Helpers
4 years ago
Providers
4 years ago
Reports
4 years ago
Tasks
4 years ago
UsageTracking
4 years ago
Conflicts.php
4 years ago
Connect.php
4 years ago
Core.php
4 years ago
Debug.php
4 years ago
Geo.php
4 years ago
MailCatcher.php
4 years ago
MailCatcherInterface.php
4 years ago
MailCatcherV6.php
4 years ago
Migration.php
4 years ago
MigrationAbstract.php
4 years ago
Options.php
4 years ago
Processor.php
4 years ago
SiteHealth.php
4 years ago
Upgrade.php
4 years ago
Uploads.php
4 years ago
WP.php
4 years ago
Core.php
1172 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPMailSMTP; |
| 4 | |
| 5 | use WPMailSMTP\Admin\AdminBarMenu; |
| 6 | use WPMailSMTP\Admin\DashboardWidget; |
| 7 | use WPMailSMTP\Admin\DebugEvents\DebugEvents; |
| 8 | use WPMailSMTP\Admin\Notifications; |
| 9 | use WPMailSMTP\Tasks\Meta; |
| 10 | use WPMailSMTP\UsageTracking\UsageTracking; |
| 11 | use WPMailSMTP\Compatibility\Compatibility; |
| 12 | use WPMailSMTP\Reports\Reports; |
| 13 | |
| 14 | /** |
| 15 | * Class Core to handle all plugin initialization. |
| 16 | * |
| 17 | * @since 1.0.0 |
| 18 | */ |
| 19 | class Core { |
| 20 | |
| 21 | /** |
| 22 | * URL to plugin directory. |
| 23 | * |
| 24 | * @since 1.0.0 |
| 25 | * |
| 26 | * @var string Without trailing slash. |
| 27 | */ |
| 28 | public $plugin_url; |
| 29 | |
| 30 | /** |
| 31 | * URL to Lite plugin assets directory. |
| 32 | * |
| 33 | * @since 1.5.0 |
| 34 | * |
| 35 | * @var string Without trailing slash. |
| 36 | */ |
| 37 | public $assets_url; |
| 38 | |
| 39 | /** |
| 40 | * Path to plugin directory. |
| 41 | * |
| 42 | * @since 1.0.0 |
| 43 | * |
| 44 | * @var string Without trailing slash. |
| 45 | */ |
| 46 | public $plugin_path; |
| 47 | |
| 48 | /** |
| 49 | * Shortcut to get access to Pro functionality using wp_mail_smtp()->pro->example(). |
| 50 | * |
| 51 | * @since 1.5.0 |
| 52 | * |
| 53 | * @var \WPMailSMTP\Pro\Pro |
| 54 | */ |
| 55 | public $pro; |
| 56 | |
| 57 | /** |
| 58 | * Core constructor. |
| 59 | * |
| 60 | * @since 1.0.0 |
| 61 | */ |
| 62 | public function __construct() { |
| 63 | |
| 64 | $this->plugin_url = rtrim( plugin_dir_url( __DIR__ ), '/\\' ); |
| 65 | $this->assets_url = $this->plugin_url . '/assets'; |
| 66 | $this->plugin_path = rtrim( plugin_dir_path( __DIR__ ), '/\\' ); |
| 67 | |
| 68 | if ( $this->is_not_loadable() ) { |
| 69 | add_action( 'admin_notices', 'wp_mail_smtp_insecure_php_version_notice' ); |
| 70 | |
| 71 | if ( WP::use_global_plugin_settings() ) { |
| 72 | add_action( 'network_admin_notices', 'wp_mail_smtp_insecure_php_version_notice' ); |
| 73 | } |
| 74 | |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | // Finally, load all the plugin. |
| 79 | $this->hooks(); |
| 80 | $this->init_early(); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Currently used for Pro version only. |
| 85 | * |
| 86 | * @since 1.5.0 |
| 87 | * |
| 88 | * @return bool |
| 89 | */ |
| 90 | protected function is_not_loadable() { |
| 91 | |
| 92 | // Check the Pro. |
| 93 | if ( |
| 94 | is_readable( $this->plugin_path . '/src/Pro/Pro.php' ) && |
| 95 | ! $this->is_pro_allowed() |
| 96 | ) { |
| 97 | // So there is a Pro version, but its PHP version check failed. |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Assign all hooks to proper places. |
| 106 | * |
| 107 | * @since 1.0.0 |
| 108 | */ |
| 109 | public function hooks() { |
| 110 | |
| 111 | // Activation hook. |
| 112 | register_activation_hook( WPMS_PLUGIN_FILE, [ $this, 'activate' ] ); |
| 113 | |
| 114 | // Redefine PHPMailer. |
| 115 | add_action( 'plugins_loaded', [ $this, 'get_processor' ] ); |
| 116 | add_action( 'plugins_loaded', [ $this, 'replace_phpmailer' ] ); |
| 117 | |
| 118 | // Various notifications. |
| 119 | add_action( 'admin_init', [ $this, 'init_notifications' ] ); |
| 120 | |
| 121 | add_action( 'init', [ $this, 'init' ] ); |
| 122 | |
| 123 | // Initialize Action Scheduler tasks. |
| 124 | add_action( 'init', [ $this, 'get_tasks' ], 5 ); |
| 125 | |
| 126 | // Initialize DB migrations. |
| 127 | add_action( 'admin_init', [ $this, 'init_migrations' ] ); |
| 128 | |
| 129 | add_action( 'plugins_loaded', [ $this, 'get_pro' ] ); |
| 130 | add_action( 'plugins_loaded', [ $this, 'get_usage_tracking' ] ); |
| 131 | add_action( 'plugins_loaded', [ $this, 'get_admin_bar_menu' ] ); |
| 132 | add_action( 'plugins_loaded', [ $this, 'get_notifications' ] ); |
| 133 | add_action( 'plugins_loaded', [ $this, 'get_connect' ], 15 ); |
| 134 | add_action( 'plugins_loaded', [ $this, 'get_compatibility' ], 0 ); |
| 135 | add_action( 'plugins_loaded', [ $this, 'get_dashboard_widget' ], 20 ); |
| 136 | add_action( 'plugins_loaded', [ $this, 'get_reports' ] ); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Initial plugin actions. |
| 141 | * |
| 142 | * @since 1.0.0 |
| 143 | */ |
| 144 | public function init() { |
| 145 | |
| 146 | // Load translations just in case. |
| 147 | load_plugin_textdomain( 'wp-mail-smtp', false, plugin_basename( wp_mail_smtp()->plugin_path ) . '/assets/languages' ); |
| 148 | |
| 149 | /* |
| 150 | * Constantly check in admin area, that we don't need to upgrade DB. |
| 151 | * Do not wait for the `admin_init` hook, because some actions are already done |
| 152 | * on `plugins_loaded`, so migration has to be done before. |
| 153 | * We should not fire this in AJAX requests. |
| 154 | */ |
| 155 | if ( WP::in_wp_admin() ) { |
| 156 | $this->get_upgrade(); |
| 157 | $this->detect_conflicts(); |
| 158 | } |
| 159 | |
| 160 | // In admin area, regardless of AJAX or not AJAX request. |
| 161 | if ( is_admin() ) { |
| 162 | $this->get_admin(); |
| 163 | $this->get_site_health()->init(); |
| 164 | |
| 165 | // Register Debug Event hooks. |
| 166 | ( new DebugEvents() )->hooks(); |
| 167 | } |
| 168 | |
| 169 | // Plugin admin area notices. Display to "admins" only. |
| 170 | if ( current_user_can( 'manage_options' ) ) { |
| 171 | add_action( 'admin_notices', array( '\WPMailSMTP\WP', 'display_admin_notices' ) ); |
| 172 | add_action( 'admin_notices', array( $this, 'display_general_notices' ) ); |
| 173 | |
| 174 | if ( WP::use_global_plugin_settings() ) { |
| 175 | add_action( 'network_admin_notices', array( '\WPMailSMTP\WP', 'display_admin_notices' ) ); |
| 176 | add_action( 'network_admin_notices', array( $this, 'display_general_notices' ) ); |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Whether the Pro part of the plugin is allowed to be loaded. |
| 183 | * |
| 184 | * @since 1.5.0 |
| 185 | * @since 1.6.0 Added a filter. |
| 186 | * |
| 187 | * @return bool |
| 188 | */ |
| 189 | protected function is_pro_allowed() { |
| 190 | |
| 191 | $is_allowed = true; |
| 192 | |
| 193 | if ( ! is_readable( $this->plugin_path . '/src/Pro/Pro.php' ) ) { |
| 194 | $is_allowed = false; |
| 195 | } |
| 196 | |
| 197 | if ( version_compare( phpversion(), '5.6', '<' ) ) { |
| 198 | $is_allowed = false; |
| 199 | } |
| 200 | |
| 201 | return apply_filters( 'wp_mail_smtp_core_is_pro_allowed', $is_allowed ); |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Get/Load the Pro code of the plugin if it exists. |
| 206 | * |
| 207 | * @since 1.6.2 |
| 208 | * |
| 209 | * @return \WPMailSMTP\Pro\Pro |
| 210 | */ |
| 211 | public function get_pro() { |
| 212 | |
| 213 | if ( ! $this->is_pro_allowed() ) { |
| 214 | return $this->pro; |
| 215 | } |
| 216 | |
| 217 | if ( ! $this->is_pro() ) { |
| 218 | $this->pro = new \WPMailSMTP\Pro\Pro(); |
| 219 | } |
| 220 | |
| 221 | return $this->pro; |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Get/Load the Tasks code of the plugin. |
| 226 | * |
| 227 | * @since 2.1.0 |
| 228 | * |
| 229 | * @return \WPMailSMTP\Tasks\Tasks |
| 230 | */ |
| 231 | public function get_tasks() { |
| 232 | |
| 233 | static $tasks; |
| 234 | |
| 235 | if ( ! isset( $tasks ) ) { |
| 236 | $tasks = apply_filters( 'wp_mail_smtp_core_get_tasks', new Tasks\Tasks() ); |
| 237 | $tasks->init(); |
| 238 | } |
| 239 | |
| 240 | return $tasks; |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * This method allows to overwrite certain core WP functions, because it's fired: |
| 245 | * - after `muplugins_loaded` hook, |
| 246 | * - before WordPress own `wp-includes/pluggable.php` file include, |
| 247 | * - before `plugin_loaded` and `plugins_loaded` hooks. |
| 248 | * |
| 249 | * @since 1.5.0 |
| 250 | */ |
| 251 | protected function init_early() { |
| 252 | |
| 253 | // Action Scheduler requires a special early loading procedure. |
| 254 | $this->load_action_scheduler(); |
| 255 | |
| 256 | // Load Pro specific files early. |
| 257 | $pro_files = $this->is_pro_allowed() ? \WPMailSMTP\Pro\Pro::PLUGGABLE_FILES : array(); |
| 258 | |
| 259 | $files = (array) apply_filters( 'wp_mail_smtp_core_init_early_include_files', $pro_files ); |
| 260 | |
| 261 | foreach ( $files as $file ) { |
| 262 | $path = $this->plugin_path . '/' . $file; |
| 263 | |
| 264 | if ( is_readable( $path ) ) { |
| 265 | /** @noinspection PhpIncludeInspection */ |
| 266 | include_once $path; |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Load the plugin core processor. |
| 273 | * |
| 274 | * @since 1.0.0 |
| 275 | * |
| 276 | * @return Processor |
| 277 | */ |
| 278 | public function get_processor() { |
| 279 | |
| 280 | static $processor; |
| 281 | |
| 282 | if ( ! isset( $processor ) ) { |
| 283 | $processor = apply_filters( 'wp_mail_smtp_core_get_processor', new Processor() ); |
| 284 | |
| 285 | if ( method_exists( $processor, 'hooks' ) ) { |
| 286 | $processor->hooks(); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | return $processor; |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * Load the plugin admin area. |
| 295 | * |
| 296 | * @since 1.0.0 |
| 297 | * |
| 298 | * @return Admin\Area |
| 299 | */ |
| 300 | public function get_admin() { |
| 301 | |
| 302 | static $admin; |
| 303 | |
| 304 | if ( ! isset( $admin ) ) { |
| 305 | $admin = apply_filters( 'wp_mail_smtp_core_get_admin', new Admin\Area() ); |
| 306 | } |
| 307 | |
| 308 | return $admin; |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * Load the plugin providers loader. |
| 313 | * |
| 314 | * @since 1.0.0 |
| 315 | * |
| 316 | * @return Providers\Loader |
| 317 | */ |
| 318 | public function get_providers() { |
| 319 | |
| 320 | static $providers; |
| 321 | |
| 322 | if ( ! isset( $providers ) ) { |
| 323 | $providers = apply_filters( 'wp_mail_smtp_core_get_providers', new Providers\Loader() ); |
| 324 | } |
| 325 | |
| 326 | return $providers; |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * Load the plugin option migrator. |
| 331 | * |
| 332 | * @deprecated 3.0.0 |
| 333 | * |
| 334 | * @since 1.0.0 |
| 335 | * |
| 336 | * @return Migration |
| 337 | */ |
| 338 | public function get_migration() { |
| 339 | |
| 340 | _deprecated_function( __METHOD__, '3.0.0' ); |
| 341 | |
| 342 | static $migration; |
| 343 | |
| 344 | if ( ! isset( $migration ) ) { |
| 345 | $migration = apply_filters( 'wp_mail_smtp_core_get_migration', new Migration() ); |
| 346 | } |
| 347 | |
| 348 | return $migration; |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * Initialize DB migrations. |
| 353 | * |
| 354 | * @since 3.0.0 |
| 355 | */ |
| 356 | public function init_migrations() { |
| 357 | |
| 358 | if ( WP::is_doing_ajax() || wp_doing_cron() ) { |
| 359 | return; |
| 360 | } |
| 361 | |
| 362 | $migrations = [ |
| 363 | Migration::class, |
| 364 | \WPMailSMTP\Admin\DebugEvents\Migration::class, |
| 365 | ]; |
| 366 | |
| 367 | /** |
| 368 | * Filters DB migrations. |
| 369 | * |
| 370 | * @since 3.0.0 |
| 371 | * |
| 372 | * @param array $migrations Migrations classes. |
| 373 | */ |
| 374 | $migrations = apply_filters( 'wp_mail_smtp_core_init_migrations', $migrations ); |
| 375 | |
| 376 | foreach ( $migrations as $migration ) { |
| 377 | if ( is_subclass_of( $migration, '\WPMailSMTP\MigrationAbstract' ) && $migration::is_enabled() ) { |
| 378 | $new_migration = new $migration(); |
| 379 | $new_migration->init(); |
| 380 | } |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | /** |
| 385 | * Load the plugin upgrader. |
| 386 | * |
| 387 | * @since 1.1.0 |
| 388 | * |
| 389 | * @return Upgrade |
| 390 | */ |
| 391 | public function get_upgrade() { |
| 392 | |
| 393 | static $upgrade; |
| 394 | |
| 395 | if ( ! isset( $upgrade ) ) { |
| 396 | $upgrade = apply_filters( 'wp_mail_smtp_core_get_upgrade', new Upgrade() ); |
| 397 | } |
| 398 | |
| 399 | return $upgrade; |
| 400 | } |
| 401 | |
| 402 | /** |
| 403 | * Get the plugin's WP Site Health object. |
| 404 | * |
| 405 | * @since 1.9.0 |
| 406 | * |
| 407 | * @return SiteHealth |
| 408 | */ |
| 409 | public function get_site_health() { |
| 410 | |
| 411 | static $site_health; |
| 412 | |
| 413 | if ( ! isset( $site_health ) ) { |
| 414 | $site_health = apply_filters( 'wp_mail_smtp_core_get_site_health', new SiteHealth() ); |
| 415 | } |
| 416 | |
| 417 | return $site_health; |
| 418 | } |
| 419 | |
| 420 | /** |
| 421 | * Display various notifications to a user |
| 422 | * |
| 423 | * @since 1.0.0 |
| 424 | */ |
| 425 | public function init_notifications() { |
| 426 | |
| 427 | // Old PHP version notification. |
| 428 | if ( |
| 429 | version_compare( phpversion(), '5.6', '<' ) && |
| 430 | is_super_admin() && |
| 431 | ( |
| 432 | isset( $GLOBALS['pagenow'] ) && |
| 433 | $GLOBALS['pagenow'] === 'index.php' |
| 434 | ) |
| 435 | ) { |
| 436 | WP::add_admin_notice( |
| 437 | sprintf( |
| 438 | wp_kses( /* translators: %1$s - WP Mail SMTP plugin name; %2$s - WPMailSMTP.com URL to a related doc. */ |
| 439 | __( 'Your site is running an outdated version of PHP that is no longer supported and may cause issues with %1$s. <a href="%2$s" target="_blank" rel="noopener noreferrer">Read more</a> for additional information.', 'wp-mail-smtp' ), |
| 440 | array( |
| 441 | 'a' => array( |
| 442 | 'href' => array(), |
| 443 | 'target' => array(), |
| 444 | 'rel' => array(), |
| 445 | ), |
| 446 | ) |
| 447 | ), |
| 448 | '<strong>WP Mail SMTP</strong>', |
| 449 | 'https://wpmailsmtp.com/docs/supported-php-versions-for-wp-mail-smtp/' |
| 450 | ) . |
| 451 | '<br><br><em>' . |
| 452 | wp_kses( |
| 453 | __( '<strong>Please Note:</strong> Support for PHP 5.5 will be discontinued in 2021. After this, if no further action is taken, WP Mail SMTP functionality will be disabled.', 'wp-mail-smtp' ), |
| 454 | array( |
| 455 | 'strong' => array(), |
| 456 | 'em' => array(), |
| 457 | ) |
| 458 | ) . |
| 459 | '</em>', |
| 460 | WP::ADMIN_NOTICE_ERROR, |
| 461 | false |
| 462 | ); |
| 463 | } |
| 464 | |
| 465 | // Awesome Motive Notifications. |
| 466 | if ( Options::init()->get( 'general', 'am_notifications_hidden' ) ) { |
| 467 | return; |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | /** |
| 472 | * Display all debug mail-delivery related notices. |
| 473 | * |
| 474 | * @since 1.3.0 |
| 475 | * @since 1.6.0 Added a filter that allows to hide debug errors. |
| 476 | */ |
| 477 | public static function display_general_notices() { |
| 478 | |
| 479 | if ( wp_mail_smtp()->is_blocked() ) { |
| 480 | ?> |
| 481 | |
| 482 | <div class="notice <?php echo esc_attr( WP::ADMIN_NOTICE_ERROR ); ?>"> |
| 483 | <p> |
| 484 | <?php |
| 485 | $notices[] = sprintf( |
| 486 | wp_kses( /* translators: %s - plugin name and its version. */ |
| 487 | __( '<strong>EMAILING DISABLED:</strong> The %s is currently blocking all emails from being sent.', 'wp-mail-smtp' ), |
| 488 | array( |
| 489 | 'strong' => true, |
| 490 | ) |
| 491 | ), |
| 492 | esc_html( 'WP Mail SMTP v' . WPMS_PLUGIN_VER ) |
| 493 | ); |
| 494 | |
| 495 | if ( Options::init()->is_const_defined( 'general', 'do_not_send' ) ) { |
| 496 | $notices[] = sprintf( |
| 497 | wp_kses( /* translators: %1$s - constant name; %2$s - constant value. */ |
| 498 | __( 'To send emails, change the value of the %1$s constant to %2$s.', 'wp-mail-smtp' ), |
| 499 | array( |
| 500 | 'code' => true, |
| 501 | ) |
| 502 | ), |
| 503 | '<code>WPMS_DO_NOT_SEND</code>', |
| 504 | '<code>false</code>' |
| 505 | ); |
| 506 | } else { |
| 507 | $notices[] = sprintf( |
| 508 | wp_kses( /* translators: %s - plugin Misc settings page URL. */ |
| 509 | __( 'To send emails, go to plugin <a href="%s">Misc settings</a> and disable the "Do Not Send" option.', 'wp-mail-smtp' ), |
| 510 | array( |
| 511 | 'a' => array( |
| 512 | 'href' => true, |
| 513 | ), |
| 514 | ) |
| 515 | ), |
| 516 | esc_url( add_query_arg( 'tab', 'misc', wp_mail_smtp()->get_admin()->get_admin_page_url() ) ) |
| 517 | ); |
| 518 | } |
| 519 | |
| 520 | echo implode( ' ', $notices ); |
| 521 | ?> |
| 522 | </p> |
| 523 | </div> |
| 524 | |
| 525 | <?php |
| 526 | return; |
| 527 | } |
| 528 | |
| 529 | if ( wp_mail_smtp()->get_admin()->is_error_delivery_notice_enabled() ) { |
| 530 | $screen = get_current_screen(); |
| 531 | |
| 532 | // Skip the error notice if not on plugin page. |
| 533 | if ( |
| 534 | is_object( $screen ) && |
| 535 | strpos( $screen->id, 'page_wp-mail-smtp' ) === false |
| 536 | ) { |
| 537 | return; |
| 538 | } |
| 539 | |
| 540 | $notice = apply_filters( |
| 541 | 'wp_mail_smtp_core_display_general_notices_email_delivery_error_notice', |
| 542 | Debug::get_last() |
| 543 | ); |
| 544 | |
| 545 | if ( ! empty( $notice ) ) { |
| 546 | ?> |
| 547 | |
| 548 | <div class="notice <?php echo esc_attr( WP::ADMIN_NOTICE_ERROR ); ?>"> |
| 549 | <p> |
| 550 | <?php |
| 551 | echo wp_kses( |
| 552 | __( '<strong>Heads up!</strong> The last email your site attempted to send was unsuccessful.', 'wp-mail-smtp' ), |
| 553 | [ |
| 554 | 'strong' => [], |
| 555 | ] |
| 556 | ); |
| 557 | ?> |
| 558 | </p> |
| 559 | |
| 560 | <blockquote> |
| 561 | <pre><?php echo $notice; ?></pre> |
| 562 | </blockquote> |
| 563 | |
| 564 | <p> |
| 565 | <?php |
| 566 | if ( ! wp_mail_smtp()->get_admin()->is_admin_page() ) { |
| 567 | printf( |
| 568 | wp_kses( /* translators: %s - plugin admin page URL. */ |
| 569 | __( 'Please review your WP Mail SMTP settings in <a href="%s">plugin admin area</a>.' ) . ' ', |
| 570 | array( |
| 571 | 'a' => array( |
| 572 | 'href' => array(), |
| 573 | ), |
| 574 | ) |
| 575 | ), |
| 576 | esc_url( wp_mail_smtp()->get_admin()->get_admin_page_url() ) |
| 577 | ); |
| 578 | } |
| 579 | |
| 580 | printf( |
| 581 | wp_kses( /* translators: %s - URL to the debug events page. */ |
| 582 | __( 'For more details please try running an Email Test or reading the latest <a href="%s">error event</a>.' ), |
| 583 | [ |
| 584 | 'a' => [ |
| 585 | 'href' => [], |
| 586 | ], |
| 587 | ] |
| 588 | ), |
| 589 | esc_url( DebugEvents::get_page_url() ) |
| 590 | ); |
| 591 | ?> |
| 592 | </p> |
| 593 | |
| 594 | <?php |
| 595 | echo wp_kses( |
| 596 | apply_filters( |
| 597 | 'wp_mail_smtp_core_display_general_notices_email_delivery_error_notice_footer', |
| 598 | '' |
| 599 | ), |
| 600 | [ |
| 601 | 'p' => [], |
| 602 | 'a' => [ |
| 603 | 'href' => [], |
| 604 | 'target' => [], |
| 605 | 'class' => [], |
| 606 | 'rel' => [], |
| 607 | ], |
| 608 | ] |
| 609 | ); |
| 610 | ?> |
| 611 | </div> |
| 612 | |
| 613 | <?php |
| 614 | } |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | /** |
| 619 | * Check whether we are working with a new plugin install. |
| 620 | * |
| 621 | * @since 1.3.0 |
| 622 | * |
| 623 | * @return bool |
| 624 | */ |
| 625 | protected function is_new_install() { |
| 626 | |
| 627 | /* |
| 628 | * No previously installed 0.*. |
| 629 | * 'wp_mail_smtp_initial_version' option appeared in 1.3.0. So we make sure it exists. |
| 630 | * No previous plugin upgrades. |
| 631 | */ |
| 632 | if ( |
| 633 | ! get_option( 'mailer', false ) && |
| 634 | get_option( 'wp_mail_smtp_initial_version', false ) && |
| 635 | version_compare( WPMS_PLUGIN_VER, get_option( 'wp_mail_smtp_initial_version' ), '=' ) |
| 636 | ) { |
| 637 | return true; |
| 638 | } |
| 639 | |
| 640 | return false; |
| 641 | } |
| 642 | |
| 643 | /** |
| 644 | * Detect if there are plugins activated that will cause a conflict. |
| 645 | * |
| 646 | * @since 1.3.0 |
| 647 | * @since 1.5.0 Moved the logic to Conflicts class. |
| 648 | */ |
| 649 | public function detect_conflicts() { |
| 650 | |
| 651 | // Display only for those who can actually deactivate plugins. |
| 652 | if ( ! current_user_can( 'manage_options' ) ) { |
| 653 | return; |
| 654 | } |
| 655 | |
| 656 | $conflicts = new Conflicts(); |
| 657 | |
| 658 | if ( $conflicts->is_detected() ) { |
| 659 | $conflicts->notify(); |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | /** |
| 664 | * Init the \PHPMailer replacement. |
| 665 | * |
| 666 | * @since 1.0.0 |
| 667 | * |
| 668 | * @return MailCatcherInterface |
| 669 | */ |
| 670 | public function replace_phpmailer() { |
| 671 | |
| 672 | global $phpmailer; |
| 673 | |
| 674 | return $this->replace_w_fake_phpmailer( $phpmailer ); |
| 675 | } |
| 676 | |
| 677 | /** |
| 678 | * Overwrite default PhpMailer with our MailCatcher. |
| 679 | * |
| 680 | * @since 1.0.0 |
| 681 | * @since 1.5.0 Throw external PhpMailer exceptions, inherits default WP behavior. |
| 682 | * |
| 683 | * @param null $obj PhpMailer object to override with own implementation. |
| 684 | * |
| 685 | * @return MailCatcherInterface |
| 686 | */ |
| 687 | protected function replace_w_fake_phpmailer( &$obj = null ) { |
| 688 | |
| 689 | $obj = $this->generate_mail_catcher( true ); |
| 690 | |
| 691 | return $obj; |
| 692 | } |
| 693 | |
| 694 | /** |
| 695 | * What to do on plugin activation. |
| 696 | * |
| 697 | * @since 1.0.0 |
| 698 | * @since 2.0.0 Changed from general `plugin_activate` hook to this plugin specific activation hook. |
| 699 | */ |
| 700 | public function activate() { |
| 701 | |
| 702 | // Store the plugin version when initial install occurred. |
| 703 | add_option( 'wp_mail_smtp_initial_version', WPMS_PLUGIN_VER, '', false ); |
| 704 | |
| 705 | // Store the plugin version activated to reference with upgrades. |
| 706 | update_option( 'wp_mail_smtp_version', WPMS_PLUGIN_VER, false ); |
| 707 | |
| 708 | // Save default options, only once. |
| 709 | Options::init()->set( Options::get_defaults(), true ); |
| 710 | |
| 711 | /** |
| 712 | * Store the timestamp of first plugin activation. |
| 713 | * |
| 714 | * @since 2.1.0 |
| 715 | */ |
| 716 | add_option( 'wp_mail_smtp_activated_time', time(), '', false ); |
| 717 | |
| 718 | /** |
| 719 | * Store the timestamp of the first plugin activation by license type. |
| 720 | * |
| 721 | * @since 2.3.0 |
| 722 | */ |
| 723 | $license_type = is_readable( $this->plugin_path . '/src/Pro/Pro.php' ) ? 'pro' : 'lite'; |
| 724 | $activated = get_option( 'wp_mail_smtp_activated', [] ); |
| 725 | |
| 726 | if ( empty( $activated[ $license_type ] ) ) { |
| 727 | $activated[ $license_type ] = time(); |
| 728 | update_option( 'wp_mail_smtp_activated', $activated ); |
| 729 | } |
| 730 | |
| 731 | // Add transient to trigger redirect to the Setup Wizard. |
| 732 | set_transient( 'wp_mail_smtp_activation_redirect', true, 30 ); |
| 733 | } |
| 734 | |
| 735 | /** |
| 736 | * Whether this is a Pro version of a plugin. |
| 737 | * |
| 738 | * @since 1.5.0 |
| 739 | * |
| 740 | * @return bool |
| 741 | */ |
| 742 | public function is_pro() { |
| 743 | |
| 744 | return apply_filters( 'wp_mail_smtp_core_is_pro', ! empty( $this->pro ) ); |
| 745 | } |
| 746 | |
| 747 | /** |
| 748 | * Get the current license type. |
| 749 | * |
| 750 | * @since 1.5.0 |
| 751 | * |
| 752 | * @return string Default value: lite. |
| 753 | */ |
| 754 | public function get_license_type() { |
| 755 | |
| 756 | $type = Options::init()->get( 'license', 'type' ); |
| 757 | |
| 758 | if ( empty( $type ) ) { |
| 759 | $type = 'lite'; |
| 760 | } |
| 761 | |
| 762 | return strtolower( $type ); |
| 763 | } |
| 764 | |
| 765 | /** |
| 766 | * Get the current license key. |
| 767 | * |
| 768 | * @since 1.5.0 |
| 769 | * |
| 770 | * @return string |
| 771 | */ |
| 772 | public function get_license_key() { |
| 773 | |
| 774 | $key = Options::init()->get( 'license', 'key' ); |
| 775 | |
| 776 | if ( empty( $key ) ) { |
| 777 | $key = ''; |
| 778 | } |
| 779 | |
| 780 | return $key; |
| 781 | } |
| 782 | |
| 783 | /** |
| 784 | * Upgrade link used within the various admin pages. |
| 785 | * |
| 786 | * @since 1.5.0 |
| 787 | * @since 1.5.1 Support all UTM params. |
| 788 | * |
| 789 | * @param array|string $utm Array of UTM params, or if string provided - utm_content URL parameter. |
| 790 | * |
| 791 | * @return string |
| 792 | */ |
| 793 | public function get_upgrade_link( $utm ) { |
| 794 | |
| 795 | // Defaults. |
| 796 | $source = 'WordPress'; |
| 797 | $medium = 'plugin-settings'; |
| 798 | $campaign = 'liteplugin'; |
| 799 | $content = 'general'; |
| 800 | |
| 801 | if ( is_array( $utm ) ) { |
| 802 | if ( isset( $utm['source'] ) ) { |
| 803 | $source = $utm['source']; |
| 804 | } |
| 805 | if ( isset( $utm['medium'] ) ) { |
| 806 | $medium = $utm['medium']; |
| 807 | } |
| 808 | if ( isset( $utm['campaign'] ) ) { |
| 809 | $campaign = $utm['campaign']; |
| 810 | } |
| 811 | if ( isset( $utm['content'] ) ) { |
| 812 | $content = $utm['content']; |
| 813 | } |
| 814 | } elseif ( is_string( $utm ) ) { |
| 815 | $content = $utm; |
| 816 | } |
| 817 | |
| 818 | $url = 'https://wpmailsmtp.com/lite-upgrade/?utm_source=' . esc_attr( rawurlencode( $source ) ) . '&utm_medium=' . esc_attr( rawurlencode( $medium ) ) . '&utm_campaign=' . esc_attr( rawurlencode( $campaign ) ); |
| 819 | |
| 820 | if ( ! empty( $content ) ) { |
| 821 | $url .= '&utm_content=' . esc_attr( rawurlencode( $content ) ); |
| 822 | } |
| 823 | |
| 824 | return apply_filters( 'wp_mail_smtp_core_get_upgrade_link', $url ); |
| 825 | } |
| 826 | |
| 827 | /** |
| 828 | * Whether the emailing functionality is blocked, with either an option or a constatnt. |
| 829 | * |
| 830 | * @since 1.7.0 |
| 831 | * |
| 832 | * @return bool |
| 833 | */ |
| 834 | public function is_blocked() { |
| 835 | |
| 836 | return (bool) Options::init()->get( 'general', 'do_not_send' ); |
| 837 | } |
| 838 | |
| 839 | /** |
| 840 | * Whether the white-labeling is enabled. |
| 841 | * White-labeling disables the plugin "About us" page, it replaces any plugin marketing texts or images with |
| 842 | * white label ones. |
| 843 | * |
| 844 | * @since 2.0.0 |
| 845 | * |
| 846 | * @return bool |
| 847 | */ |
| 848 | public function is_white_labeled() { |
| 849 | |
| 850 | return (bool) apply_filters( 'wp_mail_smtp_is_white_labeled', false ); |
| 851 | } |
| 852 | |
| 853 | /** |
| 854 | * Require the action scheduler in an early plugins_loaded hook (-10). |
| 855 | * |
| 856 | * @see https://actionscheduler.org/usage/#load-order |
| 857 | * |
| 858 | * @since 2.1.0 |
| 859 | */ |
| 860 | public function load_action_scheduler() { |
| 861 | |
| 862 | require_once $this->plugin_path . '/vendor/woocommerce/action-scheduler/action-scheduler.php'; |
| 863 | } |
| 864 | |
| 865 | /** |
| 866 | * Get the list of all custom DB tables that should be present in the DB. |
| 867 | * |
| 868 | * @since 2.1.2 |
| 869 | * |
| 870 | * @return array List of table names. |
| 871 | */ |
| 872 | public function get_custom_db_tables() { |
| 873 | |
| 874 | $tables = [ |
| 875 | Meta::get_table_name(), |
| 876 | DebugEvents::get_table_name(), |
| 877 | ]; |
| 878 | |
| 879 | return apply_filters( 'wp_mail_smtp_core_get_custom_db_tables', $tables ); |
| 880 | } |
| 881 | |
| 882 | /** |
| 883 | * Generate the correct MailCatcher object based on the PHPMailer version used in WP. |
| 884 | * |
| 885 | * Also conditionally require the needed class files. |
| 886 | * |
| 887 | * @see https://make.wordpress.org/core/2020/07/01/external-library-updates-in-wordpress-5-5-call-for-testing/ |
| 888 | * |
| 889 | * @since 2.2.0 |
| 890 | * |
| 891 | * @param bool $exceptions True if external exceptions should be thrown. |
| 892 | * |
| 893 | * @return MailCatcherInterface |
| 894 | */ |
| 895 | public function generate_mail_catcher( $exceptions = null ) { |
| 896 | |
| 897 | if ( version_compare( get_bloginfo( 'version' ), '5.5-alpha', '<' ) ) { |
| 898 | if ( ! class_exists( '\PHPMailer', false ) ) { |
| 899 | require_once ABSPATH . WPINC . '/class-phpmailer.php'; |
| 900 | } |
| 901 | |
| 902 | $mail_catcher = new MailCatcher( $exceptions ); |
| 903 | } else { |
| 904 | if ( ! class_exists( '\PHPMailer\PHPMailer\PHPMailer', false ) ) { |
| 905 | require_once ABSPATH . WPINC . '/PHPMailer/PHPMailer.php'; |
| 906 | } |
| 907 | |
| 908 | if ( ! class_exists( '\PHPMailer\PHPMailer\Exception', false ) ) { |
| 909 | require_once ABSPATH . WPINC . '/PHPMailer/Exception.php'; |
| 910 | } |
| 911 | |
| 912 | if ( ! class_exists( '\PHPMailer\PHPMailer\SMTP', false ) ) { |
| 913 | require_once ABSPATH . WPINC . '/PHPMailer/SMTP.php'; |
| 914 | } |
| 915 | |
| 916 | $mail_catcher = new MailCatcherV6( $exceptions ); |
| 917 | } |
| 918 | |
| 919 | return $mail_catcher; |
| 920 | } |
| 921 | |
| 922 | /** |
| 923 | * Check if the passed object is a valid PHPMailer object. |
| 924 | * |
| 925 | * @since 2.2.0 |
| 926 | * |
| 927 | * @param object $phpmailer A potential PHPMailer object to be tested. |
| 928 | * |
| 929 | * @return bool |
| 930 | */ |
| 931 | public function is_valid_phpmailer( $phpmailer ) { |
| 932 | |
| 933 | return $phpmailer instanceof MailCatcherInterface || |
| 934 | $phpmailer instanceof \PHPMailer || |
| 935 | $phpmailer instanceof \PHPMailer\PHPMailer\PHPMailer; |
| 936 | } |
| 937 | |
| 938 | /** |
| 939 | * Force the `mail.from_email_force` plugin option to always return true if the current saved mailer is Gmail. |
| 940 | * Alters the plugin options retrieving via the Options::get method. |
| 941 | * |
| 942 | * The gmail mailer check is performed when this filter is added. |
| 943 | * |
| 944 | * @deprecated 2.7.0 |
| 945 | * |
| 946 | * @since 2.2.0 |
| 947 | * |
| 948 | * @param mixed $value The value of the plugin option that is being retrieved via Options::get method. |
| 949 | * @param string $group The group of the plugin option that is being retrieved via Options::get method. |
| 950 | * @param string $key The key of the plugin option that is being retrieved via Options::get method. |
| 951 | * |
| 952 | * @return mixed |
| 953 | */ |
| 954 | public function gmail_mailer_get_from_email_force( $value, $group, $key ) { |
| 955 | |
| 956 | _deprecated_function( __METHOD__, '2.7.0' ); |
| 957 | |
| 958 | if ( $group === 'mail' && $key === 'from_email_force' ) { |
| 959 | $value = true; |
| 960 | } |
| 961 | |
| 962 | return $value; |
| 963 | } |
| 964 | |
| 965 | /** |
| 966 | * Load the plugin admin bar menu and initialize it. |
| 967 | * |
| 968 | * @since 2.3.0 |
| 969 | * |
| 970 | * @return AdminBarMenu |
| 971 | */ |
| 972 | public function get_admin_bar_menu() { |
| 973 | |
| 974 | static $admin_bar_menu; |
| 975 | |
| 976 | if ( ! isset( $admin_bar_menu ) ) { |
| 977 | $admin_bar_menu = apply_filters( |
| 978 | 'wp_mail_smtp_core_get_admin_bar_menu', |
| 979 | new AdminBarMenu() |
| 980 | ); |
| 981 | |
| 982 | if ( method_exists( $admin_bar_menu, 'init' ) ) { |
| 983 | $admin_bar_menu->init(); |
| 984 | } |
| 985 | } |
| 986 | |
| 987 | return $admin_bar_menu; |
| 988 | } |
| 989 | |
| 990 | /** |
| 991 | * Load the plugin usage tracking. |
| 992 | * |
| 993 | * @since 2.3.0 |
| 994 | * |
| 995 | * @return UsageTracking |
| 996 | */ |
| 997 | public function get_usage_tracking() { |
| 998 | |
| 999 | static $usage_tracking; |
| 1000 | |
| 1001 | if ( ! isset( $usage_tracking ) ) { |
| 1002 | $usage_tracking = apply_filters( 'wp_mail_smtp_core_get_usage_tracking', new UsageTracking() ); |
| 1003 | |
| 1004 | if ( method_exists( $usage_tracking, 'load' ) ) { |
| 1005 | add_action( 'after_setup_theme', [ $usage_tracking, 'load' ] ); |
| 1006 | } |
| 1007 | } |
| 1008 | |
| 1009 | return $usage_tracking; |
| 1010 | } |
| 1011 | |
| 1012 | /** |
| 1013 | * Load the plugin admin notifications functionality and initializes it. |
| 1014 | * |
| 1015 | * @since 2.3.0 |
| 1016 | * |
| 1017 | * @return Notifications |
| 1018 | */ |
| 1019 | public function get_notifications() { |
| 1020 | |
| 1021 | static $notifications; |
| 1022 | |
| 1023 | if ( ! isset( $notifications ) ) { |
| 1024 | $notifications = apply_filters( |
| 1025 | 'wp_mail_smtp_core_get_notifications', |
| 1026 | new Notifications() |
| 1027 | ); |
| 1028 | |
| 1029 | if ( method_exists( $notifications, 'init' ) ) { |
| 1030 | $notifications->init(); |
| 1031 | } |
| 1032 | } |
| 1033 | |
| 1034 | return $notifications; |
| 1035 | } |
| 1036 | |
| 1037 | /** |
| 1038 | * Prepare the HTML output for a plugin loader/spinner. |
| 1039 | * |
| 1040 | * @since 2.4.0 |
| 1041 | * |
| 1042 | * @param string $color The color of the loader ('', 'blue' or 'white'), where '' is default orange. |
| 1043 | * @param string $size The size of the loader ('lg', 'md', 'sm'). |
| 1044 | * |
| 1045 | * @return string |
| 1046 | */ |
| 1047 | public function prepare_loader( $color = '', $size = 'md' ) { |
| 1048 | |
| 1049 | $svg_name = 'loading'; |
| 1050 | |
| 1051 | if ( in_array( $color, [ 'blue', 'white' ], true ) ) { |
| 1052 | $svg_name .= '-' . $color; |
| 1053 | } |
| 1054 | |
| 1055 | if ( ! in_array( $size, [ 'lg', 'md', 'sm' ], true ) ) { |
| 1056 | $size = 'md'; |
| 1057 | } |
| 1058 | |
| 1059 | return '<img src="' . esc_url( $this->plugin_url . '/assets/images/loaders/' . $svg_name . '.svg' ) . '" alt="' . esc_attr__( 'Loading', 'wp-mail-smtp' ) . '" class="wp-mail-smtp-loading wp-mail-smtp-loading-' . $size . '">'; |
| 1060 | } |
| 1061 | |
| 1062 | /** |
| 1063 | * Initialize the Connect functionality. |
| 1064 | * This has to execute after pro was loaded, since we need check for plugin license type (if pro or not). |
| 1065 | * That's why it's hooked to the same WP hook (`plugins_loaded`) as `get_pro` with lower priority. |
| 1066 | * |
| 1067 | * @since 2.6.0 |
| 1068 | */ |
| 1069 | public function get_connect() { |
| 1070 | |
| 1071 | static $connect; |
| 1072 | |
| 1073 | if ( ! isset( $connect ) && ! $this->is_pro() ) { |
| 1074 | $connect = apply_filters( 'wp_mail_smtp_core_get_connect', new Connect() ); |
| 1075 | |
| 1076 | if ( method_exists( $connect, 'hooks' ) ) { |
| 1077 | $connect->hooks(); |
| 1078 | } |
| 1079 | } |
| 1080 | |
| 1081 | return $connect; |
| 1082 | } |
| 1083 | |
| 1084 | /** |
| 1085 | * Load the plugin compatibility functionality and initializes it. |
| 1086 | * |
| 1087 | * @since 2.8.0 |
| 1088 | * |
| 1089 | * @return Compatibility |
| 1090 | */ |
| 1091 | public function get_compatibility() { |
| 1092 | |
| 1093 | static $compatibility; |
| 1094 | |
| 1095 | if ( ! isset( $compatibility ) ) { |
| 1096 | |
| 1097 | /** |
| 1098 | * Filters compatibility instance. |
| 1099 | * |
| 1100 | * @since 2.8.0 |
| 1101 | * |
| 1102 | * @param \WPMailSMTP\Compatibility\Compatibility $compatibility Compatibility instance. |
| 1103 | */ |
| 1104 | $compatibility = apply_filters( 'wp_mail_smtp_core_get_compatibility', new Compatibility() ); |
| 1105 | |
| 1106 | if ( method_exists( $compatibility, 'init' ) ) { |
| 1107 | $compatibility->init(); |
| 1108 | } |
| 1109 | } |
| 1110 | |
| 1111 | return $compatibility; |
| 1112 | } |
| 1113 | |
| 1114 | /** |
| 1115 | * Get the Dashboard Widget object (lite or pro version). |
| 1116 | * |
| 1117 | * @since 2.9.0 |
| 1118 | * |
| 1119 | * @return DashboardWidget |
| 1120 | */ |
| 1121 | public function get_dashboard_widget() { |
| 1122 | |
| 1123 | static $dashboard_widget; |
| 1124 | |
| 1125 | if ( ! isset( $dashboard_widget ) ) { |
| 1126 | |
| 1127 | /** |
| 1128 | * Filter the dashboard widget class name. |
| 1129 | * |
| 1130 | * @since 2.9.0 |
| 1131 | * |
| 1132 | * @param DashboardWidget $class_name The dashboard widget class name to be instantiated. |
| 1133 | */ |
| 1134 | $class_name = apply_filters( 'wp_mail_smtp_core_get_dashboard_widget', DashboardWidget::class ); |
| 1135 | $dashboard_widget = new $class_name(); |
| 1136 | } |
| 1137 | |
| 1138 | return $dashboard_widget; |
| 1139 | } |
| 1140 | |
| 1141 | /** |
| 1142 | * Get the reports object (lite or pro version). |
| 1143 | * |
| 1144 | * @since 3.0.0 |
| 1145 | * |
| 1146 | * @return Reports |
| 1147 | */ |
| 1148 | public function get_reports() { |
| 1149 | |
| 1150 | static $reports; |
| 1151 | |
| 1152 | if ( ! isset( $reports ) ) { |
| 1153 | |
| 1154 | /** |
| 1155 | * Filter the reports class name. |
| 1156 | * |
| 1157 | * @since 3.0.0 |
| 1158 | * |
| 1159 | * @param Reports $class_name The reports class name to be instantiated. |
| 1160 | */ |
| 1161 | $class_name = apply_filters( 'wp_mail_smtp_core_get_reports', Reports::class ); |
| 1162 | $reports = new $class_name(); |
| 1163 | |
| 1164 | if ( method_exists( $reports, 'init' ) ) { |
| 1165 | $reports->init(); |
| 1166 | } |
| 1167 | } |
| 1168 | |
| 1169 | return $reports; |
| 1170 | } |
| 1171 | } |
| 1172 |