PluginProbe
Stream – Activity Log & Audit Trail / 3.8.2
Stream – Activity Log & Audit Trail v3.8.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 +91 -28 3.0.1 → 3.8.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,19 +40,72 @@
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 - add_action( $action, array( $this, 'callback' ), 10, 5 );
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() {
@@ -48,9 +111,9 @@
48 111 public function callback() {
49 112 $action = current_filter();
50 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,20 +145,19 @@
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 ) {
96 - $class = get_called_class();
97 - $connector = str_replace( array( 'WP_Stream\\', 'Connector_' ), array( '', '' ), $class );
159 + $connector = $this->name;
98 160
99 161 $data = apply_filters(
100 162 'wp_stream_log_data',
101 163 compact( 'connector', 'message', 'args', 'object_id', 'context', 'action', 'user_id' )
@@ -118,9 +180,9 @@
118 180
119 181 /**
120 182 * Save log data till shutdown, so other callbacks would be able to override
121 183 *
122 - * @param string $handle Special slug to be shared with other actions
184 + * @param string $handle Special slug to be shared with other actions.
123 185 * @note param mixed $arg1 Extra arguments to sent to log()
124 186 * @note param param mixed $arg2, etc..
125 187 */
126 188 public function delayed_log( $handle ) {
@@ -144,11 +206,11 @@
144 206
145 207 /**
146 208 * Compare two values and return changed keys if they are arrays
147 209 *
148 - * @param mixed $old_value Value before change
149 - * @param mixed $new_value Value after change
150 - * @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.
151 213 *
152 214 * @return array
153 215 */
154 216 public function get_changed_keys( $old_value, $new_value, $deep = false ) {
@@ -167,15 +229,16 @@
167 229 $diff = array_udiff_assoc(
168 230 $old_value,
169 231 $new_value,
170 232 function( $value1, $value2 ) {
171 - return maybe_serialize( $value1 ) !== maybe_serialize( $value2 );
233 + // Compare potentially complex nested arrays.
234 + return wp_json_encode( $value1 ) !== wp_json_encode( $value2 );
172 235 }
173 236 );
174 237
175 238 $result = array_keys( $diff );
176 239
177 - // find unexisting keys in old or new value
240 + // Find unexisting keys in old or new value.
178 241 $common_keys = array_keys( array_intersect_key( $old_value, $new_value ) );
179 242 $unique_keys_old = array_values( array_diff( array_keys( $old_value ), $common_keys ) );
180 243 $unique_keys_new = array_values( array_diff( array_keys( $new_value ), $common_keys ) );
181 244
@@ -180,9 +243,9 @@
180 243 $unique_keys_new = array_values( array_diff( array_keys( $new_value ), $common_keys ) );
181 244
182 245 $result = array_merge( $result, $unique_keys_old, $unique_keys_new );
183 246
184 - // remove numeric indexes
247 + // Remove numeric indexes.
185 248 $result = array_filter(
186 249 $result,
187 250 function( $value ) {
188 251 // @codingStandardsIgnoreStart
@@ -194,19 +257,19 @@
194 257
195 258 $result = array_values( array_unique( $result ) );
196 259
197 260 if ( false === $deep ) {
198 - 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.
199 262 }
200 263
201 264 $result = array_fill_keys( $result, null );
202 265
203 266 foreach ( $result as $key => $val ) {
204 - if ( in_array( $key, $unique_keys_old ) ) {
205 - $result[ $key ] = false; // Removed
206 - } elseif ( in_array( $key, $unique_keys_new ) ) {
207 - $result[ $key ] = true; // Added
208 - } elseif ( $deep ) { // Changed, find what changed, only if we're allowed to explore a new level
267 + if ( in_array( $key, $unique_keys_old, true ) ) {
268 + $result[ $key ] = false; // Removed.
269 + } elseif ( in_array( $key, $unique_keys_new, true ) ) {
270 + $result[ $key ] = true; // Added.
271 + } elseif ( $deep ) { // Changed, find what changed, only if we're allowed to explore a new level.
209 272 if ( is_array( $old_value[ $key ] ) && is_array( $new_value[ $key ] ) ) {
210 273 $inner = array();
211 274 $parent = $key;
212 275 $deep--;
@@ -213,10 +276,10 @@
213 276 $changed = $this->get_changed_keys( $old_value[ $key ], $new_value[ $key ], $deep );
214 277 foreach ( $changed as $child => $change ) {
215 278 $inner[ $parent . '::' . $child ] = $change;
216 279 }
217 - $result[ $key ] = 0; // Changed parent which has a changed children
218 - $result = array_merge( $result, $inner );
280 + $result[ $key ] = 0; // Changed parent which has a changed children.
281 + $result = array_merge( $result, $inner );
219 282 }
220 283 }
221 284 }
222 285