Last active
April 24, 2020 09:44
-
-
Save wpmudev-sls/ca7f7ab5695ce0d3ba692a0524248dc6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: [Hustle Pro] - checks if cookie is set | |
* Plugin URI: https://premium.wpmudev.org/ | |
* Author: Maciek Palmowski @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org/ | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
if ( defined( 'WP_CLI' ) && WP_CLI ) { | |
return; | |
} | |
add_filter( | |
'hustle_visibility_condition_options', | |
function( $conditions ) { | |
$conditions['cookie_set'] = __( 'Cookie set' ); | |
return $conditions; | |
} | |
); | |
add_action( | |
'hustle_visibility_condition_templates', | |
function() { ?> | |
<script id="hustle-visibility-rule-tpl--cookie_set" type="text/template"> | |
<label class="sui-label"><?php esc_html_e( 'Choose set cookies', 'hustle' ); ?></label> | |
<div class="sui-side-tabs"> | |
<div class="sui-tabs-menu"> | |
<label for="{{ groupId }}-{{ type }}-rule--cookie-set-except" | |
class="sui-tab-item"> | |
<input type="radio" | |
name="{{ groupId }}-{{ type }}-rule--cookie-set" | |
value="except" | |
id="{{ groupId }}-{{ type }}-rule--cookie-set-except" | |
data-attribute="filter_type" | |
{{ _.checked( filter_type, 'except' ) }} /> | |
<?php esc_html_e( 'All cookies except', 'hustle' ); ?> | |
</label> | |
<label for="{{ groupId }}-{{ type }}-rule--cookie-set-only" | |
class="sui-tab-item"> | |
<input type="radio" | |
name="{{ groupId }}-{{ type }}-rule--cookie-set" | |
value="only" | |
id="{{ groupId }}-{{ type }}-rule--cookie-set-only" | |
data-attribute="filter_type" | |
{{ _.checked( filter_type, 'only' ) }} /> | |
<?php esc_html_e( 'Only these cookies', 'hustle' ); ?> | |
</label> | |
</div> | |
<div class="sui-tabs-content"> | |
<div class="sui-tab-content active"> | |
<textarea placeholder="<?php esc_html_e( 'Enter the cookie\'s names.', 'hustle' ); ?>" | |
class="sui-form-control" | |
data-attribute="cookies">{{{ cookies }}}</textarea> | |
<span class="sui-description"><?php esc_html_e( 'Enter only one cookie name per line and.', 'hustle' ); ?></span> | |
</div> | |
</div> | |
</div> | |
</script> | |
<script> | |
jQuery( document ).on( "hustleAddViewConditions", function( event, ConditionBase ) { | |
Optin.View.Conditions.cookie_set = ConditionBase.extend({ // eslint-disable-line camelcase | |
conditionId: 'cookie_set', | |
setProperties() { | |
this.title = 'Cookie set'; | |
}, | |
defaults: function() { | |
return { | |
'cookies': '', | |
'filter_type': 'except' // except | only | |
}; | |
}, | |
getHeader: function() { | |
let length = 0; | |
if ( this.getAttribute( 'cookies' ).length ) { | |
length = this.countLines( this.getAttribute( 'cookies' ) ); | |
} | |
if ( length ) { | |
return ( 'only' === this.getAttribute( 'filter_type' ) ? optinVars.messages.condition_labels.only_these : optinVars.messages.condition_labels.except_these ).replace( '{number}', length ); | |
} else { | |
return 'only' === this.getAttribute( 'filter_type' ) ? optinVars.messages.condition_labels.none : optinVars.messages.condition_labels.all; | |
} | |
}, | |
body: function() { | |
return this.template( this.getData() ); | |
} | |
}); | |
}); | |
</script> | |
<?php | |
} | |
); | |
add_action( | |
'plugins_loaded', | |
function() { | |
if ( ! class_exists( 'Opt_In_Condition_Abstract' ) ) { | |
return; | |
} | |
class Opt_In_Condition_Cookie_Set extends Opt_In_Condition_Abstract { | |
/** | |
* Returns whether the condition was met. | |
* | |
* @since 4.2.1 | |
*/ | |
public function is_allowed() { | |
if ( ! isset( $this->args->cookies ) || ! isset( $this->args->filter_type ) || empty( $_COOKIE ) ) { | |
return false; | |
} | |
$is_cookie_set = false; | |
$cookies = $this->parse_textarea(); | |
$user_cookies_keys = array_keys( $_COOKIE ); | |
foreach ( $cookies as $key => $value ) { | |
if ( in_array( $key, $user_cookies_keys, true ) ) { | |
if ( $this->is_cookie_set( $key, $value ) ) { | |
$is_cookie_set = true; | |
break; | |
} | |
} | |
} | |
if ( 'only' === $this->args->filter_type ) { | |
return $is_cookie_set; | |
} else { | |
return ! $is_cookie_set; | |
} | |
} | |
/** | |
* Returns array from textarea. | |
* If we're checking is cookie set and it's value than it should value name|value format. | |
* | |
* @since 4.2.1 | |
*/ | |
private function parse_textarea() { | |
$cookies = array(); | |
$textarea = preg_split( '/\r\n|\r|\n/', $this->args->cookies ); | |
foreach ( $textarea as $line ) { | |
$cookie = explode( '|', $line ); | |
$cookies[ trim( $cookie[0] ) ] = ! empty( $cookie[1] ) ? trim( $cookie[1] ) : ''; | |
} | |
return $cookies; | |
} | |
/** | |
* Returns if cookie is set and/or cookie is set and has a correct value. | |
* | |
* @param mixed $key Cookie name. | |
* @param mixed $value Cookie value. | |
* @since 4.2.1 | |
*/ | |
private function is_cookie_set( $key, $value ) { | |
return ( ! empty( $value ) && isset( $_COOKIE[ $key ] ) && $_COOKIE[ $key ] === $value ) | |
|| ( empty( $value ) ); | |
} | |
} | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment