AcfBulkEditNotice.php
2 days ago
AcfNotice.php
2 days ago
AcfSortAndFilterNotice.php
2 days ago
EventsCalendarNotice.php
2 days ago
GravityFormsNotice.php
2 days ago
IntegrationNotice.php
2 days ago
IntegrationNoticeRenderer.php
2 days ago
PostEditReferrerAware.php
2 days ago
UsageAwareNotice.php
2 days ago
WooCommerceOrdersFilterNotice.php
2 days ago
WooCommerceOrdersNotice.php
2 days ago
WooCommerceOrdersScreenAware.php
2 days ago
WooCommerceOrdersSearchNotice.php
2 days ago
WooCommerceProductsBulkEditNotice.php
2 days ago
WooCommerceProductsFilterNotice.php
2 days ago
WooCommerceProductsNotice.php
2 days ago
WooCommerceProductsSearchNotice.php
2 days ago
IntegrationNoticeRenderer.php
145 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\Check\Integration; |
| 6 | |
| 7 | use AC\Ajax; |
| 8 | use AC\Asset\Script; |
| 9 | use AC\Asset\Style; |
| 10 | use AC\Capabilities; |
| 11 | use AC\Notice\NoticeState; |
| 12 | use AC\Registerable; |
| 13 | use AC\Screen; |
| 14 | use AC\View; |
| 15 | |
| 16 | class IntegrationNoticeRenderer implements Registerable |
| 17 | { |
| 18 | /** |
| 19 | * @var IntegrationNotice[] |
| 20 | */ |
| 21 | private array $notices; |
| 22 | |
| 23 | private NoticeState $state; |
| 24 | |
| 25 | /** |
| 26 | * @param IntegrationNotice[] $notices |
| 27 | */ |
| 28 | public function __construct(array $notices, NoticeState $state) |
| 29 | { |
| 30 | $this->notices = $notices; |
| 31 | $this->state = $state; |
| 32 | } |
| 33 | |
| 34 | public function register(): void |
| 35 | { |
| 36 | if (! current_user_can(Capabilities::MANAGE)) { |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | foreach ($this->notices as $notice) { |
| 41 | $handler = $this->create_ajax_handler($notice); |
| 42 | $handler->register(); |
| 43 | } |
| 44 | |
| 45 | add_action('ac/screen', [$this, 'display']); |
| 46 | } |
| 47 | |
| 48 | public function display(Screen $screen): void |
| 49 | { |
| 50 | $notice = $this->resolve($screen); |
| 51 | |
| 52 | if (! $notice) { |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | $handler = $this->create_ajax_handler($notice); |
| 57 | |
| 58 | add_action('admin_notices', function () use ($notice, $handler) { |
| 59 | echo $this->render($notice, $handler); |
| 60 | }); |
| 61 | |
| 62 | add_action('admin_enqueue_scripts', static function () { |
| 63 | (new Style('ac-message'))->enqueue(); |
| 64 | (new Script('ac-message'))->enqueue(); |
| 65 | }); |
| 66 | } |
| 67 | |
| 68 | private function resolve(Screen $screen): ?IntegrationNotice |
| 69 | { |
| 70 | foreach ($this->notices as $notice) { |
| 71 | if ($notice->is_active($screen)) { |
| 72 | $this->state->track_first_seen($notice->get_slug()); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | foreach ($this->notices as $notice) { |
| 77 | if ($notice->is_active($screen) && $this->is_ready_to_show($notice)) { |
| 78 | return $notice; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | return null; |
| 83 | } |
| 84 | |
| 85 | private function is_ready_to_show(IntegrationNotice $notice): bool |
| 86 | { |
| 87 | if ($notice instanceof UsageAwareNotice && ! $notice->is_usage_detected()) { |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | if ($this->state->is_cooldown_active(7)) { |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | if ($this->state->is_dismissed($notice->get_slug())) { |
| 96 | return false; |
| 97 | } |
| 98 | |
| 99 | if (! $this->state->is_delay_met($notice->get_slug(), $notice->get_delay_days())) { |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | return true; |
| 104 | } |
| 105 | |
| 106 | private function create_ajax_handler(IntegrationNotice $notice): Ajax\Handler |
| 107 | { |
| 108 | $handler = new Ajax\Handler(); |
| 109 | $handler |
| 110 | ->set_action('ac_dismiss_suggestion_' . $notice->get_slug()) |
| 111 | ->set_callback(function () use ($notice, $handler) { |
| 112 | $handler->verify_request(); |
| 113 | |
| 114 | if (! current_user_can(Capabilities::MANAGE)) { |
| 115 | wp_die('-1'); |
| 116 | } |
| 117 | |
| 118 | $this->state->dismiss($notice->get_slug()); |
| 119 | $this->state->track_dismissal(); |
| 120 | }); |
| 121 | |
| 122 | return $handler; |
| 123 | } |
| 124 | |
| 125 | private function render(IntegrationNotice $notice, Ajax\Handler $handler): string |
| 126 | { |
| 127 | $view = new View([ |
| 128 | 'eyebrow' => $notice->get_eyebrow(), |
| 129 | 'title' => $notice->get_title(), |
| 130 | 'description' => $notice->get_description(), |
| 131 | 'cta_label' => $notice->get_cta_label(), |
| 132 | 'cta_url' => $notice->get_cta_url(), |
| 133 | 'secondary_label' => $notice->get_secondary_label(), |
| 134 | 'secondary_url' => $notice->get_secondary_url(), |
| 135 | 'dismissible_callback' => $handler->get_params(), |
| 136 | 'extra_classes' => $notice->get_extra_classes(), |
| 137 | ]); |
| 138 | |
| 139 | $view->set_template('message/notice/integration'); |
| 140 | |
| 141 | return $view->render(); |
| 142 | } |
| 143 | |
| 144 | } |
| 145 |