| 1 |
<?php |
| 2 |
/** |
| 3 |
* The notification manager class for ThemeIsle SDK |
| 4 |
* |
| 5 |
* @package ThemeIsleSDK |
| 6 |
* @subpackage Notification |
| 7 |
* @copyright Copyright (c) 2017, Marius Cristea |
| 8 |
* @license http://opensource.org/licenses/gpl-3.0.php GNU Public License |
| 9 |
* @since 1.0.0 |
| 10 |
*/ |
| 11 |
// Exit if accessed directly. |
| 12 |
if ( ! defined( 'ABSPATH' ) ) { |
| 13 |
exit; |
| 14 |
} |
| 15 |
if ( ! class_exists( 'ThemeIsle_SDK_Notification_Manager' ) ) : |
| 16 |
/** |
| 17 |
* Notification manager model for ThemeIsle SDK. |
| 18 |
*/ |
| 19 |
class ThemeIsle_SDK_Notification_Manager { |
| 20 |
/** |
| 21 |
* Time between notifications. |
| 22 |
*/ |
| 23 |
const NOTIFICATION_INTERVAL_HOURS = 100; |
| 24 |
/** |
| 25 |
* @var array Notifications for the current product. |
| 26 |
*/ |
| 27 |
private $notifications = array(); |
| 28 |
/** |
| 29 |
* @var ThemeIsle_SDK_Product Current product. |
| 30 |
*/ |
| 31 |
private $product; |
| 32 |
/** |
| 33 |
* @var array ThemeIsle_SDK_Feedback Feedbacks available. |
| 34 |
*/ |
| 35 |
private $callbacks = array(); |
| 36 |
|
| 37 |
/** |
| 38 |
* ThemeIsle_SDK_Notification_Manager constructor. |
| 39 |
* |
| 40 |
* @param ThemeIsle_SDK_Product $product_object Product Object. |
| 41 |
* @param array $callbacks the objects that will be called when a notification is due. |
| 42 |
*/ |
| 43 |
public function __construct( $product_object, $callbacks ) { |
| 44 |
$this->product = $product_object; |
| 45 |
$this->callbacks = $callbacks; |
| 46 |
$this->setup_hooks(); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Setup the notifications. |
| 51 |
*/ |
| 52 |
function setup_notifications() { |
| 53 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 54 |
return; |
| 55 |
} |
| 56 |
// Load the notifications only if we have it installed after the required interval. |
| 57 |
if ( ( time() - $this->product->get_install_time() ) > self::NOTIFICATION_INTERVAL_HOURS * HOUR_IN_SECONDS ) { |
| 58 |
if ( $this->product instanceof ThemeIsle_SDK_Product && $this->callbacks && is_array( $this->callbacks ) ) { |
| 59 |
foreach ( $this->callbacks as $instance ) { |
| 60 |
$this->notifications[ $this->product->get_key() . get_class( $instance ) ] = $instance; |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Setup the internal hooks |
| 68 |
*/ |
| 69 |
private function setup_hooks() { |
| 70 |
add_action( 'admin_head', array( $this, 'show_notification' ) ); |
| 71 |
add_action( 'admin_init', array( $this, 'setup_notifications' ) ); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Shows the notification |
| 76 |
*/ |
| 77 |
function show_notification() { |
| 78 |
|
| 79 |
$hidden = get_option( 'themeisle_sdk_notification_hidden', array() ); |
| 80 |
$instances = $this->notifications; |
| 81 |
if ( empty( $instances ) ) { |
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
// Get timestamp of last notification. |
| 86 |
$old = 0; |
| 87 |
if ( ! empty( $hidden ) ) { |
| 88 |
$old = $hidden[ count( $hidden ) - 1 ]['time']; |
| 89 |
} |
| 90 |
// Check if the current one is expired. |
| 91 |
if ( ( time() - $old ) > self::NOTIFICATION_INTERVAL_HOURS * HOUR_IN_SECONDS ) { |
| 92 |
// Get hidden notifications key. |
| 93 |
$hidden_ones = wp_list_pluck( $hidden, 'key' ); |
| 94 |
// Get the non-hidden notifications. |
| 95 |
$available_notifications = array_diff( array_keys( $instances ), $hidden_ones ); |
| 96 |
if ( empty( $available_notifications ) ) { |
| 97 |
return; |
| 98 |
} |
| 99 |
// Get the first notification available. |
| 100 |
$new_one = reset( $available_notifications ); |
| 101 |
|
| 102 |
$instance = $instances[ $new_one ]; |
| 103 |
$hidden[] = array( |
| 104 |
'time' => time(), |
| 105 |
'key' => $new_one, |
| 106 |
); |
| 107 |
update_option( 'themeisle_sdk_notification_hidden', $hidden ); |
| 108 |
} else { |
| 109 |
$key = $hidden[ count( $hidden ) - 1 ]['key']; |
| 110 |
if ( ! isset( $this->notifications[ $key ] ) ) { |
| 111 |
return; |
| 112 |
} else { |
| 113 |
$instance = $this->notifications[ $key ]; |
| 114 |
} |
| 115 |
} |
| 116 |
$instance->show_notification(); |
| 117 |
} |
| 118 |
} |
| 119 |
endif; |
| 120 |
|