PluginProbe
WANotifier for Forms and Actions / 1.0.2
WANotifier for Forms and Actions v1.0.2
3.1.1 3.1.0 3.0.4 2.7.10 2.7.11 2.7.12 2.7.13 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 3.0.0 3.0.1 3.0.2 3.0.3 trunk 0.1.0 0.1.1 1.0.0 1.0.1 1.0.2 All 67 releases
notifier / includes / classes / class-notifier-notification-triggers.php

class-notifier-notification-triggers.php in WANotifier for Forms and Actions 1.0.2, at includes/classes/class-notifier-notification-triggers.php

252 lines 7.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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('new_post', $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('new_comment', $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('new_user', $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($trigger, $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 'trigger' => $trigger,
168 'merge_tags_data' => $data,
169 'recipient_fields' => $recipient_data
170 );
171
172 $response = Notifier::send_api_request( $params, 'POST' );
173
174 if($response->error){
175 error_log($response->message);
176 }
177
178 }
179
180 /*
181 * Get recipient fields
182 */
183 public static function get_recipient_fields($types = array()){
184 $recipient_fields = array();
185 $recipient_fields = apply_filters('notifier_notification_recipient_fields', $recipient_fields);
186
187 $final_recipient_fields = array();
188
189 if (empty($types)) {
190 $final_recipient_fields = $recipient_fields;
191 } else {
192 foreach ($types as $type) {
193 $final_recipient_fields[$type] = $recipient_fields[$type];
194 }
195 }
196
197 return $recipient_fields;
198 }
199
200 /*
201 * Get trigger recipient fields
202 */
203 public static function get_trigger_recipient_fields($trigger){
204 $recipient_fields = array();
205 $main_triggers = self::get_notification_triggers();
206 foreach ($main_triggers as $key => $triggers) {
207 foreach ($triggers as $t) {
208 if ( $trigger == $t['id'] ) {
209 $recipient_fields = $t['recipient_fields'];
210 break 2;
211 }
212 }
213 }
214
215 $final_fields = array();
216
217 foreach ($recipient_fields as $field_key => $recipient_fields_list) {
218 foreach($recipient_fields_list as $field) {
219 $final_fields[$field_key][$field['id']] = $field['label'];
220 }
221 }
222
223 return $final_fields;
224 }
225
226 /**
227 * Get recipient_field value
228 */
229 public static function get_trigger_recipient_field_value($recipient_field, $context_args) {
230 $value = '';
231 $main_triggers = self::get_notification_triggers();
232 foreach ($main_triggers as $key => $triggers) {
233 foreach ($triggers as $t) {
234 $recipient_fields = (!empty($t['recipient_fields'])) ? $t['recipient_fields'] : array();
235 if(empty($recipient_fields)){
236 continue;
237 }
238 foreach($recipient_fields as $fields){
239 foreach($fields as $field){
240 if ( isset($field['id']) && $recipient_field == $field['id'] ) {
241 $value = $field['value']($context_args);
242 break 2;
243 }
244 }
245 }
246 }
247 }
248 return $value;
249 }
250
251 }
252