| 1 |
<?php |
| 2 |
|
| 3 |
namespace Texty; |
| 4 |
|
| 5 |
/** |
| 6 |
* Notification Class |
| 7 |
*/ |
| 8 |
class Notifications { |
| 9 |
|
| 10 |
/** |
| 11 |
* Option key to hold the notifications |
| 12 |
*/ |
| 13 |
const OPTION_KEY = 'texty_notifications'; |
| 14 |
|
| 15 |
/** |
| 16 |
* Notifications |
| 17 |
* |
| 18 |
* @var array |
| 19 |
*/ |
| 20 |
private $notifications = []; |
| 21 |
|
| 22 |
/** |
| 23 |
* Whether built-in notifications have been registered |
| 24 |
* |
| 25 |
* @var bool |
| 26 |
*/ |
| 27 |
private $registered = false; |
| 28 |
|
| 29 |
/** |
| 30 |
* The notification currently driving a send-pipeline call. |
| 31 |
* |
| 32 |
* `Notification::send()` sets this around its `$gateway->send()` loop so |
| 33 |
* that `Dispatcher::log_sms` (hooked on `texty_after_send_sms`) can write |
| 34 |
* the originating notification's id + group into the SmsStat row without |
| 35 |
* having to pass them through the gateway pipeline. |
| 36 |
* |
| 37 |
* @var \Texty\Notifications\Notification|null |
| 38 |
*/ |
| 39 |
private $active = null; |
| 40 |
|
| 41 |
/** |
| 42 |
* Set the active notification for the duration of a send. |
| 43 |
* |
| 44 |
* @param \Texty\Notifications\Notification|null $notification |
| 45 |
* |
| 46 |
* @return void |
| 47 |
*/ |
| 48 |
public function set_active( $notification ) { |
| 49 |
$this->active = $notification; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Get the active notification, if any. |
| 54 |
* |
| 55 |
* @return \Texty\Notifications\Notification|null |
| 56 |
*/ |
| 57 |
public function get_active() { |
| 58 |
return $this->active; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Clear the active notification. |
| 63 |
* |
| 64 |
* @return void |
| 65 |
*/ |
| 66 |
public function clear_active() { |
| 67 |
$this->active = null; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Register a notification |
| 72 |
* |
| 73 |
* @param string $key Notification identifier |
| 74 |
* @param string $classname Fully qualified class name |
| 75 |
* |
| 76 |
* @return void |
| 77 |
*/ |
| 78 |
public function register( $key, $classname ) { |
| 79 |
$this->notifications[ $key ] = $classname; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Get a notification class |
| 84 |
* |
| 85 |
* @param string $key |
| 86 |
* |
| 87 |
* @return false|string |
| 88 |
*/ |
| 89 |
public function get( $key ) { |
| 90 |
$notifications = $this->all(); |
| 91 |
|
| 92 |
if ( array_key_exists( $key, $notifications ) ) { |
| 93 |
return $notifications[ $key ]; |
| 94 |
} |
| 95 |
|
| 96 |
return false; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Get available notification classes |
| 101 |
* |
| 102 |
* @return array |
| 103 |
*/ |
| 104 |
public function all() { |
| 105 |
if ( ! $this->registered ) { |
| 106 |
$this->notifications = [ |
| 107 |
'registration' => __NAMESPACE__ . '\Notifications\WP\Registration', |
| 108 |
'comment' => __NAMESPACE__ . '\Notifications\WP\Comment', |
| 109 |
]; |
| 110 |
|
| 111 |
/** |
| 112 |
* Fires to allow registration of custom notifications. |
| 113 |
* |
| 114 |
* @param Notifications $manager The notifications manager instance |
| 115 |
*/ |
| 116 |
do_action( 'texty_register_notifications', $this ); |
| 117 |
|
| 118 |
$this->registered = true; |
| 119 |
} |
| 120 |
|
| 121 |
return apply_filters( 'texty_available_notifications', $this->notifications ); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Get the name of the groups |
| 126 |
* |
| 127 |
* @return void |
| 128 |
*/ |
| 129 |
public function get_groups() { |
| 130 |
return apply_filters( 'texty_notification_groups', [ // phpcs:ignore |
| 131 |
'wp' => [ |
| 132 |
'title' => __( 'WordPress', 'texty' ), |
| 133 |
'description' => __( 'Default WordPress system alerts', 'texty' ), |
| 134 |
'available' => true, |
| 135 |
], |
| 136 |
'wc' => [ |
| 137 |
'title' => __( 'WooCommerce', 'texty' ), |
| 138 |
'description' => __( 'WooCommerce order and customer alerts', 'texty' ), |
| 139 |
'available' => class_exists( 'WooCommerce' ) ? true : false, |
| 140 |
], |
| 141 |
'dokan' => [ |
| 142 |
'title' => __( 'Dokan', 'texty' ), |
| 143 |
'description' => __( 'Vendor and marketplace alerts', 'texty' ), |
| 144 |
'available' => class_exists( 'WeDevs_Dokan' ) ? true : false, |
| 145 |
], |
| 146 |
] ); // phpcs:ignore |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Retreive all the settings |
| 151 |
* |
| 152 |
* @return array |
| 153 |
*/ |
| 154 |
public function settings() { |
| 155 |
return get_option( self::OPTION_KEY, [] ); |
| 156 |
} |
| 157 |
} |
| 158 |
|