Completed
Push — update/base-styles-210 ( 2e278b...ad767b )
by Jeremy
22:25 queued 13:15
created

creative-mail.php ➔ try_install()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 9
nop 0
dl 0
loc 34
rs 8.7537
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
add_action( 'jetpack_activated_plugin', __NAMESPACE__ . '\configure_plugin', 10, 2 );
23
24
/**
25
 * Verify the intent to install Creative Mail, and kick off installation.
26
 *
27
 * This works in tandem with a JITM set up in the JITM package.
28
 */
29
function try_install() {
30
	if ( ! isset( $_GET['creative-mail-action'] ) ) {
31
		return;
32
	}
33
34
	check_admin_referer( 'creative-mail-install' );
35
36
	$result   = false;
37
	$redirect = admin_url( 'edit.php?post_type=feedback' );
38
39
	// Attempt to install and activate the plugin.
40
	if ( current_user_can( 'activate_plugins' ) ) {
41
		switch ( $_GET['creative-mail-action'] ) {
42
			case 'install':
43
				$result = install_and_activate();
44
				break;
45
			case 'activate':
46
				$result = activate();
47
				break;
48
		}
49
	}
50
51
	if ( $result ) {
52
		/** This action is already documented in _inc/lib/class.core-rest-api-endpoints.php */
53
		do_action( 'jetpack_activated_plugin', PLUGIN_FILE, 'jitm' );
54
		$redirect = admin_url( 'admin.php?page=creativemail' );
55
	} else {
56
		$redirect = add_query_arg( 'creative-mail-install-error', true, $redirect );
57
	}
58
59
	wp_safe_redirect( $redirect );
60
61
	exit;
62
}
63
64
/**
65
 * Install and activate the Creative Mail plugin.
66
 *
67
 * @return bool result of installation
68
 */
69
function install_and_activate() {
70
	jetpack_require_lib( 'plugins' );
71
	$result = \Jetpack_Plugins::install_and_activate_plugin( PLUGIN_SLUG );
72
73
	if ( is_wp_error( $result ) ) {
74
		return false;
75
	} else {
76
		return true;
77
	}
78
}
79
80
/**
81
 * Activate the Creative Mail plugin.
82
 *
83
 * @return bool result of activation
84
 */
85
function activate() {
86
	$result = activate_plugin( PLUGIN_FILE );
87
88
	// Activate_plugin() returns null on success.
89
	return is_null( $result );
90
}
91
92
/**
93
 * Notify the user that the installation of Creative Mail failed.
94
 */
95
function error_notice() {
96
	if ( empty( $_GET['creative-mail-install-error'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
97
		return;
98
	}
99
100
	?>
101
	<div class="notice notice-error is-dismissible">
102
		<p><?php esc_html_e( 'There was an error installing Creative Mail.', 'jetpack' ); ?></p>
103
	</div>
104
	<?php
105
}
106
107
/**
108
 * Set some options when first activating the plugin via Jetpack.
109
 *
110
 * @since 8.9.0
111
 *
112
 * @param string $plugin_file Plugin file.
113
 * @param string $source      Where did the plugin installation originate.
114
 */
115
function configure_plugin( $plugin_file, $source ) {
116
	if ( PLUGIN_FILE !== $plugin_file ) {
117
		return;
118
	}
119
120
	$plugin_info = array(
121
		'plugin'  => 'jetpack',
122
		'version' => JETPACK__VERSION,
123
		'time'    => time(),
124
		'source'  => esc_attr( $source ),
125
	);
126
127
	update_option( 'ce4wp_referred_by', $plugin_info );
128
}
129