| 1 |
<?php |
| 2 |
namespace Activitypub; |
| 3 |
|
| 4 |
use WP_DEBUG; |
| 5 |
use WP_DEBUG_LOG; |
| 6 |
|
| 7 |
/** |
| 8 |
* ActivityPub Debug Class |
| 9 |
* |
| 10 |
* @author Matthias Pfefferle |
| 11 |
*/ |
| 12 |
class Debug { |
| 13 |
/** |
| 14 |
* Initialize the class, registering WordPress hooks |
| 15 |
*/ |
| 16 |
public static function init() { |
| 17 |
if ( WP_DEBUG_LOG ) { |
| 18 |
\add_action( 'activitypub_safe_remote_post_response', array( self::class, 'log_remote_post_responses' ), 10, 4 ); |
| 19 |
} |
| 20 |
if ( is_user_logged_in() ) { |
| 21 |
\add_filter( 'comment_text', array( self::class, 'show_comment_source' ), 10, 2 ); |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
public static function show_comment_source( $comment_text, $comment ) { |
| 26 |
$was_sent = \get_comment_meta( $comment->comment_ID, 'activitypub_status', true ); |
| 27 |
|
| 28 |
if ( $was_sent ) { |
| 29 |
// translators: %s is the federation state of the comment |
| 30 |
$comment_text .= '<p><small>' . sprintf( \__( 'Activitypub → (%s)', 'activitypub' ), $was_sent ) . '</small></p>'; |
| 31 |
} |
| 32 |
|
| 33 |
$was_received = \get_comment_meta( $comment->comment_ID, 'protocol', true ); |
| 34 |
|
| 35 |
if ( 'activitypub' === $was_received ) { |
| 36 |
$comment_text .= '<p><small>' . \__( '← ActivityPub', 'activitypub' ) . '</small></p>'; |
| 37 |
} |
| 38 |
|
| 39 |
return $comment_text; |
| 40 |
} |
| 41 |
|
| 42 |
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed, VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 43 |
public static function log_remote_post_responses( $response, $url, $body, $user_id ) { |
| 44 |
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log, WordPress.PHP.DevelopmentFunctions.error_log_print_r |
| 45 |
\error_log( "Request to: {$url} with response: " . \print_r( $response, true ) ); |
| 46 |
} |
| 47 |
|
| 48 |
public static function write_log( $log ) { |
| 49 |
if ( \is_array( $log ) || \is_object( $log ) ) { |
| 50 |
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log, WordPress.PHP.DevelopmentFunctions.error_log_print_r |
| 51 |
\error_log( \print_r( $log, true ) ); |
| 52 |
} else { |
| 53 |
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log |
| 54 |
\error_log( $log ); |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
|