Files
wp-bootstrap/functions.php

383 lines
15 KiB
PHP
Raw Permalink Normal View History

<?php
/**
* WP Bootstrap functions and definitions.
*
* @link https://developer.wordpress.org/themes/basics/theme-functions/
*
* @package WPBootstrap
* @since 0.0.1
*/
// Prevent direct access.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// Load Composer autoloader.
if ( file_exists( get_template_directory() . '/vendor/autoload.php' ) ) {
require_once get_template_directory() . '/vendor/autoload.php';
}
/**
* Sets up theme defaults and registers support for various WordPress features.
*/
if ( ! function_exists( 'wp_bootstrap_setup' ) ) :
function wp_bootstrap_setup() {
// Add support for post formats.
add_theme_support( 'post-formats', array(
'aside', 'audio', 'chat', 'gallery', 'image',
'link', 'quote', 'status', 'video',
) );
// Make theme available for translation.
load_theme_textdomain( 'wp-bootstrap', get_template_directory() . '/languages' );
// Add editor styles.
add_editor_style( 'assets/css/editor-style.css' );
// Register navigation menu locations.
register_nav_menus( array(
'primary' => __( 'Primary Navigation', 'wp-bootstrap' ),
'footer' => __( 'Footer Navigation', 'wp-bootstrap' ),
) );
}
endif;
add_action( 'after_setup_theme', 'wp_bootstrap_setup' );
/**
* Enqueue theme scripts and styles.
*/
if ( ! function_exists( 'wp_bootstrap_enqueue_scripts' ) ) :
function wp_bootstrap_enqueue_scripts() {
$theme_version = wp_get_theme()->get( 'Version' );
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
// Enqueue the compiled Bootstrap + custom CSS.
wp_enqueue_style(
'wp-bootstrap-style',
get_template_directory_uri() . '/assets/css/style' . $suffix . '.css',
array(),
$theme_version
);
// Enqueue Bootstrap JS bundle (includes Popper).
wp_enqueue_script(
'wp-bootstrap-js',
get_template_directory_uri() . '/assets/js/bootstrap.bundle.min.js',
array(),
$theme_version,
true
);
// Enqueue dark mode toggle script.
wp_enqueue_script(
'wp-bootstrap-dark-mode',
get_template_directory_uri() . '/assets/js/dark-mode.js',
array(),
$theme_version,
array(
'strategy' => 'defer',
'in_footer' => false,
)
);
// Inline script to prevent flash of wrong theme.
wp_add_inline_script(
'wp-bootstrap-dark-mode',
'(function(){var t=localStorage.getItem("wp-bootstrap-theme")||(window.matchMedia("(prefers-color-scheme:dark)").matches?"dark":"light");document.documentElement.setAttribute("data-bs-theme",t)})();',
'before'
);
}
endif;
add_action( 'wp_enqueue_scripts', 'wp_bootstrap_enqueue_scripts' );
/**
* Register block pattern categories.
*/
if ( ! function_exists( 'wp_bootstrap_pattern_categories' ) ) :
function wp_bootstrap_pattern_categories() {
register_block_pattern_category(
'wp-bootstrap_page',
array(
'label' => __( 'Pages', 'wp-bootstrap' ),
'description' => __( 'A collection of full page layouts.', 'wp-bootstrap' ),
)
);
register_block_pattern_category(
'wp-bootstrap_hero',
array(
'label' => __( 'Hero Sections', 'wp-bootstrap' ),
'description' => __( 'Large hero and banner sections.', 'wp-bootstrap' ),
)
);
register_block_pattern_category(
'wp-bootstrap_cta',
array(
'label' => __( 'Call to Action', 'wp-bootstrap' ),
'description' => __( 'Call to action sections.', 'wp-bootstrap' ),
)
);
register_block_pattern_category(
'wp-bootstrap_features',
array(
'label' => __( 'Features', 'wp-bootstrap' ),
'description' => __( 'Feature and service showcase sections.', 'wp-bootstrap' ),
)
);
register_block_pattern_category(
'wp-bootstrap_testimonials',
array(
'label' => __( 'Testimonials', 'wp-bootstrap' ),
'description' => __( 'Testimonial and review sections.', 'wp-bootstrap' ),
)
);
register_block_pattern_category(
'wp-bootstrap_pricing',
array(
'label' => __( 'Pricing', 'wp-bootstrap' ),
'description' => __( 'Pricing table sections.', 'wp-bootstrap' ),
)
);
register_block_pattern_category(
'wp-bootstrap_contact',
array(
'label' => __( 'Contact', 'wp-bootstrap' ),
'description' => __( 'Contact information sections.', 'wp-bootstrap' ),
)
);
register_block_pattern_category(
'wp-bootstrap_text',
array(
'label' => __( 'Text & Content', 'wp-bootstrap' ),
'description' => __( 'Text-focused content sections.', 'wp-bootstrap' ),
)
);
}
endif;
add_action( 'init', 'wp_bootstrap_pattern_categories' );
/**
* Register custom block styles for Bootstrap components.
*
* @since 0.1.0
*/
if ( ! function_exists( 'wp_bootstrap_block_styles' ) ) :
function wp_bootstrap_block_styles() {
// core/list - Checkmark style.
register_block_style( 'core/list', array(
'name' => 'checkmark-list',
'label' => __( 'Checkmark', 'wp-bootstrap' ),
'inline_style' => '
ul.is-style-checkmark-list { list-style-type: "\2713"; }
ul.is-style-checkmark-list li { padding-inline-start: 1ch; }',
) );
// core/list - Unstyled.
register_block_style( 'core/list', array(
'name' => 'list-unstyled',
'label' => __( 'Unstyled', 'wp-bootstrap' ),
'inline_style' => '
ul.is-style-list-unstyled, ol.is-style-list-unstyled { list-style: none; padding-left: 0; }',
) );
// core/group - Card.
register_block_style( 'core/group', array(
'name' => 'card',
'label' => __( 'Card', 'wp-bootstrap' ),
'inline_style' => '
.is-style-card { border: 1px solid var(--wp--preset--color--light, #dee2e6); border-radius: 0.375rem; padding: var(--wp--preset--spacing--40); background: var(--wp--preset--color--base); }',
) );
// core/group - Card with Shadow.
register_block_style( 'core/group', array(
'name' => 'card-shadow',
'label' => __( 'Card with Shadow', 'wp-bootstrap' ),
'inline_style' => '
.is-style-card-shadow { border: 1px solid var(--wp--preset--color--light, #dee2e6); border-radius: 0.375rem; padding: var(--wp--preset--spacing--40); background: var(--wp--preset--color--base); box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.15); }',
) );
// core/group - Alert Info.
register_block_style( 'core/group', array(
'name' => 'alert-info',
'label' => __( 'Alert - Info', 'wp-bootstrap' ),
'inline_style' => '
.is-style-alert-info { background-color: #cff4fc; border: 1px solid #9eeaf9; color: #055160; border-radius: 0.375rem; padding: var(--wp--preset--spacing--30); }',
) );
// core/group - Alert Success.
register_block_style( 'core/group', array(
'name' => 'alert-success',
'label' => __( 'Alert - Success', 'wp-bootstrap' ),
'inline_style' => '
.is-style-alert-success { background-color: #d1e7dd; border: 1px solid #a3cfbb; color: #0a3622; border-radius: 0.375rem; padding: var(--wp--preset--spacing--30); }',
) );
// core/group - Alert Warning.
register_block_style( 'core/group', array(
'name' => 'alert-warning',
'label' => __( 'Alert - Warning', 'wp-bootstrap' ),
'inline_style' => '
.is-style-alert-warning { background-color: #fff3cd; border: 1px solid #ffe69c; color: #664d03; border-radius: 0.375rem; padding: var(--wp--preset--spacing--30); }',
) );
// core/group - Alert Danger.
register_block_style( 'core/group', array(
'name' => 'alert-danger',
'label' => __( 'Alert - Danger', 'wp-bootstrap' ),
'inline_style' => '
.is-style-alert-danger { background-color: #f8d7da; border: 1px solid #f1aeb5; color: #58151c; border-radius: 0.375rem; padding: var(--wp--preset--spacing--30); }',
) );
// core/table - Striped Rows.
register_block_style( 'core/table', array(
'name' => 'table-striped',
'label' => __( 'Striped Rows', 'wp-bootstrap' ),
'inline_style' => '
.is-style-table-striped tbody tr:nth-of-type(odd) > * { background-color: rgba(0, 0, 0, 0.05); }',
) );
// core/table - Hover Rows.
register_block_style( 'core/table', array(
'name' => 'table-hover',
'label' => __( 'Hover Rows', 'wp-bootstrap' ),
'inline_style' => '
.is-style-table-hover tbody tr:hover > * { background-color: rgba(0, 0, 0, 0.075); }',
) );
// core/table - Bordered.
register_block_style( 'core/table', array(
'name' => 'table-bordered',
'label' => __( 'Bordered', 'wp-bootstrap' ),
'inline_style' => '
.is-style-table-bordered, .is-style-table-bordered td, .is-style-table-bordered th { border: 1px solid var(--wp--preset--color--light, #dee2e6); }',
) );
// core/quote - Accent Border.
register_block_style( 'core/quote', array(
'name' => 'blockquote-accent',
'label' => __( 'Accent Border', 'wp-bootstrap' ),
'inline_style' => '
.is-style-blockquote-accent { border-left: 4px solid var(--wp--preset--color--primary); background: var(--wp--preset--color--light); padding: var(--wp--preset--spacing--30); border-radius: 0 0.375rem 0.375rem 0; }',
) );
// core/image - Shadow.
register_block_style( 'core/image', array(
'name' => 'shadow',
'label' => __( 'Shadow', 'wp-bootstrap' ),
'inline_style' => '
.is-style-shadow img { box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.15); }',
) );
// core/image - Large Rounded.
register_block_style( 'core/image', array(
'name' => 'rounded-lg',
'label' => __( 'Large Rounded', 'wp-bootstrap' ),
'inline_style' => '
.is-style-rounded-lg img { border-radius: 0.75rem; }',
) );
// core/button - Large.
register_block_style( 'core/button', array(
'name' => 'btn-lg',
'label' => __( 'Large', 'wp-bootstrap' ),
'inline_style' => '
.is-style-btn-lg .wp-block-button__link { padding: 0.75rem 1.5rem; font-size: 1.25rem; border-radius: 0.5rem; }',
) );
// core/button - Small.
register_block_style( 'core/button', array(
'name' => 'btn-sm',
'label' => __( 'Small', 'wp-bootstrap' ),
'inline_style' => '
.is-style-btn-sm .wp-block-button__link { padding: 0.25rem 0.5rem; font-size: 0.875rem; border-radius: 0.25rem; }',
) );
// core/separator - Wide.
register_block_style( 'core/separator', array(
'name' => 'separator-wide',
'label' => __( 'Wide', 'wp-bootstrap' ),
'inline_style' => '
.is-style-separator-wide { max-width: none; }',
) );
}
endif;
add_action( 'init', 'wp_bootstrap_block_styles' );
/**
* Initialize Twig template engine.
*/
if ( ! function_exists( 'wp_bootstrap_init_twig' ) ) :
function wp_bootstrap_init_twig() {
if ( class_exists( '\\WPBootstrap\\Twig\\TwigService' ) ) {
\WPBootstrap\Twig\TwigService::getInstance();
}
}
endif;
add_action( 'after_setup_theme', 'wp_bootstrap_init_twig' );
/**
* Initialize Twig template controller for frontend rendering.
*
* Hooks into template_redirect to render Bootstrap 5 HTML
* via Twig templates instead of FSE block markup on the frontend.
*
* @since 0.1.1
*/
if ( ! function_exists( 'wp_bootstrap_init_templates' ) ) :
function wp_bootstrap_init_templates() {
if ( class_exists( '\\WPBootstrap\\Template\\TemplateController' ) ) {
new \WPBootstrap\Template\TemplateController();
}
}
endif;
add_action( 'after_setup_theme', 'wp_bootstrap_init_templates' );
/**
* Customize comment form fields with Bootstrap classes.
*
* @since 0.1.1
*
* @param array $fields Default comment form fields.
* @return array Modified fields with Bootstrap classes.
*/
if ( ! function_exists( 'wp_bootstrap_comment_form_fields' ) ) :
function wp_bootstrap_comment_form_fields( array $fields ): array {
$commenter = wp_get_current_commenter();
$required = get_option( 'require_name_email' );
$req_attr = $required ? ' required' : '';
$fields['author'] = '<div class="mb-3">'
. '<label for="author" class="form-label">' . __( 'Name', 'wp-bootstrap' ) . ( $required ? ' <span class="text-danger">*</span>' : '' ) . '</label>'
. '<input id="author" name="author" type="text" class="form-control" value="' . esc_attr( $commenter['comment_author'] ) . '"' . $req_attr . '>'
. '</div>';
$fields['email'] = '<div class="mb-3">'
. '<label for="email" class="form-label">' . __( 'Email', 'wp-bootstrap' ) . ( $required ? ' <span class="text-danger">*</span>' : '' ) . '</label>'
. '<input id="email" name="email" type="email" class="form-control" value="' . esc_attr( $commenter['comment_author_email'] ) . '"' . $req_attr . '>'
. '</div>';
$fields['url'] = '<div class="mb-3">'
. '<label for="url" class="form-label">' . __( 'Website', 'wp-bootstrap' ) . '</label>'
. '<input id="url" name="url" type="url" class="form-control" value="' . esc_attr( $commenter['comment_author_url'] ) . '">'
. '</div>';
if ( isset( $fields['cookies'] ) ) {
$fields['cookies'] = '<div class="mb-3 form-check">'
. '<input id="wp-comment-cookies-consent" name="wp-comment-cookies-consent" type="checkbox" class="form-check-input" value="yes">'
. '<label for="wp-comment-cookies-consent" class="form-check-label">'
. __( 'Save my name, email, and website in this browser for the next time I comment.', 'wp-bootstrap' )
. '</label></div>';
}
return $fields;
}
endif;
add_filter( 'comment_form_default_fields', 'wp_bootstrap_comment_form_fields' );