/**
 * Insert wprss_feed_item posts into the DB
 *
 * @since 3.0
 */
function wprss_items_insert_post($items, $feed_ID)
{
    update_post_meta($feed_ID, 'wprss_feed_is_updating', $update_started_at = time());
    wprss_log_obj('Starting import of items for feed ' . $feed_ID, $update_started_at, null, WPRSS_LOG_LEVEL_INFO);
    // Gather the permalinks of existing feed item's related to this feed source
    $existing_permalinks = wprss_get_existing_permalinks($feed_ID);
    // Count of items inserted
    $items_inserted = 0;
    foreach ($items as $item) {
        // Normalize the URL
        $permalink = wprss_normalize_permalink($item->get_permalink());
        wprss_log_obj('Importing item', $permalink, null, WPRSS_LOG_LEVEL_INFO);
        wprss_log_obj('Original permalink', $item->get_permalink(), null, WPRSS_LOG_LEVEL_SYSTEM);
        // Save the enclosure URL
        $enclosure_url = '';
        if ($enclosure = $item->get_enclosure(0)) {
            wprss_log('Item has an enclosure', null, WPRSS_LOG_LEVEL_SYSTEM);
            if ($enclosure->get_link()) {
                $enclosure_url = $enclosure->get_link();
                wprss_log_obj('Enclosure has link', $enclosure_url, null, WPRSS_LOG_LEVEL_SYSTEM);
            }
        }
        /* OLD NORMALIZATION CODE - TO NORMALIZE URLS FROM PROXY URLS
        			$response = wp_remote_head( $permalink );
        			if ( !is_wp_error(  $response ) && isset( $response['headers']['location'] ) ) {
        				$permalink = current( explode( '?', $response['headers']['location'] ) );
        			}*/
        // Check if newly fetched item already present in existing feed items,
        // if not insert it into wp_posts and insert post meta.
        if (!array_key_exists($permalink, $existing_permalinks)) {
            wprss_log("Importing (unique) feed item (Source: {$feed_ID})", null, WPRSS_LOG_LEVEL_INFO);
            // Extend the importing time and refresh the feed's updating flag to reflect that it is active
            $extend_time = wprss_flag_feed_as_updating($feed_ID);
            $extend_time_f = date('Y-m-d H:i:s', $extend_time);
            $time_limit = wprss_get_item_import_time_limit();
            wprss_log("Extended execution time limit by {$time_limit}. (Current Time: {$extend_time_f})", null, WPRSS_LOG_LEVEL_INFO);
            set_time_limit($time_limit);
            // Apply filters that determine if the feed item should be inserted into the DB or not.
            $item = apply_filters('wprss_insert_post_item_conditionals', $item, $feed_ID, $permalink);
            // Check if the imported count should still be updated, even if the item is NULL
            $still_update_count = apply_filters('wprss_still_update_import_count', FALSE);
            // If the item is not NULL, continue to inserting the feed item post into the DB
            if ($item !== NULL && !is_bool($item)) {
                wprss_log('Using core logic', null, WPRSS_LOG_LEVEL_SYSTEM);
                // Get the date and GTM date and normalize if not valid dor not present
                $format = 'Y-m-d H:i:s';
                $has_date = $item->get_date('U') ? TRUE : FALSE;
                $timestamp = $has_date ? $item->get_date('U') : date('U');
                $date = date($format, $timestamp);
                $date_gmt = gmdate($format, $timestamp);
                // Prepare the item data
                $feed_item = apply_filters('wprss_populate_post_data', array('post_title' => html_entity_decode($item->get_title()), 'post_content' => '', 'post_status' => 'publish', 'post_type' => 'wprss_feed_item', 'post_date' => $date, 'post_date_gmt' => $date_gmt), $item);
                wprss_log('Post data filters applied', null, WPRSS_LOG_LEVEL_SYSTEM);
                if (defined('ICL_SITEPRESS_VERSION')) {
                    @(include_once WP_PLUGIN_DIR . '/sitepress-multilingual-cms/inc/wpml-api.php');
                }
                if (defined('ICL_LANGUAGE_CODE')) {
                    $_POST['icl_post_language'] = $language_code = ICL_LANGUAGE_CODE;
                    wprss_log_obj('WPML detected. Language code determined', $language_code, null, WPRSS_LOG_LEVEL_SYSTEM);
                }
                // Create and insert post object into the DB
                $inserted_ID = wp_insert_post($feed_item);
                if (!is_wp_error($inserted_ID)) {
                    if (is_object($inserted_ID)) {
                        if (isset($inserted_ID['ID'])) {
                            $inserted_ID = $inserted_ID['ID'];
                        } elseif (isset($inserted_ID->ID)) {
                            $inserted_ID = $inserted_ID->ID;
                        }
                    }
                    // Increment the inserted items counter
                    $items_inserted++;
                    // Create and insert post meta into the DB
                    wprss_items_insert_post_meta($inserted_ID, $item, $feed_ID, $permalink, $enclosure_url);
                    // Remember newly added permalink
                    $existing_permalinks[$permalink] = 1;
                    wprss_log_obj('Item imported', $inserted_ID, null, WPRSS_LOG_LEVEL_INFO);
                } else {
                    update_post_meta($source, 'wprss_error_last_import', 'An error occurred while inserting a feed item into the database.');
                    wprss_log_obj('Failed to insert post', $feed_item, 'wprss_items_insert_post > wp_insert_post');
                }
            } elseif (is_bool($item) && $item === TRUE || $still_update_count === TRUE && $item !== FALSE) {
                $items_inserted++;
            }
        } else {
            wprss_log('Item already exists and will be skipped', null, WPRSS_LOG_LEVEL_NOTICE);
        }
        wprss_log_obj('Finished importing item', $permalink, null, WPRSS_LOG_LEVEL_INFO);
    }
    update_post_meta($feed_ID, 'wprss_last_update_items', $items_inserted);
    wprss_log_obj(sprintf('Finished importing %1$d items for feed source', $items_inserted), $feed_ID, null, WPRSS_LOG_LEVEL_INFO);
}
/**
 * The AJAX function for the 'Fetch Feed Items' row action on the
 * 'All Feed Sources' page.
 *
 * @since 3.3
 */
function wprss_fetch_feeds_action_hook()
{
    if (isset($_POST['id']) && !empty($_POST['id'])) {
        if (!current_user_can('edit_feed_sources')) {
            die;
        }
        $id = $_POST['id'];
        update_post_meta($id, 'wprss_force_next_fetch', '1');
        // Prepare the schedule args
        $schedule_args = array(strval($id));
        // Get the current schedule - do nothing if not scheduled
        $next_scheduled = wp_next_scheduled('wprss_fetch_single_feed_hook', $schedule_args);
        if ($next_scheduled !== FALSE) {
            // If scheduled, unschedule it
            wp_unschedule_event($next_scheduled, 'wprss_fetch_single_feed_hook', $schedule_args);
            // Get the interval option for the feed source
            $interval = get_post_meta($id, 'wprss_update_interval', TRUE);
            // if the feed source uses its own interval
            if ($interval !== '' && $interval !== wprss_get_default_feed_source_update_interval()) {
                // Add meta in feed source. This is used to notify the source that it needs to reschedule it
                update_post_meta($id, 'wprss_reschedule_event', $next_scheduled);
            }
        }
        // Schedule the event for 5 seconds from now
        wp_schedule_single_event(time() + 1, 'wprss_fetch_single_feed_hook', $schedule_args);
        wprss_flag_feed_as_updating($id);
        die;
    }
}