Skip to content
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

Add cookie helper functions to modify the cache key more easily. #580

Merged
merged 4 commits into from
Aug 8, 2018

Conversation

donnchawp
Copy link
Contributor

Inspired by https://wordpress.org/support/topic/php-cookies-doesnt-work-with-wp-super-cache/
This will make it much easier for plugin authors that use cookies to
identify visitors to integrate with WP Super Cache.
Use the function wpsc_add_cookie( $name ) to add a cookie name to the
list of cookies and wpsc_delete_cookie( $name ) to remove it.

Example code to add the cookie "euCookie" used by the EU Cookie Law
plugin;
https://wordpress.org/plugins/eu-cookie-law/


function add_wpsc_cookies() {
if ( function_exists( 'wpsc_add_cookie' ) ) {
wpsc_add_cookie( 'euCookie' );
}
}

add_action( 'init', 'add_wpsc_cookies' );

It's wrapped in an action on "init" to make sure that WP Super Cache is
loaded but the wpsc_add_cookie() function only needs to be called once.
Calling it multiple times does not add multiple entries for the same
cookie name.

Inspired by
https://wordpress.org/support/topic/php-cookies-doesnt-work-with-wp-super-cache/
This will make it much easier for plugin authors that use cookies to
identify visitors to integrate with WP Super Cache.
Use the function wpsc_add_cookie( $name ) to add a cookie name to the
list of cookies and wpsc_delete_cookie( $name ) to remove it.

Example code to add the cookie "euCookie" used by the EU Cookie Law
plugin;
https://wordpress.org/plugins/eu-cookie-law/

--------------
function add_wpsc_cookies() {
    if ( function_exists( 'wpsc_add_cookie' ) ) {
        wpsc_add_cookie( 'euCookie' );
    }
}

add_action( 'init', 'add_wpsc_cookies' );
--------------

It's wrapped in an action on "init" to make sure that WP Super Cache is
loaded but the wpsc_add_cookie() function only needs to be called once.
Calling it multiple times does not add multiple entries for the same
cookie name.
@donnchawp donnchawp self-assigned this Aug 7, 2018
@donnchawp donnchawp merged commit 337d173 into master Aug 8, 2018
@donnchawp donnchawp deleted the add_cookie_helpers branch August 8, 2018 11:31
@johnbillion
Copy link

This is neat and I can immediately see a use for it in some of my own plugins.

Do you think this would be better implemented as a filter, to make it more WordPress-y? For example:

add_filter( 'wpsc_cookies', function( array $cookies ) {
	$cookies[] = 'euCookie';
	return $cookies;
} );

@andreasciamanna
Copy link

andreasciamanna commented Aug 8, 2018

I see an action (not a filter) more appropriate for adding a cookie key.

Something like do_action( 'wpsc_add_cookie', 'euCookie' );.

A filter would also do it, but I don't think it's the right way to use filters and may also cause problems if the filtered array of cookies' keys gets messed up by a wrong implementation.

In my opinion, an action is also cleaner (no need to check if the function exists).

I'll be happy to implement it in the next WPML bugfix version, by the way.

@donnchawp
Copy link
Contributor Author

I thought about implementing it as a filter first but since the cookie key is only supposed to be added once I wanted to get away from having code that runs unnecessarily each time a plugin runs.

However, it is more convenient. I'll create a new PR with such a filter (and while I'm at it do the same for loading plugins)

donnchawp added a commit that referenced this pull request Aug 9, 2018
These filters will allow WordPress plugins to modify the cookies and
plugins lists used by WP Super Cache instead of the add/delete functions
introducted in #574 and #580.
Duplicate entries are removed so the filter function does not need to
worry about that. Example code:

`function myplugin_add_wpsc_cookies_filter( $cookies ) {
    $cookies[] = 'myplugin';
    return $cookies;
}
add_filter( 'wpsc_cookies', 'myplugin_add_wpsc_cookies_filter' );`

`function myplugin_delete_wpsc_cookies_filter( $cookies ) {
    if ( in_array( 'myplugin', $cookies ) ) {
        unset( $cookies[ array_search( 'myplugin', $cookies ) ] );
    }
    return $cookies;
}
add_filter( 'wpsc_cookies', 'myplugin_delete_wpsc_cookies_filter' );`
@donnchawp
Copy link
Contributor Author

#582 will add the filter you need. Does it look ok? Is "init" too early to fire that filter?

donnchawp added a commit that referenced this pull request Aug 9, 2018
…tings (#582)

This PR adds four actions to modify wpsc_cookies and wpsc_plugins:

* wpsc_add_plugin
* wpsc_delete_plugin
* wpsc_add_cookie
* wpsc_delete_cookie

These actions will allow WordPress plugins to modify the cookies and
plugins lists used by WP Super Cache instead of the add/delete functions
introduced in #574 and #580.
Duplicate entries are removed. Example code:

For example, to add a cookie name called 'euCookie':

`do_action( 'wpsc_add_cookie', 'euCookie' );`

To remove that cookie:

`do_action( 'wpsc_delete_cookie', 'euCookie' );`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants