CMS Project Sync
This commit is contained in:
+36
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
/**
|
||||
* Autoloader file for the Bluehost Blueprint.
|
||||
*
|
||||
* This file registers an autoloader function to load theme classes automatically.
|
||||
*
|
||||
* @author Bluehost
|
||||
* @package Bluehost\Blueprint
|
||||
* @version 1.0.0
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Prevent direct access.
|
||||
}
|
||||
|
||||
/**
|
||||
* Autoloader for automatically loading theme classes
|
||||
*/
|
||||
spl_autoload_register(
|
||||
function ( $class_name ) {
|
||||
// Only handle classes that start with 'Bluehost\Blueprint'.
|
||||
if ( strpos( $class_name, 'Bluehost\Blueprint' ) !== 0 ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Convert the class name to a relative file path (e.g., 'Bluehost\Blueprint\Utility' -> 'class-utility.php').
|
||||
$class_file = strtolower( str_replace( 'Bluehost\Blueprint\\', '', $class_name ) );
|
||||
$class_file = strtolower( str_replace( '_', '-', $class_file ) ) . '.php';
|
||||
$file = get_template_directory() . '/inc/classes/class-' . $class_file;
|
||||
|
||||
// If the file exists, include it.
|
||||
if ( file_exists( $file ) ) {
|
||||
require_once $file;
|
||||
}
|
||||
}
|
||||
);
|
||||
+123
@@ -0,0 +1,123 @@
|
||||
<?php
|
||||
/**
|
||||
* Assets.php
|
||||
*
|
||||
* Registers and enqueues all necessary styles and scripts.
|
||||
*
|
||||
* @package Bluehost\Blueprint
|
||||
* @author Bluehost
|
||||
* @since 1.0.0
|
||||
*/
|
||||
|
||||
namespace Bluehost\Blueprint;
|
||||
|
||||
/**
|
||||
* Class Assets
|
||||
*
|
||||
* Registers and enqueues all necessary styles and scripts.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
class Assets {
|
||||
/**
|
||||
* Initializes the class.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public static function init() {
|
||||
// Registers styles and scripts.
|
||||
add_action( 'wp_enqueue_scripts', array( __CLASS__, 'enqueue_frontend_assets' ) );
|
||||
|
||||
add_action( 'init', array( __CLASS__, 'enqueue_custom_block_styles' ) );
|
||||
|
||||
add_action( 'after_setup_theme', array( __CLASS__, 'enqueue_editor_style' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueue frontend CSS and JS
|
||||
*
|
||||
* Enqueues all stylesheets and scripts required for frontend functionality.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public static function enqueue_frontend_assets() {
|
||||
$theme_version = wp_get_theme()->get( 'Version' );
|
||||
|
||||
$style_uri = get_parent_theme_file_uri( 'style.css' );
|
||||
|
||||
wp_enqueue_style(
|
||||
'bluehost-blueprint-style',
|
||||
$style_uri,
|
||||
array(),
|
||||
$theme_version
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load custom block styles.
|
||||
*/
|
||||
public static function enqueue_custom_block_styles() {
|
||||
$env = wp_get_environment_type();
|
||||
$is_debug = defined( 'WP_DEBUG' ) && WP_DEBUG;
|
||||
$is_script_debug = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG;
|
||||
$use_minified = ( 'production' === $env || ! $is_debug || ! $is_script_debug );
|
||||
|
||||
$files = glob( get_template_directory() . '/assets/block-styles/*.css' );
|
||||
|
||||
foreach ( $files as $file ) {
|
||||
$filename = basename( $file, '.css' );
|
||||
|
||||
// Handle different block namespaces.
|
||||
if ( strpos( $filename, 'core-' ) === 0 ) {
|
||||
$block_name = str_replace( 'core-', 'core/', $filename );
|
||||
} elseif ( strpos( $filename, 'woocommerce-' ) === 0 ) {
|
||||
$block_name = str_replace( 'woocommerce-', 'woocommerce/', $filename );
|
||||
} else {
|
||||
$block_name = $filename;
|
||||
}
|
||||
|
||||
$suffix = $use_minified ? '.min' : '';
|
||||
$min_path = get_theme_file_path( "assets/block-styles/{$filename}{$suffix}.css" );
|
||||
$min_src = get_theme_file_uri( "assets/block-styles/{$filename}{$suffix}.css" );
|
||||
|
||||
// Fallback to non-minified version if .min.css doesn't exist.
|
||||
if ( ! file_exists( $min_path ) ) {
|
||||
$min_path = get_theme_file_path( "assets/block-styles/{$filename}.css" );
|
||||
$min_src = get_theme_file_uri( "assets/block-styles/{$filename}.css" );
|
||||
}
|
||||
|
||||
wp_enqueue_block_style(
|
||||
$block_name,
|
||||
array(
|
||||
'handle' => "nfd-block-{$filename}",
|
||||
'src' => $min_src,
|
||||
'path' => $min_path,
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueues the editor style.
|
||||
*
|
||||
* This is a required method for themes that wish to support the block editor.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public static function enqueue_editor_style() {
|
||||
$env = wp_get_environment_type();
|
||||
$is_debug = defined( 'WP_DEBUG' ) && WP_DEBUG;
|
||||
$is_script_debug = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG;
|
||||
$use_minified = ( 'production' === $env || $is_debug || $is_script_debug );
|
||||
|
||||
$suffix = $use_minified ? '.min' : '';
|
||||
$style_file = "assets/editor-styles/editor-style{$suffix}.css";
|
||||
|
||||
// Fallback to unminified if minified file does not exist.
|
||||
if ( ! file_exists( get_theme_file_path( $style_file ) ) ) {
|
||||
$style_file = 'assets/editor-styles/editor-style.css';
|
||||
}
|
||||
|
||||
add_editor_style( "./$style_file" );
|
||||
}
|
||||
}
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
/**
|
||||
* Back-compat.php
|
||||
*
|
||||
* Registers and enqueues all necessary styles and scripts to support old themes previously used by users.
|
||||
*
|
||||
* @package Bluehost\Blueprint
|
||||
* @author Bluehost
|
||||
* @since 1.0.0
|
||||
*/
|
||||
|
||||
namespace Bluehost\Blueprint;
|
||||
|
||||
/**
|
||||
* Class Back_Compat
|
||||
*
|
||||
* Registers and enqueues all necessary styles and scripts.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
class Back_Compat {
|
||||
/**
|
||||
* Initializes the class.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public static function init() {
|
||||
// Registers styles and scripts.
|
||||
add_action( 'wp_enqueue_scripts', array( __CLASS__, 'enqueue_frontend_assets' ) );
|
||||
|
||||
add_action( 'after_setup_theme', array( __CLASS__, 'enqueue_editor_style' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueue frontend CSS and JS
|
||||
*
|
||||
* Enqueues all stylesheets and scripts required for frontend functionality.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public static function enqueue_frontend_assets() {
|
||||
$theme_version = wp_get_theme()->get( 'Version' );
|
||||
|
||||
/**
|
||||
* Enqueues editor stylesheet.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
if ( get_option( 'theme_mods_yith-wonder' ) ) {
|
||||
$suffix = ( wp_get_environment_type() === 'production' || WP_DEBUG || SCRIPT_DEBUG ) ? '.min' : '';
|
||||
$path = "assets/back-compat/yith-wonder{$suffix}.css";
|
||||
$file = get_parent_theme_file_path( $path );
|
||||
$uri = get_parent_theme_file_uri( $path );
|
||||
|
||||
if ( ! file_exists( $file ) ) {
|
||||
$uri = get_parent_theme_file_uri( 'assets/back-compat/yith-wonder.css' );
|
||||
}
|
||||
|
||||
wp_enqueue_style(
|
||||
'bluehost-blueprint-legacy-yith-wonder',
|
||||
$uri,
|
||||
array(),
|
||||
$theme_version
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueues the editor style.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public static function enqueue_editor_style() {
|
||||
if ( get_option( 'theme_mods_yith-wonder' ) ) {
|
||||
$suffix = ( wp_get_environment_type() === 'production' || WP_DEBUG || SCRIPT_DEBUG ) ? '.min' : '';
|
||||
$path = "assets/back-compat/yith-wonder{$suffix}.css";
|
||||
$file = get_parent_theme_file_path( $path );
|
||||
|
||||
if ( ! file_exists( $file ) ) {
|
||||
$path = 'assets/back-compat/yith-wonder.css';
|
||||
}
|
||||
|
||||
add_editor_style( "./$path" );
|
||||
}
|
||||
}
|
||||
}
|
||||
+97
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
/**
|
||||
* Version.php
|
||||
*
|
||||
* Handles theme versioning and database migrations.
|
||||
*
|
||||
* @package Bluehost\Blueprint
|
||||
* @author Bluehost
|
||||
* @since 1.0.0
|
||||
*/
|
||||
|
||||
namespace Bluehost\Blueprint;
|
||||
|
||||
/**
|
||||
* Class Version
|
||||
*
|
||||
* Handles theme versioning and database migrations.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
class Version {
|
||||
/**
|
||||
* Option key for storing the theme version in the database.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
const OPTION_KEY = 'bluehost_blueprint_theme_version';
|
||||
|
||||
/**
|
||||
* Initializes version checks and migrations.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public static function init() {
|
||||
add_action( 'after_setup_theme', array( __CLASS__, 'check_version' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the theme version has changed and runs migrations if needed.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public static function check_version() {
|
||||
$theme_version = wp_get_theme()->get( 'Version' );
|
||||
$stored_version = get_option( self::OPTION_KEY, '1.0.0' );
|
||||
|
||||
if ( version_compare( $theme_version, $stored_version, '>' ) ) {
|
||||
self::run_migrations( $stored_version, $theme_version );
|
||||
update_option( self::OPTION_KEY, $theme_version );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs necessary migrations based on the old and new theme versions.
|
||||
*
|
||||
* @param string $old_version The previously stored version.
|
||||
* @param string $new_version The new theme version.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
private static function run_migrations( $old_version, $new_version ) {
|
||||
/**
|
||||
* Array of migrations to run.
|
||||
* key is the old version, value is the migration function.
|
||||
*
|
||||
* Example:
|
||||
* $migrations = array(
|
||||
* '1.1.0' => 'migrate_to_1_1_0',
|
||||
* );
|
||||
*/
|
||||
$migrations = array();
|
||||
|
||||
foreach ( $migrations as $version => $migration ) {
|
||||
if ( version_compare( $old_version, $version, '<' ) && version_compare( $new_version, $version, '>=' ) ) {
|
||||
// Ensure migration function exists before calling.
|
||||
if ( method_exists( __CLASS__, $migration ) ) {
|
||||
call_user_func( array( __CLASS__, $migration ) );
|
||||
} else {
|
||||
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
|
||||
error_log( "Migration method {$migration} does not exist in " . __CLASS__ );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Example migration function for version 1.1.0.
|
||||
*
|
||||
* @since 1.1.0
|
||||
*/
|
||||
private static function migrate_to_1_1_0() {
|
||||
// Example: Adding a new option to the database.
|
||||
if ( ! get_option( 'bluehost_blueprint_new_option' ) ) {
|
||||
update_option( 'bluehost_blueprint_new_option', 'value' );
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user