| 1 |
<?php |
| 2 |
|
| 3 |
namespace Allex\Module; |
| 4 |
|
| 5 |
use Allex\Container; |
| 6 |
|
| 7 |
class Reviews extends Abstract_Module |
| 8 |
{ |
| 9 |
const REPEAT_INTERVAL = '+30 days'; |
| 10 |
|
| 11 |
/** |
| 12 |
* @var mixed |
| 13 |
*/ |
| 14 |
protected $twig; |
| 15 |
|
| 16 |
/** |
| 17 |
* @var string |
| 18 |
*/ |
| 19 |
protected $plugin_name; |
| 20 |
|
| 21 |
/** |
| 22 |
* @var string |
| 23 |
*/ |
| 24 |
protected $option_name_status; |
| 25 |
|
| 26 |
/** |
| 27 |
* @var string |
| 28 |
*/ |
| 29 |
protected $option_name_timestamp; |
| 30 |
|
| 31 |
/** |
| 32 |
* @var string |
| 33 |
*/ |
| 34 |
protected $notice_text; |
| 35 |
|
| 36 |
/** |
| 37 |
* @var string |
| 38 |
*/ |
| 39 |
protected $review_link; |
| 40 |
|
| 41 |
/** |
| 42 |
* Reviews constructor. |
| 43 |
* |
| 44 |
* @param Container $container |
| 45 |
*/ |
| 46 |
public function __construct(Container $container) |
| 47 |
{ |
| 48 |
parent::__construct($container); |
| 49 |
|
| 50 |
$this->twig = $this->container['twig']; |
| 51 |
$this->plugin_name = $this->container['PLUGIN_NAME']; |
| 52 |
$this->option_name_status = $this->plugin_name . '_review_status'; |
| 53 |
$this->option_name_timestamp = $this->plugin_name . '_review_timestamp'; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* $params = [ |
| 58 |
* 'redirect_url' => '', |
| 59 |
* 'review_link' => '', |
| 60 |
* 'notice_text' => '', |
| 61 |
* ]; |
| 62 |
* |
| 63 |
* @param array $params |
| 64 |
*/ |
| 65 |
public function init($params) |
| 66 |
{ |
| 67 |
$this->notice_text = esc_html($params['notice_text']); |
| 68 |
$this->review_link = $params['review_link']; |
| 69 |
|
| 70 |
$this->handle_request_actions($params['redirect_url']); |
| 71 |
|
| 72 |
if ($this->should_display_notice()) { |
| 73 |
$this->add_action_admin_notice(); |
| 74 |
$this->reset_options(); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Handles actions triggered by the links in the notification. |
| 80 |
* |
| 81 |
* @param string $redirect_url |
| 82 |
*/ |
| 83 |
protected function handle_request_actions($redirect_url) |
| 84 |
{ |
| 85 |
// We only check GET requests |
| 86 |
if ($_SERVER['REQUEST_METHOD'] !== 'GET') { |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
if (defined('DOING_AJAX') && DOING_AJAX) { |
| 91 |
return; |
| 92 |
} |
| 93 |
|
| 94 |
if (defined('DOING_CRON') && DOING_CRON) { |
| 95 |
return; |
| 96 |
} |
| 97 |
|
| 98 |
// Check if the request is related to the current plugin. |
| 99 |
if (!isset($_GET['review_plugin']) |
| 100 |
|| $_GET['review_plugin'] != $this->plugin_name) { |
| 101 |
return; |
| 102 |
} |
| 103 |
|
| 104 |
// Check if the URL is related to any action |
| 105 |
$valid_actions = ['no', 'done']; |
| 106 |
if (!isset($_GET['review_action']) |
| 107 |
|| !in_array($_GET['review_action'], $valid_actions)) { |
| 108 |
return; |
| 109 |
} |
| 110 |
|
| 111 |
$this->store_state_status($_GET['review_action']); |
| 112 |
|
| 113 |
// Store the date for asking for the review again later. |
| 114 |
if ($_GET['review_action'] === 'no') { |
| 115 |
$this->store_state_timestamp(date('Y-m-d')); |
| 116 |
} |
| 117 |
|
| 118 |
// Adapt redirect argument passed from js. |
| 119 |
if (false === strpos($redirect_url, '.php')) { |
| 120 |
$redirect_url = "admin.php?page=$redirect_url"; |
| 121 |
} |
| 122 |
|
| 123 |
$this->redirect($redirect_url); |
| 124 |
|
| 125 |
exit; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* @param string $state |
| 130 |
*/ |
| 131 |
protected function store_state_status($status) |
| 132 |
{ |
| 133 |
update_option($this->option_name_status, $status, true); |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* @param string $timestamp |
| 138 |
*/ |
| 139 |
protected function store_state_timestamp($timestamp) |
| 140 |
{ |
| 141 |
update_option($this->option_name_timestamp, $timestamp, true); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* @param $url |
| 146 |
*/ |
| 147 |
protected function redirect($url) |
| 148 |
{ |
| 149 |
wp_redirect(admin_url($url)); |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* @return bool |
| 154 |
*/ |
| 155 |
protected function should_display_notice() |
| 156 |
{ |
| 157 |
if (!is_admin()) { |
| 158 |
return false; |
| 159 |
} |
| 160 |
|
| 161 |
if (defined('DOING_AJAX') && DOING_AJAX) { |
| 162 |
return false; |
| 163 |
} |
| 164 |
|
| 165 |
if (defined('DOING_CRON') && DOING_CRON) { |
| 166 |
return false; |
| 167 |
} |
| 168 |
|
| 169 |
// Check if the user answered "done". |
| 170 |
$status = $this->get_state_status(); |
| 171 |
|
| 172 |
if ($status === 'done') { |
| 173 |
return false; |
| 174 |
} |
| 175 |
|
| 176 |
// Check when the user answered no. We will ask again after a few days, if he still uses the plugin. |
| 177 |
if ($status === 'no') { |
| 178 |
$date = $this->get_state_timestamp(date('Y-m-d')); |
| 179 |
$date = strtotime($date . ' ' . self::REPEAT_INTERVAL); |
| 180 |
$today = strtotime(date('Y-m-d')); |
| 181 |
|
| 182 |
return $date <= $today; |
| 183 |
} |
| 184 |
|
| 185 |
return true; |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* @param string $default |
| 190 |
* |
| 191 |
* @return mixed|void |
| 192 |
*/ |
| 193 |
protected function get_state_status($default = '') |
| 194 |
{ |
| 195 |
return get_option($this->option_name_status, $default); |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* @param string $default |
| 200 |
* |
| 201 |
* @return mixed|void |
| 202 |
*/ |
| 203 |
protected function get_state_timestamp($default = '') |
| 204 |
{ |
| 205 |
return get_option($this->option_name_timestamp, $default); |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* |
| 210 |
*/ |
| 211 |
protected function add_action_admin_notice() |
| 212 |
{ |
| 213 |
add_action('admin_notices', [$this, 'render_notice']); |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* Reset the options. |
| 218 |
*/ |
| 219 |
protected function reset_options() |
| 220 |
{ |
| 221 |
delete_option($this->option_name_status); |
| 222 |
delete_option($this->option_name_timestamp); |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* |
| 227 |
* @param string $message |
| 228 |
* |
| 229 |
* @return mixed |
| 230 |
*/ |
| 231 |
public function render_notice() |
| 232 |
{ |
| 233 |
$context = [ |
| 234 |
'message' => esc_html($this->notice_text), |
| 235 |
'links' => [ |
| 236 |
'yes' => $this->review_link, |
| 237 |
'done' => admin_url("?review_plugin={$this->plugin_name}&review_action=done"), |
| 238 |
'no' => admin_url("?review_plugin={$this->plugin_name}&review_action=no"), |
| 239 |
], |
| 240 |
'labels' => [ |
| 241 |
'ok' => __('Ok, you deserve it', 'allex'), |
| 242 |
'done' => __('I already did', 'allex'), |
| 243 |
'no' => __('No, not good enough for now', 'allex'), |
| 244 |
], |
| 245 |
]; |
| 246 |
|
| 247 |
echo $this->twig->render('notice_five_star_review.twig', $context); |
| 248 |
} |
| 249 |
} |
| 250 |
|