Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
13 / 13
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%
13 / 13
100.00% covered (success)
100.00%
2 / 2
7
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
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     * Plugins_Page constructor.
30     *
31     * @param API_Interface             $api The logger's main functions. Needed for the log file path.
32     * @param Logger_Settings_Interface $settings The logger settings. Needed for the plugin slug.
33     * @param ?LoggerInterface          $logger The logger itself.
34     */
35    public function __construct(
36        protected API_Interface $api,
37        protected Logger_Settings_Interface $settings,
38        ?LoggerInterface $logger = null
39    ) {
40        $this->setLogger( $logger ?? new NullLogger() );
41    }
42
43    /**
44     * Adds 'Logs' link to under the plugin name on plugins.php.
45     * Attempts to place it immediately before the deactivate link.
46     *
47     * @hooked plugin_action_links_{plugin basename}
48     * @see \WP_Plugins_List_Table::display_rows()
49     *
50     * @param array<int|string, string>  $action_links The existing plugin links (usually "Deactivate").
51     * @param string                     $_plugin_basename The plugin's directory/filename.php.
52     * @param array<string, string|bool> $_plugin_data Associative array including PluginURI, slug, Author, Version. See `get_plugin_data()`.
53     * @param string                     $_context     The plugin context. By default, this can include 'all', 'active', 'inactive',
54     *                                                'recently_activated', 'upgrade', 'mustuse', 'dropins', and 'search'.
55     *
56     * @return array<int|string, string> The links to display below the plugin name on plugins.php.
57     */
58    public function add_logs_action_link( array $action_links, string $_plugin_basename, $_plugin_data, $_context ): array {
59
60        // Presumably the deactivate link.
61        // When a plugin is "required" it does not have a deactivate link.
62        if ( count( $action_links ) > 0 ) {
63            $deactivate_link = array_pop( $action_links );
64        }
65
66        $logs_link = $this->api->get_log_url();
67
68        $last_log_time       = $this->api->get_last_log_time();
69        $last_logs_view_time = $this->api->get_last_logs_view_time();
70
71        if ( ! is_null( $last_log_time ) && ! is_null( $last_logs_view_time )
72            && $last_log_time > $last_logs_view_time ) {
73            $action_links[] = '<b><a href="' . $logs_link . '">' . __( 'Logs', 'bh-wp-logger' ) . '</a></b>';
74        } else {
75            $action_links[] = '<a href="' . $logs_link . '">' . __( 'Logs', 'bh-wp-logger' ) . '</a>';
76        }
77
78        if ( isset( $deactivate_link ) ) {
79            $action_links[] = $deactivate_link;
80        }
81
82        return $action_links;
83    }
84}