/**
 * Prints the checkbox settings for enabling the legacy feed item.
 * 
 * @since 2.9.5
 */
function wprss_ftp_legacy_enable_option() {
	$legacy_enabled = WPRSS_FTP_Settings::get_instance()->get('legacy_enabled');
	echo WPRSS_FTP_Utils::boolean_to_checkbox(
		WPRSS_FTP_Utils::multiboolean( $legacy_enabled ),
		array(
			'id'	=>	'wprss-ftp-legacy-enabled',
			'name'	=>	WPRSS_FTP_Settings::OPTIONS_NAME . '[legacy_enabled]',
			'value'	=>	'true',
		)
	);
	echo WPRSS_Help::get_instance()->do_tooltip( WPRSS_FTP_HELP_PREFIX.'legacy_enabled' );
}
/**
 * Check if the import post is a YouTube, Dailymotion or Vimeo feed item.
 * 
 * @since 2.8
 */
function wprss_ftp_check_yt_feed( $post_id, $source_id ) {
	// Get the post form the ID
	$post = get_post( $post_id );
	// If the post is null, exit function.
	if ( $post === NULL ) return;

	// Get the permalink
	$permalink = get_post_meta( $post_id, 'wprss_item_permalink', TRUE );
	// If the permalink is empty, do not continue. Exit function
	if ( $permalink === '' ) return;

	// Get the source options
	$options = WPRSS_FTP_Settings::get_instance()->get_computed_options( $source_id );
	// If embedded content is not allowed, exit function.
	if ( WPRSS_FTP_Utils::multiboolean( $options['allow_embedded_content'] ) === FALSE ) return;

	// Search for the video host
	$found_video_host = preg_match( '/http[s]?:\/\/(www\.)?(youtube|dailymotion|vimeo)\.com\/(.*)/i', $permalink, $matches );

	// If video host was found and embedded content is allowed
	if ( $found_video_host !== 0 && $found_video_host !== FALSE ) {

		// Determine the embed link
		$embed = NULL;

		// Check which video host was found in the URL and prepare the embed link
		$host = $matches[2];
		switch( $host ) {
			case 'youtube':
				preg_match( '/(&|\?)v=([^&]+)/', $permalink, $yt_matches );
				$embed = 'http://www.youtube.com/embed/' . $yt_matches[2];
				$embed = apply_filters( 'wprss_ftp_yt_auto_embed_url', $embed, $post_id, $source_id );
				break;
			case 'vimeo':
				preg_match( '/(\d*)$/i', $permalink, $vim_matches );
				$embed = 'http://player.vimeo.com/video/' . $vim_matches[0];
				$embed = apply_filters( 'wprss_ftp_vimeo_auto_embed_url', $embed, $post_id, $source_id );
				break;
			case 'dailymotion':
				preg_match( '/(\.com\/)(video\/)(.*)/i', $permalink, $dm_matches );
				$embed = 'http://www.dailymotion.com/embed/video/' . $dm_matches[3];
				$embed = apply_filters( 'wprss_ftp_dm_auto_embed_url', $embed, $post_id, $source_id );
				break;
		}

		// If the embed link was successfully generated, add it to the post
		if ( $embed !== NULL ) {
			$content = $post->post_content;
			$video_link = apply_filters( 'wprss_ftp_enable_auto_embed_videos', FALSE ) === TRUE ? $embed : $permalink;
			$new_content = $video_link . "\n\n" . $content;
			WPRSS_FTP_Utils::update_post_content( $post_id, $new_content );

			// YouTube table fix
			// If the host found is YouTube, and the source is using featured images and removing them from the content
			// then remove the first column in the table that YouTube puts in the feed
			if ( $host === 'youtube' && WPRSS_FTP_Utils::multiboolean( $options['use_featured_image'] ) && WPRSS_FTP_Utils::multiboolean( $options['remove_ft_image'] ) ) {
				// Add a builtin extraction rule
				add_filter('wprss_ftp_built_in_rules', 'wprss_ftp_yt_table_cleanup');
			}
		}

	}
}
	/**
	 * Implementation of the shortening logic. Will shorten the specified
	 * URL using {@link https://developers.google.com/url-shortener/ Google URL Shortener API}
	 * While this function may be called directly, it is intended to be
	 * a handler for the 'wprss_ftp_shorten_url' filter; this is why the
	 * 2nd parameter is necessary.
	 * 
	 * Makes a post request to the Google URL Shortener API service.
	 * The URL of the request can be set by defining the
	 * 'WPRSS_URL_GOOGLE_URL_SHORTENER_API' constant.
	 * This method also uses the Google API key, which can be set globally
	 * by the 'google_api_key' Feed to Post setting.
	 * 
	 * @since 2.8.6
	 * @param string $url The URL to shorten
	 * @param string $method The code of the method that was used by the shortening function.
	 * @param string $key The Google API key to use for request. Default: 'google_api_key' WPRSS FTP setting.
	 * @return string|WP_Error The shortened URL on success, or a WP_Error.
	 */
	public static function shorten_url_method_google_url_shortener_api($url, $method, $key = null) {
		if ( is_wp_error( $url ) ) {
		    return $url;
		}
	    
		if( $method !== 'google_url_shortener_api' ) {
		    return $url;
		}
		
		$key = is_null($key) ? WPRSS_FTP_Settings::get_instance()->get( 'google_api_key' ) : $key;
		$serviceUrl = self::get_method_url_google_url_shortener_api();
		
		$response = wp_remote_post( add_query_arg('key', $key, $serviceUrl), array(
			'headers'		=> array( 'Content-Type' => 'application/json' ),
			'body'			=> json_encode( array( 'longUrl' => $url ) )
		));
		
		if ( is_wp_error( $response ) ) {
		    return $response;
		}
		
		$response = json_decode( $response['body'], true );
		
		if ( isset( $response['error'] ) ) {
		    $key = self::mask_string( $key );
		    $key = sprintf( '"%1$s" (%2$s chars)', $key, strlen( $key ) );
		    return new WP_Error( 'bad_request', sprintf( '"%1$s" using API key %2$s', ( isset( $response['message'] ) ? $response['message'] : 'Bad Request' ), $key ) );
		}
		
		return isset( $response['id'] ) ? $response['id'] : null;
	}
	/**
	 * Returns the post word limit setting. Used by the trim_post_content() function in
	 * the WPRSS_FTP_Converter class as a filter for 'excerpt_length'
	 * 
	 * @deprecated Check why it's no longer used, and if it is important
	 * @since 1.8
	 */
	public static function get_post_word_limit( $length ) {
		return WPRSS_FTP_Settings::get_instance()->get( 'post_word_limit' );
	}
	/**
	 * Returns whether or not a URL is allowed.
	 *
	 * @since 2.8.6
	 * @deprecated 3.0
	 * @param bool $is_allowed Whether or not the request is originally allowed
	 * @param string $host This server's host name
	 * @param string $url URL of the request
	 */
	public static function is_allow_local_requests( $is_allowed, $host, $url ) {
		$allow_local_requests = WPRSS_FTP_Settings::get_instance()->get( 'allow_local_requests' );
		if ( true === $allow_local_requests || WPRSS_FTP_Utils::multiboolean( $allow_local_requests ) ) {
			return true;
		}

		return $isAllowed;
	}
	public static function generate_tax_terms_dropdown() {
		$settings = WPRSS_FTP_Settings::get_instance();
		$taxonomy = isset( $_POST['taxonomy'] )? $_POST['taxonomy'] : $settings->get('post_taxonomy');
		$post_id = isset( $_POST['post_id'] )? $_POST['post_id'] : NULL;
		$source = isset( $_POST['source'] )? $_POST['source'] : '';

		$id = ( $source === 'meta' )? WPRSS_FTP_Meta::META_PREFIX . 'post_terms' : 'ftp-post-terms';
		$name = ( $source === 'meta' )? $id : WPRSS_FTP_Settings::OPTIONS_NAME . '[post_terms]';

		if ( $taxonomy === NULL || $taxonomy === '' ) {
			echo '<p id="'.$id.'">' . __('No terms were found for this taxonomy.', WPRSS_TEXT_DOMAIN) . '</p>';
			echo '<input type="hidden" name="' . WPRSS_FTP_Settings::OPTIONS_NAME . '[post_terms]" value="" />';
			die();
		}

		# Get the terms for the given taxonomy
		$terms = $settings->get_term_names(
			$taxonomy,
			array(
				'hide_empty'	=>	false,
				'order_by'		=>	'name'
			)
		);

		if ( $terms === NULL || count( $terms ) === 0 ) {
			echo '<p id="'.$id.'">' . __('No terms were found for this taxonomy.', WPRSS_TEXT_DOMAIN) . '</p>';
			echo '<input type="hidden" name="' . WPRSS_FTP_Settings::OPTIONS_NAME . '[post_terms]" value="" />';
			die();
		}
		else {
			# Print the terms dropdown
			$args = array(
				'id'		=>	$id,
				'name'		=>	$name,
				'selected'	=>	$settings->get('post_terms'),
				'multiple'	=>	TRUE
			);
			if ( $source === 'meta' ) {
				if ( $post_id !== NULL )
					$args['selected'] = WPRSS_FTP_Meta::get_instance()->get_meta( $post_id, 'post_terms' );
				if ( $args['selected'] === '' )
					$args['selected'] = $settings->get('post_terms');
			}
			echo self::array_to_select( $terms, $args );
			
			# Re-print the description
			$tax_meta_fields = WPRSS_FTP_Meta::get_instance()->get_meta_fields('tax');
			echo '<br><span class="description">'. $tax_meta_fields['post_terms']['desc'] .'</span>';
		}

		die();
	}
