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 4 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
61 changes: 61 additions & 0 deletions src/ACF/Image.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php # -*- coding: utf-8 -*-

namespace MultisiteGlobalMedia\ACF;

use MultisiteGlobalMedia\Helper;
use MultisiteGlobalMedia\SingleSwitcher;
use MultisiteGlobalMedia\Site;

class Image
{

use Helper;

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

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

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

/**
* Image constructor
*
* @param Site $site
* @param SingleSwitcher $siteSwitcher
*/
public function __construct(Site $site, SingleSwitcher $siteSwitcher)
{
$this->site = $site;
$this->siteSwitcher = $siteSwitcher;
$this->store = acf_get_store('values');
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.
felly marked this conversation as resolved.
Show resolved Hide resolved
public function acfLoadValue($value, $post_id, $field)
{
if ($this->idPrefixIncludedInAttachmentId((int)$value, $this->site->idSitePrefix())) {
$formatted = $this->stripSiteIdPrefixFromAttachmentId($this->site->idSitePrefix(), $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;
}
}
21 changes: 21 additions & 0 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,12 @@ public function onLoad()
add_filter('register_post_type_args', [$rest, 'registerPostTypeArgs'], 10, 2);
add_filter('rest_request_after_callbacks', [$rest, 'restRequestAfterCallbacks'], 10, 3);


felly marked this conversation as resolved.
Show resolved Hide resolved
if (\function_exists('wc')) {
$this->wcBootstrap($site, $singleSwitcher);
}

$this->acfBootstrap($site, $singleSwitcher);
}

/**
Expand All @@ -78,4 +81,22 @@ public function wcBootstrap(Site $site, SingleSwitcher $siteSwitcher)
add_action('woocommerce_new_product', [$wooCommerceGallery, 'saveGalleryIds']);
add_action('woocommerce_update_product', [$wooCommerceGallery, 'saveGalleryIds']);
}

/**
* Integration for ACF, specifically Image fields
*
* @param Site $site
* @param SingleSwitcher $siteSwitcher
*/
public function acfBootstrap(Site $site, SingleSwitcher $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', function () use ($site, $siteSwitcher) {
if (\function_exists('get_field')) {
$acfImage = new ACF\Image($site, $siteSwitcher);
add_filter('acf/load_value/type=image', array($acfImage, 'acfLoadValue'), 10, 3);
}
});
felly marked this conversation as resolved.
Show resolved Hide resolved
}
}