| 1 |
<?php |
| 2 |
/** |
| 3 |
* Notification Triggers class |
| 4 |
* |
| 5 |
* @package Wa_Notifier |
| 6 |
*/ |
| 7 |
class Notifier_Notification_Triggers { |
| 8 |
|
| 9 |
/** |
| 10 |
* Init. |
| 11 |
*/ |
| 12 |
public static function init() { |
| 13 |
add_action( 'plugins_loaded', array( __CLASS__ , 'setup_triggers_action_hooks' ), 100 ); |
| 14 |
} |
| 15 |
|
| 16 |
/** |
| 17 |
* Setup triggers action hooks |
| 18 |
*/ |
| 19 |
public static function setup_triggers_action_hooks() { |
| 20 |
$main_triggers = self::get_notification_triggers(); |
| 21 |
foreach ($main_triggers as $key => $triggers) { |
| 22 |
foreach ($triggers as $trigger) { |
| 23 |
$hook = isset($trigger['action']['hook']) ? $trigger['action']['hook'] : ''; |
| 24 |
$callback = isset($trigger['action']['callback']) ? $trigger['action']['callback'] : ''; |
| 25 |
$priority = isset($trigger['action']['priority']) ? $trigger['action']['priority'] : 10; |
| 26 |
$args_num = isset($trigger['action']['args_num']) ? $trigger['action']['args_num'] : 1; |
| 27 |
if ('' == $hook || '' == $callback) { |
| 28 |
continue; |
| 29 |
} |
| 30 |
if(self::is_trigger_enabled($trigger['id'])){ |
| 31 |
add_action($hook, $callback, $priority, $args_num); |
| 32 |
} |
| 33 |
} |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Get notification triggers |
| 39 |
*/ |
| 40 |
public static function get_notification_triggers() { |
| 41 |
$triggers = array(); |
| 42 |
$triggers['WordPress'] = array ( |
| 43 |
array( |
| 44 |
'id' => 'new_post', |
| 45 |
'label' => 'New post is published', |
| 46 |
'description' => 'Trigger notification when a new blog post is published.', |
| 47 |
'merge_tags' => Notifier_Notification_Merge_Tags::get_merge_tags( array('Post') ), |
| 48 |
'recipient_fields' => array(), |
| 49 |
'action' => array ( |
| 50 |
'hook' => 'transition_post_status', |
| 51 |
'args_num' => 3, |
| 52 |
'callback' => function ( $new_status, $old_status, $post ) { |
| 53 |
if ('post' !== get_post_type($post)) { |
| 54 |
return; |
| 55 |
} |
| 56 |
|
| 57 |
if ( 'publish' === $new_status && 'publish' !== $old_status ) { |
| 58 |
$args = array ( |
| 59 |
'object_type' => 'post', |
| 60 |
'object_id' => $post->ID |
| 61 |
); |
| 62 |
$merge_tags = Notifier_Notification_Merge_Tags::get_merge_tags( array('Post') ); |
| 63 |
self::send_trigger_request($args, $merge_tags); |
| 64 |
} |
| 65 |
} |
| 66 |
) |
| 67 |
), |
| 68 |
array( |
| 69 |
'id' => 'new_comment', |
| 70 |
'label' => 'New comment is added', |
| 71 |
'description' => 'Trigger notification when a new comment is added.', |
| 72 |
'merge_tags' => Notifier_Notification_Merge_Tags::get_merge_tags( array('Comment') ), |
| 73 |
'recipient_fields' => array(), |
| 74 |
'action' => array ( |
| 75 |
'hook' => 'comment_post', |
| 76 |
'args_num' => 3, |
| 77 |
'callback' => function ( $comment_id, $comment_approved, $commentdata ) { |
| 78 |
if ( 'spam' != $comment_approved) { |
| 79 |
$args = array ( |
| 80 |
'object_type' => 'comment', |
| 81 |
'object_id' => $comment_id |
| 82 |
); |
| 83 |
$merge_tags = Notifier_Notification_Merge_Tags::get_merge_tags( array('Comment') ); |
| 84 |
self::send_trigger_request($args, $merge_tags); |
| 85 |
} |
| 86 |
} |
| 87 |
) |
| 88 |
), |
| 89 |
array( |
| 90 |
'id' => 'new_user', |
| 91 |
'label' => 'New user is registered', |
| 92 |
'description' => 'Trigger notification when a new user is created.', |
| 93 |
'merge_tags' => Notifier_Notification_Merge_Tags::get_merge_tags( array('User') ), |
| 94 |
'recipient_fields' => array(), |
| 95 |
'action' => array ( |
| 96 |
'hook' => 'user_register', |
| 97 |
'callback' => function ( $user_id ) { |
| 98 |
$args = array ( |
| 99 |
'object_type' => 'user', |
| 100 |
'object_id' => $user_id |
| 101 |
); |
| 102 |
$merge_tags = Notifier_Notification_Merge_Tags::get_merge_tags( array('User') ); |
| 103 |
self::send_trigger_request($args, $merge_tags); |
| 104 |
} |
| 105 |
) |
| 106 |
) |
| 107 |
); |
| 108 |
return apply_filters('notifier_notification_triggers', $triggers); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Get notification triggers for dropdown |
| 113 |
*/ |
| 114 |
public static function get_notification_triggers_dropdown() { |
| 115 |
$main_triggers = self::get_notification_triggers(); |
| 116 |
$dropdown_triggers = array('' => 'Select a trigger'); |
| 117 |
foreach ($main_triggers as $key => $triggers) { |
| 118 |
$dropdown_triggers[$key] = wp_list_pluck($triggers, 'label', 'id'); |
| 119 |
} |
| 120 |
return $dropdown_triggers; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Check whether current trigger is enabled |
| 125 |
*/ |
| 126 |
public static function is_trigger_enabled($trigger) { |
| 127 |
$enabled_triggers = get_option('notifier_enabled_triggers'); |
| 128 |
|
| 129 |
if(empty($enabled_triggers)){ |
| 130 |
return false; |
| 131 |
} |
| 132 |
|
| 133 |
if(in_array($trigger, $enabled_triggers)){ |
| 134 |
return true; |
| 135 |
} |
| 136 |
|
| 137 |
return false; |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Send triggered notifications |
| 142 |
*/ |
| 143 |
public static function send_trigger_request($context_args, $merge_tags, $recipient_fields = array()) { |
| 144 |
if(empty($context_args) || empty($merge_tags)){ |
| 145 |
return false; |
| 146 |
} |
| 147 |
|
| 148 |
$data = array(); |
| 149 |
$recipient_data = array(); |
| 150 |
|
| 151 |
foreach($merge_tags as $group_name => $group_tags){ |
| 152 |
foreach($group_tags as $tag){ |
| 153 |
$data[$tag['id']] = Notifier_Notification_Merge_Tags::get_trigger_merge_tag_value($tag['id'], $context_args); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
foreach($recipient_fields as $recipient_field){ |
| 158 |
foreach($recipient_field as $field){ |
| 159 |
$recipient_data[$field['id']] = self::get_trigger_recipient_field_value($field['id'], $context_args); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
$params = array( |
| 164 |
'action' => 'fire_notification', |
| 165 |
'site_url' => site_url(), |
| 166 |
'source' => 'wp', |
| 167 |
'merge_tags_data' => $data, |
| 168 |
'recipient_fields' => $recipient_data |
| 169 |
); |
| 170 |
|
| 171 |
$response = Notifier::send_api_request( $params, 'POST' ); |
| 172 |
|
| 173 |
if($response->error){ |
| 174 |
error_log($response->message); |
| 175 |
} |
| 176 |
|
| 177 |
} |
| 178 |
|
| 179 |
/* |
| 180 |
* Get recipient fields |
| 181 |
*/ |
| 182 |
public static function get_recipient_fields($types = array()){ |
| 183 |
$recipient_fields = array(); |
| 184 |
$recipient_fields = apply_filters('notifier_notification_recipient_fields', $recipient_fields); |
| 185 |
|
| 186 |
$final_recipient_fields = array(); |
| 187 |
|
| 188 |
if (empty($types)) { |
| 189 |
$final_recipient_fields = $recipient_fields; |
| 190 |
} else { |
| 191 |
foreach ($types as $type) { |
| 192 |
$final_recipient_fields[$type] = $recipient_fields[$type]; |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
return $recipient_fields; |
| 197 |
} |
| 198 |
|
| 199 |
/* |
| 200 |
* Get trigger recipient fields |
| 201 |
*/ |
| 202 |
public static function get_trigger_recipient_fields($trigger){ |
| 203 |
$recipient_fields = array(); |
| 204 |
$main_triggers = self::get_notification_triggers(); |
| 205 |
foreach ($main_triggers as $key => $triggers) { |
| 206 |
foreach ($triggers as $t) { |
| 207 |
if ( $trigger == $t['id'] ) { |
| 208 |
$recipient_fields = $t['recipient_fields']; |
| 209 |
break 2; |
| 210 |
} |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
$final_fields = array(); |
| 215 |
|
| 216 |
foreach ($recipient_fields as $field_key => $recipient_fields_list) { |
| 217 |
foreach($recipient_fields_list as $field) { |
| 218 |
$final_fields[$field_key][$field['id']] = $field['label']; |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
return $final_fields; |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* Get recipient_field value |
| 227 |
*/ |
| 228 |
public static function get_trigger_recipient_field_value($recipient_field, $context_args) { |
| 229 |
$value = ''; |
| 230 |
$main_triggers = self::get_notification_triggers(); |
| 231 |
foreach ($main_triggers as $key => $triggers) { |
| 232 |
foreach ($triggers as $t) { |
| 233 |
$recipient_fields = (!empty($t['recipient_fields'])) ? $t['recipient_fields'] : array(); |
| 234 |
if(empty($recipient_fields)){ |
| 235 |
continue; |
| 236 |
} |
| 237 |
foreach($recipient_fields as $fields){ |
| 238 |
foreach($fields as $field){ |
| 239 |
if ( isset($field['id']) && $recipient_field == $field['id'] ) { |
| 240 |
$value = $field['value']($context_args); |
| 241 |
break 2; |
| 242 |
} |
| 243 |
} |
| 244 |
} |
| 245 |
} |
| 246 |
} |
| 247 |
return $value; |
| 248 |
} |
| 249 |
|
| 250 |
} |
| 251 |
|