Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
93.33% |
14 / 15 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| TNP_User_Hyperlink | |
93.33% |
14 / 15 |
|
0.00% |
0 / 1 |
4.00 | |
0.00% |
0 / 1 |
| replace_tnp_user_id_with_link | |
93.33% |
14 / 15 |
|
0.00% |
0 / 1 |
4.00 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * When logging with bh-wp-logger, replace mentions of Newsletter subscribers with links to their user info page. |
| 4 | * |
| 5 | * @see admin.php?page=newsletter_users_edit&id= |
| 6 | * |
| 7 | * @package brianhenryie/bh-wp-aws-ses-bounce-handler |
| 8 | */ |
| 9 | |
| 10 | namespace BrianHenryIE\AWS_SES_Bounce_Handler\Logger; |
| 11 | |
| 12 | use BrianHenryIE\AWS_SES_Bounce_Handler\WP_Logger\API\BH_WP_PSR_Logger; |
| 13 | use BrianHenryIE\AWS_SES_Bounce_Handler\WP_Logger\Logger_Settings_Interface; |
| 14 | |
| 15 | /** |
| 16 | * Filter on `bh-wp-aws-bounce-handler_bh_wp_logger_column` and replace `tnp_user:123` with links to `admin.php?page=newsletter_users_edit&id=`. |
| 17 | */ |
| 18 | class TNP_User_Hyperlink { |
| 19 | |
| 20 | /** |
| 21 | * Update `tnp_user:123` with links to the Newsletter subscriber page. |
| 22 | * Use preg_replace_callback to find and replace all instances in the string. |
| 23 | * |
| 24 | * @hooked {$plugin_slug}_bh_wp_logger_column |
| 25 | * |
| 26 | * @param string $column_output The column output so far. |
| 27 | * @param array{time:string, level:string, message:string, context:array} $item The log entry row. |
| 28 | * @param string $column_name The current column name. |
| 29 | * @param Logger_Settings_Interface $logger_settings The logger settings. |
| 30 | * @param BH_WP_PSR_Logger $bh_wp_psr_logger The logger API instance. |
| 31 | * |
| 32 | * @return string |
| 33 | */ |
| 34 | public function replace_tnp_user_id_with_link( string $column_output, array $item, string $column_name, Logger_Settings_Interface $logger_settings, BH_WP_PSR_Logger $bh_wp_psr_logger ): string { |
| 35 | |
| 36 | if ( 'message' !== $column_name ) { |
| 37 | return $column_output; |
| 38 | } |
| 39 | |
| 40 | $callback = function( array $matches ): string { |
| 41 | |
| 42 | $tnp_user_id = $matches[1]; |
| 43 | $tnp_user_display_name = $tnp_user_id; |
| 44 | |
| 45 | if ( class_exists( \Newsletter::class ) ) { |
| 46 | $newsletter = \Newsletter::instance(); |
| 47 | $user = $newsletter->get_user( $tnp_user_id ); |
| 48 | |
| 49 | if ( ! empty( $user ) ) { |
| 50 | $tnp_user_display_name = $user->email; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | $url = admin_url( "admin.php?page=newsletter_users_edit&id={$matches[1]}" ); |
| 55 | $link = "<a href=\"{$url}\">Newsletter subscriber {$tnp_user_display_name}</a>"; |
| 56 | |
| 57 | return $link; |
| 58 | }; |
| 59 | |
| 60 | $message = preg_replace_callback( '/`tnp_user:(\d+)`/', $callback, $column_output ) ?? $column_output; |
| 61 | |
| 62 | return $message; |
| 63 | } |
| 64 | } |