Reviews.php 13.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
<?php
/**
 * Plugin Review Controller Class.
 *
 * @package ContentControl
 */

namespace ContentControl\Controllers\Admin;

use ContentControl\Base\Controller;

use function ContentControl\plugin;

defined( 'ABSPATH' ) || exit;

/**
 * Class ContentControl\Admin\Reviews
 *
 * This class adds a review request system for your plugin or theme to the WP dashboard.
 *
 * @since 1.1.0
 */
class Reviews extends Controller {

	/**
	 * Enable debug mode.
	 *
	 * @var boolean
	 */
	private $debug = false;

	/**
	 * Debug trigger.
	 *
	 * @var array{group:string,code:string}
	 */
	private $debug_trigger = [
		'group' => 'time_installed',
		'code'  => 'one_week',
	];

	/**
	 * Tracking API Endpoint.
	 *
	 * @var string
	 */
	public static $api_url;

	/**
	 * Initialize review requests.
	 */
	public function init() {
		add_action( 'init', [ $this, 'hooks' ] );
		add_action( 'wp_ajax_content_control_review_action', [ $this, 'ajax_handler' ] );
	}

	/**
	 * Hook into relevant WP actions.
	 *
	 * @return void
	 */
	public function hooks() {
		if ( is_admin() && current_user_can( 'manage_options' ) ) {
			$this->installed_on();
			add_action( 'admin_notices', [ $this, 'admin_notices' ] );
			add_action( 'network_admin_notices', [ $this, 'admin_notices' ] );
			add_action( 'user_admin_notices', [ $this, 'admin_notices' ] );
		}
	}

	/**
	 * Get the install date for comparisons. Sets the date to now if none is found.
	 *
	 * @return false|string
	 */
	public function installed_on() {
		$installed_on = \get_option( 'content_control_installed_on', false );

		if ( ! $installed_on ) {
			$installed_on = current_time( 'mysql' );
			\update_option( 'content_control_installed_on', $installed_on );
		}

		return $installed_on;
	}

	/**
	 * AJAX Handler
	 *
	 * @return void
	 */
	public function ajax_handler() {
		// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
		if ( ! isset( $_REQUEST['nonce'] ) || ! wp_verify_nonce( wp_unslash( $_REQUEST['nonce'] ), 'content_control_review_action' ) ) {
			wp_send_json_error();
		}

		$args = wp_parse_args( $_REQUEST, [
			'group'  => $this->get_trigger_group(),
			'code'   => $this->get_trigger_code(),
			'pri'    => $this->get_current_trigger( 'pri' ),
			'reason' => 'maybe_later',
		] );

		try {
			$user_id = get_current_user_id();

			$dismissed_triggers                   = $this->dismissed_triggers();
			$dismissed_triggers[ $args['group'] ] = $args['pri'];
			update_user_meta( $user_id, 'content_control_reviews_dismissed_triggers', $dismissed_triggers );
			update_user_meta( $user_id, 'content_control_reviews_last_dismissed', current_time( 'mysql' ) );

			switch ( $args['reason'] ) {
				case 'maybe_later':
					update_user_meta( $user_id, 'content_control_reviews_last_dismissed', current_time( 'mysql' ) );
					break;
				case 'am_now':
				case 'already_did':
					$this->already_did( true );
					break;
				default:
					// Do nothing if the reason value does not match one of ours.
			}

			wp_send_json_success();
		} catch ( \Exception $e ) {
			wp_send_json_error( $e );
		}
	}

	/**
	 * Get the current trigger group and code.
	 *
	 * @return int|string
	 */
	public function get_trigger_group() {
		static $selected;

		if ( $this->debug ) {
			return ! empty( $this->debug_trigger['group'] ) ? $this->debug_trigger['group'] : 'time_installed';
		}

		if ( ! isset( $selected ) ) {
			$dismissed_triggers = $this->dismissed_triggers();

			$triggers = $this->triggers();

			foreach ( $triggers as $g => $group ) {
				foreach ( $group['triggers'] as $t => $trigger ) {
					if ( ! in_array( false, $trigger['conditions'], true ) && ( empty( $dismissed_triggers[ $g ] ) || $dismissed_triggers[ $g ] < $trigger['pri'] ) ) {
						$selected = $g;
						break;
					}
				}

				if ( isset( $selected ) ) {
					break;
				}
			}
		}

		return $selected;
	}

	/**
	 * Get the current trigger group and code.
	 *
	 * @return int|string
	 */
	public function get_trigger_code() {
		static $selected;

		if ( $this->debug ) {
			return ! empty( $this->debug_trigger['code'] ) ? $this->debug_trigger['code'] : 'one_week';
		}

		if ( ! isset( $selected ) ) {
			$dismissed_triggers = $this->dismissed_triggers();

			foreach ( $this->triggers() as $g => $group ) {
				foreach ( $group['triggers'] as $t => $trigger ) {
					if ( ! in_array( false, $trigger['conditions'], true ) && ( empty( $dismissed_triggers[ $g ] ) || $dismissed_triggers[ $g ] < $trigger['pri'] ) ) {
						$selected = $t;
						break;
					}
				}

				if ( isset( $selected ) ) {
					break;
				}
			}
		}

		return $selected;
	}

