Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Deactivation_Handler
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 5
42
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 init
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 add_deactivation_data
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 enqueue_script
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
2
 embed_dialog
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Intercept deactivation of plugins.
4 *
5 * @package automattic/jetpack-plugin-deactivation
6 */
7
8namespace Automattic\Jetpack\Plugin_Deactivation;
9
10use Automattic\Jetpack\Assets;
11
12/**
13 * Handles plugin deactivation.
14 *
15 * Instantiates the deactivation handler to intercept the deactivation of plugins.
16 */
17class Deactivation_Handler {
18
19    /**
20     * Plugin Deactivation package version
21     *
22     * @var string
23     */
24    const PACKAGE_VERSION = '0.3.26';
25
26    /**
27     * Slug of the plugin to intercept deactivation for.
28     *
29     * @var string
30     */
31    private $plugin;
32
33    /**
34     * Path to a PHP file that will be used as a template for the deactivation dialog.
35     *
36     * Copy ./dialog-template.php to your plugin and modify it to suit your needs.
37     *
38     * @var string
39     */
40    private $dialog_view;
41
42    /**
43     * Constructor.
44     *
45     * @param string $plugin      Slug of the plugin to intercept deactivation for.
46     * @param string $dialog_view Path to a PHP file that will be used as a template for the deactivation dialog.
47     */
48    public function __construct( $plugin, $dialog_view ) {
49        $this->plugin      = $plugin;
50        $this->dialog_view = $dialog_view;
51    }
52
53    /**
54     * Instantiates the deactivation handler to intercept the deactivation of plugins.
55     *
56     * @param string $plugin      Slug of the plugin to intercept deactivation for.
57     * @param string $dialog_view Path to a PHP file that will be used as a template for the deactivation dialog.
58     */
59    public static function init( $plugin, $dialog_view ) {
60        $instance = new self( $plugin, $dialog_view );
61
62        if ( ! file_exists( $instance->dialog_view ) ) {
63            return new \WP_Error( 'no-template', 'The plugin deactivation dialog view file does not exist.' );
64        }
65
66        add_action( 'load-plugins.php', array( $instance, 'enqueue_script' ) );
67        add_action( 'admin_footer-plugins.php', array( $instance, 'embed_dialog' ) );
68        add_filter( 'jp_plugin_deactivation_data', array( $instance, 'add_deactivation_data' ) );
69
70        return $instance;
71    }
72
73    /**
74     * Used by `jp_plugin_deactivation_data` filter to pass data to
75     * the JetpackPluginDeactivation class.
76     *
77     * @param array $data The data to pass to the JetpackPluginDeactivation class.
78     */
79    public function add_deactivation_data( $data ) {
80        $data['slugs'][] = $this->plugin;
81        return $data;
82    }
83
84    /**
85     * Enqueues the deactivation handler script and styles.
86     */
87    public function enqueue_script() {
88        Assets::register_script(
89            'jetpack-plugin-deactivation',
90            '../build/index.js',
91            __FILE__,
92            array(
93                'enqueue'    => true,
94                'in_footer'  => true,
95                'textdomain' => 'jetpack-plugin-deactivation',
96            )
97        );
98
99        /**
100         * This is going to pass plugin slugs to the script.
101         */
102        $data = array(
103            'slugs' => array(),
104        );
105        wp_localize_script( 'jetpack-plugin-deactivation', 'JetpackPluginDeactivationData', apply_filters( 'jp_plugin_deactivation_data', $data ) );
106    }
107
108    /**
109     * Add required html to the plugin page footer.
110     */
111    public function embed_dialog() {
112        ?>
113        <div
114            id="jp-plugin-deactivation-<?php echo esc_attr( $this->plugin ); ?>"
115            data-jp-plugin-deactivation="<?php echo esc_attr( $this->plugin ); ?>"
116            class="jp-plugin-deactivation"
117        >
118            <div class="jp-plugin-deactivation__dialog">
119                <?php include $this->dialog_view; ?>
120            </div>
121            <div
122                    data-jp-plugin-deactivation-action="close"
123                    class="jp-plugin-deactivation__overlay"
124            ></div>
125        </div>
126        <?php
127    }
128}