Skip to content

fix ARI default image #317

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
72 changes: 71 additions & 1 deletion inc/Services/Theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace BEA\Theme\Framework\Services;

use BEA\Theme\Framework\Framework;
use BEA\Theme\Framework\Service;
use BEA\Theme\Framework\Service_Container;


class Theme implements Service {

/**
Expand All @@ -18,6 +18,8 @@ public function register( Service_Container $container ): void {}
*/
public function boot( Service_Container $container ): void {
$this->after_setup_theme();
add_filter( 'ari_responsive_image_default_img_path', [ $this, 'set_ari_responsive_image_default_img_path' ] );
add_filter( 'ari_responsive_image_default_img_name', [ $this, 'set_ari_responsive_image_default_img_name' ] );
}

/**
Expand Down Expand Up @@ -74,4 +76,72 @@ private function i18n(): void {
// Load theme texdomain
load_theme_textdomain( 'framework-textdomain', \get_theme_file_path( '/languages' ) );
}

/**
* Set default path for ARI for minified files
*
* @param string $attr
*
* @return string
*
*/
public function set_ari_responsive_image_default_img_path( string $attr ): string {
/**
* @psalm-suppress PossiblyInvalidMethodCall
* @psalm-suppress UndefinedInterfaceMethod
*/
if ( ! Framework::get_container()->get_service( 'assets' )->is_minified() ) {
return $attr;
}

return '/dist/';
}

/**
* Set ari default image name for minified files
*
* @param string $default_img
*
* @return string
*
*/
public function set_ari_responsive_image_default_img_name( string $default_img ): string {
/**
* @psalm-suppress PossiblyInvalidMethodCall
* @psalm-suppress UndefinedInterfaceMethod
*/
if ( ! Framework::get_container()->get_service( 'assets' )->is_minified() ) {
return $default_img;
}

return $this->get_min_image( $default_img );
}

/**
* Get minified default_image
*
* @param string $original_image
*
* @return string
*
*/
protected function get_min_image( string $original_image ): string {

if ( empty( $original_image ) ) {
return $original_image;
}

if ( ! file_exists( \get_theme_file_path( '/dist/assets.json' ) ) ) {
return $original_image;
}

$json = file_get_contents( \get_theme_file_path( '/dist/assets.json' ) ); //phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
$assets = json_decode( $json, true );

if ( empty( $assets ) || JSON_ERROR_NONE !== json_last_error() ) {
return $original_image;
}

return $assets[ 'assets/' . $original_image ] ?: $original_image;
}
}