Files
HogelandLinuxWp/wpfiles/wp-content/themes/hogelandlinux/functions.php
T

382 lines
10 KiB
PHP

<?php
/**
* HogelandLinux theme functions and definitions.
*
* A universal classic theme: page content lives in the editor (Gutenberg /
* classic editor), pages show up in the menu automatically, and the original
* site's design system is exposed through shortcodes for easy composition.
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
define( 'HOGELANDLINUX_VERSION', '1.0.1' );
define( 'HOGELANDLINUX_URL', get_template_directory_uri() );
define( 'HOGELANDLINUX_PATH', get_template_directory() );
if ( ! function_exists( 'hogelandlinux_setup' ) ) {
function hogelandlinux_setup() {
load_theme_textdomain( 'hogelandlinux', HOGELANDLINUX_PATH . '/languages' );
add_theme_support( 'automatic-feed-links' );
add_theme_support( 'title-tag' );
add_theme_support( 'post-thumbnails' );
add_theme_support( 'customize-selective-refresh-widgets' );
add_theme_support(
'html5',
array(
'search-form',
'comment-form',
'comment-list',
'gallery',
'caption',
'style',
'script',
)
);
register_nav_menus(
array(
'primary' => __( 'Primary Menu', 'hogelandlinux' ),
'footer' => __( 'Footer Menu', 'hogelandlinux' ),
)
);
}
}
add_action( 'after_setup_theme', 'hogelandlinux_setup' );
/**
* Enqueue scripts and styles.
*/
function hogelandlinux_scripts() {
wp_enqueue_style(
'hogelandlinux-style',
HOGELANDLINUX_URL . '/assets/css/style.css',
array(),
HOGELANDLINUX_VERSION
);
wp_enqueue_script(
'hogelandlinux-navigation',
HOGELANDLINUX_URL . '/assets/js/main.js',
array(),
HOGELANDLINUX_VERSION,
true
);
}
add_action( 'wp_enqueue_scripts', 'hogelandlinux_scripts' );
/**
* Business info used across the site (matches the original static site).
* All values can be overridden on the Settings > General screen.
*/
function hogelandlinux_business() {
return array(
'name' => get_bloginfo( 'name' ),
'kvk' => get_option( 'hl_kvk', '91927935' ),
'email' => get_option( 'hl_email', get_option( 'admin_email', 'info@chemistry.software' ) ),
'street' => get_option( 'hl_street', 'Voorstraat 123' ),
'postal' => get_option( 'hl_postal', '9967 AA' ),
'village' => get_option( 'hl_village', 'Eenrum' ),
'domain' => wp_parse_url( home_url(), PHP_URL_HOST ),
);
}
/**
* Determine which page slug should be considered "active route" for the menu.
*/
function hogelandlinux_current_key() {
if ( is_front_page() ) {
return 'home';
}
if ( is_home() ) {
return 'blog';
}
$obj = get_queried_object();
if ( $obj && isset( $obj->post_name ) ) {
return $obj->post_name;
}
return home_url();
}
/**
* Render the primary nav. Uses a registered WordPress menu when one is set,
* otherwise falls back to listing published top-level pages (so any new page
* automatically appears in the menu).
*/
function hogelandlinux_nav( $location = 'primary' ) {
$locations = get_nav_menu_locations();
if ( ! empty( $locations[ $location ] ) ) {
wp_nav_menu(
array(
'theme_location' => $location,
'container' => false,
'menu_class' => 'nav-links',
'items_wrap' => '<ul class="nav-links">%3$s</ul>',
'fallback_cb' => false,
)
);
return;
}
if ( 'footer' === $location ) {
return;
}
// Fallback: list pages automatically.
$pages = get_pages(
array(
'sort_column' => 'menu_order',
'sort_order' => 'ASC',
'exclude' => get_option( 'page_on_front' ) ? array( (int) get_option( 'page_on_front' ) ) : array(),
)
);
if ( empty( $pages ) ) {
return;
}
echo '<ul class="nav-links">';
foreach ( $pages as $page ) {
$current = ( hogelandlinux_current_key() === $page->post_name ) ? ' active' : '';
printf(
'<li class="menu-item%s"><a href="%s">%s</a></li>',
esc_attr( $current ),
esc_url( get_permalink( $page ) ),
esc_html( get_the_title( $page ) )
);
}
echo '</ul>';
}
/**
* Fallback for the mobile menu: same pages as primary, or registered menu.
*/
function hogelandlinux_nav_mobile() {
$locations = get_nav_menu_locations();
if ( ! empty( $locations['primary'] ) ) {
wp_nav_menu(
array(
'theme_location' => 'primary',
'container' => false,
'menu_class' => '',
'items_wrap' => '%3$s',
'fallback_cb' => false,
)
);
return;
}
$pages = get_pages(
array(
'sort_column' => 'menu_order',
'sort_order' => 'ASC',
'exclude' => get_option( 'page_on_front' ) ? array( (int) get_option( 'page_on_front' ) ) : array(),
)
);
if ( empty( $pages ) ) {
return;
}
foreach ( $pages as $page ) {
$current = ( hogelandlinux_current_key() === $page->post_name ) ? ' active' : '';
printf(
'<a href="%s" class="%s">%s</a>',
esc_url( get_permalink( $page ) ),
esc_attr( trim( $current ) ),
esc_html( get_the_title( $page ) )
);
}
}
/**
* Shortcode: a generic hero banner.
*
* [hero title="..." subtitle="..." button_text="..." button_url="..."]
* [hero button_secondary_text="..." button_secondary_url="..."]
*/
function hogelandlinux_shortcode_hero( $atts ) {
$atts = shortcode_atts(
array(
'title' => '',
'subtitle' => '',
'button_text' => '',
'button_url' => '',
'button2_text' => '',
'button2_url' => '',
'large' => 'no',
),
$atts,
'hero'
);
ob_start();
?>
<section class="<?php echo ( 'yes' === $atts['large'] ) ? 'hero' : 'page-hero'; ?>">
<div class="<?php echo ( 'yes' === $atts['large'] ) ? 'hero-content' : 'container'; ?>">
<?php if ( $atts['title'] ) : ?>
<h1><?php echo esc_html( $atts['title'] ); ?></h1>
<?php endif; ?>
<?php if ( $atts['subtitle'] ) : ?>
<p class="hero-subtitle"><?php echo esc_html( $atts['subtitle'] ); ?></p>
<?php endif; ?>
<?php if ( $atts['button_text'] ) : ?>
<div class="hero-buttons">
<a href="<?php echo esc_url( $atts['button_url'] ); ?>" class="btn btn-primary"><?php echo esc_html( $atts['button_text'] ); ?></a>
<?php if ( $atts['button2_text'] ) : ?>
<a href="<?php echo esc_url( $atts['button2_url'] ); ?>" class="btn btn-secondary"><?php echo esc_html( $atts['button2_text'] ); ?></a>
<?php endif; ?>
</div>
<?php endif; ?>
</div>
</section>
<?php
return ob_get_clean();
}
add_shortcode( 'hero', 'hogelandlinux_shortcode_hero' );
/**
* Shortcode: opening of a section with an optional title.
* [section title="..."] ... [end-section]
* Use with optional class attr: [section title="..." class="section--brand"]
*/
function hogelandlinux_shortcode_section( $atts, $content = null ) {
$atts = shortcode_atts(
array(
'title' => '',
'class' => 'section--light',
),
$atts,
'section'
);
$title = $atts['title'] ? '<h2>' . esc_html( $atts['title'] ) . '</h2>' : '';
return '<section class="section ' . esc_attr( $atts['class'] ) . '"><div class="container">' . $title . do_shortcode( $content ) . '</div></section>';
}
add_shortcode( 'section', 'hogelandlinux_shortcode_section' );
/**
* Shortcode: a single card.
* [card title="..." class="card--benefit" icon="🛡️"]Body content[/card]
*/
function hogelandlinux_shortcode_card( $atts, $content = null ) {
$atts = shortcode_atts(
array(
'title' => '',
'class' => 'card--service',
'icon' => '',
'link' => '',
),
$atts,
'card'
);
$inner = '';
if ( $atts['icon'] ) {
$inner .= '<div class="card__icon">' . $atts['icon'] . '</div>';
}
if ( $atts['title'] ) {
$inner .= '<h3>' . esc_html( $atts['title'] ) . '</h3>';
}
$inner .= do_shortcode( $content );
$class = 'card ' . esc_attr( $atts['class'] );
if ( $atts['link'] ) {
return '<a class="' . esc_attr( $class . ' card-link' ) . '" href="' . esc_url( $atts['link'] ) . '">' . $inner . '</a>';
}
return '<div class="' . esc_attr( $class ) . '">' . $inner . '</div>';
}
add_shortcode( 'card', 'hogelandlinux_shortcode_card' );
/**
* Shortcode: a responsive grid.
* [grid cols="benefits|services|distros|2|3"] content [/grid]
*/
function hogelandlinux_shortcode_grid( $atts, $content = null ) {
$atts = shortcode_atts(
array(
'cols' => 'services',
),
$atts,
'grid'
);
$map = array(
'benefits' => 'benefits-grid',
'services' => 'services-grid',
'distros' => 'distros-grid',
'features' => 'features-grid',
'steps' => 'steps-grid',
'2' => 'grid grid--2-col',
'3' => 'grid grid--3-col',
);
$class = isset( $map[ $atts['cols'] ] ) ? $map[ $atts['cols'] ] : 'grid';
return '<div class="' . esc_attr( $class ) . '">' . do_shortcode( $content ) . '</div>';
}
add_shortcode( 'grid', 'hogelandlinux_shortcode_grid' );
/**
* Shortcode: a call-to-action section.
* [cta title="..." text="..." button_text="..." button_url="..."]
*/
function hogelandlinux_shortcode_cta( $atts ) {
$atts = shortcode_atts(
array(
'title' => '',
'text' => '',
'button_text' => '',
'button_url' => '',
),
$atts,
'cta'
);
ob_start();
?>
<section class="section cta">
<div class="container">
<h2><?php echo esc_html( $atts['title'] ); ?></h2>
<p><?php echo esc_html( $atts['text'] ); ?></p>
<?php if ( $atts['button_text'] ) : ?>
<a href="<?php echo esc_url( $atts['button_url'] ); ?>" class="btn btn-primary btn-large"><?php echo esc_html( $atts['button_text'] ); ?></a>
<?php endif; ?>
</div>
</section>
<?php
return ob_get_clean();
}
add_shortcode( 'cta', 'hogelandlinux_shortcode_cta' );
/**
* Shortcode: a stat tile (used in the Windows EOL stats).
* [stat icon="💻" number="1+ miljard" label="Windows 10 computers wereldwijd"]
*/
function hogelandlinux_shortcode_stat( $atts ) {
$atts = shortcode_atts(
array(
'icon' => '',
'number' => '',
'label' => '',
),
$atts,
'stat'
);
return '<div class="stat"><div class="stat-icon">' . $atts['icon'] . '</div><h3>' . esc_html( $atts['number'] ) . '</h3><p>' . esc_html( $atts['label'] ) . '</p></div>';
}
add_shortcode( 'stat', 'hogelandlinux_shortcode_stat' );
/**
* Shortcode: a collapsible disclaimer.
* [disclaimer summary="Lees de back-updisclaimer"]Body[/disclaimer]
*/
function hogelandlinux_shortcode_disclaimer( $atts, $content = null ) {
$atts = shortcode_atts(
array(
'summary' => '',
),
$atts,
'disclaimer'
);
return '<details class="disclaimer"><summary>' . esc_html( $atts['summary'] ) . '</summary><div class="disclaimer-body">' . do_shortcode( $content ) . '</div></details>';
}
add_shortcode( 'disclaimer', 'hogelandlinux_shortcode_disclaimer' );