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

fix ARI default image #317

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
54 changes: 42 additions & 12 deletions inc/Services/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ class Assets implements Service {
*/
private $assets_tools;

/**
* @var array $assets_json_index
*/
protected $assets_json_index;

/**
* @param Service_Container $container
*/
Expand Down Expand Up @@ -126,32 +131,27 @@ public function get_min_file( string $type ): string {
return '';
}

if ( ! file_exists( \get_theme_file_path( '/dist/assets.json' ) ) ) {
return '';
}
$assets = $this->get_assets_json_index_file();
lphoumpakka marked this conversation as resolved.
Show resolved Hide resolved

$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() ) {
if ( empty( $assets ) ) {
return '';
}

switch ( $type ) {
case 'css':
$file = $assets['app.css'];
$file = $assets['app.css'] ?? '';
break;
case 'editor.css':
$file = $assets['editor.css'];
$file = $assets['editor.css'] ?? '';
break;
case 'login':
$file = $assets['login.css'];
$file = $assets['login.css'] ?? '';
break;
case 'editor.js':
$file = $assets['editor.js'];
$file = $assets['editor.js'] ?? '';
break;
case 'js':
$file = $assets['app.js'];
$file = $assets['app.js'] ?? '';
break;
default:
$file = null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Plutôt que d'avoir le code pour le custom type après, je l'aurais déplacé ici :

Suggested change
$file = null;
$file = $assets[ $type ] ?? '';

Et supprimer la partie après :

// Custom type
if ( ! empty( $assets[ $type ] ) ) {
	$file = $assets[ $type ];
}

Expand All @@ -170,6 +170,36 @@ public function get_min_file( string $type ): string {
return $file;
}

/**
* Read and get assets json index only once
*
* @return array
*
* @author Léonard Phoumpakka
*/
protected function get_assets_json_index_file(): array {
if ( isset( $this->assets_json_index ) ) {
return $this->assets_json_index;
}

$this->assets_json_index = [];

if ( ! file_exists( \get_theme_file_path( '/dist/assets.json' ) ) ) {
return $this->assets_json_index;
}

$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 $this->assets_json_index;
}

$this->assets_json_index = $assets;

return $this->assets_json_index;
}

/**
* Retrieve data for a compiled asset file.
*
Expand Down
50 changes: 49 additions & 1 deletion inc/Services/Theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@

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 {

/**
* @var Service|bool
*/
protected $asset;
lphoumpakka marked this conversation as resolved.
Show resolved Hide resolved

/**
* @param Service_Container $container
*/
Expand All @@ -17,7 +22,16 @@ public function register( Service_Container $container ): void {}
* @param Service_Container $container
*/
public function boot( Service_Container $container ): void {
$this->asset = $container->get_service( 'assets' );
$this->after_setup_theme();
/**
* @psalm-suppress PossiblyInvalidMethodCall
* @psalm-suppress UndefinedInterfaceMethod
*/
if ( $this->asset->is_minified() ) {
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 +88,38 @@ 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
*
* @return string
*/
public function set_ari_responsive_image_default_img_path(): string {
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 {
return $this->get_min_default_image( $default_img );
}

/**
* Get minified default_image
*
* @param string $original_image
*
* @return string
*
* @author Léonard Phoumpakka
*/
public function get_min_default_image( string $original_image ): string {
return $this->asset->get_min_file( 'assets/' . $original_image );
}
}
Loading