Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
83.33% covered (warning)
83.33%
20 / 24
69.23% covered (warning)
69.23%
9 / 13
CRAP
0.00% covered (danger)
0.00%
0 / 1
Settings
83.33% covered (warning)
83.33%
20 / 24
69.23% covered (warning)
69.23%
9 / 13
18.34
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 get_expiry_age
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 get_add_autologin_for_admins_is_enabled
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 get_disallowed_subjects_regex_array
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_disallowed_subjects_regex_dictionary
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 get_should_use_wp_login
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_log_level
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_plugin_slug
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_plugin_version
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
 get_plugin_name
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_plugin_basename
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 get_klaviyo_private_api_key
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 is_magic_link_enabled
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Define the settings class.
4 *
5 * Includes constants for wp_option names and on construction retrieves data for wp_mail filter options.
6 *
7 * @link       https://BrianHenry.ie
8 * @since      1.0.0
9 *
10 * @package    bh-wp-autologin-urls
11 */
12
13namespace BrianHenryIE\WP_Autologin_URLs\API;
14
15use BrianHenryIE\WP_Autologin_URLs\Settings_Interface;
16use BrianHenryIE\WP_Autologin_URLs\WP_Logger\Logger_Settings_Interface;
17use BrianHenryIE\WP_Autologin_URLs\WP_Logger\Logger;
18use Psr\Log\LogLevel;
19
20/**
21 * Plain old typed object wrapping WordPress wp_options.
22 */
23class Settings implements Settings_Interface, Logger_Settings_Interface {
24
25    const ADMIN_ENABLED                   = 'bh_wp_autologin_urls_is_admin_enabled';
26    const SUBJECT_FILTER_REGEX_DICTIONARY = 'bh_wp_autologin_urls_subject_filter_regex_dictionary';
27    const LOG_LEVEL                       = 'bh_wp_autologin_urls_log_level';
28    const SHOULD_USE_WP_LOGIN             = 'bh_wp_autologin_urls_should_use_wp_login';
29    const MAGIC_LINK_ENABLED              = 'bh_wp_autologin_urls_magic_link_enabled';
30
31    const DEFAULT_LOG_LEVEL = LogLevel::NOTICE;
32
33    /**
34     * A dictionary of regex:notes, where the regex is applied to the email subject to
35     * disable adding the autologin code and the notes are for the admin UI to remind the
36     * user what the regex means.
37     *
38     * @var string[] array
39     */
40    protected $disallowed_subjects_regex_dictionary = array();
41
42    /**
43     * Queries WordPress options table for settings, provides default values and remedial validation.
44     *
45     * Settings constructor.
46     */
47    public function __construct() {
48
49        $this->disallowed_subjects_regex_dictionary = array(
50            '/^.*Login Details$/'                   => '[Example Site] Login Details',
51            '/^.*Your new password$/'               => 'Example Site Your new password',
52            '/^Password Reset Request.*$/'          => 'Password Reset Request for Example Site',
53            '/^Please complete your registration$/' => 'Please complete your registration',
54        );
55    }
56
57    /**
58     * The expiry time as used when creating the transient that stores the password hash.
59     *
60     * @return int The expiry time in seconds, as set on the settings page.
61     */
62    public function get_expiry_age(): int {
63        $expiry_time = get_option( 'bh_wp_autologin_urls_seconds_until_expiry', 604800 );
64        return intval( $expiry_time ) > 0 ? intval( $expiry_time ) : 604800;
65    }
66
67    /**
68     * The configuration setting as defined in the WordPress admin UI, saying if emails to admins should get autologin urls.
69     *
70     * @return bool Should the autologin code be added to urls in emails sent to admins?
71     */
72    public function get_add_autologin_for_admins_is_enabled(): bool {
73
74        $autologin_for_admins_is_enabled = get_option( self::ADMIN_ENABLED, 'admin_is_not_enabled' );
75        return 'admin_is_enabled' === $autologin_for_admins_is_enabled;
76    }
77
78    /**
79     * A list of regexes for email subjects that should not have autologin codes added.
80     *
81     * @return string[]
82     */
83    public function get_disallowed_subjects_regex_array(): array {
84        return array_keys( $this->get_disallowed_subjects_regex_dictionary() );
85    }
86
87    /**
88     * A dictionary of regexes and email subjects that should not have autologin codes added.
89     *
90     * @return string[]
91     */
92    public function get_disallowed_subjects_regex_dictionary(): array {
93
94        $disallowed_subjects_regex_dictionary = get_option( self::SUBJECT_FILTER_REGEX_DICTIONARY, $this->disallowed_subjects_regex_dictionary );
95
96        $disallowed_subjects_regex_dictionary = is_array( $disallowed_subjects_regex_dictionary ) ? $disallowed_subjects_regex_dictionary : $this->disallowed_subjects_regex_dictionary;
97
98        return $disallowed_subjects_regex_dictionary;
99    }
100
101    /**
102     * Change links to redirect form wp-login.php rather than going directly to the link.
103     *
104     * @return bool
105     */
106    public function get_should_use_wp_login(): bool {
107        return get_option( self::SHOULD_USE_WP_LOGIN, false ) === 'use_wp_login_is_enabled';
108    }
109
110    /**
111     * The PSR log level to print, defaults to Info.
112     *
113     * @return string
114     */
115    public function get_log_level(): string {
116
117        return get_option( self::LOG_LEVEL, self::DEFAULT_LOG_LEVEL );
118    }
119
120    /**
121     * The plugin slug as required by the logger.
122     *
123     * @used-by Logger
124     * @used-by Admin
125     *
126     * @return string
127     */
128    public function get_plugin_slug(): string {
129        return 'bh-wp-autologin-urls';
130    }
131
132    /**
133     * The plugin version, used for asset caching.
134     */
135    public function get_plugin_version(): string {
136        return defined( 'BH_WP_AUTOLOGIN_URLS_VERSION' )
137            ? constant( 'BH_WP_AUTOLOGIN_URLS_VERSION' )
138            : '2.4.2';
139    }
140
141    /**
142     * Plugin name for use by the logger in friendly messages printed to WordPress admin UI.
143     *
144     * @return string
145     * @see Logger
146     */
147    public function get_plugin_name(): string {
148        return 'Autologin URLs';
149    }
150
151    /**
152     * The plugin basename is used by the logger to add the plugins page action link.
153     * (and maybe for PHP errors)
154     *
155     * @return string
156     * @see Logger
157     */
158    public function get_plugin_basename(): string {
159        return defined( 'BH_WP_AUTOLOGIN_URLS_BASENAME' ) ? BH_WP_AUTOLOGIN_URLS_BASENAME : 'bh-wp-autologin-urls/bh-wp-autologin-urls.php';
160    }
161
162    /**
163     * Return the API key to query the Klaviyo API for user details, if entered.
164     */
165    public function get_klaviyo_private_api_key(): ?string {
166        return get_option( 'bh_wp_autologin_urls_klaviyo_private_key', null );
167    }
168
169    /**
170     * Enable/disable the magic link feature.
171     */
172    public function is_magic_link_enabled(): bool {
173        return 'magic_links_is_enabled' === get_option( self::MAGIC_LINK_ENABLED, 'magic_links_is_not_enabled' );
174    }
175}