first commit

This commit is contained in:
2024-11-10 21:08:49 +01:00
commit 0d932ce5ee
14455 changed files with 2567501 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
<?php
class nsc_bar_admin_error
{
public $errors;
public function __construct()
{
$this->errors = array();
}
public function nsc_bar_display_errors()
{
if (!empty($this->errors)) {
add_action('admin_notices', array($this, 'nsc_bar_add_admin_errors'));
}
}
public function nsc_bar_set_admin_error($error, $type = "error")
{
$this->errors[$type][] = $error;
}
public function nsc_bar_add_admin_errors()
{
foreach ($this->errors as $error_type => $type) {
foreach ($type as $error_message) {
echo '<div class="notice notice-error">
<p>' . __($error_message, "nsc_bar_cookie_consent") . '</p>
</div>';
}
}
}
}

View File

@@ -0,0 +1,86 @@
<?php
class nsc_bar_admin_settings
{
private $settings;
private $default_banner_config_file;
private $plugin_configs;
public function __construct()
{
//retrieves only the hard coded settings.
$this->plugin_configs = new nsc_bar_plugin_configs;
$this->settings = $this->plugin_configs->nsc_bar_return_plugin_settings_without_db_settings();
}
public function nsc_bar_execute_backend_wp_actions()
{
add_action('admin_menu', array($this, 'nsc_bar_add_admin_menu'));
add_action('admin_enqueue_scripts', array($this, 'nsc_bar_enqueue_script_on_admin_page'));
add_action('admin_enqueue_scripts', array($this, 'nsc_bar_enqueue_styles_on_admin_page'));
add_action('admin_enqueue_scripts', array($this, 'nsc_bar_enqueue_admin_preview_banner'), 90);
}
public function nsc_bar_add_admin_menu()
{
add_options_page($this->settings->settings_page_configs->page_title, $this->settings->settings_page_configs->menu_title, $this->settings->settings_page_configs->capability, $this->settings->plugin_slug, array($this, "nsc_bar_createAdminPage"));
}
public function nsc_bar_enqueue_script_on_admin_page($hook)
{
if ($hook == 'settings_page_nsc_bar-cookie-consent') {
wp_enqueue_script('nsc_bar_cookietypes_js', NSC_BAR_PLUGIN_URL . '/admin/js/cookietypes.v2.js', array(), NSC_BAR_VERSION);
}
}
public function nsc_bar_enqueue_styles_on_admin_page($hook)
{
if ($hook == 'settings_page_nsc_bar-cookie-consent') {
wp_enqueue_style('nsc_bar_admin_styles', NSC_BAR_PLUGIN_URL . '/admin/css/nsc_bar_admin.css', array(), NSC_BAR_VERSION);
}
}
public function nsc_bar_enqueue_admin_preview_banner($hook)
{
if ($this->show_preview($hook)) {
$nsc_bar_frontend_banner = new nsc_bar_frontend();
$nsc_bar_banner_config = new nsc_bar_banner_configs();
$nsc_bar_frontend_banner->nsc_bar_set_json_configs($nsc_bar_banner_config);
$nsc_bar_frontend_banner->nsc_bar_enqueue_scripts();
}
}
public function nsc_bar_createAdminPage()
{
$objSettings = $this->plugin_configs->nsc_bar_return_plugin_settings();
$objSettings->current_language = "xx";
$objSettings->addon_lang_description = "See <a href='https://beautiful-cookie-banner.com' target='_blank'>here</a> how to add multilanguage support to your banner.";
$objSettings->additional_tab_link_parameter = "";
if (class_exists("nsc_bara_languages") === true && class_exists("nsc_bara_admin_settings_addon") === true) {
$language_settings = new nsc_bara_languages();
$objSettings->current_language = $language_settings->nsc_bara_get_current_language();
$bara_admin_settings = new nsc_bara_admin_settings_addon();
$objSettings->additional_tab_link_parameter = $bara_admin_settings->nsc_bara_get_additonal_tab_link();
$objSettings->addon_lang_description = $bara_admin_settings->nsc_bara_get_addon_lang_description();
}
$form_fields = new nsc_bar_html_formfields;
require NSC_BAR_PLUGIN_DIR . "/admin/tpl/admin.php";
// for testing
return $objSettings;
}
public function nsc_bar_add_settings_link($links)
{
$settings_link = '<a href="options-general.php?page=nsc_bar-cookie-consent">' . __('Settings') . '</a>';
array_push($links, $settings_link);
return $links;
}
private function show_preview($hook)
{
if ($hook == 'settings_page_nsc_bar-cookie-consent' && $this->plugin_configs->nsc_bar_get_option('activate_test_banner') == true) {
return true;
}
return false;
}
}

View File