/**
 * Inserts the terms into a given post, based on their saved meta/
 *
 * This function replaces the old taxonomy code in the converter class, utilizing the new meta data
 * format used for the taxonomies. The terms for each taxonomy, along with any auto created feed terms
 * are inserted into the post.
 *
 * @since 3.1
 * @param int $post_id The ID of the post.
 * @param int $feed_id The ID of the feed source that imported the post.
 * @param object $sp_item The SimplePie_Item from which the post was converted.
 * @return bool TRUE if the terms where inserted successfully, FALSE if the post was not found.
 */
function wprss_ftp_add_taxonomies_to_post( $post_id, $feed_id, $sp_item ) {
	// If the post does not exist, stop immediately
	if ( get_post( $post_id ) === null ) return NULL;

	// Get the taxonomies meta
	$meta = WPRSS_FTP_Meta::get_instance()->get( $feed_id, 'taxonomies' );
	// If the source has the old meta saved, convert it into the new meta
	if ( $meta === '' ) {
		$meta = WPRSS_FTP_Meta::convert_post_taxonomy_meta( $feed_id );
	}

	// Get the settings instance
	$settings = WPRSS_FTP_Settings::get_instance();
	// Get the post type saved in the settings
	$settings_post_type = $settings->get( 'post_type' );
	// If it matches, add the settings taxonomies to the meta taxonomies
	if ( $settings_post_type === get_post_type( $post_id ) ) {
		// Get the taxonomies settings, and convert from old format if needed
		$settings_taxonomies = $settings->get( 'taxonomies' );
		if ( $settings_taxonomies === '' ) {
			$settings_taxonomies = WPRSS_FTP_Settings::convert_post_taxonomy_settings();
		}
		// Add to meta taxonomies
		$meta = array_merge( (array) $settings_taxonomies, (array) $meta );
	}

	$tax_iterated = array();

	// For each entry
	foreach( $meta as $entry ) {
		// Check if this taxonomy term should be applied.
		if ( wprss_ftp_should_apply_taxonomy( $entry, $sp_item ) === FALSE ) {
			continue;
		}

		// Get the data
		$taxonomy = $entry['taxonomy'];
		$terms = $entry['terms'];
		$terms = is_array( $terms ) ? $terms : array();
		$auto = WPRSS_FTP_Utils::multiboolean( $entry['auto'] );
		$feed_terms = array();

		// Repeat the taxonomy slug in an array with the same length as the terms array
		$taxonomies_array = count( $terms ) > 0 ? array_fill( 0, count($terms), $taxonomy ) : array();
		// Run the term slugs through the 'wprss_ftp_create_or_get_term'
		$terms_to_set = array_map( 'wprss_ftp_create_or_get_term', $terms, $taxonomies_array);
		// Filter the terms to remove NULL entries (NULL can be returned by 'wprss_ftp_create_or_get_term')
		$terms_to_set = array_filter( $terms_to_set );

		// If auto creation is enabled
		if ( $auto === TRUE ) {
			// If auto create is enabled, get the terms from the feed
			$feed_terms = $sp_item->get_categories();
			$feed_terms = is_array( $feed_terms )? $feed_terms : array();
			// Map the terms through our preparation function, and filter them for custom user manipulation
			$feed_terms = array_map( 'wprss_ftp_prepare_auto_created_term', $feed_terms );
			$feed_terms = apply_filters( 'wprss_auto_create_terms', $feed_terms, $taxonomy, $feed_id );
			// Repeat the taxonomy slug in an array with the same length as the terms array
			$num_terms = count( $feed_terms );
			$taxonomies_array = $num_terms > 0? array_fill( 0, $num_terms, $taxonomy ) : array();
			// Run them through the `wprss_ftp_process_auto_created_terms` function for processing
			$feed_terms = array_map( 'wprss_ftp_process_auto_created_terms', $feed_terms, $taxonomies_array );
			// Filter the terms to remove NULL entries (NULL can be returned by 'wprss_ftp_create_or_get_term')
			$feed_terms = array_filter( $feed_terms );
			// Add them to the terms to set
			$terms_to_set = array_merge( (array) $terms_to_set, (array) $feed_terms );
		}

		// Add the taxonomy to the tax_iterated array if it's not already in the array
		if ( ! in_array( $taxonomy, $tax_iterated ) ) {
			$tax_iterated[] = $taxonomy;
			// Set no terms, and override existing - to remove any default terms, like 'Uncategorized'
			wp_set_object_terms( $post_id, array(), $taxonomy, FALSE );
		}
		// Insert the terms
		wp_set_object_terms( $post_id, $terms_to_set, $taxonomy, TRUE );
		// clear the cache
		delete_option($taxonomy."_children");
	}

	return TRUE;
}
	public function get_post_types( $source_id = null ) {
		$post_types = WPRSS_FTP_Settings::get_post_types();
		if ( is_null($source_id) ) {
			return $post_types;
		}

		$assigned_type = self::get_instance()->get_meta( $source_id, 'post_type' );
		if( $assigned_type && !isset($post_types[$assigned_type]) ) {
			$post_types[$assigned_type] = sprintf( '%1$s (%2$s)', $assigned_type, __( 'Not Registered', WPRSS_TEXT_DOMAIN ) );
		}

		return $post_types;
	}
	/**
	 * Checks if the post is an imported post and if the canonical_link setting is enabled.
	 * If enabled, it adds the <link> to the page head
	 * 
	 * @since 1.8
	 */
	public static function post_head() {
		if ( ! is_singular() ) return;

		// Current post
		global $post;
		// Get the source meta, if it exists, for this post
		$source = WPRSS_FTP_Meta::get_instance()->get_meta( $post->ID, 'feed_source' );
		// If the meta exists ( i.e. is an imported post )
		if ( $source !== '' ) {
			// print the genrator tag
			?><meta name="generator" content="Feed to Post <?php echo WPRSS_FTP_VERSION; ?>" /><?php
			echo "\n";

			// Remove WordPress canonical links
			remove_action( 'wp_head', 'rel_canonical' );

			// Get the canonical_link setting
			$options = WPRSS_FTP_Settings::get_instance()->get_computed_options( $source );
			$canonical_link = isset( $options['canonical_link'] )? $options['canonical_link'] : FALSE;

			// If enabled
			if ( WPRSS_FTP_Utils::multiboolean( $canonical_link ) === TRUE ) {
				// Get the original post link
				$link = WPRSS_FTP_Meta::get_instance()->get_meta( $post->ID, 'wprss_item_permalink', false );
				// print the <link>
				?><link rel="canonical" href="<?php echo $link; ?>" /><?php
			}
		}
	}
	/**
	 * Determines the featured image for the imported post.
	 *
	 * This is ran after images have been downloaded, and remote URLS have
	 * been replaced with local ones, if necessary.
	 *
	 * @since 2.7.4
	 * @param int $post_ID The ID of the post that is being imported.
	 * @param int $source The ID of the feed source, which is importing the post
	 * @param array $images A numeric array, where each value is the local URL of an image in post content.
	 */
	public function determine_featured_image_for_post( $post_ID, $source, $images ) {
		wprss_log_obj( 'Beginning featured image selection for post:', $post_ID, NULL, WPRSS_LOG_LEVEL_SYSTEM );

		// Get the post form the ID
		$post = get_post( $post_ID );
		// If the post is null, return null.
		if ( $post === NULL ) {
			wprss_log( 'Received incorrect or NULL post ID.', NULL, WPRSS_LOG_LEVEL_ERROR );
			// Trigger action for no featured image determined for this post
			do_action( 'wprss_ftp_no_featured_image', $post_ID, $source );
			return null;
		}

		// Get the post content
		$content = $post->post_content;

		// Get the computed settings for the feed source
		$options = WPRSS_FTP_Settings::get_instance()->get_computed_options( $source );

		// If the featured image option is disabled, do NOT continue.
		if ( WPRSS_FTP_Utils::multiboolean( $options['use_featured_image'] ) === FALSE ) {
			wprss_log( "Feed source for this post has featured images disabled. Stopping ...", NULL, WPRSS_LOG_LEVEL_SYSTEM );
			// Trigger action for no featured image determined for this post
			do_action( 'wprss_ftp_no_featured_image', $post_ID, $source );
			return null;
		}

		// Start by trimming whitespace from image URLs
		$images = array_map( 'trim', $images );

		// The URL of the determined featured image
		$featured_image_url = NULL;

		// Get the minimum image size settings
		$min_width = $options['image_min_width'];
		$min_height = $options['image_min_height'];

		// DETERMINED FEATURED IMAGE
		$featured_image = NULL;
		// WHETHER OR NOT USING THE FALLBACK IMAGE (used to skip most of the image processing in the function)
		$using_fallback = FALSE;

		wprss_log( 'Featured image option: `'.$options['featured_image'].'`', NULL, WPRSS_LOG_LEVEL_SYSTEM );

		// Check which featured image option is being used
		switch ( $options['featured_image'] ) {
			default:

			// FIRST/LAST image in post content
			case 'first':
			case 'last':

				// If using the Last Image option, reverse the images array
				if ( $options['featured_image'] === 'last' )
					$images = array_reverse( $images, true );

				wprss_log( "Iterating images in post.", NULL, WPRSS_LOG_LEVEL_SYSTEM );

				// Iterate through all the images
				foreach( $images as $_old => $_image ) {
					// The the image URL is empty, or it does not obey the minimum size constraint, jump to next image
					if ( empty( $images[ $_old ] ) || !$this->image_obeys_minimum_size( $_old, $min_width, $min_height ) )
						continue;

					// Attempt to use this iamge as featured imafe
					$ft_image_found = $_image;
					$featured_image = $_image;

					wprss_log_obj( "Found good image:", $featured_image, NULL, WPRSS_LOG_LEVEL_SYSTEM );

					// Check if the image URL is local
					if ( !wprss_ftp_is_url_local( $featured_image ) ) {
						wprss_log( "Not found in gallery. Downloading ...", NULL, WPRSS_LOG_LEVEL_SYSTEM );
						// If not, download it and attach it to the post
						$featured_image = apply_filters( 'wprss_ftp_featured_image_url', $featured_image, $post_ID, $source, $options['featured_image'] );
						$featured_image = wprss_ftp_media_sideload_image( $featured_image, $post_ID, TRUE );
					}
					// If it is local, simply attach it to the post
					else {
						wprss_log( "Found in gallery. Attaching to post ...", NULL, WPRSS_LOG_LEVEL_SYSTEM );
						$featured_image = apply_filters( 'wprss_ftp_featured_image_url', $featured_image, $post_ID, $source, $options['featured_image'] );
						self::set_featured_image( $post_ID, $featured_image, TRUE );
					}

					// If no error was encountered, exit the loop
					// If an error was encountered, the next image will be tested.
					if ( !is_wp_error( $featured_image ) )
						break;
				}

				// Indicate that NO image was used as featured image
				if ( is_wp_error( $featured_image ) ) {
					$featured_image = NULL;
					WPRSS_FTP_Utils::log_object( 'Could not determine featured image from first/last', $featured_image->get_error_message(), __METHOD__, WPRSS_FTP_Utils::LOG_LEVEL_SYSTEM );
				}

				break; // END OF FIRST / LAST IMAGE CASE


			// FEED <MEDIA:THUMBNAIL> IMAGE / <ENCLOSURE> TAG
			case 'thumb':
			case 'enclosure':

				// Prepare the tag in which to look for the image
				$tag = ( $options['featured_image'] == 'thumb' )? 'media:thumbnail' : 'enclosure:thumbnail';
				$orig_tag = $tag;
				$tag = apply_filters( 'wprss_ftp_featured_image_meta_key', $tag, $post_ID, $source, $options['featured_image'] );
				WPRSS_FTP_Utils::log_object( 'Custom field used for featured image', $tag, null, WPRSS_FTP_Utils::LOG_LEVEL_SYSTEM );

				/* Get the media thumbnail from post meta ( converter class places the tag contents in post meta ).
				 * If the original meta key was modified by the `wprss_ftp_featured_image_meta_key` filter,
				 * no prefix is applied to the meta key.
				 */
				$thumbnail = trim( WPRSS_FTP_Meta::get_instance()->get_meta( $post_ID, $tag, ($use_prefix = $tag === $orig_tag) ) );

				// Check if the thumbnail is large enough to accept
				if ( $this->image_obeys_minimum_size( $thumbnail, $min_width, $min_height ) === TRUE ) {
					// Download this image, attach it to the post and use it as the featured image
					$thumbnail = apply_filters( 'wprss_ftp_featured_image_url', $thumbnail, $post_ID, $source, $options['featured_image'] );
					$featured_image = wprss_ftp_media_sideload_image( $thumbnail, $post_ID, TRUE );
					// If an error was encountered, set the featured image to NULL
					if ( is_wp_error( $featured_image ) ) {
						WPRSS_FTP_Utils::log_object( 'Could not determine featured image from thumb/emclosure', $featured_image->get_error_message(), __METHOD__, WPRSS_FTP_Utils::LOG_LEVEL_SYSTEM );
						$featured_image = NULL;
					}
				}

				break; // END OF MEDIA:THUMBNAIL / ENCLOSURE CASE


			// FALLBACK FEATURED IMAGE
			case 'fallback':

				// Get the fallback featured image
				$fallback_image = get_post_thumbnail_id( $source );

				// Check if the fallback featured image is set
				if ( !empty( $fallback_image ) ) {
					// If it is set, use it as featured image for the imported post
					self::set_featured_image( $post_ID, $fallback_image );
					// Indicate that the fallback was used
					$using_fallback = TRUE;
				}
				break;

		} // End of switch


		// If the fallback image was used, then we are done.
		if ( ! $using_fallback ) {
			// If a featured image was determined
			if ( $featured_image !== NULL && !is_wp_error( $featured_image ) ) {
				// Check for filter to remove featured image from post
				$remove_ft_image = apply_filters( 'wprss_ftp_remove_ft_image_from_content', FALSE );
				// We remove the ft image, if the filter returns TRUE, or if it returns an array and the post source is in the array.
				$remove = $remove_ft_image === TRUE || ( is_array( $remove_ft_image ) && in_array( $source, $remove_ft_image ) );

				// If removing and the ft image is in the content (not media:thumbnail)
				// (Determined either by legacy filter or meta option)
				if ( $remove || WPRSS_FTP_Utils::multiboolean( $options['remove_ft_image'] ) === TRUE ) {
					$img_to_remove = $featured_image;
					if ( $options['featured_image'] === 'first' || $options['featured_image'] === 'last' ) {
						$img_to_remove = $ft_image_found;
					}
					// Prepare the img tag regex
					$d = '|'; // In case the image tag contains the pipe somewhere, it needs to be escaped too
					$tag_search = '<img.*?src=[\'"]' . preg_quote( esc_attr( $img_to_remove ), $d ) . '[\'"].*?>';
					// Replace the tag with an empty string, and get the new content
					$new_content = preg_replace( $d . $tag_search . $d . 'i', '', $content, apply_filters('wprss_ftp_remove_ft_image_limit', 1) );
					WPRSS_FTP_Utils::log_object( sprintf('Featured image %1$sremoved from content', $new_content === $content ? 'not ' : ''), $img_to_remove, __FUNCTION__, WPRSS_FTP_Utils::LOG_LEVEL_INFO);
					// Update the post content
					WPRSS_FTP_Utils::update_post_content( $post_ID, $new_content );
				}
			}

			// However,
			// If NO featued image was determined
			else {
				$featured_image = NULL;

				// Get the user filter for using the feed image
				$user_filter = apply_filters( 'wprss_ftp_feed_image_fallback', FALSE, $post_ID, $source, $images );
				$user_filter_enabled = $user_filter === TRUE || ( is_array( $user_filter ) && in_array( $source, $user_filter ) );

				// Check if the core supports getting the feed image and if the user filter is enabled
				if ( function_exists( 'wprss_get_feed_image' ) && $user_filter_enabled ) {
					// Get the url of the feed image
					$feed_image = wprss_get_feed_image( $source );

					// Attempt to download it and attach it to the post
					$feed_image = apply_filters( 'wprss_ftp_featured_image_url', $feed_image, $post_ID, $source, $options['featured_image'] );
					$featured_image = wprss_ftp_media_sideload_image( $feed_image, $post_ID, TRUE );

					// If an error was encountered, indicate it by setting the featured image to NULL
					if ( is_wp_error( $featured_image ) || $featured_image === NULL ) {
						$featured_image = NULL;
					}
				}

				// If the feed image did not work, resort to using the fallback, if set
				if ( $featured_image == NULL ) {
					// Get the fallback image
					$fallback_image = get_post_thumbnail_id( $source );
					// If it is set, use it as the featured image for the post
					if ( !empty( $fallback_image ) ) {
						self::set_featured_image( $post_ID, $fallback_image );
					} else {
						// Trigger action for no featured image determined for this post
						do_action( 'wprss_ftp_no_featured_image', $post_ID, $source );
					}
				}
			}
		}

		do_action( 'wprss_ftp_determined_featured_image', $post_ID, $source );
	}
	/**
	 * Renders the author settings
	 * 
	 * @since 1.9.3
	 */
	public function render_author_options( $post_id = NULL, $meta_row_title = '', $meta_label_for = '' ) {
		// Get the options
		$options = WPRSS_FTP_Settings::get_instance()->get_computed_options( $post_id );
		$def_author = ( $post_id !== NULL ) ? $options['def_author'] : $this->get( 'def_author' );
		$author_fallback_method = ( $post_id !== NULL ) ? $options['author_fallback_method'] : $this->get( 'author_fallback_method' );
		$author_fallback_method = ( strtolower( $author_fallback_method ) === 'use_existing' )? 'existing' : $author_fallback_method;
		$fallback_author = ( $post_id !== NULL ) ? $options['fallback_author'] : $this->get( 'fallback_author' );
		$no_author_found = ( $post_id !== NULL ) ? $options['no_author_found'] : $this->get( 'no_author_found' );
		
		// Set the HTML tag ids
		$ids = array(
			'def_author'				=>	'ftp-def-author',
			'author_fallback_method'	=>	'ftp-author-fallback-method',
			'fallback_author'			=>	'ftp-fallback-author',
			'no_author_found'			=>	'ftp-no-author-skip'
		);
		// If in meta, copy the keys into the values
		if ( $post_id !== NULL ) {
			foreach ( $ids as $field => $id ) {
				$ids[$field] = $field;
			}
		}
		// Set the HTML tag names
		$names = array(
			'def_author'				=>	'def_author',
			'author_fallback_method'	=>	'author_fallback_method',
			'fallback_author'			=>	'fallback_author',
			'no_author_found'			=>	'no_author_found',
		);
		// Set the names appropriately according to the page, meta or settings
		foreach( $names as $field => $name) {
			$names[$field] = ( $post_id !== NULL )? WPRSS_FTP_Meta::META_PREFIX . $name : self::OPTIONS_NAME . "[$name]";
		}

		// If in meta, print the table row
		if ( $post_id !== NULL ) : ?>
			<tr>
				<th>
					<label for="<?php echo $meta_label_for; ?>">
						<?php echo $meta_row_title; ?>
					</label>
				</th>
				<td>
		<?php endif; ?>

		<!-- Author to use -->
		<span id="wprss-ftp-authors-options">
			<label for="<?php echo $ids['def_author']; ?>">Use </label>
			<?php $users = WPRSS_FTP_Meta::get_users_array( WPRSS_FTP_Admin_User_Ajax::get_instance()->is_over_threshold() ? (array)$def_author : false ) ?>
			<?php echo WPRSS_FTP_Utils::array_to_select( $users, array(
					'id'		=>	$ids['def_author'],
					'name'		=>	$names['def_author'],
					'selected'	=>	$def_author,
			));
			?>
			<script type="text/javascript">
				top.wprss.f2p.userAjax.addElement('#<?php echo $ids['def_author'] ?>');
			</script>
			<?php
			echo WPRSS_Help::get_instance()->do_tooltip( WPRSS_FTP_HELP_PREFIX.'post_author' ); ?>
		</span>
		
		<!-- Separator -->
		<?php if ( $post_id !== NULL ) : ?>
			</td></tr>
			<tr class="wprss-tr-hr wprss-ftp-authors-hide-if-using-existing">
				<th>
				</th>
				<td>
		<?php endif; ?>
		
		<!-- Section that hides when using an existing user -->
		<span class="wprss-ftp-authors-hide-if-using-existing">
			
			<!-- Radio group if author has no user -->
			<span class="ftp-author-using-in-feed">
				<label for="<?php echo $ids['author_fallback_method']; ?>">
					<?php _e( 'If the author in the feed is not an existing user', WPRSS_TEXT_DOMAIN ); ?>:
				</label>
				<br/>
				<?php
					echo implode( '', WPRSS_FTP_Utils::array_to_radio_buttons(
						array(
							'existing'	=> __( 'Use the fallback user', WPRSS_TEXT_DOMAIN ),
							'create'	=> __( 'Create a user for the author', WPRSS_TEXT_DOMAIN )
						),
						array(
							'id'		=>	$ids['author_fallback_method'],
							'name'		=>	$names['author_fallback_method'],
							'checked'	=>	$author_fallback_method,
						)
					));
				?>
			</span>
			
			<!-- Radio group if author not found in feed -->
			<span class="ftp-author-using-in-feed">
				<label for="<?php echo $ids['no_author_found']; ?>">
					<?php _e( 'If the author is missing from the feed', WPRSS_TEXT_DOMAIN ); ?>
				</label>
				<br/>
				<?php
					echo implode( WPRSS_FTP_Utils::array_to_radio_buttons(
						array(
							'fallback'	=>	__( 'Use the fallback user', WPRSS_TEXT_DOMAIN ),
							'skip'		=>	__( 'Do not import the post', WPRSS_TEXT_DOMAIN )
						),
						array(
							'id'		=>	$ids['no_author_found'],
							'name'		=>	$names['no_author_found'],
							'checked'	=>	$no_author_found,
						)
					));
				?>
			</span>
		</span>
		
		
		<?php if ( $post_id !== NULL ) : ?>
			</td></tr>
			<tr class="wprss-tr-hr wprss-ftp-authors-hide-if-using-existing">
				<th>
					<label for="<?php echo $ids['fallback_author']; ?>">
						<?php _e( 'Fallback User', WPRSS_TEXT_DOMAIN ); ?>
					</label>
				</th>
				<td>
		<?php endif; ?>
		
		<!-- Section that hides when using an existing user -->
		<span class="wprss-ftp-authors-hide-if-using-existing">
			<?php if ( $post_id === NULL ) : ?>
			<label for="<?php echo $ids['fallback_author']; ?>">
				<?php _e( 'Fallback user:'******'id'		=>	$ids['fallback_author'],
					'name'		=>	$names['fallback_author'],
					'selected'	=>	$fallback_author,
				));
			?>
			<script type="text/javascript">
				top.wprss.f2p.userAjax.addElement('#<?php echo $ids['fallback_author'] ?>', {<?php echo WPRSS_FTP_Admin_User_Ajax::REQUEST_VAR_EXISTING_USERS_ONLY ?>: true, <?php echo WPRSS_FTP_Admin_User_Ajax::REQUEST_VAR_LOGIN_NAMES ?>: true});
			</script>
			<?php echo WPRSS_Help::get_instance()->do_tooltip( WPRSS_FTP_HELP_PREFIX.'fallback_author' ); ?>
		</span>
					
		<?php // Add scripts ?>

		<script type="text/javascript">
			(function($){
				$(document).ready( function(){

					// Set a pointer to the dropdowns
					var dropdown1 = $('#<?php echo $ids['def_author']; ?>');

					// Create the function that shows/hides the second section
					var authorsSection2UI = function(){
						// Show second section only if the option to use the author in the feed is chosen
						$('.wprss-ftp-authors-hide-if-using-existing').toggle( dropdown1.val() === '.' );
					}

					// Set the on change handlers
					dropdown1.change( authorsSection2UI );

					// Run the function at least once
					authorsSection2UI();

				});
			})(jQuery);
		</script>
		<?php // End of scripts

		// If in meta, close the table row
		if ( $post_id !== NULL ) {
			?></td></tr><?php
		}
	}