t_options(); if ( $options['disable_featured_image'] && is_singular() ) { remove_action( 'generate_after_entry_header', 'generate_blog_single_featured_image' ); remove_action( 'generate_before_content', 'generate_blog_single_featured_image' ); remove_action( 'generate_after_header', 'generate_blog_single_featured_image' ); remove_action( 'generate_before_content', 'generate_featured_page_header_inside_single' ); remove_action( 'generate_after_header', 'generate_featured_page_header' ); } if ( $options['site_logo'] && GeneratePress_Elements_Helper::does_option_exist( 'site-logo' ) ) { if ( '' !== $options['site_header_merge'] ) { add_action( 'generate_after_logo', array( $this, 'add_site_logo' ) ); } else { add_filter( 'theme_mod_custom_logo', array( $this, 'replace_logo' ) ); if ( $options['retina_logo'] && GeneratePress_Elements_Helper::does_option_exist( 'retina-logo' ) ) { add_filter( 'generate_retina_logo', array( $this, 'replace_logo' ) ); } } } if ( $options['navigation_logo'] && GeneratePress_Elements_Helper::does_option_exist( 'navigation-logo' ) ) { if ( $options['site_header_merge'] && GeneratePress_Elements_Helper::does_option_exist( 'sticky-navigation' ) ) { add_action( 'generate_inside_navigation', array( $this, 'add_navigation_logo' ) ); } else { add_filter( 'generate_navigation_logo', array( $this, 'replace_logo' ) ); } } if ( $options['mobile_logo'] && GeneratePress_Elements_Helper::does_option_exist( 'mobile-logo' ) ) { if ( 'merge' === $options['site_header_merge'] ) { add_action( 'generate_inside_mobile_header', array( $this, 'add_mobile_header_logo' ) ); } else { add_filter( 'generate_mobile_header_logo', array( $this, 'replace_logo' ) ); } } if ( $options['navigation_location'] ) { add_filter( 'generate_navigation_location', array( $this, 'navigation_location' ) ); } if ( '' !== $options['site_header_merge'] ) { add_action( 'generate_before_header', array( $this, 'merged_header_start' ), 1 ); add_action( 'generate_after_header', array( $this, 'merged_header_end' ), 8 ); if ( 'contained' === $options['container'] ) { add_filter( 'generate_header_class', array( $this, 'site_header_classes' ) ); } } if ( $options['content'] ) { self::remove_template_elements(); } } /** * Returns our custom logos if set within the Page Header. * * @since 1.7 * * @return string New URLs to images. */ public static function replace_logo() { $filter = current_filter(); $options = self::get_options(); if ( 'theme_mod_custom_logo' === $filter ) { return $options['site_logo']; } if ( 'generate_retina_logo' === $filter ) { return wp_get_attachment_url( $options['retina_logo'] ); } if ( 'generate_navigation_logo' === $filter ) { return wp_get_attachment_url( $options['navigation_logo'] ); } if ( 'generate_mobile_header_logo' === $filter ) { return wp_get_attachment_url( $options['mobile_logo'] ); } } /** * Adds a new site logo element if our header is merged on desktop only. * * @since 1.7 */ public static function add_site_logo() { $options = self::get_options(); $logo_url = wp_get_attachment_url( $options['site_logo'] ); $retina_logo_url = wp_get_attachment_url( $options['retina_logo'] ); if ( ! $logo_url ) { return; } $attr = apply_filters( 'generate_page_hero_logo_attributes', array( 'class' => 'header-image', 'alt' => esc_attr( apply_filters( 'generate_logo_title', get_bloginfo( 'name', 'display' ) ) ), 'src' => $logo_url, 'title' => esc_attr( apply_filters( 'generate_logo_title', get_bloginfo( 'name', 'display' ) ) ), ) ); if ( '' !== $retina_logo_url ) { $attr['srcset'] = $logo_url . ' 1x, ' . $retina_logo_url . ' 2x'; // Add dimensions to image if retina is set. This fixes a container width bug in Firefox. $data = wp_get_attachment_metadata( $options['site_logo'] ); if ( ! empty( $data ) ) { $attr['width'] = $data['width']; $attr['height'] = $data['height']; } } $attr = array_map( 'esc_attr', $attr ); $html_attr = ''; foreach ( $attr as $name => $value ) { $html_attr .= " $name=" . '"' . $value . '"'; } echo apply_filters( 'generate_page_hero_logo_output', sprintf( // WPCS: XSS ok, sanitization ok. '', esc_url( apply_filters( 'generate_logo_href' , home_url( '/' ) ) ), esc_attr( apply_filters( 'generate_logo_title', get_bloginfo( 'name', 'display' ) ) ), $html_attr ), $logo_url, $html_attr ); } /** * Adds the custom navigation logo if needed. * Only needed if there's a sticky navigation. * * @since 1.7 */ public static function add_navigation_logo() { $options = self::get_options(); printf( '', esc_url( apply_filters( 'generate_logo_href' , home_url( '/' ) ) ), esc_attr( apply_filters( 'generate_logo_title', get_bloginfo( 'name', 'display' ) ) ), esc_url( wp_get_attachment_url( $options['navigation_logo'] ) ), esc_attr( apply_filters( 'generate_logo_title', get_bloginfo( 'name', 'display' ) ) ) ); } /** * Adds the custom mobile header if needed. * Only needed if there's a sticky navigation. * * @since 1.7 */ public static function add_mobile_header_logo() { $options = self::get_options(); if ( 'title' === GeneratePress_Elements_Helper::does_option_exist( 'mobile-header-branding' ) ) { return; } printf( '', esc_url( apply_filters( 'generate_logo_href' , home_url( '/' ) ) ), esc_attr( apply_filters( 'generate_logo_title', get_bloginfo( 'name', 'display' ) ) ), esc_url( wp_get_attachment_url( $options['mobile_logo'] ) ), esc_attr( apply_filters( 'generate_logo_title', get_bloginfo( 'name', 'display' ) ) ) ); } /** * Set the navigation location if set. * * @since 1.7 * * @return string The navigation location. */ public static function navigation_location() { $options = self::get_options(); if ( 'no-navigation' === $options['navigation_location'] ) { return ''; } else { return $options['navigation_location']; } } /** * The opening merged header element. * * @since 1.7 */ public static function merged_header_start() { echo '
'; } /** * The closing merged header element. * * @since 1.7 */ public static function merged_header_end() { echo '
'; } /** * Adds classes to the site header. * * @since 1.7 * * @param $classes Existing classes. * @return array New classes. */ public static function site_header_classes( $classes ) { $classes[] = 'grid-container'; $classes[] = 'grid-parent'; return $classes; } /** * Checks if template tags exist, and removes those elements from elsewhere. * * @since 1.7 */ public static function remove_template_elements() { $options = self::get_options(); if ( strpos( $options[ 'content' ], '{{post_title}}' ) !== false ) { add_filter( 'generate_show_title', '__return_false' ); remove_action( 'generate_archive_title', 'generate_archive_title' ); add_filter( 'post_class', array( self::$hero, 'remove_hentry' ) ); } if ( strpos( $options[ 'content' ], '{{post_date}}' ) !== false ) { add_filter( 'generate_post_date', '__return_false' ); add_filter( 'post_class', array( self::$hero, 'remove_hentry' ) ); } if ( strpos( $options[ 'content' ], '{{post_author}}' ) !== false ) { add_filter( 'generate_post_author', '__return_false' ); add_filter( 'post_class', array( self::$hero, 'remove_hentry' ) ); } if ( strpos( $options[ 'content' ], '{{post_terms.category}}' ) !== false ) { add_filter( 'generate_show_categories', '__return_false' ); } if ( strpos( $options[ 'content' ], '{{post_terms.post_tag}}' ) !== false ) { add_filter( 'generate_show_tags', '__return_false' ); } } /** * Checks for template tags and replaces them. * * @since 1.7 * * @param $content The content to check. * @return mixed The content with the template tags replaced. */ public static function template_tags( $content ) { $search = array(); $replace = array(); $search[] = '{{post_title}}'; $post_title = ''; if ( is_singular() ) { $post_title = get_the_title(); } elseif ( is_tax() || is_category() || is_tag() ) { $post_title = get_queried_object()->name; } elseif ( is_post_type_archive() ) { $post_title = post_type_archive_title( '', false ); } elseif ( is_archive() && function_exists( 'get_the_archive_title' ) ) { $post_title = get_the_archive_title(); } elseif ( is_home() ) { $post_title = __( 'Blog', 'gp-premium' ); } $replace[] = apply_filters( 'generate_page_hero_post_title', $post_title ); if ( is_singular() ) { $time_string = ''; if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) { $time_string = '' . $time_string; } $time_string = sprintf( $time_string, esc_attr( get_the_date( 'c' ) ), esc_html( get_the_date() ), esc_attr( get_the_modified_date( 'c' ) ), esc_html( get_the_modified_date() ) ); $search[] = '{{post_date}}'; $replace[] = apply_filters( 'generate_page_hero_post_date', $time_string ); // Author global $post; $author_id = $post->post_author; $author = sprintf( '', esc_url( get_author_posts_url( $author_id ) ), esc_attr( sprintf( __( 'View all posts by %s', 'gp-premium' ), get_the_author_meta( 'display_name', $author_id ) ) ), esc_html( get_the_author_meta( 'display_name', $author_id ) ) ); $search[] = '{{post_author}}'; $replace[] = apply_filters( 'generate_page_hero_post_author', $author ); // Post terms if ( strpos( $content, '{{post_terms' ) !== false ) { $data = preg_match_all( '/{{post_terms.([^}]*)}}/', $content, $matches ); foreach ( $matches[1] as $match ) { $search[] = '{{post_terms.' . $match . '}}'; $terms = get_the_term_list( get_the_ID(), $match, apply_filters( 'generate_page_hero_terms_before', '' ), apply_filters( 'generate_page_hero_terms_separator', ', ' ), apply_filters( 'generate_page_hero_terms_after', '' ) ); if ( ! is_wp_error( $terms ) ) { $replace[] = $terms; } } } // Custom field if ( strpos( $content, '{{custom_field' ) !== false ) { $data = preg_match_all( '/{{custom_field.([^}]*)}}/', $content, $matches ); foreach ( $matches[1] as $match ) { if ( null !== get_post_meta( get_the_ID(), $match, true ) && '_thumbnail_id' !== $match ) { $search[] = '{{custom_field.' . $match . '}}'; $replace[] = get_post_meta( get_the_ID(), $match, true ); } } $thumbnail_id = get_post_meta( get_the_ID(), '_thumbnail_id', true ); if ( null !== $thumbnail_id ) { $search[] = '{{custom_field._thumbnail_id}}'; $replace[] = wp_get_attachment_image( $thumbnail_id, apply_filters( 'generate_hero_thumbnail_id_size', 'medium' ) ); } } } // Taxonomy description if ( is_tax() || is_category() || is_tag() ) { if ( strpos( $content, '{{custom_field' ) !== false ) { $search[] = '{{custom_field.description}}'; $replace[] = term_description( get_queried_object()->term_id, get_queried_object()->taxonomy ); } } return str_replace( $search, $replace, $content ); } /** * When the post title, author or date are in the Page Hero, they appear outside of the * hentry element. This causes errors in Google Search Console. * * @since 1.7 * * @param array $classes * @return array */ public function remove_hentry( $classes ) { $classes = array_diff( $classes, array( 'hentry' ) ); return $classes; } }