diff --git a/js/tinymce4/media-credit-tinymce-switch.js b/js/tinymce4/media-credit-tinymce-switch.js index 5a2d68cc..2abef8f5 100644 --- a/js/tinymce4/media-credit-tinymce-switch.js +++ b/js/tinymce4/media-credit-tinymce-switch.js @@ -1,7 +1,7 @@ /* * Extend editor formatting when switching between HTML and Visual mode. * - * Based on /wp-admin/js/editor.js + * Based on revision 33771 of /wp-admin/js/editor.js (removep) * */ @@ -10,98 +10,102 @@ $(function () { - window.switchEditors._wp_Nop = function( content ) { - var blocklist1, blocklist2, - preserve_linebreaks = false, - preserve_br = false; - - // Protect pre|script tags - if ( content.indexOf( ']*>[\s\S]+?<\/\1>/g, function( a ) { - a = a.replace( /
(\r\n|\n)?/g, '' ); - a = a.replace( /<\/?p( [^>]*)?>(\r\n|\n)?/g, '' ); - return a.replace( /\r?\n/g, '' ); - }); - } - - // keep
tags inside captions and remove line breaks - if ( content.indexOf( '[caption' ) !== -1 ) { - preserve_br = true; - content = content.replace( /\[caption[\s\S]+?\[\/caption\]/g, function( a ) { - return a.replace( /]*)>/g, '' ).replace( /[\r\n\t]+/, '' ); - }); - } - - // Pretty it up for the source editor - blocklist1 = 'blockquote|ul|ol|li|table|thead|tbody|tfoot|tr|th|td|div|h[1-6]|p|fieldset'; - content = content.replace( new RegExp( '\\s*\\s*', 'g' ), '\n' ); - content = content.replace( new RegExp( '\\s*<((?:' + blocklist1 + ')(?: [^>]*)?)>', 'g' ), '\n<$1>' ); - - // Mark

if it has any attributes. - content = content.replace( /(

]+>.*?)<\/p>/g, '$1' ); - - // Separate

containing

- content = content.replace( /]*)?>\s*

/gi, '\n\n' ); - - // Remove

and
- content = content.replace( /\s*

/gi, '' ); - content = content.replace( /\s*<\/p>\s*/gi, '\n\n' ); - content = content.replace( /\n[\s\u00a0]+\n/g, '\n\n' ); - content = content.replace( /\s*
\s*/gi, '\n' ); - - // Fix some block element newline issues - content = content.replace( /\s*