@@ -0,0 +1,227 @@
<?php
class nsc_bar_banner_configs
{
private $banner_config_array;
private $banner_config_string;
private $plugin_configs;
private $banner_configs_slug;
public function __construct()
{
$this->plugin_configs = new nsc_bar_plugin_configs();
$this->nsc_bar_set_banner_configs_slug("bannersettings_json");
if (class_exists("nsc_bara_banner_configs_addon") === true) {
$bara = new nsc_bara_banner_configs_addon;
$this->nsc_bar_set_banner_configs_slug($bara->nsc_bara_get_banner_settings_slug(null));
}
}
public function nsc_bar_set_banner_configs_slug($banner_configs_slug)
{
$this->banner_configs_slug = $banner_configs_slug;
}
public function nsc_bar_set_banner_config_array($banner_config_array)
{
$this->banner_config_array = $banner_config_array;
}
public function nsc_bar_get_banner_config_array()
{
if (empty($this->banner_config_array)) {
$banner_config_array = $this->initialise_banner_configs();
$banner_config_array = apply_filters('nsc_bar_filter_banner_config_array_init', $banner_config_array);
$this->banner_config_array = $banner_config_array;
}
return $this->banner_config_array;
}
public function nsc_bar_get_banner_config_string()
{
$this->nsc_bar_get_banner_config_array();
$banner_string = json_encode($this->banner_config_array);
$validation = new nsc_bar_input_validation;
$this->banner_config_string = $validation->nsc_bar_check_valid_json_string($banner_string);
return $this->banner_config_string;
}
public function nsc_bar_update_banner_setting($field_slug, $value, $save_as)
{
if (empty($value) && $value !== false && $value !== "" && $value != 0) {
return false;
}
$this->nsc_bar_get_banner_config_array();
$value = $this->convert_to_save_as($value, $save_as);
$settings_array = $this->slug_string_to_array($field_slug);
$depth = count($settings_array);
switch ($depth) {
case 1:
$this->set_level_one_value($settings_array, $value);
break;
case 2:
$this->set_level_two_value($settings_array, $value);
break;
case 3:
$this->set_level_three_value($settings_array, $value);
break;
}
}
public function nsc_bar_get_cookie_setting($field_slug, $default_value)
{
$this->nsc_bar_get_banner_config_array();
$settings_array = $this->slug_string_to_array($field_slug);
$depth = count($settings_array);
switch ($depth) {
case 1:
$settings_value = $this->get_level_one_value($settings_array, $default_value);
break;
case 2:
$settings_value = $this->get_level_two_value($settings_array, $default_value);
break;
case 3:
$settings_value = $this->get_level_three_value($settings_array, $default_value);
break;
}
return $settings_value;
}
public function nsc_bar_save_banner_settings()
{
$this->remove_deactivated_js_function();
$json_string = json_encode($this->nsc_bar_get_banner_config_array());
return $this->plugin_configs->nsc_bar_update_option($this->banner_configs_slug, $json_string);
}
/*TODO: only needed for update to 2.0 remove after all updated*/
public function nsc_bar_remove_revokeBtn()
{
if (isset($this->banner_config_array["revokeBtn"])) {
unset($this->banner_config_array["revokeBtn"]);
}
}
private function remove_deactivated_js_function()
{
if (isset($this->banner_config_array["onPopupClose"]) && $this->banner_config_array["onPopupClose"] == "0") {
unset($this->banner_config_array["onPopupClose"]);
}
if (isset($this->banner_config_array["onStatusChange"]) && $this->banner_config_array["onStatusChange"] == "0") {
unset($this->banner_config_array["onStatusChange"]);
}
if (isset($this->banner_config_array["dismissOnScroll"]) && empty($this->banner_config_array["dismissOnScroll"])) {
unset($this->banner_config_array["dismissOnScroll"]);
}
if (isset($this->banner_config_array["dismissOnTimeout"]) && empty($this->banner_config_array["dismissOnTimeout"])) {
unset($this->banner_config_array["dismissOnTimeout"]);
}
if (isset($this->banner_config_array["makeButtonsEqual"]) && empty($this->banner_config_array["makeButtonsEqual"])) {
unset($this->banner_config_array["makeButtonsEqual"]);
}
if (isset($this->banner_config_array["showCloseX"]) && empty($this->banner_config_array["showCloseX"])) {
unset($this->banner_config_array["showCloseX"]);
}
}
private function slug_string_to_array($field_slug)
{
$settings_array = explode("_", $field_slug);
$depth = count($settings_array);
if ($depth > 3 || $depth < 1) {
throw new Exception("depth only allowed from 1 to 3 current: $depth");
}
return $settings_array;
}
private function get_level_one_value($config_array, $default = false)
{
$value = $default;
if (isset($this->banner_config_array[$config_array[0]])) {
$value = $this->banner_config_array[$config_array[0]];
}
return $value;
}
private function get_level_two_value($config_array, $default = false)
{
$value = $default;
if (isset($this->banner_config_array[$config_array[0]]) && isset($this->banner_config_array[$config_array[0]][$config_array[1]])) {
$value = $this->banner_config_array[$config_array[0]][$config_array[1]];
}
return $value;
}
private function get_level_three_value($config_array, $default = false)
{
$value = $default;
if (isset($this->banner_config_array[$config_array[0]]) && isset($this->banner_config_array[$config_array[0]][$config_array[1]]) && isset($this->banner_config_array[$config_array[0]][$config_array[1]][$config_array[2]])) {
$value = $this->banner_config_array[$config_array[0]][$config_array[1]][$config_array[2]];
}
return $value;
}
private function set_level_one_value($config_array, $value)
{
$this->nsc_bar_get_banner_config_array();
$this->banner_config_array[$config_array[0]] = $value;
}
private function set_level_two_value($config_array, $value)
{
$this->nsc_bar_get_banner_config_array();
$this->banner_config_array[$config_array[0]][$config_array[1]] = $value;
}
private function set_level_three_value($config_array, $value)
{
$this->nsc_bar_get_banner_config_array();
$this->banner_config_array[$config_array[0]][$config_array[1]][$config_array[2]] = $value;
}
private function initialise_banner_configs()
{
$banner_config_string = $this->read_banner_configs_from_db($this->banner_configs_slug);
// try to get default, if non default is not set.
if (empty($banner_config_string) && $this->banner_configs_slug !== "bannersettings_json") {
$banner_config_string = $this->read_banner_configs_from_db("bannersettings_json");
}
if (empty($banner_config_string)) {
$validate = new nsc_bar_input_validation;
$banner_config_string = $validate->nsc_bar_check_valid_json_string(file_get_contents(NSC_BAR_PLUGIN_DIR . "/public/config-default.json"));
}
return json_decode($banner_config_string, true);
}
private function read_banner_configs_from_db($slug)
{
$banner_config_string = $this->plugin_configs->nsc_bar_get_option($slug);
$validate = new nsc_bar_input_validation;
$banner_config_string = $validate->nsc_bar_check_valid_json_string($banner_config_string);
return $banner_config_string;
}
private function convert_to_save_as($value, $save_as)
{
if ($save_as === "array") {
return json_decode($value, true);
}
if ($save_as === "integer") {
return intval($value);
}
return $value;
}
}

View File

@@ -0,0 +1,240 @@
<?php
class nsc_bar_cookie_handler
{
private $banner_configs_object;
private $cookie_configs;
private $plugin_configs;
public function __construct()
{
$this->get_plugin_configs();
$this->get_banner_configs();
$this->get_cookie_configs();
}
public function nsc_bar_set_itp_cookie()
{
if ($this->plugin_configs->nsc_bar_get_option('activate_banner') == true &&
$this->plugin_configs->nsc_bar_get_option('backend_cookie_conversion') == true) {
$expiryDate = $this->get_expiry_date($this->cookie_configs['name'], $this->cookie_configs['expirydays']);
if (isset($_COOKIE[$this->cookie_configs['name']])) {
$this->set_cookie($this->cookie_configs['name'], $_COOKIE[$this->cookie_configs['name']], $expiryDate, $this->cookie_configs['path'], $this->cookie_configs['domain'], $this->cookie_configs['secure']);
$this->set_cookie(ITP_SAVER_COOKIE_NAME, $this->cookie_configs['name'] . "---_---" . $expiryDate, $expiryDate, $this->cookie_configs['path'], $this->cookie_configs['domain'], $this->cookie_configs['secure'], true);
}
if (!empty($this->cookie_configs['cookietypes'])) {
foreach ($this->cookie_configs['cookietypes'] as $cookietype_configs) {
$differentiatedCookieName = $this->cookie_configs['name'] . "_" . $cookietype_configs["cookie_suffix"];
if (isset($_COOKIE[$differentiatedCookieName])) {
$this->set_cookie($differentiatedCookieName, $_COOKIE[$differentiatedCookieName], $expiryDate, $this->cookie_configs['path'], $this->cookie_configs['domain'], $this->cookie_configs['secure']);
}
}
}
}
}
public function nsc_bar_set_default_cookies()
{
if ($this->plugin_configs->nsc_bar_get_option('activate_banner') == false) {
return;
}
if ($this->banner_configs_object->nsc_bar_get_cookie_setting("setDiffDefaultCookiesFirstPV", $this->plugin_configs->nsc_bar_return_settings_field_default_value("setDiffDefaultCookiesFirstPV")) == false) {
return;
}
$expiryDate = $this->get_expiry_date($this->cookie_configs['name'], $this->cookie_configs['expirydays']);
if (in_array($this->cookie_configs['compliance_type'], array("detailed", "detailedRev")) && !empty($this->cookie_configs['cookietypes'])) {
foreach ($this->cookie_configs['cookietypes'] as $cookietype_configs) {
$differentiatedCookieName = $this->cookie_configs['name'] . "_" . $cookietype_configs["cookie_suffix"];
$value = "deny";
if ($cookietype_configs["checked"] == "checked") {
$value = "allow";
}
if (!isset($_COOKIE[$differentiatedCookieName])) {
$this->set_cookie($differentiatedCookieName, $value, $expiryDate, $this->cookie_configs['path'], $this->cookie_configs['domain'], $this->cookie_configs['secure']);
}
}
}
}
public function nsc_bar_get_cookies_by_name($cookie_name)
{
$input_validation = new nsc_bar_input_validation();
if (isset($_COOKIE[$cookie_name])) {
return $input_validation->nsc_bar_sanitize_input($_COOKIE[$cookie_name]);
}
return null;
}
public function nsc_bar_delete_cookie_for_preview()
{
if ($this->plugin_configs->nsc_bar_get_option('activate_test_banner') == true &&
stripos($_SERVER["REQUEST_URI"], "page=nsc_bar-cookie-consent") !== false) {
$this->nsc_bar_delete_cookie();
}
}
//TODO: remove if everbody is a time on >2.1. needed for migration from 2.0 to 2.1, because the content of the cookieconsent_status cookie was changed in case of differentiated consent. from "detailed" to savesettings
public function nsc_bar_migrate_cookie_detailed_to_savesettings()
{
if (isset($_COOKIE[$this->cookie_configs['name']]) && $_COOKIE[$this->cookie_configs['name']] == "detailed") {
$expiryDate = $this->get_expiry_date($this->cookie_configs['name'], $this->cookie_configs['expirydays']);
$this->set_cookie($this->cookie_configs['name'], "savesettings", $expiryDate, $this->cookie_configs['path'], $this->cookie_configs['domain'], $this->cookie_configs['secure']);
}
}
public function nsc_bar_cookie_cleanup()
{
if (!isset($_COOKIE[$this->cookie_configs['name']])) {
return;
}
$input_validation = new nsc_bar_input_validation();
$current_cookie_value = $input_validation->nsc_bar_sanitize_input($_COOKIE[$this->cookie_configs['name']]);
if ($this->cookie_configs['compliance_type'] == "info" && $current_cookie_value != "dismiss") {
$this->nsc_bar_delete_cookie();
}
if (in_array($this->cookie_configs['compliance_type'], array("opt-in", "opt-out")) && !in_array($current_cookie_value, array("deny", "allow"))) {
$this->nsc_bar_delete_cookie();
}
if ($this->cookie_configs['compliance_type'] == "detailed" && !in_array($current_cookie_value, array("savesettings", "detailed"))) {
$this->nsc_bar_delete_cookie();
}
if ($this->cookie_configs['compliance_type'] == "detailedRev" && !in_array($current_cookie_value, array("savesettings", "allowall"))) {
$this->nsc_bar_delete_cookie();
}
if ($this->plugin_configs->nsc_bar_get_option('ask_until_acceptance') == "1" && $current_cookie_value == "deny") {
$this->nsc_bar_delete_cookie();
}
if ($this->plugin_configs->nsc_bar_get_option('ask_until_acceptance') == "1" && in_array($this->cookie_configs['compliance_type'], array("detailed", "detailedRev"))) {
foreach ($this->cookie_configs['cookietypes'] as $cookietype_configs) {
$differentiatedCookieName = $this->cookie_configs['name'] . "_" . $cookietype_configs["cookie_suffix"];
if (isset($_COOKIE[$differentiatedCookieName]) && $_COOKIE[$differentiatedCookieName] != "allow") {
$this->nsc_bar_delete_cookie(false);
}
}
}
}
public function nsc_bar_delete_cookie($delete_detailed = true)
{
if (isset($_COOKIE[$this->cookie_configs['name']])) {
unset($_COOKIE[$this->cookie_configs['name']]);
$this->set_cookie($this->cookie_configs['name'], "emptyvalue", time() - 3600, $this->cookie_configs['path'], $this->cookie_configs['domain']);
//delete itp saver cookie as well, if cookie is deleted
if (isset($_COOKIE[ITP_SAVER_COOKIE_NAME])) {
unset($_COOKIE[ITP_SAVER_COOKIE_NAME]);
$this->set_cookie(ITP_SAVER_COOKIE_NAME, "emptyvalue", time() - 3600, $this->cookie_configs['path'], $this->cookie_configs['domain']);
}
}
if ($delete_detailed === false) {
return;
}
if (!empty($this->cookie_configs['cookietypes'])) {
foreach ($this->cookie_configs['cookietypes'] as $cookietype_configs) {
$differentiatedCookieName = $this->cookie_configs['name'] . "_" . $cookietype_configs["cookie_suffix"];
if (isset($_COOKIE[$differentiatedCookieName])) {
unset($_COOKIE[$differentiatedCookieName]);
$this->set_cookie($differentiatedCookieName, "emptyvalue", time() - 3600, $this->cookie_configs['path'], $this->cookie_configs['domain']);
}
}
}
}
/*
will return the expirydate.
if the cookie was already handled by the saver it will return the original date stored in a cookie
if the cookie was never handled by this script it will create a fresh one.
*/
private function get_expiry_date($cookiename, $expiryDays)
{
$expiryDate = time() + 60 * 60 * 24 * $expiryDays;
if (!isset($_COOKIE[ITP_SAVER_COOKIE_NAME])) {
return $expiryDate;
}
$input_validation = new nsc_bar_input_validation();
$expiryCookie = $input_validation->nsc_bar_sanitize_input($_COOKIE[ITP_SAVER_COOKIE_NAME]);
$done_cookie_values = explode("---_---", $expiryCookie);
if (count($done_cookie_values) != 2) {
return $expiryDate;
}
if ($done_cookie_values[0] != $cookiename) {
return $expiryDate;
}
return $done_cookie_values[1];
}
private function get_banner_configs()
{
if (empty($this->banner_configs_object)) {
$this->banner_configs_object = new nsc_bar_banner_configs();
}
return $this->banner_configs_object;
}
private function get_plugin_configs()
{
if (empty($this->plugin_configs)) {
$this->plugin_configs = new nsc_bar_plugin_configs;
}
return $this->plugin_configs;
}
private function get_cookie_configs()
{
if (empty($this->cookie_configs)) {
$this->cookie_configs['name'] = $this->banner_configs_object->nsc_bar_get_cookie_setting("cookie_name", $this->plugin_configs->nsc_bar_return_settings_field_default_value("cookie_name"));
$this->cookie_configs['path'] = $this->banner_configs_object->nsc_bar_get_cookie_setting("cookie_path", "/");
$this->cookie_configs['domain'] = $this->banner_configs_object->nsc_bar_get_cookie_setting("cookie_domain", $this->plugin_configs->nsc_bar_return_settings_field_default_value("cookie_domain"));
$this->cookie_configs['expirydays'] = $this->banner_configs_object->nsc_bar_get_cookie_setting("cookie_expiryDays", $this->plugin_configs->nsc_bar_return_settings_field_default_value("cookie_expiryDays"));
$this->cookie_configs['secure'] = $this->banner_configs_object->nsc_bar_get_cookie_setting("cookie_secure", false);
$this->cookie_configs['cookietypes'] = $this->banner_configs_object->nsc_bar_get_cookie_setting("cookietypes", array());
$this->cookie_configs['compliance_type'] = $this->banner_configs_object->nsc_bar_get_cookie_setting("type", $this->plugin_configs->nsc_bar_return_settings_field_default_value("type"));
if (empty($this->cookie_configs['domain'])) {
$this->cookie_configs['domain'] = "";
}
}
return $this->cookie_configs;
}
private function set_cookie($name, $value, $expire, $path, $domain, $secure = false, $httpOnly = false)
{
if (version_compare(phpversion(), '7.3', '<')) {
setcookie($name, $value, $expire, $path . '; samesite=lax', $domain, $secure, $httpOnly);
return;
}
setcookie($name, $value, [
'expires' => $expire,
'path' => $path,
'domain' => $domain,
'samesite' => 'lax',
'secure' => $secure,
'httponly' => $httpOnly,
]);
}
}

View File

@@ -0,0 +1,99 @@
<?php
class nsc_bar_db_upgrader
{
public function nsc_bar_do_update()
{
$db_version = $this->get_database_version();
if ($db_version == NSC_BAR_PLUGIN_VERSION) {
// no update necessary
return;
}
$updated = $this->do_update_from_20_to_21($db_version);
$updated = $this->do_update_from_u20($db_version);
if ($updated === true) {
$this->set_database_version();
}
}
private function do_update_from_20_to_21($db_version)
{
if (version_compare($db_version, "2.1", ">=")) {
return true;
}
$banner_configs = new nsc_bar_banner_configs;
$config_array = $banner_configs->nsc_bar_get_banner_config_array();
$save = false;
$do_update = false;
// not all 2.0 version have set a db version. but only 2.0 should have cookietypes
if (isset($config_array["cookietypes"]) && empty($db_version)) {
$db_version = "2.0";
}
if (isset($config_array["type"]) && $config_array["type"] == "detailed") {
$do_update = true;
}
if (version_compare($db_version, "2.0", "=") == true && $do_update == true) {
if (isset($config_array["content"]) && isset($config_array["content"]["allow"])) {
$banner_configs->nsc_bar_update_banner_setting("content_savesettings", $config_array["content"]["allow"], "string");
$save = true;
}
}
if ($save) {
return $banner_configs->nsc_bar_save_banner_settings("xx");
} else {
return true;
}
}
private function do_update_from_u20($db_version)
{
// if empty we know its version < 2.0. because then was the db version introduced.
if (!empty($db_version)) {
return true;
}
$banner_configs = new nsc_bar_banner_configs;
$config_array = $banner_configs->nsc_bar_get_banner_config_array();
$save = false;
if ($config_array["type"] == "info" && !isset($config_array["revokable"])) {
$banner_configs->nsc_bar_update_banner_setting("revokable", false, "string");
$save = true;
}
if (isset($config_array["revokeBtn"]) && !isset($config_array["revokable"])) {
$banner_configs->nsc_bar_update_banner_setting("revokable", false, "string");
$save = true;
}
if (isset($config_array["revokeBtn"]) && strlen($config_array["revokeBtn"]) < 15) {
$banner_configs->nsc_bar_remove_revokeBtn();
$save = true;
}
if ($save) {
return $banner_configs->nsc_bar_save_banner_settings("xx");
} else {
return true;
}
}
private function set_database_version()
{
update_option(NSC_BAR_SLUG_DBVERSION, NSC_BAR_PLUGIN_VERSION);
}
private function get_database_version()
{
return get_option(NSC_BAR_SLUG_DBVERSION, null);
}
}

View File

@@ -0,0 +1,275 @@
<?php
class nsc_bar_frontend
{
private $json_config_string;
private $plugin_url;
private $plugin_configs;
public function __construct()
{
$this->plugin_url = NSC_BAR_PLUGIN_URL;
$this->active_tab = "";
$this->plugin_configs = new nsc_bar_plugin_configs();
$this->customized_font = false;
$this->cookietypes = array();
$this->cookie_name = "";
$this->compliance_type = "";
$this->pushToDl = "";
$this->custom_link = "";
$this->custom_link_new_window = "";
$this->improveBannerLoadingSpeed = false;
}
public function nsc_bar_set_json_configs($nsc_bar_banner_config)
{
$message = $nsc_bar_banner_config->nsc_bar_get_cookie_setting("content_message", false);
$filteredMessage = apply_filters('nsc_bar_cookie_bar_message', $message);
$nsc_bar_banner_config->nsc_bar_update_banner_setting("content_message", $filteredMessage, "string");
$this->json_config_string = $nsc_bar_banner_config->nsc_bar_get_banner_config_string(true);
$this->customized_font = $nsc_bar_banner_config->nsc_bar_get_cookie_setting("customizedFont", false);
$this->improveBannerLoadingSpeed = $nsc_bar_banner_config->nsc_bar_get_cookie_setting("improveBannerLoadingSpeed", false);
$this->cookietypes = $nsc_bar_banner_config->nsc_bar_get_cookie_setting("cookietypes", array());
$this->cookie_name = $nsc_bar_banner_config->nsc_bar_get_cookie_setting("cookie_name", $this->plugin_configs->nsc_bar_return_settings_field_default_value("cookie_name"));
$this->compliance_type = $nsc_bar_banner_config->nsc_bar_get_cookie_setting("type", $this->plugin_configs->nsc_bar_return_settings_field_default_value("type"));
$this->dataLayerName = $nsc_bar_banner_config->nsc_bar_get_cookie_setting("dataLayerName", $this->plugin_configs->nsc_bar_return_settings_field_default_value("dataLayerName"));
$this->pushToDl = $nsc_bar_banner_config->nsc_bar_get_cookie_setting("onStatusChange", $this->plugin_configs->nsc_bar_return_settings_field_default_value("onStatusChange"));
$this->container = $nsc_bar_banner_config->nsc_bar_get_cookie_setting("container", false);
$this->custom_link = $this->get_create_custom_link($nsc_bar_banner_config, false);
$this->custom_link_new_window = $this->get_create_custom_link($nsc_bar_banner_config, true);
}
public function nsc_bar_execute_frontend_wp_actions()
{
add_action('wp_enqueue_scripts', array($this, 'nsc_bar_enqueue_scripts'));
add_shortcode('cc_revoke_settings_link_nsc_bar', array($this, 'nsc_bar_shortcode_revoke_settings_link'));
add_shortcode('cc_show_cookie_banner_nsc_bar', array($this, 'nsc_bar_shortcode_show_cookie_banner'));
}
public function nsc_bar_enqueue_dataLayer_init_script()
{
$banner_active = $this->plugin_configs->nsc_bar_get_option('activate_banner');
$banner_active = apply_filters('nsc_bar_filter_banner_is_active', $banner_active);
if ($banner_active != true) {
return;
}
// stick to wp_print_scripts -> might cause caching problems otherwise.
add_action('wp_print_scripts', array($this, 'nsc_bar_get_dataLayer_banner_init_script'));
}
public function nsc_bar_get_dataLayer_banner_init_script($returnValue)
{
$nsc_bar_banner_config = new nsc_bar_banner_configs();
$this->pushToDl = $nsc_bar_banner_config->nsc_bar_get_cookie_setting("onStatusChange", $this->plugin_configs->nsc_bar_return_settings_field_default_value("onStatusChange"));
if ($this->pushToDl !== "1" && $returnValue !== true) {
return;
}
$this->cookie_name = $nsc_bar_banner_config->nsc_bar_get_cookie_setting("cookie_name", $this->plugin_configs->nsc_bar_return_settings_field_default_value("cookie_name"));
$this->cookietypes = $nsc_bar_banner_config->nsc_bar_get_cookie_setting("cookietypes", array());
$this->compliance_type = $nsc_bar_banner_config->nsc_bar_get_cookie_setting("type", $this->plugin_configs->nsc_bar_return_settings_field_default_value("type"));
$cookies = $this->get_consent_cookie_values();
$dataLayerValues = array();
foreach ($cookies as $cookie_name => $cookie_values) {
$cookie_value = $cookie_values["value"];
if (empty($cookie_value)) {
$cookie_value = $cookie_values["defaultValue"];
}
// goal: remove dismiss completly from application. Problem: is saved in cookie for "just info". For backward compatibility hard to change.
// first step: in datalayer dismiss will never appear.
if ($cookie_value === "dismiss") {
$cookie_value = "allow";
}
$key = esc_js($cookie_name);
$dataLayerValues[$key] = esc_js($cookie_value);
}
$dataLayerValues = apply_filters('nsc_bar_filter_data_layer_values', $dataLayerValues);
if ($returnValue === true) {
return $dataLayerValues;
}
$dataLayerValues["event"] = 'beautiful_cookie_consent_initialized';
$this->dataLayerName = $nsc_bar_banner_config->nsc_bar_get_cookie_setting("dataLayerName", $this->plugin_configs->nsc_bar_return_settings_field_default_value("dataLayerName"));
echo "<script>window." . esc_js($this->dataLayerName) . " = window." . esc_js($this->dataLayerName) . " || []; window." . esc_js($this->dataLayerName) . ".push(" . json_encode($dataLayerValues) . ")</script>";
}
public function nsc_bar_enqueue_scripts()
{
wp_register_style('nsc_bar_nice-cookie-consent', $this->plugin_url . 'public/cookieNSCconsent.min.css', array(), NSC_BAR_VERSION);
if (!empty($this->customized_font)) {
wp_add_inline_style('nsc_bar_nice-cookie-consent', '.cc-window { font-family: ' . str_replace("&#039;", "'", esc_html($this->customized_font)) . '}');
}
wp_enqueue_style('nsc_bar_nice-cookie-consent');
wp_register_script('nsc_bar_nice-cookie-consent_js', $this->plugin_url . 'public/cookieNSCconsent.min.js', array(), NSC_BAR_VERSION, true);
$eventListener = "load";
if ($this->improveBannerLoadingSpeed === "1") {
$eventListener = "DOMContentLoaded";
}
wp_add_inline_script("nsc_bar_nice-cookie-consent_js", 'window.addEventListener("' . $eventListener . '", function(){window.cookieconsent.initialise(' . $this->nsc_bar_json_with_js_function() . ')});');
wp_enqueue_script('nsc_bar_nice-cookie-consent_js');
}
public function nsc_bar_json_with_js_function()
{
$validator = new nsc_bar_input_validation();
$cleanedCookieTypes = $validator->esc_array_for_js($this->cookietypes);
$popUpCloseJsFunction = '"onPopupClose": function(){location.reload();}';
$pushToDLFunction = '"onStatusChange": function(status, chosenBefore) { var dataLayerName = "' . esc_js($this->dataLayerName) . '"; var cookieTypes = ' . json_encode($cleanedCookieTypes) . ';var cookieRootName = "' . esc_js($this->cookie_name) . '"; ' . file_get_contents(NSC_BAR_PLUGIN_DIR . "/public/onStatusChange.js") . '}';
$json_config_string_with_js = $this->json_config_string;
$json_config_string_with_js = apply_filters('nsc_bar_filter_json_config_string_before_js', $json_config_string_with_js);
if (!empty($this->container)) {
$setContainerPosition = '"container": document.querySelector("' . esc_js($this->container) . '")';
$json_config_string_with_js = str_replace(array('"container": "' . $this->container . '"', '"container":"' . $this->container . '"'), $setContainerPosition, $json_config_string_with_js);
}
if (is_admin()) {
$popUpCloseJsFunction = '"onPopupClose": function(){}';
}
$json_config_string_with_js = str_replace(array('"onPopupClose": "1"', '"onPopupClose":"1"'), $popUpCloseJsFunction, $json_config_string_with_js);
$json_config_string_with_js = str_replace(array('"onStatusChange": "1"', '"onStatusChange":"1"'), $pushToDLFunction, $json_config_string_with_js);
$json_config_string_with_js = str_replace('{{customLink}}', $this->custom_link, $json_config_string_with_js);
$json_config_string_with_js = str_replace('{{customLink_openNewWindow}}', $this->custom_link_new_window, $json_config_string_with_js);
$json_config_string_with_js = apply_filters('nsc_bar_filter_json_config_string_with_js', $json_config_string_with_js);
return $json_config_string_with_js;
}
public function nsc_bar_shortcode_show_cookie_banner()
{
$linktext = $this->plugin_configs->nsc_bar_get_option("shortcode_link_show_banner_text");
return "<a id='nsc_bar_link_show_banner' style='cursor: pointer;'>" . esc_html($linktext) . "</a>";
}
public function nsc_bar_shortcode_revoke_settings_link()
{
$banner_configs_obj = new nsc_bar_banner_configs();
$cookie_name = $banner_configs_obj->nsc_bar_get_cookie_setting("cookie_name", $this->plugin_configs->nsc_bar_return_settings_field_default_value("cookie_name"));
$cookie_domain = $banner_configs_obj->nsc_bar_get_cookie_setting("cookie_domain", $this->plugin_configs->nsc_bar_return_settings_field_default_value("cookie_domain"));
$cookie_expiry_days = $banner_configs_obj->nsc_bar_get_cookie_setting("cookie_expiryDays", $this->plugin_configs->nsc_bar_return_settings_field_default_value("cookie_expiryDays"));
$compliance_type = $banner_configs_obj->nsc_bar_get_cookie_setting("type", $this->plugin_configs->nsc_bar_return_settings_field_default_value("type"));
$link_text_opted_in = $this->plugin_configs->nsc_bar_get_option("shortcode_link_text_opted_in");
$link_text_opted_out = $this->plugin_configs->nsc_bar_get_option("shortcode_link_text_opted_out");
$wordpressUrl = get_bloginfo('url');
$hostname = parse_url($wordpressUrl, PHP_URL_HOST);
if ($hostname == "localhost") {
$domain[0] = "localhost";
} else {
$hostname = $cookie_domain;
}
switch ($compliance_type) {
case "opt-out":
$current_cookie_value = "allow";
break;
case "opt-in":
$current_cookie_value = "deny";
break;
default:
return null;
}
if (isset($_COOKIE[$cookie_name]) && $current_cookie_value != "dismiss") {
$current_cookie_value = $_COOKIE[$cookie_name];
}
switch ($current_cookie_value) {
case "allow":
$linktext = $link_text_opted_in;
$linktext_after_click = $link_text_opted_out;
$cookie_value_after_click = "deny";
break;
case "deny":
$linktext = $link_text_opted_out;
$linktext_after_click = $link_text_opted_in;
$cookie_value_after_click = "allow";
break;
default:
$linktext = "";
$linktext_after_click = "";
$cookie_value_after_click = "";
}
if (isset($_COOKIE[$cookie_name]) && $current_cookie_value != $_COOKIE[$cookie_name]) {
$linktext = $atts['link_text_opted_out'];
}
$expire = time() + 60 * 60 * 24 * $cookie_expiry_days;
$js_code = preg_replace("/\r|\n/", "", file_get_contents(NSC_BAR_PLUGIN_DIR . "/public/revoke_shortcode.js"));
return "<a id='nsc_bar_optout_link' data-link_text_after_click='" . esc_attr($linktext_after_click) . "' data-link_text_before_click='" . esc_attr($linktext) . "' data-cookiename='" . esc_attr($cookie_name) . "' data-current_cookie_value='" . esc_attr($current_cookie_value) . "'data-cookie_value_after_click='" . esc_attr($cookie_value_after_click) . "' data-expires='" . esc_attr($expire) . "' data-domain='" . esc_attr($hostname) . "' style='cursor: pointer;' onclick='" . $js_code . "'>" . esc_html($linktext) . "</a>";
}
private function get_consent_cookie_values()
{
if (empty($this->cookietypes)) {
return false;
}
$numberOfCookies = count($this->cookietypes);
$cookieHandler = new nsc_bar_cookie_handler();
$dataLayerEntries = array();
$dataLayerEntries["cookieconsent_status"] = array("value" => $cookieHandler->nsc_bar_get_cookies_by_name($this->cookie_name), "defaultValue" => $this->calculate_default_consent_setting());
if ($this->compliance_type !== "detailed" && $this->compliance_type !== "detailedRev") {
return $dataLayerEntries;
}
for ($i = 0; $i < $numberOfCookies; $i++) {
$cookie_name = $this->cookie_name . "_" . $this->cookietypes[$i]["cookie_suffix"];
$dataLayerEntries["cookieconsent_status_" . $this->cookietypes[$i]["cookie_suffix"]] = array("value" => $cookieHandler->nsc_bar_get_cookies_by_name($cookie_name), "defaultValue" => $this->calculate_default_consent_setting($this->cookietypes[$i]));
}
return $dataLayerEntries;
}
private function calculate_default_consent_setting($cookietype = array())
{
if ($this->compliance_type === "opt-in") {
return "deny";
}
if ($this->compliance_type === "opt-out" || $this->compliance_type === "info") {
return "allow";
}
if (empty($cookietype)) {
return "nochoice";
}
if ($cookietype["checked"] === "checked") {
return "allow";
}
return "deny";
}
private function get_create_custom_link($nsc_bar_banner_config, $targetBlank)
{
$link = $nsc_bar_banner_config->nsc_bar_get_cookie_setting("content_hrefsecond", false);
$link_text = $nsc_bar_banner_config->nsc_bar_get_cookie_setting("content_linksecond", false);
$link_html = "";
$target = "";
if ($targetBlank === true) {
$target = " target='_blank'";
}
if (!empty($link) && !empty($link_text)) {
$link_html = "<a class='cc-link' id='nsc-bar-customLink'" . $target . " href='" . $link . "'>" . $link_text . "</a>";
}
return $link_html;
}
}

