get( 'general', 'dashboard_widget_hidden' ) ) { return; } add_action( 'admin_init', function() { // This widget should be displayed for certain high-level users only. if ( ! current_user_can( wp_mail_smtp()->get_capability_manage_options() ) ) { return; } /** * Filters whether the initialization of the dashboard widget should be allowed. * * @since 2.9.0 * * @param bool $var If the dashboard widget should be initialized. */ if ( ! apply_filters( 'wp_mail_smtp_admin_dashboard_widget', '__return_true' ) ) { return; } $this->hooks(); } ); } /** * Widget hooks. * * @since 2.9.0 */ public function hooks() { add_action( 'admin_enqueue_scripts', [ $this, 'widget_scripts' ] ); add_action( 'wp_dashboard_setup', [ $this, 'widget_register' ] ); add_action( 'wp_ajax_wp_mail_smtp_' . static::SLUG . '_save_widget_meta', [ $this, 'save_widget_meta_ajax' ] ); add_action( 'wp_ajax_wp_mail_smtp_' . static::SLUG . '_enable_summary_report_email', [ $this, 'enable_summary_report_email_ajax', ] ); } /** * Load widget-specific scripts. * Load them only on the admin dashboard page. * * @since 2.9.0 */ public function widget_scripts() { $screen = get_current_screen(); if ( ! isset( $screen->id ) || 'dashboard' !== $screen->id ) { return; } $min = WP::asset_min(); wp_enqueue_style( 'wp-mail-smtp-dashboard-widget', wp_mail_smtp()->assets_url . '/css/dashboard-widget.min.css', [], WPMS_PLUGIN_VER ); wp_enqueue_script( 'wp-mail-smtp-chart', wp_mail_smtp()->assets_url . '/js/vendor/chart.min.js', [ 'moment' ], '2.9.4.1', true ); wp_enqueue_script( 'wp-mail-smtp-dashboard-widget', wp_mail_smtp()->assets_url . "/js/smtp-dashboard-widget{$min}.js", [ 'jquery', 'wp-mail-smtp-chart' ], WPMS_PLUGIN_VER, true ); wp_localize_script( 'wp-mail-smtp-dashboard-widget', 'wp_mail_smtp_dashboard_widget', [ 'slug' => static::SLUG, 'nonce' => wp_create_nonce( 'wp_mail_smtp_' . static::SLUG . '_nonce' ), ] ); } /** * Register the widget. * * @since 2.9.0 */ public function widget_register() { global $wp_meta_boxes; $widget_key = 'wp_mail_smtp_reports_widget_lite'; wp_add_dashboard_widget( $widget_key, esc_html__( 'WP Mail SMTP', 'wp-mail-smtp' ), [ $this, 'widget_content' ] ); // Attempt to place the widget at the top. $normal_dashboard = $wp_meta_boxes['dashboard']['normal']['core']; $widget_instance = [ $widget_key => $normal_dashboard[ $widget_key ] ]; unset( $normal_dashboard[ $widget_key ] ); $sorted_dashboard = array_merge( $widget_instance, $normal_dashboard ); //phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited $wp_meta_boxes['dashboard']['normal']['core'] = $sorted_dashboard; } /** * Save a widget meta for a current user using AJAX. * * @since 2.9.0 */ public function save_widget_meta_ajax() { check_admin_referer( 'wp_mail_smtp_' . static::SLUG . '_nonce' ); if ( ! current_user_can( wp_mail_smtp()->get_capability_manage_options() ) ) { wp_send_json_error(); } $meta = ! empty( $_POST['meta'] ) ? sanitize_key( $_POST['meta'] ) : ''; $value = ! empty( $_POST['value'] ) ? sanitize_key( $_POST['value'] ) : 0; $this->widget_meta( 'set', $meta, $value ); wp_send_json_success(); } /** * Enable summary report email using AJAX. * * @since 3.0.0 */ public function enable_summary_report_email_ajax() { check_admin_referer( 'wp_mail_smtp_' . static::SLUG . '_nonce' ); if ( ! current_user_can( wp_mail_smtp()->get_capability_manage_options() ) ) { wp_send_json_error(); } $options = Options::init(); $data = [ 'general' => [ SummaryReportEmail::SETTINGS_SLUG => false, ], ]; $options->set( $data, false, false ); wp_send_json_success(); } /** * Load widget content. * * @since 2.9.0 */ public function widget_content() { echo '
'; } /** * Increment the number of total emails sent by 1. * * @deprecated 3.0.0 * * @since 2.9.0 */ public function increment_sent_email_counter() { _deprecated_function( __METHOD__, '3.0.0' ); } /** * Widget content HTML. * * @since 2.9.0 */ private function widget_content_html() { $hide_graph = (bool) $this->widget_meta( 'get', 'hide_graph' ); ?> display_after_email_stats_block_content(); } /** * Display the content after the email stats block. * * @since 3.9.0 * * @return void */ private function display_after_email_stats_block_content() { if ( empty( $this->widget_meta( 'get', 'hide_email_alerts_banner' ) ) ) { // Check if we have error debug events. $error_debug_events_count = DebugEvents::get_error_debug_events_count(); if ( ! is_wp_error( $error_debug_events_count ) && ! empty( $error_debug_events_count ) ) { $this->show_email_alerts_banner( $error_debug_events_count ); return; } } $hide_summary_report_email_block = (bool) $this->widget_meta( 'get', 'hide_summary_report_email_block' ); if ( SummaryReportEmail::is_disabled() && ! $hide_summary_report_email_block ) { $this->show_summary_report_email_block(); } $this->show_upgrade_footer(); } /** * Display the email alerts banner. * * @since 3.9.0 * * @param int $error_count The number of debug events error. * * @return void */ private function show_email_alerts_banner( $error_count ) { ?> widget_meta( 'get', 'hide_graph' ); ?> esc_html__( 'Confirmed Emails', 'wp-mail-smtp' ), 'sent' => esc_html__( 'Unconfirmed Emails', 'wp-mail-smtp' ), 'unsent' => esc_html__( 'Failed Emails', 'wp-mail-smtp' ), ]; if ( Helpers::mailer_without_send_confirmation() ) { unset( $options['sent'] ); $options['delivered'] = esc_html__( 'Sent Emails', 'wp-mail-smtp' ); } ?> get_email_stats_data(); ?>