DebugEvents
6 months ago
Pages
6 months ago
AdminBarMenu.php
6 months ago
Area.php
6 months ago
ConnectionSettings.php
6 months ago
DashboardWidget.php
6 months ago
DomainChecker.php
6 months ago
Education.php
6 months ago
FlyoutMenu.php
6 months ago
Notifications.php
6 months ago
PageAbstract.php
6 months ago
PageInterface.php
6 months ago
ParentPageAbstract.php
6 months ago
PluginsInstallSkin.php
6 months ago
Review.php
6 months ago
SetupWizard.php
6 months ago
ParentPageAbstract.php
382 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPMailSMTP\Admin; |
| 4 | |
| 5 | use WPMailSMTP\WP; |
| 6 | |
| 7 | /** |
| 8 | * Class ParentPageAbstract. |
| 9 | * |
| 10 | * @since 2.8.0 |
| 11 | */ |
| 12 | abstract class ParentPageAbstract implements PageInterface { |
| 13 | |
| 14 | /** |
| 15 | * Slug of a page. |
| 16 | * |
| 17 | * @since 2.8.0 |
| 18 | * |
| 19 | * @var string |
| 20 | */ |
| 21 | protected $slug; |
| 22 | |
| 23 | /** |
| 24 | * Page tabs. |
| 25 | * |
| 26 | * @since 2.8.0 |
| 27 | * |
| 28 | * @var PageAbstract[] |
| 29 | */ |
| 30 | protected $tabs = []; |
| 31 | |
| 32 | /** |
| 33 | * Page default tab slug. |
| 34 | * |
| 35 | * @since 2.8.0 |
| 36 | * |
| 37 | * @var string |
| 38 | */ |
| 39 | protected $default_tab = ''; |
| 40 | |
| 41 | /** |
| 42 | * Constructor. |
| 43 | * |
| 44 | * @since 2.8.0 |
| 45 | * |
| 46 | * @param array $tabs Page tabs. |
| 47 | */ |
| 48 | public function __construct( $tabs = [] ) { |
| 49 | |
| 50 | /** |
| 51 | * Filters parent page tabs. |
| 52 | * |
| 53 | * @since 2.8.0 |
| 54 | * |
| 55 | * @param string[] $tabs Parent page tabs. |
| 56 | */ |
| 57 | $tabs = apply_filters( 'wp_mail_smtp_admin_page_' . $this->slug . '_tabs', $tabs ); |
| 58 | |
| 59 | if ( wp_mail_smtp()->get_admin()->is_admin_page( $this->slug ) ) { |
| 60 | $this->init_tabs( $tabs ); |
| 61 | $this->hooks(); |
| 62 | } |
| 63 | |
| 64 | if ( WP::is_doing_self_ajax() ) { |
| 65 | $this->init_ajax( $tabs ); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Hooks. |
| 71 | * |
| 72 | * @since 2.8.0 |
| 73 | */ |
| 74 | protected function hooks() { |
| 75 | |
| 76 | add_action( 'admin_init', [ $this, 'process_actions' ] ); |
| 77 | |
| 78 | // Register tab related hooks. |
| 79 | if ( isset( $this->tabs[ $this->get_current_tab() ] ) ) { |
| 80 | $this->tabs[ $this->get_current_tab() ]->hooks(); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Initialize ajax actions. |
| 86 | * |
| 87 | * @since 3.0.0 |
| 88 | * |
| 89 | * @param array $tabs Page tabs. |
| 90 | */ |
| 91 | private function init_ajax( $tabs ) { |
| 92 | |
| 93 | foreach ( $tabs as $tab ) { |
| 94 | if ( $this->is_valid_tab( $tab ) ) { |
| 95 | ( new $tab( $this ) )->ajax(); |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Get the page slug. |
| 102 | * |
| 103 | * @since 2.8.0 |
| 104 | * |
| 105 | * @return string |
| 106 | */ |
| 107 | public function get_slug() { |
| 108 | |
| 109 | return $this->slug; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Get the page tabs. |
| 114 | * |
| 115 | * @since 2.8.0 |
| 116 | * |
| 117 | * @return PageAbstract[] |
| 118 | */ |
| 119 | public function get_tabs() { |
| 120 | |
| 121 | return $this->tabs; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Get the page tabs slugs. |
| 126 | * |
| 127 | * @since 2.8.0 |
| 128 | * |
| 129 | * @return string[] |
| 130 | */ |
| 131 | public function get_tabs_slugs() { |
| 132 | |
| 133 | return array_map( |
| 134 | function ( $tab ) { |
| 135 | return $tab->get_slug(); |
| 136 | }, |
| 137 | $this->tabs |
| 138 | ); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Get the page/tab link. |
| 143 | * |
| 144 | * @since 2.8.0 |
| 145 | * |
| 146 | * @param string $tab Tab to generate a link to. |
| 147 | * |
| 148 | * @return string |
| 149 | */ |
| 150 | public function get_link( $tab = '' ) { |
| 151 | |
| 152 | return add_query_arg( |
| 153 | 'tab', |
| 154 | $this->get_defined_tab( $tab ), |
| 155 | WP::admin_url( 'admin.php?page=' . Area::SLUG . '-' . $this->slug ) |
| 156 | ); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Get the current tab. |
| 161 | * |
| 162 | * @since 2.8.0 |
| 163 | * |
| 164 | * @return string |
| 165 | */ |
| 166 | public function get_current_tab() { |
| 167 | |
| 168 | $tab = isset( $_GET['tab'] ) ? sanitize_key( $_GET['tab'] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 169 | |
| 170 | return $this->get_defined_tab( $tab ); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Get the tab label. |
| 175 | * |
| 176 | * @since 2.9.0 |
| 177 | * |
| 178 | * @param string $tab Tab key. |
| 179 | * |
| 180 | * @return string |
| 181 | */ |
| 182 | public function get_tab_label( $tab ) { |
| 183 | |
| 184 | $tabs = $this->get_tabs(); |
| 185 | |
| 186 | return isset( $tabs[ $tab ] ) ? $tabs[ $tab ]->get_label() : ''; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Get the tab title. |
| 191 | * |
| 192 | * @since 2.9.0 |
| 193 | * |
| 194 | * @param string $tab Tab key. |
| 195 | * |
| 196 | * @return string |
| 197 | */ |
| 198 | public function get_tab_title( $tab ) { |
| 199 | |
| 200 | $tabs = $this->get_tabs(); |
| 201 | |
| 202 | return isset( $tabs[ $tab ] ) ? $tabs[ $tab ]->get_title() : ''; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Get the defined or default tab. |
| 207 | * |
| 208 | * @since 2.8.0 |
| 209 | * |
| 210 | * @param string $tab Tab to check. |
| 211 | * |
| 212 | * @return string Defined tab. Fallback to default one if it doesn't exist. |
| 213 | */ |
| 214 | protected function get_defined_tab( $tab ) { |
| 215 | |
| 216 | $tab = sanitize_key( $tab ); |
| 217 | |
| 218 | return in_array( $tab, $this->get_tabs_slugs(), true ) ? $tab : $this->default_tab; |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Initialize tabs. |
| 223 | * |
| 224 | * @since 2.8.0 |
| 225 | * |
| 226 | * @param array $tabs Page tabs. |
| 227 | */ |
| 228 | public function init_tabs( $tabs ) { |
| 229 | |
| 230 | foreach ( $tabs as $key => $tab ) { |
| 231 | if ( ! $this->is_valid_tab( $tab ) ) { |
| 232 | continue; |
| 233 | } |
| 234 | |
| 235 | $this->tabs[ $key ] = new $tab( $this ); |
| 236 | } |
| 237 | |
| 238 | // Sort tabs by priority. |
| 239 | $this->sort_tabs(); |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * All possible plugin forms manipulation and hooks registration will be done here. |
| 244 | * |
| 245 | * @since 2.8.0 |
| 246 | */ |
| 247 | public function process_actions() { |
| 248 | |
| 249 | $tabs = $this->get_tabs_slugs(); |
| 250 | |
| 251 | // Allow to process only own tabs. |
| 252 | if ( ! array_key_exists( $this->get_current_tab(), $tabs ) ) { |
| 253 | return; |
| 254 | } |
| 255 | |
| 256 | // Process POST only if it exists. |
| 257 | // phpcs:disable WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash |
| 258 | if ( ! empty( $_POST ) && isset( $_POST['wp-mail-smtp-post'] ) ) { |
| 259 | if ( ! empty( $_POST['wp-mail-smtp'] ) ) { |
| 260 | $post = $_POST['wp-mail-smtp']; |
| 261 | } else { |
| 262 | $post = []; |
| 263 | } |
| 264 | |
| 265 | $this->tabs[ $this->get_current_tab() ]->process_post( $post ); |
| 266 | } |
| 267 | // phpcs:enable |
| 268 | |
| 269 | // This won't do anything for most pages. |
| 270 | // Works for plugin page only, when GET params are allowed. |
| 271 | $this->tabs[ $this->get_current_tab() ]->process_auth(); |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Display page content based on the current tab. |
| 276 | * |
| 277 | * @since 2.8.0 |
| 278 | */ |
| 279 | public function display() { |
| 280 | |
| 281 | $current_tab = $this->get_current_tab(); |
| 282 | $page_slug = $this->slug; |
| 283 | ?> |
| 284 | <div class="wp-mail-smtp-page-title"> |
| 285 | <?php if ( count( $this->tabs ) > 1 ) : ?> |
| 286 | <?php foreach ( $this->tabs as $tab ) : ?> |
| 287 | <a href="<?php echo esc_url( $tab->get_link() ); ?>" |
| 288 | class="tab <?php echo $current_tab === $tab->get_slug() ? 'active' : ''; ?>"> |
| 289 | <?php echo esc_html( $tab->get_label() ); ?> |
| 290 | </a> |
| 291 | <?php endforeach; ?> |
| 292 | <?php else : ?> |
| 293 | <span class="page-title"><?php echo esc_html( array_values( $this->tabs )[0]->get_title() ); ?></span> |
| 294 | <?php endif; ?> |
| 295 | |
| 296 | <?php |
| 297 | /** |
| 298 | * Fires after page title. |
| 299 | * |
| 300 | * @since 2.9.0 |
| 301 | * |
| 302 | * @param ParentPageAbstract $page Current page. |
| 303 | */ |
| 304 | do_action( "wp_mail_smtp_admin_page_{$page_slug}_{$current_tab}_display_header", $this ); |
| 305 | ?> |
| 306 | </div> |
| 307 | |
| 308 | <div class="wp-mail-smtp-page-content"> |
| 309 | <?php |
| 310 | foreach ( $this->tabs as $tab ) { |
| 311 | if ( $tab->get_slug() === $current_tab ) { |
| 312 | |
| 313 | printf( '<h1 class="screen-reader-text">%s</h1>', esc_html( $tab->get_title() ) ); |
| 314 | |
| 315 | /** |
| 316 | * Fires before tab content. |
| 317 | * |
| 318 | * @since 2.8.0 |
| 319 | * |
| 320 | * @param PageAbstract $tab Current tab. |
| 321 | */ |
| 322 | do_action( 'wp_mail_smtp_admin_pages_before_content', $tab ); |
| 323 | |
| 324 | /** |
| 325 | * Fires before tab content. |
| 326 | * |
| 327 | * @since 2.9.0 |
| 328 | * |
| 329 | * @param PageAbstract $tab Current tab. |
| 330 | */ |
| 331 | do_action( "wp_mail_smtp_admin_page_{$page_slug}_{$current_tab}_display_before", $tab ); |
| 332 | |
| 333 | $tab->display(); |
| 334 | |
| 335 | /** |
| 336 | * Fires after tab content. |
| 337 | * |
| 338 | * @since 2.8.0 |
| 339 | * |
| 340 | * @param PageAbstract $tab Current tab. |
| 341 | */ |
| 342 | do_action( "wp_mail_smtp_admin_page_{$page_slug}_{$current_tab}_display_after", $tab ); |
| 343 | |
| 344 | break; |
| 345 | } |
| 346 | } |
| 347 | ?> |
| 348 | </div> |
| 349 | <?php |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * Sort tabs by priority. |
| 354 | * |
| 355 | * @since 2.8.0 |
| 356 | */ |
| 357 | protected function sort_tabs() { |
| 358 | |
| 359 | uasort( |
| 360 | $this->tabs, |
| 361 | function ( $a, $b ) { |
| 362 | |
| 363 | return ( $a->get_priority() < $b->get_priority() ) ? - 1 : 1; |
| 364 | } |
| 365 | ); |
| 366 | } |
| 367 | |
| 368 | /** |
| 369 | * Whether tab is valid. |
| 370 | * |
| 371 | * @since 3.0.0 |
| 372 | * |
| 373 | * @param array $tab Page tab. |
| 374 | * |
| 375 | * @return bool |
| 376 | */ |
| 377 | private function is_valid_tab( $tab ) { |
| 378 | |
| 379 | return is_subclass_of( $tab, '\WPMailSMTP\Admin\PageAbstract' ); |
| 380 | } |
| 381 | } |
| 382 |