_x( 'Main menu', 'Backend - menu name', 'timber-starter' ),
]
);
// Add default posts and comments RSS feed links to head.
add_theme_support( 'automatic-feed-links' );
/*
* Let WordPress manage the document title.
* By adding theme support, we declare that this theme does not use a
* hard-coded
tag in the document head, and expect WordPress to
* provide it for us.
*/
add_theme_support( 'title-tag' );
/*
* Enable support for Post Thumbnails on posts and pages.
*
* @link https://developer.wordpress.org/themes/functionality/featured-images-post-thumbnails/
*/
add_theme_support( 'post-thumbnails' );
/*
* Switch default core markup for search form, comment form, and comments
* to output valid HTML5.
*/
add_theme_support(
'html5',
[
'comment-form',
'comment-list',
'gallery',
'caption',
]
);
/*
* Enable support for Post Formats.
*
* See: https://codex.wordpress.org/Post_Formats
*/
add_theme_support(
'post-formats',
[
'aside',
'image',
'video',
'quote',
'link',
'gallery',
'audio',
]
);
add_theme_support( 'menus' );
}
/**
* This would return 'foo bar!'.
*
* @param string $text being 'foo', then returned 'foo bar!'
*/
public function myfoo( $text ) {
$text .= ' bar!';
return $text;
}
/**
* This is where you can add your own functions to twig.
*
* @link https://timber.github.io/docs/v2/hooks/filters/#timber/twig/filters
* @param array $filters an array of Twig filters.
*/
public function add_filters_to_twig( $filters ) {
$additional_filters = [
'myfoo' => [
'callable' => [ $this, 'myfoo' ],
],
];
return array_merge( $filters, $additional_filters );
}
/**
* This is where you can add your own functions to twig.
*
* @link https://timber.github.io/docs/v2/hooks/filters/#timber/twig/functions
* @param array $functions an array of existing Twig functions.
*/
public function add_functions_to_twig( $functions ) {
$additional_functions = [
'get_theme_mod' => [
'callable' => 'get_theme_mod',
],
];
return array_merge( $functions, $additional_functions );
}
/**
* Updates Twig environment options.
*
* @see https://twig.symfony.com/doc/2.x/api.html#environment-options
*
* @param array $options an array of environment options
*
* @return array
*/
public function update_twig_environment_options( $options ) {
// $options['autoescape'] = true;
return $options;
}
/**
* New functions to add Sindikatua theme
* @author Gustavo
*/
//add svg files
public function add_svg_support($file_types) {
$new_filetypes = array();
$new_filetypes['svg'] = 'image/svg+xml';
$file_types = array_merge($file_types, $new_filetypes);
return $file_types;
}
//section "portada"
private function getBotonesImagen() {
$botones = [];
for ($i = 1; $i <= 4; $i++) {
$imagen = get_field('imagen_boton_' . $i);
$enlace = get_field('enlace_boton_' . $i);
$botones[] = [
'imagen' => $imagen,
'enlace' => $enlace,
];
}
return $botones;
}
//Get last 3 articles with category "noticias"
private function recentPostsNews() {
return Timber::get_posts([
'post_type' => 'post',
'posts_per_page' => -1,
'category_name' => 'noticias',
'post_status' => 'publish'
]);
}
private function getIconsRRSS() {
$icons_rrss = [];
for ($i = 1; $i <= 4; $i++) {
$img = get_field('rrss_img_' . $i, 'option');
$url = get_field('rrss_url_' . $i, 'option');
if ($img || $url) {
$icons_rrss[] = [
'imagen' => $img,
'url' => $url
];
}
}
return $icons_rrss;
}
private function getLogosHeader ($max_logos = 2) {
$logos = [];
for ($i = 1; $i <= $max_logos; $i++) {
$logoHeader = get_field('logo_header_' . $i, 'option');
$textoHeader = get_field('texto_header_' . $i, 'option');
if ($logoHeader || $textoHeader) {
$logos[] = [
'logo' => $logoHeader,
'texto' => $textoHeader
];
}
}
return $logos;
}
private function getVideos() {
return Timber::get_posts([
'post_type' => 'videos',
'posts_per_page' => -1, // -1 = sin límite
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC'
]);
}
private function getUltimoArticuloOpinion() {
return Timber::get_posts([
'post_type' => 'opinion',
'posts_per_page' => 1,
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC'
]);
}
private function getCampanaArticles() {
return Timber::get_posts([
'post_type' => 'campana',
'posts_per_page' => 1,
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC'
]);
}
public function generar_embed_universal($url) {
if (empty($url)) {
return false;
}
// YouTube
if (strpos($url, 'youtube.com') !== false || strpos($url, 'youtu.be') !== false) {
//error_log('es un vídeo de youtube'); // Debug
preg_match('/(?:youtube\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)([^"&?\/\s]{11})/', $url, $matches);
if (!empty($matches[1])) {
$embed_data = [
'platform' => 'youtube',
'embed_url' => 'https://www.youtube.com/embed/' . $matches[1] . '?rel=0&modestbranding=1',
'thumbnail_url' => 'https://img.youtube.com/vi/' . $matches[1]. '/hqdefault.jpg',
'allow' => 'autoplay; encrypted-media; picture-in-picture'
];
//error_log('Embed URL generada: ' . $embed_data['embed_url']);
return $embed_data;
} else {
error_log('No se pudo extraer ID de YouTube');
}
}
// Vimeo
if (strpos($url, 'vimeo.com') !== false) {
preg_match('/vimeo\.com\/(\d+)/', $url, $matches);
if (!empty($matches[1])) {
return [
'platform' => 'vimeo',
'embed_url' => 'https://player.vimeo.com/video/' . $matches[1] . '?title=0&byline=0&portrait=0',
'allow' => 'autoplay; fullscreen; picture-in-picture'
];
}
}
// Si no se reconoce la plataforma
return [
'platform' => 'generic',
'embed_url' => $url,
'allow' => 'fullscreen'
];
}
// Hacer función disponible en Twig
public function add_videos_embed_to_twig ($twig) {
//error_log('Añadiendo función generar_embed a Twig'); // Debug
$twig->addFunction(new \Twig\TwigFunction('generar_embed', [$this, 'generar_embed_universal']));
return $twig;
}
private function getPaginasAcerca() {
return Timber::get_posts([
'post_type' => 'acerca',
'post_status' => 'publish',
'orderby' => 'menu_order',
'order' => 'ASC',
'posts_per_page' => -1,
]);
}
private function getDocumentos() {
$posts = Timber::get_posts([
'post_type' => 'documento',
'post_status' => 'publish',
'orderby' => 'menu_order',
'order' => 'ASC',
'posts_per_page' => -1,
]);
//error_log('✅ Número de posts "Documentos" encontrados: ' . count($posts));
$documentos = [];
foreach ($posts as $post) {
$doc = [
'id' => $post->ID,
'title' => $post->title,
'link' => $post->link,
'content' => $post->content,
'excerpt' => $post->excerpt,
];
$archivo = get_field('documento', $post->ID);
if ($archivo && is_array($archivo)) {
$doc['documento_url'] = $archivo['url'] ?? '';
$doc['documento_filename'] = $archivo['filename'] ?? '';
$doc['documento_filesize'] = size_format($archivo['filesize'] ?? 0);
$doc['documento_extension'] = pathinfo($archivo['filename'] ?? '', PATHINFO_EXTENSION);
$doc['thumbnail'] = get_the_post_thumbnail_url($post->ID, 'medium');
}
$documentos[] = $doc;
}
return $documentos;
}
/**
* Obtiene los grupos de enlaces de la página "Enlaces"
*/
private function getEnlaces() {
$grupos = [];
// Buscar página por su slug
$page = get_page_by_path('enlaces');
if (!$page) {
error_log('🔴 Página "enlaces" no encontrada con get_page_by_path().');
return [];
}
if (!have_rows('grupos_enlaces', $page->ID)) {
error_log('🔴 Array vacío');
return [];
} else {
//error_log('✅ "grupos_enlaces" tiene filas.');
}
// Si se encuentra la página y el campo tiene filas
if ($page && have_rows('grupos_enlaces', $page->ID)) {
$field = get_field('grupos_enlaces', $page->ID);
//error_log('📦 Contenido de get_field: ' . print_r($field, true));
//error_log('✅ Página "enlaces" encontrada con ID: ' . $page->ID);
while (have_rows('grupos_enlaces', $page->ID)) {
the_row();
$grupo = [
'titulo' => get_sub_field('titulo_grupo'),
'enlaces' => []
];
// Recorrer enlaces dentro del grupo
if (have_rows('enlaces')) {
while (have_rows('enlaces')) {
the_row();
$titulo_enlace = get_sub_field('titulo_enlace');
$url_enlace = get_sub_field('url_enlace');
if (!empty($titulo_enlace) && !empty($url_enlace)) {
$enlace = [
'titulo' => $titulo_enlace,
'url' => $url_enlace,
'target' => get_sub_field('target_enlace') ?: '_self'
];
$grupo['enlaces'][] = $enlace;
}
}
}
// Añadir grupo solo si tiene título y enlaces válidos
if (!empty($grupo['titulo']) && !empty($grupo['enlaces'])) {
$grupos[] = $grupo;
}
}
}
return $grupos;
}
// Get subpages of "servicios publicos"
private function getPaginasServiciosPublicos() {
$pag_servicios_publicos = get_page_by_path('accion-sindical/servicios-publicos');
if (!$pag_servicios_publicos) {
error_log('🔴 Página "Servicios públicos" no encontrada.');
return [];
}
$subpaginas = Timber::get_posts([
'post_type' => 'page',
'post_status' => 'publish',
'post_parent' => $pag_servicios_publicos->ID,
'orderby' => 'menu_order',
'order' => 'ASC',
'posts_per_page' => -1,
]);
foreach ($subpaginas as $pagina) {
//$pagina->imagen = $pagina->thumbnail ? $pagina->thumbnail->src('medium') : null;
$pagina->thumbnail= get_the_post_thumbnail_url($pagina->ID);
}
return $subpaginas;
}
// Get subpages of "servicios privados"
private function getPaginasServiciosPrivados() {
$pag_servicios_privados = get_page_by_path('accion-sindical/servicios-privados');
if (!$pag_servicios_privados) {
error_log('🔴 Página "Servicios privados" no encontrada.');
return [];
}
$subpaginas = Timber::get_posts([
'post_type' => 'page',
'post_status' => 'publish',
'post_parent' => $pag_servicios_privados->ID,
'orderby' => 'menu_order',
'order' => 'ASC',
'posts_per_page' => -1,
]);
foreach ($subpaginas as $pagina) {
//$pagina->imagen = $pagina->thumbnail ? $pagina->thumbnail->src('medium') : null;
$pagina->thumbnail= get_the_post_thumbnail_url($pagina->ID);
}
return $subpaginas;
}
private function formContact() {
$form_test = do_shortcode('[contact-form-7 id="5ed613c" title="Formulario de contacto 1"]');
return $form_test;
}
//Global variables
public function add_global_context($context) {
$context['footer_1'] = get_field('footer_text_1', 'option');
$context['footer_2'] = get_field('footer_text_2', 'option');
$context['recent_posts_noticias'] = $this->recentPostsNews();
$context['botones_imagen'] = $this->getBotonesImagen();
$context['redes_sociales'] = $this->getIconsRRSS();
$context['logos_header'] = $this->getLogosHeader();
$context['videos'] = $this->getVideos();
$context['ultimo_articulo_opinion'] = $this->getUltimoArticuloOpinion();
$context['posts_campana'] = $this->getCampanaArticles();
$context['paginas_acerca'] = $this->getPaginasAcerca();
$context['documentos'] = $this->getDocumentos();
$context['enlaces'] = $this->getEnlaces();
$context['subpaginas_servicios_publicos'] = $this->getPaginasServiciosPublicos();
$context['subpaginas_servicios_privados'] = $this->getPaginasServiciosPrivados();
$context['form_test'] = $this->formContact();;
return $context;
}
}