Completed
Push — master ( 7cec2b...1660a9 )
by Stephanie
03:41
created

FrmAddonsController::upgrade_to_pro()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
class FrmAddonsController {
4
5
	public static function menu() {
6
		add_submenu_page( 'formidable', 'Formidable | ' . __( 'AddOns', 'formidable' ), __( 'AddOns', 'formidable' ), 'frm_view_forms', 'formidable-addons', 'FrmAddonsController::list_addons' );
7
8
		if ( ! FrmAppHelper::pro_is_installed() ) {
9
			add_submenu_page( 'formidable', 'Formidable | ' . __( 'Upgrade to Pro', 'formidable' ), __( 'Upgrade to Pro', 'formidable' ), 'frm_view_forms', 'formidable-pro-upgrade', 'FrmAddonsController::upgrade_to_pro' );
10
		}
11
	}
12
13
	public static function list_addons() {
14
		$installed_addons = apply_filters( 'frm_installed_addons', array() );
15
16
		$pro_link = 'https://formidablepro.com/pricing';
17
		$addons = self::get_ordered_addons( $pro_link );
18
19
		$plugin_names = array(
20
			'formidable-pro'    => 'formidable/pro',
21
			'wp-multilingual'   => 'formidable-wpml',
22
			'registration-lite' => 'formidable-registration',
23
			'bootstrap-modal'   => 'formidable-modal',
24
			'paypal-standard'   => 'formidable-paypal',
25
			'formidable-api'    => 'formidable-api',
26
			'authorize-net-aim' => 'formidable-authorize-net',
27
		);
28
29
		include( FrmAppHelper::plugin_path() . '/classes/views/addons/list.php' );
30
	}
31
32
	private static function get_ordered_addons( $pro_link = 'https://formidablepro.com/pricing' ) {
33
		$addons = self::get_api_addons();
34
		if ( ! is_array( $addons ) ) {
35
			$addons = array(
36
				'info' => array( 'link' => $pro_link, 'name' => 'Formidable Pro', 'slug' => 'formidable_pro' ),
37
			);
38
		} else {
39
			$addons = $addons['products'];
40
		}
41
		$addons = array_reverse( $addons );
42
43
		$keyed_addons = array();
44
		foreach ( $addons as $addon ) {
45
			$keyed_addons[ $addon['info']['slug'] ] = $addon;
46
		}
47
48
		$plugin_order = array(
49
			'formidable-pro', 'mailchimp', 'registration-lite',
50
			'paypal-standard', 'stripe', 'authorize-net-aim',
51
			'bootstrap-modal', 'math-captcha',
52
			'zapier',
53
		);
54
		$ordered_addons = array();
55
		foreach ( $plugin_order as $plugin ) {
56
			if ( isset( $keyed_addons[ $plugin ] ) ) {
57
				$ordered_addons[] = $keyed_addons[ $plugin ];
58
				unset( $keyed_addons[ $plugin ] );
59
			}
60
		}
61
		$addons = $ordered_addons + $keyed_addons;
62
		return $addons;
63
	}
64
65
	public static function license_settings() {
66
		$plugins = apply_filters( 'frm_installed_addons', array() );
67
		if ( empty( $plugins ) ) {
68
			_e( 'There are no plugins on your site that require a license', 'formidable' );
69
			return;
70
		}
71
72
		include( FrmAppHelper::plugin_path() . '/classes/views/addons/settings.php' );
73
	}
74
75
	private static function get_api_addons() {
76
		$addons = get_transient( 'frm_api_addons' );
77
		if ( $addons !== false ) {
78
			return $addons;
79
		}
80
81
		$url = 'https://formidablepro.com/edd-api/products?number=40';
82
83
		// check every 5 days
84
		$addons = self::send_api_request( $url, array( 'name' => 'frm_api_addons', 'expires' => 60 * 60 * 24 * 5 ) );
85
		if ( is_array( $addons ) ) {
86
			return $addons;
87
		}
88
89
		return false;
90
	}
91
92
	public static function get_licenses() {
93
		FrmAppHelper::permission_check('frm_change_settings');
94
		check_ajax_referer( 'frm_ajax', 'nonce' );
95
96
		$license = get_option('frmpro-credentials');
97
		if ( $license && is_array( $license ) && isset( $license['license'] ) ) {
98
			$url = 'http://formidablepro.com/frm-edd-api/licenses?l=' . urlencode( base64_encode( $license['license'] ) );
99
			$licenses = self::send_api_request( $url, array( 'name' => 'frm_api_licence', 'expires' => 60 * 60 * 5 ) );
100
			echo json_encode( $licenses );
101
		}
102
103
		wp_die();
104
	}
105
106
	private static function send_api_request( $url, $transient = array() ) {
107
		$data = get_transient( $transient['name'] );
108
		if ( $data !== false ) {
109
			return $data;
110
		}
111
112
		$arg_array = array(
113
			'body'      => array(
114
				'url'   => home_url(),
115
			),
116
			'timeout'   => 15,
117
			'sslverify' => false,
118
			'user-agent' => 'Formidable/' . FrmAppHelper::$plug_version . '; ' . home_url(),
119
		);
120
121
		$response = wp_remote_post( $url, $arg_array );
122
		$body = wp_remote_retrieve_body( $response );
123
		$data = false;
124
		if ( ! is_wp_error( $response ) && ! is_wp_error( $body ) ) {
125
			$data = json_decode( $body, true );
126
			set_transient( $transient['name'], $data, $transient['expires'] );
127
		}
128
129
		return $data;
130
	}
131
132
	public static function upgrade_to_pro() {
133
		$addons = self::get_ordered_addons();
134
		$pro_pricing = array();
135
		self::prepare_pro_info( $addons[0], $pro_pricing );
136
137
		include( FrmAppHelper::plugin_path() . '/classes/views/addons/upgrade_to_pro.php' );
138
	}
139
140
	private static function prepare_pro_info( $pro, &$pro_pricing ) {
141
		$pro_pricing = array(
142
			'personal' => array( 'id' => 5, 'price' => '49.00', 'name' => 'Personal' ),
143
			'professional' => array( 'id' => 6, 'price' => '99.00', 'name' => 'Professional' ),
144
			'smallbusiness' => array( 'id' => 3, 'price' => '199.00', 'name' => 'Small Business' ),
145
			'enterprise' => array ( 'id' => 4, 'price' => '399.00', 'name' => 'Enterprise' ),
0 ignored issues
show
introduced by
There must be no space between the Array keyword and the opening parenthesis
Loading history...
146
		);
147
148
		foreach ( $pro['pricing'] as $name => $price ) {
149
			if ( isset( $pro_pricing[ $name ] ) ) {
150
				$pro_pricing[ $name ]['price'] = $price;
151
			}
152
		}
153
	}
154
}
155