Skip to content

Instantly share code, notes, and snippets.

@pawelkmpt
Created September 23, 2022 12:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pawelkmpt/2feacacf2c53d7df2a0e00aa7171788e to your computer and use it in GitHub Desktop.
Save pawelkmpt/2feacacf2c53d7df2a0e00aa7171788e to your computer and use it in GitHub Desktop.
Control size of `pum_alm_pages_viewed` cookie
<?php
/**
* Plugin name: Popup Maker Cookies
* Description: Control size of <code>pum_alm_pages_viewed</code> cookie. <a href="https://app.clickup.com/t/28kmweq">ClickUp card ➝</a>
* Author: Paweł Kopociński
* Version: 2022.09.23
*
* @see https://app.clickup.com/t/28kmweq
*/
defined( 'ABSPATH' ) || exit;
/** @since 2022.09.23 */
add_action( 'init', static function() {
// Use WC function for simplicity.
if ( ! function_exists( 'wc_setcookie' ) ) {
return;
}
$cookie_name = 'pum_alm_pages_viewed';
$cookie_value = filter_input( INPUT_COOKIE, $cookie_name );
$items_count = 10; // Max number of post IDs to keep.
if ( ! $cookie_value ) {
return;
}
$cookie_value = explode( ',', $cookie_value );
// Do nothing if cookie size is fine.
if ( count( $cookie_value ) <= $items_count ) {
return;
}
// Limit number of IDs on the cookie value.
$cookie_value = array_map( 'absint', $cookie_value );
$cookie_value = array_splice( $cookie_value, -$items_count );
$cookie_value = implode( ',', $cookie_value );
/** @see \AutomateWoo\Cookies::set() */
wc_setcookie( $cookie_name, $cookie_value, 0, is_ssl() );
$_COOKIE[ $cookie_name ] = $cookie_value;
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment