DisplayNoticesInAdmin.php
1 year ago
EnqueueNoticesScriptsAndStyles.php
1 year ago
NoticeShouldRender.php
1 year ago
RenderAdminNotice.php
1 year ago
RenderAdminNotice.php
103 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | |
| 6 | namespace Give\Vendors\StellarWP\AdminNotices\Actions; |
| 7 | |
| 8 | use Give\Vendors\StellarWP\AdminNotices\AdminNotice; |
| 9 | use Give\Vendors\StellarWP\AdminNotices\DataTransferObjects\NoticeElementProperties; |
| 10 | use Give\Vendors\StellarWP\AdminNotices\Traits\HasNamespace; |
| 11 | use WP_HTML_Tag_Processor; |
| 12 | |
| 13 | /** |
| 14 | * Renders the admin notice based on the configuration of the notice. |
| 15 | * |
| 16 | * @since 1.1.0 refactored to use namespace and notice is passed to the __invoke method |
| 17 | * @since 1.0.0 |
| 18 | */ |
| 19 | class RenderAdminNotice |
| 20 | { |
| 21 | use HasNamespace; |
| 22 | |
| 23 | /** |
| 24 | * Renders the admin notice |
| 25 | * |
| 26 | * @since 2.0.0 custom notices have a completely different render flow |
| 27 | * @since 1.1.0 added namespacing and notice is passed to the __invoke method |
| 28 | * @since 1.0.0 |
| 29 | */ |
| 30 | public function __invoke(AdminNotice $notice): string |
| 31 | { |
| 32 | $elementProperties = new NoticeElementProperties($notice, $this->namespace); |
| 33 | $renderTextOrCallback = $notice->getRenderTextOrCallback(); |
| 34 | |
| 35 | if (is_callable($renderTextOrCallback)) { |
| 36 | $content = $renderTextOrCallback($notice, $elementProperties); |
| 37 | } else { |
| 38 | $content = $renderTextOrCallback; |
| 39 | } |
| 40 | |
| 41 | if ($notice->isCustom()) { |
| 42 | return $this->applyCustomAttributesToContent($content, $notice); |
| 43 | } |
| 44 | |
| 45 | return sprintf( |
| 46 | "<div class='%s' $elementProperties->idAttribute>%s</div>", |
| 47 | esc_attr($this->getStandardWrapperClasses($notice)), |
| 48 | $notice->shouldAutoParagraph() ? wpautop($content) : $content |
| 49 | ); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Generates the classes for the standard WordPress notice wrapper. |
| 54 | * |
| 55 | * @since 2.0.0 notice is assumed to be standard only |
| 56 | * @since 1.1.0 notice is passed instead of accessed as a property |
| 57 | * @since 1.0.0 |
| 58 | */ |
| 59 | private function getStandardWrapperClasses(AdminNotice $notice): string |
| 60 | { |
| 61 | $classes = [ |
| 62 | 'notice', |
| 63 | "notice-{$notice->getUrgency()}", |
| 64 | ]; |
| 65 | |
| 66 | if ($notice->isDismissible()) { |
| 67 | $classes[] = "is-dismissible"; |
| 68 | } |
| 69 | |
| 70 | if ($notice->getLocation() && $notice->getLocation()->isInline()) { |
| 71 | $classes[] = 'inline'; |
| 72 | } |
| 73 | |
| 74 | if ($notice->usesAlternateStyles()) { |
| 75 | $classes[] = 'notice-alt'; |
| 76 | } |
| 77 | |
| 78 | return implode(' ', $classes); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Apply the needed custom attributes to the content. |
| 83 | * |
| 84 | * @since 2.0.0 |
| 85 | */ |
| 86 | private function applyCustomAttributesToContent( |
| 87 | string $content, |
| 88 | AdminNotice $notice |
| 89 | ): string { |
| 90 | $tags = new WP_HTML_Tag_Processor($content); |
| 91 | |
| 92 | $tags->next_tag(); |
| 93 | |
| 94 | $tags->set_attribute("data-stellarwp-{$this->namespace}-notice-id", $notice->getId()); |
| 95 | |
| 96 | if ($notice->getLocation()) { |
| 97 | $tags->set_attribute("data-stellarwp-{$this->namespace}-location", (string)$notice->getLocation()); |
| 98 | } |
| 99 | |
| 100 | return $tags->__toString(); |
| 101 | } |
| 102 | } |
| 103 |