/* ====================================================== * EDUNEWS HOME ENGINE FINAL * * Elementor Compatible * ACF Compatible * WPML Compatible * Cache Elementor * Global Exclusions * Home Posts Mix * Preview Compatible * ====================================================== */ if (!defined('ABSPATH')) exit; function edunews_get_acf_field($field_name) { $post_id = get_the_ID(); if (!$post_id) { global $post; if ($post instanceof WP_Post) { $post_id = $post->ID; } } if (!$post_id) { return null; } $candidate_ids = []; if (is_preview()) { $autosave = wp_get_post_autosave($post_id); if ($autosave instanceof WP_Post) { $candidate_ids[] = $autosave->ID; } $preview_id = (int) get_query_var('preview_id'); if ($preview_id) { $candidate_ids[] = $preview_id; } } $candidate_ids[] = $post_id; $candidate_ids = array_values(array_unique(array_filter($candidate_ids))); foreach ($candidate_ids as $candidate_id) { if (function_exists('get_field')) { $value = get_field($field_name, $candidate_id); if (!empty($value)) { return $value; } } $meta = get_post_meta($candidate_id, $field_name, true); if (!empty($meta)) { return $meta; } } return null; } /* ====================================================== * HELPERS * ====================================================== */ function edunews_get_valid_post($value) { if (!$value) return null; if (is_array($value)) { $value = $value[0] ?? $value; } if (is_object($value) && isset($value->ID)) { return $value; } if (is_numeric($value)) { if (function_exists('apply_filters')) { $translated_id = apply_filters( 'wpml_object_id', $value, get_post_type($value), true ); if ($translated_id) { $value = $translated_id; } } return get_post($value); } return null; } /* ====================================================== * CACHE RENDER ELEMENTOR * ====================================================== */ function edunews_render_post_template($post_obj, $template_id) { $post_obj = edunews_get_valid_post($post_obj); if (!$post_obj || !$template_id) { return ''; } global $post; $post_backup = $post; $post = $post_obj; setup_postdata($post); ob_start(); echo \Elementor\Plugin::instance() ->frontend ->get_builder_content_for_display( $template_id, true ); $html = ob_get_clean(); wp_reset_postdata(); $post = $post_backup; return $html; } /* ====================================================== * AUTHOR RENDER * ====================================================== */ function edunews_render_author_template($author_field, $template_id = 27588) { if (!function_exists('get_field')) { return ''; } $author = edunews_get_acf_field($author_field); if (!$author) return ''; if (is_array($author) && isset($author['ID'])) { $author_id = (int) $author['ID']; } elseif (is_object($author) && isset($author->ID)) { $author_id = (int) $author->ID; } else { $author_id = (int) $author; } if (!$author_id) return ''; $cache_key = 'edunews_author_' . $author_id . '_' . $template_id; $cached = get_transient($cache_key); if ($cached !== false) { return $cached; } global $post; $post_backup = $post; $post = new stdClass(); $post->ID = 999999; $post->post_author = $author_id; $post->post_type = 'post'; $post->post_status = 'publish'; setup_postdata($post); ob_start(); echo \Elementor\Plugin::instance() ->frontend ->get_builder_content_for_display( $template_id, true ); $html = ob_get_clean(); wp_reset_postdata(); $post = $post_backup; set_transient( $cache_key, $html, HOUR_IN_SECONDS ); return $html; } /* ====================================================== * HOME MAP * ====================================================== */ function edunews_home_map() { return [ 'post_destacado' => [ 'field' => 'featured_post_a', 'template' => 27598 ], 'post_destacado_b' => [ 'field' => 'featured_post_b', 'template' => 27583 ], 'post_destacado_c' => [ 'field' => 'featured_post_c', 'template' => 27583 ], 'post_destacado_event' => [ 'field' => 'featured_event', 'template' => 27616 ], 'post_destacado_d' => [ 'field' => 'featured_post_d', 'template' => 27580 ], 'post_destacado_e' => [ 'field' => 'featured_post_e', 'template' => 27622 ], 'post_destacado_f' => [ 'field' => 'featured_post_f', 'template' => 27619 ], 'post_destacado_g' => [ 'field' => 'featured_post_g', 'template' => 27622 ], 'post_destacado_h' => [ 'field' => 'featured_post_h', 'template' => 27622 ], 'post_destacado_i' => [ 'field' => 'featured_post_i', 'template' => 27622 ], 'post_destacado_j' => [ 'field' => 'featured_post_j', 'template' => 27619 ], 'post_destacado_k' => [ 'field' => 'featured_post_k', 'template' => 27619 ], 'featured_video_a' => [ 'field' => 'featured_video_a', 'template' => 27596 ], 'featured_video_b' => [ 'field' => 'featured_video_b', 'template' => 27596 ], 'featured_video_c' => [ 'field' => 'featured_video_c', 'template' => 27601 ], 'featured_video_d' => [ 'field' => 'featured_video_d', 'template' => 27601 ], 'featured_video_e' => [ 'field' => 'featured_video_e', 'template' => 27601 ], 'featured_video_f' => [ 'field' => 'featured_video_f', 'template' => 27601 ], 'featured_read_a' => [ 'field' => 'featured_read_a', 'template' => 27593 ], 'featured_read_b' => [ 'field' => 'featured_read_b', 'template' => 27593 ], ]; } /* ====================================================== * GENERAR SHORTCODES * ====================================================== */ add_action('init', function() { $map = edunews_home_map(); foreach ($map as $shortcode => $config) { add_shortcode($shortcode, function() use ($config) { $post_obj = edunews_get_valid_post( edunews_get_acf_field($config['field']) ); if (!$post_obj) { return ''; } $template_id = $config['template']; if ($config['field'] === 'featured_post_d') { $post_type = get_post_type($post_obj->ID); if ($post_type === 'edu-bits') { $template_id = 27580; } else { $template_id = 27583; } } return edunews_render_post_template( $post_obj, $template_id ); }); } }); /* ====================================================== * AUTHORS * ====================================================== */ add_action('init', function() { $authors = [ 'author_a', 'author_b', 'author_c', 'author_d', 'author_e', 'author_f', 'author_g', 'author_h' ]; foreach ($authors as $author_field) { add_shortcode($author_field, function() use ($author_field) { return edunews_render_author_template( $author_field, 27588 ); }); } }); /* ====================================================== * EXCLUSIONES * ====================================================== */ function edunews_get_excluded_ids() { $exclude = []; $map = edunews_home_map(); foreach ($map as $config) { $post_obj = edunews_get_valid_post( edunews_get_acf_field($config['field']) ); if ($post_obj && isset($post_obj->ID)) { $exclude[] = $post_obj->ID; } } return array_unique($exclude); } /* ====================================================== * RECENT POSTS * ====================================================== */ function edunews_recent_posts_template($atts = []) { $atts = shortcode_atts([ 'posts' => 5, 'template' => 27590 ], $atts); $exclude = edunews_get_excluded_ids(); $query = new WP_Query([ 'post_type' => [ 'post', 'editorial', 'events', 'edu-reads', 'edu-media', 'edu-bits' ], 'posts_per_page' => intval($atts['posts']), 'post_status' => 'publish', 'post__not_in' => $exclude, 'ignore_sticky_posts' => true ]); if (!$query->have_posts()) { return ''; } global $post; $post_backup = $post; ob_start(); while ($query->have_posts()) { $query->the_post(); echo \Elementor\Plugin::instance() ->frontend ->get_builder_content_for_display( intval($atts['template']), true ); } wp_reset_postdata(); $post = $post_backup; return ob_get_clean(); } add_shortcode('recent_posts_template', 'edunews_recent_posts_template'); /* ====================================================== * HOME POSTS MIX * * 5 posts recientes * 5 edu-bits populares * ====================================================== */ function mostrar_home_posts_mix() { if (!class_exists('\Elementor\Plugin')) { return ''; } $template_id = 27586; global $post; $post_backup = $post; $exclude = edunews_get_excluded_ids(); ob_start(); $recent_posts = new WP_Query([ 'post_type' => 'post', 'posts_per_page' => 5, 'post_status' => 'publish', 'post__not_in' => $exclude, 'ignore_sticky_posts' => true, 'no_found_rows' => true ]); if ($recent_posts->have_posts()) { while ($recent_posts->have_posts()) { $recent_posts->the_post(); $post = get_post(get_the_ID()); setup_postdata($post); echo \Elementor\Plugin::instance() ->frontend ->get_builder_content_for_display( $template_id, true ); } } wp_reset_postdata(); $popular_bits = new WP_Query([ 'post_type' => 'edu-bits', 'posts_per_page' => 5, 'post_status' => 'publish', 'post__not_in' => $exclude, 'meta_key' => '_liked', 'orderby' => 'meta_value_num', 'order' => 'DESC', 'ignore_sticky_posts' => true, 'no_found_rows' => true ]); if ($popular_bits->have_posts()) { while ($popular_bits->have_posts()) { $popular_bits->the_post(); $post = get_post(get_the_ID()); setup_postdata($post); echo \Elementor\Plugin::instance() ->frontend ->get_builder_content_for_display( $template_id, true ); } } wp_reset_postdata(); $post = $post_backup; return ob_get_clean(); } add_shortcode('home_posts_mix', 'mostrar_home_posts_mix');