Skip to content

Commit

Permalink
Merge pull request #36 from richardsweeney/features/implement-ysacf_e…
Browse files Browse the repository at this point in the history
…xclude_fields-filter

Implement ysacf_exclude_fields filter for legacy support
  • Loading branch information
atimmer authored Aug 21, 2017
2 parents 13650d3 + 47d787f commit b45831c
Show file tree
Hide file tree
Showing 7 changed files with 264 additions and 37 deletions.
12 changes: 10 additions & 2 deletions inc/class-ac-yoast-acf-content-analysis.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public function boot() {
}

$configuration = new Yoast_ACF_Analysis_Configuration(
$this->get_blacklist(),
$this->get_blacklist_type(),
$this->get_blacklist_name(),
$this->get_field_selectors()
);

Expand Down Expand Up @@ -141,7 +142,7 @@ protected function get_field_selectors() {
*
* @return Yoast_ACF_Analysis_String_Store
*/
protected function get_blacklist() {
protected function get_blacklist_type() {

$blacklist = new Yoast_ACF_Analysis_String_Store();

Expand Down Expand Up @@ -183,4 +184,11 @@ protected function get_blacklist() {

return $blacklist;
}

/**
* @return Yoast_ACF_Analysis_String_Store
*/
protected function get_blacklist_name() {
return new Yoast_ACF_Analysis_String_Store();
}
}
70 changes: 56 additions & 14 deletions inc/configuration/class-yoast-acf-analysis-configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ class Yoast_ACF_Analysis_Configuration {
/**
* @var Yoast_ACF_Analysis_String_Store
*/
protected $blacklist;
protected $blacklist_type;

/**
* @var Yoast_ACF_Analysis_String_Store
*/
protected $blacklist_name;

/**
* @var Yoast_ACF_Analysis_String_Store
Expand All @@ -23,11 +28,17 @@ class Yoast_ACF_Analysis_Configuration {
protected $scraper_config = array();

/**
* @param Yoast_ACF_Analysis_String_Store $blacklist Blacklist Configuration Object.
* @param Yoast_ACF_Analysis_String_Store $blacklist_type Blacklist Type Configuration Object.
* @param Yoast_ACF_Analysis_String_Store $blacklist_name Blacklist Name Configuration Object.
* @param Yoast_ACF_Analysis_String_Store $field_selectors Field Selectors Configuration Object.
*/
public function __construct( Yoast_ACF_Analysis_String_Store $blacklist, Yoast_ACF_Analysis_String_Store $field_selectors ) {
$this->blacklist = $blacklist;
public function __construct(
Yoast_ACF_Analysis_String_Store $blacklist_type,
Yoast_ACF_Analysis_String_Store $blacklist_name,
Yoast_ACF_Analysis_String_Store $field_selectors
) {
$this->blacklist_type = $blacklist_type;
$this->blacklist_name = $blacklist_name;
$this->field_selectors = $field_selectors;
}

Expand All @@ -41,23 +52,53 @@ public function get_acf_version() {
}

/**
* Retrieves the blacklist store.
* Retrieves the blacklist type store.
*
* @return Yoast_ACF_Analysis_String_Store The blacklist store.
* @return Yoast_ACF_Analysis_String_Store The blacklist type store.
*/
public function get_blacklist() {
public function get_blacklist_type() {

$blacklist = apply_filters(
Yoast_ACF_Analysis_Facade::get_filter_name( 'blacklist' ),
$this->blacklist
$blacklist_type = apply_filters(
Yoast_ACF_Analysis_Facade::get_filter_name( 'blacklist_type' ),
$this->blacklist_type
);

if ( $blacklist instanceof Yoast_ACF_Analysis_String_Store ) {
return $blacklist;
if ( $blacklist_type instanceof Yoast_ACF_Analysis_String_Store ) {
return $blacklist_type;
}

return $this->blacklist;
return $this->blacklist_type;

}

/**
* Retrieves the blacklist name store.
*
* @return Yoast_ACF_Analysis_String_Store The blacklist name store.
*/
public function get_blacklist_name() {
// Implement legacy filter
$legacy_names = apply_filters(
'ysacf_exclude_fields',
array()
);

if ( is_array( $legacy_names ) && ! empty( $legacy_names ) ) {
foreach ( $legacy_names as $legacy_name ) {
$this->blacklist_name->add( $legacy_name );
}
}

$blacklist_name = apply_filters(
Yoast_ACF_Analysis_Facade::get_filter_name( 'blacklist_name' ),
$this->blacklist_name
);

if ( $blacklist_name instanceof Yoast_ACF_Analysis_String_Store ) {
return $blacklist_name;
}

return $this->blacklist_name;
}

/**
Expand Down Expand Up @@ -125,7 +166,8 @@ public function to_array() {
'acfVersion' => $this->get_acf_version(),
'scraper' => $this->get_scraper_config(),
'refreshRate' => $this->get_refresh_rate(),
'blacklist' => $this->get_blacklist()->to_array(),
'blacklistType' => $this->get_blacklist_type()->to_array(),
'blacklistName' => $this->get_blacklist_name()->to_array(),
'fieldSelectors' => $this->get_field_selectors()->to_array(),
'debug' => $this->is_debug(),
);
Expand Down
16 changes: 12 additions & 4 deletions js/src/collect/collect.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ var Collect = function(){
};

Collect.prototype.getFieldData = function () {
var field_data = this.filterBroken(this.filterBlacklist(this.getData()));
var field_data = this.filterBroken(this.filterBlacklistName(this.filterBlacklistType(this.getData())));

var used_types = _.uniq(_.pluck(field_data, 'type'));

if(config.debug) {

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

}

_.each(used_types, function(type){
Expand Down Expand Up @@ -63,9 +65,15 @@ Collect.prototype.getData = function(){

};

Collect.prototype.filterBlacklist = function(field_data){
Collect.prototype.filterBlacklistType = function(field_data){
return _.filter(field_data, function(field){
return !_.contains(config.blacklistType, field.type);
});
};

Collect.prototype.filterBlacklistName = function(field_data){
return _.filter(field_data, function(field){
return !_.contains(config.blacklist, field.type);
return !_.contains(config.blacklistName, field.name);
});
};

Expand All @@ -75,4 +83,4 @@ Collect.prototype.filterBroken = function(field_data){
});
};

module.exports = new Collect();
module.exports = new Collect();
16 changes: 12 additions & 4 deletions js/yoast-acf-analysis.js

Large diffs are not rendered by default.

53 changes: 53 additions & 0 deletions tests/js/system/tests/general/filters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
var assert = require('assert');
var logContains = require('../../helpers/logContains');
var dummyContent = require('../../helpers/dummyContent');
var simpleField = require('../../helpers/simpleField');

module.exports = {
tags: ['acf4', 'acf5', 'filters'],

before: function (browser) {
var page = browser.page.WordPressHelper();
page.newPost();
},

beforeEach: function (browser) {
},

'Text Field (blacklisted by name)' : function (browser) {

browser.execute(function() {
return jQuery('.acf-field-text').data('key') || jQuery('.field_type-text').data('field_key');
},
[],
function(result){

browser.execute(
function( key ) {
//As defined in /tests/js/system/data/acf5.php
YoastACFAnalysisConfig.blacklistName = [ 'yoast_acf_analysis_text' ];
},
[result.value]
);

} );

var hash = dummyContent.hash();

browser
.clearValue( '.field_type-text input, .acf-field-text input' )
.setValue( '.field_type-text input, .acf-field-text input', [ hash , browser.Keys.TAB ] );

browser.pause( 3000 );

logContains( browser, 'h2>' + hash, function( hasText ){
return browser.assert.ok( !hasText );
} );

browser.clearValue( '.field_type-text input, .acf-field-text input' );
},

after : function(browser) {
browser.end();
}
};
Loading

0 comments on commit b45831c

Please sign in to comment.