<?php




function thim_child_enqueue_styles()
{

    wp_enqueue_style('thim-parent-style', get_template_directory_uri() . '/style.css', array(), THIM_THEME_VERSION);
    //wp_enqueue_style('coursesstyle', get_stylesheet_directory_uri() . '/cursos/style.css', array(), '1.0.0');
}




// function add_custom_query_var($vars)
// {
//     $vars[] = "organicUnit";
//     $vars[] = "cursoid";
//     $vars[] = "anoletivo";
//     $vars[] = "idUC";
//     return $vars;
// }
// add_filter('query_vars', 'add_custom_query_var');

// function add_rewrite_rules($aRules)
// {
//     $aNewRules = array(
//         'licenciaturas/([^/]+)/?$' => 'index.php?pagename=licenciaturas',
//         'licenciatura/([^/]+)/?$' => 'index.php?pagename=licenciatura&cursoid=$matches[1]',
//         'disciplina-detalhes/([^/]+)/?$' => 'index.php?pagename=disciplina-detalhes&cursoid=$matches[1]&anoletivo=$matches[2]&idDisc=$matches[3]'
//     );
//     $aRules = $aNewRules + $aRules;
//     return $aRules;
// }

// hook add_rewrite_rules function into rewrite_rules_array
// add_filter('rewrite_rules_array', 'add_rewrite_rules');


// UPDATE CORS POLICY
// function initCors($value)
// {
//     $origin = get_http_origin();
//     $allowed_origins = ['api.easyticket.pt'];

//     if ($origin && in_array($origin, $allowed_origins)) {
//         header('Access-Control-Allow-Origin: ' . esc_url_raw($origin));
//         header('Access-Control-Allow-Methods: GET');
//         header('Access-Control-Allow-Credentials: true');
//     }

//     return $value;
// }



//UTILS FILE
require_once get_stylesheet_directory() . '/utils.php';




add_filter('template_include', function ($template) {
    if (is_singular('post')) {
        return get_template_directory() . '/single.php';
    }
    return $template;
});



// remove_action('shutdown', 'wp_ob_end_flush_all', 1); // Removido para restaurar o flush padrao do WordPress e otimizar TTFB

add_action('wp_enqueue_scripts', 'thim_child_enqueue_styles', 1000);



function my_custom_scripts()
{
    wp_enqueue_script('custom-js', get_stylesheet_directory_uri() . '/js/custom.js', array('jquery'), '', true);
}
add_action('wp_enqueue_scripts', 'my_custom_scripts');


function image_src($image, $attachment_id, $size, $icon)
{
    $default = [
        'https://example.com/img.gif',
    ];
    return (false === $image && 'post-thumbnail' === $size) ? $default : $image;
}
add_filter('wp_get_attachment_image_src', 'image_src', 10, 4);




// ADD FONTAWESOME ICONS
function add_this_script_footer()
{ ?>
    <link href="/wp-content/themes/eduma-child/assets/fontawesome5/css/fontawesome.min.css" rel="stylesheet">
    <link href="/wp-content/themes/eduma-child/assets/fontawesome5/css/all.min.css" rel="stylesheet">
    <link href="/wp-content/themes/eduma-child/style_after.css" rel="stylesheet">
<?php }
add_action('wp_footer', 'add_this_script_footer', 40);


// ADD WP-ADMIN CSS
add_action('admin_enqueue_scripts', 'load_admin_style');
function load_admin_style()
{
    wp_enqueue_style('admin_css', get_stylesheet_directory_uri() . '/custom.css', false, '1.0.0');
}


/**
 * Otimizacao: Removido ob_start('html5_slash_fixer') que retinha toda a pagina em buffer e duplicava a memoria.
 *
 * Blindagem defensiva: Se o tema pai reintroduzir o hack de internal-custom.css numa atualizacao,
 * cancelamos o enfileiramento com prioridade alta para evitar o CSS estatico e warnings.
 */
add_action('wp_enqueue_scripts', function() {
    wp_dequeue_style('thim-internal-custom-css');
    wp_deregister_style('thim-internal-custom-css');
}, 1002);

//Remove type attributes
add_action(
    'after_setup_theme',
    function () {
        $args = array(
            'search-form',
            'comment-form',
            'comment-list',
            'gallery',
            'caption',
            'meta',
            'style',
            'script',
        );
        add_theme_support('html5', $args);
    }
);


// MUDAR SLUG DOS EVENTOS
add_filter('register_post_type_args', 'tp_change_post_type', 10, 2);
function tp_change_post_type($args, $post_type)
{
    if ('tp_event' === $post_type) {
        $args['rewrite']['slug'] = 'eventos';
    }
    return $args;
};


//SET FALLBACK FEATURED IMAGE
function my_filter_thumbnail_id( $thumbnail_id, $post = null ) {
    if ( ! $thumbnail_id ) {
            $thumbnail_id = 36556; //id of default featured image
        }
    
        return  $thumbnail_id;
    }
    
 add_filter( 'post_thumbnail_id', 'my_filter_thumbnail_id', 20, 5 );


//BSB UTILITIES FILE
// require_once get_stylesheet_directory() . '/bsbutils/bsbutils.php';
add_action('elementor/query/ips_eventos_home', function ($query) {

    // garantir que é o post type certo
    if ($query->get('post_type') !== 'tp_event') {
        return;
    }

    $meta_query = $query->get('meta_query') ?: [];

    // AJUSTA A META KEY SE NECESSÁRIO
    $meta_query[] = [
        'key'     => '_event_end',
        'value'   => current_time('timestamp'),
        'compare' => '>=',
        'type'    => 'NUMERIC',
    ];

    $query->set('meta_query', $meta_query);
});



add_action('wp_head', function() {
?>
<!-- Meta Pixel Code -->
<script>
!function(f,b,e,v,n,t,s)
{if(f.fbq)return;n=f.fbq=function(){n.callMethod?
n.callMethod.apply(n,arguments):n.queue.push(arguments)};
if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';
n.queue=[];t=b.createElement(e);t.async=!0;
t.src=v;s=b.getElementsByTagName(e)[0];
s.parentNode.insertBefore(t,s)}(window, document,'script',
'https://connect.facebook.net/en_US/fbevents.js');
fbq('init', '701264397380358');
fbq('track', 'PageView');
</script>
<noscript><img height="1" width="1" style="display:none"
src="https://www.facebook.com/tr?id=701264397380358&ev=PageView&noscript=1"
/></noscript>
<!-- End Meta Pixel Code -->

<?php
});

