convertkit
/
vendor
/
convertkit
/
convertkit-wordpress-libraries
/
src
/
class-convertkit-review-request.php
class-convertkit-review-request.php in Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages 3.4.4, at vendor/convertkit/convertkit-wordpress-libraries/src/class-convertkit-review-request.php
| 1 | <?php |
| 2 | /** |
| 3 | * ConvertKit Review Request class. |
| 4 | * |
| 5 | * @package ConvertKit |
| 6 | * @author ConvertKit |
| 7 | */ |
| 8 | |
| 9 | /** |
| 10 | * Displays a one time review request notification in the WordPress |
| 11 | * Administration interface. |
| 12 | * |
| 13 | * @package ConvertKit |
| 14 | * @author ConvertKit |
| 15 | */ |
| 16 | class ConvertKit_Review_Request { |
| 17 | |
| 18 | /** |
| 19 | * Holds the Plugin name. |
| 20 | * |
| 21 | * @since 1.0.0 |
| 22 | * |
| 23 | * @var string |
| 24 | */ |
| 25 | private $plugin_name; |
| 26 | |
| 27 | /** |
| 28 | * Holds the Plugin slug. |
| 29 | * |
| 30 | * @since 1.0.0 |
| 31 | * |
| 32 | * @var string |
| 33 | */ |
| 34 | private $plugin_slug; |
| 35 | |
| 36 | /** |
| 37 | * Holds the text items to display on the review request notification. |
| 38 | * |
| 39 | * @since 1.3.4 |
| 40 | * |
| 41 | * @var array |
| 42 | */ |
| 43 | private $text_items; |
| 44 | |
| 45 | /** |
| 46 | * Holds the number of days after the Plugin requests a review to then |
| 47 | * display the review notification in WordPress' Administration interface. |
| 48 | * |
| 49 | * @since 1.0.0 |
| 50 | * |
| 51 | * @var int |
| 52 | */ |
| 53 | private $number_of_days_in_future = 3; |
| 54 | |
| 55 | /** |
| 56 | * Registers action and filter hooks. |
| 57 | * |
| 58 | * @since 1.0.0 |
| 59 | * |
| 60 | * @param string $plugin_name Plugin Name (e.g. ConvertKit). |
| 61 | * @param string $plugin_slug Plugin Slug (e.g. convertkit). |
| 62 | * @param string $plugin_path Plugin Path (unused, but kept for backward compat. with Plugins that include this argument). |
| 63 | */ |
| 64 | public function __construct( $plugin_name, $plugin_slug, $plugin_path ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter, @phpstan-ignore-line |
| 65 | |
| 66 | // Store the Plugin name, slug and text items. |
| 67 | $this->plugin_name = $plugin_name; |
| 68 | $this->plugin_slug = $plugin_slug; |
| 69 | $this->text_items = array( |
| 70 | 'message' => sprintf( |
| 71 | 'We\'d be super grateful if you could spread the word about %s and give it a 5 star rating on WordPress?', |
| 72 | $this->plugin_name |
| 73 | ), |
| 74 | 'leave_review' => 'Yes, leave review', |
| 75 | 'having_issues' => sprintf( |
| 76 | 'No, I\'m having issues with %s', |
| 77 | $this->plugin_name |
| 78 | ), |
| 79 | ); |
| 80 | |
| 81 | // Register an AJAX action to dismiss the review. |
| 82 | add_action( 'wp_ajax_' . str_replace( '-', '_', $this->plugin_slug ) . '_dismiss_review', array( $this, 'dismiss_review' ) ); |
| 83 | |
| 84 | // Maybe display a review request in the WordPress Admin notices. |
| 85 | add_action( 'admin_notices', array( $this, 'maybe_display_review_request' ) ); |
| 86 | |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Displays a dismissible WordPress Administration notice requesting a review, if requested |
| 91 | * by the main Plugin and the Review Request hasn't been disabled. |
| 92 | * |
| 93 | * @since 1.0.0 |
| 94 | */ |
| 95 | public function maybe_display_review_request() { |
| 96 | |
| 97 | // If we're not an Admin user, bail. |
| 98 | if ( ! function_exists( 'current_user_can' ) ) { |
| 99 | return; |
| 100 | } |
| 101 | if ( ! current_user_can( 'activate_plugins' ) ) { |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | // Don't display a review request on multisite. This is so that existing Plugin |
| 106 | // users who existed prior to this feature don't get bombarded with the same |
| 107 | // notification across 100+ of their sites on a multisite network. |
| 108 | if ( is_multisite() ) { |
| 109 | return; |
| 110 | } |
| 111 | |
| 112 | // If the review request was dismissed by the user, bail. |
| 113 | if ( $this->dismissed_review() ) { |
| 114 | return; |
| 115 | } |
| 116 | |
| 117 | // If no review request has been set by the plugin, bail. |
| 118 | if ( ! $this->requested_review() ) { |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | // If here, display the request for a review. |
| 123 | include_once 'views/review-request.php'; |
| 124 | |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Returns the text to display at the start of the review request notification. |
| 129 | * |
| 130 | * @since 1.3.4 |
| 131 | * |
| 132 | * @return string |
| 133 | */ |
| 134 | public function get_message_text() { |
| 135 | |
| 136 | // Return blank string if message text doesn't exist in the array. |
| 137 | if ( ! array_key_exists( 'message', $this->text_items ) ) { |
| 138 | return ''; |
| 139 | } |
| 140 | |
| 141 | return $this->text_items['message']; |
| 142 | |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Returns the text to display prompting the user to leave a review. |
| 147 | * |
| 148 | * @since 1.3.4 |
| 149 | * |
| 150 | * @return string |
| 151 | */ |
| 152 | public function get_leave_review_text() { |
| 153 | |
| 154 | // Return blank string if leave review text doesn't exist in the array. |
| 155 | if ( ! array_key_exists( 'leave_review', $this->text_items ) ) { |
| 156 | return ''; |
| 157 | } |
| 158 | |
| 159 | return $this->text_items['leave_review']; |
| 160 | |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Returns the text to display if the user is having issues with the Plugin. |
| 165 | * |
| 166 | * @since 1.3.4 |
| 167 | * |
| 168 | * @return string |
| 169 | */ |
| 170 | public function get_having_issues_text() { |
| 171 | |
| 172 | // Return blank string if leave review text doesn't exist in the array. |
| 173 | if ( ! array_key_exists( 'having_issues', $this->text_items ) ) { |
| 174 | return ''; |
| 175 | } |
| 176 | |
| 177 | return $this->text_items['having_issues']; |
| 178 | |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Sets a flag in the options table requesting a review notification be displayed |
| 183 | * in the WordPress Administration. |
| 184 | * |
| 185 | * @since 1.0.0 |
| 186 | */ |
| 187 | public function request_review() { |
| 188 | |
| 189 | // If a review has already been requested, bail. |
| 190 | $time = get_option( $this->plugin_slug . '-review-request' ); |
| 191 | if ( ! empty( $time ) ) { |
| 192 | return; |
| 193 | } |
| 194 | |
| 195 | // Request a review notification to be displayed beginning at a future timestamp. |
| 196 | update_option( $this->plugin_slug . '-review-request', time() + ( $this->number_of_days_in_future * DAY_IN_SECONDS ) ); |
| 197 | |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Flag to indicate whether a review has been requested by the Plugin, |
| 202 | * and the minimum time has passed between the Plugin requesting a review |
| 203 | * and now. |
| 204 | * |
| 205 | * @since 1.0.0 |
| 206 | * |
| 207 | * @return bool Review Requested |
| 208 | */ |
| 209 | public function requested_review() { |
| 210 | |
| 211 | // Bail if no review was requested by the Plugin. |
| 212 | $start_displaying_review_at = get_option( $this->plugin_slug . '-review-request' ); |
| 213 | if ( empty( $start_displaying_review_at ) ) { |
| 214 | return false; |
| 215 | } |
| 216 | |
| 217 | // Bail if a review was requested by the Plugin, but it's too early to display it. |
| 218 | if ( $start_displaying_review_at > time() ) { |
| 219 | return false; |
| 220 | } |
| 221 | |
| 222 | // The Plugin requested a review and it's time to display the notification. |
| 223 | return true; |
| 224 | |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Dismisses the review notification, so it isn't displayed again. |
| 229 | * |
| 230 | * @since 1.0.0 |
| 231 | */ |
| 232 | public function dismiss_review() { |
| 233 | |
| 234 | update_option( $this->plugin_slug . '-review-dismissed', 1 ); |
| 235 | |
| 236 | // Send success response if called via AJAX. |
| 237 | if ( function_exists( 'wp_doing_ajax' ) && wp_doing_ajax() ) { |
| 238 | wp_send_json_success( 1 ); |
| 239 | } |
| 240 | |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Flag to indicate whether a review request has been dismissed by the user. |
| 245 | * |
| 246 | * @since 1.0.0 |
| 247 | * |
| 248 | * @return bool Review Dismissed |
| 249 | */ |
| 250 | public function dismissed_review() { |
| 251 | |
| 252 | return get_option( $this->plugin_slug . '-review-dismissed' ); |
| 253 | |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Returns the Review URL for this Plugin. |
| 258 | * |
| 259 | * @since 1.0.0 |
| 260 | * |
| 261 | * @return string Review URL |
| 262 | */ |
| 263 | public function get_review_url() { |
| 264 | |
| 265 | return 'https://wordpress.org/support/plugin/' . $this->plugin_slug . '/reviews/?filter=5#new-post'; |
| 266 | |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Returns the Support URL for this Plugin. |
| 271 | * |
| 272 | * @since 1.0.0 |
| 273 | * |
| 274 | * @return string Review URL |
| 275 | */ |
| 276 | public function get_support_url() { |
| 277 | |
| 278 | return 'https://kit.com/support'; |
| 279 | |
| 280 | } |
| 281 | |
| 282 | } |
| 283 |