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 2 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
56 changes: 41 additions & 15 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,23 @@ public function get_min_file( string $type ): string {
return '';
}

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

$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 '';
}
$assets = $this->get_assets_json_index_file();
lphoumpakka marked this conversation as resolved.
Show resolved Hide resolved

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 +166,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
47 changes: 46 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,14 @@ public function register( Service_Container $container ): void {}
*/
public function boot( Service_Container $container ): void {
$this->after_setup_theme();
/**
* @psalm-suppress PossiblyInvalidMethodCall
* @psalm-suppress UndefinedInterfaceMethod
*/
if ( Framework::get_container()->get_service( 'assets' )->is_minified() ) {
lphoumpakka marked this conversation as resolved.
Show resolved Hide resolved
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 +82,41 @@ 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 {
lphoumpakka marked this conversation as resolved.
Show resolved Hide resolved
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 Framework::get_container()->get_service( 'assets' )->get_min_file( 'assets/' . $original_image );
}
lphoumpakka marked this conversation as resolved.
Show resolved Hide resolved
}