这是indexloc提供的服务,不要输入任何密码
Skip to content

Add scripts and styles injection samples #156

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

Merged
merged 5 commits into from
Aug 29, 2022
Merged
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
3 changes: 3 additions & 0 deletions assets/src/css/core-navigation.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/**
* Custom styles required for the core Navigatino block.
*/
8 changes: 8 additions & 0 deletions assets/src/js/core-navigation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Custom script required for the core Navigatino block.
*/

/**
* Internal dependencies
*/
import '../css/core-navigation.scss';
Empty file removed assets/src/js/index.js
Empty file.
163 changes: 163 additions & 0 deletions inc/classes/class-assets.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
<?php
/**
* Theme bootstrap file.
*
* @package Elementary-Theme
*/

namespace Elementary_Theme;

use Elementary_Theme\Traits\Singleton;

/**
* Class Assets
*
* @since 1.0.0
*/
class Assets {

use Singleton;

/**
* Constructor.
*/
protected function __construct() {
// Setup hooks.
$this->setup_hooks();

}

/**
* Setup hooks.
*
* @since 1.0.0
*/
public function setup_hooks() {
add_action( 'wp_enqueue_scripts', [ $this, 'register_assets' ] );
add_filter( 'render_block', [ $this, 'enqueue_block_specific_assets' ], 10, 2 );
}

/**
* Register assets.
*
* @since 1.0.0
*
* @action wp_enqueue_scripts
*/
public function register_assets() {

$this->register_script( 'core-navigation', 'js/core-navigation.js' );
$this->register_style( 'core-navigation', 'css/core-navigation.css' );
}

/**
* Enqueue block specific assets.
*
* @param string $markup Markup of the block.
* @param array $block Array with block information.
*
* @since 1.0.0
*/
public function enqueue_block_specific_assets( $markup, $block ) {
if ( is_array( $block ) && ! empty( $block['blockName'] ) && 'core/navigation' === $block['blockName'] ) {
wp_enqueue_script( 'core-navigation' );
wp_enqueue_style( 'core-navigation' );
}

return $markup;
}

/**
* Get asset dependencies and version info from {handle}.asset.php if exists.
*
* @param string $file File name.
* @param array $deps Script dependencies to merge with.
* @param string $ver Asset version string.
*
* @return array
*/
public function get_asset_meta( $file, $deps = [], $ver = false ) {
$asset_meta_file = sprintf( '%s/js/%s.asset.php', untrailingslashit( ELEMENTARY_THEME_BUILD_DIR ), basename( $file, '.' . pathinfo( $file )['extension'] ) );
$asset_meta = is_readable( $asset_meta_file )
? require $asset_meta_file
: [
'dependencies' => [],
'version' => $this->get_file_version( $file, $ver ),
];

$asset_meta['dependencies'] = array_merge( $deps, $asset_meta['dependencies'] );

return $asset_meta;
}

/**
* Register a new script.
*
* @param string $handle Name of the script. Should be unique.
* @param string|bool $file script file, path of the script relative to the assets/build/ directory.
* @param array $deps Optional. An array of registered script handles this script depends on. Default empty array.
* @param string|bool|null $ver Optional. String specifying script version number, if not set, filetime will be used as version number.
* @param bool $in_footer Optional. Whether to enqueue the script before </body> instead of in the <head>.
* Default 'false'.
* @return bool Whether the script has been registered. True on success, false on failure.
*/
public function register_script( $handle, $file, $deps = [], $ver = false, $in_footer = true ) {

$file_path = sprintf( '%s/%s', ELEMENTARY_THEME_BUILD_DIR, $file );

if ( ! \file_exists( $file_path ) ) {
return false;
}

$src = sprintf( ELEMENTARY_THEME_BUILD_URI . '/%s', $file );
$asset_meta = $this->get_asset_meta( $file, $deps );

return wp_register_script( $handle, $src, $asset_meta['dependencies'], $asset_meta['version'], $in_footer );
}

/**
* Register a CSS stylesheet.
*
* @param string $handle Name of the stylesheet. Should be unique.
* @param string|bool $file style file, path of the script relative to the assets/build/ directory.
* @param array $deps Optional. An array of registered stylesheet handles this stylesheet depends on. Default empty array.
* @param string|bool|null $ver Optional. String specifying script version number, if not set, filetime will be used as version number.
* @param string $media Optional. The media for which this stylesheet has been defined.
* Default 'all'. Accepts media types like 'all', 'print' and 'screen', or media queries like
* '(orientation: portrait)' and '(max-width: 640px)'.
*
* @return bool Whether the style has been registered. True on success, false on failure.
*/
public function register_style( $handle, $file, $deps = [], $ver = false, $media = 'all' ) {

$file_path = sprintf( '%s/%s', ELEMENTARY_THEME_BUILD_DIR, $file );

if ( ! \file_exists( $file_path ) ) {
return false;
}

$src = sprintf( ELEMENTARY_THEME_BUILD_URI . '/%s', $file );
$asset_meta = $this->get_asset_meta( $file, $deps );

return wp_register_style( $handle, $src, $asset_meta['dependencies'], $asset_meta['version'], $media );
}

/**
* Get file version.
*
* @param string $file File path.
* @param int|string|boolean $ver File version.
*
* @return bool|false|int
*/
public function get_file_version( $file, $ver = false ) {
if ( ! empty( $ver ) ) {
return $ver;
}

$file_path = sprintf( '%s/%s', ELEMENTARY_THEME_BUILD_DIR, $file );

return file_exists( $file_path ) ? filemtime( $file_path ) : false;
}

}
4 changes: 3 additions & 1 deletion inc/classes/class-elementary-theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Elementary_Theme\Traits\Singleton;
use Elementary_Theme\Patterns\Block_Patterns;
use Elementary_Theme\Assets;

