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