Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(content-distribution): log incoming post errors #182

Merged
merged 5 commits into from
Jan 10, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading