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