Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions wp-content/themes/humanity-theme/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,4 +292,8 @@
}
#endregion multilingualpress

/**
* Theme Multisite Global Media includes
*/
require_once realpath( __DIR__ . '/includes/mgm/class-mgm-filters.php' );
// phpcs:enable Squiz.Commenting.InlineComment.WrongStyle,PEAR.Commenting.InlineComment.WrongStyle
174 changes: 174 additions & 0 deletions wp-content/themes/humanity-theme/includes/mgm/class-mgm-filters.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
<?php

declare( strict_types = 1 );

namespace Amnesty;

use MultisiteGlobalMedia\Site;

new MGM_Filters();

/**
* Filters for Multisite Global Media block transformations
*/
class MGM_Filters {

/**
* MGM site helper object
*
* @var Site
*/
protected Site $site;

/**
* Bind hooks
*/
public function __construct() {
if ( class_exists( Site::class, false ) ) {
$this->site = new Site();
}

add_filter( 'global_media.process_post.block_amnesty_core_block_section', [ $this, 'section' ], 10, 3 );
add_filter( 'global_media.process_post.block_amnesty_core_hero', [ $this, 'hero' ], 10, 3 );
add_filter( 'global_media.process_post.block_amnesty_core_block_slider', [ $this, 'slider' ], 10, 3 );
add_filter( 'global_media.process_post.block_amnesty_core_action_block', [ $this, 'action' ], 10, 3 );
add_filter( 'global_media.process_post.block_amnesty_core_background_media_column', [ $this, 'background_media_column' ], 10, 3 );
add_filter( 'global_media.process_post.block_amnesty_core_custom_card', [ $this, 'custom_card' ], 10, 3 );
}

/**
* For when the origin is the global media site, add the prefix to the image ID
*
* @param array<string,mixed> $block the parsed block data
*
* @return array<string,mixed>
*/
public function section( array $block ): array {
$image_id = absint( $block['attrs']['backgroundImageId'] ?? '0' );

if ( $image_id ) {
$block['attrs']['backgroundImageId'] = $this->process( $image_id );
}

return $block;
}

/**
* For when the origin is the global media site, add the prefix to the image ID
*
* @param array<string,mixed> $block the parsed block data
*
* @return array<string,mixed>
*/
public function hero( array $block ): array {
$image_id = absint( $block['attrs']['imageID'] ?? '0' );
$video_id = absint( $block['attrs']['featuredVideoId'] ?? '0' );

if ( $image_id ) {
$block['attrs']['imageID'] = $this->process( $image_id );
}

if ( $video_id ) {
$block['attrs']['featuredVideoId'] = $this->process( $video_id );
}

return $block;
}

/**
* For when the origin is the global media site, add the prefix to the image ID
*
* @param array<string,mixed> $block the parsed block data
*
* @return array<string,mixed>
*/
public function slider( array $block ): array {
foreach ( (array) $block['attrs']['slides'] as $index => $slide ) {
$image_id = absint( $slide['imageId'] ?? '0' );

if ( $image_id ) {
$block['attrs']['slides'][ $index ]['imageId'] = $this->process( $image_id );
}
}

return $block;
}

/**
* For when the origin is the global media site, add the prefix to the image ID
*
* @param array<string,mixed> $block the parsed block data
*
* @return array<string,mixed>
*/
public function action( array $block ): array {
$image_id = absint( $block['attrs']['imageID'] ?? '0' );

if ( $image_id ) {
$block['attrs']['imageID'] = $this->process( $image_id );
}

return $block;
}

/**
* For when the origin is the global media site, add the prefix to the image ID
*
* @param array<string,mixed> $block the parsed block data
*
* @return array<string,mixed>
*/
public function background_media_column( array $block ): array {
$image_id = absint( $block['attrs']['image'] ?? '0' );

if ( $image_id ) {
$block['attrs']['image'] = $this->process( $image_id );
}

return $block;
}

/**
* For when the origin is the global media site, add the prefix to the image ID
*
* @param array<string,mixed> $block the parsed block data
*
* @return array<string,mixed>
*/
public function custom_card( array $block ): array {
$image_id = absint( $block['attrs']['imageID'] ?? '0' );

if ( $image_id ) {
$block['attrs']['imageID'] = $this->process( $image_id );
}

return $block;
}

/**
* Check if the given site Id prefix exists into the give attachment id
*
* @param int|string $id the attachment ID
*
* @return bool
*/
protected function media_has_prefix( int|string $id ): bool {
return false !== strpos( (string) $id, $this->site->idSitePrefix() );
}

/**
* Process an attribute value
*
* @param int|string $id the attribute value
*
* @return int
*/
protected function process( int|string $id ): int {
if ( $this->media_has_prefix( $id ) ) {
return absint( $id );
}

return absint( $this->site->idSitePrefix() . $id );
}

}