|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * MU-Plugin: Tosuto E2E Test Helper |
| 4 | + * |
| 5 | + * Queues test toasts via wp_tosuto() when specific query params are present. |
| 6 | + * Only active in wp-env testing environments. |
| 7 | + */ |
| 8 | + |
| 9 | +declare(strict_types=1); |
| 10 | + |
| 11 | +add_action('template_redirect', static function (): void { |
| 12 | + // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 13 | + if (!isset($_GET['tosuto-test'])) { |
| 14 | + return; |
| 15 | + } |
| 16 | + |
| 17 | + // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 18 | + $scenario = sanitize_text_field(wp_unslash($_GET['tosuto-test'])); |
| 19 | + |
| 20 | + switch ($scenario) { |
| 21 | + case 'js': |
| 22 | + add_action('wp_footer', static function (): void { |
| 23 | + ?> |
| 24 | + <script type="module"> |
| 25 | + import { store } from '@wordpress/interactivity'; |
| 26 | + const { actions } = store('wp-tosuto'); |
| 27 | + actions.add('JS Toast Content', { variant: 'success' }); |
| 28 | + </script> |
| 29 | + <?php |
| 30 | + }); |
| 31 | + break; |
| 32 | + |
| 33 | + case 'php': |
| 34 | + wp_tosuto('<strong>PHP Toast</strong> — queued server-side', [ |
| 35 | + 'variant' => 'default', |
| 36 | + 'duration' => 5000, |
| 37 | + ]); |
| 38 | + break; |
| 39 | + |
| 40 | + case 'php-success': |
| 41 | + wp_tosuto('Success toast from PHP', [ |
| 42 | + 'variant' => 'success', |
| 43 | + 'duration' => 5000, |
| 44 | + ]); |
| 45 | + break; |
| 46 | + |
| 47 | + case 'php-error': |
| 48 | + wp_tosuto('Error toast from PHP', [ |
| 49 | + 'variant' => 'error', |
| 50 | + 'duration' => 5000, |
| 51 | + ]); |
| 52 | + break; |
| 53 | + |
| 54 | + case 'php-warning': |
| 55 | + wp_tosuto('Warning toast from PHP', [ |
| 56 | + 'variant' => 'warning', |
| 57 | + 'duration' => 5000, |
| 58 | + ]); |
| 59 | + break; |
| 60 | + |
| 61 | + case 'php-info': |
| 62 | + wp_tosuto('Info toast from PHP', [ |
| 63 | + 'variant' => 'info', |
| 64 | + 'duration' => 5000, |
| 65 | + ]); |
| 66 | + break; |
| 67 | + |
| 68 | + case 'php-multiple': |
| 69 | + wp_tosuto('First toast', ['variant' => 'default', 'duration' => 10000]); |
| 70 | + wp_tosuto('Second toast', ['variant' => 'success', 'duration' => 10000]); |
| 71 | + wp_tosuto('Third toast', ['variant' => 'error', 'duration' => 10000]); |
| 72 | + break; |
| 73 | + |
| 74 | + case 'php-no-dismiss': |
| 75 | + wp_tosuto('Cannot dismiss this', [ |
| 76 | + 'variant' => 'default', |
| 77 | + 'duration' => 0, |
| 78 | + 'dismissable' => false, |
| 79 | + ]); |
| 80 | + break; |
| 81 | + |
| 82 | + case 'php-short-duration': |
| 83 | + wp_tosuto('Gone in 1 second', [ |
| 84 | + 'variant' => 'default', |
| 85 | + 'duration' => 1000, |
| 86 | + ]); |
| 87 | + break; |
| 88 | + |
| 89 | + case 'php-all-variants': |
| 90 | + wp_tosuto('Default variant', ['variant' => 'default', 'duration' => 0]); |
| 91 | + wp_tosuto('Success variant', ['variant' => 'success', 'duration' => 0]); |
| 92 | + wp_tosuto('Error variant', ['variant' => 'error', 'duration' => 0]); |
| 93 | + wp_tosuto('Warning variant', ['variant' => 'warning', 'duration' => 0]); |
| 94 | + wp_tosuto('Info variant', ['variant' => 'info', 'duration' => 0]); |
| 95 | + break; |
| 96 | + } |
| 97 | +}); |
0 commit comments