Skip to content

Commit

Permalink
Merge pull request #57 from bueltge/improvements/woocommerce/gallery
Browse files Browse the repository at this point in the history
After #53 some filters in the WooCommerce Gallery class are not needed
  • Loading branch information
bueltge authored Nov 5, 2018
2 parents 2ed1f0c + 75ede63 commit 776ed97
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 121 deletions.
7 changes: 0 additions & 7 deletions multisite-global-media.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,6 @@ function wcBootstrap(Site $site, SingleSwitcher $siteSwitcher)

add_action('woocommerce_new_product', [$wooCommerceGallery, 'saveGalleryIds']);
add_action('woocommerce_update_product', [$wooCommerceGallery, 'saveGalleryIds']);
add_action('add_meta_boxes', [$wooCommerceGallery, 'overrideMetaboxCallback'], PHP_INT_MAX);
add_action(
'woocommerce_single_product_image_thumbnail_html',
[$wooCommerceGallery, 'singleProductImageThumbnailHtml'],
PHP_INT_MAX,
2
);
}

add_action('plugins_loaded', __NAMESPACE__ . '\\bootstrap');
114 changes: 0 additions & 114 deletions src/WooCommerce/Gallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,119 +73,5 @@ public function saveGalleryIds(int $productId)
);

update_post_meta($product->get_id(), self::META_KEY_PRODUCT_GALLERY, $attachmentIds);
$this->storeSiteIdIntoObjectMeta($product->get_id(), $this->site->id());
}

/**
* Override Metabox Callback for product gallery metabox
*/
public function overrideMetaboxCallback()
{
global $wp_meta_boxes;

$metaboxCallback =& $wp_meta_boxes['product']['side']['low']['woocommerce-product-images']['callback'] ?? null;

if (!$metaboxCallback) {
return;
}

$metaboxCallback = function (\WP_Post $post) {
$this->activateRetrieveImageFilter();
\WC_Meta_Box_Product_Images::output($post);
$this->deactivateRetrieveImageFilter();
};
}

/**
* Retrieve the images from the global site
*
* @param mixed $image
* @param int $attachmentId
* @param $size
* @param bool $icon
* @return array
*/
public function retrieveTheImages(
$image,
int $attachmentId,
$size,
bool $icon
): array {

// We expect a boolean false because the image (siteID.00000.ID) doesn't exists.
if (\is_bool($image)) {
$image = [];
}

$this->deactivateRetrieveImageFilter();

$siteId = $this->siteIdByMetaObject($attachmentId, $this->site->id());
$idPrefix = $siteId . Site::SITE_ID_PREFIX_RIGHT_PAD;

if ($this->idPrefixIncludedInAttachmentId($attachmentId, $idPrefix)) {
$attachmentId = $this->stripSiteIdPrefixFromAttachmentId($idPrefix, $attachmentId);
// TODO Could be improved if we allow multiple switches.
$this->siteSwitcher->switchToBlog($siteId);
$image = array_filter((array)wp_get_attachment_image_src($attachmentId, $size, $icon));
$this->siteSwitcher->restoreBlog();
}

$this->activateRetrieveImageFilter();

return $image;
}

/**
* Show the gallery image on frontend
*
* @param string $html
* @param int $attachmentId
* @return string
*/
public function singleProductImageThumbnailHtml(string $html, int $attachmentId): string
{
/** @var \WC_Product $product */
global $product;

$productId = $product->get_id();

$siteId = $this->siteIdByMetaObject($productId, $this->site->id());
$idPrefix = $siteId . Site::SITE_ID_PREFIX_RIGHT_PAD;

if (!$this->idPrefixIncludedInAttachmentId($attachmentId, $idPrefix)) {
return $html;
}

$attachmentId = $this->stripSiteIdPrefixFromAttachmentId($idPrefix, $attachmentId);
$this->siteSwitcher->switchToBlog($siteId);
$html = wc_get_gallery_image_html($attachmentId);
$this->siteSwitcher->restoreBlog();

return $html;
}

/**
* Activate the retrieve image filter
*/
private function activateRetrieveImageFilter()
{
add_filter(
self::FILTER_ATTACHMENT_IMAGE_SRC,
[$this, 'retrieveTheImages'],
PHP_INT_MAX,
4
);
}

/**
* Deactivate the retrieve image filter
*/
private function deactivateRetrieveImageFilter()
{
remove_filter(
self::FILTER_ATTACHMENT_IMAGE_SRC,
[$this, 'retrieveTheImages'],
PHP_INT_MAX
);
}
}

0 comments on commit 776ed97

Please sign in to comment.