ID ) ) { return false; } return $has_thumbnail; } /** * Current post ID for template tags that do not pass one. * * @return int */ function apev_current_post_id() { $post = get_post(); return $post ? (int) $post->ID : 0; } /** * Hide the author display name. * * @param string|null $display_name Author display name. * @return string|null */ function apev_filter_the_author( $display_name ) { $post_id = apev_current_post_id(); if ( $post_id && apev_classic_context_ok() && apev_should_hide( 'author', $post_id ) ) { return ''; } return $display_name; } /** * Hide the author posts link. * * @param string $link Author link HTML. * @return string */ function apev_filter_the_author_posts_link( $link ) { $post_id = apev_current_post_id(); if ( $post_id && apev_classic_context_ok() && apev_should_hide( 'author', $post_id ) ) { return ''; } return $link; } /** * Hide published/modified dates and times. * * Timestamp formats ('U', 'G') are left untouched because themes and plugins * use them for arithmetic, and an empty string would break that on PHP 8. * * @param string|int $value Formatted date or time. * @param string $format Requested format. * @param WP_Post|int|null $post Post object. * @return string|int */ function apev_filter_date( $value, $format = '', $post = null ) { if ( in_array( $format, array( 'U', 'G' ), true ) ) { return $value; } $post = get_post( $post ); if ( $post && apev_classic_context_ok() && apev_should_hide( 'date', $post->ID ) ) { return ''; } return $value; } /** * Hide the category list. * * @param string $list Category list HTML. * @return string */ function apev_filter_category_list( $list ) { $post_id = apev_current_post_id(); if ( $post_id && apev_classic_context_ok() && apev_should_hide( 'terms', $post_id ) ) { return ''; } return $list; } /** * Hide the tag list. * * @param string $tag_list Tag list HTML. * @param string $before Text before the list. * @param string $sep Separator. * @param string $after Text after the list. * @param int $post_id Post ID. * @return string */ function apev_filter_tag_list( $tag_list, $before = '', $sep = '', $after = '', $post_id = 0 ) { $post_id = $post_id ? (int) $post_id : apev_current_post_id(); if ( $post_id && apev_classic_context_ok() && apev_should_hide( 'terms', $post_id ) ) { return ''; } return $tag_list; } /** * Hide term lists printed with the_terms(). * * @param string $term_list Term list HTML. * @return string */ function apev_filter_term_list( $term_list ) { $post_id = apev_current_post_id(); if ( $post_id && apev_classic_context_ok() && apev_should_hide( 'terms', $post_id ) ) { return ''; } return $term_list; } /** * Map of core blocks to the element they display. * * @return array */ function apev_get_block_map() { $map = array( 'core/post-title' => 'title', 'core/post-featured-image' => 'featured_image', 'core/post-author' => 'author', 'core/post-author-name' => 'author', 'core/post-author-biography' => 'author', 'core/avatar' => 'author', 'core/post-date' => 'date', 'core/post-terms' => 'terms', ); /** * Filters the block-name => element map used to hide blocks. * * @param array $map Block name => element key. */ return (array) apply_filters( 'apev_block_map', $map ); } /** * Post ID a block renders for. * * @param WP_Block|null $instance Block instance. * @return int */ function apev_block_post_id( $instance ) { if ( $instance instanceof WP_Block && isset( $instance->context['postId'] ) ) { return (int) $instance->context['postId']; } return apev_current_post_id(); } /** * Remove hidden blocks, and (optionally) the small meta groups left behind. * * @param string $block_content Rendered block HTML. * @param array $block Parsed block. * @param WP_Block|null $instance Block instance. * @return string */ function apev_filter_render_block( $block_content, $block, $instance = null ) { if ( ! is_array( $block ) || empty( $block['blockName'] ) ) { return $block_content; } $name = $block['blockName']; $map = apev_get_block_map(); if ( isset( $map[ $name ] ) ) { // The avatar block is only treated as "author" when it shows a post author, not a commenter. if ( 'core/avatar' === $name ) { if ( ! $instance instanceof WP_Block || ! isset( $instance->context['postId'] ) || isset( $instance->context['commentId'] ) ) { return $block_content; } } $post_id = apev_block_post_id( $instance ); if ( $post_id && apev_should_hide( $map[ $name ], $post_id ) ) { return ''; } return $block_content; } if ( 'core/group' === $name ) { $settings = apev_get_settings(); if ( ! empty( $settings['block_meta_cleanup'] ) ) { $post_id = apev_current_post_id(); if ( $post_id && apev_post_has_hidden_elements( $post_id ) && apev_is_leftover_meta_group( $block, $post_id ) ) { return ''; } } } return $block_content; } /** * Whether a group block only holds hidden meta blocks plus short labels * (for example "by" or "in"), so removing it leaves no orphaned words. * * @param array $block Parsed group block. * @param int $post_id Post ID. * @return bool */ function apev_is_leftover_meta_group( $block, $post_id ) { $found_hidden = false; $inner = isset( $block['innerBlocks'] ) && is_array( $block['innerBlocks'] ) ? $block['innerBlocks'] : array(); if ( empty( $inner ) ) { return false; } $only_meta = apev_scan_meta_tree( $inner, $post_id, $found_hidden, 0 ); return $only_meta && $found_hidden; } /** * Recursive helper for apev_is_leftover_meta_group(). * * @param array $blocks Parsed inner blocks. * @param int $post_id Post ID. * @param bool $found_hidden Set to true when a hidden meta block is found. * @param int $depth Current depth. * @return bool True when every block is a hidden meta block, a short label or a nested group of those. */ function apev_scan_meta_tree( $blocks, $post_id, &$found_hidden, $depth ) { if ( $depth > 5 ) { return false; } $map = apev_get_block_map(); foreach ( $blocks as $inner ) { if ( ! is_array( $inner ) ) { continue; } $name = isset( $inner['blockName'] ) ? $inner['blockName'] : null; $html = isset( $inner['innerHTML'] ) ? (string) $inner['innerHTML'] : ''; if ( null === $name ) { // Freeform content between blocks: whitespace is fine, real text is not. if ( '' !== trim( wp_strip_all_tags( $html ) ) ) { return false; } continue; } // Avatars may belong to a commenter; without block context we cannot tell, so keep the group. if ( 'core/avatar' === $name ) { return false; } if ( isset( $map[ $name ] ) ) { if ( ! apev_should_hide( $map[ $name ], $post_id ) ) { return false; } $found_hidden = true; continue; } if ( 'core/paragraph' === $name ) { $text = trim( wp_strip_all_tags( $html ) ); $length = function_exists( 'mb_strlen' ) ? mb_strlen( $text ) : strlen( $text ); if ( $length > 20 ) { return false; } continue; } if ( 'core/group' === $name ) { $children = isset( $inner['innerBlocks'] ) && is_array( $inner['innerBlocks'] ) ? $inner['innerBlocks'] : array(); if ( ! apev_scan_meta_tree( $children, $post_id, $found_hidden, $depth + 1 ) ) { return false; } continue; } return false; } return true; } /** * Add "apev-hides-*" classes to the post wrapper (classic themes) so the * fallback CSS can hide leftover labels such as "Posted on" or "by". * * @param string[] $classes Post classes. * @param string[] $class Extra classes passed to post_class(). * @param int $post_id Post ID. * @return string[] */ function apev_filter_post_class( $classes, $class, $post_id ) { if ( wp_is_block_theme() || ! apev_css_fallback_enabled() ) { return $classes; } foreach ( apev_get_element_keys() as $element ) { if ( apev_should_hide( $element, (int) $post_id ) ) { $classes[] = 'apev-hides-' . str_replace( '_', '-', $element ); } } return $classes; } /** * Whether the classic-theme CSS fallback is enabled. * * @return bool */ function apev_css_fallback_enabled() { $settings = apev_get_settings(); return ! empty( $settings['css_fallback'] ); } /** * Enqueue the classic-theme fallback stylesheet only when it can matter. */ function apev_enqueue_frontend_assets() { if ( wp_is_block_theme() || ! apev_css_fallback_enabled() ) { return; } $settings = apev_get_settings(); $needed = 'everywhere' === $settings['scope']; if ( ! $needed && is_singular() ) { $needed = apev_post_has_hidden_elements( (int) get_queried_object_id() ); } if ( ! $needed ) { return; } wp_enqueue_style( 'apev-classic-fallback', APEV_PLUGIN_URL . 'assets/css/classic-fallback.css', array(), APEV_VERSION ); }