Completed
Push — fix/youtube-shortcode-amp-comp... ( 0aea78...87f47e )
by
unknown
08:56
created

Jetpack_CRM_Data::get_crm_data()   B

Complexity

Conditions 8
Paths 32

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
nc 32
nop 0
dl 0
loc 39
rs 8.0515
c 0
b 0
f 0
1
<?php
2
/**
3
 * Compatibility functions for the Jetpack CRM plugin.
4
 *
5
 * @since 9.0.0
6
 *
7
 * @package Jetpack
8
 */
9
10
namespace Automattic\Jetpack;
11
12
/**
13
 * Provides Jetpack CRM plugin data.
14
 */
15
class Jetpack_CRM_Data {
16
17
	const JETPACK_CRM_PLUGIN_SLUG = 'zero-bs-crm/ZeroBSCRM.php';
18
19
	/**
20
	 * Provides Jetpack CRM plugin data for use in the Contact Form block sidebar menu.
21
	 *
22
	 * @return array An array containing the Jetpack CRM plugin data.
23
	 */
24
	public function get_crm_data() {
25
		jetpack_require_lib( 'plugins' );
26
		$plugins = \Jetpack_Plugins::get_plugins();
27
28
		// Set default values.
29
		$response = array(
30
			'crm_installed'          => false,
31
			'crm_active'             => false,
32
			'crm_version'            => null,
33
			'jp_form_ext_enabled'    => null,
34
			'can_install_crm'        => false,
35
			'can_activate_crm'       => false,
36
			'can_activate_extension' => false,
37
		);
38
39
		if ( isset( $plugins[ self::JETPACK_CRM_PLUGIN_SLUG ] ) ) {
40
			$response['crm_installed'] = true;
41
42
			$crm_data = $plugins[ self::JETPACK_CRM_PLUGIN_SLUG ];
43
44
			$response['crm_active']  = $crm_data['active'];
45
			$response['crm_version'] = $crm_data['Version'];
46
47
			if ( $response['crm_active'] ) {
48
				if ( function_exists( 'zeroBSCRM_isExtensionInstalled' ) ) {
49
					$response['jp_form_ext_enabled'] = zeroBSCRM_isExtensionInstalled( 'jetpackforms' );
50
				}
51
			}
52
		}
53
54
		$response['can_install_crm']  = $response['crm_installed'] ? false : current_user_can( 'install_plugins' );
55
		$response['can_activate_crm'] = $response['crm_active'] ? false : current_user_can( 'activate_plugins' );
56
57
		if ( $response['crm_active'] && function_exists( 'zeroBSCRM_extension_install_jetpackforms' ) ) {
58
			$response['can_activate_extension'] = current_user_can( 'admin_zerobs_manage_options' );
59
		}
60
61
		return $response;
62
	}
63
64
	/**
65
	 * Activates Jetpack CRM's Jetpack Forms extension. This is used by a button in the Jetpack Contact Form
66
	 * sidebar menu.
67
	 *
68
	 * @return true|WP_Error Returns true if activation is success, else returns a WP_Error object.
69
	 */
70
	public function activate_crm_jetpackforms_extension() {
71
		if ( function_exists( 'zeroBSCRM_extension_install_jetpackforms' ) ) {
72
			return zeroBSCRM_extension_install_jetpackforms();
73
		}
74
75
		return new WP_Error( 'jp_forms_extension_activation_failed', esc_html__( 'The Jetpack Forms extension could not be activated.', 'jetpack' ) );
76
	}
77
}
78