Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 51
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Plugins_Page
0.00% covered (danger)
0.00%
0 / 51
0.00% covered (danger)
0.00%
0 / 4
110
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 plugin_auto_update_setting_html
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
42
 get_licence_link_url
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
2
 append_licence_link_to_auto_update_unavailable_text
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * Add info on plugins.php, add link to display licence modal
4 *
5 * Add the "Licence: invalid" text in the Automatic Updates column on `plugins.php`.
6 * Add link to licence modal when newer plugin exists but update cannot be downloaded.
7 *
8 * @package brianhenryie/bh-wp-plugin-updater
9 */
10
11namespace BrianHenryIE\WP_Plugin_Updater\Admin;
12
13use BrianHenryIE\WP_Plugin_Updater\API_Interface;
14use BrianHenryIE\WP_Plugin_Updater\Settings_Interface;
15
16/**
17 * Add manage licence links to the plugins list table.
18 *
19 * @see \WP_Plugins_List_Table
20 */
21class Plugins_Page {
22
23    /**
24     * Constructor.
25     *
26     * @param API_Interface      $api Used to get the licence details.
27     * @param Settings_Interface $settings Used for the plugin basename and slug.
28     */
29    public function __construct(
30        protected API_Interface $api,
31        protected Settings_Interface $settings,
32    ) {
33    }
34
35    /**
36     * Add the "Licence: invalid" text in the Automatic Updates column on `plugins.php`.
37     *
38     * TODO: remove "Enable auto-updates" when the licence is invalid.
39     *
40     * @hooked plugin_auto_update_setting_html
41     * @see \WP_Plugins_List_Table::single_row()
42     *
43     * @param string $html The existing HTML for the plugins.php Automatic Updates column.
44     * @param string $file Plugin or theme file.
45     */
46    public function plugin_auto_update_setting_html( string $html, string $file ): string {
47
48        if ( $this->settings->get_plugin_basename() !== $file ) {
49            return $html;
50        }
51
52        $licence_status = $this->api->get_licence_details( false )->get_status();
53        $expires        = $this->api->get_licence_details( false )->get_expiry_date();
54
55        if ( in_array( $licence_status, array( 'valid', 'expired' ), true )
56                && ! is_null( $expires ) ) {
57            $licence_link_text = 'Licence active until ' . $expires->format( 'Y-m-d' );
58        } else {
59            $licence_link_text = "Licence: {$licence_status}";
60        }
61
62        $licence_link = sprintf(
63            '<a href="%s" class="thickbox open-plugin-details-modal" style="%s">%s</a>',
64            $this->get_licence_link_url(),
65            'display: inline-block;',
66            esc_html( $licence_link_text )
67        );
68
69        // Remove the 'Enable auto-updates' link when the licence is invalid.
70        if ( 'invalid' === $licence_status ) {
71            $html = '';
72        }
73
74        return empty( trim( wp_strip_all_tags( $html ) ) )
75            ? $licence_link
76            : "{$html} | {$licence_link}";
77    }
78
79    /**
80     * Get the URL which works to display the plugin View Details modal at the licence tab.
81     *
82     * @see \WP_Plugins_List_Table::get_view_details_link()
83     */
84    protected function get_licence_link_url(): string {
85        $licence_link_url = admin_url(
86            add_query_arg(
87                array(
88                    'plugin'    => $this->settings->get_plugin_slug(),
89                    'tab'       => 'plugin-information',
90                    'section'   => 'licence',
91                    'TB_iframe' => 'true',
92                    'width'     => '772',
93                    'height'    => '730',
94                ),
95                'plugin-install.php'
96            )
97        );
98
99        return $licence_link_url;
100    }
101
102    /**
103     * Add link to open the plugin details modal to the licence tab after the text: "Automatic update is unavailable
104     * for this plugin."
105     *
106     * @see wp_plugin_update_row()
107     * @hooked in_plugin_update_message-{$plugin_basename}
108     *
109     * @param array     $plugin_data
110     * @param \stdClass $response An object of metadata about the available plugin update.
111     */
112    public function append_licence_link_to_auto_update_unavailable_text( $plugin_data, $response ): void {
113
114        // Only print this text when there is no download link.
115        if ( ! empty( $response->package ) ) {
116            return;
117        }
118
119        // E.g. your licence has expired.
120        $licence_link_text = 'View licence details';
121
122        $licence_link = sprintf(
123            ' <a href="%s" class="thickbox open-plugin-details-modal">%s</a>',
124            $this->get_licence_link_url(),
125            esc_html( $licence_link_text )
126        );
127
128        echo wp_kses(
129            $licence_link,
130            array(
131                'a' => array(
132                    'href'  => array(),
133                    'class' => array(),
134                ),
135            )
136        );
137    }
138}