*/ private array $demo_import; private string $license_used_domains; private bool $is_dev_mode; /** * @var array */ private array $git_repositories; private bool $is_delete_data_on_deactivation; private bool $is_automatic_reports_disabled; private bool $is_automatic_reports_confirmed; public function __construct( Options $options ) { $this->options = $options; $this->version = ''; $this->license = ''; $this->license_expiration = ''; $this->license_used_domains = ''; $this->demo_import = array(); $this->is_dev_mode = false; $this->git_repositories = array(); $this->is_delete_data_on_deactivation = false; $this->is_automatic_reports_disabled = false; $this->is_automatic_reports_confirmed = false; } /** * @param array $git_repositories * * @return array */ protected function validate_git_repositories_array( array $git_repositories ): array { $valid_git_repositories = array(); foreach ( $git_repositories as $git_repository ) { if ( false === is_array( $git_repository ) || false === key_exists( 'id', $git_repository ) || false === key_exists( 'accessToken', $git_repository ) || false === key_exists( 'name', $git_repository ) ) { continue; } $valid_git_repositories[] = array( 'id' => $this->get_string_arg( 'id', $git_repository ), 'accessToken' => $this->get_string_arg( 'accessToken', $git_repository ), 'name' => $this->get_string_arg( 'name', $git_repository ), ); } return $valid_git_repositories; } public function load(): void { $option_settings = $this->options->get_option( Options::OPTION_SETTINGS ); $settings = is_array( $option_settings ) ? $option_settings : array(); $this->version = $this->get_string_arg( 'version', $settings ); $this->license = $this->get_string_arg( 'license', $settings ); $this->license_expiration = $this->get_string_arg( 'licenseExpiration', $settings ); $this->license_used_domains = $this->get_string_arg( 'licenseUsedDomains', $settings ); $this->demo_import = $this->get_array_arg( 'demoImport', $settings ); $this->is_dev_mode = $this->get_bool_arg( 'isDevMode', $settings ); $this->is_delete_data_on_deactivation = $this->get_bool_arg( 'isDeleteDataOnDeactivation', $settings ); $this->is_automatic_reports_disabled = $this->get_bool_arg( 'isWithoutAutomaticReports', $settings ); $this->is_automatic_reports_confirmed = $this->get_bool_arg( 'isAutomaticReportsConfirmed', $settings ); if ( true === isset( $settings['gitRepositories'] ) ) { $this->git_repositories = true === is_array( $settings['gitRepositories'] ) ? $this->validate_git_repositories_array( $settings['gitRepositories'] ) : array(); } } public function save(): void { $settings = array( 'version' => $this->version, 'license' => $this->license, 'licenseExpiration' => $this->license_expiration, 'licenseUsedDomains' => $this->license_used_domains, 'demoImport' => $this->demo_import, 'isDevMode' => $this->is_dev_mode, 'isDeleteDataOnDeactivation' => $this->is_delete_data_on_deactivation, 'gitRepositories' => $this->git_repositories, 'isWithoutAutomaticReports' => $this->is_automatic_reports_disabled, 'isAutomaticReportsConfirmed' => $this->is_automatic_reports_confirmed, ); $this->options->update_option( Options::OPTION_SETTINGS, $settings ); } // setters / getters. public function set_version( string $version ): void { $this->version = $version; } public function get_version(): string { return $this->version; } public function set_license( string $license ): void { $this->license = $license; } public function get_license(): string { return $this->license; } public function is_license_expired(): bool { if ( '' === $this->license_expiration ) { return false; } $expiration_date = DateTime::createFromFormat( 'Ymd', $this->license_expiration ); if ( false === $expiration_date ) { return false; } return $expiration_date < new DateTime(); } public function is_license_expires_within_month(): bool { if ( '' === $this->license_expiration ) { return false; } $expiration_date = DateTime::createFromFormat( 'Ymd', $this->license_expiration ); if ( false === $expiration_date ) { return false; } $month = new DateTime(); $month->modify( '+1 month' ); return $expiration_date < $month; } public function is_active_license(): bool { return '' !== $this->license && '' !== $this->license_expiration && ! $this->is_license_expired(); } public function is_delete_data_on_deactivation(): bool { return $this->is_delete_data_on_deactivation; } public function set_license_expiration( string $license_expiration ): void { $this->license_expiration = $license_expiration; } public function get_license_expiration( string $format = '' ): string { if ( '' === $format || '' === $this->license_expiration ) { return $this->license_expiration; } $expiration = DateTime::createFromFormat( 'Ymd', $this->license_expiration ); if ( false === $expiration ) { return ''; } return $expiration->format( $format ); } public function set_license_used_domains( string $license_used_domains ): void { $this->license_used_domains = $license_used_domains; } public function get_license_used_domains(): string { return $this->license_used_domains; } /** * @return array */ public function get_git_repositories(): array { return $this->git_repositories; } /** * @return array{id:string, accessToken:string, name:string} */ public function get_git_repository_info_by_id( string $repository_id ): ?array { foreach ( $this->get_git_repositories() as $git_repository ) { if ( $git_repository['id'] !== $repository_id ) { continue; } return $git_repository; } return null; } public function is_automatic_reports_disabled(): bool { return $this->is_automatic_reports_disabled; } public function is_automatic_reports_confirmed(): bool { return $this->is_automatic_reports_confirmed; } /** * @param array $demo_import */ public function set_demo_import( array $demo_import ): void { $this->demo_import = $demo_import; } /** * @return array */ public function get_demo_import(): array { return $this->demo_import; } public function is_dev_mode(): bool { return $this->is_dev_mode; } public function set_is_dev_mode( bool $is_dev_mode ): void { $this->is_dev_mode = $is_dev_mode; } public function set_is_delete_data_on_deactivation( bool $is_delete_data_on_deactivation ): void { $this->is_delete_data_on_deactivation = $is_delete_data_on_deactivation; } /** * @param array $git_repositories */ public function set_git_repositories( array $git_repositories ): void { $this->git_repositories = $this->validate_git_repositories_array( $git_repositories ); } public function set_is_automatic_reports_disabled( bool $is_automatic_reports_disabled ): void { $this->is_automatic_reports_disabled = $is_automatic_reports_disabled; } public function set_is_automatic_reports_confirmed( bool $is_automatic_reports_confirmed ): void { $this->is_automatic_reports_confirmed = $is_automatic_reports_confirmed; } public function maybe_delete_data_on_deactivation(): void { if ( false === $this->is_delete_data_on_deactivation ) { return; } $this->options->delete_option( Options::OPTION_SETTINGS ); $this->options->delete_transient( Options::TRANSIENT_DEACTIVATED_OTHER_INSTANCES ); $this->options->delete_transient( Options::TRANSIENT_LICENSE_EXPIRATION_DISMISS ); } }