diff --git a/includes/content-distribution/class-incoming-post.php b/includes/content-distribution/class-incoming-post.php index dda3ccbc..1a8b28f1 100644 --- a/includes/content-distribution/class-incoming-post.php +++ b/includes/content-distribution/class-incoming-post.php @@ -100,6 +100,21 @@ public function __construct( $payload ) { } } + /** + * Log a message. + * + * @param string $message The message to log. + * + * @return void + */ + protected function log( $message ) { + $prefix = '[Incoming Post]'; + if ( ! empty( $this->payload ) ) { + $prefix .= ' ' . $this->payload['network_post_id']; + } + Debugger::log( $prefix . ' ' . $message ); + } + /** * Validate a payload. * @@ -297,7 +312,7 @@ protected function upload_thumbnail() { $attachment_id = media_sideload_image( $thumbnail_url, $this->ID, '', 'id' ); if ( is_wp_error( $attachment_id ) ) { - Debugger::log( 'Failed to upload featured image for post ' . $this->ID . ' with message: ' . $attachment_id->get_error_message() ); + self::log( 'Failed to upload featured image for post ' . $this->ID . ' with message: ' . $attachment_id->get_error_message() ); return; } @@ -327,6 +342,7 @@ protected function update_taxonomy_terms() { if ( ! $term ) { $term = wp_insert_term( $term_data['name'], $taxonomy ); if ( is_wp_error( $term ) ) { + self::log( 'Failed to insert term ' . $term_data['name'] . ' for taxonomy ' . $taxonomy . ' with message: ' . $term->get_error_message() ); continue; } $term = get_term_by( 'id', $term['term_id'], $taxonomy, ARRAY_A ); @@ -395,6 +411,7 @@ public function insert( $payload = [] ) { if ( ! empty( $payload ) ) { $error = $this->update_payload( $payload ); if ( is_wp_error( $error ) ) { + self::log( 'Failed to update payload: ' . $error->get_error_message() ); return $error; } } @@ -410,6 +427,7 @@ public function insert( $payload = [] ) { ! empty( $current_payload ) && $current_payload['post_data']['modified_gmt'] > $post_data['modified_gmt'] ) { + self::log( 'Linked post content is newer than the post payload.' ); return new WP_Error( 'old_modified_date', __( 'Linked post content is newer than the post payload.', 'newspack-network' ) ); } @@ -446,11 +464,13 @@ public function insert( $payload = [] ) { $post_id = wp_insert_post( $postarr, true ); if ( is_wp_error( $post_id ) ) { + self::log( 'Failed to insert post with message: ' . $post_id->get_error_message() ); return $post_id; } // The wp_insert_post() function might return `0` on failure. if ( ! $post_id ) { + self::log( 'Failed to insert post.' ); return new WP_Error( 'insert_error', __( 'Error inserting post.', 'newspack-network' ) ); }