DebugEvents
6 days ago
EmailSendingErrors
6 days ago
Pages
6 days ago
Recommendations
6 days ago
AdminBarMenu.php
6 days ago
Area.php
6 days ago
ConnectionSettings.php
6 days ago
DashboardWidget.php
6 days ago
DomainChecker.php
6 days ago
Education.php
6 days ago
FlyoutMenu.php
6 days ago
Notifications.php
6 days ago
PageAbstract.php
6 days ago
PageInterface.php
6 days ago
ParentPageAbstract.php
6 days ago
PluginsInstallSkin.php
6 days ago
Review.php
6 days ago
SetupWizard.php
6 days ago
WooCommerceActiveLayerEducation.php
6 days ago
AdminBarMenu.php
197 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPMailSMTP\Admin; |
| 4 | |
| 5 | use WPMailSMTP\EmailSendingDebug; |
| 6 | use WPMailSMTP\Options; |
| 7 | |
| 8 | /** |
| 9 | * WP Mail SMTP admin bar menu. |
| 10 | * |
| 11 | * @since 2.3.0 |
| 12 | */ |
| 13 | class AdminBarMenu { |
| 14 | |
| 15 | /** |
| 16 | * Initialize class. |
| 17 | * |
| 18 | * @since 2.3.0 |
| 19 | */ |
| 20 | public function init() { |
| 21 | |
| 22 | $this->hooks(); |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * Register hooks. |
| 27 | * |
| 28 | * @since 2.3.0 |
| 29 | */ |
| 30 | public function hooks() { |
| 31 | |
| 32 | add_action( 'wp_enqueue_scripts', [ $this, 'enqueues' ] ); |
| 33 | add_action( 'admin_enqueue_scripts', [ $this, 'enqueues' ] ); |
| 34 | add_action( 'admin_bar_menu', [ $this, 'register' ], 999 ); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Check if current user has access to see admin bar menu. |
| 39 | * |
| 40 | * @since 2.3.0 |
| 41 | * |
| 42 | * @return bool |
| 43 | */ |
| 44 | public function has_access() { |
| 45 | |
| 46 | $access = false; |
| 47 | |
| 48 | if ( |
| 49 | is_user_logged_in() && |
| 50 | current_user_can( wp_mail_smtp()->get_capability_manage_options() ) |
| 51 | ) { |
| 52 | $access = true; |
| 53 | } |
| 54 | |
| 55 | return apply_filters( 'wp_mail_smtp_admin_adminbarmenu_has_access', $access ); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Check if new notifications are available. |
| 60 | * |
| 61 | * @since 2.3.0 |
| 62 | * |
| 63 | * @return bool |
| 64 | */ |
| 65 | public function has_notifications() { |
| 66 | |
| 67 | return wp_mail_smtp()->get_notifications()->get_count(); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Enqueue styles. |
| 72 | * |
| 73 | * @since 2.3.0 |
| 74 | */ |
| 75 | public function enqueues() { |
| 76 | |
| 77 | if ( ! is_admin_bar_showing() ) { |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | if ( ! $this->has_access() ) { |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | wp_enqueue_style( |
| 86 | 'wp-mail-smtp-admin-bar', |
| 87 | wp_mail_smtp()->assets_url . '/css/admin-bar.min.css', |
| 88 | [], |
| 89 | WPMS_PLUGIN_VER |
| 90 | ); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Register and render admin menu bar. |
| 95 | * |
| 96 | * @since 2.3.0 |
| 97 | * |
| 98 | * @param \WP_Admin_Bar $wp_admin_bar WordPress Admin Bar object. |
| 99 | */ |
| 100 | public function register( \WP_Admin_Bar $wp_admin_bar ) { |
| 101 | |
| 102 | if ( |
| 103 | ! $this->has_access() || |
| 104 | ( |
| 105 | ( |
| 106 | ! $this->has_email_sending_errors() || |
| 107 | (bool) Options::init()->get( 'general', 'email_delivery_errors_hidden' ) |
| 108 | ) && |
| 109 | empty( $this->has_notifications() ) |
| 110 | ) |
| 111 | ) { |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | $items = apply_filters( |
| 116 | 'wp_mail_smtp_admin_adminbarmenu_register', |
| 117 | [ |
| 118 | 'main_menu', |
| 119 | ], |
| 120 | $wp_admin_bar |
| 121 | ); |
| 122 | |
| 123 | foreach ( $items as $item ) { |
| 124 | $this->{ $item }( $wp_admin_bar ); |
| 125 | |
| 126 | do_action( "wp_mail_smtp_admin_adminbarmenu_register_{$item}_after", $wp_admin_bar ); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Render primary top-level admin menu bar item. |
| 132 | * |
| 133 | * @since 2.3.0 |
| 134 | * |
| 135 | * @param \WP_Admin_Bar $wp_admin_bar WordPress Admin Bar object. |
| 136 | */ |
| 137 | public function main_menu( \WP_Admin_Bar $wp_admin_bar ) { |
| 138 | |
| 139 | if ( |
| 140 | $this->has_email_sending_errors() && |
| 141 | ! (bool) Options::init()->get( 'general', 'email_delivery_errors_hidden' ) |
| 142 | ) { |
| 143 | $indicator = ' <span class="wp-mail-smtp-admin-bar-menu-error">!</span>'; |
| 144 | } elseif ( ! empty( $this->has_notifications() ) ) { |
| 145 | $count = $this->has_notifications() < 10 ? $this->has_notifications() : '!'; |
| 146 | $indicator = ' <div class="wp-mail-smtp-admin-bar-menu-notification-counter"><span>' . $count . '</span></div>'; |
| 147 | } |
| 148 | |
| 149 | if ( ! isset( $indicator ) ) { |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | $wp_admin_bar->add_menu( |
| 154 | [ |
| 155 | 'id' => 'wp-mail-smtp-menu', |
| 156 | 'title' => 'WP Mail SMTP' . $indicator, |
| 157 | 'href' => apply_filters( |
| 158 | 'wp_mail_smtp_admin_adminbarmenu_main_menu_href', |
| 159 | wp_mail_smtp()->get_admin()->get_admin_page_url() |
| 160 | ), |
| 161 | ] |
| 162 | ); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Whether any active connection has a live (failed, regular-context) email |
| 167 | * sending error recorded. |
| 168 | * |
| 169 | * Matches the filter used by EmailSendingErrors so the admin bar indicator |
| 170 | * fires for the same set of failures shown by the inline error banner. |
| 171 | * |
| 172 | * @since 4.9.0 |
| 173 | * |
| 174 | * @return bool |
| 175 | */ |
| 176 | private function has_email_sending_errors() { |
| 177 | |
| 178 | $all = EmailSendingDebug::get(); |
| 179 | |
| 180 | if ( empty( $all ) ) { |
| 181 | return false; |
| 182 | } |
| 183 | |
| 184 | $live = array_filter( |
| 185 | $all, |
| 186 | static function ( $record ) { |
| 187 | |
| 188 | return isset( $record['status'], $record['context'] ) |
| 189 | && $record['status'] === 'failed' |
| 190 | && $record['context'] === 'regular'; |
| 191 | } |
| 192 | ); |
| 193 | |
| 194 | return ! empty( $live ); |
| 195 | } |
| 196 | } |
| 197 |