| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package Freemius |
| 4 |
* @copyright Copyright (c) 2015, Freemius, Inc. |
| 5 |
* @license https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License Version 3 |
| 6 |
* @since 2.1.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
class FS_GDPR_Manager { |
| 14 |
/** |
| 15 |
* @var FS_Option_Manager |
| 16 |
*/ |
| 17 |
private $_storage; |
| 18 |
/** |
| 19 |
* @var array { |
| 20 |
* @type bool $required Are GDPR rules apply on the current context admin. |
| 21 |
* @type bool $show_opt_in_notice Should the marketing and offers opt-in message be shown to the admin or not. If not set, defaults to `true`. |
| 22 |
* @type int $notice_shown_at Last time the special GDPR opt-in message was shown to the current admin. |
| 23 |
* } |
| 24 |
*/ |
| 25 |
private $_data; |
| 26 |
/** |
| 27 |
* @var int |
| 28 |
*/ |
| 29 |
private $_wp_user_id; |
| 30 |
/** |
| 31 |
* @var string |
| 32 |
*/ |
| 33 |
private $_option_name; |
| 34 |
/** |
| 35 |
* @var FS_Admin_Notices |
| 36 |
*/ |
| 37 |
private $_notices; |
| 38 |
|
| 39 |
#-------------------------------------------------------------------------------- |
| 40 |
#region Singleton |
| 41 |
#-------------------------------------------------------------------------------- |
| 42 |
|
| 43 |
/** |
| 44 |
* @var FS_GDPR_Manager |
| 45 |
*/ |
| 46 |
private static $_instance; |
| 47 |
|
| 48 |
/** |
| 49 |
* @return FS_GDPR_Manager |
| 50 |
*/ |
| 51 |
public static function instance() { |
| 52 |
if ( ! isset( self::$_instance ) ) { |
| 53 |
self::$_instance = new self(); |
| 54 |
} |
| 55 |
|
| 56 |
return self::$_instance; |
| 57 |
} |
| 58 |
|
| 59 |
#endregion |
| 60 |
|
| 61 |
private function __construct() { |
| 62 |
$this->_storage = FS_Option_Manager::get_manager( WP_FS__GDPR_OPTION_NAME, true, true ); |
| 63 |
$this->_wp_user_id = Freemius::get_current_wp_user_id(); |
| 64 |
$this->_option_name = "u{$this->_wp_user_id}"; |
| 65 |
$this->_data = $this->_storage->get_option( $this->_option_name, array() ); |
| 66 |
$this->_notices = FS_Admin_Notices::instance( 'all_admins', '', '', true ); |
| 67 |
|
| 68 |
if ( ! is_array( $this->_data ) ) { |
| 69 |
$this->_data = array(); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Update a GDPR option for the current admin and store it. |
| 75 |
* |
| 76 |
* @author Vova Feldman (@svovaf) |
| 77 |
* @since 2.1.0 |
| 78 |
* |
| 79 |
* @param string $name |
| 80 |
* @param mixed $value |
| 81 |
*/ |
| 82 |
private function update_option( $name, $value ) { |
| 83 |
$this->_data[ $name ] = $value; |
| 84 |
|
| 85 |
$this->_storage->set_option( $this->_option_name, $this->_data, true ); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* @author Leo Fajardo (@leorw) |
| 90 |
* @since 2.1.0 |
| 91 |
* |
| 92 |
* @param bool $is_required |
| 93 |
*/ |
| 94 |
public function store_is_required( $is_required ) { |
| 95 |
$this->update_option( 'required', $is_required ); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Checks if the GDPR opt-in sticky notice is currently shown. |
| 100 |
* |
| 101 |
* @author Vova Feldman (@svovaf) |
| 102 |
* @since 2.1.0 |
| 103 |
* |
| 104 |
* @return bool |
| 105 |
*/ |
| 106 |
public function is_opt_in_notice_shown() { |
| 107 |
return $this->_notices->has_sticky( "gdpr_optin_actions_{$this->_wp_user_id}", true ); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Remove the GDPR opt-in sticky notice. |
| 112 |
* |
| 113 |
* @author Vova Feldman (@svovaf) |
| 114 |
* @since 2.1.0 |
| 115 |
*/ |
| 116 |
public function remove_opt_in_notice() { |
| 117 |
$this->_notices->remove_sticky( "gdpr_optin_actions_{$this->_wp_user_id}", true ); |
| 118 |
|
| 119 |
$this->disable_opt_in_notice(); |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Prevents the opt-in message from being added/shown. |
| 124 |
* |
| 125 |
* @author Leo Fajardo (@leorw) |
| 126 |
* @since 2.1.0 |
| 127 |
*/ |
| 128 |
public function disable_opt_in_notice() { |
| 129 |
$this->update_option( 'show_opt_in_notice', false ); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Checks if a GDPR opt-in message needs to be shown to the current admin. |
| 134 |
* |
| 135 |
* @author Vova Feldman (@svovaf) |
| 136 |
* @since 2.1.0 |
| 137 |
* |
| 138 |
* @return bool |
| 139 |
*/ |
| 140 |
public function should_show_opt_in_notice() { |
| 141 |
return ( |
| 142 |
! isset( $this->_data['show_opt_in_notice'] ) || |
| 143 |
true === $this->_data['show_opt_in_notice'] |
| 144 |
); |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Get the last time the GDPR opt-in notice was shown. |
| 149 |
* |
| 150 |
* @author Vova Feldman (@svovaf) |
| 151 |
* @since 2.1.0 |
| 152 |
* |
| 153 |
* @return false|int |
| 154 |
*/ |
| 155 |
public function last_time_notice_was_shown() { |
| 156 |
return isset( $this->_data['notice_shown_at'] ) ? |
| 157 |
$this->_data['notice_shown_at'] : |
| 158 |
false; |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Update the timestamp of the last time the GDPR opt-in message was shown to now. |
| 163 |
* |
| 164 |
* @author Vova Feldman (@svovaf) |
| 165 |
* @since 2.1.0 |
| 166 |
*/ |
| 167 |
public function notice_was_just_shown() { |
| 168 |
$this->update_option( 'notice_shown_at', WP_FS__SCRIPT_START_TIME ); |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* @param string $message |
| 173 |
* @param string|null $plugin_title |
| 174 |
* |
| 175 |
* @author Vova Feldman (@svovaf) |
| 176 |
* @since 2.1.0 |
| 177 |
*/ |
| 178 |
public function add_opt_in_sticky_notice( $message, $plugin_title = null ) { |
| 179 |
$this->_notices->add_sticky( |
| 180 |
$message, |
| 181 |
"gdpr_optin_actions_{$this->_wp_user_id}", |
| 182 |
'', |
| 183 |
'promotion', |
| 184 |
true, |
| 185 |
$this->_wp_user_id, |
| 186 |
$plugin_title, |
| 187 |
true |
| 188 |
); |
| 189 |
} |
| 190 |
} |