Add backend logic
This commit is contained in:
@ -29,6 +29,12 @@ class StarterSite extends Site {
|
||||
add_filter( 'timber/twig/functions', [ $this, 'add_functions_to_twig' ] );
|
||||
add_filter( 'timber/twig/environment/options', [ $this, 'update_twig_environment_options' ] );
|
||||
|
||||
add_filter('upload_mimes', [$this, 'add_svg_support']); //add svg files
|
||||
add_filter('timber/context', [$this, 'add_global_context']); // variables globales
|
||||
//add_filter('timber/twig', [$this, 'add_youtube_function_to_twig']);
|
||||
add_filter('timber/twig', [$this, 'add_videos_embed_to_twig']);
|
||||
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
@ -185,4 +191,174 @@ class StarterSite extends Site {
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
//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' => 3,
|
||||
'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 getVideosCarousel($cantidad = 2) {
|
||||
return Timber::get_posts([
|
||||
'post_type' => 'videos',
|
||||
'posts_per_page' => $cantidad,
|
||||
'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'
|
||||
]);
|
||||
}
|
||||
|
||||
// Añadir en functions.php si no la tienes ya
|
||||
// private function obtener_youtube_id($url) {
|
||||
// $pattern = '/(?:youtube\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)([^"&?\/\s]{11})/';
|
||||
// preg_match($pattern, $url, $matches);
|
||||
// return isset($matches[1]) ? $matches[1] : false;
|
||||
// }
|
||||
|
||||
// public function add_youtube_function_to_twig($twig) {
|
||||
// $twig->addFunction(new \Twig\TwigFunction('youtube_id', [$this, 'obtener_youtube_id']));
|
||||
// return $twig;
|
||||
// }
|
||||
|
||||
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',
|
||||
'allow' => 'accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; 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;
|
||||
}
|
||||
|
||||
//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_carousel'] = $this->getVideosCarousel();
|
||||
$context['ultimo_articulo_opinion'] = $this->getUltimoArticuloOpinion();
|
||||
|
||||
return $context;
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user