PluginProbe
Stream – Activity Log & Audit Trail / 3.6.1
Stream – Activity Log & Audit Trail v3.6.1
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 +92 -32 3.0.0 → 3.6.1 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,22 +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() {
37 - $class_name = get_called_class();
38 - $class = new $class_name;
77 + if ( $this->is_registered ) {
78 + return;
79 + }
39 80
40 - foreach ( $class->actions as $action ) {
41 - add_action( $action, array( $class, 'callback' ), null, 5 );
81 + foreach ( $this->actions as $action ) {
82 + add_action( $action, array( $this, 'callback' ), 10, 99 );
42 83 }
43 84
44 - add_filter( 'wp_stream_action_links_' . $class->name, array( $class, 'action_links' ), 10, 2 );
85 + add_filter( 'wp_stream_action_links_' . $this->name, array( $this, 'action_links' ), 10, 2 );
86 +
87 + $this->is_registered = true;
45 88 }
46 89
47 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 + /**
48 108 * Callback for all registered hooks throughout Stream
49 109 * Looks for a class method with the convention: "callback_{action name}"
50 110 */
51 111 public function callback() {
@@ -51,9 +111,9 @@
51 111 public function callback() {
52 112 $action = current_filter();
53 113 $callback = array( $this, 'callback_' . preg_replace( '/[^a-z0-9_\-]/', '_', $action ) );
54 114
55 - // 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.
56 116 if ( defined( 'WP_STREAM_TESTS' ) && WP_STREAM_TESTS ) {
57 117 /**
58 118 * Action fires during testing to test the current callback
59 119 *
@@ -61,9 +121,9 @@
61 121 */
62 122 do_action( 'wp_stream_test_' . $callback[1] );
63 123 }
64 124
65 - // Call the real function
125 + // Call the real function.
66 126 if ( is_callable( $callback ) ) {
67 127 return call_user_func_array( $callback, func_get_args() );
68 128 }
69 129 }
@@ -70,10 +130,10 @@
70 130
71 131 /**
72 132 * Add action links to Stream drop row in admin list screen
73 133 *
74 - * @param array $links Previous links registered
75 - * @param object $record Stream record
134 + * @param array $links Previous links registered.
135 + * @param object $record Stream record.
76 136 *
77 137 * @filter wp_stream_action_links_{connector}
78 138 *
79 139 * @return array Action links
@@ -85,20 +145,19 @@
85 145
86 146 /**
87 147 * Log handler
88 148 *
89 - * @param string $message sprintf-ready error message string
90 - * @param array $args sprintf (and extra) arguments to use
91 - * @param int $object_id Target object id
92 - * @param string $context Context of the event
93 - * @param string $action Action of the event
94 - * @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.
95 155 *
96 156 * @return bool
97 157 */
98 158 public function log( $message, $args, $object_id, $context, $action, $user_id = null ) {
99 - $class = get_called_class();
100 - $connector = str_replace( array( 'WP_Stream\\', 'Connector_' ), array( '', '' ), $class );
159 + $connector = $this->name;
101 160
102 161 $data = apply_filters(
103 162 'wp_stream_log_data',
104 163 compact( 'connector', 'message', 'args', 'object_id', 'context', 'action', 'user_id' )
@@ -121,9 +180,9 @@
121 180
122 181 /**
123 182 * Save log data till shutdown, so other callbacks would be able to override
124 183 *
125 - * @param string $handle Special slug to be shared with other actions
184 + * @param string $handle Special slug to be shared with other actions.
126 185 * @note param mixed $arg1 Extra arguments to sent to log()
127 186 * @note param param mixed $arg2, etc..
128 187 */
129 188 public function delayed_log( $handle ) {
@@ -147,11 +206,11 @@
147 206
148 207 /**
149 208 * Compare two values and return changed keys if they are arrays
150 209 *
151 - * @param mixed $old_value Value before change
152 - * @param mixed $new_value Value after change
153 - * @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.
154 213 *
155 214 * @return array
156 215 */
157 216 public function get_changed_keys( $old_value, $new_value, $deep = false ) {
@@ -170,15 +229,16 @@
170 229 $diff = array_udiff_assoc(
171 230 $old_value,
172 231 $new_value,
173 232 function( $value1, $value2 ) {
174 - return maybe_serialize( $value1 ) !== maybe_serialize( $value2 );
233 + // Compare potentially complex nested arrays.
234 + return wp_json_encode( $value1 ) !== wp_json_encode( $value2 );
175 235 }
176 236 );
177 237
178 238 $result = array_keys( $diff );
179 239
180 - // find unexisting keys in old or new value
240 + // Find unexisting keys in old or new value.
181 241 $common_keys = array_keys( array_intersect_key( $old_value, $new_value ) );
182 242 $unique_keys_old = array_values( array_diff( array_keys( $old_value ), $common_keys ) );
183 243 $unique_keys_new = array_values( array_diff( array_keys( $new_value ), $common_keys ) );
184 244
@@ -183,9 +243,9 @@
183 243 $unique_keys_new = array_values( array_diff( array_keys( $new_value ), $common_keys ) );
184 244
185 245 $result = array_merge( $result, $unique_keys_old, $unique_keys_new );
186 246
187 - // remove numeric indexes
247 + // Remove numeric indexes.
188 248 $result = array_filter(
189 249 $result,
190 250 function( $value ) {
191 251 // @codingStandardsIgnoreStart
@@ -197,19 +257,19 @@
197 257
198 258 $result = array_values( array_unique( $result ) );
199 259
200 260 if ( false === $deep ) {
201 - 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.
202 262 }
203 263
204 264 $result = array_fill_keys( $result, null );
205 265
206 266 foreach ( $result as $key => $val ) {
207 - if ( in_array( $key, $unique_keys_old ) ) {
208 - $result[ $key ] = false; // Removed
209 - } elseif ( in_array( $key, $unique_keys_new ) ) {
210 - $result[ $key ] = true; // Added
211 - } 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.
212 272 if ( is_array( $old_value[ $key ] ) && is_array( $new_value[ $key ] ) ) {
213 273 $inner = array();
214 274 $parent = $key;
215 275 $deep--;
@@ -216,10 +276,10 @@
216 276 $changed = $this->get_changed_keys( $old_value[ $key ], $new_value[ $key ], $deep );
217 277 foreach ( $changed as $child => $change ) {
218 278 $inner[ $parent . '::' . $child ] = $change;
219 279 }
220 - $result[ $key ] = 0; // Changed parent which has a changed children
221 - $result = array_merge( $result, $inner );
280 + $result[ $key ] = 0; // Changed parent which has a changed children.
281 + $result = array_merge( $result, $inner );
222 282 }
223 283 }
224 284 }
225 285