/
head.php
185 lines (157 loc) · 5.08 KB
/
head.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
/**
* HTML head markup and customizations.
*
* @package wporg-developer
*/
/**
* Class to handle HTML head markup.
*/
class DevHub_Head {
/**
* Initializes module.
*/
public static function init() {
add_action( 'init', array( __CLASS__, 'do_init' ) );
}
/**
* Handles adding/removing hooks as needed.
*/
public static function do_init() {
add_filter( 'document_title_parts', array( __CLASS__, 'document_title' ) );
add_filter( 'document_title_separator', array( __CLASS__, 'document_title_separator' ) );
add_action( 'wp_head', array( __CLASS__, 'output_head_tags' ), 2 );
}
/**
* Filters document title to add context based on what is being viewed.
*
* @param array $parts The document title parts.
* @return array The document title parts.
*/
public static function document_title( $parts ) {
global $page, $paged;
if ( is_feed() ) {
return $parts;
}
$title = $parts['title'];
$sep = '|';
$post_type = get_query_var( 'post_type' );
// Omit 'Home' from the home page.
if ( 'Home' === $title ) {
$title = '';
}
// Add post type to title if it's a parsed item.
elseif ( is_singular() && \DevHub\is_parsed_post_type( $post_type ) ) {
if ( $post_type_object = get_post_type_object( $post_type ) ) {
$title .= " $sep " . get_post_type_object( $post_type )->labels->singular_name;
}
}
// Add handbook name to title if relevent
elseif ( ( is_singular() || is_post_type_archive() ) && false !== strpos( $post_type, 'handbook' ) ) {
if ( $post_type_object = get_post_type_object( $post_type ) ) {
$handbook_label = get_post_type_object( $post_type )->labels->name;
$handbook_name = \WPorg_Handbook::get_name( $post_type ) . " Handbook";
// Replace title with handbook name if this is landing page for the handbook
if ( $title == $handbook_label ) {
$title = $handbook_name;
// Otherwise, append the handbook name
} else {
$title .= " $sep " . $handbook_name;
}
}
}
// Add "WP-CLI Command" to individual CLI command pages.
elseif ( is_singular( 'command' ) ) {
$title .= " $sep WP-CLI Command";
}
// Add a page number if necessary:
if ( isset( $parts['page'] ) && $parts['page'] >= 2 ) {
$title .= " $sep " . sprintf( __( 'Page %s', 'wporg' ), $parts['page'] );
}
$parts['title'] = $title;
return $parts;
}
/**
* Customizes the document title separator.
*
* @param string $separator Current document title separator.
* @return string
*/
public static function document_title_separator( $separator ) {
return '|';
}
/**
* Outputs tags for the page head.
*/
public static function output_head_tags() {
$fields = [
// FYI: 'description' and 'og:description' are set further down.
'og:title' => wp_get_document_title(),
'og:site_name' => get_bloginfo( 'name' ),
'og:type' => 'website',
'og:url' => home_url( '/' ),
'twitter:card' => 'summary_large_image',
'twitter:site' => '@WordPress',
];
$desc = '';
// Customize description and any other tags.
if ( is_front_page() ) {
$desc = __( 'Official WordPress developer resources including a code reference, handbooks (for APIs, plugin and theme development, block editor), and more.', 'wporg' );
}
elseif ( is_page( 'reference' ) ) {
$desc = __( 'Want to know what’s going on inside WordPress? Find out more information about its functions, classes, methods, and hooks.', 'wporg' );
}
elseif ( DevHub\is_parsed_post_type() ) {
if ( is_singular() ) {
$desc = DevHub\get_summary();
}
elseif ( is_post_type_archive() ) {
$post_type_items = get_post_type_object( get_post_type() )->labels->all_items;
/* translators: %s: translated label for all items of a post type. */
$desc = sprintf( __( 'Code Reference archive for WordPress %s.', 'wporg' ), strtolower( $post_type_items ) );
}
}
elseif ( is_singular() ) {
$post = get_queried_object();
if ( $post ) {
$desc = $post->post_content;
}
}
// Actually set field values for description.
if ( $desc ) {
$desc = wp_strip_all_tags( $desc );
$desc = str_replace( ' ', ' ', $desc );
$desc = preg_replace( '/\s+/', ' ', $desc );
// Trim down to <150 characters based on full words.
if ( strlen( $desc ) > 150 ) {
$truncated = '';
$words = preg_split( "/[\n\r\t ]+/", $desc, -1, PREG_SPLIT_NO_EMPTY );
while ( $words ) {
$word = array_shift( $words );
if ( strlen( $truncated ) + strlen( $word ) >= 141 ) { /* 150 - strlen( ' …' ) */
break;
}
$truncated .= $word . ' ';
}
$truncated = trim( $truncated );
if ( $words ) {
$truncated .= '…';
}
$desc = $truncated;
}
$fields[ 'description' ] = $desc;
$fields[ 'og:description'] = $desc;
}
// Output fields.
foreach ( $fields as $property => $content ) {
$attribute = 0 === strpos( $property, 'og:' ) ? 'property' : 'name';
printf(
'<meta %s="%s" content="%s" />' . "\n",
esc_attr( $attribute ),
esc_attr( $property ),
esc_attr( $content )
);
}
}
} // DevHub_Head
DevHub_Head::init();