Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 38
0.00% covered (danger)
0.00%
0 / 4
CRAP
n/a
0 / 0
Automattic\Jetpack\Jetpack_Backup\try_install
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
30
Automattic\Jetpack\Jetpack_Backup\install_and_activate
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
Automattic\Jetpack\Jetpack_Backup\activate
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
Automattic\Jetpack\Jetpack_Backup\error_notice
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Compatibility functions for the Jetpack Backup plugin.
4 * https://wordpress.org/plugins/jetpack-backup/
5 *
6 * @since 10.4
7 *
8 * @package automattic/jetpack
9 */
10
11namespace Automattic\Jetpack\Jetpack_Backup;
12
13use Automattic\Jetpack\Plugins_Installer;
14
15if ( ! defined( 'ABSPATH' ) ) {
16    exit( 0 );
17}
18
19const PLUGIN_SLUG = 'jetpack-backup';
20const PLUGIN_FILE = 'jetpack-backup/jetpack-backup.php';
21
22if ( isset( $_GET['jetpack-backup-install-error'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
23    add_action( 'admin_notices', __NAMESPACE__ . '\error_notice' );
24}
25
26if ( isset( $_GET['jetpack-backup-action'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
27    add_action( 'admin_init', __NAMESPACE__ . '\try_install' );
28}
29
30/**
31 * Verify the intent to install Jetpack Backup, and kick off installation.
32 *
33 * This works in tandem with a JITM set up in the JITM package.
34 *
35 * @return never
36 */
37function try_install() {
38    check_admin_referer( 'jetpack-backup-install' );
39
40    $result = false;
41    // If the plugin install fails, redirect to plugin install page pre-populated with jetpack-backup search term.
42    $redirect_on_error = admin_url( 'plugin-install.php?s=jetpack-backup&tab=search&type=term' );
43
44    // Attempt to install and activate the plugin.
45    if ( current_user_can( 'activate_plugins' ) ) {
46        switch ( $_GET['jetpack-backup-action'] ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated -- Function only hooked if set.
47            case 'install':
48                $result = install_and_activate();
49                break;
50            case 'activate':
51                $result = activate();
52                break;
53        }
54    }
55
56    if ( $result ) {
57        /** This action is already documented in _inc/lib/class.core-rest-api-endpoints.php */
58        do_action( 'jetpack_activated_plugin', PLUGIN_FILE, 'jitm' );
59        $redirect = admin_url( 'admin.php?page=jetpack-backup' );
60    } else {
61        $redirect = add_query_arg( 'jetpack-backup-install-error', true, $redirect_on_error );
62    }
63
64    wp_safe_redirect( $redirect );
65
66    exit( 0 );
67}
68
69/**
70 * Install and activate the Jetpack Backup plugin.
71 *
72 * @return bool result of installation
73 */
74function install_and_activate() {
75    $result = Plugins_Installer::install_and_activate_plugin( PLUGIN_SLUG );
76
77    if ( is_wp_error( $result ) ) {
78        return false;
79    } else {
80        return true;
81    }
82}
83
84/**
85 * Activate the Jetpack Backup plugin.
86 *
87 * @return bool result of activation
88 */
89function activate() {
90    $result = activate_plugin( PLUGIN_FILE );
91
92    // Activate_plugin() returns null on success.
93    return $result === null;
94}
95
96/**
97 * Notify the user that the installation of Jetpack Backup failed.
98 */
99function error_notice() {
100    wp_admin_notice(
101        esc_html__( 'There was an error installing Jetpack Backup. Please try again.', 'jetpack' ),
102        array(
103            'type'        => 'error',
104            'dismissible' => true,
105        )
106    );
107}