| 1 |
<?php |
| 2 |
namespace Depicter\Services; |
| 3 |
|
| 4 |
use Averta\Core\Utility\Arr; |
| 5 |
use Averta\Core\Utility\Data; |
| 6 |
use Depicter\Document\Helper\Helper; |
| 7 |
use Depicter\Utility\Sanitize; |
| 8 |
|
| 9 |
class SettingsManagerService |
| 10 |
{ |
| 11 |
protected array $generalSettings; |
| 12 |
|
| 13 |
protected array $paidSettings; |
| 14 |
|
| 15 |
protected function setGeneralSettings() |
| 16 |
{ |
| 17 |
$this->generalSettings = [ |
| 18 |
[ |
| 19 |
'type' => 'select', |
| 20 |
'id' => 'use_google_fonts', |
| 21 |
'label' => __( 'Google Fonts', 'depicter' ), |
| 22 |
'options' => [ |
| 23 |
'on' => __( 'Default (Enable)', 'depicter' ), |
| 24 |
'off' => __( 'Disable', 'depicter' ), |
| 25 |
'editor_only' => __( 'Load in Editor Only', 'depicter' ), |
| 26 |
'save_locally' => __( 'Save Locally', 'depicter' ) |
| 27 |
], |
| 28 |
'description' => __( 'Enable, disable, or save Google Fonts locally on your host.', 'depicter' ), |
| 29 |
'sanitize_callback' => function( $value ) { |
| 30 |
if ( in_array( $value, [ 'editor_only', 'save_locally' ] ) ) { |
| 31 |
return $value; |
| 32 |
} |
| 33 |
|
| 34 |
return Data::isTrue( $value ) ? 'on' : 'off'; |
| 35 |
} |
| 36 |
], |
| 37 |
[ |
| 38 |
'type' => 'select', |
| 39 |
'id' => 'resource_preloading', |
| 40 |
'label' => __( 'Resource Preloading', 'depicter' ), |
| 41 |
'options' => [ |
| 42 |
'on' => __( 'Default (Enable)', 'depicter' ), |
| 43 |
'off' => __( 'Disable', 'depicter' ) |
| 44 |
], |
| 45 |
'description' => __( 'Enable or disable preloading of website resources (images and CSS) for faster page load speed.', 'depicter' ), |
| 46 |
'sanitize_callback' => function( $value ) { |
| 47 |
return Data::isTrue( $value ) ? 'on' : 'off'; |
| 48 |
} |
| 49 |
], |
| 50 |
[ |
| 51 |
'type' => 'select', |
| 52 |
'id' => 'allow_unfiltered_data_upload', |
| 53 |
'label' => __( 'Allow SVG & JSON Upload?', 'depicter' ), |
| 54 |
'options' => [ |
| 55 |
'off' => __( 'Disable', 'depicter' ), |
| 56 |
'on' => __( 'Enable', 'depicter' ) |
| 57 |
], |
| 58 |
'description' => __( 'Attention! Allowing uploads of SVG or JSON files is a potential security risk.<br/>Although Depicter sanitizes such files, we recommend that you only enable this feature if you understand the security risks involved.', 'depicter' ), |
| 59 |
'sanitize_callback' => function( $value ) { |
| 60 |
return Data::isTrue( $value ) ? 'on' : 'off'; |
| 61 |
} |
| 62 |
], |
| 63 |
[ |
| 64 |
'type' => 'button', |
| 65 |
'id' => 'regenerate_css_flush_cache', |
| 66 |
'label' => __( 'Regenerate CSS & Flush Cache', 'depicter' ), |
| 67 |
'button_text' => __( 'Regenerate CSS & Flush Cache', 'depicter' ), |
| 68 |
'class' => 'button button-secondary depicter-flush-cache', |
| 69 |
'icon' => '<span class="dashicons dashicons-update" style="line-height:28px; margin-right:8px; height:28px;"></span>', |
| 70 |
'store_callback' => function( $value ) { |
| 71 |
try{ |
| 72 |
$documents = \Depicter::documentRepository()->select( [ 'id' ] )->findAll()->get(); |
| 73 |
if ( $documents ) { |
| 74 |
$documents = $documents->toArray(); |
| 75 |
foreach( $documents as $document ) { |
| 76 |
\Depicter::front()->render()->flushDocumentCache( $document['id'] ); |
| 77 |
} |
| 78 |
} |
| 79 |
return true; |
| 80 |
} catch( \Exception $e ){ |
| 81 |
return false; |
| 82 |
} |
| 83 |
} |
| 84 |
], |
| 85 |
[ |
| 86 |
'type' => 'checkbox', |
| 87 |
'id' => 'always_load_assets', |
| 88 |
'label' => __( 'Load assets on all pages?', 'depicter' ), |
| 89 |
'description' => "<br><br>". __( 'By default, Depicter will load corresponding JavaScript and CSS files on demand. but if you need to load assets on all pages, check this option. <br>(For example, if you plan to load Depicter via Ajax, you need to enable this option)', 'depicter' ), |
| 90 |
'sanitize_callback' => function( $value ) { |
| 91 |
return Data::isTrue($value); |
| 92 |
} |
| 93 |
] |
| 94 |
]; |
| 95 |
} |
| 96 |
|
| 97 |
protected function setPaidSettings() |
| 98 |
{ |
| 99 |
$this->paidSettings = [ |
| 100 |
[ |
| 101 |
'type' => 'text', |
| 102 |
'id' => 'google_recaptcha_client_key', |
| 103 |
'label' => __( 'Google Recaptcha (v3) Client key', 'depicter' ), |
| 104 |
'description' => "", |
| 105 |
], |
| 106 |
[ |
| 107 |
'type' => 'password', |
| 108 |
'id' => 'google_recaptcha_secret_key', |
| 109 |
'label' => __( 'Google Recaptcha (v3) Secret key', 'depicter' ), |
| 110 |
'description' => "", |
| 111 |
], |
| 112 |
[ |
| 113 |
'type' => 'number', |
| 114 |
'id' => 'google_recaptcha_score_threshold', |
| 115 |
'label' => __( 'Score Threshold', 'depicter' ), |
| 116 |
'default' => 0.6, |
| 117 |
'description' => __( 'reCAPTCHA v3 returns a score (1.0 is very likely a good interaction, 0.0 is very likely a bot). If the score less than or equal to this threshold, the form submission will be blocked and the message below will be displayed.', 'depicter' ), |
| 118 |
'sanitize_callback' => function( $value ) { |
| 119 |
return Sanitize::float( $value ); |
| 120 |
} |
| 121 |
], |
| 122 |
[ |
| 123 |
'type' => 'textarea', |
| 124 |
'id' => 'google_recaptcha_fail_message', |
| 125 |
'label' => __( 'Fail Message', 'depicter' ), |
| 126 |
'default' => __('Google reCAPTCHA verification failed, please try again later.', 'depicter' ), |
| 127 |
'description' => __( 'Displays to users who fail the verification process.', 'depicter'), |
| 128 |
], |
| 129 |
[ |
| 130 |
'type' => 'password', |
| 131 |
'id' => 'google_places_api_key', |
| 132 |
'label' => __( 'Google Places Api key', 'depicter' ), |
| 133 |
'description' => sprintf( |
| 134 |
/* translators: 1: opening anchor tag, 2: closing anchor tag */ |
| 135 |
__('To fetch and display reviews of a place on your website (Google Reviews), you need to provide %1$s a valid Google Places API key%2$s.', 'depicter' ), |
| 136 |
'<a href="https://docs.depicter.com/article/290-google-places-api-key" target="_blank">', |
| 137 |
'</a>' |
| 138 |
) |
| 139 |
] |
| 140 |
]; |
| 141 |
} |
| 142 |
|
| 143 |
protected function getGeneralSettings(): array |
| 144 |
{ |
| 145 |
return $this->generalSettings; |
| 146 |
} |
| 147 |
|
| 148 |
protected function getPaidSettings(): array |
| 149 |
{ |
| 150 |
return $this->paidSettings; |
| 151 |
} |
| 152 |
|
| 153 |
public function getAll(): array |
| 154 |
{ |
| 155 |
$this->setGeneralSettings(); |
| 156 |
$this->setPaidSettings(); |
| 157 |
|
| 158 |
$settings = $this->getGeneralSettings(); |
| 159 |
if ( \Depicter::auth()->isPaid() ) { |
| 160 |
$settings = Arr::merge( $this->getPaidSettings(), $settings ); |
| 161 |
} |
| 162 |
|
| 163 |
return $settings; |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* get setting by its id |
| 168 |
* |
| 169 |
* @param string $id |
| 170 |
* |
| 171 |
* @return array|null |
| 172 |
*/ |
| 173 |
public function get(string $id): ?array |
| 174 |
{ |
| 175 |
$settings = $this->getAll(); |
| 176 |
foreach ( $settings as $setting ) { |
| 177 |
if ( $setting['id'] == $id ) { |
| 178 |
return $setting; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
return null; |
| 183 |
} |
| 184 |
|
| 185 |
public function store( string $id, $value ): bool |
| 186 |
{ |
| 187 |
$setting = $this->get( $id ); |
| 188 |
if (!$setting) { |
| 189 |
return false; |
| 190 |
} |
| 191 |
|
| 192 |
if ( isset( $setting['store_callback'] ) && is_callable( $setting['store_callback'] ) ) { |
| 193 |
return call_user_func( $setting['store_callback'], $value ); |
| 194 |
} |
| 195 |
|
| 196 |
if ( isset( $setting['sanitize_callback'] ) && is_callable( $setting['sanitize_callback'] ) ) { |
| 197 |
$value = call_user_func( $setting['sanitize_callback'], $value ); |
| 198 |
} else { |
| 199 |
$value = Sanitize::textfield( $value ); |
| 200 |
} |
| 201 |
|
| 202 |
return \Depicter::options()->get( $id ) == $value || \Depicter::options()->set( $id, $value ); |
| 203 |
} |
| 204 |
|
| 205 |
public function getSettingValue(string $id) |
| 206 |
{ |
| 207 |
$setting = $this->get( $id ); |
| 208 |
if (!$setting) { |
| 209 |
return null; |
| 210 |
} |
| 211 |
return \Depicter::options()->get( $id ); |
| 212 |
} |
| 213 |
|
| 214 |
public function map($settings) { |
| 215 |
$mappedSettings = []; |
| 216 |
foreach ( $settings as $settingKey => $settingValue ) { |
| 217 |
if ( is_array( $settingValue ) ) { |
| 218 |
foreach ( $settingValue as $key => $value ) { |
| 219 |
$mappedSettings[ Helper::camelCaseToSnakeCase( $settingKey ) . '_' . Helper::camelCaseToSnakeCase( $key ) ] = $value; |
| 220 |
} |
| 221 |
} else { |
| 222 |
$mappedSettings[ Helper::camelCaseToSnakeCase( $settingKey ) ] = $settingValue; |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
return $mappedSettings; |
| 227 |
} |
| 228 |
} |
| 229 |
|