	/**
	 * Get the current trigger.
	 *
	 * @param string $key Optional. Key to return from the trigger array.
	 *
	 * @return bool|mixed
	 */
	public function get_current_trigger( $key = null ) {
		$group = $this->get_trigger_group();
		$code  = $this->get_trigger_code();

		if ( ! $group || ! $code ) {
			return false;
		}

		$trigger = $this->triggers( $group, $code );

		if ( empty( $key ) ) {
			return $trigger;
		} else {
			return isset( $trigger[ $key ] ) ? $trigger[ $key ] : false;
		}
	}

	/**
	 * Returns an array of dismissed trigger groups.
	 *
	 * Array contains the group key and highest priority trigger that has been shown previously for each group.
	 *
	 * $return = array(
	 *   'group1' => 20
	 * );
	 *
	 * @return array|mixed
	 */
	public function dismissed_triggers() {
		$user_id = get_current_user_id();

		$dismissed_triggers = get_user_meta( $user_id, 'content_control_reviews_dismissed_triggers', true );

		if ( ! $dismissed_triggers ) {
			$dismissed_triggers = [];
		}

		return $dismissed_triggers;
	}

	/**
	 * Returns true if the user has opted to never see this again. Or sets the option.
	 *
	 * @param bool $set If set this will mark the user as having opted to never see this again.
	 *
	 * @return bool
	 */
	public function already_did( $set = false ) {
		$user_id = get_current_user_id();

		if ( $set ) {
			update_user_meta( $user_id, '_content_control_reviews_already_did', true );

			return true;
		}

		return (bool) get_user_meta( $user_id, '_content_control_reviews_already_did', true );
	}

	/**
	 * Gets a list of triggers.
	 *
	 * @param string $group Trigger group.
	 * @param string $code Trigger code.
	 *
	 * @return bool|mixed
	 */
	public function triggers( $group = null, $code = null ) {
		static $triggers;

		if ( ! isset( $triggers ) ) {
			$link = 'https://wordpress.org/support/plugin/content-control/reviews/?rate=5#rate-response';

			// Translators: 1. emoji, 2. html tag, 3. html tag, 4. the number of days, 5. html tag, 6. html tag.
			$time_message = __( 'Hi there! %1$s You\'ve been using the %2$sContent Control%3$s plugin on your site for %4$s now - We hope it\'s been helpful. If you\'re enjoying the plugin, would you mind rating it %5$s5-stars%6$s to help spread the word?', 'content-control' );
			$triggers     = [
				'time_installed' => [
					'triggers' => [
						'one_week'     => [
							'message'    => sprintf(
								$time_message,
								'👋',
								'<strong>',
								'</strong>',
								__( '1 week', 'content-control' ),
								'<span class="five-stars" title="',
								'"></span>'
							),
							'conditions' => [
								strtotime( $this->installed_on() . ' +1 week' ) < time(),
							],
							'link'       => $link,
							'pri'        => 10,
						],
						'one_month'    => [
							'message'    => sprintf(
								$time_message,
								'👋',
								'<strong>',
								'</strong>',
								__( '1 month', 'content-control' ),
								'<span class="five-stars" title="',
								'"></span>'
							),
							'conditions' => [
								strtotime( $this->installed_on() . ' +1 month' ) < time(),
							],
							'link'       => $link,
							'pri'        => 20,
						],
						'three_months' => [
							'message'    => sprintf(
								$time_message,
								'👋',
								'<strong>',
								'</strong>',
								__( '3 months', 'content-control' ),
								'<span class="five-stars" title="',
								'"></span>'
							),
							'conditions' => [
								strtotime( $this->installed_on() . ' +3 months' ) < time(),
							],
							'link'       => $link,
							'pri'        => 30,
						],

					],
					'pri'      => 10,
				],
			];

			$triggers = apply_filters( 'content_control_reviews_triggers', $triggers );

			// Sort Groups.
			uasort( $triggers, [ $this, 'rsort_by_priority' ] );

			// Sort each groups triggers.
			foreach ( $triggers as $k => $v ) {
				uasort( $triggers[ $k ]['triggers'], [ $this, 'rsort_by_priority' ] );
			}
		}

		if ( isset( $group ) ) {
			if ( ! isset( $triggers[ $group ] ) ) {
				return false;
			}

			if ( ! isset( $code ) ) {
				return $triggers[ $group ];
			} else {
				return isset( $triggers[ $group ]['triggers'][ $code ] ) ? $triggers[ $group ]['triggers'][ $code ] : false;
			}
		}

		return $triggers;
	}

