Completed
Push — master ( c833ab...260717 )
by Stephanie
03:44
created

FrmAddonsController::get_api_addons()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 16
rs 9.4286
cc 3
eloc 9
nc 3
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
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_api_addons();
14
		if ( ! is_array( $addons ) ) {
15
			$addons = array(
16
				array( 'url' => $pro_link, 'name' => 'Formidable Pro', 'slug' => 'formidable_pro' ),
17
			);
18
		} else {
19
			$addons = $addons['products'];
20
		}
21
		$addons = array_reverse( $addons );
22
23
		$plugin_names = array(
24
			'formidable-pro' => 'formidable/pro', 'wp-multilingual' => 'formidable-wpml',
25
			'registration-lite' => 'formidable-registration', 'bootstrap-modal' => 'formidable-modal',
26
			'paypal-standard' => 'formidable-paypal', 'formidable-api' => 'formidable-api',
27
		);
28
29
		include( FrmAppHelper::plugin_path() . '/classes/views/addons/list.php' );
30
	}
31
32
	public static function license_settings() {
33
		$plugins = apply_filters( 'frm_installed_addons', array() );
34
		if ( empty( $plugins ) ) {
35
			_e( 'There are no plugins on your site that require a license', 'formidable' );
36
			return;
37
		}
38
39
		include( FrmAppHelper::plugin_path() . '/classes/views/addons/settings.php' );
40
	}
41
42
	private static function get_api_addons() {
43
		$addons = get_transient( 'frm_api_addons' );
44
		if ( $addons !== false ) {
45
			return $addons;
46
		}
47
48
		$url = 'https://formidablepro.com/edd-api/products?number=40';
49
50
		// check every 5 days
51
		$addons = self::send_api_request( $url, array( 'name' => 'frm_api_addons', 'expires' => 60 * 60 * 24 * 5 ) );
52
		if ( is_array( $addons ) ) {
53
			return $addons;
54
		}
55
56
		return false;
57
	}
58
59
	public static function get_licenses() {
60
		$license = get_option('frmpro-credentials');
61
		if ( $license && is_array( $license ) && isset( $license['license'] ) ) {
62
			$url = 'http://formidablepro.com/frm-edd-api/licenses?l='. urlencode( base64_encode( $license['license'] ) );
63
			$licenses = self::send_api_request( $url, array( 'name' => 'frm_api_licence', 'expires' => 60 * 60 * 5 ) );
64
			echo json_encode( $licenses );
65
		}
66
		wp_die();
67
	}
68
69
	private static function send_api_request( $url, $transient = array() ) {
70
		$data = get_transient( $transient['name'] );
71
		if ( $data !== false ) {
1 ignored issue
show
introduced by
Found "!== false". Use Yoda Condition checks, you must
Loading history...
72
			return $data;
73
		}
74
75
		$arg_array = array(
76
			'body'      => array(
77
				'url'   => home_url(),
78
			),
79
			'timeout'   => 15,
80
			'sslverify' => false,
81
			'user-agent' => 'Formidable/' . FrmAppHelper::$plug_version . '; ' . home_url(),
82
		);
83
84
		$response = wp_remote_post( $url, $arg_array );
85
		$body = wp_remote_retrieve_body( $response );
86
		if ( ! is_wp_error( $response ) && ! is_wp_error( $body ) ) {
87
			$data = json_decode( $body, true );
88
			set_transient( $transient['name'], $data, $transient['expires'] );
89
		}
90
91
		return false;
92
	}
93
}
94