diff --git a/enfold-cille/functions.php b/enfold-cille/functions.php index 29c21bf..ec3e9d0 100644 --- a/enfold-cille/functions.php +++ b/enfold-cille/functions.php @@ -228,7 +228,12 @@ function share_post( $post_id, $post ) { } // Preparing post excerpt for Twitter: - $post_excerpt = mb_strpos( $text_content, $text_title ) === 0 ? $text_content : $text_title . ( empty( $text_content ) ? '.' : ". $text_content" ); + $text_excerpt = $text_content; + if ( mb_strpos( $text_content, $text_title ) !== 0 ) { + $post_excerpt = $text_title; + $post_excerpt .= empty( $text_content ) ? '.' : ". $text_content"; + } + $post_excerpt = html_entity_decode( $text_excerpt ); $limit = has_post_thumbnail( $post ) ? 140 - 25 : 140; if ( mb_strlen( $post_excerpt ) > $limit ) { preg_match_all( '#\bhttps?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))#', $post_excerpt, $matches ); @@ -297,22 +302,27 @@ function char_counter_js() { jQuery(document).ready(function(){ var urls_regexp = new RegExp(/https?:\/\/(www\.)?[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?/gi); + function htmlDecode(input){ + input = input.replace(/(<[a-zA-Z\/][^<>]*>|\[([^\]]+)\])/ig,""); + // https://stackoverflow.com/a/34064434 + var doc = new DOMParser().parseFromString(input, "text/html"); + return doc.documentElement.textContent; + } function get_content_length(){ + var title_length = htmlDecode(jQuery("#title").val()).length; var content = ""; // Are we using visual editor? if ((typeof tinyMCE != "undefined") && tinyMCE.activeEditor && !tinyMCE.activeEditor.isHidden()){ - content = tinyMCE.activeEditor.getContent(); + content = htmlDecode(tinyMCE.activeEditor.getContent()); } // Or html editor? else { - content = jQuery("#content").val(); + content = htmlDecode(jQuery("#content").val()); } - content = content.replace(/(<[a-zA-Z\/][^<>]*>|\[([^\]]+)\])/ig,""); var content_length = content.length; var tuit_length = content_length; var urls = content.match(urls_regexp); if (urls) for (i = 0; i < urls.length; i++) tuit_length = tuit_length - urls[i].length + 23; - var title_length = jQuery("#title").val().replace(/(<[a-zA-Z\/][^<>]*>|\[([^\]]+)\])/ig,"").length; var return_text = "El título tiene " + title_length.toString() + " caracteres."; return_text += " Y el contenido " + content_length.toString(); return_text += content_length != tuit_length ? " (" + tuit_length.toString() + " en Twitter)." : ".";