Repository navigation
Expand file tree
/
Copy pathclass-alerts-trigger-engine.php
More file actions
106 lines (94 loc) · 2.17 KB
/
Copy pathclass-alerts-trigger-engine.php
File metadata and controls
106 lines (94 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
/**
* Matches hydrated alerts against a Stream record.
*
* @package WP_Stream
*/
namespace WP_Stream;
/**
* Class Alerts_Trigger_Engine
*
* @package WP_Stream
*/
class Alerts_Trigger_Engine {
/**
* Holds Instance of plugin object
*
* @var Plugin
*/
public $plugin;
/**
* Class constructor.
*
* @param Plugin $plugin Instance of plugin object.
*/
public function __construct( $plugin ) {
$this->plugin = $plugin;
$this->register_hooks();
}
/**
* Register record-insert matching hooks.
*
* @return void
*/
public function register_hooks() {
add_filter(
'wp_stream_record_inserted',
array(
$this,
'check_records',
),
10,
2
);
}
/**
* Checks record being processed against active alerts.
*
* @filter wp_stream_record_inserted
*
* @param int $record_id The record being processed.
* @param array $recordarr Record data.
*
* @return array
*/
public function check_records( $record_id, $recordarr ) {
$args = array(
'post_type' => Alerts::POST_TYPE,
'post_status' => 'wp_stream_enabled',
);
$alerts = new \WP_Query( $args );
$hydrated = array();
foreach ( $alerts->posts as $alert ) {
$hydrated[] = $this->plugin->alerts->get_alert( $alert->ID );
}
foreach ( $this->matching_alerts( $hydrated, $record_id, $recordarr ) as $alert ) {
$alert->send_alert( $record_id, $recordarr ); // @todo send_alert expects int, not array.
}
return $recordarr;
}
/**
* Return alerts whose check_record() result is true.
*
* Matching goes through Alert::check_record() so the
* wp_stream_alert_trigger_check filter still applies.
*
* @param array $alerts Hydrated Alert objects (false entries are skipped).
* @param int $record_id The record being processed.
* @param array $recordarr Record data.
* @return array Alert objects that matched.
*/
public function matching_alerts( array $alerts, $record_id, array $recordarr ) {
$matches = array();
foreach ( $alerts as $alert ) {
if ( false === $alert ) {
continue;
}
$status = $alert->check_record( $record_id, $recordarr );
if ( $status ) {
$matches[] = $alert;
}
}
return $matches;
}
}