1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* An SEO expert walks into a bar, bars, pub, public house, Irish pub, drinks, beer, wine, liquor, Grey Goose, Cristal... |
5
|
|
|
*/ |
6
|
|
|
class Jetpack_SEO { |
7
|
|
|
public function __construct() { |
8
|
|
|
add_action( 'init', array( $this, 'init' ) ); |
9
|
|
|
} |
10
|
|
|
|
11
|
|
|
public function init() { |
12
|
|
|
/** |
13
|
|
|
* Can be used to prevent SEO tools from inserting custom meta tags. |
14
|
|
|
* |
15
|
|
|
* @module seo-tools |
16
|
|
|
* |
17
|
|
|
* @since 4.4.0 |
18
|
|
|
* |
19
|
|
|
* @param bool true Should Jetpack's SEO Meta Tags be enabled. Defaults to true. |
20
|
|
|
*/ |
21
|
|
|
if ( apply_filters( 'jetpack_seo_meta_tags_enabled', true ) ) { |
22
|
|
|
add_action( 'wp_head', array( $this, 'meta_tags' ) ); |
23
|
|
|
|
24
|
|
|
// Add support for editing page excerpts in pages, regardless of theme support. |
25
|
|
|
add_post_type_support( 'page', 'excerpt' ); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Can be used to prevent SEO tools form modifying site titles. |
30
|
|
|
* |
31
|
|
|
* @module seo-tools |
32
|
|
|
* |
33
|
|
|
* @since 4.4.0 |
34
|
|
|
* |
35
|
|
|
* @param bool true Should Jetpack SEO modify site titles. Defaults to true. |
36
|
|
|
*/ |
37
|
|
|
if ( apply_filters( 'jetpack_seo_custom_titles', true ) ) { |
38
|
|
|
// Overwrite page title with custom SEO meta title for themes that support title-tag. |
39
|
|
|
add_filter( 'pre_get_document_title', array( 'Jetpack_SEO_Titles', 'get_custom_title' ) ); |
40
|
|
|
|
41
|
|
|
// Add overwrite support for themes that don't support title-tag. |
42
|
|
|
add_filter( 'wp_title', array( 'Jetpack_SEO_Titles', 'get_custom_title' ) ); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
add_filter( 'jetpack_open_graph_tags', array( $this, 'set_custom_og_tags' ) ); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
private function get_authors() { |
49
|
|
|
global $wp_query; |
50
|
|
|
|
51
|
|
|
$authors = array(); |
52
|
|
|
|
53
|
|
|
foreach ( $wp_query->posts as $post ) { |
54
|
|
|
$authors[] = get_the_author_meta( 'display_name', (int) $post->post_author ); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$authors = array_unique( $authors ); |
58
|
|
|
|
59
|
|
|
return $authors; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function set_custom_og_tags( $tags ) { |
63
|
|
|
$custom_title = Jetpack_SEO_Titles::get_custom_title(); |
64
|
|
|
|
65
|
|
|
if ( ! empty( $custom_title ) ) { |
66
|
|
|
$tags['og:title'] = $custom_title; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$post_custom_description = Jetpack_SEO_Posts::get_post_custom_description( get_post() ); |
70
|
|
|
$front_page_meta = Jetpack_SEO_Utils::get_front_page_meta_description(); |
71
|
|
|
|
72
|
|
|
if ( is_front_page() && ! empty( $front_page_meta ) ) { |
73
|
|
|
$tags['og:description'] = $front_page_meta; |
74
|
|
|
} else { |
75
|
|
|
if ( ! empty( $post_custom_description ) ) { |
76
|
|
|
$tags['og:description'] = $post_custom_description; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $tags; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function meta_tags() { |
84
|
|
|
global $wp_query; |
85
|
|
|
|
86
|
|
|
$period = ''; |
87
|
|
|
$template = ''; |
88
|
|
|
$meta = array(); |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Can be used to specify a list of themes that set their own meta tags. |
92
|
|
|
* |
93
|
|
|
* If current site is using one of the themes listed as conflicting, inserting Jetpack SEO |
94
|
|
|
* meta tags will be prevented. |
95
|
|
|
* |
96
|
|
|
* @module seo-tools |
97
|
|
|
* |
98
|
|
|
* @since 4.4.0 |
99
|
|
|
* |
100
|
|
|
* @param array List of conflicted theme names. Defaults to empty array. |
101
|
|
|
*/ |
102
|
|
|
$conflicted_themes = apply_filters( 'jetpack_seo_meta_tags_conflicted_themes', array() ); |
103
|
|
|
|
104
|
|
|
if ( isset( $conflicted_themes[ get_option( 'template' ) ] ) ) { |
105
|
|
|
return; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Can be used to insert custom site host that will used for meta title. |
110
|
|
|
* |
111
|
|
|
* @module seo-tools |
112
|
|
|
* |
113
|
|
|
* @since 4.4.0 |
114
|
|
|
* |
115
|
|
|
* @param string Name of the site host. Defaults to empty string. |
116
|
|
|
*/ |
117
|
|
|
$site_host = apply_filters( 'jetpack_seo_site_host', '' ); |
|
|
|
|
118
|
|
|
|
119
|
|
|
$meta['title'] = sprintf( _x( '%1$s', 'Site Title', 'jetpack' ), get_bloginfo( 'title' ) ); |
120
|
|
|
|
121
|
|
|
if ( ! empty( $site_host ) ) { |
122
|
|
|
$meta['title'] = sprintf( |
123
|
|
|
_x( '%1$s on %2$s', 'Site Title on WordPress', 'jetpack' ), |
124
|
|
|
get_bloginfo( 'title' ), |
125
|
|
|
$site_host |
126
|
|
|
); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$front_page_meta = Jetpack_SEO_Utils::get_front_page_meta_description(); |
130
|
|
|
$description = $front_page_meta ? $front_page_meta : get_bloginfo( 'description' ); |
131
|
|
|
$meta['description'] = trim( $description ); |
132
|
|
|
|
133
|
|
|
// Try to target things if we're on a "specific" page of any kind. |
134
|
|
|
if ( is_singular() ) { |
135
|
|
|
$meta['title'] = sprintf( |
136
|
|
|
_x( '%1$s | %2$s', 'Post Title | Site Title on WordPress', 'jetpack' ), |
137
|
|
|
get_the_title(), |
138
|
|
|
$meta['title'] |
139
|
|
|
); |
140
|
|
|
|
141
|
|
|
// Business users can overwrite the description. |
142
|
|
|
if ( ! ( is_front_page() && Jetpack_SEO_Utils::get_front_page_meta_description() ) ) { |
143
|
|
|
$description = Jetpack_SEO_Posts::get_post_description( get_post() ); |
144
|
|
|
|
145
|
|
|
if ( $description ) { |
146
|
|
|
$description = wp_trim_words( strip_shortcodes( wp_kses( $description, array() ) ) ); |
147
|
|
|
$meta['description'] = $description; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
} elseif ( is_author() ) { |
152
|
|
|
$obj = get_queried_object(); |
153
|
|
|
|
154
|
|
|
$meta['title'] = sprintf( |
155
|
|
|
_x( 'Posts by %1$s | %2$s', 'Posts by Author Name | Blog Title on WordPress', 'jetpack' ), |
156
|
|
|
$obj->display_name, |
157
|
|
|
$meta['title'] |
158
|
|
|
); |
159
|
|
|
|
160
|
|
|
$meta['description'] = sprintf( |
161
|
|
|
_x( 'Read all of the posts by %1$s on %2$s', 'Read all of the posts by Author Name on Blog Title', 'jetpack' ), |
162
|
|
|
$obj->display_name, |
163
|
|
|
get_bloginfo( 'title' ) |
164
|
|
|
); |
165
|
|
|
} elseif ( is_tag() || is_category() || is_tax() ) { |
166
|
|
|
$obj = get_queried_object(); |
167
|
|
|
|
168
|
|
|
$meta['title'] = sprintf( |
169
|
|
|
_x( 'Posts about %1$s on %2$s', 'Posts about Category on Blog Title', 'jetpack' ), |
170
|
|
|
single_term_title( '', false ), |
171
|
|
|
get_bloginfo( 'title' ) |
172
|
|
|
); |
173
|
|
|
|
174
|
|
|
$description = get_term_field( 'description', $obj->term_id, $obj->taxonomy, 'raw' ); |
175
|
|
|
|
176
|
|
|
if ( ! is_wp_error( $description ) && '' != $description ) { |
177
|
|
|
$meta['description'] = wp_trim_words( $description ); |
178
|
|
|
} else { |
179
|
|
|
$authors = $this->get_authors(); |
180
|
|
|
|
181
|
|
|
$meta['description'] = wp_sprintf( |
182
|
|
|
_x( 'Posts about %1$s written by %2$l', 'Posts about Category written by John and Bob', 'jetpack' ), |
183
|
|
|
single_term_title( '', false ), |
184
|
|
|
$authors |
185
|
|
|
); |
186
|
|
|
} |
187
|
|
|
} elseif ( is_date() ) { |
188
|
|
|
if ( is_year() ) { |
189
|
|
|
$period = get_query_var( 'year' ); |
|
|
|
|
190
|
|
|
|
191
|
|
|
$template = _nx( |
192
|
|
|
'%1$s post published by %2$l in the year %3$s', // singular |
193
|
|
|
'%1$s posts published by %2$l in the year %3$s', // plural |
194
|
|
|
count( $wp_query->posts ), // number |
195
|
|
|
'10 posts published by John in the year 2012', // context |
196
|
|
|
'jetpack' |
197
|
|
|
); |
198
|
|
|
} elseif ( is_month() ) { |
199
|
|
|
$period = date( 'F Y', mktime( 0, 0, 0, get_query_var( 'monthnum' ), 1, get_query_var( 'year' ) ) ); |
|
|
|
|
200
|
|
|
|
201
|
|
|
$template = _nx( |
202
|
|
|
'%1$s post published by %2$l during %3$s', // singular |
203
|
|
|
'%1$s posts published by %2$l during %3$s', // plural |
204
|
|
|
count( $wp_query->posts ), // number |
205
|
|
|
'10 posts publishes by John during May 2012', // context |
206
|
|
|
'jetpack' |
207
|
|
|
); |
208
|
|
|
} elseif ( is_day() ) { |
209
|
|
|
$period = date( |
|
|
|
|
210
|
|
|
'F j, Y', |
211
|
|
|
mktime( 0, 0, 0, get_query_var( 'monthnum' ), get_query_var( 'day' ), get_query_var( 'year' ) ) |
212
|
|
|
); |
213
|
|
|
|
214
|
|
|
$template = _nx( |
215
|
|
|
'%1$s post published by %2$l on %3$s', // singular |
216
|
|
|
'%1$s posts published by %2$l on %3$s', // plural |
217
|
|
|
count( $wp_query->posts ), // number |
218
|
|
|
'10 posts published by John on May 30, 2012', // context |
219
|
|
|
'jetpack' |
220
|
|
|
); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
$meta['title'] = sprintf( |
224
|
|
|
_x( 'Posts from %1$s on %2$s', 'Posts from May 2012 on Blog Title', 'jetpack' ), |
225
|
|
|
$period, |
226
|
|
|
get_bloginfo( 'title' ) |
227
|
|
|
); |
228
|
|
|
|
229
|
|
|
$authors = $this->get_authors(); |
230
|
|
|
$meta['description'] = wp_sprintf( $template, count( $wp_query->posts ), $authors, $period ); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
$custom_title = Jetpack_SEO_Titles::get_custom_title(); |
234
|
|
|
|
235
|
|
|
if ( ! empty( $custom_title ) ) { |
236
|
|
|
$meta['title'] = $custom_title; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Can be used to edit the default SEO tools meta tags. |
241
|
|
|
* |
242
|
|
|
* @module seo-tools |
243
|
|
|
* |
244
|
|
|
* @since 4.4.0 |
245
|
|
|
* |
246
|
|
|
* @param array Array that consists of meta name and meta content pairs. |
247
|
|
|
*/ |
248
|
|
|
$meta = apply_filters( 'jetpack_seo_meta_tags', $meta ); |
249
|
|
|
|
250
|
|
|
// Output them |
251
|
|
|
foreach ( $meta as $name => $content ) { |
252
|
|
|
if ( ! empty( $content ) ) { |
253
|
|
|
echo '<meta name="' . esc_attr( $name ) . '" content="' . esc_attr( $content ) . '" />' . "\n"; |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
|
This check looks for improperly formatted assignments.
Every assignment must have exactly one space before and one space after the equals operator.
To illustrate:
will have no issues, while
will report issues in lines 1 and 2.