Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 178
0.00% covered (danger)
0.00%
0 / 10
CRAP
n/a
0 / 0
scossdl_off_get_options
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 1
56
scossdl_off_default_inc_dirs
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
scossdl_off_exclude_match
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
scossdl_string_mod
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
scossdl_off_rewriter
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
42
scossdl_off_additional_directories
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
scossdl_off_filter
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
90
do_scossdl_off_ob_start
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
30
scossdl_off_update
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
56
scossdl_off_options
0.00% covered (danger)
0.00%
0 / 77
0.00% covered (danger)
0.00%
0 / 1
72
1<?php
2
3/* Taken from OSSDL CDN off-linker, a plugin by W-Mark Kubacki and used with permission */
4
5if ( ! isset( $ossdlcdn ) ) {
6    $ossdlcdn = 1; // have to default to on for existing users.
7}
8
9if ( 1 === $ossdlcdn && ! is_admin() ) {
10    add_action( 'init', 'do_scossdl_off_ob_start' );
11}
12
13/**
14 * Set up some defaults.
15 *
16 * @global string $ossdl_off_blog_url
17 * @global string $ossdl_off_cdn_url
18 * @global string $ossdl_cname
19 * @global int    $ossdl_https
20 * @global array  $ossdl_off_include_dirs
21 * @global array  $ossdl_off_excludes
22 * @global array  $ossdl_arr_of_cnames
23 *
24 * @return void
25 */
26function scossdl_off_get_options() {
27    global $ossdl_off_blog_url, $ossdl_off_cdn_url, $ossdl_cname, $ossdl_https;
28    global $ossdl_off_include_dirs, $ossdl_off_excludes, $ossdl_arr_of_cnames;
29
30    $ossdl_off_blog_url = get_option( 'ossdl_off_blog_url' );
31    if ( false === $ossdl_off_blog_url ) {
32        $ossdl_off_blog_url = untrailingslashit( get_site_url() );
33        add_option( 'ossdl_off_blog_url', $ossdl_off_blog_url );
34    }
35
36    if ( has_filter( 'ossdl_off_blog_url' ) ) {
37        $ossdl_off_blog_url = untrailingslashit( apply_filters( 'ossdl_off_blog_url', $ossdl_off_blog_url ) );
38    }
39
40    $ossdl_off_cdn_url = get_option( 'ossdl_off_cdn_url' );
41    if ( false === $ossdl_off_cdn_url ) {
42        $ossdl_off_cdn_url = untrailingslashit( get_site_url() );
43        add_option( 'ossdl_off_cdn_url', $ossdl_off_cdn_url );
44    }
45
46    $include_dirs = get_option( 'ossdl_off_include_dirs' );
47    if ( false !== $include_dirs ) {
48        $ossdl_off_include_dirs = array_filter( array_map( 'trim', explode( ',', $include_dirs ) ) );
49    } else {
50        $ossdl_off_include_dirs = scossdl_off_default_inc_dirs();
51        add_option( 'ossdl_off_include_dirs', implode( ',', $ossdl_off_include_dirs ) );
52    }
53
54    $exclude = get_option( 'ossdl_off_exclude' );
55    if ( false !== $exclude ) {
56        $ossdl_off_excludes = array_filter( array_map( 'trim', explode( ',', $exclude ) ) );
57    } else {
58        $ossdl_off_excludes = array( '.php' );
59        add_option( 'ossdl_off_exclude', implode( ',', $ossdl_off_excludes ) );
60    }
61
62    $ossdl_cname = get_option( 'ossdl_cname' );
63    if ( false !== $ossdl_cname ) {
64        $ossdl_cname = trim( $ossdl_cname );
65    } else {
66        $ossdl_cname = '';
67        add_option( 'ossdl_cname', $ossdl_cname );
68    }
69    $ossdl_arr_of_cnames = array_filter( array_map( 'trim', explode( ',', $ossdl_cname ) ) );
70
71    $ossdl_https = intval( get_option( 'ossdl_https' ) );
72}
73
74/**
75 * Get default directories.
76 *
77 * @return array
78 */
79function scossdl_off_default_inc_dirs() {
80
81    $home_path = trailingslashit( (string) parse_url( get_option( 'siteurl' ), PHP_URL_PATH ) );
82    $inc_dirs  = array();
83
84    foreach ( array( content_url(), includes_url() ) as $dir ) {
85        $dir        = wp_make_link_relative( $dir );
86        $dir        = preg_replace( '`^' . preg_quote( $home_path, '`' ) . '`', '', $dir );
87        $inc_dirs[] = trim( $dir, '/' );
88    }
89
90    return $inc_dirs;
91}
92
93/**
94 * Determines whether to exclude a match.
95 *
96 * @param string $match    URI to examine.
97 * @param array  $excludes Array of "badwords".
98 *
99 * @return boolean true if to exclude given match from rewriting.
100 */
101function scossdl_off_exclude_match( $match, $excludes ) {
102    foreach ( $excludes as $badword ) {
103        if ( false !== stripos( $match, $badword ) ) {
104            return true;
105        }
106    }
107
108    return false;
109}
110
111/**
112 * Compute string modulo, based on SHA1 hash
113 *
114 * @param string $str The string.
115 * @param int    $mod The divisor.
116 *
117 * @return int The remainder.
118 */
119function scossdl_string_mod( $str, $mod ) {
120    /**
121     * The full SHA1 is too large for PHP integer types.
122     * This should be enough for our purpose.
123     */
124    $num = hexdec( substr( sha1( $str ), 0, 5 ) );
125
126    return $num % $mod;
127}
128
129/**
130 * Rewriter of URLs, used as replace-callback.
131 *
132 * Called by #scossdl_off_filter.
133 */
134function scossdl_off_rewriter( $match ) {
135    global $ossdl_off_blog_url, $ossdl_https, $ossdl_off_excludes, $ossdl_arr_of_cnames;
136    static $count_cnames = null, $include_dirs = null;
137
138    // Set up static variables. Run once only.
139    if ( ! isset( $count_cnames ) ) {
140        $count_cnames = count( $ossdl_arr_of_cnames );
141        $include_dirs = scossdl_off_additional_directories();
142    }
143
144    if ( $ossdl_https && str_starts_with( $match[0], 'https' ) ) {
145        return $match[0];
146    }
147
148    if ( scossdl_off_exclude_match( $match[0], $ossdl_off_excludes ) ) {
149        return $match[0];
150    }
151
152    if ( preg_match( '`(' . $include_dirs . ')`', $match[0] ) ) {
153        $offset = scossdl_string_mod( $match[1], $count_cnames );
154        return str_replace( $ossdl_off_blog_url, $ossdl_arr_of_cnames[ $offset ], $match[0] );
155    }
156
157    return $match[0];
158}
159
160/**
161 * Creates a regexp compatible pattern from the directories to be included in matching.
162 *
163 * @return String with the pattern with {@literal |} as prefix, or empty
164 */
165function scossdl_off_additional_directories() {
166    global $ossdl_off_include_dirs;
167
168    $arr_dirs = array();
169    foreach ( $ossdl_off_include_dirs as $dir ) {
170        $arr_dirs[] = preg_quote( trim( $dir ), '`' );
171    }
172
173    return implode( '|', $arr_dirs );
174}
175
176/**
177 * Output filter which runs the actual plugin logic.
178 *
179 * @param  string $content The content of the output buffer.
180 *
181 * @return string The rewritten content.
182 */
183function scossdl_off_filter( $content ) {
184    global $ossdl_off_blog_url, $ossdl_off_cdn_url;
185    global $ossdl_off_include_dirs, $ossdl_off_excludes, $ossdl_arr_of_cnames;
186
187    if ( empty( $content ) || empty( $ossdl_off_cdn_url ) ||
188        $ossdl_off_blog_url === $ossdl_off_cdn_url
189    ) {
190        return $content; // no rewrite needed.
191    }
192
193    if ( empty( $ossdl_off_include_dirs ) || ! is_array( $ossdl_off_include_dirs ) ) {
194        $ossdl_off_include_dirs = scossdl_off_default_inc_dirs();
195    }
196
197    if ( empty( $ossdl_off_excludes ) || ! is_array( $ossdl_off_excludes ) ) {
198        $ossdl_off_excludes = array();
199    }
200
201    if ( ! in_array( $ossdl_off_cdn_url, (array) $ossdl_arr_of_cnames, true ) ) {
202        $ossdl_arr_of_cnames = array_merge( array( $ossdl_off_cdn_url ), (array) $ossdl_arr_of_cnames );
203    }
204
205    $ossdl_arr_of_cnames = apply_filters( 'wpsc_cdn_urls', $ossdl_arr_of_cnames );
206
207    $dirs  = scossdl_off_additional_directories();
208    $regex = '`(?<=[(\"\'])' . preg_quote( $ossdl_off_blog_url, '`' ) . '/(?:((?:' . $dirs . ')[^\"\')]+)|([^/\"\']+\.[^/\"\')]+))(?=[\"\')])`';
209    return preg_replace_callback( $regex, 'scossdl_off_rewriter', $content );
210}
211
212/**
213 * Registers scossdl_off_filter as output buffer, if needed.
214 */
215function do_scossdl_off_ob_start() {
216    global $ossdl_off_blog_url, $ossdl_off_cdn_url;
217
218    if ( class_exists( 'Jetpack' ) && Jetpack::is_module_active( 'photon' ) ) {
219        return;
220    }
221
222    scossdl_off_get_options();
223
224    if ( ! empty( $ossdl_off_cdn_url ) &&
225        $ossdl_off_blog_url !== $ossdl_off_cdn_url
226    ) {
227        add_filter( 'wp_cache_ob_callback_filter', 'scossdl_off_filter' );
228    }
229}
230
231/**
232 * Update CDN settings to the options database table.
233 */
234function scossdl_off_update() {
235
236    if ( isset( $_POST['action'], $_POST['_wpnonce'] )
237        && 'update_ossdl_off' === $_POST['action'] // WPCS: sanitization ok.
238        && wp_verify_nonce( $_POST['_wpnonce'], 'wp-cache' )
239    ) {
240        update_option( 'ossdl_off_cdn_url', untrailingslashit( wp_unslash( $_POST['ossdl_off_cdn_url'] ) ) ); // WPSC: sanitization ok.
241        update_option( 'ossdl_off_blog_url', untrailingslashit( wp_unslash( $_POST['ossdl_off_blog_url'] ) ) ); // WPSC: sanitization ok.
242
243        if ( empty( $_POST['ossdl_off_include_dirs'] ) ) {
244            $include_dirs = implode( ',', scossdl_off_default_inc_dirs() );
245        } else {
246            $include_dirs = sanitize_text_field( wp_unslash( $_POST['ossdl_off_include_dirs'] ) ); // WPSC: validation ok,sanitization ok.
247        }
248        update_option( 'ossdl_off_include_dirs', $include_dirs );
249
250        update_option( 'ossdl_off_exclude', sanitize_text_field( wp_unslash( $_POST['ossdl_off_exclude'] ) ) ); // WPSC: sanitization ok.
251        update_option( 'ossdl_cname', sanitize_text_field( wp_unslash( $_POST['ossdl_cname'] ) ) ); // WPSC: sanitization ok.
252
253        $ossdl_https = empty( $_POST['ossdl_https'] ) ? 0 : 1;
254        $ossdlcdn    = empty( $_POST['ossdlcdn'] ) ? 0 : 1;
255
256        update_option( 'ossdl_https', $ossdl_https );
257        wp_cache_setting( 'ossdlcdn', $ossdlcdn );
258    }
259}
260
261/**
262 * Show CDN settings.
263 */
264function scossdl_off_options() {
265    global $ossdlcdn, $ossdl_off_blog_url, $ossdl_off_cdn_url, $ossdl_cname, $ossdl_https;
266    global $ossdl_off_include_dirs, $ossdl_off_excludes;
267
268    scossdl_off_update();
269
270    scossdl_off_get_options();
271
272    $example_cdn_uri = ( is_ssl() ? 'https' : 'http' ) . '://cdn.' . preg_replace( '`^(https?:)?//(www\.)?`', '', get_site_url() );
273    $example_cnames  = str_replace( '://cdn.', '://cdn1.', $example_cdn_uri );
274    $example_cnames .= ',' . str_replace( '://cdn.', '://cdn2.', $example_cdn_uri );
275    $example_cnames .= ',' . str_replace( '://cdn.', '://cdn3.', $example_cdn_uri );
276
277    $example_cdn_uri  = ( get_site_url() === $ossdl_off_cdn_url ) ? $example_cdn_uri : $ossdl_off_cdn_url;
278    $example_cdn_uri .= '/wp-includes/js/jquery/jquery-migrate.js';
279    $example_cdn_uri  = esc_url( $example_cdn_uri );
280    ?>
281        <div class="wpsc-card">
282        <h3><?php _e( 'Jetpack CDN' ); ?></h3>
283        <p><?php printf(
284            __( 'The free %1$sJetpack plugin%2$s has a %3$sSite Accelerator%2$s feature that is easier to use than the CDN functionality in this plugin. However files will be cached "forever" and will not update if you update the local file. Files will need to be renamed to refresh them. The %3$sJetpack documentation%2$s explains more about this.', 'wp-super-cache' ),
285            '<a href="https://jetpack.com/">',
286            '</a>',
287            '<a href="https://jetpack.com/support/site-accelerator/">'
288        ); ?></p>
289    <?php
290    if ( class_exists( 'Jetpack' ) ) {
291        if ( Jetpack::is_module_active( 'photon' ) ) {
292            ?><p><strong><?php printf(
293                __( 'You already have Jetpack installed and %1$sSite Accelerator%2$s enabled on this blog. The CDN here is disabled to avoid conflicts with Jetpack.', 'wp-super-cache' ),
294                '<a href="https://jetpack.com/support/site-accelerator/">',
295                '</a>'
296            ); ?></strong></p><?php
297        } else {
298            ?><p><?php printf(
299                __( 'You already have Jetpack installed but %1$sSite Accelerator%2$s is disabled on this blog. Enable it on the %3$sJetpack settings page%2$s.', 'wp-super-cache' ),
300                '<a href="https://jetpack.com/support/site-accelerator/">',
301                '</a>',
302                '<a href="' . admin_url( 'admin.php?page=jetpack#/settings' ) . '">'
303            ); ?></p><?php
304        }
305    } else {
306            ?><p><strong><?php printf(
307                __( '%1$sJetpack%2$s was not found on your site but %3$syou can install it%2$s. The Site Accelerator feature is free to use on any WordPress site and offers the same benefit as other CDN services. You should give it a try!', 'wp-super-cache' ),
308                '<a href="https://jetpack.com/">',
309                '</a>',
310                '<a href="' . admin_url( 'plugin-install.php?s=jetpack&tab=search&type=term' ) . '">'
311            ); ?></strong></p><?php
312    }
313    if ( class_exists( 'Jetpack' ) && Jetpack::is_module_active( 'photon' ) ) {
314        echo '</div>'; // close wpsc-card
315        return;
316    }
317    ?>
318        <h3><?php _e( 'Simple CDN' ); ?></h3>
319        <p><?php _e( 'Your website probably uses lots of static files. Image, Javascript and CSS files are usually static files that could just as easily be served from another site or CDN. Therefore, this plugin replaces any links in the <code>wp-content</code> and <code>wp-includes</code> directories (except for PHP files) on your site with the URL you provide below. That way you can either copy all the static content to a dedicated host or mirror the files to a CDN by <a href="https://www.google.com/search?q=cdn+origin+pull" target="_blank">origin pull</a>.', 'wp-super-cache' ); ?></p>
320        <p><?php printf( __( '<strong style="color: red">WARNING:</strong> Test some static urls e.g., %s  to ensure your CDN service is fully working before saving changes.', 'wp-super-cache' ), '<code>' . esc_html( $example_cdn_uri ) . '</code>' ); ?></p>
321
322    <?php if ( get_home_url() !== $ossdl_off_blog_url ) { ?>
323        <p><?php printf( __( '<strong style="color: red">WARNING:</strong> Your siteurl and homeurl are different. The plugin is using %s as the homepage URL of your site but if that is wrong please use the filter "ossdl_off_blog_url" to fix it.', 'wp-super-cache' ), '<code>' . esc_html( $ossdl_off_blog_url ) . '</code>' ); ?></p>
324    <?php } ?>
325        <p><?php esc_html_e( 'You can define different CDN URLs for each site on a multsite network.', 'wp-super-cache' ); ?></p>
326        <p><form method="post" action="">
327        <?php wp_nonce_field( 'wp-cache' ); ?>
328        <table class="form-table"><tbody>
329            <tr valign="top">
330                <td style='text-align: right'>
331                    <input id='ossdlcdn' type="checkbox" name="ossdlcdn" value="1" <?php checked( $ossdlcdn ); ?> />
332                </td>
333                <th scope="row"><label for="ossdlcdn"><?php esc_html_e( 'Enable CDN Support', 'wp-super-cache' ); ?></label></th>
334            </tr>
335            <tr valign="top">
336                <th scope="row"><label for="ossdl_off_cdn_url"><?php esc_html_e( 'Site URL', 'wp-super-cache' ); ?></label></th>
337                <td>
338                    <input type="text" name="ossdl_off_blog_url" value="<?php echo esc_attr( untrailingslashit( $ossdl_off_blog_url ) ); ?>" size="64" class="regular-text code" /><br />
339                    <span class="description"><?php _e( 'The URL of your site. No trailing <code>/</code> please.', 'wp-super-cache' ); ?></span>
340                </td>
341            </tr>
342            <tr valign="top">
343                <th scope="row"><label for="ossdl_off_cdn_url"><?php esc_html_e( 'Off-site URL', 'wp-super-cache' ); ?></label></th>
344                <td>
345                    <input type="text" name="ossdl_off_cdn_url" value="<?php echo esc_attr( $ossdl_off_cdn_url ); ?>" size="64" class="regular-text code" /><br />
346                    <span class="description"><?php printf( __( 'The new URL to be used in place of %1$s for rewriting. No trailing <code>/</code> please.<br />Example: <code>%2$s</code>.', 'wp-super-cache' ), esc_html( get_site_url() ), esc_html( $example_cdn_uri ) ); ?></span>
347                </td>
348            </tr>
349            <tr valign="top">
350                <th scope="row"><label for="ossdl_off_include_dirs"><?php esc_html_e( 'Include directories', 'wp-super-cache' ); ?></label></th>
351                <td>
352                    <input type="text" name="ossdl_off_include_dirs" value="<?php echo esc_attr( implode( ',', $ossdl_off_include_dirs ) ); ?>" size="64" class="regular-text code" /><br />
353                    <span class="description"><?php _e( 'Directories to include in static file matching. Use a comma as the delimiter. Default is <code>wp-content, wp-includes</code>, which will be enforced if this field is left empty.', 'wp-super-cache' ); ?></span>
354                </td>
355            </tr>
356            <tr valign="top">
357                <th scope="row"><label for="ossdl_off_exclude"><?php esc_html_e( 'Exclude if substring', 'wp-super-cache' ); ?></label></th>
358                <td>
359                    <input type="text" name="ossdl_off_exclude" value="<?php echo esc_attr( implode( ',', $ossdl_off_excludes ) ); ?>" size="64" class="regular-text code" /><br />
360                    <span class="description"><?php _e( 'Excludes something from being rewritten if one of the above strings is found in the URL. Use a comma as the delimiter like this, <code>.php, .flv, .do</code>, and always include <code>.php</code> (default).', 'wp-super-cache' ); ?></span>
361                </td>
362            </tr>
363            <tr valign="top">
364                <th scope="row"><label for="ossdl_cname"><?php esc_html_e( 'Additional CNAMES', 'wp-super-cache' ); ?></label></th>
365                <td>
366                    <input type="text" name="ossdl_cname" value="<?php echo esc_attr( $ossdl_cname ); ?>" size="64" class="regular-text code" /><br />
367                    <span class="description"><?php printf( __( 'These <a href="https://www.wikipedia.org/wiki/CNAME_record">CNAMES</a> will be used in place of %1$s for rewriting (in addition to the off-site URL above). Use a comma as the delimiter. For pages with a large number of static files, this can improve browser performance. CNAMEs may also need to be configured on your CDN.<br />Example: %2$s', 'wp-super-cache' ), esc_html( get_site_url() ), esc_html( $example_cnames ) ); ?></span>
368                </td>
369            </tr>
370            <tr valign="top">
371                <th scope="row" colspan='2'><label><input type='checkbox' name='ossdl_https' value='1' <?php checked( $ossdl_https ); ?> /><?php esc_html_e( 'Skip https URLs to avoid "mixed content" errors', 'wp-super-cache' ); ?></label></th>
372            </tr>
373        </tbody></table>
374        <input type="hidden" name="action" value="update_ossdl_off" />
375        <p class="submit"><input type="submit" class="button-primary" value="<?php esc_attr_e( 'Save Changes', 'wp-super-cache' ); ?>" /></p>
376        </form></p>
377        <p>
378        <?php
379            printf(
380                /* Translators: placeholder is a link to OSSDL CDN Off Linker plugin on WordPress.org */
381                esc_html__( 'CDN functionality provided by %s by Mark Kubacki', 'wp-super-cache' ),
382                '<a href="https://wordpress.org/plugins/ossdl-cdn-off-linker/">OSSDL CDN Off Linker</a>'
383            );
384        ?>
385        </p>
386        </div> <!-- Close .wpsc-card -->
387    <?php
388}