| 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 |
static 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 |
self::$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 |
$instances = self::$notifications; |
| 79 |
if ( empty( $instances ) ) { |
| 80 |
return; |
| 81 |
} |
| 82 |
|
| 83 |
$available = array_keys( $instances ); |
| 84 |
$active = get_option( 'themeisle_sdk_active_notification', array() ); |
| 85 |
|
| 86 |
foreach ( $available as $key ) { |
| 87 |
$instance = $instances[ $key ]; |
| 88 |
if ( $instance->can_notify() ) { |
| 89 |
|
| 90 |
// Detect notification switch. |
| 91 |
if ( empty( $active['key'] ) || ( $active['key'] != $key ) ) { |
| 92 |
$active['key'] = $key; |
| 93 |
$active['time'] = time(); |
| 94 |
update_option( 'themeisle_sdk_active_notification', $active ); |
| 95 |
} |
| 96 |
if ( ( time() - $active['time'] ) > ( self::NOTIFICATION_INTERVAL_HOURS * HOUR_IN_SECONDS ) ) { |
| 97 |
$instance->show_notification(); |
| 98 |
} |
| 99 |
break; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
} |
| 104 |
} |
| 105 |
endif; |
| 106 |
|