/** * Changes the state of a feed source, using POST data * * @since 3.7 */ function wprss_change_feed_state() { // If the id and state are in POST data if (isset($_GET['wprss-feed-id'])) { // Get the id and state $feed_ID = $_GET['wprss-feed-id']; // Change the state if (wprss_is_feed_source_active($feed_ID)) { wprss_pause_feed_source($feed_ID); } else { wprss_activate_feed_source($feed_ID); } // Check for a redirect if (isset($_GET['wprss-redirect']) && $_GET['wprss-redirect'] == '1') { wp_redirect(admin_url('edit.php?post_type=wprss_feed', 301)); exit; } } }
/** * Show up the custom columns for the wprss_feed list * * @since 2.0 */ function wprss_show_custom_columns($column, $post_id) { switch ($column) { case 'errors': $errors = get_post_meta($post_id, 'wprss_error_last_import', true); $showClass = $errors === 'true' ? 'wprss-show' : ''; $msg = "This feed source experienced an error during the last feed fetch or validation check. Re-check the feed source URL or check the Error Log in the Debugging page for more details."; echo "<i title=\"{$msg}\" class=\"fa fa-warning fa-fw wprss-feed-error-symbol {$showClass}\"></i>"; break; case 'state': $active = wprss_is_feed_source_active($post_id); $text = $active ? 'Active' : 'Paused'; $button = $active ? 'Pause this feed source' : 'Activate this feed source'; $icon = $active ? 'pause' : 'play'; $value = $active ? 'paused' : 'active'; $indicator = $active ? 'green' : 'grey'; ?> <p> <span class="wprss-indicator-<?php echo $indicator; ?> " title="<?php echo $text; ?> "> <i class="fa fa-circle"></i> </span> <input type="hidden" name="wprss-redirect" value="1" /> <button type="submit" class='button-secondary' title="<?php echo $button; ?> " name="wprss-feed-id" value="<?php echo $post_id; ?> "> <i class='fa fa-<?php echo $icon; ?> '></i> </button> </p> <?php break; case 'updates': // Get the update interval $update_interval = get_post_meta($post_id, 'wprss_update_interval', TRUE); // Get the last updated and next update data $last_update = get_post_meta($post_id, 'wprss_last_update', TRUE); $last_update_items = get_post_meta($post_id, 'wprss_last_update_items', TRUE); $next_update = wprss_get_next_feed_source_update($post_id); // If using the global interval, get the timestamp of the next global update if ($update_interval === wprss_get_default_feed_source_update_interval() || $update_interval === '') { $next_update = wp_next_scheduled('wprss_fetch_all_feeds_hook', array()); } // Update the meta field if (wprss_is_feed_source_active($post_id)) { $next_update_text = $next_update === FALSE ? 'None' : human_time_diff($next_update, time()); } else { $next_update_text = 'Paused'; } update_post_meta($post_id, 'wprss_next_update', $next_update_text); ?> <p> Next update: <code class="next-update"> <?php echo $next_update_text; ?> </code> </p> <?php if ($last_update !== '') { ?> <p class="last-update-container"> Last updated: <code class="last-update"><?php echo human_time_diff($last_update, time()); ?> <?php _e('ago'); ?> </code> <?php if ($last_update_items !== '') { ?> <span class="last-update-imported-container"><br/>Last update imported: <code class="last-update-imported"><?php echo $last_update_items; ?> </code> items</span> <?php } ?> </p> <?php } break; case 'feed-count': $items = wprss_get_feed_items_for_source($post_id); $seconds_for_next_update = wprss_get_next_feed_source_update($post_id) - time(); $showClass = $seconds_for_next_update < 10 && $seconds_for_next_update > 0 || wprss_is_feed_source_deleting($post_id) ? 'wprss-show' : ''; echo '<p>'; echo "<span class=\"items-imported\">{$items->post_count}</span>"; echo "<i class=\"fa fa-fw fa-refresh fa-spin wprss-updating-feed-icon {$showClass}\" title=\"Updating feed source\"></i>"; echo '</p>'; // Set meta field for items imported update_post_meta($post_id, 'wprss_items_imported', $items->post_count); break; } }
/** * The main feed fetching function. * Fetches the feed items from the source provided and inserts them into the DB. * * Called on hook 'wprss_fetch_single_feed_hook'. * * @since 3.2 */ function wprss_fetch_insert_single_feed_items($feed_ID) { // Check if the feed source is active. if (wprss_is_feed_source_active($feed_ID) === FALSE && wprss_feed_source_force_next_fetch($feed_ID) === FALSE) { // If it is not active ( paused ), return without fetching the feed items. return; } // If the feed source is forced for next fetch, remove the force next fetch data if (wprss_feed_source_force_next_fetch($feed_ID)) { delete_post_meta($feed_ID, 'wprss_force_next_fetch'); } // Get the feed source URL from post meta, and filter it $feed_url = get_post_meta($feed_ID, 'wprss_url', true); $feed_url = apply_filters('wprss_feed_source_url', $feed_url, $feed_ID); // Get the feed limit from post meta $feed_limit = get_post_meta($feed_ID, 'wprss_limit', true); // Sanitize the limit. If smaller or equal to zero, or an empty string, set to NULL. $feed_limit = $feed_limit <= 0 || empty($feed_limit) ? NULL : $feed_limit; // Filter the URL for validaty if (filter_var($feed_url, FILTER_VALIDATE_URL)) { // Get the feed items from the source $items = wprss_get_feed_items($feed_url, $feed_ID); // If got NULL, convert to an empty array if ($items === NULL) { $items = array(); } // If the feed has its own meta limit, if ($feed_limit !== NULL) { // slice the items array using the feed meta limit // @todo - Check current number of feed items for source, and delete oldest to make room for new, to keep to the limit. // Use wprss_get_feed_items_for_source $items_to_insert = array_slice($items, 0, $feed_limit); // Gather the permalinks of existing feed item's related to this feed source $existing_permalinks = get_existing_permalinks($feed_ID); // Generate a list of items fetched, that are not already in the DB $new_items = array(); foreach ($items as $item) { $permalink = $item->get_permalink(); if (!in_array($permalink, $existing_permalinks)) { if (!is_array($new_items)) { $new_items = array(); } $new_items = array_push($new_items, $item); } } // Get the number of feed items in DB, and their count $db_feed_items = wprss_get_feed_items_for_source($feed_ID); $num_db_feed_items = $db_feed_items->post_count; // Get the number of feed items we can store until we reach the limit $num_can_insert = $feed_limit - $num_db_feed_items; // Calculate how many feed items we must delete before importing, to keep to the limit $num_feed_items_to_delete = count($new_items) - $num_can_insert; // Get an array with the DB feed items in reverse order (oldest first) $db_feed_items_reversed = array_reverse($db_feed_items->posts); // Cut the array to get only the first few that are to be deleted ( equal to $num_feed_items_to_delete ) $feed_items_to_delete = array_slice($db_feed_items_reversed, 0, $num_feed_items_to_delete); // Iterate the feed items and delete them foreach ($feed_items_to_delete as $key => $post) { wp_delete_post($post->ID, TRUE); } //$items_to_insert = $new_items; } else { $items_to_insert = $items; } // Insert the items into the db if (!empty($items_to_insert)) { wprss_items_insert_post($items_to_insert, $feed_ID); } } }
/** * */ function wprss_feed_source_updates() { $response = array(); if (!current_user_can('edit_feed_sources')) { return $response; } if (empty($_POST['wprss_heartbeat'])) { return $response; } // Get the wprss heartbeat data and extract the data $wprss_heartbeat = $_POST['wprss_heartbeat']; extract($wprss_heartbeat); // Perform the action specified by the heartbeat data switch ($action) { /* FEED SOURCE UPDATING STATUS * Used to determine whether or not to show the updating icon in the feed source table. */ case 'feed_sources': // Prepare array of IDs for feed sources currently updating $feed_sources_data = array(); // Iterate all feed sources foreach ($params as $feed_id) { $feed_sources_data[$feed_id] = array(); $feed_source_data =& $feed_sources_data[$feed_id]; // Check if the feed source is updating $seconds_for_next_update = wprss_get_next_feed_source_update($feed_id) - time(); $feed_source_data['updating'] = $seconds_for_next_update < 2 && $seconds_for_next_update > 0 || wprss_is_feed_source_updating($feed_id) || wprss_is_feed_source_deleting($feed_id); // Add the number of imported items $items = wprss_get_feed_items_for_source($feed_id); $feed_source_data['items'] = $items->post_count; // Update the meta field update_post_meta($feed_id, 'wprss_items_imported', $items->post_count); // Add the next update time $next_update = wprss_get_next_feed_source_update($feed_id); $update_interval = get_post_meta($feed_id, 'wprss_update_interval', TRUE); // If using the global interval, get the timestamp of the next global update if ($update_interval === wprss_get_default_feed_source_update_interval() || $update_interval === '') { $next_update = wp_next_scheduled('wprss_fetch_all_feeds_hook', array()); } // Set the text appropriately if (!wprss_is_feed_source_active($feed_id)) { $feed_source_data['next-update'] = __('Paused', WPRSS_TEXT_DOMAIN); } elseif ($next_update === FALSE) { $feed_source_data['next-update'] = __('None', WPRSS_TEXT_DOMAIN); } else { $feed_source_data['next-update'] = human_time_diff($next_update, time()); } // Update the meta field update_post_meta($feed_id, 'wprss_next_update', $feed_source_data['next-update']); // Add the last update information $last_update = get_post_meta($feed_id, 'wprss_last_update', TRUE); $last_update_items = get_post_meta($feed_id, 'wprss_last_update_items', TRUE); $feed_source_data['last-update'] = $last_update === '' ? '' : human_time_diff($last_update, time()); $feed_source_data['last-update-imported'] = $last_update_items; // Add any error info $errors = get_post_meta($feed_id, 'wprss_error_last_import', true); $feed_source_data['errors'] = $errors; } // Send back all the IDs $response['wprss_feed_sources_data'] = $feed_sources_data; break; } // Return the response die(json_encode($response)); }
/** * The main feed fetching function. * Fetches the feed items from the source provided and inserts them into the DB. * * Called on hook 'wprss_fetch_single_feed_hook'. * * @since 3.2 */ function wprss_fetch_insert_single_feed_items($feed_ID) { // Check if the feed source is active. if (wprss_is_feed_source_active($feed_ID) === FALSE && wprss_feed_source_force_next_fetch($feed_ID) === FALSE) { // If it is not active ( paused ), return without fetching the feed items. return; } // If the feed source is forced for next fetch, remove the force next fetch data if (wprss_feed_source_force_next_fetch($feed_ID)) { delete_post_meta($feed_ID, 'wprss_force_next_fetch'); } update_post_meta($feed_ID, 'wprss_feed_is_updating', time()); // Get the feed source URL from post meta, and filter it $feed_url = get_post_meta($feed_ID, 'wprss_url', true); $feed_url = apply_filters('wprss_feed_source_url', $feed_url, $feed_ID); // Get the feed limit from post meta $feed_limit = get_post_meta($feed_ID, 'wprss_limit', true); // If the feed has no individual limit if ($feed_limit === '' || intval($feed_limit) <= 0) { // Get the global limit $global_limit = wprss_get_general_setting('limit_feed_items_imported'); // If no global limit is set, mark as NULL if ($global_limit === '' || intval($global_limit) <= 0) { $feed_limit = NULL; } else { $feed_limit = $global_limit; } } // Filter the URL for validaty if (filter_var($feed_url, FILTER_VALIDATE_URL)) { // Get the feed items from the source $items = wprss_get_feed_items($feed_url, $feed_ID); // If got NULL, convert to an empty array if ($items === NULL) { $items = array(); } // If using a limit ... if ($feed_limit === NULL) { $items_to_insert = $items; } else { $items_to_insert = array_slice($items, 0, $feed_limit); } // Gather the permalinks of existing feed item's related to this feed source $existing_permalinks = get_existing_permalinks($feed_ID); // Generate a list of items fetched, that are not already in the DB $new_items = array(); foreach ($items_to_insert as $item) { $permalink = wprss_normalize_permalink($item->get_permalink()); // Check if not blacklisted and not already imported if (wprss_is_blacklisted($permalink) === FALSE && !in_array(trim($permalink), $existing_permalinks)) { $new_items[] = $item; } } $items_to_insert = $new_items; // If using a limit - delete any excess items to make room for the new items if ($feed_limit !== NULL) { // Get the number of feed items in DB, and their count $db_feed_items = wprss_get_feed_items_for_source($feed_ID); $num_db_feed_items = $db_feed_items->post_count; // Get the number of feed items we can store until we reach the limit $num_can_insert = $feed_limit - $num_db_feed_items; // Calculate how many feed items we must delete before importing, to keep to the limit $num_feed_items_to_delete = count($new_items) - $num_can_insert; // Get an array with the DB feed items in reverse order (oldest first) $db_feed_items_reversed = array_reverse($db_feed_items->posts); // Cut the array to get only the first few that are to be deleted ( equal to $num_feed_items_to_delete ) $feed_items_to_delete = array_slice($db_feed_items_reversed, 0, $num_feed_items_to_delete); // Iterate the feed items and delete them foreach ($feed_items_to_delete as $key => $post) { wp_delete_post($post->ID, TRUE); } } update_post_meta($feed_ID, 'wprss_last_update', time()); // Insert the items into the db if (!empty($items_to_insert)) { wprss_items_insert_post($items_to_insert, $feed_ID); } } else { wprss_log("The feed URL is not valid! Please recheck."); } $next_scheduled = get_post_meta($feed_ID, 'wprss_reschedule_event', TRUE); if ($next_scheduled !== '') { wprss_feed_source_update_start_schedule($feed_ID); delete_post_meta($feed_ID, 'wprss_reschedule_event'); } delete_post_meta($feed_ID, 'wprss_feed_is_updating'); }
/** * Show up the custom columns for the wprss_feed list * * @since 2.0 */ function wprss_show_custom_columns($column, $post_id) { switch ($column) { case 'url': $url = get_post_meta($post_id, 'wprss_url', true); echo '<a href="' . esc_url($url) . '">' . esc_url($url) . '</a>'; break; case 'description': $description = get_post_meta($post_id, 'wprss_description', true); echo esc_html($description); break; case 'id': echo esc_html($post_id); break; case 'state': $active = wprss_is_feed_source_active($post_id); $text = $active ? 'Active' : 'Paused'; $button = $active ? 'Pause this feed source' : 'Activate this feed source'; $icon = $active ? 'pause' : 'play'; $value = $active ? 'paused' : 'active'; $indicator = $active ? 'green' : 'grey'; ?> <p> <span class="wprss-indicator-<?php echo $indicator; ?> " title="<?php echo $text; ?> "> <i class="fa fa-circle"></i> </span> <input type="hidden" name="wprss-redirect" value="1" /> <button type="submit" class='button-secondary' title="<?php echo $button; ?> " name="wprss-feed-id" value="<?php echo $post_id; ?> "> <i class='fa fa-<?php echo $icon; ?> '></i> </button> </p> <?php break; case 'next-update': $interval = get_post_meta($post_id, 'wprss_update_interval', TRUE); $timestamp = wprss_get_next_feed_source_update($post_id); // If using the global interval, get the timestamp of the next glboal update if ($interval === wprss_get_default_feed_source_update_interval() || $interval === '') { $timestamp = wp_next_scheduled('wprss_fetch_all_feeds_hook', array()); } ?> <p> <code> <?php if (!wprss_is_feed_source_active($post_id)) { ?> Paused <?php } elseif ($timestamp === FALSE) { ?> None <?php } else { ?> <?php echo human_time_diff($timestamp, time()); ?> <?php } ?> </code> </p> <?php break; case 'feed-count': $items = wprss_get_feed_items_for_source($post_id); echo '<p>' . $items->post_count . '</p>'; break; } }
/** * The main feed fetching function. * Fetches the feed items from the source provided and inserts them into the DB. * * Called on hook 'wprss_fetch_single_feed_hook'. * * @since 3.2 */ function wprss_fetch_insert_single_feed_items($feed_ID) { wprss_log_obj('Starting import of feed', $feed_ID, null, WPRSS_LOG_LEVEL_INFO); global $wprss_importing_feed; $wprss_importing_feed = $feed_ID; register_shutdown_function('wprss_detect_exec_timeout'); // Check if the feed source is active. if (wprss_is_feed_source_active($feed_ID) === FALSE && wprss_feed_source_force_next_fetch($feed_ID) === FALSE) { // If it is not active ( paused ), return without fetching the feed items. wprss_log('Feed is not active and not forced. Import cancelled.', null, WPRSS_LOG_LEVEL_INFO); return; } // If the feed source is forced for next fetch, remove the force next fetch data if (wprss_feed_source_force_next_fetch($feed_ID)) { delete_post_meta($feed_ID, 'wprss_force_next_fetch'); wprss_log('Force feed flag removed', null, WPRSS_LOG_LEVEL_SYSTEM); } $start_of_update = wprss_flag_feed_as_updating($feed_ID); wprss_log_obj('Start of import time updated', date('Y-m-d H:i:s', $start_of_update), null, WPRSS_LOG_LEVEL_SYSTEM); // Get the feed source URL from post meta, and filter it $feed_url = get_post_meta($feed_ID, 'wprss_url', true); wprss_log_obj('Original feed source URL', $feed_url, null, WPRSS_LOG_LEVEL_SYSTEM); $feed_url = apply_filters('wprss_feed_source_url', $feed_url, $feed_ID); wprss_log_obj('Actual feed source URL', $feed_url, null, WPRSS_LOG_LEVEL_INFO); // Get the feed limit from post meta $feed_limit = get_post_meta($feed_ID, 'wprss_limit', true); wprss_log_obj('Feed limit value is', $feed_limit, null, WPRSS_LOG_LEVEL_SYSTEM); // If the feed has no individual limit if ($feed_limit === '' || intval($feed_limit) <= 0) { wprss_log_obj('Using global limit', $feed_limit, null, WPRSS_LOG_LEVEL_NOTICE); // Get the global limit $global_limit = wprss_get_general_setting('limit_feed_items_imported'); // If no global limit is set, mark as NULL if ($global_limit === '' || intval($global_limit) <= 0) { $feed_limit = NULL; } else { $feed_limit = $global_limit; } } wprss_log_obj('Feed import limit', $feed_limit, null, WPRSS_LOG_LEVEL_INFO); // Filter the URL for validaty if (wprss_validate_url($feed_url)) { wprss_log_obj('Feed URL is valid', $feed_url, null, WPRSS_LOG_LEVEL_INFO); // Get the feed items from the source $items = wprss_get_feed_items($feed_url, $feed_ID); // If got NULL, convert to an empty array if ($items === NULL) { $items = array(); wprss_log('Items were NULL. Using empty array', null, WPRSS_LOG_LEVEL_WARNING); } // If using a limit ... if ($feed_limit === NULL) { $items_to_insert = $items; } else { $items_to_insert = array_slice($items, 0, $feed_limit); wprss_log_obj('Sliced a segment of items', count($items_to_insert), null, WPRSS_LOG_LEVEL_SYSTEM); } // Gather the permalinks of existing feed item's related to this feed source $existing_permalinks = wprss_get_existing_permalinks($feed_ID); wprss_log_obj('Retrieved existing permalinks', count($existing_permalinks), null, WPRSS_LOG_LEVEL_SYSTEM); // Check if we should only import uniquely-titled feed items. $existing_titles = array(); $unique_titles = FALSE; if (wprss_get_general_setting('unique_titles')) { $unique_titles = TRUE; $existing_titles = wprss_get_existing_titles(); wprss_log_obj('Retrieved existing titles from global', count($existing_titles), null, WPRSS_LOG_LEVEL_SYSTEM); } else { if (get_post_meta($feed_ID, 'wprss_unique_titles', true) === 'true') { $unique_titles = TRUE; $existing_titles = wprss_get_existing_titles($feed_ID); wprss_log_obj('Retrieved existing titles from feed source', count($existing_titles), null, WPRSS_LOG_LEVEL_SYSTEM); } } // Generate a list of items fetched, that are not already in the DB $new_items = array(); foreach ($items_to_insert as $item) { $permalink = wprss_normalize_permalink($item->get_permalink()); wprss_log_obj('Normalizing permalink', sprintf('%1$s -> %2$s', $item->get_permalink(), $permalink), null, WPRSS_LOG_LEVEL_SYSTEM); // Check if not blacklisted and not already imported $is_blacklisted = wprss_is_blacklisted($permalink); $permalink_exists = array_key_exists($permalink, $existing_permalinks); $title_exists = array_key_exists($item->get_title(), $existing_titles); if ($is_blacklisted === FALSE && $permalink_exists === FALSE && $title_exists === FALSE) { $new_items[] = $item; wprss_log_obj('Permalink OK', $permalink, null, WPRSS_LOG_LEVEL_SYSTEM); if ($unique_titles) { $existing_titles[$item->get_title()] = 1; } } else { if ($is_blacklisted) { wprss_log('Permalink blacklisted', null, WPRSS_LOG_LEVEL_SYSTEM); } if ($permalink_exists) { wprss_log('Permalink already exists', null, WPRSS_LOG_LEVEL_SYSTEM); } if ($title_exists) { wprss_log('Title already exists', null, WPRSS_LOG_LEVEL_SYSTEM); } } } $original_count = count($items_to_insert); $new_count = count($new_items); if ($new_count !== $original_count) { wprss_log_obj('Items filtered out', $original_count - $new_count, null, WPRSS_LOG_LEVEL_SYSTEM); } else { wprss_log('Items to import remained untouched. Not items already exist or are blacklisted.', null, WPRSS_LOG_LEVEL_SYSTEM); } $items_to_insert = $new_items; // If using a limit - delete any excess items to make room for the new items if ($feed_limit !== NULL) { wprss_log_obj('Some items may be deleted due to limit', $feed_limit, null, WPRSS_LOG_LEVEL_SYSTEM); // Get the number of feed items in DB, and their count $db_feed_items = wprss_get_feed_items_for_source($feed_ID); $num_db_feed_items = $db_feed_items->post_count; // Get the number of feed items we can store until we reach the limit $num_can_insert = $feed_limit - $num_db_feed_items; // Calculate how many feed items we must delete before importing, to keep to the limit $num_new_items = count($new_items); $num_feed_items_to_delete = $num_can_insert > $num_new_items ? 0 : $num_new_items - $num_can_insert; // Get an array with the DB feed items in reverse order (oldest first) $db_feed_items_reversed = array_reverse($db_feed_items->posts); // Cut the array to get only the first few that are to be deleted ( equal to $num_feed_items_to_delete ) $feed_items_to_delete = array_slice($db_feed_items_reversed, 0, $num_feed_items_to_delete); wprss_log(sprintf('There already are %1$d items in the database. %2$d items can be inserted. %3$d items will be deleted', $num_db_feed_items, $num_can_insert, $num_feed_items_to_delete), null, WPRSS_LOG_LEVEL_SYSTEM); // Iterate the feed items and delete them foreach ($feed_items_to_delete as $key => $post) { wp_delete_post($post->ID, TRUE); } if ($deleted_items_count = count($feed_items_to_delete)) { wprss_log_obj('Items deleted due to limit', $deleted_items_count, null, WPRSS_LOG_LEVEL_NOTICE); } } update_post_meta($feed_ID, 'wprss_last_update', $last_update_time = time()); update_post_meta($feed_ID, 'wprss_last_update_items', 0); wprss_log_obj('Last import time updated', $last_update_time, null, WPRSS_LOG_LEVEL_SYSTEM); // Insert the items into the db if (!empty($items_to_insert)) { wprss_log_obj('There are items to insert', count($items_to_insert), null, WPRSS_LOG_LEVEL_INFO); wprss_items_insert_post($items_to_insert, $feed_ID); } } else { wprss_log_obj('The feed URL is not valid! Please recheck', $feed_url); } $next_scheduled = get_post_meta($feed_ID, 'wprss_reschedule_event', TRUE); if ($next_scheduled !== '') { wprss_feed_source_update_start_schedule($feed_ID); delete_post_meta($feed_ID, 'wprss_reschedule_event'); wprss_log('Next update rescheduled', null, WPRSS_LOG_LEVEL_SYSTEM); } wprss_flag_feed_as_idle($feed_ID); wprss_log_obj('Import complete', $feed_ID, __FUNCTION__, WPRSS_LOG_LEVEL_INFO); }
/** * Converts a single wprss_feed_item to a post. * * @param feed The wprss_feed_item object to convert * @param source The wprss_feed id of the feed item. Used to retrieve settings for conversion. * @since 1.0 */ public static function convert_to_post( $item, $source, $permalink ) { WPRSS_FTP_Utils::log( 'Starting conversion to post', __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_INFO ); $error_source = 'convert_to_post'; $source_obj = get_post( $source ); // If the feed source does not exist, exit if ( $source_obj === null || $source === '' ) { // unschedule any scheduled updates wprss_feed_source_update_stop_schedule( $source ); WPRSS_FTP_Utils::log_object( 'Source does not exist. Aborting.', $source, __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_WARNING ); return NULL; } else { // If the feed source exists, but is trashed or paused, exit if ( $source_obj->post_status !== 'trash' && !wprss_is_feed_source_active( $source ) ) { WPRSS_FTP_Utils::log_object( 'Source is inactive or trashed. Aborting.', $source, __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_NOTICE ); return NULL; } } # If we got NULL, pass it on if ( $item === NULL ) { WPRSS_FTP_Utils::log( 'Item is null. Aborting.', __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_WARNING ); return NULL; } # If the item has an empty permalink, log an error message if ( empty( $permalink ) ){ WPRSS_FTP_Utils::log( 'Encounted feed item with no permalink for feed source "' . $source_obj->post_title . '". Possibly a corrupt RSS feed.', $error_source, WPRSS_FTP_Utils::LOG_LEVEL_WARNING ); } # check existence of permalink $existing_permalinks = self::get_existing_permalinks( $source ); # If permalink exists, do nothing if ( in_array( $permalink, $existing_permalinks ) ) { WPRSS_FTP_Utils::log_object( 'Item with this permalink already exists. Aborting.', $permalink, __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_NOTICE ); return NULL; } # Get the computed options ( global settings merged against individual settings ) $options = WPRSS_FTP_Settings::get_instance()->get_computed_options( $source ); if ( $options['post_type'] === 'wprss_feed_item' ) { WPRSS_FTP_Utils::log_object( 'Legacy import method. Aborting.', $source, __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_INFO ); return $item; } self::_prepareItem( $item ); /*============================================== * 1) DETERMINE THE POST AUTHOR USER */ WPRSS_FTP_Utils::log( 'Determining post author...', __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_INFO ); // Get author-related options from meta, or from global settings, if not found $def_author = $options['def_author']; $fallback_author = $options['fallback_author']; $author_fallback_method = $options['author_fallback_method']; $fallback_user = get_user_by( 'id', $fallback_author ); if ( ! is_object( $fallback_user ) ) { $fallback_user = get_user_by( 'login', $fallback_author ); } $fallback_user = $fallback_user->ID; $no_author_found = $options['no_author_found']; // Determined user. Start with NULL $user = NULL; // If using an existing user, we are done. if ( $def_author !== '.' ) { $user = $def_author; WPRSS_FTP_Utils::log( 'Author is preset.', __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_SYSTEM ); } // If getting feed from author, determine the user to assign to the post else { /* Get the author from the feed * If author not found - use fallback user */ if ( $author = $item->get_author() ) { $has_author_name = $author->get_name() !== '' && is_string( $author->get_name() ); $has_author_email = $author->get_email() !== '' && is_string( $author->get_email() ); } else { $has_author_name = $has_author_email = false; } // Author NOT found if ( $author === NULL || !( $has_author_name || $has_author_email ) ) { // If option to use fallback when no author found, use fallback if ( $no_author_found === 'fallback' ) { $user = $fallback_user; WPRSS_FTP_Utils::log_object( 'Author is a fallback user.', $fallback_user, __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_SYSTEM ); } // Otherwise, skip the post else { WPRSS_FTP_Utils::log( 'Author could not be determined. Aborting.', __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_WARNING ); return NULL; } } // Author found else { WPRSS_FTP_Utils::log( 'Author found in feed.', __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_SYSTEM ); $author_name = $author->get_name(); $author_email = $author->get_email(); // No author name fix if ( !$has_author_name && $has_author_email ) { // "Email is actually the name"" fix if ( filter_var( $author_email, FILTER_VALIDATE_EMAIL ) === FALSE ) { // Set the name to the email, and reset the email $author_name = $author_email; $author_email = ''; // Set the flags appropriately $has_author_name = TRUE; $has_author_email = FALSE; } else { $parts = explode("@", $author_email); $author_name = $parts[0]; $has_author_name = TRUE; } WPRSS_FTP_Utils::log_object( sprintf( 'Author name determined from email "%1$s".', $author_email), $author_name, __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_SYSTEM ); } // No author email fix if ( !$has_author_email && $has_author_name ) { // Get rid of wwww and everything before it $domain_name = preg_replace( '/^www\./', '', $_SERVER['SERVER_NAME'] ); // Lowercase the author name, remove the spaces $email_username = str_replace( ' ', '', strtolower($author_name) ); // Remove all disallowed chars $email_username = preg_replace('![^\w\d_.]!', '', $email_username); // For domains with no TLDN suffix (such as localhost) if ( stripos( $domain_name, '.' ) === FALSE ) $domain_name .= '.com'; // Generate the email $author_email = "$email_username@$domain_name"; $has_author_email = TRUE; WPRSS_FTP_Utils::log_object( sprintf( 'Author email determined from name "%1$s".', $author_name), $author_email, __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_SYSTEM ); } $user_obj = FALSE; // If email is available, check if a user with this email exists $user_obj = get_user_by( 'email', $author_email ); // If search by email failed, search the email for the login if ( !$user_obj ) { $user_obj = get_user_by( 'login', $author_email ); } // If search by email failed, search by name if ( !$user_obj ) { $user_obj = get_user_by( 'login', $author_name ); } // Feed author has a user on site if ( $user_obj !== FALSE && isset( $user_obj->ID ) ) { $user = $user_obj->ID; WPRSS_FTP_Utils::log_object( 'User found in system', $user, __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_SYSTEM ); } // Author has no user on site else { $new_user_id = NULL; // Fallback method: create user if ( $author_fallback_method === 'create' ) { $random_password = wp_generate_password( $length = 12, $include_standard_special_chars = false ); $new_user_id = wp_create_user( $author_name, $random_password, $author_email ); WPRSS_FTP_Utils::log_object( 'User not found in system. Attempted to create', $author_email, __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_SYSTEM ); if ( !$new_user_id || ($is_error = is_wp_error( $new_user_id )) ) { WPRSS_FTP_Utils::log_object( 'User could not be created.', $is_error ? $new_user_id->get_error_message() : $author_email, __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_NOTICE ); $new_user_id = null; } else { // Check if the author has a URI in the feed $author_uri = $author->get_link(); if ( $author_uri !== NULL ) { // Filter the meta field and value $uri_meta = apply_filters( 'wprss_author_uri_meta_field', 'wprss_author_uri' ); $author_uri = apply_filters( 'wprss_author_uri', $author_uri, $source ); // Add the URI as user meta add_user_meta( $user, $uri_meta, $author_uri ); // Add the URI to the author wp_update_user( array( 'ID' => $new_user_id, 'user_url' => $author_uri, ) ); } // Check if the author name has spaces if ( strpos( $author_name, ' ' ) !== FALSE ) { // Split name into parts $split = explode( ' ', $author_name ); $first_name = ''; $last_name = ''; // check if we have more than one parts if ( ( $c = count( $split ) ) > 1 ) { $m = $c / 2; $first_half = array_slice( $split, 0, $m); $second_half = array_slice( $split, $m ); $first_name = implode( ' ', $first_half ); $last_name = implode( ' ', $second_half ); } // Update the user wp_update_user( array( 'ID' => $new_user_id, 'first_name' => $first_name, 'last_name' => $last_name, ) ); } } } // Fallback method: existing user // OR creating a user failed if ( $new_user_id === NULL ) { $new_user_id = $fallback_user; WPRSS_FTP_Utils::log_object( 'Falling back to existing user', $new_user_id, __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_SYSTEM ); } $user = $new_user_id; } } } WPRSS_FTP_Utils::log_object( 'Post author determined', $user, __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_INFO ); // Get WordPress' GMT offset in hours, and PHP's timezone $wp_tz = function_exists('wprss_get_timezone_string') ? wprss_get_timezone_string() : get_option( 'timezone_string ' ); $php_tz = date_default_timezone_get(); // Set Timezone to WordPress' date_default_timezone_set( $wp_tz ); WPRSS_FTP_Utils::log_object( 'Default timezone temporarily changed', $wp_tz, __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_SYSTEM ); // Prepare the rest of the post data $date_timestamp = ( $options['post_date'] === 'original' && ( $date_timestamp = $item->get_date( 'U' ) ) ) ? $date_timestamp : date( 'U' ); // Fall back to current date if explicitly configured, or no date // Prepare post dates $post_date = date( 'Y-m-d H:i:s', $date_timestamp ); $post_date_gmt = gmdate( 'Y-m-d H:i:s', $date_timestamp ); WPRSS_FTP_Utils::log_object( 'Post timestamp determined', $date_timestamp, __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_SYSTEM ); // Reset Timezone to PHP's date_default_timezone_set( $php_tz ); WPRSS_FTP_Utils::log_object( 'Default timezone restored', $php_tz, __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_SYSTEM ); // Prepare the post tags $tags_str = WPRSS_FTP_Meta::get_instance()->get_meta( $source, 'post_tags' ); $tags = array_map( 'trim', explode( ',', $tags_str ) ); if( count( $tags ) ) WPRSS_FTP_Utils::log_object( 'Tags will be added', $tags_str, __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_SYSTEM ); /*============================================== * 2) APPLY FILTERS TO POST FIELDS */ WPRSS_FTP_Utils::log( 'Applying filters to post fields...', __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_SYSTEM ); WPRSS_FTP_Utils::log( 'Applying post_title filter...', __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_SYSTEM ); $post_title = apply_filters( 'wprss_ftp_converter_post_title', $item->get_title(), $source ); WPRSS_FTP_Utils::log( 'Applying post_content filter...', __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_SYSTEM ); $post_content = apply_filters( 'wprss_ftp_converter_post_content', $item->get_content(), $source ); WPRSS_FTP_Utils::log( 'Applying post_status filter...', __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_SYSTEM ); $post_status = apply_filters( 'wprss_ftp_converter_post_status', $options['post_status'], $source ); WPRSS_FTP_Utils::log( 'Applying post_comments filter...', __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_SYSTEM ); $post_comments = apply_filters( 'wprss_ftp_converter_post_comments', $options['comment_status'], $source ); WPRSS_FTP_Utils::log( 'Applying post_type filter...', __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_SYSTEM ); $post_type = apply_filters( 'wprss_ftp_converter_post_type', $options['post_type'], $source ); WPRSS_FTP_Utils::log( 'Applying post_format filter...', __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_SYSTEM ); $post_format = apply_filters( 'wprss_ftp_converter_post_format', $options['post_format'], $source ); WPRSS_FTP_Utils::log( 'Applying post_terms filter...', __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_SYSTEM ); $post_terms = apply_filters( 'wprss_ftp_converter_post_terms', $options['post_terms'], $source ); WPRSS_FTP_Utils::log( 'Applying post_taxonomy filter...', __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_SYSTEM ); $post_taxonomy = apply_filters( 'wprss_ftp_converter_post_taxonomy', $options['post_taxonomy'], $source ); WPRSS_FTP_Utils::log( 'Applying permalink filter...', __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_SYSTEM ); $permalink = apply_filters( 'wprss_ftp_converter_permalink', $permalink, $source ); WPRSS_FTP_Utils::log( 'Applying post_author filter...', __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_SYSTEM ); $post_author = apply_filters( 'wprss_ftp_converter_post_author', $user, $source ); WPRSS_FTP_Utils::log( 'Applying post_date filter...', __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_SYSTEM ); $post_date = apply_filters( 'wprss_ftp_converter_post_date', $post_date, $source ); WPRSS_FTP_Utils::log( 'Applying post_date_gmt filter...', __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_SYSTEM ); $post_date_gmt = apply_filters( 'wprss_ftp_converter_post_date_gmt', $post_date_gmt, $source ); WPRSS_FTP_Utils::log( 'Applying post_tags filter...', __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_SYSTEM ); $post_tags = apply_filters( 'wprss_ftp_converter_post_tags', $tags, $source ); WPRSS_FTP_Utils::log( 'Applying post_language filter...', __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_SYSTEM ); $post_language = apply_filters( 'wprss_ftp_converter_post_language', $options['post_language'], $source ); WPRSS_FTP_Utils::log( 'Applying post_site filter...', __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_SYSTEM ); $post_site = apply_filters( 'wprss_ftp_converter_post_site', $options['post_site'], $source ); WPRSS_FTP_Utils::log( 'Filters applied', __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_SYSTEM ); $post_comments = ( WPRSS_FTP_Utils::multiboolean( $post_comments ) === TRUE )? 'open' : 'close'; WPRSS_FTP_Utils::log_object( 'Comments status determined', $post_comments, __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_SYSTEM ); /*============================================== * 3) CREATE THE POST */ // Initialize the excerpt to an empty string $post_excerpt = ''; // Prepare the post data WPRSS_FTP_Utils::log( 'Begin creating post...', __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_SYSTEM ); $post = array( 'post_title' => $post_title, 'post_content' => $post_content, 'post_excerpt' => $post_excerpt, 'post_date' => $post_date, 'post_date_gmt' => $post_date_gmt, 'post_status' => $post_status, 'post_type' => $post_type, 'post_author' => $post_author, 'tags_input' => implode( ', ' , $post_tags ), 'comment_status' => $post_comments ); /** * Filter the post args. * @var array $post Array containing the post fields * @var WP_Post $source An post that represents the feed source * @var SimplePie_Item $item The feed item currently being processed */ $post = apply_filters( 'wprss_ftp_post_args', $post, $source, $item ); WPRSS_FTP_Utils::log( 'Post args filters applied', __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_SYSTEM ); /*============================================== * 4) INSERT THE POST */ 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_FTP_Utils::log_object( 'WPMP Detected. Language determined.', $language_code, __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_INFO ); } // check for multisite option - and switch blogs if necessaray $switch_success = FALSE; if ( WPRSS_FTP_Utils::is_multisite() && $post_site !== '' ) { global $switched; if( $switch_success = switch_to_blog( $post_site ) ) WPRSS_FTP_Utils::log_object( 'Switched blog.', $post_site, __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_SYSTEM ); else WPRSS_FTP_Utils::log_object( 'Could not switch to blog.', $post_site, __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_NOTICE ); } // Check if embedded content is allowed $allow_embedded_content = WPRSS_FTP_Meta::get_instance()->get_meta( $source, 'allow_embedded_content' ); // If embedded content is allowed, remove KSES filtering if ( WPRSS_FTP_Utils::multiboolean( $allow_embedded_content ) === TRUE ) { kses_remove_filters(); WPRSS_FTP_Utils::log( 'Embedded content allowed', __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_INFO ); } // Insert the post $inserted_id = wp_insert_post( $post ); // If embedded content is allowed, re-add KSES filtering if ( WPRSS_FTP_Utils::multiboolean( $allow_embedded_content ) === TRUE ) { kses_init_filters(); } 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; } } WPRSS_FTP_Utils::log_object( 'Post created', $inserted_id, __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_INFO ); if ( $user === NULL ) WPRSS_FTP_Utils::log( 'Failed to determine a user for post #$inserted_id', $error_source, WPRSS_FTP_Utils::LOG_LEVEL_WARNING ); // Update the post format set_post_format( $inserted_id, $post_format ); if ( function_exists( 'wpml_update_translatable_content' ) ) { if ( $post_language === '' || $post_language === NULL ) { $post_language = ICL_LANGUAGE_CODE; } // Might be needed by WPML? $_POST['icl_post_language '] = $post_language; // Update the translation for the created post wpml_add_translatable_content( 'post_' . $post_type, $inserted_id, $post_language ); wpml_update_translatable_content( 'post_' . $post_type, $inserted_id, $post_language ); icl_cache_clear($post_type.'s_per_language'); WPRSS_FTP_Utils::log_object( 'Post translated', $post_language, __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_INFO ); } /*============================================== * 5) ADD THE POST META DATA */ WPRSS_FTP_Utils::log( 'Adding post meta', __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_INFO ); $thumbnail = ''; $enclosure_image = ''; if ( $enclosure = $item->get_enclosure() ) { $thumbnail = $enclosure->get_thumbnail(); $enclosure_image = $enclosure->get_link(); $enclosure_player = $enclosure->get_player(); WPRSS_FTP_Utils::log( 'Item has enclosure', __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_INFO ); } // Prepare the post meta, and pass though the wprss_ftp_post_meta filter. // Note: Prepend '!' to ignore the 'wprss_ftp_' prefix $post_meta_data = apply_filters( 'wprss_ftp_post_meta', array( '!wprss_item_permalink' => $permalink, 'feed_source' => $source, 'media:thumbnail' => $thumbnail, 'enclosure:thumbnail' => $enclosure_image, 'enclosure_link' => $enclosure_image, // Included twice for code readablity 'enclosure_player' => $enclosure_player, 'import_date' => time(), '!wprss_item_date' => $date_timestamp, // Required by core '!wprss_feed_id' => $source, ), $inserted_id, $source, $item ); WPRSS_FTP_Utils::log_object( 'Post meta filters applied', $inserted_id, __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_SYSTEM ); // Insert the post meta WPRSS_FTP_Meta::get_instance()->add_meta( $inserted_id, $post_meta_data ); WPRSS_FTP_Utils::log( 'Post meta added', __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_INFO ); /*============================================== * 6) ADD THE TAXONOMY TERMS * $all_post_terms = ( !is_array( $post_terms ) )? array() : $post_terms; // Check if the source auto creates taxonomy terms $auto_create_terms = WPRSS_FTP_Meta::get_instance()->get_meta( $source, 'post_auto_tax_terms' ); // If yes ... if ( WPRSS_FTP_Utils::multiboolean( $auto_create_terms ) === TRUE ) { // Get the feed categories $categories = $item->get_categories(); if ( is_array( $categories ) && count( $categories ) > 0 ) { // For each category in the feed item // Turn the categories into an array $new_categories = array(); foreach( $categories as $cat ) { $new_categories[] = array( 'name' => $cat->get_label(), 'args' => array(), ); } // Filter the categories $categories = apply_filters( 'wprss_auto_create_terms', $new_categories, $post_taxonomy, $source ); foreach ( $categories as $category_obj ) { $category = $category_obj['name']; // Find the term that matches that category $cat_term = term_exists( $category , $post_taxonomy ); // If the term does not exist create it if ( $cat_term === 0 || $cat_term === NULL ) { // check if parent field exists, and turn the slug into an id if ( isset( $category_obj['args']['parent'] ) ) { // Get the slug, and find the term by the slug $parent_slug = $category_obj['args']['parent']; $parent_term = get_term_by( 'slug', $parent_slug, $post_taxonomy, 'ARRAY_A' ); // If term not found, removed the parent arg if ( $parent_term === FALSE ) { unset( $category_obj['args']['parent'] ); } // Otherwise, change the slug to the id else $category_obj['args']['parent'] = intval( $parent_term['term_id'] ); } // Insert the term $cat_term = wp_insert_term( $category, $post_taxonomy, $category_obj['args'] ); delete_option($post_taxonomy."_children"); // clear the cache } $term_id = $cat_term['term_id']; $term_obj = get_term_by( 'id', $term_id, $post_taxonomy, 'ARRAY_A' ); if ( $term_obj !== FALSE && $term_obj !== NULL ) { if ( !is_array($all_post_terms) ) { WPRSS_FTP_Utils::log_object( 'The $all_post_terms variable is not an array:', $all_post_terms, $error_source ); } else { // Add it to the list of terms to add $all_post_terms[] = $term_obj['slug']; } } } } } $wp_categories_return = wp_set_object_terms( $inserted_id, $all_post_terms, $post_taxonomy, FALSE ); if ( !is_array( $wp_categories_return ) ) { WPRSS_FTP_Utils::log_object( "Possible error while inserting taxonomy terms for post #$inserted_id:", $all_post_terms ); } */ wprss_ftp_add_taxonomies_to_post( $inserted_id, $source, $item ); WPRSS_FTP_Utils::log( 'Added taxonomies', __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_INFO ); /*============================================== * 8) CUSTOM FIELD MAPPING */ WPRSS_FTP_Utils::log( 'Mapping custom fields', __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_INFO ); // Get the namespaces $cfm_namespaces = WPRSS_FTP_Meta::get_instance()->get_meta( $source, 'rss_namespaces' ); $cfm_namespaces = ( $cfm_namespaces === '' )? array() : $cfm_namespaces; // Get the tags $cfm_tags = WPRSS_FTP_Meta::get_instance()->get_meta( $source, 'rss_tags' ); $cfm_tags = ( $cfm_tags === '' )? array() : $cfm_tags; // Get the custom fields $cfm_fields = WPRSS_FTP_Meta::get_instance()->get_meta( $source, 'custom_fields' ); $cfm_fields = ( $cfm_fields === '' )? array() : $cfm_fields; // For each custom field mapping for ( $i = 0; $i < count( $cfm_namespaces ); $i++ ) { // Get the URL of the namespace $namespace_url = WPRSS_FTP_Settings::get_namespace_url( $cfm_namespaces[$i] ); // If the namespace url is NULL (namespace no longer exists in the settings), skip to next mapping if ( $namespace_url === NULL ) continue; // Match the syntax "tagname[attrib]" in the tag name preg_match('/([^\[]+) (\[ ([^\]]+) \])?/x', $cfm_tags[$i], $m); // If no matches, stop. Tag name is not correct. Possibly empty if ( !is_array($m) || count($m) < 2 ) continue; // Get the tag and attribute from the matches $tag_name = $m[1]; $attrib = ( isset( $m[3] ) )? $m[3] : NULL; // Get the tag from the feed item $item_tags = $item->get_item_tags( $namespace_url, $tag_name ); // Check if the tag exists. If not, skip to next mapping if ( !isset( $item_tags[0] ) ) continue; // Get the first tag found, and get its data contents $item_tag = $item_tags[0]; $attribs = $item_tag['attribs']['']; // If not using an attribute, simply get the text data if ( $attrib === NULL ) { $data = $item_tag['data']; } // Otherwise, check if the attribute exists elseif ( isset( $attribs[ $attrib ] ) ) { $data = $attribs[ $attrib ]; } // Otherwise do nothing else { continue; } // Put the data in the inserted post's meta, using the custom field as the meta key update_post_meta( $inserted_id, $cfm_fields[$i], $data ); WPRSS_FTP_Utils::log_object( 'Post meta updated', $i+1 . '/' . count($cfm_namespaces), __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_SYSTEM ); } WPRSS_FTP_Utils::log( 'Custom fields mapped', __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_INFO ); $post = get_post( $inserted_id ); if ( $post === NULL || $post === FALSE ) { $title = $item->get_title(); WPRSS_FTP_Utils::log( "An error occurred while converting a feed item into a post \"$title\". Kindly report this error to support@wprssaggregator.com" ); } else { WPRSS_FTP_Utils::log_object( 'Post created', $inserted_id, __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_INFO ); do_action( 'wprss_ftp_converter_inserted_post', $inserted_id, $source ); self::trim_words_for_post( $inserted_id, $source ); } } else { WPRSS_FTP_Utils::log( 'Failed to insert post. $inserted_id = ' . $inserted_id, $error_source ); } // If multisite and blog was switched, switch back to current blog if ( WPRSS_FTP_Utils::is_multisite() && $switch_success === TRUE ) { restore_current_blog(); WPRSS_FTP_Utils::log( 'Blog restored', __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_SYSTEM ); } WPRSS_FTP_Utils::log( 'Conversion complete', __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_INFO ); // Filter the return value $return = apply_filters( 'wprss_ftp_converter_return_post_'.$inserted_id, TRUE ); // If the return is still TRUE, ensure that the post that was created was not deleted if ( $return === TRUE ) { $post = get_post( $inserted_id ); $return = ( $post !== NULL && $post !== FALSE ); } // Log return value if anything other than TRUE else { wprss_log( 'Recieved "'.$return.'" as a return value for post #'.$inserted_id, NULL, WPRSS_LOG_LEVEL_SYSTEM ); } return $return; }