| 1 |
<?php |
| 2 |
/* |
| 3 |
Class Name: iWorks Rate |
| 4 |
Class URI: https://github.com/iworks/iworks-rate |
| 5 |
Description: Dashboard Notification module. |
| 6 |
Version: 2.3.1 |
| 7 |
Author: Marcin Pietrzak |
| 8 |
Author URI: http://iworks.pl/ |
| 9 |
License: GPLv3 or later |
| 10 |
License URI: http://www.gnu.org/licenses/gpl-3.0.html |
| 11 |
|
| 12 |
Copyright 2017-2025 Marcin Pietrzak (marcin@iworks.pl) |
| 13 |
|
| 14 |
this program is free software; you can redistribute it and/or modify |
| 15 |
it under the terms of the GNU General Public License, version 3, as |
| 16 |
published by the Free Software Foundation. |
| 17 |
|
| 18 |
This program is distributed in the hope that it will be useful, |
| 19 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 |
GNU General Public License for more details. |
| 22 |
|
| 23 |
You should have received a copy of the GNU General Public License |
| 24 |
along with this program; if not, write to the Free Software |
| 25 |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 26 |
*/ |
| 27 |
|
| 28 |
defined( 'ABSPATH' ) || exit; // Exit if accessed directly |
| 29 |
/** |
| 30 |
* iWorks_Rate - Dashboard Notification module. |
| 31 |
* |
| 32 |
* @version 2.3.2 |
| 33 |
* @author iworks (Marcin Pietrzak) |
| 34 |
* |
| 35 |
*/ |
| 36 |
if ( ! class_exists( 'iworks_rate' ) ) { |
| 37 |
class iworks_rate { |
| 38 |
|
| 39 |
/** |
| 40 |
* This class version. |
| 41 |
* |
| 42 |
* @since 1.0.1 |
| 43 |
* @var string |
| 44 |
*/ |
| 45 |
private $version = '2.3.2'; |
| 46 |
|
| 47 |
/** |
| 48 |
* $wpdb->options field name. |
| 49 |
* |
| 50 |
* @since 1.0.0 |
| 51 |
* @var string |
| 52 |
*/ |
| 53 |
protected $option_name = 'iworks_rates'; |
| 54 |
|
| 55 |
/** |
| 56 |
* List of all registered plugins. |
| 57 |
* |
| 58 |
* @since 1.0.0 |
| 59 |
* @var array |
| 60 |
*/ |
| 61 |
protected $plugins = array(); |
| 62 |
|
| 63 |
/** |
| 64 |
* Module options that are stored in database. |
| 65 |
* Timestamps are stored here. |
| 66 |
* |
| 67 |
* Note that this option is stored in site-meta for multisite installs. |
| 68 |
* |
| 69 |
* @since 1.0.0 |
| 70 |
* @var array |
| 71 |
*/ |
| 72 |
protected $stored = array(); |
| 73 |
|
| 74 |
/** |
| 75 |
* The Plugin ID |
| 76 |
* |
| 77 |
* @since 2.1.4 |
| 78 |
* @var string |
| 79 |
*/ |
| 80 |
private $plugin_id; |
| 81 |
|
| 82 |
/** |
| 83 |
* Initializes and returns the singleton instance. |
| 84 |
* |
| 85 |
* @since 1.0.0 |
| 86 |
*/ |
| 87 |
public static function instance() { |
| 88 |
static $Inst = null; |
| 89 |
if ( null === $Inst ) { |
| 90 |
$Inst = new iworks_rate(); |
| 91 |
} |
| 92 |
return $Inst; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Set up the iworks_rate module. Private singleton constructor. |
| 97 |
* |
| 98 |
* @since 1.0.0 |
| 99 |
*/ |
| 100 |
private function __construct() { |
| 101 |
/** |
| 102 |
* settings |
| 103 |
*/ |
| 104 |
$this->stored = wp_parse_args( |
| 105 |
get_site_option( $this->option_name, false ), |
| 106 |
array() |
| 107 |
); |
| 108 |
/** |
| 109 |
* actions |
| 110 |
*/ |
| 111 |
add_action( 'load-index.php', array( $this, 'load' ) ); |
| 112 |
add_action( 'iworks-register-plugin', array( $this, 'register' ), 5, 3 ); |
| 113 |
add_action( 'wp_ajax_iworks_rate_button', array( $this, 'ajax_button' ) ); |
| 114 |
add_action( 'admin_init', array( $this, 'admin_init' ) ); |
| 115 |
/** |
| 116 |
* own hooks |
| 117 |
*/ |
| 118 |
add_filter( 'iworks_rate_assistance', array( $this, 'filter_get_assistance_widget' ), 10, 2 ); |
| 119 |
add_filter( 'iworks_rate_love', array( $this, 'filter_get_love_widget' ), 10, 2 ); |
| 120 |
/** |
| 121 |
* advertising |
| 122 |
* |
| 123 |
* @since 2.1.0 |
| 124 |
*/ |
| 125 |
add_filter( 'iworks_rate_advertising_og', array( $this, 'filter_get_advertising_og' ) ); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Inicialize admin area |
| 130 |
* |
| 131 |
* @since 2.0.2 |
| 132 |
*/ |
| 133 |
public function admin_init() { |
| 134 |
foreach ( $this->plugins as $plugin_file => $plugin ) { |
| 135 |
add_filter( 'plugin_action_links_' . $plugin_file, array( $this, 'add_donate_link' ), 10, 4 ); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Add donate link to plugin_row_meta. |
| 141 |
* |
| 142 |
* @since 2.0.2 |
| 143 |
* |
| 144 |
* @param array $actions An array of the plugin's metadata, including the version, author, author URI, and plugin URI. |
| 145 |
*/ |
| 146 |
public function add_donate_link( $actions, $plugin_file, $plugin_data, $context ) { |
| 147 |
$slug = 'iworks'; |
| 148 |
if ( |
| 149 |
isset( $this->plugins[ $plugin_file ] ) |
| 150 |
&& isset( $this->plugins[ $plugin_file ]['slug'] ) |
| 151 |
) { |
| 152 |
$slug = $this->plugins[ $plugin_file ]['slug']; |
| 153 |
} |
| 154 |
$settings_page_url = apply_filters( 'iworks_rate_settings_page_url_' . $slug, null ); |
| 155 |
if ( ! empty( $settings_page_url ) ) { |
| 156 |
$actions['settings'] = sprintf( |
| 157 |
'<a href="%s">%s</a>', |
| 158 |
esc_url( $settings_page_url ), |
| 159 |
esc_html__( 'Settings', 'reading-position-indicator' ) |
| 160 |
); |
| 161 |
} |
| 162 |
$actions['donate'] = sprintf( |
| 163 |
'<a href="%s" target="_blank">%s</a>', |
| 164 |
esc_url( |
| 165 |
add_query_arg( |
| 166 |
array( |
| 167 |
'utm_source' => $slug, |
| 168 |
'utm_medium' => 'plugin-links', |
| 169 |
), |
| 170 |
'https://ko-fi.com/iworks' |
| 171 |
) |
| 172 |
), |
| 173 |
esc_html__( 'Provide us a coffee', 'reading-position-indicator' ) |
| 174 |
); |
| 175 |
return $actions; |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Load required hooks and scripts for the admin notice. |
| 180 |
* |
| 181 |
* @since 2.0.2 |
| 182 |
*/ |
| 183 |
public function load() { |
| 184 |
/** |
| 185 |
* regular message |
| 186 |
*/ |
| 187 |
$plugin_id = $this->choose_plugin(); |
| 188 |
if ( ! empty( $plugin_id ) ) { |
| 189 |
$this->plugin_id = $plugin_id; |
| 190 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ) ); |
| 191 |
add_action( 'admin_notices', array( $this, 'show' ) ); |
| 192 |
return; |
| 193 |
} |
| 194 |
/** |
| 195 |
* anniversary message |
| 196 |
*/ |
| 197 |
$plugin_id = $this->choose_anniversary_plugin(); |
| 198 |
if ( ! empty( $plugin_id ) ) { |
| 199 |
$this->plugin_id = $plugin_id; |
| 200 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ) ); |
| 201 |
add_action( 'admin_notices', array( $this, 'show_anniversary' ) ); |
| 202 |
return; |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Save persistent module-data to the WP database. |
| 208 |
* |
| 209 |
* @since 1.0.0 |
| 210 |
*/ |
| 211 |
protected function store_data() { |
| 212 |
update_site_option( $this->option_name, $this->stored ); |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Action handler for 'iworks-register-plugin' |
| 217 |
* Register an active plugin. |
| 218 |
* |
| 219 |
* @since 1.0.0 |
| 220 |
* @param string $plugin_id WordPress plugin-ID (see: plugin_basename). |
| 221 |
* @param string $title Plugin name for display. |
| 222 |
* @param string $slug the plugin slug on wp.org |
| 223 |
*/ |
| 224 |
public function register( $plugin_id, $title, $slug ) { |
| 225 |
// Ignore incorrectly registered plugins to avoid errors later. |
| 226 |
if ( empty( $plugin_id ) || empty( $title ) || empty( $slug ) ) { |
| 227 |
return; |
| 228 |
} |
| 229 |
/** |
| 230 |
* collect data |
| 231 |
*/ |
| 232 |
$data = array( |
| 233 |
'title' => $title, |
| 234 |
'slug' => $slug, |
| 235 |
); |
| 236 |
/** |
| 237 |
* add dat to plugins array |
| 238 |
*/ |
| 239 |
$this->plugins[ $plugin_id ] = $data; |
| 240 |
/** |
| 241 |
* check for option update |
| 242 |
* |
| 243 |
* @since 2.0.6 |
| 244 |
* |
| 245 |
*/ |
| 246 |
$update = false; |
| 247 |
/* |
| 248 |
* When the plugin is registered the first time we store some infos |
| 249 |
* in the persistent module-data that help us later to find out |
| 250 |
* if/which message should be displayed. |
| 251 |
*/ |
| 252 |
if ( empty( $this->stored[ $plugin_id ] ) ) { |
| 253 |
$this->stored[ $plugin_id ] = wp_parse_args( |
| 254 |
array( |
| 255 |
'registered' => time(), |
| 256 |
'show_at' => $this->get_random_future_timestamp( 7, 14 ), |
| 257 |
'rated' => 0, |
| 258 |
'hide' => 0, |
| 259 |
), |
| 260 |
$data |
| 261 |
); |
| 262 |
$update = true; |
| 263 |
} |
| 264 |
/** |
| 265 |
* check slug & mark for update if needed |
| 266 |
* |
| 267 |
* @since 2.0.6 |
| 268 |
*/ |
| 269 |
if ( $this->stored[ $plugin_id ]['slug'] !== $slug ) { |
| 270 |
$this->stored[ $plugin_id ]['slug'] = $slug; |
| 271 |
$update = true; |
| 272 |
} |
| 273 |
/** |
| 274 |
* check last anniversary days |
| 275 |
* |
| 276 |
* @since 2.3.0 |
| 277 |
*/ |
| 278 |
if ( ! isset( $this->stored[ $plugin_id ]['last_anniversary_days'] ) ) { |
| 279 |
$this->stored[ $plugin_id ]['last_anniversary_days'] = floor( |
| 280 |
( time() - $this->stored[ $plugin_id ]['registered'] ) / ( 60 * 60 * 24 ) |
| 281 |
); |
| 282 |
$update = true; |
| 283 |
} |
| 284 |
/** |
| 285 |
* check last anniversary |
| 286 |
* |
| 287 |
* @since 2.3.0 |
| 288 |
*/ |
| 289 |
if ( ! isset( $this->stored[ $plugin_id ]['last_anniversary'] ) ) { |
| 290 |
$this->stored[ $plugin_id ]['last_anniversary'] = 0; |
| 291 |
$update = true; |
| 292 |
} |
| 293 |
/** |
| 294 |
* check title - can be diferent due language |
| 295 |
* |
| 296 |
* @since 2.0.6 |
| 297 |
*/ |
| 298 |
$this->stored[ $plugin_id ]['title'] = $title; |
| 299 |
/** |
| 300 |
* Finally save the details if it is needed |
| 301 |
*/ |
| 302 |
if ( $update ) { |
| 303 |
$this->store_data(); |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
/** |
| 308 |
* Ajax handler called when the user chooses the CTA button. |
| 309 |
* |
| 310 |
* @since 1.0.0 |
| 311 |
*/ |
| 312 |
public function ajax_button() { |
| 313 |
/** |
| 314 |
* Chekc nonce |
| 315 |
* |
| 316 |
* @since 2.1.4 |
| 317 |
*/ |
| 318 |
if ( ! check_ajax_referer( 'iworks-rate' ) ) { |
| 319 |
wp_send_json_error(); |
| 320 |
} |
| 321 |
/** |
| 322 |
* get plugin ID |
| 323 |
*/ |
| 324 |
$nonce_value = sanitize_text_field( wp_unslash( filter_input( INPUT_POST, '_wpnonce', FILTER_DEFAULT ) ) ); |
| 325 |
if ( ! wp_verify_nonce( $nonce_value, 'iworks-rate' ) ) { |
| 326 |
wp_send_json_error(); |
| 327 |
} |
| 328 |
$plugin_id = sanitize_text_field( filter_input( INPUT_POST, 'plugin_id', FILTER_DEFAULT ) ); |
| 329 |
if ( empty( $plugin_id ) ) { |
| 330 |
wp_send_json_error(); |
| 331 |
} |
| 332 |
/** |
| 333 |
* sanitize plugin_id |
| 334 |
* |
| 335 |
* @since 2.1.3 |
| 336 |
*/ |
| 337 |
$plugin_id = sanitize_text_field( $plugin_id ); |
| 338 |
if ( ! isset( $this->plugins[ $plugin_id ] ) ) { |
| 339 |
wp_send_json_error(); |
| 340 |
} |
| 341 |
/** |
| 342 |
* sanitize button value |
| 343 |
* |
| 344 |
* @since 2.1.3 |
| 345 |
*/ |
| 346 |
$value = ''; |
| 347 |
if ( isset( $_POST['button'] ) ) { |
| 348 |
$value = sanitize_text_field( filter_input( INPUT_POST, 'button', FILTER_DEFAULT ) ); |
| 349 |
} |
| 350 |
switch ( $value ) { |
| 351 |
case '': |
| 352 |
case 'add-review': |
| 353 |
$this->add_weeks( $plugin_id ); |
| 354 |
break; |
| 355 |
case 'hide': |
| 356 |
$this->add_weeks( $plugin_id ); |
| 357 |
$this->hide( $plugin_id ); |
| 358 |
break; |
| 359 |
case 'donate': |
| 360 |
$this->add_months( $plugin_id ); |
| 361 |
break; |
| 362 |
case 'hide-anniversary': |
| 363 |
$this->hide_anniversary( $plugin_id ); |
| 364 |
break; |
| 365 |
} |
| 366 |
wp_send_json_success(); |
| 367 |
} |
| 368 |
|
| 369 |
/** |
| 370 |
* Hide the rate notice for a specific plugin. |
| 371 |
* |
| 372 |
* @since 2.0.0 |
| 373 |
* @param string $plugin_id The ID of the plugin to hide the notice for. |
| 374 |
*/ |
| 375 |
public function hide( $plugin_id ) { |
| 376 |
if ( ! isset( $this->stored[ $plugin_id ] ) ) { |
| 377 |
return; |
| 378 |
} |
| 379 |
$this->stored[ $plugin_id ]['hide'] = time(); |
| 380 |
$this->store_data(); |
| 381 |
} |
| 382 |
|
| 383 |
/** |
| 384 |
* Hide the anniversary notice for a specific plugin. |
| 385 |
* |
| 386 |
* @since 2.3.0 |
| 387 |
* @param string $plugin_id The ID of the plugin to hide the notice for. |
| 388 |
*/ |
| 389 |
public function hide_anniversary( $plugin_id ) { |
| 390 |
if ( ! isset( $this->stored[ $plugin_id ] ) ) { |
| 391 |
return; |
| 392 |
} |
| 393 |
/** |
| 394 |
* set proper anniversary value |
| 395 |
* |
| 396 |
* @since 2.3.2 |
| 397 |
*/ |
| 398 |
$max = floor( $this->stored[ $plugin_id ]['last_anniversary_days'] / 365 ) + 1; |
| 399 |
$value = $this->stored[ $plugin_id ]['last_anniversary'] + 1; |
| 400 |
$this->stored[ $plugin_id ]['last_anniversary'] = max( $max, $value ); |
| 401 |
/** |
| 402 |
* save data |
| 403 |
*/ |
| 404 |
$this->store_data(); |
| 405 |
} |
| 406 |
|
| 407 |
/** |
| 408 |
* Extend the show_at timestamp by a random number of weeks. |
| 409 |
* |
| 410 |
* @since 2.0.0 |
| 411 |
* @param string $plugin_id The ID of the plugin to extend the delay for. |
| 412 |
* @access private |
| 413 |
*/ |
| 414 |
private function add_weeks( $plugin_id ) { |
| 415 |
if ( ! isset( $this->stored[ $plugin_id ] ) ) { |
| 416 |
return; |
| 417 |
} |
| 418 |
$this->stored[ $plugin_id ]['show_at'] = $this->get_random_future_timestamp( 0, 7, 4, 6 ); |
| 419 |
$this->store_data(); |
| 420 |
} |
| 421 |
|
| 422 |
/** |
| 423 |
* Extend the show_at timestamp by a random number of months. |
| 424 |
* |
| 425 |
* @since 2.0.0 |
| 426 |
* @param string $plugin_id The ID of the plugin to extend the delay for. |
| 427 |
* @access private |
| 428 |
*/ |
| 429 |
private function add_months( $plugin_id ) { |
| 430 |
if ( ! isset( $this->stored[ $plugin_id ] ) ) { |
| 431 |
return; |
| 432 |
} |
| 433 |
$this->stored[ $plugin_id ]['show_at'] = $this->get_random_future_timestamp( 0, 14, 15, 30 ); |
| 434 |
$this->store_data(); |
| 435 |
} |
| 436 |
|
| 437 |
/** |
| 438 |
* Ajax handler called when the user chooses the dismiss button. |
| 439 |
* |
| 440 |
* @since 1.0.0 |
| 441 |
*/ |
| 442 |
public function dismiss() { |
| 443 |
$plugin = $this->get_plugin_from_post(); |
| 444 |
if ( is_wp_error( $plugin ) ) { |
| 445 |
wp_send_json_error(); |
| 446 |
} |
| 447 |
wp_send_json_success(); |
| 448 |
} |
| 449 |
|
| 450 |
/** |
| 451 |
* Action handler for 'load-index.php' |
| 452 |
* Set-up the Dashboard notification. |
| 453 |
* |
| 454 |
* @since 1.0.0 |
| 455 |
*/ |
| 456 |
public function enqueue() { |
| 457 |
wp_enqueue_style( |
| 458 |
__CLASS__, |
| 459 |
plugin_dir_url( __FILE__ ) . 'admin.css', |
| 460 |
array(), |
| 461 |
$this->version |
| 462 |
); |
| 463 |
wp_enqueue_script( |
| 464 |
__CLASS__, |
| 465 |
plugin_dir_url( __FILE__ ) . 'admin.js', |
| 466 |
array(), |
| 467 |
$this->version, |
| 468 |
true |
| 469 |
); |
| 470 |
} |
| 471 |
|
| 472 |
/** |
| 473 |
* Action handler for 'admin_notices' |
| 474 |
* Display the Dashboard notification. |
| 475 |
* |
| 476 |
* @since 1.0.0 |
| 477 |
*/ |
| 478 |
public function show() { |
| 479 |
$this->render_message( $this->plugin_id ); |
| 480 |
} |
| 481 |
|
| 482 |
/** |
| 483 |
* Action handler for 'admin_notices' |
| 484 |
* Display the Dashboard notification. |
| 485 |
* |
| 486 |
* @since 2.2.0 |
| 487 |
*/ |
| 488 |
public function show_anniversary() { |
| 489 |
$filename = $this->stored[ $this->plugin_id ]['last_anniversary'] ? 'anniversary' : 'first-year'; |
| 490 |
$this->render_message( $this->plugin_id, $filename, 'happy-anniversary' ); |
| 491 |
} |
| 492 |
/** |
| 493 |
* Check to see if there is a pending message to display and returns |
| 494 |
* the message details if there is. |
| 495 |
* |
| 496 |
* Note that this function is only called on the main Dashboard screen |
| 497 |
* and only when logged in as super-admin. |
| 498 |
* |
| 499 |
* @since 1.0.0 |
| 500 |
* @return object|false |
| 501 |
* string $plugin WordPress plugin ID? |
| 502 |
*/ |
| 503 |
protected function choose_plugin() { |
| 504 |
if ( wp_is_mobile() ) { |
| 505 |
return false; |
| 506 |
} |
| 507 |
/** |
| 508 |
* list |
| 509 |
*/ |
| 510 |
$choosen = array(); |
| 511 |
/** |
| 512 |
* change time by filter |
| 513 |
*/ |
| 514 |
$now = intval( apply_filters( 'iworks_rate_set_custom_time', time() ) ); |
| 515 |
foreach ( $this->stored as $plugin_id => $item ) { |
| 516 |
if ( ! isset( $this->plugins[ $plugin_id ] ) ) { |
| 517 |
if ( isset( $this->stored[ $plugin_id ] ) ) { |
| 518 |
unset( $this->stored[ $plugin_id ] ); |
| 519 |
$this->store_data(); |
| 520 |
} |
| 521 |
continue; |
| 522 |
} |
| 523 |
if ( intval( $item['show_at'] ) > $now ) { |
| 524 |
continue; |
| 525 |
} |
| 526 |
if ( YEAR_IN_SECONDS > time() - intval( $item['hide'] ) ) { |
| 527 |
continue; |
| 528 |
} |
| 529 |
$choosen[] = $plugin_id; |
| 530 |
} |
| 531 |
if ( empty( $choosen ) ) { |
| 532 |
return false; |
| 533 |
} |
| 534 |
return $choosen[ array_rand( $choosen ) ]; |
| 535 |
} |
| 536 |
|
| 537 |
/** |
| 538 |
* Choose a plugin that is eligible for an anniversary rating prompt. |
| 539 |
* |
| 540 |
* @since 2.2.0 |
| 541 |
* @access protected |
| 542 |
* @return string|false Returns the plugin ID if a matching plugin is found, false otherwise. |
| 543 |
*/ |
| 544 |
protected function choose_anniversary_plugin() { |
| 545 |
if ( wp_is_mobile() ) { |
| 546 |
return false; |
| 547 |
} |
| 548 |
/** |
| 549 |
* list |
| 550 |
*/ |
| 551 |
$choosen = array(); |
| 552 |
/** |
| 553 |
* change time by filter |
| 554 |
*/ |
| 555 |
$now = intval( apply_filters( 'iworks_rate_set_custom_time', time() ) ); |
| 556 |
foreach ( $this->stored as $plugin_id => $item ) { |
| 557 |
if ( ! isset( $this->plugins[ $plugin_id ] ) ) { |
| 558 |
if ( isset( $this->stored[ $plugin_id ] ) ) { |
| 559 |
unset( $this->stored[ $plugin_id ] ); |
| 560 |
$this->store_data(); |
| 561 |
} |
| 562 |
continue; |
| 563 |
} |
| 564 |
if ( ! isset( $item['last_anniversary_days'] ) ) { |
| 565 |
continue; |
| 566 |
} |
| 567 |
if ( ! isset( $item['last_anniversary'] ) ) { |
| 568 |
continue; |
| 569 |
} |
| 570 |
$anniversary = intval( $item['last_anniversary_days'] / 365 ); |
| 571 |
if ( $anniversary < $item['last_anniversary'] ) { |
| 572 |
continue; |
| 573 |
} |
| 574 |
$choosen[] = $plugin_id; |
| 575 |
} |
| 576 |
if ( empty( $choosen ) ) { |
| 577 |
return false; |
| 578 |
} |
| 579 |
return $choosen[ array_rand( $choosen ) ]; |
| 580 |
} |
| 581 |
|
| 582 |
/** |
| 583 |
* Renders the actual Notification message. |
| 584 |
* |
| 585 |
* @since 1.0.0 |
| 586 |
*/ |
| 587 |
protected function render_message( $plugin_id, $filename = 'thanks', $group = '' ) { |
| 588 |
$file = $this->get_file( $filename, $group ); |
| 589 |
$plugin = $this->get_plugin_data_by_plugin_id( $plugin_id ); |
| 590 |
load_template( $file, true, $plugin ); |
| 591 |
} |
| 592 |
|
| 593 |
/** |
| 594 |
* Get the absolute path to a template file. |
| 595 |
* |
| 596 |
* @since 2.0.1 |
| 597 |
* @param string $file The base name of the template file. |
| 598 |
* @param string $group Optional. Subdirectory within the templates directory. |
| 599 |
* @return string The full path to the template file. |
| 600 |
* @access private |
| 601 |
*/ |
| 602 |
private function get_file( $file, $group = '' ) { |
| 603 |
return sprintf( |
| 604 |
'%s/templates/%s%s%s.php', |
| 605 |
__DIR__, |
| 606 |
$group, |
| 607 |
'' === $group ? '' : '/', |
| 608 |
sanitize_title( $file ) |
| 609 |
); |
| 610 |
} |
| 611 |
|
| 612 |
/** |
| 613 |
* Get plugin data by its ID. |
| 614 |
* |
| 615 |
* @since 2.0.1 |
| 616 |
* @param string $plugin_id The ID of the plugin to get data for. |
| 617 |
* @return array|WP_Error Plugin data array or WP_Error on failure. |
| 618 |
* @access private |
| 619 |
*/ |
| 620 |
private function get_plugin_data_by_plugin_id( $plugin_id ) { |
| 621 |
if ( ! isset( $this->plugins[ $plugin_id ] ) ) { |
| 622 |
return new WP_Error( |
| 623 |
'no-plugin', |
| 624 |
sprintf( |
| 625 |
/* translators: %s: plugin id */ |
| 626 |
esc_html__( 'There is no plugin with id: %s.', 'reading-position-indicator' ), |
| 627 |
$plugin_id |
| 628 |
) |
| 629 |
); |
| 630 |
} |
| 631 |
$plugin = wp_parse_args( |
| 632 |
$this->plugins[ $plugin_id ], |
| 633 |
$this->stored[ $plugin_id ] |
| 634 |
); |
| 635 |
$plugin['plugin_id'] = $plugin_id; |
| 636 |
$plugin['logo'] = apply_filters( 'iworks_rate_notice_logo_style', '', $plugin ); |
| 637 |
$plugin['ajax_url'] = admin_url( 'admin-ajax.php' ); |
| 638 |
$plugin['classes'] = array( |
| 639 |
'iworks-rate', |
| 640 |
'iworks-rate-' . $plugin['slug'], |
| 641 |
'iworks-rate-notice', |
| 642 |
); |
| 643 |
if ( ! empty( $plugin['logo'] ) ) { |
| 644 |
$plugin['classes'][] = 'has-logo'; |
| 645 |
} |
| 646 |
$plugin['url'] = esc_url( |
| 647 |
sprintf( |
| 648 |
/* translators: %s: plugin slug */ |
| 649 |
_x( 'https://wordpress.org/plugins/%s', 'plugins home', 'reading-position-indicator' ), |
| 650 |
$plugin['slug'] |
| 651 |
) |
| 652 |
); |
| 653 |
$plugin['support_url'] = esc_url( |
| 654 |
sprintf( |
| 655 |
/* translators: %s: plugin slug */ |
| 656 |
_x( 'https://wordpress.org/support/plugin/%s', 'plugins support home', 'reading-position-indicator' ), |
| 657 |
$plugin['slug'] |
| 658 |
) |
| 659 |
); |
| 660 |
/** |
| 661 |
* Add utm parameters to donate link. |
| 662 |
* |
| 663 |
* @since 2.2.0 |
| 664 |
* |
| 665 |
* @param string $donate_url Donate URL. |
| 666 |
* @param array $plugin Plugin data. |
| 667 |
*/ |
| 668 |
$plugin['donate_url'] = esc_url( |
| 669 |
add_query_arg( |
| 670 |
array( |
| 671 |
'utm_source' => $plugin['slug'], |
| 672 |
'utm_medium' => 'WP-Dashboard', |
| 673 |
'utm_campaign' => 'happy-anniversary', |
| 674 |
), |
| 675 |
'https://ko-fi.com/iworks' |
| 676 |
) |
| 677 |
); |
| 678 |
/** |
| 679 |
* Change plugin data. |
| 680 |
* |
| 681 |
* Allows to change generated plugin data. |
| 682 |
* |
| 683 |
* @since 2.1.4 |
| 684 |
* |
| 685 |
* @param array $plugin { |
| 686 |
* Plugin data. |
| 687 |
* |
| 688 |
* @type string $plugin_id Plugin ID |
| 689 |
* @type string $logo Logo URL |
| 690 |
* @type string $ajax_url Admin AJAX URl |
| 691 |
* @type array $classes CSS classes |
| 692 |
* @type string $url Plugin repository URL |
| 693 |
* @type string $support_url Plugin support URL |
| 694 |
* } |
| 695 |
*/ |
| 696 |
return apply_filters( 'iworks_rate_plugin_data', $plugin ); |
| 697 |
} |
| 698 |
|
| 699 |
/** |
| 700 |
* Get plugin ID by its slug. |
| 701 |
* |
| 702 |
* @since 2.0.1 |
| 703 |
* @param string $slug The plugin slug to search for. |
| 704 |
* @return string|WP_Error The plugin ID or WP_Error if not found. |
| 705 |
* @access private |
| 706 |
*/ |
| 707 |
private function get_plugin_id_by_slug( $slug ) { |
| 708 |
foreach ( $this->stored as $plugin_id => $plugin ) { |
| 709 |
if ( $slug === $plugin['slug'] ) { |
| 710 |
return $plugin_id; |
| 711 |
} |
| 712 |
} |
| 713 |
return new WP_Error(); |
| 714 |
} |
| 715 |
|
| 716 |
/** |
| 717 |
* Get the URL to install a plugin from the WordPress.org repository. |
| 718 |
* |
| 719 |
* @since 2.1.0 |
| 720 |
* @param string $slug The plugin slug to generate install URL for. |
| 721 |
* @return string The URL to install the plugin. |
| 722 |
* @access private |
| 723 |
*/ |
| 724 |
private function get_install_plugin_url( $slug ) { |
| 725 |
return wp_nonce_url( |
| 726 |
self_admin_url( |
| 727 |
add_query_arg( |
| 728 |
array( |
| 729 |
'action' => 'install-plugin', |
| 730 |
'plugin' => $slug, |
| 731 |
), |
| 732 |
'update.php' |
| 733 |
) |
| 734 |
), |
| 735 |
'install-plugin_' . $slug |
| 736 |
); |
| 737 |
} |
| 738 |
|
| 739 |
/** |
| 740 |
* Generate a random future timestamp based on days and weeks. |
| 741 |
* |
| 742 |
* @since 2.1.5 |
| 743 |
* @param int $day_min Minimum number of days to add. |
| 744 |
* @param int $day_max Maximum number of days to add. |
| 745 |
* @param int $week_min Minimum number of weeks to add. |
| 746 |
* @param int $week_max Maximum number of weeks to add. |
| 747 |
* @return int A future timestamp. |
| 748 |
* @access private |
| 749 |
*/ |
| 750 |
private function get_random_future_timestamp( $day_min = 0, $day_max = 0, $week_min = 0, $week_max = 0 ) { |
| 751 |
$time = time(); |
| 752 |
/** |
| 753 |
* DAY_IN_SECONDS |
| 754 |
*/ |
| 755 |
$days = 0; |
| 756 |
if ( 0 < $day_max ) { |
| 757 |
$days = $this->rate_rand( $day_min, $day_max ); |
| 758 |
} |
| 759 |
$time += $days * DAY_IN_SECONDS; |
| 760 |
/** |
| 761 |
* WEEK_IN_SECONDS |
| 762 |
*/ |
| 763 |
$weeks = 0; |
| 764 |
if ( 0 < $week_max ) { |
| 765 |
$weeks = $this->rate_rand( $week_min, $week_max ); |
| 766 |
} |
| 767 |
$time += $weeks * WEEK_IN_SECONDS; |
| 768 |
/** |
| 769 |
* returns |
| 770 |
*/ |
| 771 |
return $time; |
| 772 |
} |
| 773 |
|
| 774 |
/** |
| 775 |
* Generate a random number within a range. |
| 776 |
* |
| 777 |
* Uses wp_rand() if available, falls back to mt_rand(). |
| 778 |
* |
| 779 |
* @since 2.1.5 |
| 780 |
* @param int|null $min Lower limit (included). |
| 781 |
* @param int|null $max Upper limit (included). |
| 782 |
* @return int A random number from the specified range. |
| 783 |
* @access private |
| 784 |
*/ |
| 785 |
private function rate_rand( $min = null, $max = null ) { |
| 786 |
if ( function_exists( 'wp_rand' ) ) { |
| 787 |
return wp_rand( $min, $max ); |
| 788 |
} |
| 789 |
return mt_rand( $min, $max ); |
| 790 |
} |
| 791 |
|
| 792 |
/** |
| 793 |
* Get advertising for "OG — Better Share on Social Media" plugin. |
| 794 |
* |
| 795 |
* @since 2.1.0 |
| 796 |
*/ |
| 797 |
public function filter_get_advertising_og( $data ) { |
| 798 |
return array( |
| 799 |
'iworks-adverting-og' => array( |
| 800 |
'title' => esc_html__( 'OpenGraph', 'reading-position-indicator' ), |
| 801 |
'callback' => array( $this, 'get_advertising_og_content' ), |
| 802 |
'context' => 'side', |
| 803 |
'priority' => 'low', |
| 804 |
), |
| 805 |
); |
| 806 |
} |
| 807 |
|
| 808 |
/** |
| 809 |
* Advertising content for "OG — Better Share on Social Media" plugin. |
| 810 |
* |
| 811 |
* @since 2.1.0 |
| 812 |
*/ |
| 813 |
public function get_advertising_og_content() { |
| 814 |
$args = array( |
| 815 |
'install_plugin_url' => $this->get_install_plugin_url( 'og' ), |
| 816 |
'plugin_name' => esc_html__( 'OG — Better Share on Social Media', 'reading-position-indicator' ), |
| 817 |
'plugin_wp_home' => esc_url( __( 'https://wordpress.org/plugins/og/', 'reading-position-indicator' ) ), |
| 818 |
); |
| 819 |
$file = $this->get_file( 'og', 'plugins' ); |
| 820 |
load_template( $file, true, $args ); |
| 821 |
} |
| 822 |
|
| 823 |
/** |
| 824 |
* @since 2.0.1 |
| 825 |
*/ |
| 826 |
public function filter_get_assistance_widget( $content, $slug ) { |
| 827 |
$plugin_id = $this->get_plugin_id_by_slug( $slug ); |
| 828 |
if ( is_wp_error( $plugin_id ) ) { |
| 829 |
return $content; |
| 830 |
} |
| 831 |
$this->enqueue(); |
| 832 |
$plugin = $this->get_plugin_data_by_plugin_id( $plugin_id ); |
| 833 |
if ( is_wp_error( $plugin ) ) { |
| 834 |
return $content; |
| 835 |
} |
| 836 |
$file = $this->get_file( 'support', 'widgets' ); |
| 837 |
ob_start(); |
| 838 |
load_template( $file, true, $plugin ); |
| 839 |
$content = ob_get_contents(); |
| 840 |
ob_end_clean(); |
| 841 |
return $content; |
| 842 |
} |
| 843 |
|
| 844 |
/** |
| 845 |
* @since 2.0.1 |
| 846 |
*/ |
| 847 |
public function filter_get_love_widget( $content, $slug ) { |
| 848 |
$plugin_id = $this->get_plugin_id_by_slug( $slug ); |
| 849 |
if ( is_wp_error( $plugin_id ) ) { |
| 850 |
return $content; |
| 851 |
} |
| 852 |
$this->enqueue(); |
| 853 |
$plugin = $this->get_plugin_data_by_plugin_id( $plugin_id ); |
| 854 |
if ( is_wp_error( $plugin ) ) { |
| 855 |
return $content; |
| 856 |
} |
| 857 |
$file = $this->get_file( 'donate', 'widgets' ); |
| 858 |
ob_start(); |
| 859 |
load_template( $file, true, $plugin ); |
| 860 |
$content = ob_get_contents(); |
| 861 |
ob_end_clean(); |
| 862 |
return $content; |
| 863 |
} |
| 864 |
} |
| 865 |
|
| 866 |
// Initialize the module. |
| 867 |
iworks_rate::instance(); |
| 868 |
} |
| 869 |
|