PluginProbe
Stream – Activity Log & Audit Trail / 4.0.2
Stream – Activity Log & Audit Trail v4.0.2
4.4.0 4.3.0 4.2.2 4.2.1 trunk 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.1 3.1.1 3.10.0 3.2.0 3.2.1 3.2.2 3.2.3 All 50 releases
← All changes | classes/class-connector.php +101 -27 3.0.54.0.2 View file →
@@ -1,7 +1,17 @@
1 1 <?php
2 +/**
3 + * Abstract class serving as the parent for all logger classes AKA "Connectors".
4 + * Common functionality for registering log events are defined here.
5 + *
6 + * @package WP_Stream;
7 + */
8 +
2 9 namespace WP_Stream;
3 10
11 +/**
12 + * Class - Connector
13 + */
4 14 abstract class Connector {
5 15 /**
6 16 * Connector slug
7 17 *
@@ -30,27 +40,80 @@
30 40 */
31 41 public $prev_stream = null;
32 42
33 43 /**
44 + * Register connector in the WP Admin
45 + *
46 + * @var bool
47 + */
48 + public $register_admin = true;
49 +
50 + /**
51 + * Register connector in the WP Frontend
52 + *
53 + * @var bool
54 + */
55 + public $register_frontend = true;
56 +
57 + /**
58 + * Holds connector registration status flag.
59 + *
60 + * @var bool
61 + */
62 + private $is_registered = false;
63 +
64 + /**
65 + * Is the connector currently registered?
66 + *
67 + * @return boolean
68 + */
69 + public function is_registered() {
70 + return $this->is_registered;
71 + }
72 +
73 + /**
34 74 * Register all context hooks
35 75 */
36 76 public function register() {
77 + if ( $this->is_registered ) {
78 + return;
79 + }
80 +
37 81 foreach ( $this->actions as $action ) {
38 82 add_action( $action, array( $this, 'callback' ), 10, 99 );
39 83 }
40 84
41 85 add_filter( 'wp_stream_action_links_' . $this->name, array( $this, 'action_links' ), 10, 2 );
86 +
87 + $this->is_registered = true;
42 88 }
43 89
44 90 /**
91 + * Unregister all context hooks
92 + */
93 + public function unregister() {
94 + if ( ! $this->is_registered ) {
95 + return;
96 + }
97 +
98 + foreach ( $this->actions as $action ) {
99 + remove_action( $action, array( $this, 'callback' ), 10, 99 );
100 + }
101 +
102 + remove_filter( 'wp_stream_action_links_' . $this->name, array( $this, 'action_links' ), 10, 2 );
103 +
104 + $this->is_registered = false;
105 + }
106 +
107 + /**
45 108 * Callback for all registered hooks throughout Stream
46 109 * Looks for a class method with the convention: "callback_{action name}"
47 110 */
48 111 public function callback() {
49 112 $action = current_filter();
50 - $callback = array( $this, 'callback_' . preg_replace( '/[^a-z0-9_\-]/', '_', $action ) );
113 + $callback = array( $this, 'callback_' . preg_replace( '/[^a-z0-9_]/', '_', $action ) );
51 114
52 - // For the sake of testing, trigger an action with the name of the callback
115 + // For the sake of testing, trigger an action with the name of the callback.
53 116 if ( defined( 'WP_STREAM_TESTS' ) && WP_STREAM_TESTS ) {
54 117 /**
55 118 * Action fires during testing to test the current callback
56 119 *
@@ -58,9 +121,9 @@
58 121 */
59 122 do_action( 'wp_stream_test_' . $callback[1] );
60 123 }
61 124
62 - // Call the real function
125 + // Call the real function.
63 126 if ( is_callable( $callback ) ) {
64 127 return call_user_func_array( $callback, func_get_args() );
65 128 }
66 129 }
@@ -67,10 +130,10 @@
67 130
68 131 /**
69 132 * Add action links to Stream drop row in admin list screen
70 133 *
71 - * @param array $links Previous links registered
72 - * @param object $record Stream record
134 + * @param array $links Previous links registered.
135 + * @param object $record Stream record.
73 136 *
74 137 * @filter wp_stream_action_links_{connector}
75 138 *
76 139 * @return array Action links
@@ -82,14 +145,14 @@
82 145
83 146 /**
84 147 * Log handler
85 148 *
86 - * @param string $message sprintf-ready error message string
87 - * @param array $args sprintf (and extra) arguments to use
88 - * @param int $object_id Target object id
89 - * @param string $context Context of the event
90 - * @param string $action Action of the event
91 - * @param int $user_id User responsible for the event
149 + * @param string $message sprintf-ready error message string.
150 + * @param array $args sprintf (and extra) arguments to use.
151 + * @param int $object_id Target object id.
152 + * @param string $context Context of the event.
153 + * @param string $action Action of the event.
154 + * @param int $user_id User responsible for the event.
92 155 *
93 156 * @return bool
94 157 */
95 158 public function log( $message, $args, $object_id, $context, $action, $user_id = null ) {
@@ -117,9 +180,9 @@
117 180
118 181 /**
119 182 * Save log data till shutdown, so other callbacks would be able to override
120 183 *
121 - * @param string $handle Special slug to be shared with other actions
184 + * @param string $handle Special slug to be shared with other actions.
122 185 * @note param mixed $arg1 Extra arguments to sent to log()
123 186 * @note param param mixed $arg2, etc..
124 187 */
125 188 public function delayed_log( $handle ) {
@@ -143,11 +206,11 @@
143 206
144 207 /**
145 208 * Compare two values and return changed keys if they are arrays
146 209 *
147 - * @param mixed $old_value Value before change
148 - * @param mixed $new_value Value after change
149 - * @param bool|int $deep Get array children changes keys as well, not just parents
210 + * @param mixed $old_value Value before change.
211 + * @param mixed $new_value Value after change.
212 + * @param bool|int $deep Get array children changes keys as well, not just parents.
150 213 *
151 214 * @return array
152 215 */
153 216 public function get_changed_keys( $old_value, $new_value, $deep = false ) {
@@ -165,16 +228,17 @@
165 228
166 229 $diff = array_udiff_assoc(
167 230 $old_value,
168 231 $new_value,
169 - function( $value1, $value2 ) {
170 - return maybe_serialize( $value1 ) !== maybe_serialize( $value2 );
232 + function ( $value1, $value2 ) {
233 + // Compare potentially complex nested arrays.
234 + return wp_json_encode( $value1 ) !== wp_json_encode( $value2 );
171 235 }
172 236 );
173 237
174 238 $result = array_keys( $diff );
175 239
176 - // find unexisting keys in old or new value
240 + // Find unexisting keys in old or new value.
177 241 $common_keys = array_keys( array_intersect_key( $old_value, $new_value ) );
178 242 $unique_keys_old = array_values( array_diff( array_keys( $old_value ), $common_keys ) );
179 243 $unique_keys_new = array_values( array_diff( array_keys( $new_value ), $common_keys ) );
180 244
@@ -179,12 +243,12 @@
179 243 $unique_keys_new = array_values( array_diff( array_keys( $new_value ), $common_keys ) );
180 244
181 245 $result = array_merge( $result, $unique_keys_old, $unique_keys_new );
182 246
183 - // remove numeric indexes
247 + // Remove numeric indexes.
184 248 $result = array_filter(
185 249 $result,
186 - function( $value ) {
250 + function ( $value ) {
187 251 // @codingStandardsIgnoreStart
188 252 // check if is not valid number (is_int, is_numeric and ctype_digit are not enough)
189 253 return (string) (int) $value !== (string) $value;
190 254 // @codingStandardsIgnoreEnd
@@ -193,9 +257,9 @@
193 257
194 258 $result = array_values( array_unique( $result ) );
195 259
196 260 if ( false === $deep ) {
197 - return $result; // Return an numerical based array with changed TOP PARENT keys only
261 + return $result; // Return an numerical based array with changed TOP PARENT keys only.
198 262 }
199 263
200 264 $result = array_fill_keys( $result, null );
201 265
@@ -200,22 +264,22 @@
200 264 $result = array_fill_keys( $result, null );
201 265
202 266 foreach ( $result as $key => $val ) {
203 267 if ( in_array( $key, $unique_keys_old, true ) ) {
204 - $result[ $key ] = false; // Removed
268 + $result[ $key ] = false; // Removed.
205 269 } elseif ( in_array( $key, $unique_keys_new, true ) ) {
206 - $result[ $key ] = true; // Added
207 - } elseif ( $deep ) { // Changed, find what changed, only if we're allowed to explore a new level
270 + $result[ $key ] = true; // Added.
271 + } elseif ( $deep ) { // Changed, find what changed, only if we're allowed to explore a new level.
208 272 if ( is_array( $old_value[ $key ] ) && is_array( $new_value[ $key ] ) ) {
209 273 $inner = array();
210 274 $parent = $key;
211 - $deep--;
275 + --$deep;
212 276 $changed = $this->get_changed_keys( $old_value[ $key ], $new_value[ $key ], $deep );
213 277 foreach ( $changed as $child => $change ) {
214 278 $inner[ $parent . '::' . $child ] = $change;
215 279 }
216 - $result[ $key ] = 0; // Changed parent which has a changed children
217 - $result = array_merge( $result, $inner );
280 + $result[ $key ] = 0; // Changed parent which has a changed children.
281 + $result = array_merge( $result, $inner );
218 282 }
219 283 }
220 284 }
221 285
@@ -228,6 +292,16 @@
228 292 * @return bool
229 293 */
230 294 public function is_dependency_satisfied() {
231 295 return true;
296 + }
297 +
298 + /**
299 + * Escape % characters in a string to avoid Uncaught ValueErrors in $this->log().
300 + *
301 + * @param string $value The string value to be escaped.
302 + * @return string The escaped string.
303 + */
304 + public function escape_percentages( $value ) {
305 + return str_replace( '%', '%%', $value );
232 306 }
233 307 }