Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
72.73% covered (warning)
72.73%
24 / 33
75.00% covered (warning)
75.00%
6 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
BH_WP_AWS_SES_Bounce_Handler
72.73% covered (warning)
72.73%
24 / 33
75.00% covered (warning)
75.00%
6 / 8
9.30
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
1
 set_locale
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 define_admin_settings_page_hooks
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 define_admin_ajax_hooks
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 define_admin_plugins_page_hooks
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 define_rest_hooks
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 define_wp_mail_hooks
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 define_logger_hooks
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * The file that defines the core plugin class
4 *
5 * A class definition that includes attributes and functions used across both the
6 * sns-facing side of the site and the admin area.
7 *
8 * @link       https://BrianHenry.ie
9 * @since      1.0.0
10 *
11 * @package brianhenryie/bh-wp-aws-ses-bounce-handler
12 */
13
14namespace BrianHenryIE\AWS_SES_Bounce_Handler;
15
16use BrianHenryIE\AWS_SES_Bounce_Handler\Admin\Admin_Assets;
17use BrianHenryIE\AWS_SES_Bounce_Handler\Admin\Ajax;
18use BrianHenryIE\AWS_SES_Bounce_Handler\Admin\Plugins_Page;
19use BrianHenryIE\AWS_SES_Bounce_Handler\Admin\Settings_Page;
20use BrianHenryIE\AWS_SES_Bounce_Handler\Logger\TNP_User_Hyperlink;
21use BrianHenryIE\AWS_SES_Bounce_Handler\WP_Includes\I18n;
22use BrianHenryIE\AWS_SES_Bounce_Handler\WP_Includes\REST;
23use BrianHenryIE\AWS_SES_Bounce_Handler\WP_Includes\WP_Mail;
24use Psr\Log\LoggerAwareTrait;
25use Psr\Log\LoggerInterface;
26
27
28/**
29 * The core plugin class.
30 *
31 * This is used to define internationalization, admin-specific hooks, and
32 * sns-facing site hooks.
33 *
34 * Also maintains the unique identifier of this plugin as well as the current
35 * version of the plugin.
36 */
37class BH_WP_AWS_SES_Bounce_Handler {
38
39    /**
40     * A PSR logger for the plugin's classes to use.
41     *
42     * @var LoggerInterface
43     */
44    protected LoggerInterface $logger;
45
46    /**
47     * The plugin settings.
48     *
49     * @var Settings_Interface
50     */
51    protected Settings_Interface $settings;
52
53    /**
54     * The main plugin functions which may be accessed via REST/AJAX/cron/CLI/etc.
55     *
56     * @var API_Interface
57     */
58    protected API_Interface $api;
59
60    /**
61     * Define the core functionality of the plugin.
62     *
63     * Set the plugin name and the plugin version that can be used throughout the plugin.
64     * Load the dependencies, define the locale, and set the hooks for the admin area and
65     * the sns-facing side of the site.
66     *
67     * @since    1.1.0
68     *
69     * @param API_Interface      $api Main plugin functions.
70     * @param Settings_Interface $settings The settings the plugin should be run with.
71     * @param LoggerInterface    $logger A PSR logger.
72     */
73    public function __construct( API_Interface $api, Settings_Interface $settings, LoggerInterface $logger ) {
74
75        $this->logger   = $logger;
76        $this->settings = $settings;
77        $this->api      = $api;
78
79        $this->set_locale();
80
81        $this->define_admin_settings_page_hooks();
82        $this->define_admin_ajax_hooks();
83        $this->define_admin_plugins_page_hooks();
84        $this->define_rest_hooks();
85        $this->define_wp_mail_hooks();
86        $this->define_logger_hooks();
87    }
88
89    /**
90     * Define the locale for this plugin for internationalization.
91     *
92     * Uses the I18n class in order to set the domain and to register the hook
93     * with WordPress.
94     *
95     * @since    1.0.0
96     */
97    protected function set_locale(): void {
98
99        $plugin_i18n = new I18n();
100
101        add_action( 'plugins_loaded', array( $plugin_i18n, 'load_plugin_textdomain' ) );
102    }
103
104    /**
105     * Register the hooks related to displaying the admin settings page.
106     */
107    protected function define_admin_settings_page_hooks(): void {
108
109        $settings_page = new Settings_Page( $this->api, $this->settings );
110        add_action( 'admin_menu', array( $settings_page, 'add_settings_page' ) );
111
112        $admin = new Admin_Assets( $this->api, $this->settings );
113        add_action( 'admin_enqueue_scripts', array( $admin, 'enqueue_styles' ) );
114        add_action( 'admin_enqueue_scripts', array( $admin, 'enqueue_scripts' ) );
115    }
116
117    /**
118     * Register the hooks related to handling AJAX functionality of the plugin.
119     */
120    protected function define_admin_ajax_hooks(): void {
121
122        $ajax = new Ajax( $this->api, $this->logger );
123
124        add_action( 'wp_ajax_run_ses_bounce_test', array( $ajax, 'run_ses_bounce_test' ) );
125        add_action( 'wp_ajax_fetch_test_results', array( $ajax, 'fetch_test_results' ) );
126        add_action( 'wp_ajax_delete_test_data', array( $ajax, 'delete_test_data' ) );
127
128        add_action( 'wp_ajax_bh_wp_aws_ses_bounce_handler_set_log_level', array( $ajax, 'set_log_level' ) );
129    }
130
131    /**
132     * Register the hooks related plugins.php.
133     */
134    protected function define_admin_plugins_page_hooks(): void {
135
136        $plugins_page    = new Plugins_Page( $this->settings );
137        $plugin_basename = $this->settings->get_plugin_basename();
138        add_filter( 'plugin_action_links_' . $plugin_basename, array( $plugins_page, 'action_links' ), 10, 4 );
139        add_filter( 'plugin_row_meta', array( $plugins_page, 'row_meta' ), 20, 4 );
140    }
141
142    /**
143     * Register the hooks related to the sns-facing functionality of the plugin.
144     *
145     * @since    1.0.0
146     */
147    protected function define_rest_hooks(): void {
148
149        $sns = new REST( $this->api, $this->settings, $this->logger );
150        add_action( 'rest_api_init', array( $sns, 'add_bh_aws_ses_rest_endpoint' ) );
151    }
152
153    /**
154     * Hook into wp_mail to filter bounced email addresses from outgoing mail.
155     */
156    protected function define_wp_mail_hooks(): void {
157
158        $wp_mail = new WP_Mail( $this->logger );
159
160        add_filter( 'wp_mail', array( $wp_mail, 'remove_bounced_destination_email_addresses' ) );
161        add_filter( 'pre_wp_mail', array( $wp_mail, 'cancel_sending_email_when_all_addresses_removed' ), 10, 2 );
162    }
163
164    /**
165     * Add hooks to modify the recording/output of the logs.
166     */
167    protected function define_logger_hooks(): void {
168
169        $tnp_user_hyperlink = new TNP_User_Hyperlink();
170        add_filter( "{$this->settings->get_plugin_slug()}_bh_wp_logger_column", array( $tnp_user_hyperlink, 'replace_tnp_user_id_with_link' ), 10, 5 );
171    }
172}