/**
* Class Elementary_Theme
Expand All @@ -25,6 +26,7 @@ class Elementary_Theme {
protected function __construct() {
// Instantiate classes.
Block_Patterns::get_instance();
Assets::get_instance();

// Setup hooks.
$this->setup_hooks();
Expand All @@ -36,7 +38,7 @@ protected function __construct() {
* @since 1.0.0
*/
public function setup_hooks() {
add_action( 'after_setup_theme', array( $this, 'elementary_theme_support' ) );
add_action( 'after_setup_theme', [ $this, 'elementary_theme_support' ] );
}

/**
Expand Down
20 changes: 10 additions & 10 deletions inc/classes/patterns/class-block-patterns.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,17 @@ public function setup_hooks() {
* @since 1.0.0
*/
public function elementary_theme_register_block_patterns_categories() {
$block_pattern_categories = array(
'featured' => array(
$block_pattern_categories = [
'featured' => [
'label' => __( 'Featured', 'elementary-theme' ),
),
'footer' => array(
],
'footer' => [
'label' => __( 'Footer', 'elementary-theme' ),
),
'query' => array(
],
'query' => [
'label' => __( 'Query', 'elementary-theme' ),
),
);
],
];

/**
* Filters the block pattern categories.
Expand All @@ -87,10 +87,10 @@ public function elementary_theme_register_block_patterns_categories() {
* @since 1.0.0
*/
public function elementary_theme_register_block_patterns() {
$block_patterns_classes = array(
$block_patterns_classes = [
'footer' => 'Footer',
'hidden-404' => 'Hidden_404',
);
];

/**
* Filters the theme block patterns.
Expand Down
10 changes: 6 additions & 4 deletions inc/classes/patterns/content/class-footer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ final class Footer extends Block_Pattern_Base {
* @return array Block pattern properties.
*/
public function block_pattern() {
return array(
return [
'title' => __( 'Footer', 'elementary-theme' ),
'categories' => array( 'footer' ),
'blockTypes' => array( 'core/template-part/footer' ),
'categories' => [ 'footer' ],
'blockTypes' => [ 'core/template-part/footer' ],
'content' => $this->block_pattern_content(),
);
];
}

/**
Expand All @@ -36,6 +36,7 @@ public function block_pattern() {
* @return string Block pattern content.
*/
public function block_pattern_content() {
// phpcs:disable WordPressVIPMinimum.Security.Mustache.OutputNotation
ob_start();
?>
<!-- wp:group {"layout":{"inherit":"true"}} -->
Expand All @@ -57,5 +58,6 @@ public function block_pattern_content() {
</div><!-- /wp:group -->
<?php
return ob_get_clean();
// phpcs:enable WordPressVIPMinimum.Security.Mustache.OutputNotation
}
}
4 changes: 2 additions & 2 deletions inc/classes/patterns/content/class-hidden-404.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ final class Hidden_404 extends Block_Pattern_Base {
* @return array Block pattern properties.
*/
public function block_pattern() {
return array(
return [
'title' => __( '404 content', 'elementary-theme' ),
'inserter' => false,
'content' => $this->block_pattern_content(),
);
];
}

/**
Expand Down
8 changes: 0 additions & 8 deletions index.php

This file was deleted.

9 changes: 4 additions & 5 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,14 @@ const styles = {

};

/* Example of how to add a new entry point for a JS file.
const exampleJS = {
const scripts = {
...sharedConfig,
entry: {
'example-js': path.resolve(process.cwd(), 'assets', 'src', 'js', 'example.js'),
'core-navigation': path.resolve( process.cwd(), 'assets', 'src', 'js', 'core-navigation.js' ),
},
};
*/

module.exports = [
styles, // Do not remove this.
scripts,
styles,
];