New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wpsc not serving index-mobile.html - cause and easy fix #184

Closed
DrLightman opened this Issue Feb 5, 2017 · 4 comments

Comments

Projects
None yet
2 participants
@DrLightman
Copy link

DrLightman commented Feb 5, 2017

This one is old and I've been using a fix posted by another user months ago, without that fix my mobile cache wouldn't work. Since it hasn't get to plugin master yet I'm opening this issue in the hope it will be made official.

I have mod_rewrite and mobile support enabled. I'm not reposting the other specific wpsc settings, they are the same of the one I posted in the first post of #169.

The problem: desktop cache (index.html) is served instead of mobile cache (index-mobile.html)

The reason: in the supercache_filename() definition there is this piece of code:

	if ( function_exists( "apply_filters" ) ) {
		$extra_str = apply_filters( 'supercache_filename_str', $extra_str );
	} else {
		$extra_str = do_cacheaction( 'supercache_filename_str', $extra_str );
	}

(to skip the explanation, the fixed code is at the bottom)

The problem there is that the first test passes (apply_filters does exist), but no filters have been attached at that stage yet:

global $wp_filter; echo print_r($wp_filter, true);

Array
(
)

In this condition the '-mobile' suffix doesn't get appended to the filename.

Maybe this is happening when $wp_super_cache_late_init is FALSE and so wp_cache_serve_cache_file() gets called early.

I'm leaving here the backtrace up to the supercache_filename() call just before that IF ELSE:

Array
(
    [0] => Array
        (
            [file] => D:\Siti\wptest\wp-content\plugins\wp-super-cache\wp-cache-phase1.php
            [line] => 174
            [function] => supercache_filename
            [args] => Array
                (
                )

        )

    [1] => Array
        (
            [file] => D:\Siti\wptest\wp-content\plugins\wp-super-cache\wp-cache-phase1.php
            [line] => 344
            [function] => wp_cache_serve_cache_file
            [args] => Array
                (
                )

        )

    [2] => Array
        (
            [file] => D:\Siti\wptest\wp-content\advanced-cache.php
            [line] => 20
            [args] => Array
                (
                    [0] => D:\Siti\wptest\wp-content\plugins\wp-super-cache\wp-cache-phase1.php
                )

            [function] => include_once
        )

    [3] => Array
        (
            [file] => D:\Siti\wptest\wp-settings.php
            [line] => 86
            [args] => Array
                (
                    [0] => D:\Siti\wptest\wp-content\advanced-cache.php
                )

            [function] => include
        )

    [4] => Array
        (
            [file] => D:\Siti\wptest\wp-config.php
            [line] => 101
            [args] => Array
                (
                    [0] => D:\Siti\wptest\wp-settings.php
                )

            [function] => require_once
        )

    [5] => Array
        (
            [file] => D:\Siti\wptest\wp-load.php
            [line] => 39
            [args] => Array
                (
                    [0] => D:\Siti\wptest\wp-config.php
                )

            [function] => require_once
        )

    [6] => Array
        (
            [file] => D:\Siti\wptest\wp-blog-header.php
            [line] => 13
            [args] => Array
                (
                    [0] => D:\Siti\wptest\wp-load.php
                )

            [function] => require_once
        )

    [7] => Array
        (
            [file] => D:\Siti\wptest\index.php
            [line] => 17
            [args] => Array
                (
                    [0] => D:\Siti\wptest\wp-blog-header.php
                )

            [function] => require
        )

)

The fixed code should be like this, or at least this seems to be working for me. In wp-cache-phase1.php, inside the supercache_filename() function definition:

	if ( function_exists( 'apply_filters' ) && function_exists( 'has_filter' ) && has_filter( 'supercache_filename_str' ) ) {
		$extra_str = apply_filters( 'supercache_filename_str', $extra_str );
	} else {
		$extra_str = do_cacheaction( 'supercache_filename_str', $extra_str );
	}

With that mod the ELSE code gets executed at that stage and the '-mobile' suffix it's appended because these wpsc filters have been correctly registered:

Array
(
    [add_cacheaction] => Array
        (
            [0] => awaitingmoderation_actions
            [1] => domain_mapping_actions
            [2] => searchengine_phase2_actions
        )

    [cache_admin_page] => Array
        (
            [0] => wp_supercache_awaitingmoderation_admin
            [1] => wp_supercache_badbehaviour_admin
            [2] => wp_supercache_domain_mapping_admin
            [3] => wp_super_cache_jetpack_admin
            [4] => wp_supercache_searchengine_admin
            [5] => wp_super_cache_wptouch_admin
        )

    [wp_cache_served_cache_file] => Array
        (
            [0] => wp_supercache_badbehaviour
        )

    [wp_cache_get_cookies_values] => Array
        (
            [0] => wp_supercache_searchengine
        )

    [disable_mobile_check] => Array
        (
            [0] => wp_super_cache_maybe_disable_wptouch
        )

    [supercache_filename_str] => Array
        (
            [0] => wp_cache_check_mobile
        )
)
@donnchawp

This comment has been minimized.

Copy link
Contributor

donnchawp commented Feb 6, 2017

I think this is happening because in WordPress 4.6 plugin.php was loaded before advanced-cache.php which made apply_filters() available to any caching plugin.

Your fix really isn't the correct way of fixing it though. I'll copy the add_filter() call to wp-cache-phase1.php with an appropriate function_exists() for older versions of WordPress.

@DrLightman

This comment has been minimized.

Copy link

DrLightman commented Feb 6, 2017

FYI I'm using Wordpress 4.6.3

donnchawp added a commit that referenced this issue Feb 6, 2017

Add this filter for mobile detection
Fixes #184.
Since WordPress 4.6 plugin.php has been loaded before advanced-cache.php
which means that apply_filters and add_filter are available to caching
plugins. This patch adds the mobile UA check using add_filter.
@donnchawp

This comment has been minimized.

Copy link
Contributor

donnchawp commented Feb 6, 2017

Check out #185 - it's a tiny mod to the phase1 script but it loads the mobile function using add_filter() and it fixed the problem on my sites. Does it work for you too?

@DrLightman

This comment has been minimized.

Copy link

DrLightman commented Feb 6, 2017

Yes, it works now, supercache_filename() now sees the wp filter:

global $wp_filter; echo print_r($wp_filter, true);

Array
(
    [supercache_filename_str] => Array
        (
            [10] => Array
                (
                    [wp_cache_check_mobile] => Array
                        (
                            [function] => wp_cache_check_mobile
                            [accepted_args] => 1
                        )
                )
        )
)

Good job.

@donnchawp donnchawp closed this Feb 6, 2017

kraftbj pushed a commit that referenced this issue Feb 9, 2017

donncha
plugin.php has been loaded before advanced-cache.php since WordPress …
…4.6 which means the filter system is available. Fixes #184

git-svn-id: http://plugins.svn.wordpress.org/wp-super-cache/trunk@1589993 b8457f37-d9ea-0310-8a92-e5e31aec5664
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment