Completed
Push — master ( 95de47...49ca71 )
by Stephanie
06:51
created

FrmAddonsController::menu()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
nop 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::get_affiliate() ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression \FrmAppHelper::get_affiliate() of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
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', 'wp-multilingual' => 'formidable-wpml',
21
			'registration-lite' => 'formidable-registration', 'bootstrap-modal' => 'formidable-modal',
22
			'paypal-standard' => 'formidable-paypal', 'formidable-api' => 'formidable-api',
23
		);
24
25
		include( FrmAppHelper::plugin_path() . '/classes/views/addons/list.php' );
26
	}
27
28
	private static function get_ordered_addons( $pro_link = 'https://formidablepro.com/pricing' ) {
29
		$addons = self::get_api_addons();
30
		if ( ! is_array( $addons ) ) {
31
			$addons = array(
32
				'info' => array( 'link' => $pro_link, 'name' => 'Formidable Pro', 'slug' => 'formidable_pro' ),
33
			);
34
		} else {
35
			$addons = $addons['products'];
36
		}
37
		$addons = array_reverse( $addons );
38
39
		$keyed_addons = array();
40
		foreach ( $addons as $addon ) {
41
			$keyed_addons[ $addon['info']['slug'] ] = $addon;
42
		}
43
44
		$plugin_order = array(
45
			'formidable-pro', 'mailchimp', 'registration-lite',
46
			'paypal-standard', 'bootstrap-modal', 'math-captcha',
47
		);
48
		$ordered_addons = array();
49
		foreach ( $plugin_order as $plugin ) {
50
			if ( isset( $keyed_addons[ $plugin ] ) ) {
51
				$ordered_addons[] = $keyed_addons[ $plugin ];
52
				unset( $keyed_addons[ $plugin ] );
53
			}
54
		}
55
		$addons = $ordered_addons + $keyed_addons;
56
		return $addons;
57
	}
58
59
	public static function license_settings() {
60
		$plugins = apply_filters( 'frm_installed_addons', array() );
61
		if ( empty( $plugins ) ) {
62
			_e( 'There are no plugins on your site that require a license', 'formidable' );
63
			return;
64
		}
65
66
		include( FrmAppHelper::plugin_path() . '/classes/views/addons/settings.php' );
67
	}
68
69
	private static function get_api_addons() {
70
		$addons = get_transient( 'frm_api_addons' );
71
		if ( $addons !== false ) {
72
			return $addons;
73
		}
74
75
		$url = 'https://formidablepro.com/edd-api/products?number=40';
76
77
		// check every 5 days
78
		$addons = self::send_api_request( $url, array( 'name' => 'frm_api_addons', 'expires' => 60 * 60 * 24 * 5 ) );
79
		if ( is_array( $addons ) ) {
80
			return $addons;
81
		}
82
83
		return false;
84
	}
85
86
	public static function get_licenses() {
87
		$license = get_option('frmpro-credentials');
88
		if ( $license && is_array( $license ) && isset( $license['license'] ) ) {
89
			$url = 'http://formidablepro.com/frm-edd-api/licenses?l='. urlencode( base64_encode( $license['license'] ) );
90
			$licenses = self::send_api_request( $url, array( 'name' => 'frm_api_licence', 'expires' => 60 * 60 * 5 ) );
91
			echo json_encode( $licenses );
92
		}
93
		wp_die();
94
	}
95
96
	private static function send_api_request( $url, $transient = array() ) {
97
		$data = get_transient( $transient['name'] );
98
		if ( $data !== false ) {
99
			return $data;
100
		}
101
102
		$arg_array = array(
103
			'body'      => array(
104
				'url'   => home_url(),
105
			),
106
			'timeout'   => 15,
107
			'sslverify' => false,
108
			'user-agent' => 'Formidable/' . FrmAppHelper::$plug_version . '; ' . home_url(),
109
		);
110
111
		$response = wp_remote_post( $url, $arg_array );
112
		$body = wp_remote_retrieve_body( $response );
113
		$data = false;
114
		if ( ! is_wp_error( $response ) && ! is_wp_error( $body ) ) {
115
			$data = json_decode( $body, true );
116
			set_transient( $transient['name'], $data, $transient['expires'] );
117
		}
118
119
		return $data;
120
	}
121
122
	public static function upgrade_to_pro() {
123
		$addons = self::get_ordered_addons();
124
		$pro = $addons[0];
125
		$price_id = 0;
126
127
		include( FrmAppHelper::plugin_path() . '/classes/views/addons/upgrade_to_pro.php' );
128
	}
129
}
130