Skip to content

Commit

Permalink
Merge branch 'develop' into stories/fix-asset-loading
Browse files Browse the repository at this point in the history
Conflicts:
	js/src/app.js
	js/yoast-acf-analysis.js
  • Loading branch information
kraftner committed Oct 12, 2017
2 parents 16d6b88 + 623a206 commit a69694c
Show file tree
Hide file tree
Showing 17 changed files with 456 additions and 127 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Yoast SEO: ACF Analysis
# ACF Content Analysis for Yoast SEO
WordPress plugin that adds the content of all ACF fields to the Yoast SEO score analysis.

##Description
Expand Down
1 change: 1 addition & 0 deletions inc/class-ac-yoast-acf-content-analysis.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ protected function get_blacklist_type() {
'tab',
'repeater',
'flexible_content',
'group',
);

foreach ( $default_blacklist as $type ) {
Expand Down
2 changes: 1 addition & 1 deletion inc/class-yoast-acf-analysis-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Yoast_ACF_Analysis_Assets {
* Initialize.
*/
public function init() {
$this->plugin_data = get_plugin_data( dirname( AC_SEO_ACF_ANALYSIS_PLUGIN_FILE ) );
$this->plugin_data = get_plugin_data( AC_SEO_ACF_ANALYSIS_PLUGIN_FILE );

add_filter( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ), 11 );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function remove( $item ) {
}
$this->items = array_values(
array_diff(
$this->items, [ $item ]
$this->items, array( $item )
)
);
sort( $this->items );
Expand Down
6 changes: 3 additions & 3 deletions inc/dependencies/class-yoast-acf-analysis-dependency-afc.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public function register_notifications() {
*/
public function message_plugin_not_activated() {
$message = sprintf(
/* translators: %1$s resolves to Yoast SEO: ACF Analysis, %2$s resolves to Advanced Custom Fields */
__( '%1$s requires %2$s (free or pro) to be installed and activated.', 'yoast-acf-analysis' ),
'Yoast SEO: ACF Analysis',
/* translators: %1$s resolves to ACF Content Analysis for Yoast SEO, %2$s resolves to Advanced Custom Fields */
__( '%1$s requires %2$s (free or pro) to be installed and activated.', 'acf-content-analysis-for-yoast-seo' ),
'ACF Content Analysis for Yoast SEO',
'Advanced Custom Fields'
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ public function register_notifications() {
*/
public function message_plugin_not_activated() {
$message = sprintf(
/* translators: %1$s resolves to Yoast SEO: ACF Analysis, %2$s resolves to Yoast SEO for WordPress, %3$s resolves to the minimal plugin version */
__( '%1$s requires %2$s %3$s (or higher) to be installed and activated.', 'yoast-acf-analysis' ),
'Yoast SEO: ACF Analysis',
/* translators: %1$s resolves to ACF Content Analysis for Yoast SEO, %2$s resolves to Yoast SEO for WordPress, %3$s resolves to the minimal plugin version */
__( '%1$s requires %2$s %3$s (or higher) to be installed and activated.', 'acf-content-analysis-for-yoast-seo' ),
'ACF Content Analysis for Yoast SEO',
'Yoast SEO for WordPress',
self::MINIMAL_REQUIRED_VERSION
);
Expand All @@ -55,9 +55,9 @@ public function message_plugin_not_activated() {
*/
public function message_minimum_version() {
$message = sprintf(
/* translators: %1$s resolves to Yoast SEO: ACF Analysis, %2$s resolves to Yoast SEO for WordPress, %3$s resolves to the minimal plugin version */
__( '%1$s requires %2$s %3$s or higher, please update the plugin.', 'yoast-acf-analysis' ),
'Yoast SEO: ACF Analysis',
/* translators: %1$s resolves to ACF Content Analysis for Yoast SEO, %2$s resolves to Yoast SEO for WordPress, %3$s resolves to the minimal plugin version */
__( '%1$s requires %2$s %3$s or higher, please update the plugin.', 'acf-content-analysis-for-yoast-seo' ),
'ACF Content Analysis for Yoast SEO',
'Yoast SEO for WordPress',
self::MINIMAL_REQUIRED_VERSION
);
Expand Down
43 changes: 26 additions & 17 deletions js/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,49 +13,58 @@ var App = function(){
YoastSEO.app.registerModification('content', collect.append.bind(collect), config.pluginName);

this.bindListeners();

};

App.prototype.bindListeners = function(){

var _self = this;

if(helper.acf_version >= 5){
var _self = this;

jQuery(function(){
_self.replaceVars = replaceVars.createReplaceVars(collect);

replaceVars.updateReplaceVars(collect);

acf.add_action('change remove append sortstop', _self.maybeRefresh);
acf.add_action('change remove append sortstop', replaceVars.updateReplaceVars.bind(_self, collect, _self.replaceVars));
acf.add_action('change remove append sortstop', replaceVars.updateReplaceVars.bind(_self, collect));

});

}else{

var fieldSelectors = config.fieldSelectors.slice(0);
var wysiwygSelector = 'textarea[id^=wysiwyg-acf]';

// Ignore Wysiwyg fields because they trigger a refresh in Yoast SEO itself
fieldSelectors = _.without(fieldSelectors, 'textarea[id^=wysiwyg-acf]');

var _self = this;
fieldSelectorsWithoutWysiwyg = _.without(fieldSelectors, wysiwygSelector);

jQuery(document).on('acf/setup_fields', function(){
this.replaceVars = replaceVars.createReplaceVars(collect);

replaceVars.updateReplaceVars(collect);

var fieldsWithoutWysiwyg = jQuery('#post-body, #edittag').find(fieldSelectorsWithoutWysiwyg.join(','));
var fields = jQuery('#post-body, #edittag').find(fieldSelectors.join(','));
//This would cause faster updates while typing
//fields.on('change input', _self.maybeRefresh.bind(_self) );
fields.on('change', _self.maybeRefresh.bind(_self) );
fields.on('change', replaceVars.updateReplaceVars.bind(_self, collect, _self.replaceVars));

fieldsWithoutWysiwyg.on('change', _self.maybeRefresh.bind(_self) );
// Do not ignore Wysiwyg fields for the purpose of Replace Vars.
jQuery('textarea[id^=wysiwyg-acf]').on('change', replaceVars.updateReplaceVars.bind(_self, collect, _self.replaceVars));
fields.on('change', replaceVars.updateReplaceVars.bind(_self, collect));

if (YoastSEO.wp._tinyMCEHelper) {
jQuery('textarea[id^=wysiwyg-acf]').each( function () {

jQuery(wysiwygSelector).each( function () {
YoastSEO.wp._tinyMCEHelper.addEventHandler(this.id, [ 'input', 'change', 'cut', 'paste' ],
replaceVars.updateReplaceVars.bind(_self, collect, _self.replaceVars));
replaceVars.updateReplaceVars.bind(_self, collect));
});
}

}

//Also refresh on media close as attachment data might have changed
wp.media.frame.on('close', _self.maybeRefresh.bind(_self) );
});
}

}
}
};

App.prototype.maybeRefresh = function(){

Expand Down
9 changes: 5 additions & 4 deletions js/src/collect/collect-v4.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ fields.each(function() {
var $el = jQuery(this).parents('.field').last();

field_data.push({
$el : $el,
key : $el.data('field_key'),
name : $el.data('field_name'),
type : $el.data('field_type')
$el : $el,
key : $el.data('field_key'),
name : $el.data('field_name'),
type : $el.data('field_type'),
post_meta_key : $el.data('field_name')
});

});
Expand Down
62 changes: 60 additions & 2 deletions js/src/collect/collect-v5.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,67 @@
module.exports = function(){
return _.map(acf.get_fields(), function(field){

var outerFieldsName = [
'flexible_content',
'repeater',
'group'
];

var innerFields = [];
var outerFields = [];

var fields = _.map(acf.get_fields(), function(field){

var field_data = jQuery.extend( true, {}, acf.get_data(jQuery(field)) );
field_data.$el = jQuery(field);
field_data.post_meta_key = field_data.name;

// Collect nested and parent
if( outerFieldsName.indexOf(field_data.type) === -1 ) {
innerFields.push(field_data);
}else{
outerFields.push(field_data);
}

return field_data;

});
};

if( outerFields.length === 0){
return fields;
}

// Transform field names for nested fields.
_.each(innerFields, function(inner){

_.each(outerFields, function(outer){

if (jQuery.contains(outer.$el[0], inner.$el[0])) {

// Types that hold multiple children.
if (outer.type === 'flexible_content' || outer.type === 'repeater'){

outer.children = outer.children || [];
outer.children.push(inner);
inner.parent = outer;
inner.post_meta_key = outer.name + '_' + (outer.children.length - 1) + '_' + inner.name;

}

// Types that hold single children.
if (outer.type === 'group') {

outer.children = [inner];
inner.parent = outer;
inner.post_meta_key = outer.name + '_' + inner.name;

}

}

});

});

return fields;

};
6 changes: 3 additions & 3 deletions js/src/collect/collect.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Collect.prototype.getFieldData = function () {

if(config.debug) {

console.log('Used types:')
console.log('Used types:');
console.log(used_types);

}
Expand Down Expand Up @@ -44,10 +44,10 @@ Collect.prototype.append = function(data){
});

if(config.debug){
console.log('Field data:')
console.log('Field data:');
console.table(field_data);

console.log('Data:')
console.log('Data:');
console.log(data);
}

Expand Down
55 changes: 27 additions & 28 deletions js/src/replacevars.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,52 +6,51 @@ var ReplaceVar = YoastReplaceVarPlugin.ReplaceVar;

var supportedTypes = ['email', 'text', 'textarea', 'url', 'wysiwyg'];

var createReplaceVars = function (collect) {
var replaceVars = {};

var replaceVarPluginAvailable = function(){
if (ReplaceVar === undefined) {
if (config.debug) {
console.log('Replacing ACF variables in the Snippet Window requires the latest version of wordpress-seo.');
console.log('Replacing ACF variables in the Snippet Window requires Yoast SEO >= 5.3.');
}
return false;
}
return true;
};

var updateReplaceVars = function (collect) {
if (!replaceVarPluginAvailable()) {
return;
}

fieldData = _.filter(collect.getFieldData(), function (field) { return _.contains(supportedTypes, field.type) });
replaceVars = {}
var fieldData = _.filter(collect.getFieldData(), function (field) { return _.contains(supportedTypes, field.type) });

_.each(fieldData, function(field) {
// Remove HTML tags using jQuery in case of a wysiwyg field.
var content = (field.type === 'wysiwyg') ? jQuery( jQuery.parseHTML( field.content) ).text() : field.content;
var content = (field.type === 'wysiwyg') ? jQuery(jQuery.parseHTML(field.content)).text() : field.content;

replaceVars[field.name] = new ReplaceVar( '%%cf_'+field.name+'%%', content, { source: 'direct' } );
YoastSEO.wp.replaceVarsPlugin.addReplacement( replaceVars[field.name] );
if (config.debug) {
console.log("Created ReplaceVar for: ", field.name, " with: ", content, replaceVars[field.name]);
}
});
if(replaceVars[field.post_meta_key]===undefined){

return replaceVars;
};
replaceVars[field.post_meta_key] = new ReplaceVar( '%%cf_'+field.post_meta_key+'%%', content, { source: 'direct' } );
YoastSEO.wp.replaceVarsPlugin.addReplacement( replaceVars[field.post_meta_key] );

var updateReplaceVars = function (collect, replace_vars) {
if (ReplaceVar === undefined) {
if (config.debug) {
console.log('Replacing ACF variables in the Snippet Window requires the latest version of wordpress-seo.');
}
return;
}
if (config.debug) {
console.log("Created ReplaceVar for: ", field.post_meta_key, " with: ", content, replaceVars[field.post_meta_key]);
}

fieldData = _.filter(collect.getFieldData(), function (field) { return _.contains(supportedTypes, field.type) });
_.each(fieldData, function(field) {
// Remove HTML tags using jQuery in case of a wysiwyg field.
var content = (field.type === 'wysiwyg') ? jQuery(jQuery.parseHTML(field.content)).text() : field.content;
}else{

replaceVars[field.post_meta_key].replacement = content;

if (config.debug) {
console.log("Updated ReplaceVar for: ", field.post_meta_key, " with: ", content, replaceVars[field.post_meta_key]);
}

replaceVars[field.name].replacement = content;
if (config.debug) {
console.log("Updated ReplaceVar for: ", field.name, " with: ", content, replaceVars[field.name]);
}

});
};

module.exports = {
createReplaceVars: createReplaceVars,
updateReplaceVars: updateReplaceVars
};
Loading

0 comments on commit a69694c

Please sign in to comment.