get('Version') ); } // Enqueue editor styles add_action( 'enqueue_block_editor_assets', 'enqueue_editor_styles' ); function enqueue_editor_styles() { wp_enqueue_style( 'caliope-editor-style', get_stylesheet_directory_uri() . '/editor-style.css', array(), wp_get_theme()->get('Version') ); } /** * Add caption (Description) to the featured image in the Single Post template only. */ function add_featured_image_caption( $block_content, $block ) { if ( is_single() && isset($block['blockName']) && 'core/post-featured-image' === $block['blockName'] && has_post_thumbnail() ) { $thumbnail_id = get_post_thumbnail_id(); // Get the featured image ID // Retrieve the correct caption (stored in the "Description" field) $attachment = get_post($thumbnail_id); $caption = !empty($attachment->post_excerpt) ? $attachment->post_excerpt : ''; // Insert the caption inside the figure tag if it exists if ( ! empty( $caption ) ) { $figcaption = ''; // Insert the figcaption before the closing tag $block_content = str_replace( '', $figcaption . '', $block_content ); } } return $block_content; } add_filter( 'render_block', 'add_featured_image_caption', 10, 2 ); /** * Future-proof dynamic hiding of ALL parent theme style and typography variations * Automatically detects all variations from parent theme's /styles/ directory and subdirectories * This solution adapts to future parent theme updates without requiring code changes * Preserves ALL child theme custom variations */ function hide_parent_theme_variations_dynamic() { // Only run in admin area if ( ! is_admin() ) { return; } $parent_variations = array(); $child_variations = array(); // Get parent theme variations $parent_styles_dir = get_template_directory() . '/styles'; if ( file_exists( $parent_styles_dir ) && is_dir( $parent_styles_dir ) ) { // Get style variations from root /styles/ directory $style_variation_files = glob( $parent_styles_dir . '/*.json' ); foreach ( $style_variation_files as $file ) { $variation_data = json_decode( file_get_contents( $file ), true ); if ( isset( $variation_data['title'] ) && ! empty( $variation_data['title'] ) ) { $parent_variations[] = $variation_data['title']; } } // Get typography variations from /styles/typography/ subdirectory $typography_dir = $parent_styles_dir . '/typography'; if ( file_exists( $typography_dir ) && is_dir( $typography_dir ) ) { $typography_files = glob( $typography_dir . '/*.json' ); foreach ( $typography_files as $file ) { $variation_data = json_decode( file_get_contents( $file ), true ); if ( isset( $variation_data['title'] ) && ! empty( $variation_data['title'] ) ) { $parent_variations[] = $variation_data['title']; } } } // Get variations from any other subdirectories (colors, blocks, sections, etc.) $subdirectories = array_filter( glob( $parent_styles_dir . '/*' ), 'is_dir' ); foreach ( $subdirectories as $subdir ) { // Skip typography as we already processed it if ( basename( $subdir ) === 'typography' ) { continue; } $subdir_files = glob( $subdir . '/*.json' ); foreach ( $subdir_files as $file ) { $variation_data = json_decode( file_get_contents( $file ), true ); if ( isset( $variation_data['title'] ) && ! empty( $variation_data['title'] ) ) { $parent_variations[] = $variation_data['title']; } } } } // Get child theme variations to preserve them $child_styles_dir = get_stylesheet_directory() . '/styles'; if ( file_exists( $child_styles_dir ) && is_dir( $child_styles_dir ) ) { // Get child style variations from root /styles/ directory $child_style_files = glob( $child_styles_dir . '/*.json' ); foreach ( $child_style_files as $file ) { $variation_data = json_decode( file_get_contents( $file ), true ); if ( isset( $variation_data['title'] ) && ! empty( $variation_data['title'] ) ) { $child_variations[] = $variation_data['title']; } } // Get child typography variations from /styles/typography/ subdirectory $child_typography_dir = $child_styles_dir . '/typography'; if ( file_exists( $child_typography_dir ) && is_dir( $child_typography_dir ) ) { $child_typography_files = glob( $child_typography_dir . '/*.json' ); foreach ( $child_typography_files as $file ) { $variation_data = json_decode( file_get_contents( $file ), true ); if ( isset( $variation_data['title'] ) && ! empty( $variation_data['title'] ) ) { $child_variations[] = $variation_data['title']; } } } } // Remove any child theme variations from parent list to avoid hiding custom variations $parent_variations = array_diff( $parent_variations, $child_variations ); // Fallback for Twenty Twenty-Five if dynamic detection fails if ( empty( $parent_variations ) ) { $parent_variations = array( 'Evening', 'Noon', 'Dusk', 'Afternoon', 'Twilight', 'Morning', 'Sunrise', 'Midnight', 'Beiruti & Literata', 'Vollkorn & Fira Code', 'Platypi & Ysabeau Office', 'Roboto Slab & Manrope', 'Literata & Ysabeau Office', 'Platypi & Literata', 'Literata & Fira Sans' ); // Remove any child theme variations from fallback list too $parent_variations = array_diff( $parent_variations, $child_variations ); } // Remove duplicates and ensure we have unique variations $parent_variations = array_unique( $parent_variations ); // Only proceed if we have parent variations to hide if ( empty( $parent_variations ) ) { return; } // Generate CSS to hide only parent theme variations echo ''; // Generate JavaScript with dynamic variation list echo ''; } add_action( 'admin_head', 'hide_parent_theme_variations_dynamic' ); /** * Dynamically set site logo image via CSS (fallback method) * This makes the logo work programmatically on any site using this theme */ function add_dynamic_site_logo_css() { // Only add CSS override if no custom logo is set if ( ! has_custom_logo() ) { // Get the theme directory URI $theme_uri = get_stylesheet_directory_uri(); $logo_url = $theme_uri . '/assets/images/ufrj-horizontal-cor-rgb-telas.png'; // Output the CSS with the dynamic URL echo ''; } } // Add the CSS to both frontend and admin add_action( 'wp_head', 'add_dynamic_site_logo_css' ); add_action( 'admin_head', 'add_dynamic_site_logo_css' ); /** * Set default site logo when theme is activated or if no logo is set */ function set_default_site_logo() { // Check if site logo is already set if ( ! has_custom_logo() ) { // Path to the default logo in the theme $logo_path = get_stylesheet_directory() . '/assets/images/ufrj-horizontal-cor-rgb-telas.png'; // Check if the logo file exists if ( file_exists( $logo_path ) ) { // Upload the logo to the media library $upload_dir = wp_upload_dir(); $image_data = file_get_contents( $logo_path ); $filename = basename( $logo_path ); // Check if file already exists in uploads $existing_attachment = get_posts( array( 'post_type' => 'attachment', 'meta_query' => array( array( 'key' => '_wp_attached_file', 'value' => $filename, 'compare' => 'LIKE' ) ), 'posts_per_page' => 1 ) ); if ( ! empty( $existing_attachment ) ) { // Use existing attachment $attachment_id = $existing_attachment[0]->ID; } else { // Upload new file if ( wp_mkdir_p( $upload_dir['path'] ) ) { $file = $upload_dir['path'] . '/' . $filename; } else { $file = $upload_dir['basedir'] . '/' . $filename; } file_put_contents( $file, $image_data ); // Create attachment $wp_filetype = wp_check_filetype( $filename, null ); $attachment = array( 'post_mime_type' => $wp_filetype['type'], 'post_title' => 'UFRJ Logo', 'post_content' => '', 'post_status' => 'inherit' ); $attachment_id = wp_insert_attachment( $attachment, $file ); // Generate attachment metadata require_once( ABSPATH . 'wp-admin/includes/image.php' ); $attach_data = wp_generate_attachment_metadata( $attachment_id, $file ); wp_update_attachment_metadata( $attachment_id, $attach_data ); } // Set as site logo set_theme_mod( 'custom_logo', $attachment_id ); } } } // Set default logo on theme activation add_action( 'after_switch_theme', 'set_default_site_logo' ); // Also check and set logo on init if not already set (for existing installations) add_action( 'init', 'set_default_site_logo' ); /** * Force the logo to always be the default UFRJ logo * This prevents any programmatic changes to the logo */ function force_default_logo( $value ) { // Get the default logo attachment ID $default_logo_id = get_option( 'caliope_default_logo_id' ); if ( $default_logo_id ) { return $default_logo_id; } return $value; } add_filter( 'theme_mod_custom_logo', 'force_default_logo', 10, 1 ); /** * Store the default logo ID for reference */ function store_default_logo_id() { if ( ! get_option( 'caliope_default_logo_id' ) ) { $logo_id = get_theme_mod( 'custom_logo' ); if ( $logo_id ) { update_option( 'caliope_default_logo_id', $logo_id ); } } } add_action( 'init', 'store_default_logo_id' ); /** * Override all site logo blocks to use the fixed UFRJ logo * This affects any site logo block anywhere on the site */ function override_site_logo_block( $block_content, $block ) { // Check if this is a site logo block if ( isset( $block['blockName'] ) && 'core/site-logo' === $block['blockName'] ) { // Get the theme logo URL $logo_url = get_stylesheet_directory_uri() . '/assets/images/ufrj-horizontal-cor-rgb-telas.png'; $site_name = get_bloginfo( 'name' ); $home_url = home_url( '/' ); // Extract any custom classes from the original block $classes = 'wp-block-site-logo is-default-size'; if ( isset( $block['attrs']['className'] ) ) { $classes .= ' ' . $block['attrs']['className']; } // Extract width if set $width_style = ''; if ( isset( $block['attrs']['width'] ) ) { $width_style = 'width: ' . $block['attrs']['width'] . 'px;'; } // Create the fixed logo HTML $fixed_logo = sprintf( '
', esc_attr( $classes ), esc_url( $home_url ), esc_url( $logo_url ), esc_attr( $site_name ), esc_attr( $width_style ) ); return $fixed_logo; } return $block_content; } add_filter( 'render_block', 'override_site_logo_block', 10, 2 );