Review.php
183 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AC\Check; |
| 4 | |
| 5 | use AC\Ajax; |
| 6 | use AC\Asset\Location\Absolute; |
| 7 | use AC\Asset\Script; |
| 8 | use AC\Capabilities; |
| 9 | use AC\Message; |
| 10 | use AC\Preferences; |
| 11 | use AC\Registerable; |
| 12 | use AC\Screen; |
| 13 | use AC\Type\Url\Documentation; |
| 14 | use AC\Type\Url\UtmTags; |
| 15 | |
| 16 | class Review |
| 17 | implements Registerable |
| 18 | { |
| 19 | |
| 20 | private $location; |
| 21 | |
| 22 | public function __construct(Absolute $location) |
| 23 | { |
| 24 | $this->location = $location; |
| 25 | } |
| 26 | |
| 27 | public function register(): void |
| 28 | { |
| 29 | add_action('ac/screen', [$this, 'display']); |
| 30 | |
| 31 | $this->get_ajax_handler()->register(); |
| 32 | } |
| 33 | |
| 34 | public function display(Screen $screen): void |
| 35 | { |
| 36 | if ( ! $screen->has_screen()) { |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | if ( ! current_user_can(Capabilities::MANAGE)) { |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | if ( ! $screen->is_admin_screen() && ! $screen->is_list_screen()) { |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | if ($this->get_preferences()->get('dismiss-review')) { |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | if ( ! $this->first_login_compare()) { |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | $script = new Script('ac-notice-review', $this->location->with_suffix('assets/js/message-review.js'), ['jquery'] |
| 57 | ); |
| 58 | $script->enqueue(); |
| 59 | |
| 60 | $notice = new Message\Notice\Dismissible($this->get_message(), $this->get_ajax_handler()); |
| 61 | $notice |
| 62 | ->set_id('review') |
| 63 | ->register(); |
| 64 | } |
| 65 | |
| 66 | protected function get_ajax_handler(): Ajax\Handler |
| 67 | { |
| 68 | $handler = new Ajax\Handler(); |
| 69 | $handler |
| 70 | ->set_action('ac_check_review_dismiss_notice') |
| 71 | ->set_callback([$this, 'ajax_dismiss_notice']); |
| 72 | |
| 73 | return $handler; |
| 74 | } |
| 75 | |
| 76 | protected function get_preferences(): Preferences\User |
| 77 | { |
| 78 | return new Preferences\User('check-review'); |
| 79 | } |
| 80 | |
| 81 | protected function first_login_compare(): bool |
| 82 | { |
| 83 | // Show after 30 days |
| 84 | return time() - (30 * DAY_IN_SECONDS) > $this->get_first_login(); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Return the Unix timestamp of first login |
| 89 | */ |
| 90 | protected function get_first_login(): int |
| 91 | { |
| 92 | $timestamp = $this->get_preferences()->get('first-login-review'); |
| 93 | |
| 94 | if (empty($timestamp)) { |
| 95 | $timestamp = time(); |
| 96 | |
| 97 | $this->get_preferences()->set('first-login-review', $timestamp); |
| 98 | } |
| 99 | |
| 100 | return $timestamp; |
| 101 | } |
| 102 | |
| 103 | public function ajax_dismiss_notice(): void |
| 104 | { |
| 105 | $this->get_ajax_handler()->verify_request(); |
| 106 | $this->get_preferences()->set('dismiss-review', true); |
| 107 | } |
| 108 | |
| 109 | private function get_documentation_url(string $utm_medium): string |
| 110 | { |
| 111 | return (new UtmTags(new Documentation(), $utm_medium))->get_url(); |
| 112 | } |
| 113 | |
| 114 | protected function get_message(): string |
| 115 | { |
| 116 | $product = __('Admin Columns', 'codepress-admin-columns'); |
| 117 | |
| 118 | ob_start(); |
| 119 | |
| 120 | ?> |
| 121 | |
| 122 | <div class="info"> |
| 123 | <p> |
| 124 | <?php |
| 125 | printf( |
| 126 | __( |
| 127 | "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.", |
| 128 | 'codepress-admin-columns' |
| 129 | ), |
| 130 | '<strong>' . $product . '</strong>', |
| 131 | $product, |
| 132 | '<a class="hide-review-notice-soft" href="#">' . __( |
| 133 | 'click here', |
| 134 | 'codepress-admin-columns' |
| 135 | ) . '</a>' |
| 136 | ); ?> |
| 137 | </p> |
| 138 | <p class="buttons"> |
| 139 | <a class="button button-primary" href="https://wordpress.org/support/view/plugin-reviews/codepress-admin-columns?rate=5#postform" target="_blank"><?php |
| 140 | _e('Leave a review!', 'codepress-admin-columns'); ?></a> |
| 141 | <a class="button button-secondary hide-review-notice" href='#' data-dismiss=""><?php |
| 142 | _e("Permanently hide notice", 'codepress-admin-columns'); ?></a> |
| 143 | </p> |
| 144 | </div> |
| 145 | <div class="help hidden"> |
| 146 | <a href="#" class="hide-notice hide-review-notice"></a> |
| 147 | <p> |
| 148 | <?php |
| 149 | |
| 150 | printf( |
| 151 | __( |
| 152 | "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.", |
| 153 | 'codepress-admin-columns' |
| 154 | ), |
| 155 | $product, |
| 156 | '<a href="' . esc_url($this->get_documentation_url('review-notice')) . '" target="_blank">' . __( |
| 157 | 'documentation page', |
| 158 | 'codepress-admin-columns' |
| 159 | ) . '</a>' |
| 160 | ); |
| 161 | |
| 162 | printf( |
| 163 | __('You can also find help on the %s, and %s.', 'codepress-admin-columns'), |
| 164 | '<a href="https://wordpress.org/support/plugin/codepress-admin-columns#postform" target="_blank">' . __( |
| 165 | 'Admin Columns forum on WordPress.org', |
| 166 | 'codepress-admin-columns' |
| 167 | ) . '</a>', |
| 168 | '<a href="https://wordpress.org/plugins/codepress-admin-columns/#faq" target="_blank">' . __( |
| 169 | 'find answers to frequently asked questions', |
| 170 | 'codepress-admin-columns' |
| 171 | ) . '</a>' |
| 172 | ); |
| 173 | |
| 174 | ?> |
| 175 | </p> |
| 176 | </div> |
| 177 | |
| 178 | <?php |
| 179 | |
| 180 | return ob_get_clean(); |
| 181 | } |
| 182 | |
| 183 | } |