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