encryptor = new Encryptor(); } /** * Get option value (decrypted). * * @param string $key Key. * @param mixed $default Default. * @return mixed */ public function get( $key, $default = null ) { if ( isset( $this->gotten[ $key ] ) ) { return $this->gotten[ $key ]; } $all = get_option( $this->option_name, array() ); if ( ! is_array( $all ) || ! array_key_exists( $key, $all ) ) { return $default; } $value = $all[ $key ]; if ( ! is_string( $value ) ) { return $default; } $decrypted = $this->encryptor->decrypt( $value ); $this->gotten[ $key ] = $decrypted; return $decrypted; } /** * Set option value (encrypted). * * @param string $key Key. * @param string $value Plain value. * @return void */ public function set( $key, $value ) { $all = get_option( $this->option_name, array() ); if ( ! is_array( $all ) ) { $all = array(); } $all[ $key ] = $this->encryptor->encrypt( (string) $value ); unset( $this->gotten[ $key ] ); update_option( $this->option_name, $all, false ); } /** * Set multiple option values (encrypted) from an associative array. * * @param array $pairs Associative array of key => plain value. * @return void */ public function set_multi( $pairs ) { if ( ! is_array( $pairs ) ) { return; } $all = get_option( $this->option_name, array() ); if ( ! is_array( $all ) ) { $all = array(); } foreach ( $pairs as $key => $value ) { $all[ $key ] = $this->encryptor->encrypt( (string) $value ); unset( $this->gotten[ $key ] ); } update_option( $this->option_name, $all, false ); } public function facebook_user_access_token_is_valid() { // FIXME: if there is already a valud system user token, return true if ( $this->get( 'system_user_token', '' ) ) { return true; } $expires_at = $this->get( 'user_access_token_expires_at', 0 ); if ( $expires_at < time() ) { return false; } return true; } public function get_selected_business(): array|null { $business = $this->get( 'selected_business', '' ); if ( $business === '' ) { return null; } $business_parts = explode(',', $business); if ( count( $business_parts ) !== 3 ) { return null; } return [ 'business_id' => $business_parts[0], 'waba_id' => $business_parts[1], 'phone_id' => $business_parts[2], ]; } public function delete($key) { $all = get_option( $this->option_name, array() ); if ( ! is_array( $all ) ) { $all = array(); } unset( $all[ $key ], $this->gotten[ $key ] ); delete_option( $this->option_name ); add_option( $this->option_name, $all ); } public function delete_all() { delete_option( $this->option_name ); } }