Skip to content

Commit

Permalink
feat(content-distribution): log incoming post errors (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelpeixe authored Jan 10, 2025
1 parent a694795 commit 74c9119
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion includes/content-distribution/class-incoming-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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' ) );
}

Expand Down Expand Up @@ -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' ) );
}

Expand Down

0 comments on commit 74c9119

Please sign in to comment.