Advertisement
businessdad

Aelia Foundation Classes - Set cookies only once

Mar 24th, 2018
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.89 KB | None | 0 0
  1. /**
  2.  * Aelia Foundation Classes for WooCommerce.
  3.  * Only allow to set a cookie once, unless its value changes.
  4.  *
  5.  * HOW TO USE THIS CODE
  6.  * Add the code to the bottom of your theme's functions.php file, and it
  7.  * will run automatically. For more information: https://www.skyverge.com/blog/add-custom-code-to-wordpress/
  8.  *
  9.  * GPL DISCLAIMER
  10.  * Because this code program is free of charge, there is no warranty for it, to the extent permitted by applicable law.
  11.  * Except when otherwise stated in writing the copyright holders and/or other parties provide the program "as is"
  12.  * without warranty of any kind, either expressed or implied, including, but not limited to, the implied warranties of
  13.  * merchantability and fitness for a particular purpose. The entire risk as to the quality and performance of the program
  14.  * is with you. should the program prove defective, you assume the cost of all necessary servicing, repair or correction.
  15.  *
  16.  * Need a consultation, or assistance to customise this code? Find us on Codeable: https://aelia.co/hire_us
  17.  *
  18.  * @param array cookie_data
  19.  * @param array|null
  20.  */
  21. add_filter('wc_aelia_afc_session_set_cookie', function($cookie_data) {
  22.   // The content of the static $cookies_already_set variable will be preserved
  23.   // across multiple executions of the filter
  24.   static $cookies_already_set = array();
  25.  
  26.   // If the cookie is in the "cookies already set" list, and its value is the
  27.   // same, remove the cookie data. This will prevent the AFC from setting the
  28.   // value
  29.   if(isset($cookies_already_set[$cookie_data['name']]) && ($cookies_already_set[$cookie_data['name']] === $cookie_data['value'])) {
  30.     $cookie_data = null;
  31.   }
  32.   else {
  33.     // If the cookie is new, or its value has changed, store it in the list and
  34.     // let the AFC set it
  35.     $cookies_already_set[$cookie_data['name']] = $cookie_data['value'];
  36.   }
  37.  
  38.   return $cookie_data;
  39. }, 10);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement