Files
alina-schillinger/as-coaching-theme/functions.php
2026-04-23 08:21:57 +02:00

245 lines
6.0 KiB
PHP

<?php
/**
* Theme setup and integrations.
*
* @package AS_Coaching
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
function as_coaching_get_theme_version() {
$theme = wp_get_theme();
return $theme->get( 'Version' ) ?: '1.0.0';
}
function as_coaching_setup() {
add_theme_support( 'title-tag' );
add_theme_support( 'post-thumbnails' );
add_theme_support( 'responsive-embeds' );
add_theme_support( 'editor-styles' );
add_theme_support( 'wp-block-styles' );
add_theme_support( 'align-wide' );
add_theme_support(
'html5',
array(
'comment-form',
'comment-list',
'gallery',
'caption',
'search-form',
'script',
'style',
)
);
register_nav_menus(
array(
'header' => __( 'Header Menu', 'as-coaching' ),
'footer' => __( 'Footer Menu', 'as-coaching' ),
)
);
add_editor_style(
array(
'style.css',
'assets/css/editor.css',
)
);
}
add_action( 'after_setup_theme', 'as_coaching_setup' );
function as_coaching_enqueue_assets() {
$version = as_coaching_get_theme_version();
wp_enqueue_style( 'as-coaching-style', get_stylesheet_uri(), array(), $version );
wp_enqueue_script( 'as-coaching-script', get_theme_file_uri( '/assets/js/theme.js' ), array(), $version, true );
if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
wp_enqueue_script( 'comment-reply' );
}
}
add_action( 'wp_enqueue_scripts', 'as_coaching_enqueue_assets' );
function as_coaching_section_url( $anchor ) {
if ( is_front_page() ) {
return '#' . ltrim( $anchor, '#' );
}
return trailingslashit( home_url( '/' ) ) . '#' . ltrim( $anchor, '#' );
}
function as_coaching_get_inline_svg( $relative_path, $class = '' ) {
$file = get_theme_file_path( $relative_path );
if ( ! file_exists( $file ) ) {
return '';
}
$svg = trim( file_get_contents( $file ) );
if ( '' === $svg ) {
return '';
}
$svg = preg_replace( '/<\?xml.*?\?>/i', '', $svg );
$svg = preg_replace_callback(
'/<svg\b([^>]*)>/i',
static function ( $matches ) use ( $class ) {
$attributes = $matches[1];
if ( false === strpos( $attributes, 'aria-hidden=' ) ) {
$attributes .= ' aria-hidden="true"';
}
if ( false === strpos( $attributes, 'focusable=' ) ) {
$attributes .= ' focusable="false"';
}
if ( $class ) {
if ( preg_match( '/class="([^"]*)"/i', $attributes, $class_match ) ) {
$replacement = 'class="' . esc_attr( trim( $class_match[1] . ' ' . $class ) ) . '"';
$attributes = preg_replace( '/class="([^"]*)"/i', $replacement, $attributes, 1 );
} else {
$attributes .= ' class="' . esc_attr( $class ) . '"';
}
}
return '<svg' . $attributes . '>';
},
$svg,
1
);
return $svg;
}
function as_coaching_get_svg_data_uri( $relative_path ) {
$file = get_theme_file_path( $relative_path );
if ( ! file_exists( $file ) ) {
return '';
}
$svg = trim( file_get_contents( $file ) );
if ( '' === $svg ) {
return '';
}
$svg = preg_replace( '/<\?xml.*?\?>/i', '', $svg );
$svg = preg_replace( '/>\s+</', '><', $svg );
return 'data:image/svg+xml,' . rawurlencode( $svg );
}
function as_coaching_output_favicon() {
$icon = as_coaching_get_svg_data_uri( '/assets/svg/favicon.svg' );
if ( ! $icon ) {
return;
}
?>
<link rel="icon" href="<?php echo esc_url( $icon ); ?>" type="image/svg+xml">
<link rel="apple-touch-icon" href="<?php echo esc_url( $icon ); ?>">
<?php
}
add_action( 'wp_head', 'as_coaching_output_favicon' );
add_action( 'admin_head', 'as_coaching_output_favicon' );
add_action( 'login_head', 'as_coaching_output_favicon' );
function as_coaching_register_pattern_categories() {
register_block_pattern_category(
'as-coaching-pages',
array(
'label' => __( 'AS Pages', 'as-coaching' ),
)
);
register_block_pattern_category(
'as-coaching-sections',
array(
'label' => __( 'AS Sections', 'as-coaching' ),
)
);
}
add_action( 'init', 'as_coaching_register_pattern_categories' );
function as_coaching_register_patterns() {
if ( ! function_exists( 'register_block_pattern' ) ) {
return;
}
$pattern_files = glob( get_theme_file_path( '/patterns/*.php' ) );
$registry = WP_Block_Patterns_Registry::get_instance();
if ( empty( $pattern_files ) ) {
return;
}
foreach ( $pattern_files as $pattern_file ) {
$headers = get_file_data(
$pattern_file,
array(
'title' => 'Title',
'slug' => 'Slug',
'description' => 'Description',
'categories' => 'Categories',
'keywords' => 'Keywords',
'viewportWidth' => 'Viewport Width',
'blockTypes' => 'Block Types',
'postTypes' => 'Post Types',
'templateTypes' => 'Template Types',
'inserter' => 'Inserter',
)
);
if ( empty( $headers['title'] ) || empty( $headers['slug'] ) ) {
continue;
}
if ( $registry->is_registered( $headers['slug'] ) ) {
continue;
}
ob_start();
include $pattern_file;
$content = trim( ob_get_clean() );
register_block_pattern(
$headers['slug'],
array(
'title' => $headers['title'],
'description' => $headers['description'],
'categories' => array_filter( array_map( 'trim', explode( ',', $headers['categories'] ) ) ),
'keywords' => array_filter( array_map( 'trim', explode( ',', $headers['keywords'] ) ) ),
'viewportWidth' => $headers['viewportWidth'] ? (int) $headers['viewportWidth'] : 1380,
'blockTypes' => array_filter( array_map( 'trim', explode( ',', $headers['blockTypes'] ) ) ),
'postTypes' => array_filter( array_map( 'trim', explode( ',', $headers['postTypes'] ) ) ),
'templateTypes' => array_filter( array_map( 'trim', explode( ',', $headers['templateTypes'] ) ) ),
'inserter' => ! in_array( strtolower( $headers['inserter'] ), array( 'no', 'false' ), true ),
'content' => $content,
)
);
}
}
add_action( 'init', 'as_coaching_register_patterns', 20 );
function as_coaching_header_menu_fallback( $args ) {
$menu_class = empty( $args->menu_class ) ? 'menu' : $args->menu_class;
echo '<ul class="' . esc_attr( $menu_class ) . '">';
wp_list_pages(
array(
'title_li' => '',
)
);
echo '</ul>';
return;
}