Completed
Push — add/creative-mail-compat-jitm ( ea6a8b )
by Jeremy
87:58 queued 79:22
created

creative-mail.php ➔ try_install()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 9
nop 0
dl 0
loc 31
rs 8.8017
c 0
b 0
f 0
1
<?php
2
/**
3
 * Compatibility functions for the Creative Mail plugin.
4
 * https://wordpress.org/plugins/creative-mail-by-constant-contact/
5
 *
6
 * @since 8.9.0
7
 *
8
 * @package Jetpack
9
 */
10
11
namespace Automattic\Jetpack\Creative_Mail;
12
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
const PLUGIN_SLUG = 'creative-mail-by-constant-contact';
18
const PLUGIN_FILE = 'creative-mail-by-constant-contact/creative-mail-plugin.php';
19
20
add_action( 'admin_notices', __NAMESPACE__ . '\error_notice' );
21
add_action( 'admin_init', __NAMESPACE__ . '\try_install' );
22
23
/**
24
 * Verify the intent to install Creative Mail, and kick off installation.
25
 *
26
 * This works in tandem with a JITM set up in the JITM package.
27
 */
28
function try_install() {
29
	if ( ! isset( $_GET['creative-mail-action'] ) ) {
30
		return;
31
	}
32
33
	check_admin_referer( 'creative-mail-install' );
34
35
	$result = false;
36
37
	// Attempt to install and activate the plugin.
38
	if ( current_user_can( 'activate_plugins' ) ) {
39
		switch ( $_GET['creative-mail-action'] ) {
40
			case 'install':
41
				$result = install_and_activate();
42
				break;
43
			case 'activate':
44
				$result = activate();
45
				break;
46
		}
47
	}
48
49
	if ( $result ) {
50
		$redirect = admin_url( 'admin.php?page=creativemail' );
51
	} else {
52
		$redirect = add_query_arg( 'creative-mail-install-error', true, $redirect );
0 ignored issues
show
Bug introduced by
The variable $redirect seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
53
	}
54
55
	wp_safe_redirect( $redirect );
56
57
	exit;
58
}
59
60
/**
61
 * Install and activate the Creative Mail plugin.
62
 *
63
 * @return bool result of installation
64
 */
65
function install_and_activate() {
66
	jetpack_require_lib( 'plugins' );
67
	$result = \Jetpack_Plugins::install_and_activate_plugin( PLUGIN_SLUG );
68
69
	if ( is_wp_error( $result ) ) {
70
		return false;
71
	} else {
72
		return true;
73
	}
74
}
75
76
/**
77
 * Activate the Creative Mail plugin.
78
 *
79
 * @return bool result of activation
80
 */
81
function activate() {
82
	$result = activate_plugin( PLUGIN_FILE );
83
84
	// Activate_plugin() returns null on success.
85
	return is_null( $result );
86
}
87
88
/**
89
 * Notify the user that the installation of Creative Mail failed.
90
 */
91
function error_notice() {
92
	if ( empty( $_GET['creative-mail-install-error'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
93
		return;
94
	}
95
96
	?>
97
	<div class="notice notice-error is-dismissible">
98
		<p><?php esc_html_e( 'There was an error installing Creative Mail.', 'jetpack' ); ?></p>
99
	</div>
100
	<?php
101
}
102