Skip to content

Commit

Permalink
Added some basic widget parsing to Yoast compat
Browse files Browse the repository at this point in the history
Rather than just remove widget content, this will try present something to Yoast’s analysis.
  • Loading branch information
gregpriday committed Jun 7, 2018
1 parent d526fab commit 923c136
Showing 1 changed file with 74 additions and 10 deletions.
84 changes: 74 additions & 10 deletions js/yoast-compat.js
@@ -1,22 +1,86 @@
/* global jQuery, YoastSEO */

jQuery(function($){

var decodeEntities = (function() {
// this prevents any overhead from creating the object each time
var element = document.createElement('div');

function decodeHTMLEntities (str) {
if(str && typeof str === 'string') {
// strip script/html tags
str = str.replace(/<script[^>]*>([\S\s]*?)<\/script>/gmi, '');
str = str.replace(/<\/?\w(?:[^"'>]|"[^"]*"|'[^']*')*>/gmi, '');
element.innerHTML = str;
str = element.textContent;
element.textContent = '';
}

return str;
}

return decodeHTMLEntities;
})();

var SiteOriginYoastCompat = function() {
YoastSEO.app.registerPlugin( 'siteOriginYoastCompat', { status: 'ready' } );
YoastSEO.app.registerModification( 'content', this.contentModification, 'siteOriginYoastCompat', 5 );
}
};

SiteOriginYoastCompat.prototype.contentModification = function(data) {
// Remove siteorigin_widget shortcodes because they conflict with Yoast scoring
data = data.replace(/\[siteorigin_widget.*?].*?\[\/siteorigin_widget\]/g);

// Clean out any Page Builder wrapper tags in case there are more conflicts there.
var re = new RegExp( panelsOptions.siteoriginWidgetRegex , "i" );
var $data = $(data);
if( $data.find('.panel-grid') ) {
// Remove any unimportant tags from Page Builder content
$data.find('.panel-grid, .panel-grid-cell, .so-panel.widget').contents().unwrap();
}
$data.find('input[type="hidden"]').remove();


$data.find('.so-panel.widget').each(function(i, el){
var $widget = $(el),
match = re.exec( $widget.html() );

try{
if( ! _.isNull( match ) && $widget.html().replace( re, '' ).trim() === '' ) {
var classMatch = /class="(.*?)"/.exec( match[3] ),
dataInput = jQuery( match[5] ),
data = JSON.parse( decodeEntities( dataInput.val( ) ) ),
widgetInstance = data.instance,
newHTML = '';

if( ! _.isNull(widgetInstance.title) ) {
newHTML += '<h3>' + widgetInstance.title + '</h3>';
}

if( ! _.isNull( classMatch ) ) {
var widgetClass = classMatch[1];
switch( widgetClass ) {
case 'SiteOrigin_Widget_Image_Widget':
// We want a direct assignment for the SO Image Widget to get rid of the title
newHTML = $('img').attr({
'src': '#' + widgetInstance.image,
'srcset': '',
'alt': widgetInstance.alt,
'title': widgetInstance.title,
}).prop('outerHTML');
break;

case 'WP_Widget_Media_Image':
newHTML = $('img').attr({
'src': '#' + widgetInstance.attachment_id,
'srcset': '',
'alt': widgetInstance.alt,
'title': widgetInstance.image_title,
}).prop('outerHTML');
break;
}
}

$widget.html(newHTML);
}
}
catch(e) {
// If there was an error, just clear the widget content.
$widget.html('');
}

});
return $data.html();
};

Expand Down

0 comments on commit 923c136

Please sign in to comment.