/*
 Theme Name:   Bricks Child Theme
 Theme URI:    https://bricksbuilder.io/
 Description:  Use this child theme to extend Bricks.
 Author:       Bricks
 Author URI:   https://bricksbuilder.io/
 Template:     bricks
 Version:      1.1
 Text Domain:  bricks
*/

<?php
/**
 * Pre-option filters for image sizes — ingen hook, registreres umiddelbart
 * Forhindrer unødvendige DB-kall i Bricks get_image_sizes()
 */
foreach ([
    'medium'                        => [300, 300,  0],
    'medium_large'                  => [768, 0,    0],
    'large'                         => [1024, 1024, 0],
    'thumblist'                     => [300, 200,  0],
    'thumbrelated'                  => [300, 200,  0],
    'meccarouselthumb'              => [300, 200,  1],
    'gridsquare'                    => [400, 400,  1],
    'tileview'                      => [300, 400,  1],
    '1536x1536'                     => [1536, 1536, 0],
    '2048x2048'                     => [2048, 2048, 0],
    'woocommerce_thumbnail'         => [300, 300,  1],
    'woocommerce_single'            => [600, 600,  0],
    'woocommerce_gallery_thumbnail' => [100, 100,  1],
] as $size => [$w, $h, $crop]) {
    add_filter( "pre_option_{$size}_size_w", fn() => $w );
    add_filter( "pre_option_{$size}_size_h", fn() => $h );
    add_filter( "pre_option_{$size}_crop",   fn() => $crop );
}

/**
 * Register/enqueue custom scripts and styles
 */
add_action( 'wp_enqueue_scripts', function() {
    if ( ! bricks_is_builder_main() ) {
        wp_enqueue_style( 'bricks-child', get_stylesheet_uri(), ['bricks-frontend'], filemtime( get_stylesheet_directory() . '/style.css' ) );
    }
} );

/**
 * Register custom elements
 */
add_action( 'init', function() {
    $element_files = [
        __DIR__ . '/elements/title.php',
    ];
    foreach ( $element_files as $file ) {
        \Bricks\Elements::register_element( $file );
    }
}, 11 );

/**
 * Add text strings to builder
 */
add_filter( 'bricks/builder/i18n', function( $i18n ) {
    $i18n['custom'] = esc_html__( 'Custom', 'bricks' );
    return $i18n;
} );

/**
 * Deaktiver unødvendige eksterne HTTP-kall i admin
 */
add_filter( 'http_request_args', function( $args, $url ) {
    // Blokker MEC markedsføringsnotiser
    if ( strpos( $url, 'notifications.webnus.site' ) !== false ) {
        return new WP_Error( 'blocked', 'Blokkert' );
    }
    return $args;
}, 10, 2 );

// Begrens oppdateringssjekker til kun én gang per time
add_filter( 'auto_update_plugin', '__return_false' );