PressThis:

- Simplify `getSuggestedContent()` and helpers. No need to override the global `data`.
- Replace the `press_this_source_string` and `press_this_source_link` filters with `press_this_suggested_html` that allows filtering of the link and the wrapper HTML tags.
See #31373.

git-svn-id: https://develop.svn.wordpress.org/trunk@31595 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Andrew Ozz 2015-03-01 22:43:36 +00:00
parent 0778a17875
commit aee04ab9a3
3 changed files with 44 additions and 77 deletions

View File

@ -31,6 +31,9 @@ class WP_Press_This {
* @return array Site settings. * @return array Site settings.
*/ */
public function site_settings() { public function site_settings() {
$html = '<p class="press-this-suggested-source">' . _x( 'Source:', 'Used in Press This to indicate where the content comes from.' ) .
' <cite><a href="%1$s">%2$s</a></cite></p>';
return array( return array(
// Used to trigger the bookmarklet update notice. // Used to trigger the bookmarklet update notice.
// Needs to be set here and in get_shortcut_link() in wp-includes/link-template.php. // Needs to be set here and in get_shortcut_link() in wp-includes/link-template.php.
@ -41,9 +44,18 @@ class WP_Press_This {
* *
* @since 4.2.0 * @since 4.2.0
* *
* @param bool $redir_in_parent Whether to redirect in parent window or not. Default false. * @param bool false Whether to redirect in parent window or not. Default false.
*/ */
'redir_in_parent' => apply_filters( 'press_this_redirect_in_parent', false ), 'redirInParent' => apply_filters( 'press_this_redirect_in_parent', false ),
/**
* Filter the HTML for the Press This source attribution.
*
* @since 4.2.0
*
* @param string $html Default HTML, %1$s is link href, %2$s is link text.
*/
'suggestedHTML' => apply_filters( 'press_this_suggested_html', $html ),
); );
} }

View File

@ -100,14 +100,9 @@
/** /**
* Gets the source page's canonical link, based on passed location and meta data. * Gets the source page's canonical link, based on passed location and meta data.
* *
* @param data object Usually WpPressThis_App.data
* @returns string Discovered canonical URL, or empty * @returns string Discovered canonical URL, or empty
*/ */
function getCanonicalLink( data ) { function getCanonicalLink() {
if ( ! data || data.length ) {
return '';
}
var link = ''; var link = '';
if ( data._links ) { if ( data._links ) {
@ -128,21 +123,16 @@
} }
} }
return decodeURI( link ); return checkUrl( decodeURI( link ) );
} }
/** /**
* Gets the source page's site name, based on passed meta data. * Gets the source page's site name, based on passed meta data.
* *
* @param data object Usually WpPressThis_App.data
* @returns string Discovered site name, or empty * @returns string Discovered site name, or empty
*/ */
function getSourceSiteName( data ) { function getSourceSiteName() {
if ( ! data || data.length ) { var name = '';
return '';
}
var name='';
if ( data._meta ) { if ( data._meta ) {
if ( data._meta['og:site_name'] && data._meta['og:site_name'].length ) { if ( data._meta['og:site_name'] && data._meta['og:site_name'].length ) {
@ -152,27 +142,22 @@
} }
} }
return name.replace( /\\/g, '' ); return sanitizeText( name );
} }
/** /**
* Gets the source page's title, based on passed title and meta data. * Gets the source page's title, based on passed title and meta data.
* *
* @param data object Usually WpPressThis_App.data
* @returns string Discovered page title, or empty * @returns string Discovered page title, or empty
*/ */
function getSuggestedTitle( data ) { function getSuggestedTitle() {
if ( ! data || data.length ) {
return __( 'newPost' );
}
var title = ''; var title = '';
if ( data.t ) { if ( data.t ) {
title = data.t; title = data.t;
} }
if ( ! title.length && data._meta ) { if ( ! title && data._meta ) {
if ( data._meta['twitter:title'] && data._meta['twitter:title'].length ) { if ( data._meta['twitter:title'] && data._meta['twitter:title'].length ) {
title = data._meta['twitter:title']; title = data._meta['twitter:title'];
} else if ( data._meta['og:title'] && data._meta['og:title'].length ) { } else if ( data._meta['og:title'] && data._meta['og:title'].length ) {
@ -182,60 +167,51 @@
} }
} }
if ( ! title.length ) { if ( ! title ) {
title = __( 'newPost' ); title = __( 'newPost' );
hasEmptyTitleStr = true; hasEmptyTitleStr = true;
} }
return title.replace( /\\/g, '' ); return sanitizeText( title );
} }
/** /**
* Gets the source page's suggested content, based on passed data (description, selection, etc). * Gets the source page's suggested content, based on passed data (description, selection, etc).
* Features a blockquoted excerpt, as well as content attribution, if any. * Features a blockquoted excerpt, as well as content attribution, if any.
* *
* @param data object Usually WpPressThis_App.data
* @returns string Discovered content, or empty * @returns string Discovered content, or empty
*/ */
function getSuggestedContent( data ) { function getSuggestedContent() {
if ( ! data || data.length ) { var content = '',
return ''; text = '',
} title = getSuggestedTitle(),
url = getCanonicalLink(),
var content = '', siteName = getSourceSiteName();
title = getSuggestedTitle( data ),
url = getCanonicalLink( data ),
siteName = getSourceSiteName( data );
if ( data.s && data.s.length ) { if ( data.s && data.s.length ) {
content = data.s; text = data.s;
} else if ( data._meta ) { } else if ( data._meta ) {
if ( data._meta['twitter:description'] && data._meta['twitter:description'].length ) { if ( data._meta['twitter:description'] && data._meta['twitter:description'].length ) {
content = data._meta['twitter:description']; text = data._meta['twitter:description'];
} else if ( data._meta['og:description'] && data._meta['og:description'].length ) { } else if ( data._meta['og:description'] && data._meta['og:description'].length ) {
content = data._meta['og:description']; text = data._meta['og:description'];
} else if ( data._meta.description && data._meta.description.length ) { } else if ( data._meta.description && data._meta.description.length ) {
content = data._meta.description; text = data._meta.description;
} }
} }
// Wrap suggested content in blockquote tag, if we have any. if ( text ) {
content = ( content.length ? '<blockquote class="press-this-suggested-content">' + sanitizeText( content ) + '</blockquote>' : '' ); text = sanitizeText( text );
// Wrap suggested content in blockquote tag.
content = '<blockquote class="press-this-suggested-content">' + text + '</blockquote>';
}
// Add a source attribution if there is one available. // Add a source attribution if there is one available.
if ( ( ( title.length && __( 'newPost' ) !== title ) || siteName.length ) && url.length ) { if ( url && siteConfig.suggestedHTML && ( ( title && __( 'newPost' ) !== title ) || siteName ) ) {
content += '<p class="press-this-suggested-source">'; content += siteConfig.suggestedHTML.replace( '%1$s', encodeURI( url ) ).replace( '%2$s', ( title || siteName ) );
content += __( 'source' );
content += ' <cite>';
content += __( 'sourceLink').replace( '%1$s', encodeURI( url ) ).replace( '%2$s', sanitizeText( title || siteName ) );
content += '</cite></p>';
} }
if ( ! content ) { return content || '';
content = '';
}
return content.replace( /\\/g, '' );
} }
/** /**
@ -472,7 +448,7 @@
renderError( response.data.errorMessage ); renderError( response.data.errorMessage );
hideSpinner(); hideSpinner();
} else if ( response.data.redirect ) { } else if ( response.data.redirect ) {
if ( window.opener && siteConfig.redir_in_parent ) { if ( window.opener && siteConfig.redirInParent ) {
try { try {
window.opener.location.href = response.data.redirect; window.opener.location.href = response.data.redirect;
} catch( er ) {} } catch( er ) {}
@ -514,13 +490,10 @@
} }
if ( ! hasSetFocus ) { if ( ! hasSetFocus ) {
// Append to top of content on 1st media insert editor.focus();
editor.setContent( newContent + editor.getContent() );
} else {
// Or add where the cursor was last positioned in TinyMCE
editor.execCommand( 'mceInsertContent', false, newContent );
} }
editor.execCommand( 'mceInsertContent', false, newContent );
hasSetFocus = true; hasSetFocus = true;
} }
@ -673,7 +646,6 @@
hasSetFocus = true; hasSetFocus = true;
} ); } );
} }
} }
/** /**

View File

@ -474,23 +474,6 @@ function wp_default_scripts( &$scripts ) {
$scripts->add( 'press-this', "/wp-admin/js/press-this$suffix.js", array( 'jquery', 'tags-box' ), false, 1 ); $scripts->add( 'press-this', "/wp-admin/js/press-this$suffix.js", array( 'jquery', 'tags-box' ), false, 1 );
did_action( 'init' ) && $scripts->localize( 'press-this', 'pressThisL10n', array( did_action( 'init' ) && $scripts->localize( 'press-this', 'pressThisL10n', array(
/**
* Filter the string displayed before the source attribution string in Press This.
*
* @since 4.2.0
*
* @param string $string Internationalized source string.
*/
'source' => apply_filters( 'press_this_source_string', __( 'Source:' ) ),
/**
* Filter the HTML link format for the Press This source attribution, can control target, class, etc.
*
* @since 4.2.0
*
* @param string $link_format Link format, %1$s is link href, %2$s is link text.
*/
'sourceLink' => apply_filters( 'press_this_source_link', '<a href="%1$s">%2$s</a>' ),
'newPost' => __( 'Title' ), 'newPost' => __( 'Title' ),
'unexpectedError' => __( 'Sorry, but an unexpected error occurred.' ), 'unexpectedError' => __( 'Sorry, but an unexpected error occurred.' ),
'saveAlert' => __( 'The changes you made will be lost if you navigate away from this page.' ), 'saveAlert' => __( 'The changes you made will be lost if you navigate away from this page.' ),