| @@ -1,5 +1,11 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * Debug Class. | |
| 4 | + * | |
| 5 | + * @package Activitypub | |
| 6 | + */ | |
| 7 | + | |
| 2 | 8 | namespace Activitypub; |
| 3 | 9 | |
| 4 | 10 | use WP_DEBUG; |
| 5 | 11 | use WP_DEBUG_LOG; |
| @@ -4,54 +10,58 @@ | ||
| 4 | 10 | use WP_DEBUG; |
| 5 | 11 | use WP_DEBUG_LOG; |
| 6 | 12 | |
| 7 | 13 | /** |
| 8 | - * ActivityPub Debug Class | |
| 14 | + * ActivityPub Debug Class. | |
| 9 | 15 | * |
| 10 | 16 | * @author Matthias Pfefferle |
| 11 | 17 | */ |
| 12 | 18 | class Debug { |
| 13 | 19 | /** |
| 14 | - * Initialize the class, registering WordPress hooks | |
| 20 | + * Initialize the class, registering WordPress hooks. | |
| 15 | 21 | */ |
| 16 | 22 | public static function init() { |
| 17 | 23 | if ( WP_DEBUG_LOG ) { |
| 18 | - \add_action( 'activitypub_safe_remote_post_response', array( self::class, 'log_remote_post_responses' ), 10, 4 ); | |
| 24 | + \add_action( 'activitypub_safe_remote_post_response', array( self::class, 'log_remote_post_responses' ), 10, 2 ); | |
| 25 | + \add_action( 'activitypub_inbox', array( self::class, 'log_inbox' ), 10, 3 ); | |
| 19 | 26 | } |
| 20 | - if ( is_user_logged_in() ) { | |
| 21 | - \add_filter( 'comment_text', array( self::class, 'show_comment_source' ), 10, 2 ); | |
| 22 | - } | |
| 23 | 27 | } |
| 24 | 28 | |
| 25 | - public static function show_comment_source( $comment_text, $comment ) { | |
| 26 | - $was_sent = \get_comment_meta( $comment->comment_ID, 'activitypub_status', true ); | |
| 29 | + /** | |
| 30 | + * Log the responses of remote post requests. | |
| 31 | + * | |
| 32 | + * @param array $response The response from the remote server. | |
| 33 | + * @param string $url The URL of the remote server. | |
| 34 | + */ | |
| 35 | + public static function log_remote_post_responses( $response, $url ) { | |
| 36 | + // phpcs:ignore WordPress.PHP.DevelopmentFunctions | |
| 37 | + \error_log( "[OUTBOX] Request to: {$url} with Response: " . \print_r( $response, true ) ); | |
| 38 | + } | |
| 27 | 39 | |
| 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 | - } | |
| 40 | + /** | |
| 41 | + * Log the inbox requests. | |
| 42 | + * | |
| 43 | + * @param array $data The Activity array. | |
| 44 | + * @param int $user_id The ID of the local blog user. | |
| 45 | + * @param string $type The type of the request. | |
| 46 | + */ | |
| 47 | + public static function log_inbox( $data, $user_id, $type ) { | |
| 48 | + $type = strtolower( $type ); | |
| 32 | 49 | |
| 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>'; | |
| 50 | + if ( 'delete' !== $type ) { | |
| 51 | + $actor = $data['actor'] ?? ''; | |
| 52 | + $url = object_to_uri( $actor ); | |
| 53 | + // phpcs:ignore WordPress.PHP.DevelopmentFunctions | |
| 54 | + \error_log( "[INBOX] Request From: {$url} with Activity: " . \print_r( $data, true ) ); | |
| 37 | 55 | } |
| 38 | - | |
| 39 | - return $comment_text; | |
| 40 | 56 | } |
| 41 | 57 | |
| 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 | - | |
| 58 | + /** | |
| 59 | + * Write a log entry. | |
| 60 | + * | |
| 61 | + * @param mixed $log The log entry. | |
| 62 | + */ | |
| 48 | 63 | 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 | - } | |
| 64 | + // phpcs:ignore WordPress.PHP.DevelopmentFunctions | |
| 65 | + \error_log( \print_r( $log, true ) ); | |
| 56 | 66 | } |
| 57 | 67 | } |