Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Plugins_Page
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
2 / 2
7
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 add_logs_action_link
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
6
1<?php
2/**
3 * Changes on WordPress plugins.php.
4 *
5 * Adds a Logs link to the plugin's entry.
6 * Formats that link <strong> if there are unviewed logs.
7 *
8 * e.g. /wp-admin/admin.php?page=bh-wp-logger-development-plugin-logs.
9 *
10 * @package brianhenryie/bh-wp-logger
11 */
12
13namespace BrianHenryIE\WP_Logger\Admin;
14
15use BrianHenryIE\WP_Logger\API_Interface;
16use BrianHenryIE\WP_Logger\Logger_Settings_Interface;
17use Psr\Log\LoggerAwareTrait;
18use Psr\Log\LoggerInterface;
19use Psr\Log\NullLogger;
20
21/**
22 * Class Plugins_Page
23 */
24class Plugins_Page {
25
26    use LoggerAwareTrait;
27
28    /**
29     * Needed for the plugin slug.
30     *
31     * @var Logger_Settings_Interface
32     */
33    protected Logger_Settings_Interface $settings;
34
35    /**
36     * Needed for the log file path.
37     *
38     * @var API_Interface
39     */
40    protected $api;
41
42    /**
43     * Plugins_Page constructor.
44     *
45     * @param API_Interface             $api The logger's main functions.
46     * @param Logger_Settings_Interface $settings The logger settings.
47     * @param ?LoggerInterface          $logger The logger itself.
48     */
49    public function __construct( API_Interface $api, Logger_Settings_Interface $settings, ?LoggerInterface $logger = null ) {
50
51        $this->setLogger( $logger ?? new NullLogger() );
52        $this->settings = $settings;
53        $this->api      = $api;
54    }
55
56    /**
57     * Adds 'Logs' link to under the plugin name on plugins.php.
58     * Attempts to place it immediately before the deactivate link.
59     *
60     * @hooked plugin_action_links_{plugin basename}
61     * @see \WP_Plugins_List_Table::display_rows()
62     *
63     * @param array<int|string, string>  $action_links The existing plugin links (usually "Deactivate").
64     * @param string                     $_plugin_basename The plugin's directory/filename.php.
65     * @param array<string, string|bool> $_plugin_data Associative array including PluginURI, slug, Author, Version. See `get_plugin_data()`.
66     * @param string                     $_context     The plugin context. By default this can include 'all', 'active', 'inactive',
67     *                                                'recently_activated', 'upgrade', 'mustuse', 'dropins', and 'search'.
68     *
69     * @return array<int|string, string> The links to display below the plugin name on plugins.php.
70     */
71    public function add_logs_action_link( array $action_links, string $_plugin_basename, $_plugin_data, $_context ): array {
72
73        // Presumably the deactivate link.
74        // When a plugin is "required" it does not have a deactivate link.
75        if ( count( $action_links ) > 0 ) {
76            $deactivate_link = array_pop( $action_links );
77        }
78
79        $logs_link = $this->api->get_log_url();
80
81        $last_log_time       = $this->api->get_last_log_time();
82        $last_logs_view_time = $this->api->get_last_logs_view_time();
83
84        if ( ! is_null( $last_log_time ) && ! is_null( $last_logs_view_time )
85            && $last_log_time > $last_logs_view_time ) {
86            $action_links[] = '<b><a href="' . $logs_link . '">' . __( 'Logs', 'bh-wp-logger' ) . '</a></b>';
87        } else {
88            $action_links[] = '<a href="' . $logs_link . '">' . __( 'Logs', 'bh-wp-logger' ) . '</a>';
89        }
90
91        if ( isset( $deactivate_link ) ) {
92            $action_links[] = $deactivate_link;
93        }
94
95        return $action_links;
96    }
97}