コード例 #1
0
	/**
	 * Returns the extraction rule types, as an array, for the given feed source
	 * 
	 * @since 2.5
	 */
	public static function get_extraction_rule_types( $source ) {
		$rules = WPRSS_FTP_Meta::get_instance()->get_meta( $source, 'extraction_rules_types' );
		if ( !is_array( $rules ) ) {
			return explode( "\n", $rules );
		}
		return $rules;
	}
コード例 #2
0
/**
 * Returns TRUE if using the legacy imported feed items, FALSE otherwise.
 * If a source ID is given, it returns TRUE if the source is using the wprss_feed_item
 * post type, FALSE for any other.
 * 
 * @since 2.9.5
 * @param int|string $source_id The ID of the feed source
 */
function wprss_ftp_using_feed_items( $source_id = NULL ) {
	if ( $source_id === NULL ) {
		return WPRSS_FTP_Utils::multiboolean( WPRSS_FTP_Settings::get_instance()->get('legacy_enabled') );
	} else {
		return WPRSS_FTP_Meta::get_instance()->get( $source_id, 'post_type' ) == 'wprss_feed_item';
	}
}
コード例 #3
0
	/**
	 * Handles the substitution of the placeholders.
	 * 
	 * @since 1.6
	 */
	public static function handle_appended_data( $post, $append, $wpautop = TRUE ) {
		if ( strlen($append) === 0 ) return "";

		// Get the placeholders
		$placeholders = self::get_placeholders();
		// Get the post's feed source
		$source_id = WPRSS_FTP_Meta::get_instance()->get_meta( $post->ID, 'feed_source' );
		$source = get_post( $source_id );
		// Array of [placeholder] => [text]
		$values = array();

		// Iterate each known placeholder
		foreach ( $placeholders as $placeholder => $description ) {
			$value = null;
			// Generate the text value for this placeholder
			switch ( $placeholder ) {

				case '{{feed_name}}':
					$value = $source->post_title;
					break;

				case '{{feed_url}}':
					$value = get_post_meta( $source_id, 'wprss_site_url', TRUE );
					if ( $value == '' ) {
						$value = get_post_meta( $source_id, 'wprss_url', TRUE );
					}
					break;

				case '{{post_title}}':
					$value = $post->post_title;
					break;

				case '{{post_url}}':
					$value = get_permalink( $post->ID );
					break;

				case '{{original_post_url}}':
					$value = get_post_meta( $post->ID, 'wprss_item_permalink', TRUE );
					break;

				case '{{post_import_date}}':
					$import_date = WPRSS_FTP_Meta::get_instance()->get_meta( $post->ID, 'import_date' );
					// Get the WordPress date and time format settings
					$time_format = get_option('time_format');
					$date_format = get_option('date_format');
					// Format the value and add HTML time tags
					$value = @date( "$date_format $time_format", $import_date );
					$value = '<time>' . $value . '</time>';
					break;
					
				case '{{post_publish_date}}':
					$value = get_the_date( '', $post->ID ) .' '. get_the_time( '', $post->ID );
					$value = '<time>' . $value . '</time>';
					break;

				case '{{post_author_name}}':
					$user = get_user_by( 'id', $post->post_author );
					if ( $user->first_name === '' && $user->last_name === '' ) {
						$value = $user->user_login;
					}
					$value = $user->first_name . ' ' . $user->last_name;
					break;

				case '{{post_author_url}}':
					$user = get_user_by( 'id', $post->post_author );
					$value = $user->user_url;
					break;

				default:
					$value = '/';
					break;

			}
			// Add the placeholder and its value to the values array
			$values[$placeholder] = $value;
		}
		// Replaces new lines with <br/> tags
		$values["\n"] = '<br/>';


		//== ADVANCED PLACEHOLDERS ====
		// {{meta: xyz}} Outputs meta value for the post's meta field 'xyz'
		// {{source_meta: xyz}} The same, but the meta value is taken from the post's feed source.

		// Use regex to find all the advanced placeholders used
		preg_match_all("/{{(source_)?meta\s*:\s*([^}]*)}}/ix", $append, $adv_meta);
		// The first entry in the results array is an array containing the
		// full string match (the placeholder string)
		$adv_meta_placeholders = $adv_meta[0];
		// The second entry in the results array is an array of the matched
		// optional "source_" prefix (the matching group before "meta")
		$adv_meta_source = $adv_meta[1];
		// The third entry in the results array is an array of the matched
		// meta fields (the matching group after the colon)
		$adv_meta_fields = $adv_meta[2];
		// Iterate each found meta field
		for( $i = 0; $i < count($adv_meta_fields); $i++ ) {
			// Determine if retrieveing the meta of the post or the source
			$target = ( $adv_meta_source[$i] === "source_" )? $source_id : $post->ID;
			// Get the meta value from the target post/source
			$meta = get_post_meta( $target, $adv_meta_fields[$i], TRUE );
			// Prepare the placeholder that matches this meta field
			$placeholder = $adv_meta_placeholders[$i];
			// Add the find/replace values
			$values[ $placeholder ] = $meta;
		}

		// Return the append/prepend text with all placeholders replaced with their respective text value
		$return = WPRSS_FTP_Utils::str_mass_replace( $append, $values );
		$return = apply_filters( 'wprss_ftp_handled_append_text', $return, $post->ID );
		return $wpautop === TRUE? wpautop( $return ) : $return;
	}