	/**
	 * Render admin notices if available.
	 *
	 * @return void
	 */
	public function admin_notices() {
		if ( $this->hide_notices() ) {
			return;
		}

		$group   = $this->get_trigger_group();
		$code    = $this->get_trigger_code();
		$pri     = $this->get_current_trigger( 'pri' );
		$trigger = $this->get_current_trigger();

		// Used to anonymously distinguish unique site+user combinations in terms of effectiveness of each trigger.
		$uuid = wp_hash( home_url() . '-' . get_current_user_id() );

		?>

		<script type="text/javascript">
			(function ($) {
				var trigger = {
					group: '<?php echo esc_js( $group ); ?>',
					code: '<?php echo esc_js( $code ); ?>',
					pri: '<?php echo esc_js( $pri ); ?>'
				};

				function dismiss(reason) {
					$.ajax({
						method: "POST",
						dataType: "json",
						url: ajaxurl,
						data: {
							action: 'content_control_review_action',
							nonce: '<?php echo esc_js( wp_create_nonce( 'content_control_review_action' ) ); ?>',
							group: trigger.group,
							code: trigger.code,
							pri: trigger.pri,
							reason: reason
						}
					});

					<?php if ( ! empty( $this->api_url ) ) : ?>
					$.ajax({
						method: "POST",
						dataType: "json",
						url: '<?php echo esc_js( $this->api_url ); ?>',
						data: {
							trigger_group: trigger.group,
							trigger_code: trigger.code,
							reason: reason,
							uuid: '<?php echo esc_js( $uuid ); ?>'
						}
					});
					<?php endif; ?>
				}

				$(document)
					.on('click', '.content-control-notice .content-control-dismiss', function (event) {
						var $this = $(this),
							reason = $this.data('reason'),
							notice = $this.parents('.content-control-notice');

						notice.fadeTo(100, 0, function () {
							notice.slideUp(100, function () {
								notice.remove();
							});
						});

						dismiss(reason);
					})
					.ready(function () {
						setTimeout(function () {
							$('.content-control-notice button.notice-dismiss').click(function (event) {
								dismiss('maybe_later');
							});
						}, 1000);
					});
			}(jQuery));
		</script>

		<style>
			.content-control-notice {
				display: flex;
				align-items: center;
				gap: 16px;
				padding: 8px;
				margin-top: 16px;
				margin-bottom: 16px;
			}

			.content-control-notice .notice-logo {
				flex: 0 0 110px;
				max-width: 110px;
			}

			.content-control-notice .notice-content {
				flex-grow: 1;
			}

			.content-control-notice p {
				margin-bottom: 0;
				max-width: 800px;
			}

			.content-control-notice .review-actions {
				margin-top: 10px;
				margin-bottom: 0;
				padding-left: 0;
				list-style: none;

				display: flex;
				gap: 16px;
				align-items: center;
			}
			.content-control-notice .five-stars::before {
				content: "★★★★★";
				color: #f0ad4e;
			}
		</style>

		<div class="notice notice-success is-dismissible content-control-notice">

			<div class="notice-logo">
				<img class="logo" width="110" src="<?php echo esc_attr( plugin()->get_url( 'assets/images/illustration-check.svg' ) ); ?>" />
			</div>

			<div class="notice-content">
				<p>
					<?php
					// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
					echo $trigger['message'];
					?>
					~ <a target="_blank" href="https://twitter.com/danieliser" title="Follow Daniel on Twitter">@danieliser</a>
				</p>
				<ul class="review-actions">
					<li>😁
						<a class="content-control-dismiss" target="_blank" href="<?php echo esc_attr( $trigger['link'] ); ?>" data-reason="am_now">
							<strong><?php esc_html_e( 'Ok, you deserve it', 'content-control' ); ?></strong>
						</a>
					</li>
					<li>
						🤔
						<a href="#" class="content-control-dismiss" data-reason="maybe_later">
							<?php esc_html_e( 'Maybe later, okay?', 'content-control' ); ?>
						</a>
					</li>
					<li>🙌
						<a href="#" class="content-control-dismiss" data-reason="already_did">
							<?php esc_html_e( 'I already did', 'content-control' ); ?>
						</a>
					</li>
				</ul>

			</div>

		</div>

		<?php
	}

	/**
	 * Checks if notices should be shown.
	 *
	 * @return bool
	 */
	public function hide_notices() {
		if ( $this->debug ) {
			return false;
		}

		$code = $this->get_trigger_code();

		$conditions = [
			$this->already_did(),
			$this->last_dismissed() && strtotime( $this->last_dismissed() . ' +2 weeks' ) > time(),
			empty( $code ),
		];

		return in_array( true, $conditions, true );
	}

	/**
	 * Gets the last dismissed date.
	 *
	 * @return false|string
	 */
	public function last_dismissed() {
		$user_id = get_current_user_id();

		return get_user_meta( $user_id, 'content_control_reviews_last_dismissed', true );
	}

	/**
	 * Sort array in reverse by priority value
	 *
	 * @param array{pri:int|null} $a First array to compare.
	 * @param array{pri:int|null} $b Second array to compare.
	 *
	 * @return int
	 */
	public function rsort_by_priority( $a, $b ) {
		if ( ! isset( $a['pri'] ) || ! isset( $b['pri'] ) || $a['pri'] === $b['pri'] ) {
			return 0;
		}

		return ( $a['pri'] < $b['pri'] ) ? 1 : - 1;
	}
}