View File

@@ -0,0 +1,119 @@
<?php
class nsc_bar_html_formfields
{
private $field;
private $prefix;
public function nsc_bar_return_form_field($field, $prefix)
{
$this->field = $field;
$this->prefix = $prefix;
switch ($this->field->type) {
case "checkbox":
return $this->create_checkbox();
break;
case "textarea":
return $this->create_textarea();
break;
case "text":
return $this->create_text();
break;
case "longtext":
return $this->create_text("long");
break;
case "select":
return $this->create_select();
break;
case "radio":
return $this->create_radio();
break;
case "hidden":
return $this->create_hidden_field();
break;
default:
return $this->field->pre_selected_value;
break;
}
}
public function nsc_bar_get_language_dropdown()
{
if (class_exists("nsc_bara_html_formfields_addon") === true) {
$form_fields_addon = new nsc_bara_html_formfields_addon();
return $form_fields_addon->nsc_bara_get_language_dropdown();
}
return '<select name="nsc_bar_language_selector" id="nsc_bar_countries_select"><option value="xx">Default</option></select>';
}
private function create_checkbox()
{
$checkbox = '<input ' . $this->nsc_bar_is_disabled($this->field) . ' id="ff_' . $this->prefix . $this->field->field_slug . '" type="checkbox" name="' . $this->prefix . $this->field->field_slug . '" id="' . $this->prefix . $this->field->field_slug . '" value="1" ' . checked(1, $this->field->pre_selected_value, false) . '>';
if (empty($this->nsc_bar_is_disabled($this->field)) === true) {
$checkbox = '<input type="hidden" name="' . $this->prefix . $this->field->field_slug . '_hidden" value="0"/>' . $checkbox;
}
return '<label>' . $checkbox . '</label>';
}
private function create_textarea()
{
return '<label><textarea ' . $this->nsc_bar_is_disabled($this->field) . ' id="ff_' . $this->prefix . $this->field->field_slug . '" cols="120" id="' . $this->prefix . $this->field->field_slug . '" name="' . $this->prefix . $this->field->field_slug . '" rows="20" class="large-text code" type="textarea">' . $this->convert_to_string($this->field->pre_selected_value) . '</textarea></label>';
}
private function create_hidden_field()
{
return "<input type='hidden' id='ff_" . $this->prefix . $this->field->field_slug . "' name='" . $this->prefix . $this->field->field_slug . "_hidden' value='" . $this->convert_to_string($this->field->pre_selected_value) . "'/>";
}
private function create_text($length = "short")
{
$size = 20;
if ($length == "long") {
$size = 50;
}
return '<label><input ' . $this->nsc_bar_is_disabled($this->field) . ' id="ff_' . $this->prefix . $this->field->field_slug . '" type="text" id="' . $this->prefix . $this->field->field_slug . '" name="' . $this->prefix . $this->field->field_slug . '" size="' . $size . '" maxlength="200" value="' . $this->field->pre_selected_value . '"></label>';
}
private function create_select()
{
$html = '<select ' . $this->nsc_bar_is_disabled($this->field) . ' id="ff_' . $this->prefix . $this->field->field_slug . '" name="' . $this->prefix . $this->field->field_slug . '" id="' . $this->prefix . $this->field->field_slug . '">';
foreach ($this->field->selectable_values as $selectable_value) {
$select = "";
if ($selectable_value->value == $this->field->pre_selected_value) {$select = "selected";}
$html .= '<option value="' . $selectable_value->value . '" ' . $select . '>' . $selectable_value->name . '</option>';
}
$html .= "</select>";
return '<label>' . $html . '</label>';
}
private function create_radio()
{
$html = "";
foreach ($this->field->selectable_values as $selectable_value) {
$select = "";
if ($selectable_value->value == $this->field->pre_selected_value) {$select = "checked";}
$html .= '<input ' . $this->nsc_bar_is_disabled($this->field) . ' id="ff_' . $this->prefix . $this->field->field_slug . '" type="radio" name="' . $this->prefix . $this->field->field_slug . '" value="' . $selectable_value->value . '" ' . $select . ' > ' . $selectable_value->name . ' ';
}
return '<label>' . $html . '</label>';
}
private function convert_to_string($input)
{
if (!is_string($input)) {
return json_encode($input);
}
return $input;
}
private function nsc_bar_is_disabled($field)
{
if (class_exists("nsc_bara_html_formfields_addon") !== true) {
return "";
}
$form_fields_addon = new nsc_bara_html_formfields_addon();
return $form_fields_addon->nsc_bara_is_disabled($field);
}
}

