WP_Press_This::get_suggested_content( array $data )

Gets the source page’s suggested content, based on passed data (description, selection, etc).


Description Description

Features a blockquoted excerpt, as well as content attribution, if any.


Parameters Parameters

$data

(array) (Required) The site's data.


Top ↑

Return Return

(string) Discovered content, or empty


Top ↑

Source Source

File: wp-admin/includes/class-wp-press-this.php

	public function get_suggested_content( $data ) {
		$content = $text = '';

		if ( ! empty( $data['s'] ) ) {
			$text = $data['s'];
		} else if ( ! empty( $data['_meta'] ) ) {
			if ( ! empty( $data['_meta']['twitter:description'] ) ) {
				$text = $data['_meta']['twitter:description'];
			} else if ( ! empty( $data['_meta']['og:description'] ) ) {
				$text = $data['_meta']['og:description'];
			} else if ( ! empty( $data['_meta']['description'] ) ) {
				$text = $data['_meta']['description'];
			}

			// If there is an ellipsis at the end, the description is very likely auto-generated. Better to ignore it.
			if ( $text && substr( $text, -3 ) === '...' ) {
				$text = '';
			}
		}

		$default_html = array( 'quote' => '', 'link' => '', 'embed' => '' );

		if ( ! empty( $data['u'] ) && $this->_limit_embed( $data['u'] ) ) {
			$default_html['embed'] = '<p>[embed]' . $data['u'] . '[/embed]</p>';

			if ( ! empty( $data['s'] ) ) {
				// If the user has selected some text, do quote it.
				$default_html['quote'] = '<blockquote>%1$s</blockquote>';
			}
		} else {
			$default_html['quote'] = '<blockquote>%1$s</blockquote>';
			$default_html['link'] = '<p>' . _x( 'Source:', 'Used in Press This to indicate where the content comes from.' ) .
				' <em><a href="%1$s">%2$s</a></em></p>';
		}

		/**
		 * Filters the default HTML tags used in the suggested content for the editor.
		 *
		 * The HTML strings use printf format. After filtering the content is added at the specified places with `sprintf()`.
		 *
		 * @since 4.2.0
		 *
		 * @param array $default_html Associative array with three possible keys:
		 *                                - 'quote' where %1$s is replaced with the site description or the selected content.
		 *                                - 'link' where %1$s is link href, %2$s is link text, usually the source page title.
		 *                                - 'embed' which contains an [embed] shortcode when the source page offers embeddable content.
		 * @param array $data         Associative array containing the data from the source page.
		 */
		$default_html = apply_filters( 'press_this_suggested_html', $default_html, $data );

		if ( ! empty( $default_html['embed'] ) ) {
			$content .= $default_html['embed'];
		}

		// Wrap suggested content in the specified HTML.
		if ( ! empty( $default_html['quote'] ) && $text ) {
			$content .= sprintf( $default_html['quote'], $text );
		}

		// Add source attribution if there is one available.
		if ( ! empty( $default_html['link'] ) ) {
			$title = $this->get_suggested_title( $data );
			$url = $this->get_canonical_link( $data );

			if ( ! $title ) {
				$title = $this->get_source_site_name( $data );
			}

			if ( $url && $title ) {
				$content .= sprintf( $default_html['link'], $url, $title );
			}
		}

		return $content;
	}

Top ↑

Changelog Changelog

Changelog
Version Description
4.2.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.