Skip to content

Commit 8f27855

Browse files
authored
Merge pull request #80 from Yoast/stories/handle-acf-downgrade
Get ACF version in reliable manner.
2 parents 6f5373b + 8cfbbf2 commit 8f27855

5 files changed

Lines changed: 39 additions & 27 deletions

File tree

.travis.yml

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,11 @@ branches:
99
- /^release\/\d+\.\d+(\.\d+)?(-\S*)?$/
1010
- /^hotfix\/\d+\.\d+(\.\d+)?(-\S*)?$/
1111

12-
addons:
13-
apt:
14-
sources:
15-
- ubuntu-toolchain-r-test
16-
packages:
17-
- g++-4.8
18-
1912
jobs:
2013
fast_finish: true
2114
include:
2215
- php: 7.1
23-
env: WP_VERSION=4.8 WP_MULTISITE=1 PHPLINT=1 PHPCS=1 CHECKJS=1 PHPUNIT=1 TRAVIS_NODE_VERSION=node CXX=g++-4.8
16+
env: WP_VERSION=4.8 WP_MULTISITE=1 PHPLINT=1 PHPCS=1 CHECKJS=1 PHPUNIT=1 TRAVIS_NODE_VERSION=node
2417
- php: 5.2
2518
# As 'trusty' is not supporting PHP 5.2/5.3 anymore, we need to force using 'precise'.
2619
dist: precise

inc/class-ac-yoast-seo-acf-content-analysis.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,14 @@ public function boot() {
8080
* Boots the plugin for dev environment.
8181
*/
8282
public function boot_dev() {
83-
$version = ( -1 === version_compare( get_option( 'acf_version' ), 5 ) ) ? '4' : '5';
83+
$registry = Yoast_ACF_Analysis_Facade::get_registry();
84+
$configuration = $registry->get( 'config' );
85+
86+
$version = 4;
87+
if ( version_compare( $configuration->get_acf_version(), 5, '>=' ) ) {
88+
$version = 5;
89+
}
90+
8491
require_once AC_SEO_ACF_ANALYSIS_PLUGIN_PATH . '/tests/js/system/data/acf' . $version . '.php';
8592
}
8693

inc/configuration/class-yoast-acf-analysis-configuration.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,13 @@ public function __construct(
4848
* @return string The ACF version.
4949
*/
5050
public function get_acf_version() {
51-
return get_option( 'acf_version' );
51+
// ACF 5 introduces `acf_get_setting`, so this might not always be available.
52+
if ( function_exists( 'acf_get_setting' ) ) {
53+
return acf_get_setting( 'version' );
54+
}
55+
56+
// Fall back on filter use.
57+
return apply_filters( 'acf/get_info', 'version' );
5258
}
5359

5460
/**

tests/php/unit/Configuration/ConfigurationTest.php

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
namespace Yoast\AcfAnalysis\Tests\Configuration;
44

55
use Brain\Monkey;
6-
use Brain\Monkey\Functions;
76
use Brain\Monkey\Filters;
7+
use Brain\Monkey\Functions;
88

99
class ConfigurationTest extends \PHPUnit_Framework_TestCase {
1010

@@ -15,13 +15,6 @@ protected function setUp() {
1515

1616
public function testEmpty() {
1717

18-
$version = '4.0.0';
19-
20-
Functions\expect( 'get_option' )
21-
->once()
22-
->with( 'acf_version' )
23-
->andReturn( $version );
24-
2518
$configuration = new \Yoast_ACF_Analysis_Configuration(
2619
new \Yoast_ACF_Analysis_String_Store(),
2720
new \Yoast_ACF_Analysis_String_Store(),
@@ -31,7 +24,7 @@ public function testEmpty() {
3124
$this->assertSame(
3225
[
3326
'pluginName' => \Yoast_ACF_Analysis_Facade::get_plugin_name(),
34-
'acfVersion' => $version,
27+
'acfVersion' => 'version',
3528
'scraper' => [],
3629
'refreshRate' => 1000,
3730
'blacklistType' => [],
@@ -42,6 +35,21 @@ public function testEmpty() {
4235
$configuration->to_array()
4336
);
4437

38+
$this->assertEquals( Filters\applied( 'acf/get_info' ), 1 );
39+
}
40+
41+
public function testACF5VersionFunction() {
42+
$acf_version = '5.0.0';
43+
Functions\when( 'acf_get_setting' )->justReturn( $acf_version );
44+
45+
$configuration = new \Yoast_ACF_Analysis_Configuration(
46+
new \Yoast_ACF_Analysis_String_Store(),
47+
new \Yoast_ACF_Analysis_String_Store(),
48+
new \Yoast_ACF_Analysis_String_Store()
49+
);
50+
$config = $configuration->to_array();
51+
52+
$this->assertEquals( $acf_version, $config['acfVersion'] );
4553
}
4654

4755
public function testBlacklistTypeFilter() {
@@ -180,8 +188,8 @@ public function testBlacklistNameFilterInvalid() {
180188
$this->assertSame( $store, $configuration->get_blacklist_name() );
181189
}
182190

183-
public function testScraperConfigFilter(){
184-
$config = array();
191+
public function testScraperConfigFilter() {
192+
$config = array();
185193
$blacklist = new \Yoast_ACF_Analysis_String_Store();
186194

187195
$configuration = new \Yoast_ACF_Analysis_Configuration(
@@ -198,7 +206,7 @@ public function testScraperConfigFilter(){
198206
$this->assertSame( $config, $configuration->get_scraper_config() );
199207
}
200208

201-
public function testInvalidScraperConfigFilter(){
209+
public function testInvalidScraperConfigFilter() {
202210
$blacklist = new \Yoast_ACF_Analysis_String_Store();
203211

204212
$configuration = new \Yoast_ACF_Analysis_Configuration(
@@ -245,8 +253,8 @@ public function testRefreshRateMinimumValueFilter() {
245253
$this->assertSame( 200, $configuration->get_refresh_rate() );
246254
}
247255

248-
public function testFieldSelectorsFilter(){
249-
$custom_store = new \Yoast_ACF_Analysis_String_Store();
256+
public function testFieldSelectorsFilter() {
257+
$custom_store = new \Yoast_ACF_Analysis_String_Store();
250258
$field_selector = new \Yoast_ACF_Analysis_String_Store();
251259

252260
$configuration = new \Yoast_ACF_Analysis_Configuration(

tests/php/unit/MainTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Yoast\AcfAnalysis\Tests;
44

55
use Brain\Monkey;
6-
use Brain\Monkey\Functions;
76

87
class MainTest extends \PHPUnit_Framework_TestCase {
98

@@ -13,7 +12,6 @@ protected function setUp() {
1312
}
1413

1514
public function testInvalidConfig() {
16-
1715
$registry = \Yoast_ACF_Analysis_Facade::get_registry();
1816

1917
$registry->add( 'config', 'Invalid Config' );
@@ -22,7 +20,7 @@ public function testInvalidConfig() {
2220
$testee->boot();
2321

2422
$this->assertNotSame( 'Invalid Config', $registry->get( 'config' ) );
25-
$this->assertInstanceOf('\Yoast_ACF_Analysis_Configuration', $registry->get( 'config' ) );
23+
$this->assertInstanceOf( \Yoast_ACF_Analysis_Configuration::class, $registry->get( 'config' ) );
2624

2725
}
2826

0 commit comments

Comments
 (0)