コード例 #4
0
	/**
	 * Retrieve a shortened permalink URL for the feed item identified by $post.
	 * If the post is not a feed item, returns an empty string, unless
	 * overridden by the 'wprss_ftp_shortened_item_url' filter.
	 * The shortened URL will be cached for this item. To invalidate cache,
	 * change the item's or the item source's shortening method.
	 * 
	 * Will write to log on error.
	 * 
	 * @since 2.8.6
	 * @param WP_Post|int $post A post object or ID
	 * @param null|mixed $default The value to return if the post URL cannot be shortened.
	 * @return string The shortened URL of the specified item
	 */
	public static function get_shortened_item_url($post, $default = null) {
		$post = get_post($post);
		$meta = WPRSS_FTP_Meta::get_instance();
		
		// If no permalink, or it's empty, just return it
		if( !($long_url = trim($meta->get_meta( $post->ID, 'wprss_item_permalink', false ))) || empty($long_url) ) {
			return apply_filters('wprss_ftp_shortened_item_url', $long_url, $post, false); // The false at the end means the URL was not shortened
		}
		
		// If item is not attached to feed source, return original permalink too
		if ( !($item_source_id = $meta->get_meta( $post->ID, 'feed_source' )) ) {
			return apply_filters('wprss_ftp_shortened_item_url', $long_url, $post, false); // The false at the end means the URL was not shortened
		}
		
		$short_url = trim($meta->get_meta( $post->ID, self::META_KEY_SHORT_URL)); // The shortened URL that may already exist on the item
		$item_shortening_method = trim((string)$meta->get_meta( $post->ID, self::META_KEY_URL_SHORTENING_METHOD)); // The shortener code of the item
		$source_shortening_method = trim((string)$meta->get_meta($item_source_id, self::META_KEY_URL_SHORTENING_METHOD));  // The shortener code of the source
		
		// If item has been shortened using the source's method, return that
		if( $item_shortening_method === $source_shortening_method ) {
			return apply_filters('wprss_ftp_shortened_item_url', $long_url, $post, false); // The false at the end means the URL was not shortened;
		}
		
		// Initiates the actual shortening
		$short_url = self::shorten_url($long_url, $source_shortening_method);
		
		// If URL could not be shortened using services, log and return the error
		if( is_wp_error($short_url) ) {
			/* @var $short_url WP_Error */
			WPRSS_FTP_Utils::log( sprintf('Could not shorten URL "%3$s" of post #%4$s using method "%1$s": %2$s', $source_shortening_method, $short_url->get_error_message(), $long_url, $post->ID), __FUNCTION__);
			return $default;
		}
		
		// Saving new meta data
		$meta->add_meta($post->ID, self::META_KEY_SHORT_URL, $short_url);
		$meta->add_meta($post->ID, self::META_KEY_URL_SHORTENING_METHOD, $source_shortening_method);
		
		return apply_filters('wprss_ftp_shortened_item_url', $short_url, $post, true);
	}
コード例 #5
0
	/**
	 * Changes the tags to be stripped from the feed item.
	 * 
	 * @since 2.2
	 */
	public static function feed_tags_to_strip( $tags, $source ) {
		if ( is_null( $source ) ) return $tags;
		
		$allow_embedded_content = WPRSS_FTP_Meta::get_instance()->get_meta( $source, 'allow_embedded_content' );
		if ( WPRSS_FTP_Utils::multiboolean( $allow_embedded_content ) !== true )
			return $tags;
		
		// Remove the allowed tags from the list of tags to remove
		$tags = array_diff( $tags, self::get_allowed_embed_tags() );
		$tags = array_values( array_filter( $tags ) );
		
		return $tags;
	}
