| 1 |
<?php |
| 2 |
/** |
| 3 |
* QuadLayers WP Notice Plugin Promote |
| 4 |
* |
| 5 |
* @package quadlayers/wp-notice-plugin-promote |
| 6 |
* @author QuadLayers |
| 7 |
* @link https://github.com/quadlayers/wp-notice-plugin-promote |
| 8 |
* @copyright Copyright (c) 2023 |
| 9 |
* @license GPL-3.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace QuadLayers\WP_Notice_Plugin_Promote; |
| 13 |
|
| 14 |
/** |
| 15 |
* Class Load |
| 16 |
* |
| 17 |
* @package QuadLayers\WP_Notice_Plugin_Promote |
| 18 |
*/ |
| 19 |
class Load { |
| 20 |
|
| 21 |
/** |
| 22 |
* Developer Mode. |
| 23 |
* |
| 24 |
* @var bool |
| 25 |
*/ |
| 26 |
protected $developer_mode = false; |
| 27 |
|
| 28 |
/** |
| 29 |
* Notices to display. |
| 30 |
* |
| 31 |
* @var array |
| 32 |
*/ |
| 33 |
protected $notices = array(); |
| 34 |
/** |
| 35 |
* Current Plugin File. |
| 36 |
* |
| 37 |
* @var string |
| 38 |
*/ |
| 39 |
protected $current_plugin_file; |
| 40 |
/** |
| 41 |
* Current Plugin. |
| 42 |
* |
| 43 |
* @var PluginByFile |
| 44 |
*/ |
| 45 |
protected $current_plugin; |
| 46 |
|
| 47 |
/** |
| 48 |
* Constructor. |
| 49 |
* |
| 50 |
* @param string $current_plugin_file Current plugin file. |
| 51 |
* @param array $notices Notices to display. |
| 52 |
* @param boolean $developer_mode Developer mode. |
| 53 |
*/ |
| 54 |
public function __construct( string $current_plugin_file, array $notices = array(), bool $developer_mode = false ) { |
| 55 |
/** |
| 56 |
* Only show notices in admin panel. |
| 57 |
*/ |
| 58 |
if ( ! is_admin() ) { |
| 59 |
return; |
| 60 |
} |
| 61 |
$this->developer_mode = $developer_mode; |
| 62 |
/** |
| 63 |
* Set developer mode. |
| 64 |
*/ |
| 65 |
/** |
| 66 |
* Get current plugin by file. |
| 67 |
*/ |
| 68 |
$this->current_plugin = new PluginByFile( $current_plugin_file ); |
| 69 |
/** |
| 70 |
* Only show notices if current plugin file is valid. |
| 71 |
*/ |
| 72 |
if ( ! $this->current_plugin->is_valid() ) { |
| 73 |
return; |
| 74 |
} |
| 75 |
/** |
| 76 |
* Set notices. |
| 77 |
*/ |
| 78 |
$this->notices = $notices; |
| 79 |
/** |
| 80 |
* Create transient on plugin activation to delay display notices. |
| 81 |
*/ |
| 82 |
register_activation_hook( |
| 83 |
$this->current_plugin->get_file(), |
| 84 |
function () { |
| 85 |
$notice_delay = MONTH_IN_SECONDS; |
| 86 |
if ( isset( $this->notices[0]['notice_delay'] ) ) { |
| 87 |
$notice_delay = $this->notices[0]['notice_delay']; |
| 88 |
} |
| 89 |
$this->delay_display_notices( $notice_delay ); |
| 90 |
} |
| 91 |
); |
| 92 |
/** |
| 93 |
* Add action to display notices. |
| 94 |
*/ |
| 95 |
add_action( 'admin_notices', array( $this, 'admin_notices' ) ); |
| 96 |
/** |
| 97 |
* Add action to dismiss notice by id. |
| 98 |
*/ |
| 99 |
add_action( 'wp_ajax_' . $this->get_action_name(), array( $this, 'ajax_notice_plugin_promote_dismiss' ) ); |
| 100 |
/** |
| 101 |
* Remove all data |
| 102 |
*/ |
| 103 |
if ( $this->developer_mode ) { |
| 104 |
add_action( 'admin_notices', array( $this, 'remove_all_data' ) ); |
| 105 |
} |
| 106 |
} |
| 107 |
/** |
| 108 |
* Create transient on plugin activation to delay notice one month. |
| 109 |
* |
| 110 |
* @param int $notice_delay Delay in seconds. |
| 111 |
* @return void |
| 112 |
*/ |
| 113 |
public function delay_display_notices( int $notice_delay = MONTH_IN_SECONDS ) { |
| 114 |
if ( $notice_delay === 0 ) { |
| 115 |
return; |
| 116 |
} |
| 117 |
set_transient( $this->get_transient_key(), true, $notice_delay ); |
| 118 |
} |
| 119 |
/** |
| 120 |
* Action admin notices hook. |
| 121 |
* |
| 122 |
* @return void |
| 123 |
*/ |
| 124 |
public function admin_notices() { |
| 125 |
|
| 126 |
$screen = get_current_screen(); |
| 127 |
|
| 128 |
if ( isset( $screen->parent_file ) && 'plugins.php' === $screen->parent_file && 'update' === $screen->id ) { |
| 129 |
return; |
| 130 |
} |
| 131 |
/** |
| 132 |
* Check if notices are delayed by the transient created on plugin activation. |
| 133 |
*/ |
| 134 |
$is_display_notices_delayed = $this->is_display_notices_delayed(); |
| 135 |
|
| 136 |
if ( $is_display_notices_delayed ) { |
| 137 |
return; |
| 138 |
} |
| 139 |
/** |
| 140 |
* Add script to dismiss notice via ajax. |
| 141 |
*/ |
| 142 |
?> |
| 143 |
<script> |
| 144 |
(function($) { |
| 145 |
|
| 146 |
function urlSeachParams(href) { |
| 147 |
var url = new URL(href); |
| 148 |
var params = new URLSearchParams(url.search); |
| 149 |
return params; |
| 150 |
} |
| 151 |
|
| 152 |
function ajaxCall(notice_index){ |
| 153 |
$.ajax({ |
| 154 |
type: 'POST', |
| 155 |
url: ajaxurl, |
| 156 |
data: { |
| 157 |
notice_index: notice_index, |
| 158 |
action: '<?php echo esc_attr( $this->get_action_name() ); ?>', |
| 159 |
nonce: '<?php echo esc_attr( wp_create_nonce( $this->get_action_name() ) ); ?>' |
| 160 |
}, |
| 161 |
success: function(response) { |
| 162 |
console.log(response); |
| 163 |
}, |
| 164 |
}); |
| 165 |
} |
| 166 |
|
| 167 |
$(document).ready(()=> { |
| 168 |
|
| 169 |
$('.<?php echo esc_attr( $this->get_transient_key() ); ?>').on('click', '.notice-dismiss', function(e) { |
| 170 |
e.preventDefault(); |
| 171 |
var notice_index = $(e.delegateTarget).data('notice_index'); |
| 172 |
ajaxCall(notice_index) |
| 173 |
}); |
| 174 |
$('.<?php echo esc_attr( $this->get_transient_key() ); ?>').on('click', '.link-dismiss', function(e) { |
| 175 |
e.preventDefault(); |
| 176 |
var notice_index = $(e.delegateTarget).data('notice_index'); |
| 177 |
ajaxCall(notice_index) |
| 178 |
$(e.delegateTarget).remove(); |
| 179 |
}); |
| 180 |
$('.<?php echo esc_attr( $this->get_transient_key() ); ?>').on('click', '.button-primary', function(e) { |
| 181 |
var href = $(e.target).attr('href'); |
| 182 |
var hrefParams = new urlSeachParams(href); |
| 183 |
var hrefAction = hrefParams.get('action'); |
| 184 |
if( 'activate' !== hrefAction) { |
| 185 |
return; |
| 186 |
} |
| 187 |
var notice_index = $(e.delegateTarget).data('notice_index'); |
| 188 |
ajaxCall(notice_index) |
| 189 |
|
| 190 |
}); |
| 191 |
}) |
| 192 |
})(jQuery); |
| 193 |
</script> |
| 194 |
<?php |
| 195 |
|
| 196 |
/** |
| 197 |
* Loop through notices. |
| 198 |
*/ |
| 199 |
foreach ( $this->notices as $notice_index => $notice ) { |
| 200 |
/** |
| 201 |
* Check if notice is valid. |
| 202 |
*/ |
| 203 |
$is_valid_notice = $this->is_valid_notice( $notice ); |
| 204 |
|
| 205 |
if ( ! $is_valid_notice ) { |
| 206 |
continue; |
| 207 |
} |
| 208 |
/** |
| 209 |
* Check if notice is hidden for current user base on user meta. |
| 210 |
*/ |
| 211 |
$is_notice_hidden_for_current_user = $this->is_notice_hidden_for_current_user( $notice_index ); |
| 212 |
|
| 213 |
if ( $is_notice_hidden_for_current_user ) { |
| 214 |
continue; |
| 215 |
} |
| 216 |
/** |
| 217 |
* Include notice template. |
| 218 |
*/ |
| 219 |
$this->include_notice_template( $notice, $notice_index ); |
| 220 |
/** |
| 221 |
* Display one notice at a time. |
| 222 |
*/ |
| 223 |
if ( ! $this->developer_mode ) { |
| 224 |
return; |
| 225 |
} |
| 226 |
} |
| 227 |
} |
| 228 |
/** |
| 229 |
* Create transient key. |
| 230 |
* |
| 231 |
* @return string |
| 232 |
*/ |
| 233 |
private function get_transient_key() { |
| 234 |
return 'quadlayers_' . $this->current_plugin->get_slug() . '_notice_delay'; |
| 235 |
} |
| 236 |
|
| 237 |
/** |
| 238 |
* Create action name. |
| 239 |
* |
| 240 |
* @return string |
| 241 |
*/ |
| 242 |
private function get_action_name() { |
| 243 |
return 'quadlayers_' . $this->current_plugin->get_slug() . '_notice_close'; |
| 244 |
} |
| 245 |
|
| 246 |
/** |
| 247 |
* Create user meta key. |
| 248 |
* |
| 249 |
* @param int $notice_index Notice index. |
| 250 |
* @return string |
| 251 |
*/ |
| 252 |
private function get_user_notice_meta_hidden_key( int $notice_index ) { |
| 253 |
return 'quadlayers_' . $this->current_plugin->get_slug() . '_notice_hidden_' . $notice_index; |
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* Create class to link notice to dismiss ajax action. |
| 258 |
* |
| 259 |
* @return string |
| 260 |
*/ |
| 261 |
private function get_notices_class() { |
| 262 |
return $this->get_transient_key(); |
| 263 |
} |
| 264 |
|
| 265 |
/** |
| 266 |
* Get user meta hidden notice. |
| 267 |
* |
| 268 |
* @param int $notice_index Notice index. |
| 269 |
* @return boolean |
| 270 |
*/ |
| 271 |
private function get_current_user_notice_meta_hidden( int $notice_index ) { |
| 272 |
return get_user_meta( get_current_user_id(), $this->get_user_notice_meta_hidden_key( $notice_index ), true ); |
| 273 |
} |
| 274 |
|
| 275 |
/** |
| 276 |
* Delete user meta hidden notice. |
| 277 |
* |
| 278 |
* @param int $notice_index Notice. |
| 279 |
* @return boolean |
| 280 |
*/ |
| 281 |
private function delete_current_user_notice_meta_hidden( int $notice_index ) { |
| 282 |
return delete_user_meta( get_current_user_id(), $this->get_user_notice_meta_hidden_key( $notice_index ), false ); |
| 283 |
} |
| 284 |
|
| 285 |
/** |
| 286 |
* Set user meta hidden notice. |
| 287 |
* |
| 288 |
* @param int $notice_index Notice index. |
| 289 |
* @return boolean |
| 290 |
*/ |
| 291 |
private function set_current_user_notice_meta_hidden( int $notice_index ) { |
| 292 |
return update_user_meta( get_current_user_id(), $this->get_user_notice_meta_hidden_key( $notice_index ), true ); |
| 293 |
} |
| 294 |
|
| 295 |
/** |
| 296 |
* Reset notice transient dismiss to on month. |
| 297 |
* |
| 298 |
* @return void |
| 299 |
*/ |
| 300 |
public function ajax_notice_plugin_promote_dismiss() { |
| 301 |
if ( isset( $_REQUEST['notice_index'] ) && check_admin_referer( $this->get_action_name(), 'nonce' ) ) { |
| 302 |
$notice_index = sanitize_key( $_REQUEST['notice_index'] ); |
| 303 |
$this->set_current_user_notice_meta_hidden( $notice_index ); |
| 304 |
if ( ! isset( $this->notices[ $notice_index ] ) ) { |
| 305 |
wp_send_json_error( sprintf( esc_html__( 'Unknow notice index %s', 'wp-notice-plugin-promote' ), $notice_index ) ); |
| 306 |
} |
| 307 |
$next_notice_delay = isset( $this->notices[ $notice_index + 1 ]['notice_delay'] ) ? $this->notices[ $notice_index + 1 ]['notice_delay'] : MONTH_IN_SECONDS; |
| 308 |
$this->delay_display_notices( $next_notice_delay ); |
| 309 |
wp_send_json_success( sprintf( esc_html__( 'Notice index %s removed', 'wp-notice-plugin-promote' ), $notice_index ) ); |
| 310 |
} |
| 311 |
wp_die(); |
| 312 |
} |
| 313 |
|
| 314 |
/** |
| 315 |
* Check if notice is valid based on user meta. |
| 316 |
* |
| 317 |
* @param int $notice_index Notice index. |
| 318 |
* @return boolean |
| 319 |
*/ |
| 320 |
private function is_notice_hidden_for_current_user( int $notice_index ) { |
| 321 |
if ( $this->developer_mode ) { |
| 322 |
return false; |
| 323 |
} |
| 324 |
return (bool) $this->get_current_user_notice_meta_hidden( $notice_index ); |
| 325 |
} |
| 326 |
|
| 327 |
/** |
| 328 |
* Check if notice is valid. |
| 329 |
* |
| 330 |
* @param array $notice Notice data. |
| 331 |
* @return boolean |
| 332 |
*/ |
| 333 |
private function is_valid_notice( array $notice ) { |
| 334 |
/** |
| 335 |
* Return true if notice dose not have plugin slug. |
| 336 |
*/ |
| 337 |
if ( ! isset( $notice['plugin_slug'] ) ) { |
| 338 |
return true; |
| 339 |
} |
| 340 |
$plugin = new PluginBySlugV2( $notice ); |
| 341 |
/** |
| 342 |
* Return true if plugin is not activated. |
| 343 |
*/ |
| 344 |
if ( ! $plugin->is_activated() ) { |
| 345 |
return true; |
| 346 |
} |
| 347 |
|
| 348 |
return false; |
| 349 |
} |
| 350 |
|
| 351 |
/** |
| 352 |
* Check if notices are delayed based on plugin transient. |
| 353 |
* |
| 354 |
* @return boolean |
| 355 |
*/ |
| 356 |
private function is_display_notices_delayed() { |
| 357 |
if ( $this->developer_mode ) { |
| 358 |
return false; |
| 359 |
} |
| 360 |
|
| 361 |
return get_transient( $this->get_transient_key() ); |
| 362 |
} |
| 363 |
|
| 364 |
/** |
| 365 |
* Get notice template. |
| 366 |
* |
| 367 |
* @param array $notice Notice data. |
| 368 |
* @param int $notice_index Notice index. |
| 369 |
* @return void |
| 370 |
*/ |
| 371 |
private function include_notice_template( array $notice, int $notice_index ) { |
| 372 |
|
| 373 |
$template_path = __DIR__ . '/templates/notice.php'; |
| 374 |
|
| 375 |
$notices_class = $this->get_notices_class(); |
| 376 |
|
| 377 |
/** |
| 378 |
* Add action and action link to notice if plugin slug is set. |
| 379 |
*/ |
| 380 |
if ( isset( $notice['plugin_slug'] ) ) { |
| 381 |
$plugin = new PluginBySlugV2( $notice ); |
| 382 |
$notice = array_merge( |
| 383 |
$notice, |
| 384 |
array( |
| 385 |
'action' => $plugin->get_action_label(), |
| 386 |
'action_link' => $plugin->get_action_link(), |
| 387 |
) |
| 388 |
); |
| 389 |
} |
| 390 |
|
| 391 |
/** |
| 392 |
* Add default label. |
| 393 |
*/ |
| 394 |
if ( isset( $notice['notice_link'] ) && empty( $notice['notice_link_label'] ) ) { |
| 395 |
$notice = array_merge( |
| 396 |
$notice, |
| 397 |
array( |
| 398 |
'notice_link_label' => esc_html__( 'Review Here', 'wp-notice-plugin-promote' ), |
| 399 |
) |
| 400 |
); |
| 401 |
} |
| 402 |
|
| 403 |
/** |
| 404 |
* Add default label. |
| 405 |
*/ |
| 406 |
if ( isset( $notice['notice_more_link'] ) && empty( $notice['notice_more_label'] ) ) { |
| 407 |
$notice = array_merge( |
| 408 |
$notice, |
| 409 |
array( |
| 410 |
'notice_more_label' => esc_html__( 'Learn more', 'wp-notice-plugin-promote' ), |
| 411 |
) |
| 412 |
); |
| 413 |
} |
| 414 |
|
| 415 |
extract( $notice ); |
| 416 |
|
| 417 |
include $template_path; |
| 418 |
} |
| 419 |
|
| 420 |
/** |
| 421 |
* Remove all notices data. |
| 422 |
* |
| 423 |
* @return void |
| 424 |
*/ |
| 425 |
public function remove_all_data() { |
| 426 |
/** |
| 427 |
* Loop through notices. |
| 428 |
*/ |
| 429 |
foreach ( $this->notices as $notice_index => $notice ) { |
| 430 |
$this->delete_current_user_notice_meta_hidden( $notice_index ); |
| 431 |
} |
| 432 |
|
| 433 |
delete_transient( $this->get_transient_key() ); |
| 434 |
} |
| 435 |
} |
| 436 |
|