businessdad icon

Aelia Foundation Classes - Set cookies only once

businessdad | PRO | 03/24/18 06:45:17 PM UTC | 0 ⭐ | 1060 👁️ | Never ⏰ | []
PHP |

1.89 KB

|

None

|

0 👍

/

0 👎

/**
 * Aelia Foundation Classes for WooCommerce.
 * Only allow to set a cookie once, unless its value changes.
 *
 * HOW TO USE THIS CODE
 * Add the code to the bottom of your theme's functions.php file, and it
 * will run automatically. For more information: https://www.skyverge.com/blog/add-custom-code-to-wordpress/
 *
 * GPL DISCLAIMER
 * Because this code program is free of charge, there is no warranty for it, to the extent permitted by applicable law.
 * Except when otherwise stated in writing the copyright holders and/or other parties provide the program "as is"
 * without warranty of any kind, either expressed or implied, including, but not limited to, the implied warranties of
 * merchantability and fitness for a particular purpose. The entire risk as to the quality and performance of the program
 * is with you. should the program prove defective, you assume the cost of all necessary servicing, repair or correction.
 *
 * Need a consultation, or assistance to customise this code? Find us on Codeable: https://aelia.co/hire_us
 *
 * @param array cookie_data
 * @param array|null
 */
add_filter('wc_aelia_afc_session_set_cookie', function($cookie_data) {
  // The content of the static $cookies_already_set variable will be preserved
  // across multiple executions of the filter
  static $cookies_already_set = array();
  
  // If the cookie is in the "cookies already set" list, and its value is the
  // same, remove the cookie data. This will prevent the AFC from setting the
  // value
  if(isset($cookies_already_set[$cookie_data['name']]) && ($cookies_already_set[$cookie_data['name']] === $cookie_data['value'])) {
    $cookie_data = null;
  }
  else {
    // If the cookie is new, or its value has changed, store it in the list and
    // let the AFC set it
    $cookies_already_set[$cookie_data['name']] = $cookie_data['value'];
  }
 
  return $cookie_data;
}, 10);

Comments