* * @copyright (c) 2018, César Maeso (https://superadmin.es) */ /** * Settings class. * * @since 0.0.0 */ class ARGPD_Settings { /** * Parent plugin class. * * @var string * @since 0.0.0 */ protected $plugin = null; /** * key * * @var string * @since 0.0.0 */ protected $key= 'argpd'; /** * themes * * @var string * @since 0.0.0 */ protected $informbox_themes= null; /** * themes * * @var string * @since 0.0.0 */ protected $cookie_themes= null; /** * countries * * @var string * @since 0.0.0 */ protected $countries= null; /** * states * * @var string * @since 0.0.0 */ protected $states= null; /** * settings array * * @var string * @since 0.0.0 */ protected $settings = array( 'renuncia' => 0, // pages 'avisolegalID' => 0, 'privacidadID' => 0, 'cookiesID' => 0, 'avisolegalURL' => '', 'privacidadURL' => '', 'cookiesURL' => '', 'cookies-label' => '', 'cookies-theme' => 'modern-light', 'consentimiento-label' => '', 'informbox-theme' => 'simple', // owner 'dominio' => '', 'titular' => '', 'id-fiscal' => '', 'domicilio' => '', 'provincia' => '', 'provincia-code' => '', 'pais' => 'ES', 'pais-nombre' => '', 'pais-ue' => 1, 'correo' => '', // settings 'finalidad' => '', 'hosting-info' => '', // options 'option-comments' => 0, 'option-cookies' => 0, 'option-forms' => 0, 'option-footer' => 0, // clauses 'clause-exclusion' => 0, 'clause-thirdparty' => 0, 'clause-edad' => 0, 'clause-terceros' => 0, 'clause-protegidos' => 0, 'clause-portabilidad' => 0, // thirdparty 'thirdparty-dclick' => 0, 'thirdparty-ganalytics' => 0, 'thirdparty-social' => 0, 'thirdparty-mailchimp' => 0, ); /** * Constructor. * * @since 0.0.0 * * @param Main plugin object. */ public function __construct( $plugin ) { $this->informbox_themes= [ 'simple' => __('Simple', 'argpd'), 'border' => __('Con borde', 'argpd'), 'border-number' => __('Borde + Números', 'argpd'), ]; $this->cookie_themes= [ 'classic' => __('Clásico', 'argpd'), 'classic-top' => __('Clásico en parte superior', 'argpd'), 'modern-light' => __('Moderno Claro', 'argpd'), 'modern-dark' => __('Moderno Oscuro', 'argpd'), ]; $this->countries= [ 'AR' => __('Argentina', 'argpd'), 'CO' => __('Colombia', 'argpd'), 'CL' => __('Chile', 'argpd'), 'EC' => __('Ecuador', 'argpd'), 'ES' => __('España', 'argpd'), 'MX' => __('Méjico', 'argpd'), 'PE' => __('Perú', 'argpd'), 'VE' => __('Venezuela', 'argpd'), ]; $this->plugin = $plugin; $this->init_settings(); } /** * Init settings. * * @since 0.0.0 */ function init_settings() { // get all settings foreach ($this->settings as $name => $text) { $value= get_option(sprintf("%s_%s", $this->key, $name)); if ($value){ $this->settings[$name]= $value; } } // set domain value $this->settings['dominio']= get_site_url(); // get legal pages permalinks $cookiesID= intval($this->settings['cookiesID']); if (is_int($cookiesID) && $cookiesID >0) { $this->settings['cookiesURL']= get_permalink($cookiesID); } $avisolegalID= intval($this->settings['avisolegalID']); if (is_int($avisolegalID) && $avisolegalID >0) { $this->settings['avisolegalURL']= get_permalink($avisolegalID); } $privacidadID= intval($this->settings['privacidadID']); if (is_int($privacidadID) && $privacidadID >0) { $this->settings['privacidadURL']= get_permalink($privacidadID); } $this->convert_regional_codes(); } private function convert_regional_codes(){ // convert cc2 to string $cc2= $this->settings['pais']; foreach ($this->countries as $key => $value) { if ($key == $cc2){ $this->settings['pais-nombre']= $value; } } // is ue country $this->settings['pais-ue']= ($cc2 == 'ES')?1:0; // convert state-cc2 to string $state_code= $this->settings['provincia-code']; $states= $this->get_states($cc2); foreach ($states as $i) { if ($i['code'] == $state_code){ $this->settings['provincia']= $i['name']; } } } /** * Reset settings. * * @since 0.0.0 */ public function reset(){ $this->update_setting('clause-exclusion', 0); $this->update_setting('clause-terceros', 0); $this->update_setting('clause-edad', 0); $this->update_setting('clause-protegidos', 0); $this->update_setting('clause-portabilidad', 0); $this->update_setting('thirdparty-dclick', 0); $this->update_setting('thirdparty-ganalytics', 0); $this->update_setting('thirdparty-social', 0); $this->update_setting('thirdparty-mailchimp', 0); } /** * Returns all settings * * @return array * */ function get_settings(){ return $this->settings; } /** * Returns themes * * @return array * */ function get_cookie_themes(){ return $this->cookie_themes; } /** * Returns themes * * @return array * */ function get_informbox_themes(){ return $this->informbox_themes; } /** * Returns countries * * @return array * */ function get_countries(){ return $this->countries; } /** * Returns states * * @return array * */ function get_states($country){ $fn= sprintf("%s/../assets/json/%s.json", dirname(__FILE__), strtolower($country)); if (file_exists($fn)){ $str= file_get_contents($fn); $json = json_decode($str, true); // catch error if ($json === null && json_last_error() !== JSON_ERROR_NONE) { return array(); } $states= array(); foreach($json as $state){ //array_push($states, $state['name']); array_push($states, array('name' => $state['name'], 'code' => $state['code'])); } return $states; } return array(); } /** * Returns the value of given setting key, based on if network settings are enabled or not * * @param string $name Setting to fetch * @param string $default Default Value * * @return bool|mixed|void * */ function get_setting( $name = '', $default = false ) { //return "prueba"; if( empty( $name ) ) { return false; } return $this->settings[$name]; } /** * Update value for given setting key * * @param string $name Key * @param string $value Value * * @return bool If the setting was updated or not */ function update_setting( $name = '', $value = '' ) { if( empty( $name ) ) { return false; } $value= trim(sanitize_text_field($value)); if (update_site_option(sprintf("%s_%s", $this->key, $name), $value)){ $this->settings[$name]= $value; // ($name == 'provincia-code' || $name == 'pais') && $this->convert_regional_codes(); // return true; } return false; } }