| 1 |
<?php // phcps:ignore |
| 2 |
|
| 3 |
namespace OPTN\Includes; |
| 4 |
|
| 5 |
/** |
| 6 |
* Settings |
| 7 |
* |
| 8 |
* @since 1.0.0 |
| 9 |
* @package optin |
| 10 |
* @subpackage optin/includes |
| 11 |
*/ |
| 12 |
class Settings { |
| 13 |
|
| 14 |
/** |
| 15 |
* Default settings. |
| 16 |
* |
| 17 |
* @var array |
| 18 |
*/ |
| 19 |
private static $default_settings = array( |
| 20 |
|
| 21 |
// Integrations. |
| 22 |
'int_enable_fluentcrm' => false, |
| 23 |
'int_enable_mailpoet' => false, |
| 24 |
'int_enable_webhook' => true, |
| 25 |
|
| 26 |
'int_bg_enable_ga' => false, |
| 27 |
'int_bg_enable_zapier' => false, |
| 28 |
|
| 29 |
// Global. |
| 30 |
'global_google_fonts' => true, |
| 31 |
'global_ip_tracking' => true, |
| 32 |
'global_alt_render_strategy' => false, |
| 33 |
); |
| 34 |
|
| 35 |
/** |
| 36 |
* Init settings |
| 37 |
* |
| 38 |
* @param string $version settings version. |
| 39 |
* @return void |
| 40 |
*/ |
| 41 |
public static function init_settings( $version ) { |
| 42 |
|
| 43 |
$settings = self::get_settings(); |
| 44 |
|
| 45 |
if ( empty( $settings ) ) { |
| 46 |
$settings = self::$default_settings; |
| 47 |
$settings['version'] = $version; |
| 48 |
update_option( 'optn_settings', $settings, true ); |
| 49 |
} else { |
| 50 |
foreach ( self::$default_settings as $key => $value ) { |
| 51 |
if ( ! isset( $settings[ $key ] ) ) { |
| 52 |
$settings[ $key ] = $value; |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
if ( ! isset( $settings['version'] ) ) { |
| 57 |
$settings['version'] = $version; |
| 58 |
} |
| 59 |
|
| 60 |
update_option( 'optn_settings', $settings, true ); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Set Optin settings |
| 66 |
* |
| 67 |
* @param string $key key of setting. |
| 68 |
* @param mixed $value value of setting. |
| 69 |
* @return void |
| 70 |
*/ |
| 71 |
public static function set_settings( $key, $value ) { |
| 72 |
$settings = self::get_settings(); |
| 73 |
$settings[ $key ] = $value; |
| 74 |
update_option( 'optn_settings', $settings, true ); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Get Optin Settings |
| 79 |
* |
| 80 |
* @param string|null $key key of setting. |
| 81 |
* @return array |
| 82 |
*/ |
| 83 |
public static function get_settings( $key = null ) { |
| 84 |
$settings = get_option( 'optn_settings', array() ); |
| 85 |
|
| 86 |
if ( ! empty( $key ) ) { |
| 87 |
return isset( $settings[ $key ] ) ? $settings[ $key ] : null; |
| 88 |
} |
| 89 |
|
| 90 |
return $settings; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Set Settings |
| 95 |
* |
| 96 |
* @param array $new_settings new settings. |
| 97 |
* @return void |
| 98 |
*/ |
| 99 |
public static function update_settings( $new_settings ) { |
| 100 |
|
| 101 |
$curr_settings = self::get_settings(); |
| 102 |
|
| 103 |
foreach ( $new_settings as $key => $value ) { |
| 104 |
if ( isset( $curr_settings[ $key ] ) ) { |
| 105 |
|
| 106 |
if ( is_string( $value ) || is_numeric( $value ) || is_bool( $value ) ) { |
| 107 |
|
| 108 |
if ( is_string( $value ) ) { |
| 109 |
$value = sanitize_key( $value ); |
| 110 |
} |
| 111 |
|
| 112 |
$curr_settings[ $key ] = $value; |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
update_option( 'optn_settings', $curr_settings, true ); |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Get all the global settings |
| 122 |
* |
| 123 |
* @return array |
| 124 |
*/ |
| 125 |
public static function get_global_settings() { |
| 126 |
|
| 127 |
$settings = self::get_settings(); |
| 128 |
$filterd_settings = array(); |
| 129 |
|
| 130 |
foreach ( $settings as $key => $value ) { |
| 131 |
if ( str_starts_with( $key, 'global_' ) ) { |
| 132 |
$filterd_settings[ $key ] = $value; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
return $filterd_settings; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Get all the settings related to integration |
| 141 |
* |
| 142 |
* @return array |
| 143 |
*/ |
| 144 |
public static function get_integration_settings() { |
| 145 |
|
| 146 |
$settings = self::get_settings(); |
| 147 |
$wp_plugins = array(); |
| 148 |
|
| 149 |
foreach ( $settings as $key => $value ) { |
| 150 |
if ( str_starts_with( $key, 'int_enable_' ) || str_starts_with( $key, 'int_bg_enable_' ) ) { |
| 151 |
$wp_plugins[ $key ] = $value; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
return $wp_plugins; |
| 156 |
} |
| 157 |
} |
| 158 |
|