Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
94.12% covered (success)
94.12%
16 / 17
50.00% covered (danger)
50.00%
1 / 2
CRAP
n/a
0 / 0
pcg_log_event
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
pcg_log_redact_paths
91.67% covered (success)
91.67%
11 / 12
0.00% covered (danger)
0.00%
0 / 1
9.05
1<?php
2/**
3 * Shared logstash dispatch for the Plugin Conflicts Guardian feature.
4 *
5 * @package automattic/jetpack-mu-wpcom
6 */
7
8/**
9 * Emit an event to the `atomic_plugin_conflicts_guardian` logstash bucket.
10 *
11 * Thin wrapper around `Automattic\Jetpack\Jetpack_Mu_Wpcom::log2logstash()`
12 * that pins the feature bucket and redacts ABSPATH/WP_CONTENT_DIR prefixes
13 * from any string values in `$extra` so log lines don't leak the install layout.
14 *
15 * @param string $message Event message slug (e.g. "Activation blocked").
16 * @param array  $extra   Event-specific properties; JSON-encoded into the `extra` field.
17 * @return void
18 */
19function pcg_log_event( $message, array $extra ) {
20    \Automattic\Jetpack\Jetpack_Mu_Wpcom::log2logstash(
21        'atomic_plugin_conflicts_guardian',
22        $message,
23        pcg_log_redact_paths( $extra )
24    );
25}
26
27/**
28 * Recursively replace `ABSPATH` and `WP_CONTENT_DIR` prefixes inside string
29 * values with `.../` so log lines don't leak the install layout. Keeps the
30 * relative tail (`plugins/foo/bar.php`), which is the useful part for triage.
31 *
32 * @param mixed $value Scalar or array.
33 * @return mixed
34 */
35function pcg_log_redact_paths( $value ) {
36    if ( is_array( $value ) ) {
37        return array_map( 'pcg_log_redact_paths', $value );
38    }
39    if ( ! is_string( $value ) || '' === $value ) {
40        return $value;
41    }
42    // WP_CONTENT_DIR first — it's a longer prefix that's typically *under*
43    // ABSPATH on standard installs, so swapping ABSPATH first would shadow it.
44    $replacements = array();
45    if ( defined( 'WP_CONTENT_DIR' ) && '' !== WP_CONTENT_DIR ) {
46        $replacements[ rtrim( WP_CONTENT_DIR, '/' ) . '/' ] = '.../';
47    }
48    if ( defined( 'ABSPATH' ) && '' !== ABSPATH ) {
49        $replacements[ rtrim( ABSPATH, '/' ) . '/' ] = '.../';
50    }
51    if ( empty( $replacements ) ) {
52        return $value;
53    }
54    return strtr( $value, $replacements );
55}