\s*/g, '
\n' ); - content = content.replace( /\s*\[caption([^\[]+)\[\/caption\]\s*/gi, '\n\n[caption$1[/caption]\n\n' ); - content = content.replace( /caption\]\n\n+\[caption/g, 'caption]\n\n[caption' ); - - // BEGIN MODIFICATION - // Also handle media-credit shortcode - content = content.replace( /\s*\[media-credit([^\[]+)\[\/media-credit\]\s*/gi, '\n\n[media-credit$1[/media-credit]\n\n' ); - content = content.replace( /\[\/media-credit\]\n\n([^\[]*)\[\/caption\]/gi, '[/media-credit] $1[/caption]' ); // remove extra newlines for nested media-credit - content = content.replace( /\s*\[caption([^\[]+)\[media-credit([^\[]+)\[\/media-credit\]([^\[]*)\[\/caption\]\s*/gi, '\n\n[caption$1[media-credit$2[/media-credit]$3[/caption]\n\n' ); - // END MODIFICATION - - blocklist2 = 'blockquote|ul|ol|li|table|thead|tbody|tfoot|tr|th|td|h[1-6]|pre|fieldset'; - content = content.replace( new RegExp('\\s*<((?:' + blocklist2 + ')(?: [^>]*)?)\\s*>', 'g' ), '\n<$1>' ); - content = content.replace( new RegExp('\\s*\\s*', 'g' ), '\n' ); - content = content.replace( /]*)>/g, '\t' ); - - if ( content.indexOf( '/g, '\n' ); - } - - if ( content.indexOf( ']*)?>\s*/g, '\n\n\n\n' ); - } - - if ( content.indexOf( '/g, function( a ) { - return a.replace( /[\r\n]+/g, '' ); - }); - } - - // Unmark special paragraph closing tags - content = content.replace( /<\/p#>/g, '

\n' ); - content = content.replace( /\s*(

]+>[\s\S]*?<\/p>)/g, '\n$1' ); - - // Trim whitespace - content = content.replace( /^\s+/, '' ); - content = content.replace( /[\s\u00a0]+$/, '' ); - - // put back the line breaks in pre|script - if ( preserve_linebreaks ) { - content = content.replace( //g, '\n' ); - } - - // and the
tags in captions - if ( preserve_br ) { - content = content.replace( /]*)>/g, '' ); - } - - return content; + window.switchEditors._wp_Nop = function( html ) { + var blocklist = 'blockquote|ul|ol|li|table|thead|tbody|tfoot|tr|th|td|h[1-6]|fieldset', + blocklist1 = blocklist + '|div|p', + blocklist2 = blocklist + '|pre', + preserve_linebreaks = false, + preserve_br = false; + + if ( ! html ) { + return ''; + } + + // Protect pre|script tags + if ( html.indexOf( ']*>[\s\S]+?<\/\1>/g, function( a ) { + a = a.replace( /
(\r\n|\n)?/g, '' ); + a = a.replace( /<\/?p( [^>]*)?>(\r\n|\n)?/g, '' ); + return a.replace( /\r?\n/g, '' ); + }); + } + + // keep
tags inside captions and remove line breaks + if ( html.indexOf( '[caption' ) !== -1 ) { + preserve_br = true; + html = html.replace( /\[caption[\s\S]+?\[\/caption\]/g, function( a ) { + return a.replace( /]*)>/g, '' ).replace( /[\r\n\t]+/, '' ); + }); + } + + // Pretty it up for the source editor + html = html.replace( new RegExp( '\\s*\\s*', 'g' ), '\n' ); + html = html.replace( new RegExp( '\\s*<((?:' + blocklist1 + ')(?: [^>]*)?)>', 'g' ), '\n<$1>' ); + + // Mark

if it has any attributes. + html = html.replace( /(

]+>.*?)<\/p>/g, '$1' ); + + // Separate

containing

+ html = html.replace( /]*)?>\s*

/gi, '\n\n' ); + + // Remove

and
+ html = html.replace( /\s*

/gi, '' ); + html = html.replace( /\s*<\/p>\s*/gi, '\n\n' ); + html = html.replace( /\n[\s\u00a0]+\n/g, '\n\n' ); + html = html.replace( /\s*
\s*/gi, '\n' ); + + // Fix some block element newline issues + html = html.replace( /\s*

