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