-
Notifications
You must be signed in to change notification settings - Fork 116
/
class-connector.php
297 lines (253 loc) · 7.34 KB
/
class-connector.php
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<?php
/**
* Abstract class serving as the parent for all logger classes AKA "Connectors".
* Common functionality for registering log events are defined here.
*
* @package WP_Stream;
*/
namespace WP_Stream;
/**
* Class - Connector
*/
abstract class Connector {
/**
* Connector slug
*
* @var string
*/
public $name = null;
/**
* Actions registered for this connector
*
* @var array
*/
public $actions = array();
/**
* Store delayed logs
*
* @var array
*/
public $delayed = array();
/**
* Previous Stream entry in same request
*
* @var int
*/
public $prev_stream = null;
/**
* Register connector in the WP Admin
*
* @var bool
*/
public $register_admin = true;
/**
* Register connector in the WP Frontend
*
* @var bool
*/
public $register_frontend = true;
/**
* Holds connector registration status flag.
*
* @var bool
*/
private $is_registered = false;
/**
* Is the connector currently registered?
*
* @return boolean
*/
public function is_registered() {
return $this->is_registered;
}
/**
* Register all context hooks
*/
public function register() {
if ( $this->is_registered ) {
return;
}
foreach ( $this->actions as $action ) {
add_action( $action, array( $this, 'callback' ), 10, 99 );
}
add_filter( 'wp_stream_action_links_' . $this->name, array( $this, 'action_links' ), 10, 2 );
$this->is_registered = true;
}
/**
* Unregister all context hooks
*/
public function unregister() {
if ( ! $this->is_registered ) {
return;
}
foreach ( $this->actions as $action ) {
remove_action( $action, array( $this, 'callback' ), 10, 99 );
}
remove_filter( 'wp_stream_action_links_' . $this->name, array( $this, 'action_links' ), 10, 2 );
$this->is_registered = false;
}
/**
* Callback for all registered hooks throughout Stream
* Looks for a class method with the convention: "callback_{action name}"
*/
public function callback() {
$action = current_filter();
$callback = array( $this, 'callback_' . preg_replace( '/[^a-z0-9_\-]/', '_', $action ) );
// For the sake of testing, trigger an action with the name of the callback.
if ( defined( 'WP_STREAM_TESTS' ) && WP_STREAM_TESTS ) {
/**
* Action fires during testing to test the current callback
*
* @param array $callback Callback name
*/
do_action( 'wp_stream_test_' . $callback[1] );
}
// Call the real function.
if ( is_callable( $callback ) ) {
return call_user_func_array( $callback, func_get_args() );
}
}
/**
* Add action links to Stream drop row in admin list screen
*
* @param array $links Previous links registered.
* @param object $record Stream record.
*
* @filter wp_stream_action_links_{connector}
*
* @return array Action links
*/
public function action_links( $links, $record ) {
unset( $record );
return $links;
}
/**
* Log handler
*
* @param string $message sprintf-ready error message string.
* @param array $args sprintf (and extra) arguments to use.
* @param int $object_id Target object id.
* @param string $context Context of the event.
* @param string $action Action of the event.
* @param int $user_id User responsible for the event.
*
* @return bool
*/
public function log( $message, $args, $object_id, $context, $action, $user_id = null ) {
$connector = $this->name;
$data = apply_filters(
'wp_stream_log_data',
compact( 'connector', 'message', 'args', 'object_id', 'context', 'action', 'user_id' )
);
if ( ! $data ) {
return false;
} else {
$connector = $data['connector'];
$message = $data['message'];
$args = $data['args'];
$object_id = $data['object_id'];
$context = $data['context'];
$action = $data['action'];
$user_id = $data['user_id'];
}
return call_user_func_array( array( wp_stream_get_instance()->log, 'log' ), compact( 'connector', 'message', 'args', 'object_id', 'context', 'action', 'user_id' ) );
}
/**
* Save log data till shutdown, so other callbacks would be able to override
*
* @param string $handle Special slug to be shared with other actions.
* @note param mixed $arg1 Extra arguments to sent to log()
* @note param param mixed $arg2, etc..
*/
public function delayed_log( $handle ) {
$args = func_get_args();
array_shift( $args );
$this->delayed[ $handle ] = $args;
add_action( 'shutdown', array( $this, 'delayed_log_commit' ) );
}
/**
* Commit delayed logs saved by @delayed_log
*/
public function delayed_log_commit() {
foreach ( $this->delayed as $handle => $args ) {
call_user_func_array( array( $this, 'log' ), $args );
}
}
/**
* Compare two values and return changed keys if they are arrays
*
* @param mixed $old_value Value before change.
* @param mixed $new_value Value after change.
* @param bool|int $deep Get array children changes keys as well, not just parents.
*
* @return array
*/
public function get_changed_keys( $old_value, $new_value, $deep = false ) {
if ( ! is_array( $old_value ) && ! is_array( $new_value ) ) {
return array();
}
if ( ! is_array( $old_value ) ) {
return array_keys( $new_value );
}
if ( ! is_array( $new_value ) ) {
return array_keys( $old_value );
}
$diff = array_udiff_assoc(
$old_value,
$new_value,
function( $value1, $value2 ) {
// Compare potentially complex nested arrays.
return wp_json_encode( $value1 ) !== wp_json_encode( $value2 );
}
);
$result = array_keys( $diff );
// Find unexisting keys in old or new value.
$common_keys = array_keys( array_intersect_key( $old_value, $new_value ) );
$unique_keys_old = array_values( array_diff( array_keys( $old_value ), $common_keys ) );
$unique_keys_new = array_values( array_diff( array_keys( $new_value ), $common_keys ) );
$result = array_merge( $result, $unique_keys_old, $unique_keys_new );
// Remove numeric indexes.
$result = array_filter(
$result,
function( $value ) {
// @codingStandardsIgnoreStart
// check if is not valid number (is_int, is_numeric and ctype_digit are not enough)
return (string) (int) $value !== (string) $value;
// @codingStandardsIgnoreEnd
}
);
$result = array_values( array_unique( $result ) );
if ( false === $deep ) {
return $result; // Return an numerical based array with changed TOP PARENT keys only.
}
$result = array_fill_keys( $result, null );
foreach ( $result as $key => $val ) {
if ( in_array( $key, $unique_keys_old, true ) ) {
$result[ $key ] = false; // Removed.
} elseif ( in_array( $key, $unique_keys_new, true ) ) {
$result[ $key ] = true; // Added.
} elseif ( $deep ) { // Changed, find what changed, only if we're allowed to explore a new level.
if ( is_array( $old_value[ $key ] ) && is_array( $new_value[ $key ] ) ) {
$inner = array();
$parent = $key;
$deep--;
$changed = $this->get_changed_keys( $old_value[ $key ], $new_value[ $key ], $deep );
foreach ( $changed as $child => $change ) {
$inner[ $parent . '::' . $child ] = $change;
}
$result[ $key ] = 0; // Changed parent which has a changed children.
$result = array_merge( $result, $inner );
}
}
}
return $result;
}
/**
* Allow connectors to determine if their dependencies is satisfied or not
*
* @return bool
*/
public function is_dependency_satisfied() {
return true;
}
}