Passed
Push — master ( 29aa47...a9ceae )
by Chris
09:18
created

monsterinsights_get_all_addons_data()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 36
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 20
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 36
rs 9.6
1
<?php
2
/**
3
 * Addons class.
4
 *
5
 * @since 6.0.0
6
 *
7
 * @package MonsterInsights
8
 * @author  Chris Christoff
9
 */
10
11
// Exit if accessed directly
12
if ( ! defined( 'ABSPATH' ) ) {
13
    exit;
14
}
15
16
17
/**
18
 * Callback to output the MonsterInsights addons page.
19
 *
20
 * @since 6.0.0
21
 */
22
function monsterinsights_addons_page() {
23
    echo monsterinsights_ublock_notice();
24
    monsterinsights_settings_error_page( 'monsterinsights-addons' );
25
    monsterinsights_settings_inline_js();
26
}
27
28
/**
29
 * Retrieves addons from the stored transient or remote server.
30
 *
31
 * @since 6.0.0
32
 *
33
 * @return bool | array    false | Array of licensed and unlicensed Addons.
34
 */
35
function monsterinsights_get_addons() {
36
37
    // Get license key and type.
38
	$key = '';
39
	$type = 'lite';
40
	if ( monsterinsights_is_pro_version() ) {
41
		$key  = is_network_admin() ? MonsterInsights()->license->get_network_license_key() : MonsterInsights()->license->get_site_license_key();
0 ignored issues
show
Bug Best Practice introduced by
The property $license is declared protected in MonsterInsights_Lite. Since you implement __get, consider adding a @property or @property-read.
Loading history...
42
		$type = is_network_admin() ? MonsterInsights()->license->get_network_license_type() : MonsterInsights()->license->get_site_license_type();
43
	}
44
45
    // Get addons data from transient or perform API query if no transient.
46
    if ( false === ( $addons = get_transient( '_monsterinsights_addons' ) ) ) {
47
        $addons = monsterinsights_get_addons_data( $key );
48
    }
49
50
    // If no Addons exist, return false
51
    if ( ! $addons ) {
52
        return false;
53
    }
54
55
    // Iterate through Addons, to build two arrays:
56
    // - Addons the user is licensed to use,
57
    // - Addons the user isn't licensed to use.
58
    $results = array(
59
        'licensed'  => array(),
60
        'unlicensed'=> array(),
61
    );
62
    foreach ( (array) $addons as $i => $addon ) {
63
64
        // Determine whether the user is licensed to use this Addon or not.
65
        if (
66
            empty( $type ) ||
67
            ( in_array( 'Pro', $addon->categories ) && ( $type != 'pro' && $type != 'master' ) ) ||
68
            ( in_array( 'Plus', $addon->categories ) && $type != 'plus' && $type != 'pro' && $type != 'master' ) ||
69
            ( in_array( 'Basic', $addon->categories ) && ( $type != 'basic' && $type != 'plus' && $type != 'pro' && $type != 'master' ) )
70
        ) {
71
            // Unlicensed
72
            $results['unlicensed'][] = $addon;
73
            continue;
74
        }
75
76
        // Licensed
77
        $results['licensed'][] = $addon;
78
79
    }
80
81
    // Return Addons, split by licensed and unlicensed.
82
    return $results;
83
84
}
85
86
/**
87
 * Pings the remote server for addons data.
88
 *
89
 * @since 6.0.0
90
 *
91
 * @param   string      $key    The user license key.
92
 * @return  array               Array of addon data otherwise.
93
 */
94
function monsterinsights_get_addons_data( $key ) {
95
    // Get Addons
96
    // If the key is valid, we'll get personalised upgrade URLs for each Addon (if necessary) and plugin update information.
97
    if ( monsterinsights_is_pro_version() && $key ) {
98
        $addons = MonsterInsights()->license_actions->perform_remote_request( 'get-addons-data-v600', array( 'tgm-updater-key' => $key ) );
0 ignored issues
show
Bug Best Practice introduced by
The property license_actions does not exist on MonsterInsights_Lite. Since you implemented __get, consider adding a @property annotation.
Loading history...
99
    } else {
100
        $addons = monsterinsights_get_all_addons_data();
101
    }
102
103
    // If there was an API error, set transient for only 10 minutes.
104
    if ( ! $addons ) {
105
        set_transient( '_monsterinsights_addons', false, 10 * MINUTE_IN_SECONDS );
106
        return false;
107
    }
108
109
    // If there was an error retrieving the addons, set the error.
110
    if ( isset( $addons->error ) ) {
111
        set_transient( '_monsterinsights_addons', false, 10 * MINUTE_IN_SECONDS );
112
        return false;
113
    }
114
115
    // Otherwise, our request worked. Save the data and return it.
116
    set_transient( '_monsterinsights_addons', $addons, 4 * HOUR_IN_SECONDS );
117
    return $addons;
118
119
}
120
121
/**
122
 * Get all addons without a license, for lite users.
123
 *
124
 * @return array|bool|mixed|object
125
 */
126
function monsterinsights_get_all_addons_data() {
127
	// Build the body of the request.
128
	$body = array(
129
		'tgm-updater-action'     => 'get-all-addons-data',
130
		'tgm-updater-key'        => '',
131
		'tgm-updater-wp-version' => get_bloginfo( 'version' ),
132
		'tgm-updater-referer'    => site_url(),
133
		'tgm-updater-mi-version' => MONSTERINSIGHTS_VERSION,
134
		'tgm-updater-is-pro'     => false,
135
	);
136
	$body = http_build_query( $body, '', '&' );
137
138
	// Build the headers of the request.
139
	$headers = array(
140
		'Content-Type'   => 'application/x-www-form-urlencoded',
141
		'Content-Length' => strlen( $body ),
142
	);
143
144
	// Setup variable for wp_remote_post.
145
	$post = array(
146
		'headers' => $headers,
147
		'body'    => $body,
148
	);
149
150
	// Perform the query and retrieve the response.
151
	$response      = wp_remote_post( monsterinsights_get_licensing_url(), $post );
152
	$response_code = wp_remote_retrieve_response_code( $response );
0 ignored issues
show
Bug introduced by
It seems like $response can also be of type WP_Error; however, parameter $response of wp_remote_retrieve_response_code() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

152
	$response_code = wp_remote_retrieve_response_code( /** @scrutinizer ignore-type */ $response );
Loading history...
153
	$response_body = wp_remote_retrieve_body( $response );
0 ignored issues
show
Bug introduced by
It seems like $response can also be of type WP_Error; however, parameter $response of wp_remote_retrieve_body() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

153
	$response_body = wp_remote_retrieve_body( /** @scrutinizer ignore-type */ $response );
Loading history...
154
155
	// Bail out early if there are any errors.
156
	if ( 200 !== $response_code || is_wp_error( $response_body ) ) {
157
		return false;
158
	}
159
160
	// Return the json decoded content.
161
	return json_decode( $response_body );
162
}
163
164
/**
165
 * Retrieve the plugin basename from the plugin slug.
166
 *
167
 * @since 6.0.0
168
 *
169
 * @param string $slug The plugin slug.
170
 * @return string      The plugin basename if found, else the plugin slug.
171
 */
172
function monsterinsights_get_plugin_basename_from_slug( $slug ) {
173
    $keys = array_keys( get_plugins() );
174
175
    foreach ( $keys as $key ) {
176
        if ( preg_match( '|^' . $slug . '|', $key ) ) {
177
            return $key;
178
        }
179
    }
180
181
    return $slug;
182
183
}
184