| @@ -1,17 +1,7 @@ | ||
| 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 | - | |
| 9 | 2 | namespace WP_Stream; |
| 10 | 3 | |
| 11 | -/** | |
| 12 | - * Class - Connector | |
| 13 | - */ | |
| 14 | 4 | abstract class Connector { |
| 15 | 5 | /** |
| 16 | 6 | * Connector slug |
| 17 | 7 | * |
| @@ -54,66 +44,27 @@ | ||
| 54 | 44 | */ |
| 55 | 45 | public $register_frontend = true; |
| 56 | 46 | |
| 57 | 47 | /** |
| 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 | - /** | |
| 74 | 48 | * Register all context hooks |
| 75 | 49 | */ |
| 76 | 50 | public function register() { |
| 77 | - if ( $this->is_registered ) { | |
| 78 | - return; | |
| 79 | - } | |
| 80 | - | |
| 81 | 51 | foreach ( $this->actions as $action ) { |
| 82 | 52 | add_action( $action, array( $this, 'callback' ), 10, 99 ); |
| 83 | 53 | } |
| 84 | 54 | |
| 85 | 55 | add_filter( 'wp_stream_action_links_' . $this->name, array( $this, 'action_links' ), 10, 2 ); |
| 86 | - | |
| 87 | - $this->is_registered = true; | |
| 88 | 56 | } |
| 89 | 57 | |
| 90 | 58 | /** |
| 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 | - /** | |
| 108 | 59 | * Callback for all registered hooks throughout Stream |
| 109 | 60 | * Looks for a class method with the convention: "callback_{action name}" |
| 110 | 61 | */ |
| 111 | 62 | public function callback() { |
| 112 | 63 | $action = current_filter(); |
| 113 | - $callback = array( $this, 'callback_' . preg_replace( '/[^a-z0-9_]/', '_', $action ) ); | |
| 64 | + $callback = array( $this, 'callback_' . preg_replace( '/[^a-z0-9_\-]/', '_', $action ) ); | |
| 114 | 65 | |
| 115 | - // For the sake of testing, trigger an action with the name of the callback. | |
| 66 | + // For the sake of testing, trigger an action with the name of the callback | |
| 116 | 67 | if ( defined( 'WP_STREAM_TESTS' ) && WP_STREAM_TESTS ) { |
| 117 | 68 | /** |
| 118 | 69 | * Action fires during testing to test the current callback |
| 119 | 70 | * |
| @@ -121,9 +72,9 @@ | ||
| 121 | 72 | */ |
| 122 | 73 | do_action( 'wp_stream_test_' . $callback[1] ); |
| 123 | 74 | } |
| 124 | 75 | |
| 125 | - // Call the real function. | |
| 76 | + // Call the real function | |
| 126 | 77 | if ( is_callable( $callback ) ) { |
| 127 | 78 | return call_user_func_array( $callback, func_get_args() ); |
| 128 | 79 | } |
| 129 | 80 | } |
| @@ -130,10 +81,10 @@ | ||
| 130 | 81 | |
| 131 | 82 | /** |
| 132 | 83 | * Add action links to Stream drop row in admin list screen |
| 133 | 84 | * |
| 134 | - * @param array $links Previous links registered. | |
| 135 | - * @param object $record Stream record. | |
| 85 | + * @param array $links Previous links registered | |
| 86 | + * @param object $record Stream record | |
| 136 | 87 | * |
| 137 | 88 | * @filter wp_stream_action_links_{connector} |
| 138 | 89 | * |
| 139 | 90 | * @return array Action links |
| @@ -145,14 +96,14 @@ | ||
| 145 | 96 | |
| 146 | 97 | /** |
| 147 | 98 | * Log handler |
| 148 | 99 | * |
| 149 | - * @param string $message sprintf-ready error message string. | |
| 150 | - * @param array $args sprintf (and extra) arguments to use. | |
| 151 | - * @param int|null $object_id Target object id (if any). | |
| 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. | |
| 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 | |
| 155 | 106 | * |
| 156 | 107 | * @return bool |
| 157 | 108 | */ |
| 158 | 109 | public function log( $message, $args, $object_id, $context, $action, $user_id = null ) { |
| @@ -157,16 +108,8 @@ | ||
| 157 | 108 | */ |
| 158 | 109 | public function log( $message, $args, $object_id, $context, $action, $user_id = null ) { |
| 159 | 110 | $connector = $this->name; |
| 160 | 111 | |
| 161 | - /** | |
| 162 | - * Override the data logged. Returning false to this filter will stop the data from being logged. | |
| 163 | - * Examples of this filter in use can be found in some of the custom connectors. | |
| 164 | - * | |
| 165 | - * @see Connector_ACF::log_override() | |
| 166 | - * | |
| 167 | - * @return array|false An array of the data to be logged or false if it should not be logged. | |
| 168 | - */ | |
| 169 | 112 | $data = apply_filters( |
| 170 | 113 | 'wp_stream_log_data', |
| 171 | 114 | compact( 'connector', 'message', 'args', 'object_id', 'context', 'action', 'user_id' ) |
| 172 | 115 | ); |
| @@ -188,9 +131,9 @@ | ||
| 188 | 131 | |
| 189 | 132 | /** |
| 190 | 133 | * Save log data till shutdown, so other callbacks would be able to override |
| 191 | 134 | * |
| 192 | - * @param string $handle Special slug to be shared with other actions. | |
| 135 | + * @param string $handle Special slug to be shared with other actions | |
| 193 | 136 | * @note param mixed $arg1 Extra arguments to sent to log() |
| 194 | 137 | * @note param param mixed $arg2, etc.. |
| 195 | 138 | */ |
| 196 | 139 | public function delayed_log( $handle ) { |
| @@ -214,11 +157,11 @@ | ||
| 214 | 157 | |
| 215 | 158 | /** |
| 216 | 159 | * Compare two values and return changed keys if they are arrays |
| 217 | 160 | * |
| 218 | - * @param mixed $old_value Value before change. | |
| 219 | - * @param mixed $new_value Value after change. | |
| 220 | - * @param bool|int $deep Get array children changes keys as well, not just parents. | |
| 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 | |
| 221 | 164 | * |
| 222 | 165 | * @return array |
| 223 | 166 | */ |
| 224 | 167 | public function get_changed_keys( $old_value, $new_value, $deep = false ) { |
| @@ -236,10 +179,10 @@ | ||
| 236 | 179 | |
| 237 | 180 | $diff = array_udiff_assoc( |
| 238 | 181 | $old_value, |
| 239 | 182 | $new_value, |
| 240 | - function ( $value1, $value2 ) { | |
| 241 | - // Compare potentially complex nested arrays. | |
| 183 | + function( $value1, $value2 ) { | |
| 184 | + // Compare potentially complex nested arrays | |
| 242 | 185 | return wp_json_encode( $value1 ) !== wp_json_encode( $value2 ); |
| 243 | 186 | } |
| 244 | 187 | ); |
| 245 | 188 | |
| @@ -244,9 +187,9 @@ | ||
| 244 | 187 | ); |
| 245 | 188 | |
| 246 | 189 | $result = array_keys( $diff ); |
| 247 | 190 | |
| 248 | - // Find unexisting keys in old or new value. | |
| 191 | + // find unexisting keys in old or new value | |
| 249 | 192 | $common_keys = array_keys( array_intersect_key( $old_value, $new_value ) ); |
| 250 | 193 | $unique_keys_old = array_values( array_diff( array_keys( $old_value ), $common_keys ) ); |
| 251 | 194 | $unique_keys_new = array_values( array_diff( array_keys( $new_value ), $common_keys ) ); |
| 252 | 195 | |
| @@ -251,12 +194,12 @@ | ||
| 251 | 194 | $unique_keys_new = array_values( array_diff( array_keys( $new_value ), $common_keys ) ); |
| 252 | 195 | |
| 253 | 196 | $result = array_merge( $result, $unique_keys_old, $unique_keys_new ); |
| 254 | 197 | |
| 255 | - // Remove numeric indexes. | |
| 198 | + // remove numeric indexes | |
| 256 | 199 | $result = array_filter( |
| 257 | 200 | $result, |
| 258 | - function ( $value ) { | |
| 201 | + function( $value ) { | |
| 259 | 202 | // @codingStandardsIgnoreStart |
| 260 | 203 | // check if is not valid number (is_int, is_numeric and ctype_digit are not enough) |
| 261 | 204 | return (string) (int) $value !== (string) $value; |
| 262 | 205 | // @codingStandardsIgnoreEnd |
| @@ -265,9 +208,9 @@ | ||
| 265 | 208 | |
| 266 | 209 | $result = array_values( array_unique( $result ) ); |
| 267 | 210 | |
| 268 | 211 | if ( false === $deep ) { |
| 269 | - return $result; // Return an numerical based array with changed TOP PARENT keys only. | |
| 212 | + return $result; // Return an numerical based array with changed TOP PARENT keys only | |
| 270 | 213 | } |
| 271 | 214 | |
| 272 | 215 | $result = array_fill_keys( $result, null ); |
| 273 | 216 | |
| @@ -272,21 +215,21 @@ | ||
| 272 | 215 | $result = array_fill_keys( $result, null ); |
| 273 | 216 | |
| 274 | 217 | foreach ( $result as $key => $val ) { |
| 275 | 218 | if ( in_array( $key, $unique_keys_old, true ) ) { |
| 276 | - $result[ $key ] = false; // Removed. | |
| 219 | + $result[ $key ] = false; // Removed | |
| 277 | 220 | } elseif ( in_array( $key, $unique_keys_new, true ) ) { |
| 278 | - $result[ $key ] = true; // Added. | |
| 279 | - } elseif ( $deep ) { // Changed, find what changed, only if we're allowed to explore a new level. | |
| 221 | + $result[ $key ] = true; // Added | |
| 222 | + } elseif ( $deep ) { // Changed, find what changed, only if we're allowed to explore a new level | |
| 280 | 223 | if ( is_array( $old_value[ $key ] ) && is_array( $new_value[ $key ] ) ) { |
| 281 | 224 | $inner = array(); |
| 282 | 225 | $parent = $key; |
| 283 | - --$deep; | |
| 226 | + $deep--; | |
| 284 | 227 | $changed = $this->get_changed_keys( $old_value[ $key ], $new_value[ $key ], $deep ); |
| 285 | 228 | foreach ( $changed as $child => $change ) { |
| 286 | 229 | $inner[ $parent . '::' . $child ] = $change; |
| 287 | 230 | } |
| 288 | - $result[ $key ] = 0; // Changed parent which has a changed children. | |
| 231 | + $result[ $key ] = 0; // Changed parent which has a changed children | |
| 289 | 232 | $result = array_merge( $result, $inner ); |
| 290 | 233 | } |
| 291 | 234 | } |
| 292 | 235 | } |
| @@ -300,16 +243,6 @@ | ||
| 300 | 243 | * @return bool |
| 301 | 244 | */ |
| 302 | 245 | public function is_dependency_satisfied() { |
| 303 | 246 | return true; |
| 304 | - } | |
| 305 | - | |
| 306 | - /** | |
| 307 | - * Escape % characters in a string to avoid Uncaught ValueErrors in $this->log(). | |
| 308 | - * | |
| 309 | - * @param string $value The string value to be escaped. | |
| 310 | - * @return string The escaped string. | |
| 311 | - */ | |
| 312 | - public function escape_percentages( $value ) { | |
| 313 | - return str_replace( '%', '%%', $value ); | |
| 314 | 247 | } |
| 315 | 248 | } |