コード例 #6
0
	/**
	 * Changes the post title links to the original source. This is turned on through
	 * the wprss_ftp_link_post_title filter, when the value returned is TRUE.
	 *
	 * @since 1.6
	 */
	public function link_posts_to_external( $url ) {
		// If the id parameter was not passed, do nothing and return the title.
		if ( $url === NULL || get_post() === NULL ) return $url;

		// Get the feed source for the post
		$source = WPRSS_FTP_Meta::get_instance()->get_meta( get_the_ID(), 'feed_source' );


		// IF AN IMPORTED POST
		if ( $source !== '' && !is_single() ) {
			// Check whether the title is to be linked to the external, original post
			$filter_value = apply_filters( 'wprss_ftp_link_post_title', FALSE );
			// Get the permalink meta data for the post
			$permalink = get_post_meta( get_the_ID(), 'wprss_item_permalink', TRUE );

			// If the permalink is empty, return the regular WordPress post url
			if ( $permalink === '' ) return $url;

			// If the filter value is an array, check if the source ID is in the array
			if ( is_array( $filter_value ) ) {
				$link_external = in_array( $source , $filter_value );
			}
			// If the filter value is not an array, check if it is TRUE or if it is the ID of the source
			else {
				$link_external = ( $filter_value === TRUE || strval($filter_value) === strval($source) );
			}

			// If link_external is TRUE, return the permalink of the original article.
			// Otherwise, return the regular WordPress post url
			return ( ( $link_external === TRUE )? $permalink : $url );
		}
		else {
			return $url;
		}
	}
コード例 #7
0
	/**
	 * Disables the link to source option for each feed source
	 * 
	 * @since 2.4
	 */
	public static function source_link_update() {
		// Get the meta class instance
		$meta = WPRSS_FTP_Meta::get_instance();

		if ( ! function_exists( "wprss_get_all_feed_sources" ) ) return;

		// Get all feed sources
		$feed_sources = wprss_get_all_feed_sources();

		// Keep a count of feed sources that got updated
		$count = 0;

		// Iterate all feed sources
		while ( $feed_sources->have_posts() ) {
			// Prepare loop variables
			$feed_sources->the_post();
			$ID = get_the_ID();

			// Get the source link enable and text meta for the feed sources
			$source_link = $meta->get_meta( $ID, 'source_link' );
			$source_link_text = $meta->get_meta( $ID, 'source_link_text' );
			// Get the post append text
			$post_append = $meta->get_meta( $ID, 'post_append' );
			
			// If the post's feed source has source_link enabled ...
			if ( WPRSS_FTP_Utils::multiboolean( $source_link ) === TRUE ) {

				// Disable the source link option
				update_post_meta( $ID, WPRSS_FTP_Meta::META_PREFIX . 'source_link', 'false' );

				// Increment the count
				$count++;
				
				// If an asterisk is found in the source link text, use regex to generate the linked phrase
				if ( stripos( $source_link_text, '*') !== FALSE ) {
					// Prepare the replacement <a> tag with the placeholder for feed_url
					$feed_url_link = "<a target=\"_blank\" href=\"{{feed_url}}\">$1</a>";
					// Replace the string in double asteriks into the <a> tag
					$linked_text = preg_replace(
						'/\*\*(.*?)\*\*/',									// The regex pattern to search for
						$feed_url_link,										// The replacement text
						$source_link_text									// The text to which to search in
					);
					// Prepare the replacement <a> tag with the placeholder for post_url
					$post_url_link = "<a target=\"_blank\" href=\"{{original_post_url}}\">$1</a>";
					// Replace the string in single asteriks into the <a> tag
					$linked_text = preg_replace(
						'/\*(.*?)\*/',										// The regex pattern to search for
						$post_url_link,										// The replacement text
						$linked_text										// The text to which to search in
					);

					
					// Update the post append text
					if ( strlen( $post_append ) > 0 ) {
						$post_append .= '<br/>';
					}
					$post_append .= $linked_text;
					update_post_meta( $ID, WPRSS_FTP_Meta::META_PREFIX . 'post_append', $post_append );

				}


			} // END OF SOURCLE LINK ENABLED CHECK

		} // END OF WHILE LOOP

		if ( $count > 0 ) {
			set_transient( 'wprss_ftp_admin_notices', array( 'WPRSS_FTP_Utils', 'source_link_update_notice' ), 0 );
		}

		// Restore the $post global to the current post in the main query
		wp_reset_postdata();

	} // END OF source_link_update() 
