1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Create an output buffer to capture the HTML content of the footer.php theme |
5
|
|
|
* template. Used to change links, text, run a/b tests. Etc. |
6
|
|
|
* |
7
|
|
|
* @param string $page The HTML content from footer.php template file. |
8
|
|
|
* @return string $page HTML content. |
9
|
|
|
*/ |
10
|
|
|
function wpcom_better_footer_links_buffer( $page ) { |
11
|
|
|
// Only add theme and colophon links for pub and premium themes, and VIP "partner" themes. |
12
|
|
|
if ( ! wpcom_is_pub_theme() && ! wpcom_is_premium_theme() && ! wpcom_is_vip_theme() && ! wpcom_is_a8c_theme() ) { |
13
|
|
|
return $page; |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
// Would like to only see footer content before wp_footer output. |
17
|
|
|
$output = preg_split( '/wpcom_wp_footer/i', $page, 2 ); |
18
|
|
|
|
19
|
|
|
// Run "better link" filters. |
20
|
|
|
$footer = wpcom_better_footer_links( $output[0] ); |
21
|
|
|
|
22
|
|
|
// Piece back together again. |
23
|
|
|
$page = implode( array( $footer, 'wpcom_wp_footer' . $output[1] ) ); |
24
|
|
|
|
25
|
|
|
// If nothing to join, return empty string. |
26
|
|
|
if ( 'wpcom_wp_footer' === $page ) { |
27
|
|
|
return ''; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
// Replace any dangling references of glue code. |
31
|
|
|
$page = preg_replace( '/wpcom_wp_footer/i', '', $page ); |
32
|
|
|
|
33
|
|
|
return $page; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Better WP.com footer links. |
38
|
|
|
* |
39
|
|
|
* 1. Replace default "Powered by WordPress" text and link with |
40
|
|
|
* a link to WordPress.com and custom call-out text. |
41
|
|
|
* |
42
|
|
|
* 2. Replace theme name in footer with a link to relevant theme page on the Theme Showcase. |
43
|
|
|
* URL structure: http://theme.wordpress.com/themes/{theme-slug}/ |
44
|
|
|
* |
45
|
|
|
* 3. Link to the Vertical landing page for sites that are stickered with a vertical. |
46
|
|
|
* |
47
|
|
|
* @param string $footer Footer HTML content to filter. |
48
|
|
|
* @return string $footer Filtered HTML content. |
49
|
|
|
*/ |
50
|
|
|
function wpcom_better_footer_links( $footer ) { |
51
|
|
|
// Only add theme and colophon links for pub and premium themes, and VIP "partner" themes. |
52
|
|
|
if ( ! wpcom_is_pub_theme() && ! wpcom_is_premium_theme() && ! wpcom_is_vip_theme() && ! wpcom_is_a8c_theme() ) { |
53
|
|
|
return $footer; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
// Get current theme data. |
57
|
|
|
$theme = wp_get_theme(); |
58
|
|
|
|
59
|
|
|
// Replace separator from content, since we are replacing theme and designer credits. |
60
|
|
|
// Any span separator with a .sep class will be matched and replaced by the regular expression. |
61
|
|
|
$footer = preg_replace("/\s\|\s(?=\<a)|\<span class=\"([^\"]+\s)?sep(\s[^\"]+)?\">.*<\/span>/i", '', $footer); |
62
|
|
|
|
63
|
|
|
// Handle WP.com footer text. |
64
|
|
|
$lang = get_bloginfo( 'language' ); |
65
|
|
|
|
66
|
|
|
$_blog_id = get_current_blog_id(); |
67
|
|
|
$vertical = site_vertical( $_blog_id ); |
68
|
|
|
|
69
|
|
|
$noads = defined( 'NOADVERTS' ) || defined( 'NOADSUPGRADE' ); |
70
|
|
|
if ( $vertical ) { |
71
|
|
|
if ( $noads ) { |
72
|
|
|
$credit_link = sprintf( '<a href="%s">%s.</a>', localized_wpcom_url( 'https://wordpress.com/' . $vertical . '/?ref=vertical_footer', $lang ), __( 'Blog at WordPress.com' ) ); |
73
|
|
|
} else { |
74
|
|
|
$credit_link = sprintf( '<a href="%s">%s.</a>', localized_wpcom_url( 'https://wordpress.com/' . $vertical . '/?ref=vertical_footer', $lang ), __( 'Create a free website at WordPress.com' ) ); |
75
|
|
|
} |
76
|
|
|
} else if ( $noads || mt_rand( 0, 1 ) ) { |
77
|
|
|
$credit_link = sprintf( '<a href="%s">%s.</a>', localized_wpcom_url( 'https://wordpress.com/?ref=footer_blog', $lang ), __( 'Blog at WordPress.com' ) ); |
78
|
|
|
} else { |
79
|
|
|
$credit_link = sprintf( '<a href="%s">%s.</a>', localized_wpcom_url( 'https://wordpress.com/?ref=footer_website', $lang ), __( 'Create a free website or blog at WordPress.com' ) ); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
// Replace credit link in footer, and make sure it is replaced only once, to avoid duplicates. |
83
|
|
|
$credit_link = apply_filters( 'wpcom_better_footer_credit_link', $credit_link, $lang ); |
84
|
|
|
|
85
|
|
|
// The regular expression to match the credit link replacement. |
86
|
|
|
$credit_regex = implode( '', array( |
87
|
|
|
'#' , // Open delimiter |
88
|
|
|
'<a[^>]*href="http(s?)://(([a-z]{2}|www)\.)?(wordpress|wordpress-fr|wpfr)\.(com|org|net)/?"([^>]+)?>' , // Opening link tag |
89
|
|
|
'\s*' , // Optional whitespace |
90
|
|
|
'(.+?)' , // Any word or sentence |
91
|
|
|
'\s*' , // Optional whitespace |
92
|
|
|
'</a>' , // Closing link tag |
93
|
|
|
'\.?' , // Optional period |
94
|
|
|
'(\s*&[^;]+;\s*)?' , // Optional HTML Entity |
95
|
|
|
'#i' , // Ending delimiter & modifier |
96
|
|
|
) ); |
97
|
|
|
|
98
|
|
|
// Add filter for specific themes that may need to tweak the regex a bit. |
99
|
|
|
$credit_regex = apply_filters( 'wpcom_better_footer_credit_regex', $credit_regex, $theme ); |
100
|
|
|
|
101
|
|
|
// Get the full matches of the credit regular expression, proceed if match. |
102
|
|
|
if ( preg_match_all( $credit_regex, $footer, $matches, PREG_OFFSET_CAPTURE ) ) { |
103
|
|
|
|
104
|
|
|
// Get last match and offset. |
105
|
|
|
$match = array_pop( $matches[0] ); |
106
|
|
|
$offset = $match[1]; |
107
|
|
|
|
108
|
|
|
// Split the content into two parts, which we will join later on. |
109
|
|
|
$before = substr( $footer, 0, $offset ); |
110
|
|
|
$after = substr( $footer, $offset ); |
111
|
|
|
|
112
|
|
|
// Replace on the last part. Ensure we only do one replacement to avoid duplicates. |
113
|
|
|
$after = preg_replace( $credit_regex, $credit_link, $after, 1 ); |
114
|
|
|
|
115
|
|
|
// Join the two parts. |
116
|
|
|
$footer = $before . $after; |
117
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
// Themes that have duplicate footer credit links (e.g. "Powered by WordPress.com" + another credit link). |
121
|
|
|
$powered_by_themes = array( |
122
|
|
|
'pub/toujours', |
123
|
|
|
); |
124
|
|
|
|
125
|
|
|
// Remove "Proudly powered by WordPress" on selected themes. |
126
|
|
|
if ( in_array( $theme->stylesheet, $powered_by_themes ) ) { |
127
|
|
|
$powered_string = preg_quote( __( 'Proudly powered by WordPress' ), '#' ); |
128
|
|
|
$powered_regex = sprintf( '#<a[^>]*href="http(s?)://(([a-z]{2}|www)\.)?wordpress\.(com|org)/?"([^>]+)?>%s</a>\.?#i', $powered_string ); |
129
|
|
|
$footer = preg_replace( $powered_regex, '', $footer ); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
// Only add theme and colophon link for pub and premium themes. |
133
|
|
|
if ( wpcom_is_vip_theme() ) { |
134
|
|
|
return $footer; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
// Handle adding Theme Name and colophon link to footer text. |
138
|
|
|
$theme_match = sprintf( |
139
|
|
|
'(?:\s*\|\s*)?' . // Optional pipe with spaces (non-capturing) |
140
|
|
|
'(?:<span\s[^>]+>)?' . // Optional opening span tag (non-capturing) |
141
|
|
|
'(Theme|%s)' . // $1: "Theme" or the localized equivalent |
142
|
|
|
'\s*(?: )?:\s*' . // Zero or more whitespace characters, a colon, zero or more whitespace characters |
143
|
|
|
'(%s|<a[^>]+>%s</a>)' . // $2: The theme name, or link |
144
|
|
|
'\.?' . // Optional period |
145
|
|
|
'(?:</span>)?' . // Optional closing span tag (non-capturing) |
146
|
|
|
'\.?' // Optional period |
147
|
|
|
|
148
|
|
|
, preg_quote( __( 'Theme' ), '#' ) |
149
|
|
|
, preg_quote( $theme->name, '#' ) |
150
|
|
|
, preg_quote( $theme->name, '#' ) |
151
|
|
|
); |
152
|
|
|
|
153
|
|
|
// Theme designer match. |
154
|
|
|
$designer_match = $theme_match . sprintf( |
155
|
|
|
'(' . // Start $3 |
156
|
|
|
'\s*' . // Zero or more whitespace characters |
157
|
|
|
'(?:<span\s[^>]+>)?' . // Optional opening span tag (non-capturing) |
158
|
|
|
'(?:by|%s)' . // "by" or the localized equivalent (non-capturing) |
159
|
|
|
'(?:</span>)?' . // Optional closing span tag (non-capturing) |
160
|
|
|
'\s*' . // Zero or more whitespace characters |
161
|
|
|
'(<a[^>]+>.+?</a>)?' . // $4: Maybe a full <a> element |
162
|
|
|
')' . // End $3 |
163
|
|
|
'\.?' // Optional period |
164
|
|
|
|
165
|
|
|
, preg_quote( __( 'by' ), '#' ) // localized "by" preposition |
166
|
|
|
); |
167
|
|
|
|
168
|
|
|
// Match "Design by <shop>". |
169
|
|
|
$design_by = preg_quote( $credit_link, '#' ) . sprintf( |
170
|
|
|
'\.?' . // Optional period |
171
|
|
|
'\s*' . // Optional whitespace |
172
|
|
|
'(Design by|%s)' . // "Design by" or localized equivalent |
173
|
|
|
'\s*' . // Optional whitespace |
174
|
|
|
'(<a[^>]+>.+?</a>)' . // Full link element |
175
|
|
|
'\.?' // Optional period |
176
|
|
|
|
177
|
|
|
, preg_quote( __( 'Design by' ), '#' ) |
178
|
|
|
); |
179
|
|
|
|
180
|
|
|
if ( preg_match( "#$designer_match#i", $footer ) ) { |
181
|
|
|
$footer = preg_replace( "#$designer_match#i", '', $footer, 1 ); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
if ( preg_match( "#$theme_match#i", $footer ) ) { |
185
|
|
|
$footer = preg_replace( "#$theme_match#i", '', $footer, 1 ); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
if ( preg_match( "#$design_by#i", $footer ) ) { |
189
|
|
|
$footer = preg_replace( "#$design_by#i", $credit_link, $footer, 1 ); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
return $footer; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
// Enable filters for footer content for all themes, except VIP sites. |
196
|
|
|
function better_wpcom_link_init() { |
197
|
|
|
if ( ! wpcom_is_vip() ) |
198
|
|
|
ob_start( 'wpcom_better_footer_links_buffer' ); |
199
|
|
|
} |
200
|
|
|
add_action( 'get_footer', 'better_wpcom_link_init' ); |
201
|
|
|
|
202
|
|
|
// Enable filters on those themes that need special treatment. |
203
|
|
|
function better_wpcom_link_workarounds_init() { |
204
|
|
|
if ( function_exists( 'blogly_widgets_init' ) && 'premium/blogly' === wp_get_theme()->stylesheet ) { |
205
|
|
|
add_action( 'get_sidebar', 'better_wpcom_link_init' ); |
206
|
|
|
} |
207
|
|
|
if ( function_exists( 'designer_widgets_init' ) && 'premium/designer' === wp_get_theme()->stylesheet ) { |
208
|
|
|
add_action( 'get_header', 'better_wpcom_link_init' ); |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
add_action( 'init', 'better_wpcom_link_workarounds_init' ); |
212
|
|
|
|
213
|
|
|
// Enable filters Infinite Scroll footer conntent, except VIP sites. |
214
|
|
|
//if ( ! wpcom_is_vip() ) { |
215
|
|
|
add_filter( 'infinite_scroll_credit', 'wpcom_better_footer_links' ); |
216
|
|
|
//} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Filters the default footer credits regex. |
220
|
|
|
* |
221
|
|
|
* @param string $credit_regex The regular expression for the footer credit. |
222
|
|
|
* @param object $theme The object returned by `wp_get_theme()` |
223
|
|
|
* @return string |
224
|
|
|
*/ |
225
|
|
|
function wpcom_better_footer_credit_regex_filter( $credit_regex, $theme ) { |
226
|
|
|
// Twotone renders the social menu after the credit links. If there is a WordPress.com link in the menu, |
227
|
|
|
// it will break the footer credits. Adding a space before the actual link fixes this. |
228
|
|
|
if ( 'premium/twotone' === $theme->stylesheet ) { |
229
|
|
|
$credit_regex = str_replace( '#<a', '#\s<a', $credit_regex ); |
230
|
|
|
} |
231
|
|
|
return $credit_regex; |
232
|
|
|
} |
233
|
|
|
add_filter( 'wpcom_better_footer_credit_regex', 'wpcom_better_footer_credit_regex_filter', 10, 2 ); |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Add an HTML comment flag for wp_footer output so that our footer replacement |
237
|
|
|
* script knows when to stop looking for more footer content. |
238
|
|
|
*/ |
239
|
|
|
function wpcom_footer_html_comment_flag() { |
240
|
|
|
echo "<!-- wpcom_wp_footer -->\n"; |
241
|
|
|
} |
242
|
|
|
add_action( 'wp_footer', 'wpcom_footer_html_comment_flag', 9 ); |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Add theme name to Twenty Ten footer |
246
|
|
|
*/ |
247
|
|
|
function wpcomthemes_twentyten_credits() { |
248
|
|
|
echo 'Theme: Twenty Ten'; // leave untranslated for regex match, will be translated in final output |
249
|
|
|
} |
250
|
|
|
add_action( 'twentyten_credits', 'wpcomthemes_twentyten_credits' ); |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Add theme name to Twenty Eleven footer |
254
|
|
|
*/ |
255
|
|
|
function wpcomthemes_twentyeleven_credits() { |
256
|
|
|
echo 'Theme: Twenty Eleven <span class="sep"> | </span>'; // leave untranslated for regex match, will be translated in final output |
257
|
|
|
} |
258
|
|
|
add_action( 'twentyeleven_credits', 'wpcomthemes_twentyeleven_credits' ); |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Add theme name to Twenty Twelve footer |
262
|
|
|
*/ |
263
|
|
|
function wpcomthemes_twentytwelve_credits() { |
264
|
|
|
echo 'Theme: Twenty Twelve.'; // leave untranslated for regex match, will be translated in final output |
265
|
|
|
} |
266
|
|
|
add_action( 'twentytwelve_credits', 'wpcomthemes_twentytwelve_credits' ); |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Add theme name to Twenty Thirteen footer |
270
|
|
|
*/ |
271
|
|
|
function wpcomthemes_twentythirteen_credits() { |
272
|
|
|
echo 'Theme: Twenty Thirteen.'; // leave untranslated for regex match, will be translated in final output |
273
|
|
|
} |
274
|
|
|
add_action( 'twentythirteen_credits', 'wpcomthemes_twentythirteen_credits' ); |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Add theme name to Twenty Fourteen footer |
278
|
|
|
*/ |
279
|
|
|
function wpcomthemes_twentyfourteen_credits() { |
280
|
|
|
echo 'Theme: Twenty Fourteen.'; // leave untranslated for regex match, will be translated in final output |
281
|
|
|
} |
282
|
|
|
add_action( 'twentyfourteen_credits', 'wpcomthemes_twentyfourteen_credits' ); |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Add theme name to Twenty Fifteen footer |
286
|
|
|
*/ |
287
|
|
|
function wpcomthemes_twentyfifteen_credits() { |
288
|
|
|
echo 'Theme: Twenty Fifteen.'; // leave untranslated for regex match, will be translated in final output |
289
|
|
|
} |
290
|
|
|
add_action( 'twentyfifteen_credits', 'wpcomthemes_twentyfifteen_credits' ); |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Add theme name to Twenty Sixteen footer |
294
|
|
|
*/ |
295
|
|
|
function wpcomthemes_twentysixteen_credits() { |
296
|
|
|
echo 'Theme: Twenty Sixteen.'; // leave untranslated for regex match, will be translated in final output |
297
|
|
|
} |
298
|
|
|
add_action( 'twentysixteen_credits', 'wpcomthemes_twentysixteen_credits' ); |
299
|
|
|
|
300
|
|
|
|