Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
Klaviyo_Logs
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
12
0.00% covered (danger)
0.00%
0 / 1
 link_to_klaviyo_profile_search
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2/**
3 * When printing the logs, replace instances of the Klaviyo user with links to the user profile.
4 *
5 * @package brianhenryie/bh-wp-autologin-urls
6 */
7
8namespace BrianHenryIE\WP_Autologin_URLs\Logger;
9
10use BrianHenryIE\WP_Autologin_URLs\WP_Logger\Admin\Logs_List_Table;
11use BrianHenryIE\WP_Autologin_URLs\WP_Logger\API\BH_WP_PSR_Logger;
12use BrianHenryIE\WP_Autologin_URLs\WP_Logger\Logger_Settings_Interface;
13
14/**
15 * Filters the column output of the WP_List_Table.
16 *
17 * @see Logs_List_Table::column_default()
18 */
19class Klaviyo_Logs {
20
21    /**
22     * Update `klaviyo:a1b2c3` with links Klaviyo.com search.
23     *
24     * `klaviyo:{$wp_user->ID} {$klaviyo_user_id}`
25     *
26     * There didn't seem to be any way to link directly to the profile.
27     *
28     * @hooked bh-wp-autologin-urls_bh_wp_logger_column
29     *
30     * @param string                                                                        $column_output The column output so far.
31     * @param array{time:string, level:string, message:string, context:array<string,mixed>} $item The log entry row.
32     * @param string                                                                        $column_name The current column name.
33     * @param Logger_Settings_Interface                                                     $logger_settings The logger settings.
34     * @param BH_WP_PSR_Logger                                                              $logger The logger API instance.
35     *
36     * @return string
37     */
38    public function link_to_klaviyo_profile_search( string $column_output, array $item, string $column_name, Logger_Settings_Interface $logger_settings, BH_WP_PSR_Logger $logger ): string {
39
40        if ( 'message' !== $column_name ) {
41            return $column_output;
42        }
43
44        $callback = function ( array $matches ): string {
45
46            $wp_user = get_user_by( 'id', $matches[1] );
47
48            if ( false === $wp_user ) {
49                return $matches[0];
50            }
51
52            $url  = 'https://www.klaviyo.com/search?q=' . $wp_user->user_email;
53            $link = "<a href=\"{$url}\">Klaviyo user {$matches[2]}</a>";
54
55            return $link;
56        };
57
58        $message = preg_replace_callback( '/klaviyo:(\d+)\s([^\s]+)/', $callback, $column_output ) ?? $column_output;
59
60        return $message;
61    }
62}