コード例 #1
0
	/**
	 * 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 );
	}
/**
 * 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');
			}
		}

	}
}
コード例 #3
0
	/**
	 * Handles extraction for the given post.
	 * 
	 * @since 2.5
	 */
	public static function extract( $post_id, $source ) {

		// If a source is set ( hence an imported post ), ...
		if ( $source !== '' ) {

			// Get the extraction rules of the source
			$rules = self::get_extraction_rules_and_types( $source );
			wprss_log_obj( 'Got extraction rules', $rules, NULL, WPRSS_LOG_LEVEL_SYSTEM );

			// If the rules are not an array or there are no rules, return
			if ( !is_array( $rules ) && count( $rules ) == 0 ) return;

			// Load the ganon library
			if ( version_compare(phpversion(), '5.3.1', '<') ) {
				// PHP4 Ganon
				require_once( WPRSS_FTP_LIB . 'ganon.php4' );
			}
			else {
				// PHP5+ Ganon
				require_once( WPRSS_FTP_LIB . 'ganon.php' );
			}

			// Get the post
			$post = get_post( $post_id );
			// If the post is a WP error, return
			if ( is_wp_error( $post ) || !is_object( $post ) ) return;
			// Otherwise, get the content
			$content = $post->post_content;

			// Parse the post content
			$html = str_get_dom( $content );

			// For each rule and its type
			foreach ( $rules as $rule => $type ) {

				// Trim the rule string
				$rule = trim( $rule );
				// If the rule is empty, skip it
				if ( strlen( $rule ) === 0 ) {
					continue;
				}

				// Used to replace the current html DOM
				$new_html = '';

				// Each found element ...
				foreach ( $html->select($rule) as $element ) {
					// Check the rule type
					switch( $type ) {
						// If keeping the matched element
						case 'keep' :
							// Add the element as a string to the new_html variable
							$new_html .= $element->toString(TRUE, TRUE, FALSE);
							break;
						// Remove the element
						case 'remove' :
							$element->detach();
							break;
						// Remove the element, and keep its children
						case 'remove_keep_children' :
							$element->detach( TRUE );
							break;
					}
				}

				// If the new_html variable has changed, use it as the new HMTL DOM
				if ( strlen( $new_html ) > 0 ) {
					$html = str_get_dom( $new_html );
				}

			} // End of rules foreach

			// Update the post with its new content
			$new_content = (string)$html;
			WPRSS_FTP_Utils::update_post_content( $post_id, $new_content );
		}

	}