Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated ACF Image Support #134

Merged
merged 9 commits into from
Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"brain/monkey": "^2.2",
"phpunit/phpunit": "^6.0",
"johnpbloch/wordpress-core": " ^5.0",
"woocommerce/woocommerce": "^3.0|^4.0"
"woocommerce/woocommerce": "^3.0|^4.0",
"wpackagist-plugin/advanced-custom-fields": "^5.11"
},
"autoload": {
"psr-4": {
Expand Down Expand Up @@ -71,6 +72,16 @@
"@analyze"
]
},
"repositories": [
{
"type": "composer",
"url": "https://wpackagist.org",
"only": [
"wpackagist-plugin/*",
"wpackagist-theme/*"
]
}
],
"extra": {
"installer-disable": true
}
Expand Down
22 changes: 20 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion patchwork.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"redefinable-internals": [
"filemtime"
"filemtime",
"function_exists"
]
}
93 changes: 93 additions & 0 deletions src/ACF/Image.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

# -*- coding: utf-8 -*-

declare(strict_types=1);

namespace MultisiteGlobalMedia\ACF;

use MultisiteGlobalMedia\Helper;
use MultisiteGlobalMedia\Site;
use MultisiteGlobalMedia\SiteSwitcher;

class Image
{
use Helper;

/**
* @var Site
*/
private $site;

/**
* @var SiteSwitcher
*/
private $siteSwitcher;

/**
* @var \ACF_Data
*/
private $store;

/**
* Integration for ACF, specifically Image fields
*
* @param Site $site
* @param SiteSwitcher $siteSwitcher
*/
private function __construct(Site $site, SiteSwitcher $siteSwitcher)
{
$this->site = $site;
$this->siteSwitcher = $siteSwitcher;
}

public static function bootstrap(Site $site, SiteSwitcher $siteSwitcher)
{
$image = new Image($site, $siteSwitcher);

// ACF can be included within a theme too - check in after_setup_theme action
// https://www.advancedcustomfields.com/resources/including-acf-within-a-plugin-or-theme/
\add_action('after_setup_theme', [$image, 'afterSetupTheme']);

return $image;
}
widoz marked this conversation as resolved.
Show resolved Hide resolved

/**
* Fetch ACF file fields across sites when the global prefix is used.
*
* We hook into 'load_value' which usually runs just before 'format_value'.
* Then get the formatted output of the field in the global media site's context,
* and store it in ACF's cache. So when format_value tries to use this value,
* it will find the formatted one already in the cache.
* This works around acf_format_value requiring a valid att ID as input, but
* returning a string/array as output, so it can't be easily filtered.
*
* @param $value
* @param $post_id
* @param $field
* @return mixed
*/
public function acfLoadValue($value, $post_id, $field)
{
if ($this->idPrefixIncludedInAttachmentId((int)$value, $this->site->idSitePrefix())) {
$formatted = $this->stripSiteIdPrefixFromAttachmentId($this->site->idSitePrefix(), (int)$value);
$this->siteSwitcher->switchToBlog($this->site->id());
$formatted = acf_format_value($formatted, $post_id, $field);
$this->siteSwitcher->restoreBlog();
$this->store->set("$post_id:{$field['name']}:formatted", $formatted);
}

// This filter doesn't modify the loaded value. Return it as-is.
return $value;
}

public function afterSetupTheme()
{
if (!\function_exists('get_field')) {
return;
}

$this->store = acf_get_store('values');
\add_filter('acf/load_value/type=image', array($this, 'acfLoadValue'), 10, 3);
}
}
2 changes: 2 additions & 0 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public function onLoad()
if (\function_exists('wc')) {
$this->wcBootstrap($site, $singleSwitcher);
}

ACF\Image::bootstrap($site, $singleSwitcher);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions tests/php/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
declare(strict_types=1);

error_reporting(E_ALL ^ E_DEPRECATED);
felly marked this conversation as resolved.
Show resolved Hide resolved

putenv('TESTS_PATH='.__DIR__);
putenv('LIBRARY_PATH='.dirname(__DIR__));

Expand Down
109 changes: 109 additions & 0 deletions tests/php/unit/AdvancedCustomFieldTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

# -*- coding: utf-8 -*-

declare(strict_types=1);

namespace MultisiteGlobalMedia\Tests\Unit;

use Brain\Monkey\Functions;
use MultisiteGlobalMedia\ACF\Image;
use MultisiteGlobalMedia\Site;
use MultisiteGlobalMedia\SiteSwitcher;
use MultisiteGlobalMedia\Tests\TestCase;

define('ABSPATH', 1); // ACF quits early without it
include_once dirname(__DIR__, 3) . '/vendor/wpackagist-plugin/advanced-custom-fields/includes/class-acf-data.php';
widoz marked this conversation as resolved.
Show resolved Hide resolved

class AdvancedCustomFieldTest extends TestCase
{
public function testBootstrap()
{
$siteSwitcher = $this->createMock(SiteSwitcher::class);
$site = $this->createMock(Site::class);

$testee = Image::bootstrap($site, $siteSwitcher);
self::assertEquals(10, has_action('after_setup_theme', [$testee, 'afterSetupTheme']));
}

public function testAfterSetupTheme()
{
$siteSwitcher = $this->createMock(SiteSwitcher::class);
$site = $this->createMock(Site::class);

Functions\expect('function_exists')
->once()
->with('get_field')
->andReturn(true);

Functions\expect('acf_get_store')
->once();

$testee = Image::bootstrap($site, $siteSwitcher);
$testee->afterSetupTheme();
self::assertEquals(10, has_filter('acf/load_value/type=image', [$testee, 'acfLoadValue']));
}

public function testAfterSetupThemeWithoutACF()
{
$siteSwitcher = $this->createMock(SiteSwitcher::class);
$site = $this->createMock(Site::class);

$testee = Image::bootstrap($site, $siteSwitcher);
$testee->afterSetupTheme();
self::assertFalse(has_filter('acf/load_value/type=image', [$testee, 'acfLoadValue']));
}

public function testAcfLoadValue()
{
$siteSwitcher = $this->createMock(SiteSwitcher::class);
$site = $this->createMock(Site::class);
$store = $this->createMock(\ACF_Data::class);

Functions\expect('function_exists')
->once()
->with('get_field')
->andReturn(true);

Functions\expect('acf_get_store')
->once()
->andReturn($store);

$testee = Image::bootstrap($site, $siteSwitcher);
$testee->afterSetupTheme();

$field = ['name' => 'image'];
$attachmentId = 1 . Site::SITE_ID_PREFIX_RIGHT_PAD . 1;

$site
->method('idSitePrefix')
->willReturn(1 . Site::SITE_ID_PREFIX_RIGHT_PAD);

$site
->expects($this->once())
->method('id')
->willReturn(1);

$siteSwitcher
->expects($this->once())
->method('switchToBlog')
->with(1);

$siteSwitcher
->expects($this->once())
->method('restoreBlog');

$store
->expects($this->once())
->method('set')
->with('123:image:formatted', 'formattedImage');

Functions\expect('acf_format_value')
->once()
->with(1, '123', $field)
->andReturn('formattedImage');

$returnedValue = $testee->acfLoadValue($attachmentId, '123', $field);
self::assertSame($attachmentId, $returnedValue);
}
}