Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
32.14% covered (danger)
32.14%
9 / 28
25.00% covered (danger)
25.00%
2 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
Settings
32.14% covered (danger)
32.14%
9 / 28
25.00% covered (danger)
25.00%
2 / 8
119.24
0.00% covered (danger)
0.00%
0 / 1
 get_log_level
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 get_plugin_name
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_plugin_slug
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_plugin_basename
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 get_plugin_version
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
4.13
 is_valid_version_string
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_plugin_url
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 get_plugin_dir
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2/**
3 * Object containing the plugin settings.
4 *
5 * @package    brianhenryie/bh-wp-bitcoin-gateway
6 */
7
8namespace BrianHenryIE\WP_Bitcoin_Gateway\API;
9
10use BrianHenryIE\WP_Bitcoin_Gateway\Admin\Plugins_Page;
11use BrianHenryIE\WP_Bitcoin_Gateway\Integrations\WooCommerce\Bitcoin_Gateway;
12use BrianHenryIE\WP_Bitcoin_Gateway\Integrations\WooCommerce\Frontend_Assets;
13use BrianHenryIE\WP_Bitcoin_Gateway\WP_Logger\Logger_Settings_Interface;
14use BrianHenryIE\WP_Bitcoin_Gateway\WP_Logger\Logger_Settings_Trait;
15use BrianHenryIE\WP_Bitcoin_Gateway\WP_Logger\WooCommerce_Logger_Settings_Interface;
16use BrianHenryIE\WP_Bitcoin_Gateway\Settings_Interface;
17use Psr\Log\LogLevel;
18
19/**
20 * Plain object pulling setting from wp_options.
21 */
22class Settings implements Settings_Interface, WooCommerce_Logger_Settings_Interface {
23    use Logger_Settings_Trait;
24
25    /**
26     * The minimum severity of logs to record.
27     *
28     * @see LogLevel
29     */
30    public function get_log_level(): string {
31        $default_log_level = LogLevel::NOTICE;
32        /**
33         * TODO: sync plugin log level with gateway option.
34         *
35         * @see Bitcoin_Gateway::process_admin_options()
36         */
37        $saved_setting = get_option( 'bh_wp_bitcoin_gateway_log_level', $default_log_level );
38        $log_levels    = array( LogLevel::DEBUG, LogLevel::INFO, LogLevel::ERROR, LogLevel::NOTICE, LogLevel::WARNING, 'none' );
39        return in_array( $saved_setting, $log_levels, true ) ? $saved_setting : $default_log_level;
40    }
41
42    /**
43     * Plugin name for use by the logger in friendly messages printed to WordPress admin UI.
44     *
45     * @see Logger
46     */
47    public function get_plugin_name(): string {
48        return 'Bitcoin Gateway';
49    }
50
51    /**
52     * The plugin slug is used by the logger in file and URL paths.
53     */
54    public function get_plugin_slug(): string {
55        return 'bh-wp-bitcoin-gateway';
56    }
57
58    /**
59     * Used to add links on plugins.php.
60     *
61     * @used-by Plugins_Page
62     *
63     * @see Logger_Settings_Interface::get_plugin_basename()
64     */
65    public function get_plugin_basename(): string {
66        if (
67            defined( 'BH_WP_BITCOIN_GATEWAY_BASENAME' )
68            && is_string( constant( 'BH_WP_BITCOIN_GATEWAY_BASENAME' ) )
69        ) {
70            /** @var string $plugin_file_basename */
71            $plugin_file_basename = constant( 'BH_WP_BITCOIN_GATEWAY_BASENAME' );
72            return $plugin_file_basename;
73        }
74
75        return 'bh-wp-bitcoin-gateway/bh-wp-bitcoin-gateway.php';
76    }
77
78    /**
79     * The plugin version, as used in caching JS and CSS assets.
80     */
81    public function get_plugin_version(): string {
82        if (
83            defined( 'BH_WP_BITCOIN_GATEWAY_VERSION' )
84            && is_string( constant( 'BH_WP_BITCOIN_GATEWAY_VERSION' ) ) // @phpstan-ignore booleanAnd.rightAlwaysTrue
85            && $this->is_valid_version_string( constant( 'BH_WP_BITCOIN_GATEWAY_VERSION' ) )
86        ) {
87            return constant( 'BH_WP_BITCOIN_GATEWAY_VERSION' );
88        }
89        return '2.0.0';
90    }
91
92    /**
93     * Confirm the string is a ~semver version.
94     *
95     * @param string $version_string Assumed to be a version e.g. "1.2.3".
96     */
97    protected function is_valid_version_string( string $version_string ): bool {
98        return (bool) preg_match( '/^[0-9]+(\.[0-9]+)*([a-zA-Z0-9\-]+)?$/', $version_string );
99    }
100
101    /**
102     * Return the URL of the base of the plugin.
103     *
104     * @used-by Frontend_Assets::enqueue_scripts()
105     * @used-by Frontend_Assets::enqueue_styles()
106     */
107    public function get_plugin_url(): string {
108        if (
109            defined( 'BH_WP_BITCOIN_GATEWAY_URL' )
110            && is_string( constant( 'BH_WP_BITCOIN_GATEWAY_URL' ) )
111        ) {
112            /** @var string $plugin_url */
113            $plugin_url = constant( 'BH_WP_BITCOIN_GATEWAY_URL' );
114            return $plugin_url;
115        }
116        return plugins_url( $this->get_plugin_basename() );
117    }
118
119    /**
120     * Get the absolute path to the plugin root on the server filesystem, with trailingslash.
121     */
122    public function get_plugin_dir(): string {
123        if (
124            defined( 'BH_WP_BITCOIN_GATEWAY_PATH' )
125            && is_string( constant( 'BH_WP_BITCOIN_GATEWAY_PATH' ) )
126        ) {
127            /** @var string $plugin_dir */
128            $plugin_dir = constant( 'BH_WP_BITCOIN_GATEWAY_PATH' );
129            return $plugin_dir;
130        }
131        /**
132         * @see wp_plugin_directory_constants()
133         * @var string $wp_plugin_dir
134         */
135        $wp_plugin_dir = constant( 'WP_PLUGIN_DIR' );
136        return $wp_plugin_dir . '/' . plugin_dir_path( $this->get_plugin_basename() );
137    }
138}