Skip to content

Instantly share code, notes, and snippets.

@Ratko-Solaja
Created November 7, 2016 10:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Ratko-Solaja/088f250c21f51132753cf2c4357b6af1 to your computer and use it in GitHub Desktop.
Save Ratko-Solaja/088f250c21f51132753cf2c4357b6af1 to your computer and use it in GitHub Desktop.
Helper functions.
/**
* Generate unique cookie name for the current site and return it
*
* @since 1.0.0
*/
public function get_unique_cookie_name() {
$site_url = get_bloginfo( 'url' );
$site_name = get_bloginfo( 'name' );
$suffix = '-toptal-saved-items';
$cookie_name = $site_url . $site_name . $suffix;
// Now let's strip everything
$cookie_name = str_replace( array( '[\', \']' ), '', $cookie_name );
$cookie_name = preg_replace( '/\[.*\]/U', '', $cookie_name );
$cookie_name = preg_replace( '/&(amp;)?#?[a-z0-9]+;/i', '-', $cookie_name );
$cookie_name = htmlentities( $cookie_name, ENT_COMPAT, 'utf-8' );
$cookie_name = preg_replace( '/&([a-z])(acute|uml|circ|grave|ring|cedil|slash|tilde|caron|lig|quot|rsquo);/i', '\\1', $cookie_name );
$cookie_name = preg_replace( array( '/[^a-z0-9]/i', '/[-]+/' ) , '-', $cookie_name );
$cookie_name = strtolower( trim( $cookie_name, '-' ) );
return $cookie_name;
}
/**
* Set cookie
*
* @since 1.0.4
*/
public function toptal_set_cookie( $name, $value = array(), $time = null ) {
$time = $time != null ? $time : time() + apply_filters( 'toptal_cookie_expiration', 60 * 60 * 24 * 30 );
$value = base64_encode( json_encode( stripslashes_deep( $value ) ) );
$expiration = apply_filters( 'toptal_cookie_expiration_time', $time );
$_COOKIE[ $name ] = $value;
setcookie( $name, $value, $expiration, COOKIEPATH ? COOKIEPATH : '/', COOKIE_DOMAIN, false );
}
/**
* Get cookie
*
* @since 1.0.4
*/
public function toptal_get_cookie( $name ) {
if ( isset( $_COOKIE[$name] ) ) {
return json_decode( base64_decode( stripslashes( $_COOKIE[$name] ) ), true );
}
return array();
}
/**
* Get if enabled only for logged in users
*
* @since 1.0.2
*/
public function get_user_status() {
$options = get_option( $this->plugin_name . '-settings' );
if ( ! empty( $options['toggle-status-override'] ) ) {
$status = $options['toggle-status-override'];
} else {
$status = 0;
}
return $status;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment