Review.php
166 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AC\Check; |
| 4 | |
| 5 | use AC\Ajax; |
| 6 | use AC\Asset\Location; |
| 7 | use AC\Asset\Script; |
| 8 | use AC\Capabilities; |
| 9 | use AC\Message; |
| 10 | use AC\Notice\NoticeState; |
| 11 | use AC\Registerable; |
| 12 | use AC\Screen; |
| 13 | use AC\Type\Url\Documentation; |
| 14 | use AC\Type\Url\UtmTags; |
| 15 | |
| 16 | final class Review implements Registerable |
| 17 | { |
| 18 | |
| 19 | private const SLUG = 'review'; |
| 20 | private const DELAY_DAYS = 30; |
| 21 | |
| 22 | private Location $location; |
| 23 | |
| 24 | private NoticeState $state; |
| 25 | |
| 26 | public function __construct(Location $location, NoticeState $state) |
| 27 | { |
| 28 | $this->location = $location; |
| 29 | $this->state = $state; |
| 30 | } |
| 31 | |
| 32 | public function register(): void |
| 33 | { |
| 34 | add_action('ac/screen', [$this, 'display']); |
| 35 | |
| 36 | $this->get_ajax_handler()->register(); |
| 37 | } |
| 38 | |
| 39 | public function display(Screen $screen): void |
| 40 | { |
| 41 | if ( ! current_user_can(Capabilities::MANAGE)) { |
| 42 | return; |
| 43 | } |
| 44 | |
| 45 | if ( ! $screen->has_screen()) { |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | if ( ! $screen->is_admin_screen()) { |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | if ($this->state->is_dismissed(self::SLUG)) { |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | $this->state->track_first_seen(self::SLUG); |
| 58 | |
| 59 | if ( ! $this->state->is_delay_met(self::SLUG, self::DELAY_DAYS)) { |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | $script = new Script('ac-notice-review', $this->location->with_suffix('assets/js/message-review.js'), ['jquery'] |
| 64 | ); |
| 65 | $script->enqueue(); |
| 66 | |
| 67 | $notice = new Message\Notice\Dismissible($this->get_message(), $this->get_ajax_handler()); |
| 68 | $notice |
| 69 | ->set_id('review') |
| 70 | ->register(); |
| 71 | } |
| 72 | |
| 73 | protected function get_ajax_handler(): Ajax\Handler |
| 74 | { |
| 75 | $handler = new Ajax\Handler(); |
| 76 | $handler |
| 77 | ->set_action('ac_check_review_dismiss_notice') |
| 78 | ->set_callback([$this, 'ajax_dismiss_notice']); |
| 79 | |
| 80 | return $handler; |
| 81 | } |
| 82 | |
| 83 | public function ajax_dismiss_notice(): void |
| 84 | { |
| 85 | $this->get_ajax_handler()->verify_request(); |
| 86 | $this->state->dismiss(self::SLUG); |
| 87 | } |
| 88 | |
| 89 | private function get_documentation_url(): string |
| 90 | { |
| 91 | return (new UtmTags(new Documentation(), 'review-notice'))->get_url(); |
| 92 | } |
| 93 | |
| 94 | protected function get_message(): string |
| 95 | { |
| 96 | $product = __('Admin Columns', 'codepress-admin-columns'); |
| 97 | |
| 98 | ob_start(); |
| 99 | |
| 100 | ?> |
| 101 | |
| 102 | <div class="info"> |
| 103 | <p> |
| 104 | <?php |
| 105 | printf( |
| 106 | __( |
| 107 | "We don't mean to bug you, but you've been using %s for some time now, and we were wondering if you're happy with the plugin. If so, could you please leave a review at wordpress.org? If you're not happy with %s, please %s.", |
| 108 | 'codepress-admin-columns' |
| 109 | ), |
| 110 | '<strong>' . $product . '</strong>', |
| 111 | $product, |
| 112 | '<a class="hide-review-notice-soft" href="#">' . __( |
| 113 | 'click here', |
| 114 | 'codepress-admin-columns' |
| 115 | ) . '</a>' |
| 116 | ); ?> |
| 117 | </p> |
| 118 | <p class="buttons"> |
| 119 | <a class="button button-primary" href="https://wordpress.org/support/view/plugin-reviews/codepress-admin-columns?rate=5#postform" target="_blank"><?php |
| 120 | _e('Leave a review!', 'codepress-admin-columns'); ?></a> |
| 121 | <a class="button button-secondary hide-review-notice" href='#' data-dismiss=""><?php |
| 122 | _e("Permanently hide notice", 'codepress-admin-columns'); ?></a> |
| 123 | </p> |
| 124 | </div> |
| 125 | <div class="help hidden"> |
| 126 | <a href="#" class="hide-notice hide-review-notice"></a> |
| 127 | <p> |
| 128 | <?php |
| 129 | |
| 130 | printf( |
| 131 | __( |
| 132 | "We're sorry to hear that; maybe we can help! If you're having problems properly setting up %s or if you would like help with some more advanced features, please visit our %s.", |
| 133 | 'codepress-admin-columns' |
| 134 | ), |
| 135 | $product, |
| 136 | '<a href="' . esc_url( |
| 137 | $this->get_documentation_url() |
| 138 | ) . '" target="_blank">' . __( |
| 139 | 'documentation page', |
| 140 | 'codepress-admin-columns' |
| 141 | ) . '</a>' |
| 142 | ); |
| 143 | |
| 144 | printf( |
| 145 | __('You can also find help on the %s, and %s.', 'codepress-admin-columns'), |
| 146 | '<a href="https://wordpress.org/support/plugin/codepress-admin-columns#postform" target="_blank">' . __( |
| 147 | 'Admin Columns forum on WordPress.org', |
| 148 | 'codepress-admin-columns' |
| 149 | ) . '</a>', |
| 150 | '<a href="https://wordpress.org/plugins/codepress-admin-columns/#faq" target="_blank">' . __( |
| 151 | 'find answers to frequently asked questions', |
| 152 | 'codepress-admin-columns' |
| 153 | ) . '</a>' |
| 154 | ); |
| 155 | |
| 156 | ?> |
| 157 | </p> |
| 158 | </div> |
| 159 | |
| 160 | <?php |
| 161 | |
| 162 | return ob_get_clean(); |
| 163 | } |
| 164 | |
| 165 | } |
| 166 |