PluginProbe
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 4.3.1
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v4.3.1
4.7.2 4.7.1 4.7.0 4.6.6 4.6.5 4.6.4 4.6.3 4.6.2 4.6.1 4.6.0 4.5.1 4.5.0 4.4.2 4.4.1 4.4.0 4.3.3 4.3.2 4.3.1 4.3.0 4.2.3 4.2.2 4.2.1 1.0.3 1.0.4 1.0.5 All 281 releases
surecart / app / src / WordPress / GoogleMapApiService.php
GoogleMapApiService.php
94 lines 1.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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