| @@ -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,33 +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 | - if ( WP_DEBUG && WP_DEBUG_LOG ) { | |
| 18 | - \add_action( 'activitypub_safe_remote_post_response', array( self::class, 'log_remote_post_responses' ), 10, 4 ); | |
| 23 | + if ( WP_DEBUG_LOG ) { | |
| 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 | 27 | } |
| 21 | 28 | |
| 22 | - public static function log_remote_post_responses( $response, $url, $body, $user_id ) { | |
| 23 | - // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log, WordPress.PHP.DevelopmentFunctions.error_log_print_r | |
| 24 | - \error_log( "Request to: {$url} with response: " . \print_r( $response, 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 ) ); | |
| 25 | 38 | } |
| 26 | 39 | |
| 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 ); | |
| 49 | + | |
| 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 ) ); | |
| 55 | + } | |
| 56 | + } | |
| 57 | + | |
| 58 | + /** | |
| 59 | + * Write a log entry. | |
| 60 | + * | |
| 61 | + * @param mixed $log The log entry. | |
| 62 | + */ | |
| 27 | 63 | public static function write_log( $log ) { |
| 28 | - if ( \is_array( $log ) || \is_object( $log ) ) { | |
| 29 | - // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log, WordPress.PHP.DevelopmentFunctions.error_log_print_r | |
| 30 | - \error_log( \print_r( $log, true ) ); | |
| 31 | - } else { | |
| 32 | - // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log | |
| 33 | - \error_log( $log ); | |
| 34 | - } | |
| 64 | + // phpcs:ignore WordPress.PHP.DevelopmentFunctions | |
| 65 | + \error_log( \print_r( $log, true ) ); | |
| 35 | 66 | } |
| 36 | 67 | } |