From 0683c2c3691b61032251e7b6073bad7ae2dba878 Mon Sep 17 00:00:00 2001 From: hughie molloy Date: Thu, 26 Mar 2020 14:35:54 +0000 Subject: [PATCH] Checking to see if a post is successfully inserted into the DB. Adding a UI warning if a post fails to be inserted. Also stating the status counts in the results message. --- admin-ui.php | 4 ++++ feedwordpress.php | 2 +- syndicatedlink.class.php | 6 ++++-- syndicatedpost.class.php | 11 ++++++++++- 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/admin-ui.php b/admin-ui.php index b089663..973affa 100644 --- a/admin-ui.php +++ b/admin-ui.php @@ -127,6 +127,10 @@ function _s( $n = 0, $plural = 's', $singular = '' ) { function fwp_update_set_results_message( $delta, $joiner = ';' ) { $mesg = array(); + if (isset($delta['new'])) : $mesg[] = ' '.$delta['new'].' new posts were syndicated'; endif; + if (isset($delta['updated']) and ($delta['updated'] != 0)) : $mesg[] = ' '.$delta['updated'].' existing posts were updated'; endif; + if (isset($delta['stored']) and ($delta['stored'] != 0)) : $mesg[] = ' '.$delta['stored'].' alternate versions of existing posts were stored for reference'; endif; + if (isset($delta['failed']) and ($delta['failed'] != 0)) : $mesg[] = ' Failed to import '.$delta['failed'].' post' . ($delta['failed'] != 1 ? 's' : ''); endif; $delta = wp_parse_args( $delta, diff --git a/feedwordpress.php b/feedwordpress.php index 1d266e0..14810f5 100644 --- a/feedwordpress.php +++ b/feedwordpress.php @@ -907,7 +907,7 @@ public function update( $uri = null, $crash_ts = null ) { // If at least one feed was hit for updating... if ( $pinged_that and is_null( $delta ) ) : // ... don't return error condition - $delta = array( 'new' => 0, 'updated' => 0, 'stored' => 0 ); + $delta = array('new' => 0, 'updated' => 0, 'stored' => 0, 'failed' => 0); endif; if ( $pinged_that and $timely ) : diff --git a/syndicatedlink.class.php b/syndicatedlink.class.php index 074ed94..49bdf9b 100644 --- a/syndicatedlink.class.php +++ b/syndicatedlink.class.php @@ -187,7 +187,7 @@ public function poll ($crash_ts = NULL) { // Success; clear out error setting, if any. $this->update_setting('update/error', NULL); - $new_count = array('new' => 0, 'updated' => 0, 'stored' => 0); + $new_count = array('new' => 0, 'updated' => 0, 'stored' => 0, 'failed' => 0); # -- Update Link metadata live from feed $channel = $this->magpie->channel; @@ -269,7 +269,9 @@ public function poll ($crash_ts = NULL) { $processed[] = $post->guid(); if ( ! $post->filtered()) : $new = $post->store(); - if ( $new !== false ) $new_count[$new]++; + if ( $new !== false ) { + $new_count[$new]++; + } endif; if ( !is_null($crash_ts) and (time() > $crash_ts)) : diff --git a/syndicatedpost.class.php b/syndicatedpost.class.php index 6548aef..f980ccb 100644 --- a/syndicatedpost.class.php +++ b/syndicatedpost.class.php @@ -1668,7 +1668,11 @@ public function store () { FeedWordPress::diagnostic('syndicated_posts', $diag); endif; - $this->insert_post(/*update=*/ $this->fresh_content_is_update(), $this->freshness()); + $post_id = $this->insert_post(/*update=*/ $this->fresh_content_is_update(), $this->freshness()); + + if ($post_id === false) { + return 'failed'; + } $hook = $this->fresh_storage_hook(); if ( !is_null($hook)) : @@ -1774,6 +1778,11 @@ function insert_post ($update = false, $freshness = 2) { $this->_wp_id = wp_insert_post($sdbpost, /*return wp_error=*/ true); + // If the post fails to insert mark as bad. + if (is_wp_error($this->_wp_id)) { + return false; + } + $dbpost['ID'] = $this->_wp_id; endif;