| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace RyanHellyer\UniqueHeaders; |
| 6 |
|
| 7 |
class DotorgPluginReview |
| 8 |
{ |
| 9 |
private string $slug; |
| 10 |
private string $name; |
| 11 |
private int $timeLimit; |
| 12 |
public string $nobugOption; |
| 13 |
|
| 14 |
/** |
| 15 |
* @param array{slug: string, name: string, time_limit?: int} $args |
| 16 |
*/ |
| 17 |
public function __construct(array $args) |
| 18 |
{ |
| 19 |
$this->slug = $args['slug']; |
| 20 |
$this->name = $args['name']; |
| 21 |
$this->timeLimit = $args['time_limit'] ?? WEEK_IN_SECONDS; |
| 22 |
$this->nobugOption = $this->slug . '-no-bug'; |
| 23 |
|
| 24 |
add_action('admin_init', [$this, 'checkInstallationDate']); |
| 25 |
add_action('admin_init', [$this, 'setNoBug'], 5); |
| 26 |
} |
| 27 |
|
| 28 |
public function secondsToWords(int $seconds): string |
| 29 |
{ |
| 30 |
$years = (int) round($seconds / YEAR_IN_SECONDS) % 100; |
| 31 |
if ($years > 1) { |
| 32 |
return sprintf(__('%s years', 'unique-headers'), $years); |
| 33 |
} |
| 34 |
if ($years > 0) { |
| 35 |
return __('a year', 'unique-headers'); |
| 36 |
} |
| 37 |
|
| 38 |
$weeks = (int) round($seconds / WEEK_IN_SECONDS) % 52; |
| 39 |
if ($weeks > 1) { |
| 40 |
return sprintf(__('%s weeks', 'unique-headers'), $weeks); |
| 41 |
} |
| 42 |
if ($weeks > 0) { |
| 43 |
return __('a week', 'unique-headers'); |
| 44 |
} |
| 45 |
|
| 46 |
$days = (int) round($seconds / DAY_IN_SECONDS) % 7; |
| 47 |
if ($days > 1) { |
| 48 |
return sprintf(__('%s days', 'unique-headers'), $days); |
| 49 |
} |
| 50 |
if ($days > 0) { |
| 51 |
return __('a day', 'unique-headers'); |
| 52 |
} |
| 53 |
|
| 54 |
$hours = (int) round($seconds / HOUR_IN_SECONDS) % 24; |
| 55 |
if ($hours > 1) { |
| 56 |
return sprintf(__('%s hours', 'unique-headers'), $hours); |
| 57 |
} |
| 58 |
if ($hours > 0) { |
| 59 |
return __('an hour', 'unique-headers'); |
| 60 |
} |
| 61 |
|
| 62 |
$minutes = (int) round($seconds / MINUTE_IN_SECONDS) % 60; |
| 63 |
if ($minutes > 1) { |
| 64 |
return sprintf(__('%s minutes', 'unique-headers'), $minutes); |
| 65 |
} |
| 66 |
if ($minutes > 0) { |
| 67 |
return __('a minute', 'unique-headers'); |
| 68 |
} |
| 69 |
|
| 70 |
$secs = (int) round($seconds) % 60; |
| 71 |
if ($secs > 1) { |
| 72 |
return sprintf(__('%s seconds', 'unique-headers'), $secs); |
| 73 |
} |
| 74 |
|
| 75 |
return __('a second', 'unique-headers'); |
| 76 |
} |
| 77 |
|
| 78 |
public function checkInstallationDate(): void |
| 79 |
{ |
| 80 |
if (get_site_option($this->nobugOption) === '1') { |
| 81 |
return; |
| 82 |
} |
| 83 |
|
| 84 |
$installDate = get_site_option($this->slug . '-activation-date'); |
| 85 |
if ($installDate === false) { |
| 86 |
add_site_option($this->slug . '-activation-date', time()); |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
$gap = time() - $installDate; |
| 91 |
if ($gap > $this->timeLimit) { |
| 92 |
add_action('admin_notices', [$this, 'displayAdminNotice']); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
public function displayAdminNotice(): void |
| 97 |
{ |
| 98 |
$screen = get_current_screen(); |
| 99 |
if (!isset($screen->base) || $screen->base !== 'plugins') { |
| 100 |
return; |
| 101 |
} |
| 102 |
|
| 103 |
$noBugUrl = wp_nonce_url( |
| 104 |
admin_url('?' . $this->nobugOption . '=true'), |
| 105 |
'review-nonce' |
| 106 |
); |
| 107 |
$time = $this->secondsToWords(time() - (int) get_site_option($this->slug . '-activation-date')); |
| 108 |
$reviewUrl = 'https://wordpress.org/support/view/plugin-reviews/' . $this->slug . '#postform'; |
| 109 |
|
| 110 |
?> |
| 111 |
<div class="updated"> |
| 112 |
<p> |
| 113 |
<?php |
| 114 |
echo sprintf( |
| 115 |
esc_html__( |
| 116 |
'You have been using the %1$s plugin for %2$s now, do you like it? If so, please leave us a review with your feedback!', // phpcs:ignore Generic.Files.LineLength |
| 117 |
'unique-headers' |
| 118 |
), |
| 119 |
esc_html($this->name), |
| 120 |
esc_html($time) |
| 121 |
); |
| 122 |
?> |
| 123 |
<br /><br /> |
| 124 |
<a onclick="location.href='<?php echo esc_url($noBugUrl); ?>';" |
| 125 |
class="button button-primary" |
| 126 |
href="<?php echo esc_url($reviewUrl); ?>" |
| 127 |
target="_blank"> |
| 128 |
<?php echo esc_html__('Leave A Review', 'unique-headers'); ?> |
| 129 |
</a> |
| 130 |
<a href="<?php echo esc_url($noBugUrl); ?>"> |
| 131 |
<?php echo esc_html__('No thanks.', 'unique-headers'); ?> |
| 132 |
</a> |
| 133 |
</p> |
| 134 |
</div> |
| 135 |
<?php |
| 136 |
} |
| 137 |
|
| 138 |
public function setNoBug(): void |
| 139 |
{ |
| 140 |
if ( |
| 141 |
!isset($_GET['_wpnonce']) |
| 142 |
|| !wp_verify_nonce( |
| 143 |
sanitize_key(wp_unslash($_GET['_wpnonce'])), |
| 144 |
'review-nonce' |
| 145 |
) |
| 146 |
|| !is_admin() |
| 147 |
|| !isset($_GET[$this->nobugOption]) |
| 148 |
|| !current_user_can('manage_options') |
| 149 |
) { |
| 150 |
return; |
| 151 |
} |
| 152 |
|
| 153 |
add_site_option($this->nobugOption, true); |
| 154 |
} |
| 155 |
} |
| 156 |
|