Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
73.17% covered (warning)
73.17%
30 / 41
71.43% covered (warning)
71.43%
5 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
BH_WP_Plugins_Page
73.17% covered (warning)
73.17%
30 / 41
71.43% covered (warning)
71.43%
5 / 7
11.93
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 set_locale
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 define_admin_hooks
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 define_plugins_list_table_hooks
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
2
 define_plugins_list_table_zip_download_hooks
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
6
 define_plugins_page_hooks
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 define_ajax_hooks
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * The file that defines the core plugin class
4 *
5 * A class definition that WP_Includes attributes and functions used across both the
6 * frontend-facing side of the site and the admin area.
7 *
8 * @link       https://github.com/brianhenryie/bh-wp-plugins-page
9 * @since      1.0.0
10 *
11 * @package    brianhenryie/bh-wp-plugins-page
12 */
13
14namespace BrianHenryIE\WP_Plugins_Page;
15
16use BrianHenryIE\WP_Plugins_Page\Admin\Admin_Assets;
17use BrianHenryIE\WP_Plugins_Page\Admin\AJAX;
18use BrianHenryIE\WP_Plugins_Page\Admin\Plugins_List_Table;
19use BrianHenryIE\WP_Plugins_Page\Admin\Plugins_Page;
20use BrianHenryIE\WP_Plugins_Page\Admin\Updates;
21use BrianHenryIE\WP_Plugins_Page\API\API;
22use BrianHenryIE\WP_Plugins_Page\API\Settings;
23use BrianHenryIE\WP_Plugins_Page\WP_Includes\I18n;
24use Psr\Log\LoggerInterface;
25
26/**
27 * Hooks the plugin's classes to WordPress's actions and filters.
28 */
29class BH_WP_Plugins_Page {
30
31    /**
32     * A PSR logger to log changes.
33     */
34    protected LoggerInterface $logger;
35
36    /**
37     * Plugin functions.
38     */
39    protected API $api;
40
41    /**
42     * The plugin settings.
43     */
44    protected Settings $settings;
45
46    /**
47     * Wire up actions and filters for the plugin.
48     *
49     * @param Settings        $settings The plugin settings.
50     * @param API             $api Some main plugin functions.
51     * @param LoggerInterface $logger A PSR logger.
52     */
53    public function __construct( Settings $settings, API $api, LoggerInterface $logger ) {
54
55        $this->logger   = $logger;
56        $this->api      = $api;
57        $this->settings = $settings;
58
59        $this->set_locale();
60        $this->define_admin_hooks();
61        $this->define_plugins_list_table_hooks();
62        $this->define_plugins_list_table_zip_download_hooks();
63        $this->define_plugins_page_hooks();
64        $this->define_ajax_hooks();
65    }
66
67    /**
68     * Define the locale for this plugin for internationalization.
69     *
70     * Uses the i18n class in order to set the domain and to register the hook
71     * with WordPress.
72     *
73     * @since    1.0.0
74     */
75    protected function set_locale(): void {
76
77        $plugin_i18n = new I18n();
78
79        add_action( 'plugins_loaded', array( $plugin_i18n, 'load_plugin_textdomain' ) );
80
81    }
82
83    /**
84     * Register all of the hooks related to the admin area functionality
85     * of the plugin.
86     *
87     * @since    1.0.0
88     */
89    protected function define_admin_hooks(): void {
90
91        $plugin_admin = new Admin_Assets( $this->settings );
92        add_action( 'admin_enqueue_scripts', array( $plugin_admin, 'enqueue_scripts' ), 9999 );
93    }
94
95    /**
96     * Hooks for the plugins list table.
97     * Adds the generic plugin_action_links and plugin_row_meta actions and a specific action for each active plugin.
98     *
99     * @since    1.0.4
100     */
101    protected function define_plugins_list_table_hooks(): void {
102
103        $plugins_list_table = new Plugins_List_Table();
104        $active_plugins     = (array) get_option( 'active_plugins', array() );
105
106        foreach ( $active_plugins as $plugin_basename ) {
107            add_filter(
108                "plugin_action_links_{$plugin_basename}",
109                array( $plugins_list_table, 'plugin_specific_action_links' ),
110                9999,
111                4
112            );
113        }
114        add_filter( 'plugin_row_meta', array( $plugins_list_table, 'row_meta' ), 9999, 4 );
115
116        add_filter( 'all_plugins', array( $plugins_list_table, 'edit_plugins_array' ) );
117    }
118
119    /**
120     * Define hooks for adding download links to plugins.php list table.
121     */
122    protected function define_plugins_list_table_zip_download_hooks(): void {
123
124        $updates        = new Updates();
125        $active_plugins = (array) get_option( 'active_plugins', array() );
126
127        foreach ( $active_plugins as $plugin_basename ) {
128            add_action(
129                "in_plugin_update_message-{$plugin_basename}",
130                array( $updates, 'add_zip_download_link' ),
131                10,
132                2
133            );
134        }
135    }
136
137    /**
138     * Add hooks to prevent unwanted redirects when plugins are installed.
139     */
140    protected function define_plugins_page_hooks(): void {
141
142        $plugins_page = new Plugins_Page();
143
144        add_filter( 'wp_redirect', array( $plugins_page, 'prevent_redirect' ), 1, 2 );
145
146        $active_plugins = (array) get_option( 'active_plugins', array() );
147
148        foreach ( $active_plugins as $plugin_basename ) {
149            list( $plugin_slug ) = explode( '/', $plugin_basename );
150            add_filter( "fs_redirect_on_activation_{$plugin_slug}", '__return_false' );
151        }
152    }
153
154    /**
155     * Add hook to handle AJAX requests.
156     */
157    protected function define_ajax_hooks(): void {
158
159        $ajax = new AJAX( $this->api, $this->logger );
160
161        add_action( 'wp_ajax_bh_wp_plugins_page_set_plugin_name', array( $ajax, 'set_plugin_name' ) );
162
163    }
164}