Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
50.00% covered (danger)
50.00%
6 / 12
60.00% covered (warning)
60.00%
3 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Logger_Settings_Trait
50.00% covered (danger)
50.00%
6 / 12
60.00% covered (warning)
60.00%
3 / 5
13.12
0.00% covered (danger)
0.00%
0 / 1
 get_log_level
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 get_plugin_name
100.00% covered (success)
100.00%
2 / 2
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_basename
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 get_cli_base
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Default function implementations for classes implementing `Logger_Settings_Interface`.
4 *
5 * These functions infer the current plugin's basename, name, log level.
6 *
7 * It is faster to provide your own implementation of each function.
8 *
9 * A Settings class should `implements Logger_Settings_Interface` and `use Logger_Settings_Trait`
10 * then override all the functions. This allows more functions to be added to the interface in future
11 * library updates without requiring projects to implement the new functions, i.e. it provides
12 * forward-compatibility.
13 *
14 * @package brianhenryie/bh-wp-logger
15 */
16
17namespace BrianHenryIE\WP_Logger;
18
19use BrianHenryIE\WP_Logger\WP_Includes\CLI;
20use Exception;
21use Psr\Log\LogLevel;
22
23/**
24 * Default function implementations for Logger_Settings_Interface.
25 *
26 * @see Logger_Settings_Interface
27 */
28trait Logger_Settings_Trait {
29
30    /**
31     * The log level to use.
32     *
33     * Default is Info.
34     * Looks for saved value in `get_option( 'my-plugin-slug_log_level' ).
35     * Returns `none` when the plugin basename cannot be determined.
36     *
37     * @see LogLevel
38     */
39    public function get_log_level(): string {
40        try {
41            return get_option( $this->get_plugin_slug() . '_log_level', LogLevel::INFO );
42        } catch ( \Exception ) {
43            return 'none';
44        }
45    }
46
47    /**
48     * The plugin friendly name to use in UIs.
49     *
50     * @throws Exception When the basename cannot be determined.
51     */
52    public function get_plugin_name(): string {
53        $plugin_data = get_plugin_data( $this->get_plugin_basename() );
54        return $plugin_data['Name'];
55    }
56
57    /**
58     * The plugin slug. I.e. the plugin directory name. Used in URLs and wp_options.
59     *
60     * @throws Exception When the basename cannot be determined.
61     */
62    public function get_plugin_slug(): string {
63        return explode( '/', $this->get_plugin_basename() )[0];
64    }
65
66    /**
67     * The plugin basename. Used to add the Logs link on `plugins.php`.
68     *
69     * @see https://core.trac.wordpress.org/ticket/42670
70     *
71     * @throws Exception When it cannot be determined. I.e. a symlink inside a symlink.
72     */
73    public function get_plugin_basename(): string {
74
75        // TODO: The following might work but there are known issues around symlinks that need to be tested and handled correctly.
76        // @see  https://core.trac.wordpress.org/ticket/42670
77
78        $wp_plugin_basename = plugin_basename( __DIR__ );
79
80        /** @var array<string, array<string>> $plugin_data */
81        $plugin_data = get_plugins( explode( '/', $wp_plugin_basename )[0] );
82
83        if ( 1 === count( $plugin_data ) ) {
84            return array_key_first( $plugin_data );
85        }
86
87        throw new Exception( 'Plugin installed in an unusual directory.' );
88    }
89
90    /**
91     * Default CLI commands to use the plugin slug as the base for commands.
92     *
93     * @see CLI
94     */
95    public function get_cli_base(): ?string {
96        return $this->get_plugin_slug();
97    }
98}