/** * Process a site and pull all it's posts * * @param int $site_id The ID of the site for which to pull it's posts * @param obj $client The syndication client class instance * @return array|bool Array of posts on success, false on failure */ public function process_site($site_id, $client) { global $site_manager, $client_manager; // Fetch the site status if (!in_array($site_manager->get_site_status($site_id), array('idle', ''))) { return false; } // Mark site as in progress $site_manager->update_site_status('pulling'); try { // Fetch the site's posts by calling the class located at the // namespace given during registration $posts = $client->get_posts($site_id); /** * Filter the array of posts to be processed by a pull client. * * @param array $posts The array of Types\Post objects to be pulled. * @param int $site_id The id of the site being processed. * @param Pull_Client $client The pull client processing the posts. */ $posts = apply_filters('syn_pre_pull_posts', $posts, $site_id, $client); // Process the posts we fetched $this->process_posts($posts, $site_id, $client); } catch (\Exception $e) { Syndication_Logger::log_post_error($site_id, $status = 'error', $message = __('Exception processing site.', 'push-syndication'), $log_time = null, $extra = array('error' => $e)); } // Update site status $site_manager->update_site_status('idle'); if (is_array($posts) && !empty($posts)) { return $posts; } else { return false; } }
/** * Handle the pull failure event. If the number of failures exceeds the maximum attempts set in the options, then disable the site. * * @param $site_id * @param $count */ public function handle_pull_failure_event($site_id, $count) { global $settings_manager; $site_id = (int) $site_id; $max_pull_attempts = (int) $settings_manager->get_setting('push_syndication_max_pull_attempts', 0); if (!$max_pull_attempts) { return; } if ($count >= $max_pull_attempts) { // Disable the site. update_post_meta($site_id, 'syn_site_enabled', false); // Reset the event counter. do_action('push_syndication_reset_event', 'pull_failure', $site_id); // Log what happened. Syndication_Logger::log_post_error($site_id, 'error', sprintf(__('Site disabled after %d pull failure(s).', 'push-syndication'), $count)); } }
/** * Handle the pull failure event. If the number of failures exceeds the maximum attempts set in the options, then disable the site. * * @param $site_id * @param $count */ public function handle_pull_failure_event($site_id, $count) { global $settings_manager; $site_id = (int) $site_id; $max_pull_attempts = (int) $settings_manager->get_setting('push_syndication_max_pull_attempts', 0); if (!$max_pull_attempts) { return; } if ($count >= $max_pull_attempts) { // Disable the site. update_post_meta($site_id, 'syn_site_enabled', false); /** * Fires when the site pull failure count exceeds the maximum pull attempts. * * Triggers a reset of the event counter. * * @param int $site_id The id of the site that failed. */ do_action('push_syndication_reset_event', 'pull_failure', $site_id); // Log what happened. Syndication_Logger::log_post_error($site_id, 'error', sprintf(__('Site disabled after %d pull failure(s).', 'push-syndication'), $count)); } }
/** * Prepares data for the post level log events * @param string $event Type of event new/update/delete * @param mixed $result Result object of previous wp_insert_post action * @param mixed $post Post object or post_id * @param object $site Post object for the site doing the syndication * @param string $transport_type Post meta syn_transport_type for site * @param object $client Syndication_Client class */ private function log_post_event($event, $result, $post, $site, $transport_type, $client) { if (is_int($post)) { $post = get_post($post, ARRAY_A); } if (isset($post['postmeta']) && isset($post['postmeta']['is_update'])) { $log_time = $post['postmeta']['is_update']; } else { $log_time = null; } $extra = array('post' => $post, 'result' => $result, 'transpost_type' => $transport_type, 'client' => $client); if (false == $result || is_wp_error($result)) { if (is_wp_error($result)) { $message = $result->get_error_message(); } else { $message = 'fail'; } Syndication_Logger::log_post_error($site->ID, $status = __(esc_attr($event), 'push-syndication'), $message, $log_time, $extra); } else { $guid = isset($post['post_guid']) ? sanitize_text_field($post['post_guid']) : sanitize_text_field($post['guid']); $message = sprintf('%s,%d', $guid, intval($result)); Syndication_Logger::log_post_success($site->ID, $status = __(esc_attr($event), 'push-syndication'), $message, $log_time, $extra); } }
/** * Handle a site pull failure event * * @param $site_id int The post id of the site we need to retry * @param $failed_attempts int The number of pull failures this site has experienced * * @return null */ public function handle_pull_failure_event($site_id = 0, $failed_attempts = 0) { global $settings_manager; $site_auto_retry_count = 0; $site_id = (int) $site_id; $failed_attempts = (int) $failed_attempts; $cleanup = false; // Fetch the allowable number of max pull attempts before the site is marked as 'disabled' $max_pull_attempts = (int) $settings_manager->get_setting('push_syndication_max_pull_attempts', 0); error_log('max times to retry: ' . $max_pull_attempts); // Bail if auto retry feature disabled if (!$max_pull_attempts) { return; } // Only proceed if we have a valid site id if (0 !== $site_id) { // Fetch the site post $site = get_post($site_id); // Fetch the site url $site_url = get_post_meta($site->ID, 'syn_feed_url', true); // Fetch the number of times we've tried to auto-retry $site_auto_retry_count = (int) get_post_meta($site_id, 'syn_failed_auto_retry_attempts', true); // Store the current time for repeated use below $time_now = time(); // Create a string time to be sent to the logger // Add 1 so our log items appear to occur a second later // and hence order better in the log viewer // without this, sometimes when the pull occurs quickly // these log items appear to occur at the same time as the failure $log_time = date('Y-m-d H:i:s', $time_now + 1); // Only proceed if we haven't hit the pull attempt ceiling if ($failed_attempts < $max_pull_attempts) { /** * Filter the auto retry limit. * * @param int $retry_limit The maximum number of times a client should auto_retry * when failing. Default is 3. */ $auto_retry_limit = apply_filters('pull_syndication_failure_auto_retry_limit', 3); // Are we still below the auto retry limit? if ($site_auto_retry_count <= $auto_retry_limit) { // Yes we are.. // Run in one minute by default /** * Filter the time to next retry when triggering an auto-retry. * * @param int $timestamp The time you want the auto-retry to occur. Default is one * minute from now ( time() + MINUTE_IN_SECONDS ). */ $auto_retry_interval = apply_filters('syndication_failure_auto_retry_interval', $time_now + MINUTE_IN_SECONDS); Syndication_Logger::log_post_info($site->ID, $status = 'start_auto_retry', $message = sprintf(__('Connection retry %d of %d to %s in %s..', 'push-syndication'), $site_auto_retry_count + 1, $auto_retry_limit, $site_url, human_time_diff($time_now, $auto_retry_interval)), '', $extra = array()); // Schedule a pull retry for one minute in the future wp_schedule_single_event($auto_retry_interval, 'syn_pull_content', array(array($site->ID))); // Increment our auto retry counter $site_auto_retry_count++; // And update the post meta auto retry count update_post_meta($site->ID, 'syn_failed_auto_retry_attempts', $site_auto_retry_count); } else { // Auto Retry limit met // Let's cleanup after ourselves $cleanup = true; } } else { // Retry attempt limit met // The site has been disabled, let's cleanup after ourselves $cleanup = true; } // Should we cleanup after ourselves? if ($cleanup) { // Remove the auto retry if there was one delete_post_meta($site->ID, 'syn_failed_auto_retry_attempts'); Syndication_Logger::log_post_error($site->ID, $status = 'end_auto_retry', $message = sprintf(__('Failed %d times to reconnect to %s', 'push-syndication'), ++$site_auto_retry_count, $site_url), $log_time, $extra = array()); } } }