|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Set up the SEO & social sharing meta tags. |
| 4 | + */ |
| 5 | + |
| 6 | +namespace WordPressdotorg\Theme\Theme_Directory_2024\SEO_Social_Meta; |
| 7 | + |
| 8 | +use const WordPressdotorg\Theme\Theme_Directory_2024\THEME_POST_TYPE; |
| 9 | +use function WordPressdotorg\Theme\Theme_Directory_2024\{ get_query_tags, get_tag_labels, get_theme_information }; |
| 10 | + |
| 11 | +add_filter( 'document_title_parts', __NAMESPACE__ . '\set_document_title' ); |
| 12 | +add_filter( 'document_title_separator', __NAMESPACE__ . '\document_title_separator' ); |
| 13 | +add_action( 'jetpack_open_graph_tags', __NAMESPACE__ . '\add_social_meta_tags', 20 ); |
| 14 | +add_action( 'jetpack_seo_meta_tags', __NAMESPACE__ . '\add_seo_meta_tags', 20 ); |
| 15 | + |
| 16 | +add_filter( 'jetpack_enable_open_graph', '__return_true', 100 ); // Enable Jetpack Open Graph tags. |
| 17 | +remove_action( 'wp_head', 'wporg_themes_add_meta_tags' ); // Remove the theme-plugin-added meta tags. |
| 18 | + |
| 19 | +/** |
| 20 | + * Append an optimized site name. |
| 21 | + * |
| 22 | + * @param array $title { |
| 23 | + * The document title parts. |
| 24 | + * |
| 25 | + * @type string $title Title of the viewed page. |
| 26 | + * @type string $page Optional. Page number if paginated. |
| 27 | + * @type string $tagline Optional. Site description when on home page. |
| 28 | + * @type string $site Optional. Site title when not on home page. |
| 29 | + * } |
| 30 | + * @return array Filtered title parts. |
| 31 | + */ |
| 32 | +function set_document_title( $title ) { |
| 33 | + global $wp_query; |
| 34 | + |
| 35 | + $current_browse = $wp_query->query['browse'] ?? false; |
| 36 | + |
| 37 | + if ( is_front_page() && ! $current_browse ) { |
| 38 | + $title['title'] = __( 'WordPress Theme Directory', 'wporg-themes' ); |
| 39 | + $title['tagline'] = __( 'WordPress.org', 'wporg-themes' ); |
| 40 | + } else { |
| 41 | + if ( is_singular( THEME_POST_TYPE ) ) { |
| 42 | + if ( isset( $wp_query->query_vars['view'] ) ) { |
| 43 | + /* translators: Theme title */ |
| 44 | + $title['title'] = sprintf( __( '%s Preview', 'wporg-themes' ), $title['title'] ); |
| 45 | + } |
| 46 | + |
| 47 | + $title['title'] .= ' ' . document_title_separator() . ' ' . __( 'WordPress Theme', 'wporg-themes' ); |
| 48 | + } elseif ( is_author() ) { |
| 49 | + /* translators: Author name */ |
| 50 | + $title['title'] = sprintf( __( 'WordPress Themes by %s', 'wporg-themes' ), $title['title'] ); |
| 51 | + } else { |
| 52 | + $title['title'] = get_archive_title(); |
| 53 | + } |
| 54 | + |
| 55 | + // If results are paged and the max number of pages is known. |
| 56 | + if ( is_paged() && $wp_query->max_num_pages ) { |
| 57 | + $title['page'] = sprintf( |
| 58 | + // translators: 1: current page number, 2: total number of pages |
| 59 | + __( 'Page %1$s of %2$s', 'wporg-themes' ), |
| 60 | + get_query_var( 'paged' ), |
| 61 | + $wp_query->max_num_pages |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + $title['site'] = __( 'WordPress.org', 'wporg-themes' ); |
| 66 | + } |
| 67 | + |
| 68 | + return $title; |
| 69 | +} |
| 70 | + |
| 71 | +/** |
| 72 | + * Set the separator for the document title. |
| 73 | + * |
| 74 | + * @return string Document title separator. |
| 75 | + */ |
| 76 | +function document_title_separator() { |
| 77 | + return ( is_feed() ) ? '—' : '|'; |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * Add meta tags for richer social media integrations. |
| 82 | + */ |
| 83 | +function add_social_meta_tags( $tags ) { |
| 84 | + $default_image = 'https://wordpress.org/files/2024/04/wordpress-homepage-ogimage-202404.png'; |
| 85 | + $site_title = function_exists( '\WordPressdotorg\site_brand' ) ? \WordPressdotorg\site_brand() : 'WordPress.org'; |
| 86 | + $description = __( 'Find the perfect theme for your WordPress website. Choose from thousands of stunning designs with a wide variety of features and customization options.', 'wporg-themes' ); |
| 87 | + |
| 88 | + $tags['og:site_name'] = $site_title; |
| 89 | + $tags['og:title'] = __( 'WordPress Theme Directory', 'wporg-themes' ); |
| 90 | + $tags['og:description'] = $description; |
| 91 | + $tags['og:image'] = esc_url( $default_image ); |
| 92 | + $tags['og:image:alt'] = __( 'WordPress Theme Directory', 'wporg-themes' ); |
| 93 | + $tags['og:locale'] = get_locale(); |
| 94 | + $tags['twitter:card'] = 'summary_large_image'; |
| 95 | + |
| 96 | + $current_browse = $wp_query->query['browse'] ?? false; |
| 97 | + |
| 98 | + if ( is_front_page() && ! $current_browse ) { |
| 99 | + return $tags; |
| 100 | + } |
| 101 | + |
| 102 | + $tags['og:title'] = get_archive_title(); |
| 103 | + |
| 104 | + if ( is_author() ) { |
| 105 | + $tags['og:title'] = sprintf( |
| 106 | + /* translators: Author name */ |
| 107 | + __( 'WordPress Themes by %s', 'wporg-themes' ), |
| 108 | + get_the_author() |
| 109 | + ); |
| 110 | + } else if ( is_singular( THEME_POST_TYPE ) ) { |
| 111 | + $theme = get_theme_information(); |
| 112 | + if ( ! $theme ) { |
| 113 | + return $tags; |
| 114 | + } |
| 115 | + |
| 116 | + $sep = document_title_separator(); |
| 117 | + |
| 118 | + $tags['og:title'] = join( ' ', [ $theme->name, $sep, __( 'WordPress Theme Directory', 'wporg-themes' ) ] ); |
| 119 | + $tags['twitter:text:title'] = join( ' ', [ $theme->name, $sep, __( 'WordPress Theme Directory', 'wporg-themes' ) ] ); |
| 120 | + $tags['og:description'] = $theme->description; |
| 121 | + $tags['twitter:description'] = $theme->description; |
| 122 | + |
| 123 | + if ( $theme->screenshot_url ) { |
| 124 | + $tags['og:image'] = $theme->screenshot_url; |
| 125 | + $tags['og:image:alt'] = $theme->name; |
| 126 | + $tags['twitter:image'] = $theme->screenshot_url; |
| 127 | + $tags['twitter:image:alt'] = $theme->name; |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + return $tags; |
| 132 | +} |
| 133 | + |
| 134 | + |
| 135 | +/** |
| 136 | + * Update the description meta value. |
| 137 | + */ |
| 138 | +function add_seo_meta_tags( $tags ) { |
| 139 | + $description = __( 'Find the perfect theme for your WordPress website. Choose from thousands of stunning designs with a wide variety of features and customization options.', 'wporg-themes' ); |
| 140 | + $tags['description'] = $description; |
| 141 | + |
| 142 | + if ( is_singular( THEME_POST_TYPE ) ) { |
| 143 | + $theme = get_theme_information(); |
| 144 | + if ( $theme ) { |
| 145 | + $tags['description'] = $theme->description; |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + return $tags; |
| 150 | +} |
| 151 | + |
| 152 | +/** |
| 153 | + * Get a human-friendly title for the current view. |
| 154 | + * |
| 155 | + * @return string |
| 156 | + */ |
| 157 | +function get_archive_title() { |
| 158 | + global $wp_query; |
| 159 | + |
| 160 | + $author = isset( $wp_query->query['author_name'] ) ? get_user_by( 'slug', $wp_query->query['author_name'] ) : false; |
| 161 | + $current_browse = $wp_query->query['browse'] ?? false; |
| 162 | + $tags = get_query_tags(); |
| 163 | + |
| 164 | + if ( is_front_page() && ! $current_browse && ! is_paged() ) { |
| 165 | + $title = 'WordPress Themes'; |
| 166 | + } else if ( $author ) { |
| 167 | + // translators: %s Author name. |
| 168 | + $title = sprintf( __( 'Author: %s', 'wporg-themes' ), $author->display_name ); |
| 169 | + } else if ( is_search() ) { |
| 170 | + $title = __( 'Search results', 'wporg-themes' ); |
| 171 | + } else if ( ! empty( $tags ) ) { |
| 172 | + $labels = get_tag_labels( $tags ); |
| 173 | + $title = wp_sprintf_l( '%l', $labels ); |
| 174 | + } else if ( 'community' === $current_browse ) { |
| 175 | + $title = __( 'Community themes', 'wporg-themes' ); |
| 176 | + } else if ( 'commercial' === $current_browse ) { |
| 177 | + $title = __( 'Commercial themes', 'wporg-themes' ); |
| 178 | + } else if ( 'new' === $current_browse ) { |
| 179 | + $title = __( 'Latest themes', 'wporg-themes' ); |
| 180 | + } else if ( 'updated' === $current_browse ) { |
| 181 | + $title = __( 'Recently updated themes', 'wporg-themes' ); |
| 182 | + } else if ( 'favorites' === $current_browse ) { |
| 183 | + $title = __( 'My favorites', 'wporg-themes' ); |
| 184 | + } else { |
| 185 | + $title = __( 'All themes', 'wporg-themes' ); |
| 186 | + } |
| 187 | + |
| 188 | + return $title; |
| 189 | +} |
0 commit comments