Skip to content

Commit

Permalink
Version 2.6.2
Browse files Browse the repository at this point in the history
git-svn-id: http://plugins.svn.wordpress.org/media-credit/trunk@1233671 b8457f37-d9ea-0310-8a92-e5e31aec5664
  • Loading branch information
mundschenk-at committed Aug 29, 2015
1 parent a83216c commit 4dace12
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 100 deletions.
190 changes: 97 additions & 93 deletions js/tinymce4/media-credit-tinymce-switch.js
Original file line number Diff line number Diff line change
@@ -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)
*
*/

Expand All @@ -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( '<pre' ) !== -1 || content.indexOf( '<script' ) !== -1 ) {
preserve_linebreaks = true;
content = content.replace( /<(pre|script)[^>]*>[\s\S]+?<\/\1>/g, function( a ) {
a = a.replace( /<br ?\/?>(\r\n|\n)?/g, '<wp-line-break>' );
a = a.replace( /<\/?p( [^>]*)?>(\r\n|\n)?/g, '<wp-line-break>' );
return a.replace( /\r?\n/g, '<wp-line-break>' );
});
}

// keep <br> 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( /<br([^>]*)>/g, '<wp-temp-br$1>' ).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*</(' + blocklist1 + ')>\\s*', 'g' ), '</$1>\n' );
content = content.replace( new RegExp( '\\s*<((?:' + blocklist1 + ')(?: [^>]*)?)>', 'g' ), '\n<$1>' );

// Mark </p> if it has any attributes.
content = content.replace( /(<p [^>]+>.*?)<\/p>/g, '$1</p#>' );

// Separate <div> containing <p>
content = content.replace( /<div( [^>]*)?>\s*<p>/gi, '<div$1>\n\n' );

// Remove <p> and <br />
content = content.replace( /\s*<p>/gi, '' );
content = content.replace( /\s*<\/p>\s*/gi, '\n\n' );
content = content.replace( /\n[\s\u00a0]+\n/g, '\n\n' );
content = content.replace( /\s*<br ?\/?>\s*/gi, '\n' );

// Fix some block element newline issues
content = content.replace( /\s*<div/g, '\n<div' );
content = content.replace( /<\/div>\s*/g, '</div>\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*</(' + blocklist2 + ')>\\s*', 'g' ), '</$1>\n' );
content = content.replace( /<li([^>]*)>/g, '\t<li$1>' );

if ( content.indexOf( '<option' ) !== -1 ) {
content = content.replace( /\s*<option/g, '\n<option' );
content = content.replace( /\s*<\/select>/g, '\n</select>' );
}

if ( content.indexOf( '<hr' ) !== -1 ) {
content = content.replace( /\s*<hr( [^>]*)?>\s*/g, '\n\n<hr$1>\n\n' );
}

if ( content.indexOf( '<object' ) !== -1 ) {
content = content.replace( /<object[\s\S]+?<\/object>/g, function( a ) {
return a.replace( /[\r\n]+/g, '' );
});
}

// Unmark special paragraph closing tags
content = content.replace( /<\/p#>/g, '</p>\n' );
content = content.replace( /\s*(<p [^>]+>[\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( /<wp-line-break>/g, '\n' );
}

// and the <br> tags in captions
if ( preserve_br ) {
content = content.replace( /<wp-temp-br([^>]*)>/g, '<br$1>' );
}

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( '<pre' ) !== -1 || html.indexOf( '<script' ) !== -1 ) {
preserve_linebreaks = true;
html = html.replace( /<(pre|script)[^>]*>[\s\S]+?<\/\1>/g, function( a ) {
a = a.replace( /<br ?\/?>(\r\n|\n)?/g, '<wp-line-break>' );
a = a.replace( /<\/?p( [^>]*)?>(\r\n|\n)?/g, '<wp-line-break>' );
return a.replace( /\r?\n/g, '<wp-line-break>' );
});
}

// keep <br> 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( /<br([^>]*)>/g, '<wp-temp-br$1>' ).replace( /[\r\n\t]+/, '' );
});
}

// Pretty it up for the source editor
html = html.replace( new RegExp( '\\s*</(' + blocklist1 + ')>\\s*', 'g' ), '</$1>\n' );
html = html.replace( new RegExp( '\\s*<((?:' + blocklist1 + ')(?: [^>]*)?)>', 'g' ), '\n<$1>' );

// Mark </p> if it has any attributes.
html = html.replace( /(<p [^>]+>.*?)<\/p>/g, '$1</p#>' );

// Separate <div> containing <p>
html = html.replace( /<div( [^>]*)?>\s*<p>/gi, '<div$1>\n\n' );

// Remove <p> and <br />
html = html.replace( /\s*<p>/gi, '' );
html = html.replace( /\s*<\/p>\s*/gi, '\n\n' );
html = html.replace( /\n[\s\u00a0]+\n/g, '\n\n' );
html = html.replace( /\s*<br ?\/?>\s*/gi, '\n' );

// Fix some block element newline issues
html = html.replace( /\s*<div/g, '\n<div' );
html = html.replace( /<\/div>\s*/g, '</div>\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*</(' + blocklist2 + ')>\\s*', 'g' ), '</$1>\n' );
html = html.replace( /<li([^>]*)>/g, '\t<li$1>' );

if ( html.indexOf( '<option' ) !== -1 ) {
html = html.replace( /\s*<option/g, '\n<option' );
html = html.replace( /\s*<\/select>/g, '\n</select>' );
}

if ( html.indexOf( '<hr' ) !== -1 ) {
html = html.replace( /\s*<hr( [^>]*)?>\s*/g, '\n\n<hr$1>\n\n' );
}

if ( html.indexOf( '<object' ) !== -1 ) {
html = html.replace( /<object[\s\S]+?<\/object>/g, function( a ) {
return a.replace( /[\r\n]+/g, '' );
});
}

// Unmark special paragraph closing tags
html = html.replace( /<\/p#>/g, '</p>\n' );
html = html.replace( /\s*(<p [^>]+>[\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( /<wp-line-break>/g, '\n' );
}

// and the <br> tags in captions
if ( preserve_br ) {
html = html.replace( /<wp-temp-br([^>]*)>/g, '<br$1>' );
}

return html;
};

});
Expand Down
13 changes: 9 additions & 4 deletions js/tinymce4/media-credit-tinymce.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]+)['"]? ?/ );
Expand All @@ -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 );
Expand Down Expand Up @@ -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({
Expand Down
4 changes: 2 additions & 2 deletions media-credit.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Expand Down
6 changes: 5 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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"

Expand Down

0 comments on commit 4dace12

Please sign in to comment.