public function fetchPosts() { global $_wp_additional_image_sizes; global $wpdb; $lastRefreshTime = (int) get_option('wpab_last_refresh_time', 0); $fetchPeriod = (int) get_option('wpab_fetch_feed_period', 30) * MINUTE_IN_SECONDS; if (time() - $lastRefreshTime < $fetchPeriod) { return; } WPAutoblog_Log::log(null, 'Started updating feeds'); set_time_limit($fetchPeriod); update_option('wpab_last_refresh_time', time()); $body = null; $blogs = get_posts(array('post_type' => self::postName(), 'posts_per_page' => -1)); $cache_ids = array(); foreach ($blogs as $k => $blog) { $url = get_post_meta($blog->ID, WPAB_ID . '-feed', true) ?: get_post_meta($blog->ID, WPAB_ID . '-url', true); $feed = new SimplePie(); $feed->set_feed_url($url); $feed->set_cache_location('../cache/'); $feed->set_cache_duration($fetchPeriod); $feed->set_timeout(15); $result = $feed->init(); $feed->handle_content_type(); if (!$result) { WPAutoblog_Log::log($feed->error(), 'An error occured while fetching the feed: <i>' . $blog->post_title . '</i>'); continue; } $items = $feed->get_items(); foreach ($items as $item) { if (!in_array($item->get_id(), $cache_ids)) { $posts = get_posts(array('hierarchical' => false, 'meta_key' => WPAB_ID . '-uuid', 'meta_value' => $item->get_id(), 'post_type' => WPAutoblog_BlogFeedPost::postName())); if (count($posts) === 0) { $images = $this->extractImages($item->get_content()); $attachments = array(); $isFeaturedImageFeatured = get_field('featured_image_featured', $blog->ID); $isFirstImageFeatured = get_field('first_image_featured', $blog->ID); $featuredImageFetched = false; if ($isFeaturedImageFeatured) { $enclosure = $item->get_enclosure(); $featuredImageUrl = $enclosure->get_link(); if ($featuredImageUrl) { $attachments[] = WPAutoblog_BlogFeedPost::prepareAttachment($featuredImageUrl); $featuredImageFetched = true; } } if ($isFirstImageFeatured && count($images) && !$featuredImageFetched) { $attachments[] = WPAutoblog_BlogFeedPost::prepareAttachment($images[0]); } if ($this->readability->isAvailable()) { $body = $this->readability->parse($item->get_permalink()); } $cache_ids[] = $item->get_id(); $post_id = $this->createBlogPost($blog, $item, $body, $attachments); $this->assignCategories($post_id, $blog->ID); do_action(WPAB_ID . '_blog_post_created_from_feed', $post_id, $item, $attachments); WPAutoblog_Stat::incrementPostsCount($item->get_date('Y/m/d')); } } } } $postsToSweep = $wpdb->get_results('SELECT post_id FROM `wp_postmeta` WHERE meta_key = "' . WPAB_ID . '-uuid" GROUP BY meta_value HAVING COUNT(*) > 1 LIMIT 0, 100', ARRAY_A); $postsToSweepIds = array_map(function ($item) { return $item['post_id']; }, $postsToSweep); WPAutoblog_Sweeper::remove($postsToSweepIds); WPAutoblog_Log::log($feed, 'Finished updating feeds'); }
public static function install() { WPAutoblog_Log::install(); WPAutoblog_Stat::install(); }
public function prepare_items($from = null, $to = null) { $columns = $this->get_columns(); $this->_column_headers = array($columns, array(), array()); $this->items = WPAutoblog_Stat::getStats($from ?: null, $to ?: null); }