Completed
Push — master ( 707409...ea219b )
by Stephanie
10:05
created

FrmAddonsController::get_ordered_addons()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 30
Code Lines 21

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 30
rs 8.439
cc 5
eloc 21
nc 12
nop 1
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
9
	public static function list_addons() {
10
		$installed_addons = apply_filters( 'frm_installed_addons', array() );
11
12
		$pro_link = 'http://formidablepro.com/pricing';
13
		$addons = self::get_ordered_addons( $pro_link );
14
15
		$plugin_names = array(
16
			'formidable-pro' => 'formidable/pro', 'wp-multilingual' => 'formidable-wpml',
17
			'registration-lite' => 'formidable-registration', 'bootstrap-modal' => 'formidable-modal',
18
			'paypal-standard' => 'formidable-paypal', 'formidable-api' => 'formidable-api',
19
		);
20
21
		include( FrmAppHelper::plugin_path() . '/classes/views/addons/list.php' );
22
	}
23
24
	private static function get_ordered_addons( $pro_link ) {
25
		$addons = self::get_api_addons();
26
		if ( ! is_array( $addons ) ) {
27
			$addons = array(
28
				'info' => array( 'link' => $pro_link, 'name' => 'Formidable Pro', 'slug' => 'formidable_pro' ),
29
			);
30
		} else {
31
			$addons = $addons['products'];
32
		}
33
		$addons = array_reverse( $addons );
34
35
		$keyed_addons = array();
36
		foreach ( $addons as $addon ) {
37
			$keyed_addons[ $addon['info']['slug'] ] = $addon;
38
		}
39
40
		$plugin_order = array(
41
			'formidable-pro', 'mailchimp', 'registration-lite',
42
			'paypal-standard', 'bootstrap-modal', 'math-captcha'
0 ignored issues
show
introduced by
Comma required after last value in array declaration
Loading history...
43
		);
44
		$ordered_addons = array();
45
		foreach ( $plugin_order as $plugin ) {
46
			if ( isset( $keyed_addons[ $plugin ] ) ) {
47
				$ordered_addons[] = $keyed_addons[ $plugin ];
48
				unset( $keyed_addons[ $plugin ] );
49
			}
50
		}
51
		$addons = $ordered_addons + $keyed_addons;
52
		return $addons;
53
	}
54
55
	public static function license_settings() {
56
		$plugins = apply_filters( 'frm_installed_addons', array() );
57
		if ( empty( $plugins ) ) {
58
			_e( 'There are no plugins on your site that require a license', 'formidable' );
59
			return;
60
		}
61
62
		include( FrmAppHelper::plugin_path() . '/classes/views/addons/settings.php' );
63
	}
64
65
	private static function get_api_addons() {
66
		$addons = get_transient( 'frm_api_addons' );
67
		if ( $addons !== false ) {
68
			return $addons;
69
		}
70
71
		$url = 'https://formidablepro.com/edd-api/products?number=40';
72
73
		// check every 5 days
74
		$addons = self::send_api_request( $url, array( 'name' => 'frm_api_addons', 'expires' => 60 * 60 * 24 * 5 ) );
75
		if ( is_array( $addons ) ) {
76
			return $addons;
77
		}
78
79
		return false;
80
	}
81
82
	public static function get_licenses() {
83
		$license = get_option('frmpro-credentials');
84
		if ( $license && is_array( $license ) && isset( $license['license'] ) ) {
85
			$url = 'http://formidablepro.com/frm-edd-api/licenses?l='. urlencode( base64_encode( $license['license'] ) );
86
			$licenses = self::send_api_request( $url, array( 'name' => 'frm_api_licence', 'expires' => 60 * 60 * 5 ) );
87
			echo json_encode( $licenses );
88
		}
89
		wp_die();
90
	}
91
92
	private static function send_api_request( $url, $transient = array() ) {
93
		$data = get_transient( $transient['name'] );
94
		if ( $data !== false ) {
95
			return $data;
96
		}
97
98
		$arg_array = array(
99
			'body'      => array(
100
				'url'   => home_url(),
101
			),
102
			'timeout'   => 15,
103
			'sslverify' => false,
104
			'user-agent' => 'Formidable/' . FrmAppHelper::$plug_version . '; ' . home_url(),
105
		);
106
107
		$response = wp_remote_post( $url, $arg_array );
108
		$body = wp_remote_retrieve_body( $response );
109
		$data = false;
110
		if ( ! is_wp_error( $response ) && ! is_wp_error( $body ) ) {
111
			$data = json_decode( $body, true );
112
			set_transient( $transient['name'], $data, $transient['expires'] );
113
		}
114
115
		return $data;
116
	}
117
}
118