Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Last active November 4, 2019 13:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpmudev-sls/4a33f0f47825519f3466ed787e536eee to your computer and use it in GitHub Desktop.
Save wpmudev-sls/4a33f0f47825519f3466ed787e536eee to your computer and use it in GitHub Desktop.
[Hustle Pro] - Only show one per user web session for Popup and Slide-in
<?php
/**
* Plugin Name: [Hustle Pro] - Only show one per user web session for Popup and Slidein
* Description: [Hustle Pro] - Only show one per user web session for Popup and Slidein - 1147666672064761
* Author: Thobk @ WPMUDEV
* Author URI: https://premium.wpmudev.org
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Default this MU will enable this feature on the Popup and Slide-in
* you can disable these by set on the wp-config.php file,ex:
* define('WPMUDEV_HUSTLE_PER_SESSION_DISABLE_ON_SLIDEIN', true);
* define('WPMUDEV_HUSTLE_PER_SESSION_DISABLE_ON_POPUP', true);
*/
add_action( 'plugins_loaded', 'wpmudev_hustle_only_show_once_per_user_session_func', 100 );
function wpmudev_hustle_only_show_once_per_user_session_func() {
if( defined('HUSTLE_SUI_VERSION') && class_exists( 'Hustle_Module_Collection' ) ){
class WPMUDEV_Hustle_Control_User_Session{
private $activate_session_filters;
public function __construct(){
add_filter( 'hustle_sort_modules', array( $this, 'hustle_sort_modules') );
add_action( 'wp_footer', array( $this, 'maybe_set_user_session'), 21 );
add_action( 'wp_ajax_wpmudev_hust_set_user_session', array($this, 'set_user_session'));
add_action( 'wp_ajax_nopriv_wpmudev_hust_set_user_session', array($this, 'set_user_session'));
$this->activate_session_filters = [];
if( ! ( defined('WPMUDEV_HUSTLE_PER_SESSION_DISABLE_ON_POPUP') && WPMUDEV_HUSTLE_PER_SESSION_DISABLE_ON_POPUP ) ){
$this->activate_session_filters['popup'] = 1;
}
if( ! ( defined('WPMUDEV_HUSTLE_PER_SESSION_DISABLE_ON_SLIDEIN') && WPMUDEV_HUSTLE_PER_SESSION_DISABLE_ON_SLIDEIN ) ){
$this->activate_session_filters['slidein'] = 1;
}
}
public function start_session(){
if ( ! session_id() ) {
session_start();
}
}
public function hustle_sort_modules( $modules ){
$this->start_session();
if( isset( $_SESSION['wpmudev-hustle-user-session'] ) && is_array( $_SESSION['wpmudev-hustle-user-session'] ) ){
foreach( $modules as $id => $module ){
if( in_array( $module->id, $_SESSION['wpmudev-hustle-user-session'] ) ){
unset( $modules[ $id ] );
}
}
}
return $modules;
}
public function set_user_session(){
if( ! ( isset( $_POST['nonce'], $_POST['type'], $this->activate_session_filters[ $_POST['type'] ] ) && wp_verify_nonce( $_POST['nonce'], "wpmudev_mu_hustle" ) ) ){
wp_die( -1, 403 );
}
$this->start_session();
$_SESSION['wpmudev-hustle-user-session'] = isset( $_SESSION['wpmudev-hustle-user-session'] ) ? $_SESSION['wpmudev-hustle-user-session'] : [];
$_SESSION['wpmudev-hustle-user-session'][] = (int) $_POST['id'];
wp_send_json_success();
}
public function maybe_set_user_session(){
if( empty( $this->activate_session_filters ) ) return;
$custom_script = '<script>
(function($){
$(function(){
var _session_filters = '. json_encode( $this->activate_session_filters ) .';
if( window.HUI ){
$.each(_session_filters, function(_module){
$(".hustle-"+_module).on("hustle:module:closed", function(_module_obj){
$.ajax({
"url": incOpt.ajaxurl,
"method": "POST",
"data":{
"action": "wpmudev_hust_set_user_session",
"nonce": "'. wp_create_nonce( 'wpmudev_mu_hustle' ) .'",
"type": _module,
"id": $(this).data("id")
}
}).fail(function(){
alert("Have error, please try again!");
});
});
});
}
});
})(window.jQuery)
</script>';
echo $custom_script;
}
}
$run = new WPMUDEV_Hustle_Control_User_Session;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment