From aee04ab9a35d78086e48ed48ec12b20d5248ec06 Mon Sep 17 00:00:00 2001 From: Andrew Ozz Date: Sun, 1 Mar 2015 22:43:36 +0000 Subject: [PATCH] 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 --- src/wp-admin/includes/class-wp-press-this.php | 16 +++- src/wp-admin/js/press-this.js | 88 +++++++------------ src/wp-includes/script-loader.php | 17 ---- 3 files changed, 44 insertions(+), 77 deletions(-) diff --git a/src/wp-admin/includes/class-wp-press-this.php b/src/wp-admin/includes/class-wp-press-this.php index b3acb52b5c..1ca2ce2ff9 100644 --- a/src/wp-admin/includes/class-wp-press-this.php +++ b/src/wp-admin/includes/class-wp-press-this.php @@ -31,6 +31,9 @@ class WP_Press_This { * @return array Site settings. */ public function site_settings() { + $html = '

' . _x( 'Source:', 'Used in Press This to indicate where the content comes from.' ) . + ' %2$s

'; + return array( // Used to trigger the bookmarklet update notice. // 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 * - * @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 ), ); } diff --git a/src/wp-admin/js/press-this.js b/src/wp-admin/js/press-this.js index 9836a2602e..d718abcf2d 100644 --- a/src/wp-admin/js/press-this.js +++ b/src/wp-admin/js/press-this.js @@ -100,14 +100,9 @@ /** * 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 */ - function getCanonicalLink( data ) { - if ( ! data || data.length ) { - return ''; - } - + function getCanonicalLink() { var link = ''; 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. * - * @param data object Usually WpPressThis_App.data * @returns string Discovered site name, or empty */ - function getSourceSiteName( data ) { - if ( ! data || data.length ) { - return ''; - } - - var name=''; + function getSourceSiteName() { + var name = ''; if ( data._meta ) { 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. * - * @param data object Usually WpPressThis_App.data * @returns string Discovered page title, or empty */ - function getSuggestedTitle( data ) { - if ( ! data || data.length ) { - return __( 'newPost' ); - } - + function getSuggestedTitle() { var title = ''; if ( data.t ) { title = data.t; } - if ( ! title.length && data._meta ) { + if ( ! title && data._meta ) { if ( data._meta['twitter:title'] && data._meta['twitter:title'].length ) { title = data._meta['twitter:title']; } else if ( data._meta['og:title'] && data._meta['og:title'].length ) { @@ -182,60 +167,51 @@ } } - if ( ! title.length ) { + if ( ! title ) { title = __( 'newPost' ); hasEmptyTitleStr = true; } - return title.replace( /\\/g, '' ); + return sanitizeText( title ); } /** * 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. * - * @param data object Usually WpPressThis_App.data * @returns string Discovered content, or empty */ - function getSuggestedContent( data ) { - if ( ! data || data.length ) { - return ''; - } - - var content = '', - title = getSuggestedTitle( data ), - url = getCanonicalLink( data ), - siteName = getSourceSiteName( data ); + function getSuggestedContent() { + var content = '', + text = '', + title = getSuggestedTitle(), + url = getCanonicalLink(), + siteName = getSourceSiteName(); if ( data.s && data.s.length ) { - content = data.s; + text = data.s; } else if ( data._meta ) { 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 ) { - content = data._meta['og:description']; + text = data._meta['og:description']; } 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. - content = ( content.length ? '
' + sanitizeText( content ) + '
' : '' ); + if ( text ) { + text = sanitizeText( text ); + // Wrap suggested content in blockquote tag. + content = '
' + text + '
'; + } // Add a source attribution if there is one available. - if ( ( ( title.length && __( 'newPost' ) !== title ) || siteName.length ) && url.length ) { - content += '

'; - content += __( 'source' ); - content += ' '; - content += __( 'sourceLink').replace( '%1$s', encodeURI( url ) ).replace( '%2$s', sanitizeText( title || siteName ) ); - content += '

'; + if ( url && siteConfig.suggestedHTML && ( ( title && __( 'newPost' ) !== title ) || siteName ) ) { + content += siteConfig.suggestedHTML.replace( '%1$s', encodeURI( url ) ).replace( '%2$s', ( title || siteName ) ); } - if ( ! content ) { - content = ''; - } - - return content.replace( /\\/g, '' ); + return content || ''; } /** @@ -472,7 +448,7 @@ renderError( response.data.errorMessage ); hideSpinner(); } else if ( response.data.redirect ) { - if ( window.opener && siteConfig.redir_in_parent ) { + if ( window.opener && siteConfig.redirInParent ) { try { window.opener.location.href = response.data.redirect; } catch( er ) {} @@ -514,13 +490,10 @@ } if ( ! hasSetFocus ) { - // Append to top of content on 1st media insert - editor.setContent( newContent + editor.getContent() ); - } else { - // Or add where the cursor was last positioned in TinyMCE - editor.execCommand( 'mceInsertContent', false, newContent ); + editor.focus(); } + editor.execCommand( 'mceInsertContent', false, newContent ); hasSetFocus = true; } @@ -673,7 +646,6 @@ hasSetFocus = true; } ); } - } /** diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index bdd68710b3..fcedd6d588 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -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 ); 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', '%2$s' ), 'newPost' => __( 'Title' ), 'unexpectedError' => __( 'Sorry, but an unexpected error occurred.' ), 'saveAlert' => __( 'The changes you made will be lost if you navigate away from this page.' ),