\s*/g, '
\n' ); + html = html.replace( /\s*\[caption([^\[]+)\[\/caption\]\s*/gi, '\n\n[caption$1[/caption]\n\n' ); + html = html.replace( /caption\]\n\n+\[caption/g, 'caption]\n\n[caption' ); + + // BEGIN MODIFICATION + // Also handle media-credit shortcode + html = html.replace( /\s*\[media-credit([^\[]+)\[\/media-credit\]\s*/gi, '\n\n[media-credit$1[/media-credit]\n\n' ); + html = html.replace( /\[\/media-credit\]\n\n([^\[]*)\[\/caption\]/gi, '[/media-credit] $1[/caption]' ); // remove extra newlines for nested media-credit + html = html.replace( /\s*\[caption([^\[]+)\[media-credit([^\[]+)\[\/media-credit\]([^\[]*)\[\/caption\]\s*/gi, '\n\n[caption$1[media-credit$2[/media-credit]$3[/caption]\n\n' ); + // END MODIFICATION + + html = html.replace( new RegExp('\\s*<((?:' + blocklist2 + ')(?: [^>]*)?)\\s*>', 'g' ), '\n<$1>' ); + html = html.replace( new RegExp('\\s*\\s*', 'g' ), '\n' ); + html = html.replace( /]*)>/g, '\t' ); + + if ( html.indexOf( '/g, '\n' ); + } + + if ( html.indexOf( ']*)?>\s*/g, '\n\n\n\n' ); + } + + if ( html.indexOf( '/g, function( a ) { + return a.replace( /[\r\n]+/g, '' ); + }); + } + + // Unmark special paragraph closing tags + html = html.replace( /<\/p#>/g, '

\n' ); + html = html.replace( /\s*(

]+>[\s\S]*?<\/p>)/g, '\n$1' ); + + // Trim whitespace + html = html.replace( /^\s+/, '' ); + html = html.replace( /[\s\u00a0]+$/, '' ); + + // put back the line breaks in pre|script + if ( preserve_linebreaks ) { + html = html.replace( //g, '\n' ); + } + + // and the
tags in captions + if ( preserve_br ) { + html = html.replace( /]*)>/g, '' ); + } + + return html; }; }); diff --git a/js/tinymce4/media-credit-tinymce.js b/js/tinymce4/media-credit-tinymce.js index 433b245f..72c20d17 100644 --- a/js/tinymce4/media-credit-tinymce.js +++ b/js/tinymce4/media-credit-tinymce.js @@ -183,7 +183,7 @@ tinymce.PluginManager.add( 'mediacredit', function( editor ) { return content.replace(pattern, function(a,b,c){ - var id, align, w, img, width, out, link, name, credit + var id, align, w, img, width, out, link, name, credit, trim = tinymce.trim; id = b.match( /id=['"]?([0-9]+)['"]? ?/ ); @@ -206,11 +206,16 @@ tinymce.PluginManager.add( 'mediacredit', function( editor ) { b = b.replace( link[0], '' ); } - //name = b.match(/name=['"]([^'"]*)['"] ?/i); - name = trim( b ).replace( /name=['"]/, '' ).replace( /['"]$/, '' ); + /* Name matching is more complicated to allow both ' and " inside each other */ + name = b.match(/name=[']([^']*)['] ?/i); + if ( !name ) { + name = b.match(/name=["]([^"]*)["] ?/i); + } if ( name ) { b = b.replace( name[0], '' ); + } else { + name = ''; // otherwise null gets handled as a string } c = trim( c ); @@ -241,7 +246,7 @@ tinymce.PluginManager.add( 'mediacredit', function( editor ) { width += 10; } - credit = name ? name : ($mediaCredit.id[id] + $mediaCredit.separator + $mediaCredit.organization); + credit = '' + (name ? name : ($mediaCredit.id[id] + $mediaCredit.separator + $mediaCredit.organization)); credit = credit.replace(/<[^>]+>(.*)<\/[^>]+>/g, '$1'); // basic sanitation out = img + wp.html.string({ diff --git a/media-credit.php b/media-credit.php index 076913de..d8286c5f 100644 --- a/media-credit.php +++ b/media-credit.php @@ -3,13 +3,13 @@ Plugin Name: Media Credit Plugin URI: http://www.scottbressler.com/blog/plugins/media-credit/ Description: This plugin adds a "Credit" field to the media uploading and editing tool and inserts this credit when the images appear on your blog. -Version: 2.6.1 +Version: 2.6.2 Author: Scott Bressler Author URI: http://www.scottbressler.com/blog/ License: GPL2 */ -define( 'MEDIA_CREDIT_VERSION', '2.6.1' ); +define( 'MEDIA_CREDIT_VERSION', '2.6.2' ); define( 'MEDIA_CREDIT_URL', plugins_url(plugin_basename(dirname(__FILE__)).'/') ); define( 'MEDIA_CREDIT_EMPTY_META_STRING', ' ' ); define( 'MEDIA_CREDIT_POSTMETA_KEY', '_media_credit' ); diff --git a/readme.txt b/readme.txt index 503780c7..b84c7485 100644 --- a/readme.txt +++ b/readme.txt @@ -4,7 +4,7 @@ Donate link: http://www.scottbressler.com/blog/plugins/ Tags: media, image, images, credit, byline, author, user Requires at least: 4.2 Tested up to: 4.3 -Stable tag: 2.6.1 +Stable tag: 2.6.2 Adds a "Credit" field when uploading media to posts and displays it under the images on your blog to properly credit the artist. @@ -92,6 +92,10 @@ Feel free to get in touch with us about anything you'd like us to add to this li == Changelog == += 2.6.2 (Aug. 29, 2015) = +* Updated JS for switching between Visual and HTML editors +* Fixed freeform credit parsing in Visual editor + = 2.6.1 (Aug. 17, 2015) = * Fixed "media credit at end"