/**
  * Import the feeds on schedule
  */
 function do_hourly()
 {
     $engine = new rssPIEngine();
     $engine->import_feed();
 }
 /**
  * Import any feeds
  */
 function ajax_import()
 {
     global $rss_post_importer;
     // if there's nothing for processing or invalid data, bail
     if (!isset($_POST['feed'])) {
         wp_send_json_error(array('message' => 'no feed provided'));
     }
     $_found = false;
     foreach ($this->options['feeds'] as $id => $f) {
         if ($f['id'] == $_POST['feed']) {
             $_found = $id;
             break;
         }
     }
     if ($_found === false) {
         wp_send_json_error(array('message' => 'wrong feed id provided'));
     }
     // TODO: make this better
     if ($_found == 0) {
         // check for valid key only for the first feed
         $this->is_key_valid = $rss_post_importer->is_valid_key($this->options['settings']['feeds_api_key']);
         $this->options['settings']['is_key_valid'] = $this->is_key_valid;
         // if the key is not fine
         if (!empty($this->options['settings']['feeds_api_key']) && !$this->is_key_valid) {
             // unset from settings
             unset($this->options['settings']['feeds_api_key']);
         }
         // update options
         $new_options = array('feeds' => $this->options['feeds'], 'settings' => $this->options['settings'], 'latest_import' => $this->options['latest_import'], 'imports' => $this->options['imports'], 'upgraded' => $this->options['upgraded']);
         // update in db
         update_option('rss_pi_feeds', $new_options);
     }
     $post_count = 0;
     $f = $this->options['feeds'][$_found];
     $engine = new rssPIEngine();
     // filter cache lifetime
     add_filter('wp_feed_cache_transient_lifetime', array($engine, 'frequency'));
     // prepare, import feed and count imported posts
     if ($items = $engine->do_import($f)) {
         $post_count += count($items);
     }
     remove_filter('wp_feed_cache_transient_lifetime', array($engine, 'frequency'));
     // reformulate import count
     $imports = intval($this->options['imports']) + $post_count;
     // update options
     $new_options = array('feeds' => $this->options['feeds'], 'settings' => $this->options['settings'], 'latest_import' => date("Y-m-d H:i:s"), 'imports' => $imports, 'upgraded' => $this->options['upgraded']);
     // update in db
     update_option('rss_pi_feeds', $new_options);
     global $rss_post_importer;
     // reload options
     $rss_post_importer->load_options();
     // log this
     rssPILog::log($post_count);
     wp_send_json_success(array('count' => $post_count, 'url' => $f['url']));
 }