Example #1
0
/**
 * 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', time());
    // Gather the permalinks of existing feed item's related to this feed source
    $existing_permalinks = 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());
        // Save the enclosure URL
        $enclosure_url = '';
        if ($enclosure = $item->get_enclosure(0)) {
            if ($enclosure->get_link()) {
                $enclosure_url = $enclosure->get_link();
            }
        }
        /* 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 (!in_array($permalink, $existing_permalinks)) {
            // 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)) {
                $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' => get_date_from_gmt($item->get_date('Y-m-d H:i:s')), 'post_date_gmt' => $item->get_date('Y-m-d H:i:s')), $item);
                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;
                }
                // 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;
                } else {
                    update_post_meta($source, "wprss_error_last_import", "true");
                    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) {
                $items_inserted++;
            }
        }
    }
    update_post_meta($feed_ID, 'wprss_last_update_items', $items_inserted);
}
/**
 * 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);
}