Admin
2 months ago
Assets
2 weeks ago
CLI
2 years ago
Cache
2 weeks ago
Pages
10 months ago
PostTypes
2 months ago
Posts
1 year ago
Shortcodes
1 month ago
Taxonomies
1 year ago
Templates
1 year ago
Users
2 months ago
ActionsService.php
3 years ago
CompatibilityService.php
4 months ago
CurrencyService.php
4 months ago
GoogleMapApiService.php
2 months ago
HealthService.php
2 months ago
LineItemStateService.php
2 years ago
PluginActionLinksService.php
1 year ago
PluginService.php
1 year ago
PluginServiceProvider.php
1 year ago
RecaptchaValidationService.php
2 years ago
StateService.php
7 months ago
ThemeService.php
4 months ago
ThemeServiceProvider.php
7 months ago
TranslationsServiceProvider.php
3 months ago
UpgradeNoticeService.php
1 year ago
GoogleMapApiService.php
94 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\WordPress; |
| 4 | |
| 5 | use SureCart\Support\Encryption; |
| 6 | |
| 7 | /** |
| 8 | * Google Map Api Service. |
| 9 | */ |
| 10 | class GoogleMapApiService { |
| 11 | /** |
| 12 | * Google Map API Key Option Name. |
| 13 | */ |
| 14 | public const API_KEY_OPTION_NAME = 'surecart_google_map_api_key'; |
| 15 | |
| 16 | /** |
| 17 | * Google Map Enabled Option Name. |
| 18 | */ |
| 19 | public const API_KEY_ENABLED_OPTION_NAME = 'surecart_google_map_api_key_enabled'; |
| 20 | |
| 21 | /** |
| 22 | * Register settings. |
| 23 | */ |
| 24 | public function bootstrap() { |
| 25 | add_filter( 'pre_update_option_' . self::API_KEY_OPTION_NAME, [ $this, 'encryptSettings' ], 10, 3 ); |
| 26 | add_filter( 'option_' . self::API_KEY_OPTION_NAME, [ $this, 'decryptSettings' ], 10 ); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Encrypt the Google Map API key before saving it to the database. |
| 31 | * |
| 32 | * @param mixed $value New value. |
| 33 | * @param mixed $old_value Old value. |
| 34 | * @param string $option Option name. |
| 35 | * |
| 36 | * @return string |
| 37 | */ |
| 38 | public function encryptSettings( $value, $old_value, $option ) { |
| 39 | if ( empty( $value ) ) { |
| 40 | return $value; |
| 41 | } |
| 42 | |
| 43 | $encrypted = Encryption::encrypt( $value ); |
| 44 | if ( is_wp_error( $encrypted ) ) { |
| 45 | return $old_value; |
| 46 | } |
| 47 | |
| 48 | return $encrypted; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Decrypt the Google Map API key before returning it. |
| 53 | * |
| 54 | * @param mixed $value Value. |
| 55 | * |
| 56 | * @return string |
| 57 | */ |
| 58 | public function decryptSettings( $value ) { |
| 59 | if ( empty( $value ) || is_wp_error( $value ) ) { |
| 60 | return ''; |
| 61 | } |
| 62 | |
| 63 | $decrypted_value = Encryption::decrypt( $value ); |
| 64 | if ( empty( $decrypted_value ) || is_wp_error( $decrypted_value ) ) { |
| 65 | return ''; |
| 66 | } |
| 67 | |
| 68 | return $decrypted_value; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Is Google Map Enabled? |
| 73 | * |
| 74 | * @return boolean |
| 75 | */ |
| 76 | public function isEnabled() { |
| 77 | return (bool) get_option( self::API_KEY_ENABLED_OPTION_NAME, false ); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Get Google Map API key. |
| 82 | * |
| 83 | * @return string |
| 84 | */ |
| 85 | public function getApiKey() { |
| 86 | // If not enabled, return. |
| 87 | if ( ! $this->isEnabled() ) { |
| 88 | return ''; |
| 89 | } |
| 90 | |
| 91 | return get_option( self::API_KEY_OPTION_NAME, '' ); |
| 92 | } |
| 93 | } |
| 94 |