Admin
3 weeks ago
Builder
3 weeks ago
Customizer
3 weeks ago
Exceptions
3 weeks ago
Helpers
3 weeks ago
Integrations
3 weeks ago
Migrations
3 weeks ago
ReviewAlerts
3 weeks ago
Services
3 weeks ago
Settings
3 weeks ago
Support
3 weeks ago
Traits
3 weeks ago
Utils
3 weeks ago
AuthorizationStatusCheck.php
3 weeks ago
BusinessDataCache.php
3 weeks ago
Clear_Cache.php
3 weeks ago
Container.php
3 weeks ago
DisplayElements.php
3 weeks ago
Email_Notification.php
3 weeks ago
Error_Reporter.php
3 weeks ago
Feed.php
3 weeks ago
FeedCache.php
3 weeks ago
FeedCacheUpdater.php
3 weeks ago
FeedDisplay.php
3 weeks ago
Feed_Locator.php
3 weeks ago
Parser.php
3 weeks ago
PostAggregator.php
3 weeks ago
RemoteRequest.php
3 weeks ago
SBR_Education.php
3 weeks ago
SBR_Schema_Service.php
3 weeks ago
SBR_Settings.php
3 weeks ago
ServiceContainer.php
3 weeks ago
SinglePostCache.php
3 weeks ago
TemplateRenderer.php
3 weeks ago
Tooltip_Wizard.php
3 weeks ago
Util.php
3 weeks ago
SBR_Settings.php
214 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SmashBalloon\Reviews\Common; |
| 4 | |
| 5 | use Smashballoon\Customizer\V2\Feed_Saver; |
| 6 | |
| 7 | class SBR_Settings { |
| 8 | /** |
| 9 | * @var array |
| 10 | */ |
| 11 | protected $atts; |
| 12 | |
| 13 | /** |
| 14 | * @var array |
| 15 | */ |
| 16 | protected $db; |
| 17 | |
| 18 | /** |
| 19 | * @var array |
| 20 | */ |
| 21 | protected $settings; |
| 22 | |
| 23 | /** |
| 24 | * @var array |
| 25 | */ |
| 26 | protected $feed_type_and_terms; |
| 27 | |
| 28 | /** |
| 29 | * @var array |
| 30 | */ |
| 31 | protected $connected_accounts; |
| 32 | |
| 33 | /** |
| 34 | * @var array |
| 35 | */ |
| 36 | protected $connected_accounts_in_feed; |
| 37 | |
| 38 | /** |
| 39 | * @var string |
| 40 | */ |
| 41 | protected $transient_name; |
| 42 | |
| 43 | /** |
| 44 | * SBR_Settings constructor. |
| 45 | * |
| 46 | * Overwritten in the Pro version. |
| 47 | * |
| 48 | * @param array $atts shortcode settings |
| 49 | * @param array $db settings from the wp_options table |
| 50 | */ |
| 51 | public function __construct($atts, $db, $preview_settings = false) |
| 52 | { |
| 53 | $atts = is_array($atts) ? $atts : array(); |
| 54 | |
| 55 | if (! empty($atts['feed'])) { |
| 56 | $this->settings = self::get_settings_by_feed_id($atts['feed'], $preview_settings); |
| 57 | |
| 58 | if (! empty($this->settings)) { |
| 59 | $this->settings['customizer'] = isset($atts['customizer']) && $atts['customizer'] == true ? true : false; |
| 60 | $this->settings['feed'] = intval($atts['feed']); |
| 61 | |
| 62 | $this->settings['localization'] = Util::get_api_call_language($this->settings); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // convert string 'false' and 'true' to booleans |
| 67 | foreach ($atts as $key => $value) { |
| 68 | if ($value === 'false') { |
| 69 | $atts[ $key ] = false; |
| 70 | } elseif ($value === 'true') { |
| 71 | $atts[ $key ] = true; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | $this->atts = $atts; |
| 76 | $this->db = $db; |
| 77 | |
| 78 | $this->connected_accounts = isset($db['connected_accounts']) ? $db['connected_accounts'] : array(); |
| 79 | } |
| 80 | |
| 81 | |
| 82 | /** |
| 83 | * Get Settings By Feed ID |
| 84 | * |
| 85 | * @since 1.0 |
| 86 | */ |
| 87 | public static function get_settings_by_feed_id($feed_id, $preview_settings = false, $single_review = false) |
| 88 | { |
| 89 | global $wpdb; |
| 90 | |
| 91 | if (is_array($preview_settings)) { |
| 92 | return $preview_settings; |
| 93 | } |
| 94 | |
| 95 | if ($single_review && intval($feed_id) < 1) { |
| 96 | return sbr_settings_defaults(); |
| 97 | } |
| 98 | |
| 99 | if (intval($feed_id) < 1) { |
| 100 | return false; |
| 101 | } |
| 102 | $container = Container::get_instance(); |
| 103 | $feed_saver = $container->get(Feed_Saver::class); |
| 104 | $feed_saver->set_feed_id($feed_id); |
| 105 | |
| 106 | return $feed_saver->get_feed_settings(); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * @return array |
| 111 | * |
| 112 | * @since 1.0 |
| 113 | */ |
| 114 | public function get_settings() |
| 115 | { |
| 116 | return $this->settings; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * @return array |
| 121 | * |
| 122 | * @since 1.0 |
| 123 | */ |
| 124 | public function get_connected_accounts() |
| 125 | { |
| 126 | return $this->connected_accounts; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * @return array|bool |
| 131 | * |
| 132 | * @since 1.0 |
| 133 | */ |
| 134 | public function get_connected_accounts_in_feed() |
| 135 | { |
| 136 | if (isset($this->connected_accounts_in_feed)) { |
| 137 | return $this->connected_accounts_in_feed; |
| 138 | } else { |
| 139 | return false; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * @return bool|string |
| 145 | * |
| 146 | * @since 1.0 |
| 147 | */ |
| 148 | public function get_transient_name() |
| 149 | { |
| 150 | if (isset($this->transient_name)) { |
| 151 | return $this->transient_name; |
| 152 | } else { |
| 153 | return false; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * @return float|int |
| 159 | * |
| 160 | * @since 1.0 |
| 161 | */ |
| 162 | public function get_cache_time_in_seconds() |
| 163 | { |
| 164 | if ($this->db['caching_type'] === 'background') { |
| 165 | return SBR_CRON_UPDATE_CACHE_TIME; |
| 166 | } else { |
| 167 | //If the caching time doesn't exist in the database then set it to be 1 hour |
| 168 | $cache_time = isset($this->settings['cache_time']) ? (int)$this->settings['cache_time'] : 1; |
| 169 | $cache_time_unit = isset($this->settings['cache_time_unit']) ? $this->settings['cache_time_unit'] : 'hours'; |
| 170 | |
| 171 | //Calculate the cache time in seconds |
| 172 | if ($cache_time_unit == 'minutes') { |
| 173 | $cache_time_unit = 60; |
| 174 | } |
| 175 | if ($cache_time_unit == 'hours') { |
| 176 | $cache_time_unit = 60 * 60; |
| 177 | } |
| 178 | if ($cache_time_unit == 'days') { |
| 179 | $cache_time_unit = 60 * 60 * 24; |
| 180 | } |
| 181 | |
| 182 | $cache_time = max(900, $cache_time * $cache_time_unit); |
| 183 | |
| 184 | return $cache_time; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | public function update_settings($update_array = []) |
| 189 | { |
| 190 | if (!is_array($update_array)) { |
| 191 | return false; |
| 192 | } |
| 193 | |
| 194 | $updated = array_merge($this->settings, array_map(function ($value) { |
| 195 | return $this->convert_value($value); |
| 196 | }, $update_array)); |
| 197 | |
| 198 | return update_option('sbr_settings', $updated); |
| 199 | } |
| 200 | |
| 201 | private function convert_value($value) |
| 202 | { |
| 203 | switch ($value) { |
| 204 | case 'true': |
| 205 | return true; |
| 206 | case 'false': |
| 207 | return false; |
| 208 | default: |
| 209 | return $value; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | } |
| 214 |