Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 67
0.00% covered (danger)
0.00%
0 / 14
CRAP
n/a
0 / 0
wpcom_better_footer_credit_can_customize
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
wpcom_better_footer_credit_apply
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
20
wpcom_better_footer_credit_default
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
42
wpcom_better_footer_credit_url
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
widont
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
20
wido
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
wido_menu_titles
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
wpcom_edit_post_admin_url_redirect
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
72
wpcom_edit_post_admin_url_redirect_add_hook
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
wpcom_edit_post_admin_url_redirect_remove_hook
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
wpcom_new_post_admin_url_redirect
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
20
wpcom_new_post_admin_url_redirect_add_hook
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
wpcom_new_post_admin_url_redirect_remove_hook
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
wpcom_disable_mobile_theme_on_signup
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
30
1<?php // phpcs:ignore Squiz.Commenting.FileComment.Missing
2/**
3 * This file is taken from wp-content/blog-plugins/theme-optimizations.php on wpcom and is split into this and 'footer-credit-optimizations.php'.
4 * The latter will hold all footer-credit-related logic that can run outside of wpcom environment. I hope to port these files back
5 */
6
7use Automattic\Jetpack\Device_Detection\User_Agent_Info;
8
9require_once __DIR__ . '/footer-credit-optimizations.php';
10
11// Footer credit wpcom-related hooks:
12
13/**
14 * Determine whether or not one can customize the footer credit.
15 *
16 * @param bool $previous_value Unused.
17 *
18 * @return bool
19 */
20function wpcom_better_footer_credit_can_customize( $previous_value ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
21    $plan = WPCOM_Store::get_subscribed_bundle_product_id_for_blog();
22    return in_array( $plan, array( WPCOM_BUSINESS_BUNDLE ), true );
23}
24add_filter( 'wpcom_better_footer_credit_can_customize', 'wpcom_better_footer_credit_can_customize', 10, 1 );
25
26/**
27 * Determine whether or not to use a WordPress.com-ified footer credit.
28 *
29 * @param bool $previous_value Unused.
30 *
31 * @return bool
32 */
33function wpcom_better_footer_credit_apply( $previous_value ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
34    return wpcom_is_pub_theme() && ! wpcom_is_premium_theme() && ! wpcom_is_vip_theme() && ! wpcom_is_a8c_theme();
35}
36add_filter( 'wpcom_better_footer_credit_apply', 'wpcom_better_footer_credit_apply', 10, 1 );
37
38/**
39 * Replace footer credit with one specific to WordPress.com.
40 *
41 * @param string      $original Original footer credit (unused).
42 * @param string|null $lang Language (e.g. 'en-za'), or null to detect language.
43 *
44 * @return string WordPress.com-ified footer credit.
45 */
46function wpcom_better_footer_credit_default( $original, $lang ) {
47    // Handle WP.com footer text.
48
49    $_blog_id = get_current_blog_id();
50    $vertical = site_vertical( $_blog_id );
51
52    $noads = defined( 'NOADVERTS' ) || defined( 'NOADSUPGRADE' );
53    if ( $vertical ) {
54        if ( $noads ) {
55            $credit_link = sprintf( '<a href="%s">%s.</a>', localized_wpcom_url( 'https://wordpress.com/' . $vertical . '/?ref=vertical_footer', $lang ), esc_html__( 'Blog at WordPress.com', 'wpcomsh' ) );
56        } else {
57            $credit_link = sprintf( '<a href="%s">%s.</a>', localized_wpcom_url( 'https://wordpress.com/' . $vertical . '/?ref=vertical_footer', $lang ), esc_html__( 'Create a free website at WordPress.com', 'wpcomsh' ) );
58        }
59    } elseif ( $noads || wp_rand( 0, 1 ) ) {
60        $credit_link = sprintf( '<a href="%s">%s.</a>', localized_wpcom_url( 'https://wordpress.com/?ref=footer_blog', $lang ), esc_html__( 'Blog at WordPress.com', 'wpcomsh' ) );
61    } else {
62        $credit_link = sprintf( '<a href="%s">%s.</a>', localized_wpcom_url( 'https://wordpress.com/?ref=footer_website', $lang ), esc_html__( 'Create a free website or blog at WordPress.com', 'wpcomsh' ) );
63    }
64    return $credit_link;
65}
66add_filter( 'wpcom_better_footer_credit_link', 'wpcom_better_footer_credit_default', 10, 2 );
67
68/**
69 * Localize credit URL.
70 *
71 * @param string      $original Original URL.
72 * @param string|null $lang Language (e.g. 'de-ch'), or null to detect language.
73 *
74 * @return string Localized URL.
75 */
76function wpcom_better_footer_credit_url( $original, $lang ) {
77    return localized_wpcom_url( $original, $lang );
78}
79add_filter( 'wpcom_better_footer_credit_url', 'wpcom_better_footer_credit_url', 10, 2 );
80
81/**
82 * Prevent widows in post titles
83 *
84 * Isn't applied when the user is on a non-tablet mobile device, the title contains three or fewer words, or words are over a certain (filterable) length.
85 *
86 * A multi-byte aware re-implementation of this - http://www.shauninman.com/post/heap/2006/08/22/widont_wordpress_plugin
87 *
88 * DEVELOPER NOTE: Please run the units at bin/tests/content-filters/widont.php when updating this function!
89 *
90 * @param string $str Text content.
91 * @uses jetpack_is_mobile()
92 * @uses User_Agent_Info::is_tablet()
93 * @uses apply_filters()
94 * @filter the_title
95 * @return string
96 */
97function widont( $str = '' ) {
98    // Don't apply on non-tablet mobile devices so the browsers can fit to the viewport properly.
99    $ua = new User_Agent_Info();
100    if ( jetpack_is_mobile() && ! $ua->is_tablet() ) {
101        return $str;
102    }
103
104    // We're dealing with whitespace from here out, let's not have any false positives. :)
105    $str = trim( $str );
106
107    // If string contains three or fewer words, don't join.
108    if ( count( preg_split( '#\s+#', $str ) ) <= 3 ) {
109        return $str;
110    }
111
112    // Don't join if words exceed a certain length: minimum 10 characters, default 15 characters, filterable via `widont_max_word_length`.
113    $widont_max_word_length = max( 10, absint( apply_filters( 'widont_max_word_length', 15 ) ) );
114
115    $regex = '#\s+(\P{Z}{1,' . $widont_max_word_length . '})\s+(\P{Z}{1,' . $widont_max_word_length . '})$#u';
116
117    return preg_replace( $regex, ' $1&nbsp;$2', $str );
118}
119add_filter( 'the_title', 'widont' );
120
121/**
122 * Replace extra non-breaking spaces (`&#160;`) with literal spaces.
123 *
124 * @param string $str Text content.
125 *
126 * @return string Replaced text content.
127 */
128function wido( $str = '' ) {
129    return str_replace( '&#160;', ' ', $str );
130}
131add_filter( 'the_title_rss', 'wido' );
132
133/**
134 * Replace extra non-breaking spaces (`&nbsp;`) with literal spaces.
135 * Used in menu titles - see #1718-wpcom
136 *
137 * @param string $content Text content.
138 *
139 * @return string Replaced text content.
140 */
141function wido_menu_titles( $content ) {
142    return str_replace( '&nbsp;', ' ', $content );
143}
144add_filter( 'walker_nav_menu_start_el', 'wido_menu_titles' );
145
146/**
147 * Filter links to the Edit Post screen in the admin.
148 *
149 * These links now point to WP Admin, but we want users to be redirected to the new WP.com editor.
150 *
151 * @param string $url URL.
152 * @param int    $post_id Post ID.
153 *
154 * @return string
155 */
156function wpcom_edit_post_admin_url_redirect( $url, $post_id ) {
157    $_blog_id = get_current_blog_id();
158
159    $post = get_post( $post_id );
160    if ( ! $post ) {
161        return $url;
162    }
163
164    if ( wpcom_is_vip( $_blog_id ) || ( is_super_admin() && ! is_user_member_of_blog( get_current_user_id(), $_blog_id ) ) ) {
165        return $url;
166    }
167
168    $post_type = $post->post_type;
169
170    if ( in_array( $post_type, array( 'post', 'page' ), true ) ) {
171        $path_prefix = $post_type;
172    } elseif ( in_array( $post_type, apply_filters( 'rest_api_allowed_post_types', array( 'post', 'page', 'revision' ) ), true ) ) {
173        $path_prefix = sprintf( 'edit/%s', $post_type );
174    }
175
176    if ( ! isset( $path_prefix ) ) {
177        return $url;
178    }
179
180    $site_slug = WPCOM_Masterbar::get_calypso_site_slug( $_blog_id );
181    return esc_url_raw( sprintf( 'https://wordpress.com/%s/%s/%d', $path_prefix, $site_slug, $post_id ) );
182}
183
184/**
185 * Add edit post admin URL redirect hook.
186 */
187function wpcom_edit_post_admin_url_redirect_add_hook() {
188    add_filter( 'get_edit_post_link', 'wpcom_edit_post_admin_url_redirect', 10, 2 );
189}
190add_action( 'wp_head', 'wpcom_edit_post_admin_url_redirect_add_hook' );
191
192/**
193 * Remove edit post admin URL redirect hook.
194 */
195function wpcom_edit_post_admin_url_redirect_remove_hook() {
196    remove_filter( 'get_edit_post_link', 'wpcom_edit_post_admin_url_redirect', 10 );
197}
198add_action( 'wp_footer', 'wpcom_edit_post_admin_url_redirect_remove_hook' );
199
200/**
201 * Filter links to the New Post screen in the admin.
202 *
203 * These links now point to WP Admin, but we want users to be redirected to the new WP.com editor.
204 *
205 * @param string $url URL.
206 * @param string $path Path.
207 *
208 * @return string
209 */
210function wpcom_new_post_admin_url_redirect( $url, $path ) {
211    if ( 'post-new.php' !== $path || ( is_super_admin() && ! is_user_member_of_blog() ) ) {
212        return $url;
213    }
214
215    $site_slug = WPCOM_Masterbar::get_calypso_site_slug( get_current_blog_id() );
216    return esc_url_raw( sprintf( 'https://wordpress.com/post/%s', $site_slug ) );
217}
218
219/**
220 * Add new post admin URL redirect hook.
221 */
222function wpcom_new_post_admin_url_redirect_add_hook() {
223    add_filter( 'admin_url', 'wpcom_new_post_admin_url_redirect', 10, 2 );
224}
225add_action( 'wp_head', 'wpcom_new_post_admin_url_redirect_add_hook' );
226
227/**
228 * Remove new post admin URL redirect hook.
229 */
230function wpcom_new_post_admin_url_redirect_remove_hook() {
231    remove_filter( 'admin_url', 'wpcom_new_post_admin_url_redirect', 10 );
232}
233add_action( 'wp_footer', 'wpcom_new_post_admin_url_redirect_remove_hook' );
234
235/**
236 * When a default theme is enabled at signup (new blog creation), ensure that
237 * it's mobile setting is turned off -- if the theme is responsive.
238 *
239 * See track_signup_default_theme() in wp-content/mu-plugins/usage.php
240 *
241 * @param int $_blog_id Blog ID.
242 * @return void
243 */
244function wpcom_disable_mobile_theme_on_signup( $_blog_id ) {
245    $stylesheet = get_blog_option( (int) $_blog_id, 'stylesheet' );
246
247    // Avoid processing for signups that aren't new, like blog name changes.
248    if ( empty( $stylesheet ) || 'dashboard' === $stylesheet || 'h4' === $stylesheet ) {
249        return;
250    }
251
252    // Disable mobile theme.
253    if ( in_array( 'responsive-layout', (array) wp_get_theme( $stylesheet )->get( 'Tags' ), true ) ) {
254        update_blog_option( $_blog_id, 'wp_mobile_disable', 1 );
255    }
256}
257add_action( 'wpmu_new_blog', 'wpcom_disable_mobile_theme_on_signup', 10, 1 );