Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
30.77% covered (danger)
30.77%
8 / 26
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Admin_Assets
30.77% covered (danger)
30.77%
8 / 26
0.00% covered (danger)
0.00%
0 / 2
17.95
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
 enqueue_scripts
32.00% covered (danger)
32.00%
8 / 25
0.00% covered (danger)
0.00%
0 / 1
12.86
1<?php
2/**
3 * The admin-specific functionality of the plugin.
4 *
5 * @link       https://github.com/brianhenryie/bh-wp-plugins-page
6 * @since      1.0.0
7 *
8 * @package    brianhenryie/bh-wp-plugins-page
9 */
10
11namespace BrianHenryIE\WP_Plugins_Page\Admin;
12
13use BrianHenryIE\WP_Plugins_Page\API\API;
14use BrianHenryIE\WP_Plugins_Page\API\Settings;
15
16/**
17 * The admin-specific functionality of the plugin.
18 *
19 * Defines the plugin name, version, and two examples hooks for how to
20 * enqueue the admin-specific stylesheet and JavaScript.
21 *
22 * @package    brianhenryie/bh-wp-plugins-page
23 *
24 * @author     BrianHenryIE <BrianHenryIE@gmail.com>
25 */
26class Admin_Assets {
27
28    /**
29     * Uses the plugin version for JS caching.
30     *
31     * @uses Settings::get_plugin_version()
32     */
33    protected Settings $settings;
34
35    /**
36     * Constructor.
37     *
38     * @param Settings $settings The plugin settings.
39     */
40    public function __construct( Settings $settings ) {
41        $this->settings = $settings;
42    }
43
44    /**
45     * Register the JavaScript for the admin area.
46     *
47     * @hooked admin_enqueue_scripts
48     *
49     * @since    1.0.0
50     */
51    public function enqueue_scripts(): void {
52
53        global $pagenow;
54        if ( 'plugins.php' !== $pagenow ) {
55            return;
56        }
57
58        $plugin_basename = defined( 'BH_WP_PLUGINS_PAGE_BASENAME' ) ? BH_WP_PLUGINS_PAGE_BASENAME : 'bh-wp-plugins-page/bh-wp-plugins-page.php';
59        $js_url          = plugin_dir_url( $plugin_basename ) . 'assets/bh-wp-plugins-page-admin.js';
60        $css_url         = plugin_dir_url( $plugin_basename ) . 'assets/bh-wp-plugins-page-admin.css';
61        $version         = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? time() : $this->settings->get_plugin_version();
62
63        wp_enqueue_script( 'bh-wp-plugins-page', $js_url, array( 'jquery' ), $version, true );
64        $ajax_data      = array(
65            'ajaxUrl' => admin_url( 'admin-ajax.php' ),
66            'nonce'   => wp_create_nonce( AJAX::class ),
67        );
68        $ajax_data_json = wp_json_encode( $ajax_data, JSON_PRETTY_PRINT );
69
70        $changes = get_option( API::PLUGINS_PAGE_CHANGES_OPTION_NAME, array() );
71
72        $bh_wp_plugins_page_changes = wp_json_encode( $changes );
73
74        $script = <<<EOD
75var bh_wp_plugins_page_ajax_data = $ajax_data_json;
76var bh_wp_plugins_page_changes = $bh_wp_plugins_page_changes;
77EOD;
78
79        wp_add_inline_script(
80            'bh-wp-plugins-page',
81            $script,
82            'before'
83        );
84
85        wp_enqueue_style( 'bh-wp-plugins-page', $css_url, array(), $version );
86    }
87
88}