|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* Each title format is an array of arrays containing two values: |
|
5
|
|
|
* - type |
|
6
|
|
|
* - value |
|
7
|
|
|
* |
|
8
|
|
|
* Possible values for type are: 'token' and 'string'. |
|
9
|
|
|
* Possible values for 'value' are: any string in case that 'type' is set |
|
10
|
|
|
* to 'string', or allowed token values for page type in case that 'type' |
|
11
|
|
|
* is set to 'token'. |
|
12
|
|
|
* |
|
13
|
|
|
* Examples of valid formats: |
|
14
|
|
|
* |
|
15
|
|
|
* [ |
|
16
|
|
|
* 'front_page' => [ |
|
17
|
|
|
* [ 'type' => 'string', 'value' => 'Front page title and site name:'], |
|
18
|
|
|
* [ 'type' => 'token', 'value' => 'site_name'] |
|
19
|
|
|
* ], |
|
20
|
|
|
* 'posts' => [ |
|
21
|
|
|
* [ 'type' => 'token', 'value' => 'site_name' ], |
|
22
|
|
|
* [ 'type' => 'string', 'value' => ' | ' ], |
|
23
|
|
|
* [ 'type' => 'token', 'value' => 'post_title' ] |
|
24
|
|
|
* ], |
|
25
|
|
|
* 'pages' => [], |
|
26
|
|
|
* 'groups' => [], |
|
27
|
|
|
* 'archives' => [] |
|
28
|
|
|
* ] |
|
29
|
|
|
* Custom title for given page type is created by concatenating all of the array 'value' parts. |
|
30
|
|
|
* Tokens are replaced with their corresponding values for current site. |
|
31
|
|
|
* Empty array signals that we are not overriding the default title for particular page type. |
|
32
|
|
|
*/ |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Class containing utility static methods for managing SEO custom title formats. |
|
36
|
|
|
*/ |
|
37
|
|
|
class Jetpack_SEO_Titles { |
|
38
|
|
|
/** |
|
39
|
|
|
* Site option name used to store custom title formats. |
|
40
|
|
|
*/ |
|
41
|
|
|
const TITLE_FORMATS_OPTION = 'advanced_seo_title_formats'; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Retrieves custom title formats from site option. |
|
45
|
|
|
* |
|
46
|
|
|
* @return array Array of custom title formats, or empty array. |
|
47
|
|
|
*/ |
|
48
|
|
|
public static function get_custom_title_formats() { |
|
49
|
|
|
if( Jetpack_SEO_Utils::is_enabled_jetpack_seo() ) { |
|
50
|
|
|
return get_option( self::TITLE_FORMATS_OPTION, array() ); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return array(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Returns tokens that are currently supported for each page type. |
|
58
|
|
|
* |
|
59
|
|
|
* @return array Array of allowed token strings. |
|
60
|
|
|
*/ |
|
61
|
|
|
public static function get_allowed_tokens() { |
|
62
|
|
|
return array( |
|
63
|
|
|
'front_page' => array( 'site_name', 'tagline' ), |
|
64
|
|
|
'posts' => array( 'site_name', 'tagline', 'post_title' ), |
|
65
|
|
|
'pages' => array( 'site_name', 'tagline', 'page_title' ), |
|
66
|
|
|
'groups' => array( 'site_name', 'tagline', 'group_title' ), |
|
67
|
|
|
'archives' => array( 'site_name', 'tagline', 'date' ), |
|
68
|
|
|
); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Used to modify the default title with custom SEO title. |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $default_title Default title for current page. |
|
75
|
|
|
* |
|
76
|
|
|
* @return string Custom title with replaced tokens or default title. |
|
77
|
|
|
*/ |
|
78
|
|
|
public static function get_custom_title( $default_title = '' ) { |
|
79
|
|
|
// Don't filter title for unsupported themes. |
|
80
|
|
|
if ( self::is_conflicted_theme() ) { |
|
81
|
|
|
return $default_title; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$page_type = self::get_page_type(); |
|
85
|
|
|
|
|
86
|
|
|
// Keep default title if invalid page type is supplied. |
|
87
|
|
|
if ( empty( $page_type ) ) { |
|
88
|
|
|
return $default_title; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$title_formats = self::get_custom_title_formats(); |
|
92
|
|
|
|
|
93
|
|
|
// Keep default title if user has not defined custom title for this page type. |
|
94
|
|
|
if ( empty( $title_formats[ $page_type ] ) ) { |
|
95
|
|
|
return $default_title; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
if ( ! Jetpack_SEO_Utils::is_enabled_jetpack_seo() ) { |
|
99
|
|
|
return $default_title; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$custom_title = ''; |
|
103
|
|
|
$format_array = $title_formats[ $page_type ]; |
|
104
|
|
|
|
|
105
|
|
|
foreach ( $format_array as $item ) { |
|
106
|
|
|
if ( 'token' == $item['type'] ) { |
|
107
|
|
|
$custom_title .= self::get_token_value( $item['value'] ); |
|
108
|
|
|
} else { |
|
109
|
|
|
$custom_title .= $item['value']; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return esc_html( $custom_title ); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Returns string value for given token. |
|
118
|
|
|
* |
|
119
|
|
|
* @param string $token_name The token name value that should be replaced. |
|
120
|
|
|
* |
|
121
|
|
|
* @return string Token replacement for current site, or empty string for unknown token name. |
|
122
|
|
|
*/ |
|
123
|
|
|
public static function get_token_value( $token_name ) { |
|
124
|
|
|
|
|
125
|
|
|
switch ( $token_name ) { |
|
126
|
|
|
case 'site_name': |
|
127
|
|
|
return get_bloginfo( 'name' ); |
|
128
|
|
|
|
|
129
|
|
|
case 'tagline': |
|
130
|
|
|
return get_bloginfo( 'description' ); |
|
131
|
|
|
|
|
132
|
|
|
case 'post_title': |
|
133
|
|
|
case 'page_title': |
|
134
|
|
|
return get_the_title(); |
|
135
|
|
|
|
|
136
|
|
|
case 'group_title': |
|
137
|
|
|
return single_tag_title( '', false ); |
|
138
|
|
|
|
|
139
|
|
|
case 'date': |
|
140
|
|
|
return self::get_date_for_title(); |
|
141
|
|
|
|
|
142
|
|
|
default: |
|
143
|
|
|
return ''; |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Returns page type for current page. We need this helper in order to determine what |
|
149
|
|
|
* user defined title format should be used for custom title. |
|
150
|
|
|
* |
|
151
|
|
|
* @return string|bool Type of current page or false if unsupported. |
|
152
|
|
|
*/ |
|
153
|
|
|
public static function get_page_type() { |
|
154
|
|
|
|
|
155
|
|
|
if ( is_front_page() ) { |
|
156
|
|
|
return 'front_page'; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
if ( is_category() || is_tag() ) { |
|
160
|
|
|
return 'groups'; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
if ( is_archive() && ! is_author() ) { |
|
164
|
|
|
return 'archives'; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
if ( is_page() ) { |
|
168
|
|
|
return 'pages'; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
if ( is_singular() ) { |
|
172
|
|
|
return 'posts'; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
return false; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Returns the value that should be used as a replacement for the date token, |
|
180
|
|
|
* depending on the archive path specified. |
|
181
|
|
|
* |
|
182
|
|
|
* @return string Token replacement for a given date, or empty string if no date is specified. |
|
183
|
|
|
*/ |
|
184
|
|
|
public static function get_date_for_title() { |
|
185
|
|
|
// If archive year, month, and day are specified. |
|
186
|
|
|
if ( is_day() ) { |
|
187
|
|
|
return get_the_date(); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
// If archive year, and month are specified. |
|
191
|
|
|
if ( is_month() ) { |
|
192
|
|
|
return trim( single_month_title( ' ', false ) ); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
// Only archive year is specified. |
|
196
|
|
|
if ( is_year() ) { |
|
197
|
|
|
return get_query_var( 'year' ); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
return ''; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* Checks if current theme is defining custom title that won't work nicely |
|
205
|
|
|
* with our custom SEO title override. |
|
206
|
|
|
* |
|
207
|
|
|
* @return bool True if current theme sets custom title, false otherwise. |
|
208
|
|
|
*/ |
|
209
|
|
|
public static function is_conflicted_theme() { |
|
210
|
|
|
/** |
|
211
|
|
|
* Can be used to specify a list of themes that use their own custom title format. |
|
212
|
|
|
* |
|
213
|
|
|
* If current site is using one of the themes listed as conflicting, |
|
214
|
|
|
* Jetpack SEO custom title formats will be disabled. |
|
215
|
|
|
* |
|
216
|
|
|
* @module seo-tools |
|
217
|
|
|
* |
|
218
|
|
|
* @since 4.4.0 |
|
219
|
|
|
* |
|
220
|
|
|
* @param array List of conflicted theme names. Defaults to empty array. |
|
221
|
|
|
*/ |
|
222
|
|
|
$conflicted_themes = apply_filters( 'jetpack_seo_custom_title_conflicted_themes', array() ); |
|
223
|
|
|
|
|
224
|
|
|
return isset( $conflicted_themes[ get_option( 'template' ) ] ); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* Checks if a given format conforms to predefined SEO title templates. |
|
229
|
|
|
* |
|
230
|
|
|
* Every format type and token must be whitelisted. |
|
231
|
|
|
* @see get_allowed_tokens() |
|
232
|
|
|
* |
|
233
|
|
|
* @param array $title_formats Template of SEO title to check. |
|
234
|
|
|
* |
|
235
|
|
|
* @return bool True if the formats are valid, false otherwise. |
|
236
|
|
|
*/ |
|
237
|
|
|
public static function are_valid_title_formats( $title_formats ) { |
|
238
|
|
|
$allowed_tokens = self::get_allowed_tokens(); |
|
239
|
|
|
|
|
240
|
|
|
if ( ! is_array( $title_formats ) ) { |
|
241
|
|
|
return false; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
foreach ( $title_formats as $format_type => $format_array ) { |
|
245
|
|
|
if ( ! in_array( $format_type, array_keys( $allowed_tokens ) ) ) { |
|
246
|
|
|
return false; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
if ( ! is_array( $format_array ) ) { |
|
250
|
|
|
return false; |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
foreach ( $format_array as $item ) { |
|
254
|
|
|
if ( empty( $item['type'] ) || empty( $item['value'] ) ) { |
|
255
|
|
|
return false; |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
if ( 'token' == $item['type'] ) { |
|
259
|
|
|
if ( ! in_array( $item['value'], $allowed_tokens[ $format_type ] ) ) { |
|
260
|
|
|
return false; |
|
261
|
|
|
} |
|
262
|
|
|
} |
|
263
|
|
|
} |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
return true; |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* Combines the previous values of title formats, stored as array in site options, |
|
271
|
|
|
* with the new values that are provided. |
|
272
|
|
|
* |
|
273
|
|
|
* @param array $new_formats Array containing new title formats. |
|
274
|
|
|
* |
|
275
|
|
|
* @return array $result Array of updated title formats, or empty array if no update was performed. |
|
276
|
|
|
*/ |
|
277
|
|
|
public static function update_title_formats( $new_formats ) { |
|
278
|
|
|
// Empty array signals that custom title shouldn't be used. |
|
279
|
|
|
$empty_formats = array( |
|
280
|
|
|
'front_page' => array(), |
|
281
|
|
|
'posts' => array(), |
|
282
|
|
|
'pages' => array(), |
|
283
|
|
|
'groups' => array(), |
|
284
|
|
|
'archives' => array(), |
|
285
|
|
|
); |
|
286
|
|
|
|
|
287
|
|
|
$previous_formats = self::get_custom_title_formats(); |
|
288
|
|
|
|
|
289
|
|
|
$result = array_merge( $empty_formats, $previous_formats, $new_formats ); |
|
290
|
|
|
|
|
291
|
|
|
if ( update_option( self::TITLE_FORMATS_OPTION, $result ) ) { |
|
292
|
|
|
return $result; |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
return array(); |
|
296
|
|
|
} |
|
297
|
|
|
} |
|
298
|
|
|
|