View File

@@ -0,0 +1,196 @@
<?php
class nsc_bar_input_validation
{
private $admin_error_obj;
public function __construct()
{
$this->admin_error_obj = new nsc_bar_admin_error;
}
public function nsc_bar_validate_field_custom_save($extra_validation_value, $input)
{
$return = $this->nsc_bar_sanitize_input($input);
switch ($extra_validation_value) {
case "nsc_bar_check_input_color_code":
$return = $this->nsc_bar_check_input_color_code($return);
break;
case "nsc_bar_check_input_json_settings":
$return = $this->nsc_bar_check_input_json_settings($return);
break;
case "nsc_bar_check_valid_json_string":
$return = $this->nsc_bar_check_valid_json_string($return);
break;
case "nsc_bar_check_cookietypes":
$return = $this->nsc_bar_check_cookietypes($return);
break;
case "nsc_bar_replace_doublequote_with_single":
$return = $this->nsc_bar_replace_doublequote_with_single($return);
break;
case "nsc_bar_integer":
$return = $this->nsc_bar_integer($return);
break;
case "nsc_bara_custom_services":
$return = $this->nsc_bar_bara_custom_services($return);
break;
}
$return = apply_filters('nsc_bar_filter_input_validation', $return, $extra_validation_value);
return $return;
}
public function nsc_bar_sanitize_input($input)
{
$cleandValue = stripslashes($input);
return sanitize_text_field($cleandValue);
}
public function nsc_bar_bara_custom_services($input)
{
$testedJson = $this->nsc_bar_check_valid_json_string($input);
if (empty($testedJson)) {
return null;
}
if (class_exists("nsc_bara_input_validation")) {
$bara_validation = new nsc_bara_input_validation;
return $bara_validation->nsc_bara_custom_services($testedJson);
}
return null;
}
public function nsc_bar_integer($input)
{
$valid = preg_match("/^[0-9]*$/", $input);
if (empty($valid) && $input != "") {
$this->admin_error_obj->nsc_bar_set_admin_error("Number could not be saved. Please provide an integer. Your input: " . $input);
$input = "";
}
$this->admin_error_obj->nsc_bar_display_errors();
return $input;
}
public function nsc_bar_check_input_color_code($input)
{
return $input;
}
public function nsc_bar_replace_doublequote_with_single($input)
{
return str_replace(['"', "\""], "'", $input);
}
public function nsc_bar_check_valid_json_string($json_string)
{
if ($json_string == "1") {
return null;
}
$php_version_good = $this->php_version_good();
switch ($php_version_good) {
case true:
$tested_json_string = json_encode(json_decode($json_string), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
break;
default:
$tested_json_string = json_encode(json_decode($json_string));
break;
}
if (empty($tested_json_string) || $tested_json_string == "null") {
$this->admin_error_obj->nsc_bar_set_admin_error("Please provide a valid json string. Data was not saved.");
return null;
} else {
return $tested_json_string;
}
}
public function php_version_good($minVersion = '5.4.0')
{
if (version_compare(phpversion(), $minVersion, '>=')) {
return true;
} else {
return false;
}
}
public function nsc_bar_check_cookietypes($input)
{
//should be an impossible case, because default settings have cookie types and the frontend js makes it impossible to delete all cookie types.
if (empty($input)) {
//$this->admin_error_obj->nsc_bar_set_admin_error("Please provide at least one cookie type.");
//$this->admin_error_obj->nsc_bar_display_errors();
//TODO: if all installation are >= v2.0 change this line to "return null" and uncomment lines above.
$input = '[{"label": "Technical","checked": "checked","disabled":"disabled","cookie_suffix":"tech"}]';
}
$valid = $this->nsc_bar_check_valid_json_string($input);
if (empty($valid)) {
$this->admin_error_obj->nsc_bar_display_errors();
return $this->get_old_cookietype_value();
}
$arr_cookietypes = json_decode($valid, true);
foreach ($arr_cookietypes as $arr_cookietype) {
if (preg_match('/^[a-z_]+$/', $arr_cookietype["cookie_suffix"]) === 0) {
$this->admin_error_obj->nsc_bar_set_admin_error("Cookie suffix must be only lowercase letter and underscores.");
return $this->get_old_cookietype_value();
}
if (strlen($arr_cookietype["cookie_suffix"]) > 10) {
$this->admin_error_obj->nsc_bar_set_admin_error("Cookie suffix must only have ten characters.");
return $this->get_old_cookietype_value();
}
}
return $valid;
}
private function get_old_cookietype_value()
{
$banner_configs = new nsc_bar_banner_configs;
$banner_config_array = $banner_configs->nsc_bar_get_banner_config_array();
$old_value = null;
if (isset($banner_config_array["cookietypes"])) {
$old_value = json_encode($banner_config_array["cookietypes"]);
}
return $old_value;
}
public function nsc_bar_check_input_json_settings($input)
{
$valid = $this->nsc_bar_check_valid_json_string($input);
if (empty($valid)) {
$banner_configs = new nsc_bar_banner_configs;
$old_value = $banner_configs->nsc_bar_get_banner_config_string();
$this->admin_error_obj->nsc_bar_display_errors();
return $old_value;
}
return $input;
}
public function esc_array_for_js($array_to_escape)
{
$escapedArray = array();
foreach ($array_to_escape as $key => $value) {
$escKey = esc_js($key);
if (!is_array($value)) {
$escValue = esc_js($value);
$escapedArray[$escKey] = $escValue;
}
if (is_array($value)) {
foreach ($value as $key_of_v => $value_of_v) {
$escKey_v = esc_js($key_of_v);
$escValue_v = esc_js($value_of_v);
$escapedArray[$escKey][$escKey_v] = $escValue_v;
}
}
}
return $escapedArray;
}
public function return_errors_obj()
{
return $this->admin_error_obj;
}
}

View File

@@ -0,0 +1,54 @@
<?php
class nsc_bar_on_activation
{
public function nsc_bar_do_update()
{
$db_version = $this->get_database_version();
$updated = true;
// if empty we know its version < 2.0. because then was the db version introduced.
if (empty($db_version)) {
$updated = $this->do_update_to_20();
}
if ($updated === true && NSC_BAR_PLUGIN_VERSION != $db_version) {
$this->set_database_version();
}
}
private function do_update_to_20()
{
$banner_configs = new nsc_bar_banner_configs;
$config_array = $banner_configs->nsc_bar_get_banner_config_array();
$save = false;
if ($config_array["type"] == "info") {
$banner_configs->nsc_bar_update_banner_setting("revokable", false, "string");
$banner_configs->nsc_bar_remove_revokeBtn();
$save = true;
}
if (isset($config_array["revokeBtn"])) {
$banner_configs->nsc_bar_update_banner_setting("revokable", false, "string");
$banner_configs->nsc_bar_remove_revokeBtn();
$save = true;
}
if ($save) {
return $banner_configs->nsc_bar_save_banner_settings();
} else {
return true;
}
}
private function set_database_version()
{
update_option(NSC_BAR_SLUG_DBVERSION, NSC_BAR_PLUGIN_VERSION);
}
private function get_database_version()
{
return get_option(NSC_BAR_SLUG_DBVERSION, null);
}
}

View File

@@ -0,0 +1,191 @@
<?php
class nsc_bar_plugin_configs
{
private $settingsFile;
private $settings_as_object;
private $settings_as_object_without_db;
private $active_tab;
public function nsc_bar_get_option($option_slug)
{
$option_value = false;
$settings_for_options = $this->nsc_bar_return_plugin_settings_without_db_settings();
foreach ($settings_for_options->setting_page_fields->tabs as $tab) {
foreach ($tab->tabfields as $field) {
if (strpos($field->field_slug, $option_slug . "_") === 0) {
$option_slug = $field->field_slug;
}
if ($field->field_slug == $option_slug || strpos($option_slug, "bannersettings_json_") === 0) {
$option_value = get_option($settings_for_options->plugin_prefix . $option_slug, $field->pre_selected_value);
break;
}
}
}
return $option_value;
}
public function nsc_bar_update_option($option_name, $option_value)
{
$settings_for_options = $this->nsc_bar_return_plugin_settings_without_db_settings();
$option_name_with_prefix = $settings_for_options->plugin_prefix . $option_name;
return update_option($option_name_with_prefix, $option_value, true);
}
public function nsc_bar_delete_option($option_name)
{
$settings_for_options = $this->nsc_bar_return_plugin_settings_without_db_settings();
$option_name_with_prefix = $settings_for_options->plugin_prefix . $option_name;
delete_option($option_name_with_prefix);
}
public function nsc_bar_return_plugin_settings()
{
if (empty($this->settings_as_object)) {
$this->settings_as_object = $this->nsc_bar_return_plugin_settings_without_db_settings();
$this->add_current_setting_values();
$this->add_html_description_templates();
$this->add_bannersettings_json();
}
return $this->settings_as_object;
}
public function nsc_bar_plugin_prefix()
{
$this->nsc_bar_return_plugin_settings_without_db_settings();
return $this->settings_as_object_without_db->plugin_prefix;
}
public function nsc_bar_return_settings_field_default_value($searched_field_slug)
{
$settings_field = $this->return_settings_field($searched_field_slug);
return $settings_field->pre_selected_value;
}
public function nsc_bar_replace_variables_in_config($varname, $replace_value)
{
$configs = $this->nsc_bar_return_plugin_settings();
$configs_string = json_encode($configs);
$configs_string = str_replace("{{" . $varname . "}}", $replace_value, $configs_string);
$this->settings_as_object = json_decode($configs_string);
}
public function nsc_bar_return_plugin_settings_without_db_settings()
{
if (empty($this->settings_as_object_without_db)) {
$this->settings_as_object_without_db = $this->set_settings_as_object();
if (empty($this->settings_as_object_without_db)) {
throw new Exception($this->settingsFile . " was not readable. Make sure it contains valid json.");
}
}
return $this->settings_as_object_without_db;
}
private function return_settings_field($searched_field_slug)
{
$this->nsc_bar_return_plugin_settings();
foreach ($this->settings_as_object->setting_page_fields->tabs as $tab) {
$number_of_fields = count($tab->tabfields);
for ($i = 0; $i < $number_of_fields; $i++) {
if ($tab->tabfields[$i]->field_slug == $searched_field_slug) {
return $tab->tabfields[$i];
}
}
}
}
private function set_settings_as_object()
{
$this->settingsFile = NSC_BAR_PLUGIN_DIR . "/plugin-configs.json";
$settings = file_get_contents($this->settingsFile);
$settings = json_decode($settings);
$settings = apply_filters('nsc_bar_plugin_settings_as_an_object', $settings);
if (empty($settings)) {
throw new Exception($this->settingsFile . " was not readable. Make sure it contains valid json.");
}
if (class_exists("nsc_bara_addon_configs")) {
$bara = new nsc_bara_addon_configs;
$settings = $bara->nsc_bara_add_addon_settings($settings);
}
return $settings;
}
private function get_active_tab()
{
$this->active_tab = "";
if (isset($_GET["tab"])) {
$this->active_tab = sanitize_text_field($_GET["tab"]);
} else {
$this->active_tab = $this->settings_as_object->setting_page_fields->tabs[0]->tab_slug;
}
}
private function add_html_description_templates()
{
$number_of_tabs = count($this->settings_as_object->setting_page_fields->tabs);
for ($t = 0; $t < $number_of_tabs; $t++) {
$this->settings_as_object->setting_page_fields->tabs[$t]->tab_description = $this->get_tab_description_tipps_template($t, "tab_description");
$this->settings_as_object->setting_page_fields->tabs[$t]->tab_tipps = $this->get_tab_description_tipps_template($t, "tab_tipps");
}
}
private function get_tab_description_tipps_template($tab_index, $type)
{
// TODO: needed for premium add-on versions without this tab <= 1.2.1
if (isset($this->settings_as_object->setting_page_fields->tabs[$tab_index]->$type) === false) {
return "";
}
if (strpos($this->settings_as_object->setting_page_fields->tabs[$tab_index]->$type, ".html") === false ||
!file_exists(NSC_BAR_PLUGIN_DIR . "/admin/tpl/" . $this->settings_as_object->setting_page_fields->tabs[$tab_index]->$type)) {
return $this->settings_as_object->setting_page_fields->tabs[$tab_index]->$type;
}
return file_get_contents(NSC_BAR_PLUGIN_DIR . "/admin/tpl/" . $this->settings_as_object->setting_page_fields->tabs[$tab_index]->$type);
}
// this fuctions gets the value saved in wordpress db using get_option
// and adds it to the settings object in the pre_selected_value field.
// if no value is set it sets the default value from settings file.
private function add_current_setting_values()
{
$banner_configs = new nsc_bar_banner_configs;
$this->get_active_tab();
$this->settings_as_object->setting_page_fields->active_tab_slug = $this->active_tab;
$number_of_tabs = count($this->settings_as_object->setting_page_fields->tabs);
for ($t = 0; $t < $number_of_tabs; $t++) {
$number_of_fields_in_this_tab = count($this->settings_as_object->setting_page_fields->tabs[$t]->tabfields);
if ($this->active_tab == $this->settings_as_object->setting_page_fields->tabs[$t]->tab_slug) {
$this->settings_as_object->setting_page_fields->tabs[$t]->active = true;
$this->settings_as_object->setting_page_fields->active_tab_index = $t;
}
for ($f = 0; $f < $number_of_fields_in_this_tab; $f++) {
$default_value = $this->settings_as_object->setting_page_fields->tabs[$t]->tabfields[$f]->pre_selected_value;
$field_slug_without_prefix = $this->settings_as_object->setting_page_fields->tabs[$t]->tabfields[$f]->field_slug;
if ($this->settings_as_object->setting_page_fields->tabs[$t]->tabfields[$f]->save_in_db === false) {
$this->settings_as_object->setting_page_fields->tabs[$t]->tabfields[$f]->pre_selected_value = $banner_configs->nsc_bar_get_cookie_setting($field_slug_without_prefix, $default_value);
continue;
}
$field_slug = $this->settings_as_object->plugin_prefix . $field_slug_without_prefix;
$this->settings_as_object->setting_page_fields->tabs[$t]->tabfields[$f]->pre_selected_value = get_option($field_slug, $default_value);
}
}
}
private function add_bannersettings_json()
{
$banner_configs = new nsc_bar_banner_configs;
foreach ($this->settings_as_object->setting_page_fields->tabs as $tabindex => $tab) {
foreach ($tab->tabfields as $fieldindex => $field) {
if ($field->field_slug === "bannersettings_json") {
//value from DB or default file:
$json_config = $banner_configs->nsc_bar_get_banner_config_string();
$this->settings_as_object->setting_page_fields->tabs[$tabindex]->tabfields[$fieldindex]->pre_selected_value = $json_config;
break (2);
}
}
}
}
}

View File

@@ -0,0 +1,118 @@
<?php
class nsc_bar_save_form_fields
{
private $plugin_settings;
private $banner_configs_obj;
private $plugin_configs;
public function __construct()
{
$this->plugin_configs = new nsc_bar_plugin_configs();
$this->plugin_settings = $this->plugin_configs->nsc_bar_return_plugin_settings_without_db_settings();
$this->banner_configs_obj = new nsc_bar_banner_configs();
}
public function nsc_bar_save_submitted_form_fields()
{
$updated = $this->save_settings(null);
$saved_language_configs = $this->banner_configs_obj->nsc_bar_get_banner_config_array();
$this->override_other_addon_configs($updated, $saved_language_configs);
//needed for testing
return $saved_language_configs;
}
private function save_settings($addon_settings)
{
$tabs = $this->plugin_settings->setting_page_fields->tabs;
$plugin_prefix = $this->plugin_settings->plugin_prefix;
$validate = new nsc_bar_input_validation;
$banner_settings_updated = false;
$configs_updated = false;
foreach ($tabs as $tab_index => $tab) {
foreach ($tab->tabfields as $tabfield_index => $tabfield) {
$tabfield_slug = $plugin_prefix . $tabfield->field_slug;
$manager_string = $this->value_save_manager($tabfield, $tabfield_slug, $addon_settings);
if ($manager_string === "update_banner") {
$post_value = isset($_POST[$tabfield_slug]) ? $_POST[$tabfield_slug] : $_POST[$tabfield_slug . "_hidden"];
$new_value = $validate->nsc_bar_validate_field_custom_save($tabfield->extra_validation_name, $post_value);
$this->banner_configs_obj->nsc_bar_update_banner_setting($tabfield->field_slug, $new_value, $tabfield->save_as);
$banner_settings_updated = true;
}
if ($manager_string === "update_wp_option") {
$post_value = isset($_POST[$tabfield_slug]) ? $_POST[$tabfield_slug] : $_POST[$tabfield_slug . "_hidden"];
$new_value = $validate->nsc_bar_validate_field_custom_save($tabfield->extra_validation_name, $post_value);
$this->plugin_configs->nsc_bar_update_option($tabfield->field_slug, $new_value);
$configs_updated = true;
}
}
}
if ($banner_settings_updated) {
$this->banner_configs_obj->nsc_bar_save_banner_settings();
}
$validate->return_errors_obj()->nsc_bar_display_errors();
if ($banner_settings_updated === true || $configs_updated === true) {
return true;
}
return false;
}
private function override_other_addon_configs($updated, $saved_language_configs)
{
if (class_exists("nsc_bara_save_form_fields_addon") !== true) {
return false;
}
$bara = new nsc_bara_save_form_fields_addon;
if ($bara->nsc_bara_must_i_save_other_languages($updated) !== true) {
return false;
}
$bara_banner_configs = new nsc_bara_banner_configs_addon;
$language_configs = $bara->nsc_bara_get_all_languages_configs($saved_language_configs);
foreach ($language_configs as $lang => $language_config) {
$addon_settings = $bara->nsc_bara_get_addon_settings("override", $lang);
$this->banner_configs_obj->nsc_bar_set_banner_config_array($language_config);
// to know where to save
$this->banner_configs_obj->nsc_bar_set_banner_configs_slug($bara_banner_configs->nsc_bara_get_banner_settings_slug($addon_settings));
$this->save_settings($addon_settings);
}
}
private function value_save_manager($tabfield, $tabfield_slug, $addon_settings)
{
if (
$tabfield->save_in_db === false &&
$this->save_field_with_data_from_post($tabfield, $addon_settings) &&
(isset($_POST[$tabfield_slug]) || isset($_POST[$tabfield_slug . "_hidden"]))
) {
return "update_banner";
}
if (
$tabfield->save_in_db === true &&
$this->save_field_with_data_from_post($tabfield, $addon_settings) &&
(isset($_POST[$tabfield_slug]) || isset($_POST[$tabfield_slug . "_hidden"]))
) {
return "update_wp_option";
}
return "skip";
}
private function save_field_with_data_from_post($tabfield, $addon_settings)
{
if (class_exists("nsc_bara_save_form_fields_addon") === true) {
$save_fields_addon = new nsc_bara_save_form_fields_addon();
return $save_fields_addon->nsc_bara_save_field_with_data_from_post($tabfield, $addon_settings);
}
return true;
}
}

View File

@@ -0,0 +1,37 @@
<?php
class nsc_bar_uninstaller
{
public function nsc_bar_deleteOptions()
{
$plugin_configs = new nsc_bar_plugin_configs;
$settings = $plugin_configs->nsc_bar_return_plugin_settings_without_db_settings();
foreach ($settings->setting_page_fields->tabs as $tab) {
foreach ($tab->tabfields as $fields) {
$plugin_configs->nsc_bar_delete_option($fields->field_slug);
}
}
delete_option(NSC_BAR_SLUG_DBVERSION);
// delete options of add on if add on is not there
if (class_exists("nsc_bara_addon_configs") === false) {
$translated_settings = $this->nsc_bar_get_all_nsc_bar_settings();
foreach ($translated_settings as $name) {
delete_option($name);
}
}
}
private function nsc_bar_get_all_nsc_bar_settings()
{
global $wpdb;
$options = $wpdb->get_results("select * from $wpdb->options where option_name like 'nsc_bar_%'");
$names = array();
foreach ($options as $option) {
$names[] = $option->option_name;
}
return $names;
}
}