コード例 #8
0
/**
 * 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;
}
コード例 #9
0
	/**
	 * Returns the singleton instance
	 * 
	 * @return WPRSS_FTP_Meta The singleton instance of this class.
	 */
	public static function get_instance() {
		if ( self::$instance === NULL ) {
			self::$instance = new self();
		}
		return self::$instance;
	}
コード例 #10
0
	/**
	 * Prints the enclosure link in the post content.
	 *
	 * @since 2.8
	 */
	public static function enclosure_link( $content ) {
		global $post;

		// Start with the original content
		$new_content = $content;

		// check if enclosure is enabled, check filter for before and after (default: before), check filter to modify print

		// Check if not in the feed and if showing the post, not processing the content
		if ( is_feed() || !( is_singular() || is_home() || is_front_page() || is_search() || is_preview() ) ) {
			return $content;
		}

		// 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 === '' ) {
			return $content;
		}

		// Check if enclosure is enabled
		$enclosure_enabled = get_post_meta( $source, 'wprss_enclosure', TRUE );
		if ( $enclosure_enabled === '' || !WPRSS_FTP_Utils::multiboolean( $enclosure_enabled ) ) {
			return $content;
		}

		// Get the post enclosure link
		$enclosure_link = WPRSS_FTP_Meta::get_instance()->get_meta( $post->ID, 'enclosure_link' );
		// Prepare the default output
		$enclosure_output = "<a href='$enclosure_link'>$enclosure_link</a>";

		// Get the position of the enclosure link. Default: 'before' post content
		$pos = apply_filters( 'wprss_ftp_enclosure_link_position', 'before' );

		// Filter the enclosure output
		$enclosure_output = apply_filters( 'wprss_ftp_enclosure_output', $enclosure_output, $enclosure_link, $source );

		// Show the enclosure link at the appropriate position
		switch( $pos ) {
			default:
			case 'before':
				$new_content = $enclosure_output . $content;
				break;
			case 'after':
				$new_content = $content . $enclosure_output;
				break;
		}
		
		// Return the new content
		return $new_content;
	}
コード例 #11
0
	/**
	 * Checks if the post is to be rejected since no featured image was determined.
	 *
	 * If the 'must_have_ft_image' is enabled for the feed source that imported the post, the
	 * post is deleted. Otherwise no action is taken.
	 *
	 * This function triggers the action 'wprss_ftp_after_post_rejected_no_ft_image' upon
	 * completion.
	 *
	 * @since 3.3
	 * @param $post_id   int The ID of the post.
	 * @param $source_id int The ID of the feed source.
	 */
	public function maybe_reject_post( $post_id, $source_id ) {
		wprss_log( 'Checking if post should be rejected due to lack of a featured image ...', NULL, WPRSS_LOG_LEVEL_SYSTEM );
		// Get the meta option value that determines if posts are allowed if they have no featured image
		$must_have_ft_image = WPRSS_FTP_Meta::get_instance()->get( $source_id, 'must_have_ft_image' );
		// Check if the option is enabled
		$enabled = WPRSS_FTP_Utils::multiboolean( $must_have_ft_image ) === TRUE;
		if ( $enabled ) {
			// Filter force deletion. False for sending post to trash
			$force_delete = apply_filters( 'wprss_ftp_post_rejected_no_ft_image_force_delete', TRUE );
			// Delete the post
			wp_delete_post( $post_id, $force_delete );
			// Return null from the convertor
			add_filter( 'wprss_ftp_converter_return_post_'.$post_id, '__return_false' );
			// Logging
			wprss_log( 'Post rejected.', NULL, WPRSS_LOG_LEVEL_SYSTEM );
		} else {
			// Logging
			wprss_log( 'Post accepted.', NULL, WPRSS_LOG_LEVEL_SYSTEM );
		}
		// Trigger action. First param: TRUE reject, FALSE accepted.
		do_action( 'wprss_ftp_after_post_rejected_no_ft_image', $enabled, $source_id );
	}
コード例 #12
0
	/**
	 * Converts the old taxonomy meta fields into the new format.
	 * Does NOT save into database.
	 *
	 * @since 3.1
	 * @param int $post_id The ID of the post
	 * @return array The new settings fields
	 */
	public static function convert_post_taxonomy_settings() {
		// Prepare the old fields
		$old_fields = array(
			'post_taxonomy',
			'post_terms',
			'post_auto_tax_terms',
			'post_tags'
		);
		// Prepare the new fields array
		$settings = array();
		// Generate the new fields
		foreach( $old_fields as $old_field ) {
			$settings[ $old_field ] = self::get_instance()->get( $old_field );
		}
		// Return the new fields
		return WPRSS_FTP_Meta::convert_taxonomy_option( $settings );
	}