refactor: theme from lovable
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
add_action( 'wp_enqueue_scripts', 'cascapedia_st_jules_enqueue_assets' );
|
||||
function cascapedia_st_jules_enqueue_assets(): void {
|
||||
$manifest_path = get_theme_file_path( 'assets/dist/.vite/manifest.json' );
|
||||
|
||||
if ( ! file_exists( $manifest_path ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$manifest = json_decode( file_get_contents( $manifest_path ), true );
|
||||
$entry = $manifest['assets/src/main.tsx'] ?? null;
|
||||
|
||||
if ( ! is_array( $entry ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! empty( $entry['css'] ) && is_array( $entry['css'] ) ) {
|
||||
foreach ( $entry['css'] as $index => $css_file ) {
|
||||
$css_path = get_theme_file_path( 'assets/dist/' . $css_file );
|
||||
wp_enqueue_style(
|
||||
'cascapedia-st-jules-' . $index,
|
||||
get_theme_file_uri( 'assets/dist/' . $css_file ),
|
||||
array(),
|
||||
file_exists( $css_path ) ? (string) filemtime( $css_path ) : wp_get_theme()->get( 'Version' )
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! empty( $entry['file'] ) ) {
|
||||
$js_path = get_theme_file_path( 'assets/dist/' . $entry['file'] );
|
||||
wp_enqueue_script(
|
||||
'cascapedia-st-jules-app',
|
||||
get_theme_file_uri( 'assets/dist/' . $entry['file'] ),
|
||||
array(),
|
||||
file_exists( $js_path ) ? (string) filemtime( $js_path ) : wp_get_theme()->get( 'Version' ),
|
||||
true
|
||||
);
|
||||
|
||||
$page_data = cascapedia_st_jules_current_page_data();
|
||||
wp_add_inline_script(
|
||||
'cascapedia-st-jules-app',
|
||||
'window.__CASCA_PAGE__ = ' . wp_json_encode( $page_data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES ) . ';',
|
||||
'before'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function cascapedia_st_jules_current_page_data(): array {
|
||||
$post_id = get_queried_object_id();
|
||||
if ( ! $post_id && ( is_home() || is_front_page() ) ) {
|
||||
$post_id = (int) get_option( 'page_on_front' );
|
||||
}
|
||||
|
||||
$sections = $post_id ? json_decode( (string) get_post_meta( $post_id, '_react_sections_json', true ), true ) : array();
|
||||
|
||||
$data = array(
|
||||
'id' => $post_id,
|
||||
'seo' => array(
|
||||
'title' => array(
|
||||
'fr' => $post_id ? (string) get_post_meta( $post_id, '_seo_title_fr', true ) : '',
|
||||
'en' => $post_id ? (string) get_post_meta( $post_id, '_seo_title_en', true ) : '',
|
||||
),
|
||||
'description' => array(
|
||||
'fr' => $post_id ? (string) get_post_meta( $post_id, '_seo_description_fr', true ) : '',
|
||||
'en' => $post_id ? (string) get_post_meta( $post_id, '_seo_description_en', true ) : '',
|
||||
),
|
||||
),
|
||||
'sections' => is_array( $sections ) ? $sections : array(),
|
||||
);
|
||||
|
||||
return cascapedia_st_jules_rewrite_asset_urls( $data );
|
||||
}
|
||||
|
||||
function cascapedia_st_jules_rewrite_asset_urls( mixed $value ): mixed {
|
||||
if ( is_array( $value ) ) {
|
||||
foreach ( $value as $key => $item ) {
|
||||
$value[ $key ] = cascapedia_st_jules_rewrite_asset_urls( $item );
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
if ( is_string( $value ) && str_starts_with( $value, '/assets/' ) ) {
|
||||
return get_theme_file_uri( 'assets/dist' . $value );
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
Reference in New Issue
Block a user